Implement -mmemcpy-strategy= and -mmemset-strategy= options
[official-gcc.git] / gcc / ada / sem_res.adb
blob61f7712a0902af307e826d029c5e4f4ef2faabef
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-2013, 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_Entity_Name (N : Node_Id; Typ : Entity_Id);
182 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
183 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
184 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
185 procedure Resolve_If_Expression (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 -- Versions with 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 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
338 begin
339 Scope_Suppress.Suppress := (others => True);
340 Analyze_And_Resolve (N, Typ);
341 Scope_Suppress.Suppress := Sva;
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 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
378 begin
379 Scope_Suppress.Suppress := (others => True);
380 Analyze_And_Resolve (N);
381 Scope_Suppress.Suppress := Sva;
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_If_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 Nam_In (Attribute_Name (Parent (N)), Name_Address,
1009 Name_Code_Address,
1010 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 this is a package renaming, get renamed entity, which will be
1355 -- the scope of the operands if operaton is type-correct.
1357 if Present (Renamed_Entity (Pack)) then
1358 Pack := Renamed_Entity (Pack);
1359 end if;
1361 -- If the entity being called is defined in the given package, it is
1362 -- a renaming of a predefined operator, and known to be legal.
1364 if Scope (Entity (Name (N))) = Pack
1365 and then Pack /= Standard_Standard
1366 then
1367 null;
1369 -- Visibility does not need to be checked in an instance: if the
1370 -- operator was not visible in the generic it has been diagnosed
1371 -- already, else there is an implicit copy of it in the instance.
1373 elsif In_Instance then
1374 null;
1376 elsif Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide)
1377 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1378 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1379 then
1380 if Pack /= Standard_Standard then
1381 Error := True;
1382 end if;
1384 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1385 -- available.
1387 elsif Ada_Version >= Ada_2005
1388 and then Nam_In (Op_Name, Name_Op_Eq, Name_Op_Ne)
1389 and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1390 then
1391 null;
1393 else
1394 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1396 if Op_Name = Name_Op_Concat then
1397 Opnd_Type := Base_Type (Typ);
1399 elsif (Scope (Opnd_Type) = Standard_Standard
1400 and then Is_Binary)
1401 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1402 and then Is_Binary
1403 and then not Comes_From_Source (Opnd_Type))
1404 then
1405 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1406 end if;
1408 if Scope (Opnd_Type) = Standard_Standard then
1410 -- Verify that the scope contains a type that corresponds to
1411 -- the given literal. Optimize the case where Pack is Standard.
1413 if Pack /= Standard_Standard then
1415 if Opnd_Type = Universal_Integer then
1416 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1418 elsif Opnd_Type = Universal_Real then
1419 Orig_Type := Type_In_P (Is_Real_Type'Access);
1421 elsif Opnd_Type = Any_String then
1422 Orig_Type := Type_In_P (Is_String_Type'Access);
1424 elsif Opnd_Type = Any_Access then
1425 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1427 elsif Opnd_Type = Any_Composite then
1428 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1430 if Present (Orig_Type) then
1431 if Has_Private_Component (Orig_Type) then
1432 Orig_Type := Empty;
1433 else
1434 Set_Etype (Act1, Orig_Type);
1436 if Is_Binary then
1437 Set_Etype (Act2, Orig_Type);
1438 end if;
1439 end if;
1440 end if;
1442 else
1443 Orig_Type := Empty;
1444 end if;
1446 Error := No (Orig_Type);
1447 end if;
1449 elsif Ekind (Opnd_Type) = E_Allocator_Type
1450 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1451 then
1452 Error := True;
1454 -- If the type is defined elsewhere, and the operator is not
1455 -- defined in the given scope (by a renaming declaration, e.g.)
1456 -- then this is an error as well. If an extension of System is
1457 -- present, and the type may be defined there, Pack must be
1458 -- System itself.
1460 elsif Scope (Opnd_Type) /= Pack
1461 and then Scope (Op_Id) /= Pack
1462 and then (No (System_Aux_Id)
1463 or else Scope (Opnd_Type) /= System_Aux_Id
1464 or else Pack /= Scope (System_Aux_Id))
1465 then
1466 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1467 Error := True;
1468 else
1469 Error := not Operand_Type_In_Scope (Pack);
1470 end if;
1472 elsif Pack = Standard_Standard
1473 and then not Operand_Type_In_Scope (Standard_Standard)
1474 then
1475 Error := True;
1476 end if;
1477 end if;
1479 if Error then
1480 Error_Msg_Node_2 := Pack;
1481 Error_Msg_NE
1482 ("& not declared in&", N, Selector_Name (Name (N)));
1483 Set_Etype (N, Any_Type);
1484 return;
1486 -- Detect a mismatch between the context type and the result type
1487 -- in the named package, which is otherwise not detected if the
1488 -- operands are universal. Check is only needed if source entity is
1489 -- an operator, not a function that renames an operator.
1491 elsif Nkind (Parent (N)) /= N_Type_Conversion
1492 and then Ekind (Entity (Name (N))) = E_Operator
1493 and then Is_Numeric_Type (Typ)
1494 and then not Is_Universal_Numeric_Type (Typ)
1495 and then Scope (Base_Type (Typ)) /= Pack
1496 and then not In_Instance
1497 then
1498 if Is_Fixed_Point_Type (Typ)
1499 and then Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide)
1500 then
1501 -- Already checked above
1503 null;
1505 -- Operator may be defined in an extension of System
1507 elsif Present (System_Aux_Id)
1508 and then Scope (Opnd_Type) = System_Aux_Id
1509 then
1510 null;
1512 else
1513 -- Could we use Wrong_Type here??? (this would require setting
1514 -- Etype (N) to the actual type found where Typ was expected).
1516 Error_Msg_NE ("expect }", N, Typ);
1517 end if;
1518 end if;
1519 end if;
1521 Set_Chars (Op_Node, Op_Name);
1523 if not Is_Private_Type (Etype (N)) then
1524 Set_Etype (Op_Node, Base_Type (Etype (N)));
1525 else
1526 Set_Etype (Op_Node, Etype (N));
1527 end if;
1529 -- If this is a call to a function that renames a predefined equality,
1530 -- the renaming declaration provides a type that must be used to
1531 -- resolve the operands. This must be done now because resolution of
1532 -- the equality node will not resolve any remaining ambiguity, and it
1533 -- assumes that the first operand is not overloaded.
1535 if Nam_In (Op_Name, Name_Op_Eq, Name_Op_Ne)
1536 and then Ekind (Func) = E_Function
1537 and then Is_Overloaded (Act1)
1538 then
1539 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1540 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1541 end if;
1543 Set_Entity (Op_Node, Op_Id);
1544 Generate_Reference (Op_Id, N, ' ');
1546 -- Do rewrite setting Comes_From_Source on the result if the original
1547 -- call came from source. Although it is not strictly the case that the
1548 -- operator as such comes from the source, logically it corresponds
1549 -- exactly to the function call in the source, so it should be marked
1550 -- this way (e.g. to make sure that validity checks work fine).
1552 declare
1553 CS : constant Boolean := Comes_From_Source (N);
1554 begin
1555 Rewrite (N, Op_Node);
1556 Set_Comes_From_Source (N, CS);
1557 end;
1559 -- If this is an arithmetic operator and the result type is private,
1560 -- the operands and the result must be wrapped in conversion to
1561 -- expose the underlying numeric type and expand the proper checks,
1562 -- e.g. on division.
1564 if Is_Private_Type (Typ) then
1565 case Nkind (N) is
1566 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1567 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
1568 Resolve_Intrinsic_Operator (N, Typ);
1570 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
1571 Resolve_Intrinsic_Unary_Operator (N, Typ);
1573 when others =>
1574 Resolve (N, Typ);
1575 end case;
1576 else
1577 Resolve (N, Typ);
1578 end if;
1580 -- If in ASIS_Mode, propagate operand types to original actuals of
1581 -- function call, which would otherwise not be fully resolved. If
1582 -- the call has already been constant-folded, nothing to do.
1584 if ASIS_Mode and then Nkind (N) in N_Op then
1585 if Is_Binary then
1586 Set_Parameter_Associations
1587 (Original_Node (N),
1588 New_List (New_Copy_Tree (Left_Opnd (N)),
1589 New_Copy_Tree (Right_Opnd (N))));
1590 else
1591 Set_Parameter_Associations
1592 (Original_Node (N),
1593 New_List (New_Copy_Tree (Right_Opnd (N))));
1594 end if;
1595 end if;
1596 end Make_Call_Into_Operator;
1598 -------------------
1599 -- Operator_Kind --
1600 -------------------
1602 function Operator_Kind
1603 (Op_Name : Name_Id;
1604 Is_Binary : Boolean) return Node_Kind
1606 Kind : Node_Kind;
1608 begin
1609 -- Use CASE statement or array???
1611 if Is_Binary then
1612 if Op_Name = Name_Op_And then
1613 Kind := N_Op_And;
1614 elsif Op_Name = Name_Op_Or then
1615 Kind := N_Op_Or;
1616 elsif Op_Name = Name_Op_Xor then
1617 Kind := N_Op_Xor;
1618 elsif Op_Name = Name_Op_Eq then
1619 Kind := N_Op_Eq;
1620 elsif Op_Name = Name_Op_Ne then
1621 Kind := N_Op_Ne;
1622 elsif Op_Name = Name_Op_Lt then
1623 Kind := N_Op_Lt;
1624 elsif Op_Name = Name_Op_Le then
1625 Kind := N_Op_Le;
1626 elsif Op_Name = Name_Op_Gt then
1627 Kind := N_Op_Gt;
1628 elsif Op_Name = Name_Op_Ge then
1629 Kind := N_Op_Ge;
1630 elsif Op_Name = Name_Op_Add then
1631 Kind := N_Op_Add;
1632 elsif Op_Name = Name_Op_Subtract then
1633 Kind := N_Op_Subtract;
1634 elsif Op_Name = Name_Op_Concat then
1635 Kind := N_Op_Concat;
1636 elsif Op_Name = Name_Op_Multiply then
1637 Kind := N_Op_Multiply;
1638 elsif Op_Name = Name_Op_Divide then
1639 Kind := N_Op_Divide;
1640 elsif Op_Name = Name_Op_Mod then
1641 Kind := N_Op_Mod;
1642 elsif Op_Name = Name_Op_Rem then
1643 Kind := N_Op_Rem;
1644 elsif Op_Name = Name_Op_Expon then
1645 Kind := N_Op_Expon;
1646 else
1647 raise Program_Error;
1648 end if;
1650 -- Unary operators
1652 else
1653 if Op_Name = Name_Op_Add then
1654 Kind := N_Op_Plus;
1655 elsif Op_Name = Name_Op_Subtract then
1656 Kind := N_Op_Minus;
1657 elsif Op_Name = Name_Op_Abs then
1658 Kind := N_Op_Abs;
1659 elsif Op_Name = Name_Op_Not then
1660 Kind := N_Op_Not;
1661 else
1662 raise Program_Error;
1663 end if;
1664 end if;
1666 return Kind;
1667 end Operator_Kind;
1669 ----------------------------
1670 -- Preanalyze_And_Resolve --
1671 ----------------------------
1673 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1674 Save_Full_Analysis : constant Boolean := Full_Analysis;
1676 begin
1677 Full_Analysis := False;
1678 Expander_Mode_Save_And_Set (False);
1680 -- Normally, we suppress all checks for this preanalysis. There is no
1681 -- point in processing them now, since they will be applied properly
1682 -- and in the proper location when the default expressions reanalyzed
1683 -- and reexpanded later on. We will also have more information at that
1684 -- point for possible suppression of individual checks.
1686 -- However, in SPARK mode, most expansion is suppressed, and this
1687 -- later reanalysis and reexpansion may not occur. SPARK mode does
1688 -- require the setting of checking flags for proof purposes, so we
1689 -- do the SPARK preanalysis without suppressing checks.
1691 -- This special handling for SPARK mode is required for example in the
1692 -- case of Ada 2012 constructs such as quantified expressions, which are
1693 -- expanded in two separate steps.
1695 if SPARK_Mode then
1696 Analyze_And_Resolve (N, T);
1697 else
1698 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1699 end if;
1701 Expander_Mode_Restore;
1702 Full_Analysis := Save_Full_Analysis;
1703 end Preanalyze_And_Resolve;
1705 -- Version without context type
1707 procedure Preanalyze_And_Resolve (N : Node_Id) is
1708 Save_Full_Analysis : constant Boolean := Full_Analysis;
1710 begin
1711 Full_Analysis := False;
1712 Expander_Mode_Save_And_Set (False);
1714 Analyze (N);
1715 Resolve (N, Etype (N), Suppress => All_Checks);
1717 Expander_Mode_Restore;
1718 Full_Analysis := Save_Full_Analysis;
1719 end Preanalyze_And_Resolve;
1721 ----------------------------------
1722 -- Replace_Actual_Discriminants --
1723 ----------------------------------
1725 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1726 Loc : constant Source_Ptr := Sloc (N);
1727 Tsk : Node_Id := Empty;
1729 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1730 -- Comment needed???
1732 -------------------
1733 -- Process_Discr --
1734 -------------------
1736 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1737 Ent : Entity_Id;
1739 begin
1740 if Nkind (Nod) = N_Identifier then
1741 Ent := Entity (Nod);
1743 if Present (Ent)
1744 and then Ekind (Ent) = E_Discriminant
1745 then
1746 Rewrite (Nod,
1747 Make_Selected_Component (Loc,
1748 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1749 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1751 Set_Etype (Nod, Etype (Ent));
1752 end if;
1754 end if;
1756 return OK;
1757 end Process_Discr;
1759 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1761 -- Start of processing for Replace_Actual_Discriminants
1763 begin
1764 if not Full_Expander_Active then
1765 return;
1766 end if;
1768 if Nkind (Name (N)) = N_Selected_Component then
1769 Tsk := Prefix (Name (N));
1771 elsif Nkind (Name (N)) = N_Indexed_Component then
1772 Tsk := Prefix (Prefix (Name (N)));
1773 end if;
1775 if No (Tsk) then
1776 return;
1777 else
1778 Replace_Discrs (Default);
1779 end if;
1780 end Replace_Actual_Discriminants;
1782 -------------
1783 -- Resolve --
1784 -------------
1786 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1787 Ambiguous : Boolean := False;
1788 Ctx_Type : Entity_Id := Typ;
1789 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1790 Err_Type : Entity_Id := Empty;
1791 Found : Boolean := False;
1792 From_Lib : Boolean;
1793 I : Interp_Index;
1794 I1 : Interp_Index := 0; -- prevent junk warning
1795 It : Interp;
1796 It1 : Interp;
1797 Seen : Entity_Id := Empty; -- prevent junk warning
1799 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1800 -- Determine whether a node comes from a predefined library unit or
1801 -- Standard.
1803 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1804 -- Try and fix up a literal so that it matches its expected type. New
1805 -- literals are manufactured if necessary to avoid cascaded errors.
1807 function Proper_Current_Scope return Entity_Id;
1808 -- Return the current scope. Skip loop scopes created for the purpose of
1809 -- quantified expression analysis since those do not appear in the tree.
1811 procedure Report_Ambiguous_Argument;
1812 -- Additional diagnostics when an ambiguous call has an ambiguous
1813 -- argument (typically a controlling actual).
1815 procedure Resolution_Failed;
1816 -- Called when attempt at resolving current expression fails
1818 ------------------------------------
1819 -- Comes_From_Predefined_Lib_Unit --
1820 -------------------------------------
1822 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1823 begin
1824 return
1825 Sloc (Nod) = Standard_Location
1826 or else Is_Predefined_File_Name
1827 (Unit_File_Name (Get_Source_Unit (Sloc (Nod))));
1828 end Comes_From_Predefined_Lib_Unit;
1830 --------------------
1831 -- Patch_Up_Value --
1832 --------------------
1834 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1835 begin
1836 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
1837 Rewrite (N,
1838 Make_Real_Literal (Sloc (N),
1839 Realval => UR_From_Uint (Intval (N))));
1840 Set_Etype (N, Universal_Real);
1841 Set_Is_Static_Expression (N);
1843 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
1844 Rewrite (N,
1845 Make_Integer_Literal (Sloc (N),
1846 Intval => UR_To_Uint (Realval (N))));
1847 Set_Etype (N, Universal_Integer);
1848 Set_Is_Static_Expression (N);
1850 elsif Nkind (N) = N_String_Literal
1851 and then Is_Character_Type (Typ)
1852 then
1853 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1854 Rewrite (N,
1855 Make_Character_Literal (Sloc (N),
1856 Chars => Name_Find,
1857 Char_Literal_Value =>
1858 UI_From_Int (Character'Pos ('A'))));
1859 Set_Etype (N, Any_Character);
1860 Set_Is_Static_Expression (N);
1862 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
1863 Rewrite (N,
1864 Make_String_Literal (Sloc (N),
1865 Strval => End_String));
1867 elsif Nkind (N) = N_Range then
1868 Patch_Up_Value (Low_Bound (N), Typ);
1869 Patch_Up_Value (High_Bound (N), Typ);
1870 end if;
1871 end Patch_Up_Value;
1873 --------------------------
1874 -- Proper_Current_Scope --
1875 --------------------------
1877 function Proper_Current_Scope return Entity_Id is
1878 S : Entity_Id := Current_Scope;
1880 begin
1881 while Present (S) loop
1883 -- Skip a loop scope created for quantified expression analysis
1885 if Ekind (S) = E_Loop
1886 and then Nkind (Parent (S)) = N_Quantified_Expression
1887 then
1888 S := Scope (S);
1889 else
1890 exit;
1891 end if;
1892 end loop;
1894 return S;
1895 end Proper_Current_Scope;
1897 -------------------------------
1898 -- Report_Ambiguous_Argument --
1899 -------------------------------
1901 procedure Report_Ambiguous_Argument is
1902 Arg : constant Node_Id := First (Parameter_Associations (N));
1903 I : Interp_Index;
1904 It : Interp;
1906 begin
1907 if Nkind (Arg) = N_Function_Call
1908 and then Is_Entity_Name (Name (Arg))
1909 and then Is_Overloaded (Name (Arg))
1910 then
1911 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
1913 -- Could use comments on what is going on here???
1915 Get_First_Interp (Name (Arg), I, It);
1916 while Present (It.Nam) loop
1917 Error_Msg_Sloc := Sloc (It.Nam);
1919 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
1920 Error_Msg_N ("interpretation (inherited) #!", Arg);
1921 else
1922 Error_Msg_N ("interpretation #!", Arg);
1923 end if;
1925 Get_Next_Interp (I, It);
1926 end loop;
1927 end if;
1928 end Report_Ambiguous_Argument;
1930 -----------------------
1931 -- Resolution_Failed --
1932 -----------------------
1934 procedure Resolution_Failed is
1935 begin
1936 Patch_Up_Value (N, Typ);
1937 Set_Etype (N, Typ);
1938 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1939 Set_Is_Overloaded (N, False);
1941 -- The caller will return without calling the expander, so we need
1942 -- to set the analyzed flag. Note that it is fine to set Analyzed
1943 -- to True even if we are in the middle of a shallow analysis,
1944 -- (see the spec of sem for more details) since this is an error
1945 -- situation anyway, and there is no point in repeating the
1946 -- analysis later (indeed it won't work to repeat it later, since
1947 -- we haven't got a clear resolution of which entity is being
1948 -- referenced.)
1950 Set_Analyzed (N, True);
1951 return;
1952 end Resolution_Failed;
1954 -- Start of processing for Resolve
1956 begin
1957 if N = Error then
1958 return;
1959 end if;
1961 -- Access attribute on remote subprogram cannot be used for a non-remote
1962 -- access-to-subprogram type.
1964 if Nkind (N) = N_Attribute_Reference
1965 and then Nam_In (Attribute_Name (N), Name_Access,
1966 Name_Unrestricted_Access,
1967 Name_Unchecked_Access)
1968 and then Comes_From_Source (N)
1969 and then Is_Entity_Name (Prefix (N))
1970 and then Is_Subprogram (Entity (Prefix (N)))
1971 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1972 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1973 then
1974 Error_Msg_N
1975 ("prefix must statically denote a non-remote subprogram", N);
1976 end if;
1978 From_Lib := Comes_From_Predefined_Lib_Unit (N);
1980 -- If the context is a Remote_Access_To_Subprogram, access attributes
1981 -- must be resolved with the corresponding fat pointer. There is no need
1982 -- to check for the attribute name since the return type of an
1983 -- attribute is never a remote type.
1985 if Nkind (N) = N_Attribute_Reference
1986 and then Comes_From_Source (N)
1987 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
1988 then
1989 declare
1990 Attr : constant Attribute_Id :=
1991 Get_Attribute_Id (Attribute_Name (N));
1992 Pref : constant Node_Id := Prefix (N);
1993 Decl : Node_Id;
1994 Spec : Node_Id;
1995 Is_Remote : Boolean := True;
1997 begin
1998 -- Check that Typ is a remote access-to-subprogram type
2000 if Is_Remote_Access_To_Subprogram_Type (Typ) then
2002 -- Prefix (N) must statically denote a remote subprogram
2003 -- declared in a package specification.
2005 if Attr = Attribute_Access or else
2006 Attr = Attribute_Unchecked_Access or else
2007 Attr = Attribute_Unrestricted_Access
2008 then
2009 Decl := Unit_Declaration_Node (Entity (Pref));
2011 if Nkind (Decl) = N_Subprogram_Body then
2012 Spec := Corresponding_Spec (Decl);
2014 if not No (Spec) then
2015 Decl := Unit_Declaration_Node (Spec);
2016 end if;
2017 end if;
2019 Spec := Parent (Decl);
2021 if not Is_Entity_Name (Prefix (N))
2022 or else Nkind (Spec) /= N_Package_Specification
2023 or else
2024 not Is_Remote_Call_Interface (Defining_Entity (Spec))
2025 then
2026 Is_Remote := False;
2027 Error_Msg_N
2028 ("prefix must statically denote a remote subprogram ",
2030 end if;
2032 -- If we are generating code in distributed mode, perform
2033 -- semantic checks against corresponding remote entities.
2035 if Full_Expander_Active
2036 and then Get_PCS_Name /= Name_No_DSA
2037 then
2038 Check_Subtype_Conformant
2039 (New_Id => Entity (Prefix (N)),
2040 Old_Id => Designated_Type
2041 (Corresponding_Remote_Type (Typ)),
2042 Err_Loc => N);
2044 if Is_Remote then
2045 Process_Remote_AST_Attribute (N, Typ);
2046 end if;
2047 end if;
2048 end if;
2049 end if;
2050 end;
2051 end if;
2053 Debug_A_Entry ("resolving ", N);
2055 if Debug_Flag_V then
2056 Write_Overloads (N);
2057 end if;
2059 if Comes_From_Source (N) then
2060 if Is_Fixed_Point_Type (Typ) then
2061 Check_Restriction (No_Fixed_Point, N);
2063 elsif Is_Floating_Point_Type (Typ)
2064 and then Typ /= Universal_Real
2065 and then Typ /= Any_Real
2066 then
2067 Check_Restriction (No_Floating_Point, N);
2068 end if;
2069 end if;
2071 -- Return if already analyzed
2073 if Analyzed (N) then
2074 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2075 Analyze_Dimension (N);
2076 return;
2078 -- A Raise_Expression takes its type from context. The Etype was set
2079 -- to Any_Type, reflecting the fact that the expression itself does
2080 -- not specify any possible interpretation. So we set the type to the
2081 -- resolution type here and now. We need to do this before Resolve sees
2082 -- the Any_Type value.
2084 elsif Nkind (N) = N_Raise_Expression then
2085 Set_Etype (N, Typ);
2087 -- Any other case of Any_Type as the Etype value means that we had
2088 -- a previous error.
2090 elsif Etype (N) = Any_Type then
2091 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2092 return;
2093 end if;
2095 Check_Parameterless_Call (N);
2097 -- If not overloaded, then we know the type, and all that needs doing
2098 -- is to check that this type is compatible with the context.
2100 if not Is_Overloaded (N) then
2101 Found := Covers (Typ, Etype (N));
2102 Expr_Type := Etype (N);
2104 -- In the overloaded case, we must select the interpretation that
2105 -- is compatible with the context (i.e. the type passed to Resolve)
2107 else
2108 -- Loop through possible interpretations
2110 Get_First_Interp (N, I, It);
2111 Interp_Loop : while Present (It.Typ) loop
2113 if Debug_Flag_V then
2114 Write_Str ("Interp: ");
2115 Write_Interp (It);
2116 end if;
2118 -- We are only interested in interpretations that are compatible
2119 -- with the expected type, any other interpretations are ignored.
2121 if not Covers (Typ, It.Typ) then
2122 if Debug_Flag_V then
2123 Write_Str (" interpretation incompatible with context");
2124 Write_Eol;
2125 end if;
2127 else
2128 -- Skip the current interpretation if it is disabled by an
2129 -- abstract operator. This action is performed only when the
2130 -- type against which we are resolving is the same as the
2131 -- type of the interpretation.
2133 if Ada_Version >= Ada_2005
2134 and then It.Typ = Typ
2135 and then Typ /= Universal_Integer
2136 and then Typ /= Universal_Real
2137 and then Present (It.Abstract_Op)
2138 then
2139 if Debug_Flag_V then
2140 Write_Line ("Skip.");
2141 end if;
2143 goto Continue;
2144 end if;
2146 -- First matching interpretation
2148 if not Found then
2149 Found := True;
2150 I1 := I;
2151 Seen := It.Nam;
2152 Expr_Type := It.Typ;
2154 -- Matching interpretation that is not the first, maybe an
2155 -- error, but there are some cases where preference rules are
2156 -- used to choose between the two possibilities. These and
2157 -- some more obscure cases are handled in Disambiguate.
2159 else
2160 -- If the current statement is part of a predefined library
2161 -- unit, then all interpretations which come from user level
2162 -- packages should not be considered.
2164 if From_Lib
2165 and then not Comes_From_Predefined_Lib_Unit (It.Nam)
2166 then
2167 goto Continue;
2168 end if;
2170 Error_Msg_Sloc := Sloc (Seen);
2171 It1 := Disambiguate (N, I1, I, Typ);
2173 -- Disambiguation has succeeded. Skip the remaining
2174 -- interpretations.
2176 if It1 /= No_Interp then
2177 Seen := It1.Nam;
2178 Expr_Type := It1.Typ;
2180 while Present (It.Typ) loop
2181 Get_Next_Interp (I, It);
2182 end loop;
2184 else
2185 -- Before we issue an ambiguity complaint, check for
2186 -- the case of a subprogram call where at least one
2187 -- of the arguments is Any_Type, and if so, suppress
2188 -- the message, since it is a cascaded error.
2190 if Nkind (N) in N_Subprogram_Call then
2191 declare
2192 A : Node_Id;
2193 E : Node_Id;
2195 begin
2196 A := First_Actual (N);
2197 while Present (A) loop
2198 E := A;
2200 if Nkind (E) = N_Parameter_Association then
2201 E := Explicit_Actual_Parameter (E);
2202 end if;
2204 if Etype (E) = Any_Type then
2205 if Debug_Flag_V then
2206 Write_Str ("Any_Type in call");
2207 Write_Eol;
2208 end if;
2210 exit Interp_Loop;
2211 end if;
2213 Next_Actual (A);
2214 end loop;
2215 end;
2217 elsif Nkind (N) in N_Binary_Op
2218 and then (Etype (Left_Opnd (N)) = Any_Type
2219 or else Etype (Right_Opnd (N)) = Any_Type)
2220 then
2221 exit Interp_Loop;
2223 elsif Nkind (N) in N_Unary_Op
2224 and then Etype (Right_Opnd (N)) = Any_Type
2225 then
2226 exit Interp_Loop;
2227 end if;
2229 -- Not that special case, so issue message using the
2230 -- flag Ambiguous to control printing of the header
2231 -- message only at the start of an ambiguous set.
2233 if not Ambiguous then
2234 if Nkind (N) = N_Function_Call
2235 and then Nkind (Name (N)) = N_Explicit_Dereference
2236 then
2237 Error_Msg_N
2238 ("ambiguous expression "
2239 & "(cannot resolve indirect call)!", N);
2240 else
2241 Error_Msg_NE -- CODEFIX
2242 ("ambiguous expression (cannot resolve&)!",
2243 N, It.Nam);
2244 end if;
2246 Ambiguous := True;
2248 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2249 Error_Msg_N
2250 ("\\possible interpretation (inherited)#!", N);
2251 else
2252 Error_Msg_N -- CODEFIX
2253 ("\\possible interpretation#!", N);
2254 end if;
2256 if Nkind (N) in N_Subprogram_Call
2257 and then Present (Parameter_Associations (N))
2258 then
2259 Report_Ambiguous_Argument;
2260 end if;
2261 end if;
2263 Error_Msg_Sloc := Sloc (It.Nam);
2265 -- By default, the error message refers to the candidate
2266 -- interpretation. But if it is a predefined operator, it
2267 -- is implicitly declared at the declaration of the type
2268 -- of the operand. Recover the sloc of that declaration
2269 -- for the error message.
2271 if Nkind (N) in N_Op
2272 and then Scope (It.Nam) = Standard_Standard
2273 and then not Is_Overloaded (Right_Opnd (N))
2274 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2275 Standard_Standard
2276 then
2277 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2279 if Comes_From_Source (Err_Type)
2280 and then Present (Parent (Err_Type))
2281 then
2282 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2283 end if;
2285 elsif Nkind (N) in N_Binary_Op
2286 and then Scope (It.Nam) = Standard_Standard
2287 and then not Is_Overloaded (Left_Opnd (N))
2288 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2289 Standard_Standard
2290 then
2291 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2293 if Comes_From_Source (Err_Type)
2294 and then Present (Parent (Err_Type))
2295 then
2296 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2297 end if;
2299 -- If this is an indirect call, use the subprogram_type
2300 -- in the message, to have a meaningful location. Also
2301 -- indicate if this is an inherited operation, created
2302 -- by a type declaration.
2304 elsif Nkind (N) = N_Function_Call
2305 and then Nkind (Name (N)) = N_Explicit_Dereference
2306 and then Is_Type (It.Nam)
2307 then
2308 Err_Type := It.Nam;
2309 Error_Msg_Sloc :=
2310 Sloc (Associated_Node_For_Itype (Err_Type));
2311 else
2312 Err_Type := Empty;
2313 end if;
2315 if Nkind (N) in N_Op
2316 and then Scope (It.Nam) = Standard_Standard
2317 and then Present (Err_Type)
2318 then
2319 -- Special-case the message for universal_fixed
2320 -- operators, which are not declared with the type
2321 -- of the operand, but appear forever in Standard.
2323 if It.Typ = Universal_Fixed
2324 and then Scope (It.Nam) = Standard_Standard
2325 then
2326 Error_Msg_N
2327 ("\\possible interpretation as universal_fixed "
2328 & "operation (RM 4.5.5 (19))", N);
2329 else
2330 Error_Msg_N
2331 ("\\possible interpretation (predefined)#!", N);
2332 end if;
2334 elsif
2335 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2336 then
2337 Error_Msg_N
2338 ("\\possible interpretation (inherited)#!", N);
2339 else
2340 Error_Msg_N -- CODEFIX
2341 ("\\possible interpretation#!", N);
2342 end if;
2344 end if;
2345 end if;
2347 -- We have a matching interpretation, Expr_Type is the type
2348 -- from this interpretation, and Seen is the entity.
2350 -- For an operator, just set the entity name. The type will be
2351 -- set by the specific operator resolution routine.
2353 if Nkind (N) in N_Op then
2354 Set_Entity (N, Seen);
2355 Generate_Reference (Seen, N);
2357 elsif Nkind (N) = N_Case_Expression then
2358 Set_Etype (N, Expr_Type);
2360 elsif Nkind (N) = N_Character_Literal then
2361 Set_Etype (N, Expr_Type);
2363 elsif Nkind (N) = N_If_Expression then
2364 Set_Etype (N, Expr_Type);
2366 -- AI05-0139-2: Expression is overloaded because type has
2367 -- implicit dereference. If type matches context, no implicit
2368 -- dereference is involved.
2370 elsif Has_Implicit_Dereference (Expr_Type) then
2371 Set_Etype (N, Expr_Type);
2372 Set_Is_Overloaded (N, False);
2373 exit Interp_Loop;
2375 elsif Is_Overloaded (N)
2376 and then Present (It.Nam)
2377 and then Ekind (It.Nam) = E_Discriminant
2378 and then Has_Implicit_Dereference (It.Nam)
2379 then
2380 Build_Explicit_Dereference (N, It.Nam);
2382 -- For an explicit dereference, attribute reference, range,
2383 -- short-circuit form (which is not an operator node), or call
2384 -- with a name that is an explicit dereference, there is
2385 -- nothing to be done at this point.
2387 elsif Nkind_In (N, N_Explicit_Dereference,
2388 N_Attribute_Reference,
2389 N_And_Then,
2390 N_Indexed_Component,
2391 N_Or_Else,
2392 N_Range,
2393 N_Selected_Component,
2394 N_Slice)
2395 or else Nkind (Name (N)) = N_Explicit_Dereference
2396 then
2397 null;
2399 -- For procedure or function calls, set the type of the name,
2400 -- and also the entity pointer for the prefix.
2402 elsif Nkind (N) in N_Subprogram_Call
2403 and then Is_Entity_Name (Name (N))
2404 then
2405 Set_Etype (Name (N), Expr_Type);
2406 Set_Entity (Name (N), Seen);
2407 Generate_Reference (Seen, Name (N));
2409 elsif Nkind (N) = N_Function_Call
2410 and then Nkind (Name (N)) = N_Selected_Component
2411 then
2412 Set_Etype (Name (N), Expr_Type);
2413 Set_Entity (Selector_Name (Name (N)), Seen);
2414 Generate_Reference (Seen, Selector_Name (Name (N)));
2416 -- For all other cases, just set the type of the Name
2418 else
2419 Set_Etype (Name (N), Expr_Type);
2420 end if;
2422 end if;
2424 <<Continue>>
2426 -- Move to next interpretation
2428 exit Interp_Loop when No (It.Typ);
2430 Get_Next_Interp (I, It);
2431 end loop Interp_Loop;
2432 end if;
2434 -- At this stage Found indicates whether or not an acceptable
2435 -- interpretation exists. If not, then we have an error, except that if
2436 -- the context is Any_Type as a result of some other error, then we
2437 -- suppress the error report.
2439 if not Found then
2440 if Typ /= Any_Type then
2442 -- If type we are looking for is Void, then this is the procedure
2443 -- call case, and the error is simply that what we gave is not a
2444 -- procedure name (we think of procedure calls as expressions with
2445 -- types internally, but the user doesn't think of them this way!)
2447 if Typ = Standard_Void_Type then
2449 -- Special case message if function used as a procedure
2451 if Nkind (N) = N_Procedure_Call_Statement
2452 and then Is_Entity_Name (Name (N))
2453 and then Ekind (Entity (Name (N))) = E_Function
2454 then
2455 Error_Msg_NE
2456 ("cannot use function & in a procedure call",
2457 Name (N), Entity (Name (N)));
2459 -- Otherwise give general message (not clear what cases this
2460 -- covers, but no harm in providing for them!)
2462 else
2463 Error_Msg_N ("expect procedure name in procedure call", N);
2464 end if;
2466 Found := True;
2468 -- Otherwise we do have a subexpression with the wrong type
2470 -- Check for the case of an allocator which uses an access type
2471 -- instead of the designated type. This is a common error and we
2472 -- specialize the message, posting an error on the operand of the
2473 -- allocator, complaining that we expected the designated type of
2474 -- the allocator.
2476 elsif Nkind (N) = N_Allocator
2477 and then Ekind (Typ) in Access_Kind
2478 and then Ekind (Etype (N)) in Access_Kind
2479 and then Designated_Type (Etype (N)) = Typ
2480 then
2481 Wrong_Type (Expression (N), Designated_Type (Typ));
2482 Found := True;
2484 -- Check for view mismatch on Null in instances, for which the
2485 -- view-swapping mechanism has no identifier.
2487 elsif (In_Instance or else In_Inlined_Body)
2488 and then (Nkind (N) = N_Null)
2489 and then Is_Private_Type (Typ)
2490 and then Is_Access_Type (Full_View (Typ))
2491 then
2492 Resolve (N, Full_View (Typ));
2493 Set_Etype (N, Typ);
2494 return;
2496 -- Check for an aggregate. Sometimes we can get bogus aggregates
2497 -- from misuse of parentheses, and we are about to complain about
2498 -- the aggregate without even looking inside it.
2500 -- Instead, if we have an aggregate of type Any_Composite, then
2501 -- analyze and resolve the component fields, and then only issue
2502 -- another message if we get no errors doing this (otherwise
2503 -- assume that the errors in the aggregate caused the problem).
2505 elsif Nkind (N) = N_Aggregate
2506 and then Etype (N) = Any_Composite
2507 then
2508 -- Disable expansion in any case. If there is a type mismatch
2509 -- it may be fatal to try to expand the aggregate. The flag
2510 -- would otherwise be set to false when the error is posted.
2512 Expander_Active := False;
2514 declare
2515 procedure Check_Aggr (Aggr : Node_Id);
2516 -- Check one aggregate, and set Found to True if we have a
2517 -- definite error in any of its elements
2519 procedure Check_Elmt (Aelmt : Node_Id);
2520 -- Check one element of aggregate and set Found to True if
2521 -- we definitely have an error in the element.
2523 ----------------
2524 -- Check_Aggr --
2525 ----------------
2527 procedure Check_Aggr (Aggr : Node_Id) is
2528 Elmt : Node_Id;
2530 begin
2531 if Present (Expressions (Aggr)) then
2532 Elmt := First (Expressions (Aggr));
2533 while Present (Elmt) loop
2534 Check_Elmt (Elmt);
2535 Next (Elmt);
2536 end loop;
2537 end if;
2539 if Present (Component_Associations (Aggr)) then
2540 Elmt := First (Component_Associations (Aggr));
2541 while Present (Elmt) loop
2543 -- If this is a default-initialized component, then
2544 -- there is nothing to check. The box will be
2545 -- replaced by the appropriate call during late
2546 -- expansion.
2548 if not Box_Present (Elmt) then
2549 Check_Elmt (Expression (Elmt));
2550 end if;
2552 Next (Elmt);
2553 end loop;
2554 end if;
2555 end Check_Aggr;
2557 ----------------
2558 -- Check_Elmt --
2559 ----------------
2561 procedure Check_Elmt (Aelmt : Node_Id) is
2562 begin
2563 -- If we have a nested aggregate, go inside it (to
2564 -- attempt a naked analyze-resolve of the aggregate can
2565 -- cause undesirable cascaded errors). Do not resolve
2566 -- expression if it needs a type from context, as for
2567 -- integer * fixed expression.
2569 if Nkind (Aelmt) = N_Aggregate then
2570 Check_Aggr (Aelmt);
2572 else
2573 Analyze (Aelmt);
2575 if not Is_Overloaded (Aelmt)
2576 and then Etype (Aelmt) /= Any_Fixed
2577 then
2578 Resolve (Aelmt);
2579 end if;
2581 if Etype (Aelmt) = Any_Type then
2582 Found := True;
2583 end if;
2584 end if;
2585 end Check_Elmt;
2587 begin
2588 Check_Aggr (N);
2589 end;
2590 end if;
2592 -- If an error message was issued already, Found got reset to
2593 -- True, so if it is still False, issue standard Wrong_Type msg.
2595 if not Found then
2596 if Is_Overloaded (N)
2597 and then Nkind (N) = N_Function_Call
2598 then
2599 declare
2600 Subp_Name : Node_Id;
2601 begin
2602 if Is_Entity_Name (Name (N)) then
2603 Subp_Name := Name (N);
2605 elsif Nkind (Name (N)) = N_Selected_Component then
2607 -- Protected operation: retrieve operation name
2609 Subp_Name := Selector_Name (Name (N));
2611 else
2612 raise Program_Error;
2613 end if;
2615 Error_Msg_Node_2 := Typ;
2616 Error_Msg_NE
2617 ("no visible interpretation of& "
2618 & "matches expected type&", N, Subp_Name);
2619 end;
2621 if All_Errors_Mode then
2622 declare
2623 Index : Interp_Index;
2624 It : Interp;
2626 begin
2627 Error_Msg_N ("\\possible interpretations:", N);
2629 Get_First_Interp (Name (N), Index, It);
2630 while Present (It.Nam) loop
2631 Error_Msg_Sloc := Sloc (It.Nam);
2632 Error_Msg_Node_2 := It.Nam;
2633 Error_Msg_NE
2634 ("\\ type& for & declared#", N, It.Typ);
2635 Get_Next_Interp (Index, It);
2636 end loop;
2637 end;
2639 else
2640 Error_Msg_N ("\use -gnatf for details", N);
2641 end if;
2643 else
2644 Wrong_Type (N, Typ);
2645 end if;
2646 end if;
2647 end if;
2649 Resolution_Failed;
2650 return;
2652 -- Test if we have more than one interpretation for the context
2654 elsif Ambiguous then
2655 Resolution_Failed;
2656 return;
2658 -- Only one intepretation
2660 else
2661 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
2662 -- the "+" on T is abstract, and the operands are of universal type,
2663 -- the above code will have (incorrectly) resolved the "+" to the
2664 -- universal one in Standard. Therefore check for this case and give
2665 -- an error. We can't do this earlier, because it would cause legal
2666 -- cases to get errors (when some other type has an abstract "+").
2668 if Ada_Version >= Ada_2005
2669 and then Nkind (N) in N_Op
2670 and then Is_Overloaded (N)
2671 and then Is_Universal_Numeric_Type (Etype (Entity (N)))
2672 then
2673 Get_First_Interp (N, I, It);
2674 while Present (It.Typ) loop
2675 if Present (It.Abstract_Op) and then
2676 Etype (It.Abstract_Op) = Typ
2677 then
2678 Error_Msg_NE
2679 ("cannot call abstract subprogram &!", N, It.Abstract_Op);
2680 return;
2681 end if;
2683 Get_Next_Interp (I, It);
2684 end loop;
2685 end if;
2687 -- Here we have an acceptable interpretation for the context
2689 -- Propagate type information and normalize tree for various
2690 -- predefined operations. If the context only imposes a class of
2691 -- types, rather than a specific type, propagate the actual type
2692 -- downward.
2694 if Typ = Any_Integer or else
2695 Typ = Any_Boolean or else
2696 Typ = Any_Modular or else
2697 Typ = Any_Real or else
2698 Typ = Any_Discrete
2699 then
2700 Ctx_Type := Expr_Type;
2702 -- Any_Fixed is legal in a real context only if a specific fixed-
2703 -- point type is imposed. If Norman Cohen can be confused by this,
2704 -- it deserves a separate message.
2706 if Typ = Any_Real
2707 and then Expr_Type = Any_Fixed
2708 then
2709 Error_Msg_N ("illegal context for mixed mode operation", N);
2710 Set_Etype (N, Universal_Real);
2711 Ctx_Type := Universal_Real;
2712 end if;
2713 end if;
2715 -- A user-defined operator is transformed into a function call at
2716 -- this point, so that further processing knows that operators are
2717 -- really operators (i.e. are predefined operators). User-defined
2718 -- operators that are intrinsic are just renamings of the predefined
2719 -- ones, and need not be turned into calls either, but if they rename
2720 -- a different operator, we must transform the node accordingly.
2721 -- Instantiations of Unchecked_Conversion are intrinsic but are
2722 -- treated as functions, even if given an operator designator.
2724 if Nkind (N) in N_Op
2725 and then Present (Entity (N))
2726 and then Ekind (Entity (N)) /= E_Operator
2727 then
2729 if not Is_Predefined_Op (Entity (N)) then
2730 Rewrite_Operator_As_Call (N, Entity (N));
2732 elsif Present (Alias (Entity (N)))
2733 and then
2734 Nkind (Parent (Parent (Entity (N)))) =
2735 N_Subprogram_Renaming_Declaration
2736 then
2737 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2739 -- If the node is rewritten, it will be fully resolved in
2740 -- Rewrite_Renamed_Operator.
2742 if Analyzed (N) then
2743 return;
2744 end if;
2745 end if;
2746 end if;
2748 case N_Subexpr'(Nkind (N)) is
2750 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2752 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2754 when N_Short_Circuit
2755 => Resolve_Short_Circuit (N, Ctx_Type);
2757 when N_Attribute_Reference
2758 => Resolve_Attribute (N, Ctx_Type);
2760 when N_Case_Expression
2761 => Resolve_Case_Expression (N, Ctx_Type);
2763 when N_Character_Literal
2764 => Resolve_Character_Literal (N, Ctx_Type);
2766 when N_Expanded_Name
2767 => Resolve_Entity_Name (N, Ctx_Type);
2769 when N_Explicit_Dereference
2770 => Resolve_Explicit_Dereference (N, Ctx_Type);
2772 when N_Expression_With_Actions
2773 => Resolve_Expression_With_Actions (N, Ctx_Type);
2775 when N_Extension_Aggregate
2776 => Resolve_Extension_Aggregate (N, Ctx_Type);
2778 when N_Function_Call
2779 => Resolve_Call (N, Ctx_Type);
2781 when N_Identifier
2782 => Resolve_Entity_Name (N, Ctx_Type);
2784 when N_If_Expression
2785 => Resolve_If_Expression (N, Ctx_Type);
2787 when N_Indexed_Component
2788 => Resolve_Indexed_Component (N, Ctx_Type);
2790 when N_Integer_Literal
2791 => Resolve_Integer_Literal (N, Ctx_Type);
2793 when N_Membership_Test
2794 => Resolve_Membership_Op (N, Ctx_Type);
2796 when N_Null => Resolve_Null (N, Ctx_Type);
2798 when N_Op_And | N_Op_Or | N_Op_Xor
2799 => Resolve_Logical_Op (N, Ctx_Type);
2801 when N_Op_Eq | N_Op_Ne
2802 => Resolve_Equality_Op (N, Ctx_Type);
2804 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2805 => Resolve_Comparison_Op (N, Ctx_Type);
2807 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2809 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2810 N_Op_Divide | N_Op_Mod | N_Op_Rem
2812 => Resolve_Arithmetic_Op (N, Ctx_Type);
2814 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2816 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2818 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2819 => Resolve_Unary_Op (N, Ctx_Type);
2821 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2823 when N_Procedure_Call_Statement
2824 => Resolve_Call (N, Ctx_Type);
2826 when N_Operator_Symbol
2827 => Resolve_Operator_Symbol (N, Ctx_Type);
2829 when N_Qualified_Expression
2830 => Resolve_Qualified_Expression (N, Ctx_Type);
2832 -- Why is the following null, needs a comment ???
2834 when N_Quantified_Expression
2835 => null;
2837 -- Nothing to do for Raise_Expression, since we took care of
2838 -- setting the Etype earlier, and no other processing is needed.
2840 when N_Raise_Expression
2841 => null;
2843 when N_Raise_xxx_Error
2844 => Set_Etype (N, Ctx_Type);
2846 when N_Range => Resolve_Range (N, Ctx_Type);
2848 when N_Real_Literal
2849 => Resolve_Real_Literal (N, Ctx_Type);
2851 when N_Reference => Resolve_Reference (N, Ctx_Type);
2853 when N_Selected_Component
2854 => Resolve_Selected_Component (N, Ctx_Type);
2856 when N_Slice => Resolve_Slice (N, Ctx_Type);
2858 when N_String_Literal
2859 => Resolve_String_Literal (N, Ctx_Type);
2861 when N_Subprogram_Info
2862 => Resolve_Subprogram_Info (N, Ctx_Type);
2864 when N_Type_Conversion
2865 => Resolve_Type_Conversion (N, Ctx_Type);
2867 when N_Unchecked_Expression =>
2868 Resolve_Unchecked_Expression (N, Ctx_Type);
2870 when N_Unchecked_Type_Conversion =>
2871 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
2872 end case;
2874 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
2875 -- expression of an anonymous access type that occurs in the context
2876 -- of a named general access type, except when the expression is that
2877 -- of a membership test. This ensures proper legality checking in
2878 -- terms of allowed conversions (expressions that would be illegal to
2879 -- convert implicitly are allowed in membership tests).
2881 if Ada_Version >= Ada_2012
2882 and then Ekind (Ctx_Type) = E_General_Access_Type
2883 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
2884 and then Nkind (Parent (N)) not in N_Membership_Test
2885 then
2886 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
2887 Analyze_And_Resolve (N, Ctx_Type);
2888 end if;
2890 -- If the subexpression was replaced by a non-subexpression, then
2891 -- all we do is to expand it. The only legitimate case we know of
2892 -- is converting procedure call statement to entry call statements,
2893 -- but there may be others, so we are making this test general.
2895 if Nkind (N) not in N_Subexpr then
2896 Debug_A_Exit ("resolving ", N, " (done)");
2897 Expand (N);
2898 return;
2899 end if;
2901 -- The expression is definitely NOT overloaded at this point, so
2902 -- we reset the Is_Overloaded flag to avoid any confusion when
2903 -- reanalyzing the node.
2905 Set_Is_Overloaded (N, False);
2907 -- Freeze expression type, entity if it is a name, and designated
2908 -- type if it is an allocator (RM 13.14(10,11,13)).
2910 -- Now that the resolution of the type of the node is complete, and
2911 -- we did not detect an error, we can expand this node. We skip the
2912 -- expand call if we are in a default expression, see section
2913 -- "Handling of Default Expressions" in Sem spec.
2915 Debug_A_Exit ("resolving ", N, " (done)");
2917 -- We unconditionally freeze the expression, even if we are in
2918 -- default expression mode (the Freeze_Expression routine tests this
2919 -- flag and only freezes static types if it is set).
2921 -- Ada 2012 (AI05-177): Expression functions do not freeze. Only
2922 -- their use (in an expanded call) freezes.
2924 if Ekind (Proper_Current_Scope) /= E_Function
2925 or else Nkind (Original_Node (Unit_Declaration_Node
2926 (Proper_Current_Scope))) /= N_Expression_Function
2927 then
2928 Freeze_Expression (N);
2929 end if;
2931 -- Now we can do the expansion
2933 Expand (N);
2934 end if;
2935 end Resolve;
2937 -------------
2938 -- Resolve --
2939 -------------
2941 -- Version with check(s) suppressed
2943 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2944 begin
2945 if Suppress = All_Checks then
2946 declare
2947 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
2948 begin
2949 Scope_Suppress.Suppress := (others => True);
2950 Resolve (N, Typ);
2951 Scope_Suppress.Suppress := Sva;
2952 end;
2954 else
2955 declare
2956 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
2957 begin
2958 Scope_Suppress.Suppress (Suppress) := True;
2959 Resolve (N, Typ);
2960 Scope_Suppress.Suppress (Suppress) := Svg;
2961 end;
2962 end if;
2963 end Resolve;
2965 -------------
2966 -- Resolve --
2967 -------------
2969 -- Version with implicit type
2971 procedure Resolve (N : Node_Id) is
2972 begin
2973 Resolve (N, Etype (N));
2974 end Resolve;
2976 ---------------------
2977 -- Resolve_Actuals --
2978 ---------------------
2980 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2981 Loc : constant Source_Ptr := Sloc (N);
2982 A : Node_Id;
2983 F : Entity_Id;
2984 A_Typ : Entity_Id;
2985 F_Typ : Entity_Id;
2986 Prev : Node_Id := Empty;
2987 Orig_A : Node_Id;
2989 procedure Check_Argument_Order;
2990 -- Performs a check for the case where the actuals are all simple
2991 -- identifiers that correspond to the formal names, but in the wrong
2992 -- order, which is considered suspicious and cause for a warning.
2994 procedure Check_Prefixed_Call;
2995 -- If the original node is an overloaded call in prefix notation,
2996 -- insert an 'Access or a dereference as needed over the first actual.
2997 -- Try_Object_Operation has already verified that there is a valid
2998 -- interpretation, but the form of the actual can only be determined
2999 -- once the primitive operation is identified.
3001 procedure Insert_Default;
3002 -- If the actual is missing in a call, insert in the actuals list
3003 -- an instance of the default expression. The insertion is always
3004 -- a named association.
3006 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
3007 -- Check whether T1 and T2, or their full views, are derived from a
3008 -- common type. Used to enforce the restrictions on array conversions
3009 -- of AI95-00246.
3011 function Static_Concatenation (N : Node_Id) return Boolean;
3012 -- Predicate to determine whether an actual that is a concatenation
3013 -- will be evaluated statically and does not need a transient scope.
3014 -- This must be determined before the actual is resolved and expanded
3015 -- because if needed the transient scope must be introduced earlier.
3017 --------------------------
3018 -- Check_Argument_Order --
3019 --------------------------
3021 procedure Check_Argument_Order is
3022 begin
3023 -- Nothing to do if no parameters, or original node is neither a
3024 -- function call nor a procedure call statement (happens in the
3025 -- operator-transformed-to-function call case), or the call does
3026 -- not come from source, or this warning is off.
3028 if not Warn_On_Parameter_Order
3029 or else No (Parameter_Associations (N))
3030 or else Nkind (Original_Node (N)) not in N_Subprogram_Call
3031 or else not Comes_From_Source (N)
3032 then
3033 return;
3034 end if;
3036 declare
3037 Nargs : constant Nat := List_Length (Parameter_Associations (N));
3039 begin
3040 -- Nothing to do if only one parameter
3042 if Nargs < 2 then
3043 return;
3044 end if;
3046 -- Here if at least two arguments
3048 declare
3049 Actuals : array (1 .. Nargs) of Node_Id;
3050 Actual : Node_Id;
3051 Formal : Node_Id;
3053 Wrong_Order : Boolean := False;
3054 -- Set True if an out of order case is found
3056 begin
3057 -- Collect identifier names of actuals, fail if any actual is
3058 -- not a simple identifier, and record max length of name.
3060 Actual := First (Parameter_Associations (N));
3061 for J in Actuals'Range loop
3062 if Nkind (Actual) /= N_Identifier then
3063 return;
3064 else
3065 Actuals (J) := Actual;
3066 Next (Actual);
3067 end if;
3068 end loop;
3070 -- If we got this far, all actuals are identifiers and the list
3071 -- of their names is stored in the Actuals array.
3073 Formal := First_Formal (Nam);
3074 for J in Actuals'Range loop
3076 -- If we ran out of formals, that's odd, probably an error
3077 -- which will be detected elsewhere, but abandon the search.
3079 if No (Formal) then
3080 return;
3081 end if;
3083 -- If name matches and is in order OK
3085 if Chars (Formal) = Chars (Actuals (J)) then
3086 null;
3088 else
3089 -- If no match, see if it is elsewhere in list and if so
3090 -- flag potential wrong order if type is compatible.
3092 for K in Actuals'Range loop
3093 if Chars (Formal) = Chars (Actuals (K))
3094 and then
3095 Has_Compatible_Type (Actuals (K), Etype (Formal))
3096 then
3097 Wrong_Order := True;
3098 goto Continue;
3099 end if;
3100 end loop;
3102 -- No match
3104 return;
3105 end if;
3107 <<Continue>> Next_Formal (Formal);
3108 end loop;
3110 -- If Formals left over, also probably an error, skip warning
3112 if Present (Formal) then
3113 return;
3114 end if;
3116 -- Here we give the warning if something was out of order
3118 if Wrong_Order then
3119 Error_Msg_N
3120 ("?P?actuals for this call may be in wrong order", N);
3121 end if;
3122 end;
3123 end;
3124 end Check_Argument_Order;
3126 -------------------------
3127 -- Check_Prefixed_Call --
3128 -------------------------
3130 procedure Check_Prefixed_Call is
3131 Act : constant Node_Id := First_Actual (N);
3132 A_Type : constant Entity_Id := Etype (Act);
3133 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3134 Orig : constant Node_Id := Original_Node (N);
3135 New_A : Node_Id;
3137 begin
3138 -- Check whether the call is a prefixed call, with or without
3139 -- additional actuals.
3141 if Nkind (Orig) = N_Selected_Component
3142 or else
3143 (Nkind (Orig) = N_Indexed_Component
3144 and then Nkind (Prefix (Orig)) = N_Selected_Component
3145 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3146 and then Is_Entity_Name (Act)
3147 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3148 then
3149 if Is_Access_Type (A_Type)
3150 and then not Is_Access_Type (F_Type)
3151 then
3152 -- Introduce dereference on object in prefix
3154 New_A :=
3155 Make_Explicit_Dereference (Sloc (Act),
3156 Prefix => Relocate_Node (Act));
3157 Rewrite (Act, New_A);
3158 Analyze (Act);
3160 elsif Is_Access_Type (F_Type)
3161 and then not Is_Access_Type (A_Type)
3162 then
3163 -- Introduce an implicit 'Access in prefix
3165 if not Is_Aliased_View (Act) then
3166 Error_Msg_NE
3167 ("object in prefixed call to& must be aliased"
3168 & " (RM-2005 4.3.1 (13))",
3169 Prefix (Act), Nam);
3170 end if;
3172 Rewrite (Act,
3173 Make_Attribute_Reference (Loc,
3174 Attribute_Name => Name_Access,
3175 Prefix => Relocate_Node (Act)));
3176 end if;
3178 Analyze (Act);
3179 end if;
3180 end Check_Prefixed_Call;
3182 --------------------
3183 -- Insert_Default --
3184 --------------------
3186 procedure Insert_Default is
3187 Actval : Node_Id;
3188 Assoc : Node_Id;
3190 begin
3191 -- Missing argument in call, nothing to insert
3193 if No (Default_Value (F)) then
3194 return;
3196 else
3197 -- Note that we do a full New_Copy_Tree, so that any associated
3198 -- Itypes are properly copied. This may not be needed any more,
3199 -- but it does no harm as a safety measure! Defaults of a generic
3200 -- formal may be out of bounds of the corresponding actual (see
3201 -- cc1311b) and an additional check may be required.
3203 Actval :=
3204 New_Copy_Tree
3205 (Default_Value (F),
3206 New_Scope => Current_Scope,
3207 New_Sloc => Loc);
3209 if Is_Concurrent_Type (Scope (Nam))
3210 and then Has_Discriminants (Scope (Nam))
3211 then
3212 Replace_Actual_Discriminants (N, Actval);
3213 end if;
3215 if Is_Overloadable (Nam)
3216 and then Present (Alias (Nam))
3217 then
3218 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3219 and then not Is_Tagged_Type (Etype (F))
3220 then
3221 -- If default is a real literal, do not introduce a
3222 -- conversion whose effect may depend on the run-time
3223 -- size of universal real.
3225 if Nkind (Actval) = N_Real_Literal then
3226 Set_Etype (Actval, Base_Type (Etype (F)));
3227 else
3228 Actval := Unchecked_Convert_To (Etype (F), Actval);
3229 end if;
3230 end if;
3232 if Is_Scalar_Type (Etype (F)) then
3233 Enable_Range_Check (Actval);
3234 end if;
3236 Set_Parent (Actval, N);
3238 -- Resolve aggregates with their base type, to avoid scope
3239 -- anomalies: the subtype was first built in the subprogram
3240 -- declaration, and the current call may be nested.
3242 if Nkind (Actval) = N_Aggregate then
3243 Analyze_And_Resolve (Actval, Etype (F));
3244 else
3245 Analyze_And_Resolve (Actval, Etype (Actval));
3246 end if;
3248 else
3249 Set_Parent (Actval, N);
3251 -- See note above concerning aggregates
3253 if Nkind (Actval) = N_Aggregate
3254 and then Has_Discriminants (Etype (Actval))
3255 then
3256 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3258 -- Resolve entities with their own type, which may differ from
3259 -- the type of a reference in a generic context (the view
3260 -- swapping mechanism did not anticipate the re-analysis of
3261 -- default values in calls).
3263 elsif Is_Entity_Name (Actval) then
3264 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3266 else
3267 Analyze_And_Resolve (Actval, Etype (Actval));
3268 end if;
3269 end if;
3271 -- If default is a tag indeterminate function call, propagate tag
3272 -- to obtain proper dispatching.
3274 if Is_Controlling_Formal (F)
3275 and then Nkind (Default_Value (F)) = N_Function_Call
3276 then
3277 Set_Is_Controlling_Actual (Actval);
3278 end if;
3280 end if;
3282 -- If the default expression raises constraint error, then just
3283 -- silently replace it with an N_Raise_Constraint_Error node, since
3284 -- we already gave the warning on the subprogram spec. If node is
3285 -- already a Raise_Constraint_Error leave as is, to prevent loops in
3286 -- the warnings removal machinery.
3288 if Raises_Constraint_Error (Actval)
3289 and then Nkind (Actval) /= N_Raise_Constraint_Error
3290 then
3291 Rewrite (Actval,
3292 Make_Raise_Constraint_Error (Loc,
3293 Reason => CE_Range_Check_Failed));
3294 Set_Raises_Constraint_Error (Actval);
3295 Set_Etype (Actval, Etype (F));
3296 end if;
3298 Assoc :=
3299 Make_Parameter_Association (Loc,
3300 Explicit_Actual_Parameter => Actval,
3301 Selector_Name => Make_Identifier (Loc, Chars (F)));
3303 -- Case of insertion is first named actual
3305 if No (Prev) or else
3306 Nkind (Parent (Prev)) /= N_Parameter_Association
3307 then
3308 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3309 Set_First_Named_Actual (N, Actval);
3311 if No (Prev) then
3312 if No (Parameter_Associations (N)) then
3313 Set_Parameter_Associations (N, New_List (Assoc));
3314 else
3315 Append (Assoc, Parameter_Associations (N));
3316 end if;
3318 else
3319 Insert_After (Prev, Assoc);
3320 end if;
3322 -- Case of insertion is not first named actual
3324 else
3325 Set_Next_Named_Actual
3326 (Assoc, Next_Named_Actual (Parent (Prev)));
3327 Set_Next_Named_Actual (Parent (Prev), Actval);
3328 Append (Assoc, Parameter_Associations (N));
3329 end if;
3331 Mark_Rewrite_Insertion (Assoc);
3332 Mark_Rewrite_Insertion (Actval);
3334 Prev := Actval;
3335 end Insert_Default;
3337 -------------------
3338 -- Same_Ancestor --
3339 -------------------
3341 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3342 FT1 : Entity_Id := T1;
3343 FT2 : Entity_Id := T2;
3345 begin
3346 if Is_Private_Type (T1)
3347 and then Present (Full_View (T1))
3348 then
3349 FT1 := Full_View (T1);
3350 end if;
3352 if Is_Private_Type (T2)
3353 and then Present (Full_View (T2))
3354 then
3355 FT2 := Full_View (T2);
3356 end if;
3358 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3359 end Same_Ancestor;
3361 --------------------------
3362 -- Static_Concatenation --
3363 --------------------------
3365 function Static_Concatenation (N : Node_Id) return Boolean is
3366 begin
3367 case Nkind (N) is
3368 when N_String_Literal =>
3369 return True;
3371 when N_Op_Concat =>
3373 -- Concatenation is static when both operands are static and
3374 -- the concatenation operator is a predefined one.
3376 return Scope (Entity (N)) = Standard_Standard
3377 and then
3378 Static_Concatenation (Left_Opnd (N))
3379 and then
3380 Static_Concatenation (Right_Opnd (N));
3382 when others =>
3383 if Is_Entity_Name (N) then
3384 declare
3385 Ent : constant Entity_Id := Entity (N);
3386 begin
3387 return Ekind (Ent) = E_Constant
3388 and then Present (Constant_Value (Ent))
3389 and then
3390 Is_Static_Expression (Constant_Value (Ent));
3391 end;
3393 else
3394 return False;
3395 end if;
3396 end case;
3397 end Static_Concatenation;
3399 -- Start of processing for Resolve_Actuals
3401 begin
3402 Check_Argument_Order;
3403 Check_Function_Writable_Actuals (N);
3405 if Present (First_Actual (N)) then
3406 Check_Prefixed_Call;
3407 end if;
3409 A := First_Actual (N);
3410 F := First_Formal (Nam);
3411 while Present (F) loop
3412 if No (A) and then Needs_No_Actuals (Nam) then
3413 null;
3415 -- If we have an error in any actual or formal, indicated by a type
3416 -- of Any_Type, then abandon resolution attempt, and set result type
3417 -- to Any_Type.
3419 elsif (Present (A) and then Etype (A) = Any_Type)
3420 or else Etype (F) = Any_Type
3421 then
3422 Set_Etype (N, Any_Type);
3423 return;
3424 end if;
3426 -- Case where actual is present
3428 -- If the actual is an entity, generate a reference to it now. We
3429 -- do this before the actual is resolved, because a formal of some
3430 -- protected subprogram, or a task discriminant, will be rewritten
3431 -- during expansion, and the source entity reference may be lost.
3433 if Present (A)
3434 and then Is_Entity_Name (A)
3435 and then Comes_From_Source (N)
3436 then
3437 Orig_A := Entity (A);
3439 if Present (Orig_A) then
3440 if Is_Formal (Orig_A)
3441 and then Ekind (F) /= E_In_Parameter
3442 then
3443 Generate_Reference (Orig_A, A, 'm');
3445 elsif not Is_Overloaded (A) then
3446 if Ekind (F) /= E_Out_Parameter then
3447 Generate_Reference (Orig_A, A);
3449 -- RM 6.4.1(12): For an out parameter that is passed by
3450 -- copy, the formal parameter object is created, and:
3452 -- * For an access type, the formal parameter is initialized
3453 -- from the value of the actual, without checking that the
3454 -- value satisfies any constraint, any predicate, or any
3455 -- exclusion of the null value.
3457 -- * For a scalar type that has the Default_Value aspect
3458 -- specified, the formal parameter is initialized from the
3459 -- value of the actual, without checking that the value
3460 -- satisfies any constraint or any predicate.
3461 -- I do not understand why this case is included??? this is
3462 -- not a case where an OUT parameter is treated as IN OUT.
3464 -- * For a composite type with discriminants or that has
3465 -- implicit initial values for any subcomponents, the
3466 -- behavior is as for an in out parameter passed by copy.
3468 -- Hence for these cases we generate the read reference now
3469 -- (the write reference will be generated later by
3470 -- Note_Possible_Modification).
3472 elsif Is_By_Copy_Type (Etype (F))
3473 and then
3474 (Is_Access_Type (Etype (F))
3475 or else
3476 (Is_Scalar_Type (Etype (F))
3477 and then
3478 Present (Default_Aspect_Value (Etype (F))))
3479 or else
3480 (Is_Composite_Type (Etype (F))
3481 and then (Has_Discriminants (Etype (F))
3482 or else Is_Partially_Initialized_Type
3483 (Etype (F)))))
3484 then
3485 Generate_Reference (Orig_A, A);
3486 end if;
3487 end if;
3488 end if;
3489 end if;
3491 if Present (A)
3492 and then (Nkind (Parent (A)) /= N_Parameter_Association
3493 or else Chars (Selector_Name (Parent (A))) = Chars (F))
3494 then
3495 -- If style checking mode on, check match of formal name
3497 if Style_Check then
3498 if Nkind (Parent (A)) = N_Parameter_Association then
3499 Check_Identifier (Selector_Name (Parent (A)), F);
3500 end if;
3501 end if;
3503 -- If the formal is Out or In_Out, do not resolve and expand the
3504 -- conversion, because it is subsequently expanded into explicit
3505 -- temporaries and assignments. However, the object of the
3506 -- conversion can be resolved. An exception is the case of tagged
3507 -- type conversion with a class-wide actual. In that case we want
3508 -- the tag check to occur and no temporary will be needed (no
3509 -- representation change can occur) and the parameter is passed by
3510 -- reference, so we go ahead and resolve the type conversion.
3511 -- Another exception is the case of reference to component or
3512 -- subcomponent of a bit-packed array, in which case we want to
3513 -- defer expansion to the point the in and out assignments are
3514 -- performed.
3516 if Ekind (F) /= E_In_Parameter
3517 and then Nkind (A) = N_Type_Conversion
3518 and then not Is_Class_Wide_Type (Etype (Expression (A)))
3519 then
3520 if Ekind (F) = E_In_Out_Parameter
3521 and then Is_Array_Type (Etype (F))
3522 then
3523 -- In a view conversion, the conversion must be legal in
3524 -- both directions, and thus both component types must be
3525 -- aliased, or neither (4.6 (8)).
3527 -- The extra rule in 4.6 (24.9.2) seems unduly restrictive:
3528 -- the privacy requirement should not apply to generic
3529 -- types, and should be checked in an instance. ARG query
3530 -- is in order ???
3532 if Has_Aliased_Components (Etype (Expression (A))) /=
3533 Has_Aliased_Components (Etype (F))
3534 then
3535 Error_Msg_N
3536 ("both component types in a view conversion must be"
3537 & " aliased, or neither", A);
3539 -- Comment here??? what set of cases???
3541 elsif
3542 not Same_Ancestor (Etype (F), Etype (Expression (A)))
3543 then
3544 -- Check view conv between unrelated by ref array types
3546 if Is_By_Reference_Type (Etype (F))
3547 or else Is_By_Reference_Type (Etype (Expression (A)))
3548 then
3549 Error_Msg_N
3550 ("view conversion between unrelated by reference "
3551 & "array types not allowed (\'A'I-00246)", A);
3553 -- In Ada 2005 mode, check view conversion component
3554 -- type cannot be private, tagged, or volatile. Note
3555 -- that we only apply this to source conversions. The
3556 -- generated code can contain conversions which are
3557 -- not subject to this test, and we cannot extract the
3558 -- component type in such cases since it is not present.
3560 elsif Comes_From_Source (A)
3561 and then Ada_Version >= Ada_2005
3562 then
3563 declare
3564 Comp_Type : constant Entity_Id :=
3565 Component_Type
3566 (Etype (Expression (A)));
3567 begin
3568 if (Is_Private_Type (Comp_Type)
3569 and then not Is_Generic_Type (Comp_Type))
3570 or else Is_Tagged_Type (Comp_Type)
3571 or else Is_Volatile (Comp_Type)
3572 then
3573 Error_Msg_N
3574 ("component type of a view conversion cannot"
3575 & " be private, tagged, or volatile"
3576 & " (RM 4.6 (24))",
3577 Expression (A));
3578 end if;
3579 end;
3580 end if;
3581 end if;
3582 end if;
3584 -- Resolve expression if conversion is all OK
3586 if (Conversion_OK (A)
3587 or else Valid_Conversion (A, Etype (A), Expression (A)))
3588 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
3589 then
3590 Resolve (Expression (A));
3591 end if;
3593 -- If the actual is a function call that returns a limited
3594 -- unconstrained object that needs finalization, create a
3595 -- transient scope for it, so that it can receive the proper
3596 -- finalization list.
3598 elsif Nkind (A) = N_Function_Call
3599 and then Is_Limited_Record (Etype (F))
3600 and then not Is_Constrained (Etype (F))
3601 and then Full_Expander_Active
3602 and then (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3603 then
3604 Establish_Transient_Scope (A, False);
3605 Resolve (A, Etype (F));
3607 -- A small optimization: if one of the actuals is a concatenation
3608 -- create a block around a procedure call to recover stack space.
3609 -- This alleviates stack usage when several procedure calls in
3610 -- the same statement list use concatenation. We do not perform
3611 -- this wrapping for code statements, where the argument is a
3612 -- static string, and we want to preserve warnings involving
3613 -- sequences of such statements.
3615 elsif Nkind (A) = N_Op_Concat
3616 and then Nkind (N) = N_Procedure_Call_Statement
3617 and then Full_Expander_Active
3618 and then
3619 not (Is_Intrinsic_Subprogram (Nam)
3620 and then Chars (Nam) = Name_Asm)
3621 and then not Static_Concatenation (A)
3622 then
3623 Establish_Transient_Scope (A, False);
3624 Resolve (A, Etype (F));
3626 else
3627 if Nkind (A) = N_Type_Conversion
3628 and then Is_Array_Type (Etype (F))
3629 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3630 and then
3631 (Is_Limited_Type (Etype (F))
3632 or else Is_Limited_Type (Etype (Expression (A))))
3633 then
3634 Error_Msg_N
3635 ("conversion between unrelated limited array types "
3636 & "not allowed (\A\I-00246)", A);
3638 if Is_Limited_Type (Etype (F)) then
3639 Explain_Limited_Type (Etype (F), A);
3640 end if;
3642 if Is_Limited_Type (Etype (Expression (A))) then
3643 Explain_Limited_Type (Etype (Expression (A)), A);
3644 end if;
3645 end if;
3647 -- (Ada 2005: AI-251): If the actual is an allocator whose
3648 -- directly designated type is a class-wide interface, we build
3649 -- an anonymous access type to use it as the type of the
3650 -- allocator. Later, when the subprogram call is expanded, if
3651 -- the interface has a secondary dispatch table the expander
3652 -- will add a type conversion to force the correct displacement
3653 -- of the pointer.
3655 if Nkind (A) = N_Allocator then
3656 declare
3657 DDT : constant Entity_Id :=
3658 Directly_Designated_Type (Base_Type (Etype (F)));
3660 New_Itype : Entity_Id;
3662 begin
3663 if Is_Class_Wide_Type (DDT)
3664 and then Is_Interface (DDT)
3665 then
3666 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
3667 Set_Etype (New_Itype, Etype (A));
3668 Set_Directly_Designated_Type (New_Itype,
3669 Directly_Designated_Type (Etype (A)));
3670 Set_Etype (A, New_Itype);
3671 end if;
3673 -- Ada 2005, AI-162:If the actual is an allocator, the
3674 -- innermost enclosing statement is the master of the
3675 -- created object. This needs to be done with expansion
3676 -- enabled only, otherwise the transient scope will not
3677 -- be removed in the expansion of the wrapped construct.
3679 if (Is_Controlled (DDT) or else Has_Task (DDT))
3680 and then Full_Expander_Active
3681 then
3682 Establish_Transient_Scope (A, False);
3683 end if;
3684 end;
3686 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3687 Check_Restriction (No_Access_Parameter_Allocators, A);
3688 end if;
3689 end if;
3691 -- (Ada 2005): The call may be to a primitive operation of a
3692 -- tagged synchronized type, declared outside of the type. In
3693 -- this case the controlling actual must be converted to its
3694 -- corresponding record type, which is the formal type. The
3695 -- actual may be a subtype, either because of a constraint or
3696 -- because it is a generic actual, so use base type to locate
3697 -- concurrent type.
3699 F_Typ := Base_Type (Etype (F));
3701 if Is_Tagged_Type (F_Typ)
3702 and then (Is_Concurrent_Type (F_Typ)
3703 or else Is_Concurrent_Record_Type (F_Typ))
3704 then
3705 -- If the actual is overloaded, look for an interpretation
3706 -- that has a synchronized type.
3708 if not Is_Overloaded (A) then
3709 A_Typ := Base_Type (Etype (A));
3711 else
3712 declare
3713 Index : Interp_Index;
3714 It : Interp;
3716 begin
3717 Get_First_Interp (A, Index, It);
3718 while Present (It.Typ) loop
3719 if Is_Concurrent_Type (It.Typ)
3720 or else Is_Concurrent_Record_Type (It.Typ)
3721 then
3722 A_Typ := Base_Type (It.Typ);
3723 exit;
3724 end if;
3726 Get_Next_Interp (Index, It);
3727 end loop;
3728 end;
3729 end if;
3731 declare
3732 Full_A_Typ : Entity_Id;
3734 begin
3735 if Present (Full_View (A_Typ)) then
3736 Full_A_Typ := Base_Type (Full_View (A_Typ));
3737 else
3738 Full_A_Typ := A_Typ;
3739 end if;
3741 -- Tagged synchronized type (case 1): the actual is a
3742 -- concurrent type.
3744 if Is_Concurrent_Type (A_Typ)
3745 and then Corresponding_Record_Type (A_Typ) = F_Typ
3746 then
3747 Rewrite (A,
3748 Unchecked_Convert_To
3749 (Corresponding_Record_Type (A_Typ), A));
3750 Resolve (A, Etype (F));
3752 -- Tagged synchronized type (case 2): the formal is a
3753 -- concurrent type.
3755 elsif Ekind (Full_A_Typ) = E_Record_Type
3756 and then Present
3757 (Corresponding_Concurrent_Type (Full_A_Typ))
3758 and then Is_Concurrent_Type (F_Typ)
3759 and then Present (Corresponding_Record_Type (F_Typ))
3760 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
3761 then
3762 Resolve (A, Corresponding_Record_Type (F_Typ));
3764 -- Common case
3766 else
3767 Resolve (A, Etype (F));
3768 end if;
3769 end;
3770 else
3772 -- not a synchronized operation.
3774 Resolve (A, Etype (F));
3775 end if;
3776 end if;
3778 A_Typ := Etype (A);
3779 F_Typ := Etype (F);
3781 if Comes_From_Source (Original_Node (N))
3782 and then Nkind_In (Original_Node (N), N_Function_Call,
3783 N_Procedure_Call_Statement)
3784 then
3785 -- In formal mode, check that actual parameters matching
3786 -- formals of tagged types are objects (or ancestor type
3787 -- conversions of objects), not general expressions.
3789 if Is_Actual_Tagged_Parameter (A) then
3790 if Is_SPARK_Object_Reference (A) then
3791 null;
3793 elsif Nkind (A) = N_Type_Conversion then
3794 declare
3795 Operand : constant Node_Id := Expression (A);
3796 Operand_Typ : constant Entity_Id := Etype (Operand);
3797 Target_Typ : constant Entity_Id := A_Typ;
3799 begin
3800 if not Is_SPARK_Object_Reference (Operand) then
3801 Check_SPARK_Restriction
3802 ("object required", Operand);
3804 -- In formal mode, the only view conversions are those
3805 -- involving ancestor conversion of an extended type.
3807 elsif not
3808 (Is_Tagged_Type (Target_Typ)
3809 and then not Is_Class_Wide_Type (Target_Typ)
3810 and then Is_Tagged_Type (Operand_Typ)
3811 and then not Is_Class_Wide_Type (Operand_Typ)
3812 and then Is_Ancestor (Target_Typ, Operand_Typ))
3813 then
3814 if Ekind_In
3815 (F, E_Out_Parameter, E_In_Out_Parameter)
3816 then
3817 Check_SPARK_Restriction
3818 ("ancestor conversion is the only permitted "
3819 & "view conversion", A);
3820 else
3821 Check_SPARK_Restriction
3822 ("ancestor conversion required", A);
3823 end if;
3825 else
3826 null;
3827 end if;
3828 end;
3830 else
3831 Check_SPARK_Restriction ("object required", A);
3832 end if;
3834 -- In formal mode, the only view conversions are those
3835 -- involving ancestor conversion of an extended type.
3837 elsif Nkind (A) = N_Type_Conversion
3838 and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
3839 then
3840 Check_SPARK_Restriction
3841 ("ancestor conversion is the only permitted view "
3842 & "conversion", A);
3843 end if;
3844 end if;
3846 -- has warnings suppressed, then we reset Never_Set_In_Source for
3847 -- the calling entity. The reason for this is to catch cases like
3848 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
3849 -- uses trickery to modify an IN parameter.
3851 if Ekind (F) = E_In_Parameter
3852 and then Is_Entity_Name (A)
3853 and then Present (Entity (A))
3854 and then Ekind (Entity (A)) = E_Variable
3855 and then Has_Warnings_Off (F_Typ)
3856 then
3857 Set_Never_Set_In_Source (Entity (A), False);
3858 end if;
3860 -- Perform error checks for IN and IN OUT parameters
3862 if Ekind (F) /= E_Out_Parameter then
3864 -- Check unset reference. For scalar parameters, it is clearly
3865 -- wrong to pass an uninitialized value as either an IN or
3866 -- IN-OUT parameter. For composites, it is also clearly an
3867 -- error to pass a completely uninitialized value as an IN
3868 -- parameter, but the case of IN OUT is trickier. We prefer
3869 -- not to give a warning here. For example, suppose there is
3870 -- a routine that sets some component of a record to False.
3871 -- It is perfectly reasonable to make this IN-OUT and allow
3872 -- either initialized or uninitialized records to be passed
3873 -- in this case.
3875 -- For partially initialized composite values, we also avoid
3876 -- warnings, since it is quite likely that we are passing a
3877 -- partially initialized value and only the initialized fields
3878 -- will in fact be read in the subprogram.
3880 if Is_Scalar_Type (A_Typ)
3881 or else (Ekind (F) = E_In_Parameter
3882 and then not Is_Partially_Initialized_Type (A_Typ))
3883 then
3884 Check_Unset_Reference (A);
3885 end if;
3887 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3888 -- actual to a nested call, since this is case of reading an
3889 -- out parameter, which is not allowed.
3891 if Ada_Version = Ada_83
3892 and then Is_Entity_Name (A)
3893 and then Ekind (Entity (A)) = E_Out_Parameter
3894 then
3895 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3896 end if;
3897 end if;
3899 -- Case of OUT or IN OUT parameter
3901 if Ekind (F) /= E_In_Parameter then
3903 -- For an Out parameter, check for useless assignment. Note
3904 -- that we can't set Last_Assignment this early, because we may
3905 -- kill current values in Resolve_Call, and that call would
3906 -- clobber the Last_Assignment field.
3908 -- Note: call Warn_On_Useless_Assignment before doing the check
3909 -- below for Is_OK_Variable_For_Out_Formal so that the setting
3910 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
3911 -- reflects the last assignment, not this one!
3913 if Ekind (F) = E_Out_Parameter then
3914 if Warn_On_Modified_As_Out_Parameter (F)
3915 and then Is_Entity_Name (A)
3916 and then Present (Entity (A))
3917 and then Comes_From_Source (N)
3918 then
3919 Warn_On_Useless_Assignment (Entity (A), A);
3920 end if;
3921 end if;
3923 -- Validate the form of the actual. Note that the call to
3924 -- Is_OK_Variable_For_Out_Formal generates the required
3925 -- reference in this case.
3927 -- A call to an initialization procedure for an aggregate
3928 -- component may initialize a nested component of a constant
3929 -- designated object. In this context the object is variable.
3931 if not Is_OK_Variable_For_Out_Formal (A)
3932 and then not Is_Init_Proc (Nam)
3933 then
3934 Error_Msg_NE ("actual for& must be a variable", A, F);
3935 end if;
3937 -- What's the following about???
3939 if Is_Entity_Name (A) then
3940 Kill_Checks (Entity (A));
3941 else
3942 Kill_All_Checks;
3943 end if;
3944 end if;
3946 if Etype (A) = Any_Type then
3947 Set_Etype (N, Any_Type);
3948 return;
3949 end if;
3951 -- Apply appropriate range checks for in, out, and in-out
3952 -- parameters. Out and in-out parameters also need a separate
3953 -- check, if there is a type conversion, to make sure the return
3954 -- value meets the constraints of the variable before the
3955 -- conversion.
3957 -- Gigi looks at the check flag and uses the appropriate types.
3958 -- For now since one flag is used there is an optimization which
3959 -- might not be done in the In Out case since Gigi does not do
3960 -- any analysis. More thought required about this ???
3962 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
3964 -- Apply predicate checks, unless this is a call to the
3965 -- predicate check function itself, which would cause an
3966 -- infinite recursion, or it is a call to an initialization
3967 -- procedure whose operand is of course an unfinished object.
3969 if not (Ekind (Nam) = E_Function
3970 and then (Is_Predicate_Function (Nam)
3971 or else
3972 Is_Predicate_Function_M (Nam)))
3973 and then not Is_Init_Proc (Nam)
3974 then
3975 Apply_Predicate_Check (A, F_Typ);
3976 end if;
3978 -- Apply required constraint checks
3980 if Is_Scalar_Type (Etype (A)) then
3981 Apply_Scalar_Range_Check (A, F_Typ);
3983 elsif Is_Array_Type (Etype (A)) then
3984 Apply_Length_Check (A, F_Typ);
3986 elsif Is_Record_Type (F_Typ)
3987 and then Has_Discriminants (F_Typ)
3988 and then Is_Constrained (F_Typ)
3989 and then (not Is_Derived_Type (F_Typ)
3990 or else Comes_From_Source (Nam))
3991 then
3992 Apply_Discriminant_Check (A, F_Typ);
3994 elsif Is_Access_Type (F_Typ)
3995 and then Is_Array_Type (Designated_Type (F_Typ))
3996 and then Is_Constrained (Designated_Type (F_Typ))
3997 then
3998 Apply_Length_Check (A, F_Typ);
4000 elsif Is_Access_Type (F_Typ)
4001 and then Has_Discriminants (Designated_Type (F_Typ))
4002 and then Is_Constrained (Designated_Type (F_Typ))
4003 then
4004 Apply_Discriminant_Check (A, F_Typ);
4006 else
4007 Apply_Range_Check (A, F_Typ);
4008 end if;
4010 -- Ada 2005 (AI-231): Note that the controlling parameter case
4011 -- already existed in Ada 95, which is partially checked
4012 -- elsewhere (see Checks), and we don't want the warning
4013 -- message to differ.
4015 if Is_Access_Type (F_Typ)
4016 and then Can_Never_Be_Null (F_Typ)
4017 and then Known_Null (A)
4018 then
4019 if Is_Controlling_Formal (F) then
4020 Apply_Compile_Time_Constraint_Error
4021 (N => A,
4022 Msg => "null value not allowed here??",
4023 Reason => CE_Access_Check_Failed);
4025 elsif Ada_Version >= Ada_2005 then
4026 Apply_Compile_Time_Constraint_Error
4027 (N => A,
4028 Msg => "(Ada 2005) null not allowed in "
4029 & "null-excluding formal??",
4030 Reason => CE_Null_Not_Allowed);
4031 end if;
4032 end if;
4033 end if;
4035 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
4036 if Nkind (A) = N_Type_Conversion then
4037 if Is_Scalar_Type (A_Typ) then
4038 Apply_Scalar_Range_Check
4039 (Expression (A), Etype (Expression (A)), A_Typ);
4040 else
4041 Apply_Range_Check
4042 (Expression (A), Etype (Expression (A)), A_Typ);
4043 end if;
4045 else
4046 if Is_Scalar_Type (F_Typ) then
4047 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4048 elsif Is_Array_Type (F_Typ)
4049 and then Ekind (F) = E_Out_Parameter
4050 then
4051 Apply_Length_Check (A, F_Typ);
4052 else
4053 Apply_Range_Check (A, A_Typ, F_Typ);
4054 end if;
4055 end if;
4056 end if;
4058 -- An actual associated with an access parameter is implicitly
4059 -- converted to the anonymous access type of the formal and must
4060 -- satisfy the legality checks for access conversions.
4062 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4063 if not Valid_Conversion (A, F_Typ, A) then
4064 Error_Msg_N
4065 ("invalid implicit conversion for access parameter", A);
4066 end if;
4068 -- If the actual is an access selected component of a variable,
4069 -- the call may modify its designated object. It is reasonable
4070 -- to treat this as a potential modification of the enclosing
4071 -- record, to prevent spurious warnings that it should be
4072 -- declared as a constant, because intuitively programmers
4073 -- regard the designated subcomponent as part of the record.
4075 if Nkind (A) = N_Selected_Component
4076 and then Is_Entity_Name (Prefix (A))
4077 and then not Is_Constant_Object (Entity (Prefix (A)))
4078 then
4079 Note_Possible_Modification (A, Sure => False);
4080 end if;
4081 end if;
4083 -- Check bad case of atomic/volatile argument (RM C.6(12))
4085 if Is_By_Reference_Type (Etype (F))
4086 and then Comes_From_Source (N)
4087 then
4088 if Is_Atomic_Object (A)
4089 and then not Is_Atomic (Etype (F))
4090 then
4091 Error_Msg_NE
4092 ("cannot pass atomic argument to non-atomic formal&",
4093 A, F);
4095 elsif Is_Volatile_Object (A)
4096 and then not Is_Volatile (Etype (F))
4097 then
4098 Error_Msg_NE
4099 ("cannot pass volatile argument to non-volatile formal&",
4100 A, F);
4101 end if;
4102 end if;
4104 -- Check that subprograms don't have improper controlling
4105 -- arguments (RM 3.9.2 (9)).
4107 -- A primitive operation may have an access parameter of an
4108 -- incomplete tagged type, but a dispatching call is illegal
4109 -- if the type is still incomplete.
4111 if Is_Controlling_Formal (F) then
4112 Set_Is_Controlling_Actual (A);
4114 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4115 declare
4116 Desig : constant Entity_Id := Designated_Type (Etype (F));
4117 begin
4118 if Ekind (Desig) = E_Incomplete_Type
4119 and then No (Full_View (Desig))
4120 and then No (Non_Limited_View (Desig))
4121 then
4122 Error_Msg_NE
4123 ("premature use of incomplete type& "
4124 & "in dispatching call", A, Desig);
4125 end if;
4126 end;
4127 end if;
4129 elsif Nkind (A) = N_Explicit_Dereference then
4130 Validate_Remote_Access_To_Class_Wide_Type (A);
4131 end if;
4133 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
4134 and then not Is_Class_Wide_Type (F_Typ)
4135 and then not Is_Controlling_Formal (F)
4136 then
4137 Error_Msg_N ("class-wide argument not allowed here!", A);
4139 if Is_Subprogram (Nam)
4140 and then Comes_From_Source (Nam)
4141 then
4142 Error_Msg_Node_2 := F_Typ;
4143 Error_Msg_NE
4144 ("& is not a dispatching operation of &!", A, Nam);
4145 end if;
4147 -- Apply the checks described in 3.10.2(27): if the context is a
4148 -- specific access-to-object, the actual cannot be class-wide.
4149 -- Use base type to exclude access_to_subprogram cases.
4151 elsif Is_Access_Type (A_Typ)
4152 and then Is_Access_Type (F_Typ)
4153 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
4154 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
4155 or else (Nkind (A) = N_Attribute_Reference
4156 and then
4157 Is_Class_Wide_Type (Etype (Prefix (A)))))
4158 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
4159 and then not Is_Controlling_Formal (F)
4161 -- Disable these checks for call to imported C++ subprograms
4163 and then not
4164 (Is_Entity_Name (Name (N))
4165 and then Is_Imported (Entity (Name (N)))
4166 and then Convention (Entity (Name (N))) = Convention_CPP)
4167 then
4168 Error_Msg_N
4169 ("access to class-wide argument not allowed here!", A);
4171 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4172 Error_Msg_Node_2 := Designated_Type (F_Typ);
4173 Error_Msg_NE
4174 ("& is not a dispatching operation of &!", A, Nam);
4175 end if;
4176 end if;
4178 Eval_Actual (A);
4180 -- If it is a named association, treat the selector_name as a
4181 -- proper identifier, and mark the corresponding entity. Ignore
4182 -- this reference in SPARK mode, as it refers to an entity not in
4183 -- scope at the point of reference, so the reference should be
4184 -- ignored for computing effects of subprograms.
4186 if Nkind (Parent (A)) = N_Parameter_Association
4187 and then not SPARK_Mode
4188 then
4189 Set_Entity (Selector_Name (Parent (A)), F);
4190 Generate_Reference (F, Selector_Name (Parent (A)));
4191 Set_Etype (Selector_Name (Parent (A)), F_Typ);
4192 Generate_Reference (F_Typ, N, ' ');
4193 end if;
4195 Prev := A;
4197 if Ekind (F) /= E_Out_Parameter then
4198 Check_Unset_Reference (A);
4199 end if;
4201 Next_Actual (A);
4203 -- Case where actual is not present
4205 else
4206 Insert_Default;
4207 end if;
4209 Next_Formal (F);
4210 end loop;
4211 end Resolve_Actuals;
4213 -----------------------
4214 -- Resolve_Allocator --
4215 -----------------------
4217 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
4218 Desig_T : constant Entity_Id := Designated_Type (Typ);
4219 E : constant Node_Id := Expression (N);
4220 Subtyp : Entity_Id;
4221 Discrim : Entity_Id;
4222 Constr : Node_Id;
4223 Aggr : Node_Id;
4224 Assoc : Node_Id := Empty;
4225 Disc_Exp : Node_Id;
4227 procedure Check_Allocator_Discrim_Accessibility
4228 (Disc_Exp : Node_Id;
4229 Alloc_Typ : Entity_Id);
4230 -- Check that accessibility level associated with an access discriminant
4231 -- initialized in an allocator by the expression Disc_Exp is not deeper
4232 -- than the level of the allocator type Alloc_Typ. An error message is
4233 -- issued if this condition is violated. Specialized checks are done for
4234 -- the cases of a constraint expression which is an access attribute or
4235 -- an access discriminant.
4237 function In_Dispatching_Context return Boolean;
4238 -- If the allocator is an actual in a call, it is allowed to be class-
4239 -- wide when the context is not because it is a controlling actual.
4241 -------------------------------------------
4242 -- Check_Allocator_Discrim_Accessibility --
4243 -------------------------------------------
4245 procedure Check_Allocator_Discrim_Accessibility
4246 (Disc_Exp : Node_Id;
4247 Alloc_Typ : Entity_Id)
4249 begin
4250 if Type_Access_Level (Etype (Disc_Exp)) >
4251 Deepest_Type_Access_Level (Alloc_Typ)
4252 then
4253 Error_Msg_N
4254 ("operand type has deeper level than allocator type", Disc_Exp);
4256 -- When the expression is an Access attribute the level of the prefix
4257 -- object must not be deeper than that of the allocator's type.
4259 elsif Nkind (Disc_Exp) = N_Attribute_Reference
4260 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
4261 Attribute_Access
4262 and then Object_Access_Level (Prefix (Disc_Exp)) >
4263 Deepest_Type_Access_Level (Alloc_Typ)
4264 then
4265 Error_Msg_N
4266 ("prefix of attribute has deeper level than allocator type",
4267 Disc_Exp);
4269 -- When the expression is an access discriminant the check is against
4270 -- the level of the prefix object.
4272 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
4273 and then Nkind (Disc_Exp) = N_Selected_Component
4274 and then Object_Access_Level (Prefix (Disc_Exp)) >
4275 Deepest_Type_Access_Level (Alloc_Typ)
4276 then
4277 Error_Msg_N
4278 ("access discriminant has deeper level than allocator type",
4279 Disc_Exp);
4281 -- All other cases are legal
4283 else
4284 null;
4285 end if;
4286 end Check_Allocator_Discrim_Accessibility;
4288 ----------------------------
4289 -- In_Dispatching_Context --
4290 ----------------------------
4292 function In_Dispatching_Context return Boolean is
4293 Par : constant Node_Id := Parent (N);
4295 begin
4296 return Nkind (Par) in N_Subprogram_Call
4297 and then Is_Entity_Name (Name (Par))
4298 and then Is_Dispatching_Operation (Entity (Name (Par)));
4299 end In_Dispatching_Context;
4301 -- Start of processing for Resolve_Allocator
4303 begin
4304 -- Replace general access with specific type
4306 if Ekind (Etype (N)) = E_Allocator_Type then
4307 Set_Etype (N, Base_Type (Typ));
4308 end if;
4310 if Is_Abstract_Type (Typ) then
4311 Error_Msg_N ("type of allocator cannot be abstract", N);
4312 end if;
4314 -- For qualified expression, resolve the expression using the given
4315 -- subtype (nothing to do for type mark, subtype indication)
4317 if Nkind (E) = N_Qualified_Expression then
4318 if Is_Class_Wide_Type (Etype (E))
4319 and then not Is_Class_Wide_Type (Desig_T)
4320 and then not In_Dispatching_Context
4321 then
4322 Error_Msg_N
4323 ("class-wide allocator not allowed for this access type", N);
4324 end if;
4326 Resolve (Expression (E), Etype (E));
4327 Check_Unset_Reference (Expression (E));
4329 -- A qualified expression requires an exact match of the type.
4330 -- Class-wide matching is not allowed.
4332 if (Is_Class_Wide_Type (Etype (Expression (E)))
4333 or else Is_Class_Wide_Type (Etype (E)))
4334 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
4335 then
4336 Wrong_Type (Expression (E), Etype (E));
4337 end if;
4339 -- Calls to build-in-place functions are not currently supported in
4340 -- allocators for access types associated with a simple storage pool.
4341 -- Supporting such allocators may require passing additional implicit
4342 -- parameters to build-in-place functions (or a significant revision
4343 -- of the current b-i-p implementation to unify the handling for
4344 -- multiple kinds of storage pools). ???
4346 if Is_Immutably_Limited_Type (Desig_T)
4347 and then Nkind (Expression (E)) = N_Function_Call
4348 then
4349 declare
4350 Pool : constant Entity_Id :=
4351 Associated_Storage_Pool (Root_Type (Typ));
4352 begin
4353 if Present (Pool)
4354 and then
4355 Present (Get_Rep_Pragma
4356 (Etype (Pool), Name_Simple_Storage_Pool_Type))
4357 then
4358 Error_Msg_N
4359 ("limited function calls not yet supported in simple "
4360 & "storage pool allocators", Expression (E));
4361 end if;
4362 end;
4363 end if;
4365 -- A special accessibility check is needed for allocators that
4366 -- constrain access discriminants. The level of the type of the
4367 -- expression used to constrain an access discriminant cannot be
4368 -- deeper than the type of the allocator (in contrast to access
4369 -- parameters, where the level of the actual can be arbitrary).
4371 -- We can't use Valid_Conversion to perform this check because in
4372 -- general the type of the allocator is unrelated to the type of
4373 -- the access discriminant.
4375 if Ekind (Typ) /= E_Anonymous_Access_Type
4376 or else Is_Local_Anonymous_Access (Typ)
4377 then
4378 Subtyp := Entity (Subtype_Mark (E));
4380 Aggr := Original_Node (Expression (E));
4382 if Has_Discriminants (Subtyp)
4383 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
4384 then
4385 Discrim := First_Discriminant (Base_Type (Subtyp));
4387 -- Get the first component expression of the aggregate
4389 if Present (Expressions (Aggr)) then
4390 Disc_Exp := First (Expressions (Aggr));
4392 elsif Present (Component_Associations (Aggr)) then
4393 Assoc := First (Component_Associations (Aggr));
4395 if Present (Assoc) then
4396 Disc_Exp := Expression (Assoc);
4397 else
4398 Disc_Exp := Empty;
4399 end if;
4401 else
4402 Disc_Exp := Empty;
4403 end if;
4405 while Present (Discrim) and then Present (Disc_Exp) loop
4406 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4407 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4408 end if;
4410 Next_Discriminant (Discrim);
4412 if Present (Discrim) then
4413 if Present (Assoc) then
4414 Next (Assoc);
4415 Disc_Exp := Expression (Assoc);
4417 elsif Present (Next (Disc_Exp)) then
4418 Next (Disc_Exp);
4420 else
4421 Assoc := First (Component_Associations (Aggr));
4423 if Present (Assoc) then
4424 Disc_Exp := Expression (Assoc);
4425 else
4426 Disc_Exp := Empty;
4427 end if;
4428 end if;
4429 end if;
4430 end loop;
4431 end if;
4432 end if;
4434 -- For a subtype mark or subtype indication, freeze the subtype
4436 else
4437 Freeze_Expression (E);
4439 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
4440 Error_Msg_N
4441 ("initialization required for access-to-constant allocator", N);
4442 end if;
4444 -- A special accessibility check is needed for allocators that
4445 -- constrain access discriminants. The level of the type of the
4446 -- expression used to constrain an access discriminant cannot be
4447 -- deeper than the type of the allocator (in contrast to access
4448 -- parameters, where the level of the actual can be arbitrary).
4449 -- We can't use Valid_Conversion to perform this check because
4450 -- in general the type of the allocator is unrelated to the type
4451 -- of the access discriminant.
4453 if Nkind (Original_Node (E)) = N_Subtype_Indication
4454 and then (Ekind (Typ) /= E_Anonymous_Access_Type
4455 or else Is_Local_Anonymous_Access (Typ))
4456 then
4457 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
4459 if Has_Discriminants (Subtyp) then
4460 Discrim := First_Discriminant (Base_Type (Subtyp));
4461 Constr := First (Constraints (Constraint (Original_Node (E))));
4462 while Present (Discrim) and then Present (Constr) loop
4463 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4464 if Nkind (Constr) = N_Discriminant_Association then
4465 Disc_Exp := Original_Node (Expression (Constr));
4466 else
4467 Disc_Exp := Original_Node (Constr);
4468 end if;
4470 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4471 end if;
4473 Next_Discriminant (Discrim);
4474 Next (Constr);
4475 end loop;
4476 end if;
4477 end if;
4478 end if;
4480 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4481 -- check that the level of the type of the created object is not deeper
4482 -- than the level of the allocator's access type, since extensions can
4483 -- now occur at deeper levels than their ancestor types. This is a
4484 -- static accessibility level check; a run-time check is also needed in
4485 -- the case of an initialized allocator with a class-wide argument (see
4486 -- Expand_Allocator_Expression).
4488 if Ada_Version >= Ada_2005
4489 and then Is_Class_Wide_Type (Desig_T)
4490 then
4491 declare
4492 Exp_Typ : Entity_Id;
4494 begin
4495 if Nkind (E) = N_Qualified_Expression then
4496 Exp_Typ := Etype (E);
4497 elsif Nkind (E) = N_Subtype_Indication then
4498 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4499 else
4500 Exp_Typ := Entity (E);
4501 end if;
4503 if Type_Access_Level (Exp_Typ) >
4504 Deepest_Type_Access_Level (Typ)
4505 then
4506 if In_Instance_Body then
4507 Error_Msg_N
4508 ("??type in allocator has deeper level than "
4509 & "designated class-wide type", E);
4510 Error_Msg_N
4511 ("\??Program_Error will be raised at run time", E);
4512 Rewrite (N,
4513 Make_Raise_Program_Error (Sloc (N),
4514 Reason => PE_Accessibility_Check_Failed));
4515 Set_Etype (N, Typ);
4517 -- Do not apply Ada 2005 accessibility checks on a class-wide
4518 -- allocator if the type given in the allocator is a formal
4519 -- type. A run-time check will be performed in the instance.
4521 elsif not Is_Generic_Type (Exp_Typ) then
4522 Error_Msg_N ("type in allocator has deeper level than "
4523 & "designated class-wide type", E);
4524 end if;
4525 end if;
4526 end;
4527 end if;
4529 -- Check for allocation from an empty storage pool
4531 if No_Pool_Assigned (Typ) then
4532 Error_Msg_N ("allocation from empty storage pool!", N);
4534 -- If the context is an unchecked conversion, as may happen within an
4535 -- inlined subprogram, the allocator is being resolved with its own
4536 -- anonymous type. In that case, if the target type has a specific
4537 -- storage pool, it must be inherited explicitly by the allocator type.
4539 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4540 and then No (Associated_Storage_Pool (Typ))
4541 then
4542 Set_Associated_Storage_Pool
4543 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4544 end if;
4546 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
4547 Check_Restriction (No_Anonymous_Allocators, N);
4548 end if;
4550 -- Check that an allocator with task parts isn't for a nested access
4551 -- type when restriction No_Task_Hierarchy applies.
4553 if not Is_Library_Level_Entity (Base_Type (Typ))
4554 and then Has_Task (Base_Type (Desig_T))
4555 then
4556 Check_Restriction (No_Task_Hierarchy, N);
4557 end if;
4559 -- An erroneous allocator may be rewritten as a raise Program_Error
4560 -- statement.
4562 if Nkind (N) = N_Allocator then
4564 -- An anonymous access discriminant is the definition of a
4565 -- coextension.
4567 if Ekind (Typ) = E_Anonymous_Access_Type
4568 and then Nkind (Associated_Node_For_Itype (Typ)) =
4569 N_Discriminant_Specification
4570 then
4571 declare
4572 Discr : constant Entity_Id :=
4573 Defining_Identifier (Associated_Node_For_Itype (Typ));
4575 begin
4576 Check_Restriction (No_Coextensions, N);
4578 -- Ada 2012 AI05-0052: If the designated type of the allocator
4579 -- is limited, then the allocator shall not be used to define
4580 -- the value of an access discriminant unless the discriminated
4581 -- type is immutably limited.
4583 if Ada_Version >= Ada_2012
4584 and then Is_Limited_Type (Desig_T)
4585 and then not Is_Immutably_Limited_Type (Scope (Discr))
4586 then
4587 Error_Msg_N
4588 ("only immutably limited types can have anonymous "
4589 & "access discriminants designating a limited type", N);
4590 end if;
4591 end;
4593 -- Avoid marking an allocator as a dynamic coextension if it is
4594 -- within a static construct.
4596 if not Is_Static_Coextension (N) then
4597 Set_Is_Dynamic_Coextension (N);
4598 end if;
4600 -- Cleanup for potential static coextensions
4602 else
4603 Set_Is_Dynamic_Coextension (N, False);
4604 Set_Is_Static_Coextension (N, False);
4605 end if;
4606 end if;
4608 -- Report a simple error: if the designated object is a local task,
4609 -- its body has not been seen yet, and its activation will fail an
4610 -- elaboration check.
4612 if Is_Task_Type (Desig_T)
4613 and then Scope (Base_Type (Desig_T)) = Current_Scope
4614 and then Is_Compilation_Unit (Current_Scope)
4615 and then Ekind (Current_Scope) = E_Package
4616 and then not In_Package_Body (Current_Scope)
4617 then
4618 Error_Msg_N ("??cannot activate task before body seen", N);
4619 Error_Msg_N ("\??Program_Error will be raised at run time", N);
4620 end if;
4622 -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a
4623 -- type with a task component on a subpool. This action must raise
4624 -- Program_Error at runtime.
4626 if Ada_Version >= Ada_2012
4627 and then Nkind (N) = N_Allocator
4628 and then Present (Subpool_Handle_Name (N))
4629 and then Has_Task (Desig_T)
4630 then
4631 Error_Msg_N ("??cannot allocate task on subpool", N);
4632 Error_Msg_N ("\??Program_Error will be raised at run time", N);
4634 Rewrite (N,
4635 Make_Raise_Program_Error (Sloc (N),
4636 Reason => PE_Explicit_Raise));
4637 Set_Etype (N, Typ);
4638 end if;
4639 end Resolve_Allocator;
4641 ---------------------------
4642 -- Resolve_Arithmetic_Op --
4643 ---------------------------
4645 -- Used for resolving all arithmetic operators except exponentiation
4647 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
4648 L : constant Node_Id := Left_Opnd (N);
4649 R : constant Node_Id := Right_Opnd (N);
4650 TL : constant Entity_Id := Base_Type (Etype (L));
4651 TR : constant Entity_Id := Base_Type (Etype (R));
4652 T : Entity_Id;
4653 Rop : Node_Id;
4655 B_Typ : constant Entity_Id := Base_Type (Typ);
4656 -- We do the resolution using the base type, because intermediate values
4657 -- in expressions always are of the base type, not a subtype of it.
4659 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4660 -- Returns True if N is in a context that expects "any real type"
4662 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4663 -- Return True iff given type is Integer or universal real/integer
4665 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4666 -- Choose type of integer literal in fixed-point operation to conform
4667 -- to available fixed-point type. T is the type of the other operand,
4668 -- which is needed to determine the expected type of N.
4670 procedure Set_Operand_Type (N : Node_Id);
4671 -- Set operand type to T if universal
4673 -------------------------------
4674 -- Expected_Type_Is_Any_Real --
4675 -------------------------------
4677 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4678 begin
4679 -- N is the expression after "delta" in a fixed_point_definition;
4680 -- see RM-3.5.9(6):
4682 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4683 N_Decimal_Fixed_Point_Definition,
4685 -- N is one of the bounds in a real_range_specification;
4686 -- see RM-3.5.7(5):
4688 N_Real_Range_Specification,
4690 -- N is the expression of a delta_constraint;
4691 -- see RM-J.3(3):
4693 N_Delta_Constraint);
4694 end Expected_Type_Is_Any_Real;
4696 -----------------------------
4697 -- Is_Integer_Or_Universal --
4698 -----------------------------
4700 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4701 T : Entity_Id;
4702 Index : Interp_Index;
4703 It : Interp;
4705 begin
4706 if not Is_Overloaded (N) then
4707 T := Etype (N);
4708 return Base_Type (T) = Base_Type (Standard_Integer)
4709 or else T = Universal_Integer
4710 or else T = Universal_Real;
4711 else
4712 Get_First_Interp (N, Index, It);
4713 while Present (It.Typ) loop
4714 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4715 or else It.Typ = Universal_Integer
4716 or else It.Typ = Universal_Real
4717 then
4718 return True;
4719 end if;
4721 Get_Next_Interp (Index, It);
4722 end loop;
4723 end if;
4725 return False;
4726 end Is_Integer_Or_Universal;
4728 ----------------------------
4729 -- Set_Mixed_Mode_Operand --
4730 ----------------------------
4732 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4733 Index : Interp_Index;
4734 It : Interp;
4736 begin
4737 if Universal_Interpretation (N) = Universal_Integer then
4739 -- A universal integer literal is resolved as standard integer
4740 -- except in the case of a fixed-point result, where we leave it
4741 -- as universal (to be handled by Exp_Fixd later on)
4743 if Is_Fixed_Point_Type (T) then
4744 Resolve (N, Universal_Integer);
4745 else
4746 Resolve (N, Standard_Integer);
4747 end if;
4749 elsif Universal_Interpretation (N) = Universal_Real
4750 and then (T = Base_Type (Standard_Integer)
4751 or else T = Universal_Integer
4752 or else T = Universal_Real)
4753 then
4754 -- A universal real can appear in a fixed-type context. We resolve
4755 -- the literal with that context, even though this might raise an
4756 -- exception prematurely (the other operand may be zero).
4758 Resolve (N, B_Typ);
4760 elsif Etype (N) = Base_Type (Standard_Integer)
4761 and then T = Universal_Real
4762 and then Is_Overloaded (N)
4763 then
4764 -- Integer arg in mixed-mode operation. Resolve with universal
4765 -- type, in case preference rule must be applied.
4767 Resolve (N, Universal_Integer);
4769 elsif Etype (N) = T
4770 and then B_Typ /= Universal_Fixed
4771 then
4772 -- Not a mixed-mode operation, resolve with context
4774 Resolve (N, B_Typ);
4776 elsif Etype (N) = Any_Fixed then
4778 -- N may itself be a mixed-mode operation, so use context type
4780 Resolve (N, B_Typ);
4782 elsif Is_Fixed_Point_Type (T)
4783 and then B_Typ = Universal_Fixed
4784 and then Is_Overloaded (N)
4785 then
4786 -- Must be (fixed * fixed) operation, operand must have one
4787 -- compatible interpretation.
4789 Resolve (N, Any_Fixed);
4791 elsif Is_Fixed_Point_Type (B_Typ)
4792 and then (T = Universal_Real or else Is_Fixed_Point_Type (T))
4793 and then Is_Overloaded (N)
4794 then
4795 -- C * F(X) in a fixed context, where C is a real literal or a
4796 -- fixed-point expression. F must have either a fixed type
4797 -- interpretation or an integer interpretation, but not both.
4799 Get_First_Interp (N, Index, It);
4800 while Present (It.Typ) loop
4801 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
4802 if Analyzed (N) then
4803 Error_Msg_N ("ambiguous operand in fixed operation", N);
4804 else
4805 Resolve (N, Standard_Integer);
4806 end if;
4808 elsif Is_Fixed_Point_Type (It.Typ) then
4809 if Analyzed (N) then
4810 Error_Msg_N ("ambiguous operand in fixed operation", N);
4811 else
4812 Resolve (N, It.Typ);
4813 end if;
4814 end if;
4816 Get_Next_Interp (Index, It);
4817 end loop;
4819 -- Reanalyze the literal with the fixed type of the context. If
4820 -- context is Universal_Fixed, we are within a conversion, leave
4821 -- the literal as a universal real because there is no usable
4822 -- fixed type, and the target of the conversion plays no role in
4823 -- the resolution.
4825 declare
4826 Op2 : Node_Id;
4827 T2 : Entity_Id;
4829 begin
4830 if N = L then
4831 Op2 := R;
4832 else
4833 Op2 := L;
4834 end if;
4836 if B_Typ = Universal_Fixed
4837 and then Nkind (Op2) = N_Real_Literal
4838 then
4839 T2 := Universal_Real;
4840 else
4841 T2 := B_Typ;
4842 end if;
4844 Set_Analyzed (Op2, False);
4845 Resolve (Op2, T2);
4846 end;
4848 else
4849 Resolve (N);
4850 end if;
4851 end Set_Mixed_Mode_Operand;
4853 ----------------------
4854 -- Set_Operand_Type --
4855 ----------------------
4857 procedure Set_Operand_Type (N : Node_Id) is
4858 begin
4859 if Etype (N) = Universal_Integer
4860 or else Etype (N) = Universal_Real
4861 then
4862 Set_Etype (N, T);
4863 end if;
4864 end Set_Operand_Type;
4866 -- Start of processing for Resolve_Arithmetic_Op
4868 begin
4869 if Comes_From_Source (N)
4870 and then Ekind (Entity (N)) = E_Function
4871 and then Is_Imported (Entity (N))
4872 and then Is_Intrinsic_Subprogram (Entity (N))
4873 then
4874 Resolve_Intrinsic_Operator (N, Typ);
4875 return;
4877 -- Special-case for mixed-mode universal expressions or fixed point type
4878 -- operation: each argument is resolved separately. The same treatment
4879 -- is required if one of the operands of a fixed point operation is
4880 -- universal real, since in this case we don't do a conversion to a
4881 -- specific fixed-point type (instead the expander handles the case).
4883 -- Set the type of the node to its universal interpretation because
4884 -- legality checks on an exponentiation operand need the context.
4886 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
4887 and then Present (Universal_Interpretation (L))
4888 and then Present (Universal_Interpretation (R))
4889 then
4890 Set_Etype (N, B_Typ);
4891 Resolve (L, Universal_Interpretation (L));
4892 Resolve (R, Universal_Interpretation (R));
4894 elsif (B_Typ = Universal_Real
4895 or else Etype (N) = Universal_Fixed
4896 or else (Etype (N) = Any_Fixed
4897 and then Is_Fixed_Point_Type (B_Typ))
4898 or else (Is_Fixed_Point_Type (B_Typ)
4899 and then (Is_Integer_Or_Universal (L)
4900 or else
4901 Is_Integer_Or_Universal (R))))
4902 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4903 then
4904 if TL = Universal_Integer or else TR = Universal_Integer then
4905 Check_For_Visible_Operator (N, B_Typ);
4906 end if;
4908 -- If context is a fixed type and one operand is integer, the other
4909 -- is resolved with the type of the context.
4911 if Is_Fixed_Point_Type (B_Typ)
4912 and then (Base_Type (TL) = Base_Type (Standard_Integer)
4913 or else TL = Universal_Integer)
4914 then
4915 Resolve (R, B_Typ);
4916 Resolve (L, TL);
4918 elsif Is_Fixed_Point_Type (B_Typ)
4919 and then (Base_Type (TR) = Base_Type (Standard_Integer)
4920 or else TR = Universal_Integer)
4921 then
4922 Resolve (L, B_Typ);
4923 Resolve (R, TR);
4925 else
4926 Set_Mixed_Mode_Operand (L, TR);
4927 Set_Mixed_Mode_Operand (R, TL);
4928 end if;
4930 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
4931 -- multiplying operators from being used when the expected type is
4932 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
4933 -- some cases where the expected type is actually Any_Real;
4934 -- Expected_Type_Is_Any_Real takes care of that case.
4936 if Etype (N) = Universal_Fixed
4937 or else Etype (N) = Any_Fixed
4938 then
4939 if B_Typ = Universal_Fixed
4940 and then not Expected_Type_Is_Any_Real (N)
4941 and then not Nkind_In (Parent (N), N_Type_Conversion,
4942 N_Unchecked_Type_Conversion)
4943 then
4944 Error_Msg_N ("type cannot be determined from context!", N);
4945 Error_Msg_N ("\explicit conversion to result type required", N);
4947 Set_Etype (L, Any_Type);
4948 Set_Etype (R, Any_Type);
4950 else
4951 if Ada_Version = Ada_83
4952 and then Etype (N) = Universal_Fixed
4953 and then not
4954 Nkind_In (Parent (N), N_Type_Conversion,
4955 N_Unchecked_Type_Conversion)
4956 then
4957 Error_Msg_N
4958 ("(Ada 83) fixed-point operation "
4959 & "needs explicit conversion", N);
4960 end if;
4962 -- The expected type is "any real type" in contexts like
4964 -- type T is delta <universal_fixed-expression> ...
4966 -- in which case we need to set the type to Universal_Real
4967 -- so that static expression evaluation will work properly.
4969 if Expected_Type_Is_Any_Real (N) then
4970 Set_Etype (N, Universal_Real);
4971 else
4972 Set_Etype (N, B_Typ);
4973 end if;
4974 end if;
4976 elsif Is_Fixed_Point_Type (B_Typ)
4977 and then (Is_Integer_Or_Universal (L)
4978 or else Nkind (L) = N_Real_Literal
4979 or else Nkind (R) = N_Real_Literal
4980 or else Is_Integer_Or_Universal (R))
4981 then
4982 Set_Etype (N, B_Typ);
4984 elsif Etype (N) = Any_Fixed then
4986 -- If no previous errors, this is only possible if one operand is
4987 -- overloaded and the context is universal. Resolve as such.
4989 Set_Etype (N, B_Typ);
4990 end if;
4992 else
4993 if (TL = Universal_Integer or else TL = Universal_Real)
4994 and then
4995 (TR = Universal_Integer or else TR = Universal_Real)
4996 then
4997 Check_For_Visible_Operator (N, B_Typ);
4998 end if;
5000 -- If the context is Universal_Fixed and the operands are also
5001 -- universal fixed, this is an error, unless there is only one
5002 -- applicable fixed_point type (usually Duration).
5004 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
5005 T := Unique_Fixed_Point_Type (N);
5007 if T = Any_Type then
5008 Set_Etype (N, T);
5009 return;
5010 else
5011 Resolve (L, T);
5012 Resolve (R, T);
5013 end if;
5015 else
5016 Resolve (L, B_Typ);
5017 Resolve (R, B_Typ);
5018 end if;
5020 -- If one of the arguments was resolved to a non-universal type.
5021 -- label the result of the operation itself with the same type.
5022 -- Do the same for the universal argument, if any.
5024 T := Intersect_Types (L, R);
5025 Set_Etype (N, Base_Type (T));
5026 Set_Operand_Type (L);
5027 Set_Operand_Type (R);
5028 end if;
5030 Generate_Operator_Reference (N, Typ);
5031 Analyze_Dimension (N);
5032 Eval_Arithmetic_Op (N);
5034 -- In SPARK, a multiplication or division with operands of fixed point
5035 -- types shall be qualified or explicitly converted to identify the
5036 -- result type.
5038 if (Is_Fixed_Point_Type (Etype (L))
5039 or else Is_Fixed_Point_Type (Etype (R)))
5040 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
5041 and then
5042 not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion)
5043 then
5044 Check_SPARK_Restriction
5045 ("operation should be qualified or explicitly converted", N);
5046 end if;
5048 -- Set overflow and division checking bit
5050 if Nkind (N) in N_Op then
5051 if not Overflow_Checks_Suppressed (Etype (N)) then
5052 Enable_Overflow_Check (N);
5053 end if;
5055 -- Give warning if explicit division by zero
5057 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
5058 and then not Division_Checks_Suppressed (Etype (N))
5059 then
5060 Rop := Right_Opnd (N);
5062 if Compile_Time_Known_Value (Rop)
5063 and then ((Is_Integer_Type (Etype (Rop))
5064 and then Expr_Value (Rop) = Uint_0)
5065 or else
5066 (Is_Real_Type (Etype (Rop))
5067 and then Expr_Value_R (Rop) = Ureal_0))
5068 then
5069 -- Specialize the warning message according to the operation.
5070 -- The following warnings are for the case
5072 case Nkind (N) is
5073 when N_Op_Divide =>
5075 -- For division, we have two cases, for float division
5076 -- of an unconstrained float type, on a machine where
5077 -- Machine_Overflows is false, we don't get an exception
5078 -- at run-time, but rather an infinity or Nan. The Nan
5079 -- case is pretty obscure, so just warn about infinities.
5081 if Is_Floating_Point_Type (Typ)
5082 and then not Is_Constrained (Typ)
5083 and then not Machine_Overflows_On_Target
5084 then
5085 Error_Msg_N
5086 ("float division by zero, may generate "
5087 & "'+'/'- infinity??", Right_Opnd (N));
5089 -- For all other cases, we get a Constraint_Error
5091 else
5092 Apply_Compile_Time_Constraint_Error
5093 (N, "division by zero??", CE_Divide_By_Zero,
5094 Loc => Sloc (Right_Opnd (N)));
5095 end if;
5097 when N_Op_Rem =>
5098 Apply_Compile_Time_Constraint_Error
5099 (N, "rem with zero divisor??", CE_Divide_By_Zero,
5100 Loc => Sloc (Right_Opnd (N)));
5102 when N_Op_Mod =>
5103 Apply_Compile_Time_Constraint_Error
5104 (N, "mod with zero divisor??", CE_Divide_By_Zero,
5105 Loc => Sloc (Right_Opnd (N)));
5107 -- Division by zero can only happen with division, rem,
5108 -- and mod operations.
5110 when others =>
5111 raise Program_Error;
5112 end case;
5114 -- Otherwise just set the flag to check at run time
5116 else
5117 Activate_Division_Check (N);
5118 end if;
5119 end if;
5121 -- If Restriction No_Implicit_Conditionals is active, then it is
5122 -- violated if either operand can be negative for mod, or for rem
5123 -- if both operands can be negative.
5125 if Restriction_Check_Required (No_Implicit_Conditionals)
5126 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
5127 then
5128 declare
5129 Lo : Uint;
5130 Hi : Uint;
5131 OK : Boolean;
5133 LNeg : Boolean;
5134 RNeg : Boolean;
5135 -- Set if corresponding operand might be negative
5137 begin
5138 Determine_Range
5139 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
5140 LNeg := (not OK) or else Lo < 0;
5142 Determine_Range
5143 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
5144 RNeg := (not OK) or else Lo < 0;
5146 -- Check if we will be generating conditionals. There are two
5147 -- cases where that can happen, first for REM, the only case
5148 -- is largest negative integer mod -1, where the division can
5149 -- overflow, but we still have to give the right result. The
5150 -- front end generates a test for this annoying case. Here we
5151 -- just test if both operands can be negative (that's what the
5152 -- expander does, so we match its logic here).
5154 -- The second case is mod where either operand can be negative.
5155 -- In this case, the back end has to generate additional tests.
5157 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
5158 or else
5159 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
5160 then
5161 Check_Restriction (No_Implicit_Conditionals, N);
5162 end if;
5163 end;
5164 end if;
5165 end if;
5167 Check_Unset_Reference (L);
5168 Check_Unset_Reference (R);
5169 Check_Function_Writable_Actuals (N);
5170 end Resolve_Arithmetic_Op;
5172 ------------------
5173 -- Resolve_Call --
5174 ------------------
5176 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
5177 Loc : constant Source_Ptr := Sloc (N);
5178 Subp : constant Node_Id := Name (N);
5179 Nam : Entity_Id;
5180 I : Interp_Index;
5181 It : Interp;
5182 Norm_OK : Boolean;
5183 Scop : Entity_Id;
5184 Rtype : Entity_Id;
5186 function Same_Or_Aliased_Subprograms
5187 (S : Entity_Id;
5188 E : Entity_Id) return Boolean;
5189 -- Returns True if the subprogram entity S is the same as E or else
5190 -- S is an alias of E.
5192 ---------------------------------
5193 -- Same_Or_Aliased_Subprograms --
5194 ---------------------------------
5196 function Same_Or_Aliased_Subprograms
5197 (S : Entity_Id;
5198 E : Entity_Id) return Boolean
5200 Subp_Alias : constant Entity_Id := Alias (S);
5201 begin
5202 return S = E
5203 or else (Present (Subp_Alias) and then Subp_Alias = E);
5204 end Same_Or_Aliased_Subprograms;
5206 -- Start of processing for Resolve_Call
5208 begin
5209 -- The context imposes a unique interpretation with type Typ on a
5210 -- procedure or function call. Find the entity of the subprogram that
5211 -- yields the expected type, and propagate the corresponding formal
5212 -- constraints on the actuals. The caller has established that an
5213 -- interpretation exists, and emitted an error if not unique.
5215 -- First deal with the case of a call to an access-to-subprogram,
5216 -- dereference made explicit in Analyze_Call.
5218 if Ekind (Etype (Subp)) = E_Subprogram_Type then
5219 if not Is_Overloaded (Subp) then
5220 Nam := Etype (Subp);
5222 else
5223 -- Find the interpretation whose type (a subprogram type) has a
5224 -- return type that is compatible with the context. Analysis of
5225 -- the node has established that one exists.
5227 Nam := Empty;
5229 Get_First_Interp (Subp, I, It);
5230 while Present (It.Typ) loop
5231 if Covers (Typ, Etype (It.Typ)) then
5232 Nam := It.Typ;
5233 exit;
5234 end if;
5236 Get_Next_Interp (I, It);
5237 end loop;
5239 if No (Nam) then
5240 raise Program_Error;
5241 end if;
5242 end if;
5244 -- If the prefix is not an entity, then resolve it
5246 if not Is_Entity_Name (Subp) then
5247 Resolve (Subp, Nam);
5248 end if;
5250 -- For an indirect call, we always invalidate checks, since we do not
5251 -- know whether the subprogram is local or global. Yes we could do
5252 -- better here, e.g. by knowing that there are no local subprograms,
5253 -- but it does not seem worth the effort. Similarly, we kill all
5254 -- knowledge of current constant values.
5256 Kill_Current_Values;
5258 -- If this is a procedure call which is really an entry call, do
5259 -- the conversion of the procedure call to an entry call. Protected
5260 -- operations use the same circuitry because the name in the call
5261 -- can be an arbitrary expression with special resolution rules.
5263 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
5264 or else (Is_Entity_Name (Subp)
5265 and then Ekind (Entity (Subp)) = E_Entry)
5266 then
5267 Resolve_Entry_Call (N, Typ);
5268 Check_Elab_Call (N);
5270 -- Kill checks and constant values, as above for indirect case
5271 -- Who knows what happens when another task is activated?
5273 Kill_Current_Values;
5274 return;
5276 -- Normal subprogram call with name established in Resolve
5278 elsif not (Is_Type (Entity (Subp))) then
5279 Nam := Entity (Subp);
5280 Set_Entity_With_Style_Check (Subp, Nam);
5282 -- Otherwise we must have the case of an overloaded call
5284 else
5285 pragma Assert (Is_Overloaded (Subp));
5287 -- Initialize Nam to prevent warning (we know it will be assigned
5288 -- in the loop below, but the compiler does not know that).
5290 Nam := Empty;
5292 Get_First_Interp (Subp, I, It);
5293 while Present (It.Typ) loop
5294 if Covers (Typ, It.Typ) then
5295 Nam := It.Nam;
5296 Set_Entity_With_Style_Check (Subp, Nam);
5297 exit;
5298 end if;
5300 Get_Next_Interp (I, It);
5301 end loop;
5302 end if;
5304 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
5305 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
5306 and then Nkind (Subp) /= N_Explicit_Dereference
5307 and then Present (Parameter_Associations (N))
5308 then
5309 -- The prefix is a parameterless function call that returns an access
5310 -- to subprogram. If parameters are present in the current call, add
5311 -- add an explicit dereference. We use the base type here because
5312 -- within an instance these may be subtypes.
5314 -- The dereference is added either in Analyze_Call or here. Should
5315 -- be consolidated ???
5317 Set_Is_Overloaded (Subp, False);
5318 Set_Etype (Subp, Etype (Nam));
5319 Insert_Explicit_Dereference (Subp);
5320 Nam := Designated_Type (Etype (Nam));
5321 Resolve (Subp, Nam);
5322 end if;
5324 -- Check that a call to Current_Task does not occur in an entry body
5326 if Is_RTE (Nam, RE_Current_Task) then
5327 declare
5328 P : Node_Id;
5330 begin
5331 P := N;
5332 loop
5333 P := Parent (P);
5335 -- Exclude calls that occur within the default of a formal
5336 -- parameter of the entry, since those are evaluated outside
5337 -- of the body.
5339 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
5341 if Nkind (P) = N_Entry_Body
5342 or else (Nkind (P) = N_Subprogram_Body
5343 and then Is_Entry_Barrier_Function (P))
5344 then
5345 Rtype := Etype (N);
5346 Error_Msg_NE
5347 ("??& should not be used in entry body (RM C.7(17))",
5348 N, Nam);
5349 Error_Msg_NE
5350 ("\Program_Error will be raised at run time??", N, Nam);
5351 Rewrite (N,
5352 Make_Raise_Program_Error (Loc,
5353 Reason => PE_Current_Task_In_Entry_Body));
5354 Set_Etype (N, Rtype);
5355 return;
5356 end if;
5357 end loop;
5358 end;
5359 end if;
5361 -- Check that a procedure call does not occur in the context of the
5362 -- entry call statement of a conditional or timed entry call. Note that
5363 -- the case of a call to a subprogram renaming of an entry will also be
5364 -- rejected. The test for N not being an N_Entry_Call_Statement is
5365 -- defensive, covering the possibility that the processing of entry
5366 -- calls might reach this point due to later modifications of the code
5367 -- above.
5369 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5370 and then Nkind (N) /= N_Entry_Call_Statement
5371 and then Entry_Call_Statement (Parent (N)) = N
5372 then
5373 if Ada_Version < Ada_2005 then
5374 Error_Msg_N ("entry call required in select statement", N);
5376 -- Ada 2005 (AI-345): If a procedure_call_statement is used
5377 -- for a procedure_or_entry_call, the procedure_name or
5378 -- procedure_prefix of the procedure_call_statement shall denote
5379 -- an entry renamed by a procedure, or (a view of) a primitive
5380 -- subprogram of a limited interface whose first parameter is
5381 -- a controlling parameter.
5383 elsif Nkind (N) = N_Procedure_Call_Statement
5384 and then not Is_Renamed_Entry (Nam)
5385 and then not Is_Controlling_Limited_Procedure (Nam)
5386 then
5387 Error_Msg_N
5388 ("entry call or dispatching primitive of interface required", N);
5389 end if;
5390 end if;
5392 -- Check that this is not a call to a protected procedure or entry from
5393 -- within a protected function.
5395 Check_Internal_Protected_Use (N, Nam);
5397 -- Freeze the subprogram name if not in a spec-expression. Note that
5398 -- we freeze procedure calls as well as function calls. Procedure calls
5399 -- are not frozen according to the rules (RM 13.14(14)) because it is
5400 -- impossible to have a procedure call to a non-frozen procedure in
5401 -- pure Ada, but in the code that we generate in the expander, this
5402 -- rule needs extending because we can generate procedure calls that
5403 -- need freezing.
5405 -- In Ada 2012, expression functions may be called within pre/post
5406 -- conditions of subsequent functions or expression functions. Such
5407 -- calls do not freeze when they appear within generated bodies,
5408 -- (including the body of another expression function) which would
5409 -- place the freeze node in the wrong scope. An expression function
5410 -- is frozen in the usual fashion, by the appearance of a real body,
5411 -- or at the end of a declarative part.
5413 if Is_Entity_Name (Subp) and then not In_Spec_Expression
5414 and then not Is_Expression_Function (Current_Scope)
5415 and then
5416 (not Is_Expression_Function (Entity (Subp))
5417 or else Scope (Entity (Subp)) = Current_Scope)
5418 then
5419 Freeze_Expression (Subp);
5420 end if;
5422 -- For a predefined operator, the type of the result is the type imposed
5423 -- by context, except for a predefined operation on universal fixed.
5424 -- Otherwise The type of the call is the type returned by the subprogram
5425 -- being called.
5427 if Is_Predefined_Op (Nam) then
5428 if Etype (N) /= Universal_Fixed then
5429 Set_Etype (N, Typ);
5430 end if;
5432 -- If the subprogram returns an array type, and the context requires the
5433 -- component type of that array type, the node is really an indexing of
5434 -- the parameterless call. Resolve as such. A pathological case occurs
5435 -- when the type of the component is an access to the array type. In
5436 -- this case the call is truly ambiguous.
5438 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
5439 and then
5440 ((Is_Array_Type (Etype (Nam))
5441 and then Covers (Typ, Component_Type (Etype (Nam))))
5442 or else (Is_Access_Type (Etype (Nam))
5443 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5444 and then
5445 Covers
5446 (Typ,
5447 Component_Type (Designated_Type (Etype (Nam))))))
5448 then
5449 declare
5450 Index_Node : Node_Id;
5451 New_Subp : Node_Id;
5452 Ret_Type : constant Entity_Id := Etype (Nam);
5454 begin
5455 if Is_Access_Type (Ret_Type)
5456 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
5457 then
5458 Error_Msg_N
5459 ("cannot disambiguate function call and indexing", N);
5460 else
5461 New_Subp := Relocate_Node (Subp);
5462 Set_Entity (Subp, Nam);
5464 if (Is_Array_Type (Ret_Type)
5465 and then Component_Type (Ret_Type) /= Any_Type)
5466 or else
5467 (Is_Access_Type (Ret_Type)
5468 and then
5469 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
5470 then
5471 if Needs_No_Actuals (Nam) then
5473 -- Indexed call to a parameterless function
5475 Index_Node :=
5476 Make_Indexed_Component (Loc,
5477 Prefix =>
5478 Make_Function_Call (Loc,
5479 Name => New_Subp),
5480 Expressions => Parameter_Associations (N));
5481 else
5482 -- An Ada 2005 prefixed call to a primitive operation
5483 -- whose first parameter is the prefix. This prefix was
5484 -- prepended to the parameter list, which is actually a
5485 -- list of indexes. Remove the prefix in order to build
5486 -- the proper indexed component.
5488 Index_Node :=
5489 Make_Indexed_Component (Loc,
5490 Prefix =>
5491 Make_Function_Call (Loc,
5492 Name => New_Subp,
5493 Parameter_Associations =>
5494 New_List
5495 (Remove_Head (Parameter_Associations (N)))),
5496 Expressions => Parameter_Associations (N));
5497 end if;
5499 -- Preserve the parenthesis count of the node
5501 Set_Paren_Count (Index_Node, Paren_Count (N));
5503 -- Since we are correcting a node classification error made
5504 -- by the parser, we call Replace rather than Rewrite.
5506 Replace (N, Index_Node);
5508 Set_Etype (Prefix (N), Ret_Type);
5509 Set_Etype (N, Typ);
5510 Resolve_Indexed_Component (N, Typ);
5511 Check_Elab_Call (Prefix (N));
5512 end if;
5513 end if;
5515 return;
5516 end;
5518 else
5519 Set_Etype (N, Etype (Nam));
5520 end if;
5522 -- In the case where the call is to an overloaded subprogram, Analyze
5523 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
5524 -- such a case Normalize_Actuals needs to be called once more to order
5525 -- the actuals correctly. Otherwise the call will have the ordering
5526 -- given by the last overloaded subprogram whether this is the correct
5527 -- one being called or not.
5529 if Is_Overloaded (Subp) then
5530 Normalize_Actuals (N, Nam, False, Norm_OK);
5531 pragma Assert (Norm_OK);
5532 end if;
5534 -- In any case, call is fully resolved now. Reset Overload flag, to
5535 -- prevent subsequent overload resolution if node is analyzed again
5537 Set_Is_Overloaded (Subp, False);
5538 Set_Is_Overloaded (N, False);
5540 -- If we are calling the current subprogram from immediately within its
5541 -- body, then that is the case where we can sometimes detect cases of
5542 -- infinite recursion statically. Do not try this in case restriction
5543 -- No_Recursion is in effect anyway, and do it only for source calls.
5545 if Comes_From_Source (N) then
5546 Scop := Current_Scope;
5548 -- Issue warning for possible infinite recursion in the absence
5549 -- of the No_Recursion restriction.
5551 if Same_Or_Aliased_Subprograms (Nam, Scop)
5552 and then not Restriction_Active (No_Recursion)
5553 and then Check_Infinite_Recursion (N)
5554 then
5555 -- Here we detected and flagged an infinite recursion, so we do
5556 -- not need to test the case below for further warnings. Also we
5557 -- are all done if we now have a raise SE node.
5559 if Nkind (N) = N_Raise_Storage_Error then
5560 return;
5561 end if;
5563 -- If call is to immediately containing subprogram, then check for
5564 -- the case of a possible run-time detectable infinite recursion.
5566 else
5567 Scope_Loop : while Scop /= Standard_Standard loop
5568 if Same_Or_Aliased_Subprograms (Nam, Scop) then
5570 -- Although in general case, recursion is not statically
5571 -- checkable, the case of calling an immediately containing
5572 -- subprogram is easy to catch.
5574 Check_Restriction (No_Recursion, N);
5576 -- If the recursive call is to a parameterless subprogram,
5577 -- then even if we can't statically detect infinite
5578 -- recursion, this is pretty suspicious, and we output a
5579 -- warning. Furthermore, we will try later to detect some
5580 -- cases here at run time by expanding checking code (see
5581 -- Detect_Infinite_Recursion in package Exp_Ch6).
5583 -- If the recursive call is within a handler, do not emit a
5584 -- warning, because this is a common idiom: loop until input
5585 -- is correct, catch illegal input in handler and restart.
5587 if No (First_Formal (Nam))
5588 and then Etype (Nam) = Standard_Void_Type
5589 and then not Error_Posted (N)
5590 and then Nkind (Parent (N)) /= N_Exception_Handler
5591 then
5592 -- For the case of a procedure call. We give the message
5593 -- only if the call is the first statement in a sequence
5594 -- of statements, or if all previous statements are
5595 -- simple assignments. This is simply a heuristic to
5596 -- decrease false positives, without losing too many good
5597 -- warnings. The idea is that these previous statements
5598 -- may affect global variables the procedure depends on.
5599 -- We also exclude raise statements, that may arise from
5600 -- constraint checks and are probably unrelated to the
5601 -- intended control flow.
5603 if Nkind (N) = N_Procedure_Call_Statement
5604 and then Is_List_Member (N)
5605 then
5606 declare
5607 P : Node_Id;
5608 begin
5609 P := Prev (N);
5610 while Present (P) loop
5611 if not Nkind_In (P,
5612 N_Assignment_Statement,
5613 N_Raise_Constraint_Error)
5614 then
5615 exit Scope_Loop;
5616 end if;
5618 Prev (P);
5619 end loop;
5620 end;
5621 end if;
5623 -- Do not give warning if we are in a conditional context
5625 declare
5626 K : constant Node_Kind := Nkind (Parent (N));
5627 begin
5628 if (K = N_Loop_Statement
5629 and then Present (Iteration_Scheme (Parent (N))))
5630 or else K = N_If_Statement
5631 or else K = N_Elsif_Part
5632 or else K = N_Case_Statement_Alternative
5633 then
5634 exit Scope_Loop;
5635 end if;
5636 end;
5638 -- Here warning is to be issued
5640 Set_Has_Recursive_Call (Nam);
5641 Error_Msg_N
5642 ("??possible infinite recursion!", N);
5643 Error_Msg_N
5644 ("\??Storage_Error may be raised at run time!", N);
5645 end if;
5647 exit Scope_Loop;
5648 end if;
5650 Scop := Scope (Scop);
5651 end loop Scope_Loop;
5652 end if;
5653 end if;
5655 -- Check obsolescent reference to Ada.Characters.Handling subprogram
5657 Check_Obsolescent_2005_Entity (Nam, Subp);
5659 -- If subprogram name is a predefined operator, it was given in
5660 -- functional notation. Replace call node with operator node, so
5661 -- that actuals can be resolved appropriately.
5663 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5664 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5665 return;
5667 elsif Present (Alias (Nam))
5668 and then Is_Predefined_Op (Alias (Nam))
5669 then
5670 Resolve_Actuals (N, Nam);
5671 Make_Call_Into_Operator (N, Typ, Alias (Nam));
5672 return;
5673 end if;
5675 -- Create a transient scope if the resulting type requires it
5677 -- There are several notable exceptions:
5679 -- a) In init procs, the transient scope overhead is not needed, and is
5680 -- even incorrect when the call is a nested initialization call for a
5681 -- component whose expansion may generate adjust calls. However, if the
5682 -- call is some other procedure call within an initialization procedure
5683 -- (for example a call to Create_Task in the init_proc of the task
5684 -- run-time record) a transient scope must be created around this call.
5686 -- b) Enumeration literal pseudo-calls need no transient scope
5688 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
5689 -- functions) do not use the secondary stack even though the return
5690 -- type may be unconstrained.
5692 -- d) Calls to a build-in-place function, since such functions may
5693 -- allocate their result directly in a target object, and cases where
5694 -- the result does get allocated in the secondary stack are checked for
5695 -- within the specialized Exp_Ch6 procedures for expanding those
5696 -- build-in-place calls.
5698 -- e) If the subprogram is marked Inline_Always, then even if it returns
5699 -- an unconstrained type the call does not require use of the secondary
5700 -- stack. However, inlining will only take place if the body to inline
5701 -- is already present. It may not be available if e.g. the subprogram is
5702 -- declared in a child instance.
5704 -- If this is an initialization call for a type whose construction
5705 -- uses the secondary stack, and it is not a nested call to initialize
5706 -- a component, we do need to create a transient scope for it. We
5707 -- check for this by traversing the type in Check_Initialization_Call.
5709 if Is_Inlined (Nam)
5710 and then Has_Pragma_Inline_Always (Nam)
5711 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5712 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5713 and then not Debug_Flag_Dot_K
5714 then
5715 null;
5717 elsif Is_Inlined (Nam)
5718 and then Has_Pragma_Inline (Nam)
5719 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5720 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5721 and then Debug_Flag_Dot_K
5722 then
5723 null;
5725 elsif Ekind (Nam) = E_Enumeration_Literal
5726 or else Is_Build_In_Place_Function (Nam)
5727 or else Is_Intrinsic_Subprogram (Nam)
5728 then
5729 null;
5731 elsif Full_Expander_Active
5732 and then Is_Type (Etype (Nam))
5733 and then Requires_Transient_Scope (Etype (Nam))
5734 and then
5735 (not Within_Init_Proc
5736 or else
5737 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
5738 then
5739 Establish_Transient_Scope (N, Sec_Stack => True);
5741 -- If the call appears within the bounds of a loop, it will
5742 -- be rewritten and reanalyzed, nothing left to do here.
5744 if Nkind (N) /= N_Function_Call then
5745 return;
5746 end if;
5748 elsif Is_Init_Proc (Nam)
5749 and then not Within_Init_Proc
5750 then
5751 Check_Initialization_Call (N, Nam);
5752 end if;
5754 -- A protected function cannot be called within the definition of the
5755 -- enclosing protected type.
5757 if Is_Protected_Type (Scope (Nam))
5758 and then In_Open_Scopes (Scope (Nam))
5759 and then not Has_Completion (Scope (Nam))
5760 then
5761 Error_Msg_NE
5762 ("& cannot be called before end of protected definition", N, Nam);
5763 end if;
5765 -- Propagate interpretation to actuals, and add default expressions
5766 -- where needed.
5768 if Present (First_Formal (Nam)) then
5769 Resolve_Actuals (N, Nam);
5771 -- Overloaded literals are rewritten as function calls, for purpose of
5772 -- resolution. After resolution, we can replace the call with the
5773 -- literal itself.
5775 elsif Ekind (Nam) = E_Enumeration_Literal then
5776 Copy_Node (Subp, N);
5777 Resolve_Entity_Name (N, Typ);
5779 -- Avoid validation, since it is a static function call
5781 Generate_Reference (Nam, Subp);
5782 return;
5783 end if;
5785 -- If the subprogram is not global, then kill all saved values and
5786 -- checks. This is a bit conservative, since in many cases we could do
5787 -- better, but it is not worth the effort. Similarly, we kill constant
5788 -- values. However we do not need to do this for internal entities
5789 -- (unless they are inherited user-defined subprograms), since they
5790 -- are not in the business of molesting local values.
5792 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
5793 -- kill all checks and values for calls to global subprograms. This
5794 -- takes care of the case where an access to a local subprogram is
5795 -- taken, and could be passed directly or indirectly and then called
5796 -- from almost any context.
5798 -- Note: we do not do this step till after resolving the actuals. That
5799 -- way we still take advantage of the current value information while
5800 -- scanning the actuals.
5802 -- We suppress killing values if we are processing the nodes associated
5803 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
5804 -- type kills all the values as part of analyzing the code that
5805 -- initializes the dispatch tables.
5807 if Inside_Freezing_Actions = 0
5808 and then (not Is_Library_Level_Entity (Nam)
5809 or else Suppress_Value_Tracking_On_Call
5810 (Nearest_Dynamic_Scope (Current_Scope)))
5811 and then (Comes_From_Source (Nam)
5812 or else (Present (Alias (Nam))
5813 and then Comes_From_Source (Alias (Nam))))
5814 then
5815 Kill_Current_Values;
5816 end if;
5818 -- If we are warning about unread OUT parameters, this is the place to
5819 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
5820 -- after the above call to Kill_Current_Values (since that call clears
5821 -- the Last_Assignment field of all local variables).
5823 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
5824 and then Comes_From_Source (N)
5825 and then In_Extended_Main_Source_Unit (N)
5826 then
5827 declare
5828 F : Entity_Id;
5829 A : Node_Id;
5831 begin
5832 F := First_Formal (Nam);
5833 A := First_Actual (N);
5834 while Present (F) and then Present (A) loop
5835 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
5836 and then Warn_On_Modified_As_Out_Parameter (F)
5837 and then Is_Entity_Name (A)
5838 and then Present (Entity (A))
5839 and then Comes_From_Source (N)
5840 and then Safe_To_Capture_Value (N, Entity (A))
5841 then
5842 Set_Last_Assignment (Entity (A), A);
5843 end if;
5845 Next_Formal (F);
5846 Next_Actual (A);
5847 end loop;
5848 end;
5849 end if;
5851 -- If the subprogram is a primitive operation, check whether or not
5852 -- it is a correct dispatching call.
5854 if Is_Overloadable (Nam)
5855 and then Is_Dispatching_Operation (Nam)
5856 then
5857 Check_Dispatching_Call (N);
5859 elsif Ekind (Nam) /= E_Subprogram_Type
5860 and then Is_Abstract_Subprogram (Nam)
5861 and then not In_Instance
5862 then
5863 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
5864 end if;
5866 -- If this is a dispatching call, generate the appropriate reference,
5867 -- for better source navigation in GPS.
5869 if Is_Overloadable (Nam)
5870 and then Present (Controlling_Argument (N))
5871 then
5872 Generate_Reference (Nam, Subp, 'R');
5874 -- Normal case, not a dispatching call: generate a call reference
5876 else
5877 Generate_Reference (Nam, Subp, 's');
5878 end if;
5880 if Is_Intrinsic_Subprogram (Nam) then
5881 Check_Intrinsic_Call (N);
5882 end if;
5884 -- Check for violation of restriction No_Specific_Termination_Handlers
5885 -- and warn on a potentially blocking call to Abort_Task.
5887 if Restriction_Check_Required (No_Specific_Termination_Handlers)
5888 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
5889 or else
5890 Is_RTE (Nam, RE_Specific_Handler))
5891 then
5892 Check_Restriction (No_Specific_Termination_Handlers, N);
5894 elsif Is_RTE (Nam, RE_Abort_Task) then
5895 Check_Potentially_Blocking_Operation (N);
5896 end if;
5898 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
5899 -- timing event violates restriction No_Relative_Delay (AI-0211). We
5900 -- need to check the second argument to determine whether it is an
5901 -- absolute or relative timing event.
5903 if Restriction_Check_Required (No_Relative_Delay)
5904 and then Is_RTE (Nam, RE_Set_Handler)
5905 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
5906 then
5907 Check_Restriction (No_Relative_Delay, N);
5908 end if;
5910 -- Issue an error for a call to an eliminated subprogram. This routine
5911 -- will not perform the check if the call appears within a default
5912 -- expression.
5914 Check_For_Eliminated_Subprogram (Subp, Nam);
5916 -- In formal mode, the primitive operations of a tagged type or type
5917 -- extension do not include functions that return the tagged type.
5919 if Nkind (N) = N_Function_Call
5920 and then Is_Tagged_Type (Etype (N))
5921 and then Is_Entity_Name (Name (N))
5922 and then Is_Inherited_Operation_For_Type (Entity (Name (N)), Etype (N))
5923 then
5924 Check_SPARK_Restriction ("function not inherited", N);
5925 end if;
5927 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
5928 -- class-wide and the call dispatches on result in a context that does
5929 -- not provide a tag, the call raises Program_Error.
5931 if Nkind (N) = N_Function_Call
5932 and then In_Instance
5933 and then Is_Generic_Actual_Type (Typ)
5934 and then Is_Class_Wide_Type (Typ)
5935 and then Has_Controlling_Result (Nam)
5936 and then Nkind (Parent (N)) = N_Object_Declaration
5937 then
5938 -- Verify that none of the formals are controlling
5940 declare
5941 Call_OK : Boolean := False;
5942 F : Entity_Id;
5944 begin
5945 F := First_Formal (Nam);
5946 while Present (F) loop
5947 if Is_Controlling_Formal (F) then
5948 Call_OK := True;
5949 exit;
5950 end if;
5952 Next_Formal (F);
5953 end loop;
5955 if not Call_OK then
5956 Error_Msg_N ("!?? cannot determine tag of result", N);
5957 Error_Msg_N ("!?? Program_Error will be raised", N);
5958 Insert_Action (N,
5959 Make_Raise_Program_Error (Sloc (N),
5960 Reason => PE_Explicit_Raise));
5961 end if;
5962 end;
5963 end if;
5965 -- Check the dimensions of the actuals in the call. For function calls,
5966 -- propagate the dimensions from the returned type to N.
5968 Analyze_Dimension_Call (N, Nam);
5970 -- All done, evaluate call and deal with elaboration issues
5972 Eval_Call (N);
5973 Check_Elab_Call (N);
5974 Warn_On_Overlapping_Actuals (Nam, N);
5975 end Resolve_Call;
5977 -----------------------------
5978 -- Resolve_Case_Expression --
5979 -----------------------------
5981 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
5982 Alt : Node_Id;
5984 begin
5985 Alt := First (Alternatives (N));
5986 while Present (Alt) loop
5987 Resolve (Expression (Alt), Typ);
5988 Next (Alt);
5989 end loop;
5991 Set_Etype (N, Typ);
5992 Eval_Case_Expression (N);
5993 end Resolve_Case_Expression;
5995 -------------------------------
5996 -- Resolve_Character_Literal --
5997 -------------------------------
5999 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
6000 B_Typ : constant Entity_Id := Base_Type (Typ);
6001 C : Entity_Id;
6003 begin
6004 -- Verify that the character does belong to the type of the context
6006 Set_Etype (N, B_Typ);
6007 Eval_Character_Literal (N);
6009 -- Wide_Wide_Character literals must always be defined, since the set
6010 -- of wide wide character literals is complete, i.e. if a character
6011 -- literal is accepted by the parser, then it is OK for wide wide
6012 -- character (out of range character literals are rejected).
6014 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
6015 return;
6017 -- Always accept character literal for type Any_Character, which
6018 -- occurs in error situations and in comparisons of literals, both
6019 -- of which should accept all literals.
6021 elsif B_Typ = Any_Character then
6022 return;
6024 -- For Standard.Character or a type derived from it, check that the
6025 -- literal is in range.
6027 elsif Root_Type (B_Typ) = Standard_Character then
6028 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
6029 return;
6030 end if;
6032 -- For Standard.Wide_Character or a type derived from it, check that the
6033 -- literal is in range.
6035 elsif Root_Type (B_Typ) = Standard_Wide_Character then
6036 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
6037 return;
6038 end if;
6040 -- For Standard.Wide_Wide_Character or a type derived from it, we
6041 -- know the literal is in range, since the parser checked!
6043 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
6044 return;
6046 -- If the entity is already set, this has already been resolved in a
6047 -- generic context, or comes from expansion. Nothing else to do.
6049 elsif Present (Entity (N)) then
6050 return;
6052 -- Otherwise we have a user defined character type, and we can use the
6053 -- standard visibility mechanisms to locate the referenced entity.
6055 else
6056 C := Current_Entity (N);
6057 while Present (C) loop
6058 if Etype (C) = B_Typ then
6059 Set_Entity_With_Style_Check (N, C);
6060 Generate_Reference (C, N);
6061 return;
6062 end if;
6064 C := Homonym (C);
6065 end loop;
6066 end if;
6068 -- If we fall through, then the literal does not match any of the
6069 -- entries of the enumeration type. This isn't just a constraint error
6070 -- situation, it is an illegality (see RM 4.2).
6072 Error_Msg_NE
6073 ("character not defined for }", N, First_Subtype (B_Typ));
6074 end Resolve_Character_Literal;
6076 ---------------------------
6077 -- Resolve_Comparison_Op --
6078 ---------------------------
6080 -- Context requires a boolean type, and plays no role in resolution.
6081 -- Processing identical to that for equality operators. The result type is
6082 -- the base type, which matters when pathological subtypes of booleans with
6083 -- limited ranges are used.
6085 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
6086 L : constant Node_Id := Left_Opnd (N);
6087 R : constant Node_Id := Right_Opnd (N);
6088 T : Entity_Id;
6090 begin
6091 -- If this is an intrinsic operation which is not predefined, use the
6092 -- types of its declared arguments to resolve the possibly overloaded
6093 -- operands. Otherwise the operands are unambiguous and specify the
6094 -- expected type.
6096 if Scope (Entity (N)) /= Standard_Standard then
6097 T := Etype (First_Entity (Entity (N)));
6099 else
6100 T := Find_Unique_Type (L, R);
6102 if T = Any_Fixed then
6103 T := Unique_Fixed_Point_Type (L);
6104 end if;
6105 end if;
6107 Set_Etype (N, Base_Type (Typ));
6108 Generate_Reference (T, N, ' ');
6110 -- Skip remaining processing if already set to Any_Type
6112 if T = Any_Type then
6113 return;
6114 end if;
6116 -- Deal with other error cases
6118 if T = Any_String or else
6119 T = Any_Composite or else
6120 T = Any_Character
6121 then
6122 if T = Any_Character then
6123 Ambiguous_Character (L);
6124 else
6125 Error_Msg_N ("ambiguous operands for comparison", N);
6126 end if;
6128 Set_Etype (N, Any_Type);
6129 return;
6130 end if;
6132 -- Resolve the operands if types OK
6134 Resolve (L, T);
6135 Resolve (R, T);
6136 Check_Unset_Reference (L);
6137 Check_Unset_Reference (R);
6138 Generate_Operator_Reference (N, T);
6139 Check_Low_Bound_Tested (N);
6141 -- In SPARK, ordering operators <, <=, >, >= are not defined for Boolean
6142 -- types or array types except String.
6144 if Is_Boolean_Type (T) then
6145 Check_SPARK_Restriction
6146 ("comparison is not defined on Boolean type", N);
6148 elsif Is_Array_Type (T)
6149 and then Base_Type (T) /= Standard_String
6150 then
6151 Check_SPARK_Restriction
6152 ("comparison is not defined on array types other than String", N);
6153 end if;
6155 -- Check comparison on unordered enumeration
6157 if Bad_Unordered_Enumeration_Reference (N, Etype (L)) then
6158 Error_Msg_N ("comparison on unordered enumeration type?U?", N);
6159 end if;
6161 -- Evaluate the relation (note we do this after the above check since
6162 -- this Eval call may change N to True/False.
6164 Analyze_Dimension (N);
6165 Eval_Relational_Op (N);
6166 end Resolve_Comparison_Op;
6168 -----------------------------------------
6169 -- Resolve_Discrete_Subtype_Indication --
6170 -----------------------------------------
6172 procedure Resolve_Discrete_Subtype_Indication
6173 (N : Node_Id;
6174 Typ : Entity_Id)
6176 R : Node_Id;
6177 S : Entity_Id;
6179 begin
6180 Analyze (Subtype_Mark (N));
6181 S := Entity (Subtype_Mark (N));
6183 if Nkind (Constraint (N)) /= N_Range_Constraint then
6184 Error_Msg_N ("expect range constraint for discrete type", N);
6185 Set_Etype (N, Any_Type);
6187 else
6188 R := Range_Expression (Constraint (N));
6190 if R = Error then
6191 return;
6192 end if;
6194 Analyze (R);
6196 if Base_Type (S) /= Base_Type (Typ) then
6197 Error_Msg_NE
6198 ("expect subtype of }", N, First_Subtype (Typ));
6200 -- Rewrite the constraint as a range of Typ
6201 -- to allow compilation to proceed further.
6203 Set_Etype (N, Typ);
6204 Rewrite (Low_Bound (R),
6205 Make_Attribute_Reference (Sloc (Low_Bound (R)),
6206 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6207 Attribute_Name => Name_First));
6208 Rewrite (High_Bound (R),
6209 Make_Attribute_Reference (Sloc (High_Bound (R)),
6210 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6211 Attribute_Name => Name_First));
6213 else
6214 Resolve (R, Typ);
6215 Set_Etype (N, Etype (R));
6217 -- Additionally, we must check that the bounds are compatible
6218 -- with the given subtype, which might be different from the
6219 -- type of the context.
6221 Apply_Range_Check (R, S);
6223 -- ??? If the above check statically detects a Constraint_Error
6224 -- it replaces the offending bound(s) of the range R with a
6225 -- Constraint_Error node. When the itype which uses these bounds
6226 -- is frozen the resulting call to Duplicate_Subexpr generates
6227 -- a new temporary for the bounds.
6229 -- Unfortunately there are other itypes that are also made depend
6230 -- on these bounds, so when Duplicate_Subexpr is called they get
6231 -- a forward reference to the newly created temporaries and Gigi
6232 -- aborts on such forward references. This is probably sign of a
6233 -- more fundamental problem somewhere else in either the order of
6234 -- itype freezing or the way certain itypes are constructed.
6236 -- To get around this problem we call Remove_Side_Effects right
6237 -- away if either bounds of R are a Constraint_Error.
6239 declare
6240 L : constant Node_Id := Low_Bound (R);
6241 H : constant Node_Id := High_Bound (R);
6243 begin
6244 if Nkind (L) = N_Raise_Constraint_Error then
6245 Remove_Side_Effects (L);
6246 end if;
6248 if Nkind (H) = N_Raise_Constraint_Error then
6249 Remove_Side_Effects (H);
6250 end if;
6251 end;
6253 Check_Unset_Reference (Low_Bound (R));
6254 Check_Unset_Reference (High_Bound (R));
6255 end if;
6256 end if;
6257 end Resolve_Discrete_Subtype_Indication;
6259 -------------------------
6260 -- Resolve_Entity_Name --
6261 -------------------------
6263 -- Used to resolve identifiers and expanded names
6265 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
6266 E : constant Entity_Id := Entity (N);
6268 begin
6269 -- If garbage from errors, set to Any_Type and return
6271 if No (E) and then Total_Errors_Detected /= 0 then
6272 Set_Etype (N, Any_Type);
6273 return;
6274 end if;
6276 -- Replace named numbers by corresponding literals. Note that this is
6277 -- the one case where Resolve_Entity_Name must reset the Etype, since
6278 -- it is currently marked as universal.
6280 if Ekind (E) = E_Named_Integer then
6281 Set_Etype (N, Typ);
6282 Eval_Named_Integer (N);
6284 elsif Ekind (E) = E_Named_Real then
6285 Set_Etype (N, Typ);
6286 Eval_Named_Real (N);
6288 -- For enumeration literals, we need to make sure that a proper style
6289 -- check is done, since such literals are overloaded, and thus we did
6290 -- not do a style check during the first phase of analysis.
6292 elsif Ekind (E) = E_Enumeration_Literal then
6293 Set_Entity_With_Style_Check (N, E);
6294 Eval_Entity_Name (N);
6296 -- Case of subtype name appearing as an operand in expression
6298 elsif Is_Type (E) then
6300 -- Allow use of subtype if it is a concurrent type where we are
6301 -- currently inside the body. This will eventually be expanded into a
6302 -- call to Self (for tasks) or _object (for protected objects). Any
6303 -- other use of a subtype is invalid.
6305 if Is_Concurrent_Type (E)
6306 and then In_Open_Scopes (E)
6307 then
6308 null;
6310 -- Any other use is an error
6312 else
6313 Error_Msg_N
6314 ("invalid use of subtype mark in expression or call", N);
6315 end if;
6317 -- Check discriminant use if entity is discriminant in current scope,
6318 -- i.e. discriminant of record or concurrent type currently being
6319 -- analyzed. Uses in corresponding body are unrestricted.
6321 elsif Ekind (E) = E_Discriminant
6322 and then Scope (E) = Current_Scope
6323 and then not Has_Completion (Current_Scope)
6324 then
6325 Check_Discriminant_Use (N);
6327 -- A parameterless generic function cannot appear in a context that
6328 -- requires resolution.
6330 elsif Ekind (E) = E_Generic_Function then
6331 Error_Msg_N ("illegal use of generic function", N);
6333 elsif Ekind (E) = E_Out_Parameter
6334 and then Ada_Version = Ada_83
6335 and then (Nkind (Parent (N)) in N_Op
6336 or else (Nkind (Parent (N)) = N_Assignment_Statement
6337 and then N = Expression (Parent (N)))
6338 or else Nkind (Parent (N)) = N_Explicit_Dereference)
6339 then
6340 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
6342 -- In all other cases, just do the possible static evaluation
6344 else
6345 -- A deferred constant that appears in an expression must have a
6346 -- completion, unless it has been removed by in-place expansion of
6347 -- an aggregate.
6349 if Ekind (E) = E_Constant
6350 and then Comes_From_Source (E)
6351 and then No (Constant_Value (E))
6352 and then Is_Frozen (Etype (E))
6353 and then not In_Spec_Expression
6354 and then not Is_Imported (E)
6355 then
6356 if No_Initialization (Parent (E))
6357 or else (Present (Full_View (E))
6358 and then No_Initialization (Parent (Full_View (E))))
6359 then
6360 null;
6361 else
6362 Error_Msg_N (
6363 "deferred constant is frozen before completion", N);
6364 end if;
6365 end if;
6367 Eval_Entity_Name (N);
6368 end if;
6369 end Resolve_Entity_Name;
6371 -------------------
6372 -- Resolve_Entry --
6373 -------------------
6375 procedure Resolve_Entry (Entry_Name : Node_Id) is
6376 Loc : constant Source_Ptr := Sloc (Entry_Name);
6377 Nam : Entity_Id;
6378 New_N : Node_Id;
6379 S : Entity_Id;
6380 Tsk : Entity_Id;
6381 E_Name : Node_Id;
6382 Index : Node_Id;
6384 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
6385 -- If the bounds of the entry family being called depend on task
6386 -- discriminants, build a new index subtype where a discriminant is
6387 -- replaced with the value of the discriminant of the target task.
6388 -- The target task is the prefix of the entry name in the call.
6390 -----------------------
6391 -- Actual_Index_Type --
6392 -----------------------
6394 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
6395 Typ : constant Entity_Id := Entry_Index_Type (E);
6396 Tsk : constant Entity_Id := Scope (E);
6397 Lo : constant Node_Id := Type_Low_Bound (Typ);
6398 Hi : constant Node_Id := Type_High_Bound (Typ);
6399 New_T : Entity_Id;
6401 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
6402 -- If the bound is given by a discriminant, replace with a reference
6403 -- to the discriminant of the same name in the target task. If the
6404 -- entry name is the target of a requeue statement and the entry is
6405 -- in the current protected object, the bound to be used is the
6406 -- discriminal of the object (see Apply_Range_Checks for details of
6407 -- the transformation).
6409 -----------------------------
6410 -- Actual_Discriminant_Ref --
6411 -----------------------------
6413 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
6414 Typ : constant Entity_Id := Etype (Bound);
6415 Ref : Node_Id;
6417 begin
6418 Remove_Side_Effects (Bound);
6420 if not Is_Entity_Name (Bound)
6421 or else Ekind (Entity (Bound)) /= E_Discriminant
6422 then
6423 return Bound;
6425 elsif Is_Protected_Type (Tsk)
6426 and then In_Open_Scopes (Tsk)
6427 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
6428 then
6429 -- Note: here Bound denotes a discriminant of the corresponding
6430 -- record type tskV, whose discriminal is a formal of the
6431 -- init-proc tskVIP. What we want is the body discriminal,
6432 -- which is associated to the discriminant of the original
6433 -- concurrent type tsk.
6435 return New_Occurrence_Of
6436 (Find_Body_Discriminal (Entity (Bound)), Loc);
6438 else
6439 Ref :=
6440 Make_Selected_Component (Loc,
6441 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
6442 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
6443 Analyze (Ref);
6444 Resolve (Ref, Typ);
6445 return Ref;
6446 end if;
6447 end Actual_Discriminant_Ref;
6449 -- Start of processing for Actual_Index_Type
6451 begin
6452 if not Has_Discriminants (Tsk)
6453 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
6454 then
6455 return Entry_Index_Type (E);
6457 else
6458 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
6459 Set_Etype (New_T, Base_Type (Typ));
6460 Set_Size_Info (New_T, Typ);
6461 Set_RM_Size (New_T, RM_Size (Typ));
6462 Set_Scalar_Range (New_T,
6463 Make_Range (Sloc (Entry_Name),
6464 Low_Bound => Actual_Discriminant_Ref (Lo),
6465 High_Bound => Actual_Discriminant_Ref (Hi)));
6467 return New_T;
6468 end if;
6469 end Actual_Index_Type;
6471 -- Start of processing of Resolve_Entry
6473 begin
6474 -- Find name of entry being called, and resolve prefix of name with its
6475 -- own type. The prefix can be overloaded, and the name and signature of
6476 -- the entry must be taken into account.
6478 if Nkind (Entry_Name) = N_Indexed_Component then
6480 -- Case of dealing with entry family within the current tasks
6482 E_Name := Prefix (Entry_Name);
6484 else
6485 E_Name := Entry_Name;
6486 end if;
6488 if Is_Entity_Name (E_Name) then
6490 -- Entry call to an entry (or entry family) in the current task. This
6491 -- is legal even though the task will deadlock. Rewrite as call to
6492 -- current task.
6494 -- This can also be a call to an entry in an enclosing task. If this
6495 -- is a single task, we have to retrieve its name, because the scope
6496 -- of the entry is the task type, not the object. If the enclosing
6497 -- task is a task type, the identity of the task is given by its own
6498 -- self variable.
6500 -- Finally this can be a requeue on an entry of the same task or
6501 -- protected object.
6503 S := Scope (Entity (E_Name));
6505 for J in reverse 0 .. Scope_Stack.Last loop
6506 if Is_Task_Type (Scope_Stack.Table (J).Entity)
6507 and then not Comes_From_Source (S)
6508 then
6509 -- S is an enclosing task or protected object. The concurrent
6510 -- declaration has been converted into a type declaration, and
6511 -- the object itself has an object declaration that follows
6512 -- the type in the same declarative part.
6514 Tsk := Next_Entity (S);
6515 while Etype (Tsk) /= S loop
6516 Next_Entity (Tsk);
6517 end loop;
6519 S := Tsk;
6520 exit;
6522 elsif S = Scope_Stack.Table (J).Entity then
6524 -- Call to current task. Will be transformed into call to Self
6526 exit;
6528 end if;
6529 end loop;
6531 New_N :=
6532 Make_Selected_Component (Loc,
6533 Prefix => New_Occurrence_Of (S, Loc),
6534 Selector_Name =>
6535 New_Occurrence_Of (Entity (E_Name), Loc));
6536 Rewrite (E_Name, New_N);
6537 Analyze (E_Name);
6539 elsif Nkind (Entry_Name) = N_Selected_Component
6540 and then Is_Overloaded (Prefix (Entry_Name))
6541 then
6542 -- Use the entry name (which must be unique at this point) to find
6543 -- the prefix that returns the corresponding task/protected type.
6545 declare
6546 Pref : constant Node_Id := Prefix (Entry_Name);
6547 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
6548 I : Interp_Index;
6549 It : Interp;
6551 begin
6552 Get_First_Interp (Pref, I, It);
6553 while Present (It.Typ) loop
6554 if Scope (Ent) = It.Typ then
6555 Set_Etype (Pref, It.Typ);
6556 exit;
6557 end if;
6559 Get_Next_Interp (I, It);
6560 end loop;
6561 end;
6562 end if;
6564 if Nkind (Entry_Name) = N_Selected_Component then
6565 Resolve (Prefix (Entry_Name));
6567 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6568 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6569 Resolve (Prefix (Prefix (Entry_Name)));
6570 Index := First (Expressions (Entry_Name));
6571 Resolve (Index, Entry_Index_Type (Nam));
6573 -- Up to this point the expression could have been the actual in a
6574 -- simple entry call, and be given by a named association.
6576 if Nkind (Index) = N_Parameter_Association then
6577 Error_Msg_N ("expect expression for entry index", Index);
6578 else
6579 Apply_Range_Check (Index, Actual_Index_Type (Nam));
6580 end if;
6581 end if;
6582 end Resolve_Entry;
6584 ------------------------
6585 -- Resolve_Entry_Call --
6586 ------------------------
6588 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
6589 Entry_Name : constant Node_Id := Name (N);
6590 Loc : constant Source_Ptr := Sloc (Entry_Name);
6591 Actuals : List_Id;
6592 First_Named : Node_Id;
6593 Nam : Entity_Id;
6594 Norm_OK : Boolean;
6595 Obj : Node_Id;
6596 Was_Over : Boolean;
6598 begin
6599 -- We kill all checks here, because it does not seem worth the effort to
6600 -- do anything better, an entry call is a big operation.
6602 Kill_All_Checks;
6604 -- Processing of the name is similar for entry calls and protected
6605 -- operation calls. Once the entity is determined, we can complete
6606 -- the resolution of the actuals.
6608 -- The selector may be overloaded, in the case of a protected object
6609 -- with overloaded functions. The type of the context is used for
6610 -- resolution.
6612 if Nkind (Entry_Name) = N_Selected_Component
6613 and then Is_Overloaded (Selector_Name (Entry_Name))
6614 and then Typ /= Standard_Void_Type
6615 then
6616 declare
6617 I : Interp_Index;
6618 It : Interp;
6620 begin
6621 Get_First_Interp (Selector_Name (Entry_Name), I, It);
6622 while Present (It.Typ) loop
6623 if Covers (Typ, It.Typ) then
6624 Set_Entity (Selector_Name (Entry_Name), It.Nam);
6625 Set_Etype (Entry_Name, It.Typ);
6627 Generate_Reference (It.Typ, N, ' ');
6628 end if;
6630 Get_Next_Interp (I, It);
6631 end loop;
6632 end;
6633 end if;
6635 Resolve_Entry (Entry_Name);
6637 if Nkind (Entry_Name) = N_Selected_Component then
6639 -- Simple entry call
6641 Nam := Entity (Selector_Name (Entry_Name));
6642 Obj := Prefix (Entry_Name);
6643 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
6645 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6647 -- Call to member of entry family
6649 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6650 Obj := Prefix (Prefix (Entry_Name));
6651 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
6652 end if;
6654 -- We cannot in general check the maximum depth of protected entry calls
6655 -- at compile time. But we can tell that any protected entry call at all
6656 -- violates a specified nesting depth of zero.
6658 if Is_Protected_Type (Scope (Nam)) then
6659 Check_Restriction (Max_Entry_Queue_Length, N);
6660 end if;
6662 -- Use context type to disambiguate a protected function that can be
6663 -- called without actuals and that returns an array type, and where the
6664 -- argument list may be an indexing of the returned value.
6666 if Ekind (Nam) = E_Function
6667 and then Needs_No_Actuals (Nam)
6668 and then Present (Parameter_Associations (N))
6669 and then
6670 ((Is_Array_Type (Etype (Nam))
6671 and then Covers (Typ, Component_Type (Etype (Nam))))
6673 or else (Is_Access_Type (Etype (Nam))
6674 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6675 and then
6676 Covers
6677 (Typ,
6678 Component_Type (Designated_Type (Etype (Nam))))))
6679 then
6680 declare
6681 Index_Node : Node_Id;
6683 begin
6684 Index_Node :=
6685 Make_Indexed_Component (Loc,
6686 Prefix =>
6687 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
6688 Expressions => Parameter_Associations (N));
6690 -- Since we are correcting a node classification error made by the
6691 -- parser, we call Replace rather than Rewrite.
6693 Replace (N, Index_Node);
6694 Set_Etype (Prefix (N), Etype (Nam));
6695 Set_Etype (N, Typ);
6696 Resolve_Indexed_Component (N, Typ);
6697 return;
6698 end;
6699 end if;
6701 if Ekind_In (Nam, E_Entry, E_Entry_Family)
6702 and then Present (PPC_Wrapper (Nam))
6703 and then Current_Scope /= PPC_Wrapper (Nam)
6704 then
6705 -- Rewrite as call to the precondition wrapper, adding the task
6706 -- object to the list of actuals. If the call is to a member of an
6707 -- entry family, include the index as well.
6709 declare
6710 New_Call : Node_Id;
6711 New_Actuals : List_Id;
6713 begin
6714 New_Actuals := New_List (Obj);
6716 if Nkind (Entry_Name) = N_Indexed_Component then
6717 Append_To (New_Actuals,
6718 New_Copy_Tree (First (Expressions (Entry_Name))));
6719 end if;
6721 Append_List (Parameter_Associations (N), New_Actuals);
6722 New_Call :=
6723 Make_Procedure_Call_Statement (Loc,
6724 Name =>
6725 New_Occurrence_Of (PPC_Wrapper (Nam), Loc),
6726 Parameter_Associations => New_Actuals);
6727 Rewrite (N, New_Call);
6728 Analyze_And_Resolve (N);
6729 return;
6730 end;
6731 end if;
6733 -- The operation name may have been overloaded. Order the actuals
6734 -- according to the formals of the resolved entity, and set the return
6735 -- type to that of the operation.
6737 if Was_Over then
6738 Normalize_Actuals (N, Nam, False, Norm_OK);
6739 pragma Assert (Norm_OK);
6740 Set_Etype (N, Etype (Nam));
6741 end if;
6743 Resolve_Actuals (N, Nam);
6744 Check_Internal_Protected_Use (N, Nam);
6746 -- Create a call reference to the entry
6748 Generate_Reference (Nam, Entry_Name, 's');
6750 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
6751 Check_Potentially_Blocking_Operation (N);
6752 end if;
6754 -- Verify that a procedure call cannot masquerade as an entry
6755 -- call where an entry call is expected.
6757 if Ekind (Nam) = E_Procedure then
6758 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6759 and then N = Entry_Call_Statement (Parent (N))
6760 then
6761 Error_Msg_N ("entry call required in select statement", N);
6763 elsif Nkind (Parent (N)) = N_Triggering_Alternative
6764 and then N = Triggering_Statement (Parent (N))
6765 then
6766 Error_Msg_N ("triggering statement cannot be procedure call", N);
6768 elsif Ekind (Scope (Nam)) = E_Task_Type
6769 and then not In_Open_Scopes (Scope (Nam))
6770 then
6771 Error_Msg_N ("task has no entry with this name", Entry_Name);
6772 end if;
6773 end if;
6775 -- After resolution, entry calls and protected procedure calls are
6776 -- changed into entry calls, for expansion. The structure of the node
6777 -- does not change, so it can safely be done in place. Protected
6778 -- function calls must keep their structure because they are
6779 -- subexpressions.
6781 if Ekind (Nam) /= E_Function then
6783 -- A protected operation that is not a function may modify the
6784 -- corresponding object, and cannot apply to a constant. If this
6785 -- is an internal call, the prefix is the type itself.
6787 if Is_Protected_Type (Scope (Nam))
6788 and then not Is_Variable (Obj)
6789 and then (not Is_Entity_Name (Obj)
6790 or else not Is_Type (Entity (Obj)))
6791 then
6792 Error_Msg_N
6793 ("prefix of protected procedure or entry call must be variable",
6794 Entry_Name);
6795 end if;
6797 Actuals := Parameter_Associations (N);
6798 First_Named := First_Named_Actual (N);
6800 Rewrite (N,
6801 Make_Entry_Call_Statement (Loc,
6802 Name => Entry_Name,
6803 Parameter_Associations => Actuals));
6805 Set_First_Named_Actual (N, First_Named);
6806 Set_Analyzed (N, True);
6808 -- Protected functions can return on the secondary stack, in which
6809 -- case we must trigger the transient scope mechanism.
6811 elsif Full_Expander_Active
6812 and then Requires_Transient_Scope (Etype (Nam))
6813 then
6814 Establish_Transient_Scope (N, Sec_Stack => True);
6815 end if;
6816 end Resolve_Entry_Call;
6818 -------------------------
6819 -- Resolve_Equality_Op --
6820 -------------------------
6822 -- Both arguments must have the same type, and the boolean context does
6823 -- not participate in the resolution. The first pass verifies that the
6824 -- interpretation is not ambiguous, and the type of the left argument is
6825 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
6826 -- are strings or aggregates, allocators, or Null, they are ambiguous even
6827 -- though they carry a single (universal) type. Diagnose this case here.
6829 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
6830 L : constant Node_Id := Left_Opnd (N);
6831 R : constant Node_Id := Right_Opnd (N);
6832 T : Entity_Id := Find_Unique_Type (L, R);
6834 procedure Check_If_Expression (Cond : Node_Id);
6835 -- The resolution rule for if expressions requires that each such must
6836 -- have a unique type. This means that if several dependent expressions
6837 -- are of a non-null anonymous access type, and the context does not
6838 -- impose an expected type (as can be the case in an equality operation)
6839 -- the expression must be rejected.
6841 procedure Explain_Redundancy (N : Node_Id);
6842 -- Attempt to explain the nature of a redundant comparison with True. If
6843 -- the expression N is too complex, this routine issues a general error
6844 -- message.
6846 function Find_Unique_Access_Type return Entity_Id;
6847 -- In the case of allocators and access attributes, the context must
6848 -- provide an indication of the specific access type to be used. If
6849 -- one operand is of such a "generic" access type, check whether there
6850 -- is a specific visible access type that has the same designated type.
6851 -- This is semantically dubious, and of no interest to any real code,
6852 -- but c48008a makes it all worthwhile.
6854 -------------------------
6855 -- Check_If_Expression --
6856 -------------------------
6858 procedure Check_If_Expression (Cond : Node_Id) is
6859 Then_Expr : Node_Id;
6860 Else_Expr : Node_Id;
6862 begin
6863 if Nkind (Cond) = N_If_Expression then
6864 Then_Expr := Next (First (Expressions (Cond)));
6865 Else_Expr := Next (Then_Expr);
6867 if Nkind (Then_Expr) /= N_Null
6868 and then Nkind (Else_Expr) /= N_Null
6869 then
6870 Error_Msg_N ("cannot determine type of if expression", Cond);
6871 end if;
6872 end if;
6873 end Check_If_Expression;
6875 ------------------------
6876 -- Explain_Redundancy --
6877 ------------------------
6879 procedure Explain_Redundancy (N : Node_Id) is
6880 Error : Name_Id;
6881 Val : Node_Id;
6882 Val_Id : Entity_Id;
6884 begin
6885 Val := N;
6887 -- Strip the operand down to an entity
6889 loop
6890 if Nkind (Val) = N_Selected_Component then
6891 Val := Selector_Name (Val);
6892 else
6893 exit;
6894 end if;
6895 end loop;
6897 -- The construct denotes an entity
6899 if Is_Entity_Name (Val) and then Present (Entity (Val)) then
6900 Val_Id := Entity (Val);
6902 -- Do not generate an error message when the comparison is done
6903 -- against the enumeration literal Standard.True.
6905 if Ekind (Val_Id) /= E_Enumeration_Literal then
6907 -- Build a customized error message
6909 Name_Len := 0;
6910 Add_Str_To_Name_Buffer ("?r?");
6912 if Ekind (Val_Id) = E_Component then
6913 Add_Str_To_Name_Buffer ("component ");
6915 elsif Ekind (Val_Id) = E_Constant then
6916 Add_Str_To_Name_Buffer ("constant ");
6918 elsif Ekind (Val_Id) = E_Discriminant then
6919 Add_Str_To_Name_Buffer ("discriminant ");
6921 elsif Is_Formal (Val_Id) then
6922 Add_Str_To_Name_Buffer ("parameter ");
6924 elsif Ekind (Val_Id) = E_Variable then
6925 Add_Str_To_Name_Buffer ("variable ");
6926 end if;
6928 Add_Str_To_Name_Buffer ("& is always True!");
6929 Error := Name_Find;
6931 Error_Msg_NE (Get_Name_String (Error), Val, Val_Id);
6932 end if;
6934 -- The construct is too complex to disect, issue a general message
6936 else
6937 Error_Msg_N ("?r?expression is always True!", Val);
6938 end if;
6939 end Explain_Redundancy;
6941 -----------------------------
6942 -- Find_Unique_Access_Type --
6943 -----------------------------
6945 function Find_Unique_Access_Type return Entity_Id is
6946 Acc : Entity_Id;
6947 E : Entity_Id;
6948 S : Entity_Id;
6950 begin
6951 if Ekind_In (Etype (R), E_Allocator_Type,
6952 E_Access_Attribute_Type)
6953 then
6954 Acc := Designated_Type (Etype (R));
6956 elsif Ekind_In (Etype (L), E_Allocator_Type,
6957 E_Access_Attribute_Type)
6958 then
6959 Acc := Designated_Type (Etype (L));
6960 else
6961 return Empty;
6962 end if;
6964 S := Current_Scope;
6965 while S /= Standard_Standard loop
6966 E := First_Entity (S);
6967 while Present (E) loop
6968 if Is_Type (E)
6969 and then Is_Access_Type (E)
6970 and then Ekind (E) /= E_Allocator_Type
6971 and then Designated_Type (E) = Base_Type (Acc)
6972 then
6973 return E;
6974 end if;
6976 Next_Entity (E);
6977 end loop;
6979 S := Scope (S);
6980 end loop;
6982 return Empty;
6983 end Find_Unique_Access_Type;
6985 -- Start of processing for Resolve_Equality_Op
6987 begin
6988 Set_Etype (N, Base_Type (Typ));
6989 Generate_Reference (T, N, ' ');
6991 if T = Any_Fixed then
6992 T := Unique_Fixed_Point_Type (L);
6993 end if;
6995 if T /= Any_Type then
6996 if T = Any_String or else
6997 T = Any_Composite or else
6998 T = Any_Character
6999 then
7000 if T = Any_Character then
7001 Ambiguous_Character (L);
7002 else
7003 Error_Msg_N ("ambiguous operands for equality", N);
7004 end if;
7006 Set_Etype (N, Any_Type);
7007 return;
7009 elsif T = Any_Access
7010 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
7011 then
7012 T := Find_Unique_Access_Type;
7014 if No (T) then
7015 Error_Msg_N ("ambiguous operands for equality", N);
7016 Set_Etype (N, Any_Type);
7017 return;
7018 end if;
7020 -- If expressions must have a single type, and if the context does
7021 -- not impose one the dependent expressions cannot be anonymous
7022 -- access types.
7024 -- Why no similar processing for case expressions???
7026 elsif Ada_Version >= Ada_2012
7027 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
7028 E_Anonymous_Access_Subprogram_Type)
7029 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
7030 E_Anonymous_Access_Subprogram_Type)
7031 then
7032 Check_If_Expression (L);
7033 Check_If_Expression (R);
7034 end if;
7036 Resolve (L, T);
7037 Resolve (R, T);
7039 -- In SPARK, equality operators = and /= for array types other than
7040 -- String are only defined when, for each index position, the
7041 -- operands have equal static bounds.
7043 if Is_Array_Type (T) then
7045 -- Protect call to Matching_Static_Array_Bounds to avoid costly
7046 -- operation if not needed.
7048 if Restriction_Check_Required (SPARK_05)
7049 and then Base_Type (T) /= Standard_String
7050 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
7051 and then Etype (L) /= Any_Composite -- or else L in error
7052 and then Etype (R) /= Any_Composite -- or else R in error
7053 and then not Matching_Static_Array_Bounds (Etype (L), Etype (R))
7054 then
7055 Check_SPARK_Restriction
7056 ("array types should have matching static bounds", N);
7057 end if;
7058 end if;
7060 -- If the unique type is a class-wide type then it will be expanded
7061 -- into a dispatching call to the predefined primitive. Therefore we
7062 -- check here for potential violation of such restriction.
7064 if Is_Class_Wide_Type (T) then
7065 Check_Restriction (No_Dispatching_Calls, N);
7066 end if;
7068 if Warn_On_Redundant_Constructs
7069 and then Comes_From_Source (N)
7070 and then Comes_From_Source (R)
7071 and then Is_Entity_Name (R)
7072 and then Entity (R) = Standard_True
7073 then
7074 Error_Msg_N -- CODEFIX
7075 ("?r?comparison with True is redundant!", N);
7076 Explain_Redundancy (Original_Node (R));
7077 end if;
7079 Check_Unset_Reference (L);
7080 Check_Unset_Reference (R);
7081 Generate_Operator_Reference (N, T);
7082 Check_Low_Bound_Tested (N);
7084 -- If this is an inequality, it may be the implicit inequality
7085 -- created for a user-defined operation, in which case the corres-
7086 -- ponding equality operation is not intrinsic, and the operation
7087 -- cannot be constant-folded. Else fold.
7089 if Nkind (N) = N_Op_Eq
7090 or else Comes_From_Source (Entity (N))
7091 or else Ekind (Entity (N)) = E_Operator
7092 or else Is_Intrinsic_Subprogram
7093 (Corresponding_Equality (Entity (N)))
7094 then
7095 Analyze_Dimension (N);
7096 Eval_Relational_Op (N);
7098 elsif Nkind (N) = N_Op_Ne
7099 and then Is_Abstract_Subprogram (Entity (N))
7100 then
7101 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
7102 end if;
7104 -- Ada 2005: If one operand is an anonymous access type, convert the
7105 -- other operand to it, to ensure that the underlying types match in
7106 -- the back-end. Same for access_to_subprogram, and the conversion
7107 -- verifies that the types are subtype conformant.
7109 -- We apply the same conversion in the case one of the operands is a
7110 -- private subtype of the type of the other.
7112 -- Why the Expander_Active test here ???
7114 if Full_Expander_Active
7115 and then
7116 (Ekind_In (T, E_Anonymous_Access_Type,
7117 E_Anonymous_Access_Subprogram_Type)
7118 or else Is_Private_Type (T))
7119 then
7120 if Etype (L) /= T then
7121 Rewrite (L,
7122 Make_Unchecked_Type_Conversion (Sloc (L),
7123 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
7124 Expression => Relocate_Node (L)));
7125 Analyze_And_Resolve (L, T);
7126 end if;
7128 if (Etype (R)) /= T then
7129 Rewrite (R,
7130 Make_Unchecked_Type_Conversion (Sloc (R),
7131 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
7132 Expression => Relocate_Node (R)));
7133 Analyze_And_Resolve (R, T);
7134 end if;
7135 end if;
7136 end if;
7137 end Resolve_Equality_Op;
7139 ----------------------------------
7140 -- Resolve_Explicit_Dereference --
7141 ----------------------------------
7143 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
7144 Loc : constant Source_Ptr := Sloc (N);
7145 New_N : Node_Id;
7146 P : constant Node_Id := Prefix (N);
7148 P_Typ : Entity_Id;
7149 -- The candidate prefix type, if overloaded
7151 I : Interp_Index;
7152 It : Interp;
7154 begin
7155 Check_Fully_Declared_Prefix (Typ, P);
7156 P_Typ := Empty;
7158 if Is_Overloaded (P) then
7160 -- Use the context type to select the prefix that has the correct
7161 -- designated type. Keep the first match, which will be the inner-
7162 -- most.
7164 Get_First_Interp (P, I, It);
7166 while Present (It.Typ) loop
7167 if Is_Access_Type (It.Typ)
7168 and then Covers (Typ, Designated_Type (It.Typ))
7169 then
7170 if No (P_Typ) then
7171 P_Typ := It.Typ;
7172 end if;
7174 -- Remove access types that do not match, but preserve access
7175 -- to subprogram interpretations, in case a further dereference
7176 -- is needed (see below).
7178 elsif Ekind (It.Typ) /= E_Access_Subprogram_Type then
7179 Remove_Interp (I);
7180 end if;
7182 Get_Next_Interp (I, It);
7183 end loop;
7185 if Present (P_Typ) then
7186 Resolve (P, P_Typ);
7187 Set_Etype (N, Designated_Type (P_Typ));
7189 else
7190 -- If no interpretation covers the designated type of the prefix,
7191 -- this is the pathological case where not all implementations of
7192 -- the prefix allow the interpretation of the node as a call. Now
7193 -- that the expected type is known, Remove other interpretations
7194 -- from prefix, rewrite it as a call, and resolve again, so that
7195 -- the proper call node is generated.
7197 Get_First_Interp (P, I, It);
7198 while Present (It.Typ) loop
7199 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
7200 Remove_Interp (I);
7201 end if;
7203 Get_Next_Interp (I, It);
7204 end loop;
7206 New_N :=
7207 Make_Function_Call (Loc,
7208 Name =>
7209 Make_Explicit_Dereference (Loc,
7210 Prefix => P),
7211 Parameter_Associations => New_List);
7213 Save_Interps (N, New_N);
7214 Rewrite (N, New_N);
7215 Analyze_And_Resolve (N, Typ);
7216 return;
7217 end if;
7219 -- If not overloaded, resolve P with its own type
7221 else
7222 Resolve (P);
7223 end if;
7225 if Is_Access_Type (Etype (P)) then
7226 Apply_Access_Check (N);
7227 end if;
7229 -- If the designated type is a packed unconstrained array type, and the
7230 -- explicit dereference is not in the context of an attribute reference,
7231 -- then we must compute and set the actual subtype, since it is needed
7232 -- by Gigi. The reason we exclude the attribute case is that this is
7233 -- handled fine by Gigi, and in fact we use such attributes to build the
7234 -- actual subtype. We also exclude generated code (which builds actual
7235 -- subtypes directly if they are needed).
7237 if Is_Array_Type (Etype (N))
7238 and then Is_Packed (Etype (N))
7239 and then not Is_Constrained (Etype (N))
7240 and then Nkind (Parent (N)) /= N_Attribute_Reference
7241 and then Comes_From_Source (N)
7242 then
7243 Set_Etype (N, Get_Actual_Subtype (N));
7244 end if;
7246 -- Note: No Eval processing is required for an explicit dereference,
7247 -- because such a name can never be static.
7249 end Resolve_Explicit_Dereference;
7251 -------------------------------------
7252 -- Resolve_Expression_With_Actions --
7253 -------------------------------------
7255 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
7256 begin
7257 Set_Etype (N, Typ);
7258 end Resolve_Expression_With_Actions;
7260 ---------------------------
7261 -- Resolve_If_Expression --
7262 ---------------------------
7264 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id) is
7265 Condition : constant Node_Id := First (Expressions (N));
7266 Then_Expr : constant Node_Id := Next (Condition);
7267 Else_Expr : Node_Id := Next (Then_Expr);
7268 Else_Typ : Entity_Id;
7269 Then_Typ : Entity_Id;
7271 begin
7272 Resolve (Condition, Any_Boolean);
7273 Resolve (Then_Expr, Typ);
7274 Then_Typ := Etype (Then_Expr);
7276 -- When the "then" expression is of a scalar subtype different from the
7277 -- result subtype, then insert a conversion to ensure the generation of
7278 -- a constraint check. The same is done for the else part below, again
7279 -- comparing subtypes rather than base types.
7281 if Is_Scalar_Type (Then_Typ)
7282 and then Then_Typ /= Typ
7283 then
7284 Rewrite (Then_Expr, Convert_To (Typ, Then_Expr));
7285 Analyze_And_Resolve (Then_Expr, Typ);
7286 end if;
7288 -- If ELSE expression present, just resolve using the determined type
7290 if Present (Else_Expr) then
7291 Resolve (Else_Expr, Typ);
7292 Else_Typ := Etype (Else_Expr);
7294 if Is_Scalar_Type (Else_Typ)
7295 and then Else_Typ /= Typ
7296 then
7297 Rewrite (Else_Expr, Convert_To (Typ, Else_Expr));
7298 Analyze_And_Resolve (Else_Expr, Typ);
7299 end if;
7301 -- If no ELSE expression is present, root type must be Standard.Boolean
7302 -- and we provide a Standard.True result converted to the appropriate
7303 -- Boolean type (in case it is a derived boolean type).
7305 elsif Root_Type (Typ) = Standard_Boolean then
7306 Else_Expr :=
7307 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
7308 Analyze_And_Resolve (Else_Expr, Typ);
7309 Append_To (Expressions (N), Else_Expr);
7311 else
7312 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
7313 Append_To (Expressions (N), Error);
7314 end if;
7316 Set_Etype (N, Typ);
7317 Eval_If_Expression (N);
7318 end Resolve_If_Expression;
7320 -------------------------------
7321 -- Resolve_Indexed_Component --
7322 -------------------------------
7324 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
7325 Name : constant Node_Id := Prefix (N);
7326 Expr : Node_Id;
7327 Array_Type : Entity_Id := Empty; -- to prevent junk warning
7328 Index : Node_Id;
7330 begin
7331 if Is_Overloaded (Name) then
7333 -- Use the context type to select the prefix that yields the correct
7334 -- component type.
7336 declare
7337 I : Interp_Index;
7338 It : Interp;
7339 I1 : Interp_Index := 0;
7340 P : constant Node_Id := Prefix (N);
7341 Found : Boolean := False;
7343 begin
7344 Get_First_Interp (P, I, It);
7345 while Present (It.Typ) loop
7346 if (Is_Array_Type (It.Typ)
7347 and then Covers (Typ, Component_Type (It.Typ)))
7348 or else (Is_Access_Type (It.Typ)
7349 and then Is_Array_Type (Designated_Type (It.Typ))
7350 and then
7351 Covers
7352 (Typ,
7353 Component_Type (Designated_Type (It.Typ))))
7354 then
7355 if Found then
7356 It := Disambiguate (P, I1, I, Any_Type);
7358 if It = No_Interp then
7359 Error_Msg_N ("ambiguous prefix for indexing", N);
7360 Set_Etype (N, Typ);
7361 return;
7363 else
7364 Found := True;
7365 Array_Type := It.Typ;
7366 I1 := I;
7367 end if;
7369 else
7370 Found := True;
7371 Array_Type := It.Typ;
7372 I1 := I;
7373 end if;
7374 end if;
7376 Get_Next_Interp (I, It);
7377 end loop;
7378 end;
7380 else
7381 Array_Type := Etype (Name);
7382 end if;
7384 Resolve (Name, Array_Type);
7385 Array_Type := Get_Actual_Subtype_If_Available (Name);
7387 -- If prefix is access type, dereference to get real array type.
7388 -- Note: we do not apply an access check because the expander always
7389 -- introduces an explicit dereference, and the check will happen there.
7391 if Is_Access_Type (Array_Type) then
7392 Array_Type := Designated_Type (Array_Type);
7393 end if;
7395 -- If name was overloaded, set component type correctly now
7396 -- If a misplaced call to an entry family (which has no index types)
7397 -- return. Error will be diagnosed from calling context.
7399 if Is_Array_Type (Array_Type) then
7400 Set_Etype (N, Component_Type (Array_Type));
7401 else
7402 return;
7403 end if;
7405 Index := First_Index (Array_Type);
7406 Expr := First (Expressions (N));
7408 -- The prefix may have resolved to a string literal, in which case its
7409 -- etype has a special representation. This is only possible currently
7410 -- if the prefix is a static concatenation, written in functional
7411 -- notation.
7413 if Ekind (Array_Type) = E_String_Literal_Subtype then
7414 Resolve (Expr, Standard_Positive);
7416 else
7417 while Present (Index) and Present (Expr) loop
7418 Resolve (Expr, Etype (Index));
7419 Check_Unset_Reference (Expr);
7421 if Is_Scalar_Type (Etype (Expr)) then
7422 Apply_Scalar_Range_Check (Expr, Etype (Index));
7423 else
7424 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
7425 end if;
7427 Next_Index (Index);
7428 Next (Expr);
7429 end loop;
7430 end if;
7432 Analyze_Dimension (N);
7434 -- Do not generate the warning on suspicious index if we are analyzing
7435 -- package Ada.Tags; otherwise we will report the warning with the
7436 -- Prims_Ptr field of the dispatch table.
7438 if Scope (Etype (Prefix (N))) = Standard_Standard
7439 or else not
7440 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
7441 Ada_Tags)
7442 then
7443 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
7444 Eval_Indexed_Component (N);
7445 end if;
7447 -- If the array type is atomic, and is packed, and we are in a left side
7448 -- context, then this is worth a warning, since we have a situation
7449 -- where the access to the component may cause extra read/writes of
7450 -- the atomic array object, which could be considered unexpected.
7452 if Nkind (N) = N_Indexed_Component
7453 and then (Is_Atomic (Array_Type)
7454 or else (Is_Entity_Name (Prefix (N))
7455 and then Is_Atomic (Entity (Prefix (N)))))
7456 and then Is_Bit_Packed_Array (Array_Type)
7457 and then Is_LHS (N)
7458 then
7459 Error_Msg_N ("??assignment to component of packed atomic array",
7460 Prefix (N));
7461 Error_Msg_N ("??\may cause unexpected accesses to atomic object",
7462 Prefix (N));
7463 end if;
7464 end Resolve_Indexed_Component;
7466 -----------------------------
7467 -- Resolve_Integer_Literal --
7468 -----------------------------
7470 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
7471 begin
7472 Set_Etype (N, Typ);
7473 Eval_Integer_Literal (N);
7474 end Resolve_Integer_Literal;
7476 --------------------------------
7477 -- Resolve_Intrinsic_Operator --
7478 --------------------------------
7480 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
7481 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7482 Op : Entity_Id;
7483 Orig_Op : constant Entity_Id := Entity (N);
7484 Arg1 : Node_Id;
7485 Arg2 : Node_Id;
7487 function Convert_Operand (Opnd : Node_Id) return Node_Id;
7488 -- If the operand is a literal, it cannot be the expression in a
7489 -- conversion. Use a qualified expression instead.
7491 function Convert_Operand (Opnd : Node_Id) return Node_Id is
7492 Loc : constant Source_Ptr := Sloc (Opnd);
7493 Res : Node_Id;
7494 begin
7495 if Nkind_In (Opnd, N_Integer_Literal, N_Real_Literal) then
7496 Res :=
7497 Make_Qualified_Expression (Loc,
7498 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
7499 Expression => Relocate_Node (Opnd));
7500 Analyze (Res);
7502 else
7503 Res := Unchecked_Convert_To (Btyp, Opnd);
7504 end if;
7506 return Res;
7507 end Convert_Operand;
7509 -- Start of processing for Resolve_Intrinsic_Operator
7511 begin
7512 -- We must preserve the original entity in a generic setting, so that
7513 -- the legality of the operation can be verified in an instance.
7515 if not Full_Expander_Active then
7516 return;
7517 end if;
7519 Op := Entity (N);
7520 while Scope (Op) /= Standard_Standard loop
7521 Op := Homonym (Op);
7522 pragma Assert (Present (Op));
7523 end loop;
7525 Set_Entity (N, Op);
7526 Set_Is_Overloaded (N, False);
7528 -- If the result or operand types are private, rewrite with unchecked
7529 -- conversions on the operands and the result, to expose the proper
7530 -- underlying numeric type.
7532 if Is_Private_Type (Typ)
7533 or else Is_Private_Type (Etype (Left_Opnd (N)))
7534 or else Is_Private_Type (Etype (Right_Opnd (N)))
7535 then
7536 Arg1 := Convert_Operand (Left_Opnd (N));
7537 -- Unchecked_Convert_To (Btyp, Left_Opnd (N));
7538 -- What on earth is this commented out fragment of code???
7540 if Nkind (N) = N_Op_Expon then
7541 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
7542 else
7543 Arg2 := Convert_Operand (Right_Opnd (N));
7544 end if;
7546 if Nkind (Arg1) = N_Type_Conversion then
7547 Save_Interps (Left_Opnd (N), Expression (Arg1));
7548 end if;
7550 if Nkind (Arg2) = N_Type_Conversion then
7551 Save_Interps (Right_Opnd (N), Expression (Arg2));
7552 end if;
7554 Set_Left_Opnd (N, Arg1);
7555 Set_Right_Opnd (N, Arg2);
7557 Set_Etype (N, Btyp);
7558 Rewrite (N, Unchecked_Convert_To (Typ, N));
7559 Resolve (N, Typ);
7561 elsif Typ /= Etype (Left_Opnd (N))
7562 or else Typ /= Etype (Right_Opnd (N))
7563 then
7564 -- Add explicit conversion where needed, and save interpretations in
7565 -- case operands are overloaded. If the context is a VMS operation,
7566 -- assert that the conversion is legal (the operands have the proper
7567 -- types to select the VMS intrinsic). Note that in rare cases the
7568 -- VMS operators may be visible, but the default System is being used
7569 -- and Address is a private type.
7571 Arg1 := Convert_To (Typ, Left_Opnd (N));
7572 Arg2 := Convert_To (Typ, Right_Opnd (N));
7574 if Nkind (Arg1) = N_Type_Conversion then
7575 Save_Interps (Left_Opnd (N), Expression (Arg1));
7577 if Is_VMS_Operator (Orig_Op) then
7578 Set_Conversion_OK (Arg1);
7579 end if;
7580 else
7581 Save_Interps (Left_Opnd (N), Arg1);
7582 end if;
7584 if Nkind (Arg2) = N_Type_Conversion then
7585 Save_Interps (Right_Opnd (N), Expression (Arg2));
7587 if Is_VMS_Operator (Orig_Op) then
7588 Set_Conversion_OK (Arg2);
7589 end if;
7590 else
7591 Save_Interps (Right_Opnd (N), Arg2);
7592 end if;
7594 Rewrite (Left_Opnd (N), Arg1);
7595 Rewrite (Right_Opnd (N), Arg2);
7596 Analyze (Arg1);
7597 Analyze (Arg2);
7598 Resolve_Arithmetic_Op (N, Typ);
7600 else
7601 Resolve_Arithmetic_Op (N, Typ);
7602 end if;
7603 end Resolve_Intrinsic_Operator;
7605 --------------------------------------
7606 -- Resolve_Intrinsic_Unary_Operator --
7607 --------------------------------------
7609 procedure Resolve_Intrinsic_Unary_Operator
7610 (N : Node_Id;
7611 Typ : Entity_Id)
7613 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7614 Op : Entity_Id;
7615 Arg2 : Node_Id;
7617 begin
7618 Op := Entity (N);
7619 while Scope (Op) /= Standard_Standard loop
7620 Op := Homonym (Op);
7621 pragma Assert (Present (Op));
7622 end loop;
7624 Set_Entity (N, Op);
7626 if Is_Private_Type (Typ) then
7627 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
7628 Save_Interps (Right_Opnd (N), Expression (Arg2));
7630 Set_Right_Opnd (N, Arg2);
7632 Set_Etype (N, Btyp);
7633 Rewrite (N, Unchecked_Convert_To (Typ, N));
7634 Resolve (N, Typ);
7636 else
7637 Resolve_Unary_Op (N, Typ);
7638 end if;
7639 end Resolve_Intrinsic_Unary_Operator;
7641 ------------------------
7642 -- Resolve_Logical_Op --
7643 ------------------------
7645 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
7646 B_Typ : Entity_Id;
7648 begin
7649 Check_No_Direct_Boolean_Operators (N);
7651 -- Predefined operations on scalar types yield the base type. On the
7652 -- other hand, logical operations on arrays yield the type of the
7653 -- arguments (and the context).
7655 if Is_Array_Type (Typ) then
7656 B_Typ := Typ;
7657 else
7658 B_Typ := Base_Type (Typ);
7659 end if;
7661 -- OK if this is a VMS-specific intrinsic operation
7663 if Is_VMS_Operator (Entity (N)) then
7664 null;
7666 -- The following test is required because the operands of the operation
7667 -- may be literals, in which case the resulting type appears to be
7668 -- compatible with a signed integer type, when in fact it is compatible
7669 -- only with modular types. If the context itself is universal, the
7670 -- operation is illegal.
7672 elsif not Valid_Boolean_Arg (Typ) then
7673 Error_Msg_N ("invalid context for logical operation", N);
7674 Set_Etype (N, Any_Type);
7675 return;
7677 elsif Typ = Any_Modular then
7678 Error_Msg_N
7679 ("no modular type available in this context", N);
7680 Set_Etype (N, Any_Type);
7681 return;
7683 elsif Is_Modular_Integer_Type (Typ)
7684 and then Etype (Left_Opnd (N)) = Universal_Integer
7685 and then Etype (Right_Opnd (N)) = Universal_Integer
7686 then
7687 Check_For_Visible_Operator (N, B_Typ);
7688 end if;
7690 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
7691 -- is active and the result type is standard Boolean (do not mess with
7692 -- ops that return a nonstandard Boolean type, because something strange
7693 -- is going on).
7695 -- Note: you might expect this replacement to be done during expansion,
7696 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
7697 -- is used, no part of the right operand of an "and" or "or" operator
7698 -- should be executed if the left operand would short-circuit the
7699 -- evaluation of the corresponding "and then" or "or else". If we left
7700 -- the replacement to expansion time, then run-time checks associated
7701 -- with such operands would be evaluated unconditionally, due to being
7702 -- before the condition prior to the rewriting as short-circuit forms
7703 -- during expansion.
7705 if Short_Circuit_And_Or
7706 and then B_Typ = Standard_Boolean
7707 and then Nkind_In (N, N_Op_And, N_Op_Or)
7708 then
7709 if Nkind (N) = N_Op_And then
7710 Rewrite (N,
7711 Make_And_Then (Sloc (N),
7712 Left_Opnd => Relocate_Node (Left_Opnd (N)),
7713 Right_Opnd => Relocate_Node (Right_Opnd (N))));
7714 Analyze_And_Resolve (N, B_Typ);
7716 -- Case of OR changed to OR ELSE
7718 else
7719 Rewrite (N,
7720 Make_Or_Else (Sloc (N),
7721 Left_Opnd => Relocate_Node (Left_Opnd (N)),
7722 Right_Opnd => Relocate_Node (Right_Opnd (N))));
7723 Analyze_And_Resolve (N, B_Typ);
7724 end if;
7726 -- Return now, since analysis of the rewritten ops will take care of
7727 -- other reference bookkeeping and expression folding.
7729 return;
7730 end if;
7732 Resolve (Left_Opnd (N), B_Typ);
7733 Resolve (Right_Opnd (N), B_Typ);
7735 Check_Unset_Reference (Left_Opnd (N));
7736 Check_Unset_Reference (Right_Opnd (N));
7738 Set_Etype (N, B_Typ);
7739 Generate_Operator_Reference (N, B_Typ);
7740 Eval_Logical_Op (N);
7742 -- In SPARK, logical operations AND, OR and XOR for arrays are defined
7743 -- only when both operands have same static lower and higher bounds. Of
7744 -- course the types have to match, so only check if operands are
7745 -- compatible and the node itself has no errors.
7747 if Is_Array_Type (B_Typ)
7748 and then Nkind (N) in N_Binary_Op
7749 then
7750 declare
7751 Left_Typ : constant Node_Id := Etype (Left_Opnd (N));
7752 Right_Typ : constant Node_Id := Etype (Right_Opnd (N));
7754 begin
7755 -- Protect call to Matching_Static_Array_Bounds to avoid costly
7756 -- operation if not needed.
7758 if Restriction_Check_Required (SPARK_05)
7759 and then Base_Type (Left_Typ) = Base_Type (Right_Typ)
7760 and then Left_Typ /= Any_Composite -- or Left_Opnd in error
7761 and then Right_Typ /= Any_Composite -- or Right_Opnd in error
7762 and then not Matching_Static_Array_Bounds (Left_Typ, Right_Typ)
7763 then
7764 Check_SPARK_Restriction
7765 ("array types should have matching static bounds", N);
7766 end if;
7767 end;
7768 end if;
7770 Check_Function_Writable_Actuals (N);
7771 end Resolve_Logical_Op;
7773 ---------------------------
7774 -- Resolve_Membership_Op --
7775 ---------------------------
7777 -- The context can only be a boolean type, and does not determine the
7778 -- arguments. Arguments should be unambiguous, but the preference rule for
7779 -- universal types applies.
7781 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
7782 pragma Warnings (Off, Typ);
7784 L : constant Node_Id := Left_Opnd (N);
7785 R : constant Node_Id := Right_Opnd (N);
7786 T : Entity_Id;
7788 procedure Resolve_Set_Membership;
7789 -- Analysis has determined a unique type for the left operand. Use it to
7790 -- resolve the disjuncts.
7792 ----------------------------
7793 -- Resolve_Set_Membership --
7794 ----------------------------
7796 procedure Resolve_Set_Membership is
7797 Alt : Node_Id;
7798 Ltyp : constant Entity_Id := Etype (L);
7800 begin
7801 Resolve (L, Ltyp);
7803 Alt := First (Alternatives (N));
7804 while Present (Alt) loop
7806 -- Alternative is an expression, a range
7807 -- or a subtype mark.
7809 if not Is_Entity_Name (Alt)
7810 or else not Is_Type (Entity (Alt))
7811 then
7812 Resolve (Alt, Ltyp);
7813 end if;
7815 Next (Alt);
7816 end loop;
7818 -- Check for duplicates for discrete case
7820 if Is_Discrete_Type (Ltyp) then
7821 declare
7822 type Ent is record
7823 Alt : Node_Id;
7824 Val : Uint;
7825 end record;
7827 Alts : array (0 .. List_Length (Alternatives (N))) of Ent;
7828 Nalts : Nat;
7830 begin
7831 -- Loop checking duplicates. This is quadratic, but giant sets
7832 -- are unlikely in this context so it's a reasonable choice.
7834 Nalts := 0;
7835 Alt := First (Alternatives (N));
7836 while Present (Alt) loop
7837 if Is_Static_Expression (Alt)
7838 and then (Nkind_In (Alt, N_Integer_Literal,
7839 N_Character_Literal)
7840 or else Nkind (Alt) in N_Has_Entity)
7841 then
7842 Nalts := Nalts + 1;
7843 Alts (Nalts) := (Alt, Expr_Value (Alt));
7845 for J in 1 .. Nalts - 1 loop
7846 if Alts (J).Val = Alts (Nalts).Val then
7847 Error_Msg_Sloc := Sloc (Alts (J).Alt);
7848 Error_Msg_N ("duplicate of value given#??", Alt);
7849 end if;
7850 end loop;
7851 end if;
7853 Alt := Next (Alt);
7854 end loop;
7855 end;
7856 end if;
7857 end Resolve_Set_Membership;
7859 -- Start of processing for Resolve_Membership_Op
7861 begin
7862 if L = Error or else R = Error then
7863 return;
7864 end if;
7866 if Present (Alternatives (N)) then
7867 Resolve_Set_Membership;
7868 Check_Function_Writable_Actuals (N);
7869 return;
7871 elsif not Is_Overloaded (R)
7872 and then
7873 (Etype (R) = Universal_Integer
7874 or else
7875 Etype (R) = Universal_Real)
7876 and then Is_Overloaded (L)
7877 then
7878 T := Etype (R);
7880 -- Ada 2005 (AI-251): Support the following case:
7882 -- type I is interface;
7883 -- type T is tagged ...
7885 -- function Test (O : I'Class) is
7886 -- begin
7887 -- return O in T'Class.
7888 -- end Test;
7890 -- In this case we have nothing else to do. The membership test will be
7891 -- done at run time.
7893 elsif Ada_Version >= Ada_2005
7894 and then Is_Class_Wide_Type (Etype (L))
7895 and then Is_Interface (Etype (L))
7896 and then Is_Class_Wide_Type (Etype (R))
7897 and then not Is_Interface (Etype (R))
7898 then
7899 return;
7900 else
7901 T := Intersect_Types (L, R);
7902 end if;
7904 -- If mixed-mode operations are present and operands are all literal,
7905 -- the only interpretation involves Duration, which is probably not
7906 -- the intention of the programmer.
7908 if T = Any_Fixed then
7909 T := Unique_Fixed_Point_Type (N);
7911 if T = Any_Type then
7912 return;
7913 end if;
7914 end if;
7916 Resolve (L, T);
7917 Check_Unset_Reference (L);
7919 if Nkind (R) = N_Range
7920 and then not Is_Scalar_Type (T)
7921 then
7922 Error_Msg_N ("scalar type required for range", R);
7923 end if;
7925 if Is_Entity_Name (R) then
7926 Freeze_Expression (R);
7927 else
7928 Resolve (R, T);
7929 Check_Unset_Reference (R);
7930 end if;
7932 Eval_Membership_Op (N);
7933 Check_Function_Writable_Actuals (N);
7934 end Resolve_Membership_Op;
7936 ------------------
7937 -- Resolve_Null --
7938 ------------------
7940 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
7941 Loc : constant Source_Ptr := Sloc (N);
7943 begin
7944 -- Handle restriction against anonymous null access values This
7945 -- restriction can be turned off using -gnatdj.
7947 -- Ada 2005 (AI-231): Remove restriction
7949 if Ada_Version < Ada_2005
7950 and then not Debug_Flag_J
7951 and then Ekind (Typ) = E_Anonymous_Access_Type
7952 and then Comes_From_Source (N)
7953 then
7954 -- In the common case of a call which uses an explicitly null value
7955 -- for an access parameter, give specialized error message.
7957 if Nkind (Parent (N)) in N_Subprogram_Call then
7958 Error_Msg_N
7959 ("null is not allowed as argument for an access parameter", N);
7961 -- Standard message for all other cases (are there any?)
7963 else
7964 Error_Msg_N
7965 ("null cannot be of an anonymous access type", N);
7966 end if;
7967 end if;
7969 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
7970 -- assignment to a null-excluding object
7972 if Ada_Version >= Ada_2005
7973 and then Can_Never_Be_Null (Typ)
7974 and then Nkind (Parent (N)) = N_Assignment_Statement
7975 then
7976 if not Inside_Init_Proc then
7977 Insert_Action
7978 (Compile_Time_Constraint_Error (N,
7979 "(Ada 2005) null not allowed in null-excluding objects??"),
7980 Make_Raise_Constraint_Error (Loc,
7981 Reason => CE_Access_Check_Failed));
7982 else
7983 Insert_Action (N,
7984 Make_Raise_Constraint_Error (Loc,
7985 Reason => CE_Access_Check_Failed));
7986 end if;
7987 end if;
7989 -- In a distributed context, null for a remote access to subprogram may
7990 -- need to be replaced with a special record aggregate. In this case,
7991 -- return after having done the transformation.
7993 if (Ekind (Typ) = E_Record_Type
7994 or else Is_Remote_Access_To_Subprogram_Type (Typ))
7995 and then Remote_AST_Null_Value (N, Typ)
7996 then
7997 return;
7998 end if;
8000 -- The null literal takes its type from the context
8002 Set_Etype (N, Typ);
8003 end Resolve_Null;
8005 -----------------------
8006 -- Resolve_Op_Concat --
8007 -----------------------
8009 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
8011 -- We wish to avoid deep recursion, because concatenations are often
8012 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
8013 -- operands nonrecursively until we find something that is not a simple
8014 -- concatenation (A in this case). We resolve that, and then walk back
8015 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
8016 -- to do the rest of the work at each level. The Parent pointers allow
8017 -- us to avoid recursion, and thus avoid running out of memory. See also
8018 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
8020 NN : Node_Id := N;
8021 Op1 : Node_Id;
8023 begin
8024 -- The following code is equivalent to:
8026 -- Resolve_Op_Concat_First (NN, Typ);
8027 -- Resolve_Op_Concat_Arg (N, ...);
8028 -- Resolve_Op_Concat_Rest (N, Typ);
8030 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
8031 -- operand is a concatenation.
8033 -- Walk down left operands
8035 loop
8036 Resolve_Op_Concat_First (NN, Typ);
8037 Op1 := Left_Opnd (NN);
8038 exit when not (Nkind (Op1) = N_Op_Concat
8039 and then not Is_Array_Type (Component_Type (Typ))
8040 and then Entity (Op1) = Entity (NN));
8041 NN := Op1;
8042 end loop;
8044 -- Now (given the above example) NN is A&B and Op1 is A
8046 -- First resolve Op1 ...
8048 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
8050 -- ... then walk NN back up until we reach N (where we started), calling
8051 -- Resolve_Op_Concat_Rest along the way.
8053 loop
8054 Resolve_Op_Concat_Rest (NN, Typ);
8055 exit when NN = N;
8056 NN := Parent (NN);
8057 end loop;
8059 if Base_Type (Etype (N)) /= Standard_String then
8060 Check_SPARK_Restriction
8061 ("result of concatenation should have type String", N);
8062 end if;
8063 end Resolve_Op_Concat;
8065 ---------------------------
8066 -- Resolve_Op_Concat_Arg --
8067 ---------------------------
8069 procedure Resolve_Op_Concat_Arg
8070 (N : Node_Id;
8071 Arg : Node_Id;
8072 Typ : Entity_Id;
8073 Is_Comp : Boolean)
8075 Btyp : constant Entity_Id := Base_Type (Typ);
8076 Ctyp : constant Entity_Id := Component_Type (Typ);
8078 begin
8079 if In_Instance then
8080 if Is_Comp
8081 or else (not Is_Overloaded (Arg)
8082 and then Etype (Arg) /= Any_Composite
8083 and then Covers (Ctyp, Etype (Arg)))
8084 then
8085 Resolve (Arg, Ctyp);
8086 else
8087 Resolve (Arg, Btyp);
8088 end if;
8090 -- If both Array & Array and Array & Component are visible, there is a
8091 -- potential ambiguity that must be reported.
8093 elsif Has_Compatible_Type (Arg, Ctyp) then
8094 if Nkind (Arg) = N_Aggregate
8095 and then Is_Composite_Type (Ctyp)
8096 then
8097 if Is_Private_Type (Ctyp) then
8098 Resolve (Arg, Btyp);
8100 -- If the operation is user-defined and not overloaded use its
8101 -- profile. The operation may be a renaming, in which case it has
8102 -- been rewritten, and we want the original profile.
8104 elsif not Is_Overloaded (N)
8105 and then Comes_From_Source (Entity (Original_Node (N)))
8106 and then Ekind (Entity (Original_Node (N))) = E_Function
8107 then
8108 Resolve (Arg,
8109 Etype
8110 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
8111 return;
8113 -- Otherwise an aggregate may match both the array type and the
8114 -- component type.
8116 else
8117 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
8118 Set_Etype (Arg, Any_Type);
8119 end if;
8121 else
8122 if Is_Overloaded (Arg)
8123 and then Has_Compatible_Type (Arg, Typ)
8124 and then Etype (Arg) /= Any_Type
8125 then
8126 declare
8127 I : Interp_Index;
8128 It : Interp;
8129 Func : Entity_Id;
8131 begin
8132 Get_First_Interp (Arg, I, It);
8133 Func := It.Nam;
8134 Get_Next_Interp (I, It);
8136 -- Special-case the error message when the overloading is
8137 -- caused by a function that yields an array and can be
8138 -- called without parameters.
8140 if It.Nam = Func then
8141 Error_Msg_Sloc := Sloc (Func);
8142 Error_Msg_N ("ambiguous call to function#", Arg);
8143 Error_Msg_NE
8144 ("\\interpretation as call yields&", Arg, Typ);
8145 Error_Msg_NE
8146 ("\\interpretation as indexing of call yields&",
8147 Arg, Component_Type (Typ));
8149 else
8150 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
8152 Get_First_Interp (Arg, I, It);
8153 while Present (It.Nam) loop
8154 Error_Msg_Sloc := Sloc (It.Nam);
8156 if Base_Type (It.Typ) = Btyp
8157 or else
8158 Base_Type (It.Typ) = Base_Type (Ctyp)
8159 then
8160 Error_Msg_N -- CODEFIX
8161 ("\\possible interpretation#", Arg);
8162 end if;
8164 Get_Next_Interp (I, It);
8165 end loop;
8166 end if;
8167 end;
8168 end if;
8170 Resolve (Arg, Component_Type (Typ));
8172 if Nkind (Arg) = N_String_Literal then
8173 Set_Etype (Arg, Component_Type (Typ));
8174 end if;
8176 if Arg = Left_Opnd (N) then
8177 Set_Is_Component_Left_Opnd (N);
8178 else
8179 Set_Is_Component_Right_Opnd (N);
8180 end if;
8181 end if;
8183 else
8184 Resolve (Arg, Btyp);
8185 end if;
8187 -- Concatenation is restricted in SPARK: each operand must be either a
8188 -- string literal, the name of a string constant, a static character or
8189 -- string expression, or another concatenation. Arg cannot be a
8190 -- concatenation here as callers of Resolve_Op_Concat_Arg call it
8191 -- separately on each final operand, past concatenation operations.
8193 if Is_Character_Type (Etype (Arg)) then
8194 if not Is_Static_Expression (Arg) then
8195 Check_SPARK_Restriction
8196 ("character operand for concatenation should be static", Arg);
8197 end if;
8199 elsif Is_String_Type (Etype (Arg)) then
8200 if not (Nkind_In (Arg, N_Identifier, N_Expanded_Name)
8201 and then Is_Constant_Object (Entity (Arg)))
8202 and then not Is_Static_Expression (Arg)
8203 then
8204 Check_SPARK_Restriction
8205 ("string operand for concatenation should be static", Arg);
8206 end if;
8208 -- Do not issue error on an operand that is neither a character nor a
8209 -- string, as the error is issued in Resolve_Op_Concat.
8211 else
8212 null;
8213 end if;
8215 Check_Unset_Reference (Arg);
8216 end Resolve_Op_Concat_Arg;
8218 -----------------------------
8219 -- Resolve_Op_Concat_First --
8220 -----------------------------
8222 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
8223 Btyp : constant Entity_Id := Base_Type (Typ);
8224 Op1 : constant Node_Id := Left_Opnd (N);
8225 Op2 : constant Node_Id := Right_Opnd (N);
8227 begin
8228 -- The parser folds an enormous sequence of concatenations of string
8229 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
8230 -- in the right operand. If the expression resolves to a predefined "&"
8231 -- operator, all is well. Otherwise, the parser's folding is wrong, so
8232 -- we give an error. See P_Simple_Expression in Par.Ch4.
8234 if Nkind (Op2) = N_String_Literal
8235 and then Is_Folded_In_Parser (Op2)
8236 and then Ekind (Entity (N)) = E_Function
8237 then
8238 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
8239 and then String_Length (Strval (Op1)) = 0);
8240 Error_Msg_N ("too many user-defined concatenations", N);
8241 return;
8242 end if;
8244 Set_Etype (N, Btyp);
8246 if Is_Limited_Composite (Btyp) then
8247 Error_Msg_N ("concatenation not available for limited array", N);
8248 Explain_Limited_Type (Btyp, N);
8249 end if;
8250 end Resolve_Op_Concat_First;
8252 ----------------------------
8253 -- Resolve_Op_Concat_Rest --
8254 ----------------------------
8256 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
8257 Op1 : constant Node_Id := Left_Opnd (N);
8258 Op2 : constant Node_Id := Right_Opnd (N);
8260 begin
8261 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
8263 Generate_Operator_Reference (N, Typ);
8265 if Is_String_Type (Typ) then
8266 Eval_Concatenation (N);
8267 end if;
8269 -- If this is not a static concatenation, but the result is a string
8270 -- type (and not an array of strings) ensure that static string operands
8271 -- have their subtypes properly constructed.
8273 if Nkind (N) /= N_String_Literal
8274 and then Is_Character_Type (Component_Type (Typ))
8275 then
8276 Set_String_Literal_Subtype (Op1, Typ);
8277 Set_String_Literal_Subtype (Op2, Typ);
8278 end if;
8279 end Resolve_Op_Concat_Rest;
8281 ----------------------
8282 -- Resolve_Op_Expon --
8283 ----------------------
8285 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
8286 B_Typ : constant Entity_Id := Base_Type (Typ);
8288 begin
8289 -- Catch attempts to do fixed-point exponentiation with universal
8290 -- operands, which is a case where the illegality is not caught during
8291 -- normal operator analysis.
8293 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
8294 Error_Msg_N ("exponentiation not available for fixed point", N);
8295 return;
8297 elsif Nkind (Parent (N)) in N_Op
8298 and then Is_Fixed_Point_Type (Etype (Parent (N)))
8299 and then Etype (N) = Universal_Real
8300 and then Comes_From_Source (N)
8301 then
8302 Error_Msg_N ("exponentiation not available for fixed point", N);
8303 return;
8304 end if;
8306 if Comes_From_Source (N)
8307 and then Ekind (Entity (N)) = E_Function
8308 and then Is_Imported (Entity (N))
8309 and then Is_Intrinsic_Subprogram (Entity (N))
8310 then
8311 Resolve_Intrinsic_Operator (N, Typ);
8312 return;
8313 end if;
8315 if Etype (Left_Opnd (N)) = Universal_Integer
8316 or else Etype (Left_Opnd (N)) = Universal_Real
8317 then
8318 Check_For_Visible_Operator (N, B_Typ);
8319 end if;
8321 -- We do the resolution using the base type, because intermediate values
8322 -- in expressions always are of the base type, not a subtype of it.
8324 Resolve (Left_Opnd (N), B_Typ);
8325 Resolve (Right_Opnd (N), Standard_Integer);
8327 Check_Unset_Reference (Left_Opnd (N));
8328 Check_Unset_Reference (Right_Opnd (N));
8330 Set_Etype (N, B_Typ);
8331 Generate_Operator_Reference (N, B_Typ);
8333 Analyze_Dimension (N);
8335 if Ada_Version >= Ada_2012 and then Has_Dimension_System (B_Typ) then
8336 -- Evaluate the exponentiation operator for dimensioned type
8338 Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ);
8339 else
8340 Eval_Op_Expon (N);
8341 end if;
8343 -- Set overflow checking bit. Much cleverer code needed here eventually
8344 -- and perhaps the Resolve routines should be separated for the various
8345 -- arithmetic operations, since they will need different processing. ???
8347 if Nkind (N) in N_Op then
8348 if not Overflow_Checks_Suppressed (Etype (N)) then
8349 Enable_Overflow_Check (N);
8350 end if;
8351 end if;
8352 end Resolve_Op_Expon;
8354 --------------------
8355 -- Resolve_Op_Not --
8356 --------------------
8358 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
8359 B_Typ : Entity_Id;
8361 function Parent_Is_Boolean return Boolean;
8362 -- This function determines if the parent node is a boolean operator or
8363 -- operation (comparison op, membership test, or short circuit form) and
8364 -- the not in question is the left operand of this operation. Note that
8365 -- if the not is in parens, then false is returned.
8367 -----------------------
8368 -- Parent_Is_Boolean --
8369 -----------------------
8371 function Parent_Is_Boolean return Boolean is
8372 begin
8373 if Paren_Count (N) /= 0 then
8374 return False;
8376 else
8377 case Nkind (Parent (N)) is
8378 when N_Op_And |
8379 N_Op_Eq |
8380 N_Op_Ge |
8381 N_Op_Gt |
8382 N_Op_Le |
8383 N_Op_Lt |
8384 N_Op_Ne |
8385 N_Op_Or |
8386 N_Op_Xor |
8387 N_In |
8388 N_Not_In |
8389 N_And_Then |
8390 N_Or_Else =>
8392 return Left_Opnd (Parent (N)) = N;
8394 when others =>
8395 return False;
8396 end case;
8397 end if;
8398 end Parent_Is_Boolean;
8400 -- Start of processing for Resolve_Op_Not
8402 begin
8403 -- Predefined operations on scalar types yield the base type. On the
8404 -- other hand, logical operations on arrays yield the type of the
8405 -- arguments (and the context).
8407 if Is_Array_Type (Typ) then
8408 B_Typ := Typ;
8409 else
8410 B_Typ := Base_Type (Typ);
8411 end if;
8413 if Is_VMS_Operator (Entity (N)) then
8414 null;
8416 -- Straightforward case of incorrect arguments
8418 elsif not Valid_Boolean_Arg (Typ) then
8419 Error_Msg_N ("invalid operand type for operator&", N);
8420 Set_Etype (N, Any_Type);
8421 return;
8423 -- Special case of probable missing parens
8425 elsif Typ = Universal_Integer or else Typ = Any_Modular then
8426 if Parent_Is_Boolean then
8427 Error_Msg_N
8428 ("operand of not must be enclosed in parentheses",
8429 Right_Opnd (N));
8430 else
8431 Error_Msg_N
8432 ("no modular type available in this context", N);
8433 end if;
8435 Set_Etype (N, Any_Type);
8436 return;
8438 -- OK resolution of NOT
8440 else
8441 -- Warn if non-boolean types involved. This is a case like not a < b
8442 -- where a and b are modular, where we will get (not a) < b and most
8443 -- likely not (a < b) was intended.
8445 if Warn_On_Questionable_Missing_Parens
8446 and then not Is_Boolean_Type (Typ)
8447 and then Parent_Is_Boolean
8448 then
8449 Error_Msg_N ("?q?not expression should be parenthesized here!", N);
8450 end if;
8452 -- Warn on double negation if checking redundant constructs
8454 if Warn_On_Redundant_Constructs
8455 and then Comes_From_Source (N)
8456 and then Comes_From_Source (Right_Opnd (N))
8457 and then Root_Type (Typ) = Standard_Boolean
8458 and then Nkind (Right_Opnd (N)) = N_Op_Not
8459 then
8460 Error_Msg_N ("redundant double negation?r?", N);
8461 end if;
8463 -- Complete resolution and evaluation of NOT
8465 Resolve (Right_Opnd (N), B_Typ);
8466 Check_Unset_Reference (Right_Opnd (N));
8467 Set_Etype (N, B_Typ);
8468 Generate_Operator_Reference (N, B_Typ);
8469 Eval_Op_Not (N);
8470 end if;
8471 end Resolve_Op_Not;
8473 -----------------------------
8474 -- Resolve_Operator_Symbol --
8475 -----------------------------
8477 -- Nothing to be done, all resolved already
8479 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
8480 pragma Warnings (Off, N);
8481 pragma Warnings (Off, Typ);
8483 begin
8484 null;
8485 end Resolve_Operator_Symbol;
8487 ----------------------------------
8488 -- Resolve_Qualified_Expression --
8489 ----------------------------------
8491 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
8492 pragma Warnings (Off, Typ);
8494 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
8495 Expr : constant Node_Id := Expression (N);
8497 begin
8498 Resolve (Expr, Target_Typ);
8500 -- Protect call to Matching_Static_Array_Bounds to avoid costly
8501 -- operation if not needed.
8503 if Restriction_Check_Required (SPARK_05)
8504 and then Is_Array_Type (Target_Typ)
8505 and then Is_Array_Type (Etype (Expr))
8506 and then Etype (Expr) /= Any_Composite -- or else Expr in error
8507 and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr))
8508 then
8509 Check_SPARK_Restriction
8510 ("array types should have matching static bounds", N);
8511 end if;
8513 -- A qualified expression requires an exact match of the type, class-
8514 -- wide matching is not allowed. However, if the qualifying type is
8515 -- specific and the expression has a class-wide type, it may still be
8516 -- okay, since it can be the result of the expansion of a call to a
8517 -- dispatching function, so we also have to check class-wideness of the
8518 -- type of the expression's original node.
8520 if (Is_Class_Wide_Type (Target_Typ)
8521 or else
8522 (Is_Class_Wide_Type (Etype (Expr))
8523 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
8524 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
8525 then
8526 Wrong_Type (Expr, Target_Typ);
8527 end if;
8529 -- If the target type is unconstrained, then we reset the type of the
8530 -- result from the type of the expression. For other cases, the actual
8531 -- subtype of the expression is the target type.
8533 if Is_Composite_Type (Target_Typ)
8534 and then not Is_Constrained (Target_Typ)
8535 then
8536 Set_Etype (N, Etype (Expr));
8537 end if;
8539 Analyze_Dimension (N);
8540 Eval_Qualified_Expression (N);
8541 end Resolve_Qualified_Expression;
8543 -------------------
8544 -- Resolve_Range --
8545 -------------------
8547 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
8548 L : constant Node_Id := Low_Bound (N);
8549 H : constant Node_Id := High_Bound (N);
8551 function First_Last_Ref return Boolean;
8552 -- Returns True if N is of the form X'First .. X'Last where X is the
8553 -- same entity for both attributes.
8555 --------------------
8556 -- First_Last_Ref --
8557 --------------------
8559 function First_Last_Ref return Boolean is
8560 Lorig : constant Node_Id := Original_Node (L);
8561 Horig : constant Node_Id := Original_Node (H);
8563 begin
8564 if Nkind (Lorig) = N_Attribute_Reference
8565 and then Nkind (Horig) = N_Attribute_Reference
8566 and then Attribute_Name (Lorig) = Name_First
8567 and then Attribute_Name (Horig) = Name_Last
8568 then
8569 declare
8570 PL : constant Node_Id := Prefix (Lorig);
8571 PH : constant Node_Id := Prefix (Horig);
8572 begin
8573 if Is_Entity_Name (PL)
8574 and then Is_Entity_Name (PH)
8575 and then Entity (PL) = Entity (PH)
8576 then
8577 return True;
8578 end if;
8579 end;
8580 end if;
8582 return False;
8583 end First_Last_Ref;
8585 -- Start of processing for Resolve_Range
8587 begin
8588 Set_Etype (N, Typ);
8589 Resolve (L, Typ);
8590 Resolve (H, Typ);
8592 -- Check for inappropriate range on unordered enumeration type
8594 if Bad_Unordered_Enumeration_Reference (N, Typ)
8596 -- Exclude X'First .. X'Last if X is the same entity for both
8598 and then not First_Last_Ref
8599 then
8600 Error_Msg ("subrange of unordered enumeration type?U?", Sloc (N));
8601 end if;
8603 Check_Unset_Reference (L);
8604 Check_Unset_Reference (H);
8606 -- We have to check the bounds for being within the base range as
8607 -- required for a non-static context. Normally this is automatic and
8608 -- done as part of evaluating expressions, but the N_Range node is an
8609 -- exception, since in GNAT we consider this node to be a subexpression,
8610 -- even though in Ada it is not. The circuit in Sem_Eval could check for
8611 -- this, but that would put the test on the main evaluation path for
8612 -- expressions.
8614 Check_Non_Static_Context (L);
8615 Check_Non_Static_Context (H);
8617 -- Check for an ambiguous range over character literals. This will
8618 -- happen with a membership test involving only literals.
8620 if Typ = Any_Character then
8621 Ambiguous_Character (L);
8622 Set_Etype (N, Any_Type);
8623 return;
8624 end if;
8626 -- If bounds are static, constant-fold them, so size computations are
8627 -- identical between front-end and back-end. Do not perform this
8628 -- transformation while analyzing generic units, as type information
8629 -- would be lost when reanalyzing the constant node in the instance.
8631 if Is_Discrete_Type (Typ) and then Full_Expander_Active then
8632 if Is_OK_Static_Expression (L) then
8633 Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L));
8634 end if;
8636 if Is_OK_Static_Expression (H) then
8637 Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H));
8638 end if;
8639 end if;
8640 end Resolve_Range;
8642 --------------------------
8643 -- Resolve_Real_Literal --
8644 --------------------------
8646 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
8647 Actual_Typ : constant Entity_Id := Etype (N);
8649 begin
8650 -- Special processing for fixed-point literals to make sure that the
8651 -- value is an exact multiple of small where this is required. We skip
8652 -- this for the universal real case, and also for generic types.
8654 if Is_Fixed_Point_Type (Typ)
8655 and then Typ /= Universal_Fixed
8656 and then Typ /= Any_Fixed
8657 and then not Is_Generic_Type (Typ)
8658 then
8659 declare
8660 Val : constant Ureal := Realval (N);
8661 Cintr : constant Ureal := Val / Small_Value (Typ);
8662 Cint : constant Uint := UR_Trunc (Cintr);
8663 Den : constant Uint := Norm_Den (Cintr);
8664 Stat : Boolean;
8666 begin
8667 -- Case of literal is not an exact multiple of the Small
8669 if Den /= 1 then
8671 -- For a source program literal for a decimal fixed-point type,
8672 -- this is statically illegal (RM 4.9(36)).
8674 if Is_Decimal_Fixed_Point_Type (Typ)
8675 and then Actual_Typ = Universal_Real
8676 and then Comes_From_Source (N)
8677 then
8678 Error_Msg_N ("value has extraneous low order digits", N);
8679 end if;
8681 -- Generate a warning if literal from source
8683 if Is_Static_Expression (N)
8684 and then Warn_On_Bad_Fixed_Value
8685 then
8686 Error_Msg_N
8687 ("?b?static fixed-point value is not a multiple of Small!",
8689 end if;
8691 -- Replace literal by a value that is the exact representation
8692 -- of a value of the type, i.e. a multiple of the small value,
8693 -- by truncation, since Machine_Rounds is false for all GNAT
8694 -- fixed-point types (RM 4.9(38)).
8696 Stat := Is_Static_Expression (N);
8697 Rewrite (N,
8698 Make_Real_Literal (Sloc (N),
8699 Realval => Small_Value (Typ) * Cint));
8701 Set_Is_Static_Expression (N, Stat);
8702 end if;
8704 -- In all cases, set the corresponding integer field
8706 Set_Corresponding_Integer_Value (N, Cint);
8707 end;
8708 end if;
8710 -- Now replace the actual type by the expected type as usual
8712 Set_Etype (N, Typ);
8713 Eval_Real_Literal (N);
8714 end Resolve_Real_Literal;
8716 -----------------------
8717 -- Resolve_Reference --
8718 -----------------------
8720 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
8721 P : constant Node_Id := Prefix (N);
8723 begin
8724 -- Replace general access with specific type
8726 if Ekind (Etype (N)) = E_Allocator_Type then
8727 Set_Etype (N, Base_Type (Typ));
8728 end if;
8730 Resolve (P, Designated_Type (Etype (N)));
8732 -- If we are taking the reference of a volatile entity, then treat it as
8733 -- a potential modification of this entity. This is too conservative,
8734 -- but necessary because remove side effects can cause transformations
8735 -- of normal assignments into reference sequences that otherwise fail to
8736 -- notice the modification.
8738 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
8739 Note_Possible_Modification (P, Sure => False);
8740 end if;
8741 end Resolve_Reference;
8743 --------------------------------
8744 -- Resolve_Selected_Component --
8745 --------------------------------
8747 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
8748 Comp : Entity_Id;
8749 Comp1 : Entity_Id := Empty; -- prevent junk warning
8750 P : constant Node_Id := Prefix (N);
8751 S : constant Node_Id := Selector_Name (N);
8752 T : Entity_Id := Etype (P);
8753 I : Interp_Index;
8754 I1 : Interp_Index := 0; -- prevent junk warning
8755 It : Interp;
8756 It1 : Interp;
8757 Found : Boolean;
8759 function Init_Component return Boolean;
8760 -- Check whether this is the initialization of a component within an
8761 -- init proc (by assignment or call to another init proc). If true,
8762 -- there is no need for a discriminant check.
8764 --------------------
8765 -- Init_Component --
8766 --------------------
8768 function Init_Component return Boolean is
8769 begin
8770 return Inside_Init_Proc
8771 and then Nkind (Prefix (N)) = N_Identifier
8772 and then Chars (Prefix (N)) = Name_uInit
8773 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
8774 end Init_Component;
8776 -- Start of processing for Resolve_Selected_Component
8778 begin
8779 if Is_Overloaded (P) then
8781 -- Use the context type to select the prefix that has a selector
8782 -- of the correct name and type.
8784 Found := False;
8785 Get_First_Interp (P, I, It);
8787 Search : while Present (It.Typ) loop
8788 if Is_Access_Type (It.Typ) then
8789 T := Designated_Type (It.Typ);
8790 else
8791 T := It.Typ;
8792 end if;
8794 -- Locate selected component. For a private prefix the selector
8795 -- can denote a discriminant.
8797 if Is_Record_Type (T) or else Is_Private_Type (T) then
8799 -- The visible components of a class-wide type are those of
8800 -- the root type.
8802 if Is_Class_Wide_Type (T) then
8803 T := Etype (T);
8804 end if;
8806 Comp := First_Entity (T);
8807 while Present (Comp) loop
8808 if Chars (Comp) = Chars (S)
8809 and then Covers (Etype (Comp), Typ)
8810 then
8811 if not Found then
8812 Found := True;
8813 I1 := I;
8814 It1 := It;
8815 Comp1 := Comp;
8817 else
8818 It := Disambiguate (P, I1, I, Any_Type);
8820 if It = No_Interp then
8821 Error_Msg_N
8822 ("ambiguous prefix for selected component", N);
8823 Set_Etype (N, Typ);
8824 return;
8826 else
8827 It1 := It;
8829 -- There may be an implicit dereference. Retrieve
8830 -- designated record type.
8832 if Is_Access_Type (It1.Typ) then
8833 T := Designated_Type (It1.Typ);
8834 else
8835 T := It1.Typ;
8836 end if;
8838 if Scope (Comp1) /= T then
8840 -- Resolution chooses the new interpretation.
8841 -- Find the component with the right name.
8843 Comp1 := First_Entity (T);
8844 while Present (Comp1)
8845 and then Chars (Comp1) /= Chars (S)
8846 loop
8847 Comp1 := Next_Entity (Comp1);
8848 end loop;
8849 end if;
8851 exit Search;
8852 end if;
8853 end if;
8854 end if;
8856 Comp := Next_Entity (Comp);
8857 end loop;
8858 end if;
8860 Get_Next_Interp (I, It);
8861 end loop Search;
8863 Resolve (P, It1.Typ);
8864 Set_Etype (N, Typ);
8865 Set_Entity_With_Style_Check (S, Comp1);
8867 else
8868 -- Resolve prefix with its type
8870 Resolve (P, T);
8871 end if;
8873 -- Generate cross-reference. We needed to wait until full overloading
8874 -- resolution was complete to do this, since otherwise we can't tell if
8875 -- we are an lvalue or not.
8877 if May_Be_Lvalue (N) then
8878 Generate_Reference (Entity (S), S, 'm');
8879 else
8880 Generate_Reference (Entity (S), S, 'r');
8881 end if;
8883 -- If prefix is an access type, the node will be transformed into an
8884 -- explicit dereference during expansion. The type of the node is the
8885 -- designated type of that of the prefix.
8887 if Is_Access_Type (Etype (P)) then
8888 T := Designated_Type (Etype (P));
8889 Check_Fully_Declared_Prefix (T, P);
8890 else
8891 T := Etype (P);
8892 end if;
8894 if Has_Discriminants (T)
8895 and then Ekind_In (Entity (S), E_Component, E_Discriminant)
8896 and then Present (Original_Record_Component (Entity (S)))
8897 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
8898 and then not Discriminant_Checks_Suppressed (T)
8899 and then not Init_Component
8900 then
8901 Set_Do_Discriminant_Check (N);
8902 end if;
8904 if Ekind (Entity (S)) = E_Void then
8905 Error_Msg_N ("premature use of component", S);
8906 end if;
8908 -- If the prefix is a record conversion, this may be a renamed
8909 -- discriminant whose bounds differ from those of the original
8910 -- one, so we must ensure that a range check is performed.
8912 if Nkind (P) = N_Type_Conversion
8913 and then Ekind (Entity (S)) = E_Discriminant
8914 and then Is_Discrete_Type (Typ)
8915 then
8916 Set_Etype (N, Base_Type (Typ));
8917 end if;
8919 -- Note: No Eval processing is required, because the prefix is of a
8920 -- record type, or protected type, and neither can possibly be static.
8922 -- If the array type is atomic, and is packed, and we are in a left side
8923 -- context, then this is worth a warning, since we have a situation
8924 -- where the access to the component may cause extra read/writes of the
8925 -- atomic array object, which could be considered unexpected.
8927 if Nkind (N) = N_Selected_Component
8928 and then (Is_Atomic (T)
8929 or else (Is_Entity_Name (Prefix (N))
8930 and then Is_Atomic (Entity (Prefix (N)))))
8931 and then Is_Packed (T)
8932 and then Is_LHS (N)
8933 then
8934 Error_Msg_N
8935 ("??assignment to component of packed atomic record", Prefix (N));
8936 Error_Msg_N
8937 ("\??may cause unexpected accesses to atomic object", Prefix (N));
8938 end if;
8940 Analyze_Dimension (N);
8941 end Resolve_Selected_Component;
8943 -------------------
8944 -- Resolve_Shift --
8945 -------------------
8947 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
8948 B_Typ : constant Entity_Id := Base_Type (Typ);
8949 L : constant Node_Id := Left_Opnd (N);
8950 R : constant Node_Id := Right_Opnd (N);
8952 begin
8953 -- We do the resolution using the base type, because intermediate values
8954 -- in expressions always are of the base type, not a subtype of it.
8956 Resolve (L, B_Typ);
8957 Resolve (R, Standard_Natural);
8959 Check_Unset_Reference (L);
8960 Check_Unset_Reference (R);
8962 Set_Etype (N, B_Typ);
8963 Generate_Operator_Reference (N, B_Typ);
8964 Eval_Shift (N);
8965 end Resolve_Shift;
8967 ---------------------------
8968 -- Resolve_Short_Circuit --
8969 ---------------------------
8971 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
8972 B_Typ : constant Entity_Id := Base_Type (Typ);
8973 L : constant Node_Id := Left_Opnd (N);
8974 R : constant Node_Id := Right_Opnd (N);
8976 begin
8977 Resolve (L, B_Typ);
8978 Resolve (R, B_Typ);
8980 -- Check for issuing warning for always False assert/check, this happens
8981 -- when assertions are turned off, in which case the pragma Assert/Check
8982 -- was transformed into:
8984 -- if False and then <condition> then ...
8986 -- and we detect this pattern
8988 if Warn_On_Assertion_Failure
8989 and then Is_Entity_Name (R)
8990 and then Entity (R) = Standard_False
8991 and then Nkind (Parent (N)) = N_If_Statement
8992 and then Nkind (N) = N_And_Then
8993 and then Is_Entity_Name (L)
8994 and then Entity (L) = Standard_False
8995 then
8996 declare
8997 Orig : constant Node_Id := Original_Node (Parent (N));
8999 begin
9000 -- Special handling of Asssert pragma
9002 if Nkind (Orig) = N_Pragma
9003 and then Pragma_Name (Orig) = Name_Assert
9004 then
9005 declare
9006 Expr : constant Node_Id :=
9007 Original_Node
9008 (Expression
9009 (First (Pragma_Argument_Associations (Orig))));
9011 begin
9012 -- Don't warn if original condition is explicit False,
9013 -- since obviously the failure is expected in this case.
9015 if Is_Entity_Name (Expr)
9016 and then Entity (Expr) = Standard_False
9017 then
9018 null;
9020 -- Issue warning. We do not want the deletion of the
9021 -- IF/AND-THEN to take this message with it. We achieve this
9022 -- by making sure that the expanded code points to the Sloc
9023 -- of the expression, not the original pragma.
9025 else
9026 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
9027 -- The source location of the expression is not usually
9028 -- the best choice here. For example, it gets located on
9029 -- the last AND keyword in a chain of boolean expressiond
9030 -- AND'ed together. It is best to put the message on the
9031 -- first character of the assertion, which is the effect
9032 -- of the First_Node call here.
9034 Error_Msg_F
9035 ("?A?assertion would fail at run time!",
9036 Expression
9037 (First (Pragma_Argument_Associations (Orig))));
9038 end if;
9039 end;
9041 -- Similar processing for Check pragma
9043 elsif Nkind (Orig) = N_Pragma
9044 and then Pragma_Name (Orig) = Name_Check
9045 then
9046 -- Don't want to warn if original condition is explicit False
9048 declare
9049 Expr : constant Node_Id :=
9050 Original_Node
9051 (Expression
9052 (Next (First (Pragma_Argument_Associations (Orig)))));
9053 begin
9054 if Is_Entity_Name (Expr)
9055 and then Entity (Expr) = Standard_False
9056 then
9057 null;
9059 -- Post warning
9061 else
9062 -- Again use Error_Msg_F rather than Error_Msg_N, see
9063 -- comment above for an explanation of why we do this.
9065 Error_Msg_F
9066 ("?A?check would fail at run time!",
9067 Expression
9068 (Last (Pragma_Argument_Associations (Orig))));
9069 end if;
9070 end;
9071 end if;
9072 end;
9073 end if;
9075 -- Continue with processing of short circuit
9077 Check_Unset_Reference (L);
9078 Check_Unset_Reference (R);
9080 Set_Etype (N, B_Typ);
9081 Eval_Short_Circuit (N);
9082 end Resolve_Short_Circuit;
9084 -------------------
9085 -- Resolve_Slice --
9086 -------------------
9088 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
9089 Drange : constant Node_Id := Discrete_Range (N);
9090 Name : constant Node_Id := Prefix (N);
9091 Array_Type : Entity_Id := Empty;
9092 Index_Type : Entity_Id;
9094 begin
9095 if Is_Overloaded (Name) then
9097 -- Use the context type to select the prefix that yields the correct
9098 -- array type.
9100 declare
9101 I : Interp_Index;
9102 I1 : Interp_Index := 0;
9103 It : Interp;
9104 P : constant Node_Id := Prefix (N);
9105 Found : Boolean := False;
9107 begin
9108 Get_First_Interp (P, I, It);
9109 while Present (It.Typ) loop
9110 if (Is_Array_Type (It.Typ)
9111 and then Covers (Typ, It.Typ))
9112 or else (Is_Access_Type (It.Typ)
9113 and then Is_Array_Type (Designated_Type (It.Typ))
9114 and then Covers (Typ, Designated_Type (It.Typ)))
9115 then
9116 if Found then
9117 It := Disambiguate (P, I1, I, Any_Type);
9119 if It = No_Interp then
9120 Error_Msg_N ("ambiguous prefix for slicing", N);
9121 Set_Etype (N, Typ);
9122 return;
9123 else
9124 Found := True;
9125 Array_Type := It.Typ;
9126 I1 := I;
9127 end if;
9128 else
9129 Found := True;
9130 Array_Type := It.Typ;
9131 I1 := I;
9132 end if;
9133 end if;
9135 Get_Next_Interp (I, It);
9136 end loop;
9137 end;
9139 else
9140 Array_Type := Etype (Name);
9141 end if;
9143 Resolve (Name, Array_Type);
9145 if Is_Access_Type (Array_Type) then
9146 Apply_Access_Check (N);
9147 Array_Type := Designated_Type (Array_Type);
9149 -- If the prefix is an access to an unconstrained array, we must use
9150 -- the actual subtype of the object to perform the index checks. The
9151 -- object denoted by the prefix is implicit in the node, so we build
9152 -- an explicit representation for it in order to compute the actual
9153 -- subtype.
9155 if not Is_Constrained (Array_Type) then
9156 Remove_Side_Effects (Prefix (N));
9158 declare
9159 Obj : constant Node_Id :=
9160 Make_Explicit_Dereference (Sloc (N),
9161 Prefix => New_Copy_Tree (Prefix (N)));
9162 begin
9163 Set_Etype (Obj, Array_Type);
9164 Set_Parent (Obj, Parent (N));
9165 Array_Type := Get_Actual_Subtype (Obj);
9166 end;
9167 end if;
9169 elsif Is_Entity_Name (Name)
9170 or else Nkind (Name) = N_Explicit_Dereference
9171 or else (Nkind (Name) = N_Function_Call
9172 and then not Is_Constrained (Etype (Name)))
9173 then
9174 Array_Type := Get_Actual_Subtype (Name);
9176 -- If the name is a selected component that depends on discriminants,
9177 -- build an actual subtype for it. This can happen only when the name
9178 -- itself is overloaded; otherwise the actual subtype is created when
9179 -- the selected component is analyzed.
9181 elsif Nkind (Name) = N_Selected_Component
9182 and then Full_Analysis
9183 and then Depends_On_Discriminant (First_Index (Array_Type))
9184 then
9185 declare
9186 Act_Decl : constant Node_Id :=
9187 Build_Actual_Subtype_Of_Component (Array_Type, Name);
9188 begin
9189 Insert_Action (N, Act_Decl);
9190 Array_Type := Defining_Identifier (Act_Decl);
9191 end;
9193 -- Maybe this should just be "else", instead of checking for the
9194 -- specific case of slice??? This is needed for the case where the
9195 -- prefix is an Image attribute, which gets expanded to a slice, and so
9196 -- has a constrained subtype which we want to use for the slice range
9197 -- check applied below (the range check won't get done if the
9198 -- unconstrained subtype of the 'Image is used).
9200 elsif Nkind (Name) = N_Slice then
9201 Array_Type := Etype (Name);
9202 end if;
9204 -- If name was overloaded, set slice type correctly now
9206 Set_Etype (N, Array_Type);
9208 -- If the range is specified by a subtype mark, no resolution is
9209 -- necessary. Else resolve the bounds, and apply needed checks.
9211 if not Is_Entity_Name (Drange) then
9212 if Ekind (Array_Type) = E_String_Literal_Subtype then
9213 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
9214 else
9215 Index_Type := Etype (First_Index (Array_Type));
9216 end if;
9218 Resolve (Drange, Base_Type (Index_Type));
9220 if Nkind (Drange) = N_Range then
9222 -- Ensure that side effects in the bounds are properly handled
9224 Force_Evaluation (Low_Bound (Drange));
9225 Force_Evaluation (High_Bound (Drange));
9227 -- Do not apply the range check to nodes associated with the
9228 -- frontend expansion of the dispatch table. We first check
9229 -- if Ada.Tags is already loaded to avoid the addition of an
9230 -- undesired dependence on such run-time unit.
9232 if not Tagged_Type_Expansion
9233 or else not
9234 (RTU_Loaded (Ada_Tags)
9235 and then Nkind (Prefix (N)) = N_Selected_Component
9236 and then Present (Entity (Selector_Name (Prefix (N))))
9237 and then Entity (Selector_Name (Prefix (N))) =
9238 RTE_Record_Component (RE_Prims_Ptr))
9239 then
9240 Apply_Range_Check (Drange, Index_Type);
9241 end if;
9242 end if;
9243 end if;
9245 Set_Slice_Subtype (N);
9247 -- Check bad use of type with predicates
9249 if Has_Predicates (Etype (Drange)) then
9250 Bad_Predicated_Subtype_Use
9251 ("subtype& has predicate, not allowed in slice",
9252 Drange, Etype (Drange));
9254 -- Otherwise here is where we check suspicious indexes
9256 elsif Nkind (Drange) = N_Range then
9257 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
9258 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
9259 end if;
9261 Analyze_Dimension (N);
9262 Eval_Slice (N);
9263 end Resolve_Slice;
9265 ----------------------------
9266 -- Resolve_String_Literal --
9267 ----------------------------
9269 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
9270 C_Typ : constant Entity_Id := Component_Type (Typ);
9271 R_Typ : constant Entity_Id := Root_Type (C_Typ);
9272 Loc : constant Source_Ptr := Sloc (N);
9273 Str : constant String_Id := Strval (N);
9274 Strlen : constant Nat := String_Length (Str);
9275 Subtype_Id : Entity_Id;
9276 Need_Check : Boolean;
9278 begin
9279 -- For a string appearing in a concatenation, defer creation of the
9280 -- string_literal_subtype until the end of the resolution of the
9281 -- concatenation, because the literal may be constant-folded away. This
9282 -- is a useful optimization for long concatenation expressions.
9284 -- If the string is an aggregate built for a single character (which
9285 -- happens in a non-static context) or a is null string to which special
9286 -- checks may apply, we build the subtype. Wide strings must also get a
9287 -- string subtype if they come from a one character aggregate. Strings
9288 -- generated by attributes might be static, but it is often hard to
9289 -- determine whether the enclosing context is static, so we generate
9290 -- subtypes for them as well, thus losing some rarer optimizations ???
9291 -- Same for strings that come from a static conversion.
9293 Need_Check :=
9294 (Strlen = 0 and then Typ /= Standard_String)
9295 or else Nkind (Parent (N)) /= N_Op_Concat
9296 or else (N /= Left_Opnd (Parent (N))
9297 and then N /= Right_Opnd (Parent (N)))
9298 or else ((Typ = Standard_Wide_String
9299 or else Typ = Standard_Wide_Wide_String)
9300 and then Nkind (Original_Node (N)) /= N_String_Literal);
9302 -- If the resolving type is itself a string literal subtype, we can just
9303 -- reuse it, since there is no point in creating another.
9305 if Ekind (Typ) = E_String_Literal_Subtype then
9306 Subtype_Id := Typ;
9308 elsif Nkind (Parent (N)) = N_Op_Concat
9309 and then not Need_Check
9310 and then not Nkind_In (Original_Node (N), N_Character_Literal,
9311 N_Attribute_Reference,
9312 N_Qualified_Expression,
9313 N_Type_Conversion)
9314 then
9315 Subtype_Id := Typ;
9317 -- Otherwise we must create a string literal subtype. Note that the
9318 -- whole idea of string literal subtypes is simply to avoid the need
9319 -- for building a full fledged array subtype for each literal.
9321 else
9322 Set_String_Literal_Subtype (N, Typ);
9323 Subtype_Id := Etype (N);
9324 end if;
9326 if Nkind (Parent (N)) /= N_Op_Concat
9327 or else Need_Check
9328 then
9329 Set_Etype (N, Subtype_Id);
9330 Eval_String_Literal (N);
9331 end if;
9333 if Is_Limited_Composite (Typ)
9334 or else Is_Private_Composite (Typ)
9335 then
9336 Error_Msg_N ("string literal not available for private array", N);
9337 Set_Etype (N, Any_Type);
9338 return;
9339 end if;
9341 -- The validity of a null string has been checked in the call to
9342 -- Eval_String_Literal.
9344 if Strlen = 0 then
9345 return;
9347 -- Always accept string literal with component type Any_Character, which
9348 -- occurs in error situations and in comparisons of literals, both of
9349 -- which should accept all literals.
9351 elsif R_Typ = Any_Character then
9352 return;
9354 -- If the type is bit-packed, then we always transform the string
9355 -- literal into a full fledged aggregate.
9357 elsif Is_Bit_Packed_Array (Typ) then
9358 null;
9360 -- Deal with cases of Wide_Wide_String, Wide_String, and String
9362 else
9363 -- For Standard.Wide_Wide_String, or any other type whose component
9364 -- type is Standard.Wide_Wide_Character, we know that all the
9365 -- characters in the string must be acceptable, since the parser
9366 -- accepted the characters as valid character literals.
9368 if R_Typ = Standard_Wide_Wide_Character then
9369 null;
9371 -- For the case of Standard.String, or any other type whose component
9372 -- type is Standard.Character, we must make sure that there are no
9373 -- wide characters in the string, i.e. that it is entirely composed
9374 -- of characters in range of type Character.
9376 -- If the string literal is the result of a static concatenation, the
9377 -- test has already been performed on the components, and need not be
9378 -- repeated.
9380 elsif R_Typ = Standard_Character
9381 and then Nkind (Original_Node (N)) /= N_Op_Concat
9382 then
9383 for J in 1 .. Strlen loop
9384 if not In_Character_Range (Get_String_Char (Str, J)) then
9386 -- If we are out of range, post error. This is one of the
9387 -- very few places that we place the flag in the middle of
9388 -- a token, right under the offending wide character. Not
9389 -- quite clear if this is right wrt wide character encoding
9390 -- sequences, but it's only an error message!
9392 Error_Msg
9393 ("literal out of range of type Standard.Character",
9394 Source_Ptr (Int (Loc) + J));
9395 return;
9396 end if;
9397 end loop;
9399 -- For the case of Standard.Wide_String, or any other type whose
9400 -- component type is Standard.Wide_Character, we must make sure that
9401 -- there are no wide characters in the string, i.e. that it is
9402 -- entirely composed of characters in range of type Wide_Character.
9404 -- If the string literal is the result of a static concatenation,
9405 -- the test has already been performed on the components, and need
9406 -- not be repeated.
9408 elsif R_Typ = Standard_Wide_Character
9409 and then Nkind (Original_Node (N)) /= N_Op_Concat
9410 then
9411 for J in 1 .. Strlen loop
9412 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
9414 -- If we are out of range, post error. This is one of the
9415 -- very few places that we place the flag in the middle of
9416 -- a token, right under the offending wide character.
9418 -- This is not quite right, because characters in general
9419 -- will take more than one character position ???
9421 Error_Msg
9422 ("literal out of range of type Standard.Wide_Character",
9423 Source_Ptr (Int (Loc) + J));
9424 return;
9425 end if;
9426 end loop;
9428 -- If the root type is not a standard character, then we will convert
9429 -- the string into an aggregate and will let the aggregate code do
9430 -- the checking. Standard Wide_Wide_Character is also OK here.
9432 else
9433 null;
9434 end if;
9436 -- See if the component type of the array corresponding to the string
9437 -- has compile time known bounds. If yes we can directly check
9438 -- whether the evaluation of the string will raise constraint error.
9439 -- Otherwise we need to transform the string literal into the
9440 -- corresponding character aggregate and let the aggregate code do
9441 -- the checking.
9443 if Is_Standard_Character_Type (R_Typ) then
9445 -- Check for the case of full range, where we are definitely OK
9447 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
9448 return;
9449 end if;
9451 -- Here the range is not the complete base type range, so check
9453 declare
9454 Comp_Typ_Lo : constant Node_Id :=
9455 Type_Low_Bound (Component_Type (Typ));
9456 Comp_Typ_Hi : constant Node_Id :=
9457 Type_High_Bound (Component_Type (Typ));
9459 Char_Val : Uint;
9461 begin
9462 if Compile_Time_Known_Value (Comp_Typ_Lo)
9463 and then Compile_Time_Known_Value (Comp_Typ_Hi)
9464 then
9465 for J in 1 .. Strlen loop
9466 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
9468 if Char_Val < Expr_Value (Comp_Typ_Lo)
9469 or else Char_Val > Expr_Value (Comp_Typ_Hi)
9470 then
9471 Apply_Compile_Time_Constraint_Error
9472 (N, "character out of range??",
9473 CE_Range_Check_Failed,
9474 Loc => Source_Ptr (Int (Loc) + J));
9475 end if;
9476 end loop;
9478 return;
9479 end if;
9480 end;
9481 end if;
9482 end if;
9484 -- If we got here we meed to transform the string literal into the
9485 -- equivalent qualified positional array aggregate. This is rather
9486 -- heavy artillery for this situation, but it is hard work to avoid.
9488 declare
9489 Lits : constant List_Id := New_List;
9490 P : Source_Ptr := Loc + 1;
9491 C : Char_Code;
9493 begin
9494 -- Build the character literals, we give them source locations that
9495 -- correspond to the string positions, which is a bit tricky given
9496 -- the possible presence of wide character escape sequences.
9498 for J in 1 .. Strlen loop
9499 C := Get_String_Char (Str, J);
9500 Set_Character_Literal_Name (C);
9502 Append_To (Lits,
9503 Make_Character_Literal (P,
9504 Chars => Name_Find,
9505 Char_Literal_Value => UI_From_CC (C)));
9507 if In_Character_Range (C) then
9508 P := P + 1;
9510 -- Should we have a call to Skip_Wide here ???
9512 -- ??? else
9513 -- Skip_Wide (P);
9515 end if;
9516 end loop;
9518 Rewrite (N,
9519 Make_Qualified_Expression (Loc,
9520 Subtype_Mark => New_Reference_To (Typ, Loc),
9521 Expression =>
9522 Make_Aggregate (Loc, Expressions => Lits)));
9524 Analyze_And_Resolve (N, Typ);
9525 end;
9526 end Resolve_String_Literal;
9528 -----------------------------
9529 -- Resolve_Subprogram_Info --
9530 -----------------------------
9532 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
9533 begin
9534 Set_Etype (N, Typ);
9535 end Resolve_Subprogram_Info;
9537 -----------------------------
9538 -- Resolve_Type_Conversion --
9539 -----------------------------
9541 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
9542 Conv_OK : constant Boolean := Conversion_OK (N);
9543 Operand : constant Node_Id := Expression (N);
9544 Operand_Typ : constant Entity_Id := Etype (Operand);
9545 Target_Typ : constant Entity_Id := Etype (N);
9546 Rop : Node_Id;
9547 Orig_N : Node_Id;
9548 Orig_T : Node_Id;
9550 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
9551 -- Set to False to suppress cases where we want to suppress the test
9552 -- for redundancy to avoid possible false positives on this warning.
9554 begin
9555 if not Conv_OK
9556 and then not Valid_Conversion (N, Target_Typ, Operand)
9557 then
9558 return;
9559 end if;
9561 -- If the Operand Etype is Universal_Fixed, then the conversion is
9562 -- never redundant. We need this check because by the time we have
9563 -- finished the rather complex transformation, the conversion looks
9564 -- redundant when it is not.
9566 if Operand_Typ = Universal_Fixed then
9567 Test_Redundant := False;
9569 -- If the operand is marked as Any_Fixed, then special processing is
9570 -- required. This is also a case where we suppress the test for a
9571 -- redundant conversion, since most certainly it is not redundant.
9573 elsif Operand_Typ = Any_Fixed then
9574 Test_Redundant := False;
9576 -- Mixed-mode operation involving a literal. Context must be a fixed
9577 -- type which is applied to the literal subsequently.
9579 if Is_Fixed_Point_Type (Typ) then
9580 Set_Etype (Operand, Universal_Real);
9582 elsif Is_Numeric_Type (Typ)
9583 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
9584 and then (Etype (Right_Opnd (Operand)) = Universal_Real
9585 or else
9586 Etype (Left_Opnd (Operand)) = Universal_Real)
9587 then
9588 -- Return if expression is ambiguous
9590 if Unique_Fixed_Point_Type (N) = Any_Type then
9591 return;
9593 -- If nothing else, the available fixed type is Duration
9595 else
9596 Set_Etype (Operand, Standard_Duration);
9597 end if;
9599 -- Resolve the real operand with largest available precision
9601 if Etype (Right_Opnd (Operand)) = Universal_Real then
9602 Rop := New_Copy_Tree (Right_Opnd (Operand));
9603 else
9604 Rop := New_Copy_Tree (Left_Opnd (Operand));
9605 end if;
9607 Resolve (Rop, Universal_Real);
9609 -- If the operand is a literal (it could be a non-static and
9610 -- illegal exponentiation) check whether the use of Duration
9611 -- is potentially inaccurate.
9613 if Nkind (Rop) = N_Real_Literal
9614 and then Realval (Rop) /= Ureal_0
9615 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
9616 then
9617 Error_Msg_N
9618 ("??universal real operand can only "
9619 & "be interpreted as Duration!", Rop);
9620 Error_Msg_N
9621 ("\??precision will be lost in the conversion!", Rop);
9622 end if;
9624 elsif Is_Numeric_Type (Typ)
9625 and then Nkind (Operand) in N_Op
9626 and then Unique_Fixed_Point_Type (N) /= Any_Type
9627 then
9628 Set_Etype (Operand, Standard_Duration);
9630 else
9631 Error_Msg_N ("invalid context for mixed mode operation", N);
9632 Set_Etype (Operand, Any_Type);
9633 return;
9634 end if;
9635 end if;
9637 Resolve (Operand);
9639 -- In SPARK, a type conversion between array types should be restricted
9640 -- to types which have matching static bounds.
9642 -- Protect call to Matching_Static_Array_Bounds to avoid costly
9643 -- operation if not needed.
9645 if Restriction_Check_Required (SPARK_05)
9646 and then Is_Array_Type (Target_Typ)
9647 and then Is_Array_Type (Operand_Typ)
9648 and then Operand_Typ /= Any_Composite -- or else Operand in error
9649 and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ)
9650 then
9651 Check_SPARK_Restriction
9652 ("array types should have matching static bounds", N);
9653 end if;
9655 -- In formal mode, the operand of an ancestor type conversion must be an
9656 -- object (not an expression).
9658 if Is_Tagged_Type (Target_Typ)
9659 and then not Is_Class_Wide_Type (Target_Typ)
9660 and then Is_Tagged_Type (Operand_Typ)
9661 and then not Is_Class_Wide_Type (Operand_Typ)
9662 and then Is_Ancestor (Target_Typ, Operand_Typ)
9663 and then not Is_SPARK_Object_Reference (Operand)
9664 then
9665 Check_SPARK_Restriction ("object required", Operand);
9666 end if;
9668 Analyze_Dimension (N);
9670 -- Note: we do the Eval_Type_Conversion call before applying the
9671 -- required checks for a subtype conversion. This is important, since
9672 -- both are prepared under certain circumstances to change the type
9673 -- conversion to a constraint error node, but in the case of
9674 -- Eval_Type_Conversion this may reflect an illegality in the static
9675 -- case, and we would miss the illegality (getting only a warning
9676 -- message), if we applied the type conversion checks first.
9678 Eval_Type_Conversion (N);
9680 -- Even when evaluation is not possible, we may be able to simplify the
9681 -- conversion or its expression. This needs to be done before applying
9682 -- checks, since otherwise the checks may use the original expression
9683 -- and defeat the simplifications. This is specifically the case for
9684 -- elimination of the floating-point Truncation attribute in
9685 -- float-to-int conversions.
9687 Simplify_Type_Conversion (N);
9689 -- If after evaluation we still have a type conversion, then we may need
9690 -- to apply checks required for a subtype conversion.
9692 -- Skip these type conversion checks if universal fixed operands
9693 -- operands involved, since range checks are handled separately for
9694 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
9696 if Nkind (N) = N_Type_Conversion
9697 and then not Is_Generic_Type (Root_Type (Target_Typ))
9698 and then Target_Typ /= Universal_Fixed
9699 and then Operand_Typ /= Universal_Fixed
9700 then
9701 Apply_Type_Conversion_Checks (N);
9702 end if;
9704 -- Issue warning for conversion of simple object to its own type. We
9705 -- have to test the original nodes, since they may have been rewritten
9706 -- by various optimizations.
9708 Orig_N := Original_Node (N);
9710 -- Here we test for a redundant conversion if the warning mode is
9711 -- active (and was not locally reset), and we have a type conversion
9712 -- from source not appearing in a generic instance.
9714 if Test_Redundant
9715 and then Nkind (Orig_N) = N_Type_Conversion
9716 and then Comes_From_Source (Orig_N)
9717 and then not In_Instance
9718 then
9719 Orig_N := Original_Node (Expression (Orig_N));
9720 Orig_T := Target_Typ;
9722 -- If the node is part of a larger expression, the Target_Type
9723 -- may not be the original type of the node if the context is a
9724 -- condition. Recover original type to see if conversion is needed.
9726 if Is_Boolean_Type (Orig_T)
9727 and then Nkind (Parent (N)) in N_Op
9728 then
9729 Orig_T := Etype (Parent (N));
9730 end if;
9732 -- If we have an entity name, then give the warning if the entity
9733 -- is the right type, or if it is a loop parameter covered by the
9734 -- original type (that's needed because loop parameters have an
9735 -- odd subtype coming from the bounds).
9737 if (Is_Entity_Name (Orig_N)
9738 and then
9739 (Etype (Entity (Orig_N)) = Orig_T
9740 or else
9741 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
9742 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
9744 -- If not an entity, then type of expression must match
9746 or else Etype (Orig_N) = Orig_T
9747 then
9748 -- One more check, do not give warning if the analyzed conversion
9749 -- has an expression with non-static bounds, and the bounds of the
9750 -- target are static. This avoids junk warnings in cases where the
9751 -- conversion is necessary to establish staticness, for example in
9752 -- a case statement.
9754 if not Is_OK_Static_Subtype (Operand_Typ)
9755 and then Is_OK_Static_Subtype (Target_Typ)
9756 then
9757 null;
9759 -- Finally, if this type conversion occurs in a context requiring
9760 -- a prefix, and the expression is a qualified expression then the
9761 -- type conversion is not redundant, since a qualified expression
9762 -- is not a prefix, whereas a type conversion is. For example, "X
9763 -- := T'(Funx(...)).Y;" is illegal because a selected component
9764 -- requires a prefix, but a type conversion makes it legal: "X :=
9765 -- T(T'(Funx(...))).Y;"
9767 -- In Ada 2012, a qualified expression is a name, so this idiom is
9768 -- no longer needed, but we still suppress the warning because it
9769 -- seems unfriendly for warnings to pop up when you switch to the
9770 -- newer language version.
9772 elsif Nkind (Orig_N) = N_Qualified_Expression
9773 and then Nkind_In (Parent (N), N_Attribute_Reference,
9774 N_Indexed_Component,
9775 N_Selected_Component,
9776 N_Slice,
9777 N_Explicit_Dereference)
9778 then
9779 null;
9781 -- Never warn on conversion to Long_Long_Integer'Base since
9782 -- that is most likely an artifact of the extended overflow
9783 -- checking and comes from complex expanded code.
9785 elsif Orig_T = Base_Type (Standard_Long_Long_Integer) then
9786 null;
9788 -- Here we give the redundant conversion warning. If it is an
9789 -- entity, give the name of the entity in the message. If not,
9790 -- just mention the expression.
9792 -- Shoudn't we test Warn_On_Redundant_Constructs here ???
9794 else
9795 if Is_Entity_Name (Orig_N) then
9796 Error_Msg_Node_2 := Orig_T;
9797 Error_Msg_NE -- CODEFIX
9798 ("??redundant conversion, & is of type &!",
9799 N, Entity (Orig_N));
9800 else
9801 Error_Msg_NE
9802 ("??redundant conversion, expression is of type&!",
9803 N, Orig_T);
9804 end if;
9805 end if;
9806 end if;
9807 end if;
9809 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
9810 -- No need to perform any interface conversion if the type of the
9811 -- expression coincides with the target type.
9813 if Ada_Version >= Ada_2005
9814 and then Full_Expander_Active
9815 and then Operand_Typ /= Target_Typ
9816 then
9817 declare
9818 Opnd : Entity_Id := Operand_Typ;
9819 Target : Entity_Id := Target_Typ;
9821 begin
9822 if Is_Access_Type (Opnd) then
9823 Opnd := Designated_Type (Opnd);
9824 end if;
9826 if Is_Access_Type (Target_Typ) then
9827 Target := Designated_Type (Target);
9828 end if;
9830 if Opnd = Target then
9831 null;
9833 -- Conversion from interface type
9835 elsif Is_Interface (Opnd) then
9837 -- Ada 2005 (AI-217): Handle entities from limited views
9839 if From_With_Type (Opnd) then
9840 Error_Msg_Qual_Level := 99;
9841 Error_Msg_NE -- CODEFIX
9842 ("missing WITH clause on package &", N,
9843 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
9844 Error_Msg_N
9845 ("type conversions require visibility of the full view",
9848 elsif From_With_Type (Target)
9849 and then not
9850 (Is_Access_Type (Target_Typ)
9851 and then Present (Non_Limited_View (Etype (Target))))
9852 then
9853 Error_Msg_Qual_Level := 99;
9854 Error_Msg_NE -- CODEFIX
9855 ("missing WITH clause on package &", N,
9856 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
9857 Error_Msg_N
9858 ("type conversions require visibility of the full view",
9861 else
9862 Expand_Interface_Conversion (N);
9863 end if;
9865 -- Conversion to interface type
9867 elsif Is_Interface (Target) then
9869 -- Handle subtypes
9871 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
9872 Opnd := Etype (Opnd);
9873 end if;
9875 if Is_Class_Wide_Type (Opnd)
9876 or else Interface_Present_In_Ancestor
9877 (Typ => Opnd,
9878 Iface => Target)
9879 then
9880 Expand_Interface_Conversion (N);
9881 else
9882 Error_Msg_Name_1 := Chars (Etype (Target));
9883 Error_Msg_Name_2 := Chars (Opnd);
9884 Error_Msg_N
9885 ("wrong interface conversion (% is not a progenitor "
9886 & "of %)", N);
9887 end if;
9888 end if;
9889 end;
9890 end if;
9892 -- Ada 2012: if target type has predicates, the result requires a
9893 -- predicate check. If the context is a call to another predicate
9894 -- check we must prevent infinite recursion.
9896 if Has_Predicates (Target_Typ) then
9897 if Nkind (Parent (N)) = N_Function_Call
9898 and then Present (Name (Parent (N)))
9899 and then (Is_Predicate_Function (Entity (Name (Parent (N))))
9900 or else
9901 Is_Predicate_Function_M (Entity (Name (Parent (N)))))
9902 then
9903 null;
9905 else
9906 Apply_Predicate_Check (N, Target_Typ);
9907 end if;
9908 end if;
9909 end Resolve_Type_Conversion;
9911 ----------------------
9912 -- Resolve_Unary_Op --
9913 ----------------------
9915 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
9916 B_Typ : constant Entity_Id := Base_Type (Typ);
9917 R : constant Node_Id := Right_Opnd (N);
9918 OK : Boolean;
9919 Lo : Uint;
9920 Hi : Uint;
9922 begin
9923 if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then
9924 Error_Msg_Name_1 := Chars (Typ);
9925 Check_SPARK_Restriction
9926 ("unary operator not defined for modular type%", N);
9927 end if;
9929 -- Deal with intrinsic unary operators
9931 if Comes_From_Source (N)
9932 and then Ekind (Entity (N)) = E_Function
9933 and then Is_Imported (Entity (N))
9934 and then Is_Intrinsic_Subprogram (Entity (N))
9935 then
9936 Resolve_Intrinsic_Unary_Operator (N, Typ);
9937 return;
9938 end if;
9940 -- Deal with universal cases
9942 if Etype (R) = Universal_Integer
9943 or else
9944 Etype (R) = Universal_Real
9945 then
9946 Check_For_Visible_Operator (N, B_Typ);
9947 end if;
9949 Set_Etype (N, B_Typ);
9950 Resolve (R, B_Typ);
9952 -- Generate warning for expressions like abs (x mod 2)
9954 if Warn_On_Redundant_Constructs
9955 and then Nkind (N) = N_Op_Abs
9956 then
9957 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
9959 if OK and then Hi >= Lo and then Lo >= 0 then
9960 Error_Msg_N -- CODEFIX
9961 ("?r?abs applied to known non-negative value has no effect", N);
9962 end if;
9963 end if;
9965 -- Deal with reference generation
9967 Check_Unset_Reference (R);
9968 Generate_Operator_Reference (N, B_Typ);
9969 Analyze_Dimension (N);
9970 Eval_Unary_Op (N);
9972 -- Set overflow checking bit. Much cleverer code needed here eventually
9973 -- and perhaps the Resolve routines should be separated for the various
9974 -- arithmetic operations, since they will need different processing ???
9976 if Nkind (N) in N_Op then
9977 if not Overflow_Checks_Suppressed (Etype (N)) then
9978 Enable_Overflow_Check (N);
9979 end if;
9980 end if;
9982 -- Generate warning for expressions like -5 mod 3 for integers. No need
9983 -- to worry in the floating-point case, since parens do not affect the
9984 -- result so there is no point in giving in a warning.
9986 declare
9987 Norig : constant Node_Id := Original_Node (N);
9988 Rorig : Node_Id;
9989 Val : Uint;
9990 HB : Uint;
9991 LB : Uint;
9992 Lval : Uint;
9993 Opnd : Node_Id;
9995 begin
9996 if Warn_On_Questionable_Missing_Parens
9997 and then Comes_From_Source (Norig)
9998 and then Is_Integer_Type (Typ)
9999 and then Nkind (Norig) = N_Op_Minus
10000 then
10001 Rorig := Original_Node (Right_Opnd (Norig));
10003 -- We are looking for cases where the right operand is not
10004 -- parenthesized, and is a binary operator, multiply, divide, or
10005 -- mod. These are the cases where the grouping can affect results.
10007 if Paren_Count (Rorig) = 0
10008 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
10009 then
10010 -- For mod, we always give the warning, since the value is
10011 -- affected by the parenthesization (e.g. (-5) mod 315 /=
10012 -- -(5 mod 315)). But for the other cases, the only concern is
10013 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
10014 -- overflows, but (-2) * 64 does not). So we try to give the
10015 -- message only when overflow is possible.
10017 if Nkind (Rorig) /= N_Op_Mod
10018 and then Compile_Time_Known_Value (R)
10019 then
10020 Val := Expr_Value (R);
10022 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
10023 HB := Expr_Value (Type_High_Bound (Typ));
10024 else
10025 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
10026 end if;
10028 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
10029 LB := Expr_Value (Type_Low_Bound (Typ));
10030 else
10031 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
10032 end if;
10034 -- Note that the test below is deliberately excluding the
10035 -- largest negative number, since that is a potentially
10036 -- troublesome case (e.g. -2 * x, where the result is the
10037 -- largest negative integer has an overflow with 2 * x).
10039 if Val > LB and then Val <= HB then
10040 return;
10041 end if;
10042 end if;
10044 -- For the multiplication case, the only case we have to worry
10045 -- about is when (-a)*b is exactly the largest negative number
10046 -- so that -(a*b) can cause overflow. This can only happen if
10047 -- a is a power of 2, and more generally if any operand is a
10048 -- constant that is not a power of 2, then the parentheses
10049 -- cannot affect whether overflow occurs. We only bother to
10050 -- test the left most operand
10052 -- Loop looking at left operands for one that has known value
10054 Opnd := Rorig;
10055 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
10056 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
10057 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
10059 -- Operand value of 0 or 1 skips warning
10061 if Lval <= 1 then
10062 return;
10064 -- Otherwise check power of 2, if power of 2, warn, if
10065 -- anything else, skip warning.
10067 else
10068 while Lval /= 2 loop
10069 if Lval mod 2 = 1 then
10070 return;
10071 else
10072 Lval := Lval / 2;
10073 end if;
10074 end loop;
10076 exit Opnd_Loop;
10077 end if;
10078 end if;
10080 -- Keep looking at left operands
10082 Opnd := Left_Opnd (Opnd);
10083 end loop Opnd_Loop;
10085 -- For rem or "/" we can only have a problematic situation
10086 -- if the divisor has a value of minus one or one. Otherwise
10087 -- overflow is impossible (divisor > 1) or we have a case of
10088 -- division by zero in any case.
10090 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
10091 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
10092 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
10093 then
10094 return;
10095 end if;
10097 -- If we fall through warning should be issued
10099 -- Shouldn't we test Warn_On_Questionable_Missing_Parens ???
10101 Error_Msg_N
10102 ("??unary minus expression should be parenthesized here!", N);
10103 end if;
10104 end if;
10105 end;
10106 end Resolve_Unary_Op;
10108 ----------------------------------
10109 -- Resolve_Unchecked_Expression --
10110 ----------------------------------
10112 procedure Resolve_Unchecked_Expression
10113 (N : Node_Id;
10114 Typ : Entity_Id)
10116 begin
10117 Resolve (Expression (N), Typ, Suppress => All_Checks);
10118 Set_Etype (N, Typ);
10119 end Resolve_Unchecked_Expression;
10121 ---------------------------------------
10122 -- Resolve_Unchecked_Type_Conversion --
10123 ---------------------------------------
10125 procedure Resolve_Unchecked_Type_Conversion
10126 (N : Node_Id;
10127 Typ : Entity_Id)
10129 pragma Warnings (Off, Typ);
10131 Operand : constant Node_Id := Expression (N);
10132 Opnd_Type : constant Entity_Id := Etype (Operand);
10134 begin
10135 -- Resolve operand using its own type
10137 Resolve (Operand, Opnd_Type);
10138 Analyze_Dimension (N);
10139 Eval_Unchecked_Conversion (N);
10140 end Resolve_Unchecked_Type_Conversion;
10142 ------------------------------
10143 -- Rewrite_Operator_As_Call --
10144 ------------------------------
10146 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
10147 Loc : constant Source_Ptr := Sloc (N);
10148 Actuals : constant List_Id := New_List;
10149 New_N : Node_Id;
10151 begin
10152 if Nkind (N) in N_Binary_Op then
10153 Append (Left_Opnd (N), Actuals);
10154 end if;
10156 Append (Right_Opnd (N), Actuals);
10158 New_N :=
10159 Make_Function_Call (Sloc => Loc,
10160 Name => New_Occurrence_Of (Nam, Loc),
10161 Parameter_Associations => Actuals);
10163 Preserve_Comes_From_Source (New_N, N);
10164 Preserve_Comes_From_Source (Name (New_N), N);
10165 Rewrite (N, New_N);
10166 Set_Etype (N, Etype (Nam));
10167 end Rewrite_Operator_As_Call;
10169 ------------------------------
10170 -- Rewrite_Renamed_Operator --
10171 ------------------------------
10173 procedure Rewrite_Renamed_Operator
10174 (N : Node_Id;
10175 Op : Entity_Id;
10176 Typ : Entity_Id)
10178 Nam : constant Name_Id := Chars (Op);
10179 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
10180 Op_Node : Node_Id;
10182 begin
10183 -- Rewrite the operator node using the real operator, not its renaming.
10184 -- Exclude user-defined intrinsic operations of the same name, which are
10185 -- treated separately and rewritten as calls.
10187 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
10188 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
10189 Set_Chars (Op_Node, Nam);
10190 Set_Etype (Op_Node, Etype (N));
10191 Set_Entity (Op_Node, Op);
10192 Set_Right_Opnd (Op_Node, Right_Opnd (N));
10194 -- Indicate that both the original entity and its renaming are
10195 -- referenced at this point.
10197 Generate_Reference (Entity (N), N);
10198 Generate_Reference (Op, N);
10200 if Is_Binary then
10201 Set_Left_Opnd (Op_Node, Left_Opnd (N));
10202 end if;
10204 Rewrite (N, Op_Node);
10206 -- If the context type is private, add the appropriate conversions so
10207 -- that the operator is applied to the full view. This is done in the
10208 -- routines that resolve intrinsic operators.
10210 if Is_Intrinsic_Subprogram (Op)
10211 and then Is_Private_Type (Typ)
10212 then
10213 case Nkind (N) is
10214 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
10215 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
10216 Resolve_Intrinsic_Operator (N, Typ);
10218 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
10219 Resolve_Intrinsic_Unary_Operator (N, Typ);
10221 when others =>
10222 Resolve (N, Typ);
10223 end case;
10224 end if;
10226 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
10228 -- Operator renames a user-defined operator of the same name. Use the
10229 -- original operator in the node, which is the one Gigi knows about.
10231 Set_Entity (N, Op);
10232 Set_Is_Overloaded (N, False);
10233 end if;
10234 end Rewrite_Renamed_Operator;
10236 -----------------------
10237 -- Set_Slice_Subtype --
10238 -----------------------
10240 -- Build an implicit subtype declaration to represent the type delivered by
10241 -- the slice. This is an abbreviated version of an array subtype. We define
10242 -- an index subtype for the slice, using either the subtype name or the
10243 -- discrete range of the slice. To be consistent with index usage elsewhere
10244 -- we create a list header to hold the single index. This list is not
10245 -- otherwise attached to the syntax tree.
10247 procedure Set_Slice_Subtype (N : Node_Id) is
10248 Loc : constant Source_Ptr := Sloc (N);
10249 Index_List : constant List_Id := New_List;
10250 Index : Node_Id;
10251 Index_Subtype : Entity_Id;
10252 Index_Type : Entity_Id;
10253 Slice_Subtype : Entity_Id;
10254 Drange : constant Node_Id := Discrete_Range (N);
10256 begin
10257 if Is_Entity_Name (Drange) then
10258 Index_Subtype := Entity (Drange);
10260 else
10261 -- We force the evaluation of a range. This is definitely needed in
10262 -- the renamed case, and seems safer to do unconditionally. Note in
10263 -- any case that since we will create and insert an Itype referring
10264 -- to this range, we must make sure any side effect removal actions
10265 -- are inserted before the Itype definition.
10267 if Nkind (Drange) = N_Range then
10268 Force_Evaluation (Low_Bound (Drange));
10269 Force_Evaluation (High_Bound (Drange));
10270 end if;
10272 Index_Type := Base_Type (Etype (Drange));
10274 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
10276 -- Take a new copy of Drange (where bounds have been rewritten to
10277 -- reference side-effect-free names). Using a separate tree ensures
10278 -- that further expansion (e.g. while rewriting a slice assignment
10279 -- into a FOR loop) does not attempt to remove side effects on the
10280 -- bounds again (which would cause the bounds in the index subtype
10281 -- definition to refer to temporaries before they are defined) (the
10282 -- reason is that some names are considered side effect free here
10283 -- for the subtype, but not in the context of a loop iteration
10284 -- scheme).
10286 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
10287 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
10288 Set_Etype (Index_Subtype, Index_Type);
10289 Set_Size_Info (Index_Subtype, Index_Type);
10290 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
10291 end if;
10293 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
10295 Index := New_Occurrence_Of (Index_Subtype, Loc);
10296 Set_Etype (Index, Index_Subtype);
10297 Append (Index, Index_List);
10299 Set_First_Index (Slice_Subtype, Index);
10300 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
10301 Set_Is_Constrained (Slice_Subtype, True);
10303 Check_Compile_Time_Size (Slice_Subtype);
10305 -- The Etype of the existing Slice node is reset to this slice subtype.
10306 -- Its bounds are obtained from its first index.
10308 Set_Etype (N, Slice_Subtype);
10310 -- For packed slice subtypes, freeze immediately (except in the case of
10311 -- being in a "spec expression" where we never freeze when we first see
10312 -- the expression).
10314 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
10315 Freeze_Itype (Slice_Subtype, N);
10317 -- For all other cases insert an itype reference in the slice's actions
10318 -- so that the itype is frozen at the proper place in the tree (i.e. at
10319 -- the point where actions for the slice are analyzed). Note that this
10320 -- is different from freezing the itype immediately, which might be
10321 -- premature (e.g. if the slice is within a transient scope). This needs
10322 -- to be done only if expansion is enabled.
10324 elsif Full_Expander_Active then
10325 Ensure_Defined (Typ => Slice_Subtype, N => N);
10326 end if;
10327 end Set_Slice_Subtype;
10329 --------------------------------
10330 -- Set_String_Literal_Subtype --
10331 --------------------------------
10333 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
10334 Loc : constant Source_Ptr := Sloc (N);
10335 Low_Bound : constant Node_Id :=
10336 Type_Low_Bound (Etype (First_Index (Typ)));
10337 Subtype_Id : Entity_Id;
10339 begin
10340 if Nkind (N) /= N_String_Literal then
10341 return;
10342 end if;
10344 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
10345 Set_String_Literal_Length (Subtype_Id, UI_From_Int
10346 (String_Length (Strval (N))));
10347 Set_Etype (Subtype_Id, Base_Type (Typ));
10348 Set_Is_Constrained (Subtype_Id);
10349 Set_Etype (N, Subtype_Id);
10351 -- The low bound is set from the low bound of the corresponding index
10352 -- type. Note that we do not store the high bound in the string literal
10353 -- subtype, but it can be deduced if necessary from the length and the
10354 -- low bound.
10356 if Is_OK_Static_Expression (Low_Bound) then
10357 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
10359 -- If the lower bound is not static we create a range for the string
10360 -- literal, using the index type and the known length of the literal.
10361 -- The index type is not necessarily Positive, so the upper bound is
10362 -- computed as T'Val (T'Pos (Low_Bound) + L - 1).
10364 else
10365 declare
10366 Index_List : constant List_Id := New_List;
10367 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
10368 High_Bound : constant Node_Id :=
10369 Make_Attribute_Reference (Loc,
10370 Attribute_Name => Name_Val,
10371 Prefix =>
10372 New_Occurrence_Of (Index_Type, Loc),
10373 Expressions => New_List (
10374 Make_Op_Add (Loc,
10375 Left_Opnd =>
10376 Make_Attribute_Reference (Loc,
10377 Attribute_Name => Name_Pos,
10378 Prefix =>
10379 New_Occurrence_Of (Index_Type, Loc),
10380 Expressions =>
10381 New_List (New_Copy_Tree (Low_Bound))),
10382 Right_Opnd =>
10383 Make_Integer_Literal (Loc,
10384 String_Length (Strval (N)) - 1))));
10386 Array_Subtype : Entity_Id;
10387 Drange : Node_Id;
10388 Index : Node_Id;
10389 Index_Subtype : Entity_Id;
10391 begin
10392 if Is_Integer_Type (Index_Type) then
10393 Set_String_Literal_Low_Bound
10394 (Subtype_Id, Make_Integer_Literal (Loc, 1));
10396 else
10397 -- If the index type is an enumeration type, build bounds
10398 -- expression with attributes.
10400 Set_String_Literal_Low_Bound
10401 (Subtype_Id,
10402 Make_Attribute_Reference (Loc,
10403 Attribute_Name => Name_First,
10404 Prefix =>
10405 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
10406 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Index_Type);
10407 end if;
10409 Analyze_And_Resolve (String_Literal_Low_Bound (Subtype_Id));
10411 -- Build bona fide subtype for the string, and wrap it in an
10412 -- unchecked conversion, because the backend expects the
10413 -- String_Literal_Subtype to have a static lower bound.
10415 Index_Subtype :=
10416 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
10417 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
10418 Set_Scalar_Range (Index_Subtype, Drange);
10419 Set_Parent (Drange, N);
10420 Analyze_And_Resolve (Drange, Index_Type);
10422 -- In the context, the Index_Type may already have a constraint,
10423 -- so use common base type on string subtype. The base type may
10424 -- be used when generating attributes of the string, for example
10425 -- in the context of a slice assignment.
10427 Set_Etype (Index_Subtype, Base_Type (Index_Type));
10428 Set_Size_Info (Index_Subtype, Index_Type);
10429 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
10431 Array_Subtype := Create_Itype (E_Array_Subtype, N);
10433 Index := New_Occurrence_Of (Index_Subtype, Loc);
10434 Set_Etype (Index, Index_Subtype);
10435 Append (Index, Index_List);
10437 Set_First_Index (Array_Subtype, Index);
10438 Set_Etype (Array_Subtype, Base_Type (Typ));
10439 Set_Is_Constrained (Array_Subtype, True);
10441 Rewrite (N,
10442 Make_Unchecked_Type_Conversion (Loc,
10443 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
10444 Expression => Relocate_Node (N)));
10445 Set_Etype (N, Array_Subtype);
10446 end;
10447 end if;
10448 end Set_String_Literal_Subtype;
10450 ------------------------------
10451 -- Simplify_Type_Conversion --
10452 ------------------------------
10454 procedure Simplify_Type_Conversion (N : Node_Id) is
10455 begin
10456 if Nkind (N) = N_Type_Conversion then
10457 declare
10458 Operand : constant Node_Id := Expression (N);
10459 Target_Typ : constant Entity_Id := Etype (N);
10460 Opnd_Typ : constant Entity_Id := Etype (Operand);
10462 begin
10463 if Is_Floating_Point_Type (Opnd_Typ)
10464 and then
10465 (Is_Integer_Type (Target_Typ)
10466 or else (Is_Fixed_Point_Type (Target_Typ)
10467 and then Conversion_OK (N)))
10468 and then Nkind (Operand) = N_Attribute_Reference
10469 and then Attribute_Name (Operand) = Name_Truncation
10471 -- Special processing required if the conversion is the expression
10472 -- of a Truncation attribute reference. In this case we replace:
10474 -- ityp (ftyp'Truncation (x))
10476 -- by
10478 -- ityp (x)
10480 -- with the Float_Truncate flag set, which is more efficient.
10482 then
10483 Rewrite (Operand,
10484 Relocate_Node (First (Expressions (Operand))));
10485 Set_Float_Truncate (N, True);
10486 end if;
10487 end;
10488 end if;
10489 end Simplify_Type_Conversion;
10491 -----------------------------
10492 -- Unique_Fixed_Point_Type --
10493 -----------------------------
10495 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
10496 T1 : Entity_Id := Empty;
10497 T2 : Entity_Id;
10498 Item : Node_Id;
10499 Scop : Entity_Id;
10501 procedure Fixed_Point_Error;
10502 -- Give error messages for true ambiguity. Messages are posted on node
10503 -- N, and entities T1, T2 are the possible interpretations.
10505 -----------------------
10506 -- Fixed_Point_Error --
10507 -----------------------
10509 procedure Fixed_Point_Error is
10510 begin
10511 Error_Msg_N ("ambiguous universal_fixed_expression", N);
10512 Error_Msg_NE ("\\possible interpretation as}", N, T1);
10513 Error_Msg_NE ("\\possible interpretation as}", N, T2);
10514 end Fixed_Point_Error;
10516 -- Start of processing for Unique_Fixed_Point_Type
10518 begin
10519 -- The operations on Duration are visible, so Duration is always a
10520 -- possible interpretation.
10522 T1 := Standard_Duration;
10524 -- Look for fixed-point types in enclosing scopes
10526 Scop := Current_Scope;
10527 while Scop /= Standard_Standard loop
10528 T2 := First_Entity (Scop);
10529 while Present (T2) loop
10530 if Is_Fixed_Point_Type (T2)
10531 and then Current_Entity (T2) = T2
10532 and then Scope (Base_Type (T2)) = Scop
10533 then
10534 if Present (T1) then
10535 Fixed_Point_Error;
10536 return Any_Type;
10537 else
10538 T1 := T2;
10539 end if;
10540 end if;
10542 Next_Entity (T2);
10543 end loop;
10545 Scop := Scope (Scop);
10546 end loop;
10548 -- Look for visible fixed type declarations in the context
10550 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
10551 while Present (Item) loop
10552 if Nkind (Item) = N_With_Clause then
10553 Scop := Entity (Name (Item));
10554 T2 := First_Entity (Scop);
10555 while Present (T2) loop
10556 if Is_Fixed_Point_Type (T2)
10557 and then Scope (Base_Type (T2)) = Scop
10558 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
10559 then
10560 if Present (T1) then
10561 Fixed_Point_Error;
10562 return Any_Type;
10563 else
10564 T1 := T2;
10565 end if;
10566 end if;
10568 Next_Entity (T2);
10569 end loop;
10570 end if;
10572 Next (Item);
10573 end loop;
10575 if Nkind (N) = N_Real_Literal then
10576 Error_Msg_NE
10577 ("??real literal interpreted as }!", N, T1);
10578 else
10579 Error_Msg_NE
10580 ("??universal_fixed expression interpreted as }!", N, T1);
10581 end if;
10583 return T1;
10584 end Unique_Fixed_Point_Type;
10586 ----------------------
10587 -- Valid_Conversion --
10588 ----------------------
10590 function Valid_Conversion
10591 (N : Node_Id;
10592 Target : Entity_Id;
10593 Operand : Node_Id;
10594 Report_Errs : Boolean := True) return Boolean
10596 Target_Type : constant Entity_Id := Base_Type (Target);
10597 Opnd_Type : Entity_Id := Etype (Operand);
10598 Inc_Ancestor : Entity_Id;
10600 function Conversion_Check
10601 (Valid : Boolean;
10602 Msg : String) return Boolean;
10603 -- Little routine to post Msg if Valid is False, returns Valid value
10605 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id);
10606 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
10608 procedure Conversion_Error_NE
10609 (Msg : String;
10610 N : Node_Or_Entity_Id;
10611 E : Node_Or_Entity_Id);
10612 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
10614 function Valid_Tagged_Conversion
10615 (Target_Type : Entity_Id;
10616 Opnd_Type : Entity_Id) return Boolean;
10617 -- Specifically test for validity of tagged conversions
10619 function Valid_Array_Conversion return Boolean;
10620 -- Check index and component conformance, and accessibility levels if
10621 -- the component types are anonymous access types (Ada 2005).
10623 ----------------------
10624 -- Conversion_Check --
10625 ----------------------
10627 function Conversion_Check
10628 (Valid : Boolean;
10629 Msg : String) return Boolean
10631 begin
10632 if not Valid
10634 -- A generic unit has already been analyzed and we have verified
10635 -- that a particular conversion is OK in that context. Since the
10636 -- instance is reanalyzed without relying on the relationships
10637 -- established during the analysis of the generic, it is possible
10638 -- to end up with inconsistent views of private types. Do not emit
10639 -- the error message in such cases. The rest of the machinery in
10640 -- Valid_Conversion still ensures the proper compatibility of
10641 -- target and operand types.
10643 and then not In_Instance
10644 then
10645 Conversion_Error_N (Msg, Operand);
10646 end if;
10648 return Valid;
10649 end Conversion_Check;
10651 ------------------------
10652 -- Conversion_Error_N --
10653 ------------------------
10655 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id) is
10656 begin
10657 if Report_Errs then
10658 Error_Msg_N (Msg, N);
10659 end if;
10660 end Conversion_Error_N;
10662 -------------------------
10663 -- Conversion_Error_NE --
10664 -------------------------
10666 procedure Conversion_Error_NE
10667 (Msg : String;
10668 N : Node_Or_Entity_Id;
10669 E : Node_Or_Entity_Id)
10671 begin
10672 if Report_Errs then
10673 Error_Msg_NE (Msg, N, E);
10674 end if;
10675 end Conversion_Error_NE;
10677 ----------------------------
10678 -- Valid_Array_Conversion --
10679 ----------------------------
10681 function Valid_Array_Conversion return Boolean
10683 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
10684 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
10686 Opnd_Index : Node_Id;
10687 Opnd_Index_Type : Entity_Id;
10689 Target_Comp_Type : constant Entity_Id :=
10690 Component_Type (Target_Type);
10691 Target_Comp_Base : constant Entity_Id :=
10692 Base_Type (Target_Comp_Type);
10694 Target_Index : Node_Id;
10695 Target_Index_Type : Entity_Id;
10697 begin
10698 -- Error if wrong number of dimensions
10701 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
10702 then
10703 Conversion_Error_N
10704 ("incompatible number of dimensions for conversion", Operand);
10705 return False;
10707 -- Number of dimensions matches
10709 else
10710 -- Loop through indexes of the two arrays
10712 Target_Index := First_Index (Target_Type);
10713 Opnd_Index := First_Index (Opnd_Type);
10714 while Present (Target_Index) and then Present (Opnd_Index) loop
10715 Target_Index_Type := Etype (Target_Index);
10716 Opnd_Index_Type := Etype (Opnd_Index);
10718 -- Error if index types are incompatible
10720 if not (Is_Integer_Type (Target_Index_Type)
10721 and then Is_Integer_Type (Opnd_Index_Type))
10722 and then (Root_Type (Target_Index_Type)
10723 /= Root_Type (Opnd_Index_Type))
10724 then
10725 Conversion_Error_N
10726 ("incompatible index types for array conversion",
10727 Operand);
10728 return False;
10729 end if;
10731 Next_Index (Target_Index);
10732 Next_Index (Opnd_Index);
10733 end loop;
10735 -- If component types have same base type, all set
10737 if Target_Comp_Base = Opnd_Comp_Base then
10738 null;
10740 -- Here if base types of components are not the same. The only
10741 -- time this is allowed is if we have anonymous access types.
10743 -- The conversion of arrays of anonymous access types can lead
10744 -- to dangling pointers. AI-392 formalizes the accessibility
10745 -- checks that must be applied to such conversions to prevent
10746 -- out-of-scope references.
10748 elsif Ekind_In
10749 (Target_Comp_Base, E_Anonymous_Access_Type,
10750 E_Anonymous_Access_Subprogram_Type)
10751 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
10752 and then
10753 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
10754 then
10755 if Type_Access_Level (Target_Type) <
10756 Deepest_Type_Access_Level (Opnd_Type)
10757 then
10758 if In_Instance_Body then
10759 Conversion_Error_N
10760 ("??source array type has deeper accessibility "
10761 & "level than target", Operand);
10762 Conversion_Error_N
10763 ("\??Program_Error will be raised at run time",
10764 Operand);
10765 Rewrite (N,
10766 Make_Raise_Program_Error (Sloc (N),
10767 Reason => PE_Accessibility_Check_Failed));
10768 Set_Etype (N, Target_Type);
10769 return False;
10771 -- Conversion not allowed because of accessibility levels
10773 else
10774 Conversion_Error_N
10775 ("source array type has deeper accessibility "
10776 & "level than target", Operand);
10777 return False;
10778 end if;
10780 else
10781 null;
10782 end if;
10784 -- All other cases where component base types do not match
10786 else
10787 Conversion_Error_N
10788 ("incompatible component types for array conversion",
10789 Operand);
10790 return False;
10791 end if;
10793 -- Check that component subtypes statically match. For numeric
10794 -- types this means that both must be either constrained or
10795 -- unconstrained. For enumeration types the bounds must match.
10796 -- All of this is checked in Subtypes_Statically_Match.
10798 if not Subtypes_Statically_Match
10799 (Target_Comp_Type, Opnd_Comp_Type)
10800 then
10801 Conversion_Error_N
10802 ("component subtypes must statically match", Operand);
10803 return False;
10804 end if;
10805 end if;
10807 return True;
10808 end Valid_Array_Conversion;
10810 -----------------------------
10811 -- Valid_Tagged_Conversion --
10812 -----------------------------
10814 function Valid_Tagged_Conversion
10815 (Target_Type : Entity_Id;
10816 Opnd_Type : Entity_Id) return Boolean
10818 begin
10819 -- Upward conversions are allowed (RM 4.6(22))
10821 if Covers (Target_Type, Opnd_Type)
10822 or else Is_Ancestor (Target_Type, Opnd_Type)
10823 then
10824 return True;
10826 -- Downward conversion are allowed if the operand is class-wide
10827 -- (RM 4.6(23)).
10829 elsif Is_Class_Wide_Type (Opnd_Type)
10830 and then Covers (Opnd_Type, Target_Type)
10831 then
10832 return True;
10834 elsif Covers (Opnd_Type, Target_Type)
10835 or else Is_Ancestor (Opnd_Type, Target_Type)
10836 then
10837 return
10838 Conversion_Check (False,
10839 "downward conversion of tagged objects not allowed");
10841 -- Ada 2005 (AI-251): The conversion to/from interface types is
10842 -- always valid
10844 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
10845 return True;
10847 -- If the operand is a class-wide type obtained through a limited_
10848 -- with clause, and the context includes the non-limited view, use
10849 -- it to determine whether the conversion is legal.
10851 elsif Is_Class_Wide_Type (Opnd_Type)
10852 and then From_With_Type (Opnd_Type)
10853 and then Present (Non_Limited_View (Etype (Opnd_Type)))
10854 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
10855 then
10856 return True;
10858 elsif Is_Access_Type (Opnd_Type)
10859 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
10860 then
10861 return True;
10863 else
10864 Conversion_Error_NE
10865 ("invalid tagged conversion, not compatible with}",
10866 N, First_Subtype (Opnd_Type));
10867 return False;
10868 end if;
10869 end Valid_Tagged_Conversion;
10871 -- Start of processing for Valid_Conversion
10873 begin
10874 Check_Parameterless_Call (Operand);
10876 if Is_Overloaded (Operand) then
10877 declare
10878 I : Interp_Index;
10879 I1 : Interp_Index;
10880 It : Interp;
10881 It1 : Interp;
10882 N1 : Entity_Id;
10883 T1 : Entity_Id;
10885 begin
10886 -- Remove procedure calls, which syntactically cannot appear in
10887 -- this context, but which cannot be removed by type checking,
10888 -- because the context does not impose a type.
10890 -- When compiling for VMS, spurious ambiguities can be produced
10891 -- when arithmetic operations have a literal operand and return
10892 -- System.Address or a descendant of it. These ambiguities are
10893 -- otherwise resolved by the context, but for conversions there
10894 -- is no context type and the removal of the spurious operations
10895 -- must be done explicitly here.
10897 -- The node may be labelled overloaded, but still contain only one
10898 -- interpretation because others were discarded earlier. If this
10899 -- is the case, retain the single interpretation if legal.
10901 Get_First_Interp (Operand, I, It);
10902 Opnd_Type := It.Typ;
10903 Get_Next_Interp (I, It);
10905 if Present (It.Typ)
10906 and then Opnd_Type /= Standard_Void_Type
10907 then
10908 -- More than one candidate interpretation is available
10910 Get_First_Interp (Operand, I, It);
10911 while Present (It.Typ) loop
10912 if It.Typ = Standard_Void_Type then
10913 Remove_Interp (I);
10914 end if;
10916 if Present (System_Aux_Id)
10917 and then Is_Descendent_Of_Address (It.Typ)
10918 then
10919 Remove_Interp (I);
10920 end if;
10922 Get_Next_Interp (I, It);
10923 end loop;
10924 end if;
10926 Get_First_Interp (Operand, I, It);
10927 I1 := I;
10928 It1 := It;
10930 if No (It.Typ) then
10931 Conversion_Error_N ("illegal operand in conversion", Operand);
10932 return False;
10933 end if;
10935 Get_Next_Interp (I, It);
10937 if Present (It.Typ) then
10938 N1 := It1.Nam;
10939 T1 := It1.Typ;
10940 It1 := Disambiguate (Operand, I1, I, Any_Type);
10942 if It1 = No_Interp then
10943 Conversion_Error_N
10944 ("ambiguous operand in conversion", Operand);
10946 -- If the interpretation involves a standard operator, use
10947 -- the location of the type, which may be user-defined.
10949 if Sloc (It.Nam) = Standard_Location then
10950 Error_Msg_Sloc := Sloc (It.Typ);
10951 else
10952 Error_Msg_Sloc := Sloc (It.Nam);
10953 end if;
10955 Conversion_Error_N -- CODEFIX
10956 ("\\possible interpretation#!", Operand);
10958 if Sloc (N1) = Standard_Location then
10959 Error_Msg_Sloc := Sloc (T1);
10960 else
10961 Error_Msg_Sloc := Sloc (N1);
10962 end if;
10964 Conversion_Error_N -- CODEFIX
10965 ("\\possible interpretation#!", Operand);
10967 return False;
10968 end if;
10969 end if;
10971 Set_Etype (Operand, It1.Typ);
10972 Opnd_Type := It1.Typ;
10973 end;
10974 end if;
10976 -- If we are within a child unit, check whether the type of the
10977 -- expression has an ancestor in a parent unit, in which case it
10978 -- belongs to its derivation class even if the ancestor is private.
10979 -- See RM 7.3.1 (5.2/3).
10981 Inc_Ancestor := Get_Incomplete_View_Of_Ancestor (Opnd_Type);
10983 -- Numeric types
10985 if Is_Numeric_Type (Target_Type) then
10987 -- A universal fixed expression can be converted to any numeric type
10989 if Opnd_Type = Universal_Fixed then
10990 return True;
10992 -- Also no need to check when in an instance or inlined body, because
10993 -- the legality has been established when the template was analyzed.
10994 -- Furthermore, numeric conversions may occur where only a private
10995 -- view of the operand type is visible at the instantiation point.
10996 -- This results in a spurious error if we check that the operand type
10997 -- is a numeric type.
10999 -- Note: in a previous version of this unit, the following tests were
11000 -- applied only for generated code (Comes_From_Source set to False),
11001 -- but in fact the test is required for source code as well, since
11002 -- this situation can arise in source code.
11004 elsif In_Instance or else In_Inlined_Body then
11005 return True;
11007 -- Otherwise we need the conversion check
11009 else
11010 return Conversion_Check
11011 (Is_Numeric_Type (Opnd_Type)
11012 or else
11013 (Present (Inc_Ancestor)
11014 and then Is_Numeric_Type (Inc_Ancestor)),
11015 "illegal operand for numeric conversion");
11016 end if;
11018 -- Array types
11020 elsif Is_Array_Type (Target_Type) then
11021 if not Is_Array_Type (Opnd_Type)
11022 or else Opnd_Type = Any_Composite
11023 or else Opnd_Type = Any_String
11024 then
11025 Conversion_Error_N
11026 ("illegal operand for array conversion", Operand);
11027 return False;
11029 else
11030 return Valid_Array_Conversion;
11031 end if;
11033 -- Ada 2005 (AI-251): Anonymous access types where target references an
11034 -- interface type.
11036 elsif Ekind_In (Target_Type, E_General_Access_Type,
11037 E_Anonymous_Access_Type)
11038 and then Is_Interface (Directly_Designated_Type (Target_Type))
11039 then
11040 -- Check the static accessibility rule of 4.6(17). Note that the
11041 -- check is not enforced when within an instance body, since the
11042 -- RM requires such cases to be caught at run time.
11044 -- If the operand is a rewriting of an allocator no check is needed
11045 -- because there are no accessibility issues.
11047 if Nkind (Original_Node (N)) = N_Allocator then
11048 null;
11050 elsif Ekind (Target_Type) /= E_Anonymous_Access_Type then
11051 if Type_Access_Level (Opnd_Type) >
11052 Deepest_Type_Access_Level (Target_Type)
11053 then
11054 -- In an instance, this is a run-time check, but one we know
11055 -- will fail, so generate an appropriate warning. The raise
11056 -- will be generated by Expand_N_Type_Conversion.
11058 if In_Instance_Body then
11059 Conversion_Error_N
11060 ("??cannot convert local pointer to non-local access type",
11061 Operand);
11062 Conversion_Error_N
11063 ("\??Program_Error will be raised at run time", Operand);
11065 else
11066 Conversion_Error_N
11067 ("cannot convert local pointer to non-local access type",
11068 Operand);
11069 return False;
11070 end if;
11072 -- Special accessibility checks are needed in the case of access
11073 -- discriminants declared for a limited type.
11075 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
11076 and then not Is_Local_Anonymous_Access (Opnd_Type)
11077 then
11078 -- When the operand is a selected access discriminant the check
11079 -- needs to be made against the level of the object denoted by
11080 -- the prefix of the selected name (Object_Access_Level handles
11081 -- checking the prefix of the operand for this case).
11083 if Nkind (Operand) = N_Selected_Component
11084 and then Object_Access_Level (Operand) >
11085 Deepest_Type_Access_Level (Target_Type)
11086 then
11087 -- In an instance, this is a run-time check, but one we know
11088 -- will fail, so generate an appropriate warning. The raise
11089 -- will be generated by Expand_N_Type_Conversion.
11091 if In_Instance_Body then
11092 Conversion_Error_N
11093 ("??cannot convert access discriminant to non-local "
11094 & "access type", Operand);
11095 Conversion_Error_N
11096 ("\??Program_Error will be raised at run time",
11097 Operand);
11098 else
11099 Conversion_Error_N
11100 ("cannot convert access discriminant to non-local "
11101 & "access type", Operand);
11102 return False;
11103 end if;
11104 end if;
11106 -- The case of a reference to an access discriminant from
11107 -- within a limited type declaration (which will appear as
11108 -- a discriminal) is always illegal because the level of the
11109 -- discriminant is considered to be deeper than any (nameable)
11110 -- access type.
11112 if Is_Entity_Name (Operand)
11113 and then not Is_Local_Anonymous_Access (Opnd_Type)
11114 and then
11115 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
11116 and then Present (Discriminal_Link (Entity (Operand)))
11117 then
11118 Conversion_Error_N
11119 ("discriminant has deeper accessibility level than target",
11120 Operand);
11121 return False;
11122 end if;
11123 end if;
11124 end if;
11126 return True;
11128 -- General and anonymous access types
11130 elsif Ekind_In (Target_Type, E_General_Access_Type,
11131 E_Anonymous_Access_Type)
11132 and then
11133 Conversion_Check
11134 (Is_Access_Type (Opnd_Type)
11135 and then not
11136 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
11137 E_Access_Protected_Subprogram_Type),
11138 "must be an access-to-object type")
11139 then
11140 if Is_Access_Constant (Opnd_Type)
11141 and then not Is_Access_Constant (Target_Type)
11142 then
11143 Conversion_Error_N
11144 ("access-to-constant operand type not allowed", Operand);
11145 return False;
11146 end if;
11148 -- Check the static accessibility rule of 4.6(17). Note that the
11149 -- check is not enforced when within an instance body, since the RM
11150 -- requires such cases to be caught at run time.
11152 if Ekind (Target_Type) /= E_Anonymous_Access_Type
11153 or else Is_Local_Anonymous_Access (Target_Type)
11154 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
11155 N_Object_Declaration
11156 then
11157 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
11158 -- conversions from an anonymous access type to a named general
11159 -- access type. Such conversions are not allowed in the case of
11160 -- access parameters and stand-alone objects of an anonymous
11161 -- access type. The implicit conversion case is recognized by
11162 -- testing that Comes_From_Source is False and that it's been
11163 -- rewritten. The Comes_From_Source test isn't sufficient because
11164 -- nodes in inlined calls to predefined library routines can have
11165 -- Comes_From_Source set to False. (Is there a better way to test
11166 -- for implicit conversions???)
11168 if Ada_Version >= Ada_2012
11169 and then not Comes_From_Source (N)
11170 and then N /= Original_Node (N)
11171 and then Ekind (Target_Type) = E_General_Access_Type
11172 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
11173 then
11174 if Is_Itype (Opnd_Type) then
11176 -- Implicit conversions aren't allowed for objects of an
11177 -- anonymous access type, since such objects have nonstatic
11178 -- levels in Ada 2012.
11180 if Nkind (Associated_Node_For_Itype (Opnd_Type)) =
11181 N_Object_Declaration
11182 then
11183 Conversion_Error_N
11184 ("implicit conversion of stand-alone anonymous "
11185 & "access object not allowed", Operand);
11186 return False;
11188 -- Implicit conversions aren't allowed for anonymous access
11189 -- parameters. The "not Is_Local_Anonymous_Access_Type" test
11190 -- is done to exclude anonymous access results.
11192 elsif not Is_Local_Anonymous_Access (Opnd_Type)
11193 and then Nkind_In (Associated_Node_For_Itype (Opnd_Type),
11194 N_Function_Specification,
11195 N_Procedure_Specification)
11196 then
11197 Conversion_Error_N
11198 ("implicit conversion of anonymous access formal "
11199 & "not allowed", Operand);
11200 return False;
11202 -- This is a case where there's an enclosing object whose
11203 -- to which the "statically deeper than" relationship does
11204 -- not apply (such as an access discriminant selected from
11205 -- a dereference of an access parameter).
11207 elsif Object_Access_Level (Operand)
11208 = Scope_Depth (Standard_Standard)
11209 then
11210 Conversion_Error_N
11211 ("implicit conversion of anonymous access value "
11212 & "not allowed", Operand);
11213 return False;
11215 -- In other cases, the level of the operand's type must be
11216 -- statically less deep than that of the target type, else
11217 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
11219 elsif Type_Access_Level (Opnd_Type) >
11220 Deepest_Type_Access_Level (Target_Type)
11221 then
11222 Conversion_Error_N
11223 ("implicit conversion of anonymous access value "
11224 & "violates accessibility", Operand);
11225 return False;
11226 end if;
11227 end if;
11229 elsif Type_Access_Level (Opnd_Type) >
11230 Deepest_Type_Access_Level (Target_Type)
11231 then
11232 -- In an instance, this is a run-time check, but one we know
11233 -- will fail, so generate an appropriate warning. The raise
11234 -- will be generated by Expand_N_Type_Conversion.
11236 if In_Instance_Body then
11237 Conversion_Error_N
11238 ("??cannot convert local pointer to non-local access type",
11239 Operand);
11240 Conversion_Error_N
11241 ("\??Program_Error will be raised at run time", Operand);
11243 else
11244 -- Avoid generation of spurious error message
11246 if not Error_Posted (N) then
11247 Conversion_Error_N
11248 ("cannot convert local pointer to non-local access type",
11249 Operand);
11250 end if;
11252 return False;
11253 end if;
11255 -- Special accessibility checks are needed in the case of access
11256 -- discriminants declared for a limited type.
11258 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
11259 and then not Is_Local_Anonymous_Access (Opnd_Type)
11260 then
11261 -- When the operand is a selected access discriminant the check
11262 -- needs to be made against the level of the object denoted by
11263 -- the prefix of the selected name (Object_Access_Level handles
11264 -- checking the prefix of the operand for this case).
11266 if Nkind (Operand) = N_Selected_Component
11267 and then Object_Access_Level (Operand) >
11268 Deepest_Type_Access_Level (Target_Type)
11269 then
11270 -- In an instance, this is a run-time check, but one we know
11271 -- will fail, so generate an appropriate warning. The raise
11272 -- will be generated by Expand_N_Type_Conversion.
11274 if In_Instance_Body then
11275 Conversion_Error_N
11276 ("??cannot convert access discriminant to non-local "
11277 & "access type", Operand);
11278 Conversion_Error_N
11279 ("\??Program_Error will be raised at run time",
11280 Operand);
11282 else
11283 Conversion_Error_N
11284 ("cannot convert access discriminant to non-local "
11285 & "access type", Operand);
11286 return False;
11287 end if;
11288 end if;
11290 -- The case of a reference to an access discriminant from
11291 -- within a limited type declaration (which will appear as
11292 -- a discriminal) is always illegal because the level of the
11293 -- discriminant is considered to be deeper than any (nameable)
11294 -- access type.
11296 if Is_Entity_Name (Operand)
11297 and then
11298 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
11299 and then Present (Discriminal_Link (Entity (Operand)))
11300 then
11301 Conversion_Error_N
11302 ("discriminant has deeper accessibility level than target",
11303 Operand);
11304 return False;
11305 end if;
11306 end if;
11307 end if;
11309 -- In the presence of limited_with clauses we have to use non-limited
11310 -- views, if available.
11312 Check_Limited : declare
11313 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
11314 -- Helper function to handle limited views
11316 --------------------------
11317 -- Full_Designated_Type --
11318 --------------------------
11320 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
11321 Desig : constant Entity_Id := Designated_Type (T);
11323 begin
11324 -- Handle the limited view of a type
11326 if Is_Incomplete_Type (Desig)
11327 and then From_With_Type (Desig)
11328 and then Present (Non_Limited_View (Desig))
11329 then
11330 return Available_View (Desig);
11331 else
11332 return Desig;
11333 end if;
11334 end Full_Designated_Type;
11336 -- Local Declarations
11338 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
11339 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
11341 Same_Base : constant Boolean :=
11342 Base_Type (Target) = Base_Type (Opnd);
11344 -- Start of processing for Check_Limited
11346 begin
11347 if Is_Tagged_Type (Target) then
11348 return Valid_Tagged_Conversion (Target, Opnd);
11350 else
11351 if not Same_Base then
11352 Conversion_Error_NE
11353 ("target designated type not compatible with }",
11354 N, Base_Type (Opnd));
11355 return False;
11357 -- Ada 2005 AI-384: legality rule is symmetric in both
11358 -- designated types. The conversion is legal (with possible
11359 -- constraint check) if either designated type is
11360 -- unconstrained.
11362 elsif Subtypes_Statically_Match (Target, Opnd)
11363 or else
11364 (Has_Discriminants (Target)
11365 and then
11366 (not Is_Constrained (Opnd)
11367 or else not Is_Constrained (Target)))
11368 then
11369 -- Special case, if Value_Size has been used to make the
11370 -- sizes different, the conversion is not allowed even
11371 -- though the subtypes statically match.
11373 if Known_Static_RM_Size (Target)
11374 and then Known_Static_RM_Size (Opnd)
11375 and then RM_Size (Target) /= RM_Size (Opnd)
11376 then
11377 Conversion_Error_NE
11378 ("target designated subtype not compatible with }",
11379 N, Opnd);
11380 Conversion_Error_NE
11381 ("\because sizes of the two designated subtypes differ",
11382 N, Opnd);
11383 return False;
11385 -- Normal case where conversion is allowed
11387 else
11388 return True;
11389 end if;
11391 else
11392 Error_Msg_NE
11393 ("target designated subtype not compatible with }",
11394 N, Opnd);
11395 return False;
11396 end if;
11397 end if;
11398 end Check_Limited;
11400 -- Access to subprogram types. If the operand is an access parameter,
11401 -- the type has a deeper accessibility that any master, and cannot be
11402 -- assigned. We must make an exception if the conversion is part of an
11403 -- assignment and the target is the return object of an extended return
11404 -- statement, because in that case the accessibility check takes place
11405 -- after the return.
11407 elsif Is_Access_Subprogram_Type (Target_Type)
11408 and then No (Corresponding_Remote_Type (Opnd_Type))
11409 then
11410 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
11411 and then Is_Entity_Name (Operand)
11412 and then Ekind (Entity (Operand)) = E_In_Parameter
11413 and then
11414 (Nkind (Parent (N)) /= N_Assignment_Statement
11415 or else not Is_Entity_Name (Name (Parent (N)))
11416 or else not Is_Return_Object (Entity (Name (Parent (N)))))
11417 then
11418 Conversion_Error_N
11419 ("illegal attempt to store anonymous access to subprogram",
11420 Operand);
11421 Conversion_Error_N
11422 ("\value has deeper accessibility than any master "
11423 & "(RM 3.10.2 (13))",
11424 Operand);
11426 Error_Msg_NE
11427 ("\use named access type for& instead of access parameter",
11428 Operand, Entity (Operand));
11429 end if;
11431 -- Check that the designated types are subtype conformant
11433 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
11434 Old_Id => Designated_Type (Opnd_Type),
11435 Err_Loc => N);
11437 -- Check the static accessibility rule of 4.6(20)
11439 if Type_Access_Level (Opnd_Type) >
11440 Deepest_Type_Access_Level (Target_Type)
11441 then
11442 Conversion_Error_N
11443 ("operand type has deeper accessibility level than target",
11444 Operand);
11446 -- Check that if the operand type is declared in a generic body,
11447 -- then the target type must be declared within that same body
11448 -- (enforces last sentence of 4.6(20)).
11450 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
11451 declare
11452 O_Gen : constant Node_Id :=
11453 Enclosing_Generic_Body (Opnd_Type);
11455 T_Gen : Node_Id;
11457 begin
11458 T_Gen := Enclosing_Generic_Body (Target_Type);
11459 while Present (T_Gen) and then T_Gen /= O_Gen loop
11460 T_Gen := Enclosing_Generic_Body (T_Gen);
11461 end loop;
11463 if T_Gen /= O_Gen then
11464 Conversion_Error_N
11465 ("target type must be declared in same generic body "
11466 & "as operand type", N);
11467 end if;
11468 end;
11469 end if;
11471 return True;
11473 -- Remote subprogram access types
11475 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
11476 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
11477 then
11478 -- It is valid to convert from one RAS type to another provided
11479 -- that their specification statically match.
11481 Check_Subtype_Conformant
11482 (New_Id =>
11483 Designated_Type (Corresponding_Remote_Type (Target_Type)),
11484 Old_Id =>
11485 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
11486 Err_Loc =>
11488 return True;
11490 -- If it was legal in the generic, it's legal in the instance
11492 elsif In_Instance_Body then
11493 return True;
11495 -- If both are tagged types, check legality of view conversions
11497 elsif Is_Tagged_Type (Target_Type)
11498 and then
11499 Is_Tagged_Type (Opnd_Type)
11500 then
11501 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
11503 -- Types derived from the same root type are convertible
11505 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
11506 return True;
11508 -- In an instance or an inlined body, there may be inconsistent views of
11509 -- the same type, or of types derived from a common root.
11511 elsif (In_Instance or In_Inlined_Body)
11512 and then
11513 Root_Type (Underlying_Type (Target_Type)) =
11514 Root_Type (Underlying_Type (Opnd_Type))
11515 then
11516 return True;
11518 -- Special check for common access type error case
11520 elsif Ekind (Target_Type) = E_Access_Type
11521 and then Is_Access_Type (Opnd_Type)
11522 then
11523 Conversion_Error_N ("target type must be general access type!", N);
11524 Conversion_Error_NE -- CODEFIX
11525 ("add ALL to }!", N, Target_Type);
11526 return False;
11528 else
11529 Conversion_Error_NE
11530 ("invalid conversion, not compatible with }", N, Opnd_Type);
11531 return False;
11532 end if;
11533 end Valid_Conversion;
11535 end Sem_Res;