Reverting merge from trunk
[official-gcc.git] / gcc / ada / sem_res.adb
blob9a76e04adf61f39b2ad3ce1505ed829b87ecf692
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 Rewrite (First (Parameter_Associations (Original_Node (N))),
1587 New_Copy_Tree (Left_Opnd (N)));
1588 Rewrite (Next (First (Parameter_Associations (Original_Node (N)))),
1589 New_Copy_Tree (Right_Opnd (N)));
1590 else
1591 Rewrite (First (Parameter_Associations (Original_Node (N))),
1592 New_Copy_Tree (Right_Opnd (N)));
1593 end if;
1595 Set_Parent (Original_Node (N), Parent (N));
1596 end if;
1597 end Make_Call_Into_Operator;
1599 -------------------
1600 -- Operator_Kind --
1601 -------------------
1603 function Operator_Kind
1604 (Op_Name : Name_Id;
1605 Is_Binary : Boolean) return Node_Kind
1607 Kind : Node_Kind;
1609 begin
1610 -- Use CASE statement or array???
1612 if Is_Binary then
1613 if Op_Name = Name_Op_And then
1614 Kind := N_Op_And;
1615 elsif Op_Name = Name_Op_Or then
1616 Kind := N_Op_Or;
1617 elsif Op_Name = Name_Op_Xor then
1618 Kind := N_Op_Xor;
1619 elsif Op_Name = Name_Op_Eq then
1620 Kind := N_Op_Eq;
1621 elsif Op_Name = Name_Op_Ne then
1622 Kind := N_Op_Ne;
1623 elsif Op_Name = Name_Op_Lt then
1624 Kind := N_Op_Lt;
1625 elsif Op_Name = Name_Op_Le then
1626 Kind := N_Op_Le;
1627 elsif Op_Name = Name_Op_Gt then
1628 Kind := N_Op_Gt;
1629 elsif Op_Name = Name_Op_Ge then
1630 Kind := N_Op_Ge;
1631 elsif Op_Name = Name_Op_Add then
1632 Kind := N_Op_Add;
1633 elsif Op_Name = Name_Op_Subtract then
1634 Kind := N_Op_Subtract;
1635 elsif Op_Name = Name_Op_Concat then
1636 Kind := N_Op_Concat;
1637 elsif Op_Name = Name_Op_Multiply then
1638 Kind := N_Op_Multiply;
1639 elsif Op_Name = Name_Op_Divide then
1640 Kind := N_Op_Divide;
1641 elsif Op_Name = Name_Op_Mod then
1642 Kind := N_Op_Mod;
1643 elsif Op_Name = Name_Op_Rem then
1644 Kind := N_Op_Rem;
1645 elsif Op_Name = Name_Op_Expon then
1646 Kind := N_Op_Expon;
1647 else
1648 raise Program_Error;
1649 end if;
1651 -- Unary operators
1653 else
1654 if Op_Name = Name_Op_Add then
1655 Kind := N_Op_Plus;
1656 elsif Op_Name = Name_Op_Subtract then
1657 Kind := N_Op_Minus;
1658 elsif Op_Name = Name_Op_Abs then
1659 Kind := N_Op_Abs;
1660 elsif Op_Name = Name_Op_Not then
1661 Kind := N_Op_Not;
1662 else
1663 raise Program_Error;
1664 end if;
1665 end if;
1667 return Kind;
1668 end Operator_Kind;
1670 ----------------------------
1671 -- Preanalyze_And_Resolve --
1672 ----------------------------
1674 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1675 Save_Full_Analysis : constant Boolean := Full_Analysis;
1677 begin
1678 Full_Analysis := False;
1679 Expander_Mode_Save_And_Set (False);
1681 -- Normally, we suppress all checks for this preanalysis. There is no
1682 -- point in processing them now, since they will be applied properly
1683 -- and in the proper location when the default expressions reanalyzed
1684 -- and reexpanded later on. We will also have more information at that
1685 -- point for possible suppression of individual checks.
1687 -- However, in SPARK mode, most expansion is suppressed, and this
1688 -- later reanalysis and reexpansion may not occur. SPARK mode does
1689 -- require the setting of checking flags for proof purposes, so we
1690 -- do the SPARK preanalysis without suppressing checks.
1692 -- This special handling for SPARK mode is required for example in the
1693 -- case of Ada 2012 constructs such as quantified expressions, which are
1694 -- expanded in two separate steps.
1696 if SPARK_Mode then
1697 Analyze_And_Resolve (N, T);
1698 else
1699 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1700 end if;
1702 Expander_Mode_Restore;
1703 Full_Analysis := Save_Full_Analysis;
1704 end Preanalyze_And_Resolve;
1706 -- Version without context type
1708 procedure Preanalyze_And_Resolve (N : Node_Id) is
1709 Save_Full_Analysis : constant Boolean := Full_Analysis;
1711 begin
1712 Full_Analysis := False;
1713 Expander_Mode_Save_And_Set (False);
1715 Analyze (N);
1716 Resolve (N, Etype (N), Suppress => All_Checks);
1718 Expander_Mode_Restore;
1719 Full_Analysis := Save_Full_Analysis;
1720 end Preanalyze_And_Resolve;
1722 ----------------------------------
1723 -- Replace_Actual_Discriminants --
1724 ----------------------------------
1726 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1727 Loc : constant Source_Ptr := Sloc (N);
1728 Tsk : Node_Id := Empty;
1730 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1731 -- Comment needed???
1733 -------------------
1734 -- Process_Discr --
1735 -------------------
1737 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1738 Ent : Entity_Id;
1740 begin
1741 if Nkind (Nod) = N_Identifier then
1742 Ent := Entity (Nod);
1744 if Present (Ent)
1745 and then Ekind (Ent) = E_Discriminant
1746 then
1747 Rewrite (Nod,
1748 Make_Selected_Component (Loc,
1749 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1750 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1752 Set_Etype (Nod, Etype (Ent));
1753 end if;
1755 end if;
1757 return OK;
1758 end Process_Discr;
1760 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1762 -- Start of processing for Replace_Actual_Discriminants
1764 begin
1765 if not Full_Expander_Active then
1766 return;
1767 end if;
1769 if Nkind (Name (N)) = N_Selected_Component then
1770 Tsk := Prefix (Name (N));
1772 elsif Nkind (Name (N)) = N_Indexed_Component then
1773 Tsk := Prefix (Prefix (Name (N)));
1774 end if;
1776 if No (Tsk) then
1777 return;
1778 else
1779 Replace_Discrs (Default);
1780 end if;
1781 end Replace_Actual_Discriminants;
1783 -------------
1784 -- Resolve --
1785 -------------
1787 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1788 Ambiguous : Boolean := False;
1789 Ctx_Type : Entity_Id := Typ;
1790 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1791 Err_Type : Entity_Id := Empty;
1792 Found : Boolean := False;
1793 From_Lib : Boolean;
1794 I : Interp_Index;
1795 I1 : Interp_Index := 0; -- prevent junk warning
1796 It : Interp;
1797 It1 : Interp;
1798 Seen : Entity_Id := Empty; -- prevent junk warning
1800 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1801 -- Determine whether a node comes from a predefined library unit or
1802 -- Standard.
1804 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1805 -- Try and fix up a literal so that it matches its expected type. New
1806 -- literals are manufactured if necessary to avoid cascaded errors.
1808 function Proper_Current_Scope return Entity_Id;
1809 -- Return the current scope. Skip loop scopes created for the purpose of
1810 -- quantified expression analysis since those do not appear in the tree.
1812 procedure Report_Ambiguous_Argument;
1813 -- Additional diagnostics when an ambiguous call has an ambiguous
1814 -- argument (typically a controlling actual).
1816 procedure Resolution_Failed;
1817 -- Called when attempt at resolving current expression fails
1819 ------------------------------------
1820 -- Comes_From_Predefined_Lib_Unit --
1821 -------------------------------------
1823 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1824 begin
1825 return
1826 Sloc (Nod) = Standard_Location
1827 or else Is_Predefined_File_Name
1828 (Unit_File_Name (Get_Source_Unit (Sloc (Nod))));
1829 end Comes_From_Predefined_Lib_Unit;
1831 --------------------
1832 -- Patch_Up_Value --
1833 --------------------
1835 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1836 begin
1837 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
1838 Rewrite (N,
1839 Make_Real_Literal (Sloc (N),
1840 Realval => UR_From_Uint (Intval (N))));
1841 Set_Etype (N, Universal_Real);
1842 Set_Is_Static_Expression (N);
1844 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
1845 Rewrite (N,
1846 Make_Integer_Literal (Sloc (N),
1847 Intval => UR_To_Uint (Realval (N))));
1848 Set_Etype (N, Universal_Integer);
1849 Set_Is_Static_Expression (N);
1851 elsif Nkind (N) = N_String_Literal
1852 and then Is_Character_Type (Typ)
1853 then
1854 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1855 Rewrite (N,
1856 Make_Character_Literal (Sloc (N),
1857 Chars => Name_Find,
1858 Char_Literal_Value =>
1859 UI_From_Int (Character'Pos ('A'))));
1860 Set_Etype (N, Any_Character);
1861 Set_Is_Static_Expression (N);
1863 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
1864 Rewrite (N,
1865 Make_String_Literal (Sloc (N),
1866 Strval => End_String));
1868 elsif Nkind (N) = N_Range then
1869 Patch_Up_Value (Low_Bound (N), Typ);
1870 Patch_Up_Value (High_Bound (N), Typ);
1871 end if;
1872 end Patch_Up_Value;
1874 --------------------------
1875 -- Proper_Current_Scope --
1876 --------------------------
1878 function Proper_Current_Scope return Entity_Id is
1879 S : Entity_Id := Current_Scope;
1881 begin
1882 while Present (S) loop
1884 -- Skip a loop scope created for quantified expression analysis
1886 if Ekind (S) = E_Loop
1887 and then Nkind (Parent (S)) = N_Quantified_Expression
1888 then
1889 S := Scope (S);
1890 else
1891 exit;
1892 end if;
1893 end loop;
1895 return S;
1896 end Proper_Current_Scope;
1898 -------------------------------
1899 -- Report_Ambiguous_Argument --
1900 -------------------------------
1902 procedure Report_Ambiguous_Argument is
1903 Arg : constant Node_Id := First (Parameter_Associations (N));
1904 I : Interp_Index;
1905 It : Interp;
1907 begin
1908 if Nkind (Arg) = N_Function_Call
1909 and then Is_Entity_Name (Name (Arg))
1910 and then Is_Overloaded (Name (Arg))
1911 then
1912 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
1914 -- Could use comments on what is going on here???
1916 Get_First_Interp (Name (Arg), I, It);
1917 while Present (It.Nam) loop
1918 Error_Msg_Sloc := Sloc (It.Nam);
1920 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
1921 Error_Msg_N ("interpretation (inherited) #!", Arg);
1922 else
1923 Error_Msg_N ("interpretation #!", Arg);
1924 end if;
1926 Get_Next_Interp (I, It);
1927 end loop;
1928 end if;
1929 end Report_Ambiguous_Argument;
1931 -----------------------
1932 -- Resolution_Failed --
1933 -----------------------
1935 procedure Resolution_Failed is
1936 begin
1937 Patch_Up_Value (N, Typ);
1938 Set_Etype (N, Typ);
1939 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1940 Set_Is_Overloaded (N, False);
1942 -- The caller will return without calling the expander, so we need
1943 -- to set the analyzed flag. Note that it is fine to set Analyzed
1944 -- to True even if we are in the middle of a shallow analysis,
1945 -- (see the spec of sem for more details) since this is an error
1946 -- situation anyway, and there is no point in repeating the
1947 -- analysis later (indeed it won't work to repeat it later, since
1948 -- we haven't got a clear resolution of which entity is being
1949 -- referenced.)
1951 Set_Analyzed (N, True);
1952 return;
1953 end Resolution_Failed;
1955 -- Start of processing for Resolve
1957 begin
1958 if N = Error then
1959 return;
1960 end if;
1962 -- Access attribute on remote subprogram cannot be used for a non-remote
1963 -- access-to-subprogram type.
1965 if Nkind (N) = N_Attribute_Reference
1966 and then Nam_In (Attribute_Name (N), Name_Access,
1967 Name_Unrestricted_Access,
1968 Name_Unchecked_Access)
1969 and then Comes_From_Source (N)
1970 and then Is_Entity_Name (Prefix (N))
1971 and then Is_Subprogram (Entity (Prefix (N)))
1972 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1973 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1974 then
1975 Error_Msg_N
1976 ("prefix must statically denote a non-remote subprogram", N);
1977 end if;
1979 From_Lib := Comes_From_Predefined_Lib_Unit (N);
1981 -- If the context is a Remote_Access_To_Subprogram, access attributes
1982 -- must be resolved with the corresponding fat pointer. There is no need
1983 -- to check for the attribute name since the return type of an
1984 -- attribute is never a remote type.
1986 if Nkind (N) = N_Attribute_Reference
1987 and then Comes_From_Source (N)
1988 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
1989 then
1990 declare
1991 Attr : constant Attribute_Id :=
1992 Get_Attribute_Id (Attribute_Name (N));
1993 Pref : constant Node_Id := Prefix (N);
1994 Decl : Node_Id;
1995 Spec : Node_Id;
1996 Is_Remote : Boolean := True;
1998 begin
1999 -- Check that Typ is a remote access-to-subprogram type
2001 if Is_Remote_Access_To_Subprogram_Type (Typ) then
2003 -- Prefix (N) must statically denote a remote subprogram
2004 -- declared in a package specification.
2006 if Attr = Attribute_Access or else
2007 Attr = Attribute_Unchecked_Access or else
2008 Attr = Attribute_Unrestricted_Access
2009 then
2010 Decl := Unit_Declaration_Node (Entity (Pref));
2012 if Nkind (Decl) = N_Subprogram_Body then
2013 Spec := Corresponding_Spec (Decl);
2015 if not No (Spec) then
2016 Decl := Unit_Declaration_Node (Spec);
2017 end if;
2018 end if;
2020 Spec := Parent (Decl);
2022 if not Is_Entity_Name (Prefix (N))
2023 or else Nkind (Spec) /= N_Package_Specification
2024 or else
2025 not Is_Remote_Call_Interface (Defining_Entity (Spec))
2026 then
2027 Is_Remote := False;
2028 Error_Msg_N
2029 ("prefix must statically denote a remote subprogram ",
2031 end if;
2033 -- If we are generating code in distributed mode, perform
2034 -- semantic checks against corresponding remote entities.
2036 if Full_Expander_Active
2037 and then Get_PCS_Name /= Name_No_DSA
2038 then
2039 Check_Subtype_Conformant
2040 (New_Id => Entity (Prefix (N)),
2041 Old_Id => Designated_Type
2042 (Corresponding_Remote_Type (Typ)),
2043 Err_Loc => N);
2045 if Is_Remote then
2046 Process_Remote_AST_Attribute (N, Typ);
2047 end if;
2048 end if;
2049 end if;
2050 end if;
2051 end;
2052 end if;
2054 Debug_A_Entry ("resolving ", N);
2056 if Debug_Flag_V then
2057 Write_Overloads (N);
2058 end if;
2060 if Comes_From_Source (N) then
2061 if Is_Fixed_Point_Type (Typ) then
2062 Check_Restriction (No_Fixed_Point, N);
2064 elsif Is_Floating_Point_Type (Typ)
2065 and then Typ /= Universal_Real
2066 and then Typ /= Any_Real
2067 then
2068 Check_Restriction (No_Floating_Point, N);
2069 end if;
2070 end if;
2072 -- Return if already analyzed
2074 if Analyzed (N) then
2075 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2076 Analyze_Dimension (N);
2077 return;
2079 -- A Raise_Expression takes its type from context. The Etype was set
2080 -- to Any_Type, reflecting the fact that the expression itself does
2081 -- not specify any possible interpretation. So we set the type to the
2082 -- resolution type here and now. We need to do this before Resolve sees
2083 -- the Any_Type value.
2085 elsif Nkind (N) = N_Raise_Expression then
2086 Set_Etype (N, Typ);
2088 -- Any other case of Any_Type as the Etype value means that we had
2089 -- a previous error.
2091 elsif Etype (N) = Any_Type then
2092 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2093 return;
2094 end if;
2096 Check_Parameterless_Call (N);
2098 -- The resolution of an Expression_With_Actions is determined by
2099 -- its Expression.
2101 if Nkind (N) = N_Expression_With_Actions then
2102 Resolve (Expression (N), Typ);
2104 Found := True;
2105 Expr_Type := Etype (Expression (N));
2107 -- If not overloaded, then we know the type, and all that needs doing
2108 -- is to check that this type is compatible with the context.
2110 elsif not Is_Overloaded (N) then
2111 Found := Covers (Typ, Etype (N));
2112 Expr_Type := Etype (N);
2114 -- In the overloaded case, we must select the interpretation that
2115 -- is compatible with the context (i.e. the type passed to Resolve)
2117 else
2118 -- Loop through possible interpretations
2120 Get_First_Interp (N, I, It);
2121 Interp_Loop : while Present (It.Typ) loop
2123 if Debug_Flag_V then
2124 Write_Str ("Interp: ");
2125 Write_Interp (It);
2126 end if;
2128 -- We are only interested in interpretations that are compatible
2129 -- with the expected type, any other interpretations are ignored.
2131 if not Covers (Typ, It.Typ) then
2132 if Debug_Flag_V then
2133 Write_Str (" interpretation incompatible with context");
2134 Write_Eol;
2135 end if;
2137 else
2138 -- Skip the current interpretation if it is disabled by an
2139 -- abstract operator. This action is performed only when the
2140 -- type against which we are resolving is the same as the
2141 -- type of the interpretation.
2143 if Ada_Version >= Ada_2005
2144 and then It.Typ = Typ
2145 and then Typ /= Universal_Integer
2146 and then Typ /= Universal_Real
2147 and then Present (It.Abstract_Op)
2148 then
2149 if Debug_Flag_V then
2150 Write_Line ("Skip.");
2151 end if;
2153 goto Continue;
2154 end if;
2156 -- First matching interpretation
2158 if not Found then
2159 Found := True;
2160 I1 := I;
2161 Seen := It.Nam;
2162 Expr_Type := It.Typ;
2164 -- Matching interpretation that is not the first, maybe an
2165 -- error, but there are some cases where preference rules are
2166 -- used to choose between the two possibilities. These and
2167 -- some more obscure cases are handled in Disambiguate.
2169 else
2170 -- If the current statement is part of a predefined library
2171 -- unit, then all interpretations which come from user level
2172 -- packages should not be considered.
2174 if From_Lib
2175 and then not Comes_From_Predefined_Lib_Unit (It.Nam)
2176 then
2177 goto Continue;
2178 end if;
2180 Error_Msg_Sloc := Sloc (Seen);
2181 It1 := Disambiguate (N, I1, I, Typ);
2183 -- Disambiguation has succeeded. Skip the remaining
2184 -- interpretations.
2186 if It1 /= No_Interp then
2187 Seen := It1.Nam;
2188 Expr_Type := It1.Typ;
2190 while Present (It.Typ) loop
2191 Get_Next_Interp (I, It);
2192 end loop;
2194 else
2195 -- Before we issue an ambiguity complaint, check for
2196 -- the case of a subprogram call where at least one
2197 -- of the arguments is Any_Type, and if so, suppress
2198 -- the message, since it is a cascaded error.
2200 if Nkind (N) in N_Subprogram_Call then
2201 declare
2202 A : Node_Id;
2203 E : Node_Id;
2205 begin
2206 A := First_Actual (N);
2207 while Present (A) loop
2208 E := A;
2210 if Nkind (E) = N_Parameter_Association then
2211 E := Explicit_Actual_Parameter (E);
2212 end if;
2214 if Etype (E) = Any_Type then
2215 if Debug_Flag_V then
2216 Write_Str ("Any_Type in call");
2217 Write_Eol;
2218 end if;
2220 exit Interp_Loop;
2221 end if;
2223 Next_Actual (A);
2224 end loop;
2225 end;
2227 elsif Nkind (N) in N_Binary_Op
2228 and then (Etype (Left_Opnd (N)) = Any_Type
2229 or else Etype (Right_Opnd (N)) = Any_Type)
2230 then
2231 exit Interp_Loop;
2233 elsif Nkind (N) in N_Unary_Op
2234 and then Etype (Right_Opnd (N)) = Any_Type
2235 then
2236 exit Interp_Loop;
2237 end if;
2239 -- Not that special case, so issue message using the
2240 -- flag Ambiguous to control printing of the header
2241 -- message only at the start of an ambiguous set.
2243 if not Ambiguous then
2244 if Nkind (N) = N_Function_Call
2245 and then Nkind (Name (N)) = N_Explicit_Dereference
2246 then
2247 Error_Msg_N
2248 ("ambiguous expression "
2249 & "(cannot resolve indirect call)!", N);
2250 else
2251 Error_Msg_NE -- CODEFIX
2252 ("ambiguous expression (cannot resolve&)!",
2253 N, It.Nam);
2254 end if;
2256 Ambiguous := True;
2258 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2259 Error_Msg_N
2260 ("\\possible interpretation (inherited)#!", N);
2261 else
2262 Error_Msg_N -- CODEFIX
2263 ("\\possible interpretation#!", N);
2264 end if;
2266 if Nkind (N) in N_Subprogram_Call
2267 and then Present (Parameter_Associations (N))
2268 then
2269 Report_Ambiguous_Argument;
2270 end if;
2271 end if;
2273 Error_Msg_Sloc := Sloc (It.Nam);
2275 -- By default, the error message refers to the candidate
2276 -- interpretation. But if it is a predefined operator, it
2277 -- is implicitly declared at the declaration of the type
2278 -- of the operand. Recover the sloc of that declaration
2279 -- for the error message.
2281 if Nkind (N) in N_Op
2282 and then Scope (It.Nam) = Standard_Standard
2283 and then not Is_Overloaded (Right_Opnd (N))
2284 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2285 Standard_Standard
2286 then
2287 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2289 if Comes_From_Source (Err_Type)
2290 and then Present (Parent (Err_Type))
2291 then
2292 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2293 end if;
2295 elsif Nkind (N) in N_Binary_Op
2296 and then Scope (It.Nam) = Standard_Standard
2297 and then not Is_Overloaded (Left_Opnd (N))
2298 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2299 Standard_Standard
2300 then
2301 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2303 if Comes_From_Source (Err_Type)
2304 and then Present (Parent (Err_Type))
2305 then
2306 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2307 end if;
2309 -- If this is an indirect call, use the subprogram_type
2310 -- in the message, to have a meaningful location. Also
2311 -- indicate if this is an inherited operation, created
2312 -- by a type declaration.
2314 elsif Nkind (N) = N_Function_Call
2315 and then Nkind (Name (N)) = N_Explicit_Dereference
2316 and then Is_Type (It.Nam)
2317 then
2318 Err_Type := It.Nam;
2319 Error_Msg_Sloc :=
2320 Sloc (Associated_Node_For_Itype (Err_Type));
2321 else
2322 Err_Type := Empty;
2323 end if;
2325 if Nkind (N) in N_Op
2326 and then Scope (It.Nam) = Standard_Standard
2327 and then Present (Err_Type)
2328 then
2329 -- Special-case the message for universal_fixed
2330 -- operators, which are not declared with the type
2331 -- of the operand, but appear forever in Standard.
2333 if It.Typ = Universal_Fixed
2334 and then Scope (It.Nam) = Standard_Standard
2335 then
2336 Error_Msg_N
2337 ("\\possible interpretation as universal_fixed "
2338 & "operation (RM 4.5.5 (19))", N);
2339 else
2340 Error_Msg_N
2341 ("\\possible interpretation (predefined)#!", N);
2342 end if;
2344 elsif
2345 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2346 then
2347 Error_Msg_N
2348 ("\\possible interpretation (inherited)#!", N);
2349 else
2350 Error_Msg_N -- CODEFIX
2351 ("\\possible interpretation#!", N);
2352 end if;
2354 end if;
2355 end if;
2357 -- We have a matching interpretation, Expr_Type is the type
2358 -- from this interpretation, and Seen is the entity.
2360 -- For an operator, just set the entity name. The type will be
2361 -- set by the specific operator resolution routine.
2363 if Nkind (N) in N_Op then
2364 Set_Entity (N, Seen);
2365 Generate_Reference (Seen, N);
2367 elsif Nkind (N) = N_Case_Expression then
2368 Set_Etype (N, Expr_Type);
2370 elsif Nkind (N) = N_Character_Literal then
2371 Set_Etype (N, Expr_Type);
2373 elsif Nkind (N) = N_If_Expression then
2374 Set_Etype (N, Expr_Type);
2376 -- AI05-0139-2: Expression is overloaded because type has
2377 -- implicit dereference. If type matches context, no implicit
2378 -- dereference is involved.
2380 elsif Has_Implicit_Dereference (Expr_Type) then
2381 Set_Etype (N, Expr_Type);
2382 Set_Is_Overloaded (N, False);
2383 exit Interp_Loop;
2385 elsif Is_Overloaded (N)
2386 and then Present (It.Nam)
2387 and then Ekind (It.Nam) = E_Discriminant
2388 and then Has_Implicit_Dereference (It.Nam)
2389 then
2390 Build_Explicit_Dereference (N, It.Nam);
2392 -- For an explicit dereference, attribute reference, range,
2393 -- short-circuit form (which is not an operator node), or call
2394 -- with a name that is an explicit dereference, there is
2395 -- nothing to be done at this point.
2397 elsif Nkind_In (N, N_Explicit_Dereference,
2398 N_Attribute_Reference,
2399 N_And_Then,
2400 N_Indexed_Component,
2401 N_Or_Else,
2402 N_Range,
2403 N_Selected_Component,
2404 N_Slice)
2405 or else Nkind (Name (N)) = N_Explicit_Dereference
2406 then
2407 null;
2409 -- For procedure or function calls, set the type of the name,
2410 -- and also the entity pointer for the prefix.
2412 elsif Nkind (N) in N_Subprogram_Call
2413 and then Is_Entity_Name (Name (N))
2414 then
2415 Set_Etype (Name (N), Expr_Type);
2416 Set_Entity (Name (N), Seen);
2417 Generate_Reference (Seen, Name (N));
2419 elsif Nkind (N) = N_Function_Call
2420 and then Nkind (Name (N)) = N_Selected_Component
2421 then
2422 Set_Etype (Name (N), Expr_Type);
2423 Set_Entity (Selector_Name (Name (N)), Seen);
2424 Generate_Reference (Seen, Selector_Name (Name (N)));
2426 -- For all other cases, just set the type of the Name
2428 else
2429 Set_Etype (Name (N), Expr_Type);
2430 end if;
2432 end if;
2434 <<Continue>>
2436 -- Move to next interpretation
2438 exit Interp_Loop when No (It.Typ);
2440 Get_Next_Interp (I, It);
2441 end loop Interp_Loop;
2442 end if;
2444 -- At this stage Found indicates whether or not an acceptable
2445 -- interpretation exists. If not, then we have an error, except that if
2446 -- the context is Any_Type as a result of some other error, then we
2447 -- suppress the error report.
2449 if not Found then
2450 if Typ /= Any_Type then
2452 -- If type we are looking for is Void, then this is the procedure
2453 -- call case, and the error is simply that what we gave is not a
2454 -- procedure name (we think of procedure calls as expressions with
2455 -- types internally, but the user doesn't think of them this way!)
2457 if Typ = Standard_Void_Type then
2459 -- Special case message if function used as a procedure
2461 if Nkind (N) = N_Procedure_Call_Statement
2462 and then Is_Entity_Name (Name (N))
2463 and then Ekind (Entity (Name (N))) = E_Function
2464 then
2465 Error_Msg_NE
2466 ("cannot use function & in a procedure call",
2467 Name (N), Entity (Name (N)));
2469 -- Otherwise give general message (not clear what cases this
2470 -- covers, but no harm in providing for them!)
2472 else
2473 Error_Msg_N ("expect procedure name in procedure call", N);
2474 end if;
2476 Found := True;
2478 -- Otherwise we do have a subexpression with the wrong type
2480 -- Check for the case of an allocator which uses an access type
2481 -- instead of the designated type. This is a common error and we
2482 -- specialize the message, posting an error on the operand of the
2483 -- allocator, complaining that we expected the designated type of
2484 -- the allocator.
2486 elsif Nkind (N) = N_Allocator
2487 and then Ekind (Typ) in Access_Kind
2488 and then Ekind (Etype (N)) in Access_Kind
2489 and then Designated_Type (Etype (N)) = Typ
2490 then
2491 Wrong_Type (Expression (N), Designated_Type (Typ));
2492 Found := True;
2494 -- Check for view mismatch on Null in instances, for which the
2495 -- view-swapping mechanism has no identifier.
2497 elsif (In_Instance or else In_Inlined_Body)
2498 and then (Nkind (N) = N_Null)
2499 and then Is_Private_Type (Typ)
2500 and then Is_Access_Type (Full_View (Typ))
2501 then
2502 Resolve (N, Full_View (Typ));
2503 Set_Etype (N, Typ);
2504 return;
2506 -- Check for an aggregate. Sometimes we can get bogus aggregates
2507 -- from misuse of parentheses, and we are about to complain about
2508 -- the aggregate without even looking inside it.
2510 -- Instead, if we have an aggregate of type Any_Composite, then
2511 -- analyze and resolve the component fields, and then only issue
2512 -- another message if we get no errors doing this (otherwise
2513 -- assume that the errors in the aggregate caused the problem).
2515 elsif Nkind (N) = N_Aggregate
2516 and then Etype (N) = Any_Composite
2517 then
2518 -- Disable expansion in any case. If there is a type mismatch
2519 -- it may be fatal to try to expand the aggregate. The flag
2520 -- would otherwise be set to false when the error is posted.
2522 Expander_Active := False;
2524 declare
2525 procedure Check_Aggr (Aggr : Node_Id);
2526 -- Check one aggregate, and set Found to True if we have a
2527 -- definite error in any of its elements
2529 procedure Check_Elmt (Aelmt : Node_Id);
2530 -- Check one element of aggregate and set Found to True if
2531 -- we definitely have an error in the element.
2533 ----------------
2534 -- Check_Aggr --
2535 ----------------
2537 procedure Check_Aggr (Aggr : Node_Id) is
2538 Elmt : Node_Id;
2540 begin
2541 if Present (Expressions (Aggr)) then
2542 Elmt := First (Expressions (Aggr));
2543 while Present (Elmt) loop
2544 Check_Elmt (Elmt);
2545 Next (Elmt);
2546 end loop;
2547 end if;
2549 if Present (Component_Associations (Aggr)) then
2550 Elmt := First (Component_Associations (Aggr));
2551 while Present (Elmt) loop
2553 -- If this is a default-initialized component, then
2554 -- there is nothing to check. The box will be
2555 -- replaced by the appropriate call during late
2556 -- expansion.
2558 if not Box_Present (Elmt) then
2559 Check_Elmt (Expression (Elmt));
2560 end if;
2562 Next (Elmt);
2563 end loop;
2564 end if;
2565 end Check_Aggr;
2567 ----------------
2568 -- Check_Elmt --
2569 ----------------
2571 procedure Check_Elmt (Aelmt : Node_Id) is
2572 begin
2573 -- If we have a nested aggregate, go inside it (to
2574 -- attempt a naked analyze-resolve of the aggregate can
2575 -- cause undesirable cascaded errors). Do not resolve
2576 -- expression if it needs a type from context, as for
2577 -- integer * fixed expression.
2579 if Nkind (Aelmt) = N_Aggregate then
2580 Check_Aggr (Aelmt);
2582 else
2583 Analyze (Aelmt);
2585 if not Is_Overloaded (Aelmt)
2586 and then Etype (Aelmt) /= Any_Fixed
2587 then
2588 Resolve (Aelmt);
2589 end if;
2591 if Etype (Aelmt) = Any_Type then
2592 Found := True;
2593 end if;
2594 end if;
2595 end Check_Elmt;
2597 begin
2598 Check_Aggr (N);
2599 end;
2600 end if;
2602 -- If an error message was issued already, Found got reset to
2603 -- True, so if it is still False, issue standard Wrong_Type msg.
2605 if not Found then
2606 if Is_Overloaded (N)
2607 and then Nkind (N) = N_Function_Call
2608 then
2609 declare
2610 Subp_Name : Node_Id;
2611 begin
2612 if Is_Entity_Name (Name (N)) then
2613 Subp_Name := Name (N);
2615 elsif Nkind (Name (N)) = N_Selected_Component then
2617 -- Protected operation: retrieve operation name
2619 Subp_Name := Selector_Name (Name (N));
2621 else
2622 raise Program_Error;
2623 end if;
2625 Error_Msg_Node_2 := Typ;
2626 Error_Msg_NE
2627 ("no visible interpretation of& "
2628 & "matches expected type&", N, Subp_Name);
2629 end;
2631 if All_Errors_Mode then
2632 declare
2633 Index : Interp_Index;
2634 It : Interp;
2636 begin
2637 Error_Msg_N ("\\possible interpretations:", N);
2639 Get_First_Interp (Name (N), Index, It);
2640 while Present (It.Nam) loop
2641 Error_Msg_Sloc := Sloc (It.Nam);
2642 Error_Msg_Node_2 := It.Nam;
2643 Error_Msg_NE
2644 ("\\ type& for & declared#", N, It.Typ);
2645 Get_Next_Interp (Index, It);
2646 end loop;
2647 end;
2649 else
2650 Error_Msg_N ("\use -gnatf for details", N);
2651 end if;
2653 else
2654 Wrong_Type (N, Typ);
2655 end if;
2656 end if;
2657 end if;
2659 Resolution_Failed;
2660 return;
2662 -- Test if we have more than one interpretation for the context
2664 elsif Ambiguous then
2665 Resolution_Failed;
2666 return;
2668 -- Only one intepretation
2670 else
2671 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
2672 -- the "+" on T is abstract, and the operands are of universal type,
2673 -- the above code will have (incorrectly) resolved the "+" to the
2674 -- universal one in Standard. Therefore check for this case and give
2675 -- an error. We can't do this earlier, because it would cause legal
2676 -- cases to get errors (when some other type has an abstract "+").
2678 if Ada_Version >= Ada_2005
2679 and then Nkind (N) in N_Op
2680 and then Is_Overloaded (N)
2681 and then Is_Universal_Numeric_Type (Etype (Entity (N)))
2682 then
2683 Get_First_Interp (N, I, It);
2684 while Present (It.Typ) loop
2685 if Present (It.Abstract_Op) and then
2686 Etype (It.Abstract_Op) = Typ
2687 then
2688 Error_Msg_NE
2689 ("cannot call abstract subprogram &!", N, It.Abstract_Op);
2690 return;
2691 end if;
2693 Get_Next_Interp (I, It);
2694 end loop;
2695 end if;
2697 -- Here we have an acceptable interpretation for the context
2699 -- Propagate type information and normalize tree for various
2700 -- predefined operations. If the context only imposes a class of
2701 -- types, rather than a specific type, propagate the actual type
2702 -- downward.
2704 if Typ = Any_Integer or else
2705 Typ = Any_Boolean or else
2706 Typ = Any_Modular or else
2707 Typ = Any_Real or else
2708 Typ = Any_Discrete
2709 then
2710 Ctx_Type := Expr_Type;
2712 -- Any_Fixed is legal in a real context only if a specific fixed-
2713 -- point type is imposed. If Norman Cohen can be confused by this,
2714 -- it deserves a separate message.
2716 if Typ = Any_Real
2717 and then Expr_Type = Any_Fixed
2718 then
2719 Error_Msg_N ("illegal context for mixed mode operation", N);
2720 Set_Etype (N, Universal_Real);
2721 Ctx_Type := Universal_Real;
2722 end if;
2723 end if;
2725 -- A user-defined operator is transformed into a function call at
2726 -- this point, so that further processing knows that operators are
2727 -- really operators (i.e. are predefined operators). User-defined
2728 -- operators that are intrinsic are just renamings of the predefined
2729 -- ones, and need not be turned into calls either, but if they rename
2730 -- a different operator, we must transform the node accordingly.
2731 -- Instantiations of Unchecked_Conversion are intrinsic but are
2732 -- treated as functions, even if given an operator designator.
2734 if Nkind (N) in N_Op
2735 and then Present (Entity (N))
2736 and then Ekind (Entity (N)) /= E_Operator
2737 then
2739 if not Is_Predefined_Op (Entity (N)) then
2740 Rewrite_Operator_As_Call (N, Entity (N));
2742 elsif Present (Alias (Entity (N)))
2743 and then
2744 Nkind (Parent (Parent (Entity (N)))) =
2745 N_Subprogram_Renaming_Declaration
2746 then
2747 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2749 -- If the node is rewritten, it will be fully resolved in
2750 -- Rewrite_Renamed_Operator.
2752 if Analyzed (N) then
2753 return;
2754 end if;
2755 end if;
2756 end if;
2758 case N_Subexpr'(Nkind (N)) is
2760 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2762 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2764 when N_Short_Circuit
2765 => Resolve_Short_Circuit (N, Ctx_Type);
2767 when N_Attribute_Reference
2768 => Resolve_Attribute (N, Ctx_Type);
2770 when N_Case_Expression
2771 => Resolve_Case_Expression (N, Ctx_Type);
2773 when N_Character_Literal
2774 => Resolve_Character_Literal (N, Ctx_Type);
2776 when N_Expanded_Name
2777 => Resolve_Entity_Name (N, Ctx_Type);
2779 when N_Explicit_Dereference
2780 => Resolve_Explicit_Dereference (N, Ctx_Type);
2782 when N_Expression_With_Actions
2783 => Resolve_Expression_With_Actions (N, Ctx_Type);
2785 when N_Extension_Aggregate
2786 => Resolve_Extension_Aggregate (N, Ctx_Type);
2788 when N_Function_Call
2789 => Resolve_Call (N, Ctx_Type);
2791 when N_Identifier
2792 => Resolve_Entity_Name (N, Ctx_Type);
2794 when N_If_Expression
2795 => Resolve_If_Expression (N, Ctx_Type);
2797 when N_Indexed_Component
2798 => Resolve_Indexed_Component (N, Ctx_Type);
2800 when N_Integer_Literal
2801 => Resolve_Integer_Literal (N, Ctx_Type);
2803 when N_Membership_Test
2804 => Resolve_Membership_Op (N, Ctx_Type);
2806 when N_Null => Resolve_Null (N, Ctx_Type);
2808 when N_Op_And | N_Op_Or | N_Op_Xor
2809 => Resolve_Logical_Op (N, Ctx_Type);
2811 when N_Op_Eq | N_Op_Ne
2812 => Resolve_Equality_Op (N, Ctx_Type);
2814 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2815 => Resolve_Comparison_Op (N, Ctx_Type);
2817 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2819 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2820 N_Op_Divide | N_Op_Mod | N_Op_Rem
2822 => Resolve_Arithmetic_Op (N, Ctx_Type);
2824 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2826 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2828 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2829 => Resolve_Unary_Op (N, Ctx_Type);
2831 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2833 when N_Procedure_Call_Statement
2834 => Resolve_Call (N, Ctx_Type);
2836 when N_Operator_Symbol
2837 => Resolve_Operator_Symbol (N, Ctx_Type);
2839 when N_Qualified_Expression
2840 => Resolve_Qualified_Expression (N, Ctx_Type);
2842 -- Why is the following null, needs a comment ???
2844 when N_Quantified_Expression
2845 => null;
2847 -- Nothing to do for Raise_Expression, since we took care of
2848 -- setting the Etype earlier, and no other processing is needed.
2850 when N_Raise_Expression
2851 => null;
2853 when N_Raise_xxx_Error
2854 => Set_Etype (N, Ctx_Type);
2856 when N_Range => Resolve_Range (N, Ctx_Type);
2858 when N_Real_Literal
2859 => Resolve_Real_Literal (N, Ctx_Type);
2861 when N_Reference => Resolve_Reference (N, Ctx_Type);
2863 when N_Selected_Component
2864 => Resolve_Selected_Component (N, Ctx_Type);
2866 when N_Slice => Resolve_Slice (N, Ctx_Type);
2868 when N_String_Literal
2869 => Resolve_String_Literal (N, Ctx_Type);
2871 when N_Subprogram_Info
2872 => Resolve_Subprogram_Info (N, Ctx_Type);
2874 when N_Type_Conversion
2875 => Resolve_Type_Conversion (N, Ctx_Type);
2877 when N_Unchecked_Expression =>
2878 Resolve_Unchecked_Expression (N, Ctx_Type);
2880 when N_Unchecked_Type_Conversion =>
2881 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
2882 end case;
2884 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
2885 -- expression of an anonymous access type that occurs in the context
2886 -- of a named general access type, except when the expression is that
2887 -- of a membership test. This ensures proper legality checking in
2888 -- terms of allowed conversions (expressions that would be illegal to
2889 -- convert implicitly are allowed in membership tests).
2891 if Ada_Version >= Ada_2012
2892 and then Ekind (Ctx_Type) = E_General_Access_Type
2893 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
2894 and then Nkind (Parent (N)) not in N_Membership_Test
2895 then
2896 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
2897 Analyze_And_Resolve (N, Ctx_Type);
2898 end if;
2900 -- If the subexpression was replaced by a non-subexpression, then
2901 -- all we do is to expand it. The only legitimate case we know of
2902 -- is converting procedure call statement to entry call statements,
2903 -- but there may be others, so we are making this test general.
2905 if Nkind (N) not in N_Subexpr then
2906 Debug_A_Exit ("resolving ", N, " (done)");
2907 Expand (N);
2908 return;
2909 end if;
2911 -- The expression is definitely NOT overloaded at this point, so
2912 -- we reset the Is_Overloaded flag to avoid any confusion when
2913 -- reanalyzing the node.
2915 Set_Is_Overloaded (N, False);
2917 -- Freeze expression type, entity if it is a name, and designated
2918 -- type if it is an allocator (RM 13.14(10,11,13)).
2920 -- Now that the resolution of the type of the node is complete, and
2921 -- we did not detect an error, we can expand this node. We skip the
2922 -- expand call if we are in a default expression, see section
2923 -- "Handling of Default Expressions" in Sem spec.
2925 Debug_A_Exit ("resolving ", N, " (done)");
2927 -- We unconditionally freeze the expression, even if we are in
2928 -- default expression mode (the Freeze_Expression routine tests this
2929 -- flag and only freezes static types if it is set).
2931 -- Ada 2012 (AI05-177): Expression functions do not freeze. Only
2932 -- their use (in an expanded call) freezes.
2934 if Ekind (Proper_Current_Scope) /= E_Function
2935 or else Nkind (Original_Node (Unit_Declaration_Node
2936 (Proper_Current_Scope))) /= N_Expression_Function
2937 then
2938 Freeze_Expression (N);
2939 end if;
2941 -- Now we can do the expansion
2943 Expand (N);
2944 end if;
2945 end Resolve;
2947 -------------
2948 -- Resolve --
2949 -------------
2951 -- Version with check(s) suppressed
2953 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2954 begin
2955 if Suppress = All_Checks then
2956 declare
2957 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
2958 begin
2959 Scope_Suppress.Suppress := (others => True);
2960 Resolve (N, Typ);
2961 Scope_Suppress.Suppress := Sva;
2962 end;
2964 else
2965 declare
2966 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
2967 begin
2968 Scope_Suppress.Suppress (Suppress) := True;
2969 Resolve (N, Typ);
2970 Scope_Suppress.Suppress (Suppress) := Svg;
2971 end;
2972 end if;
2973 end Resolve;
2975 -------------
2976 -- Resolve --
2977 -------------
2979 -- Version with implicit type
2981 procedure Resolve (N : Node_Id) is
2982 begin
2983 Resolve (N, Etype (N));
2984 end Resolve;
2986 ---------------------
2987 -- Resolve_Actuals --
2988 ---------------------
2990 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2991 Loc : constant Source_Ptr := Sloc (N);
2992 A : Node_Id;
2993 F : Entity_Id;
2994 A_Typ : Entity_Id;
2995 F_Typ : Entity_Id;
2996 Prev : Node_Id := Empty;
2997 Orig_A : Node_Id;
2999 procedure Check_Argument_Order;
3000 -- Performs a check for the case where the actuals are all simple
3001 -- identifiers that correspond to the formal names, but in the wrong
3002 -- order, which is considered suspicious and cause for a warning.
3004 procedure Check_Prefixed_Call;
3005 -- If the original node is an overloaded call in prefix notation,
3006 -- insert an 'Access or a dereference as needed over the first actual.
3007 -- Try_Object_Operation has already verified that there is a valid
3008 -- interpretation, but the form of the actual can only be determined
3009 -- once the primitive operation is identified.
3011 procedure Insert_Default;
3012 -- If the actual is missing in a call, insert in the actuals list
3013 -- an instance of the default expression. The insertion is always
3014 -- a named association.
3016 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
3017 -- Check whether T1 and T2, or their full views, are derived from a
3018 -- common type. Used to enforce the restrictions on array conversions
3019 -- of AI95-00246.
3021 function Static_Concatenation (N : Node_Id) return Boolean;
3022 -- Predicate to determine whether an actual that is a concatenation
3023 -- will be evaluated statically and does not need a transient scope.
3024 -- This must be determined before the actual is resolved and expanded
3025 -- because if needed the transient scope must be introduced earlier.
3027 --------------------------
3028 -- Check_Argument_Order --
3029 --------------------------
3031 procedure Check_Argument_Order is
3032 begin
3033 -- Nothing to do if no parameters, or original node is neither a
3034 -- function call nor a procedure call statement (happens in the
3035 -- operator-transformed-to-function call case), or the call does
3036 -- not come from source, or this warning is off.
3038 if not Warn_On_Parameter_Order
3039 or else No (Parameter_Associations (N))
3040 or else Nkind (Original_Node (N)) not in N_Subprogram_Call
3041 or else not Comes_From_Source (N)
3042 then
3043 return;
3044 end if;
3046 declare
3047 Nargs : constant Nat := List_Length (Parameter_Associations (N));
3049 begin
3050 -- Nothing to do if only one parameter
3052 if Nargs < 2 then
3053 return;
3054 end if;
3056 -- Here if at least two arguments
3058 declare
3059 Actuals : array (1 .. Nargs) of Node_Id;
3060 Actual : Node_Id;
3061 Formal : Node_Id;
3063 Wrong_Order : Boolean := False;
3064 -- Set True if an out of order case is found
3066 begin
3067 -- Collect identifier names of actuals, fail if any actual is
3068 -- not a simple identifier, and record max length of name.
3070 Actual := First (Parameter_Associations (N));
3071 for J in Actuals'Range loop
3072 if Nkind (Actual) /= N_Identifier then
3073 return;
3074 else
3075 Actuals (J) := Actual;
3076 Next (Actual);
3077 end if;
3078 end loop;
3080 -- If we got this far, all actuals are identifiers and the list
3081 -- of their names is stored in the Actuals array.
3083 Formal := First_Formal (Nam);
3084 for J in Actuals'Range loop
3086 -- If we ran out of formals, that's odd, probably an error
3087 -- which will be detected elsewhere, but abandon the search.
3089 if No (Formal) then
3090 return;
3091 end if;
3093 -- If name matches and is in order OK
3095 if Chars (Formal) = Chars (Actuals (J)) then
3096 null;
3098 else
3099 -- If no match, see if it is elsewhere in list and if so
3100 -- flag potential wrong order if type is compatible.
3102 for K in Actuals'Range loop
3103 if Chars (Formal) = Chars (Actuals (K))
3104 and then
3105 Has_Compatible_Type (Actuals (K), Etype (Formal))
3106 then
3107 Wrong_Order := True;
3108 goto Continue;
3109 end if;
3110 end loop;
3112 -- No match
3114 return;
3115 end if;
3117 <<Continue>> Next_Formal (Formal);
3118 end loop;
3120 -- If Formals left over, also probably an error, skip warning
3122 if Present (Formal) then
3123 return;
3124 end if;
3126 -- Here we give the warning if something was out of order
3128 if Wrong_Order then
3129 Error_Msg_N
3130 ("?P?actuals for this call may be in wrong order", N);
3131 end if;
3132 end;
3133 end;
3134 end Check_Argument_Order;
3136 -------------------------
3137 -- Check_Prefixed_Call --
3138 -------------------------
3140 procedure Check_Prefixed_Call is
3141 Act : constant Node_Id := First_Actual (N);
3142 A_Type : constant Entity_Id := Etype (Act);
3143 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3144 Orig : constant Node_Id := Original_Node (N);
3145 New_A : Node_Id;
3147 begin
3148 -- Check whether the call is a prefixed call, with or without
3149 -- additional actuals.
3151 if Nkind (Orig) = N_Selected_Component
3152 or else
3153 (Nkind (Orig) = N_Indexed_Component
3154 and then Nkind (Prefix (Orig)) = N_Selected_Component
3155 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3156 and then Is_Entity_Name (Act)
3157 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3158 then
3159 if Is_Access_Type (A_Type)
3160 and then not Is_Access_Type (F_Type)
3161 then
3162 -- Introduce dereference on object in prefix
3164 New_A :=
3165 Make_Explicit_Dereference (Sloc (Act),
3166 Prefix => Relocate_Node (Act));
3167 Rewrite (Act, New_A);
3168 Analyze (Act);
3170 elsif Is_Access_Type (F_Type)
3171 and then not Is_Access_Type (A_Type)
3172 then
3173 -- Introduce an implicit 'Access in prefix
3175 if not Is_Aliased_View (Act) then
3176 Error_Msg_NE
3177 ("object in prefixed call to& must be aliased"
3178 & " (RM-2005 4.3.1 (13))",
3179 Prefix (Act), Nam);
3180 end if;
3182 Rewrite (Act,
3183 Make_Attribute_Reference (Loc,
3184 Attribute_Name => Name_Access,
3185 Prefix => Relocate_Node (Act)));
3186 end if;
3188 Analyze (Act);
3189 end if;
3190 end Check_Prefixed_Call;
3192 --------------------
3193 -- Insert_Default --
3194 --------------------
3196 procedure Insert_Default is
3197 Actval : Node_Id;
3198 Assoc : Node_Id;
3200 begin
3201 -- Missing argument in call, nothing to insert
3203 if No (Default_Value (F)) then
3204 return;
3206 else
3207 -- Note that we do a full New_Copy_Tree, so that any associated
3208 -- Itypes are properly copied. This may not be needed any more,
3209 -- but it does no harm as a safety measure! Defaults of a generic
3210 -- formal may be out of bounds of the corresponding actual (see
3211 -- cc1311b) and an additional check may be required.
3213 Actval :=
3214 New_Copy_Tree
3215 (Default_Value (F),
3216 New_Scope => Current_Scope,
3217 New_Sloc => Loc);
3219 if Is_Concurrent_Type (Scope (Nam))
3220 and then Has_Discriminants (Scope (Nam))
3221 then
3222 Replace_Actual_Discriminants (N, Actval);
3223 end if;
3225 if Is_Overloadable (Nam)
3226 and then Present (Alias (Nam))
3227 then
3228 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3229 and then not Is_Tagged_Type (Etype (F))
3230 then
3231 -- If default is a real literal, do not introduce a
3232 -- conversion whose effect may depend on the run-time
3233 -- size of universal real.
3235 if Nkind (Actval) = N_Real_Literal then
3236 Set_Etype (Actval, Base_Type (Etype (F)));
3237 else
3238 Actval := Unchecked_Convert_To (Etype (F), Actval);
3239 end if;
3240 end if;
3242 if Is_Scalar_Type (Etype (F)) then
3243 Enable_Range_Check (Actval);
3244 end if;
3246 Set_Parent (Actval, N);
3248 -- Resolve aggregates with their base type, to avoid scope
3249 -- anomalies: the subtype was first built in the subprogram
3250 -- declaration, and the current call may be nested.
3252 if Nkind (Actval) = N_Aggregate then
3253 Analyze_And_Resolve (Actval, Etype (F));
3254 else
3255 Analyze_And_Resolve (Actval, Etype (Actval));
3256 end if;
3258 else
3259 Set_Parent (Actval, N);
3261 -- See note above concerning aggregates
3263 if Nkind (Actval) = N_Aggregate
3264 and then Has_Discriminants (Etype (Actval))
3265 then
3266 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3268 -- Resolve entities with their own type, which may differ from
3269 -- the type of a reference in a generic context (the view
3270 -- swapping mechanism did not anticipate the re-analysis of
3271 -- default values in calls).
3273 elsif Is_Entity_Name (Actval) then
3274 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3276 else
3277 Analyze_And_Resolve (Actval, Etype (Actval));
3278 end if;
3279 end if;
3281 -- If default is a tag indeterminate function call, propagate tag
3282 -- to obtain proper dispatching.
3284 if Is_Controlling_Formal (F)
3285 and then Nkind (Default_Value (F)) = N_Function_Call
3286 then
3287 Set_Is_Controlling_Actual (Actval);
3288 end if;
3290 end if;
3292 -- If the default expression raises constraint error, then just
3293 -- silently replace it with an N_Raise_Constraint_Error node, since
3294 -- we already gave the warning on the subprogram spec. If node is
3295 -- already a Raise_Constraint_Error leave as is, to prevent loops in
3296 -- the warnings removal machinery.
3298 if Raises_Constraint_Error (Actval)
3299 and then Nkind (Actval) /= N_Raise_Constraint_Error
3300 then
3301 Rewrite (Actval,
3302 Make_Raise_Constraint_Error (Loc,
3303 Reason => CE_Range_Check_Failed));
3304 Set_Raises_Constraint_Error (Actval);
3305 Set_Etype (Actval, Etype (F));
3306 end if;
3308 Assoc :=
3309 Make_Parameter_Association (Loc,
3310 Explicit_Actual_Parameter => Actval,
3311 Selector_Name => Make_Identifier (Loc, Chars (F)));
3313 -- Case of insertion is first named actual
3315 if No (Prev) or else
3316 Nkind (Parent (Prev)) /= N_Parameter_Association
3317 then
3318 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3319 Set_First_Named_Actual (N, Actval);
3321 if No (Prev) then
3322 if No (Parameter_Associations (N)) then
3323 Set_Parameter_Associations (N, New_List (Assoc));
3324 else
3325 Append (Assoc, Parameter_Associations (N));
3326 end if;
3328 else
3329 Insert_After (Prev, Assoc);
3330 end if;
3332 -- Case of insertion is not first named actual
3334 else
3335 Set_Next_Named_Actual
3336 (Assoc, Next_Named_Actual (Parent (Prev)));
3337 Set_Next_Named_Actual (Parent (Prev), Actval);
3338 Append (Assoc, Parameter_Associations (N));
3339 end if;
3341 Mark_Rewrite_Insertion (Assoc);
3342 Mark_Rewrite_Insertion (Actval);
3344 Prev := Actval;
3345 end Insert_Default;
3347 -------------------
3348 -- Same_Ancestor --
3349 -------------------
3351 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3352 FT1 : Entity_Id := T1;
3353 FT2 : Entity_Id := T2;
3355 begin
3356 if Is_Private_Type (T1)
3357 and then Present (Full_View (T1))
3358 then
3359 FT1 := Full_View (T1);
3360 end if;
3362 if Is_Private_Type (T2)
3363 and then Present (Full_View (T2))
3364 then
3365 FT2 := Full_View (T2);
3366 end if;
3368 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3369 end Same_Ancestor;
3371 --------------------------
3372 -- Static_Concatenation --
3373 --------------------------
3375 function Static_Concatenation (N : Node_Id) return Boolean is
3376 begin
3377 case Nkind (N) is
3378 when N_String_Literal =>
3379 return True;
3381 when N_Op_Concat =>
3383 -- Concatenation is static when both operands are static and
3384 -- the concatenation operator is a predefined one.
3386 return Scope (Entity (N)) = Standard_Standard
3387 and then
3388 Static_Concatenation (Left_Opnd (N))
3389 and then
3390 Static_Concatenation (Right_Opnd (N));
3392 when others =>
3393 if Is_Entity_Name (N) then
3394 declare
3395 Ent : constant Entity_Id := Entity (N);
3396 begin
3397 return Ekind (Ent) = E_Constant
3398 and then Present (Constant_Value (Ent))
3399 and then
3400 Is_Static_Expression (Constant_Value (Ent));
3401 end;
3403 else
3404 return False;
3405 end if;
3406 end case;
3407 end Static_Concatenation;
3409 -- Start of processing for Resolve_Actuals
3411 begin
3412 Check_Argument_Order;
3413 Check_Function_Writable_Actuals (N);
3415 if Present (First_Actual (N)) then
3416 Check_Prefixed_Call;
3417 end if;
3419 A := First_Actual (N);
3420 F := First_Formal (Nam);
3421 while Present (F) loop
3422 if No (A) and then Needs_No_Actuals (Nam) then
3423 null;
3425 -- If we have an error in any actual or formal, indicated by a type
3426 -- of Any_Type, then abandon resolution attempt, and set result type
3427 -- to Any_Type.
3429 elsif (Present (A) and then Etype (A) = Any_Type)
3430 or else Etype (F) = Any_Type
3431 then
3432 Set_Etype (N, Any_Type);
3433 return;
3434 end if;
3436 -- Case where actual is present
3438 -- If the actual is an entity, generate a reference to it now. We
3439 -- do this before the actual is resolved, because a formal of some
3440 -- protected subprogram, or a task discriminant, will be rewritten
3441 -- during expansion, and the source entity reference may be lost.
3443 if Present (A)
3444 and then Is_Entity_Name (A)
3445 and then Comes_From_Source (N)
3446 then
3447 Orig_A := Entity (A);
3449 if Present (Orig_A) then
3450 if Is_Formal (Orig_A)
3451 and then Ekind (F) /= E_In_Parameter
3452 then
3453 Generate_Reference (Orig_A, A, 'm');
3455 elsif not Is_Overloaded (A) then
3456 if Ekind (F) /= E_Out_Parameter then
3457 Generate_Reference (Orig_A, A);
3459 -- RM 6.4.1(12): For an out parameter that is passed by
3460 -- copy, the formal parameter object is created, and:
3462 -- * For an access type, the formal parameter is initialized
3463 -- from the value of the actual, without checking that the
3464 -- value satisfies any constraint, any predicate, or any
3465 -- exclusion of the null value.
3467 -- * For a scalar type that has the Default_Value aspect
3468 -- specified, the formal parameter is initialized from the
3469 -- value of the actual, without checking that the value
3470 -- satisfies any constraint or any predicate.
3471 -- I do not understand why this case is included??? this is
3472 -- not a case where an OUT parameter is treated as IN OUT.
3474 -- * For a composite type with discriminants or that has
3475 -- implicit initial values for any subcomponents, the
3476 -- behavior is as for an in out parameter passed by copy.
3478 -- Hence for these cases we generate the read reference now
3479 -- (the write reference will be generated later by
3480 -- Note_Possible_Modification).
3482 elsif Is_By_Copy_Type (Etype (F))
3483 and then
3484 (Is_Access_Type (Etype (F))
3485 or else
3486 (Is_Scalar_Type (Etype (F))
3487 and then
3488 Present (Default_Aspect_Value (Etype (F))))
3489 or else
3490 (Is_Composite_Type (Etype (F))
3491 and then (Has_Discriminants (Etype (F))
3492 or else Is_Partially_Initialized_Type
3493 (Etype (F)))))
3494 then
3495 Generate_Reference (Orig_A, A);
3496 end if;
3497 end if;
3498 end if;
3499 end if;
3501 if Present (A)
3502 and then (Nkind (Parent (A)) /= N_Parameter_Association
3503 or else Chars (Selector_Name (Parent (A))) = Chars (F))
3504 then
3505 -- If style checking mode on, check match of formal name
3507 if Style_Check then
3508 if Nkind (Parent (A)) = N_Parameter_Association then
3509 Check_Identifier (Selector_Name (Parent (A)), F);
3510 end if;
3511 end if;
3513 -- If the formal is Out or In_Out, do not resolve and expand the
3514 -- conversion, because it is subsequently expanded into explicit
3515 -- temporaries and assignments. However, the object of the
3516 -- conversion can be resolved. An exception is the case of tagged
3517 -- type conversion with a class-wide actual. In that case we want
3518 -- the tag check to occur and no temporary will be needed (no
3519 -- representation change can occur) and the parameter is passed by
3520 -- reference, so we go ahead and resolve the type conversion.
3521 -- Another exception is the case of reference to component or
3522 -- subcomponent of a bit-packed array, in which case we want to
3523 -- defer expansion to the point the in and out assignments are
3524 -- performed.
3526 if Ekind (F) /= E_In_Parameter
3527 and then Nkind (A) = N_Type_Conversion
3528 and then not Is_Class_Wide_Type (Etype (Expression (A)))
3529 then
3530 if Ekind (F) = E_In_Out_Parameter
3531 and then Is_Array_Type (Etype (F))
3532 then
3533 -- In a view conversion, the conversion must be legal in
3534 -- both directions, and thus both component types must be
3535 -- aliased, or neither (4.6 (8)).
3537 -- The extra rule in 4.6 (24.9.2) seems unduly restrictive:
3538 -- the privacy requirement should not apply to generic
3539 -- types, and should be checked in an instance. ARG query
3540 -- is in order ???
3542 if Has_Aliased_Components (Etype (Expression (A))) /=
3543 Has_Aliased_Components (Etype (F))
3544 then
3545 Error_Msg_N
3546 ("both component types in a view conversion must be"
3547 & " aliased, or neither", A);
3549 -- Comment here??? what set of cases???
3551 elsif
3552 not Same_Ancestor (Etype (F), Etype (Expression (A)))
3553 then
3554 -- Check view conv between unrelated by ref array types
3556 if Is_By_Reference_Type (Etype (F))
3557 or else Is_By_Reference_Type (Etype (Expression (A)))
3558 then
3559 Error_Msg_N
3560 ("view conversion between unrelated by reference "
3561 & "array types not allowed (\'A'I-00246)", A);
3563 -- In Ada 2005 mode, check view conversion component
3564 -- type cannot be private, tagged, or volatile. Note
3565 -- that we only apply this to source conversions. The
3566 -- generated code can contain conversions which are
3567 -- not subject to this test, and we cannot extract the
3568 -- component type in such cases since it is not present.
3570 elsif Comes_From_Source (A)
3571 and then Ada_Version >= Ada_2005
3572 then
3573 declare
3574 Comp_Type : constant Entity_Id :=
3575 Component_Type
3576 (Etype (Expression (A)));
3577 begin
3578 if (Is_Private_Type (Comp_Type)
3579 and then not Is_Generic_Type (Comp_Type))
3580 or else Is_Tagged_Type (Comp_Type)
3581 or else Is_Volatile (Comp_Type)
3582 then
3583 Error_Msg_N
3584 ("component type of a view conversion cannot"
3585 & " be private, tagged, or volatile"
3586 & " (RM 4.6 (24))",
3587 Expression (A));
3588 end if;
3589 end;
3590 end if;
3591 end if;
3592 end if;
3594 -- Resolve expression if conversion is all OK
3596 if (Conversion_OK (A)
3597 or else Valid_Conversion (A, Etype (A), Expression (A)))
3598 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
3599 then
3600 Resolve (Expression (A));
3601 end if;
3603 -- If the actual is a function call that returns a limited
3604 -- unconstrained object that needs finalization, create a
3605 -- transient scope for it, so that it can receive the proper
3606 -- finalization list.
3608 elsif Nkind (A) = N_Function_Call
3609 and then Is_Limited_Record (Etype (F))
3610 and then not Is_Constrained (Etype (F))
3611 and then Full_Expander_Active
3612 and then (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3613 then
3614 Establish_Transient_Scope (A, Sec_Stack => False);
3615 Resolve (A, Etype (F));
3617 -- A small optimization: if one of the actuals is a concatenation
3618 -- create a block around a procedure call to recover stack space.
3619 -- This alleviates stack usage when several procedure calls in
3620 -- the same statement list use concatenation. We do not perform
3621 -- this wrapping for code statements, where the argument is a
3622 -- static string, and we want to preserve warnings involving
3623 -- sequences of such statements.
3625 elsif Nkind (A) = N_Op_Concat
3626 and then Nkind (N) = N_Procedure_Call_Statement
3627 and then Full_Expander_Active
3628 and then
3629 not (Is_Intrinsic_Subprogram (Nam)
3630 and then Chars (Nam) = Name_Asm)
3631 and then not Static_Concatenation (A)
3632 then
3633 Establish_Transient_Scope (A, Sec_Stack => False);
3634 Resolve (A, Etype (F));
3636 else
3637 if Nkind (A) = N_Type_Conversion
3638 and then Is_Array_Type (Etype (F))
3639 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3640 and then
3641 (Is_Limited_Type (Etype (F))
3642 or else Is_Limited_Type (Etype (Expression (A))))
3643 then
3644 Error_Msg_N
3645 ("conversion between unrelated limited array types "
3646 & "not allowed ('A'I-00246)", A);
3648 if Is_Limited_Type (Etype (F)) then
3649 Explain_Limited_Type (Etype (F), A);
3650 end if;
3652 if Is_Limited_Type (Etype (Expression (A))) then
3653 Explain_Limited_Type (Etype (Expression (A)), A);
3654 end if;
3655 end if;
3657 -- (Ada 2005: AI-251): If the actual is an allocator whose
3658 -- directly designated type is a class-wide interface, we build
3659 -- an anonymous access type to use it as the type of the
3660 -- allocator. Later, when the subprogram call is expanded, if
3661 -- the interface has a secondary dispatch table the expander
3662 -- will add a type conversion to force the correct displacement
3663 -- of the pointer.
3665 if Nkind (A) = N_Allocator then
3666 declare
3667 DDT : constant Entity_Id :=
3668 Directly_Designated_Type (Base_Type (Etype (F)));
3670 New_Itype : Entity_Id;
3672 begin
3673 if Is_Class_Wide_Type (DDT)
3674 and then Is_Interface (DDT)
3675 then
3676 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
3677 Set_Etype (New_Itype, Etype (A));
3678 Set_Directly_Designated_Type
3679 (New_Itype, Directly_Designated_Type (Etype (A)));
3680 Set_Etype (A, New_Itype);
3681 end if;
3683 -- Ada 2005, AI-162:If the actual is an allocator, the
3684 -- innermost enclosing statement is the master of the
3685 -- created object. This needs to be done with expansion
3686 -- enabled only, otherwise the transient scope will not
3687 -- be removed in the expansion of the wrapped construct.
3689 if (Is_Controlled (DDT) or else Has_Task (DDT))
3690 and then Full_Expander_Active
3691 then
3692 Establish_Transient_Scope (A, Sec_Stack => False);
3693 end if;
3694 end;
3696 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3697 Check_Restriction (No_Access_Parameter_Allocators, A);
3698 end if;
3699 end if;
3701 -- (Ada 2005): The call may be to a primitive operation of a
3702 -- tagged synchronized type, declared outside of the type. In
3703 -- this case the controlling actual must be converted to its
3704 -- corresponding record type, which is the formal type. The
3705 -- actual may be a subtype, either because of a constraint or
3706 -- because it is a generic actual, so use base type to locate
3707 -- concurrent type.
3709 F_Typ := Base_Type (Etype (F));
3711 if Is_Tagged_Type (F_Typ)
3712 and then (Is_Concurrent_Type (F_Typ)
3713 or else Is_Concurrent_Record_Type (F_Typ))
3714 then
3715 -- If the actual is overloaded, look for an interpretation
3716 -- that has a synchronized type.
3718 if not Is_Overloaded (A) then
3719 A_Typ := Base_Type (Etype (A));
3721 else
3722 declare
3723 Index : Interp_Index;
3724 It : Interp;
3726 begin
3727 Get_First_Interp (A, Index, It);
3728 while Present (It.Typ) loop
3729 if Is_Concurrent_Type (It.Typ)
3730 or else Is_Concurrent_Record_Type (It.Typ)
3731 then
3732 A_Typ := Base_Type (It.Typ);
3733 exit;
3734 end if;
3736 Get_Next_Interp (Index, It);
3737 end loop;
3738 end;
3739 end if;
3741 declare
3742 Full_A_Typ : Entity_Id;
3744 begin
3745 if Present (Full_View (A_Typ)) then
3746 Full_A_Typ := Base_Type (Full_View (A_Typ));
3747 else
3748 Full_A_Typ := A_Typ;
3749 end if;
3751 -- Tagged synchronized type (case 1): the actual is a
3752 -- concurrent type.
3754 if Is_Concurrent_Type (A_Typ)
3755 and then Corresponding_Record_Type (A_Typ) = F_Typ
3756 then
3757 Rewrite (A,
3758 Unchecked_Convert_To
3759 (Corresponding_Record_Type (A_Typ), A));
3760 Resolve (A, Etype (F));
3762 -- Tagged synchronized type (case 2): the formal is a
3763 -- concurrent type.
3765 elsif Ekind (Full_A_Typ) = E_Record_Type
3766 and then Present
3767 (Corresponding_Concurrent_Type (Full_A_Typ))
3768 and then Is_Concurrent_Type (F_Typ)
3769 and then Present (Corresponding_Record_Type (F_Typ))
3770 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
3771 then
3772 Resolve (A, Corresponding_Record_Type (F_Typ));
3774 -- Common case
3776 else
3777 Resolve (A, Etype (F));
3778 end if;
3779 end;
3781 -- Not a synchronized operation
3783 else
3784 Resolve (A, Etype (F));
3785 end if;
3786 end if;
3788 A_Typ := Etype (A);
3789 F_Typ := Etype (F);
3791 if Comes_From_Source (Original_Node (N))
3792 and then Nkind_In (Original_Node (N), N_Function_Call,
3793 N_Procedure_Call_Statement)
3794 then
3795 -- In formal mode, check that actual parameters matching
3796 -- formals of tagged types are objects (or ancestor type
3797 -- conversions of objects), not general expressions.
3799 if Is_Actual_Tagged_Parameter (A) then
3800 if Is_SPARK_Object_Reference (A) then
3801 null;
3803 elsif Nkind (A) = N_Type_Conversion then
3804 declare
3805 Operand : constant Node_Id := Expression (A);
3806 Operand_Typ : constant Entity_Id := Etype (Operand);
3807 Target_Typ : constant Entity_Id := A_Typ;
3809 begin
3810 if not Is_SPARK_Object_Reference (Operand) then
3811 Check_SPARK_Restriction
3812 ("object required", Operand);
3814 -- In formal mode, the only view conversions are those
3815 -- involving ancestor conversion of an extended type.
3817 elsif not
3818 (Is_Tagged_Type (Target_Typ)
3819 and then not Is_Class_Wide_Type (Target_Typ)
3820 and then Is_Tagged_Type (Operand_Typ)
3821 and then not Is_Class_Wide_Type (Operand_Typ)
3822 and then Is_Ancestor (Target_Typ, Operand_Typ))
3823 then
3824 if Ekind_In
3825 (F, E_Out_Parameter, E_In_Out_Parameter)
3826 then
3827 Check_SPARK_Restriction
3828 ("ancestor conversion is the only permitted "
3829 & "view conversion", A);
3830 else
3831 Check_SPARK_Restriction
3832 ("ancestor conversion required", A);
3833 end if;
3835 else
3836 null;
3837 end if;
3838 end;
3840 else
3841 Check_SPARK_Restriction ("object required", A);
3842 end if;
3844 -- In formal mode, the only view conversions are those
3845 -- involving ancestor conversion of an extended type.
3847 elsif Nkind (A) = N_Type_Conversion
3848 and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
3849 then
3850 Check_SPARK_Restriction
3851 ("ancestor conversion is the only permitted view "
3852 & "conversion", A);
3853 end if;
3854 end if;
3856 -- has warnings suppressed, then we reset Never_Set_In_Source for
3857 -- the calling entity. The reason for this is to catch cases like
3858 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
3859 -- uses trickery to modify an IN parameter.
3861 if Ekind (F) = E_In_Parameter
3862 and then Is_Entity_Name (A)
3863 and then Present (Entity (A))
3864 and then Ekind (Entity (A)) = E_Variable
3865 and then Has_Warnings_Off (F_Typ)
3866 then
3867 Set_Never_Set_In_Source (Entity (A), False);
3868 end if;
3870 -- Perform error checks for IN and IN OUT parameters
3872 if Ekind (F) /= E_Out_Parameter then
3874 -- Check unset reference. For scalar parameters, it is clearly
3875 -- wrong to pass an uninitialized value as either an IN or
3876 -- IN-OUT parameter. For composites, it is also clearly an
3877 -- error to pass a completely uninitialized value as an IN
3878 -- parameter, but the case of IN OUT is trickier. We prefer
3879 -- not to give a warning here. For example, suppose there is
3880 -- a routine that sets some component of a record to False.
3881 -- It is perfectly reasonable to make this IN-OUT and allow
3882 -- either initialized or uninitialized records to be passed
3883 -- in this case.
3885 -- For partially initialized composite values, we also avoid
3886 -- warnings, since it is quite likely that we are passing a
3887 -- partially initialized value and only the initialized fields
3888 -- will in fact be read in the subprogram.
3890 if Is_Scalar_Type (A_Typ)
3891 or else (Ekind (F) = E_In_Parameter
3892 and then not Is_Partially_Initialized_Type (A_Typ))
3893 then
3894 Check_Unset_Reference (A);
3895 end if;
3897 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3898 -- actual to a nested call, since this is case of reading an
3899 -- out parameter, which is not allowed.
3901 if Ada_Version = Ada_83
3902 and then Is_Entity_Name (A)
3903 and then Ekind (Entity (A)) = E_Out_Parameter
3904 then
3905 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3906 end if;
3907 end if;
3909 -- Case of OUT or IN OUT parameter
3911 if Ekind (F) /= E_In_Parameter then
3913 -- For an Out parameter, check for useless assignment. Note
3914 -- that we can't set Last_Assignment this early, because we may
3915 -- kill current values in Resolve_Call, and that call would
3916 -- clobber the Last_Assignment field.
3918 -- Note: call Warn_On_Useless_Assignment before doing the check
3919 -- below for Is_OK_Variable_For_Out_Formal so that the setting
3920 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
3921 -- reflects the last assignment, not this one!
3923 if Ekind (F) = E_Out_Parameter then
3924 if Warn_On_Modified_As_Out_Parameter (F)
3925 and then Is_Entity_Name (A)
3926 and then Present (Entity (A))
3927 and then Comes_From_Source (N)
3928 then
3929 Warn_On_Useless_Assignment (Entity (A), A);
3930 end if;
3931 end if;
3933 -- Validate the form of the actual. Note that the call to
3934 -- Is_OK_Variable_For_Out_Formal generates the required
3935 -- reference in this case.
3937 -- A call to an initialization procedure for an aggregate
3938 -- component may initialize a nested component of a constant
3939 -- designated object. In this context the object is variable.
3941 if not Is_OK_Variable_For_Out_Formal (A)
3942 and then not Is_Init_Proc (Nam)
3943 then
3944 Error_Msg_NE ("actual for& must be a variable", A, F);
3946 if Is_Subprogram (Current_Scope)
3947 and then
3948 (Is_Invariant_Procedure (Current_Scope)
3949 or else Is_Predicate_Function (Current_Scope))
3950 then
3951 Error_Msg_N
3952 ("function used in predicate cannot "
3953 & "modify its argument", F);
3954 end if;
3955 end if;
3957 -- What's the following about???
3959 if Is_Entity_Name (A) then
3960 Kill_Checks (Entity (A));
3961 else
3962 Kill_All_Checks;
3963 end if;
3964 end if;
3966 if Etype (A) = Any_Type then
3967 Set_Etype (N, Any_Type);
3968 return;
3969 end if;
3971 -- Apply appropriate range checks for in, out, and in-out
3972 -- parameters. Out and in-out parameters also need a separate
3973 -- check, if there is a type conversion, to make sure the return
3974 -- value meets the constraints of the variable before the
3975 -- conversion.
3977 -- Gigi looks at the check flag and uses the appropriate types.
3978 -- For now since one flag is used there is an optimization which
3979 -- might not be done in the In Out case since Gigi does not do
3980 -- any analysis. More thought required about this ???
3982 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
3984 -- Apply predicate checks, unless this is a call to the
3985 -- predicate check function itself, which would cause an
3986 -- infinite recursion, or it is a call to an initialization
3987 -- procedure whose operand is of course an unfinished object.
3989 if not (Ekind (Nam) = E_Function
3990 and then (Is_Predicate_Function (Nam)
3991 or else
3992 Is_Predicate_Function_M (Nam)))
3993 and then not Is_Init_Proc (Nam)
3994 then
3995 Apply_Predicate_Check (A, F_Typ);
3996 end if;
3998 -- Apply required constraint checks
4000 if Is_Scalar_Type (Etype (A)) then
4001 Apply_Scalar_Range_Check (A, F_Typ);
4003 elsif Is_Array_Type (Etype (A)) then
4004 Apply_Length_Check (A, F_Typ);
4006 elsif Is_Record_Type (F_Typ)
4007 and then Has_Discriminants (F_Typ)
4008 and then Is_Constrained (F_Typ)
4009 and then (not Is_Derived_Type (F_Typ)
4010 or else Comes_From_Source (Nam))
4011 then
4012 Apply_Discriminant_Check (A, F_Typ);
4014 elsif Is_Access_Type (F_Typ)
4015 and then Is_Array_Type (Designated_Type (F_Typ))
4016 and then Is_Constrained (Designated_Type (F_Typ))
4017 then
4018 Apply_Length_Check (A, F_Typ);
4020 elsif Is_Access_Type (F_Typ)
4021 and then Has_Discriminants (Designated_Type (F_Typ))
4022 and then Is_Constrained (Designated_Type (F_Typ))
4023 then
4024 Apply_Discriminant_Check (A, F_Typ);
4026 else
4027 Apply_Range_Check (A, F_Typ);
4028 end if;
4030 -- Ada 2005 (AI-231): Note that the controlling parameter case
4031 -- already existed in Ada 95, which is partially checked
4032 -- elsewhere (see Checks), and we don't want the warning
4033 -- message to differ.
4035 if Is_Access_Type (F_Typ)
4036 and then Can_Never_Be_Null (F_Typ)
4037 and then Known_Null (A)
4038 then
4039 if Is_Controlling_Formal (F) then
4040 Apply_Compile_Time_Constraint_Error
4041 (N => A,
4042 Msg => "null value not allowed here??",
4043 Reason => CE_Access_Check_Failed);
4045 elsif Ada_Version >= Ada_2005 then
4046 Apply_Compile_Time_Constraint_Error
4047 (N => A,
4048 Msg => "(Ada 2005) null not allowed in "
4049 & "null-excluding formal??",
4050 Reason => CE_Null_Not_Allowed);
4051 end if;
4052 end if;
4053 end if;
4055 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
4056 if Nkind (A) = N_Type_Conversion then
4057 if Is_Scalar_Type (A_Typ) then
4058 Apply_Scalar_Range_Check
4059 (Expression (A), Etype (Expression (A)), A_Typ);
4060 else
4061 Apply_Range_Check
4062 (Expression (A), Etype (Expression (A)), A_Typ);
4063 end if;
4065 else
4066 if Is_Scalar_Type (F_Typ) then
4067 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4068 elsif Is_Array_Type (F_Typ)
4069 and then Ekind (F) = E_Out_Parameter
4070 then
4071 Apply_Length_Check (A, F_Typ);
4072 else
4073 Apply_Range_Check (A, A_Typ, F_Typ);
4074 end if;
4075 end if;
4076 end if;
4078 -- An actual associated with an access parameter is implicitly
4079 -- converted to the anonymous access type of the formal and must
4080 -- satisfy the legality checks for access conversions.
4082 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4083 if not Valid_Conversion (A, F_Typ, A) then
4084 Error_Msg_N
4085 ("invalid implicit conversion for access parameter", A);
4086 end if;
4088 -- If the actual is an access selected component of a variable,
4089 -- the call may modify its designated object. It is reasonable
4090 -- to treat this as a potential modification of the enclosing
4091 -- record, to prevent spurious warnings that it should be
4092 -- declared as a constant, because intuitively programmers
4093 -- regard the designated subcomponent as part of the record.
4095 if Nkind (A) = N_Selected_Component
4096 and then Is_Entity_Name (Prefix (A))
4097 and then not Is_Constant_Object (Entity (Prefix (A)))
4098 then
4099 Note_Possible_Modification (A, Sure => False);
4100 end if;
4101 end if;
4103 -- Check bad case of atomic/volatile argument (RM C.6(12))
4105 if Is_By_Reference_Type (Etype (F))
4106 and then Comes_From_Source (N)
4107 then
4108 if Is_Atomic_Object (A)
4109 and then not Is_Atomic (Etype (F))
4110 then
4111 Error_Msg_NE
4112 ("cannot pass atomic argument to non-atomic formal&",
4113 A, F);
4115 elsif Is_Volatile_Object (A)
4116 and then not Is_Volatile (Etype (F))
4117 then
4118 Error_Msg_NE
4119 ("cannot pass volatile argument to non-volatile formal&",
4120 A, F);
4121 end if;
4122 end if;
4124 -- Check that subprograms don't have improper controlling
4125 -- arguments (RM 3.9.2 (9)).
4127 -- A primitive operation may have an access parameter of an
4128 -- incomplete tagged type, but a dispatching call is illegal
4129 -- if the type is still incomplete.
4131 if Is_Controlling_Formal (F) then
4132 Set_Is_Controlling_Actual (A);
4134 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4135 declare
4136 Desig : constant Entity_Id := Designated_Type (Etype (F));
4137 begin
4138 if Ekind (Desig) = E_Incomplete_Type
4139 and then No (Full_View (Desig))
4140 and then No (Non_Limited_View (Desig))
4141 then
4142 Error_Msg_NE
4143 ("premature use of incomplete type& "
4144 & "in dispatching call", A, Desig);
4145 end if;
4146 end;
4147 end if;
4149 elsif Nkind (A) = N_Explicit_Dereference then
4150 Validate_Remote_Access_To_Class_Wide_Type (A);
4151 end if;
4153 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
4154 and then not Is_Class_Wide_Type (F_Typ)
4155 and then not Is_Controlling_Formal (F)
4156 then
4157 Error_Msg_N ("class-wide argument not allowed here!", A);
4159 if Is_Subprogram (Nam)
4160 and then Comes_From_Source (Nam)
4161 then
4162 Error_Msg_Node_2 := F_Typ;
4163 Error_Msg_NE
4164 ("& is not a dispatching operation of &!", A, Nam);
4165 end if;
4167 -- Apply the checks described in 3.10.2(27): if the context is a
4168 -- specific access-to-object, the actual cannot be class-wide.
4169 -- Use base type to exclude access_to_subprogram cases.
4171 elsif Is_Access_Type (A_Typ)
4172 and then Is_Access_Type (F_Typ)
4173 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
4174 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
4175 or else (Nkind (A) = N_Attribute_Reference
4176 and then
4177 Is_Class_Wide_Type (Etype (Prefix (A)))))
4178 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
4179 and then not Is_Controlling_Formal (F)
4181 -- Disable these checks for call to imported C++ subprograms
4183 and then not
4184 (Is_Entity_Name (Name (N))
4185 and then Is_Imported (Entity (Name (N)))
4186 and then Convention (Entity (Name (N))) = Convention_CPP)
4187 then
4188 Error_Msg_N
4189 ("access to class-wide argument not allowed here!", A);
4191 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4192 Error_Msg_Node_2 := Designated_Type (F_Typ);
4193 Error_Msg_NE
4194 ("& is not a dispatching operation of &!", A, Nam);
4195 end if;
4196 end if;
4198 Eval_Actual (A);
4200 -- If it is a named association, treat the selector_name as a
4201 -- proper identifier, and mark the corresponding entity.
4203 if Nkind (Parent (A)) = N_Parameter_Association
4205 -- Ignore reference in SPARK mode, as it refers to an entity not
4206 -- in scope at the point of reference, so the reference should
4207 -- be ignored for computing effects of subprograms.
4209 and then not SPARK_Mode
4210 then
4211 Set_Entity (Selector_Name (Parent (A)), F);
4212 Generate_Reference (F, Selector_Name (Parent (A)));
4213 Set_Etype (Selector_Name (Parent (A)), F_Typ);
4214 Generate_Reference (F_Typ, N, ' ');
4215 end if;
4217 Prev := A;
4219 if Ekind (F) /= E_Out_Parameter then
4220 Check_Unset_Reference (A);
4221 end if;
4223 Next_Actual (A);
4225 -- Case where actual is not present
4227 else
4228 Insert_Default;
4229 end if;
4231 Next_Formal (F);
4232 end loop;
4233 end Resolve_Actuals;
4235 -----------------------
4236 -- Resolve_Allocator --
4237 -----------------------
4239 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
4240 Desig_T : constant Entity_Id := Designated_Type (Typ);
4241 E : constant Node_Id := Expression (N);
4242 Subtyp : Entity_Id;
4243 Discrim : Entity_Id;
4244 Constr : Node_Id;
4245 Aggr : Node_Id;
4246 Assoc : Node_Id := Empty;
4247 Disc_Exp : Node_Id;
4249 procedure Check_Allocator_Discrim_Accessibility
4250 (Disc_Exp : Node_Id;
4251 Alloc_Typ : Entity_Id);
4252 -- Check that accessibility level associated with an access discriminant
4253 -- initialized in an allocator by the expression Disc_Exp is not deeper
4254 -- than the level of the allocator type Alloc_Typ. An error message is
4255 -- issued if this condition is violated. Specialized checks are done for
4256 -- the cases of a constraint expression which is an access attribute or
4257 -- an access discriminant.
4259 function In_Dispatching_Context return Boolean;
4260 -- If the allocator is an actual in a call, it is allowed to be class-
4261 -- wide when the context is not because it is a controlling actual.
4263 -------------------------------------------
4264 -- Check_Allocator_Discrim_Accessibility --
4265 -------------------------------------------
4267 procedure Check_Allocator_Discrim_Accessibility
4268 (Disc_Exp : Node_Id;
4269 Alloc_Typ : Entity_Id)
4271 begin
4272 if Type_Access_Level (Etype (Disc_Exp)) >
4273 Deepest_Type_Access_Level (Alloc_Typ)
4274 then
4275 Error_Msg_N
4276 ("operand type has deeper level than allocator type", Disc_Exp);
4278 -- When the expression is an Access attribute the level of the prefix
4279 -- object must not be deeper than that of the allocator's type.
4281 elsif Nkind (Disc_Exp) = N_Attribute_Reference
4282 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
4283 Attribute_Access
4284 and then Object_Access_Level (Prefix (Disc_Exp)) >
4285 Deepest_Type_Access_Level (Alloc_Typ)
4286 then
4287 Error_Msg_N
4288 ("prefix of attribute has deeper level than allocator type",
4289 Disc_Exp);
4291 -- When the expression is an access discriminant the check is against
4292 -- the level of the prefix object.
4294 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
4295 and then Nkind (Disc_Exp) = N_Selected_Component
4296 and then Object_Access_Level (Prefix (Disc_Exp)) >
4297 Deepest_Type_Access_Level (Alloc_Typ)
4298 then
4299 Error_Msg_N
4300 ("access discriminant has deeper level than allocator type",
4301 Disc_Exp);
4303 -- All other cases are legal
4305 else
4306 null;
4307 end if;
4308 end Check_Allocator_Discrim_Accessibility;
4310 ----------------------------
4311 -- In_Dispatching_Context --
4312 ----------------------------
4314 function In_Dispatching_Context return Boolean is
4315 Par : constant Node_Id := Parent (N);
4317 begin
4318 return Nkind (Par) in N_Subprogram_Call
4319 and then Is_Entity_Name (Name (Par))
4320 and then Is_Dispatching_Operation (Entity (Name (Par)));
4321 end In_Dispatching_Context;
4323 -- Start of processing for Resolve_Allocator
4325 begin
4326 -- Replace general access with specific type
4328 if Ekind (Etype (N)) = E_Allocator_Type then
4329 Set_Etype (N, Base_Type (Typ));
4330 end if;
4332 if Is_Abstract_Type (Typ) then
4333 Error_Msg_N ("type of allocator cannot be abstract", N);
4334 end if;
4336 -- For qualified expression, resolve the expression using the given
4337 -- subtype (nothing to do for type mark, subtype indication)
4339 if Nkind (E) = N_Qualified_Expression then
4340 if Is_Class_Wide_Type (Etype (E))
4341 and then not Is_Class_Wide_Type (Desig_T)
4342 and then not In_Dispatching_Context
4343 then
4344 Error_Msg_N
4345 ("class-wide allocator not allowed for this access type", N);
4346 end if;
4348 Resolve (Expression (E), Etype (E));
4349 Check_Unset_Reference (Expression (E));
4351 -- A qualified expression requires an exact match of the type.
4352 -- Class-wide matching is not allowed.
4354 if (Is_Class_Wide_Type (Etype (Expression (E)))
4355 or else Is_Class_Wide_Type (Etype (E)))
4356 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
4357 then
4358 Wrong_Type (Expression (E), Etype (E));
4359 end if;
4361 -- Calls to build-in-place functions are not currently supported in
4362 -- allocators for access types associated with a simple storage pool.
4363 -- Supporting such allocators may require passing additional implicit
4364 -- parameters to build-in-place functions (or a significant revision
4365 -- of the current b-i-p implementation to unify the handling for
4366 -- multiple kinds of storage pools). ???
4368 if Is_Limited_View (Desig_T)
4369 and then Nkind (Expression (E)) = N_Function_Call
4370 then
4371 declare
4372 Pool : constant Entity_Id :=
4373 Associated_Storage_Pool (Root_Type (Typ));
4374 begin
4375 if Present (Pool)
4376 and then
4377 Present (Get_Rep_Pragma
4378 (Etype (Pool), Name_Simple_Storage_Pool_Type))
4379 then
4380 Error_Msg_N
4381 ("limited function calls not yet supported in simple "
4382 & "storage pool allocators", Expression (E));
4383 end if;
4384 end;
4385 end if;
4387 -- A special accessibility check is needed for allocators that
4388 -- constrain access discriminants. The level of the type of the
4389 -- expression used to constrain an access discriminant cannot be
4390 -- deeper than the type of the allocator (in contrast to access
4391 -- parameters, where the level of the actual can be arbitrary).
4393 -- We can't use Valid_Conversion to perform this check because in
4394 -- general the type of the allocator is unrelated to the type of
4395 -- the access discriminant.
4397 if Ekind (Typ) /= E_Anonymous_Access_Type
4398 or else Is_Local_Anonymous_Access (Typ)
4399 then
4400 Subtyp := Entity (Subtype_Mark (E));
4402 Aggr := Original_Node (Expression (E));
4404 if Has_Discriminants (Subtyp)
4405 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
4406 then
4407 Discrim := First_Discriminant (Base_Type (Subtyp));
4409 -- Get the first component expression of the aggregate
4411 if Present (Expressions (Aggr)) then
4412 Disc_Exp := First (Expressions (Aggr));
4414 elsif Present (Component_Associations (Aggr)) then
4415 Assoc := First (Component_Associations (Aggr));
4417 if Present (Assoc) then
4418 Disc_Exp := Expression (Assoc);
4419 else
4420 Disc_Exp := Empty;
4421 end if;
4423 else
4424 Disc_Exp := Empty;
4425 end if;
4427 while Present (Discrim) and then Present (Disc_Exp) loop
4428 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4429 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4430 end if;
4432 Next_Discriminant (Discrim);
4434 if Present (Discrim) then
4435 if Present (Assoc) then
4436 Next (Assoc);
4437 Disc_Exp := Expression (Assoc);
4439 elsif Present (Next (Disc_Exp)) then
4440 Next (Disc_Exp);
4442 else
4443 Assoc := First (Component_Associations (Aggr));
4445 if Present (Assoc) then
4446 Disc_Exp := Expression (Assoc);
4447 else
4448 Disc_Exp := Empty;
4449 end if;
4450 end if;
4451 end if;
4452 end loop;
4453 end if;
4454 end if;
4456 -- For a subtype mark or subtype indication, freeze the subtype
4458 else
4459 Freeze_Expression (E);
4461 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
4462 Error_Msg_N
4463 ("initialization required for access-to-constant allocator", N);
4464 end if;
4466 -- A special accessibility check is needed for allocators that
4467 -- constrain access discriminants. The level of the type of the
4468 -- expression used to constrain an access discriminant cannot be
4469 -- deeper than the type of the allocator (in contrast to access
4470 -- parameters, where the level of the actual can be arbitrary).
4471 -- We can't use Valid_Conversion to perform this check because
4472 -- in general the type of the allocator is unrelated to the type
4473 -- of the access discriminant.
4475 if Nkind (Original_Node (E)) = N_Subtype_Indication
4476 and then (Ekind (Typ) /= E_Anonymous_Access_Type
4477 or else Is_Local_Anonymous_Access (Typ))
4478 then
4479 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
4481 if Has_Discriminants (Subtyp) then
4482 Discrim := First_Discriminant (Base_Type (Subtyp));
4483 Constr := First (Constraints (Constraint (Original_Node (E))));
4484 while Present (Discrim) and then Present (Constr) loop
4485 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4486 if Nkind (Constr) = N_Discriminant_Association then
4487 Disc_Exp := Original_Node (Expression (Constr));
4488 else
4489 Disc_Exp := Original_Node (Constr);
4490 end if;
4492 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4493 end if;
4495 Next_Discriminant (Discrim);
4496 Next (Constr);
4497 end loop;
4498 end if;
4499 end if;
4500 end if;
4502 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4503 -- check that the level of the type of the created object is not deeper
4504 -- than the level of the allocator's access type, since extensions can
4505 -- now occur at deeper levels than their ancestor types. This is a
4506 -- static accessibility level check; a run-time check is also needed in
4507 -- the case of an initialized allocator with a class-wide argument (see
4508 -- Expand_Allocator_Expression).
4510 if Ada_Version >= Ada_2005
4511 and then Is_Class_Wide_Type (Desig_T)
4512 then
4513 declare
4514 Exp_Typ : Entity_Id;
4516 begin
4517 if Nkind (E) = N_Qualified_Expression then
4518 Exp_Typ := Etype (E);
4519 elsif Nkind (E) = N_Subtype_Indication then
4520 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4521 else
4522 Exp_Typ := Entity (E);
4523 end if;
4525 if Type_Access_Level (Exp_Typ) >
4526 Deepest_Type_Access_Level (Typ)
4527 then
4528 if In_Instance_Body then
4529 Error_Msg_N
4530 ("??type in allocator has deeper level than "
4531 & "designated class-wide type", E);
4532 Error_Msg_N
4533 ("\??Program_Error will be raised at run time", E);
4534 Rewrite (N,
4535 Make_Raise_Program_Error (Sloc (N),
4536 Reason => PE_Accessibility_Check_Failed));
4537 Set_Etype (N, Typ);
4539 -- Do not apply Ada 2005 accessibility checks on a class-wide
4540 -- allocator if the type given in the allocator is a formal
4541 -- type. A run-time check will be performed in the instance.
4543 elsif not Is_Generic_Type (Exp_Typ) then
4544 Error_Msg_N ("type in allocator has deeper level than "
4545 & "designated class-wide type", E);
4546 end if;
4547 end if;
4548 end;
4549 end if;
4551 -- Check for allocation from an empty storage pool
4553 if No_Pool_Assigned (Typ) then
4554 Error_Msg_N ("allocation from empty storage pool!", N);
4556 -- If the context is an unchecked conversion, as may happen within an
4557 -- inlined subprogram, the allocator is being resolved with its own
4558 -- anonymous type. In that case, if the target type has a specific
4559 -- storage pool, it must be inherited explicitly by the allocator type.
4561 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4562 and then No (Associated_Storage_Pool (Typ))
4563 then
4564 Set_Associated_Storage_Pool
4565 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4566 end if;
4568 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
4569 Check_Restriction (No_Anonymous_Allocators, N);
4570 end if;
4572 -- Check that an allocator with task parts isn't for a nested access
4573 -- type when restriction No_Task_Hierarchy applies.
4575 if not Is_Library_Level_Entity (Base_Type (Typ))
4576 and then Has_Task (Base_Type (Desig_T))
4577 then
4578 Check_Restriction (No_Task_Hierarchy, N);
4579 end if;
4581 -- An erroneous allocator may be rewritten as a raise Program_Error
4582 -- statement.
4584 if Nkind (N) = N_Allocator then
4586 -- An anonymous access discriminant is the definition of a
4587 -- coextension.
4589 if Ekind (Typ) = E_Anonymous_Access_Type
4590 and then Nkind (Associated_Node_For_Itype (Typ)) =
4591 N_Discriminant_Specification
4592 then
4593 declare
4594 Discr : constant Entity_Id :=
4595 Defining_Identifier (Associated_Node_For_Itype (Typ));
4597 begin
4598 Check_Restriction (No_Coextensions, N);
4600 -- Ada 2012 AI05-0052: If the designated type of the allocator
4601 -- is limited, then the allocator shall not be used to define
4602 -- the value of an access discriminant unless the discriminated
4603 -- type is immutably limited.
4605 if Ada_Version >= Ada_2012
4606 and then Is_Limited_Type (Desig_T)
4607 and then not Is_Limited_View (Scope (Discr))
4608 then
4609 Error_Msg_N
4610 ("only immutably limited types can have anonymous "
4611 & "access discriminants designating a limited type", N);
4612 end if;
4613 end;
4615 -- Avoid marking an allocator as a dynamic coextension if it is
4616 -- within a static construct.
4618 if not Is_Static_Coextension (N) then
4619 Set_Is_Dynamic_Coextension (N);
4620 end if;
4622 -- Cleanup for potential static coextensions
4624 else
4625 Set_Is_Dynamic_Coextension (N, False);
4626 Set_Is_Static_Coextension (N, False);
4627 end if;
4628 end if;
4630 -- Report a simple error: if the designated object is a local task,
4631 -- its body has not been seen yet, and its activation will fail an
4632 -- elaboration check.
4634 if Is_Task_Type (Desig_T)
4635 and then Scope (Base_Type (Desig_T)) = Current_Scope
4636 and then Is_Compilation_Unit (Current_Scope)
4637 and then Ekind (Current_Scope) = E_Package
4638 and then not In_Package_Body (Current_Scope)
4639 then
4640 Error_Msg_N ("??cannot activate task before body seen", N);
4641 Error_Msg_N ("\??Program_Error will be raised at run time", N);
4642 end if;
4644 -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a
4645 -- type with a task component on a subpool. This action must raise
4646 -- Program_Error at runtime.
4648 if Ada_Version >= Ada_2012
4649 and then Nkind (N) = N_Allocator
4650 and then Present (Subpool_Handle_Name (N))
4651 and then Has_Task (Desig_T)
4652 then
4653 Error_Msg_N ("??cannot allocate task on subpool", N);
4654 Error_Msg_N ("\??Program_Error will be raised at run time", N);
4656 Rewrite (N,
4657 Make_Raise_Program_Error (Sloc (N),
4658 Reason => PE_Explicit_Raise));
4659 Set_Etype (N, Typ);
4660 end if;
4661 end Resolve_Allocator;
4663 ---------------------------
4664 -- Resolve_Arithmetic_Op --
4665 ---------------------------
4667 -- Used for resolving all arithmetic operators except exponentiation
4669 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
4670 L : constant Node_Id := Left_Opnd (N);
4671 R : constant Node_Id := Right_Opnd (N);
4672 TL : constant Entity_Id := Base_Type (Etype (L));
4673 TR : constant Entity_Id := Base_Type (Etype (R));
4674 T : Entity_Id;
4675 Rop : Node_Id;
4677 B_Typ : constant Entity_Id := Base_Type (Typ);
4678 -- We do the resolution using the base type, because intermediate values
4679 -- in expressions always are of the base type, not a subtype of it.
4681 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4682 -- Returns True if N is in a context that expects "any real type"
4684 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4685 -- Return True iff given type is Integer or universal real/integer
4687 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4688 -- Choose type of integer literal in fixed-point operation to conform
4689 -- to available fixed-point type. T is the type of the other operand,
4690 -- which is needed to determine the expected type of N.
4692 procedure Set_Operand_Type (N : Node_Id);
4693 -- Set operand type to T if universal
4695 -------------------------------
4696 -- Expected_Type_Is_Any_Real --
4697 -------------------------------
4699 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4700 begin
4701 -- N is the expression after "delta" in a fixed_point_definition;
4702 -- see RM-3.5.9(6):
4704 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4705 N_Decimal_Fixed_Point_Definition,
4707 -- N is one of the bounds in a real_range_specification;
4708 -- see RM-3.5.7(5):
4710 N_Real_Range_Specification,
4712 -- N is the expression of a delta_constraint;
4713 -- see RM-J.3(3):
4715 N_Delta_Constraint);
4716 end Expected_Type_Is_Any_Real;
4718 -----------------------------
4719 -- Is_Integer_Or_Universal --
4720 -----------------------------
4722 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4723 T : Entity_Id;
4724 Index : Interp_Index;
4725 It : Interp;
4727 begin
4728 if not Is_Overloaded (N) then
4729 T := Etype (N);
4730 return Base_Type (T) = Base_Type (Standard_Integer)
4731 or else T = Universal_Integer
4732 or else T = Universal_Real;
4733 else
4734 Get_First_Interp (N, Index, It);
4735 while Present (It.Typ) loop
4736 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4737 or else It.Typ = Universal_Integer
4738 or else It.Typ = Universal_Real
4739 then
4740 return True;
4741 end if;
4743 Get_Next_Interp (Index, It);
4744 end loop;
4745 end if;
4747 return False;
4748 end Is_Integer_Or_Universal;
4750 ----------------------------
4751 -- Set_Mixed_Mode_Operand --
4752 ----------------------------
4754 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4755 Index : Interp_Index;
4756 It : Interp;
4758 begin
4759 if Universal_Interpretation (N) = Universal_Integer then
4761 -- A universal integer literal is resolved as standard integer
4762 -- except in the case of a fixed-point result, where we leave it
4763 -- as universal (to be handled by Exp_Fixd later on)
4765 if Is_Fixed_Point_Type (T) then
4766 Resolve (N, Universal_Integer);
4767 else
4768 Resolve (N, Standard_Integer);
4769 end if;
4771 elsif Universal_Interpretation (N) = Universal_Real
4772 and then (T = Base_Type (Standard_Integer)
4773 or else T = Universal_Integer
4774 or else T = Universal_Real)
4775 then
4776 -- A universal real can appear in a fixed-type context. We resolve
4777 -- the literal with that context, even though this might raise an
4778 -- exception prematurely (the other operand may be zero).
4780 Resolve (N, B_Typ);
4782 elsif Etype (N) = Base_Type (Standard_Integer)
4783 and then T = Universal_Real
4784 and then Is_Overloaded (N)
4785 then
4786 -- Integer arg in mixed-mode operation. Resolve with universal
4787 -- type, in case preference rule must be applied.
4789 Resolve (N, Universal_Integer);
4791 elsif Etype (N) = T
4792 and then B_Typ /= Universal_Fixed
4793 then
4794 -- Not a mixed-mode operation, resolve with context
4796 Resolve (N, B_Typ);
4798 elsif Etype (N) = Any_Fixed then
4800 -- N may itself be a mixed-mode operation, so use context type
4802 Resolve (N, B_Typ);
4804 elsif Is_Fixed_Point_Type (T)
4805 and then B_Typ = Universal_Fixed
4806 and then Is_Overloaded (N)
4807 then
4808 -- Must be (fixed * fixed) operation, operand must have one
4809 -- compatible interpretation.
4811 Resolve (N, Any_Fixed);
4813 elsif Is_Fixed_Point_Type (B_Typ)
4814 and then (T = Universal_Real or else Is_Fixed_Point_Type (T))
4815 and then Is_Overloaded (N)
4816 then
4817 -- C * F(X) in a fixed context, where C is a real literal or a
4818 -- fixed-point expression. F must have either a fixed type
4819 -- interpretation or an integer interpretation, but not both.
4821 Get_First_Interp (N, Index, It);
4822 while Present (It.Typ) loop
4823 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
4824 if Analyzed (N) then
4825 Error_Msg_N ("ambiguous operand in fixed operation", N);
4826 else
4827 Resolve (N, Standard_Integer);
4828 end if;
4830 elsif Is_Fixed_Point_Type (It.Typ) then
4831 if Analyzed (N) then
4832 Error_Msg_N ("ambiguous operand in fixed operation", N);
4833 else
4834 Resolve (N, It.Typ);
4835 end if;
4836 end if;
4838 Get_Next_Interp (Index, It);
4839 end loop;
4841 -- Reanalyze the literal with the fixed type of the context. If
4842 -- context is Universal_Fixed, we are within a conversion, leave
4843 -- the literal as a universal real because there is no usable
4844 -- fixed type, and the target of the conversion plays no role in
4845 -- the resolution.
4847 declare
4848 Op2 : Node_Id;
4849 T2 : Entity_Id;
4851 begin
4852 if N = L then
4853 Op2 := R;
4854 else
4855 Op2 := L;
4856 end if;
4858 if B_Typ = Universal_Fixed
4859 and then Nkind (Op2) = N_Real_Literal
4860 then
4861 T2 := Universal_Real;
4862 else
4863 T2 := B_Typ;
4864 end if;
4866 Set_Analyzed (Op2, False);
4867 Resolve (Op2, T2);
4868 end;
4870 else
4871 Resolve (N);
4872 end if;
4873 end Set_Mixed_Mode_Operand;
4875 ----------------------
4876 -- Set_Operand_Type --
4877 ----------------------
4879 procedure Set_Operand_Type (N : Node_Id) is
4880 begin
4881 if Etype (N) = Universal_Integer
4882 or else Etype (N) = Universal_Real
4883 then
4884 Set_Etype (N, T);
4885 end if;
4886 end Set_Operand_Type;
4888 -- Start of processing for Resolve_Arithmetic_Op
4890 begin
4891 if Comes_From_Source (N)
4892 and then Ekind (Entity (N)) = E_Function
4893 and then Is_Imported (Entity (N))
4894 and then Is_Intrinsic_Subprogram (Entity (N))
4895 then
4896 Resolve_Intrinsic_Operator (N, Typ);
4897 return;
4899 -- Special-case for mixed-mode universal expressions or fixed point type
4900 -- operation: each argument is resolved separately. The same treatment
4901 -- is required if one of the operands of a fixed point operation is
4902 -- universal real, since in this case we don't do a conversion to a
4903 -- specific fixed-point type (instead the expander handles the case).
4905 -- Set the type of the node to its universal interpretation because
4906 -- legality checks on an exponentiation operand need the context.
4908 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
4909 and then Present (Universal_Interpretation (L))
4910 and then Present (Universal_Interpretation (R))
4911 then
4912 Set_Etype (N, B_Typ);
4913 Resolve (L, Universal_Interpretation (L));
4914 Resolve (R, Universal_Interpretation (R));
4916 elsif (B_Typ = Universal_Real
4917 or else Etype (N) = Universal_Fixed
4918 or else (Etype (N) = Any_Fixed
4919 and then Is_Fixed_Point_Type (B_Typ))
4920 or else (Is_Fixed_Point_Type (B_Typ)
4921 and then (Is_Integer_Or_Universal (L)
4922 or else
4923 Is_Integer_Or_Universal (R))))
4924 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4925 then
4926 if TL = Universal_Integer or else TR = Universal_Integer then
4927 Check_For_Visible_Operator (N, B_Typ);
4928 end if;
4930 -- If context is a fixed type and one operand is integer, the other
4931 -- is resolved with the type of the context.
4933 if Is_Fixed_Point_Type (B_Typ)
4934 and then (Base_Type (TL) = Base_Type (Standard_Integer)
4935 or else TL = Universal_Integer)
4936 then
4937 Resolve (R, B_Typ);
4938 Resolve (L, TL);
4940 elsif Is_Fixed_Point_Type (B_Typ)
4941 and then (Base_Type (TR) = Base_Type (Standard_Integer)
4942 or else TR = Universal_Integer)
4943 then
4944 Resolve (L, B_Typ);
4945 Resolve (R, TR);
4947 else
4948 Set_Mixed_Mode_Operand (L, TR);
4949 Set_Mixed_Mode_Operand (R, TL);
4950 end if;
4952 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
4953 -- multiplying operators from being used when the expected type is
4954 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
4955 -- some cases where the expected type is actually Any_Real;
4956 -- Expected_Type_Is_Any_Real takes care of that case.
4958 if Etype (N) = Universal_Fixed
4959 or else Etype (N) = Any_Fixed
4960 then
4961 if B_Typ = Universal_Fixed
4962 and then not Expected_Type_Is_Any_Real (N)
4963 and then not Nkind_In (Parent (N), N_Type_Conversion,
4964 N_Unchecked_Type_Conversion)
4965 then
4966 Error_Msg_N ("type cannot be determined from context!", N);
4967 Error_Msg_N ("\explicit conversion to result type required", N);
4969 Set_Etype (L, Any_Type);
4970 Set_Etype (R, Any_Type);
4972 else
4973 if Ada_Version = Ada_83
4974 and then Etype (N) = Universal_Fixed
4975 and then not
4976 Nkind_In (Parent (N), N_Type_Conversion,
4977 N_Unchecked_Type_Conversion)
4978 then
4979 Error_Msg_N
4980 ("(Ada 83) fixed-point operation "
4981 & "needs explicit conversion", N);
4982 end if;
4984 -- The expected type is "any real type" in contexts like
4986 -- type T is delta <universal_fixed-expression> ...
4988 -- in which case we need to set the type to Universal_Real
4989 -- so that static expression evaluation will work properly.
4991 if Expected_Type_Is_Any_Real (N) then
4992 Set_Etype (N, Universal_Real);
4993 else
4994 Set_Etype (N, B_Typ);
4995 end if;
4996 end if;
4998 elsif Is_Fixed_Point_Type (B_Typ)
4999 and then (Is_Integer_Or_Universal (L)
5000 or else Nkind (L) = N_Real_Literal
5001 or else Nkind (R) = N_Real_Literal
5002 or else Is_Integer_Or_Universal (R))
5003 then
5004 Set_Etype (N, B_Typ);
5006 elsif Etype (N) = Any_Fixed then
5008 -- If no previous errors, this is only possible if one operand is
5009 -- overloaded and the context is universal. Resolve as such.
5011 Set_Etype (N, B_Typ);
5012 end if;
5014 else
5015 if (TL = Universal_Integer or else TL = Universal_Real)
5016 and then
5017 (TR = Universal_Integer or else TR = Universal_Real)
5018 then
5019 Check_For_Visible_Operator (N, B_Typ);
5020 end if;
5022 -- If the context is Universal_Fixed and the operands are also
5023 -- universal fixed, this is an error, unless there is only one
5024 -- applicable fixed_point type (usually Duration).
5026 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
5027 T := Unique_Fixed_Point_Type (N);
5029 if T = Any_Type then
5030 Set_Etype (N, T);
5031 return;
5032 else
5033 Resolve (L, T);
5034 Resolve (R, T);
5035 end if;
5037 else
5038 Resolve (L, B_Typ);
5039 Resolve (R, B_Typ);
5040 end if;
5042 -- If one of the arguments was resolved to a non-universal type.
5043 -- label the result of the operation itself with the same type.
5044 -- Do the same for the universal argument, if any.
5046 T := Intersect_Types (L, R);
5047 Set_Etype (N, Base_Type (T));
5048 Set_Operand_Type (L);
5049 Set_Operand_Type (R);
5050 end if;
5052 Generate_Operator_Reference (N, Typ);
5053 Analyze_Dimension (N);
5054 Eval_Arithmetic_Op (N);
5056 -- In SPARK, a multiplication or division with operands of fixed point
5057 -- types shall be qualified or explicitly converted to identify the
5058 -- result type.
5060 if (Is_Fixed_Point_Type (Etype (L))
5061 or else Is_Fixed_Point_Type (Etype (R)))
5062 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
5063 and then
5064 not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion)
5065 then
5066 Check_SPARK_Restriction
5067 ("operation should be qualified or explicitly converted", N);
5068 end if;
5070 -- Set overflow and division checking bit
5072 if Nkind (N) in N_Op then
5073 if not Overflow_Checks_Suppressed (Etype (N)) then
5074 Enable_Overflow_Check (N);
5075 end if;
5077 -- Give warning if explicit division by zero
5079 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
5080 and then not Division_Checks_Suppressed (Etype (N))
5081 then
5082 Rop := Right_Opnd (N);
5084 if Compile_Time_Known_Value (Rop)
5085 and then ((Is_Integer_Type (Etype (Rop))
5086 and then Expr_Value (Rop) = Uint_0)
5087 or else
5088 (Is_Real_Type (Etype (Rop))
5089 and then Expr_Value_R (Rop) = Ureal_0))
5090 then
5091 -- Specialize the warning message according to the operation.
5092 -- The following warnings are for the case
5094 case Nkind (N) is
5095 when N_Op_Divide =>
5097 -- For division, we have two cases, for float division
5098 -- of an unconstrained float type, on a machine where
5099 -- Machine_Overflows is false, we don't get an exception
5100 -- at run-time, but rather an infinity or Nan. The Nan
5101 -- case is pretty obscure, so just warn about infinities.
5103 if Is_Floating_Point_Type (Typ)
5104 and then not Is_Constrained (Typ)
5105 and then not Machine_Overflows_On_Target
5106 then
5107 Error_Msg_N
5108 ("float division by zero, may generate "
5109 & "'+'/'- infinity??", Right_Opnd (N));
5111 -- For all other cases, we get a Constraint_Error
5113 else
5114 Apply_Compile_Time_Constraint_Error
5115 (N, "division by zero??", CE_Divide_By_Zero,
5116 Loc => Sloc (Right_Opnd (N)));
5117 end if;
5119 when N_Op_Rem =>
5120 Apply_Compile_Time_Constraint_Error
5121 (N, "rem with zero divisor??", CE_Divide_By_Zero,
5122 Loc => Sloc (Right_Opnd (N)));
5124 when N_Op_Mod =>
5125 Apply_Compile_Time_Constraint_Error
5126 (N, "mod with zero divisor??", CE_Divide_By_Zero,
5127 Loc => Sloc (Right_Opnd (N)));
5129 -- Division by zero can only happen with division, rem,
5130 -- and mod operations.
5132 when others =>
5133 raise Program_Error;
5134 end case;
5136 -- Otherwise just set the flag to check at run time
5138 else
5139 Activate_Division_Check (N);
5140 end if;
5141 end if;
5143 -- If Restriction No_Implicit_Conditionals is active, then it is
5144 -- violated if either operand can be negative for mod, or for rem
5145 -- if both operands can be negative.
5147 if Restriction_Check_Required (No_Implicit_Conditionals)
5148 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
5149 then
5150 declare
5151 Lo : Uint;
5152 Hi : Uint;
5153 OK : Boolean;
5155 LNeg : Boolean;
5156 RNeg : Boolean;
5157 -- Set if corresponding operand might be negative
5159 begin
5160 Determine_Range
5161 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
5162 LNeg := (not OK) or else Lo < 0;
5164 Determine_Range
5165 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
5166 RNeg := (not OK) or else Lo < 0;
5168 -- Check if we will be generating conditionals. There are two
5169 -- cases where that can happen, first for REM, the only case
5170 -- is largest negative integer mod -1, where the division can
5171 -- overflow, but we still have to give the right result. The
5172 -- front end generates a test for this annoying case. Here we
5173 -- just test if both operands can be negative (that's what the
5174 -- expander does, so we match its logic here).
5176 -- The second case is mod where either operand can be negative.
5177 -- In this case, the back end has to generate additional tests.
5179 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
5180 or else
5181 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
5182 then
5183 Check_Restriction (No_Implicit_Conditionals, N);
5184 end if;
5185 end;
5186 end if;
5187 end if;
5189 Check_Unset_Reference (L);
5190 Check_Unset_Reference (R);
5191 Check_Function_Writable_Actuals (N);
5192 end Resolve_Arithmetic_Op;
5194 ------------------
5195 -- Resolve_Call --
5196 ------------------
5198 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
5199 Loc : constant Source_Ptr := Sloc (N);
5200 Subp : constant Node_Id := Name (N);
5201 Nam : Entity_Id;
5202 I : Interp_Index;
5203 It : Interp;
5204 Norm_OK : Boolean;
5205 Scop : Entity_Id;
5206 Rtype : Entity_Id;
5208 function Same_Or_Aliased_Subprograms
5209 (S : Entity_Id;
5210 E : Entity_Id) return Boolean;
5211 -- Returns True if the subprogram entity S is the same as E or else
5212 -- S is an alias of E.
5214 ---------------------------------
5215 -- Same_Or_Aliased_Subprograms --
5216 ---------------------------------
5218 function Same_Or_Aliased_Subprograms
5219 (S : Entity_Id;
5220 E : Entity_Id) return Boolean
5222 Subp_Alias : constant Entity_Id := Alias (S);
5223 begin
5224 return S = E
5225 or else (Present (Subp_Alias) and then Subp_Alias = E);
5226 end Same_Or_Aliased_Subprograms;
5228 -- Start of processing for Resolve_Call
5230 begin
5231 -- The context imposes a unique interpretation with type Typ on a
5232 -- procedure or function call. Find the entity of the subprogram that
5233 -- yields the expected type, and propagate the corresponding formal
5234 -- constraints on the actuals. The caller has established that an
5235 -- interpretation exists, and emitted an error if not unique.
5237 -- First deal with the case of a call to an access-to-subprogram,
5238 -- dereference made explicit in Analyze_Call.
5240 if Ekind (Etype (Subp)) = E_Subprogram_Type then
5241 if not Is_Overloaded (Subp) then
5242 Nam := Etype (Subp);
5244 else
5245 -- Find the interpretation whose type (a subprogram type) has a
5246 -- return type that is compatible with the context. Analysis of
5247 -- the node has established that one exists.
5249 Nam := Empty;
5251 Get_First_Interp (Subp, I, It);
5252 while Present (It.Typ) loop
5253 if Covers (Typ, Etype (It.Typ)) then
5254 Nam := It.Typ;
5255 exit;
5256 end if;
5258 Get_Next_Interp (I, It);
5259 end loop;
5261 if No (Nam) then
5262 raise Program_Error;
5263 end if;
5264 end if;
5266 -- If the prefix is not an entity, then resolve it
5268 if not Is_Entity_Name (Subp) then
5269 Resolve (Subp, Nam);
5270 end if;
5272 -- For an indirect call, we always invalidate checks, since we do not
5273 -- know whether the subprogram is local or global. Yes we could do
5274 -- better here, e.g. by knowing that there are no local subprograms,
5275 -- but it does not seem worth the effort. Similarly, we kill all
5276 -- knowledge of current constant values.
5278 Kill_Current_Values;
5280 -- If this is a procedure call which is really an entry call, do
5281 -- the conversion of the procedure call to an entry call. Protected
5282 -- operations use the same circuitry because the name in the call
5283 -- can be an arbitrary expression with special resolution rules.
5285 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
5286 or else (Is_Entity_Name (Subp)
5287 and then Ekind (Entity (Subp)) = E_Entry)
5288 then
5289 Resolve_Entry_Call (N, Typ);
5290 Check_Elab_Call (N);
5292 -- Kill checks and constant values, as above for indirect case
5293 -- Who knows what happens when another task is activated?
5295 Kill_Current_Values;
5296 return;
5298 -- Normal subprogram call with name established in Resolve
5300 elsif not (Is_Type (Entity (Subp))) then
5301 Nam := Entity (Subp);
5302 Set_Entity_With_Style_Check (Subp, Nam);
5304 -- Otherwise we must have the case of an overloaded call
5306 else
5307 pragma Assert (Is_Overloaded (Subp));
5309 -- Initialize Nam to prevent warning (we know it will be assigned
5310 -- in the loop below, but the compiler does not know that).
5312 Nam := Empty;
5314 Get_First_Interp (Subp, I, It);
5315 while Present (It.Typ) loop
5316 if Covers (Typ, It.Typ) then
5317 Nam := It.Nam;
5318 Set_Entity_With_Style_Check (Subp, Nam);
5319 exit;
5320 end if;
5322 Get_Next_Interp (I, It);
5323 end loop;
5324 end if;
5326 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
5327 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
5328 and then Nkind (Subp) /= N_Explicit_Dereference
5329 and then Present (Parameter_Associations (N))
5330 then
5331 -- The prefix is a parameterless function call that returns an access
5332 -- to subprogram. If parameters are present in the current call, add
5333 -- add an explicit dereference. We use the base type here because
5334 -- within an instance these may be subtypes.
5336 -- The dereference is added either in Analyze_Call or here. Should
5337 -- be consolidated ???
5339 Set_Is_Overloaded (Subp, False);
5340 Set_Etype (Subp, Etype (Nam));
5341 Insert_Explicit_Dereference (Subp);
5342 Nam := Designated_Type (Etype (Nam));
5343 Resolve (Subp, Nam);
5344 end if;
5346 -- Check that a call to Current_Task does not occur in an entry body
5348 if Is_RTE (Nam, RE_Current_Task) then
5349 declare
5350 P : Node_Id;
5352 begin
5353 P := N;
5354 loop
5355 P := Parent (P);
5357 -- Exclude calls that occur within the default of a formal
5358 -- parameter of the entry, since those are evaluated outside
5359 -- of the body.
5361 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
5363 if Nkind (P) = N_Entry_Body
5364 or else (Nkind (P) = N_Subprogram_Body
5365 and then Is_Entry_Barrier_Function (P))
5366 then
5367 Rtype := Etype (N);
5368 Error_Msg_NE
5369 ("??& should not be used in entry body (RM C.7(17))",
5370 N, Nam);
5371 Error_Msg_NE
5372 ("\Program_Error will be raised at run time??", N, Nam);
5373 Rewrite (N,
5374 Make_Raise_Program_Error (Loc,
5375 Reason => PE_Current_Task_In_Entry_Body));
5376 Set_Etype (N, Rtype);
5377 return;
5378 end if;
5379 end loop;
5380 end;
5381 end if;
5383 -- Check that a procedure call does not occur in the context of the
5384 -- entry call statement of a conditional or timed entry call. Note that
5385 -- the case of a call to a subprogram renaming of an entry will also be
5386 -- rejected. The test for N not being an N_Entry_Call_Statement is
5387 -- defensive, covering the possibility that the processing of entry
5388 -- calls might reach this point due to later modifications of the code
5389 -- above.
5391 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5392 and then Nkind (N) /= N_Entry_Call_Statement
5393 and then Entry_Call_Statement (Parent (N)) = N
5394 then
5395 if Ada_Version < Ada_2005 then
5396 Error_Msg_N ("entry call required in select statement", N);
5398 -- Ada 2005 (AI-345): If a procedure_call_statement is used
5399 -- for a procedure_or_entry_call, the procedure_name or
5400 -- procedure_prefix of the procedure_call_statement shall denote
5401 -- an entry renamed by a procedure, or (a view of) a primitive
5402 -- subprogram of a limited interface whose first parameter is
5403 -- a controlling parameter.
5405 elsif Nkind (N) = N_Procedure_Call_Statement
5406 and then not Is_Renamed_Entry (Nam)
5407 and then not Is_Controlling_Limited_Procedure (Nam)
5408 then
5409 Error_Msg_N
5410 ("entry call or dispatching primitive of interface required", N);
5411 end if;
5412 end if;
5414 -- Check that this is not a call to a protected procedure or entry from
5415 -- within a protected function.
5417 Check_Internal_Protected_Use (N, Nam);
5419 -- Freeze the subprogram name if not in a spec-expression. Note that
5420 -- we freeze procedure calls as well as function calls. Procedure calls
5421 -- are not frozen according to the rules (RM 13.14(14)) because it is
5422 -- impossible to have a procedure call to a non-frozen procedure in
5423 -- pure Ada, but in the code that we generate in the expander, this
5424 -- rule needs extending because we can generate procedure calls that
5425 -- need freezing.
5427 -- In Ada 2012, expression functions may be called within pre/post
5428 -- conditions of subsequent functions or expression functions. Such
5429 -- calls do not freeze when they appear within generated bodies,
5430 -- (including the body of another expression function) which would
5431 -- place the freeze node in the wrong scope. An expression function
5432 -- is frozen in the usual fashion, by the appearance of a real body,
5433 -- or at the end of a declarative part.
5435 if Is_Entity_Name (Subp) and then not In_Spec_Expression
5436 and then not Is_Expression_Function (Current_Scope)
5437 and then
5438 (not Is_Expression_Function (Entity (Subp))
5439 or else Scope (Entity (Subp)) = Current_Scope)
5440 then
5441 Freeze_Expression (Subp);
5442 end if;
5444 -- For a predefined operator, the type of the result is the type imposed
5445 -- by context, except for a predefined operation on universal fixed.
5446 -- Otherwise The type of the call is the type returned by the subprogram
5447 -- being called.
5449 if Is_Predefined_Op (Nam) then
5450 if Etype (N) /= Universal_Fixed then
5451 Set_Etype (N, Typ);
5452 end if;
5454 -- If the subprogram returns an array type, and the context requires the
5455 -- component type of that array type, the node is really an indexing of
5456 -- the parameterless call. Resolve as such. A pathological case occurs
5457 -- when the type of the component is an access to the array type. In
5458 -- this case the call is truly ambiguous.
5460 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
5461 and then
5462 ((Is_Array_Type (Etype (Nam))
5463 and then Covers (Typ, Component_Type (Etype (Nam))))
5464 or else (Is_Access_Type (Etype (Nam))
5465 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5466 and then
5467 Covers
5468 (Typ,
5469 Component_Type (Designated_Type (Etype (Nam))))))
5470 then
5471 declare
5472 Index_Node : Node_Id;
5473 New_Subp : Node_Id;
5474 Ret_Type : constant Entity_Id := Etype (Nam);
5476 begin
5477 if Is_Access_Type (Ret_Type)
5478 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
5479 then
5480 Error_Msg_N
5481 ("cannot disambiguate function call and indexing", N);
5482 else
5483 New_Subp := Relocate_Node (Subp);
5485 -- The called entity may be an explicit dereference, in which
5486 -- case there is no entity to set.
5488 if Nkind (New_Subp) /= N_Explicit_Dereference then
5489 Set_Entity (Subp, Nam);
5490 end if;
5492 if (Is_Array_Type (Ret_Type)
5493 and then Component_Type (Ret_Type) /= Any_Type)
5494 or else
5495 (Is_Access_Type (Ret_Type)
5496 and then
5497 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
5498 then
5499 if Needs_No_Actuals (Nam) then
5501 -- Indexed call to a parameterless function
5503 Index_Node :=
5504 Make_Indexed_Component (Loc,
5505 Prefix =>
5506 Make_Function_Call (Loc,
5507 Name => New_Subp),
5508 Expressions => Parameter_Associations (N));
5509 else
5510 -- An Ada 2005 prefixed call to a primitive operation
5511 -- whose first parameter is the prefix. This prefix was
5512 -- prepended to the parameter list, which is actually a
5513 -- list of indexes. Remove the prefix in order to build
5514 -- the proper indexed component.
5516 Index_Node :=
5517 Make_Indexed_Component (Loc,
5518 Prefix =>
5519 Make_Function_Call (Loc,
5520 Name => New_Subp,
5521 Parameter_Associations =>
5522 New_List
5523 (Remove_Head (Parameter_Associations (N)))),
5524 Expressions => Parameter_Associations (N));
5525 end if;
5527 -- Preserve the parenthesis count of the node
5529 Set_Paren_Count (Index_Node, Paren_Count (N));
5531 -- Since we are correcting a node classification error made
5532 -- by the parser, we call Replace rather than Rewrite.
5534 Replace (N, Index_Node);
5536 Set_Etype (Prefix (N), Ret_Type);
5537 Set_Etype (N, Typ);
5538 Resolve_Indexed_Component (N, Typ);
5539 Check_Elab_Call (Prefix (N));
5540 end if;
5541 end if;
5543 return;
5544 end;
5546 else
5547 Set_Etype (N, Etype (Nam));
5548 end if;
5550 -- In the case where the call is to an overloaded subprogram, Analyze
5551 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
5552 -- such a case Normalize_Actuals needs to be called once more to order
5553 -- the actuals correctly. Otherwise the call will have the ordering
5554 -- given by the last overloaded subprogram whether this is the correct
5555 -- one being called or not.
5557 if Is_Overloaded (Subp) then
5558 Normalize_Actuals (N, Nam, False, Norm_OK);
5559 pragma Assert (Norm_OK);
5560 end if;
5562 -- In any case, call is fully resolved now. Reset Overload flag, to
5563 -- prevent subsequent overload resolution if node is analyzed again
5565 Set_Is_Overloaded (Subp, False);
5566 Set_Is_Overloaded (N, False);
5568 -- If we are calling the current subprogram from immediately within its
5569 -- body, then that is the case where we can sometimes detect cases of
5570 -- infinite recursion statically. Do not try this in case restriction
5571 -- No_Recursion is in effect anyway, and do it only for source calls.
5573 if Comes_From_Source (N) then
5574 Scop := Current_Scope;
5576 -- Issue warning for possible infinite recursion in the absence
5577 -- of the No_Recursion restriction.
5579 if Same_Or_Aliased_Subprograms (Nam, Scop)
5580 and then not Restriction_Active (No_Recursion)
5581 and then Check_Infinite_Recursion (N)
5582 then
5583 -- Here we detected and flagged an infinite recursion, so we do
5584 -- not need to test the case below for further warnings. Also we
5585 -- are all done if we now have a raise SE node.
5587 if Nkind (N) = N_Raise_Storage_Error then
5588 return;
5589 end if;
5591 -- If call is to immediately containing subprogram, then check for
5592 -- the case of a possible run-time detectable infinite recursion.
5594 else
5595 Scope_Loop : while Scop /= Standard_Standard loop
5596 if Same_Or_Aliased_Subprograms (Nam, Scop) then
5598 -- Although in general case, recursion is not statically
5599 -- checkable, the case of calling an immediately containing
5600 -- subprogram is easy to catch.
5602 Check_Restriction (No_Recursion, N);
5604 -- If the recursive call is to a parameterless subprogram,
5605 -- then even if we can't statically detect infinite
5606 -- recursion, this is pretty suspicious, and we output a
5607 -- warning. Furthermore, we will try later to detect some
5608 -- cases here at run time by expanding checking code (see
5609 -- Detect_Infinite_Recursion in package Exp_Ch6).
5611 -- If the recursive call is within a handler, do not emit a
5612 -- warning, because this is a common idiom: loop until input
5613 -- is correct, catch illegal input in handler and restart.
5615 if No (First_Formal (Nam))
5616 and then Etype (Nam) = Standard_Void_Type
5617 and then not Error_Posted (N)
5618 and then Nkind (Parent (N)) /= N_Exception_Handler
5619 then
5620 -- For the case of a procedure call. We give the message
5621 -- only if the call is the first statement in a sequence
5622 -- of statements, or if all previous statements are
5623 -- simple assignments. This is simply a heuristic to
5624 -- decrease false positives, without losing too many good
5625 -- warnings. The idea is that these previous statements
5626 -- may affect global variables the procedure depends on.
5627 -- We also exclude raise statements, that may arise from
5628 -- constraint checks and are probably unrelated to the
5629 -- intended control flow.
5631 if Nkind (N) = N_Procedure_Call_Statement
5632 and then Is_List_Member (N)
5633 then
5634 declare
5635 P : Node_Id;
5636 begin
5637 P := Prev (N);
5638 while Present (P) loop
5639 if not Nkind_In (P,
5640 N_Assignment_Statement,
5641 N_Raise_Constraint_Error)
5642 then
5643 exit Scope_Loop;
5644 end if;
5646 Prev (P);
5647 end loop;
5648 end;
5649 end if;
5651 -- Do not give warning if we are in a conditional context
5653 declare
5654 K : constant Node_Kind := Nkind (Parent (N));
5655 begin
5656 if (K = N_Loop_Statement
5657 and then Present (Iteration_Scheme (Parent (N))))
5658 or else K = N_If_Statement
5659 or else K = N_Elsif_Part
5660 or else K = N_Case_Statement_Alternative
5661 then
5662 exit Scope_Loop;
5663 end if;
5664 end;
5666 -- Here warning is to be issued
5668 Set_Has_Recursive_Call (Nam);
5669 Error_Msg_N
5670 ("??possible infinite recursion!", N);
5671 Error_Msg_N
5672 ("\??Storage_Error may be raised at run time!", N);
5673 end if;
5675 exit Scope_Loop;
5676 end if;
5678 Scop := Scope (Scop);
5679 end loop Scope_Loop;
5680 end if;
5681 end if;
5683 -- Check obsolescent reference to Ada.Characters.Handling subprogram
5685 Check_Obsolescent_2005_Entity (Nam, Subp);
5687 -- If subprogram name is a predefined operator, it was given in
5688 -- functional notation. Replace call node with operator node, so
5689 -- that actuals can be resolved appropriately.
5691 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5692 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5693 return;
5695 elsif Present (Alias (Nam))
5696 and then Is_Predefined_Op (Alias (Nam))
5697 then
5698 Resolve_Actuals (N, Nam);
5699 Make_Call_Into_Operator (N, Typ, Alias (Nam));
5700 return;
5701 end if;
5703 -- Create a transient scope if the resulting type requires it
5705 -- There are several notable exceptions:
5707 -- a) In init procs, the transient scope overhead is not needed, and is
5708 -- even incorrect when the call is a nested initialization call for a
5709 -- component whose expansion may generate adjust calls. However, if the
5710 -- call is some other procedure call within an initialization procedure
5711 -- (for example a call to Create_Task in the init_proc of the task
5712 -- run-time record) a transient scope must be created around this call.
5714 -- b) Enumeration literal pseudo-calls need no transient scope
5716 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
5717 -- functions) do not use the secondary stack even though the return
5718 -- type may be unconstrained.
5720 -- d) Calls to a build-in-place function, since such functions may
5721 -- allocate their result directly in a target object, and cases where
5722 -- the result does get allocated in the secondary stack are checked for
5723 -- within the specialized Exp_Ch6 procedures for expanding those
5724 -- build-in-place calls.
5726 -- e) If the subprogram is marked Inline_Always, then even if it returns
5727 -- an unconstrained type the call does not require use of the secondary
5728 -- stack. However, inlining will only take place if the body to inline
5729 -- is already present. It may not be available if e.g. the subprogram is
5730 -- declared in a child instance.
5732 -- If this is an initialization call for a type whose construction
5733 -- uses the secondary stack, and it is not a nested call to initialize
5734 -- a component, we do need to create a transient scope for it. We
5735 -- check for this by traversing the type in Check_Initialization_Call.
5737 if Is_Inlined (Nam)
5738 and then Has_Pragma_Inline_Always (Nam)
5739 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5740 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5741 and then not Debug_Flag_Dot_K
5742 then
5743 null;
5745 elsif Is_Inlined (Nam)
5746 and then Has_Pragma_Inline (Nam)
5747 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5748 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5749 and then Debug_Flag_Dot_K
5750 then
5751 null;
5753 elsif Ekind (Nam) = E_Enumeration_Literal
5754 or else Is_Build_In_Place_Function (Nam)
5755 or else Is_Intrinsic_Subprogram (Nam)
5756 then
5757 null;
5759 elsif Full_Expander_Active
5760 and then Is_Type (Etype (Nam))
5761 and then Requires_Transient_Scope (Etype (Nam))
5762 and then
5763 (not Within_Init_Proc
5764 or else
5765 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
5766 then
5767 Establish_Transient_Scope (N, Sec_Stack => True);
5769 -- If the call appears within the bounds of a loop, it will
5770 -- be rewritten and reanalyzed, nothing left to do here.
5772 if Nkind (N) /= N_Function_Call then
5773 return;
5774 end if;
5776 elsif Is_Init_Proc (Nam)
5777 and then not Within_Init_Proc
5778 then
5779 Check_Initialization_Call (N, Nam);
5780 end if;
5782 -- A protected function cannot be called within the definition of the
5783 -- enclosing protected type.
5785 if Is_Protected_Type (Scope (Nam))
5786 and then In_Open_Scopes (Scope (Nam))
5787 and then not Has_Completion (Scope (Nam))
5788 then
5789 Error_Msg_NE
5790 ("& cannot be called before end of protected definition", N, Nam);
5791 end if;
5793 -- Propagate interpretation to actuals, and add default expressions
5794 -- where needed.
5796 if Present (First_Formal (Nam)) then
5797 Resolve_Actuals (N, Nam);
5799 -- Overloaded literals are rewritten as function calls, for purpose of
5800 -- resolution. After resolution, we can replace the call with the
5801 -- literal itself.
5803 elsif Ekind (Nam) = E_Enumeration_Literal then
5804 Copy_Node (Subp, N);
5805 Resolve_Entity_Name (N, Typ);
5807 -- Avoid validation, since it is a static function call
5809 Generate_Reference (Nam, Subp);
5810 return;
5811 end if;
5813 -- If the subprogram is not global, then kill all saved values and
5814 -- checks. This is a bit conservative, since in many cases we could do
5815 -- better, but it is not worth the effort. Similarly, we kill constant
5816 -- values. However we do not need to do this for internal entities
5817 -- (unless they are inherited user-defined subprograms), since they
5818 -- are not in the business of molesting local values.
5820 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
5821 -- kill all checks and values for calls to global subprograms. This
5822 -- takes care of the case where an access to a local subprogram is
5823 -- taken, and could be passed directly or indirectly and then called
5824 -- from almost any context.
5826 -- Note: we do not do this step till after resolving the actuals. That
5827 -- way we still take advantage of the current value information while
5828 -- scanning the actuals.
5830 -- We suppress killing values if we are processing the nodes associated
5831 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
5832 -- type kills all the values as part of analyzing the code that
5833 -- initializes the dispatch tables.
5835 if Inside_Freezing_Actions = 0
5836 and then (not Is_Library_Level_Entity (Nam)
5837 or else Suppress_Value_Tracking_On_Call
5838 (Nearest_Dynamic_Scope (Current_Scope)))
5839 and then (Comes_From_Source (Nam)
5840 or else (Present (Alias (Nam))
5841 and then Comes_From_Source (Alias (Nam))))
5842 then
5843 Kill_Current_Values;
5844 end if;
5846 -- If we are warning about unread OUT parameters, this is the place to
5847 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
5848 -- after the above call to Kill_Current_Values (since that call clears
5849 -- the Last_Assignment field of all local variables).
5851 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
5852 and then Comes_From_Source (N)
5853 and then In_Extended_Main_Source_Unit (N)
5854 then
5855 declare
5856 F : Entity_Id;
5857 A : Node_Id;
5859 begin
5860 F := First_Formal (Nam);
5861 A := First_Actual (N);
5862 while Present (F) and then Present (A) loop
5863 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
5864 and then Warn_On_Modified_As_Out_Parameter (F)
5865 and then Is_Entity_Name (A)
5866 and then Present (Entity (A))
5867 and then Comes_From_Source (N)
5868 and then Safe_To_Capture_Value (N, Entity (A))
5869 then
5870 Set_Last_Assignment (Entity (A), A);
5871 end if;
5873 Next_Formal (F);
5874 Next_Actual (A);
5875 end loop;
5876 end;
5877 end if;
5879 -- If the subprogram is a primitive operation, check whether or not
5880 -- it is a correct dispatching call.
5882 if Is_Overloadable (Nam)
5883 and then Is_Dispatching_Operation (Nam)
5884 then
5885 Check_Dispatching_Call (N);
5887 elsif Ekind (Nam) /= E_Subprogram_Type
5888 and then Is_Abstract_Subprogram (Nam)
5889 and then not In_Instance
5890 then
5891 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
5892 end if;
5894 -- If this is a dispatching call, generate the appropriate reference,
5895 -- for better source navigation in GPS.
5897 if Is_Overloadable (Nam)
5898 and then Present (Controlling_Argument (N))
5899 then
5900 Generate_Reference (Nam, Subp, 'R');
5902 -- Normal case, not a dispatching call: generate a call reference
5904 else
5905 Generate_Reference (Nam, Subp, 's');
5906 end if;
5908 if Is_Intrinsic_Subprogram (Nam) then
5909 Check_Intrinsic_Call (N);
5910 end if;
5912 -- Check for violation of restriction No_Specific_Termination_Handlers
5913 -- and warn on a potentially blocking call to Abort_Task.
5915 if Restriction_Check_Required (No_Specific_Termination_Handlers)
5916 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
5917 or else
5918 Is_RTE (Nam, RE_Specific_Handler))
5919 then
5920 Check_Restriction (No_Specific_Termination_Handlers, N);
5922 elsif Is_RTE (Nam, RE_Abort_Task) then
5923 Check_Potentially_Blocking_Operation (N);
5924 end if;
5926 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
5927 -- timing event violates restriction No_Relative_Delay (AI-0211). We
5928 -- need to check the second argument to determine whether it is an
5929 -- absolute or relative timing event.
5931 if Restriction_Check_Required (No_Relative_Delay)
5932 and then Is_RTE (Nam, RE_Set_Handler)
5933 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
5934 then
5935 Check_Restriction (No_Relative_Delay, N);
5936 end if;
5938 -- Issue an error for a call to an eliminated subprogram. This routine
5939 -- will not perform the check if the call appears within a default
5940 -- expression.
5942 Check_For_Eliminated_Subprogram (Subp, Nam);
5944 -- In formal mode, the primitive operations of a tagged type or type
5945 -- extension do not include functions that return the tagged type.
5947 if Nkind (N) = N_Function_Call
5948 and then Is_Tagged_Type (Etype (N))
5949 and then Is_Entity_Name (Name (N))
5950 and then Is_Inherited_Operation_For_Type (Entity (Name (N)), Etype (N))
5951 then
5952 Check_SPARK_Restriction ("function not inherited", N);
5953 end if;
5955 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
5956 -- class-wide and the call dispatches on result in a context that does
5957 -- not provide a tag, the call raises Program_Error.
5959 if Nkind (N) = N_Function_Call
5960 and then In_Instance
5961 and then Is_Generic_Actual_Type (Typ)
5962 and then Is_Class_Wide_Type (Typ)
5963 and then Has_Controlling_Result (Nam)
5964 and then Nkind (Parent (N)) = N_Object_Declaration
5965 then
5966 -- Verify that none of the formals are controlling
5968 declare
5969 Call_OK : Boolean := False;
5970 F : Entity_Id;
5972 begin
5973 F := First_Formal (Nam);
5974 while Present (F) loop
5975 if Is_Controlling_Formal (F) then
5976 Call_OK := True;
5977 exit;
5978 end if;
5980 Next_Formal (F);
5981 end loop;
5983 if not Call_OK then
5984 Error_Msg_N ("!?? cannot determine tag of result", N);
5985 Error_Msg_N ("!?? Program_Error will be raised", N);
5986 Insert_Action (N,
5987 Make_Raise_Program_Error (Sloc (N),
5988 Reason => PE_Explicit_Raise));
5989 end if;
5990 end;
5991 end if;
5993 -- Check the dimensions of the actuals in the call. For function calls,
5994 -- propagate the dimensions from the returned type to N.
5996 Analyze_Dimension_Call (N, Nam);
5998 -- All done, evaluate call and deal with elaboration issues
6000 Eval_Call (N);
6001 Check_Elab_Call (N);
6002 Warn_On_Overlapping_Actuals (Nam, N);
6003 end Resolve_Call;
6005 -----------------------------
6006 -- Resolve_Case_Expression --
6007 -----------------------------
6009 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
6010 Alt : Node_Id;
6012 begin
6013 Alt := First (Alternatives (N));
6014 while Present (Alt) loop
6015 Resolve (Expression (Alt), Typ);
6016 Next (Alt);
6017 end loop;
6019 Set_Etype (N, Typ);
6020 Eval_Case_Expression (N);
6021 end Resolve_Case_Expression;
6023 -------------------------------
6024 -- Resolve_Character_Literal --
6025 -------------------------------
6027 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
6028 B_Typ : constant Entity_Id := Base_Type (Typ);
6029 C : Entity_Id;
6031 begin
6032 -- Verify that the character does belong to the type of the context
6034 Set_Etype (N, B_Typ);
6035 Eval_Character_Literal (N);
6037 -- Wide_Wide_Character literals must always be defined, since the set
6038 -- of wide wide character literals is complete, i.e. if a character
6039 -- literal is accepted by the parser, then it is OK for wide wide
6040 -- character (out of range character literals are rejected).
6042 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
6043 return;
6045 -- Always accept character literal for type Any_Character, which
6046 -- occurs in error situations and in comparisons of literals, both
6047 -- of which should accept all literals.
6049 elsif B_Typ = Any_Character then
6050 return;
6052 -- For Standard.Character or a type derived from it, check that the
6053 -- literal is in range.
6055 elsif Root_Type (B_Typ) = Standard_Character then
6056 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
6057 return;
6058 end if;
6060 -- For Standard.Wide_Character or a type derived from it, check that the
6061 -- literal is in range.
6063 elsif Root_Type (B_Typ) = Standard_Wide_Character then
6064 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
6065 return;
6066 end if;
6068 -- For Standard.Wide_Wide_Character or a type derived from it, we
6069 -- know the literal is in range, since the parser checked!
6071 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
6072 return;
6074 -- If the entity is already set, this has already been resolved in a
6075 -- generic context, or comes from expansion. Nothing else to do.
6077 elsif Present (Entity (N)) then
6078 return;
6080 -- Otherwise we have a user defined character type, and we can use the
6081 -- standard visibility mechanisms to locate the referenced entity.
6083 else
6084 C := Current_Entity (N);
6085 while Present (C) loop
6086 if Etype (C) = B_Typ then
6087 Set_Entity_With_Style_Check (N, C);
6088 Generate_Reference (C, N);
6089 return;
6090 end if;
6092 C := Homonym (C);
6093 end loop;
6094 end if;
6096 -- If we fall through, then the literal does not match any of the
6097 -- entries of the enumeration type. This isn't just a constraint error
6098 -- situation, it is an illegality (see RM 4.2).
6100 Error_Msg_NE
6101 ("character not defined for }", N, First_Subtype (B_Typ));
6102 end Resolve_Character_Literal;
6104 ---------------------------
6105 -- Resolve_Comparison_Op --
6106 ---------------------------
6108 -- Context requires a boolean type, and plays no role in resolution.
6109 -- Processing identical to that for equality operators. The result type is
6110 -- the base type, which matters when pathological subtypes of booleans with
6111 -- limited ranges are used.
6113 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
6114 L : constant Node_Id := Left_Opnd (N);
6115 R : constant Node_Id := Right_Opnd (N);
6116 T : Entity_Id;
6118 begin
6119 -- If this is an intrinsic operation which is not predefined, use the
6120 -- types of its declared arguments to resolve the possibly overloaded
6121 -- operands. Otherwise the operands are unambiguous and specify the
6122 -- expected type.
6124 if Scope (Entity (N)) /= Standard_Standard then
6125 T := Etype (First_Entity (Entity (N)));
6127 else
6128 T := Find_Unique_Type (L, R);
6130 if T = Any_Fixed then
6131 T := Unique_Fixed_Point_Type (L);
6132 end if;
6133 end if;
6135 Set_Etype (N, Base_Type (Typ));
6136 Generate_Reference (T, N, ' ');
6138 -- Skip remaining processing if already set to Any_Type
6140 if T = Any_Type then
6141 return;
6142 end if;
6144 -- Deal with other error cases
6146 if T = Any_String or else
6147 T = Any_Composite or else
6148 T = Any_Character
6149 then
6150 if T = Any_Character then
6151 Ambiguous_Character (L);
6152 else
6153 Error_Msg_N ("ambiguous operands for comparison", N);
6154 end if;
6156 Set_Etype (N, Any_Type);
6157 return;
6158 end if;
6160 -- Resolve the operands if types OK
6162 Resolve (L, T);
6163 Resolve (R, T);
6164 Check_Unset_Reference (L);
6165 Check_Unset_Reference (R);
6166 Generate_Operator_Reference (N, T);
6167 Check_Low_Bound_Tested (N);
6169 -- In SPARK, ordering operators <, <=, >, >= are not defined for Boolean
6170 -- types or array types except String.
6172 if Is_Boolean_Type (T) then
6173 Check_SPARK_Restriction
6174 ("comparison is not defined on Boolean type", N);
6176 elsif Is_Array_Type (T)
6177 and then Base_Type (T) /= Standard_String
6178 then
6179 Check_SPARK_Restriction
6180 ("comparison is not defined on array types other than String", N);
6181 end if;
6183 -- Check comparison on unordered enumeration
6185 if Bad_Unordered_Enumeration_Reference (N, Etype (L)) then
6186 Error_Msg_N ("comparison on unordered enumeration type?U?", N);
6187 end if;
6189 -- Evaluate the relation (note we do this after the above check since
6190 -- this Eval call may change N to True/False.
6192 Analyze_Dimension (N);
6193 Eval_Relational_Op (N);
6194 end Resolve_Comparison_Op;
6196 -----------------------------------------
6197 -- Resolve_Discrete_Subtype_Indication --
6198 -----------------------------------------
6200 procedure Resolve_Discrete_Subtype_Indication
6201 (N : Node_Id;
6202 Typ : Entity_Id)
6204 R : Node_Id;
6205 S : Entity_Id;
6207 begin
6208 Analyze (Subtype_Mark (N));
6209 S := Entity (Subtype_Mark (N));
6211 if Nkind (Constraint (N)) /= N_Range_Constraint then
6212 Error_Msg_N ("expect range constraint for discrete type", N);
6213 Set_Etype (N, Any_Type);
6215 else
6216 R := Range_Expression (Constraint (N));
6218 if R = Error then
6219 return;
6220 end if;
6222 Analyze (R);
6224 if Base_Type (S) /= Base_Type (Typ) then
6225 Error_Msg_NE
6226 ("expect subtype of }", N, First_Subtype (Typ));
6228 -- Rewrite the constraint as a range of Typ
6229 -- to allow compilation to proceed further.
6231 Set_Etype (N, Typ);
6232 Rewrite (Low_Bound (R),
6233 Make_Attribute_Reference (Sloc (Low_Bound (R)),
6234 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6235 Attribute_Name => Name_First));
6236 Rewrite (High_Bound (R),
6237 Make_Attribute_Reference (Sloc (High_Bound (R)),
6238 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6239 Attribute_Name => Name_First));
6241 else
6242 Resolve (R, Typ);
6243 Set_Etype (N, Etype (R));
6245 -- Additionally, we must check that the bounds are compatible
6246 -- with the given subtype, which might be different from the
6247 -- type of the context.
6249 Apply_Range_Check (R, S);
6251 -- ??? If the above check statically detects a Constraint_Error
6252 -- it replaces the offending bound(s) of the range R with a
6253 -- Constraint_Error node. When the itype which uses these bounds
6254 -- is frozen the resulting call to Duplicate_Subexpr generates
6255 -- a new temporary for the bounds.
6257 -- Unfortunately there are other itypes that are also made depend
6258 -- on these bounds, so when Duplicate_Subexpr is called they get
6259 -- a forward reference to the newly created temporaries and Gigi
6260 -- aborts on such forward references. This is probably sign of a
6261 -- more fundamental problem somewhere else in either the order of
6262 -- itype freezing or the way certain itypes are constructed.
6264 -- To get around this problem we call Remove_Side_Effects right
6265 -- away if either bounds of R are a Constraint_Error.
6267 declare
6268 L : constant Node_Id := Low_Bound (R);
6269 H : constant Node_Id := High_Bound (R);
6271 begin
6272 if Nkind (L) = N_Raise_Constraint_Error then
6273 Remove_Side_Effects (L);
6274 end if;
6276 if Nkind (H) = N_Raise_Constraint_Error then
6277 Remove_Side_Effects (H);
6278 end if;
6279 end;
6281 Check_Unset_Reference (Low_Bound (R));
6282 Check_Unset_Reference (High_Bound (R));
6283 end if;
6284 end if;
6285 end Resolve_Discrete_Subtype_Indication;
6287 -------------------------
6288 -- Resolve_Entity_Name --
6289 -------------------------
6291 -- Used to resolve identifiers and expanded names
6293 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
6294 E : constant Entity_Id := Entity (N);
6296 begin
6297 -- If garbage from errors, set to Any_Type and return
6299 if No (E) and then Total_Errors_Detected /= 0 then
6300 Set_Etype (N, Any_Type);
6301 return;
6302 end if;
6304 -- Replace named numbers by corresponding literals. Note that this is
6305 -- the one case where Resolve_Entity_Name must reset the Etype, since
6306 -- it is currently marked as universal.
6308 if Ekind (E) = E_Named_Integer then
6309 Set_Etype (N, Typ);
6310 Eval_Named_Integer (N);
6312 elsif Ekind (E) = E_Named_Real then
6313 Set_Etype (N, Typ);
6314 Eval_Named_Real (N);
6316 -- For enumeration literals, we need to make sure that a proper style
6317 -- check is done, since such literals are overloaded, and thus we did
6318 -- not do a style check during the first phase of analysis.
6320 elsif Ekind (E) = E_Enumeration_Literal then
6321 Set_Entity_With_Style_Check (N, E);
6322 Eval_Entity_Name (N);
6324 -- Case of subtype name appearing as an operand in expression
6326 elsif Is_Type (E) then
6328 -- Allow use of subtype if it is a concurrent type where we are
6329 -- currently inside the body. This will eventually be expanded into a
6330 -- call to Self (for tasks) or _object (for protected objects). Any
6331 -- other use of a subtype is invalid.
6333 if Is_Concurrent_Type (E)
6334 and then In_Open_Scopes (E)
6335 then
6336 null;
6338 -- Any other use is an error
6340 else
6341 Error_Msg_N
6342 ("invalid use of subtype mark in expression or call", N);
6343 end if;
6345 -- Check discriminant use if entity is discriminant in current scope,
6346 -- i.e. discriminant of record or concurrent type currently being
6347 -- analyzed. Uses in corresponding body are unrestricted.
6349 elsif Ekind (E) = E_Discriminant
6350 and then Scope (E) = Current_Scope
6351 and then not Has_Completion (Current_Scope)
6352 then
6353 Check_Discriminant_Use (N);
6355 -- A parameterless generic function cannot appear in a context that
6356 -- requires resolution.
6358 elsif Ekind (E) = E_Generic_Function then
6359 Error_Msg_N ("illegal use of generic function", N);
6361 elsif Ekind (E) = E_Out_Parameter
6362 and then Ada_Version = Ada_83
6363 and then (Nkind (Parent (N)) in N_Op
6364 or else (Nkind (Parent (N)) = N_Assignment_Statement
6365 and then N = Expression (Parent (N)))
6366 or else Nkind (Parent (N)) = N_Explicit_Dereference)
6367 then
6368 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
6370 -- In all other cases, just do the possible static evaluation
6372 else
6373 -- A deferred constant that appears in an expression must have a
6374 -- completion, unless it has been removed by in-place expansion of
6375 -- an aggregate.
6377 if Ekind (E) = E_Constant
6378 and then Comes_From_Source (E)
6379 and then No (Constant_Value (E))
6380 and then Is_Frozen (Etype (E))
6381 and then not In_Spec_Expression
6382 and then not Is_Imported (E)
6383 then
6384 if No_Initialization (Parent (E))
6385 or else (Present (Full_View (E))
6386 and then No_Initialization (Parent (Full_View (E))))
6387 then
6388 null;
6389 else
6390 Error_Msg_N (
6391 "deferred constant is frozen before completion", N);
6392 end if;
6393 end if;
6395 Eval_Entity_Name (N);
6396 end if;
6397 end Resolve_Entity_Name;
6399 -------------------
6400 -- Resolve_Entry --
6401 -------------------
6403 procedure Resolve_Entry (Entry_Name : Node_Id) is
6404 Loc : constant Source_Ptr := Sloc (Entry_Name);
6405 Nam : Entity_Id;
6406 New_N : Node_Id;
6407 S : Entity_Id;
6408 Tsk : Entity_Id;
6409 E_Name : Node_Id;
6410 Index : Node_Id;
6412 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
6413 -- If the bounds of the entry family being called depend on task
6414 -- discriminants, build a new index subtype where a discriminant is
6415 -- replaced with the value of the discriminant of the target task.
6416 -- The target task is the prefix of the entry name in the call.
6418 -----------------------
6419 -- Actual_Index_Type --
6420 -----------------------
6422 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
6423 Typ : constant Entity_Id := Entry_Index_Type (E);
6424 Tsk : constant Entity_Id := Scope (E);
6425 Lo : constant Node_Id := Type_Low_Bound (Typ);
6426 Hi : constant Node_Id := Type_High_Bound (Typ);
6427 New_T : Entity_Id;
6429 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
6430 -- If the bound is given by a discriminant, replace with a reference
6431 -- to the discriminant of the same name in the target task. If the
6432 -- entry name is the target of a requeue statement and the entry is
6433 -- in the current protected object, the bound to be used is the
6434 -- discriminal of the object (see Apply_Range_Checks for details of
6435 -- the transformation).
6437 -----------------------------
6438 -- Actual_Discriminant_Ref --
6439 -----------------------------
6441 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
6442 Typ : constant Entity_Id := Etype (Bound);
6443 Ref : Node_Id;
6445 begin
6446 Remove_Side_Effects (Bound);
6448 if not Is_Entity_Name (Bound)
6449 or else Ekind (Entity (Bound)) /= E_Discriminant
6450 then
6451 return Bound;
6453 elsif Is_Protected_Type (Tsk)
6454 and then In_Open_Scopes (Tsk)
6455 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
6456 then
6457 -- Note: here Bound denotes a discriminant of the corresponding
6458 -- record type tskV, whose discriminal is a formal of the
6459 -- init-proc tskVIP. What we want is the body discriminal,
6460 -- which is associated to the discriminant of the original
6461 -- concurrent type tsk.
6463 return New_Occurrence_Of
6464 (Find_Body_Discriminal (Entity (Bound)), Loc);
6466 else
6467 Ref :=
6468 Make_Selected_Component (Loc,
6469 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
6470 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
6471 Analyze (Ref);
6472 Resolve (Ref, Typ);
6473 return Ref;
6474 end if;
6475 end Actual_Discriminant_Ref;
6477 -- Start of processing for Actual_Index_Type
6479 begin
6480 if not Has_Discriminants (Tsk)
6481 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
6482 then
6483 return Entry_Index_Type (E);
6485 else
6486 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
6487 Set_Etype (New_T, Base_Type (Typ));
6488 Set_Size_Info (New_T, Typ);
6489 Set_RM_Size (New_T, RM_Size (Typ));
6490 Set_Scalar_Range (New_T,
6491 Make_Range (Sloc (Entry_Name),
6492 Low_Bound => Actual_Discriminant_Ref (Lo),
6493 High_Bound => Actual_Discriminant_Ref (Hi)));
6495 return New_T;
6496 end if;
6497 end Actual_Index_Type;
6499 -- Start of processing of Resolve_Entry
6501 begin
6502 -- Find name of entry being called, and resolve prefix of name with its
6503 -- own type. The prefix can be overloaded, and the name and signature of
6504 -- the entry must be taken into account.
6506 if Nkind (Entry_Name) = N_Indexed_Component then
6508 -- Case of dealing with entry family within the current tasks
6510 E_Name := Prefix (Entry_Name);
6512 else
6513 E_Name := Entry_Name;
6514 end if;
6516 if Is_Entity_Name (E_Name) then
6518 -- Entry call to an entry (or entry family) in the current task. This
6519 -- is legal even though the task will deadlock. Rewrite as call to
6520 -- current task.
6522 -- This can also be a call to an entry in an enclosing task. If this
6523 -- is a single task, we have to retrieve its name, because the scope
6524 -- of the entry is the task type, not the object. If the enclosing
6525 -- task is a task type, the identity of the task is given by its own
6526 -- self variable.
6528 -- Finally this can be a requeue on an entry of the same task or
6529 -- protected object.
6531 S := Scope (Entity (E_Name));
6533 for J in reverse 0 .. Scope_Stack.Last loop
6534 if Is_Task_Type (Scope_Stack.Table (J).Entity)
6535 and then not Comes_From_Source (S)
6536 then
6537 -- S is an enclosing task or protected object. The concurrent
6538 -- declaration has been converted into a type declaration, and
6539 -- the object itself has an object declaration that follows
6540 -- the type in the same declarative part.
6542 Tsk := Next_Entity (S);
6543 while Etype (Tsk) /= S loop
6544 Next_Entity (Tsk);
6545 end loop;
6547 S := Tsk;
6548 exit;
6550 elsif S = Scope_Stack.Table (J).Entity then
6552 -- Call to current task. Will be transformed into call to Self
6554 exit;
6556 end if;
6557 end loop;
6559 New_N :=
6560 Make_Selected_Component (Loc,
6561 Prefix => New_Occurrence_Of (S, Loc),
6562 Selector_Name =>
6563 New_Occurrence_Of (Entity (E_Name), Loc));
6564 Rewrite (E_Name, New_N);
6565 Analyze (E_Name);
6567 elsif Nkind (Entry_Name) = N_Selected_Component
6568 and then Is_Overloaded (Prefix (Entry_Name))
6569 then
6570 -- Use the entry name (which must be unique at this point) to find
6571 -- the prefix that returns the corresponding task/protected type.
6573 declare
6574 Pref : constant Node_Id := Prefix (Entry_Name);
6575 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
6576 I : Interp_Index;
6577 It : Interp;
6579 begin
6580 Get_First_Interp (Pref, I, It);
6581 while Present (It.Typ) loop
6582 if Scope (Ent) = It.Typ then
6583 Set_Etype (Pref, It.Typ);
6584 exit;
6585 end if;
6587 Get_Next_Interp (I, It);
6588 end loop;
6589 end;
6590 end if;
6592 if Nkind (Entry_Name) = N_Selected_Component then
6593 Resolve (Prefix (Entry_Name));
6595 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6596 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6597 Resolve (Prefix (Prefix (Entry_Name)));
6598 Index := First (Expressions (Entry_Name));
6599 Resolve (Index, Entry_Index_Type (Nam));
6601 -- Up to this point the expression could have been the actual in a
6602 -- simple entry call, and be given by a named association.
6604 if Nkind (Index) = N_Parameter_Association then
6605 Error_Msg_N ("expect expression for entry index", Index);
6606 else
6607 Apply_Range_Check (Index, Actual_Index_Type (Nam));
6608 end if;
6609 end if;
6610 end Resolve_Entry;
6612 ------------------------
6613 -- Resolve_Entry_Call --
6614 ------------------------
6616 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
6617 Entry_Name : constant Node_Id := Name (N);
6618 Loc : constant Source_Ptr := Sloc (Entry_Name);
6619 Actuals : List_Id;
6620 First_Named : Node_Id;
6621 Nam : Entity_Id;
6622 Norm_OK : Boolean;
6623 Obj : Node_Id;
6624 Was_Over : Boolean;
6626 begin
6627 -- We kill all checks here, because it does not seem worth the effort to
6628 -- do anything better, an entry call is a big operation.
6630 Kill_All_Checks;
6632 -- Processing of the name is similar for entry calls and protected
6633 -- operation calls. Once the entity is determined, we can complete
6634 -- the resolution of the actuals.
6636 -- The selector may be overloaded, in the case of a protected object
6637 -- with overloaded functions. The type of the context is used for
6638 -- resolution.
6640 if Nkind (Entry_Name) = N_Selected_Component
6641 and then Is_Overloaded (Selector_Name (Entry_Name))
6642 and then Typ /= Standard_Void_Type
6643 then
6644 declare
6645 I : Interp_Index;
6646 It : Interp;
6648 begin
6649 Get_First_Interp (Selector_Name (Entry_Name), I, It);
6650 while Present (It.Typ) loop
6651 if Covers (Typ, It.Typ) then
6652 Set_Entity (Selector_Name (Entry_Name), It.Nam);
6653 Set_Etype (Entry_Name, It.Typ);
6655 Generate_Reference (It.Typ, N, ' ');
6656 end if;
6658 Get_Next_Interp (I, It);
6659 end loop;
6660 end;
6661 end if;
6663 Resolve_Entry (Entry_Name);
6665 if Nkind (Entry_Name) = N_Selected_Component then
6667 -- Simple entry call
6669 Nam := Entity (Selector_Name (Entry_Name));
6670 Obj := Prefix (Entry_Name);
6671 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
6673 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6675 -- Call to member of entry family
6677 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6678 Obj := Prefix (Prefix (Entry_Name));
6679 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
6680 end if;
6682 -- We cannot in general check the maximum depth of protected entry calls
6683 -- at compile time. But we can tell that any protected entry call at all
6684 -- violates a specified nesting depth of zero.
6686 if Is_Protected_Type (Scope (Nam)) then
6687 Check_Restriction (Max_Entry_Queue_Length, N);
6688 end if;
6690 -- Use context type to disambiguate a protected function that can be
6691 -- called without actuals and that returns an array type, and where the
6692 -- argument list may be an indexing of the returned value.
6694 if Ekind (Nam) = E_Function
6695 and then Needs_No_Actuals (Nam)
6696 and then Present (Parameter_Associations (N))
6697 and then
6698 ((Is_Array_Type (Etype (Nam))
6699 and then Covers (Typ, Component_Type (Etype (Nam))))
6701 or else (Is_Access_Type (Etype (Nam))
6702 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6703 and then
6704 Covers
6705 (Typ,
6706 Component_Type (Designated_Type (Etype (Nam))))))
6707 then
6708 declare
6709 Index_Node : Node_Id;
6711 begin
6712 Index_Node :=
6713 Make_Indexed_Component (Loc,
6714 Prefix =>
6715 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
6716 Expressions => Parameter_Associations (N));
6718 -- Since we are correcting a node classification error made by the
6719 -- parser, we call Replace rather than Rewrite.
6721 Replace (N, Index_Node);
6722 Set_Etype (Prefix (N), Etype (Nam));
6723 Set_Etype (N, Typ);
6724 Resolve_Indexed_Component (N, Typ);
6725 return;
6726 end;
6727 end if;
6729 if Ekind_In (Nam, E_Entry, E_Entry_Family)
6730 and then Present (PPC_Wrapper (Nam))
6731 and then Current_Scope /= PPC_Wrapper (Nam)
6732 then
6733 -- Rewrite as call to the precondition wrapper, adding the task
6734 -- object to the list of actuals. If the call is to a member of an
6735 -- entry family, include the index as well.
6737 declare
6738 New_Call : Node_Id;
6739 New_Actuals : List_Id;
6741 begin
6742 New_Actuals := New_List (Obj);
6744 if Nkind (Entry_Name) = N_Indexed_Component then
6745 Append_To (New_Actuals,
6746 New_Copy_Tree (First (Expressions (Entry_Name))));
6747 end if;
6749 Append_List (Parameter_Associations (N), New_Actuals);
6750 New_Call :=
6751 Make_Procedure_Call_Statement (Loc,
6752 Name =>
6753 New_Occurrence_Of (PPC_Wrapper (Nam), Loc),
6754 Parameter_Associations => New_Actuals);
6755 Rewrite (N, New_Call);
6756 Analyze_And_Resolve (N);
6757 return;
6758 end;
6759 end if;
6761 -- The operation name may have been overloaded. Order the actuals
6762 -- according to the formals of the resolved entity, and set the return
6763 -- type to that of the operation.
6765 if Was_Over then
6766 Normalize_Actuals (N, Nam, False, Norm_OK);
6767 pragma Assert (Norm_OK);
6768 Set_Etype (N, Etype (Nam));
6769 end if;
6771 Resolve_Actuals (N, Nam);
6772 Check_Internal_Protected_Use (N, Nam);
6774 -- Create a call reference to the entry
6776 Generate_Reference (Nam, Entry_Name, 's');
6778 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
6779 Check_Potentially_Blocking_Operation (N);
6780 end if;
6782 -- Verify that a procedure call cannot masquerade as an entry
6783 -- call where an entry call is expected.
6785 if Ekind (Nam) = E_Procedure then
6786 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6787 and then N = Entry_Call_Statement (Parent (N))
6788 then
6789 Error_Msg_N ("entry call required in select statement", N);
6791 elsif Nkind (Parent (N)) = N_Triggering_Alternative
6792 and then N = Triggering_Statement (Parent (N))
6793 then
6794 Error_Msg_N ("triggering statement cannot be procedure call", N);
6796 elsif Ekind (Scope (Nam)) = E_Task_Type
6797 and then not In_Open_Scopes (Scope (Nam))
6798 then
6799 Error_Msg_N ("task has no entry with this name", Entry_Name);
6800 end if;
6801 end if;
6803 -- After resolution, entry calls and protected procedure calls are
6804 -- changed into entry calls, for expansion. The structure of the node
6805 -- does not change, so it can safely be done in place. Protected
6806 -- function calls must keep their structure because they are
6807 -- subexpressions.
6809 if Ekind (Nam) /= E_Function then
6811 -- A protected operation that is not a function may modify the
6812 -- corresponding object, and cannot apply to a constant. If this
6813 -- is an internal call, the prefix is the type itself.
6815 if Is_Protected_Type (Scope (Nam))
6816 and then not Is_Variable (Obj)
6817 and then (not Is_Entity_Name (Obj)
6818 or else not Is_Type (Entity (Obj)))
6819 then
6820 Error_Msg_N
6821 ("prefix of protected procedure or entry call must be variable",
6822 Entry_Name);
6823 end if;
6825 Actuals := Parameter_Associations (N);
6826 First_Named := First_Named_Actual (N);
6828 Rewrite (N,
6829 Make_Entry_Call_Statement (Loc,
6830 Name => Entry_Name,
6831 Parameter_Associations => Actuals));
6833 Set_First_Named_Actual (N, First_Named);
6834 Set_Analyzed (N, True);
6836 -- Protected functions can return on the secondary stack, in which
6837 -- case we must trigger the transient scope mechanism.
6839 elsif Full_Expander_Active
6840 and then Requires_Transient_Scope (Etype (Nam))
6841 then
6842 Establish_Transient_Scope (N, Sec_Stack => True);
6843 end if;
6844 end Resolve_Entry_Call;
6846 -------------------------
6847 -- Resolve_Equality_Op --
6848 -------------------------
6850 -- Both arguments must have the same type, and the boolean context does
6851 -- not participate in the resolution. The first pass verifies that the
6852 -- interpretation is not ambiguous, and the type of the left argument is
6853 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
6854 -- are strings or aggregates, allocators, or Null, they are ambiguous even
6855 -- though they carry a single (universal) type. Diagnose this case here.
6857 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
6858 L : constant Node_Id := Left_Opnd (N);
6859 R : constant Node_Id := Right_Opnd (N);
6860 T : Entity_Id := Find_Unique_Type (L, R);
6862 procedure Check_If_Expression (Cond : Node_Id);
6863 -- The resolution rule for if expressions requires that each such must
6864 -- have a unique type. This means that if several dependent expressions
6865 -- are of a non-null anonymous access type, and the context does not
6866 -- impose an expected type (as can be the case in an equality operation)
6867 -- the expression must be rejected.
6869 procedure Explain_Redundancy (N : Node_Id);
6870 -- Attempt to explain the nature of a redundant comparison with True. If
6871 -- the expression N is too complex, this routine issues a general error
6872 -- message.
6874 function Find_Unique_Access_Type return Entity_Id;
6875 -- In the case of allocators and access attributes, the context must
6876 -- provide an indication of the specific access type to be used. If
6877 -- one operand is of such a "generic" access type, check whether there
6878 -- is a specific visible access type that has the same designated type.
6879 -- This is semantically dubious, and of no interest to any real code,
6880 -- but c48008a makes it all worthwhile.
6882 -------------------------
6883 -- Check_If_Expression --
6884 -------------------------
6886 procedure Check_If_Expression (Cond : Node_Id) is
6887 Then_Expr : Node_Id;
6888 Else_Expr : Node_Id;
6890 begin
6891 if Nkind (Cond) = N_If_Expression then
6892 Then_Expr := Next (First (Expressions (Cond)));
6893 Else_Expr := Next (Then_Expr);
6895 if Nkind (Then_Expr) /= N_Null
6896 and then Nkind (Else_Expr) /= N_Null
6897 then
6898 Error_Msg_N ("cannot determine type of if expression", Cond);
6899 end if;
6900 end if;
6901 end Check_If_Expression;
6903 ------------------------
6904 -- Explain_Redundancy --
6905 ------------------------
6907 procedure Explain_Redundancy (N : Node_Id) is
6908 Error : Name_Id;
6909 Val : Node_Id;
6910 Val_Id : Entity_Id;
6912 begin
6913 Val := N;
6915 -- Strip the operand down to an entity
6917 loop
6918 if Nkind (Val) = N_Selected_Component then
6919 Val := Selector_Name (Val);
6920 else
6921 exit;
6922 end if;
6923 end loop;
6925 -- The construct denotes an entity
6927 if Is_Entity_Name (Val) and then Present (Entity (Val)) then
6928 Val_Id := Entity (Val);
6930 -- Do not generate an error message when the comparison is done
6931 -- against the enumeration literal Standard.True.
6933 if Ekind (Val_Id) /= E_Enumeration_Literal then
6935 -- Build a customized error message
6937 Name_Len := 0;
6938 Add_Str_To_Name_Buffer ("?r?");
6940 if Ekind (Val_Id) = E_Component then
6941 Add_Str_To_Name_Buffer ("component ");
6943 elsif Ekind (Val_Id) = E_Constant then
6944 Add_Str_To_Name_Buffer ("constant ");
6946 elsif Ekind (Val_Id) = E_Discriminant then
6947 Add_Str_To_Name_Buffer ("discriminant ");
6949 elsif Is_Formal (Val_Id) then
6950 Add_Str_To_Name_Buffer ("parameter ");
6952 elsif Ekind (Val_Id) = E_Variable then
6953 Add_Str_To_Name_Buffer ("variable ");
6954 end if;
6956 Add_Str_To_Name_Buffer ("& is always True!");
6957 Error := Name_Find;
6959 Error_Msg_NE (Get_Name_String (Error), Val, Val_Id);
6960 end if;
6962 -- The construct is too complex to disect, issue a general message
6964 else
6965 Error_Msg_N ("?r?expression is always True!", Val);
6966 end if;
6967 end Explain_Redundancy;
6969 -----------------------------
6970 -- Find_Unique_Access_Type --
6971 -----------------------------
6973 function Find_Unique_Access_Type return Entity_Id is
6974 Acc : Entity_Id;
6975 E : Entity_Id;
6976 S : Entity_Id;
6978 begin
6979 if Ekind_In (Etype (R), E_Allocator_Type,
6980 E_Access_Attribute_Type)
6981 then
6982 Acc := Designated_Type (Etype (R));
6984 elsif Ekind_In (Etype (L), E_Allocator_Type,
6985 E_Access_Attribute_Type)
6986 then
6987 Acc := Designated_Type (Etype (L));
6988 else
6989 return Empty;
6990 end if;
6992 S := Current_Scope;
6993 while S /= Standard_Standard loop
6994 E := First_Entity (S);
6995 while Present (E) loop
6996 if Is_Type (E)
6997 and then Is_Access_Type (E)
6998 and then Ekind (E) /= E_Allocator_Type
6999 and then Designated_Type (E) = Base_Type (Acc)
7000 then
7001 return E;
7002 end if;
7004 Next_Entity (E);
7005 end loop;
7007 S := Scope (S);
7008 end loop;
7010 return Empty;
7011 end Find_Unique_Access_Type;
7013 -- Start of processing for Resolve_Equality_Op
7015 begin
7016 Set_Etype (N, Base_Type (Typ));
7017 Generate_Reference (T, N, ' ');
7019 if T = Any_Fixed then
7020 T := Unique_Fixed_Point_Type (L);
7021 end if;
7023 if T /= Any_Type then
7024 if T = Any_String or else
7025 T = Any_Composite or else
7026 T = Any_Character
7027 then
7028 if T = Any_Character then
7029 Ambiguous_Character (L);
7030 else
7031 Error_Msg_N ("ambiguous operands for equality", N);
7032 end if;
7034 Set_Etype (N, Any_Type);
7035 return;
7037 elsif T = Any_Access
7038 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
7039 then
7040 T := Find_Unique_Access_Type;
7042 if No (T) then
7043 Error_Msg_N ("ambiguous operands for equality", N);
7044 Set_Etype (N, Any_Type);
7045 return;
7046 end if;
7048 -- If expressions must have a single type, and if the context does
7049 -- not impose one the dependent expressions cannot be anonymous
7050 -- access types.
7052 -- Why no similar processing for case expressions???
7054 elsif Ada_Version >= Ada_2012
7055 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
7056 E_Anonymous_Access_Subprogram_Type)
7057 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
7058 E_Anonymous_Access_Subprogram_Type)
7059 then
7060 Check_If_Expression (L);
7061 Check_If_Expression (R);
7062 end if;
7064 Resolve (L, T);
7065 Resolve (R, T);
7067 -- In SPARK, equality operators = and /= for array types other than
7068 -- String are only defined when, for each index position, the
7069 -- operands have equal static bounds.
7071 if Is_Array_Type (T) then
7073 -- Protect call to Matching_Static_Array_Bounds to avoid costly
7074 -- operation if not needed.
7076 if Restriction_Check_Required (SPARK_05)
7077 and then Base_Type (T) /= Standard_String
7078 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
7079 and then Etype (L) /= Any_Composite -- or else L in error
7080 and then Etype (R) /= Any_Composite -- or else R in error
7081 and then not Matching_Static_Array_Bounds (Etype (L), Etype (R))
7082 then
7083 Check_SPARK_Restriction
7084 ("array types should have matching static bounds", N);
7085 end if;
7086 end if;
7088 -- If the unique type is a class-wide type then it will be expanded
7089 -- into a dispatching call to the predefined primitive. Therefore we
7090 -- check here for potential violation of such restriction.
7092 if Is_Class_Wide_Type (T) then
7093 Check_Restriction (No_Dispatching_Calls, N);
7094 end if;
7096 if Warn_On_Redundant_Constructs
7097 and then Comes_From_Source (N)
7098 and then Comes_From_Source (R)
7099 and then Is_Entity_Name (R)
7100 and then Entity (R) = Standard_True
7101 then
7102 Error_Msg_N -- CODEFIX
7103 ("?r?comparison with True is redundant!", N);
7104 Explain_Redundancy (Original_Node (R));
7105 end if;
7107 Check_Unset_Reference (L);
7108 Check_Unset_Reference (R);
7109 Generate_Operator_Reference (N, T);
7110 Check_Low_Bound_Tested (N);
7112 -- If this is an inequality, it may be the implicit inequality
7113 -- created for a user-defined operation, in which case the corres-
7114 -- ponding equality operation is not intrinsic, and the operation
7115 -- cannot be constant-folded. Else fold.
7117 if Nkind (N) = N_Op_Eq
7118 or else Comes_From_Source (Entity (N))
7119 or else Ekind (Entity (N)) = E_Operator
7120 or else Is_Intrinsic_Subprogram
7121 (Corresponding_Equality (Entity (N)))
7122 then
7123 Analyze_Dimension (N);
7124 Eval_Relational_Op (N);
7126 elsif Nkind (N) = N_Op_Ne
7127 and then Is_Abstract_Subprogram (Entity (N))
7128 then
7129 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
7130 end if;
7132 -- Ada 2005: If one operand is an anonymous access type, convert the
7133 -- other operand to it, to ensure that the underlying types match in
7134 -- the back-end. Same for access_to_subprogram, and the conversion
7135 -- verifies that the types are subtype conformant.
7137 -- We apply the same conversion in the case one of the operands is a
7138 -- private subtype of the type of the other.
7140 -- Why the Expander_Active test here ???
7142 if Full_Expander_Active
7143 and then
7144 (Ekind_In (T, E_Anonymous_Access_Type,
7145 E_Anonymous_Access_Subprogram_Type)
7146 or else Is_Private_Type (T))
7147 then
7148 if Etype (L) /= T then
7149 Rewrite (L,
7150 Make_Unchecked_Type_Conversion (Sloc (L),
7151 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
7152 Expression => Relocate_Node (L)));
7153 Analyze_And_Resolve (L, T);
7154 end if;
7156 if (Etype (R)) /= T then
7157 Rewrite (R,
7158 Make_Unchecked_Type_Conversion (Sloc (R),
7159 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
7160 Expression => Relocate_Node (R)));
7161 Analyze_And_Resolve (R, T);
7162 end if;
7163 end if;
7164 end if;
7165 end Resolve_Equality_Op;
7167 ----------------------------------
7168 -- Resolve_Explicit_Dereference --
7169 ----------------------------------
7171 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
7172 Loc : constant Source_Ptr := Sloc (N);
7173 New_N : Node_Id;
7174 P : constant Node_Id := Prefix (N);
7176 P_Typ : Entity_Id;
7177 -- The candidate prefix type, if overloaded
7179 I : Interp_Index;
7180 It : Interp;
7182 begin
7183 Check_Fully_Declared_Prefix (Typ, P);
7184 P_Typ := Empty;
7186 if Is_Overloaded (P) then
7188 -- Use the context type to select the prefix that has the correct
7189 -- designated type. Keep the first match, which will be the inner-
7190 -- most.
7192 Get_First_Interp (P, I, It);
7194 while Present (It.Typ) loop
7195 if Is_Access_Type (It.Typ)
7196 and then Covers (Typ, Designated_Type (It.Typ))
7197 then
7198 if No (P_Typ) then
7199 P_Typ := It.Typ;
7200 end if;
7202 -- Remove access types that do not match, but preserve access
7203 -- to subprogram interpretations, in case a further dereference
7204 -- is needed (see below).
7206 elsif Ekind (It.Typ) /= E_Access_Subprogram_Type then
7207 Remove_Interp (I);
7208 end if;
7210 Get_Next_Interp (I, It);
7211 end loop;
7213 if Present (P_Typ) then
7214 Resolve (P, P_Typ);
7215 Set_Etype (N, Designated_Type (P_Typ));
7217 else
7218 -- If no interpretation covers the designated type of the prefix,
7219 -- this is the pathological case where not all implementations of
7220 -- the prefix allow the interpretation of the node as a call. Now
7221 -- that the expected type is known, Remove other interpretations
7222 -- from prefix, rewrite it as a call, and resolve again, so that
7223 -- the proper call node is generated.
7225 Get_First_Interp (P, I, It);
7226 while Present (It.Typ) loop
7227 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
7228 Remove_Interp (I);
7229 end if;
7231 Get_Next_Interp (I, It);
7232 end loop;
7234 New_N :=
7235 Make_Function_Call (Loc,
7236 Name =>
7237 Make_Explicit_Dereference (Loc,
7238 Prefix => P),
7239 Parameter_Associations => New_List);
7241 Save_Interps (N, New_N);
7242 Rewrite (N, New_N);
7243 Analyze_And_Resolve (N, Typ);
7244 return;
7245 end if;
7247 -- If not overloaded, resolve P with its own type
7249 else
7250 Resolve (P);
7251 end if;
7253 if Is_Access_Type (Etype (P)) then
7254 Apply_Access_Check (N);
7255 end if;
7257 -- If the designated type is a packed unconstrained array type, and the
7258 -- explicit dereference is not in the context of an attribute reference,
7259 -- then we must compute and set the actual subtype, since it is needed
7260 -- by Gigi. The reason we exclude the attribute case is that this is
7261 -- handled fine by Gigi, and in fact we use such attributes to build the
7262 -- actual subtype. We also exclude generated code (which builds actual
7263 -- subtypes directly if they are needed).
7265 if Is_Array_Type (Etype (N))
7266 and then Is_Packed (Etype (N))
7267 and then not Is_Constrained (Etype (N))
7268 and then Nkind (Parent (N)) /= N_Attribute_Reference
7269 and then Comes_From_Source (N)
7270 then
7271 Set_Etype (N, Get_Actual_Subtype (N));
7272 end if;
7274 -- Note: No Eval processing is required for an explicit dereference,
7275 -- because such a name can never be static.
7277 end Resolve_Explicit_Dereference;
7279 -------------------------------------
7280 -- Resolve_Expression_With_Actions --
7281 -------------------------------------
7283 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
7284 begin
7285 Set_Etype (N, Typ);
7287 -- If N has no actions, and its expression has been constant folded,
7288 -- then rewrite N as just its expression. Note, we can't do this in
7289 -- the general case of Is_Empty_List (Actions (N)) as this would cause
7290 -- Expression (N) to be expanded again.
7292 if Is_Empty_List (Actions (N))
7293 and then Compile_Time_Known_Value (Expression (N))
7294 then
7295 Rewrite (N, Expression (N));
7296 end if;
7297 end Resolve_Expression_With_Actions;
7299 ---------------------------
7300 -- Resolve_If_Expression --
7301 ---------------------------
7303 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id) is
7304 Condition : constant Node_Id := First (Expressions (N));
7305 Then_Expr : constant Node_Id := Next (Condition);
7306 Else_Expr : Node_Id := Next (Then_Expr);
7307 Else_Typ : Entity_Id;
7308 Then_Typ : Entity_Id;
7310 begin
7311 Resolve (Condition, Any_Boolean);
7312 Resolve (Then_Expr, Typ);
7313 Then_Typ := Etype (Then_Expr);
7315 -- When the "then" expression is of a scalar subtype different from the
7316 -- result subtype, then insert a conversion to ensure the generation of
7317 -- a constraint check. The same is done for the else part below, again
7318 -- comparing subtypes rather than base types.
7320 if Is_Scalar_Type (Then_Typ)
7321 and then Then_Typ /= Typ
7322 then
7323 Rewrite (Then_Expr, Convert_To (Typ, Then_Expr));
7324 Analyze_And_Resolve (Then_Expr, Typ);
7325 end if;
7327 -- If ELSE expression present, just resolve using the determined type
7329 if Present (Else_Expr) then
7330 Resolve (Else_Expr, Typ);
7331 Else_Typ := Etype (Else_Expr);
7333 if Is_Scalar_Type (Else_Typ)
7334 and then Else_Typ /= Typ
7335 then
7336 Rewrite (Else_Expr, Convert_To (Typ, Else_Expr));
7337 Analyze_And_Resolve (Else_Expr, Typ);
7338 end if;
7340 -- If no ELSE expression is present, root type must be Standard.Boolean
7341 -- and we provide a Standard.True result converted to the appropriate
7342 -- Boolean type (in case it is a derived boolean type).
7344 elsif Root_Type (Typ) = Standard_Boolean then
7345 Else_Expr :=
7346 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
7347 Analyze_And_Resolve (Else_Expr, Typ);
7348 Append_To (Expressions (N), Else_Expr);
7350 else
7351 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
7352 Append_To (Expressions (N), Error);
7353 end if;
7355 Set_Etype (N, Typ);
7356 Eval_If_Expression (N);
7357 end Resolve_If_Expression;
7359 -------------------------------
7360 -- Resolve_Indexed_Component --
7361 -------------------------------
7363 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
7364 Name : constant Node_Id := Prefix (N);
7365 Expr : Node_Id;
7366 Array_Type : Entity_Id := Empty; -- to prevent junk warning
7367 Index : Node_Id;
7369 begin
7370 if Is_Overloaded (Name) then
7372 -- Use the context type to select the prefix that yields the correct
7373 -- component type.
7375 declare
7376 I : Interp_Index;
7377 It : Interp;
7378 I1 : Interp_Index := 0;
7379 P : constant Node_Id := Prefix (N);
7380 Found : Boolean := False;
7382 begin
7383 Get_First_Interp (P, I, It);
7384 while Present (It.Typ) loop
7385 if (Is_Array_Type (It.Typ)
7386 and then Covers (Typ, Component_Type (It.Typ)))
7387 or else (Is_Access_Type (It.Typ)
7388 and then Is_Array_Type (Designated_Type (It.Typ))
7389 and then
7390 Covers
7391 (Typ,
7392 Component_Type (Designated_Type (It.Typ))))
7393 then
7394 if Found then
7395 It := Disambiguate (P, I1, I, Any_Type);
7397 if It = No_Interp then
7398 Error_Msg_N ("ambiguous prefix for indexing", N);
7399 Set_Etype (N, Typ);
7400 return;
7402 else
7403 Found := True;
7404 Array_Type := It.Typ;
7405 I1 := I;
7406 end if;
7408 else
7409 Found := True;
7410 Array_Type := It.Typ;
7411 I1 := I;
7412 end if;
7413 end if;
7415 Get_Next_Interp (I, It);
7416 end loop;
7417 end;
7419 else
7420 Array_Type := Etype (Name);
7421 end if;
7423 Resolve (Name, Array_Type);
7424 Array_Type := Get_Actual_Subtype_If_Available (Name);
7426 -- If prefix is access type, dereference to get real array type.
7427 -- Note: we do not apply an access check because the expander always
7428 -- introduces an explicit dereference, and the check will happen there.
7430 if Is_Access_Type (Array_Type) then
7431 Array_Type := Designated_Type (Array_Type);
7432 end if;
7434 -- If name was overloaded, set component type correctly now
7435 -- If a misplaced call to an entry family (which has no index types)
7436 -- return. Error will be diagnosed from calling context.
7438 if Is_Array_Type (Array_Type) then
7439 Set_Etype (N, Component_Type (Array_Type));
7440 else
7441 return;
7442 end if;
7444 Index := First_Index (Array_Type);
7445 Expr := First (Expressions (N));
7447 -- The prefix may have resolved to a string literal, in which case its
7448 -- etype has a special representation. This is only possible currently
7449 -- if the prefix is a static concatenation, written in functional
7450 -- notation.
7452 if Ekind (Array_Type) = E_String_Literal_Subtype then
7453 Resolve (Expr, Standard_Positive);
7455 else
7456 while Present (Index) and Present (Expr) loop
7457 Resolve (Expr, Etype (Index));
7458 Check_Unset_Reference (Expr);
7460 if Is_Scalar_Type (Etype (Expr)) then
7461 Apply_Scalar_Range_Check (Expr, Etype (Index));
7462 else
7463 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
7464 end if;
7466 Next_Index (Index);
7467 Next (Expr);
7468 end loop;
7469 end if;
7471 Analyze_Dimension (N);
7473 -- Do not generate the warning on suspicious index if we are analyzing
7474 -- package Ada.Tags; otherwise we will report the warning with the
7475 -- Prims_Ptr field of the dispatch table.
7477 if Scope (Etype (Prefix (N))) = Standard_Standard
7478 or else not
7479 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
7480 Ada_Tags)
7481 then
7482 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
7483 Eval_Indexed_Component (N);
7484 end if;
7486 -- If the array type is atomic, and is packed, and we are in a left side
7487 -- context, then this is worth a warning, since we have a situation
7488 -- where the access to the component may cause extra read/writes of
7489 -- the atomic array object, which could be considered unexpected.
7491 if Nkind (N) = N_Indexed_Component
7492 and then (Is_Atomic (Array_Type)
7493 or else (Is_Entity_Name (Prefix (N))
7494 and then Is_Atomic (Entity (Prefix (N)))))
7495 and then Is_Bit_Packed_Array (Array_Type)
7496 and then Is_LHS (N)
7497 then
7498 Error_Msg_N ("??assignment to component of packed atomic array",
7499 Prefix (N));
7500 Error_Msg_N ("??\may cause unexpected accesses to atomic object",
7501 Prefix (N));
7502 end if;
7503 end Resolve_Indexed_Component;
7505 -----------------------------
7506 -- Resolve_Integer_Literal --
7507 -----------------------------
7509 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
7510 begin
7511 Set_Etype (N, Typ);
7512 Eval_Integer_Literal (N);
7513 end Resolve_Integer_Literal;
7515 --------------------------------
7516 -- Resolve_Intrinsic_Operator --
7517 --------------------------------
7519 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
7520 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7521 Op : Entity_Id;
7522 Orig_Op : constant Entity_Id := Entity (N);
7523 Arg1 : Node_Id;
7524 Arg2 : Node_Id;
7526 function Convert_Operand (Opnd : Node_Id) return Node_Id;
7527 -- If the operand is a literal, it cannot be the expression in a
7528 -- conversion. Use a qualified expression instead.
7530 function Convert_Operand (Opnd : Node_Id) return Node_Id is
7531 Loc : constant Source_Ptr := Sloc (Opnd);
7532 Res : Node_Id;
7533 begin
7534 if Nkind_In (Opnd, N_Integer_Literal, N_Real_Literal) then
7535 Res :=
7536 Make_Qualified_Expression (Loc,
7537 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
7538 Expression => Relocate_Node (Opnd));
7539 Analyze (Res);
7541 else
7542 Res := Unchecked_Convert_To (Btyp, Opnd);
7543 end if;
7545 return Res;
7546 end Convert_Operand;
7548 -- Start of processing for Resolve_Intrinsic_Operator
7550 begin
7551 -- We must preserve the original entity in a generic setting, so that
7552 -- the legality of the operation can be verified in an instance.
7554 if not Full_Expander_Active then
7555 return;
7556 end if;
7558 Op := Entity (N);
7559 while Scope (Op) /= Standard_Standard loop
7560 Op := Homonym (Op);
7561 pragma Assert (Present (Op));
7562 end loop;
7564 Set_Entity (N, Op);
7565 Set_Is_Overloaded (N, False);
7567 -- If the result or operand types are private, rewrite with unchecked
7568 -- conversions on the operands and the result, to expose the proper
7569 -- underlying numeric type.
7571 if Is_Private_Type (Typ)
7572 or else Is_Private_Type (Etype (Left_Opnd (N)))
7573 or else Is_Private_Type (Etype (Right_Opnd (N)))
7574 then
7575 Arg1 := Convert_Operand (Left_Opnd (N));
7576 -- Unchecked_Convert_To (Btyp, Left_Opnd (N));
7577 -- What on earth is this commented out fragment of code???
7579 if Nkind (N) = N_Op_Expon then
7580 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
7581 else
7582 Arg2 := Convert_Operand (Right_Opnd (N));
7583 end if;
7585 if Nkind (Arg1) = N_Type_Conversion then
7586 Save_Interps (Left_Opnd (N), Expression (Arg1));
7587 end if;
7589 if Nkind (Arg2) = N_Type_Conversion then
7590 Save_Interps (Right_Opnd (N), Expression (Arg2));
7591 end if;
7593 Set_Left_Opnd (N, Arg1);
7594 Set_Right_Opnd (N, Arg2);
7596 Set_Etype (N, Btyp);
7597 Rewrite (N, Unchecked_Convert_To (Typ, N));
7598 Resolve (N, Typ);
7600 elsif Typ /= Etype (Left_Opnd (N))
7601 or else Typ /= Etype (Right_Opnd (N))
7602 then
7603 -- Add explicit conversion where needed, and save interpretations in
7604 -- case operands are overloaded. If the context is a VMS operation,
7605 -- assert that the conversion is legal (the operands have the proper
7606 -- types to select the VMS intrinsic). Note that in rare cases the
7607 -- VMS operators may be visible, but the default System is being used
7608 -- and Address is a private type.
7610 Arg1 := Convert_To (Typ, Left_Opnd (N));
7611 Arg2 := Convert_To (Typ, Right_Opnd (N));
7613 if Nkind (Arg1) = N_Type_Conversion then
7614 Save_Interps (Left_Opnd (N), Expression (Arg1));
7616 if Is_VMS_Operator (Orig_Op) then
7617 Set_Conversion_OK (Arg1);
7618 end if;
7619 else
7620 Save_Interps (Left_Opnd (N), Arg1);
7621 end if;
7623 if Nkind (Arg2) = N_Type_Conversion then
7624 Save_Interps (Right_Opnd (N), Expression (Arg2));
7626 if Is_VMS_Operator (Orig_Op) then
7627 Set_Conversion_OK (Arg2);
7628 end if;
7629 else
7630 Save_Interps (Right_Opnd (N), Arg2);
7631 end if;
7633 Rewrite (Left_Opnd (N), Arg1);
7634 Rewrite (Right_Opnd (N), Arg2);
7635 Analyze (Arg1);
7636 Analyze (Arg2);
7637 Resolve_Arithmetic_Op (N, Typ);
7639 else
7640 Resolve_Arithmetic_Op (N, Typ);
7641 end if;
7642 end Resolve_Intrinsic_Operator;
7644 --------------------------------------
7645 -- Resolve_Intrinsic_Unary_Operator --
7646 --------------------------------------
7648 procedure Resolve_Intrinsic_Unary_Operator
7649 (N : Node_Id;
7650 Typ : Entity_Id)
7652 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7653 Op : Entity_Id;
7654 Arg2 : Node_Id;
7656 begin
7657 Op := Entity (N);
7658 while Scope (Op) /= Standard_Standard loop
7659 Op := Homonym (Op);
7660 pragma Assert (Present (Op));
7661 end loop;
7663 Set_Entity (N, Op);
7665 if Is_Private_Type (Typ) then
7666 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
7667 Save_Interps (Right_Opnd (N), Expression (Arg2));
7669 Set_Right_Opnd (N, Arg2);
7671 Set_Etype (N, Btyp);
7672 Rewrite (N, Unchecked_Convert_To (Typ, N));
7673 Resolve (N, Typ);
7675 else
7676 Resolve_Unary_Op (N, Typ);
7677 end if;
7678 end Resolve_Intrinsic_Unary_Operator;
7680 ------------------------
7681 -- Resolve_Logical_Op --
7682 ------------------------
7684 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
7685 B_Typ : Entity_Id;
7687 begin
7688 Check_No_Direct_Boolean_Operators (N);
7690 -- Predefined operations on scalar types yield the base type. On the
7691 -- other hand, logical operations on arrays yield the type of the
7692 -- arguments (and the context).
7694 if Is_Array_Type (Typ) then
7695 B_Typ := Typ;
7696 else
7697 B_Typ := Base_Type (Typ);
7698 end if;
7700 -- OK if this is a VMS-specific intrinsic operation
7702 if Is_VMS_Operator (Entity (N)) then
7703 null;
7705 -- The following test is required because the operands of the operation
7706 -- may be literals, in which case the resulting type appears to be
7707 -- compatible with a signed integer type, when in fact it is compatible
7708 -- only with modular types. If the context itself is universal, the
7709 -- operation is illegal.
7711 elsif not Valid_Boolean_Arg (Typ) then
7712 Error_Msg_N ("invalid context for logical operation", N);
7713 Set_Etype (N, Any_Type);
7714 return;
7716 elsif Typ = Any_Modular then
7717 Error_Msg_N
7718 ("no modular type available in this context", N);
7719 Set_Etype (N, Any_Type);
7720 return;
7722 elsif Is_Modular_Integer_Type (Typ)
7723 and then Etype (Left_Opnd (N)) = Universal_Integer
7724 and then Etype (Right_Opnd (N)) = Universal_Integer
7725 then
7726 Check_For_Visible_Operator (N, B_Typ);
7727 end if;
7729 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
7730 -- is active and the result type is standard Boolean (do not mess with
7731 -- ops that return a nonstandard Boolean type, because something strange
7732 -- is going on).
7734 -- Note: you might expect this replacement to be done during expansion,
7735 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
7736 -- is used, no part of the right operand of an "and" or "or" operator
7737 -- should be executed if the left operand would short-circuit the
7738 -- evaluation of the corresponding "and then" or "or else". If we left
7739 -- the replacement to expansion time, then run-time checks associated
7740 -- with such operands would be evaluated unconditionally, due to being
7741 -- before the condition prior to the rewriting as short-circuit forms
7742 -- during expansion.
7744 if Short_Circuit_And_Or
7745 and then B_Typ = Standard_Boolean
7746 and then Nkind_In (N, N_Op_And, N_Op_Or)
7747 then
7748 if Nkind (N) = N_Op_And then
7749 Rewrite (N,
7750 Make_And_Then (Sloc (N),
7751 Left_Opnd => Relocate_Node (Left_Opnd (N)),
7752 Right_Opnd => Relocate_Node (Right_Opnd (N))));
7753 Analyze_And_Resolve (N, B_Typ);
7755 -- Case of OR changed to OR ELSE
7757 else
7758 Rewrite (N,
7759 Make_Or_Else (Sloc (N),
7760 Left_Opnd => Relocate_Node (Left_Opnd (N)),
7761 Right_Opnd => Relocate_Node (Right_Opnd (N))));
7762 Analyze_And_Resolve (N, B_Typ);
7763 end if;
7765 -- Return now, since analysis of the rewritten ops will take care of
7766 -- other reference bookkeeping and expression folding.
7768 return;
7769 end if;
7771 Resolve (Left_Opnd (N), B_Typ);
7772 Resolve (Right_Opnd (N), B_Typ);
7774 Check_Unset_Reference (Left_Opnd (N));
7775 Check_Unset_Reference (Right_Opnd (N));
7777 Set_Etype (N, B_Typ);
7778 Generate_Operator_Reference (N, B_Typ);
7779 Eval_Logical_Op (N);
7781 -- In SPARK, logical operations AND, OR and XOR for arrays are defined
7782 -- only when both operands have same static lower and higher bounds. Of
7783 -- course the types have to match, so only check if operands are
7784 -- compatible and the node itself has no errors.
7786 if Is_Array_Type (B_Typ)
7787 and then Nkind (N) in N_Binary_Op
7788 then
7789 declare
7790 Left_Typ : constant Node_Id := Etype (Left_Opnd (N));
7791 Right_Typ : constant Node_Id := Etype (Right_Opnd (N));
7793 begin
7794 -- Protect call to Matching_Static_Array_Bounds to avoid costly
7795 -- operation if not needed.
7797 if Restriction_Check_Required (SPARK_05)
7798 and then Base_Type (Left_Typ) = Base_Type (Right_Typ)
7799 and then Left_Typ /= Any_Composite -- or Left_Opnd in error
7800 and then Right_Typ /= Any_Composite -- or Right_Opnd in error
7801 and then not Matching_Static_Array_Bounds (Left_Typ, Right_Typ)
7802 then
7803 Check_SPARK_Restriction
7804 ("array types should have matching static bounds", N);
7805 end if;
7806 end;
7807 end if;
7809 Check_Function_Writable_Actuals (N);
7810 end Resolve_Logical_Op;
7812 ---------------------------
7813 -- Resolve_Membership_Op --
7814 ---------------------------
7816 -- The context can only be a boolean type, and does not determine the
7817 -- arguments. Arguments should be unambiguous, but the preference rule for
7818 -- universal types applies.
7820 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
7821 pragma Warnings (Off, Typ);
7823 L : constant Node_Id := Left_Opnd (N);
7824 R : constant Node_Id := Right_Opnd (N);
7825 T : Entity_Id;
7827 procedure Resolve_Set_Membership;
7828 -- Analysis has determined a unique type for the left operand. Use it to
7829 -- resolve the disjuncts.
7831 ----------------------------
7832 -- Resolve_Set_Membership --
7833 ----------------------------
7835 procedure Resolve_Set_Membership is
7836 Alt : Node_Id;
7837 Ltyp : constant Entity_Id := Etype (L);
7839 begin
7840 Resolve (L, Ltyp);
7842 Alt := First (Alternatives (N));
7843 while Present (Alt) loop
7845 -- Alternative is an expression, a range
7846 -- or a subtype mark.
7848 if not Is_Entity_Name (Alt)
7849 or else not Is_Type (Entity (Alt))
7850 then
7851 Resolve (Alt, Ltyp);
7852 end if;
7854 Next (Alt);
7855 end loop;
7857 -- Check for duplicates for discrete case
7859 if Is_Discrete_Type (Ltyp) then
7860 declare
7861 type Ent is record
7862 Alt : Node_Id;
7863 Val : Uint;
7864 end record;
7866 Alts : array (0 .. List_Length (Alternatives (N))) of Ent;
7867 Nalts : Nat;
7869 begin
7870 -- Loop checking duplicates. This is quadratic, but giant sets
7871 -- are unlikely in this context so it's a reasonable choice.
7873 Nalts := 0;
7874 Alt := First (Alternatives (N));
7875 while Present (Alt) loop
7876 if Is_Static_Expression (Alt)
7877 and then (Nkind_In (Alt, N_Integer_Literal,
7878 N_Character_Literal)
7879 or else Nkind (Alt) in N_Has_Entity)
7880 then
7881 Nalts := Nalts + 1;
7882 Alts (Nalts) := (Alt, Expr_Value (Alt));
7884 for J in 1 .. Nalts - 1 loop
7885 if Alts (J).Val = Alts (Nalts).Val then
7886 Error_Msg_Sloc := Sloc (Alts (J).Alt);
7887 Error_Msg_N ("duplicate of value given#??", Alt);
7888 end if;
7889 end loop;
7890 end if;
7892 Alt := Next (Alt);
7893 end loop;
7894 end;
7895 end if;
7896 end Resolve_Set_Membership;
7898 -- Start of processing for Resolve_Membership_Op
7900 begin
7901 if L = Error or else R = Error then
7902 return;
7903 end if;
7905 if Present (Alternatives (N)) then
7906 Resolve_Set_Membership;
7907 Check_Function_Writable_Actuals (N);
7908 return;
7910 elsif not Is_Overloaded (R)
7911 and then
7912 (Etype (R) = Universal_Integer
7913 or else
7914 Etype (R) = Universal_Real)
7915 and then Is_Overloaded (L)
7916 then
7917 T := Etype (R);
7919 -- Ada 2005 (AI-251): Support the following case:
7921 -- type I is interface;
7922 -- type T is tagged ...
7924 -- function Test (O : I'Class) is
7925 -- begin
7926 -- return O in T'Class.
7927 -- end Test;
7929 -- In this case we have nothing else to do. The membership test will be
7930 -- done at run time.
7932 elsif Ada_Version >= Ada_2005
7933 and then Is_Class_Wide_Type (Etype (L))
7934 and then Is_Interface (Etype (L))
7935 and then Is_Class_Wide_Type (Etype (R))
7936 and then not Is_Interface (Etype (R))
7937 then
7938 return;
7939 else
7940 T := Intersect_Types (L, R);
7941 end if;
7943 -- If mixed-mode operations are present and operands are all literal,
7944 -- the only interpretation involves Duration, which is probably not
7945 -- the intention of the programmer.
7947 if T = Any_Fixed then
7948 T := Unique_Fixed_Point_Type (N);
7950 if T = Any_Type then
7951 return;
7952 end if;
7953 end if;
7955 Resolve (L, T);
7956 Check_Unset_Reference (L);
7958 if Nkind (R) = N_Range
7959 and then not Is_Scalar_Type (T)
7960 then
7961 Error_Msg_N ("scalar type required for range", R);
7962 end if;
7964 if Is_Entity_Name (R) then
7965 Freeze_Expression (R);
7966 else
7967 Resolve (R, T);
7968 Check_Unset_Reference (R);
7969 end if;
7971 Eval_Membership_Op (N);
7972 Check_Function_Writable_Actuals (N);
7973 end Resolve_Membership_Op;
7975 ------------------
7976 -- Resolve_Null --
7977 ------------------
7979 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
7980 Loc : constant Source_Ptr := Sloc (N);
7982 begin
7983 -- Handle restriction against anonymous null access values This
7984 -- restriction can be turned off using -gnatdj.
7986 -- Ada 2005 (AI-231): Remove restriction
7988 if Ada_Version < Ada_2005
7989 and then not Debug_Flag_J
7990 and then Ekind (Typ) = E_Anonymous_Access_Type
7991 and then Comes_From_Source (N)
7992 then
7993 -- In the common case of a call which uses an explicitly null value
7994 -- for an access parameter, give specialized error message.
7996 if Nkind (Parent (N)) in N_Subprogram_Call then
7997 Error_Msg_N
7998 ("null is not allowed as argument for an access parameter", N);
8000 -- Standard message for all other cases (are there any?)
8002 else
8003 Error_Msg_N
8004 ("null cannot be of an anonymous access type", N);
8005 end if;
8006 end if;
8008 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
8009 -- assignment to a null-excluding object
8011 if Ada_Version >= Ada_2005
8012 and then Can_Never_Be_Null (Typ)
8013 and then Nkind (Parent (N)) = N_Assignment_Statement
8014 then
8015 if not Inside_Init_Proc then
8016 Insert_Action
8017 (Compile_Time_Constraint_Error (N,
8018 "(Ada 2005) null not allowed in null-excluding objects??"),
8019 Make_Raise_Constraint_Error (Loc,
8020 Reason => CE_Access_Check_Failed));
8021 else
8022 Insert_Action (N,
8023 Make_Raise_Constraint_Error (Loc,
8024 Reason => CE_Access_Check_Failed));
8025 end if;
8026 end if;
8028 -- In a distributed context, null for a remote access to subprogram may
8029 -- need to be replaced with a special record aggregate. In this case,
8030 -- return after having done the transformation.
8032 if (Ekind (Typ) = E_Record_Type
8033 or else Is_Remote_Access_To_Subprogram_Type (Typ))
8034 and then Remote_AST_Null_Value (N, Typ)
8035 then
8036 return;
8037 end if;
8039 -- The null literal takes its type from the context
8041 Set_Etype (N, Typ);
8042 end Resolve_Null;
8044 -----------------------
8045 -- Resolve_Op_Concat --
8046 -----------------------
8048 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
8050 -- We wish to avoid deep recursion, because concatenations are often
8051 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
8052 -- operands nonrecursively until we find something that is not a simple
8053 -- concatenation (A in this case). We resolve that, and then walk back
8054 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
8055 -- to do the rest of the work at each level. The Parent pointers allow
8056 -- us to avoid recursion, and thus avoid running out of memory. See also
8057 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
8059 NN : Node_Id := N;
8060 Op1 : Node_Id;
8062 begin
8063 -- The following code is equivalent to:
8065 -- Resolve_Op_Concat_First (NN, Typ);
8066 -- Resolve_Op_Concat_Arg (N, ...);
8067 -- Resolve_Op_Concat_Rest (N, Typ);
8069 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
8070 -- operand is a concatenation.
8072 -- Walk down left operands
8074 loop
8075 Resolve_Op_Concat_First (NN, Typ);
8076 Op1 := Left_Opnd (NN);
8077 exit when not (Nkind (Op1) = N_Op_Concat
8078 and then not Is_Array_Type (Component_Type (Typ))
8079 and then Entity (Op1) = Entity (NN));
8080 NN := Op1;
8081 end loop;
8083 -- Now (given the above example) NN is A&B and Op1 is A
8085 -- First resolve Op1 ...
8087 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
8089 -- ... then walk NN back up until we reach N (where we started), calling
8090 -- Resolve_Op_Concat_Rest along the way.
8092 loop
8093 Resolve_Op_Concat_Rest (NN, Typ);
8094 exit when NN = N;
8095 NN := Parent (NN);
8096 end loop;
8098 if Base_Type (Etype (N)) /= Standard_String then
8099 Check_SPARK_Restriction
8100 ("result of concatenation should have type String", N);
8101 end if;
8102 end Resolve_Op_Concat;
8104 ---------------------------
8105 -- Resolve_Op_Concat_Arg --
8106 ---------------------------
8108 procedure Resolve_Op_Concat_Arg
8109 (N : Node_Id;
8110 Arg : Node_Id;
8111 Typ : Entity_Id;
8112 Is_Comp : Boolean)
8114 Btyp : constant Entity_Id := Base_Type (Typ);
8115 Ctyp : constant Entity_Id := Component_Type (Typ);
8117 begin
8118 if In_Instance then
8119 if Is_Comp
8120 or else (not Is_Overloaded (Arg)
8121 and then Etype (Arg) /= Any_Composite
8122 and then Covers (Ctyp, Etype (Arg)))
8123 then
8124 Resolve (Arg, Ctyp);
8125 else
8126 Resolve (Arg, Btyp);
8127 end if;
8129 -- If both Array & Array and Array & Component are visible, there is a
8130 -- potential ambiguity that must be reported.
8132 elsif Has_Compatible_Type (Arg, Ctyp) then
8133 if Nkind (Arg) = N_Aggregate
8134 and then Is_Composite_Type (Ctyp)
8135 then
8136 if Is_Private_Type (Ctyp) then
8137 Resolve (Arg, Btyp);
8139 -- If the operation is user-defined and not overloaded use its
8140 -- profile. The operation may be a renaming, in which case it has
8141 -- been rewritten, and we want the original profile.
8143 elsif not Is_Overloaded (N)
8144 and then Comes_From_Source (Entity (Original_Node (N)))
8145 and then Ekind (Entity (Original_Node (N))) = E_Function
8146 then
8147 Resolve (Arg,
8148 Etype
8149 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
8150 return;
8152 -- Otherwise an aggregate may match both the array type and the
8153 -- component type.
8155 else
8156 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
8157 Set_Etype (Arg, Any_Type);
8158 end if;
8160 else
8161 if Is_Overloaded (Arg)
8162 and then Has_Compatible_Type (Arg, Typ)
8163 and then Etype (Arg) /= Any_Type
8164 then
8165 declare
8166 I : Interp_Index;
8167 It : Interp;
8168 Func : Entity_Id;
8170 begin
8171 Get_First_Interp (Arg, I, It);
8172 Func := It.Nam;
8173 Get_Next_Interp (I, It);
8175 -- Special-case the error message when the overloading is
8176 -- caused by a function that yields an array and can be
8177 -- called without parameters.
8179 if It.Nam = Func then
8180 Error_Msg_Sloc := Sloc (Func);
8181 Error_Msg_N ("ambiguous call to function#", Arg);
8182 Error_Msg_NE
8183 ("\\interpretation as call yields&", Arg, Typ);
8184 Error_Msg_NE
8185 ("\\interpretation as indexing of call yields&",
8186 Arg, Component_Type (Typ));
8188 else
8189 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
8191 Get_First_Interp (Arg, I, It);
8192 while Present (It.Nam) loop
8193 Error_Msg_Sloc := Sloc (It.Nam);
8195 if Base_Type (It.Typ) = Btyp
8196 or else
8197 Base_Type (It.Typ) = Base_Type (Ctyp)
8198 then
8199 Error_Msg_N -- CODEFIX
8200 ("\\possible interpretation#", Arg);
8201 end if;
8203 Get_Next_Interp (I, It);
8204 end loop;
8205 end if;
8206 end;
8207 end if;
8209 Resolve (Arg, Component_Type (Typ));
8211 if Nkind (Arg) = N_String_Literal then
8212 Set_Etype (Arg, Component_Type (Typ));
8213 end if;
8215 if Arg = Left_Opnd (N) then
8216 Set_Is_Component_Left_Opnd (N);
8217 else
8218 Set_Is_Component_Right_Opnd (N);
8219 end if;
8220 end if;
8222 else
8223 Resolve (Arg, Btyp);
8224 end if;
8226 -- Concatenation is restricted in SPARK: each operand must be either a
8227 -- string literal, the name of a string constant, a static character or
8228 -- string expression, or another concatenation. Arg cannot be a
8229 -- concatenation here as callers of Resolve_Op_Concat_Arg call it
8230 -- separately on each final operand, past concatenation operations.
8232 if Is_Character_Type (Etype (Arg)) then
8233 if not Is_Static_Expression (Arg) then
8234 Check_SPARK_Restriction
8235 ("character operand for concatenation should be static", Arg);
8236 end if;
8238 elsif Is_String_Type (Etype (Arg)) then
8239 if not (Nkind_In (Arg, N_Identifier, N_Expanded_Name)
8240 and then Is_Constant_Object (Entity (Arg)))
8241 and then not Is_Static_Expression (Arg)
8242 then
8243 Check_SPARK_Restriction
8244 ("string operand for concatenation should be static", Arg);
8245 end if;
8247 -- Do not issue error on an operand that is neither a character nor a
8248 -- string, as the error is issued in Resolve_Op_Concat.
8250 else
8251 null;
8252 end if;
8254 Check_Unset_Reference (Arg);
8255 end Resolve_Op_Concat_Arg;
8257 -----------------------------
8258 -- Resolve_Op_Concat_First --
8259 -----------------------------
8261 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
8262 Btyp : constant Entity_Id := Base_Type (Typ);
8263 Op1 : constant Node_Id := Left_Opnd (N);
8264 Op2 : constant Node_Id := Right_Opnd (N);
8266 begin
8267 -- The parser folds an enormous sequence of concatenations of string
8268 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
8269 -- in the right operand. If the expression resolves to a predefined "&"
8270 -- operator, all is well. Otherwise, the parser's folding is wrong, so
8271 -- we give an error. See P_Simple_Expression in Par.Ch4.
8273 if Nkind (Op2) = N_String_Literal
8274 and then Is_Folded_In_Parser (Op2)
8275 and then Ekind (Entity (N)) = E_Function
8276 then
8277 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
8278 and then String_Length (Strval (Op1)) = 0);
8279 Error_Msg_N ("too many user-defined concatenations", N);
8280 return;
8281 end if;
8283 Set_Etype (N, Btyp);
8285 if Is_Limited_Composite (Btyp) then
8286 Error_Msg_N ("concatenation not available for limited array", N);
8287 Explain_Limited_Type (Btyp, N);
8288 end if;
8289 end Resolve_Op_Concat_First;
8291 ----------------------------
8292 -- Resolve_Op_Concat_Rest --
8293 ----------------------------
8295 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
8296 Op1 : constant Node_Id := Left_Opnd (N);
8297 Op2 : constant Node_Id := Right_Opnd (N);
8299 begin
8300 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
8302 Generate_Operator_Reference (N, Typ);
8304 if Is_String_Type (Typ) then
8305 Eval_Concatenation (N);
8306 end if;
8308 -- If this is not a static concatenation, but the result is a string
8309 -- type (and not an array of strings) ensure that static string operands
8310 -- have their subtypes properly constructed.
8312 if Nkind (N) /= N_String_Literal
8313 and then Is_Character_Type (Component_Type (Typ))
8314 then
8315 Set_String_Literal_Subtype (Op1, Typ);
8316 Set_String_Literal_Subtype (Op2, Typ);
8317 end if;
8318 end Resolve_Op_Concat_Rest;
8320 ----------------------
8321 -- Resolve_Op_Expon --
8322 ----------------------
8324 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
8325 B_Typ : constant Entity_Id := Base_Type (Typ);
8327 begin
8328 -- Catch attempts to do fixed-point exponentiation with universal
8329 -- operands, which is a case where the illegality is not caught during
8330 -- normal operator analysis. This is not done in preanalysis mode
8331 -- since the tree is not fully decorated during preanalysis.
8333 if Full_Analysis then
8334 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
8335 Error_Msg_N ("exponentiation not available for fixed point", N);
8336 return;
8338 elsif Nkind (Parent (N)) in N_Op
8339 and then Is_Fixed_Point_Type (Etype (Parent (N)))
8340 and then Etype (N) = Universal_Real
8341 and then Comes_From_Source (N)
8342 then
8343 Error_Msg_N ("exponentiation not available for fixed point", N);
8344 return;
8345 end if;
8346 end if;
8348 if Comes_From_Source (N)
8349 and then Ekind (Entity (N)) = E_Function
8350 and then Is_Imported (Entity (N))
8351 and then Is_Intrinsic_Subprogram (Entity (N))
8352 then
8353 Resolve_Intrinsic_Operator (N, Typ);
8354 return;
8355 end if;
8357 if Etype (Left_Opnd (N)) = Universal_Integer
8358 or else Etype (Left_Opnd (N)) = Universal_Real
8359 then
8360 Check_For_Visible_Operator (N, B_Typ);
8361 end if;
8363 -- We do the resolution using the base type, because intermediate values
8364 -- in expressions are always of the base type, not a subtype of it.
8366 Resolve (Left_Opnd (N), B_Typ);
8367 Resolve (Right_Opnd (N), Standard_Integer);
8369 Check_Unset_Reference (Left_Opnd (N));
8370 Check_Unset_Reference (Right_Opnd (N));
8372 Set_Etype (N, B_Typ);
8373 Generate_Operator_Reference (N, B_Typ);
8375 Analyze_Dimension (N);
8377 if Ada_Version >= Ada_2012 and then Has_Dimension_System (B_Typ) then
8378 -- Evaluate the exponentiation operator for dimensioned type
8380 Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ);
8381 else
8382 Eval_Op_Expon (N);
8383 end if;
8385 -- Set overflow checking bit. Much cleverer code needed here eventually
8386 -- and perhaps the Resolve routines should be separated for the various
8387 -- arithmetic operations, since they will need different processing. ???
8389 if Nkind (N) in N_Op then
8390 if not Overflow_Checks_Suppressed (Etype (N)) then
8391 Enable_Overflow_Check (N);
8392 end if;
8393 end if;
8394 end Resolve_Op_Expon;
8396 --------------------
8397 -- Resolve_Op_Not --
8398 --------------------
8400 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
8401 B_Typ : Entity_Id;
8403 function Parent_Is_Boolean return Boolean;
8404 -- This function determines if the parent node is a boolean operator or
8405 -- operation (comparison op, membership test, or short circuit form) and
8406 -- the not in question is the left operand of this operation. Note that
8407 -- if the not is in parens, then false is returned.
8409 -----------------------
8410 -- Parent_Is_Boolean --
8411 -----------------------
8413 function Parent_Is_Boolean return Boolean is
8414 begin
8415 if Paren_Count (N) /= 0 then
8416 return False;
8418 else
8419 case Nkind (Parent (N)) is
8420 when N_Op_And |
8421 N_Op_Eq |
8422 N_Op_Ge |
8423 N_Op_Gt |
8424 N_Op_Le |
8425 N_Op_Lt |
8426 N_Op_Ne |
8427 N_Op_Or |
8428 N_Op_Xor |
8429 N_In |
8430 N_Not_In |
8431 N_And_Then |
8432 N_Or_Else =>
8434 return Left_Opnd (Parent (N)) = N;
8436 when others =>
8437 return False;
8438 end case;
8439 end if;
8440 end Parent_Is_Boolean;
8442 -- Start of processing for Resolve_Op_Not
8444 begin
8445 -- Predefined operations on scalar types yield the base type. On the
8446 -- other hand, logical operations on arrays yield the type of the
8447 -- arguments (and the context).
8449 if Is_Array_Type (Typ) then
8450 B_Typ := Typ;
8451 else
8452 B_Typ := Base_Type (Typ);
8453 end if;
8455 if Is_VMS_Operator (Entity (N)) then
8456 null;
8458 -- Straightforward case of incorrect arguments
8460 elsif not Valid_Boolean_Arg (Typ) then
8461 Error_Msg_N ("invalid operand type for operator&", N);
8462 Set_Etype (N, Any_Type);
8463 return;
8465 -- Special case of probable missing parens
8467 elsif Typ = Universal_Integer or else Typ = Any_Modular then
8468 if Parent_Is_Boolean then
8469 Error_Msg_N
8470 ("operand of not must be enclosed in parentheses",
8471 Right_Opnd (N));
8472 else
8473 Error_Msg_N
8474 ("no modular type available in this context", N);
8475 end if;
8477 Set_Etype (N, Any_Type);
8478 return;
8480 -- OK resolution of NOT
8482 else
8483 -- Warn if non-boolean types involved. This is a case like not a < b
8484 -- where a and b are modular, where we will get (not a) < b and most
8485 -- likely not (a < b) was intended.
8487 if Warn_On_Questionable_Missing_Parens
8488 and then not Is_Boolean_Type (Typ)
8489 and then Parent_Is_Boolean
8490 then
8491 Error_Msg_N ("?q?not expression should be parenthesized here!", N);
8492 end if;
8494 -- Warn on double negation if checking redundant constructs
8496 if Warn_On_Redundant_Constructs
8497 and then Comes_From_Source (N)
8498 and then Comes_From_Source (Right_Opnd (N))
8499 and then Root_Type (Typ) = Standard_Boolean
8500 and then Nkind (Right_Opnd (N)) = N_Op_Not
8501 then
8502 Error_Msg_N ("redundant double negation?r?", N);
8503 end if;
8505 -- Complete resolution and evaluation of NOT
8507 Resolve (Right_Opnd (N), B_Typ);
8508 Check_Unset_Reference (Right_Opnd (N));
8509 Set_Etype (N, B_Typ);
8510 Generate_Operator_Reference (N, B_Typ);
8511 Eval_Op_Not (N);
8512 end if;
8513 end Resolve_Op_Not;
8515 -----------------------------
8516 -- Resolve_Operator_Symbol --
8517 -----------------------------
8519 -- Nothing to be done, all resolved already
8521 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
8522 pragma Warnings (Off, N);
8523 pragma Warnings (Off, Typ);
8525 begin
8526 null;
8527 end Resolve_Operator_Symbol;
8529 ----------------------------------
8530 -- Resolve_Qualified_Expression --
8531 ----------------------------------
8533 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
8534 pragma Warnings (Off, Typ);
8536 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
8537 Expr : constant Node_Id := Expression (N);
8539 begin
8540 Resolve (Expr, Target_Typ);
8542 -- Protect call to Matching_Static_Array_Bounds to avoid costly
8543 -- operation if not needed.
8545 if Restriction_Check_Required (SPARK_05)
8546 and then Is_Array_Type (Target_Typ)
8547 and then Is_Array_Type (Etype (Expr))
8548 and then Etype (Expr) /= Any_Composite -- or else Expr in error
8549 and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr))
8550 then
8551 Check_SPARK_Restriction
8552 ("array types should have matching static bounds", N);
8553 end if;
8555 -- A qualified expression requires an exact match of the type, class-
8556 -- wide matching is not allowed. However, if the qualifying type is
8557 -- specific and the expression has a class-wide type, it may still be
8558 -- okay, since it can be the result of the expansion of a call to a
8559 -- dispatching function, so we also have to check class-wideness of the
8560 -- type of the expression's original node.
8562 if (Is_Class_Wide_Type (Target_Typ)
8563 or else
8564 (Is_Class_Wide_Type (Etype (Expr))
8565 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
8566 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
8567 then
8568 Wrong_Type (Expr, Target_Typ);
8569 end if;
8571 -- If the target type is unconstrained, then we reset the type of the
8572 -- result from the type of the expression. For other cases, the actual
8573 -- subtype of the expression is the target type.
8575 if Is_Composite_Type (Target_Typ)
8576 and then not Is_Constrained (Target_Typ)
8577 then
8578 Set_Etype (N, Etype (Expr));
8579 end if;
8581 Analyze_Dimension (N);
8582 Eval_Qualified_Expression (N);
8583 end Resolve_Qualified_Expression;
8585 -------------------
8586 -- Resolve_Range --
8587 -------------------
8589 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
8590 L : constant Node_Id := Low_Bound (N);
8591 H : constant Node_Id := High_Bound (N);
8593 function First_Last_Ref return Boolean;
8594 -- Returns True if N is of the form X'First .. X'Last where X is the
8595 -- same entity for both attributes.
8597 --------------------
8598 -- First_Last_Ref --
8599 --------------------
8601 function First_Last_Ref return Boolean is
8602 Lorig : constant Node_Id := Original_Node (L);
8603 Horig : constant Node_Id := Original_Node (H);
8605 begin
8606 if Nkind (Lorig) = N_Attribute_Reference
8607 and then Nkind (Horig) = N_Attribute_Reference
8608 and then Attribute_Name (Lorig) = Name_First
8609 and then Attribute_Name (Horig) = Name_Last
8610 then
8611 declare
8612 PL : constant Node_Id := Prefix (Lorig);
8613 PH : constant Node_Id := Prefix (Horig);
8614 begin
8615 if Is_Entity_Name (PL)
8616 and then Is_Entity_Name (PH)
8617 and then Entity (PL) = Entity (PH)
8618 then
8619 return True;
8620 end if;
8621 end;
8622 end if;
8624 return False;
8625 end First_Last_Ref;
8627 -- Start of processing for Resolve_Range
8629 begin
8630 Set_Etype (N, Typ);
8631 Resolve (L, Typ);
8632 Resolve (H, Typ);
8634 -- Check for inappropriate range on unordered enumeration type
8636 if Bad_Unordered_Enumeration_Reference (N, Typ)
8638 -- Exclude X'First .. X'Last if X is the same entity for both
8640 and then not First_Last_Ref
8641 then
8642 Error_Msg ("subrange of unordered enumeration type?U?", Sloc (N));
8643 end if;
8645 Check_Unset_Reference (L);
8646 Check_Unset_Reference (H);
8648 -- We have to check the bounds for being within the base range as
8649 -- required for a non-static context. Normally this is automatic and
8650 -- done as part of evaluating expressions, but the N_Range node is an
8651 -- exception, since in GNAT we consider this node to be a subexpression,
8652 -- even though in Ada it is not. The circuit in Sem_Eval could check for
8653 -- this, but that would put the test on the main evaluation path for
8654 -- expressions.
8656 Check_Non_Static_Context (L);
8657 Check_Non_Static_Context (H);
8659 -- Check for an ambiguous range over character literals. This will
8660 -- happen with a membership test involving only literals.
8662 if Typ = Any_Character then
8663 Ambiguous_Character (L);
8664 Set_Etype (N, Any_Type);
8665 return;
8666 end if;
8668 -- If bounds are static, constant-fold them, so size computations are
8669 -- identical between front-end and back-end. Do not perform this
8670 -- transformation while analyzing generic units, as type information
8671 -- would be lost when reanalyzing the constant node in the instance.
8673 if Is_Discrete_Type (Typ) and then Full_Expander_Active then
8674 if Is_OK_Static_Expression (L) then
8675 Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L));
8676 end if;
8678 if Is_OK_Static_Expression (H) then
8679 Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H));
8680 end if;
8681 end if;
8682 end Resolve_Range;
8684 --------------------------
8685 -- Resolve_Real_Literal --
8686 --------------------------
8688 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
8689 Actual_Typ : constant Entity_Id := Etype (N);
8691 begin
8692 -- Special processing for fixed-point literals to make sure that the
8693 -- value is an exact multiple of small where this is required. We skip
8694 -- this for the universal real case, and also for generic types.
8696 if Is_Fixed_Point_Type (Typ)
8697 and then Typ /= Universal_Fixed
8698 and then Typ /= Any_Fixed
8699 and then not Is_Generic_Type (Typ)
8700 then
8701 declare
8702 Val : constant Ureal := Realval (N);
8703 Cintr : constant Ureal := Val / Small_Value (Typ);
8704 Cint : constant Uint := UR_Trunc (Cintr);
8705 Den : constant Uint := Norm_Den (Cintr);
8706 Stat : Boolean;
8708 begin
8709 -- Case of literal is not an exact multiple of the Small
8711 if Den /= 1 then
8713 -- For a source program literal for a decimal fixed-point type,
8714 -- this is statically illegal (RM 4.9(36)).
8716 if Is_Decimal_Fixed_Point_Type (Typ)
8717 and then Actual_Typ = Universal_Real
8718 and then Comes_From_Source (N)
8719 then
8720 Error_Msg_N ("value has extraneous low order digits", N);
8721 end if;
8723 -- Generate a warning if literal from source
8725 if Is_Static_Expression (N)
8726 and then Warn_On_Bad_Fixed_Value
8727 then
8728 Error_Msg_N
8729 ("?b?static fixed-point value is not a multiple of Small!",
8731 end if;
8733 -- Replace literal by a value that is the exact representation
8734 -- of a value of the type, i.e. a multiple of the small value,
8735 -- by truncation, since Machine_Rounds is false for all GNAT
8736 -- fixed-point types (RM 4.9(38)).
8738 Stat := Is_Static_Expression (N);
8739 Rewrite (N,
8740 Make_Real_Literal (Sloc (N),
8741 Realval => Small_Value (Typ) * Cint));
8743 Set_Is_Static_Expression (N, Stat);
8744 end if;
8746 -- In all cases, set the corresponding integer field
8748 Set_Corresponding_Integer_Value (N, Cint);
8749 end;
8750 end if;
8752 -- Now replace the actual type by the expected type as usual
8754 Set_Etype (N, Typ);
8755 Eval_Real_Literal (N);
8756 end Resolve_Real_Literal;
8758 -----------------------
8759 -- Resolve_Reference --
8760 -----------------------
8762 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
8763 P : constant Node_Id := Prefix (N);
8765 begin
8766 -- Replace general access with specific type
8768 if Ekind (Etype (N)) = E_Allocator_Type then
8769 Set_Etype (N, Base_Type (Typ));
8770 end if;
8772 Resolve (P, Designated_Type (Etype (N)));
8774 -- If we are taking the reference of a volatile entity, then treat it as
8775 -- a potential modification of this entity. This is too conservative,
8776 -- but necessary because remove side effects can cause transformations
8777 -- of normal assignments into reference sequences that otherwise fail to
8778 -- notice the modification.
8780 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
8781 Note_Possible_Modification (P, Sure => False);
8782 end if;
8783 end Resolve_Reference;
8785 --------------------------------
8786 -- Resolve_Selected_Component --
8787 --------------------------------
8789 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
8790 Comp : Entity_Id;
8791 Comp1 : Entity_Id := Empty; -- prevent junk warning
8792 P : constant Node_Id := Prefix (N);
8793 S : constant Node_Id := Selector_Name (N);
8794 T : Entity_Id := Etype (P);
8795 I : Interp_Index;
8796 I1 : Interp_Index := 0; -- prevent junk warning
8797 It : Interp;
8798 It1 : Interp;
8799 Found : Boolean;
8801 function Init_Component return Boolean;
8802 -- Check whether this is the initialization of a component within an
8803 -- init proc (by assignment or call to another init proc). If true,
8804 -- there is no need for a discriminant check.
8806 --------------------
8807 -- Init_Component --
8808 --------------------
8810 function Init_Component return Boolean is
8811 begin
8812 return Inside_Init_Proc
8813 and then Nkind (Prefix (N)) = N_Identifier
8814 and then Chars (Prefix (N)) = Name_uInit
8815 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
8816 end Init_Component;
8818 -- Start of processing for Resolve_Selected_Component
8820 begin
8821 if Is_Overloaded (P) then
8823 -- Use the context type to select the prefix that has a selector
8824 -- of the correct name and type.
8826 Found := False;
8827 Get_First_Interp (P, I, It);
8829 Search : while Present (It.Typ) loop
8830 if Is_Access_Type (It.Typ) then
8831 T := Designated_Type (It.Typ);
8832 else
8833 T := It.Typ;
8834 end if;
8836 -- Locate selected component. For a private prefix the selector
8837 -- can denote a discriminant.
8839 if Is_Record_Type (T) or else Is_Private_Type (T) then
8841 -- The visible components of a class-wide type are those of
8842 -- the root type.
8844 if Is_Class_Wide_Type (T) then
8845 T := Etype (T);
8846 end if;
8848 Comp := First_Entity (T);
8849 while Present (Comp) loop
8850 if Chars (Comp) = Chars (S)
8851 and then Covers (Etype (Comp), Typ)
8852 then
8853 if not Found then
8854 Found := True;
8855 I1 := I;
8856 It1 := It;
8857 Comp1 := Comp;
8859 else
8860 It := Disambiguate (P, I1, I, Any_Type);
8862 if It = No_Interp then
8863 Error_Msg_N
8864 ("ambiguous prefix for selected component", N);
8865 Set_Etype (N, Typ);
8866 return;
8868 else
8869 It1 := It;
8871 -- There may be an implicit dereference. Retrieve
8872 -- designated record type.
8874 if Is_Access_Type (It1.Typ) then
8875 T := Designated_Type (It1.Typ);
8876 else
8877 T := It1.Typ;
8878 end if;
8880 if Scope (Comp1) /= T then
8882 -- Resolution chooses the new interpretation.
8883 -- Find the component with the right name.
8885 Comp1 := First_Entity (T);
8886 while Present (Comp1)
8887 and then Chars (Comp1) /= Chars (S)
8888 loop
8889 Comp1 := Next_Entity (Comp1);
8890 end loop;
8891 end if;
8893 exit Search;
8894 end if;
8895 end if;
8896 end if;
8898 Comp := Next_Entity (Comp);
8899 end loop;
8900 end if;
8902 Get_Next_Interp (I, It);
8903 end loop Search;
8905 Resolve (P, It1.Typ);
8906 Set_Etype (N, Typ);
8907 Set_Entity_With_Style_Check (S, Comp1);
8909 else
8910 -- Resolve prefix with its type
8912 Resolve (P, T);
8913 end if;
8915 -- Generate cross-reference. We needed to wait until full overloading
8916 -- resolution was complete to do this, since otherwise we can't tell if
8917 -- we are an lvalue or not.
8919 if May_Be_Lvalue (N) then
8920 Generate_Reference (Entity (S), S, 'm');
8921 else
8922 Generate_Reference (Entity (S), S, 'r');
8923 end if;
8925 -- If prefix is an access type, the node will be transformed into an
8926 -- explicit dereference during expansion. The type of the node is the
8927 -- designated type of that of the prefix.
8929 if Is_Access_Type (Etype (P)) then
8930 T := Designated_Type (Etype (P));
8931 Check_Fully_Declared_Prefix (T, P);
8932 else
8933 T := Etype (P);
8934 end if;
8936 if Has_Discriminants (T)
8937 and then Ekind_In (Entity (S), E_Component, E_Discriminant)
8938 and then Present (Original_Record_Component (Entity (S)))
8939 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
8940 and then not Discriminant_Checks_Suppressed (T)
8941 and then not Init_Component
8942 then
8943 Set_Do_Discriminant_Check (N);
8944 end if;
8946 if Ekind (Entity (S)) = E_Void then
8947 Error_Msg_N ("premature use of component", S);
8948 end if;
8950 -- If the prefix is a record conversion, this may be a renamed
8951 -- discriminant whose bounds differ from those of the original
8952 -- one, so we must ensure that a range check is performed.
8954 if Nkind (P) = N_Type_Conversion
8955 and then Ekind (Entity (S)) = E_Discriminant
8956 and then Is_Discrete_Type (Typ)
8957 then
8958 Set_Etype (N, Base_Type (Typ));
8959 end if;
8961 -- Note: No Eval processing is required, because the prefix is of a
8962 -- record type, or protected type, and neither can possibly be static.
8964 -- If the array type is atomic, and is packed, and we are in a left side
8965 -- context, then this is worth a warning, since we have a situation
8966 -- where the access to the component may cause extra read/writes of the
8967 -- atomic array object, which could be considered unexpected.
8969 if Nkind (N) = N_Selected_Component
8970 and then (Is_Atomic (T)
8971 or else (Is_Entity_Name (Prefix (N))
8972 and then Is_Atomic (Entity (Prefix (N)))))
8973 and then Is_Packed (T)
8974 and then Is_LHS (N)
8975 then
8976 Error_Msg_N
8977 ("??assignment to component of packed atomic record", Prefix (N));
8978 Error_Msg_N
8979 ("\??may cause unexpected accesses to atomic object", Prefix (N));
8980 end if;
8982 Analyze_Dimension (N);
8983 end Resolve_Selected_Component;
8985 -------------------
8986 -- Resolve_Shift --
8987 -------------------
8989 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
8990 B_Typ : constant Entity_Id := Base_Type (Typ);
8991 L : constant Node_Id := Left_Opnd (N);
8992 R : constant Node_Id := Right_Opnd (N);
8994 begin
8995 -- We do the resolution using the base type, because intermediate values
8996 -- in expressions always are of the base type, not a subtype of it.
8998 Resolve (L, B_Typ);
8999 Resolve (R, Standard_Natural);
9001 Check_Unset_Reference (L);
9002 Check_Unset_Reference (R);
9004 Set_Etype (N, B_Typ);
9005 Generate_Operator_Reference (N, B_Typ);
9006 Eval_Shift (N);
9007 end Resolve_Shift;
9009 ---------------------------
9010 -- Resolve_Short_Circuit --
9011 ---------------------------
9013 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
9014 B_Typ : constant Entity_Id := Base_Type (Typ);
9015 L : constant Node_Id := Left_Opnd (N);
9016 R : constant Node_Id := Right_Opnd (N);
9018 begin
9019 -- Ensure all actions associated with the left operand (e.g.
9020 -- finalization of transient controlled objects) are fully evaluated
9021 -- locally within an expression with actions. This is particularly
9022 -- helpful for coverage analysis. However this should not happen in
9023 -- generics.
9025 if Full_Expander_Active then
9026 declare
9027 Reloc_L : constant Node_Id := Relocate_Node (L);
9028 begin
9029 Save_Interps (Old_N => L, New_N => Reloc_L);
9031 Rewrite (L,
9032 Make_Expression_With_Actions (Sloc (L),
9033 Actions => New_List,
9034 Expression => Reloc_L));
9036 -- Set Comes_From_Source on L to preserve warnings for unset
9037 -- reference.
9039 Set_Comes_From_Source (L, Comes_From_Source (Reloc_L));
9040 end;
9041 end if;
9043 Resolve (L, B_Typ);
9044 Resolve (R, B_Typ);
9046 -- Check for issuing warning for always False assert/check, this happens
9047 -- when assertions are turned off, in which case the pragma Assert/Check
9048 -- was transformed into:
9050 -- if False and then <condition> then ...
9052 -- and we detect this pattern
9054 if Warn_On_Assertion_Failure
9055 and then Is_Entity_Name (R)
9056 and then Entity (R) = Standard_False
9057 and then Nkind (Parent (N)) = N_If_Statement
9058 and then Nkind (N) = N_And_Then
9059 and then Is_Entity_Name (L)
9060 and then Entity (L) = Standard_False
9061 then
9062 declare
9063 Orig : constant Node_Id := Original_Node (Parent (N));
9065 begin
9066 -- Special handling of Asssert pragma
9068 if Nkind (Orig) = N_Pragma
9069 and then Pragma_Name (Orig) = Name_Assert
9070 then
9071 declare
9072 Expr : constant Node_Id :=
9073 Original_Node
9074 (Expression
9075 (First (Pragma_Argument_Associations (Orig))));
9077 begin
9078 -- Don't warn if original condition is explicit False,
9079 -- since obviously the failure is expected in this case.
9081 if Is_Entity_Name (Expr)
9082 and then Entity (Expr) = Standard_False
9083 then
9084 null;
9086 -- Issue warning. We do not want the deletion of the
9087 -- IF/AND-THEN to take this message with it. We achieve this
9088 -- by making sure that the expanded code points to the Sloc
9089 -- of the expression, not the original pragma.
9091 else
9092 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
9093 -- The source location of the expression is not usually
9094 -- the best choice here. For example, it gets located on
9095 -- the last AND keyword in a chain of boolean expressiond
9096 -- AND'ed together. It is best to put the message on the
9097 -- first character of the assertion, which is the effect
9098 -- of the First_Node call here.
9100 Error_Msg_F
9101 ("?A?assertion would fail at run time!",
9102 Expression
9103 (First (Pragma_Argument_Associations (Orig))));
9104 end if;
9105 end;
9107 -- Similar processing for Check pragma
9109 elsif Nkind (Orig) = N_Pragma
9110 and then Pragma_Name (Orig) = Name_Check
9111 then
9112 -- Don't want to warn if original condition is explicit False
9114 declare
9115 Expr : constant Node_Id :=
9116 Original_Node
9117 (Expression
9118 (Next (First (Pragma_Argument_Associations (Orig)))));
9119 begin
9120 if Is_Entity_Name (Expr)
9121 and then Entity (Expr) = Standard_False
9122 then
9123 null;
9125 -- Post warning
9127 else
9128 -- Again use Error_Msg_F rather than Error_Msg_N, see
9129 -- comment above for an explanation of why we do this.
9131 Error_Msg_F
9132 ("?A?check would fail at run time!",
9133 Expression
9134 (Last (Pragma_Argument_Associations (Orig))));
9135 end if;
9136 end;
9137 end if;
9138 end;
9139 end if;
9141 -- Continue with processing of short circuit
9143 Check_Unset_Reference (L);
9144 Check_Unset_Reference (R);
9146 Set_Etype (N, B_Typ);
9147 Eval_Short_Circuit (N);
9148 end Resolve_Short_Circuit;
9150 -------------------
9151 -- Resolve_Slice --
9152 -------------------
9154 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
9155 Drange : constant Node_Id := Discrete_Range (N);
9156 Name : constant Node_Id := Prefix (N);
9157 Array_Type : Entity_Id := Empty;
9158 Index_Type : Entity_Id;
9160 begin
9161 if Is_Overloaded (Name) then
9163 -- Use the context type to select the prefix that yields the correct
9164 -- array type.
9166 declare
9167 I : Interp_Index;
9168 I1 : Interp_Index := 0;
9169 It : Interp;
9170 P : constant Node_Id := Prefix (N);
9171 Found : Boolean := False;
9173 begin
9174 Get_First_Interp (P, I, It);
9175 while Present (It.Typ) loop
9176 if (Is_Array_Type (It.Typ)
9177 and then Covers (Typ, It.Typ))
9178 or else (Is_Access_Type (It.Typ)
9179 and then Is_Array_Type (Designated_Type (It.Typ))
9180 and then Covers (Typ, Designated_Type (It.Typ)))
9181 then
9182 if Found then
9183 It := Disambiguate (P, I1, I, Any_Type);
9185 if It = No_Interp then
9186 Error_Msg_N ("ambiguous prefix for slicing", N);
9187 Set_Etype (N, Typ);
9188 return;
9189 else
9190 Found := True;
9191 Array_Type := It.Typ;
9192 I1 := I;
9193 end if;
9194 else
9195 Found := True;
9196 Array_Type := It.Typ;
9197 I1 := I;
9198 end if;
9199 end if;
9201 Get_Next_Interp (I, It);
9202 end loop;
9203 end;
9205 else
9206 Array_Type := Etype (Name);
9207 end if;
9209 Resolve (Name, Array_Type);
9211 if Is_Access_Type (Array_Type) then
9212 Apply_Access_Check (N);
9213 Array_Type := Designated_Type (Array_Type);
9215 -- If the prefix is an access to an unconstrained array, we must use
9216 -- the actual subtype of the object to perform the index checks. The
9217 -- object denoted by the prefix is implicit in the node, so we build
9218 -- an explicit representation for it in order to compute the actual
9219 -- subtype.
9221 if not Is_Constrained (Array_Type) then
9222 Remove_Side_Effects (Prefix (N));
9224 declare
9225 Obj : constant Node_Id :=
9226 Make_Explicit_Dereference (Sloc (N),
9227 Prefix => New_Copy_Tree (Prefix (N)));
9228 begin
9229 Set_Etype (Obj, Array_Type);
9230 Set_Parent (Obj, Parent (N));
9231 Array_Type := Get_Actual_Subtype (Obj);
9232 end;
9233 end if;
9235 elsif Is_Entity_Name (Name)
9236 or else Nkind (Name) = N_Explicit_Dereference
9237 or else (Nkind (Name) = N_Function_Call
9238 and then not Is_Constrained (Etype (Name)))
9239 then
9240 Array_Type := Get_Actual_Subtype (Name);
9242 -- If the name is a selected component that depends on discriminants,
9243 -- build an actual subtype for it. This can happen only when the name
9244 -- itself is overloaded; otherwise the actual subtype is created when
9245 -- the selected component is analyzed.
9247 elsif Nkind (Name) = N_Selected_Component
9248 and then Full_Analysis
9249 and then Depends_On_Discriminant (First_Index (Array_Type))
9250 then
9251 declare
9252 Act_Decl : constant Node_Id :=
9253 Build_Actual_Subtype_Of_Component (Array_Type, Name);
9254 begin
9255 Insert_Action (N, Act_Decl);
9256 Array_Type := Defining_Identifier (Act_Decl);
9257 end;
9259 -- Maybe this should just be "else", instead of checking for the
9260 -- specific case of slice??? This is needed for the case where the
9261 -- prefix is an Image attribute, which gets expanded to a slice, and so
9262 -- has a constrained subtype which we want to use for the slice range
9263 -- check applied below (the range check won't get done if the
9264 -- unconstrained subtype of the 'Image is used).
9266 elsif Nkind (Name) = N_Slice then
9267 Array_Type := Etype (Name);
9268 end if;
9270 -- If name was overloaded, set slice type correctly now
9272 Set_Etype (N, Array_Type);
9274 -- If the range is specified by a subtype mark, no resolution is
9275 -- necessary. Else resolve the bounds, and apply needed checks.
9277 if not Is_Entity_Name (Drange) then
9278 if Ekind (Array_Type) = E_String_Literal_Subtype then
9279 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
9280 else
9281 Index_Type := Etype (First_Index (Array_Type));
9282 end if;
9284 Resolve (Drange, Base_Type (Index_Type));
9286 if Nkind (Drange) = N_Range then
9288 -- Ensure that side effects in the bounds are properly handled
9290 Force_Evaluation (Low_Bound (Drange));
9291 Force_Evaluation (High_Bound (Drange));
9293 -- Do not apply the range check to nodes associated with the
9294 -- frontend expansion of the dispatch table. We first check
9295 -- if Ada.Tags is already loaded to avoid the addition of an
9296 -- undesired dependence on such run-time unit.
9298 if not Tagged_Type_Expansion
9299 or else not
9300 (RTU_Loaded (Ada_Tags)
9301 and then Nkind (Prefix (N)) = N_Selected_Component
9302 and then Present (Entity (Selector_Name (Prefix (N))))
9303 and then Entity (Selector_Name (Prefix (N))) =
9304 RTE_Record_Component (RE_Prims_Ptr))
9305 then
9306 Apply_Range_Check (Drange, Index_Type);
9307 end if;
9308 end if;
9309 end if;
9311 Set_Slice_Subtype (N);
9313 -- Check bad use of type with predicates
9315 if Has_Predicates (Etype (Drange)) then
9316 Bad_Predicated_Subtype_Use
9317 ("subtype& has predicate, not allowed in slice",
9318 Drange, Etype (Drange));
9320 -- Otherwise here is where we check suspicious indexes
9322 elsif Nkind (Drange) = N_Range then
9323 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
9324 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
9325 end if;
9327 Analyze_Dimension (N);
9328 Eval_Slice (N);
9329 end Resolve_Slice;
9331 ----------------------------
9332 -- Resolve_String_Literal --
9333 ----------------------------
9335 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
9336 C_Typ : constant Entity_Id := Component_Type (Typ);
9337 R_Typ : constant Entity_Id := Root_Type (C_Typ);
9338 Loc : constant Source_Ptr := Sloc (N);
9339 Str : constant String_Id := Strval (N);
9340 Strlen : constant Nat := String_Length (Str);
9341 Subtype_Id : Entity_Id;
9342 Need_Check : Boolean;
9344 begin
9345 -- For a string appearing in a concatenation, defer creation of the
9346 -- string_literal_subtype until the end of the resolution of the
9347 -- concatenation, because the literal may be constant-folded away. This
9348 -- is a useful optimization for long concatenation expressions.
9350 -- If the string is an aggregate built for a single character (which
9351 -- happens in a non-static context) or a is null string to which special
9352 -- checks may apply, we build the subtype. Wide strings must also get a
9353 -- string subtype if they come from a one character aggregate. Strings
9354 -- generated by attributes might be static, but it is often hard to
9355 -- determine whether the enclosing context is static, so we generate
9356 -- subtypes for them as well, thus losing some rarer optimizations ???
9357 -- Same for strings that come from a static conversion.
9359 Need_Check :=
9360 (Strlen = 0 and then Typ /= Standard_String)
9361 or else Nkind (Parent (N)) /= N_Op_Concat
9362 or else (N /= Left_Opnd (Parent (N))
9363 and then N /= Right_Opnd (Parent (N)))
9364 or else ((Typ = Standard_Wide_String
9365 or else Typ = Standard_Wide_Wide_String)
9366 and then Nkind (Original_Node (N)) /= N_String_Literal);
9368 -- If the resolving type is itself a string literal subtype, we can just
9369 -- reuse it, since there is no point in creating another.
9371 if Ekind (Typ) = E_String_Literal_Subtype then
9372 Subtype_Id := Typ;
9374 elsif Nkind (Parent (N)) = N_Op_Concat
9375 and then not Need_Check
9376 and then not Nkind_In (Original_Node (N), N_Character_Literal,
9377 N_Attribute_Reference,
9378 N_Qualified_Expression,
9379 N_Type_Conversion)
9380 then
9381 Subtype_Id := Typ;
9383 -- Otherwise we must create a string literal subtype. Note that the
9384 -- whole idea of string literal subtypes is simply to avoid the need
9385 -- for building a full fledged array subtype for each literal.
9387 else
9388 Set_String_Literal_Subtype (N, Typ);
9389 Subtype_Id := Etype (N);
9390 end if;
9392 if Nkind (Parent (N)) /= N_Op_Concat
9393 or else Need_Check
9394 then
9395 Set_Etype (N, Subtype_Id);
9396 Eval_String_Literal (N);
9397 end if;
9399 if Is_Limited_Composite (Typ)
9400 or else Is_Private_Composite (Typ)
9401 then
9402 Error_Msg_N ("string literal not available for private array", N);
9403 Set_Etype (N, Any_Type);
9404 return;
9405 end if;
9407 -- The validity of a null string has been checked in the call to
9408 -- Eval_String_Literal.
9410 if Strlen = 0 then
9411 return;
9413 -- Always accept string literal with component type Any_Character, which
9414 -- occurs in error situations and in comparisons of literals, both of
9415 -- which should accept all literals.
9417 elsif R_Typ = Any_Character then
9418 return;
9420 -- If the type is bit-packed, then we always transform the string
9421 -- literal into a full fledged aggregate.
9423 elsif Is_Bit_Packed_Array (Typ) then
9424 null;
9426 -- Deal with cases of Wide_Wide_String, Wide_String, and String
9428 else
9429 -- For Standard.Wide_Wide_String, or any other type whose component
9430 -- type is Standard.Wide_Wide_Character, we know that all the
9431 -- characters in the string must be acceptable, since the parser
9432 -- accepted the characters as valid character literals.
9434 if R_Typ = Standard_Wide_Wide_Character then
9435 null;
9437 -- For the case of Standard.String, or any other type whose component
9438 -- type is Standard.Character, we must make sure that there are no
9439 -- wide characters in the string, i.e. that it is entirely composed
9440 -- of characters in range of type Character.
9442 -- If the string literal is the result of a static concatenation, the
9443 -- test has already been performed on the components, and need not be
9444 -- repeated.
9446 elsif R_Typ = Standard_Character
9447 and then Nkind (Original_Node (N)) /= N_Op_Concat
9448 then
9449 for J in 1 .. Strlen loop
9450 if not In_Character_Range (Get_String_Char (Str, J)) then
9452 -- If we are out of range, post error. This is one of the
9453 -- very few places that we place the flag in the middle of
9454 -- a token, right under the offending wide character. Not
9455 -- quite clear if this is right wrt wide character encoding
9456 -- sequences, but it's only an error message!
9458 Error_Msg
9459 ("literal out of range of type Standard.Character",
9460 Source_Ptr (Int (Loc) + J));
9461 return;
9462 end if;
9463 end loop;
9465 -- For the case of Standard.Wide_String, or any other type whose
9466 -- component type is Standard.Wide_Character, we must make sure that
9467 -- there are no wide characters in the string, i.e. that it is
9468 -- entirely composed of characters in range of type Wide_Character.
9470 -- If the string literal is the result of a static concatenation,
9471 -- the test has already been performed on the components, and need
9472 -- not be repeated.
9474 elsif R_Typ = Standard_Wide_Character
9475 and then Nkind (Original_Node (N)) /= N_Op_Concat
9476 then
9477 for J in 1 .. Strlen loop
9478 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
9480 -- If we are out of range, post error. This is one of the
9481 -- very few places that we place the flag in the middle of
9482 -- a token, right under the offending wide character.
9484 -- This is not quite right, because characters in general
9485 -- will take more than one character position ???
9487 Error_Msg
9488 ("literal out of range of type Standard.Wide_Character",
9489 Source_Ptr (Int (Loc) + J));
9490 return;
9491 end if;
9492 end loop;
9494 -- If the root type is not a standard character, then we will convert
9495 -- the string into an aggregate and will let the aggregate code do
9496 -- the checking. Standard Wide_Wide_Character is also OK here.
9498 else
9499 null;
9500 end if;
9502 -- See if the component type of the array corresponding to the string
9503 -- has compile time known bounds. If yes we can directly check
9504 -- whether the evaluation of the string will raise constraint error.
9505 -- Otherwise we need to transform the string literal into the
9506 -- corresponding character aggregate and let the aggregate code do
9507 -- the checking.
9509 if Is_Standard_Character_Type (R_Typ) then
9511 -- Check for the case of full range, where we are definitely OK
9513 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
9514 return;
9515 end if;
9517 -- Here the range is not the complete base type range, so check
9519 declare
9520 Comp_Typ_Lo : constant Node_Id :=
9521 Type_Low_Bound (Component_Type (Typ));
9522 Comp_Typ_Hi : constant Node_Id :=
9523 Type_High_Bound (Component_Type (Typ));
9525 Char_Val : Uint;
9527 begin
9528 if Compile_Time_Known_Value (Comp_Typ_Lo)
9529 and then Compile_Time_Known_Value (Comp_Typ_Hi)
9530 then
9531 for J in 1 .. Strlen loop
9532 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
9534 if Char_Val < Expr_Value (Comp_Typ_Lo)
9535 or else Char_Val > Expr_Value (Comp_Typ_Hi)
9536 then
9537 Apply_Compile_Time_Constraint_Error
9538 (N, "character out of range??",
9539 CE_Range_Check_Failed,
9540 Loc => Source_Ptr (Int (Loc) + J));
9541 end if;
9542 end loop;
9544 return;
9545 end if;
9546 end;
9547 end if;
9548 end if;
9550 -- If we got here we meed to transform the string literal into the
9551 -- equivalent qualified positional array aggregate. This is rather
9552 -- heavy artillery for this situation, but it is hard work to avoid.
9554 declare
9555 Lits : constant List_Id := New_List;
9556 P : Source_Ptr := Loc + 1;
9557 C : Char_Code;
9559 begin
9560 -- Build the character literals, we give them source locations that
9561 -- correspond to the string positions, which is a bit tricky given
9562 -- the possible presence of wide character escape sequences.
9564 for J in 1 .. Strlen loop
9565 C := Get_String_Char (Str, J);
9566 Set_Character_Literal_Name (C);
9568 Append_To (Lits,
9569 Make_Character_Literal (P,
9570 Chars => Name_Find,
9571 Char_Literal_Value => UI_From_CC (C)));
9573 if In_Character_Range (C) then
9574 P := P + 1;
9576 -- Should we have a call to Skip_Wide here ???
9578 -- ??? else
9579 -- Skip_Wide (P);
9581 end if;
9582 end loop;
9584 Rewrite (N,
9585 Make_Qualified_Expression (Loc,
9586 Subtype_Mark => New_Reference_To (Typ, Loc),
9587 Expression =>
9588 Make_Aggregate (Loc, Expressions => Lits)));
9590 Analyze_And_Resolve (N, Typ);
9591 end;
9592 end Resolve_String_Literal;
9594 -----------------------------
9595 -- Resolve_Subprogram_Info --
9596 -----------------------------
9598 procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
9599 begin
9600 Set_Etype (N, Typ);
9601 end Resolve_Subprogram_Info;
9603 -----------------------------
9604 -- Resolve_Type_Conversion --
9605 -----------------------------
9607 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
9608 Conv_OK : constant Boolean := Conversion_OK (N);
9609 Operand : constant Node_Id := Expression (N);
9610 Operand_Typ : constant Entity_Id := Etype (Operand);
9611 Target_Typ : constant Entity_Id := Etype (N);
9612 Rop : Node_Id;
9613 Orig_N : Node_Id;
9614 Orig_T : Node_Id;
9616 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
9617 -- Set to False to suppress cases where we want to suppress the test
9618 -- for redundancy to avoid possible false positives on this warning.
9620 begin
9621 if not Conv_OK
9622 and then not Valid_Conversion (N, Target_Typ, Operand)
9623 then
9624 return;
9625 end if;
9627 -- If the Operand Etype is Universal_Fixed, then the conversion is
9628 -- never redundant. We need this check because by the time we have
9629 -- finished the rather complex transformation, the conversion looks
9630 -- redundant when it is not.
9632 if Operand_Typ = Universal_Fixed then
9633 Test_Redundant := False;
9635 -- If the operand is marked as Any_Fixed, then special processing is
9636 -- required. This is also a case where we suppress the test for a
9637 -- redundant conversion, since most certainly it is not redundant.
9639 elsif Operand_Typ = Any_Fixed then
9640 Test_Redundant := False;
9642 -- Mixed-mode operation involving a literal. Context must be a fixed
9643 -- type which is applied to the literal subsequently.
9645 if Is_Fixed_Point_Type (Typ) then
9646 Set_Etype (Operand, Universal_Real);
9648 elsif Is_Numeric_Type (Typ)
9649 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
9650 and then (Etype (Right_Opnd (Operand)) = Universal_Real
9651 or else
9652 Etype (Left_Opnd (Operand)) = Universal_Real)
9653 then
9654 -- Return if expression is ambiguous
9656 if Unique_Fixed_Point_Type (N) = Any_Type then
9657 return;
9659 -- If nothing else, the available fixed type is Duration
9661 else
9662 Set_Etype (Operand, Standard_Duration);
9663 end if;
9665 -- Resolve the real operand with largest available precision
9667 if Etype (Right_Opnd (Operand)) = Universal_Real then
9668 Rop := New_Copy_Tree (Right_Opnd (Operand));
9669 else
9670 Rop := New_Copy_Tree (Left_Opnd (Operand));
9671 end if;
9673 Resolve (Rop, Universal_Real);
9675 -- If the operand is a literal (it could be a non-static and
9676 -- illegal exponentiation) check whether the use of Duration
9677 -- is potentially inaccurate.
9679 if Nkind (Rop) = N_Real_Literal
9680 and then Realval (Rop) /= Ureal_0
9681 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
9682 then
9683 Error_Msg_N
9684 ("??universal real operand can only "
9685 & "be interpreted as Duration!", Rop);
9686 Error_Msg_N
9687 ("\??precision will be lost in the conversion!", Rop);
9688 end if;
9690 elsif Is_Numeric_Type (Typ)
9691 and then Nkind (Operand) in N_Op
9692 and then Unique_Fixed_Point_Type (N) /= Any_Type
9693 then
9694 Set_Etype (Operand, Standard_Duration);
9696 else
9697 Error_Msg_N ("invalid context for mixed mode operation", N);
9698 Set_Etype (Operand, Any_Type);
9699 return;
9700 end if;
9701 end if;
9703 Resolve (Operand);
9705 -- In SPARK, a type conversion between array types should be restricted
9706 -- to types which have matching static bounds.
9708 -- Protect call to Matching_Static_Array_Bounds to avoid costly
9709 -- operation if not needed.
9711 if Restriction_Check_Required (SPARK_05)
9712 and then Is_Array_Type (Target_Typ)
9713 and then Is_Array_Type (Operand_Typ)
9714 and then Operand_Typ /= Any_Composite -- or else Operand in error
9715 and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ)
9716 then
9717 Check_SPARK_Restriction
9718 ("array types should have matching static bounds", N);
9719 end if;
9721 -- In formal mode, the operand of an ancestor type conversion must be an
9722 -- object (not an expression).
9724 if Is_Tagged_Type (Target_Typ)
9725 and then not Is_Class_Wide_Type (Target_Typ)
9726 and then Is_Tagged_Type (Operand_Typ)
9727 and then not Is_Class_Wide_Type (Operand_Typ)
9728 and then Is_Ancestor (Target_Typ, Operand_Typ)
9729 and then not Is_SPARK_Object_Reference (Operand)
9730 then
9731 Check_SPARK_Restriction ("object required", Operand);
9732 end if;
9734 Analyze_Dimension (N);
9736 -- Note: we do the Eval_Type_Conversion call before applying the
9737 -- required checks for a subtype conversion. This is important, since
9738 -- both are prepared under certain circumstances to change the type
9739 -- conversion to a constraint error node, but in the case of
9740 -- Eval_Type_Conversion this may reflect an illegality in the static
9741 -- case, and we would miss the illegality (getting only a warning
9742 -- message), if we applied the type conversion checks first.
9744 Eval_Type_Conversion (N);
9746 -- Even when evaluation is not possible, we may be able to simplify the
9747 -- conversion or its expression. This needs to be done before applying
9748 -- checks, since otherwise the checks may use the original expression
9749 -- and defeat the simplifications. This is specifically the case for
9750 -- elimination of the floating-point Truncation attribute in
9751 -- float-to-int conversions.
9753 Simplify_Type_Conversion (N);
9755 -- If after evaluation we still have a type conversion, then we may need
9756 -- to apply checks required for a subtype conversion.
9758 -- Skip these type conversion checks if universal fixed operands
9759 -- operands involved, since range checks are handled separately for
9760 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
9762 if Nkind (N) = N_Type_Conversion
9763 and then not Is_Generic_Type (Root_Type (Target_Typ))
9764 and then Target_Typ /= Universal_Fixed
9765 and then Operand_Typ /= Universal_Fixed
9766 then
9767 Apply_Type_Conversion_Checks (N);
9768 end if;
9770 -- Issue warning for conversion of simple object to its own type. We
9771 -- have to test the original nodes, since they may have been rewritten
9772 -- by various optimizations.
9774 Orig_N := Original_Node (N);
9776 -- Here we test for a redundant conversion if the warning mode is
9777 -- active (and was not locally reset), and we have a type conversion
9778 -- from source not appearing in a generic instance.
9780 if Test_Redundant
9781 and then Nkind (Orig_N) = N_Type_Conversion
9782 and then Comes_From_Source (Orig_N)
9783 and then not In_Instance
9784 then
9785 Orig_N := Original_Node (Expression (Orig_N));
9786 Orig_T := Target_Typ;
9788 -- If the node is part of a larger expression, the Target_Type
9789 -- may not be the original type of the node if the context is a
9790 -- condition. Recover original type to see if conversion is needed.
9792 if Is_Boolean_Type (Orig_T)
9793 and then Nkind (Parent (N)) in N_Op
9794 then
9795 Orig_T := Etype (Parent (N));
9796 end if;
9798 -- If we have an entity name, then give the warning if the entity
9799 -- is the right type, or if it is a loop parameter covered by the
9800 -- original type (that's needed because loop parameters have an
9801 -- odd subtype coming from the bounds).
9803 if (Is_Entity_Name (Orig_N)
9804 and then
9805 (Etype (Entity (Orig_N)) = Orig_T
9806 or else
9807 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
9808 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
9810 -- If not an entity, then type of expression must match
9812 or else Etype (Orig_N) = Orig_T
9813 then
9814 -- One more check, do not give warning if the analyzed conversion
9815 -- has an expression with non-static bounds, and the bounds of the
9816 -- target are static. This avoids junk warnings in cases where the
9817 -- conversion is necessary to establish staticness, for example in
9818 -- a case statement.
9820 if not Is_OK_Static_Subtype (Operand_Typ)
9821 and then Is_OK_Static_Subtype (Target_Typ)
9822 then
9823 null;
9825 -- Finally, if this type conversion occurs in a context requiring
9826 -- a prefix, and the expression is a qualified expression then the
9827 -- type conversion is not redundant, since a qualified expression
9828 -- is not a prefix, whereas a type conversion is. For example, "X
9829 -- := T'(Funx(...)).Y;" is illegal because a selected component
9830 -- requires a prefix, but a type conversion makes it legal: "X :=
9831 -- T(T'(Funx(...))).Y;"
9833 -- In Ada 2012, a qualified expression is a name, so this idiom is
9834 -- no longer needed, but we still suppress the warning because it
9835 -- seems unfriendly for warnings to pop up when you switch to the
9836 -- newer language version.
9838 elsif Nkind (Orig_N) = N_Qualified_Expression
9839 and then Nkind_In (Parent (N), N_Attribute_Reference,
9840 N_Indexed_Component,
9841 N_Selected_Component,
9842 N_Slice,
9843 N_Explicit_Dereference)
9844 then
9845 null;
9847 -- Never warn on conversion to Long_Long_Integer'Base since
9848 -- that is most likely an artifact of the extended overflow
9849 -- checking and comes from complex expanded code.
9851 elsif Orig_T = Base_Type (Standard_Long_Long_Integer) then
9852 null;
9854 -- Here we give the redundant conversion warning. If it is an
9855 -- entity, give the name of the entity in the message. If not,
9856 -- just mention the expression.
9858 -- Shoudn't we test Warn_On_Redundant_Constructs here ???
9860 else
9861 if Is_Entity_Name (Orig_N) then
9862 Error_Msg_Node_2 := Orig_T;
9863 Error_Msg_NE -- CODEFIX
9864 ("??redundant conversion, & is of type &!",
9865 N, Entity (Orig_N));
9866 else
9867 Error_Msg_NE
9868 ("??redundant conversion, expression is of type&!",
9869 N, Orig_T);
9870 end if;
9871 end if;
9872 end if;
9873 end if;
9875 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
9876 -- No need to perform any interface conversion if the type of the
9877 -- expression coincides with the target type.
9879 if Ada_Version >= Ada_2005
9880 and then Full_Expander_Active
9881 and then Operand_Typ /= Target_Typ
9882 then
9883 declare
9884 Opnd : Entity_Id := Operand_Typ;
9885 Target : Entity_Id := Target_Typ;
9887 begin
9888 if Is_Access_Type (Opnd) then
9889 Opnd := Designated_Type (Opnd);
9890 end if;
9892 if Is_Access_Type (Target_Typ) then
9893 Target := Designated_Type (Target);
9894 end if;
9896 if Opnd = Target then
9897 null;
9899 -- Conversion from interface type
9901 elsif Is_Interface (Opnd) then
9903 -- Ada 2005 (AI-217): Handle entities from limited views
9905 if From_Limited_With (Opnd) then
9906 Error_Msg_Qual_Level := 99;
9907 Error_Msg_NE -- CODEFIX
9908 ("missing WITH clause on package &", N,
9909 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
9910 Error_Msg_N
9911 ("type conversions require visibility of the full view",
9914 elsif From_Limited_With (Target)
9915 and then not
9916 (Is_Access_Type (Target_Typ)
9917 and then Present (Non_Limited_View (Etype (Target))))
9918 then
9919 Error_Msg_Qual_Level := 99;
9920 Error_Msg_NE -- CODEFIX
9921 ("missing WITH clause on package &", N,
9922 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
9923 Error_Msg_N
9924 ("type conversions require visibility of the full view",
9927 else
9928 Expand_Interface_Conversion (N);
9929 end if;
9931 -- Conversion to interface type
9933 elsif Is_Interface (Target) then
9935 -- Handle subtypes
9937 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
9938 Opnd := Etype (Opnd);
9939 end if;
9941 if Is_Class_Wide_Type (Opnd)
9942 or else Interface_Present_In_Ancestor
9943 (Typ => Opnd,
9944 Iface => Target)
9945 then
9946 Expand_Interface_Conversion (N);
9947 else
9948 Error_Msg_Name_1 := Chars (Etype (Target));
9949 Error_Msg_Name_2 := Chars (Opnd);
9950 Error_Msg_N
9951 ("wrong interface conversion (% is not a progenitor "
9952 & "of %)", N);
9953 end if;
9954 end if;
9955 end;
9956 end if;
9958 -- Ada 2012: if target type has predicates, the result requires a
9959 -- predicate check. If the context is a call to another predicate
9960 -- check we must prevent infinite recursion.
9962 if Has_Predicates (Target_Typ) then
9963 if Nkind (Parent (N)) = N_Function_Call
9964 and then Present (Name (Parent (N)))
9965 and then (Is_Predicate_Function (Entity (Name (Parent (N))))
9966 or else
9967 Is_Predicate_Function_M (Entity (Name (Parent (N)))))
9968 then
9969 null;
9971 else
9972 Apply_Predicate_Check (N, Target_Typ);
9973 end if;
9974 end if;
9975 end Resolve_Type_Conversion;
9977 ----------------------
9978 -- Resolve_Unary_Op --
9979 ----------------------
9981 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
9982 B_Typ : constant Entity_Id := Base_Type (Typ);
9983 R : constant Node_Id := Right_Opnd (N);
9984 OK : Boolean;
9985 Lo : Uint;
9986 Hi : Uint;
9988 begin
9989 if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then
9990 Error_Msg_Name_1 := Chars (Typ);
9991 Check_SPARK_Restriction
9992 ("unary operator not defined for modular type%", N);
9993 end if;
9995 -- Deal with intrinsic unary operators
9997 if Comes_From_Source (N)
9998 and then Ekind (Entity (N)) = E_Function
9999 and then Is_Imported (Entity (N))
10000 and then Is_Intrinsic_Subprogram (Entity (N))
10001 then
10002 Resolve_Intrinsic_Unary_Operator (N, Typ);
10003 return;
10004 end if;
10006 -- Deal with universal cases
10008 if Etype (R) = Universal_Integer
10009 or else
10010 Etype (R) = Universal_Real
10011 then
10012 Check_For_Visible_Operator (N, B_Typ);
10013 end if;
10015 Set_Etype (N, B_Typ);
10016 Resolve (R, B_Typ);
10018 -- Generate warning for expressions like abs (x mod 2)
10020 if Warn_On_Redundant_Constructs
10021 and then Nkind (N) = N_Op_Abs
10022 then
10023 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
10025 if OK and then Hi >= Lo and then Lo >= 0 then
10026 Error_Msg_N -- CODEFIX
10027 ("?r?abs applied to known non-negative value has no effect", N);
10028 end if;
10029 end if;
10031 -- Deal with reference generation
10033 Check_Unset_Reference (R);
10034 Generate_Operator_Reference (N, B_Typ);
10035 Analyze_Dimension (N);
10036 Eval_Unary_Op (N);
10038 -- Set overflow checking bit. Much cleverer code needed here eventually
10039 -- and perhaps the Resolve routines should be separated for the various
10040 -- arithmetic operations, since they will need different processing ???
10042 if Nkind (N) in N_Op then
10043 if not Overflow_Checks_Suppressed (Etype (N)) then
10044 Enable_Overflow_Check (N);
10045 end if;
10046 end if;
10048 -- Generate warning for expressions like -5 mod 3 for integers. No need
10049 -- to worry in the floating-point case, since parens do not affect the
10050 -- result so there is no point in giving in a warning.
10052 declare
10053 Norig : constant Node_Id := Original_Node (N);
10054 Rorig : Node_Id;
10055 Val : Uint;
10056 HB : Uint;
10057 LB : Uint;
10058 Lval : Uint;
10059 Opnd : Node_Id;
10061 begin
10062 if Warn_On_Questionable_Missing_Parens
10063 and then Comes_From_Source (Norig)
10064 and then Is_Integer_Type (Typ)
10065 and then Nkind (Norig) = N_Op_Minus
10066 then
10067 Rorig := Original_Node (Right_Opnd (Norig));
10069 -- We are looking for cases where the right operand is not
10070 -- parenthesized, and is a binary operator, multiply, divide, or
10071 -- mod. These are the cases where the grouping can affect results.
10073 if Paren_Count (Rorig) = 0
10074 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
10075 then
10076 -- For mod, we always give the warning, since the value is
10077 -- affected by the parenthesization (e.g. (-5) mod 315 /=
10078 -- -(5 mod 315)). But for the other cases, the only concern is
10079 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
10080 -- overflows, but (-2) * 64 does not). So we try to give the
10081 -- message only when overflow is possible.
10083 if Nkind (Rorig) /= N_Op_Mod
10084 and then Compile_Time_Known_Value (R)
10085 then
10086 Val := Expr_Value (R);
10088 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
10089 HB := Expr_Value (Type_High_Bound (Typ));
10090 else
10091 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
10092 end if;
10094 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
10095 LB := Expr_Value (Type_Low_Bound (Typ));
10096 else
10097 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
10098 end if;
10100 -- Note that the test below is deliberately excluding the
10101 -- largest negative number, since that is a potentially
10102 -- troublesome case (e.g. -2 * x, where the result is the
10103 -- largest negative integer has an overflow with 2 * x).
10105 if Val > LB and then Val <= HB then
10106 return;
10107 end if;
10108 end if;
10110 -- For the multiplication case, the only case we have to worry
10111 -- about is when (-a)*b is exactly the largest negative number
10112 -- so that -(a*b) can cause overflow. This can only happen if
10113 -- a is a power of 2, and more generally if any operand is a
10114 -- constant that is not a power of 2, then the parentheses
10115 -- cannot affect whether overflow occurs. We only bother to
10116 -- test the left most operand
10118 -- Loop looking at left operands for one that has known value
10120 Opnd := Rorig;
10121 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
10122 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
10123 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
10125 -- Operand value of 0 or 1 skips warning
10127 if Lval <= 1 then
10128 return;
10130 -- Otherwise check power of 2, if power of 2, warn, if
10131 -- anything else, skip warning.
10133 else
10134 while Lval /= 2 loop
10135 if Lval mod 2 = 1 then
10136 return;
10137 else
10138 Lval := Lval / 2;
10139 end if;
10140 end loop;
10142 exit Opnd_Loop;
10143 end if;
10144 end if;
10146 -- Keep looking at left operands
10148 Opnd := Left_Opnd (Opnd);
10149 end loop Opnd_Loop;
10151 -- For rem or "/" we can only have a problematic situation
10152 -- if the divisor has a value of minus one or one. Otherwise
10153 -- overflow is impossible (divisor > 1) or we have a case of
10154 -- division by zero in any case.
10156 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
10157 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
10158 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
10159 then
10160 return;
10161 end if;
10163 -- If we fall through warning should be issued
10165 -- Shouldn't we test Warn_On_Questionable_Missing_Parens ???
10167 Error_Msg_N
10168 ("??unary minus expression should be parenthesized here!", N);
10169 end if;
10170 end if;
10171 end;
10172 end Resolve_Unary_Op;
10174 ----------------------------------
10175 -- Resolve_Unchecked_Expression --
10176 ----------------------------------
10178 procedure Resolve_Unchecked_Expression
10179 (N : Node_Id;
10180 Typ : Entity_Id)
10182 begin
10183 Resolve (Expression (N), Typ, Suppress => All_Checks);
10184 Set_Etype (N, Typ);
10185 end Resolve_Unchecked_Expression;
10187 ---------------------------------------
10188 -- Resolve_Unchecked_Type_Conversion --
10189 ---------------------------------------
10191 procedure Resolve_Unchecked_Type_Conversion
10192 (N : Node_Id;
10193 Typ : Entity_Id)
10195 pragma Warnings (Off, Typ);
10197 Operand : constant Node_Id := Expression (N);
10198 Opnd_Type : constant Entity_Id := Etype (Operand);
10200 begin
10201 -- Resolve operand using its own type
10203 Resolve (Operand, Opnd_Type);
10204 Analyze_Dimension (N);
10205 Eval_Unchecked_Conversion (N);
10206 end Resolve_Unchecked_Type_Conversion;
10208 ------------------------------
10209 -- Rewrite_Operator_As_Call --
10210 ------------------------------
10212 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
10213 Loc : constant Source_Ptr := Sloc (N);
10214 Actuals : constant List_Id := New_List;
10215 New_N : Node_Id;
10217 begin
10218 if Nkind (N) in N_Binary_Op then
10219 Append (Left_Opnd (N), Actuals);
10220 end if;
10222 Append (Right_Opnd (N), Actuals);
10224 New_N :=
10225 Make_Function_Call (Sloc => Loc,
10226 Name => New_Occurrence_Of (Nam, Loc),
10227 Parameter_Associations => Actuals);
10229 Preserve_Comes_From_Source (New_N, N);
10230 Preserve_Comes_From_Source (Name (New_N), N);
10231 Rewrite (N, New_N);
10232 Set_Etype (N, Etype (Nam));
10233 end Rewrite_Operator_As_Call;
10235 ------------------------------
10236 -- Rewrite_Renamed_Operator --
10237 ------------------------------
10239 procedure Rewrite_Renamed_Operator
10240 (N : Node_Id;
10241 Op : Entity_Id;
10242 Typ : Entity_Id)
10244 Nam : constant Name_Id := Chars (Op);
10245 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
10246 Op_Node : Node_Id;
10248 begin
10249 -- Rewrite the operator node using the real operator, not its renaming.
10250 -- Exclude user-defined intrinsic operations of the same name, which are
10251 -- treated separately and rewritten as calls.
10253 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
10254 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
10255 Set_Chars (Op_Node, Nam);
10256 Set_Etype (Op_Node, Etype (N));
10257 Set_Entity (Op_Node, Op);
10258 Set_Right_Opnd (Op_Node, Right_Opnd (N));
10260 -- Indicate that both the original entity and its renaming are
10261 -- referenced at this point.
10263 Generate_Reference (Entity (N), N);
10264 Generate_Reference (Op, N);
10266 if Is_Binary then
10267 Set_Left_Opnd (Op_Node, Left_Opnd (N));
10268 end if;
10270 Rewrite (N, Op_Node);
10272 -- If the context type is private, add the appropriate conversions so
10273 -- that the operator is applied to the full view. This is done in the
10274 -- routines that resolve intrinsic operators.
10276 if Is_Intrinsic_Subprogram (Op)
10277 and then Is_Private_Type (Typ)
10278 then
10279 case Nkind (N) is
10280 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
10281 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
10282 Resolve_Intrinsic_Operator (N, Typ);
10284 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
10285 Resolve_Intrinsic_Unary_Operator (N, Typ);
10287 when others =>
10288 Resolve (N, Typ);
10289 end case;
10290 end if;
10292 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
10294 -- Operator renames a user-defined operator of the same name. Use the
10295 -- original operator in the node, which is the one Gigi knows about.
10297 Set_Entity (N, Op);
10298 Set_Is_Overloaded (N, False);
10299 end if;
10300 end Rewrite_Renamed_Operator;
10302 -----------------------
10303 -- Set_Slice_Subtype --
10304 -----------------------
10306 -- Build an implicit subtype declaration to represent the type delivered by
10307 -- the slice. This is an abbreviated version of an array subtype. We define
10308 -- an index subtype for the slice, using either the subtype name or the
10309 -- discrete range of the slice. To be consistent with index usage elsewhere
10310 -- we create a list header to hold the single index. This list is not
10311 -- otherwise attached to the syntax tree.
10313 procedure Set_Slice_Subtype (N : Node_Id) is
10314 Loc : constant Source_Ptr := Sloc (N);
10315 Index_List : constant List_Id := New_List;
10316 Index : Node_Id;
10317 Index_Subtype : Entity_Id;
10318 Index_Type : Entity_Id;
10319 Slice_Subtype : Entity_Id;
10320 Drange : constant Node_Id := Discrete_Range (N);
10322 begin
10323 if Is_Entity_Name (Drange) then
10324 Index_Subtype := Entity (Drange);
10326 else
10327 -- We force the evaluation of a range. This is definitely needed in
10328 -- the renamed case, and seems safer to do unconditionally. Note in
10329 -- any case that since we will create and insert an Itype referring
10330 -- to this range, we must make sure any side effect removal actions
10331 -- are inserted before the Itype definition.
10333 if Nkind (Drange) = N_Range then
10334 Force_Evaluation (Low_Bound (Drange));
10335 Force_Evaluation (High_Bound (Drange));
10336 end if;
10338 Index_Type := Base_Type (Etype (Drange));
10340 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
10342 -- Take a new copy of Drange (where bounds have been rewritten to
10343 -- reference side-effect-free names). Using a separate tree ensures
10344 -- that further expansion (e.g. while rewriting a slice assignment
10345 -- into a FOR loop) does not attempt to remove side effects on the
10346 -- bounds again (which would cause the bounds in the index subtype
10347 -- definition to refer to temporaries before they are defined) (the
10348 -- reason is that some names are considered side effect free here
10349 -- for the subtype, but not in the context of a loop iteration
10350 -- scheme).
10352 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
10353 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
10354 Set_Etype (Index_Subtype, Index_Type);
10355 Set_Size_Info (Index_Subtype, Index_Type);
10356 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
10357 end if;
10359 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
10361 Index := New_Occurrence_Of (Index_Subtype, Loc);
10362 Set_Etype (Index, Index_Subtype);
10363 Append (Index, Index_List);
10365 Set_First_Index (Slice_Subtype, Index);
10366 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
10367 Set_Is_Constrained (Slice_Subtype, True);
10369 Check_Compile_Time_Size (Slice_Subtype);
10371 -- The Etype of the existing Slice node is reset to this slice subtype.
10372 -- Its bounds are obtained from its first index.
10374 Set_Etype (N, Slice_Subtype);
10376 -- For packed slice subtypes, freeze immediately (except in the case of
10377 -- being in a "spec expression" where we never freeze when we first see
10378 -- the expression).
10380 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
10381 Freeze_Itype (Slice_Subtype, N);
10383 -- For all other cases insert an itype reference in the slice's actions
10384 -- so that the itype is frozen at the proper place in the tree (i.e. at
10385 -- the point where actions for the slice are analyzed). Note that this
10386 -- is different from freezing the itype immediately, which might be
10387 -- premature (e.g. if the slice is within a transient scope). This needs
10388 -- to be done only if expansion is enabled.
10390 elsif Full_Expander_Active then
10391 Ensure_Defined (Typ => Slice_Subtype, N => N);
10392 end if;
10393 end Set_Slice_Subtype;
10395 --------------------------------
10396 -- Set_String_Literal_Subtype --
10397 --------------------------------
10399 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
10400 Loc : constant Source_Ptr := Sloc (N);
10401 Low_Bound : constant Node_Id :=
10402 Type_Low_Bound (Etype (First_Index (Typ)));
10403 Subtype_Id : Entity_Id;
10405 begin
10406 if Nkind (N) /= N_String_Literal then
10407 return;
10408 end if;
10410 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
10411 Set_String_Literal_Length (Subtype_Id, UI_From_Int
10412 (String_Length (Strval (N))));
10413 Set_Etype (Subtype_Id, Base_Type (Typ));
10414 Set_Is_Constrained (Subtype_Id);
10415 Set_Etype (N, Subtype_Id);
10417 -- The low bound is set from the low bound of the corresponding index
10418 -- type. Note that we do not store the high bound in the string literal
10419 -- subtype, but it can be deduced if necessary from the length and the
10420 -- low bound.
10422 if Is_OK_Static_Expression (Low_Bound) then
10423 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
10425 -- If the lower bound is not static we create a range for the string
10426 -- literal, using the index type and the known length of the literal.
10427 -- The index type is not necessarily Positive, so the upper bound is
10428 -- computed as T'Val (T'Pos (Low_Bound) + L - 1).
10430 else
10431 declare
10432 Index_List : constant List_Id := New_List;
10433 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
10434 High_Bound : constant Node_Id :=
10435 Make_Attribute_Reference (Loc,
10436 Attribute_Name => Name_Val,
10437 Prefix =>
10438 New_Occurrence_Of (Index_Type, Loc),
10439 Expressions => New_List (
10440 Make_Op_Add (Loc,
10441 Left_Opnd =>
10442 Make_Attribute_Reference (Loc,
10443 Attribute_Name => Name_Pos,
10444 Prefix =>
10445 New_Occurrence_Of (Index_Type, Loc),
10446 Expressions =>
10447 New_List (New_Copy_Tree (Low_Bound))),
10448 Right_Opnd =>
10449 Make_Integer_Literal (Loc,
10450 String_Length (Strval (N)) - 1))));
10452 Array_Subtype : Entity_Id;
10453 Drange : Node_Id;
10454 Index : Node_Id;
10455 Index_Subtype : Entity_Id;
10457 begin
10458 if Is_Integer_Type (Index_Type) then
10459 Set_String_Literal_Low_Bound
10460 (Subtype_Id, Make_Integer_Literal (Loc, 1));
10462 else
10463 -- If the index type is an enumeration type, build bounds
10464 -- expression with attributes.
10466 Set_String_Literal_Low_Bound
10467 (Subtype_Id,
10468 Make_Attribute_Reference (Loc,
10469 Attribute_Name => Name_First,
10470 Prefix =>
10471 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
10472 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Index_Type);
10473 end if;
10475 Analyze_And_Resolve (String_Literal_Low_Bound (Subtype_Id));
10477 -- Build bona fide subtype for the string, and wrap it in an
10478 -- unchecked conversion, because the backend expects the
10479 -- String_Literal_Subtype to have a static lower bound.
10481 Index_Subtype :=
10482 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
10483 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
10484 Set_Scalar_Range (Index_Subtype, Drange);
10485 Set_Parent (Drange, N);
10486 Analyze_And_Resolve (Drange, Index_Type);
10488 -- In the context, the Index_Type may already have a constraint,
10489 -- so use common base type on string subtype. The base type may
10490 -- be used when generating attributes of the string, for example
10491 -- in the context of a slice assignment.
10493 Set_Etype (Index_Subtype, Base_Type (Index_Type));
10494 Set_Size_Info (Index_Subtype, Index_Type);
10495 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
10497 Array_Subtype := Create_Itype (E_Array_Subtype, N);
10499 Index := New_Occurrence_Of (Index_Subtype, Loc);
10500 Set_Etype (Index, Index_Subtype);
10501 Append (Index, Index_List);
10503 Set_First_Index (Array_Subtype, Index);
10504 Set_Etype (Array_Subtype, Base_Type (Typ));
10505 Set_Is_Constrained (Array_Subtype, True);
10507 Rewrite (N,
10508 Make_Unchecked_Type_Conversion (Loc,
10509 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
10510 Expression => Relocate_Node (N)));
10511 Set_Etype (N, Array_Subtype);
10512 end;
10513 end if;
10514 end Set_String_Literal_Subtype;
10516 ------------------------------
10517 -- Simplify_Type_Conversion --
10518 ------------------------------
10520 procedure Simplify_Type_Conversion (N : Node_Id) is
10521 begin
10522 if Nkind (N) = N_Type_Conversion then
10523 declare
10524 Operand : constant Node_Id := Expression (N);
10525 Target_Typ : constant Entity_Id := Etype (N);
10526 Opnd_Typ : constant Entity_Id := Etype (Operand);
10528 begin
10529 if Is_Floating_Point_Type (Opnd_Typ)
10530 and then
10531 (Is_Integer_Type (Target_Typ)
10532 or else (Is_Fixed_Point_Type (Target_Typ)
10533 and then Conversion_OK (N)))
10534 and then Nkind (Operand) = N_Attribute_Reference
10535 and then Attribute_Name (Operand) = Name_Truncation
10537 -- Special processing required if the conversion is the expression
10538 -- of a Truncation attribute reference. In this case we replace:
10540 -- ityp (ftyp'Truncation (x))
10542 -- by
10544 -- ityp (x)
10546 -- with the Float_Truncate flag set, which is more efficient.
10548 then
10549 Rewrite (Operand,
10550 Relocate_Node (First (Expressions (Operand))));
10551 Set_Float_Truncate (N, True);
10552 end if;
10553 end;
10554 end if;
10555 end Simplify_Type_Conversion;
10557 -----------------------------
10558 -- Unique_Fixed_Point_Type --
10559 -----------------------------
10561 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
10562 T1 : Entity_Id := Empty;
10563 T2 : Entity_Id;
10564 Item : Node_Id;
10565 Scop : Entity_Id;
10567 procedure Fixed_Point_Error;
10568 -- Give error messages for true ambiguity. Messages are posted on node
10569 -- N, and entities T1, T2 are the possible interpretations.
10571 -----------------------
10572 -- Fixed_Point_Error --
10573 -----------------------
10575 procedure Fixed_Point_Error is
10576 begin
10577 Error_Msg_N ("ambiguous universal_fixed_expression", N);
10578 Error_Msg_NE ("\\possible interpretation as}", N, T1);
10579 Error_Msg_NE ("\\possible interpretation as}", N, T2);
10580 end Fixed_Point_Error;
10582 -- Start of processing for Unique_Fixed_Point_Type
10584 begin
10585 -- The operations on Duration are visible, so Duration is always a
10586 -- possible interpretation.
10588 T1 := Standard_Duration;
10590 -- Look for fixed-point types in enclosing scopes
10592 Scop := Current_Scope;
10593 while Scop /= Standard_Standard loop
10594 T2 := First_Entity (Scop);
10595 while Present (T2) loop
10596 if Is_Fixed_Point_Type (T2)
10597 and then Current_Entity (T2) = T2
10598 and then Scope (Base_Type (T2)) = Scop
10599 then
10600 if Present (T1) then
10601 Fixed_Point_Error;
10602 return Any_Type;
10603 else
10604 T1 := T2;
10605 end if;
10606 end if;
10608 Next_Entity (T2);
10609 end loop;
10611 Scop := Scope (Scop);
10612 end loop;
10614 -- Look for visible fixed type declarations in the context
10616 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
10617 while Present (Item) loop
10618 if Nkind (Item) = N_With_Clause then
10619 Scop := Entity (Name (Item));
10620 T2 := First_Entity (Scop);
10621 while Present (T2) loop
10622 if Is_Fixed_Point_Type (T2)
10623 and then Scope (Base_Type (T2)) = Scop
10624 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
10625 then
10626 if Present (T1) then
10627 Fixed_Point_Error;
10628 return Any_Type;
10629 else
10630 T1 := T2;
10631 end if;
10632 end if;
10634 Next_Entity (T2);
10635 end loop;
10636 end if;
10638 Next (Item);
10639 end loop;
10641 if Nkind (N) = N_Real_Literal then
10642 Error_Msg_NE
10643 ("??real literal interpreted as }!", N, T1);
10644 else
10645 Error_Msg_NE
10646 ("??universal_fixed expression interpreted as }!", N, T1);
10647 end if;
10649 return T1;
10650 end Unique_Fixed_Point_Type;
10652 ----------------------
10653 -- Valid_Conversion --
10654 ----------------------
10656 function Valid_Conversion
10657 (N : Node_Id;
10658 Target : Entity_Id;
10659 Operand : Node_Id;
10660 Report_Errs : Boolean := True) return Boolean
10662 Target_Type : constant Entity_Id := Base_Type (Target);
10663 Opnd_Type : Entity_Id := Etype (Operand);
10664 Inc_Ancestor : Entity_Id;
10666 function Conversion_Check
10667 (Valid : Boolean;
10668 Msg : String) return Boolean;
10669 -- Little routine to post Msg if Valid is False, returns Valid value
10671 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id);
10672 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
10674 procedure Conversion_Error_NE
10675 (Msg : String;
10676 N : Node_Or_Entity_Id;
10677 E : Node_Or_Entity_Id);
10678 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
10680 function Valid_Tagged_Conversion
10681 (Target_Type : Entity_Id;
10682 Opnd_Type : Entity_Id) return Boolean;
10683 -- Specifically test for validity of tagged conversions
10685 function Valid_Array_Conversion return Boolean;
10686 -- Check index and component conformance, and accessibility levels if
10687 -- the component types are anonymous access types (Ada 2005).
10689 ----------------------
10690 -- Conversion_Check --
10691 ----------------------
10693 function Conversion_Check
10694 (Valid : Boolean;
10695 Msg : String) return Boolean
10697 begin
10698 if not Valid
10700 -- A generic unit has already been analyzed and we have verified
10701 -- that a particular conversion is OK in that context. Since the
10702 -- instance is reanalyzed without relying on the relationships
10703 -- established during the analysis of the generic, it is possible
10704 -- to end up with inconsistent views of private types. Do not emit
10705 -- the error message in such cases. The rest of the machinery in
10706 -- Valid_Conversion still ensures the proper compatibility of
10707 -- target and operand types.
10709 and then not In_Instance
10710 then
10711 Conversion_Error_N (Msg, Operand);
10712 end if;
10714 return Valid;
10715 end Conversion_Check;
10717 ------------------------
10718 -- Conversion_Error_N --
10719 ------------------------
10721 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id) is
10722 begin
10723 if Report_Errs then
10724 Error_Msg_N (Msg, N);
10725 end if;
10726 end Conversion_Error_N;
10728 -------------------------
10729 -- Conversion_Error_NE --
10730 -------------------------
10732 procedure Conversion_Error_NE
10733 (Msg : String;
10734 N : Node_Or_Entity_Id;
10735 E : Node_Or_Entity_Id)
10737 begin
10738 if Report_Errs then
10739 Error_Msg_NE (Msg, N, E);
10740 end if;
10741 end Conversion_Error_NE;
10743 ----------------------------
10744 -- Valid_Array_Conversion --
10745 ----------------------------
10747 function Valid_Array_Conversion return Boolean
10749 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
10750 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
10752 Opnd_Index : Node_Id;
10753 Opnd_Index_Type : Entity_Id;
10755 Target_Comp_Type : constant Entity_Id :=
10756 Component_Type (Target_Type);
10757 Target_Comp_Base : constant Entity_Id :=
10758 Base_Type (Target_Comp_Type);
10760 Target_Index : Node_Id;
10761 Target_Index_Type : Entity_Id;
10763 begin
10764 -- Error if wrong number of dimensions
10767 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
10768 then
10769 Conversion_Error_N
10770 ("incompatible number of dimensions for conversion", Operand);
10771 return False;
10773 -- Number of dimensions matches
10775 else
10776 -- Loop through indexes of the two arrays
10778 Target_Index := First_Index (Target_Type);
10779 Opnd_Index := First_Index (Opnd_Type);
10780 while Present (Target_Index) and then Present (Opnd_Index) loop
10781 Target_Index_Type := Etype (Target_Index);
10782 Opnd_Index_Type := Etype (Opnd_Index);
10784 -- Error if index types are incompatible
10786 if not (Is_Integer_Type (Target_Index_Type)
10787 and then Is_Integer_Type (Opnd_Index_Type))
10788 and then (Root_Type (Target_Index_Type)
10789 /= Root_Type (Opnd_Index_Type))
10790 then
10791 Conversion_Error_N
10792 ("incompatible index types for array conversion",
10793 Operand);
10794 return False;
10795 end if;
10797 Next_Index (Target_Index);
10798 Next_Index (Opnd_Index);
10799 end loop;
10801 -- If component types have same base type, all set
10803 if Target_Comp_Base = Opnd_Comp_Base then
10804 null;
10806 -- Here if base types of components are not the same. The only
10807 -- time this is allowed is if we have anonymous access types.
10809 -- The conversion of arrays of anonymous access types can lead
10810 -- to dangling pointers. AI-392 formalizes the accessibility
10811 -- checks that must be applied to such conversions to prevent
10812 -- out-of-scope references.
10814 elsif Ekind_In
10815 (Target_Comp_Base, E_Anonymous_Access_Type,
10816 E_Anonymous_Access_Subprogram_Type)
10817 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
10818 and then
10819 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
10820 then
10821 if Type_Access_Level (Target_Type) <
10822 Deepest_Type_Access_Level (Opnd_Type)
10823 then
10824 if In_Instance_Body then
10825 Conversion_Error_N
10826 ("??source array type has deeper accessibility "
10827 & "level than target", Operand);
10828 Conversion_Error_N
10829 ("\??Program_Error will be raised at run time",
10830 Operand);
10831 Rewrite (N,
10832 Make_Raise_Program_Error (Sloc (N),
10833 Reason => PE_Accessibility_Check_Failed));
10834 Set_Etype (N, Target_Type);
10835 return False;
10837 -- Conversion not allowed because of accessibility levels
10839 else
10840 Conversion_Error_N
10841 ("source array type has deeper accessibility "
10842 & "level than target", Operand);
10843 return False;
10844 end if;
10846 else
10847 null;
10848 end if;
10850 -- All other cases where component base types do not match
10852 else
10853 Conversion_Error_N
10854 ("incompatible component types for array conversion",
10855 Operand);
10856 return False;
10857 end if;
10859 -- Check that component subtypes statically match. For numeric
10860 -- types this means that both must be either constrained or
10861 -- unconstrained. For enumeration types the bounds must match.
10862 -- All of this is checked in Subtypes_Statically_Match.
10864 if not Subtypes_Statically_Match
10865 (Target_Comp_Type, Opnd_Comp_Type)
10866 then
10867 Conversion_Error_N
10868 ("component subtypes must statically match", Operand);
10869 return False;
10870 end if;
10871 end if;
10873 return True;
10874 end Valid_Array_Conversion;
10876 -----------------------------
10877 -- Valid_Tagged_Conversion --
10878 -----------------------------
10880 function Valid_Tagged_Conversion
10881 (Target_Type : Entity_Id;
10882 Opnd_Type : Entity_Id) return Boolean
10884 begin
10885 -- Upward conversions are allowed (RM 4.6(22))
10887 if Covers (Target_Type, Opnd_Type)
10888 or else Is_Ancestor (Target_Type, Opnd_Type)
10889 then
10890 return True;
10892 -- Downward conversion are allowed if the operand is class-wide
10893 -- (RM 4.6(23)).
10895 elsif Is_Class_Wide_Type (Opnd_Type)
10896 and then Covers (Opnd_Type, Target_Type)
10897 then
10898 return True;
10900 elsif Covers (Opnd_Type, Target_Type)
10901 or else Is_Ancestor (Opnd_Type, Target_Type)
10902 then
10903 return
10904 Conversion_Check (False,
10905 "downward conversion of tagged objects not allowed");
10907 -- Ada 2005 (AI-251): The conversion to/from interface types is
10908 -- always valid
10910 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
10911 return True;
10913 -- If the operand is a class-wide type obtained through a limited_
10914 -- with clause, and the context includes the non-limited view, use
10915 -- it to determine whether the conversion is legal.
10917 elsif Is_Class_Wide_Type (Opnd_Type)
10918 and then From_Limited_With (Opnd_Type)
10919 and then Present (Non_Limited_View (Etype (Opnd_Type)))
10920 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
10921 then
10922 return True;
10924 elsif Is_Access_Type (Opnd_Type)
10925 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
10926 then
10927 return True;
10929 else
10930 Conversion_Error_NE
10931 ("invalid tagged conversion, not compatible with}",
10932 N, First_Subtype (Opnd_Type));
10933 return False;
10934 end if;
10935 end Valid_Tagged_Conversion;
10937 -- Start of processing for Valid_Conversion
10939 begin
10940 Check_Parameterless_Call (Operand);
10942 if Is_Overloaded (Operand) then
10943 declare
10944 I : Interp_Index;
10945 I1 : Interp_Index;
10946 It : Interp;
10947 It1 : Interp;
10948 N1 : Entity_Id;
10949 T1 : Entity_Id;
10951 begin
10952 -- Remove procedure calls, which syntactically cannot appear in
10953 -- this context, but which cannot be removed by type checking,
10954 -- because the context does not impose a type.
10956 -- When compiling for VMS, spurious ambiguities can be produced
10957 -- when arithmetic operations have a literal operand and return
10958 -- System.Address or a descendant of it. These ambiguities are
10959 -- otherwise resolved by the context, but for conversions there
10960 -- is no context type and the removal of the spurious operations
10961 -- must be done explicitly here.
10963 -- The node may be labelled overloaded, but still contain only one
10964 -- interpretation because others were discarded earlier. If this
10965 -- is the case, retain the single interpretation if legal.
10967 Get_First_Interp (Operand, I, It);
10968 Opnd_Type := It.Typ;
10969 Get_Next_Interp (I, It);
10971 if Present (It.Typ)
10972 and then Opnd_Type /= Standard_Void_Type
10973 then
10974 -- More than one candidate interpretation is available
10976 Get_First_Interp (Operand, I, It);
10977 while Present (It.Typ) loop
10978 if It.Typ = Standard_Void_Type then
10979 Remove_Interp (I);
10980 end if;
10982 if Present (System_Aux_Id)
10983 and then Is_Descendent_Of_Address (It.Typ)
10984 then
10985 Remove_Interp (I);
10986 end if;
10988 Get_Next_Interp (I, It);
10989 end loop;
10990 end if;
10992 Get_First_Interp (Operand, I, It);
10993 I1 := I;
10994 It1 := It;
10996 if No (It.Typ) then
10997 Conversion_Error_N ("illegal operand in conversion", Operand);
10998 return False;
10999 end if;
11001 Get_Next_Interp (I, It);
11003 if Present (It.Typ) then
11004 N1 := It1.Nam;
11005 T1 := It1.Typ;
11006 It1 := Disambiguate (Operand, I1, I, Any_Type);
11008 if It1 = No_Interp then
11009 Conversion_Error_N
11010 ("ambiguous operand in conversion", Operand);
11012 -- If the interpretation involves a standard operator, use
11013 -- the location of the type, which may be user-defined.
11015 if Sloc (It.Nam) = Standard_Location then
11016 Error_Msg_Sloc := Sloc (It.Typ);
11017 else
11018 Error_Msg_Sloc := Sloc (It.Nam);
11019 end if;
11021 Conversion_Error_N -- CODEFIX
11022 ("\\possible interpretation#!", Operand);
11024 if Sloc (N1) = Standard_Location then
11025 Error_Msg_Sloc := Sloc (T1);
11026 else
11027 Error_Msg_Sloc := Sloc (N1);
11028 end if;
11030 Conversion_Error_N -- CODEFIX
11031 ("\\possible interpretation#!", Operand);
11033 return False;
11034 end if;
11035 end if;
11037 Set_Etype (Operand, It1.Typ);
11038 Opnd_Type := It1.Typ;
11039 end;
11040 end if;
11042 -- If we are within a child unit, check whether the type of the
11043 -- expression has an ancestor in a parent unit, in which case it
11044 -- belongs to its derivation class even if the ancestor is private.
11045 -- See RM 7.3.1 (5.2/3).
11047 Inc_Ancestor := Get_Incomplete_View_Of_Ancestor (Opnd_Type);
11049 -- Numeric types
11051 if Is_Numeric_Type (Target_Type) then
11053 -- A universal fixed expression can be converted to any numeric type
11055 if Opnd_Type = Universal_Fixed then
11056 return True;
11058 -- Also no need to check when in an instance or inlined body, because
11059 -- the legality has been established when the template was analyzed.
11060 -- Furthermore, numeric conversions may occur where only a private
11061 -- view of the operand type is visible at the instantiation point.
11062 -- This results in a spurious error if we check that the operand type
11063 -- is a numeric type.
11065 -- Note: in a previous version of this unit, the following tests were
11066 -- applied only for generated code (Comes_From_Source set to False),
11067 -- but in fact the test is required for source code as well, since
11068 -- this situation can arise in source code.
11070 elsif In_Instance or else In_Inlined_Body then
11071 return True;
11073 -- Otherwise we need the conversion check
11075 else
11076 return Conversion_Check
11077 (Is_Numeric_Type (Opnd_Type)
11078 or else
11079 (Present (Inc_Ancestor)
11080 and then Is_Numeric_Type (Inc_Ancestor)),
11081 "illegal operand for numeric conversion");
11082 end if;
11084 -- Array types
11086 elsif Is_Array_Type (Target_Type) then
11087 if not Is_Array_Type (Opnd_Type)
11088 or else Opnd_Type = Any_Composite
11089 or else Opnd_Type = Any_String
11090 then
11091 Conversion_Error_N
11092 ("illegal operand for array conversion", Operand);
11093 return False;
11095 else
11096 return Valid_Array_Conversion;
11097 end if;
11099 -- Ada 2005 (AI-251): Anonymous access types where target references an
11100 -- interface type.
11102 elsif Ekind_In (Target_Type, E_General_Access_Type,
11103 E_Anonymous_Access_Type)
11104 and then Is_Interface (Directly_Designated_Type (Target_Type))
11105 then
11106 -- Check the static accessibility rule of 4.6(17). Note that the
11107 -- check is not enforced when within an instance body, since the
11108 -- RM requires such cases to be caught at run time.
11110 -- If the operand is a rewriting of an allocator no check is needed
11111 -- because there are no accessibility issues.
11113 if Nkind (Original_Node (N)) = N_Allocator then
11114 null;
11116 elsif Ekind (Target_Type) /= E_Anonymous_Access_Type then
11117 if Type_Access_Level (Opnd_Type) >
11118 Deepest_Type_Access_Level (Target_Type)
11119 then
11120 -- In an instance, this is a run-time check, but one we know
11121 -- will fail, so generate an appropriate warning. The raise
11122 -- will be generated by Expand_N_Type_Conversion.
11124 if In_Instance_Body then
11125 Conversion_Error_N
11126 ("??cannot convert local pointer to non-local access type",
11127 Operand);
11128 Conversion_Error_N
11129 ("\??Program_Error will be raised at run time", Operand);
11131 else
11132 Conversion_Error_N
11133 ("cannot convert local pointer to non-local access type",
11134 Operand);
11135 return False;
11136 end if;
11138 -- Special accessibility checks are needed in the case of access
11139 -- discriminants declared for a limited type.
11141 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
11142 and then not Is_Local_Anonymous_Access (Opnd_Type)
11143 then
11144 -- When the operand is a selected access discriminant the check
11145 -- needs to be made against the level of the object denoted by
11146 -- the prefix of the selected name (Object_Access_Level handles
11147 -- checking the prefix of the operand for this case).
11149 if Nkind (Operand) = N_Selected_Component
11150 and then Object_Access_Level (Operand) >
11151 Deepest_Type_Access_Level (Target_Type)
11152 then
11153 -- In an instance, this is a run-time check, but one we know
11154 -- will fail, so generate an appropriate warning. The raise
11155 -- will be generated by Expand_N_Type_Conversion.
11157 if In_Instance_Body then
11158 Conversion_Error_N
11159 ("??cannot convert access discriminant to non-local "
11160 & "access type", Operand);
11161 Conversion_Error_N
11162 ("\??Program_Error will be raised at run time",
11163 Operand);
11164 else
11165 Conversion_Error_N
11166 ("cannot convert access discriminant to non-local "
11167 & "access type", Operand);
11168 return False;
11169 end if;
11170 end if;
11172 -- The case of a reference to an access discriminant from
11173 -- within a limited type declaration (which will appear as
11174 -- a discriminal) is always illegal because the level of the
11175 -- discriminant is considered to be deeper than any (nameable)
11176 -- access type.
11178 if Is_Entity_Name (Operand)
11179 and then not Is_Local_Anonymous_Access (Opnd_Type)
11180 and then
11181 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
11182 and then Present (Discriminal_Link (Entity (Operand)))
11183 then
11184 Conversion_Error_N
11185 ("discriminant has deeper accessibility level than target",
11186 Operand);
11187 return False;
11188 end if;
11189 end if;
11190 end if;
11192 return True;
11194 -- General and anonymous access types
11196 elsif Ekind_In (Target_Type, E_General_Access_Type,
11197 E_Anonymous_Access_Type)
11198 and then
11199 Conversion_Check
11200 (Is_Access_Type (Opnd_Type)
11201 and then not
11202 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
11203 E_Access_Protected_Subprogram_Type),
11204 "must be an access-to-object type")
11205 then
11206 if Is_Access_Constant (Opnd_Type)
11207 and then not Is_Access_Constant (Target_Type)
11208 then
11209 Conversion_Error_N
11210 ("access-to-constant operand type not allowed", Operand);
11211 return False;
11212 end if;
11214 -- Check the static accessibility rule of 4.6(17). Note that the
11215 -- check is not enforced when within an instance body, since the RM
11216 -- requires such cases to be caught at run time.
11218 if Ekind (Target_Type) /= E_Anonymous_Access_Type
11219 or else Is_Local_Anonymous_Access (Target_Type)
11220 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
11221 N_Object_Declaration
11222 then
11223 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
11224 -- conversions from an anonymous access type to a named general
11225 -- access type. Such conversions are not allowed in the case of
11226 -- access parameters and stand-alone objects of an anonymous
11227 -- access type. The implicit conversion case is recognized by
11228 -- testing that Comes_From_Source is False and that it's been
11229 -- rewritten. The Comes_From_Source test isn't sufficient because
11230 -- nodes in inlined calls to predefined library routines can have
11231 -- Comes_From_Source set to False. (Is there a better way to test
11232 -- for implicit conversions???)
11234 if Ada_Version >= Ada_2012
11235 and then not Comes_From_Source (N)
11236 and then N /= Original_Node (N)
11237 and then Ekind (Target_Type) = E_General_Access_Type
11238 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
11239 then
11240 if Is_Itype (Opnd_Type) then
11242 -- Implicit conversions aren't allowed for objects of an
11243 -- anonymous access type, since such objects have nonstatic
11244 -- levels in Ada 2012.
11246 if Nkind (Associated_Node_For_Itype (Opnd_Type)) =
11247 N_Object_Declaration
11248 then
11249 Conversion_Error_N
11250 ("implicit conversion of stand-alone anonymous "
11251 & "access object not allowed", Operand);
11252 return False;
11254 -- Implicit conversions aren't allowed for anonymous access
11255 -- parameters. The "not Is_Local_Anonymous_Access_Type" test
11256 -- is done to exclude anonymous access results.
11258 elsif not Is_Local_Anonymous_Access (Opnd_Type)
11259 and then Nkind_In (Associated_Node_For_Itype (Opnd_Type),
11260 N_Function_Specification,
11261 N_Procedure_Specification)
11262 then
11263 Conversion_Error_N
11264 ("implicit conversion of anonymous access formal "
11265 & "not allowed", Operand);
11266 return False;
11268 -- This is a case where there's an enclosing object whose
11269 -- to which the "statically deeper than" relationship does
11270 -- not apply (such as an access discriminant selected from
11271 -- a dereference of an access parameter).
11273 elsif Object_Access_Level (Operand)
11274 = Scope_Depth (Standard_Standard)
11275 then
11276 Conversion_Error_N
11277 ("implicit conversion of anonymous access value "
11278 & "not allowed", Operand);
11279 return False;
11281 -- In other cases, the level of the operand's type must be
11282 -- statically less deep than that of the target type, else
11283 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
11285 elsif Type_Access_Level (Opnd_Type) >
11286 Deepest_Type_Access_Level (Target_Type)
11287 then
11288 Conversion_Error_N
11289 ("implicit conversion of anonymous access value "
11290 & "violates accessibility", Operand);
11291 return False;
11292 end if;
11293 end if;
11295 elsif Type_Access_Level (Opnd_Type) >
11296 Deepest_Type_Access_Level (Target_Type)
11297 then
11298 -- In an instance, this is a run-time check, but one we know
11299 -- will fail, so generate an appropriate warning. The raise
11300 -- will be generated by Expand_N_Type_Conversion.
11302 if In_Instance_Body then
11303 Conversion_Error_N
11304 ("??cannot convert local pointer to non-local access type",
11305 Operand);
11306 Conversion_Error_N
11307 ("\??Program_Error will be raised at run time", Operand);
11309 else
11310 -- Avoid generation of spurious error message
11312 if not Error_Posted (N) then
11313 Conversion_Error_N
11314 ("cannot convert local pointer to non-local access type",
11315 Operand);
11316 end if;
11318 return False;
11319 end if;
11321 -- Special accessibility checks are needed in the case of access
11322 -- discriminants declared for a limited type.
11324 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
11325 and then not Is_Local_Anonymous_Access (Opnd_Type)
11326 then
11327 -- When the operand is a selected access discriminant the check
11328 -- needs to be made against the level of the object denoted by
11329 -- the prefix of the selected name (Object_Access_Level handles
11330 -- checking the prefix of the operand for this case).
11332 if Nkind (Operand) = N_Selected_Component
11333 and then Object_Access_Level (Operand) >
11334 Deepest_Type_Access_Level (Target_Type)
11335 then
11336 -- In an instance, this is a run-time check, but one we know
11337 -- will fail, so generate an appropriate warning. The raise
11338 -- will be generated by Expand_N_Type_Conversion.
11340 if In_Instance_Body then
11341 Conversion_Error_N
11342 ("??cannot convert access discriminant to non-local "
11343 & "access type", Operand);
11344 Conversion_Error_N
11345 ("\??Program_Error will be raised at run time",
11346 Operand);
11348 else
11349 Conversion_Error_N
11350 ("cannot convert access discriminant to non-local "
11351 & "access type", Operand);
11352 return False;
11353 end if;
11354 end if;
11356 -- The case of a reference to an access discriminant from
11357 -- within a limited type declaration (which will appear as
11358 -- a discriminal) is always illegal because the level of the
11359 -- discriminant is considered to be deeper than any (nameable)
11360 -- access type.
11362 if Is_Entity_Name (Operand)
11363 and then
11364 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
11365 and then Present (Discriminal_Link (Entity (Operand)))
11366 then
11367 Conversion_Error_N
11368 ("discriminant has deeper accessibility level than target",
11369 Operand);
11370 return False;
11371 end if;
11372 end if;
11373 end if;
11375 -- In the presence of limited_with clauses we have to use non-limited
11376 -- views, if available.
11378 Check_Limited : declare
11379 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
11380 -- Helper function to handle limited views
11382 --------------------------
11383 -- Full_Designated_Type --
11384 --------------------------
11386 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
11387 Desig : constant Entity_Id := Designated_Type (T);
11389 begin
11390 -- Handle the limited view of a type
11392 if Is_Incomplete_Type (Desig)
11393 and then From_Limited_With (Desig)
11394 and then Present (Non_Limited_View (Desig))
11395 then
11396 return Available_View (Desig);
11397 else
11398 return Desig;
11399 end if;
11400 end Full_Designated_Type;
11402 -- Local Declarations
11404 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
11405 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
11407 Same_Base : constant Boolean :=
11408 Base_Type (Target) = Base_Type (Opnd);
11410 -- Start of processing for Check_Limited
11412 begin
11413 if Is_Tagged_Type (Target) then
11414 return Valid_Tagged_Conversion (Target, Opnd);
11416 else
11417 if not Same_Base then
11418 Conversion_Error_NE
11419 ("target designated type not compatible with }",
11420 N, Base_Type (Opnd));
11421 return False;
11423 -- Ada 2005 AI-384: legality rule is symmetric in both
11424 -- designated types. The conversion is legal (with possible
11425 -- constraint check) if either designated type is
11426 -- unconstrained.
11428 elsif Subtypes_Statically_Match (Target, Opnd)
11429 or else
11430 (Has_Discriminants (Target)
11431 and then
11432 (not Is_Constrained (Opnd)
11433 or else not Is_Constrained (Target)))
11434 then
11435 -- Special case, if Value_Size has been used to make the
11436 -- sizes different, the conversion is not allowed even
11437 -- though the subtypes statically match.
11439 if Known_Static_RM_Size (Target)
11440 and then Known_Static_RM_Size (Opnd)
11441 and then RM_Size (Target) /= RM_Size (Opnd)
11442 then
11443 Conversion_Error_NE
11444 ("target designated subtype not compatible with }",
11445 N, Opnd);
11446 Conversion_Error_NE
11447 ("\because sizes of the two designated subtypes differ",
11448 N, Opnd);
11449 return False;
11451 -- Normal case where conversion is allowed
11453 else
11454 return True;
11455 end if;
11457 else
11458 Error_Msg_NE
11459 ("target designated subtype not compatible with }",
11460 N, Opnd);
11461 return False;
11462 end if;
11463 end if;
11464 end Check_Limited;
11466 -- Access to subprogram types. If the operand is an access parameter,
11467 -- the type has a deeper accessibility that any master, and cannot be
11468 -- assigned. We must make an exception if the conversion is part of an
11469 -- assignment and the target is the return object of an extended return
11470 -- statement, because in that case the accessibility check takes place
11471 -- after the return.
11473 elsif Is_Access_Subprogram_Type (Target_Type)
11474 and then No (Corresponding_Remote_Type (Opnd_Type))
11475 then
11476 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
11477 and then Is_Entity_Name (Operand)
11478 and then Ekind (Entity (Operand)) = E_In_Parameter
11479 and then
11480 (Nkind (Parent (N)) /= N_Assignment_Statement
11481 or else not Is_Entity_Name (Name (Parent (N)))
11482 or else not Is_Return_Object (Entity (Name (Parent (N)))))
11483 then
11484 Conversion_Error_N
11485 ("illegal attempt to store anonymous access to subprogram",
11486 Operand);
11487 Conversion_Error_N
11488 ("\value has deeper accessibility than any master "
11489 & "(RM 3.10.2 (13))",
11490 Operand);
11492 Error_Msg_NE
11493 ("\use named access type for& instead of access parameter",
11494 Operand, Entity (Operand));
11495 end if;
11497 -- Check that the designated types are subtype conformant
11499 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
11500 Old_Id => Designated_Type (Opnd_Type),
11501 Err_Loc => N);
11503 -- Check the static accessibility rule of 4.6(20)
11505 if Type_Access_Level (Opnd_Type) >
11506 Deepest_Type_Access_Level (Target_Type)
11507 then
11508 Conversion_Error_N
11509 ("operand type has deeper accessibility level than target",
11510 Operand);
11512 -- Check that if the operand type is declared in a generic body,
11513 -- then the target type must be declared within that same body
11514 -- (enforces last sentence of 4.6(20)).
11516 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
11517 declare
11518 O_Gen : constant Node_Id :=
11519 Enclosing_Generic_Body (Opnd_Type);
11521 T_Gen : Node_Id;
11523 begin
11524 T_Gen := Enclosing_Generic_Body (Target_Type);
11525 while Present (T_Gen) and then T_Gen /= O_Gen loop
11526 T_Gen := Enclosing_Generic_Body (T_Gen);
11527 end loop;
11529 if T_Gen /= O_Gen then
11530 Conversion_Error_N
11531 ("target type must be declared in same generic body "
11532 & "as operand type", N);
11533 end if;
11534 end;
11535 end if;
11537 return True;
11539 -- Remote subprogram access types
11541 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
11542 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
11543 then
11544 -- It is valid to convert from one RAS type to another provided
11545 -- that their specification statically match.
11547 Check_Subtype_Conformant
11548 (New_Id =>
11549 Designated_Type (Corresponding_Remote_Type (Target_Type)),
11550 Old_Id =>
11551 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
11552 Err_Loc =>
11554 return True;
11556 -- If it was legal in the generic, it's legal in the instance
11558 elsif In_Instance_Body then
11559 return True;
11561 -- If both are tagged types, check legality of view conversions
11563 elsif Is_Tagged_Type (Target_Type)
11564 and then
11565 Is_Tagged_Type (Opnd_Type)
11566 then
11567 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
11569 -- Types derived from the same root type are convertible
11571 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
11572 return True;
11574 -- In an instance or an inlined body, there may be inconsistent views of
11575 -- the same type, or of types derived from a common root.
11577 elsif (In_Instance or In_Inlined_Body)
11578 and then
11579 Root_Type (Underlying_Type (Target_Type)) =
11580 Root_Type (Underlying_Type (Opnd_Type))
11581 then
11582 return True;
11584 -- Special check for common access type error case
11586 elsif Ekind (Target_Type) = E_Access_Type
11587 and then Is_Access_Type (Opnd_Type)
11588 then
11589 Conversion_Error_N ("target type must be general access type!", N);
11590 Conversion_Error_NE -- CODEFIX
11591 ("add ALL to }!", N, Target_Type);
11592 return False;
11594 else
11595 Conversion_Error_NE
11596 ("invalid conversion, not compatible with }", N, Opnd_Type);
11597 return False;
11598 end if;
11599 end Valid_Conversion;
11601 end Sem_Res;