6d69ab4a52359b450e408d8b6a96270247a7f4e0
[gcc.git] / gcc / ada / sem_res.adb
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-2014, 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 ------------------------------------------------------------------------------
25
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;
80
81 package body Sem_Res is
82
83 -----------------------
84 -- Local Subprograms --
85 -----------------------
86
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.
93
94 -- Note that Resolve_Attribute is separated off in Sem_Attr
95
96 procedure Check_Discriminant_Use (N : Node_Id);
97 -- Enforce the restrictions on the use of discriminants when constraining
98 -- a component of a discriminated type (record or concurrent type).
99
100 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
101 -- Given a node for an operator associated with type T, check that
102 -- the operator is visible. Operators all of whose operands are
103 -- universal must be checked for visibility during resolution
104 -- because their type is not determinable based on their operands.
105
106 procedure Check_Fully_Declared_Prefix
107 (Typ : Entity_Id;
108 Pref : Node_Id);
109 -- Check that the type of the prefix of a dereference is not incomplete
110
111 function Check_Infinite_Recursion (N : Node_Id) return Boolean;
112 -- Given a call node, N, which is known to occur immediately within the
113 -- subprogram being called, determines whether it is a detectable case of
114 -- an infinite recursion, and if so, outputs appropriate messages. Returns
115 -- True if an infinite recursion is detected, and False otherwise.
116
117 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id);
118 -- If the type of the object being initialized uses the secondary stack
119 -- directly or indirectly, create a transient scope for the call to the
120 -- init proc. This is because we do not create transient scopes for the
121 -- initialization of individual components within the init proc itself.
122 -- Could be optimized away perhaps?
123
124 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
125 -- N is the node for a logical operator. If the operator is predefined, and
126 -- the root type of the operands is Standard.Boolean, then a check is made
127 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
128 -- the style check for Style_Check_Boolean_And_Or.
129
130 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
131 -- Determine whether E is an access type declared by an access declaration,
132 -- and not an (anonymous) allocator type.
133
134 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
135 -- Utility to check whether the entity for an operator is a predefined
136 -- operator, in which case the expression is left as an operator in the
137 -- tree (else it is rewritten into a call). An instance of an intrinsic
138 -- conversion operation may be given an operator name, but is not treated
139 -- like an operator. Note that an operator that is an imported back-end
140 -- builtin has convention Intrinsic, but is expected to be rewritten into
141 -- a call, so such an operator is not treated as predefined by this
142 -- predicate.
143
144 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
145 -- If a default expression in entry call N depends on the discriminants
146 -- of the task, it must be replaced with a reference to the discriminant
147 -- of the task being called.
148
149 procedure Resolve_Op_Concat_Arg
150 (N : Node_Id;
151 Arg : Node_Id;
152 Typ : Entity_Id;
153 Is_Comp : Boolean);
154 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
155 -- concatenation operator. The operand is either of the array type or of
156 -- the component type. If the operand is an aggregate, and the component
157 -- type is composite, this is ambiguous if component type has aggregates.
158
159 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
160 -- Does the first part of the work of Resolve_Op_Concat
161
162 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
163 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
164 -- has been resolved. See Resolve_Op_Concat for details.
165
166 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
167 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
168 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
169 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
170 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
171 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
172 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
173 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
174 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
175 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
176 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id);
177 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id);
178 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
179 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
180 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
181 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
182 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
183 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
184 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
185 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
186 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
187 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
188 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id);
189 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
190 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
191 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
192 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
193 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
194 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
195 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
196 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
197 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
198 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
199 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
200 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
201
202 function Operator_Kind
203 (Op_Name : Name_Id;
204 Is_Binary : Boolean) return Node_Kind;
205 -- Utility to map the name of an operator into the corresponding Node. Used
206 -- by other node rewriting procedures.
207
208 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
209 -- Resolve actuals of call, and add default expressions for missing ones.
210 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
211 -- called subprogram.
212
213 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
214 -- Called from Resolve_Call, when the prefix denotes an entry or element
215 -- of entry family. Actuals are resolved as for subprograms, and the node
216 -- is rebuilt as an entry call. Also called for protected operations. Typ
217 -- is the context type, which is used when the operation is a protected
218 -- function with no arguments, and the return value is indexed.
219
220 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
221 -- A call to a user-defined intrinsic operator is rewritten as a call to
222 -- the corresponding predefined operator, with suitable conversions. Note
223 -- that this applies only for intrinsic operators that denote predefined
224 -- operators, not ones that are intrinsic imports of back-end builtins.
225
226 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
227 -- Ditto, for unary operators (arithmetic ones and "not" on signed
228 -- integer types for VMS).
229
230 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
231 -- If an operator node resolves to a call to a user-defined operator,
232 -- rewrite the node as a function call.
233
234 procedure Make_Call_Into_Operator
235 (N : Node_Id;
236 Typ : Entity_Id;
237 Op_Id : Entity_Id);
238 -- Inverse transformation: if an operator is given in functional notation,
239 -- then after resolving the node, transform into an operator node, so
240 -- that operands are resolved properly. Recall that predefined operators
241 -- do not have a full signature and special resolution rules apply.
242
243 procedure Rewrite_Renamed_Operator
244 (N : Node_Id;
245 Op : Entity_Id;
246 Typ : Entity_Id);
247 -- An operator can rename another, e.g. in an instantiation. In that
248 -- case, the proper operator node must be constructed and resolved.
249
250 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
251 -- The String_Literal_Subtype is built for all strings that are not
252 -- operands of a static concatenation operation. If the argument is
253 -- not a N_String_Literal node, then the call has no effect.
254
255 procedure Set_Slice_Subtype (N : Node_Id);
256 -- Build subtype of array type, with the range specified by the slice
257
258 procedure Simplify_Type_Conversion (N : Node_Id);
259 -- Called after N has been resolved and evaluated, but before range checks
260 -- have been applied. Currently simplifies a combination of floating-point
261 -- to integer conversion and Truncation attribute.
262
263 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
264 -- A universal_fixed expression in an universal context is unambiguous if
265 -- there is only one applicable fixed point type. Determining whether there
266 -- is only one requires a search over all visible entities, and happens
267 -- only in very pathological cases (see 6115-006).
268
269 -------------------------
270 -- Ambiguous_Character --
271 -------------------------
272
273 procedure Ambiguous_Character (C : Node_Id) is
274 E : Entity_Id;
275
276 begin
277 if Nkind (C) = N_Character_Literal then
278 Error_Msg_N ("ambiguous character literal", C);
279
280 -- First the ones in Standard
281
282 Error_Msg_N ("\\possible interpretation: Character!", C);
283 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
284
285 -- Include Wide_Wide_Character in Ada 2005 mode
286
287 if Ada_Version >= Ada_2005 then
288 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
289 end if;
290
291 -- Now any other types that match
292
293 E := Current_Entity (C);
294 while Present (E) loop
295 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
296 E := Homonym (E);
297 end loop;
298 end if;
299 end Ambiguous_Character;
300
301 -------------------------
302 -- Analyze_And_Resolve --
303 -------------------------
304
305 procedure Analyze_And_Resolve (N : Node_Id) is
306 begin
307 Analyze (N);
308 Resolve (N);
309 end Analyze_And_Resolve;
310
311 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
312 begin
313 Analyze (N);
314 Resolve (N, Typ);
315 end Analyze_And_Resolve;
316
317 -- Versions with check(s) suppressed
318
319 procedure Analyze_And_Resolve
320 (N : Node_Id;
321 Typ : Entity_Id;
322 Suppress : Check_Id)
323 is
324 Scop : constant Entity_Id := Current_Scope;
325
326 begin
327 if Suppress = All_Checks then
328 declare
329 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
330 begin
331 Scope_Suppress.Suppress := (others => True);
332 Analyze_And_Resolve (N, Typ);
333 Scope_Suppress.Suppress := Sva;
334 end;
335
336 else
337 declare
338 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
339 begin
340 Scope_Suppress.Suppress (Suppress) := True;
341 Analyze_And_Resolve (N, Typ);
342 Scope_Suppress.Suppress (Suppress) := Svg;
343 end;
344 end if;
345
346 if Current_Scope /= Scop
347 and then Scope_Is_Transient
348 then
349 -- This can only happen if a transient scope was created for an inner
350 -- expression, which will be removed upon completion of the analysis
351 -- of an enclosing construct. The transient scope must have the
352 -- suppress status of the enclosing environment, not of this Analyze
353 -- call.
354
355 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
356 Scope_Suppress;
357 end if;
358 end Analyze_And_Resolve;
359
360 procedure Analyze_And_Resolve
361 (N : Node_Id;
362 Suppress : Check_Id)
363 is
364 Scop : constant Entity_Id := Current_Scope;
365
366 begin
367 if Suppress = All_Checks then
368 declare
369 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
370 begin
371 Scope_Suppress.Suppress := (others => True);
372 Analyze_And_Resolve (N);
373 Scope_Suppress.Suppress := Sva;
374 end;
375
376 else
377 declare
378 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
379 begin
380 Scope_Suppress.Suppress (Suppress) := True;
381 Analyze_And_Resolve (N);
382 Scope_Suppress.Suppress (Suppress) := Svg;
383 end;
384 end if;
385
386 if Current_Scope /= Scop and then Scope_Is_Transient then
387 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
388 Scope_Suppress;
389 end if;
390 end Analyze_And_Resolve;
391
392 ----------------------------
393 -- Check_Discriminant_Use --
394 ----------------------------
395
396 procedure Check_Discriminant_Use (N : Node_Id) is
397 PN : constant Node_Id := Parent (N);
398 Disc : constant Entity_Id := Entity (N);
399 P : Node_Id;
400 D : Node_Id;
401
402 begin
403 -- Any use in a spec-expression is legal
404
405 if In_Spec_Expression then
406 null;
407
408 elsif Nkind (PN) = N_Range then
409
410 -- Discriminant cannot be used to constrain a scalar type
411
412 P := Parent (PN);
413
414 if Nkind (P) = N_Range_Constraint
415 and then Nkind (Parent (P)) = N_Subtype_Indication
416 and then Nkind (Parent (Parent (P))) = N_Component_Definition
417 then
418 Error_Msg_N ("discriminant cannot constrain scalar type", N);
419
420 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
421
422 -- The following check catches the unusual case where a
423 -- discriminant appears within an index constraint that is part of
424 -- a larger expression within a constraint on a component, e.g. "C
425 -- : Int range 1 .. F (new A(1 .. D))". For now we only check case
426 -- of record components, and note that a similar check should also
427 -- apply in the case of discriminant constraints below. ???
428
429 -- Note that the check for N_Subtype_Declaration below is to
430 -- detect the valid use of discriminants in the constraints of a
431 -- subtype declaration when this subtype declaration appears
432 -- inside the scope of a record type (which is syntactically
433 -- illegal, but which may be created as part of derived type
434 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
435 -- for more info.
436
437 if Ekind (Current_Scope) = E_Record_Type
438 and then Scope (Disc) = Current_Scope
439 and then not
440 (Nkind (Parent (P)) = N_Subtype_Indication
441 and then
442 Nkind_In (Parent (Parent (P)), N_Component_Definition,
443 N_Subtype_Declaration)
444 and then Paren_Count (N) = 0)
445 then
446 Error_Msg_N
447 ("discriminant must appear alone in component constraint", N);
448 return;
449 end if;
450
451 -- Detect a common error:
452
453 -- type R (D : Positive := 100) is record
454 -- Name : String (1 .. D);
455 -- end record;
456
457 -- The default value causes an object of type R to be allocated
458 -- with room for Positive'Last characters. The RM does not mandate
459 -- the allocation of the maximum size, but that is what GNAT does
460 -- so we should warn the programmer that there is a problem.
461
462 Check_Large : declare
463 SI : Node_Id;
464 T : Entity_Id;
465 TB : Node_Id;
466 CB : Entity_Id;
467
468 function Large_Storage_Type (T : Entity_Id) return Boolean;
469 -- Return True if type T has a large enough range that any
470 -- array whose index type covered the whole range of the type
471 -- would likely raise Storage_Error.
472
473 ------------------------
474 -- Large_Storage_Type --
475 ------------------------
476
477 function Large_Storage_Type (T : Entity_Id) return Boolean is
478 begin
479 -- The type is considered large if its bounds are known at
480 -- compile time and if it requires at least as many bits as
481 -- a Positive to store the possible values.
482
483 return Compile_Time_Known_Value (Type_Low_Bound (T))
484 and then Compile_Time_Known_Value (Type_High_Bound (T))
485 and then
486 Minimum_Size (T, Biased => True) >=
487 RM_Size (Standard_Positive);
488 end Large_Storage_Type;
489
490 -- Start of processing for Check_Large
491
492 begin
493 -- Check that the Disc has a large range
494
495 if not Large_Storage_Type (Etype (Disc)) then
496 goto No_Danger;
497 end if;
498
499 -- If the enclosing type is limited, we allocate only the
500 -- default value, not the maximum, and there is no need for
501 -- a warning.
502
503 if Is_Limited_Type (Scope (Disc)) then
504 goto No_Danger;
505 end if;
506
507 -- Check that it is the high bound
508
509 if N /= High_Bound (PN)
510 or else No (Discriminant_Default_Value (Disc))
511 then
512 goto No_Danger;
513 end if;
514
515 -- Check the array allows a large range at this bound. First
516 -- find the array
517
518 SI := Parent (P);
519
520 if Nkind (SI) /= N_Subtype_Indication then
521 goto No_Danger;
522 end if;
523
524 T := Entity (Subtype_Mark (SI));
525
526 if not Is_Array_Type (T) then
527 goto No_Danger;
528 end if;
529
530 -- Next, find the dimension
531
532 TB := First_Index (T);
533 CB := First (Constraints (P));
534 while True
535 and then Present (TB)
536 and then Present (CB)
537 and then CB /= PN
538 loop
539 Next_Index (TB);
540 Next (CB);
541 end loop;
542
543 if CB /= PN then
544 goto No_Danger;
545 end if;
546
547 -- Now, check the dimension has a large range
548
549 if not Large_Storage_Type (Etype (TB)) then
550 goto No_Danger;
551 end if;
552
553 -- Warn about the danger
554
555 Error_Msg_N
556 ("??creation of & object may raise Storage_Error!",
557 Scope (Disc));
558
559 <<No_Danger>>
560 null;
561
562 end Check_Large;
563 end if;
564
565 -- Legal case is in index or discriminant constraint
566
567 elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint,
568 N_Discriminant_Association)
569 then
570 if Paren_Count (N) > 0 then
571 Error_Msg_N
572 ("discriminant in constraint must appear alone", N);
573
574 elsif Nkind (N) = N_Expanded_Name
575 and then Comes_From_Source (N)
576 then
577 Error_Msg_N
578 ("discriminant must appear alone as a direct name", N);
579 end if;
580
581 return;
582
583 -- Otherwise, context is an expression. It should not be within (i.e. a
584 -- subexpression of) a constraint for a component.
585
586 else
587 D := PN;
588 P := Parent (PN);
589 while not Nkind_In (P, N_Component_Declaration,
590 N_Subtype_Indication,
591 N_Entry_Declaration)
592 loop
593 D := P;
594 P := Parent (P);
595 exit when No (P);
596 end loop;
597
598 -- If the discriminant is used in an expression that is a bound of a
599 -- scalar type, an Itype is created and the bounds are attached to
600 -- its range, not to the original subtype indication. Such use is of
601 -- course a double fault.
602
603 if (Nkind (P) = N_Subtype_Indication
604 and then Nkind_In (Parent (P), N_Component_Definition,
605 N_Derived_Type_Definition)
606 and then D = Constraint (P))
607
608 -- The constraint itself may be given by a subtype indication,
609 -- rather than by a more common discrete range.
610
611 or else (Nkind (P) = N_Subtype_Indication
612 and then
613 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
614 or else Nkind (P) = N_Entry_Declaration
615 or else Nkind (D) = N_Defining_Identifier
616 then
617 Error_Msg_N
618 ("discriminant in constraint must appear alone", N);
619 end if;
620 end if;
621 end Check_Discriminant_Use;
622
623 --------------------------------
624 -- Check_For_Visible_Operator --
625 --------------------------------
626
627 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
628 begin
629 if Is_Invisible_Operator (N, T) then
630 Error_Msg_NE -- CODEFIX
631 ("operator for} is not directly visible!", N, First_Subtype (T));
632 Error_Msg_N -- CODEFIX
633 ("use clause would make operation legal!", N);
634 end if;
635 end Check_For_Visible_Operator;
636
637 ----------------------------------
638 -- Check_Fully_Declared_Prefix --
639 ----------------------------------
640
641 procedure Check_Fully_Declared_Prefix
642 (Typ : Entity_Id;
643 Pref : Node_Id)
644 is
645 begin
646 -- Check that the designated type of the prefix of a dereference is
647 -- not an incomplete type. This cannot be done unconditionally, because
648 -- dereferences of private types are legal in default expressions. This
649 -- case is taken care of in Check_Fully_Declared, called below. There
650 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
651
652 -- This consideration also applies to similar checks for allocators,
653 -- qualified expressions, and type conversions.
654
655 -- An additional exception concerns other per-object expressions that
656 -- are not directly related to component declarations, in particular
657 -- representation pragmas for tasks. These will be per-object
658 -- expressions if they depend on discriminants or some global entity.
659 -- If the task has access discriminants, the designated type may be
660 -- incomplete at the point the expression is resolved. This resolution
661 -- takes place within the body of the initialization procedure, where
662 -- the discriminant is replaced by its discriminal.
663
664 if Is_Entity_Name (Pref)
665 and then Ekind (Entity (Pref)) = E_In_Parameter
666 then
667 null;
668
669 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
670 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
671 -- Analyze_Object_Renaming, and Freeze_Entity.
672
673 elsif Ada_Version >= Ada_2005
674 and then Is_Entity_Name (Pref)
675 and then Is_Access_Type (Etype (Pref))
676 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
677 E_Incomplete_Type
678 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
679 then
680 null;
681 else
682 Check_Fully_Declared (Typ, Parent (Pref));
683 end if;
684 end Check_Fully_Declared_Prefix;
685
686 ------------------------------
687 -- Check_Infinite_Recursion --
688 ------------------------------
689
690 function Check_Infinite_Recursion (N : Node_Id) return Boolean is
691 P : Node_Id;
692 C : Node_Id;
693
694 function Same_Argument_List return Boolean;
695 -- Check whether list of actuals is identical to list of formals of
696 -- called function (which is also the enclosing scope).
697
698 ------------------------
699 -- Same_Argument_List --
700 ------------------------
701
702 function Same_Argument_List return Boolean is
703 A : Node_Id;
704 F : Entity_Id;
705 Subp : Entity_Id;
706
707 begin
708 if not Is_Entity_Name (Name (N)) then
709 return False;
710 else
711 Subp := Entity (Name (N));
712 end if;
713
714 F := First_Formal (Subp);
715 A := First_Actual (N);
716 while Present (F) and then Present (A) loop
717 if not Is_Entity_Name (A)
718 or else Entity (A) /= F
719 then
720 return False;
721 end if;
722
723 Next_Actual (A);
724 Next_Formal (F);
725 end loop;
726
727 return True;
728 end Same_Argument_List;
729
730 -- Start of processing for Check_Infinite_Recursion
731
732 begin
733 -- Special case, if this is a procedure call and is a call to the
734 -- current procedure with the same argument list, then this is for
735 -- sure an infinite recursion and we insert a call to raise SE.
736
737 if Is_List_Member (N)
738 and then List_Length (List_Containing (N)) = 1
739 and then Same_Argument_List
740 then
741 declare
742 P : constant Node_Id := Parent (N);
743 begin
744 if Nkind (P) = N_Handled_Sequence_Of_Statements
745 and then Nkind (Parent (P)) = N_Subprogram_Body
746 and then Is_Empty_List (Declarations (Parent (P)))
747 then
748 Error_Msg_Warn := SPARK_Mode /= On;
749 Error_Msg_N ("!infinite recursion<<", N);
750 Error_Msg_N ("\!Storage_Error [<<", N);
751 Insert_Action (N,
752 Make_Raise_Storage_Error (Sloc (N),
753 Reason => SE_Infinite_Recursion));
754 return True;
755 end if;
756 end;
757 end if;
758
759 -- If not that special case, search up tree, quitting if we reach a
760 -- construct (e.g. a conditional) that tells us that this is not a
761 -- case for an infinite recursion warning.
762
763 C := N;
764 loop
765 P := Parent (C);
766
767 -- If no parent, then we were not inside a subprogram, this can for
768 -- example happen when processing certain pragmas in a spec. Just
769 -- return False in this case.
770
771 if No (P) then
772 return False;
773 end if;
774
775 -- Done if we get to subprogram body, this is definitely an infinite
776 -- recursion case if we did not find anything to stop us.
777
778 exit when Nkind (P) = N_Subprogram_Body;
779
780 -- If appearing in conditional, result is false
781
782 if Nkind_In (P, N_Or_Else,
783 N_And_Then,
784 N_Case_Expression,
785 N_Case_Statement,
786 N_If_Expression,
787 N_If_Statement)
788 then
789 return False;
790
791 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
792 and then C /= First (Statements (P))
793 then
794 -- If the call is the expression of a return statement and the
795 -- actuals are identical to the formals, it's worth a warning.
796 -- However, we skip this if there is an immediately preceding
797 -- raise statement, since the call is never executed.
798
799 -- Furthermore, this corresponds to a common idiom:
800
801 -- function F (L : Thing) return Boolean is
802 -- begin
803 -- raise Program_Error;
804 -- return F (L);
805 -- end F;
806
807 -- for generating a stub function
808
809 if Nkind (Parent (N)) = N_Simple_Return_Statement
810 and then Same_Argument_List
811 then
812 exit when not Is_List_Member (Parent (N));
813
814 -- OK, return statement is in a statement list, look for raise
815
816 declare
817 Nod : Node_Id;
818
819 begin
820 -- Skip past N_Freeze_Entity nodes generated by expansion
821
822 Nod := Prev (Parent (N));
823 while Present (Nod)
824 and then Nkind (Nod) = N_Freeze_Entity
825 loop
826 Prev (Nod);
827 end loop;
828
829 -- If no raise statement, give warning. We look at the
830 -- original node, because in the case of "raise ... with
831 -- ...", the node has been transformed into a call.
832
833 exit when Nkind (Original_Node (Nod)) /= N_Raise_Statement
834 and then
835 (Nkind (Nod) not in N_Raise_xxx_Error
836 or else Present (Condition (Nod)));
837 end;
838 end if;
839
840 return False;
841
842 else
843 C := P;
844 end if;
845 end loop;
846
847 Error_Msg_Warn := SPARK_Mode /= On;
848 Error_Msg_N ("!possible infinite recursion<<", N);
849 Error_Msg_N ("\!??Storage_Error ]<<", N);
850
851 return True;
852 end Check_Infinite_Recursion;
853
854 -------------------------------
855 -- Check_Initialization_Call --
856 -------------------------------
857
858 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
859 Typ : constant Entity_Id := Etype (First_Formal (Nam));
860
861 function Uses_SS (T : Entity_Id) return Boolean;
862 -- Check whether the creation of an object of the type will involve
863 -- use of the secondary stack. If T is a record type, this is true
864 -- if the expression for some component uses the secondary stack, e.g.
865 -- through a call to a function that returns an unconstrained value.
866 -- False if T is controlled, because cleanups occur elsewhere.
867
868 -------------
869 -- Uses_SS --
870 -------------
871
872 function Uses_SS (T : Entity_Id) return Boolean is
873 Comp : Entity_Id;
874 Expr : Node_Id;
875 Full_Type : Entity_Id := Underlying_Type (T);
876
877 begin
878 -- Normally we want to use the underlying type, but if it's not set
879 -- then continue with T.
880
881 if not Present (Full_Type) then
882 Full_Type := T;
883 end if;
884
885 if Is_Controlled (Full_Type) then
886 return False;
887
888 elsif Is_Array_Type (Full_Type) then
889 return Uses_SS (Component_Type (Full_Type));
890
891 elsif Is_Record_Type (Full_Type) then
892 Comp := First_Component (Full_Type);
893 while Present (Comp) loop
894 if Ekind (Comp) = E_Component
895 and then Nkind (Parent (Comp)) = N_Component_Declaration
896 then
897 -- The expression for a dynamic component may be rewritten
898 -- as a dereference, so retrieve original node.
899
900 Expr := Original_Node (Expression (Parent (Comp)));
901
902 -- Return True if the expression is a call to a function
903 -- (including an attribute function such as Image, or a
904 -- user-defined operator) with a result that requires a
905 -- transient scope.
906
907 if (Nkind (Expr) = N_Function_Call
908 or else Nkind (Expr) in N_Op
909 or else (Nkind (Expr) = N_Attribute_Reference
910 and then Present (Expressions (Expr))))
911 and then Requires_Transient_Scope (Etype (Expr))
912 then
913 return True;
914
915 elsif Uses_SS (Etype (Comp)) then
916 return True;
917 end if;
918 end if;
919
920 Next_Component (Comp);
921 end loop;
922
923 return False;
924
925 else
926 return False;
927 end if;
928 end Uses_SS;
929
930 -- Start of processing for Check_Initialization_Call
931
932 begin
933 -- Establish a transient scope if the type needs it
934
935 if Uses_SS (Typ) then
936 Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
937 end if;
938 end Check_Initialization_Call;
939
940 ---------------------------------------
941 -- Check_No_Direct_Boolean_Operators --
942 ---------------------------------------
943
944 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
945 begin
946 if Scope (Entity (N)) = Standard_Standard
947 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
948 then
949 -- Restriction only applies to original source code
950
951 if Comes_From_Source (N) then
952 Check_Restriction (No_Direct_Boolean_Operators, N);
953 end if;
954 end if;
955
956 -- Do style check (but skip if in instance, error is on template)
957
958 if Style_Check then
959 if not In_Instance then
960 Check_Boolean_Operator (N);
961 end if;
962 end if;
963 end Check_No_Direct_Boolean_Operators;
964
965 ------------------------------
966 -- Check_Parameterless_Call --
967 ------------------------------
968
969 procedure Check_Parameterless_Call (N : Node_Id) is
970 Nam : Node_Id;
971
972 function Prefix_Is_Access_Subp return Boolean;
973 -- If the prefix is of an access_to_subprogram type, the node must be
974 -- rewritten as a call. Ditto if the prefix is overloaded and all its
975 -- interpretations are access to subprograms.
976
977 ---------------------------
978 -- Prefix_Is_Access_Subp --
979 ---------------------------
980
981 function Prefix_Is_Access_Subp return Boolean is
982 I : Interp_Index;
983 It : Interp;
984
985 begin
986 -- If the context is an attribute reference that can apply to
987 -- functions, this is never a parameterless call (RM 4.1.4(6)).
988
989 if Nkind (Parent (N)) = N_Attribute_Reference
990 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
991 Name_Code_Address,
992 Name_Access)
993 then
994 return False;
995 end if;
996
997 if not Is_Overloaded (N) then
998 return
999 Ekind (Etype (N)) = E_Subprogram_Type
1000 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1001 else
1002 Get_First_Interp (N, I, It);
1003 while Present (It.Typ) loop
1004 if Ekind (It.Typ) /= E_Subprogram_Type
1005 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1006 then
1007 return False;
1008 end if;
1009
1010 Get_Next_Interp (I, It);
1011 end loop;
1012
1013 return True;
1014 end if;
1015 end Prefix_Is_Access_Subp;
1016
1017 -- Start of processing for Check_Parameterless_Call
1018
1019 begin
1020 -- Defend against junk stuff if errors already detected
1021
1022 if Total_Errors_Detected /= 0 then
1023 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1024 return;
1025 elsif Nkind (N) in N_Has_Chars
1026 and then Chars (N) in Error_Name_Or_No_Name
1027 then
1028 return;
1029 end if;
1030
1031 Require_Entity (N);
1032 end if;
1033
1034 -- If the context expects a value, and the name is a procedure, this is
1035 -- most likely a missing 'Access. Don't try to resolve the parameterless
1036 -- call, error will be caught when the outer call is analyzed.
1037
1038 if Is_Entity_Name (N)
1039 and then Ekind (Entity (N)) = E_Procedure
1040 and then not Is_Overloaded (N)
1041 and then
1042 Nkind_In (Parent (N), N_Parameter_Association,
1043 N_Function_Call,
1044 N_Procedure_Call_Statement)
1045 then
1046 return;
1047 end if;
1048
1049 -- Rewrite as call if overloadable entity that is (or could be, in the
1050 -- overloaded case) a function call. If we know for sure that the entity
1051 -- is an enumeration literal, we do not rewrite it.
1052
1053 -- If the entity is the name of an operator, it cannot be a call because
1054 -- operators cannot have default parameters. In this case, this must be
1055 -- a string whose contents coincide with an operator name. Set the kind
1056 -- of the node appropriately.
1057
1058 if (Is_Entity_Name (N)
1059 and then Nkind (N) /= N_Operator_Symbol
1060 and then Is_Overloadable (Entity (N))
1061 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1062 or else Is_Overloaded (N)))
1063
1064 -- Rewrite as call if it is an explicit dereference of an expression of
1065 -- a subprogram access type, and the subprogram type is not that of a
1066 -- procedure or entry.
1067
1068 or else
1069 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1070
1071 -- Rewrite as call if it is a selected component which is a function,
1072 -- this is the case of a call to a protected function (which may be
1073 -- overloaded with other protected operations).
1074
1075 or else
1076 (Nkind (N) = N_Selected_Component
1077 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1078 or else
1079 (Ekind_In (Entity (Selector_Name (N)), E_Entry,
1080 E_Procedure)
1081 and then Is_Overloaded (Selector_Name (N)))))
1082
1083 -- If one of the above three conditions is met, rewrite as call. Apply
1084 -- the rewriting only once.
1085
1086 then
1087 if Nkind (Parent (N)) /= N_Function_Call
1088 or else N /= Name (Parent (N))
1089 then
1090
1091 -- This may be a prefixed call that was not fully analyzed, e.g.
1092 -- an actual in an instance.
1093
1094 if Ada_Version >= Ada_2005
1095 and then Nkind (N) = N_Selected_Component
1096 and then Is_Dispatching_Operation (Entity (Selector_Name (N)))
1097 then
1098 Analyze_Selected_Component (N);
1099
1100 if Nkind (N) /= N_Selected_Component then
1101 return;
1102 end if;
1103 end if;
1104
1105 Nam := New_Copy (N);
1106
1107 -- If overloaded, overload set belongs to new copy
1108
1109 Save_Interps (N, Nam);
1110
1111 -- Change node to parameterless function call (note that the
1112 -- Parameter_Associations associations field is left set to Empty,
1113 -- its normal default value since there are no parameters)
1114
1115 Change_Node (N, N_Function_Call);
1116 Set_Name (N, Nam);
1117 Set_Sloc (N, Sloc (Nam));
1118 Analyze_Call (N);
1119 end if;
1120
1121 elsif Nkind (N) = N_Parameter_Association then
1122 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1123
1124 elsif Nkind (N) = N_Operator_Symbol then
1125 Change_Operator_Symbol_To_String_Literal (N);
1126 Set_Is_Overloaded (N, False);
1127 Set_Etype (N, Any_String);
1128 end if;
1129 end Check_Parameterless_Call;
1130
1131 -----------------------------
1132 -- Is_Definite_Access_Type --
1133 -----------------------------
1134
1135 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1136 Btyp : constant Entity_Id := Base_Type (E);
1137 begin
1138 return Ekind (Btyp) = E_Access_Type
1139 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1140 and then Comes_From_Source (Btyp));
1141 end Is_Definite_Access_Type;
1142
1143 ----------------------
1144 -- Is_Predefined_Op --
1145 ----------------------
1146
1147 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1148 begin
1149 -- Predefined operators are intrinsic subprograms
1150
1151 if not Is_Intrinsic_Subprogram (Nam) then
1152 return False;
1153 end if;
1154
1155 -- A call to a back-end builtin is never a predefined operator
1156
1157 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1158 return False;
1159 end if;
1160
1161 return not Is_Generic_Instance (Nam)
1162 and then Chars (Nam) in Any_Operator_Name
1163 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1164 end Is_Predefined_Op;
1165
1166 -----------------------------
1167 -- Make_Call_Into_Operator --
1168 -----------------------------
1169
1170 procedure Make_Call_Into_Operator
1171 (N : Node_Id;
1172 Typ : Entity_Id;
1173 Op_Id : Entity_Id)
1174 is
1175 Op_Name : constant Name_Id := Chars (Op_Id);
1176 Act1 : Node_Id := First_Actual (N);
1177 Act2 : Node_Id := Next_Actual (Act1);
1178 Error : Boolean := False;
1179 Func : constant Entity_Id := Entity (Name (N));
1180 Is_Binary : constant Boolean := Present (Act2);
1181 Op_Node : Node_Id;
1182 Opnd_Type : Entity_Id;
1183 Orig_Type : Entity_Id := Empty;
1184 Pack : Entity_Id;
1185
1186 type Kind_Test is access function (E : Entity_Id) return Boolean;
1187
1188 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1189 -- If the operand is not universal, and the operator is given by an
1190 -- expanded name, verify that the operand has an interpretation with a
1191 -- type defined in the given scope of the operator.
1192
1193 function Type_In_P (Test : Kind_Test) return Entity_Id;
1194 -- Find a type of the given class in package Pack that contains the
1195 -- operator.
1196
1197 ---------------------------
1198 -- Operand_Type_In_Scope --
1199 ---------------------------
1200
1201 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1202 Nod : constant Node_Id := Right_Opnd (Op_Node);
1203 I : Interp_Index;
1204 It : Interp;
1205
1206 begin
1207 if not Is_Overloaded (Nod) then
1208 return Scope (Base_Type (Etype (Nod))) = S;
1209
1210 else
1211 Get_First_Interp (Nod, I, It);
1212 while Present (It.Typ) loop
1213 if Scope (Base_Type (It.Typ)) = S then
1214 return True;
1215 end if;
1216
1217 Get_Next_Interp (I, It);
1218 end loop;
1219
1220 return False;
1221 end if;
1222 end Operand_Type_In_Scope;
1223
1224 ---------------
1225 -- Type_In_P --
1226 ---------------
1227
1228 function Type_In_P (Test : Kind_Test) return Entity_Id is
1229 E : Entity_Id;
1230
1231 function In_Decl return Boolean;
1232 -- Verify that node is not part of the type declaration for the
1233 -- candidate type, which would otherwise be invisible.
1234
1235 -------------
1236 -- In_Decl --
1237 -------------
1238
1239 function In_Decl return Boolean is
1240 Decl_Node : constant Node_Id := Parent (E);
1241 N2 : Node_Id;
1242
1243 begin
1244 N2 := N;
1245
1246 if Etype (E) = Any_Type then
1247 return True;
1248
1249 elsif No (Decl_Node) then
1250 return False;
1251
1252 else
1253 while Present (N2)
1254 and then Nkind (N2) /= N_Compilation_Unit
1255 loop
1256 if N2 = Decl_Node then
1257 return True;
1258 else
1259 N2 := Parent (N2);
1260 end if;
1261 end loop;
1262
1263 return False;
1264 end if;
1265 end In_Decl;
1266
1267 -- Start of processing for Type_In_P
1268
1269 begin
1270 -- If the context type is declared in the prefix package, this is the
1271 -- desired base type.
1272
1273 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1274 return Base_Type (Typ);
1275
1276 else
1277 E := First_Entity (Pack);
1278 while Present (E) loop
1279 if Test (E)
1280 and then not In_Decl
1281 then
1282 return E;
1283 end if;
1284
1285 Next_Entity (E);
1286 end loop;
1287
1288 return Empty;
1289 end if;
1290 end Type_In_P;
1291
1292 -- Start of processing for Make_Call_Into_Operator
1293
1294 begin
1295 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1296
1297 -- Binary operator
1298
1299 if Is_Binary then
1300 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1301 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1302 Save_Interps (Act1, Left_Opnd (Op_Node));
1303 Save_Interps (Act2, Right_Opnd (Op_Node));
1304 Act1 := Left_Opnd (Op_Node);
1305 Act2 := Right_Opnd (Op_Node);
1306
1307 -- Unary operator
1308
1309 else
1310 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1311 Save_Interps (Act1, Right_Opnd (Op_Node));
1312 Act1 := Right_Opnd (Op_Node);
1313 end if;
1314
1315 -- If the operator is denoted by an expanded name, and the prefix is
1316 -- not Standard, but the operator is a predefined one whose scope is
1317 -- Standard, then this is an implicit_operator, inserted as an
1318 -- interpretation by the procedure of the same name. This procedure
1319 -- overestimates the presence of implicit operators, because it does
1320 -- not examine the type of the operands. Verify now that the operand
1321 -- type appears in the given scope. If right operand is universal,
1322 -- check the other operand. In the case of concatenation, either
1323 -- argument can be the component type, so check the type of the result.
1324 -- If both arguments are literals, look for a type of the right kind
1325 -- defined in the given scope. This elaborate nonsense is brought to
1326 -- you courtesy of b33302a. The type itself must be frozen, so we must
1327 -- find the type of the proper class in the given scope.
1328
1329 -- A final wrinkle is the multiplication operator for fixed point types,
1330 -- which is defined in Standard only, and not in the scope of the
1331 -- fixed point type itself.
1332
1333 if Nkind (Name (N)) = N_Expanded_Name then
1334 Pack := Entity (Prefix (Name (N)));
1335
1336 -- If this is a package renaming, get renamed entity, which will be
1337 -- the scope of the operands if operaton is type-correct.
1338
1339 if Present (Renamed_Entity (Pack)) then
1340 Pack := Renamed_Entity (Pack);
1341 end if;
1342
1343 -- If the entity being called is defined in the given package, it is
1344 -- a renaming of a predefined operator, and known to be legal.
1345
1346 if Scope (Entity (Name (N))) = Pack
1347 and then Pack /= Standard_Standard
1348 then
1349 null;
1350
1351 -- Visibility does not need to be checked in an instance: if the
1352 -- operator was not visible in the generic it has been diagnosed
1353 -- already, else there is an implicit copy of it in the instance.
1354
1355 elsif In_Instance then
1356 null;
1357
1358 elsif Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide)
1359 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1360 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1361 then
1362 if Pack /= Standard_Standard then
1363 Error := True;
1364 end if;
1365
1366 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1367 -- available.
1368
1369 elsif Ada_Version >= Ada_2005
1370 and then Nam_In (Op_Name, Name_Op_Eq, Name_Op_Ne)
1371 and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1372 then
1373 null;
1374
1375 else
1376 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1377
1378 if Op_Name = Name_Op_Concat then
1379 Opnd_Type := Base_Type (Typ);
1380
1381 elsif (Scope (Opnd_Type) = Standard_Standard
1382 and then Is_Binary)
1383 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1384 and then Is_Binary
1385 and then not Comes_From_Source (Opnd_Type))
1386 then
1387 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1388 end if;
1389
1390 if Scope (Opnd_Type) = Standard_Standard then
1391
1392 -- Verify that the scope contains a type that corresponds to
1393 -- the given literal. Optimize the case where Pack is Standard.
1394
1395 if Pack /= Standard_Standard then
1396
1397 if Opnd_Type = Universal_Integer then
1398 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1399
1400 elsif Opnd_Type = Universal_Real then
1401 Orig_Type := Type_In_P (Is_Real_Type'Access);
1402
1403 elsif Opnd_Type = Any_String then
1404 Orig_Type := Type_In_P (Is_String_Type'Access);
1405
1406 elsif Opnd_Type = Any_Access then
1407 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1408
1409 elsif Opnd_Type = Any_Composite then
1410 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1411
1412 if Present (Orig_Type) then
1413 if Has_Private_Component (Orig_Type) then
1414 Orig_Type := Empty;
1415 else
1416 Set_Etype (Act1, Orig_Type);
1417
1418 if Is_Binary then
1419 Set_Etype (Act2, Orig_Type);
1420 end if;
1421 end if;
1422 end if;
1423
1424 else
1425 Orig_Type := Empty;
1426 end if;
1427
1428 Error := No (Orig_Type);
1429 end if;
1430
1431 elsif Ekind (Opnd_Type) = E_Allocator_Type
1432 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1433 then
1434 Error := True;
1435
1436 -- If the type is defined elsewhere, and the operator is not
1437 -- defined in the given scope (by a renaming declaration, e.g.)
1438 -- then this is an error as well. If an extension of System is
1439 -- present, and the type may be defined there, Pack must be
1440 -- System itself.
1441
1442 elsif Scope (Opnd_Type) /= Pack
1443 and then Scope (Op_Id) /= Pack
1444 and then (No (System_Aux_Id)
1445 or else Scope (Opnd_Type) /= System_Aux_Id
1446 or else Pack /= Scope (System_Aux_Id))
1447 then
1448 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1449 Error := True;
1450 else
1451 Error := not Operand_Type_In_Scope (Pack);
1452 end if;
1453
1454 elsif Pack = Standard_Standard
1455 and then not Operand_Type_In_Scope (Standard_Standard)
1456 then
1457 Error := True;
1458 end if;
1459 end if;
1460
1461 if Error then
1462 Error_Msg_Node_2 := Pack;
1463 Error_Msg_NE
1464 ("& not declared in&", N, Selector_Name (Name (N)));
1465 Set_Etype (N, Any_Type);
1466 return;
1467
1468 -- Detect a mismatch between the context type and the result type
1469 -- in the named package, which is otherwise not detected if the
1470 -- operands are universal. Check is only needed if source entity is
1471 -- an operator, not a function that renames an operator.
1472
1473 elsif Nkind (Parent (N)) /= N_Type_Conversion
1474 and then Ekind (Entity (Name (N))) = E_Operator
1475 and then Is_Numeric_Type (Typ)
1476 and then not Is_Universal_Numeric_Type (Typ)
1477 and then Scope (Base_Type (Typ)) /= Pack
1478 and then not In_Instance
1479 then
1480 if Is_Fixed_Point_Type (Typ)
1481 and then Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide)
1482 then
1483 -- Already checked above
1484
1485 null;
1486
1487 -- Operator may be defined in an extension of System
1488
1489 elsif Present (System_Aux_Id)
1490 and then Scope (Opnd_Type) = System_Aux_Id
1491 then
1492 null;
1493
1494 else
1495 -- Could we use Wrong_Type here??? (this would require setting
1496 -- Etype (N) to the actual type found where Typ was expected).
1497
1498 Error_Msg_NE ("expect }", N, Typ);
1499 end if;
1500 end if;
1501 end if;
1502
1503 Set_Chars (Op_Node, Op_Name);
1504
1505 if not Is_Private_Type (Etype (N)) then
1506 Set_Etype (Op_Node, Base_Type (Etype (N)));
1507 else
1508 Set_Etype (Op_Node, Etype (N));
1509 end if;
1510
1511 -- If this is a call to a function that renames a predefined equality,
1512 -- the renaming declaration provides a type that must be used to
1513 -- resolve the operands. This must be done now because resolution of
1514 -- the equality node will not resolve any remaining ambiguity, and it
1515 -- assumes that the first operand is not overloaded.
1516
1517 if Nam_In (Op_Name, Name_Op_Eq, Name_Op_Ne)
1518 and then Ekind (Func) = E_Function
1519 and then Is_Overloaded (Act1)
1520 then
1521 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1522 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1523 end if;
1524
1525 Set_Entity (Op_Node, Op_Id);
1526 Generate_Reference (Op_Id, N, ' ');
1527
1528 -- Do rewrite setting Comes_From_Source on the result if the original
1529 -- call came from source. Although it is not strictly the case that the
1530 -- operator as such comes from the source, logically it corresponds
1531 -- exactly to the function call in the source, so it should be marked
1532 -- this way (e.g. to make sure that validity checks work fine).
1533
1534 declare
1535 CS : constant Boolean := Comes_From_Source (N);
1536 begin
1537 Rewrite (N, Op_Node);
1538 Set_Comes_From_Source (N, CS);
1539 end;
1540
1541 -- If this is an arithmetic operator and the result type is private,
1542 -- the operands and the result must be wrapped in conversion to
1543 -- expose the underlying numeric type and expand the proper checks,
1544 -- e.g. on division.
1545
1546 if Is_Private_Type (Typ) then
1547 case Nkind (N) is
1548 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1549 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
1550 Resolve_Intrinsic_Operator (N, Typ);
1551
1552 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
1553 Resolve_Intrinsic_Unary_Operator (N, Typ);
1554
1555 when others =>
1556 Resolve (N, Typ);
1557 end case;
1558 else
1559 Resolve (N, Typ);
1560 end if;
1561
1562 -- If in ASIS_Mode, propagate operand types to original actuals of
1563 -- function call, which would otherwise not be fully resolved. If
1564 -- the call has already been constant-folded, nothing to do. We
1565 -- relocate the operand nodes rather than copy them, to preserve
1566 -- original_node pointers, given that the operands themselves may
1567 -- have been rewritten.
1568
1569 if ASIS_Mode and then Nkind (N) in N_Op then
1570 if Is_Binary then
1571 Rewrite (First (Parameter_Associations (Original_Node (N))),
1572 Relocate_Node (Left_Opnd (N)));
1573 Rewrite (Next (First (Parameter_Associations (Original_Node (N)))),
1574 Relocate_Node (Right_Opnd (N)));
1575 else
1576 Rewrite (First (Parameter_Associations (Original_Node (N))),
1577 Relocate_Node (Right_Opnd (N)));
1578 end if;
1579
1580 Set_Parent (Original_Node (N), Parent (N));
1581 end if;
1582 end Make_Call_Into_Operator;
1583
1584 -------------------
1585 -- Operator_Kind --
1586 -------------------
1587
1588 function Operator_Kind
1589 (Op_Name : Name_Id;
1590 Is_Binary : Boolean) return Node_Kind
1591 is
1592 Kind : Node_Kind;
1593
1594 begin
1595 -- Use CASE statement or array???
1596
1597 if Is_Binary then
1598 if Op_Name = Name_Op_And then
1599 Kind := N_Op_And;
1600 elsif Op_Name = Name_Op_Or then
1601 Kind := N_Op_Or;
1602 elsif Op_Name = Name_Op_Xor then
1603 Kind := N_Op_Xor;
1604 elsif Op_Name = Name_Op_Eq then
1605 Kind := N_Op_Eq;
1606 elsif Op_Name = Name_Op_Ne then
1607 Kind := N_Op_Ne;
1608 elsif Op_Name = Name_Op_Lt then
1609 Kind := N_Op_Lt;
1610 elsif Op_Name = Name_Op_Le then
1611 Kind := N_Op_Le;
1612 elsif Op_Name = Name_Op_Gt then
1613 Kind := N_Op_Gt;
1614 elsif Op_Name = Name_Op_Ge then
1615 Kind := N_Op_Ge;
1616 elsif Op_Name = Name_Op_Add then
1617 Kind := N_Op_Add;
1618 elsif Op_Name = Name_Op_Subtract then
1619 Kind := N_Op_Subtract;
1620 elsif Op_Name = Name_Op_Concat then
1621 Kind := N_Op_Concat;
1622 elsif Op_Name = Name_Op_Multiply then
1623 Kind := N_Op_Multiply;
1624 elsif Op_Name = Name_Op_Divide then
1625 Kind := N_Op_Divide;
1626 elsif Op_Name = Name_Op_Mod then
1627 Kind := N_Op_Mod;
1628 elsif Op_Name = Name_Op_Rem then
1629 Kind := N_Op_Rem;
1630 elsif Op_Name = Name_Op_Expon then
1631 Kind := N_Op_Expon;
1632 else
1633 raise Program_Error;
1634 end if;
1635
1636 -- Unary operators
1637
1638 else
1639 if Op_Name = Name_Op_Add then
1640 Kind := N_Op_Plus;
1641 elsif Op_Name = Name_Op_Subtract then
1642 Kind := N_Op_Minus;
1643 elsif Op_Name = Name_Op_Abs then
1644 Kind := N_Op_Abs;
1645 elsif Op_Name = Name_Op_Not then
1646 Kind := N_Op_Not;
1647 else
1648 raise Program_Error;
1649 end if;
1650 end if;
1651
1652 return Kind;
1653 end Operator_Kind;
1654
1655 ----------------------------
1656 -- Preanalyze_And_Resolve --
1657 ----------------------------
1658
1659 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1660 Save_Full_Analysis : constant Boolean := Full_Analysis;
1661
1662 begin
1663 Full_Analysis := False;
1664 Expander_Mode_Save_And_Set (False);
1665
1666 -- Normally, we suppress all checks for this preanalysis. There is no
1667 -- point in processing them now, since they will be applied properly
1668 -- and in the proper location when the default expressions reanalyzed
1669 -- and reexpanded later on. We will also have more information at that
1670 -- point for possible suppression of individual checks.
1671
1672 -- However, in SPARK mode, most expansion is suppressed, and this
1673 -- later reanalysis and reexpansion may not occur. SPARK mode does
1674 -- require the setting of checking flags for proof purposes, so we
1675 -- do the SPARK preanalysis without suppressing checks.
1676
1677 -- This special handling for SPARK mode is required for example in the
1678 -- case of Ada 2012 constructs such as quantified expressions, which are
1679 -- expanded in two separate steps.
1680
1681 if GNATprove_Mode then
1682 Analyze_And_Resolve (N, T);
1683 else
1684 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1685 end if;
1686
1687 Expander_Mode_Restore;
1688 Full_Analysis := Save_Full_Analysis;
1689 end Preanalyze_And_Resolve;
1690
1691 -- Version without context type
1692
1693 procedure Preanalyze_And_Resolve (N : Node_Id) is
1694 Save_Full_Analysis : constant Boolean := Full_Analysis;
1695
1696 begin
1697 Full_Analysis := False;
1698 Expander_Mode_Save_And_Set (False);
1699
1700 Analyze (N);
1701 Resolve (N, Etype (N), Suppress => All_Checks);
1702
1703 Expander_Mode_Restore;
1704 Full_Analysis := Save_Full_Analysis;
1705 end Preanalyze_And_Resolve;
1706
1707 ----------------------------------
1708 -- Replace_Actual_Discriminants --
1709 ----------------------------------
1710
1711 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1712 Loc : constant Source_Ptr := Sloc (N);
1713 Tsk : Node_Id := Empty;
1714
1715 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1716 -- Comment needed???
1717
1718 -------------------
1719 -- Process_Discr --
1720 -------------------
1721
1722 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1723 Ent : Entity_Id;
1724
1725 begin
1726 if Nkind (Nod) = N_Identifier then
1727 Ent := Entity (Nod);
1728
1729 if Present (Ent)
1730 and then Ekind (Ent) = E_Discriminant
1731 then
1732 Rewrite (Nod,
1733 Make_Selected_Component (Loc,
1734 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1735 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1736
1737 Set_Etype (Nod, Etype (Ent));
1738 end if;
1739
1740 end if;
1741
1742 return OK;
1743 end Process_Discr;
1744
1745 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1746
1747 -- Start of processing for Replace_Actual_Discriminants
1748
1749 begin
1750 if not Expander_Active then
1751 return;
1752 end if;
1753
1754 if Nkind (Name (N)) = N_Selected_Component then
1755 Tsk := Prefix (Name (N));
1756
1757 elsif Nkind (Name (N)) = N_Indexed_Component then
1758 Tsk := Prefix (Prefix (Name (N)));
1759 end if;
1760
1761 if No (Tsk) then
1762 return;
1763 else
1764 Replace_Discrs (Default);
1765 end if;
1766 end Replace_Actual_Discriminants;
1767
1768 -------------
1769 -- Resolve --
1770 -------------
1771
1772 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1773 Ambiguous : Boolean := False;
1774 Ctx_Type : Entity_Id := Typ;
1775 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1776 Err_Type : Entity_Id := Empty;
1777 Found : Boolean := False;
1778 From_Lib : Boolean;
1779 I : Interp_Index;
1780 I1 : Interp_Index := 0; -- prevent junk warning
1781 It : Interp;
1782 It1 : Interp;
1783 Seen : Entity_Id := Empty; -- prevent junk warning
1784
1785 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1786 -- Determine whether a node comes from a predefined library unit or
1787 -- Standard.
1788
1789 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1790 -- Try and fix up a literal so that it matches its expected type. New
1791 -- literals are manufactured if necessary to avoid cascaded errors.
1792
1793 procedure Report_Ambiguous_Argument;
1794 -- Additional diagnostics when an ambiguous call has an ambiguous
1795 -- argument (typically a controlling actual).
1796
1797 procedure Resolution_Failed;
1798 -- Called when attempt at resolving current expression fails
1799
1800 ------------------------------------
1801 -- Comes_From_Predefined_Lib_Unit --
1802 -------------------------------------
1803
1804 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1805 begin
1806 return
1807 Sloc (Nod) = Standard_Location
1808 or else Is_Predefined_File_Name
1809 (Unit_File_Name (Get_Source_Unit (Sloc (Nod))));
1810 end Comes_From_Predefined_Lib_Unit;
1811
1812 --------------------
1813 -- Patch_Up_Value --
1814 --------------------
1815
1816 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1817 begin
1818 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
1819 Rewrite (N,
1820 Make_Real_Literal (Sloc (N),
1821 Realval => UR_From_Uint (Intval (N))));
1822 Set_Etype (N, Universal_Real);
1823 Set_Is_Static_Expression (N);
1824
1825 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
1826 Rewrite (N,
1827 Make_Integer_Literal (Sloc (N),
1828 Intval => UR_To_Uint (Realval (N))));
1829 Set_Etype (N, Universal_Integer);
1830 Set_Is_Static_Expression (N);
1831
1832 elsif Nkind (N) = N_String_Literal
1833 and then Is_Character_Type (Typ)
1834 then
1835 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1836 Rewrite (N,
1837 Make_Character_Literal (Sloc (N),
1838 Chars => Name_Find,
1839 Char_Literal_Value =>
1840 UI_From_Int (Character'Pos ('A'))));
1841 Set_Etype (N, Any_Character);
1842 Set_Is_Static_Expression (N);
1843
1844 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
1845 Rewrite (N,
1846 Make_String_Literal (Sloc (N),
1847 Strval => End_String));
1848
1849 elsif Nkind (N) = N_Range then
1850 Patch_Up_Value (Low_Bound (N), Typ);
1851 Patch_Up_Value (High_Bound (N), Typ);
1852 end if;
1853 end Patch_Up_Value;
1854
1855 -------------------------------
1856 -- Report_Ambiguous_Argument --
1857 -------------------------------
1858
1859 procedure Report_Ambiguous_Argument is
1860 Arg : constant Node_Id := First (Parameter_Associations (N));
1861 I : Interp_Index;
1862 It : Interp;
1863
1864 begin
1865 if Nkind (Arg) = N_Function_Call
1866 and then Is_Entity_Name (Name (Arg))
1867 and then Is_Overloaded (Name (Arg))
1868 then
1869 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
1870
1871 -- Could use comments on what is going on here???
1872
1873 Get_First_Interp (Name (Arg), I, It);
1874 while Present (It.Nam) loop
1875 Error_Msg_Sloc := Sloc (It.Nam);
1876
1877 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
1878 Error_Msg_N ("interpretation (inherited) #!", Arg);
1879 else
1880 Error_Msg_N ("interpretation #!", Arg);
1881 end if;
1882
1883 Get_Next_Interp (I, It);
1884 end loop;
1885 end if;
1886 end Report_Ambiguous_Argument;
1887
1888 -----------------------
1889 -- Resolution_Failed --
1890 -----------------------
1891
1892 procedure Resolution_Failed is
1893 begin
1894 Patch_Up_Value (N, Typ);
1895 Set_Etype (N, Typ);
1896 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1897 Set_Is_Overloaded (N, False);
1898
1899 -- The caller will return without calling the expander, so we need
1900 -- to set the analyzed flag. Note that it is fine to set Analyzed
1901 -- to True even if we are in the middle of a shallow analysis,
1902 -- (see the spec of sem for more details) since this is an error
1903 -- situation anyway, and there is no point in repeating the
1904 -- analysis later (indeed it won't work to repeat it later, since
1905 -- we haven't got a clear resolution of which entity is being
1906 -- referenced.)
1907
1908 Set_Analyzed (N, True);
1909 return;
1910 end Resolution_Failed;
1911
1912 -- Start of processing for Resolve
1913
1914 begin
1915 if N = Error then
1916 return;
1917 end if;
1918
1919 -- Access attribute on remote subprogram cannot be used for a non-remote
1920 -- access-to-subprogram type.
1921
1922 if Nkind (N) = N_Attribute_Reference
1923 and then Nam_In (Attribute_Name (N), Name_Access,
1924 Name_Unrestricted_Access,
1925 Name_Unchecked_Access)
1926 and then Comes_From_Source (N)
1927 and then Is_Entity_Name (Prefix (N))
1928 and then Is_Subprogram (Entity (Prefix (N)))
1929 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1930 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1931 then
1932 Error_Msg_N
1933 ("prefix must statically denote a non-remote subprogram", N);
1934 end if;
1935
1936 From_Lib := Comes_From_Predefined_Lib_Unit (N);
1937
1938 -- If the context is a Remote_Access_To_Subprogram, access attributes
1939 -- must be resolved with the corresponding fat pointer. There is no need
1940 -- to check for the attribute name since the return type of an
1941 -- attribute is never a remote type.
1942
1943 if Nkind (N) = N_Attribute_Reference
1944 and then Comes_From_Source (N)
1945 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
1946 then
1947 declare
1948 Attr : constant Attribute_Id :=
1949 Get_Attribute_Id (Attribute_Name (N));
1950 Pref : constant Node_Id := Prefix (N);
1951 Decl : Node_Id;
1952 Spec : Node_Id;
1953 Is_Remote : Boolean := True;
1954
1955 begin
1956 -- Check that Typ is a remote access-to-subprogram type
1957
1958 if Is_Remote_Access_To_Subprogram_Type (Typ) then
1959
1960 -- Prefix (N) must statically denote a remote subprogram
1961 -- declared in a package specification.
1962
1963 if Attr = Attribute_Access or else
1964 Attr = Attribute_Unchecked_Access or else
1965 Attr = Attribute_Unrestricted_Access
1966 then
1967 Decl := Unit_Declaration_Node (Entity (Pref));
1968
1969 if Nkind (Decl) = N_Subprogram_Body then
1970 Spec := Corresponding_Spec (Decl);
1971
1972 if not No (Spec) then
1973 Decl := Unit_Declaration_Node (Spec);
1974 end if;
1975 end if;
1976
1977 Spec := Parent (Decl);
1978
1979 if not Is_Entity_Name (Prefix (N))
1980 or else Nkind (Spec) /= N_Package_Specification
1981 or else
1982 not Is_Remote_Call_Interface (Defining_Entity (Spec))
1983 then
1984 Is_Remote := False;
1985 Error_Msg_N
1986 ("prefix must statically denote a remote subprogram ",
1987 N);
1988 end if;
1989
1990 -- If we are generating code in distributed mode, perform
1991 -- semantic checks against corresponding remote entities.
1992
1993 if Expander_Active
1994 and then Get_PCS_Name /= Name_No_DSA
1995 then
1996 Check_Subtype_Conformant
1997 (New_Id => Entity (Prefix (N)),
1998 Old_Id => Designated_Type
1999 (Corresponding_Remote_Type (Typ)),
2000 Err_Loc => N);
2001
2002 if Is_Remote then
2003 Process_Remote_AST_Attribute (N, Typ);
2004 end if;
2005 end if;
2006 end if;
2007 end if;
2008 end;
2009 end if;
2010
2011 Debug_A_Entry ("resolving ", N);
2012
2013 if Debug_Flag_V then
2014 Write_Overloads (N);
2015 end if;
2016
2017 if Comes_From_Source (N) then
2018 if Is_Fixed_Point_Type (Typ) then
2019 Check_Restriction (No_Fixed_Point, N);
2020
2021 elsif Is_Floating_Point_Type (Typ)
2022 and then Typ /= Universal_Real
2023 and then Typ /= Any_Real
2024 then
2025 Check_Restriction (No_Floating_Point, N);
2026 end if;
2027 end if;
2028
2029 -- Return if already analyzed
2030
2031 if Analyzed (N) then
2032 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2033 Analyze_Dimension (N);
2034 return;
2035
2036 -- Any case of Any_Type as the Etype value means that we had a
2037 -- previous error.
2038
2039 elsif Etype (N) = Any_Type then
2040 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2041 return;
2042 end if;
2043
2044 Check_Parameterless_Call (N);
2045
2046 -- The resolution of an Expression_With_Actions is determined by
2047 -- its Expression.
2048
2049 if Nkind (N) = N_Expression_With_Actions then
2050 Resolve (Expression (N), Typ);
2051
2052 Found := True;
2053 Expr_Type := Etype (Expression (N));
2054
2055 -- If not overloaded, then we know the type, and all that needs doing
2056 -- is to check that this type is compatible with the context.
2057
2058 elsif not Is_Overloaded (N) then
2059 Found := Covers (Typ, Etype (N));
2060 Expr_Type := Etype (N);
2061
2062 -- In the overloaded case, we must select the interpretation that
2063 -- is compatible with the context (i.e. the type passed to Resolve)
2064
2065 else
2066 -- Loop through possible interpretations
2067
2068 Get_First_Interp (N, I, It);
2069 Interp_Loop : while Present (It.Typ) loop
2070
2071 if Debug_Flag_V then
2072 Write_Str ("Interp: ");
2073 Write_Interp (It);
2074 end if;
2075
2076 -- We are only interested in interpretations that are compatible
2077 -- with the expected type, any other interpretations are ignored.
2078
2079 if not Covers (Typ, It.Typ) then
2080 if Debug_Flag_V then
2081 Write_Str (" interpretation incompatible with context");
2082 Write_Eol;
2083 end if;
2084
2085 else
2086 -- Skip the current interpretation if it is disabled by an
2087 -- abstract operator. This action is performed only when the
2088 -- type against which we are resolving is the same as the
2089 -- type of the interpretation.
2090
2091 if Ada_Version >= Ada_2005
2092 and then It.Typ = Typ
2093 and then Typ /= Universal_Integer
2094 and then Typ /= Universal_Real
2095 and then Present (It.Abstract_Op)
2096 then
2097 if Debug_Flag_V then
2098 Write_Line ("Skip.");
2099 end if;
2100
2101 goto Continue;
2102 end if;
2103
2104 -- First matching interpretation
2105
2106 if not Found then
2107 Found := True;
2108 I1 := I;
2109 Seen := It.Nam;
2110 Expr_Type := It.Typ;
2111
2112 -- Matching interpretation that is not the first, maybe an
2113 -- error, but there are some cases where preference rules are
2114 -- used to choose between the two possibilities. These and
2115 -- some more obscure cases are handled in Disambiguate.
2116
2117 else
2118 -- If the current statement is part of a predefined library
2119 -- unit, then all interpretations which come from user level
2120 -- packages should not be considered. Check previous and
2121 -- current one.
2122
2123 if From_Lib then
2124 if not Comes_From_Predefined_Lib_Unit (It.Nam) then
2125 goto Continue;
2126
2127 elsif not Comes_From_Predefined_Lib_Unit (Seen) then
2128
2129 -- Previous interpretation must be discarded
2130
2131 I1 := I;
2132 Seen := It.Nam;
2133 Expr_Type := It.Typ;
2134 Set_Entity (N, Seen);
2135 goto Continue;
2136 end if;
2137 end if;
2138
2139 -- Otherwise apply further disambiguation steps
2140
2141 Error_Msg_Sloc := Sloc (Seen);
2142 It1 := Disambiguate (N, I1, I, Typ);
2143
2144 -- Disambiguation has succeeded. Skip the remaining
2145 -- interpretations.
2146
2147 if It1 /= No_Interp then
2148 Seen := It1.Nam;
2149 Expr_Type := It1.Typ;
2150
2151 while Present (It.Typ) loop
2152 Get_Next_Interp (I, It);
2153 end loop;
2154
2155 else
2156 -- Before we issue an ambiguity complaint, check for
2157 -- the case of a subprogram call where at least one
2158 -- of the arguments is Any_Type, and if so, suppress
2159 -- the message, since it is a cascaded error.
2160
2161 if Nkind (N) in N_Subprogram_Call then
2162 declare
2163 A : Node_Id;
2164 E : Node_Id;
2165
2166 begin
2167 A := First_Actual (N);
2168 while Present (A) loop
2169 E := A;
2170
2171 if Nkind (E) = N_Parameter_Association then
2172 E := Explicit_Actual_Parameter (E);
2173 end if;
2174
2175 if Etype (E) = Any_Type then
2176 if Debug_Flag_V then
2177 Write_Str ("Any_Type in call");
2178 Write_Eol;
2179 end if;
2180
2181 exit Interp_Loop;
2182 end if;
2183
2184 Next_Actual (A);
2185 end loop;
2186 end;
2187
2188 elsif Nkind (N) in N_Binary_Op
2189 and then (Etype (Left_Opnd (N)) = Any_Type
2190 or else Etype (Right_Opnd (N)) = Any_Type)
2191 then
2192 exit Interp_Loop;
2193
2194 elsif Nkind (N) in N_Unary_Op
2195 and then Etype (Right_Opnd (N)) = Any_Type
2196 then
2197 exit Interp_Loop;
2198 end if;
2199
2200 -- Not that special case, so issue message using the
2201 -- flag Ambiguous to control printing of the header
2202 -- message only at the start of an ambiguous set.
2203
2204 if not Ambiguous then
2205 if Nkind (N) = N_Function_Call
2206 and then Nkind (Name (N)) = N_Explicit_Dereference
2207 then
2208 Error_Msg_N
2209 ("ambiguous expression "
2210 & "(cannot resolve indirect call)!", N);
2211 else
2212 Error_Msg_NE -- CODEFIX
2213 ("ambiguous expression (cannot resolve&)!",
2214 N, It.Nam);
2215 end if;
2216
2217 Ambiguous := True;
2218
2219 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2220 Error_Msg_N
2221 ("\\possible interpretation (inherited)#!", N);
2222 else
2223 Error_Msg_N -- CODEFIX
2224 ("\\possible interpretation#!", N);
2225 end if;
2226
2227 if Nkind (N) in N_Subprogram_Call
2228 and then Present (Parameter_Associations (N))
2229 then
2230 Report_Ambiguous_Argument;
2231 end if;
2232 end if;
2233
2234 Error_Msg_Sloc := Sloc (It.Nam);
2235
2236 -- By default, the error message refers to the candidate
2237 -- interpretation. But if it is a predefined operator, it
2238 -- is implicitly declared at the declaration of the type
2239 -- of the operand. Recover the sloc of that declaration
2240 -- for the error message.
2241
2242 if Nkind (N) in N_Op
2243 and then Scope (It.Nam) = Standard_Standard
2244 and then not Is_Overloaded (Right_Opnd (N))
2245 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2246 Standard_Standard
2247 then
2248 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2249
2250 if Comes_From_Source (Err_Type)
2251 and then Present (Parent (Err_Type))
2252 then
2253 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2254 end if;
2255
2256 elsif Nkind (N) in N_Binary_Op
2257 and then Scope (It.Nam) = Standard_Standard
2258 and then not Is_Overloaded (Left_Opnd (N))
2259 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2260 Standard_Standard
2261 then
2262 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2263
2264 if Comes_From_Source (Err_Type)
2265 and then Present (Parent (Err_Type))
2266 then
2267 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2268 end if;
2269
2270 -- If this is an indirect call, use the subprogram_type
2271 -- in the message, to have a meaningful location. Also
2272 -- indicate if this is an inherited operation, created
2273 -- by a type declaration.
2274
2275 elsif Nkind (N) = N_Function_Call
2276 and then Nkind (Name (N)) = N_Explicit_Dereference
2277 and then Is_Type (It.Nam)
2278 then
2279 Err_Type := It.Nam;
2280 Error_Msg_Sloc :=
2281 Sloc (Associated_Node_For_Itype (Err_Type));
2282 else
2283 Err_Type := Empty;
2284 end if;
2285
2286 if Nkind (N) in N_Op
2287 and then Scope (It.Nam) = Standard_Standard
2288 and then Present (Err_Type)
2289 then
2290 -- Special-case the message for universal_fixed
2291 -- operators, which are not declared with the type
2292 -- of the operand, but appear forever in Standard.
2293
2294 if It.Typ = Universal_Fixed
2295 and then Scope (It.Nam) = Standard_Standard
2296 then
2297 Error_Msg_N
2298 ("\\possible interpretation as universal_fixed "
2299 & "operation (RM 4.5.5 (19))", N);
2300 else
2301 Error_Msg_N
2302 ("\\possible interpretation (predefined)#!", N);
2303 end if;
2304
2305 elsif
2306 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2307 then
2308 Error_Msg_N
2309 ("\\possible interpretation (inherited)#!", N);
2310 else
2311 Error_Msg_N -- CODEFIX
2312 ("\\possible interpretation#!", N);
2313 end if;
2314
2315 end if;
2316 end if;
2317
2318 -- We have a matching interpretation, Expr_Type is the type
2319 -- from this interpretation, and Seen is the entity.
2320
2321 -- For an operator, just set the entity name. The type will be
2322 -- set by the specific operator resolution routine.
2323
2324 if Nkind (N) in N_Op then
2325 Set_Entity (N, Seen);
2326 Generate_Reference (Seen, N);
2327
2328 elsif Nkind (N) = N_Case_Expression then
2329 Set_Etype (N, Expr_Type);
2330
2331 elsif Nkind (N) = N_Character_Literal then
2332 Set_Etype (N, Expr_Type);
2333
2334 elsif Nkind (N) = N_If_Expression then
2335 Set_Etype (N, Expr_Type);
2336
2337 -- AI05-0139-2: Expression is overloaded because type has
2338 -- implicit dereference. If type matches context, no implicit
2339 -- dereference is involved.
2340
2341 elsif Has_Implicit_Dereference (Expr_Type) then
2342 Set_Etype (N, Expr_Type);
2343 Set_Is_Overloaded (N, False);
2344 exit Interp_Loop;
2345
2346 elsif Is_Overloaded (N)
2347 and then Present (It.Nam)
2348 and then Ekind (It.Nam) = E_Discriminant
2349 and then Has_Implicit_Dereference (It.Nam)
2350 then
2351 -- If the node is a general indexing, the dereference is
2352 -- is inserted when resolving the rewritten form, else
2353 -- insert it now.
2354
2355 if Nkind (N) /= N_Indexed_Component
2356 or else No (Generalized_Indexing (N))
2357 then
2358 Build_Explicit_Dereference (N, It.Nam);
2359 end if;
2360
2361 -- For an explicit dereference, attribute reference, range,
2362 -- short-circuit form (which is not an operator node), or call
2363 -- with a name that is an explicit dereference, there is
2364 -- nothing to be done at this point.
2365
2366 elsif Nkind_In (N, N_Explicit_Dereference,
2367 N_Attribute_Reference,
2368 N_And_Then,
2369 N_Indexed_Component,
2370 N_Or_Else,
2371 N_Range,
2372 N_Selected_Component,
2373 N_Slice)
2374 or else Nkind (Name (N)) = N_Explicit_Dereference
2375 then
2376 null;
2377
2378 -- For procedure or function calls, set the type of the name,
2379 -- and also the entity pointer for the prefix.
2380
2381 elsif Nkind (N) in N_Subprogram_Call
2382 and then Is_Entity_Name (Name (N))
2383 then
2384 Set_Etype (Name (N), Expr_Type);
2385 Set_Entity (Name (N), Seen);
2386 Generate_Reference (Seen, Name (N));
2387
2388 elsif Nkind (N) = N_Function_Call
2389 and then Nkind (Name (N)) = N_Selected_Component
2390 then
2391 Set_Etype (Name (N), Expr_Type);
2392 Set_Entity (Selector_Name (Name (N)), Seen);
2393 Generate_Reference (Seen, Selector_Name (Name (N)));
2394
2395 -- For all other cases, just set the type of the Name
2396
2397 else
2398 Set_Etype (Name (N), Expr_Type);
2399 end if;
2400
2401 end if;
2402
2403 <<Continue>>
2404
2405 -- Move to next interpretation
2406
2407 exit Interp_Loop when No (It.Typ);
2408
2409 Get_Next_Interp (I, It);
2410 end loop Interp_Loop;
2411 end if;
2412
2413 -- At this stage Found indicates whether or not an acceptable
2414 -- interpretation exists. If not, then we have an error, except that if
2415 -- the context is Any_Type as a result of some other error, then we
2416 -- suppress the error report.
2417
2418 if not Found then
2419 if Typ /= Any_Type then
2420
2421 -- If type we are looking for is Void, then this is the procedure
2422 -- call case, and the error is simply that what we gave is not a
2423 -- procedure name (we think of procedure calls as expressions with
2424 -- types internally, but the user doesn't think of them this way).
2425
2426 if Typ = Standard_Void_Type then
2427
2428 -- Special case message if function used as a procedure
2429
2430 if Nkind (N) = N_Procedure_Call_Statement
2431 and then Is_Entity_Name (Name (N))
2432 and then Ekind (Entity (Name (N))) = E_Function
2433 then
2434 Error_Msg_NE
2435 ("cannot use function & in a procedure call",
2436 Name (N), Entity (Name (N)));
2437
2438 -- Otherwise give general message (not clear what cases this
2439 -- covers, but no harm in providing for them).
2440
2441 else
2442 Error_Msg_N ("expect procedure name in procedure call", N);
2443 end if;
2444
2445 Found := True;
2446
2447 -- Otherwise we do have a subexpression with the wrong type
2448
2449 -- Check for the case of an allocator which uses an access type
2450 -- instead of the designated type. This is a common error and we
2451 -- specialize the message, posting an error on the operand of the
2452 -- allocator, complaining that we expected the designated type of
2453 -- the allocator.
2454
2455 elsif Nkind (N) = N_Allocator
2456 and then Is_Access_Type (Typ)
2457 and then Is_Access_Type (Etype (N))
2458 and then Designated_Type (Etype (N)) = Typ
2459 then
2460 Wrong_Type (Expression (N), Designated_Type (Typ));
2461 Found := True;
2462
2463 -- Check for view mismatch on Null in instances, for which the
2464 -- view-swapping mechanism has no identifier.
2465
2466 elsif (In_Instance or else In_Inlined_Body)
2467 and then (Nkind (N) = N_Null)
2468 and then Is_Private_Type (Typ)
2469 and then Is_Access_Type (Full_View (Typ))
2470 then
2471 Resolve (N, Full_View (Typ));
2472 Set_Etype (N, Typ);
2473 return;
2474
2475 -- Check for an aggregate. Sometimes we can get bogus aggregates
2476 -- from misuse of parentheses, and we are about to complain about
2477 -- the aggregate without even looking inside it.
2478
2479 -- Instead, if we have an aggregate of type Any_Composite, then
2480 -- analyze and resolve the component fields, and then only issue
2481 -- another message if we get no errors doing this (otherwise
2482 -- assume that the errors in the aggregate caused the problem).
2483
2484 elsif Nkind (N) = N_Aggregate
2485 and then Etype (N) = Any_Composite
2486 then
2487 -- Disable expansion in any case. If there is a type mismatch
2488 -- it may be fatal to try to expand the aggregate. The flag
2489 -- would otherwise be set to false when the error is posted.
2490
2491 Expander_Active := False;
2492
2493 declare
2494 procedure Check_Aggr (Aggr : Node_Id);
2495 -- Check one aggregate, and set Found to True if we have a
2496 -- definite error in any of its elements
2497
2498 procedure Check_Elmt (Aelmt : Node_Id);
2499 -- Check one element of aggregate and set Found to True if
2500 -- we definitely have an error in the element.
2501
2502 ----------------
2503 -- Check_Aggr --
2504 ----------------
2505
2506 procedure Check_Aggr (Aggr : Node_Id) is
2507 Elmt : Node_Id;
2508
2509 begin
2510 if Present (Expressions (Aggr)) then
2511 Elmt := First (Expressions (Aggr));
2512 while Present (Elmt) loop
2513 Check_Elmt (Elmt);
2514 Next (Elmt);
2515 end loop;
2516 end if;
2517
2518 if Present (Component_Associations (Aggr)) then
2519 Elmt := First (Component_Associations (Aggr));
2520 while Present (Elmt) loop
2521
2522 -- If this is a default-initialized component, then
2523 -- there is nothing to check. The box will be
2524 -- replaced by the appropriate call during late
2525 -- expansion.
2526
2527 if not Box_Present (Elmt) then
2528 Check_Elmt (Expression (Elmt));
2529 end if;
2530
2531 Next (Elmt);
2532 end loop;
2533 end if;
2534 end Check_Aggr;
2535
2536 ----------------
2537 -- Check_Elmt --
2538 ----------------
2539
2540 procedure Check_Elmt (Aelmt : Node_Id) is
2541 begin
2542 -- If we have a nested aggregate, go inside it (to
2543 -- attempt a naked analyze-resolve of the aggregate can
2544 -- cause undesirable cascaded errors). Do not resolve
2545 -- expression if it needs a type from context, as for
2546 -- integer * fixed expression.
2547
2548 if Nkind (Aelmt) = N_Aggregate then
2549 Check_Aggr (Aelmt);
2550
2551 else
2552 Analyze (Aelmt);
2553
2554 if not Is_Overloaded (Aelmt)
2555 and then Etype (Aelmt) /= Any_Fixed
2556 then
2557 Resolve (Aelmt);
2558 end if;
2559
2560 if Etype (Aelmt) = Any_Type then
2561 Found := True;
2562 end if;
2563 end if;
2564 end Check_Elmt;
2565
2566 begin
2567 Check_Aggr (N);
2568 end;
2569 end if;
2570
2571 -- Looks like we have a type error, but check for special case
2572 -- of Address wanted, integer found, with the configuration pragma
2573 -- Allow_Integer_Address active. If we have this case, introduce
2574 -- an unchecked conversion to allow the integer expression to be
2575 -- treated as an Address. The reverse case of integer wanted,
2576 -- Address found, is treated in an analogous manner.
2577
2578 if Address_Integer_Convert_OK (Typ, Etype (N)) then
2579 Rewrite (N, Unchecked_Convert_To (Typ, Relocate_Node (N)));
2580 Analyze_And_Resolve (N, Typ);
2581 return;
2582 end if;
2583
2584 -- That special Allow_Integer_Address check did not appply, so we
2585 -- have a real type error. If an error message was issued already,
2586 -- Found got reset to True, so if it's still False, issue standard
2587 -- Wrong_Type message.
2588
2589 if not Found then
2590 if Is_Overloaded (N) and then Nkind (N) = N_Function_Call then
2591 declare
2592 Subp_Name : Node_Id;
2593
2594 begin
2595 if Is_Entity_Name (Name (N)) then
2596 Subp_Name := Name (N);
2597
2598 elsif Nkind (Name (N)) = N_Selected_Component then
2599
2600 -- Protected operation: retrieve operation name
2601
2602 Subp_Name := Selector_Name (Name (N));
2603
2604 else
2605 raise Program_Error;
2606 end if;
2607
2608 Error_Msg_Node_2 := Typ;
2609 Error_Msg_NE
2610 ("no visible interpretation of& "
2611 & "matches expected type&", N, Subp_Name);
2612 end;
2613
2614 if All_Errors_Mode then
2615 declare
2616 Index : Interp_Index;
2617 It : Interp;
2618
2619 begin
2620 Error_Msg_N ("\\possible interpretations:", N);
2621
2622 Get_First_Interp (Name (N), Index, It);
2623 while Present (It.Nam) loop
2624 Error_Msg_Sloc := Sloc (It.Nam);
2625 Error_Msg_Node_2 := It.Nam;
2626 Error_Msg_NE
2627 ("\\ type& for & declared#", N, It.Typ);
2628 Get_Next_Interp (Index, It);
2629 end loop;
2630 end;
2631
2632 else
2633 Error_Msg_N ("\use -gnatf for details", N);
2634 end if;
2635
2636 else
2637 Wrong_Type (N, Typ);
2638 end if;
2639 end if;
2640 end if;
2641
2642 Resolution_Failed;
2643 return;
2644
2645 -- Test if we have more than one interpretation for the context
2646
2647 elsif Ambiguous then
2648 Resolution_Failed;
2649 return;
2650
2651 -- Only one intepretation
2652
2653 else
2654 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
2655 -- the "+" on T is abstract, and the operands are of universal type,
2656 -- the above code will have (incorrectly) resolved the "+" to the
2657 -- universal one in Standard. Therefore check for this case and give
2658 -- an error. We can't do this earlier, because it would cause legal
2659 -- cases to get errors (when some other type has an abstract "+").
2660
2661 if Ada_Version >= Ada_2005
2662 and then Nkind (N) in N_Op
2663 and then Is_Overloaded (N)
2664 and then Is_Universal_Numeric_Type (Etype (Entity (N)))
2665 then
2666 Get_First_Interp (N, I, It);
2667 while Present (It.Typ) loop
2668 if Present (It.Abstract_Op) and then
2669 Etype (It.Abstract_Op) = Typ
2670 then
2671 Error_Msg_NE
2672 ("cannot call abstract subprogram &!", N, It.Abstract_Op);
2673 return;
2674 end if;
2675
2676 Get_Next_Interp (I, It);
2677 end loop;
2678 end if;
2679
2680 -- Here we have an acceptable interpretation for the context
2681
2682 -- Propagate type information and normalize tree for various
2683 -- predefined operations. If the context only imposes a class of
2684 -- types, rather than a specific type, propagate the actual type
2685 -- downward.
2686
2687 if Typ = Any_Integer or else
2688 Typ = Any_Boolean or else
2689 Typ = Any_Modular or else
2690 Typ = Any_Real or else
2691 Typ = Any_Discrete
2692 then
2693 Ctx_Type := Expr_Type;
2694
2695 -- Any_Fixed is legal in a real context only if a specific fixed-
2696 -- point type is imposed. If Norman Cohen can be confused by this,
2697 -- it deserves a separate message.
2698
2699 if Typ = Any_Real
2700 and then Expr_Type = Any_Fixed
2701 then
2702 Error_Msg_N ("illegal context for mixed mode operation", N);
2703 Set_Etype (N, Universal_Real);
2704 Ctx_Type := Universal_Real;
2705 end if;
2706 end if;
2707
2708 -- A user-defined operator is transformed into a function call at
2709 -- this point, so that further processing knows that operators are
2710 -- really operators (i.e. are predefined operators). User-defined
2711 -- operators that are intrinsic are just renamings of the predefined
2712 -- ones, and need not be turned into calls either, but if they rename
2713 -- a different operator, we must transform the node accordingly.
2714 -- Instantiations of Unchecked_Conversion are intrinsic but are
2715 -- treated as functions, even if given an operator designator.
2716
2717 if Nkind (N) in N_Op
2718 and then Present (Entity (N))
2719 and then Ekind (Entity (N)) /= E_Operator
2720 then
2721
2722 if not Is_Predefined_Op (Entity (N)) then
2723 Rewrite_Operator_As_Call (N, Entity (N));
2724
2725 elsif Present (Alias (Entity (N)))
2726 and then
2727 Nkind (Parent (Parent (Entity (N)))) =
2728 N_Subprogram_Renaming_Declaration
2729 then
2730 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2731
2732 -- If the node is rewritten, it will be fully resolved in
2733 -- Rewrite_Renamed_Operator.
2734
2735 if Analyzed (N) then
2736 return;
2737 end if;
2738 end if;
2739 end if;
2740
2741 case N_Subexpr'(Nkind (N)) is
2742
2743 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2744
2745 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2746
2747 when N_Short_Circuit
2748 => Resolve_Short_Circuit (N, Ctx_Type);
2749
2750 when N_Attribute_Reference
2751 => Resolve_Attribute (N, Ctx_Type);
2752
2753 when N_Case_Expression
2754 => Resolve_Case_Expression (N, Ctx_Type);
2755
2756 when N_Character_Literal
2757 => Resolve_Character_Literal (N, Ctx_Type);
2758
2759 when N_Expanded_Name
2760 => Resolve_Entity_Name (N, Ctx_Type);
2761
2762 when N_Explicit_Dereference
2763 => Resolve_Explicit_Dereference (N, Ctx_Type);
2764
2765 when N_Expression_With_Actions
2766 => Resolve_Expression_With_Actions (N, Ctx_Type);
2767
2768 when N_Extension_Aggregate
2769 => Resolve_Extension_Aggregate (N, Ctx_Type);
2770
2771 when N_Function_Call
2772 => Resolve_Call (N, Ctx_Type);
2773
2774 when N_Identifier
2775 => Resolve_Entity_Name (N, Ctx_Type);
2776
2777 when N_If_Expression
2778 => Resolve_If_Expression (N, Ctx_Type);
2779
2780 when N_Indexed_Component
2781 => Resolve_Indexed_Component (N, Ctx_Type);
2782
2783 when N_Integer_Literal
2784 => Resolve_Integer_Literal (N, Ctx_Type);
2785
2786 when N_Membership_Test
2787 => Resolve_Membership_Op (N, Ctx_Type);
2788
2789 when N_Null => Resolve_Null (N, Ctx_Type);
2790
2791 when N_Op_And | N_Op_Or | N_Op_Xor
2792 => Resolve_Logical_Op (N, Ctx_Type);
2793
2794 when N_Op_Eq | N_Op_Ne
2795 => Resolve_Equality_Op (N, Ctx_Type);
2796
2797 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2798 => Resolve_Comparison_Op (N, Ctx_Type);
2799
2800 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2801
2802 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2803 N_Op_Divide | N_Op_Mod | N_Op_Rem
2804
2805 => Resolve_Arithmetic_Op (N, Ctx_Type);
2806
2807 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2808
2809 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2810
2811 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2812 => Resolve_Unary_Op (N, Ctx_Type);
2813
2814 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2815
2816 when N_Procedure_Call_Statement
2817 => Resolve_Call (N, Ctx_Type);
2818
2819 when N_Operator_Symbol
2820 => Resolve_Operator_Symbol (N, Ctx_Type);
2821
2822 when N_Qualified_Expression
2823 => Resolve_Qualified_Expression (N, Ctx_Type);
2824
2825 -- Why is the following null, needs a comment ???
2826
2827 when N_Quantified_Expression
2828 => null;
2829
2830 when N_Raise_Expression
2831 => Resolve_Raise_Expression (N, Ctx_Type);
2832
2833 when N_Raise_xxx_Error
2834 => Set_Etype (N, Ctx_Type);
2835
2836 when N_Range => Resolve_Range (N, Ctx_Type);
2837
2838 when N_Real_Literal
2839 => Resolve_Real_Literal (N, Ctx_Type);
2840
2841 when N_Reference => Resolve_Reference (N, Ctx_Type);
2842
2843 when N_Selected_Component
2844 => Resolve_Selected_Component (N, Ctx_Type);
2845
2846 when N_Slice => Resolve_Slice (N, Ctx_Type);
2847
2848 when N_String_Literal
2849 => Resolve_String_Literal (N, Ctx_Type);
2850
2851 when N_Type_Conversion
2852 => Resolve_Type_Conversion (N, Ctx_Type);
2853
2854 when N_Unchecked_Expression =>
2855 Resolve_Unchecked_Expression (N, Ctx_Type);
2856
2857 when N_Unchecked_Type_Conversion =>
2858 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
2859 end case;
2860
2861 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
2862 -- expression of an anonymous access type that occurs in the context
2863 -- of a named general access type, except when the expression is that
2864 -- of a membership test. This ensures proper legality checking in
2865 -- terms of allowed conversions (expressions that would be illegal to
2866 -- convert implicitly are allowed in membership tests).
2867
2868 if Ada_Version >= Ada_2012
2869 and then Ekind (Ctx_Type) = E_General_Access_Type
2870 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
2871 and then Nkind (Parent (N)) not in N_Membership_Test
2872 then
2873 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
2874 Analyze_And_Resolve (N, Ctx_Type);
2875 end if;
2876
2877 -- If the subexpression was replaced by a non-subexpression, then
2878 -- all we do is to expand it. The only legitimate case we know of
2879 -- is converting procedure call statement to entry call statements,
2880 -- but there may be others, so we are making this test general.
2881
2882 if Nkind (N) not in N_Subexpr then
2883 Debug_A_Exit ("resolving ", N, " (done)");
2884 Expand (N);
2885 return;
2886 end if;
2887
2888 -- The expression is definitely NOT overloaded at this point, so
2889 -- we reset the Is_Overloaded flag to avoid any confusion when
2890 -- reanalyzing the node.
2891
2892 Set_Is_Overloaded (N, False);
2893
2894 -- Freeze expression type, entity if it is a name, and designated
2895 -- type if it is an allocator (RM 13.14(10,11,13)).
2896
2897 -- Now that the resolution of the type of the node is complete, and
2898 -- we did not detect an error, we can expand this node. We skip the
2899 -- expand call if we are in a default expression, see section
2900 -- "Handling of Default Expressions" in Sem spec.
2901
2902 Debug_A_Exit ("resolving ", N, " (done)");
2903
2904 -- We unconditionally freeze the expression, even if we are in
2905 -- default expression mode (the Freeze_Expression routine tests this
2906 -- flag and only freezes static types if it is set).
2907
2908 -- Ada 2012 (AI05-177): The declaration of an expression function
2909 -- does not cause freezing, but we never reach here in that case.
2910 -- Here we are resolving the corresponding expanded body, so we do
2911 -- need to perform normal freezing.
2912
2913 Freeze_Expression (N);
2914
2915 -- Now we can do the expansion
2916
2917 Expand (N);
2918 end if;
2919 end Resolve;
2920
2921 -------------
2922 -- Resolve --
2923 -------------
2924
2925 -- Version with check(s) suppressed
2926
2927 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2928 begin
2929 if Suppress = All_Checks then
2930 declare
2931 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
2932 begin
2933 Scope_Suppress.Suppress := (others => True);
2934 Resolve (N, Typ);
2935 Scope_Suppress.Suppress := Sva;
2936 end;
2937
2938 else
2939 declare
2940 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
2941 begin
2942 Scope_Suppress.Suppress (Suppress) := True;
2943 Resolve (N, Typ);
2944 Scope_Suppress.Suppress (Suppress) := Svg;
2945 end;
2946 end if;
2947 end Resolve;
2948
2949 -------------
2950 -- Resolve --
2951 -------------
2952
2953 -- Version with implicit type
2954
2955 procedure Resolve (N : Node_Id) is
2956 begin
2957 Resolve (N, Etype (N));
2958 end Resolve;
2959
2960 ---------------------
2961 -- Resolve_Actuals --
2962 ---------------------
2963
2964 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2965 Loc : constant Source_Ptr := Sloc (N);
2966 A : Node_Id;
2967 A_Id : Entity_Id;
2968 A_Typ : Entity_Id;
2969 F : Entity_Id;
2970 F_Typ : Entity_Id;
2971 Prev : Node_Id := Empty;
2972 Orig_A : Node_Id;
2973
2974 procedure Check_Argument_Order;
2975 -- Performs a check for the case where the actuals are all simple
2976 -- identifiers that correspond to the formal names, but in the wrong
2977 -- order, which is considered suspicious and cause for a warning.
2978
2979 procedure Check_Prefixed_Call;
2980 -- If the original node is an overloaded call in prefix notation,
2981 -- insert an 'Access or a dereference as needed over the first actual.
2982 -- Try_Object_Operation has already verified that there is a valid
2983 -- interpretation, but the form of the actual can only be determined
2984 -- once the primitive operation is identified.
2985
2986 procedure Insert_Default;
2987 -- If the actual is missing in a call, insert in the actuals list
2988 -- an instance of the default expression. The insertion is always
2989 -- a named association.
2990
2991 procedure Property_Error
2992 (Var : Node_Id;
2993 Var_Id : Entity_Id;
2994 Prop_Nam : Name_Id);
2995 -- Emit an error concerning variable Var with entity Var_Id that has
2996 -- enabled property Prop_Nam when it acts as an actual parameter in a
2997 -- call and the corresponding formal parameter is of mode IN.
2998
2999 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
3000 -- Check whether T1 and T2, or their full views, are derived from a
3001 -- common type. Used to enforce the restrictions on array conversions
3002 -- of AI95-00246.
3003
3004 function Static_Concatenation (N : Node_Id) return Boolean;
3005 -- Predicate to determine whether an actual that is a concatenation
3006 -- will be evaluated statically and does not need a transient scope.
3007 -- This must be determined before the actual is resolved and expanded
3008 -- because if needed the transient scope must be introduced earlier.
3009
3010 --------------------------
3011 -- Check_Argument_Order --
3012 --------------------------
3013
3014 procedure Check_Argument_Order is
3015 begin
3016 -- Nothing to do if no parameters, or original node is neither a
3017 -- function call nor a procedure call statement (happens in the
3018 -- operator-transformed-to-function call case), or the call does
3019 -- not come from source, or this warning is off.
3020
3021 if not Warn_On_Parameter_Order
3022 or else No (Parameter_Associations (N))
3023 or else Nkind (Original_Node (N)) not in N_Subprogram_Call
3024 or else not Comes_From_Source (N)
3025 then
3026 return;
3027 end if;
3028
3029 declare
3030 Nargs : constant Nat := List_Length (Parameter_Associations (N));
3031
3032 begin
3033 -- Nothing to do if only one parameter
3034
3035 if Nargs < 2 then
3036 return;
3037 end if;
3038
3039 -- Here if at least two arguments
3040
3041 declare
3042 Actuals : array (1 .. Nargs) of Node_Id;
3043 Actual : Node_Id;
3044 Formal : Node_Id;
3045
3046 Wrong_Order : Boolean := False;
3047 -- Set True if an out of order case is found
3048
3049 begin
3050 -- Collect identifier names of actuals, fail if any actual is
3051 -- not a simple identifier, and record max length of name.
3052
3053 Actual := First (Parameter_Associations (N));
3054 for J in Actuals'Range loop
3055 if Nkind (Actual) /= N_Identifier then
3056 return;
3057 else
3058 Actuals (J) := Actual;
3059 Next (Actual);
3060 end if;
3061 end loop;
3062
3063 -- If we got this far, all actuals are identifiers and the list
3064 -- of their names is stored in the Actuals array.
3065
3066 Formal := First_Formal (Nam);
3067 for J in Actuals'Range loop
3068
3069 -- If we ran out of formals, that's odd, probably an error
3070 -- which will be detected elsewhere, but abandon the search.
3071
3072 if No (Formal) then
3073 return;
3074 end if;
3075
3076 -- If name matches and is in order OK
3077
3078 if Chars (Formal) = Chars (Actuals (J)) then
3079 null;
3080
3081 else
3082 -- If no match, see if it is elsewhere in list and if so
3083 -- flag potential wrong order if type is compatible.
3084
3085 for K in Actuals'Range loop
3086 if Chars (Formal) = Chars (Actuals (K))
3087 and then
3088 Has_Compatible_Type (Actuals (K), Etype (Formal))
3089 then
3090 Wrong_Order := True;
3091 goto Continue;
3092 end if;
3093 end loop;
3094
3095 -- No match
3096
3097 return;
3098 end if;
3099
3100 <<Continue>> Next_Formal (Formal);
3101 end loop;
3102
3103 -- If Formals left over, also probably an error, skip warning
3104
3105 if Present (Formal) then
3106 return;
3107 end if;
3108
3109 -- Here we give the warning if something was out of order
3110
3111 if Wrong_Order then
3112 Error_Msg_N
3113 ("?P?actuals for this call may be in wrong order", N);
3114 end if;
3115 end;
3116 end;
3117 end Check_Argument_Order;
3118
3119 -------------------------
3120 -- Check_Prefixed_Call --
3121 -------------------------
3122
3123 procedure Check_Prefixed_Call is
3124 Act : constant Node_Id := First_Actual (N);
3125 A_Type : constant Entity_Id := Etype (Act);
3126 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3127 Orig : constant Node_Id := Original_Node (N);
3128 New_A : Node_Id;
3129
3130 begin
3131 -- Check whether the call is a prefixed call, with or without
3132 -- additional actuals.
3133
3134 if Nkind (Orig) = N_Selected_Component
3135 or else
3136 (Nkind (Orig) = N_Indexed_Component
3137 and then Nkind (Prefix (Orig)) = N_Selected_Component
3138 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3139 and then Is_Entity_Name (Act)
3140 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3141 then
3142 if Is_Access_Type (A_Type)
3143 and then not Is_Access_Type (F_Type)
3144 then
3145 -- Introduce dereference on object in prefix
3146
3147 New_A :=
3148 Make_Explicit_Dereference (Sloc (Act),
3149 Prefix => Relocate_Node (Act));
3150 Rewrite (Act, New_A);
3151 Analyze (Act);
3152
3153 elsif Is_Access_Type (F_Type)
3154 and then not Is_Access_Type (A_Type)
3155 then
3156 -- Introduce an implicit 'Access in prefix
3157
3158 if not Is_Aliased_View (Act) then
3159 Error_Msg_NE
3160 ("object in prefixed call to& must be aliased"
3161 & " (RM-2005 4.3.1 (13))",
3162 Prefix (Act), Nam);
3163 end if;
3164
3165 Rewrite (Act,
3166 Make_Attribute_Reference (Loc,
3167 Attribute_Name => Name_Access,
3168 Prefix => Relocate_Node (Act)));
3169 end if;
3170
3171 Analyze (Act);
3172 end if;
3173 end Check_Prefixed_Call;
3174
3175 --------------------
3176 -- Insert_Default --
3177 --------------------
3178
3179 procedure Insert_Default is
3180 Actval : Node_Id;
3181 Assoc : Node_Id;
3182
3183 begin
3184 -- Missing argument in call, nothing to insert
3185
3186 if No (Default_Value (F)) then
3187 return;
3188
3189 else
3190 -- Note that we do a full New_Copy_Tree, so that any associated
3191 -- Itypes are properly copied. This may not be needed any more,
3192 -- but it does no harm as a safety measure. Defaults of a generic
3193 -- formal may be out of bounds of the corresponding actual (see
3194 -- cc1311b) and an additional check may be required.
3195
3196 Actval :=
3197 New_Copy_Tree
3198 (Default_Value (F),
3199 New_Scope => Current_Scope,
3200 New_Sloc => Loc);
3201
3202 if Is_Concurrent_Type (Scope (Nam))
3203 and then Has_Discriminants (Scope (Nam))
3204 then
3205 Replace_Actual_Discriminants (N, Actval);
3206 end if;
3207
3208 if Is_Overloadable (Nam)
3209 and then Present (Alias (Nam))
3210 then
3211 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3212 and then not Is_Tagged_Type (Etype (F))
3213 then
3214 -- If default is a real literal, do not introduce a
3215 -- conversion whose effect may depend on the run-time
3216 -- size of universal real.
3217
3218 if Nkind (Actval) = N_Real_Literal then
3219 Set_Etype (Actval, Base_Type (Etype (F)));
3220 else
3221 Actval := Unchecked_Convert_To (Etype (F), Actval);
3222 end if;
3223 end if;
3224
3225 if Is_Scalar_Type (Etype (F)) then
3226 Enable_Range_Check (Actval);
3227 end if;
3228
3229 Set_Parent (Actval, N);
3230
3231 -- Resolve aggregates with their base type, to avoid scope
3232 -- anomalies: the subtype was first built in the subprogram
3233 -- declaration, and the current call may be nested.
3234
3235 if Nkind (Actval) = N_Aggregate then
3236 Analyze_And_Resolve (Actval, Etype (F));
3237 else
3238 Analyze_And_Resolve (Actval, Etype (Actval));
3239 end if;
3240
3241 else
3242 Set_Parent (Actval, N);
3243
3244 -- See note above concerning aggregates
3245
3246 if Nkind (Actval) = N_Aggregate
3247 and then Has_Discriminants (Etype (Actval))
3248 then
3249 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3250
3251 -- Resolve entities with their own type, which may differ from
3252 -- the type of a reference in a generic context (the view
3253 -- swapping mechanism did not anticipate the re-analysis of
3254 -- default values in calls).
3255
3256 elsif Is_Entity_Name (Actval) then
3257 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3258
3259 else
3260 Analyze_And_Resolve (Actval, Etype (Actval));
3261 end if;
3262 end if;
3263
3264 -- If default is a tag indeterminate function call, propagate tag
3265 -- to obtain proper dispatching.
3266
3267 if Is_Controlling_Formal (F)
3268 and then Nkind (Default_Value (F)) = N_Function_Call
3269 then
3270 Set_Is_Controlling_Actual (Actval);
3271 end if;
3272
3273 end if;
3274
3275 -- If the default expression raises constraint error, then just
3276 -- silently replace it with an N_Raise_Constraint_Error node, since
3277 -- we already gave the warning on the subprogram spec. If node is
3278 -- already a Raise_Constraint_Error leave as is, to prevent loops in
3279 -- the warnings removal machinery.
3280
3281 if Raises_Constraint_Error (Actval)
3282 and then Nkind (Actval) /= N_Raise_Constraint_Error
3283 then
3284 Rewrite (Actval,
3285 Make_Raise_Constraint_Error (Loc,
3286 Reason => CE_Range_Check_Failed));
3287 Set_Raises_Constraint_Error (Actval);
3288 Set_Etype (Actval, Etype (F));
3289 end if;
3290
3291 Assoc :=
3292 Make_Parameter_Association (Loc,
3293 Explicit_Actual_Parameter => Actval,
3294 Selector_Name => Make_Identifier (Loc, Chars (F)));
3295
3296 -- Case of insertion is first named actual
3297
3298 if No (Prev) or else
3299 Nkind (Parent (Prev)) /= N_Parameter_Association
3300 then
3301 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3302 Set_First_Named_Actual (N, Actval);
3303
3304 if No (Prev) then
3305 if No (Parameter_Associations (N)) then
3306 Set_Parameter_Associations (N, New_List (Assoc));
3307 else
3308 Append (Assoc, Parameter_Associations (N));
3309 end if;
3310
3311 else
3312 Insert_After (Prev, Assoc);
3313 end if;
3314
3315 -- Case of insertion is not first named actual
3316
3317 else
3318 Set_Next_Named_Actual
3319 (Assoc, Next_Named_Actual (Parent (Prev)));
3320 Set_Next_Named_Actual (Parent (Prev), Actval);
3321 Append (Assoc, Parameter_Associations (N));
3322 end if;
3323
3324 Mark_Rewrite_Insertion (Assoc);
3325 Mark_Rewrite_Insertion (Actval);
3326
3327 Prev := Actval;
3328 end Insert_Default;
3329
3330 --------------------
3331 -- Property_Error --
3332 --------------------
3333
3334 procedure Property_Error
3335 (Var : Node_Id;
3336 Var_Id : Entity_Id;
3337 Prop_Nam : Name_Id)
3338 is
3339 begin
3340 Error_Msg_Name_1 := Prop_Nam;
3341 Error_Msg_NE
3342 ("external variable & with enabled property % cannot appear as "
3343 & "actual in procedure call (SPARK RM 7.1.3(11))", Var, Var_Id);
3344 Error_Msg_N ("\\corresponding formal parameter has mode In", Var);
3345 end Property_Error;
3346
3347 -------------------
3348 -- Same_Ancestor --
3349 -------------------
3350
3351 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3352 FT1 : Entity_Id := T1;
3353 FT2 : Entity_Id := T2;
3354
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;
3361
3362 if Is_Private_Type (T2)
3363 and then Present (Full_View (T2))
3364 then
3365 FT2 := Full_View (T2);
3366 end if;
3367
3368 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3369 end Same_Ancestor;
3370
3371 --------------------------
3372 -- Static_Concatenation --
3373 --------------------------
3374
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;
3380
3381 when N_Op_Concat =>
3382
3383 -- Concatenation is static when both operands are static and
3384 -- the concatenation operator is a predefined one.
3385
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));
3391
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;
3402
3403 else
3404 return False;
3405 end if;
3406 end case;
3407 end Static_Concatenation;
3408
3409 -- Start of processing for Resolve_Actuals
3410
3411 begin
3412 Check_Argument_Order;
3413 Check_Function_Writable_Actuals (N);
3414
3415 if Present (First_Actual (N)) then
3416 Check_Prefixed_Call;
3417 end if;
3418
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;
3424
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. Skip this if the actual is a Raise_Expression, whose
3428 -- type is imposed from context.
3429
3430 elsif (Present (A) and then Etype (A) = Any_Type)
3431 or else Etype (F) = Any_Type
3432 then
3433 if Nkind (A) /= N_Raise_Expression then
3434 Set_Etype (N, Any_Type);
3435 return;
3436 end if;
3437 end if;
3438
3439 -- Case where actual is present
3440
3441 -- If the actual is an entity, generate a reference to it now. We
3442 -- do this before the actual is resolved, because a formal of some
3443 -- protected subprogram, or a task discriminant, will be rewritten
3444 -- during expansion, and the source entity reference may be lost.
3445
3446 if Present (A)
3447 and then Is_Entity_Name (A)
3448 and then Comes_From_Source (N)
3449 then
3450 Orig_A := Entity (A);
3451
3452 if Present (Orig_A) then
3453 if Is_Formal (Orig_A)
3454 and then Ekind (F) /= E_In_Parameter
3455 then
3456 Generate_Reference (Orig_A, A, 'm');
3457
3458 elsif not Is_Overloaded (A) then
3459 if Ekind (F) /= E_Out_Parameter then
3460 Generate_Reference (Orig_A, A);
3461
3462 -- RM 6.4.1(12): For an out parameter that is passed by
3463 -- copy, the formal parameter object is created, and:
3464
3465 -- * For an access type, the formal parameter is initialized
3466 -- from the value of the actual, without checking that the
3467 -- value satisfies any constraint, any predicate, or any
3468 -- exclusion of the null value.
3469
3470 -- * For a scalar type that has the Default_Value aspect
3471 -- specified, the formal parameter is initialized from the
3472 -- value of the actual, without checking that the value
3473 -- satisfies any constraint or any predicate.
3474 -- I do not understand why this case is included??? this is
3475 -- not a case where an OUT parameter is treated as IN OUT.
3476
3477 -- * For a composite type with discriminants or that has
3478 -- implicit initial values for any subcomponents, the
3479 -- behavior is as for an in out parameter passed by copy.
3480
3481 -- Hence for these cases we generate the read reference now
3482 -- (the write reference will be generated later by
3483 -- Note_Possible_Modification).
3484
3485 elsif Is_By_Copy_Type (Etype (F))
3486 and then
3487 (Is_Access_Type (Etype (F))
3488 or else
3489 (Is_Scalar_Type (Etype (F))
3490 and then
3491 Present (Default_Aspect_Value (Etype (F))))
3492 or else
3493 (Is_Composite_Type (Etype (F))
3494 and then (Has_Discriminants (Etype (F))
3495 or else Is_Partially_Initialized_Type
3496 (Etype (F)))))
3497 then
3498 Generate_Reference (Orig_A, A);
3499 end if;
3500 end if;
3501 end if;
3502 end if;
3503
3504 if Present (A)
3505 and then (Nkind (Parent (A)) /= N_Parameter_Association
3506 or else Chars (Selector_Name (Parent (A))) = Chars (F))
3507 then
3508 -- If style checking mode on, check match of formal name
3509
3510 if Style_Check then
3511 if Nkind (Parent (A)) = N_Parameter_Association then
3512 Check_Identifier (Selector_Name (Parent (A)), F);
3513 end if;
3514 end if;
3515
3516 -- If the formal is Out or In_Out, do not resolve and expand the
3517 -- conversion, because it is subsequently expanded into explicit
3518 -- temporaries and assignments. However, the object of the
3519 -- conversion can be resolved. An exception is the case of tagged
3520 -- type conversion with a class-wide actual. In that case we want
3521 -- the tag check to occur and no temporary will be needed (no
3522 -- representation change can occur) and the parameter is passed by
3523 -- reference, so we go ahead and resolve the type conversion.
3524 -- Another exception is the case of reference to component or
3525 -- subcomponent of a bit-packed array, in which case we want to
3526 -- defer expansion to the point the in and out assignments are
3527 -- performed.
3528
3529 if Ekind (F) /= E_In_Parameter
3530 and then Nkind (A) = N_Type_Conversion
3531 and then not Is_Class_Wide_Type (Etype (Expression (A)))
3532 then
3533 if Ekind (F) = E_In_Out_Parameter
3534 and then Is_Array_Type (Etype (F))
3535 then
3536 -- In a view conversion, the conversion must be legal in
3537 -- both directions, and thus both component types must be
3538 -- aliased, or neither (4.6 (8)).
3539
3540 -- The extra rule in 4.6 (24.9.2) seems unduly restrictive:
3541 -- the privacy requirement should not apply to generic
3542 -- types, and should be checked in an instance. ARG query
3543 -- is in order ???
3544
3545 if Has_Aliased_Components (Etype (Expression (A))) /=
3546 Has_Aliased_Components (Etype (F))
3547 then
3548 Error_Msg_N
3549 ("both component types in a view conversion must be"
3550 & " aliased, or neither", A);
3551
3552 -- Comment here??? what set of cases???
3553
3554 elsif
3555 not Same_Ancestor (Etype (F), Etype (Expression (A)))
3556 then
3557 -- Check view conv between unrelated by ref array types
3558
3559 if Is_By_Reference_Type (Etype (F))
3560 or else Is_By_Reference_Type (Etype (Expression (A)))
3561 then
3562 Error_Msg_N
3563 ("view conversion between unrelated by reference "
3564 & "array types not allowed (\'A'I-00246)", A);
3565
3566 -- In Ada 2005 mode, check view conversion component
3567 -- type cannot be private, tagged, or volatile. Note
3568 -- that we only apply this to source conversions. The
3569 -- generated code can contain conversions which are
3570 -- not subject to this test, and we cannot extract the
3571 -- component type in such cases since it is not present.
3572
3573 elsif Comes_From_Source (A)
3574 and then Ada_Version >= Ada_2005
3575 then
3576 declare
3577 Comp_Type : constant Entity_Id :=
3578 Component_Type
3579 (Etype (Expression (A)));
3580 begin
3581 if (Is_Private_Type (Comp_Type)
3582 and then not Is_Generic_Type (Comp_Type))
3583 or else Is_Tagged_Type (Comp_Type)
3584 or else Is_Volatile (Comp_Type)
3585 then
3586 Error_Msg_N
3587 ("component type of a view conversion cannot"
3588 & " be private, tagged, or volatile"
3589 & " (RM 4.6 (24))",
3590 Expression (A));
3591 end if;
3592 end;
3593 end if;
3594 end if;
3595 end if;
3596
3597 -- Resolve expression if conversion is all OK
3598
3599 if (Conversion_OK (A)
3600 or else Valid_Conversion (A, Etype (A), Expression (A)))
3601 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
3602 then
3603 Resolve (Expression (A));
3604 end if;
3605
3606 -- If the actual is a function call that returns a limited
3607 -- unconstrained object that needs finalization, create a
3608 -- transient scope for it, so that it can receive the proper
3609 -- finalization list.
3610
3611 elsif Nkind (A) = N_Function_Call
3612 and then Is_Limited_Record (Etype (F))
3613 and then not Is_Constrained (Etype (F))
3614 and then Expander_Active
3615 and then (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3616 then
3617 Establish_Transient_Scope (A, Sec_Stack => False);
3618 Resolve (A, Etype (F));
3619
3620 -- A small optimization: if one of the actuals is a concatenation
3621 -- create a block around a procedure call to recover stack space.
3622 -- This alleviates stack usage when several procedure calls in
3623 -- the same statement list use concatenation. We do not perform
3624 -- this wrapping for code statements, where the argument is a
3625 -- static string, and we want to preserve warnings involving
3626 -- sequences of such statements.
3627
3628 elsif Nkind (A) = N_Op_Concat
3629 and then Nkind (N) = N_Procedure_Call_Statement
3630 and then Expander_Active
3631 and then
3632 not (Is_Intrinsic_Subprogram (Nam)
3633 and then Chars (Nam) = Name_Asm)
3634 and then not Static_Concatenation (A)
3635 then
3636 Establish_Transient_Scope (A, Sec_Stack => False);
3637 Resolve (A, Etype (F));
3638
3639 else
3640 if Nkind (A) = N_Type_Conversion
3641 and then Is_Array_Type (Etype (F))
3642 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3643 and then
3644 (Is_Limited_Type (Etype (F))
3645 or else Is_Limited_Type (Etype (Expression (A))))
3646 then
3647 Error_Msg_N
3648 ("conversion between unrelated limited array types "
3649 & "not allowed ('A'I-00246)", A);
3650
3651 if Is_Limited_Type (Etype (F)) then
3652 Explain_Limited_Type (Etype (F), A);
3653 end if;
3654
3655 if Is_Limited_Type (Etype (Expression (A))) then
3656 Explain_Limited_Type (Etype (Expression (A)), A);
3657 end if;
3658 end if;
3659
3660 -- (Ada 2005: AI-251): If the actual is an allocator whose
3661 -- directly designated type is a class-wide interface, we build
3662 -- an anonymous access type to use it as the type of the
3663 -- allocator. Later, when the subprogram call is expanded, if
3664 -- the interface has a secondary dispatch table the expander
3665 -- will add a type conversion to force the correct displacement
3666 -- of the pointer.
3667
3668 if Nkind (A) = N_Allocator then
3669 declare
3670 DDT : constant Entity_Id :=
3671 Directly_Designated_Type (Base_Type (Etype (F)));
3672
3673 New_Itype : Entity_Id;
3674
3675 begin
3676 if Is_Class_Wide_Type (DDT)
3677 and then Is_Interface (DDT)
3678 then
3679 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
3680 Set_Etype (New_Itype, Etype (A));
3681 Set_Directly_Designated_Type
3682 (New_Itype, Directly_Designated_Type (Etype (A)));
3683 Set_Etype (A, New_Itype);
3684 end if;
3685
3686 -- Ada 2005, AI-162:If the actual is an allocator, the
3687 -- innermost enclosing statement is the master of the
3688 -- created object. This needs to be done with expansion
3689 -- enabled only, otherwise the transient scope will not
3690 -- be removed in the expansion of the wrapped construct.
3691
3692 if (Is_Controlled (DDT) or else Has_Task (DDT))
3693 and then Expander_Active
3694 then
3695 Establish_Transient_Scope (A, Sec_Stack => False);
3696 end if;
3697 end;
3698
3699 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3700 Check_Restriction (No_Access_Parameter_Allocators, A);
3701 end if;
3702 end if;
3703
3704 -- (Ada 2005): The call may be to a primitive operation of a
3705 -- tagged synchronized type, declared outside of the type. In
3706 -- this case the controlling actual must be converted to its
3707 -- corresponding record type, which is the formal type. The
3708 -- actual may be a subtype, either because of a constraint or
3709 -- because it is a generic actual, so use base type to locate
3710 -- concurrent type.
3711
3712 F_Typ := Base_Type (Etype (F));
3713
3714 if Is_Tagged_Type (F_Typ)
3715 and then (Is_Concurrent_Type (F_Typ)
3716 or else Is_Concurrent_Record_Type (F_Typ))
3717 then
3718 -- If the actual is overloaded, look for an interpretation
3719 -- that has a synchronized type.
3720
3721 if not Is_Overloaded (A) then
3722 A_Typ := Base_Type (Etype (A));
3723
3724 else
3725 declare
3726 Index : Interp_Index;
3727 It : Interp;
3728
3729 begin
3730 Get_First_Interp (A, Index, It);
3731 while Present (It.Typ) loop
3732 if Is_Concurrent_Type (It.Typ)
3733 or else Is_Concurrent_Record_Type (It.Typ)
3734 then
3735 A_Typ := Base_Type (It.Typ);
3736 exit;
3737 end if;
3738
3739 Get_Next_Interp (Index, It);
3740 end loop;
3741 end;
3742 end if;
3743
3744 declare
3745 Full_A_Typ : Entity_Id;
3746
3747 begin
3748 if Present (Full_View (A_Typ)) then
3749 Full_A_Typ := Base_Type (Full_View (A_Typ));
3750 else
3751 Full_A_Typ := A_Typ;
3752 end if;
3753
3754 -- Tagged synchronized type (case 1): the actual is a
3755 -- concurrent type.
3756
3757 if Is_Concurrent_Type (A_Typ)
3758 and then Corresponding_Record_Type (A_Typ) = F_Typ
3759 then
3760 Rewrite (A,
3761 Unchecked_Convert_To
3762 (Corresponding_Record_Type (A_Typ), A));
3763 Resolve (A, Etype (F));
3764
3765 -- Tagged synchronized type (case 2): the formal is a
3766 -- concurrent type.
3767
3768 elsif Ekind (Full_A_Typ) = E_Record_Type
3769 and then Present
3770 (Corresponding_Concurrent_Type (Full_A_Typ))
3771 and then Is_Concurrent_Type (F_Typ)
3772 and then Present (Corresponding_Record_Type (F_Typ))
3773 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
3774 then
3775 Resolve (A, Corresponding_Record_Type (F_Typ));
3776
3777 -- Common case
3778
3779 else
3780 Resolve (A, Etype (F));
3781 end if;
3782 end;
3783
3784 -- Not a synchronized operation
3785
3786 else
3787 Resolve (A, Etype (F));
3788 end if;
3789 end if;
3790
3791 A_Typ := Etype (A);
3792 F_Typ := Etype (F);
3793
3794 if Comes_From_Source (Original_Node (N))
3795 and then Nkind_In (Original_Node (N), N_Function_Call,
3796 N_Procedure_Call_Statement)
3797 then
3798 -- In formal mode, check that actual parameters matching
3799 -- formals of tagged types are objects (or ancestor type
3800 -- conversions of objects), not general expressions.
3801
3802 if Is_Actual_Tagged_Parameter (A) then
3803 if Is_SPARK_Object_Reference (A) then
3804 null;
3805
3806 elsif Nkind (A) = N_Type_Conversion then
3807 declare
3808 Operand : constant Node_Id := Expression (A);
3809 Operand_Typ : constant Entity_Id := Etype (Operand);
3810 Target_Typ : constant Entity_Id := A_Typ;
3811
3812 begin
3813 if not Is_SPARK_Object_Reference (Operand) then
3814 Check_SPARK_Restriction
3815 ("object required", Operand);
3816
3817 -- In formal mode, the only view conversions are those
3818 -- involving ancestor conversion of an extended type.
3819
3820 elsif not
3821 (Is_Tagged_Type (Target_Typ)
3822 and then not Is_Class_Wide_Type (Target_Typ)
3823 and then Is_Tagged_Type (Operand_Typ)
3824 and then not Is_Class_Wide_Type (Operand_Typ)
3825 and then Is_Ancestor (Target_Typ, Operand_Typ))
3826 then
3827 if Ekind_In
3828 (F, E_Out_Parameter, E_In_Out_Parameter)
3829 then
3830 Check_SPARK_Restriction
3831 ("ancestor conversion is the only permitted "
3832 & "view conversion", A);
3833 else
3834 Check_SPARK_Restriction
3835 ("ancestor conversion required", A);
3836 end if;
3837
3838 else
3839 null;
3840 end if;
3841 end;
3842
3843 else
3844 Check_SPARK_Restriction ("object required", A);
3845 end if;
3846
3847 -- In formal mode, the only view conversions are those
3848 -- involving ancestor conversion of an extended type.
3849
3850 elsif Nkind (A) = N_Type_Conversion
3851 and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
3852 then
3853 Check_SPARK_Restriction
3854 ("ancestor conversion is the only permitted view "
3855 & "conversion", A);
3856 end if;
3857 end if;
3858
3859 -- has warnings suppressed, then we reset Never_Set_In_Source for
3860 -- the calling entity. The reason for this is to catch cases like
3861 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
3862 -- uses trickery to modify an IN parameter.
3863
3864 if Ekind (F) = E_In_Parameter
3865 and then Is_Entity_Name (A)
3866 and then Present (Entity (A))
3867 and then Ekind (Entity (A)) = E_Variable
3868 and then Has_Warnings_Off (F_Typ)
3869 then
3870 Set_Never_Set_In_Source (Entity (A), False);
3871 end if;
3872
3873 -- Perform error checks for IN and IN OUT parameters
3874
3875 if Ekind (F) /= E_Out_Parameter then
3876
3877 -- Check unset reference. For scalar parameters, it is clearly
3878 -- wrong to pass an uninitialized value as either an IN or
3879 -- IN-OUT parameter. For composites, it is also clearly an
3880 -- error to pass a completely uninitialized value as an IN
3881 -- parameter, but the case of IN OUT is trickier. We prefer
3882 -- not to give a warning here. For example, suppose there is
3883 -- a routine that sets some component of a record to False.
3884 -- It is perfectly reasonable to make this IN-OUT and allow
3885 -- either initialized or uninitialized records to be passed
3886 -- in this case.
3887
3888 -- For partially initialized composite values, we also avoid
3889 -- warnings, since it is quite likely that we are passing a
3890 -- partially initialized value and only the initialized fields
3891 -- will in fact be read in the subprogram.
3892
3893 if Is_Scalar_Type (A_Typ)
3894 or else (Ekind (F) = E_In_Parameter
3895 and then not Is_Partially_Initialized_Type (A_Typ))
3896 then
3897 Check_Unset_Reference (A);
3898 end if;
3899
3900 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3901 -- actual to a nested call, since this is case of reading an
3902 -- out parameter, which is not allowed.
3903
3904 if Ada_Version = Ada_83
3905 and then Is_Entity_Name (A)
3906 and then Ekind (Entity (A)) = E_Out_Parameter
3907 then
3908 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3909 end if;
3910 end if;
3911
3912 -- Case of OUT or IN OUT parameter
3913
3914 if Ekind (F) /= E_In_Parameter then
3915
3916 -- For an Out parameter, check for useless assignment. Note
3917 -- that we can't set Last_Assignment this early, because we may
3918 -- kill current values in Resolve_Call, and that call would
3919 -- clobber the Last_Assignment field.
3920
3921 -- Note: call Warn_On_Useless_Assignment before doing the check
3922 -- below for Is_OK_Variable_For_Out_Formal so that the setting
3923 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
3924 -- reflects the last assignment, not this one.
3925
3926 if Ekind (F) = E_Out_Parameter then
3927 if Warn_On_Modified_As_Out_Parameter (F)
3928 and then Is_Entity_Name (A)
3929 and then Present (Entity (A))
3930 and then Comes_From_Source (N)
3931 then
3932 Warn_On_Useless_Assignment (Entity (A), A);
3933 end if;
3934 end if;
3935
3936 -- Validate the form of the actual. Note that the call to
3937 -- Is_OK_Variable_For_Out_Formal generates the required
3938 -- reference in this case.
3939
3940 -- A call to an initialization procedure for an aggregate
3941 -- component may initialize a nested component of a constant
3942 -- designated object. In this context the object is variable.
3943
3944 if not Is_OK_Variable_For_Out_Formal (A)
3945 and then not Is_Init_Proc (Nam)
3946 then
3947 Error_Msg_NE ("actual for& must be a variable", A, F);
3948
3949 if Is_Subprogram (Current_Scope)
3950 and then
3951 (Is_Invariant_Procedure (Current_Scope)
3952 or else Is_Predicate_Function (Current_Scope))
3953 then
3954 Error_Msg_N
3955 ("function used in predicate cannot "
3956 & "modify its argument", F);
3957 end if;
3958 end if;
3959
3960 -- What's the following about???
3961
3962 if Is_Entity_Name (A) then
3963 Kill_Checks (Entity (A));
3964 else
3965 Kill_All_Checks;
3966 end if;
3967 end if;
3968
3969 if Etype (A) = Any_Type then
3970 Set_Etype (N, Any_Type);
3971 return;
3972 end if;
3973
3974 -- Apply appropriate range checks for in, out, and in-out
3975 -- parameters. Out and in-out parameters also need a separate
3976 -- check, if there is a type conversion, to make sure the return
3977 -- value meets the constraints of the variable before the
3978 -- conversion.
3979
3980 -- Gigi looks at the check flag and uses the appropriate types.
3981 -- For now since one flag is used there is an optimization which
3982 -- might not be done in the In Out case since Gigi does not do
3983 -- any analysis. More thought required about this ???
3984
3985 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
3986
3987 -- Apply predicate checks, unless this is a call to the
3988 -- predicate check function itself, which would cause an
3989 -- infinite recursion, or it is a call to an initialization
3990 -- procedure whose operand is of course an unfinished object.
3991
3992 if not (Ekind (Nam) = E_Function
3993 and then (Is_Predicate_Function (Nam)
3994 or else
3995 Is_Predicate_Function_M (Nam)))
3996 and then not Is_Init_Proc (Nam)
3997 then
3998 Apply_Predicate_Check (A, F_Typ);
3999 end if;
4000
4001 -- Apply required constraint checks
4002
4003 if Is_Scalar_Type (Etype (A)) then
4004 Apply_Scalar_Range_Check (A, F_Typ);
4005
4006 elsif Is_Array_Type (Etype (A)) then
4007 Apply_Length_Check (A, F_Typ);
4008
4009 elsif Is_Record_Type (F_Typ)
4010 and then Has_Discriminants (F_Typ)
4011 and then Is_Constrained (F_Typ)
4012 and then (not Is_Derived_Type (F_Typ)
4013 or else Comes_From_Source (Nam))
4014 then
4015 Apply_Discriminant_Check (A, F_Typ);
4016
4017 -- For view conversions of a discriminated object, apply
4018 -- check to object itself, the conversion alreay has the
4019 -- proper type.
4020
4021 if Nkind (A) = N_Type_Conversion
4022 and then Is_Constrained (Etype (Expression (A)))
4023 then
4024 Apply_Discriminant_Check (Expression (A), F_Typ);
4025 end if;
4026
4027 elsif Is_Access_Type (F_Typ)
4028 and then Is_Array_Type (Designated_Type (F_Typ))
4029 and then Is_Constrained (Designated_Type (F_Typ))
4030 then
4031 Apply_Length_Check (A, F_Typ);
4032
4033 elsif Is_Access_Type (F_Typ)
4034 and then Has_Discriminants (Designated_Type (F_Typ))
4035 and then Is_Constrained (Designated_Type (F_Typ))
4036 then
4037 Apply_Discriminant_Check (A, F_Typ);
4038
4039 else
4040 Apply_Range_Check (A, F_Typ);
4041 end if;
4042
4043 -- Ada 2005 (AI-231): Note that the controlling parameter case
4044 -- already existed in Ada 95, which is partially checked
4045 -- elsewhere (see Checks), and we don't want the warning
4046 -- message to differ.
4047
4048 if Is_Access_Type (F_Typ)
4049 and then Can_Never_Be_Null (F_Typ)
4050 and then Known_Null (A)
4051 then
4052 if Is_Controlling_Formal (F) then
4053 Apply_Compile_Time_Constraint_Error
4054 (N => A,
4055 Msg => "null value not allowed here??",
4056 Reason => CE_Access_Check_Failed);
4057
4058 elsif Ada_Version >= Ada_2005 then
4059 Apply_Compile_Time_Constraint_Error
4060 (N => A,
4061 Msg => "(Ada 2005) null not allowed in "
4062 & "null-excluding formal??",
4063 Reason => CE_Null_Not_Allowed);
4064 end if;
4065 end if;
4066 end if;
4067
4068 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
4069 if Nkind (A) = N_Type_Conversion then
4070 if Is_Scalar_Type (A_Typ) then
4071 Apply_Scalar_Range_Check
4072 (Expression (A), Etype (Expression (A)), A_Typ);
4073 else
4074 Apply_Range_Check
4075 (Expression (A), Etype (Expression (A)), A_Typ);
4076 end if;
4077
4078 else
4079 if Is_Scalar_Type (F_Typ) then
4080 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4081 elsif Is_Array_Type (F_Typ)
4082 and then Ekind (F) = E_Out_Parameter
4083 then
4084 Apply_Length_Check (A, F_Typ);
4085 else
4086 Apply_Range_Check (A, A_Typ, F_Typ);
4087 end if;
4088 end if;
4089 end if;
4090
4091 -- An actual associated with an access parameter is implicitly
4092 -- converted to the anonymous access type of the formal and must
4093 -- satisfy the legality checks for access conversions.
4094
4095 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4096 if not Valid_Conversion (A, F_Typ, A) then
4097 Error_Msg_N
4098 ("invalid implicit conversion for access parameter", A);
4099 end if;
4100
4101 -- If the actual is an access selected component of a variable,
4102 -- the call may modify its designated object. It is reasonable
4103 -- to treat this as a potential modification of the enclosing
4104 -- record, to prevent spurious warnings that it should be
4105 -- declared as a constant, because intuitively programmers
4106 -- regard the designated subcomponent as part of the record.
4107
4108 if Nkind (A) = N_Selected_Component
4109 and then Is_Entity_Name (Prefix (A))
4110 and then not Is_Constant_Object (Entity (Prefix (A)))
4111 then
4112 Note_Possible_Modification (A, Sure => False);
4113 end if;
4114 end if;
4115
4116 -- Check bad case of atomic/volatile argument (RM C.6(12))
4117
4118 if Is_By_Reference_Type (Etype (F))
4119 and then Comes_From_Source (N)
4120 then
4121 if Is_Atomic_Object (A)
4122 and then not Is_Atomic (Etype (F))
4123 then
4124 Error_Msg_NE
4125 ("cannot pass atomic argument to non-atomic formal&",
4126 A, F);
4127
4128 elsif Is_Volatile_Object (A)
4129 and then not Is_Volatile (Etype (F))
4130 then
4131 Error_Msg_NE
4132 ("cannot pass volatile argument to non-volatile formal&",
4133 A, F);
4134 end if;
4135 end if;
4136
4137 -- Check that subprograms don't have improper controlling
4138 -- arguments (RM 3.9.2 (9)).
4139
4140 -- A primitive operation may have an access parameter of an
4141 -- incomplete tagged type, but a dispatching call is illegal
4142 -- if the type is still incomplete.
4143
4144 if Is_Controlling_Formal (F) then
4145 Set_Is_Controlling_Actual (A);
4146
4147 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4148 declare
4149 Desig : constant Entity_Id := Designated_Type (Etype (F));
4150 begin
4151 if Ekind (Desig) = E_Incomplete_Type
4152 and then No (Full_View (Desig))
4153 and then No (Non_Limited_View (Desig))
4154 then
4155 Error_Msg_NE
4156 ("premature use of incomplete type& "
4157 & "in dispatching call", A, Desig);
4158 end if;
4159 end;
4160 end if;
4161
4162 elsif Nkind (A) = N_Explicit_Dereference then
4163 Validate_Remote_Access_To_Class_Wide_Type (A);
4164 end if;
4165
4166 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
4167 and then not Is_Class_Wide_Type (F_Typ)
4168 and then not Is_Controlling_Formal (F)
4169 then
4170 Error_Msg_N ("class-wide argument not allowed here!", A);
4171
4172 if Is_Subprogram (Nam)
4173 and then Comes_From_Source (Nam)
4174 then
4175 Error_Msg_Node_2 := F_Typ;
4176 Error_Msg_NE
4177 ("& is not a dispatching operation of &!", A, Nam);
4178 end if;
4179
4180 -- Apply the checks described in 3.10.2(27): if the context is a
4181 -- specific access-to-object, the actual cannot be class-wide.
4182 -- Use base type to exclude access_to_subprogram cases.
4183
4184 elsif Is_Access_Type (A_Typ)
4185 and then Is_Access_Type (F_Typ)
4186 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
4187 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
4188 or else (Nkind (A) = N_Attribute_Reference
4189 and then
4190 Is_Class_Wide_Type (Etype (Prefix (A)))))
4191 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
4192 and then not Is_Controlling_Formal (F)
4193
4194 -- Disable these checks for call to imported C++ subprograms
4195
4196 and then not
4197 (Is_Entity_Name (Name (N))
4198 and then Is_Imported (Entity (Name (N)))
4199 and then Convention (Entity (Name (N))) = Convention_CPP)
4200 then
4201 Error_Msg_N
4202 ("access to class-wide argument not allowed here!", A);
4203
4204 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4205 Error_Msg_Node_2 := Designated_Type (F_Typ);
4206 Error_Msg_NE
4207 ("& is not a dispatching operation of &!", A, Nam);
4208 end if;
4209 end if;
4210
4211 Eval_Actual (A);
4212
4213 -- If it is a named association, treat the selector_name as a
4214 -- proper identifier, and mark the corresponding entity.
4215
4216 if Nkind (Parent (A)) = N_Parameter_Association
4217
4218 -- Ignore reference in SPARK mode, as it refers to an entity not
4219 -- in scope at the point of reference, so the reference should
4220 -- be ignored for computing effects of subprograms.
4221
4222 and then not GNATprove_Mode
4223 then
4224 Set_Entity (Selector_Name (Parent (A)), F);
4225 Generate_Reference (F, Selector_Name (Parent (A)));
4226 Set_Etype (Selector_Name (Parent (A)), F_Typ);
4227 Generate_Reference (F_Typ, N, ' ');
4228 end if;
4229
4230 Prev := A;
4231
4232 if Ekind (F) /= E_Out_Parameter then
4233 Check_Unset_Reference (A);
4234 end if;
4235
4236 -- The following checks are only relevant when SPARK_Mode is on as
4237 -- they are not standard Ada legality rule.
4238
4239 if SPARK_Mode = On
4240 and then Is_SPARK_Volatile_Object (A)
4241 then
4242 -- A volatile object may act as an actual parameter when the
4243 -- corresponding formal is of a non-scalar volatile type.
4244
4245 if Is_Volatile (Etype (F))
4246 and then not Is_Scalar_Type (Etype (F))
4247 then
4248 null;
4249
4250 -- A volatile object may act as an actual parameter in a call
4251 -- to an instance of Unchecked_Conversion.
4252
4253 elsif Is_Unchecked_Conversion_Instance (Nam) then
4254 null;
4255
4256 else
4257 Error_Msg_N
4258 ("volatile object cannot act as actual in a call (SPARK "
4259 & "RM 7.1.3(12))", A);
4260 end if;
4261
4262 -- Detect an external variable with an enabled property that
4263 -- does not match the mode of the corresponding formal in a
4264 -- procedure call.
4265
4266 -- why only procedure calls ???
4267
4268 if Ekind (Nam) = E_Procedure
4269 and then Is_Entity_Name (A)
4270 and then Present (Entity (A))
4271 and then Ekind (Entity (A)) = E_Variable
4272 then
4273 A_Id := Entity (A);
4274
4275 if Ekind (F) = E_In_Parameter then
4276 if Async_Readers_Enabled (A_Id) then
4277 Property_Error (A, A_Id, Name_Async_Readers);
4278 elsif Effective_Reads_Enabled (A_Id) then
4279 Property_Error (A, A_Id, Name_Effective_Reads);
4280 elsif Effective_Writes_Enabled (A_Id) then
4281 Property_Error (A, A_Id, Name_Effective_Writes);
4282 end if;
4283
4284 elsif Ekind (F) = E_Out_Parameter
4285 and then Async_Writers_Enabled (A_Id)
4286 then
4287 Error_Msg_Name_1 := Name_Async_Writers;
4288 Error_Msg_NE
4289 ("external variable & with enabled property % cannot "
4290 & "appear as actual in procedure call "
4291 & "(SPARK RM 7.1.3(11))", A, A_Id);
4292 Error_Msg_N
4293 ("\\corresponding formal parameter has mode Out", A);
4294 end if;
4295 end if;
4296 end if;
4297
4298 Next_Actual (A);
4299
4300 -- Case where actual is not present
4301
4302 else
4303 Insert_Default;
4304 end if;
4305
4306 Next_Formal (F);
4307 end loop;
4308 end Resolve_Actuals;
4309
4310 -----------------------
4311 -- Resolve_Allocator --
4312 -----------------------
4313
4314 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
4315 Desig_T : constant Entity_Id := Designated_Type (Typ);
4316 E : constant Node_Id := Expression (N);
4317 Subtyp : Entity_Id;
4318 Discrim : Entity_Id;
4319 Constr : Node_Id;
4320 Aggr : Node_Id;
4321 Assoc : Node_Id := Empty;
4322 Disc_Exp : Node_Id;
4323
4324 procedure Check_Allocator_Discrim_Accessibility
4325 (Disc_Exp : Node_Id;
4326 Alloc_Typ : Entity_Id);
4327 -- Check that accessibility level associated with an access discriminant
4328 -- initialized in an allocator by the expression Disc_Exp is not deeper
4329 -- than the level of the allocator type Alloc_Typ. An error message is
4330 -- issued if this condition is violated. Specialized checks are done for
4331 -- the cases of a constraint expression which is an access attribute or
4332 -- an access discriminant.
4333
4334 function In_Dispatching_Context return Boolean;
4335 -- If the allocator is an actual in a call, it is allowed to be class-
4336 -- wide when the context is not because it is a controlling actual.
4337
4338 -------------------------------------------
4339 -- Check_Allocator_Discrim_Accessibility --
4340 -------------------------------------------
4341
4342 procedure Check_Allocator_Discrim_Accessibility
4343 (Disc_Exp : Node_Id;
4344 Alloc_Typ : Entity_Id)
4345 is
4346 begin
4347 if Type_Access_Level (Etype (Disc_Exp)) >
4348 Deepest_Type_Access_Level (Alloc_Typ)
4349 then
4350 Error_Msg_N
4351 ("operand type has deeper level than allocator type", Disc_Exp);
4352
4353 -- When the expression is an Access attribute the level of the prefix
4354 -- object must not be deeper than that of the allocator's type.
4355
4356 elsif Nkind (Disc_Exp) = N_Attribute_Reference
4357 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
4358 Attribute_Access
4359 and then Object_Access_Level (Prefix (Disc_Exp)) >
4360 Deepest_Type_Access_Level (Alloc_Typ)
4361 then
4362 Error_Msg_N
4363 ("prefix of attribute has deeper level than allocator type",
4364 Disc_Exp);
4365
4366 -- When the expression is an access discriminant the check is against
4367 -- the level of the prefix object.
4368
4369 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
4370 and then Nkind (Disc_Exp) = N_Selected_Component
4371 and then Object_Access_Level (Prefix (Disc_Exp)) >
4372 Deepest_Type_Access_Level (Alloc_Typ)
4373 then
4374 Error_Msg_N
4375 ("access discriminant has deeper level than allocator type",
4376 Disc_Exp);
4377
4378 -- All other cases are legal
4379
4380 else
4381 null;
4382 end if;
4383 end Check_Allocator_Discrim_Accessibility;
4384
4385 ----------------------------
4386 -- In_Dispatching_Context --
4387 ----------------------------
4388
4389 function In_Dispatching_Context return Boolean is
4390 Par : constant Node_Id := Parent (N);
4391
4392 begin
4393 return Nkind (Par) in N_Subprogram_Call
4394 and then Is_Entity_Name (Name (Par))
4395 and then Is_Dispatching_Operation (Entity (Name (Par)));
4396 end In_Dispatching_Context;
4397
4398 -- Start of processing for Resolve_Allocator
4399
4400 begin
4401 -- Replace general access with specific type
4402
4403 if Ekind (Etype (N)) = E_Allocator_Type then
4404 Set_Etype (N, Base_Type (Typ));
4405 end if;
4406
4407 if Is_Abstract_Type (Typ) then
4408 Error_Msg_N ("type of allocator cannot be abstract", N);
4409 end if;
4410
4411 -- For qualified expression, resolve the expression using the given
4412 -- subtype (nothing to do for type mark, subtype indication)
4413
4414 if Nkind (E) = N_Qualified_Expression then
4415 if Is_Class_Wide_Type (Etype (E))
4416 and then not Is_Class_Wide_Type (Desig_T)
4417 and then not In_Dispatching_Context
4418 then
4419 Error_Msg_N
4420 ("class-wide allocator not allowed for this access type", N);
4421 end if;
4422
4423 Resolve (Expression (E), Etype (E));
4424 Check_Unset_Reference (Expression (E));
4425
4426 -- A qualified expression requires an exact match of the type.
4427 -- Class-wide matching is not allowed.
4428
4429 if (Is_Class_Wide_Type (Etype (Expression (E)))
4430 or else Is_Class_Wide_Type (Etype (E)))
4431 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
4432 then
4433 Wrong_Type (Expression (E), Etype (E));
4434 end if;
4435
4436 -- Calls to build-in-place functions are not currently supported in
4437 -- allocators for access types associated with a simple storage pool.
4438 -- Supporting such allocators may require passing additional implicit
4439 -- parameters to build-in-place functions (or a significant revision
4440 -- of the current b-i-p implementation to unify the handling for
4441 -- multiple kinds of storage pools). ???
4442
4443 if Is_Limited_View (Desig_T)
4444 and then Nkind (Expression (E)) = N_Function_Call
4445 then
4446 declare
4447 Pool : constant Entity_Id :=
4448 Associated_Storage_Pool (Root_Type (Typ));
4449 begin
4450 if Present (Pool)
4451 and then
4452 Present (Get_Rep_Pragma
4453 (Etype (Pool), Name_Simple_Storage_Pool_Type))
4454 then
4455 Error_Msg_N
4456 ("limited function calls not yet supported in simple "
4457 & "storage pool allocators", Expression (E));
4458 end if;
4459 end;
4460 end if;
4461
4462 -- A special accessibility check is needed for allocators that
4463 -- constrain access discriminants. The level of the type of the
4464 -- expression used to constrain an access discriminant cannot be
4465 -- deeper than the type of the allocator (in contrast to access
4466 -- parameters, where the level of the actual can be arbitrary).
4467
4468 -- We can't use Valid_Conversion to perform this check because in
4469 -- general the type of the allocator is unrelated to the type of
4470 -- the access discriminant.
4471
4472 if Ekind (Typ) /= E_Anonymous_Access_Type
4473 or else Is_Local_Anonymous_Access (Typ)
4474 then
4475 Subtyp := Entity (Subtype_Mark (E));
4476
4477 Aggr := Original_Node (Expression (E));
4478
4479 if Has_Discriminants (Subtyp)
4480 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
4481 then
4482 Discrim := First_Discriminant (Base_Type (Subtyp));
4483
4484 -- Get the first component expression of the aggregate
4485
4486 if Present (Expressions (Aggr)) then
4487 Disc_Exp := First (Expressions (Aggr));
4488
4489 elsif Present (Component_Associations (Aggr)) then
4490 Assoc := First (Component_Associations (Aggr));
4491
4492 if Present (Assoc) then
4493 Disc_Exp := Expression (Assoc);
4494 else
4495 Disc_Exp := Empty;
4496 end if;
4497
4498 else
4499 Disc_Exp := Empty;
4500 end if;
4501
4502 while Present (Discrim) and then Present (Disc_Exp) loop
4503 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4504 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4505 end if;
4506
4507 Next_Discriminant (Discrim);
4508
4509 if Present (Discrim) then
4510 if Present (Assoc) then
4511 Next (Assoc);
4512 Disc_Exp := Expression (Assoc);
4513
4514 elsif Present (Next (Disc_Exp)) then
4515 Next (Disc_Exp);
4516
4517 else
4518 Assoc := First (Component_Associations (Aggr));
4519
4520 if Present (Assoc) then
4521 Disc_Exp := Expression (Assoc);
4522 else
4523 Disc_Exp := Empty;
4524 end if;
4525 end if;
4526 end if;
4527 end loop;
4528 end if;
4529 end if;
4530
4531 -- For a subtype mark or subtype indication, freeze the subtype
4532
4533 else
4534 Freeze_Expression (E);
4535
4536 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
4537 Error_Msg_N
4538 ("initialization required for access-to-constant allocator", N);
4539 end if;
4540
4541 -- A special accessibility check is needed for allocators that
4542 -- constrain access discriminants. The level of the type of the
4543 -- expression used to constrain an access discriminant cannot be
4544 -- deeper than the type of the allocator (in contrast to access
4545 -- parameters, where the level of the actual can be arbitrary).
4546 -- We can't use Valid_Conversion to perform this check because
4547 -- in general the type of the allocator is unrelated to the type
4548 -- of the access discriminant.
4549
4550 if Nkind (Original_Node (E)) = N_Subtype_Indication
4551 and then (Ekind (Typ) /= E_Anonymous_Access_Type
4552 or else Is_Local_Anonymous_Access (Typ))
4553 then
4554 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
4555
4556 if Has_Discriminants (Subtyp) then
4557 Discrim := First_Discriminant (Base_Type (Subtyp));
4558 Constr := First (Constraints (Constraint (Original_Node (E))));
4559 while Present (Discrim) and then Present (Constr) loop
4560 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4561 if Nkind (Constr) = N_Discriminant_Association then
4562 Disc_Exp := Original_Node (Expression (Constr));
4563 else
4564 Disc_Exp := Original_Node (Constr);
4565 end if;
4566
4567 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4568 end if;
4569
4570 Next_Discriminant (Discrim);
4571 Next (Constr);
4572 end loop;
4573 end if;
4574 end if;
4575 end if;
4576
4577 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4578 -- check that the level of the type of the created object is not deeper
4579 -- than the level of the allocator's access type, since extensions can
4580 -- now occur at deeper levels than their ancestor types. This is a
4581 -- static accessibility level check; a run-time check is also needed in
4582 -- the case of an initialized allocator with a class-wide argument (see
4583 -- Expand_Allocator_Expression).
4584
4585 if Ada_Version >= Ada_2005
4586 and then Is_Class_Wide_Type (Desig_T)
4587 then
4588 declare
4589 Exp_Typ : Entity_Id;
4590
4591 begin
4592 if Nkind (E) = N_Qualified_Expression then
4593 Exp_Typ := Etype (E);
4594 elsif Nkind (E) = N_Subtype_Indication then
4595 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4596 else
4597 Exp_Typ := Entity (E);
4598 end if;
4599
4600 if Type_Access_Level (Exp_Typ) >
4601 Deepest_Type_Access_Level (Typ)
4602 then
4603 if In_Instance_Body then
4604 Error_Msg_Warn := SPARK_Mode /= On;
4605 Error_Msg_N
4606 ("type in allocator has deeper level than "
4607 & "designated class-wide type<<", E);
4608 Error_Msg_N ("\Program_Error [<<", E);
4609 Rewrite (N,
4610 Make_Raise_Program_Error (Sloc (N),
4611 Reason => PE_Accessibility_Check_Failed));
4612 Set_Etype (N, Typ);
4613
4614 -- Do not apply Ada 2005 accessibility checks on a class-wide
4615 -- allocator if the type given in the allocator is a formal
4616 -- type. A run-time check will be performed in the instance.
4617
4618 elsif not Is_Generic_Type (Exp_Typ) then
4619 Error_Msg_N ("type in allocator has deeper level than "
4620 & "designated class-wide type", E);
4621 end if;
4622 end if;
4623 end;
4624 end if;
4625
4626 -- Check for allocation from an empty storage pool
4627
4628 if No_Pool_Assigned (Typ) then
4629 Error_Msg_N ("allocation from empty storage pool!", N);
4630
4631 -- If the context is an unchecked conversion, as may happen within an
4632 -- inlined subprogram, the allocator is being resolved with its own
4633 -- anonymous type. In that case, if the target type has a specific
4634 -- storage pool, it must be inherited explicitly by the allocator type.
4635
4636 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4637 and then No (Associated_Storage_Pool (Typ))
4638 then
4639 Set_Associated_Storage_Pool
4640 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4641 end if;
4642
4643 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
4644 Check_Restriction (No_Anonymous_Allocators, N);
4645 end if;
4646
4647 -- Check that an allocator with task parts isn't for a nested access
4648 -- type when restriction No_Task_Hierarchy applies.
4649
4650 if not Is_Library_Level_Entity (Base_Type (Typ))
4651 and then Has_Task (Base_Type (Desig_T))
4652 then
4653 Check_Restriction (No_Task_Hierarchy, N);
4654 end if;
4655
4656 -- An illegal allocator may be rewritten as a raise Program_Error
4657 -- statement.
4658
4659 if Nkind (N) = N_Allocator then
4660
4661 -- An anonymous access discriminant is the definition of a
4662 -- coextension.
4663
4664 if Ekind (Typ) = E_Anonymous_Access_Type
4665 and then Nkind (Associated_Node_For_Itype (Typ)) =
4666 N_Discriminant_Specification
4667 then
4668 declare
4669 Discr : constant Entity_Id :=
4670 Defining_Identifier (Associated_Node_For_Itype (Typ));
4671
4672 begin
4673 Check_Restriction (No_Coextensions, N);
4674
4675 -- Ada 2012 AI05-0052: If the designated type of the allocator
4676 -- is limited, then the allocator shall not be used to define
4677 -- the value of an access discriminant unless the discriminated
4678 -- type is immutably limited.
4679
4680 if Ada_Version >= Ada_2012
4681 and then Is_Limited_Type (Desig_T)
4682 and then not Is_Limited_View (Scope (Discr))
4683 then
4684 Error_Msg_N
4685 ("only immutably limited types can have anonymous "
4686 & "access discriminants designating a limited type", N);
4687 end if;
4688 end;
4689
4690 -- Avoid marking an allocator as a dynamic coextension if it is
4691 -- within a static construct.
4692
4693 if not Is_Static_Coextension (N) then
4694 Set_Is_Dynamic_Coextension (N);
4695 end if;
4696
4697 -- Cleanup for potential static coextensions
4698
4699 else
4700 Set_Is_Dynamic_Coextension (N, False);
4701 Set_Is_Static_Coextension (N, False);
4702 end if;
4703 end if;
4704
4705 -- Report a simple error: if the designated object is a local task,
4706 -- its body has not been seen yet, and its activation will fail an
4707 -- elaboration check.
4708
4709 if Is_Task_Type (Desig_T)
4710 and then Scope (Base_Type (Desig_T)) = Current_Scope
4711 and then Is_Compilation_Unit (Current_Scope)
4712 and then Ekind (Current_Scope) = E_Package
4713 and then not In_Package_Body (Current_Scope)
4714 then
4715 Error_Msg_Warn := SPARK_Mode /= On;
4716 Error_Msg_N ("cannot activate task before body seen<<", N);
4717 Error_Msg_N ("\Program_Error [<<", N);
4718 end if;
4719
4720 -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a
4721 -- type with a task component on a subpool. This action must raise
4722 -- Program_Error at runtime.
4723
4724 if Ada_Version >= Ada_2012
4725 and then Nkind (N) = N_Allocator
4726 and then Present (Subpool_Handle_Name (N))
4727 and then Has_Task (Desig_T)
4728 then
4729 Error_Msg_Warn := SPARK_Mode /= On;
4730 Error_Msg_N ("cannot allocate task on subpool<<", N);
4731 Error_Msg_N ("\Program_Error [<<", N);
4732
4733 Rewrite (N,
4734 Make_Raise_Program_Error (Sloc (N),
4735 Reason => PE_Explicit_Raise));
4736 Set_Etype (N, Typ);
4737 end if;
4738 end Resolve_Allocator;
4739
4740 ---------------------------
4741 -- Resolve_Arithmetic_Op --
4742 ---------------------------
4743
4744 -- Used for resolving all arithmetic operators except exponentiation
4745
4746 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
4747 L : constant Node_Id := Left_Opnd (N);
4748 R : constant Node_Id := Right_Opnd (N);
4749 TL : constant Entity_Id := Base_Type (Etype (L));
4750 TR : constant Entity_Id := Base_Type (Etype (R));
4751 T : Entity_Id;
4752 Rop : Node_Id;
4753
4754 B_Typ : constant Entity_Id := Base_Type (Typ);
4755 -- We do the resolution using the base type, because intermediate values
4756 -- in expressions always are of the base type, not a subtype of it.
4757
4758 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4759 -- Returns True if N is in a context that expects "any real type"
4760
4761 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4762 -- Return True iff given type is Integer or universal real/integer
4763
4764 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4765 -- Choose type of integer literal in fixed-point operation to conform
4766 -- to available fixed-point type. T is the type of the other operand,
4767 -- which is needed to determine the expected type of N.
4768
4769 procedure Set_Operand_Type (N : Node_Id);
4770 -- Set operand type to T if universal
4771
4772 -------------------------------
4773 -- Expected_Type_Is_Any_Real --
4774 -------------------------------
4775
4776 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4777 begin
4778 -- N is the expression after "delta" in a fixed_point_definition;
4779 -- see RM-3.5.9(6):
4780
4781 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4782 N_Decimal_Fixed_Point_Definition,
4783
4784 -- N is one of the bounds in a real_range_specification;
4785 -- see RM-3.5.7(5):
4786
4787 N_Real_Range_Specification,
4788
4789 -- N is the expression of a delta_constraint;
4790 -- see RM-J.3(3):
4791
4792 N_Delta_Constraint);
4793 end Expected_Type_Is_Any_Real;
4794
4795 -----------------------------
4796 -- Is_Integer_Or_Universal --
4797 -----------------------------
4798
4799 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4800 T : Entity_Id;
4801 Index : Interp_Index;
4802 It : Interp;
4803
4804 begin
4805 if not Is_Overloaded (N) then
4806 T := Etype (N);
4807 return Base_Type (T) = Base_Type (Standard_Integer)
4808 or else T = Universal_Integer
4809 or else T = Universal_Real;
4810 else
4811 Get_First_Interp (N, Index, It);
4812 while Present (It.Typ) loop
4813 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4814 or else It.Typ = Universal_Integer
4815 or else It.Typ = Universal_Real
4816 then
4817 return True;
4818 end if;
4819
4820 Get_Next_Interp (Index, It);
4821 end loop;
4822 end if;
4823
4824 return False;
4825 end Is_Integer_Or_Universal;
4826
4827 ----------------------------
4828 -- Set_Mixed_Mode_Operand --
4829 ----------------------------
4830
4831 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4832 Index : Interp_Index;
4833 It : Interp;
4834
4835 begin
4836 if Universal_Interpretation (N) = Universal_Integer then
4837
4838 -- A universal integer literal is resolved as standard integer
4839 -- except in the case of a fixed-point result, where we leave it
4840 -- as universal (to be handled by Exp_Fixd later on)
4841
4842 if Is_Fixed_Point_Type (T) then
4843 Resolve (N, Universal_Integer);
4844 else
4845 Resolve (N, Standard_Integer);
4846 end if;
4847
4848 elsif Universal_Interpretation (N) = Universal_Real
4849 and then (T = Base_Type (Standard_Integer)
4850 or else T = Universal_Integer
4851 or else T = Universal_Real)
4852 then
4853 -- A universal real can appear in a fixed-type context. We resolve
4854 -- the literal with that context, even though this might raise an
4855 -- exception prematurely (the other operand may be zero).
4856
4857 Resolve (N, B_Typ);
4858
4859 elsif Etype (N) = Base_Type (Standard_Integer)
4860 and then T = Universal_Real
4861 and then Is_Overloaded (N)
4862 then
4863 -- Integer arg in mixed-mode operation. Resolve with universal
4864 -- type, in case preference rule must be applied.
4865
4866 Resolve (N, Universal_Integer);
4867
4868 elsif Etype (N) = T
4869 and then B_Typ /= Universal_Fixed
4870 then
4871 -- Not a mixed-mode operation, resolve with context
4872
4873 Resolve (N, B_Typ);
4874
4875 elsif Etype (N) = Any_Fixed then
4876
4877 -- N may itself be a mixed-mode operation, so use context type
4878
4879 Resolve (N, B_Typ);
4880
4881 elsif Is_Fixed_Point_Type (T)
4882 and then B_Typ = Universal_Fixed
4883 and then Is_Overloaded (N)
4884 then
4885 -- Must be (fixed * fixed) operation, operand must have one
4886 -- compatible interpretation.
4887
4888 Resolve (N, Any_Fixed);
4889
4890 elsif Is_Fixed_Point_Type (B_Typ)
4891 and then (T = Universal_Real or else Is_Fixed_Point_Type (T))
4892 and then Is_Overloaded (N)
4893 then
4894 -- C * F(X) in a fixed context, where C is a real literal or a
4895 -- fixed-point expression. F must have either a fixed type
4896 -- interpretation or an integer interpretation, but not both.
4897
4898 Get_First_Interp (N, Index, It);
4899 while Present (It.Typ) loop
4900 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
4901 if Analyzed (N) then
4902 Error_Msg_N ("ambiguous operand in fixed operation", N);
4903 else
4904 Resolve (N, Standard_Integer);
4905 end if;
4906
4907 elsif Is_Fixed_Point_Type (It.Typ) then
4908 if Analyzed (N) then
4909 Error_Msg_N ("ambiguous operand in fixed operation", N);
4910 else
4911 Resolve (N, It.Typ);
4912 end if;
4913 end if;
4914
4915 Get_Next_Interp (Index, It);
4916 end loop;
4917
4918 -- Reanalyze the literal with the fixed type of the context. If
4919 -- context is Universal_Fixed, we are within a conversion, leave
4920 -- the literal as a universal real because there is no usable
4921 -- fixed type, and the target of the conversion plays no role in
4922 -- the resolution.
4923
4924 declare
4925 Op2 : Node_Id;
4926 T2 : Entity_Id;
4927
4928 begin
4929 if N = L then
4930 Op2 := R;
4931 else
4932 Op2 := L;
4933 end if;
4934
4935 if B_Typ = Universal_Fixed
4936 and then Nkind (Op2) = N_Real_Literal
4937 then
4938 T2 := Universal_Real;
4939 else
4940 T2 := B_Typ;
4941 end if;
4942
4943 Set_Analyzed (Op2, False);
4944 Resolve (Op2, T2);
4945 end;
4946
4947 else
4948 Resolve (N);
4949 end if;
4950 end Set_Mixed_Mode_Operand;
4951
4952 ----------------------
4953 -- Set_Operand_Type --
4954 ----------------------
4955
4956 procedure Set_Operand_Type (N : Node_Id) is
4957 begin
4958 if Etype (N) = Universal_Integer
4959 or else Etype (N) = Universal_Real
4960 then
4961 Set_Etype (N, T);
4962 end if;
4963 end Set_Operand_Type;
4964
4965 -- Start of processing for Resolve_Arithmetic_Op
4966
4967 begin
4968 if Comes_From_Source (N)
4969 and then Ekind (Entity (N)) = E_Function
4970 and then Is_Imported (Entity (N))
4971 and then Is_Intrinsic_Subprogram (Entity (N))
4972 then
4973 Resolve_Intrinsic_Operator (N, Typ);
4974 return;
4975
4976 -- Special-case for mixed-mode universal expressions or fixed point type
4977 -- operation: each argument is resolved separately. The same treatment
4978 -- is required if one of the operands of a fixed point operation is
4979 -- universal real, since in this case we don't do a conversion to a
4980 -- specific fixed-point type (instead the expander handles the case).
4981
4982 -- Set the type of the node to its universal interpretation because
4983 -- legality checks on an exponentiation operand need the context.
4984
4985 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
4986 and then Present (Universal_Interpretation (L))
4987 and then Present (Universal_Interpretation (R))
4988 then
4989 Set_Etype (N, B_Typ);
4990 Resolve (L, Universal_Interpretation (L));
4991 Resolve (R, Universal_Interpretation (R));
4992
4993 elsif (B_Typ = Universal_Real
4994 or else Etype (N) = Universal_Fixed
4995 or else (Etype (N) = Any_Fixed
4996 and then Is_Fixed_Point_Type (B_Typ))
4997 or else (Is_Fixed_Point_Type (B_Typ)
4998 and then (Is_Integer_Or_Universal (L)
4999 or else
5000 Is_Integer_Or_Universal (R))))
5001 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
5002 then
5003 if TL = Universal_Integer or else TR = Universal_Integer then
5004 Check_For_Visible_Operator (N, B_Typ);
5005 end if;
5006
5007 -- If context is a fixed type and one operand is integer, the other
5008 -- is resolved with the type of the context.
5009
5010 if Is_Fixed_Point_Type (B_Typ)
5011 and then (Base_Type (TL) = Base_Type (Standard_Integer)
5012 or else TL = Universal_Integer)
5013 then
5014 Resolve (R, B_Typ);
5015 Resolve (L, TL);
5016
5017 elsif Is_Fixed_Point_Type (B_Typ)
5018 and then (Base_Type (TR) = Base_Type (Standard_Integer)
5019 or else TR = Universal_Integer)
5020 then
5021 Resolve (L, B_Typ);
5022 Resolve (R, TR);
5023
5024 else
5025 Set_Mixed_Mode_Operand (L, TR);
5026 Set_Mixed_Mode_Operand (R, TL);
5027 end if;
5028
5029 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
5030 -- multiplying operators from being used when the expected type is
5031 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
5032 -- some cases where the expected type is actually Any_Real;
5033 -- Expected_Type_Is_Any_Real takes care of that case.
5034
5035 if Etype (N) = Universal_Fixed
5036 or else Etype (N) = Any_Fixed
5037 then
5038 if B_Typ = Universal_Fixed
5039 and then not Expected_Type_Is_Any_Real (N)
5040 and then not Nkind_In (Parent (N), N_Type_Conversion,
5041 N_Unchecked_Type_Conversion)
5042 then
5043 Error_Msg_N ("type cannot be determined from context!", N);
5044 Error_Msg_N ("\explicit conversion to result type required", N);
5045
5046 Set_Etype (L, Any_Type);
5047 Set_Etype (R, Any_Type);
5048
5049 else
5050 if Ada_Version = Ada_83
5051 and then Etype (N) = Universal_Fixed
5052 and then not
5053 Nkind_In (Parent (N), N_Type_Conversion,
5054 N_Unchecked_Type_Conversion)
5055 then
5056 Error_Msg_N
5057 ("(Ada 83) fixed-point operation "
5058 & "needs explicit conversion", N);
5059 end if;
5060
5061 -- The expected type is "any real type" in contexts like
5062
5063 -- type T is delta <universal_fixed-expression> ...
5064
5065 -- in which case we need to set the type to Universal_Real
5066 -- so that static expression evaluation will work properly.
5067
5068 if Expected_Type_Is_Any_Real (N) then
5069 Set_Etype (N, Universal_Real);
5070 else
5071 Set_Etype (N, B_Typ);
5072 end if;
5073 end if;
5074
5075 elsif Is_Fixed_Point_Type (B_Typ)
5076 and then (Is_Integer_Or_Universal (L)
5077 or else Nkind (L) = N_Real_Literal
5078 or else Nkind (R) = N_Real_Literal
5079 or else Is_Integer_Or_Universal (R))
5080 then
5081 Set_Etype (N, B_Typ);
5082
5083 elsif Etype (N) = Any_Fixed then
5084
5085 -- If no previous errors, this is only possible if one operand is
5086 -- overloaded and the context is universal. Resolve as such.
5087
5088 Set_Etype (N, B_Typ);
5089 end if;
5090
5091 else
5092 if (TL = Universal_Integer or else TL = Universal_Real)
5093 and then
5094 (TR = Universal_Integer or else TR = Universal_Real)
5095 then
5096 Check_For_Visible_Operator (N, B_Typ);
5097 end if;
5098
5099 -- If the context is Universal_Fixed and the operands are also
5100 -- universal fixed, this is an error, unless there is only one
5101 -- applicable fixed_point type (usually Duration).
5102
5103 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
5104 T := Unique_Fixed_Point_Type (N);
5105
5106 if T = Any_Type then
5107 Set_Etype (N, T);
5108 return;
5109 else
5110 Resolve (L, T);
5111 Resolve (R, T);
5112 end if;
5113
5114 else
5115 Resolve (L, B_Typ);
5116 Resolve (R, B_Typ);
5117 end if;
5118
5119 -- If one of the arguments was resolved to a non-universal type.
5120 -- label the result of the operation itself with the same type.
5121 -- Do the same for the universal argument, if any.
5122
5123 T := Intersect_Types (L, R);
5124 Set_Etype (N, Base_Type (T));
5125 Set_Operand_Type (L);
5126 Set_Operand_Type (R);
5127 end if;
5128
5129 Generate_Operator_Reference (N, Typ);
5130 Analyze_Dimension (N);
5131 Eval_Arithmetic_Op (N);
5132
5133 -- In SPARK, a multiplication or division with operands of fixed point
5134 -- types shall be qualified or explicitly converted to identify the
5135 -- result type.
5136
5137 if (Is_Fixed_Point_Type (Etype (L))
5138 or else Is_Fixed_Point_Type (Etype (R)))
5139 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
5140 and then
5141 not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion)
5142 then
5143 Check_SPARK_Restriction
5144 ("operation should be qualified or explicitly converted", N);
5145 end if;
5146
5147 -- Set overflow and division checking bit
5148
5149 if Nkind (N) in N_Op then
5150 if not Overflow_Checks_Suppressed (Etype (N)) then
5151 Enable_Overflow_Check (N);
5152 end if;
5153
5154 -- Give warning if explicit division by zero
5155
5156 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
5157 and then not Division_Checks_Suppressed (Etype (N))
5158 then
5159 Rop := Right_Opnd (N);
5160
5161 if Compile_Time_Known_Value (Rop)
5162 and then ((Is_Integer_Type (Etype (Rop))
5163 and then Expr_Value (Rop) = Uint_0)
5164 or else
5165 (Is_Real_Type (Etype (Rop))
5166 and then Expr_Value_R (Rop) = Ureal_0))
5167 then
5168 -- Specialize the warning message according to the operation.
5169 -- The following warnings are for the case
5170
5171 case Nkind (N) is
5172 when N_Op_Divide =>
5173
5174 -- For division, we have two cases, for float division
5175 -- of an unconstrained float type, on a machine where
5176 -- Machine_Overflows is false, we don't get an exception
5177 -- at run-time, but rather an infinity or Nan. The Nan
5178 -- case is pretty obscure, so just warn about infinities.
5179
5180 if Is_Floating_Point_Type (Typ)
5181 and then not Is_Constrained (Typ)
5182 and then not Machine_Overflows_On_Target
5183 then
5184 Error_Msg_N
5185 ("float division by zero, may generate "
5186 & "'+'/'- infinity??", Right_Opnd (N));
5187
5188 -- For all other cases, we get a Constraint_Error
5189
5190 else
5191 Apply_Compile_Time_Constraint_Error
5192 (N, "division by zero??", CE_Divide_By_Zero,
5193 Loc => Sloc (Right_Opnd (N)));
5194 end if;
5195
5196 when N_Op_Rem =>
5197 Apply_Compile_Time_Constraint_Error
5198 (N, "rem with zero divisor??", CE_Divide_By_Zero,
5199 Loc => Sloc (Right_Opnd (N)));
5200
5201 when N_Op_Mod =>
5202 Apply_Compile_Time_Constraint_Error
5203 (N, "mod with zero divisor??", CE_Divide_By_Zero,
5204 Loc => Sloc (Right_Opnd (N)));
5205
5206 -- Division by zero can only happen with division, rem,
5207 -- and mod operations.
5208
5209 when others =>
5210 raise Program_Error;
5211 end case;
5212
5213 -- Otherwise just set the flag to check at run time
5214
5215 else
5216 Activate_Division_Check (N);
5217 end if;
5218 end if;
5219
5220 -- If Restriction No_Implicit_Conditionals is active, then it is
5221 -- violated if either operand can be negative for mod, or for rem
5222 -- if both operands can be negative.
5223
5224 if Restriction_Check_Required (No_Implicit_Conditionals)
5225 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
5226 then
5227 declare
5228 Lo : Uint;
5229 Hi : Uint;
5230 OK : Boolean;
5231
5232 LNeg : Boolean;
5233 RNeg : Boolean;
5234 -- Set if corresponding operand might be negative
5235
5236 begin
5237 Determine_Range
5238 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
5239 LNeg := (not OK) or else Lo < 0;
5240
5241 Determine_Range
5242 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
5243 RNeg := (not OK) or else Lo < 0;
5244
5245 -- Check if we will be generating conditionals. There are two
5246 -- cases where that can happen, first for REM, the only case
5247 -- is largest negative integer mod -1, where the division can
5248 -- overflow, but we still have to give the right result. The
5249 -- front end generates a test for this annoying case. Here we
5250 -- just test if both operands can be negative (that's what the
5251 -- expander does, so we match its logic here).
5252
5253 -- The second case is mod where either operand can be negative.
5254 -- In this case, the back end has to generate additional tests.
5255
5256 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
5257 or else
5258 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
5259 then
5260 Check_Restriction (No_Implicit_Conditionals, N);
5261 end if;
5262 end;
5263 end if;
5264 end if;
5265
5266 Check_Unset_Reference (L);
5267 Check_Unset_Reference (R);
5268 Check_Function_Writable_Actuals (N);
5269 end Resolve_Arithmetic_Op;
5270
5271 ------------------
5272 -- Resolve_Call --
5273 ------------------
5274
5275 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
5276 Loc : constant Source_Ptr := Sloc (N);
5277 Subp : constant Node_Id := Name (N);
5278 Nam : Entity_Id;
5279 I : Interp_Index;
5280 It : Interp;
5281 Norm_OK : Boolean;
5282 Scop : Entity_Id;
5283 Rtype : Entity_Id;
5284
5285 function Same_Or_Aliased_Subprograms
5286 (S : Entity_Id;
5287 E : Entity_Id) return Boolean;
5288 -- Returns True if the subprogram entity S is the same as E or else
5289 -- S is an alias of E.
5290
5291 ---------------------------------
5292 -- Same_Or_Aliased_Subprograms --
5293 ---------------------------------
5294
5295 function Same_Or_Aliased_Subprograms
5296 (S : Entity_Id;
5297 E : Entity_Id) return Boolean
5298 is
5299 Subp_Alias : constant Entity_Id := Alias (S);
5300 begin
5301 return S = E or else (Present (Subp_Alias) and then Subp_Alias = E);
5302 end Same_Or_Aliased_Subprograms;
5303
5304 -- Start of processing for Resolve_Call
5305
5306 begin
5307 -- The context imposes a unique interpretation with type Typ on a
5308 -- procedure or function call. Find the entity of the subprogram that
5309 -- yields the expected type, and propagate the corresponding formal
5310 -- constraints on the actuals. The caller has established that an
5311 -- interpretation exists, and emitted an error if not unique.
5312
5313 -- First deal with the case of a call to an access-to-subprogram,
5314 -- dereference made explicit in Analyze_Call.
5315
5316 if Ekind (Etype (Subp)) = E_Subprogram_Type then
5317 if not Is_Overloaded (Subp) then
5318 Nam := Etype (Subp);
5319
5320 else
5321 -- Find the interpretation whose type (a subprogram type) has a
5322 -- return type that is compatible with the context. Analysis of
5323 -- the node has established that one exists.
5324
5325 Nam := Empty;
5326
5327 Get_First_Interp (Subp, I, It);
5328 while Present (It.Typ) loop
5329 if Covers (Typ, Etype (It.Typ)) then
5330 Nam := It.Typ;
5331 exit;
5332 end if;
5333
5334 Get_Next_Interp (I, It);
5335 end loop;
5336
5337 if No (Nam) then
5338 raise Program_Error;
5339 end if;
5340 end if;
5341
5342 -- If the prefix is not an entity, then resolve it
5343
5344 if not Is_Entity_Name (Subp) then
5345 Resolve (Subp, Nam);
5346 end if;
5347
5348 -- For an indirect call, we always invalidate checks, since we do not
5349 -- know whether the subprogram is local or global. Yes we could do
5350 -- better here, e.g. by knowing that there are no local subprograms,
5351 -- but it does not seem worth the effort. Similarly, we kill all
5352 -- knowledge of current constant values.
5353
5354 Kill_Current_Values;
5355
5356 -- If this is a procedure call which is really an entry call, do
5357 -- the conversion of the procedure call to an entry call. Protected
5358 -- operations use the same circuitry because the name in the call
5359 -- can be an arbitrary expression with special resolution rules.
5360
5361 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
5362 or else (Is_Entity_Name (Subp)
5363 and then Ekind (Entity (Subp)) = E_Entry)
5364 then
5365 Resolve_Entry_Call (N, Typ);
5366 Check_Elab_Call (N);
5367
5368 -- Kill checks and constant values, as above for indirect case
5369 -- Who knows what happens when another task is activated?
5370
5371 Kill_Current_Values;
5372 return;
5373
5374 -- Normal subprogram call with name established in Resolve
5375
5376 elsif not (Is_Type (Entity (Subp))) then
5377 Nam := Entity (Subp);
5378 Set_Entity_With_Checks (Subp, Nam);
5379
5380 -- Otherwise we must have the case of an overloaded call
5381
5382 else
5383 pragma Assert (Is_Overloaded (Subp));
5384
5385 -- Initialize Nam to prevent warning (we know it will be assigned
5386 -- in the loop below, but the compiler does not know that).
5387
5388 Nam := Empty;
5389
5390 Get_First_Interp (Subp, I, It);
5391 while Present (It.Typ) loop
5392 if Covers (Typ, It.Typ) then
5393 Nam := It.Nam;
5394 Set_Entity_With_Checks (Subp, Nam);
5395 exit;
5396 end if;
5397
5398 Get_Next_Interp (I, It);
5399 end loop;
5400 end if;
5401
5402 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
5403 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
5404 and then Nkind (Subp) /= N_Explicit_Dereference
5405 and then Present (Parameter_Associations (N))
5406 then
5407 -- The prefix is a parameterless function call that returns an access
5408 -- to subprogram. If parameters are present in the current call, add
5409 -- add an explicit dereference. We use the base type here because
5410 -- within an instance these may be subtypes.
5411
5412 -- The dereference is added either in Analyze_Call or here. Should
5413 -- be consolidated ???
5414
5415 Set_Is_Overloaded (Subp, False);
5416 Set_Etype (Subp, Etype (Nam));
5417 Insert_Explicit_Dereference (Subp);
5418 Nam := Designated_Type (Etype (Nam));
5419 Resolve (Subp, Nam);
5420 end if;
5421
5422 -- Check that a call to Current_Task does not occur in an entry body
5423
5424 if Is_RTE (Nam, RE_Current_Task) then
5425 declare
5426 P : Node_Id;
5427
5428 begin
5429 P := N;
5430 loop
5431 P := Parent (P);
5432
5433 -- Exclude calls that occur within the default of a formal
5434 -- parameter of the entry, since those are evaluated outside
5435 -- of the body.
5436
5437 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
5438
5439 if Nkind (P) = N_Entry_Body
5440 or else (Nkind (P) = N_Subprogram_Body
5441 and then Is_Entry_Barrier_Function (P))
5442 then
5443 Rtype := Etype (N);
5444 Error_Msg_Warn := SPARK_Mode /= On;
5445 Error_Msg_NE
5446 ("& should not be used in entry body (RM C.7(17))<<",
5447 N, Nam);
5448 Error_Msg_NE ("\Program_Error [<<", N, Nam);
5449 Rewrite (N,
5450 Make_Raise_Program_Error (Loc,
5451 Reason => PE_Current_Task_In_Entry_Body));
5452 Set_Etype (N, Rtype);
5453 return;
5454 end if;
5455 end loop;
5456 end;
5457 end if;
5458
5459 -- Check that a procedure call does not occur in the context of the
5460 -- entry call statement of a conditional or timed entry call. Note that
5461 -- the case of a call to a subprogram renaming of an entry will also be
5462 -- rejected. The test for N not being an N_Entry_Call_Statement is
5463 -- defensive, covering the possibility that the processing of entry
5464 -- calls might reach this point due to later modifications of the code
5465 -- above.
5466
5467 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5468 and then Nkind (N) /= N_Entry_Call_Statement
5469 and then Entry_Call_Statement (Parent (N)) = N
5470 then
5471 if Ada_Version < Ada_2005 then
5472 Error_Msg_N ("entry call required in select statement", N);
5473
5474 -- Ada 2005 (AI-345): If a procedure_call_statement is used
5475 -- for a procedure_or_entry_call, the procedure_name or
5476 -- procedure_prefix of the procedure_call_statement shall denote
5477 -- an entry renamed by a procedure, or (a view of) a primitive
5478 -- subprogram of a limited interface whose first parameter is
5479 -- a controlling parameter.
5480
5481 elsif Nkind (N) = N_Procedure_Call_Statement
5482 and then not Is_Renamed_Entry (Nam)
5483 and then not Is_Controlling_Limited_Procedure (Nam)
5484 then
5485 Error_Msg_N
5486 ("entry call or dispatching primitive of interface required", N);
5487 end if;
5488 end if;
5489
5490 -- If the SPARK_05 restriction is active, we are not allowed
5491 -- to have a call to a subprogram before we see its completion.
5492
5493 if not Has_Completion (Nam)
5494 and then Restriction_Check_Required (SPARK_05)
5495
5496 -- Don't flag strange internal calls
5497
5498 and then Comes_From_Source (N)
5499 and then Comes_From_Source (Nam)
5500
5501 -- Only flag calls in extended main source
5502
5503 and then In_Extended_Main_Source_Unit (Nam)
5504 and then In_Extended_Main_Source_Unit (N)
5505
5506 -- Exclude enumeration literals from this processing
5507
5508 and then Ekind (Nam) /= E_Enumeration_Literal
5509 then
5510 Check_SPARK_Restriction
5511 ("call to subprogram cannot appear before its body", N);
5512 end if;
5513
5514 -- Check that this is not a call to a protected procedure or entry from
5515 -- within a protected function.
5516
5517 Check_Internal_Protected_Use (N, Nam);
5518
5519 -- Freeze the subprogram name if not in a spec-expression. Note that
5520 -- we freeze procedure calls as well as function calls. Procedure calls
5521 -- are not frozen according to the rules (RM 13.14(14)) because it is
5522 -- impossible to have a procedure call to a non-frozen procedure in
5523 -- pure Ada, but in the code that we generate in the expander, this
5524 -- rule needs extending because we can generate procedure calls that
5525 -- need freezing.
5526
5527 -- In Ada 2012, expression functions may be called within pre/post
5528 -- conditions of subsequent functions or expression functions. Such
5529 -- calls do not freeze when they appear within generated bodies,
5530 -- (including the body of another expression function) which would
5531 -- place the freeze node in the wrong scope. An expression function
5532 -- is frozen in the usual fashion, by the appearance of a real body,
5533 -- or at the end of a declarative part.
5534
5535 if Is_Entity_Name (Subp) and then not In_Spec_Expression
5536 and then not Is_Expression_Function (Current_Scope)
5537 and then
5538 (not Is_Expression_Function (Entity (Subp))
5539 or else Scope (Entity (Subp)) = Current_Scope)
5540 then
5541 Freeze_Expression (Subp);
5542 end if;
5543
5544 -- For a predefined operator, the type of the result is the type imposed
5545 -- by context, except for a predefined operation on universal fixed.
5546 -- Otherwise The type of the call is the type returned by the subprogram
5547 -- being called.
5548
5549 if Is_Predefined_Op (Nam) then
5550 if Etype (N) /= Universal_Fixed then
5551 Set_Etype (N, Typ);
5552 end if;
5553
5554 -- If the subprogram returns an array type, and the context requires the
5555 -- component type of that array type, the node is really an indexing of
5556 -- the parameterless call. Resolve as such. A pathological case occurs
5557 -- when the type of the component is an access to the array type. In
5558 -- this case the call is truly ambiguous.
5559
5560 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
5561 and then
5562 ((Is_Array_Type (Etype (Nam))
5563 and then Covers (Typ, Component_Type (Etype (Nam))))
5564 or else
5565 (Is_Access_Type (Etype (Nam))
5566 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5567 and then
5568 Covers (Typ, Component_Type (Designated_Type (Etype (Nam))))))
5569 then
5570 declare
5571 Index_Node : Node_Id;
5572 New_Subp : Node_Id;
5573 Ret_Type : constant Entity_Id := Etype (Nam);
5574
5575 begin
5576 if Is_Access_Type (Ret_Type)
5577 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
5578 then
5579 Error_Msg_N
5580 ("cannot disambiguate function call and indexing", N);
5581 else
5582 New_Subp := Relocate_Node (Subp);
5583
5584 -- The called entity may be an explicit dereference, in which
5585 -- case there is no entity to set.
5586
5587 if Nkind (New_Subp) /= N_Explicit_Dereference then
5588 Set_Entity (Subp, Nam);
5589 end if;
5590
5591 if (Is_Array_Type (Ret_Type)
5592 and then Component_Type (Ret_Type) /= Any_Type)
5593 or else
5594 (Is_Access_Type (Ret_Type)
5595 and then
5596 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
5597 then
5598 if Needs_No_Actuals (Nam) then
5599
5600 -- Indexed call to a parameterless function
5601
5602 Index_Node :=
5603 Make_Indexed_Component (Loc,
5604 Prefix =>
5605 Make_Function_Call (Loc,
5606 Name => New_Subp),
5607 Expressions => Parameter_Associations (N));
5608 else
5609 -- An Ada 2005 prefixed call to a primitive operation
5610 -- whose first parameter is the prefix. This prefix was
5611 -- prepended to the parameter list, which is actually a
5612 -- list of indexes. Remove the prefix in order to build
5613 -- the proper indexed component.
5614
5615 Index_Node :=
5616 Make_Indexed_Component (Loc,
5617 Prefix =>
5618 Make_Function_Call (Loc,
5619 Name => New_Subp,
5620 Parameter_Associations =>
5621 New_List
5622 (Remove_Head (Parameter_Associations (N)))),
5623 Expressions => Parameter_Associations (N));
5624 end if;
5625
5626 -- Preserve the parenthesis count of the node
5627
5628 Set_Paren_Count (Index_Node, Paren_Count (N));
5629
5630 -- Since we are correcting a node classification error made
5631 -- by the parser, we call Replace rather than Rewrite.
5632
5633 Replace (N, Index_Node);
5634
5635 Set_Etype (Prefix (N), Ret_Type);
5636 Set_Etype (N, Typ);
5637 Resolve_Indexed_Component (N, Typ);
5638 Check_Elab_Call (Prefix (N));
5639 end if;
5640 end if;
5641
5642 return;
5643 end;
5644
5645 else
5646 Set_Etype (N, Etype (Nam));
5647 end if;
5648
5649 -- In the case where the call is to an overloaded subprogram, Analyze
5650 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
5651 -- such a case Normalize_Actuals needs to be called once more to order
5652 -- the actuals correctly. Otherwise the call will have the ordering
5653 -- given by the last overloaded subprogram whether this is the correct
5654 -- one being called or not.
5655
5656 if Is_Overloaded (Subp) then
5657 Normalize_Actuals (N, Nam, False, Norm_OK);
5658 pragma Assert (Norm_OK);
5659 end if;
5660
5661 -- In any case, call is fully resolved now. Reset Overload flag, to
5662 -- prevent subsequent overload resolution if node is analyzed again
5663
5664 Set_Is_Overloaded (Subp, False);
5665 Set_Is_Overloaded (N, False);
5666
5667 -- If we are calling the current subprogram from immediately within its
5668 -- body, then that is the case where we can sometimes detect cases of
5669 -- infinite recursion statically. Do not try this in case restriction
5670 -- No_Recursion is in effect anyway, and do it only for source calls.
5671
5672 if Comes_From_Source (N) then
5673 Scop := Current_Scope;
5674
5675 -- Check violation of SPARK_05 restriction which does not permit
5676 -- a subprogram body to contain a call to the subprogram directly.
5677
5678 if Restriction_Check_Required (SPARK_05)
5679 and then Same_Or_Aliased_Subprograms (Nam, Scop)
5680 then
5681 Check_SPARK_Restriction
5682 ("subprogram may not contain direct call to itself", N);
5683 end if;
5684
5685 -- Issue warning for possible infinite recursion in the absence
5686 -- of the No_Recursion restriction.
5687
5688 if Same_Or_Aliased_Subprograms (Nam, Scop)
5689 and then not Restriction_Active (No_Recursion)
5690 and then Check_Infinite_Recursion (N)
5691 then
5692 -- Here we detected and flagged an infinite recursion, so we do
5693 -- not need to test the case below for further warnings. Also we
5694 -- are all done if we now have a raise SE node.
5695
5696 if Nkind (N) = N_Raise_Storage_Error then
5697 return;
5698 end if;
5699
5700 -- If call is to immediately containing subprogram, then check for
5701 -- the case of a possible run-time detectable infinite recursion.
5702
5703 else
5704 Scope_Loop : while Scop /= Standard_Standard loop
5705 if Same_Or_Aliased_Subprograms (Nam, Scop) then
5706
5707 -- Although in general case, recursion is not statically
5708 -- checkable, the case of calling an immediately containing
5709 -- subprogram is easy to catch.
5710
5711 Check_Restriction (No_Recursion, N);
5712
5713 -- If the recursive call is to a parameterless subprogram,
5714 -- then even if we can't statically detect infinite
5715 -- recursion, this is pretty suspicious, and we output a
5716 -- warning. Furthermore, we will try later to detect some
5717 -- cases here at run time by expanding checking code (see
5718 -- Detect_Infinite_Recursion in package Exp_Ch6).
5719
5720 -- If the recursive call is within a handler, do not emit a
5721 -- warning, because this is a common idiom: loop until input
5722 -- is correct, catch illegal input in handler and restart.
5723
5724 if No (First_Formal (Nam))
5725 and then Etype (Nam) = Standard_Void_Type
5726 and then not Error_Posted (N)
5727 and then Nkind (Parent (N)) /= N_Exception_Handler
5728 then
5729 -- For the case of a procedure call. We give the message
5730 -- only if the call is the first statement in a sequence
5731 -- of statements, or if all previous statements are
5732 -- simple assignments. This is simply a heuristic to
5733 -- decrease false positives, without losing too many good
5734 -- warnings. The idea is that these previous statements
5735 -- may affect global variables the procedure depends on.
5736 -- We also exclude raise statements, that may arise from
5737 -- constraint checks and are probably unrelated to the
5738 -- intended control flow.
5739
5740 if Nkind (N) = N_Procedure_Call_Statement
5741 and then Is_List_Member (N)
5742 then
5743 declare
5744 P : Node_Id;
5745 begin
5746 P := Prev (N);
5747 while Present (P) loop
5748 if not Nkind_In (P,
5749 N_Assignment_Statement,
5750 N_Raise_Constraint_Error)
5751 then
5752 exit Scope_Loop;
5753 end if;
5754
5755 Prev (P);
5756 end loop;
5757 end;
5758 end if;
5759
5760 -- Do not give warning if we are in a conditional context
5761
5762 declare
5763 K : constant Node_Kind := Nkind (Parent (N));
5764 begin
5765 if (K = N_Loop_Statement
5766 and then Present (Iteration_Scheme (Parent (N))))
5767 or else K = N_If_Statement
5768 or else K = N_Elsif_Part
5769 or else K = N_Case_Statement_Alternative
5770 then
5771 exit Scope_Loop;
5772 end if;
5773 end;
5774
5775 -- Here warning is to be issued
5776
5777 Set_Has_Recursive_Call (Nam);
5778 Error_Msg_Warn := SPARK_Mode /= On;
5779 Error_Msg_N ("possible infinite recursion<<!", N);
5780 Error_Msg_N ("\Storage_Error ]<<!", N);
5781 end if;
5782
5783 exit Scope_Loop;
5784 end if;
5785
5786 Scop := Scope (Scop);
5787 end loop Scope_Loop;
5788 end if;
5789 end if;
5790
5791 -- Check obsolescent reference to Ada.Characters.Handling subprogram
5792
5793 Check_Obsolescent_2005_Entity (Nam, Subp);
5794
5795 -- If subprogram name is a predefined operator, it was given in
5796 -- functional notation. Replace call node with operator node, so
5797 -- that actuals can be resolved appropriately.
5798
5799 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5800 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5801 return;
5802
5803 elsif Present (Alias (Nam))
5804 and then Is_Predefined_Op (Alias (Nam))
5805 then
5806 Resolve_Actuals (N, Nam);
5807 Make_Call_Into_Operator (N, Typ, Alias (Nam));
5808 return;
5809 end if;
5810
5811 -- Create a transient scope if the resulting type requires it
5812
5813 -- There are several notable exceptions:
5814
5815 -- a) In init procs, the transient scope overhead is not needed, and is
5816 -- even incorrect when the call is a nested initialization call for a
5817 -- component whose expansion may generate adjust calls. However, if the
5818 -- call is some other procedure call within an initialization procedure
5819 -- (for example a call to Create_Task in the init_proc of the task
5820 -- run-time record) a transient scope must be created around this call.
5821
5822 -- b) Enumeration literal pseudo-calls need no transient scope
5823
5824 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
5825 -- functions) do not use the secondary stack even though the return
5826 -- type may be unconstrained.
5827
5828 -- d) Calls to a build-in-place function, since such functions may
5829 -- allocate their result directly in a target object, and cases where
5830 -- the result does get allocated in the secondary stack are checked for
5831 -- within the specialized Exp_Ch6 procedures for expanding those
5832 -- build-in-place calls.
5833
5834 -- e) If the subprogram is marked Inline_Always, then even if it returns
5835 -- an unconstrained type the call does not require use of the secondary
5836 -- stack. However, inlining will only take place if the body to inline
5837 -- is already present. It may not be available if e.g. the subprogram is
5838 -- declared in a child instance.
5839
5840 -- If this is an initialization call for a type whose construction
5841 -- uses the secondary stack, and it is not a nested call to initialize
5842 -- a component, we do need to create a transient scope for it. We
5843 -- check for this by traversing the type in Check_Initialization_Call.
5844
5845 if Is_Inlined (Nam)
5846 and then Has_Pragma_Inline_Always (Nam)
5847 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5848 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5849 and then not Debug_Flag_Dot_K
5850 then
5851 null;
5852
5853 elsif Is_Inlined (Nam)
5854 and then Has_Pragma_Inline (Nam)
5855 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5856 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5857 and then Debug_Flag_Dot_K
5858 then
5859 null;
5860
5861 elsif Ekind (Nam) = E_Enumeration_Literal
5862 or else Is_Build_In_Place_Function (Nam)
5863 or else Is_Intrinsic_Subprogram (Nam)
5864 then
5865 null;
5866
5867 elsif Expander_Active
5868 and then Is_Type (Etype (Nam))
5869 and then Requires_Transient_Scope (Etype (Nam))
5870 and then
5871 (not Within_Init_Proc
5872 or else
5873 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
5874 then
5875 Establish_Transient_Scope (N, Sec_Stack => True);
5876
5877 -- If the call appears within the bounds of a loop, it will
5878 -- be rewritten and reanalyzed, nothing left to do here.
5879
5880 if Nkind (N) /= N_Function_Call then
5881 return;
5882 end if;
5883
5884 elsif Is_Init_Proc (Nam)
5885 and then not Within_Init_Proc
5886 then
5887 Check_Initialization_Call (N, Nam);
5888 end if;
5889
5890 -- A protected function cannot be called within the definition of the
5891 -- enclosing protected type.
5892
5893 if Is_Protected_Type (Scope (Nam))
5894 and then In_Open_Scopes (Scope (Nam))
5895 and then not Has_Completion (Scope (Nam))
5896 then
5897 Error_Msg_NE
5898 ("& cannot be called before end of protected definition", N, Nam);
5899 end if;
5900
5901 -- Propagate interpretation to actuals, and add default expressions
5902 -- where needed.
5903
5904 if Present (First_Formal (Nam)) then
5905 Resolve_Actuals (N, Nam);
5906
5907 -- Overloaded literals are rewritten as function calls, for purpose of
5908 -- resolution. After resolution, we can replace the call with the
5909 -- literal itself.
5910
5911 elsif Ekind (Nam) = E_Enumeration_Literal then
5912 Copy_Node (Subp, N);
5913 Resolve_Entity_Name (N, Typ);
5914
5915 -- Avoid validation, since it is a static function call
5916
5917 Generate_Reference (Nam, Subp);
5918 return;
5919 end if;
5920
5921 -- If the subprogram is not global, then kill all saved values and
5922 -- checks. This is a bit conservative, since in many cases we could do
5923 -- better, but it is not worth the effort. Similarly, we kill constant
5924 -- values. However we do not need to do this for internal entities
5925 -- (unless they are inherited user-defined subprograms), since they
5926 -- are not in the business of molesting local values.
5927
5928 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
5929 -- kill all checks and values for calls to global subprograms. This
5930 -- takes care of the case where an access to a local subprogram is
5931 -- taken, and could be passed directly or indirectly and then called
5932 -- from almost any context.
5933
5934 -- Note: we do not do this step till after resolving the actuals. That
5935 -- way we still take advantage of the current value information while
5936 -- scanning the actuals.
5937
5938 -- We suppress killing values if we are processing the nodes associated
5939 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
5940 -- type kills all the values as part of analyzing the code that
5941 -- initializes the dispatch tables.
5942
5943 if Inside_Freezing_Actions = 0
5944 and then (not Is_Library_Level_Entity (Nam)
5945 or else Suppress_Value_Tracking_On_Call
5946 (Nearest_Dynamic_Scope (Current_Scope)))
5947 and then (Comes_From_Source (Nam)
5948 or else (Present (Alias (Nam))
5949 and then Comes_From_Source (Alias (Nam))))
5950 then
5951 Kill_Current_Values;
5952 end if;
5953
5954 -- If we are warning about unread OUT parameters, this is the place to
5955 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
5956 -- after the above call to Kill_Current_Values (since that call clears
5957 -- the Last_Assignment field of all local variables).
5958
5959 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
5960 and then Comes_From_Source (N)
5961 and then In_Extended_Main_Source_Unit (N)
5962 then
5963 declare
5964 F : Entity_Id;
5965 A : Node_Id;
5966
5967 begin
5968 F := First_Formal (Nam);
5969 A := First_Actual (N);
5970 while Present (F) and then Present (A) loop
5971 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
5972 and then Warn_On_Modified_As_Out_Parameter (F)
5973 and then Is_Entity_Name (A)
5974 and then Present (Entity (A))
5975 and then Comes_From_Source (N)
5976 and then Safe_To_Capture_Value (N, Entity (A))
5977 then
5978 Set_Last_Assignment (Entity (A), A);
5979 end if;
5980
5981 Next_Formal (F);
5982 Next_Actual (A);
5983 end loop;
5984 end;
5985 end if;
5986
5987 -- If the subprogram is a primitive operation, check whether or not
5988 -- it is a correct dispatching call.
5989
5990 if Is_Overloadable (Nam)
5991 and then Is_Dispatching_Operation (Nam)
5992 then
5993 Check_Dispatching_Call (N);
5994
5995 elsif Ekind (Nam) /= E_Subprogram_Type
5996 and then Is_Abstract_Subprogram (Nam)
5997 and then not In_Instance
5998 then
5999 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
6000 end if;
6001
6002 -- If this is a dispatching call, generate the appropriate reference,
6003 -- for better source navigation in GPS.
6004
6005 if Is_Overloadable (Nam)
6006 and then Present (Controlling_Argument (N))
6007 then
6008 Generate_Reference (Nam, Subp, 'R');
6009
6010 -- Normal case, not a dispatching call: generate a call reference
6011
6012 else
6013 Generate_Reference (Nam, Subp, 's');
6014 end if;
6015
6016 if Is_Intrinsic_Subprogram (Nam) then
6017 Check_Intrinsic_Call (N);
6018 end if;
6019
6020 -- Check for violation of restriction No_Specific_Termination_Handlers
6021 -- and warn on a potentially blocking call to Abort_Task.
6022
6023 if Restriction_Check_Required (No_Specific_Termination_Handlers)
6024 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
6025 or else
6026 Is_RTE (Nam, RE_Specific_Handler))
6027 then
6028 Check_Restriction (No_Specific_Termination_Handlers, N);
6029
6030 elsif Is_RTE (Nam, RE_Abort_Task) then
6031 Check_Potentially_Blocking_Operation (N);
6032 end if;
6033
6034 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
6035 -- timing event violates restriction No_Relative_Delay (AI-0211). We
6036 -- need to check the second argument to determine whether it is an
6037 -- absolute or relative timing event.
6038
6039 if Restriction_Check_Required (No_Relative_Delay)
6040 and then Is_RTE (Nam, RE_Set_Handler)
6041 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
6042 then
6043 Check_Restriction (No_Relative_Delay, N);
6044 end if;
6045
6046 -- Issue an error for a call to an eliminated subprogram. This routine
6047 -- will not perform the check if the call appears within a default
6048 -- expression.
6049
6050 Check_For_Eliminated_Subprogram (Subp, Nam);
6051
6052 -- In formal mode, the primitive operations of a tagged type or type
6053 -- extension do not include functions that return the tagged type.
6054
6055 if Nkind (N) = N_Function_Call
6056 and then Is_Tagged_Type (Etype (N))
6057 and then Is_Entity_Name (Name (N))
6058 and then Is_Inherited_Operation_For_Type (Entity (Name (N)), Etype (N))
6059 then
6060 Check_SPARK_Restriction ("function not inherited", N);
6061 end if;
6062
6063 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
6064 -- class-wide and the call dispatches on result in a context that does
6065 -- not provide a tag, the call raises Program_Error.
6066
6067 if Nkind (N) = N_Function_Call
6068 and then In_Instance
6069 and then Is_Generic_Actual_Type (Typ)
6070 and then Is_Class_Wide_Type (Typ)
6071 and then Has_Controlling_Result (Nam)
6072 and then Nkind (Parent (N)) = N_Object_Declaration
6073 then
6074 -- Verify that none of the formals are controlling
6075
6076 declare
6077 Call_OK : Boolean := False;
6078 F : Entity_Id;
6079
6080 begin
6081 F := First_Formal (Nam);
6082 while Present (F) loop
6083 if Is_Controlling_Formal (F) then
6084 Call_OK := True;
6085 exit;
6086 end if;
6087
6088 Next_Formal (F);
6089 end loop;
6090
6091 if not Call_OK then
6092 Error_Msg_Warn := SPARK_Mode /= On;
6093 Error_Msg_N ("!cannot determine tag of result<<", N);
6094 Error_Msg_N ("\Program_Error [<<!", N);
6095 Insert_Action (N,
6096 Make_Raise_Program_Error (Sloc (N),
6097 Reason => PE_Explicit_Raise));
6098 end if;
6099 end;
6100 end if;
6101
6102 -- Check the dimensions of the actuals in the call. For function calls,
6103 -- propagate the dimensions from the returned type to N.
6104
6105 Analyze_Dimension_Call (N, Nam);
6106
6107 -- All done, evaluate call and deal with elaboration issues
6108
6109 Eval_Call (N);
6110 Check_Elab_Call (N);
6111 Warn_On_Overlapping_Actuals (Nam, N);
6112 end Resolve_Call;
6113
6114 -----------------------------
6115 -- Resolve_Case_Expression --
6116 -----------------------------
6117
6118 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
6119 Alt : Node_Id;
6120
6121 begin
6122 Alt := First (Alternatives (N));
6123 while Present (Alt) loop
6124 Resolve (Expression (Alt), Typ);
6125 Next (Alt);
6126 end loop;
6127
6128 Set_Etype (N, Typ);
6129 Eval_Case_Expression (N);
6130 end Resolve_Case_Expression;
6131
6132 -------------------------------
6133 -- Resolve_Character_Literal --
6134 -------------------------------
6135
6136 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
6137 B_Typ : constant Entity_Id := Base_Type (Typ);
6138 C : Entity_Id;
6139
6140 begin
6141 -- Verify that the character does belong to the type of the context
6142
6143 Set_Etype (N, B_Typ);
6144 Eval_Character_Literal (N);
6145
6146 -- Wide_Wide_Character literals must always be defined, since the set
6147 -- of wide wide character literals is complete, i.e. if a character
6148 -- literal is accepted by the parser, then it is OK for wide wide
6149 -- character (out of range character literals are rejected).
6150
6151 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
6152 return;
6153
6154 -- Always accept character literal for type Any_Character, which
6155 -- occurs in error situations and in comparisons of literals, both
6156 -- of which should accept all literals.
6157
6158 elsif B_Typ = Any_Character then
6159 return;
6160
6161 -- For Standard.Character or a type derived from it, check that the
6162 -- literal is in range.
6163
6164 elsif Root_Type (B_Typ) = Standard_Character then
6165 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
6166 return;
6167 end if;
6168
6169 -- For Standard.Wide_Character or a type derived from it, check that the
6170 -- literal is in range.
6171
6172 elsif Root_Type (B_Typ) = Standard_Wide_Character then
6173 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
6174 return;
6175 end if;
6176
6177 -- For Standard.Wide_Wide_Character or a type derived from it, we
6178 -- know the literal is in range, since the parser checked.
6179
6180 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
6181 return;
6182
6183 -- If the entity is already set, this has already been resolved in a
6184 -- generic context, or comes from expansion. Nothing else to do.
6185
6186 elsif Present (Entity (N)) then
6187 return;
6188
6189 -- Otherwise we have a user defined character type, and we can use the
6190 -- standard visibility mechanisms to locate the referenced entity.
6191
6192 else
6193 C := Current_Entity (N);
6194 while Present (C) loop
6195 if Etype (C) = B_Typ then
6196 Set_Entity_With_Checks (N, C);
6197 Generate_Reference (C, N);
6198 return;
6199 end if;
6200
6201 C := Homonym (C);
6202 end loop;
6203 end if;
6204
6205 -- If we fall through, then the literal does not match any of the
6206 -- entries of the enumeration type. This isn't just a constraint error
6207 -- situation, it is an illegality (see RM 4.2).
6208
6209 Error_Msg_NE
6210 ("character not defined for }", N, First_Subtype (B_Typ));
6211 end Resolve_Character_Literal;
6212
6213 ---------------------------
6214 -- Resolve_Comparison_Op --
6215 ---------------------------
6216
6217 -- Context requires a boolean type, and plays no role in resolution.
6218 -- Processing identical to that for equality operators. The result type is
6219 -- the base type, which matters when pathological subtypes of booleans with
6220 -- limited ranges are used.
6221
6222 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
6223 L : constant Node_Id := Left_Opnd (N);
6224 R : constant Node_Id := Right_Opnd (N);
6225 T : Entity_Id;
6226
6227 begin
6228 -- If this is an intrinsic operation which is not predefined, use the
6229 -- types of its declared arguments to resolve the possibly overloaded
6230 -- operands. Otherwise the operands are unambiguous and specify the
6231 -- expected type.
6232
6233 if Scope (Entity (N)) /= Standard_Standard then
6234 T := Etype (First_Entity (Entity (N)));
6235
6236 else
6237 T := Find_Unique_Type (L, R);
6238
6239 if T = Any_Fixed then
6240 T := Unique_Fixed_Point_Type (L);
6241 end if;
6242 end if;
6243
6244 Set_Etype (N, Base_Type (Typ));
6245 Generate_Reference (T, N, ' ');
6246
6247 -- Skip remaining processing if already set to Any_Type
6248
6249 if T = Any_Type then
6250 return;
6251 end if;
6252
6253 -- Deal with other error cases
6254
6255 if T = Any_String or else
6256 T = Any_Composite or else
6257 T = Any_Character
6258 then
6259 if T = Any_Character then
6260 Ambiguous_Character (L);
6261 else
6262 Error_Msg_N ("ambiguous operands for comparison", N);
6263 end if;
6264
6265 Set_Etype (N, Any_Type);
6266 return;
6267 end if;
6268
6269 -- Resolve the operands if types OK
6270
6271 Resolve (L, T);
6272 Resolve (R, T);
6273 Check_Unset_Reference (L);
6274 Check_Unset_Reference (R);
6275 Generate_Operator_Reference (N, T);
6276 Check_Low_Bound_Tested (N);
6277
6278 -- In SPARK, ordering operators <, <=, >, >= are not defined for Boolean
6279 -- types or array types except String.
6280
6281 if Is_Boolean_Type (T) then
6282 Check_SPARK_Restriction
6283 ("comparison is not defined on Boolean type", N);
6284
6285 elsif Is_Array_Type (T)
6286 and then Base_Type (T) /= Standard_String
6287 then
6288 Check_SPARK_Restriction
6289 ("comparison is not defined on array types other than String", N);
6290 end if;
6291
6292 -- Check comparison on unordered enumeration
6293
6294 if Bad_Unordered_Enumeration_Reference (N, Etype (L)) then
6295 Error_Msg_Sloc := Sloc (Etype (L));
6296 Error_Msg_NE
6297 ("comparison on unordered enumeration type& declared#?U?",
6298 N, Etype (L));
6299 end if;
6300
6301 -- Evaluate the relation (note we do this after the above check since
6302 -- this Eval call may change N to True/False.
6303
6304 Analyze_Dimension (N);
6305 Eval_Relational_Op (N);
6306 end Resolve_Comparison_Op;
6307
6308 -----------------------------------------
6309 -- Resolve_Discrete_Subtype_Indication --
6310 -----------------------------------------
6311
6312 procedure Resolve_Discrete_Subtype_Indication
6313 (N : Node_Id;
6314 Typ : Entity_Id)
6315 is
6316 R : Node_Id;
6317 S : Entity_Id;
6318
6319 begin
6320 Analyze (Subtype_Mark (N));
6321 S := Entity (Subtype_Mark (N));
6322
6323 if Nkind (Constraint (N)) /= N_Range_Constraint then
6324 Error_Msg_N ("expect range constraint for discrete type", N);
6325 Set_Etype (N, Any_Type);
6326
6327 else
6328 R := Range_Expression (Constraint (N));
6329
6330 if R = Error then
6331 return;
6332 end if;
6333
6334 Analyze (R);
6335
6336 if Base_Type (S) /= Base_Type (Typ) then
6337 Error_Msg_NE
6338 ("expect subtype of }", N, First_Subtype (Typ));
6339
6340 -- Rewrite the constraint as a range of Typ
6341 -- to allow compilation to proceed further.
6342
6343 Set_Etype (N, Typ);
6344 Rewrite (Low_Bound (R),
6345 Make_Attribute_Reference (Sloc (Low_Bound (R)),
6346 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6347 Attribute_Name => Name_First));
6348 Rewrite (High_Bound (R),
6349 Make_Attribute_Reference (Sloc (High_Bound (R)),
6350 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
6351 Attribute_Name => Name_First));
6352
6353 else
6354 Resolve (R, Typ);
6355 Set_Etype (N, Etype (R));
6356
6357 -- Additionally, we must check that the bounds are compatible
6358 -- with the given subtype, which might be different from the
6359 -- type of the context.
6360
6361 Apply_Range_Check (R, S);
6362
6363 -- ??? If the above check statically detects a Constraint_Error
6364 -- it replaces the offending bound(s) of the range R with a
6365 -- Constraint_Error node. When the itype which uses these bounds
6366 -- is frozen the resulting call to Duplicate_Subexpr generates
6367 -- a new temporary for the bounds.
6368
6369 -- Unfortunately there are other itypes that are also made depend
6370 -- on these bounds, so when Duplicate_Subexpr is called they get
6371 -- a forward reference to the newly created temporaries and Gigi
6372 -- aborts on such forward references. This is probably sign of a
6373 -- more fundamental problem somewhere else in either the order of
6374 -- itype freezing or the way certain itypes are constructed.
6375
6376 -- To get around this problem we call Remove_Side_Effects right
6377 -- away if either bounds of R are a Constraint_Error.
6378
6379 declare
6380 L : constant Node_Id := Low_Bound (R);
6381 H : constant Node_Id := High_Bound (R);
6382
6383 begin
6384 if Nkind (L) = N_Raise_Constraint_Error then
6385 Remove_Side_Effects (L);
6386 end if;
6387
6388 if Nkind (H) = N_Raise_Constraint_Error then
6389 Remove_Side_Effects (H);
6390 end if;
6391 end;
6392
6393 Check_Unset_Reference (Low_Bound (R));
6394 Check_Unset_Reference (High_Bound (R));
6395 end if;
6396 end if;
6397 end Resolve_Discrete_Subtype_Indication;
6398
6399 -------------------------
6400 -- Resolve_Entity_Name --
6401 -------------------------
6402
6403 -- Used to resolve identifiers and expanded names
6404
6405 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
6406 function Appears_In_Check (Nod : Node_Id) return Boolean;
6407 -- Denote whether an arbitrary node Nod appears in a check node
6408
6409 ----------------------
6410 -- Appears_In_Check --
6411 ----------------------
6412
6413 function Appears_In_Check (Nod : Node_Id) return Boolean is
6414 Par : Node_Id;
6415
6416 begin
6417 -- Climb the parent chain looking for a check node
6418
6419 Par := Nod;
6420 while Present (Par) loop
6421 if Nkind (Par) in N_Raise_xxx_Error then
6422 return True;
6423
6424 -- Prevent the search from going too far
6425
6426 elsif Is_Body_Or_Package_Declaration (Par) then
6427 exit;
6428 end if;
6429
6430 Par := Parent (Par);
6431 end loop;
6432
6433 return False;
6434 end Appears_In_Check;
6435
6436 -- Local variables
6437
6438 E : constant Entity_Id := Entity (N);
6439 Par : constant Node_Id := Parent (N);
6440
6441 -- Start of processing for Resolve_Entity_Name
6442
6443 begin
6444 -- If garbage from errors, set to Any_Type and return
6445
6446 if No (E) and then Total_Errors_Detected /= 0 then
6447 Set_Etype (N, Any_Type);
6448 return;
6449 end if;
6450
6451 -- Replace named numbers by corresponding literals. Note that this is
6452 -- the one case where Resolve_Entity_Name must reset the Etype, since
6453 -- it is currently marked as universal.
6454
6455 if Ekind (E) = E_Named_Integer then
6456 Set_Etype (N, Typ);
6457 Eval_Named_Integer (N);
6458
6459 elsif Ekind (E) = E_Named_Real then
6460 Set_Etype (N, Typ);
6461 Eval_Named_Real (N);
6462
6463 -- For enumeration literals, we need to make sure that a proper style
6464 -- check is done, since such literals are overloaded, and thus we did
6465 -- not do a style check during the first phase of analysis.
6466
6467 elsif Ekind (E) = E_Enumeration_Literal then
6468 Set_Entity_With_Checks (N, E);
6469 Eval_Entity_Name (N);
6470
6471 -- Case of subtype name appearing as an operand in expression
6472
6473 elsif Is_Type (E) then
6474
6475 -- Allow use of subtype if it is a concurrent type where we are
6476 -- currently inside the body. This will eventually be expanded into a
6477 -- call to Self (for tasks) or _object (for protected objects). Any
6478 -- other use of a subtype is invalid.
6479
6480 if Is_Concurrent_Type (E)
6481 and then In_Open_Scopes (E)
6482 then
6483 null;
6484
6485 -- Any other use is an error
6486
6487 else
6488 Error_Msg_N
6489 ("invalid use of subtype mark in expression or call", N);
6490 end if;
6491
6492 -- Check discriminant use if entity is discriminant in current scope,
6493 -- i.e. discriminant of record or concurrent type currently being
6494 -- analyzed. Uses in corresponding body are unrestricted.
6495
6496 elsif Ekind (E) = E_Discriminant
6497 and then Scope (E) = Current_Scope
6498 and then not Has_Completion (Current_Scope)
6499 then
6500 Check_Discriminant_Use (N);
6501
6502 -- A parameterless generic function cannot appear in a context that
6503 -- requires resolution.
6504
6505 elsif Ekind (E) = E_Generic_Function then
6506 Error_Msg_N ("illegal use of generic function", N);
6507
6508 elsif Ekind (E) = E_Out_Parameter
6509 and then Ada_Version = Ada_83
6510 and then (Nkind (Parent (N)) in N_Op
6511 or else (Nkind (Parent (N)) = N_Assignment_Statement
6512 and then N = Expression (Parent (N)))
6513 or else Nkind (Parent (N)) = N_Explicit_Dereference)
6514 then
6515 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
6516
6517 -- In all other cases, just do the possible static evaluation
6518
6519 else
6520 -- A deferred constant that appears in an expression must have a
6521 -- completion, unless it has been removed by in-place expansion of
6522 -- an aggregate.
6523
6524 if Ekind (E) = E_Constant
6525 and then Comes_From_Source (E)
6526 and then No (Constant_Value (E))
6527 and then Is_Frozen (Etype (E))
6528 and then not In_Spec_Expression
6529 and then not Is_Imported (E)
6530 then
6531 if No_Initialization (Parent (E))
6532 or else (Present (Full_View (E))
6533 and then No_Initialization (Parent (Full_View (E))))
6534 then
6535 null;
6536 else
6537 Error_Msg_N (
6538 "deferred constant is frozen before completion", N);
6539 end if;
6540 end if;
6541
6542 Eval_Entity_Name (N);
6543 end if;
6544
6545 -- A volatile object subject to enabled properties Async_Writers or
6546 -- Effective_Reads must appear in a specific context. The following
6547 -- checks are only relevant when SPARK_Mode is on as they are not
6548 -- standard Ada legality rules.
6549
6550 if SPARK_Mode = On
6551 and then Is_Object (E)
6552 and then Is_SPARK_Volatile (E)
6553 and then Comes_From_Source (E)
6554 and then
6555 (Async_Writers_Enabled (E) or else Effective_Reads_Enabled (E))
6556 then
6557 -- The volatile object can appear on either side of an assignment
6558
6559 if Nkind (Par) = N_Assignment_Statement then
6560 null;
6561
6562 -- The volatile object is part of the initialization expression of
6563 -- another object. Ensure that the climb of the parent chain came
6564 -- from the expression side and not from the name side.
6565
6566 elsif Nkind (Par) = N_Object_Declaration
6567 and then Present (Expression (Par))
6568 and then N = Expression (Par)
6569 then
6570 null;
6571
6572 -- The volatile object appears as an actual parameter in a call to an
6573 -- instance of Unchecked_Conversion whose result is renamed.
6574
6575 elsif Nkind (Par) = N_Function_Call
6576 and then Is_Unchecked_Conversion_Instance (Entity (Name (Par)))
6577 and then Nkind (Parent (Par)) = N_Object_Renaming_Declaration
6578 then
6579 null;
6580
6581 -- Assume that references to volatile objects that appear as actual
6582 -- parameters in a procedure call are always legal. The full legality
6583 -- check is done when the actuals are resolved.
6584
6585 elsif Nkind (Par) = N_Procedure_Call_Statement then
6586 null;
6587
6588 -- Allow references to volatile objects in various checks
6589
6590 elsif Appears_In_Check (Par) then
6591 null;
6592
6593 else
6594 Error_Msg_N
6595 ("volatile object cannot appear in this context "
6596 & "(SPARK RM 7.1.3(13))", N);
6597 end if;
6598 end if;
6599 end Resolve_Entity_Name;
6600
6601 -------------------
6602 -- Resolve_Entry --
6603 -------------------
6604
6605 procedure Resolve_Entry (Entry_Name : Node_Id) is
6606 Loc : constant Source_Ptr := Sloc (Entry_Name);
6607 Nam : Entity_Id;
6608 New_N : Node_Id;
6609 S : Entity_Id;
6610 Tsk : Entity_Id;
6611 E_Name : Node_Id;
6612 Index : Node_Id;
6613
6614 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
6615 -- If the bounds of the entry family being called depend on task
6616 -- discriminants, build a new index subtype where a discriminant is
6617 -- replaced with the value of the discriminant of the target task.
6618 -- The target task is the prefix of the entry name in the call.
6619
6620 -----------------------
6621 -- Actual_Index_Type --
6622 -----------------------
6623
6624 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
6625 Typ : constant Entity_Id := Entry_Index_Type (E);
6626 Tsk : constant Entity_Id := Scope (E);
6627 Lo : constant Node_Id := Type_Low_Bound (Typ);
6628 Hi : constant Node_Id := Type_High_Bound (Typ);
6629 New_T : Entity_Id;
6630
6631 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
6632 -- If the bound is given by a discriminant, replace with a reference
6633 -- to the discriminant of the same name in the target task. If the
6634 -- entry name is the target of a requeue statement and the entry is
6635 -- in the current protected object, the bound to be used is the
6636 -- discriminal of the object (see Apply_Range_Checks for details of
6637 -- the transformation).
6638
6639 -----------------------------
6640 -- Actual_Discriminant_Ref --
6641 -----------------------------
6642
6643 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
6644 Typ : constant Entity_Id := Etype (Bound);
6645 Ref : Node_Id;
6646
6647 begin
6648 Remove_Side_Effects (Bound);
6649
6650 if not Is_Entity_Name (Bound)
6651 or else Ekind (Entity (Bound)) /= E_Discriminant
6652 then
6653 return Bound;
6654
6655 elsif Is_Protected_Type (Tsk)
6656 and then In_Open_Scopes (Tsk)
6657 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
6658 then
6659 -- Note: here Bound denotes a discriminant of the corresponding
6660 -- record type tskV, whose discriminal is a formal of the
6661 -- init-proc tskVIP. What we want is the body discriminal,
6662 -- which is associated to the discriminant of the original
6663 -- concurrent type tsk.
6664
6665 return New_Occurrence_Of
6666 (Find_Body_Discriminal (Entity (Bound)), Loc);
6667
6668 else
6669 Ref :=
6670 Make_Selected_Component (Loc,
6671 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
6672 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
6673 Analyze (Ref);
6674 Resolve (Ref, Typ);
6675 return Ref;
6676 end if;
6677 end Actual_Discriminant_Ref;
6678
6679 -- Start of processing for Actual_Index_Type
6680
6681 begin
6682 if not Has_Discriminants (Tsk)
6683 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
6684 then
6685 return Entry_Index_Type (E);
6686
6687 else
6688 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
6689 Set_Etype (New_T, Base_Type (Typ));
6690 Set_Size_Info (New_T, Typ);
6691 Set_RM_Size (New_T, RM_Size (Typ));
6692 Set_Scalar_Range (New_T,
6693 Make_Range (Sloc (Entry_Name),
6694 Low_Bound => Actual_Discriminant_Ref (Lo),
6695 High_Bound => Actual_Discriminant_Ref (Hi)));
6696
6697 return New_T;
6698 end if;
6699 end Actual_Index_Type;
6700
6701 -- Start of processing of Resolve_Entry
6702
6703 begin
6704 -- Find name of entry being called, and resolve prefix of name with its
6705 -- own type. The prefix can be overloaded, and the name and signature of
6706 -- the entry must be taken into account.
6707
6708 if Nkind (Entry_Name) = N_Indexed_Component then
6709
6710 -- Case of dealing with entry family within the current tasks
6711
6712 E_Name := Prefix (Entry_Name);
6713
6714 else
6715 E_Name := Entry_Name;
6716 end if;
6717
6718 if Is_Entity_Name (E_Name) then
6719
6720 -- Entry call to an entry (or entry family) in the current task. This
6721 -- is legal even though the task will deadlock. Rewrite as call to
6722 -- current task.
6723
6724 -- This can also be a call to an entry in an enclosing task. If this
6725 -- is a single task, we have to retrieve its name, because the scope
6726 -- of the entry is the task type, not the object. If the enclosing
6727 -- task is a task type, the identity of the task is given by its own
6728 -- self variable.
6729
6730 -- Finally this can be a requeue on an entry of the same task or
6731 -- protected object.
6732
6733 S := Scope (Entity (E_Name));
6734
6735 for J in reverse 0 .. Scope_Stack.Last loop
6736 if Is_Task_Type (Scope_Stack.Table (J).Entity)
6737 and then not Comes_From_Source (S)
6738 then
6739 -- S is an enclosing task or protected object. The concurrent
6740 -- declaration has been converted into a type declaration, and
6741 -- the object itself has an object declaration that follows
6742 -- the type in the same declarative part.
6743
6744 Tsk := Next_Entity (S);
6745 while Etype (Tsk) /= S loop
6746 Next_Entity (Tsk);
6747 end loop;
6748
6749 S := Tsk;
6750 exit;
6751
6752 elsif S = Scope_Stack.Table (J).Entity then
6753
6754 -- Call to current task. Will be transformed into call to Self
6755
6756 exit;
6757
6758 end if;
6759 end loop;
6760
6761 New_N :=
6762 Make_Selected_Component (Loc,
6763 Prefix => New_Occurrence_Of (S, Loc),
6764 Selector_Name =>
6765 New_Occurrence_Of (Entity (E_Name), Loc));
6766 Rewrite (E_Name, New_N);
6767 Analyze (E_Name);
6768
6769 elsif Nkind (Entry_Name) = N_Selected_Component
6770 and then Is_Overloaded (Prefix (Entry_Name))
6771 then
6772 -- Use the entry name (which must be unique at this point) to find
6773 -- the prefix that returns the corresponding task/protected type.
6774
6775 declare
6776 Pref : constant Node_Id := Prefix (Entry_Name);
6777 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
6778 I : Interp_Index;
6779 It : Interp;
6780
6781 begin
6782 Get_First_Interp (Pref, I, It);
6783 while Present (It.Typ) loop
6784 if Scope (Ent) = It.Typ then
6785 Set_Etype (Pref, It.Typ);
6786 exit;
6787 end if;
6788
6789 Get_Next_Interp (I, It);
6790 end loop;
6791 end;
6792 end if;
6793
6794 if Nkind (Entry_Name) = N_Selected_Component then
6795 Resolve (Prefix (Entry_Name));
6796
6797 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6798 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6799 Resolve (Prefix (Prefix (Entry_Name)));
6800 Index := First (Expressions (Entry_Name));
6801 Resolve (Index, Entry_Index_Type (Nam));
6802
6803 -- Up to this point the expression could have been the actual in a
6804 -- simple entry call, and be given by a named association.
6805
6806 if Nkind (Index) = N_Parameter_Association then
6807 Error_Msg_N ("expect expression for entry index", Index);
6808 else
6809 Apply_Range_Check (Index, Actual_Index_Type (Nam));
6810 end if;
6811 end if;
6812 end Resolve_Entry;
6813
6814 ------------------------
6815 -- Resolve_Entry_Call --
6816 ------------------------
6817
6818 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
6819 Entry_Name : constant Node_Id := Name (N);
6820 Loc : constant Source_Ptr := Sloc (Entry_Name);
6821 Actuals : List_Id;
6822 First_Named : Node_Id;
6823 Nam : Entity_Id;
6824 Norm_OK : Boolean;
6825 Obj : Node_Id;
6826 Was_Over : Boolean;
6827
6828 begin
6829 -- We kill all checks here, because it does not seem worth the effort to
6830 -- do anything better, an entry call is a big operation.
6831
6832 Kill_All_Checks;
6833
6834 -- Processing of the name is similar for entry calls and protected
6835 -- operation calls. Once the entity is determined, we can complete
6836 -- the resolution of the actuals.
6837
6838 -- The selector may be overloaded, in the case of a protected object
6839 -- with overloaded functions. The type of the context is used for
6840 -- resolution.
6841
6842 if Nkind (Entry_Name) = N_Selected_Component
6843 and then Is_Overloaded (Selector_Name (Entry_Name))
6844 and then Typ /= Standard_Void_Type
6845 then
6846 declare
6847 I : Interp_Index;
6848 It : Interp;
6849
6850 begin
6851 Get_First_Interp (Selector_Name (Entry_Name), I, It);
6852 while Present (It.Typ) loop
6853 if Covers (Typ, It.Typ) then
6854 Set_Entity (Selector_Name (Entry_Name), It.Nam);
6855 Set_Etype (Entry_Name, It.Typ);
6856
6857 Generate_Reference (It.Typ, N, ' ');
6858 end if;
6859
6860 Get_Next_Interp (I, It);
6861 end loop;
6862 end;
6863 end if;
6864
6865 Resolve_Entry (Entry_Name);
6866
6867 if Nkind (Entry_Name) = N_Selected_Component then
6868
6869 -- Simple entry call
6870
6871 Nam := Entity (Selector_Name (Entry_Name));
6872 Obj := Prefix (Entry_Name);
6873 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
6874
6875 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6876
6877 -- Call to member of entry family
6878
6879 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6880 Obj := Prefix (Prefix (Entry_Name));
6881 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
6882 end if;
6883
6884 -- We cannot in general check the maximum depth of protected entry calls
6885 -- at compile time. But we can tell that any protected entry call at all
6886 -- violates a specified nesting depth of zero.
6887
6888 if Is_Protected_Type (Scope (Nam)) then
6889 Check_Restriction (Max_Entry_Queue_Length, N);
6890 end if;
6891
6892 -- Use context type to disambiguate a protected function that can be
6893 -- called without actuals and that returns an array type, and where the
6894 -- argument list may be an indexing of the returned value.
6895
6896 if Ekind (Nam) = E_Function
6897 and then Needs_No_Actuals (Nam)
6898 and then Present (Parameter_Associations (N))
6899 and then
6900 ((Is_Array_Type (Etype (Nam))
6901 and then Covers (Typ, Component_Type (Etype (Nam))))
6902
6903 or else (Is_Access_Type (Etype (Nam))
6904 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6905 and then
6906 Covers
6907 (Typ,
6908 Component_Type (Designated_Type (Etype (Nam))))))
6909 then
6910 declare
6911 Index_Node : Node_Id;
6912
6913 begin
6914 Index_Node :=
6915 Make_Indexed_Component (Loc,
6916 Prefix =>
6917 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
6918 Expressions => Parameter_Associations (N));
6919
6920 -- Since we are correcting a node classification error made by the
6921 -- parser, we call Replace rather than Rewrite.
6922
6923 Replace (N, Index_Node);
6924 Set_Etype (Prefix (N), Etype (Nam));
6925 Set_Etype (N, Typ);
6926 Resolve_Indexed_Component (N, Typ);
6927 return;
6928 end;
6929 end if;
6930
6931 if Ekind_In (Nam, E_Entry, E_Entry_Family)
6932 and then Present (PPC_Wrapper (Nam))
6933 and then Current_Scope /= PPC_Wrapper (Nam)
6934 then
6935 -- Rewrite as call to the precondition wrapper, adding the task
6936 -- object to the list of actuals. If the call is to a member of an
6937 -- entry family, include the index as well.
6938
6939 declare
6940 New_Call : Node_Id;
6941 New_Actuals : List_Id;
6942
6943 begin
6944 New_Actuals := New_List (Obj);
6945
6946 if Nkind (Entry_Name) = N_Indexed_Component then
6947 Append_To (New_Actuals,
6948 New_Copy_Tree (First (Expressions (Entry_Name))));
6949 end if;
6950
6951 Append_List (Parameter_Associations (N), New_Actuals);
6952 New_Call :=
6953 Make_Procedure_Call_Statement (Loc,
6954 Name =>
6955 New_Occurrence_Of (PPC_Wrapper (Nam), Loc),
6956 Parameter_Associations => New_Actuals);
6957 Rewrite (N, New_Call);
6958 Analyze_And_Resolve (N);
6959 return;
6960 end;
6961 end if;
6962
6963 -- The operation name may have been overloaded. Order the actuals
6964 -- according to the formals of the resolved entity, and set the return
6965 -- type to that of the operation.
6966
6967 if Was_Over then
6968 Normalize_Actuals (N, Nam, False, Norm_OK);
6969 pragma Assert (Norm_OK);
6970 Set_Etype (N, Etype (Nam));
6971 end if;
6972
6973 Resolve_Actuals (N, Nam);
6974 Check_Internal_Protected_Use (N, Nam);
6975
6976 -- Create a call reference to the entry
6977
6978 Generate_Reference (Nam, Entry_Name, 's');
6979
6980 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
6981 Check_Potentially_Blocking_Operation (N);
6982 end if;
6983
6984 -- Verify that a procedure call cannot masquerade as an entry
6985 -- call where an entry call is expected.
6986
6987 if Ekind (Nam) = E_Procedure then
6988 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6989 and then N = Entry_Call_Statement (Parent (N))
6990 then
6991 Error_Msg_N ("entry call required in select statement", N);
6992
6993 elsif Nkind (Parent (N)) = N_Triggering_Alternative
6994 and then N = Triggering_Statement (Parent (N))
6995 then
6996 Error_Msg_N ("triggering statement cannot be procedure call", N);
6997
6998 elsif Ekind (Scope (Nam)) = E_Task_Type
6999 and then not In_Open_Scopes (Scope (Nam))
7000 then
7001 Error_Msg_N ("task has no entry with this name", Entry_Name);
7002 end if;
7003 end if;
7004
7005 -- After resolution, entry calls and protected procedure calls are
7006 -- changed into entry calls, for expansion. The structure of the node
7007 -- does not change, so it can safely be done in place. Protected
7008 -- function calls must keep their structure because they are
7009 -- subexpressions.
7010
7011 if Ekind (Nam) /= E_Function then
7012
7013 -- A protected operation that is not a function may modify the
7014 -- corresponding object, and cannot apply to a constant. If this
7015 -- is an internal call, the prefix is the type itself.
7016
7017 if Is_Protected_Type (Scope (Nam))
7018 and then not Is_Variable (Obj)
7019 and then (not Is_Entity_Name (Obj)
7020 or else not Is_Type (Entity (Obj)))
7021 then
7022 Error_Msg_N
7023 ("prefix of protected procedure or entry call must be variable",
7024 Entry_Name);
7025 end if;
7026
7027 Actuals := Parameter_Associations (N);
7028 First_Named := First_Named_Actual (N);
7029
7030 Rewrite (N,
7031 Make_Entry_Call_Statement (Loc,
7032 Name => Entry_Name,
7033 Parameter_Associations => Actuals));
7034
7035 Set_First_Named_Actual (N, First_Named);
7036 Set_Analyzed (N, True);
7037
7038 -- Protected functions can return on the secondary stack, in which
7039 -- case we must trigger the transient scope mechanism.
7040
7041 elsif Expander_Active
7042 and then Requires_Transient_Scope (Etype (Nam))
7043 then
7044 Establish_Transient_Scope (N, Sec_Stack => True);
7045 end if;
7046 end Resolve_Entry_Call;
7047
7048 -------------------------
7049 -- Resolve_Equality_Op --
7050 -------------------------
7051
7052 -- Both arguments must have the same type, and the boolean context does
7053 -- not participate in the resolution. The first pass verifies that the
7054 -- interpretation is not ambiguous, and the type of the left argument is
7055 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
7056 -- are strings or aggregates, allocators, or Null, they are ambiguous even
7057 -- though they carry a single (universal) type. Diagnose this case here.
7058
7059 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
7060 L : constant Node_Id := Left_Opnd (N);
7061 R : constant Node_Id := Right_Opnd (N);
7062 T : Entity_Id := Find_Unique_Type (L, R);
7063
7064 procedure Check_If_Expression (Cond : Node_Id);
7065 -- The resolution rule for if expressions requires that each such must
7066 -- have a unique type. This means that if several dependent expressions
7067 -- are of a non-null anonymous access type, and the context does not
7068 -- impose an expected type (as can be the case in an equality operation)
7069 -- the expression must be rejected.
7070
7071 procedure Explain_Redundancy (N : Node_Id);
7072 -- Attempt to explain the nature of a redundant comparison with True. If
7073 -- the expression N is too complex, this routine issues a general error
7074 -- message.
7075
7076 function Find_Unique_Access_Type return Entity_Id;
7077 -- In the case of allocators and access attributes, the context must
7078 -- provide an indication of the specific access type to be used. If
7079 -- one operand is of such a "generic" access type, check whether there
7080 -- is a specific visible access type that has the same designated type.
7081 -- This is semantically dubious, and of no interest to any real code,
7082 -- but c48008a makes it all worthwhile.
7083
7084 -------------------------
7085 -- Check_If_Expression --
7086 -------------------------
7087
7088 procedure Check_If_Expression (Cond : Node_Id) is
7089 Then_Expr : Node_Id;
7090 Else_Expr : Node_Id;
7091
7092 begin
7093 if Nkind (Cond) = N_If_Expression then
7094 Then_Expr := Next (First (Expressions (Cond)));
7095 Else_Expr := Next (Then_Expr);
7096
7097 if Nkind (Then_Expr) /= N_Null
7098 and then Nkind (Else_Expr) /= N_Null
7099 then
7100 Error_Msg_N ("cannot determine type of if expression", Cond);
7101 end if;
7102 end if;
7103 end Check_If_Expression;
7104
7105 ------------------------
7106 -- Explain_Redundancy --
7107 ------------------------
7108
7109 procedure Explain_Redundancy (N : Node_Id) is
7110 Error : Name_Id;
7111 Val : Node_Id;
7112 Val_Id : Entity_Id;
7113
7114 begin
7115 Val := N;
7116
7117 -- Strip the operand down to an entity
7118
7119 loop
7120 if Nkind (Val) = N_Selected_Component then
7121 Val := Selector_Name (Val);
7122 else
7123 exit;
7124 end if;
7125 end loop;
7126
7127 -- The construct denotes an entity
7128
7129 if Is_Entity_Name (Val) and then Present (Entity (Val)) then
7130 Val_Id := Entity (Val);
7131
7132 -- Do not generate an error message when the comparison is done
7133 -- against the enumeration literal Standard.True.
7134
7135 if Ekind (Val_Id) /= E_Enumeration_Literal then
7136
7137 -- Build a customized error message
7138
7139 Name_Len := 0;
7140 Add_Str_To_Name_Buffer ("?r?");
7141
7142 if Ekind (Val_Id) = E_Component then
7143 Add_Str_To_Name_Buffer ("component ");
7144
7145 elsif Ekind (Val_Id) = E_Constant then
7146 Add_Str_To_Name_Buffer ("constant ");
7147
7148 elsif Ekind (Val_Id) = E_Discriminant then
7149 Add_Str_To_Name_Buffer ("discriminant ");
7150
7151 elsif Is_Formal (Val_Id) then
7152 Add_Str_To_Name_Buffer ("parameter ");
7153
7154 elsif Ekind (Val_Id) = E_Variable then
7155 Add_Str_To_Name_Buffer ("variable ");
7156 end if;
7157
7158 Add_Str_To_Name_Buffer ("& is always True!");
7159 Error := Name_Find;
7160
7161 Error_Msg_NE (Get_Name_String (Error), Val, Val_Id);
7162 end if;
7163
7164 -- The construct is too complex to disect, issue a general message
7165
7166 else
7167 Error_Msg_N ("?r?expression is always True!", Val);
7168 end if;
7169 end Explain_Redundancy;
7170
7171 -----------------------------
7172 -- Find_Unique_Access_Type --
7173 -----------------------------
7174
7175 function Find_Unique_Access_Type return Entity_Id is
7176 Acc : Entity_Id;
7177 E : Entity_Id;
7178 S : Entity_Id;
7179
7180 begin
7181 if Ekind_In (Etype (R), E_Allocator_Type,
7182 E_Access_Attribute_Type)
7183 then
7184 Acc := Designated_Type (Etype (R));
7185
7186 elsif Ekind_In (Etype (L), E_Allocator_Type,
7187 E_Access_Attribute_Type)
7188 then
7189 Acc := Designated_Type (Etype (L));
7190 else
7191 return Empty;
7192 end if;
7193
7194 S := Current_Scope;
7195 while S /= Standard_Standard loop
7196 E := First_Entity (S);
7197 while Present (E) loop
7198 if Is_Type (E)
7199 and then Is_Access_Type (E)
7200 and then Ekind (E) /= E_Allocator_Type
7201 and then Designated_Type (E) = Base_Type (Acc)
7202 then
7203 return E;
7204 end if;
7205
7206 Next_Entity (E);
7207 end loop;
7208
7209 S := Scope (S);
7210 end loop;
7211
7212 return Empty;
7213 end Find_Unique_Access_Type;
7214
7215 -- Start of processing for Resolve_Equality_Op
7216
7217 begin
7218 Set_Etype (N, Base_Type (Typ));
7219 Generate_Reference (T, N, ' ');
7220
7221 if T = Any_Fixed then
7222 T := Unique_Fixed_Point_Type (L);
7223 end if;
7224
7225 if T /= Any_Type then
7226 if T = Any_String or else
7227 T = Any_Composite or else
7228 T = Any_Character
7229 then
7230 if T = Any_Character then
7231 Ambiguous_Character (L);
7232 else
7233 Error_Msg_N ("ambiguous operands for equality", N);
7234 end if;
7235
7236 Set_Etype (N, Any_Type);
7237 return;
7238
7239 elsif T = Any_Access
7240 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
7241 then
7242 T := Find_Unique_Access_Type;
7243
7244 if No (T) then
7245 Error_Msg_N ("ambiguous operands for equality", N);
7246 Set_Etype (N, Any_Type);
7247 return;
7248 end if;
7249
7250 -- If expressions must have a single type, and if the context does
7251 -- not impose one the dependent expressions cannot be anonymous
7252 -- access types.
7253
7254 -- Why no similar processing for case expressions???
7255
7256 elsif Ada_Version >= Ada_2012
7257 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
7258 E_Anonymous_Access_Subprogram_Type)
7259 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
7260 E_Anonymous_Access_Subprogram_Type)
7261 then
7262 Check_If_Expression (L);
7263 Check_If_Expression (R);
7264 end if;
7265
7266 Resolve (L, T);
7267 Resolve (R, T);
7268
7269 -- In SPARK, equality operators = and /= for array types other than
7270 -- String are only defined when, for each index position, the
7271 -- operands have equal static bounds.
7272
7273 if Is_Array_Type (T) then
7274
7275 -- Protect call to Matching_Static_Array_Bounds to avoid costly
7276 -- operation if not needed.
7277
7278 if Restriction_Check_Required (SPARK_05)
7279 and then Base_Type (T) /= Standard_String
7280 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
7281 and then Etype (L) /= Any_Composite -- or else L in error
7282 and then Etype (R) /= Any_Composite -- or else R in error
7283 and then not Matching_Static_Array_Bounds (Etype (L), Etype (R))
7284 then
7285 Check_SPARK_Restriction
7286 ("array types should have matching static bounds", N);
7287 end if;
7288 end if;
7289
7290 -- If the unique type is a class-wide type then it will be expanded
7291 -- into a dispatching call to the predefined primitive. Therefore we
7292 -- check here for potential violation of such restriction.
7293
7294 if Is_Class_Wide_Type (T) then
7295 Check_Restriction (No_Dispatching_Calls, N);
7296 end if;
7297
7298 if Warn_On_Redundant_Constructs
7299 and then Comes_From_Source (N)
7300 and then Comes_From_Source (R)
7301 and then Is_Entity_Name (R)
7302 and then Entity (R) = Standard_True
7303 then
7304 Error_Msg_N -- CODEFIX
7305 ("?r?comparison with True is redundant!", N);
7306 Explain_Redundancy (Original_Node (R));
7307 end if;
7308
7309 Check_Unset_Reference (L);
7310 Check_Unset_Reference (R);
7311 Generate_Operator_Reference (N, T);
7312 Check_Low_Bound_Tested (N);
7313
7314 -- If this is an inequality, it may be the implicit inequality
7315 -- created for a user-defined operation, in which case the corres-
7316 -- ponding equality operation is not intrinsic, and the operation
7317 -- cannot be constant-folded. Else fold.
7318
7319 if Nkind (N) = N_Op_Eq
7320 or else Comes_From_Source (Entity (N))
7321 or else Ekind (Entity (N)) = E_Operator
7322 or else Is_Intrinsic_Subprogram
7323 (Corresponding_Equality (Entity (N)))
7324 then
7325 Analyze_Dimension (N);
7326 Eval_Relational_Op (N);
7327
7328 elsif Nkind (N) = N_Op_Ne
7329 and then Is_Abstract_Subprogram (Entity (N))
7330 then
7331 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
7332 end if;
7333
7334 -- Ada 2005: If one operand is an anonymous access type, convert the
7335 -- other operand to it, to ensure that the underlying types match in
7336 -- the back-end. Same for access_to_subprogram, and the conversion
7337 -- verifies that the types are subtype conformant.
7338
7339 -- We apply the same conversion in the case one of the operands is a
7340 -- private subtype of the type of the other.
7341
7342 -- Why the Expander_Active test here ???
7343
7344 if Expander_Active
7345 and then
7346 (Ekind_In (T, E_Anonymous_Access_Type,
7347 E_Anonymous_Access_Subprogram_Type)
7348 or else Is_Private_Type (T))
7349 then
7350 if Etype (L) /= T then
7351 Rewrite (L,
7352 Make_Unchecked_Type_Conversion (Sloc (L),
7353 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
7354 Expression => Relocate_Node (L)));
7355 Analyze_And_Resolve (L, T);
7356 end if;
7357
7358 if (Etype (R)) /= T then
7359 Rewrite (R,
7360 Make_Unchecked_Type_Conversion (Sloc (R),
7361 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
7362 Expression => Relocate_Node (R)));
7363 Analyze_And_Resolve (R, T);
7364 end if;
7365 end if;
7366 end if;
7367 end Resolve_Equality_Op;
7368
7369 ----------------------------------
7370 -- Resolve_Explicit_Dereference --
7371 ----------------------------------
7372
7373 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
7374 Loc : constant Source_Ptr := Sloc (N);
7375 New_N : Node_Id;
7376 P : constant Node_Id := Prefix (N);
7377
7378 P_Typ : Entity_Id;
7379 -- The candidate prefix type, if overloaded
7380
7381 I : Interp_Index;
7382 It : Interp;
7383
7384 begin
7385 Check_Fully_Declared_Prefix (Typ, P);
7386 P_Typ := Empty;
7387
7388 -- A useful optimization: check whether the dereference denotes an
7389 -- element of a container, and if so rewrite it as a call to the
7390 -- corresponding Element function.
7391
7392 -- Disabled for now, on advice of ARG. A more restricted form of the
7393 -- predicate might be acceptable ???
7394
7395 -- if Is_Container_Element (N) then
7396 -- return;
7397 -- end if;
7398
7399 if Is_Overloaded (P) then
7400
7401 -- Use the context type to select the prefix that has the correct
7402 -- designated type. Keep the first match, which will be the inner-
7403 -- most.
7404
7405 Get_First_Interp (P, I, It);
7406
7407 while Present (It.Typ) loop
7408 if Is_Access_Type (It.Typ)
7409 and then Covers (Typ, Designated_Type (It.Typ))
7410 then
7411 if No (P_Typ) then
7412 P_Typ := It.Typ;
7413 end if;
7414
7415 -- Remove access types that do not match, but preserve access
7416 -- to subprogram interpretations, in case a further dereference
7417 -- is needed (see below).
7418
7419 elsif Ekind (It.Typ) /= E_Access_Subprogram_Type then
7420 Remove_Interp (I);
7421 end if;
7422
7423 Get_Next_Interp (I, It);
7424 end loop;
7425
7426 if Present (P_Typ) then
7427 Resolve (P, P_Typ);
7428 Set_Etype (N, Designated_Type (P_Typ));
7429
7430 else
7431 -- If no interpretation covers the designated type of the prefix,
7432 -- this is the pathological case where not all implementations of
7433 -- the prefix allow the interpretation of the node as a call. Now
7434 -- that the expected type is known, Remove other interpretations
7435 -- from prefix, rewrite it as a call, and resolve again, so that
7436 -- the proper call node is generated.
7437
7438 Get_First_Interp (P, I, It);
7439 while Present (It.Typ) loop
7440 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
7441 Remove_Interp (I);
7442 end if;
7443
7444 Get_Next_Interp (I, It);
7445 end loop;
7446
7447 New_N :=
7448 Make_Function_Call (Loc,
7449 Name =>
7450 Make_Explicit_Dereference (Loc,
7451 Prefix => P),
7452 Parameter_Associations => New_List);
7453
7454 Save_Interps (N, New_N);
7455 Rewrite (N, New_N);
7456 Analyze_And_Resolve (N, Typ);
7457 return;
7458 end if;
7459
7460 -- If not overloaded, resolve P with its own type
7461
7462 else
7463 Resolve (P);
7464 end if;
7465
7466 if Is_Access_Type (Etype (P)) then
7467 Apply_Access_Check (N);
7468 end if;
7469
7470 -- If the designated type is a packed unconstrained array type, and the
7471 -- explicit dereference is not in the context of an attribute reference,
7472 -- then we must compute and set the actual subtype, since it is needed
7473 -- by Gigi. The reason we exclude the attribute case is that this is
7474 -- handled fine by Gigi, and in fact we use such attributes to build the
7475 -- actual subtype. We also exclude generated code (which builds actual
7476 -- subtypes directly if they are needed).
7477
7478 if Is_Array_Type (Etype (N))
7479 and then Is_Packed (Etype (N))
7480 and then not Is_Constrained (Etype (N))
7481 and then Nkind (Parent (N)) /= N_Attribute_Reference
7482 and then Comes_From_Source (N)
7483 then
7484 Set_Etype (N, Get_Actual_Subtype (N));
7485 end if;
7486
7487 -- Note: No Eval processing is required for an explicit dereference,
7488 -- because such a name can never be static.
7489
7490 end Resolve_Explicit_Dereference;
7491
7492 -------------------------------------
7493 -- Resolve_Expression_With_Actions --
7494 -------------------------------------
7495
7496 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
7497 begin
7498 Set_Etype (N, Typ);
7499
7500 -- If N has no actions, and its expression has been constant folded,
7501 -- then rewrite N as just its expression. Note, we can't do this in
7502 -- the general case of Is_Empty_List (Actions (N)) as this would cause
7503 -- Expression (N) to be expanded again.
7504
7505 if Is_Empty_List (Actions (N))
7506 and then Compile_Time_Known_Value (Expression (N))
7507 then
7508 Rewrite (N, Expression (N));
7509 end if;
7510 end Resolve_Expression_With_Actions;
7511
7512 ----------------------------------
7513 -- Resolve_Generalized_Indexing --
7514 ----------------------------------
7515
7516 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id) is
7517 Indexing : constant Node_Id := Generalized_Indexing (N);
7518 Call : Node_Id;
7519 Indices : List_Id;
7520 Pref : Node_Id;
7521
7522 begin
7523 -- In ASIS mode, propagate the information about the indices back to
7524 -- to the original indexing node. The generalized indexing is either
7525 -- a function call, or a dereference of one. The actuals include the
7526 -- prefix of the original node, which is the container expression.
7527
7528 if ASIS_Mode then
7529 Resolve (Indexing, Typ);
7530 Set_Etype (N, Etype (Indexing));
7531 Set_Is_Overloaded (N, False);
7532
7533 Call := Indexing;
7534 while Nkind_In (Call, N_Explicit_Dereference, N_Selected_Component)
7535 loop
7536 Call := Prefix (Call);
7537 end loop;
7538
7539 if Nkind (Call) = N_Function_Call then
7540 Indices := Parameter_Associations (Call);
7541 Pref := Remove_Head (Indices);
7542 Set_Expressions (N, Indices);
7543 Set_Prefix (N, Pref);
7544 end if;
7545
7546 else
7547 Rewrite (N, Indexing);
7548 Resolve (N, Typ);
7549 end if;
7550 end Resolve_Generalized_Indexing;
7551
7552 ---------------------------
7553 -- Resolve_If_Expression --
7554 ---------------------------
7555
7556 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id) is
7557 Condition : constant Node_Id := First (Expressions (N));
7558 Then_Expr : constant Node_Id := Next (Condition);
7559 Else_Expr : Node_Id := Next (Then_Expr);
7560 Else_Typ : Entity_Id;
7561 Then_Typ : Entity_Id;
7562
7563 begin
7564 Resolve (Condition, Any_Boolean);
7565 Resolve (Then_Expr, Typ);
7566 Then_Typ := Etype (Then_Expr);
7567
7568 -- When the "then" expression is of a scalar subtype different from the
7569 -- result subtype, then insert a conversion to ensure the generation of
7570 -- a constraint check. The same is done for the else part below, again
7571 -- comparing subtypes rather than base types.
7572
7573 if Is_Scalar_Type (Then_Typ)
7574 and then Then_Typ /= Typ
7575 then
7576 Rewrite (Then_Expr, Convert_To (Typ, Then_Expr));
7577 Analyze_And_Resolve (Then_Expr, Typ);
7578 end if;
7579
7580 -- If ELSE expression present, just resolve using the determined type
7581
7582 if Present (Else_Expr) then
7583 Resolve (Else_Expr, Typ);
7584 Else_Typ := Etype (Else_Expr);
7585
7586 if Is_Scalar_Type (Else_Typ)
7587 and then Else_Typ /= Typ
7588 then
7589 Rewrite (Else_Expr, Convert_To (Typ, Else_Expr));
7590 Analyze_And_Resolve (Else_Expr, Typ);
7591 end if;
7592
7593 -- If no ELSE expression is present, root type must be Standard.Boolean
7594 -- and we provide a Standard.True result converted to the appropriate
7595 -- Boolean type (in case it is a derived boolean type).
7596
7597 elsif Root_Type (Typ) = Standard_Boolean then
7598 Else_Expr :=
7599 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
7600 Analyze_And_Resolve (Else_Expr, Typ);
7601 Append_To (Expressions (N), Else_Expr);
7602
7603 else
7604 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
7605 Append_To (Expressions (N), Error);
7606 end if;
7607
7608 Set_Etype (N, Typ);
7609 Eval_If_Expression (N);
7610 end Resolve_If_Expression;
7611
7612 -------------------------------
7613 -- Resolve_Indexed_Component --
7614 -------------------------------
7615
7616 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
7617 Name : constant Node_Id := Prefix (N);
7618 Expr : Node_Id;
7619 Array_Type : Entity_Id := Empty; -- to prevent junk warning
7620 Index : Node_Id;
7621
7622 begin
7623 if Present (Generalized_Indexing (N)) then
7624 Resolve_Generalized_Indexing (N, Typ);
7625 return;
7626 end if;
7627
7628 if Is_Overloaded (Name) then
7629
7630 -- Use the context type to select the prefix that yields the correct
7631 -- component type.
7632
7633 declare
7634 I : Interp_Index;
7635 It : Interp;
7636 I1 : Interp_Index := 0;
7637 P : constant Node_Id := Prefix (N);
7638 Found : Boolean := False;
7639
7640 begin
7641 Get_First_Interp (P, I, It);
7642 while Present (It.Typ) loop
7643 if (Is_Array_Type (It.Typ)
7644 and then Covers (Typ, Component_Type (It.Typ)))
7645 or else (Is_Access_Type (It.Typ)
7646 and then Is_Array_Type (Designated_Type (It.Typ))
7647 and then
7648 Covers
7649 (Typ,
7650 Component_Type (Designated_Type (It.Typ))))
7651 then
7652 if Found then
7653 It := Disambiguate (P, I1, I, Any_Type);
7654
7655 if It = No_Interp then
7656 Error_Msg_N ("ambiguous prefix for indexing", N);
7657 Set_Etype (N, Typ);
7658 return;
7659
7660 else
7661 Found := True;
7662 Array_Type := It.Typ;
7663 I1 := I;
7664 end if;
7665
7666 else
7667 Found := True;
7668 Array_Type := It.Typ;
7669 I1 := I;
7670 end if;
7671 end if;
7672
7673 Get_Next_Interp (I, It);
7674 end loop;
7675 end;
7676
7677 else
7678 Array_Type := Etype (Name);
7679 end if;
7680
7681 Resolve (Name, Array_Type);
7682 Array_Type := Get_Actual_Subtype_If_Available (Name);
7683
7684 -- If prefix is access type, dereference to get real array type.
7685 -- Note: we do not apply an access check because the expander always
7686 -- introduces an explicit dereference, and the check will happen there.
7687
7688 if Is_Access_Type (Array_Type) then
7689 Array_Type := Designated_Type (Array_Type);
7690 end if;
7691
7692 -- If name was overloaded, set component type correctly now
7693 -- If a misplaced call to an entry family (which has no index types)
7694 -- return. Error will be diagnosed from calling context.
7695
7696 if Is_Array_Type (Array_Type) then
7697 Set_Etype (N, Component_Type (Array_Type));
7698 else
7699 return;
7700 end if;
7701
7702 Index := First_Index (Array_Type);
7703 Expr := First (Expressions (N));
7704
7705 -- The prefix may have resolved to a string literal, in which case its
7706 -- etype has a special representation. This is only possible currently
7707 -- if the prefix is a static concatenation, written in functional
7708 -- notation.
7709
7710 if Ekind (Array_Type) = E_String_Literal_Subtype then
7711 Resolve (Expr, Standard_Positive);
7712
7713 else
7714 while Present (Index) and Present (Expr) loop
7715 Resolve (Expr, Etype (Index));
7716 Check_Unset_Reference (Expr);
7717
7718 if Is_Scalar_Type (Etype (Expr)) then
7719 Apply_Scalar_Range_Check (Expr, Etype (Index));
7720 else
7721 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
7722 end if;
7723
7724 Next_Index (Index);
7725 Next (Expr);
7726 end loop;
7727 end if;
7728
7729 Analyze_Dimension (N);
7730
7731 -- Do not generate the warning on suspicious index if we are analyzing
7732 -- package Ada.Tags; otherwise we will report the warning with the
7733 -- Prims_Ptr field of the dispatch table.
7734
7735 if Scope (Etype (Prefix (N))) = Standard_Standard
7736 or else not
7737 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
7738 Ada_Tags)
7739 then
7740 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
7741 Eval_Indexed_Component (N);
7742 end if;
7743
7744 -- If the array type is atomic, and is packed, and we are in a left side
7745 -- context, then this is worth a warning, since we have a situation
7746 -- where the access to the component may cause extra read/writes of
7747 -- the atomic array object, which could be considered unexpected.
7748
7749 if Nkind (N) = N_Indexed_Component
7750 and then (Is_Atomic (Array_Type)
7751 or else (Is_Entity_Name (Prefix (N))
7752 and then Is_Atomic (Entity (Prefix (N)))))
7753 and then Is_Bit_Packed_Array (Array_Type)
7754 and then Is_LHS (N) = Yes
7755 then
7756 Error_Msg_N ("??assignment to component of packed atomic array",
7757 Prefix (N));
7758 Error_Msg_N ("??\may cause unexpected accesses to atomic object",
7759 Prefix (N));
7760 end if;
7761 end Resolve_Indexed_Component;
7762
7763 -----------------------------
7764 -- Resolve_Integer_Literal --
7765 -----------------------------
7766
7767 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
7768 begin
7769 Set_Etype (N, Typ);
7770 Eval_Integer_Literal (N);
7771 end Resolve_Integer_Literal;
7772
7773 --------------------------------
7774 -- Resolve_Intrinsic_Operator --
7775 --------------------------------
7776
7777 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
7778 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7779 Op : Entity_Id;
7780 Orig_Op : constant Entity_Id := Entity (N);
7781 Arg1 : Node_Id;
7782 Arg2 : Node_Id;
7783
7784 function Convert_Operand (Opnd : Node_Id) return Node_Id;
7785 -- If the operand is a literal, it cannot be the expression in a
7786 -- conversion. Use a qualified expression instead.
7787
7788 function Convert_Operand (Opnd : Node_Id) return Node_Id is
7789 Loc : constant Source_Ptr := Sloc (Opnd);
7790 Res : Node_Id;
7791 begin
7792 if Nkind_In (Opnd, N_Integer_Literal, N_Real_Literal) then
7793 Res :=
7794 Make_Qualified_Expression (Loc,
7795 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
7796 Expression => Relocate_Node (Opnd));
7797 Analyze (Res);
7798
7799 else
7800 Res := Unchecked_Convert_To (Btyp, Opnd);
7801 end if;
7802
7803 return Res;
7804 end Convert_Operand;
7805
7806 -- Start of processing for Resolve_Intrinsic_Operator
7807
7808 begin
7809 -- We must preserve the original entity in a generic setting, so that
7810 -- the legality of the operation can be verified in an instance.
7811
7812 if not Expander_Active then
7813 return;
7814 end if;
7815
7816 Op := Entity (N);
7817 while Scope (Op) /= Standard_Standard loop
7818 Op := Homonym (Op);
7819 pragma Assert (Present (Op));
7820 end loop;
7821
7822 Set_Entity (N, Op);
7823 Set_Is_Overloaded (N, False);
7824
7825 -- If the result or operand types are private, rewrite with unchecked
7826 -- conversions on the operands and the result, to expose the proper
7827 -- underlying numeric type.
7828
7829 if Is_Private_Type (Typ)
7830 or else Is_Private_Type (Etype (Left_Opnd (N)))
7831 or else Is_Private_Type (Etype (Right_Opnd (N)))
7832 then
7833 Arg1 := Convert_Operand (Left_Opnd (N));
7834 -- Unchecked_Convert_To (Btyp, Left_Opnd (N));
7835 -- What on earth is this commented out fragment of code???
7836
7837 if Nkind (N) = N_Op_Expon then
7838 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
7839 else
7840 Arg2 := Convert_Operand (Right_Opnd (N));
7841 end if;
7842
7843 if Nkind (Arg1) = N_Type_Conversion then
7844 Save_Interps (Left_Opnd (N), Expression (Arg1));
7845 end if;
7846
7847 if Nkind (Arg2) = N_Type_Conversion then
7848 Save_Interps (Right_Opnd (N), Expression (Arg2));
7849 end if;
7850
7851 Set_Left_Opnd (N, Arg1);
7852 Set_Right_Opnd (N, Arg2);
7853
7854 Set_Etype (N, Btyp);
7855 Rewrite (N, Unchecked_Convert_To (Typ, N));
7856 Resolve (N, Typ);
7857
7858 elsif Typ /= Etype (Left_Opnd (N))
7859 or else Typ /= Etype (Right_Opnd (N))
7860 then
7861 -- Add explicit conversion where needed, and save interpretations in
7862 -- case operands are overloaded. If the context is a VMS operation,
7863 -- assert that the conversion is legal (the operands have the proper
7864 -- types to select the VMS intrinsic). Note that in rare cases the
7865 -- VMS operators may be visible, but the default System is being used
7866 -- and Address is a private type.
7867
7868 Arg1 := Convert_To (Typ, Left_Opnd (N));
7869 Arg2 := Convert_To (Typ, Right_Opnd (N));
7870
7871 if Nkind (Arg1) = N_Type_Conversion then
7872 Save_Interps (Left_Opnd (N), Expression (Arg1));
7873
7874 if Is_VMS_Operator (Orig_Op) then
7875 Set_Conversion_OK (Arg1);
7876 end if;
7877 else
7878 Save_Interps (Left_Opnd (N), Arg1);
7879 end if;
7880
7881 if Nkind (Arg2) = N_Type_Conversion then
7882 Save_Interps (Right_Opnd (N), Expression (Arg2));
7883
7884 if Is_VMS_Operator (Orig_Op) then
7885 Set_Conversion_OK (Arg2);
7886 end if;
7887 else
7888 Save_Interps (Right_Opnd (N), Arg2);
7889 end if;
7890
7891 Rewrite (Left_Opnd (N), Arg1);
7892 Rewrite (Right_Opnd (N), Arg2);
7893 Analyze (Arg1);
7894 Analyze (Arg2);
7895 Resolve_Arithmetic_Op (N, Typ);
7896
7897 else
7898 Resolve_Arithmetic_Op (N, Typ);
7899 end if;
7900 end Resolve_Intrinsic_Operator;
7901
7902 --------------------------------------
7903 -- Resolve_Intrinsic_Unary_Operator --
7904 --------------------------------------
7905
7906 procedure Resolve_Intrinsic_Unary_Operator
7907 (N : Node_Id;
7908 Typ : Entity_Id)
7909 is
7910 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
7911 Op : Entity_Id;
7912 Arg2 : Node_Id;
7913
7914 begin
7915 Op := Entity (N);
7916 while Scope (Op) /= Standard_Standard loop
7917 Op := Homonym (Op);
7918 pragma Assert (Present (Op));
7919 end loop;
7920
7921 Set_Entity (N, Op);
7922
7923 if Is_Private_Type (Typ) then
7924 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
7925 Save_Interps (Right_Opnd (N), Expression (Arg2));
7926
7927 Set_Right_Opnd (N, Arg2);
7928
7929 Set_Etype (N, Btyp);
7930 Rewrite (N, Unchecked_Convert_To (Typ, N));
7931 Resolve (N, Typ);
7932
7933 else
7934 Resolve_Unary_Op (N, Typ);
7935 end if;
7936 end Resolve_Intrinsic_Unary_Operator;
7937
7938 ------------------------
7939 -- Resolve_Logical_Op --
7940 ------------------------
7941
7942 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
7943 B_Typ : Entity_Id;
7944
7945 begin
7946 Check_No_Direct_Boolean_Operators (N);
7947
7948 -- Predefined operations on scalar types yield the base type. On the
7949 -- other hand, logical operations on arrays yield the type of the
7950 -- arguments (and the context).
7951
7952 if Is_Array_Type (Typ) then
7953 B_Typ := Typ;
7954 else
7955 B_Typ := Base_Type (Typ);
7956 end if;
7957
7958 -- OK if this is a VMS-specific intrinsic operation
7959
7960 if Is_VMS_Operator (Entity (N)) then
7961 null;
7962
7963 -- The following test is required because the operands of the operation
7964 -- may be literals, in which case the resulting type appears to be
7965 -- compatible with a signed integer type, when in fact it is compatible
7966 -- only with modular types. If the context itself is universal, the
7967 -- operation is illegal.
7968
7969 elsif not Valid_Boolean_Arg (Typ) then
7970 Error_Msg_N ("invalid context for logical operation", N);
7971 Set_Etype (N, Any_Type);
7972 return;
7973
7974 elsif Typ = Any_Modular then
7975 Error_Msg_N
7976 ("no modular type available in this context", N);
7977 Set_Etype (N, Any_Type);
7978 return;
7979
7980 elsif Is_Modular_Integer_Type (Typ)
7981 and then Etype (Left_Opnd (N)) = Universal_Integer
7982 and then Etype (Right_Opnd (N)) = Universal_Integer
7983 then
7984 Check_For_Visible_Operator (N, B_Typ);
7985 end if;
7986
7987 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
7988 -- is active and the result type is standard Boolean (do not mess with
7989 -- ops that return a nonstandard Boolean type, because something strange
7990 -- is going on).
7991
7992 -- Note: you might expect this replacement to be done during expansion,
7993 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
7994 -- is used, no part of the right operand of an "and" or "or" operator
7995 -- should be executed if the left operand would short-circuit the
7996 -- evaluation of the corresponding "and then" or "or else". If we left
7997 -- the replacement to expansion time, then run-time checks associated
7998 -- with such operands would be evaluated unconditionally, due to being
7999 -- before the condition prior to the rewriting as short-circuit forms
8000 -- during expansion.
8001
8002 if Short_Circuit_And_Or
8003 and then B_Typ = Standard_Boolean
8004 and then Nkind_In (N, N_Op_And, N_Op_Or)
8005 then
8006 if Nkind (N) = N_Op_And then
8007 Rewrite (N,
8008 Make_And_Then (Sloc (N),
8009 Left_Opnd => Relocate_Node (Left_Opnd (N)),
8010 Right_Opnd => Relocate_Node (Right_Opnd (N))));
8011 Analyze_And_Resolve (N, B_Typ);
8012
8013 -- Case of OR changed to OR ELSE
8014
8015 else
8016 Rewrite (N,
8017 Make_Or_Else (Sloc (N),
8018 Left_Opnd => Relocate_Node (Left_Opnd (N)),
8019 Right_Opnd => Relocate_Node (Right_Opnd (N))));
8020 Analyze_And_Resolve (N, B_Typ);
8021 end if;
8022
8023 -- Return now, since analysis of the rewritten ops will take care of
8024 -- other reference bookkeeping and expression folding.
8025
8026 return;
8027 end if;
8028
8029 Resolve (Left_Opnd (N), B_Typ);
8030 Resolve (Right_Opnd (N), B_Typ);
8031
8032 Check_Unset_Reference (Left_Opnd (N));
8033 Check_Unset_Reference (Right_Opnd (N));
8034
8035 Set_Etype (N, B_Typ);
8036 Generate_Operator_Reference (N, B_Typ);
8037 Eval_Logical_Op (N);
8038
8039 -- In SPARK, logical operations AND, OR and XOR for arrays are defined
8040 -- only when both operands have same static lower and higher bounds. Of
8041 -- course the types have to match, so only check if operands are
8042 -- compatible and the node itself has no errors.
8043
8044 if Is_Array_Type (B_Typ)
8045 and then Nkind (N) in N_Binary_Op
8046 then
8047 declare
8048 Left_Typ : constant Node_Id := Etype (Left_Opnd (N));
8049 Right_Typ : constant Node_Id := Etype (Right_Opnd (N));
8050
8051 begin
8052 -- Protect call to Matching_Static_Array_Bounds to avoid costly
8053 -- operation if not needed.
8054
8055 if Restriction_Check_Required (SPARK_05)
8056 and then Base_Type (Left_Typ) = Base_Type (Right_Typ)
8057 and then Left_Typ /= Any_Composite -- or Left_Opnd in error
8058 and then Right_Typ /= Any_Composite -- or Right_Opnd in error
8059 and then not Matching_Static_Array_Bounds (Left_Typ, Right_Typ)
8060 then
8061 Check_SPARK_Restriction
8062 ("array types should have matching static bounds", N);
8063 end if;
8064 end;
8065 end if;
8066
8067 Check_Function_Writable_Actuals (N);
8068 end Resolve_Logical_Op;
8069
8070 ---------------------------
8071 -- Resolve_Membership_Op --
8072 ---------------------------
8073
8074 -- The context can only be a boolean type, and does not determine the
8075 -- arguments. Arguments should be unambiguous, but the preference rule for
8076 -- universal types applies.
8077
8078 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
8079 pragma Warnings (Off, Typ);
8080
8081 L : constant Node_Id := Left_Opnd (N);
8082 R : constant Node_Id := Right_Opnd (N);
8083 T : Entity_Id;
8084
8085 procedure Resolve_Set_Membership;
8086 -- Analysis has determined a unique type for the left operand. Use it to
8087 -- resolve the disjuncts.
8088
8089 ----------------------------
8090 -- Resolve_Set_Membership --
8091 ----------------------------
8092
8093 procedure Resolve_Set_Membership is
8094 Alt : Node_Id;
8095 Ltyp : constant Entity_Id := Etype (L);
8096
8097 begin
8098 Resolve (L, Ltyp);
8099
8100 Alt := First (Alternatives (N));
8101 while Present (Alt) loop
8102
8103 -- Alternative is an expression, a range
8104 -- or a subtype mark.
8105
8106 if not Is_Entity_Name (Alt)
8107 or else not Is_Type (Entity (Alt))
8108 then
8109 Resolve (Alt, Ltyp);
8110 end if;
8111
8112 Next (Alt);
8113 end loop;
8114
8115 -- Check for duplicates for discrete case
8116
8117 if Is_Discrete_Type (Ltyp) then
8118 declare
8119 type Ent is record
8120 Alt : Node_Id;
8121 Val : Uint;
8122 end record;
8123
8124 Alts : array (0 .. List_Length (Alternatives (N))) of Ent;
8125 Nalts : Nat;
8126
8127 begin
8128 -- Loop checking duplicates. This is quadratic, but giant sets
8129 -- are unlikely in this context so it's a reasonable choice.
8130
8131 Nalts := 0;
8132 Alt := First (Alternatives (N));
8133 while Present (Alt) loop
8134 if Is_Static_Expression (Alt)
8135 and then (Nkind_In (Alt, N_Integer_Literal,
8136 N_Character_Literal)
8137 or else Nkind (Alt) in N_Has_Entity)
8138 then
8139 Nalts := Nalts + 1;
8140 Alts (Nalts) := (Alt, Expr_Value (Alt));
8141
8142 for J in 1 .. Nalts - 1 loop
8143 if Alts (J).Val = Alts (Nalts).Val then
8144 Error_Msg_Sloc := Sloc (Alts (J).Alt);
8145 Error_Msg_N ("duplicate of value given#??", Alt);
8146 end if;
8147 end loop;
8148 end if;
8149
8150 Alt := Next (Alt);
8151 end loop;
8152 end;
8153 end if;
8154 end Resolve_Set_Membership;
8155
8156 -- Start of processing for Resolve_Membership_Op
8157
8158 begin
8159 if L = Error or else R = Error then
8160 return;
8161 end if;
8162
8163 if Present (Alternatives (N)) then
8164 Resolve_Set_Membership;
8165 Check_Function_Writable_Actuals (N);
8166 return;
8167
8168 elsif not Is_Overloaded (R)
8169 and then
8170 (Etype (R) = Universal_Integer
8171 or else
8172 Etype (R) = Universal_Real)
8173 and then Is_Overloaded (L)
8174 then
8175 T := Etype (R);
8176
8177 -- Ada 2005 (AI-251): Support the following case:
8178
8179 -- type I is interface;
8180 -- type T is tagged ...
8181
8182 -- function Test (O : I'Class) is
8183 -- begin
8184 -- return O in T'Class.
8185 -- end Test;
8186
8187 -- In this case we have nothing else to do. The membership test will be
8188 -- done at run time.
8189
8190 elsif Ada_Version >= Ada_2005
8191 and then Is_Class_Wide_Type (Etype (L))
8192 and then Is_Interface (Etype (L))
8193 and then Is_Class_Wide_Type (Etype (R))
8194 and then not Is_Interface (Etype (R))
8195 then
8196 return;
8197 else
8198 T := Intersect_Types (L, R);
8199 end if;
8200
8201 -- If mixed-mode operations are present and operands are all literal,
8202 -- the only interpretation involves Duration, which is probably not
8203 -- the intention of the programmer.
8204
8205 if T = Any_Fixed then
8206 T := Unique_Fixed_Point_Type (N);
8207
8208 if T = Any_Type then
8209 return;
8210 end if;
8211 end if;
8212
8213 Resolve (L, T);
8214 Check_Unset_Reference (L);
8215
8216 if Nkind (R) = N_Range
8217 and then not Is_Scalar_Type (T)
8218 then
8219 Error_Msg_N ("scalar type required for range", R);
8220 end if;
8221
8222 if Is_Entity_Name (R) then
8223 Freeze_Expression (R);
8224 else
8225 Resolve (R, T);
8226 Check_Unset_Reference (R);
8227 end if;
8228
8229 Eval_Membership_Op (N);
8230 Check_Function_Writable_Actuals (N);
8231 end Resolve_Membership_Op;
8232
8233 ------------------
8234 -- Resolve_Null --
8235 ------------------
8236
8237 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
8238 Loc : constant Source_Ptr := Sloc (N);
8239
8240 begin
8241 -- Handle restriction against anonymous null access values This
8242 -- restriction can be turned off using -gnatdj.
8243
8244 -- Ada 2005 (AI-231): Remove restriction
8245
8246 if Ada_Version < Ada_2005
8247 and then not Debug_Flag_J
8248 and then Ekind (Typ) = E_Anonymous_Access_Type
8249 and then Comes_From_Source (N)
8250 then
8251 -- In the common case of a call which uses an explicitly null value
8252 -- for an access parameter, give specialized error message.
8253
8254 if Nkind (Parent (N)) in N_Subprogram_Call then
8255 Error_Msg_N
8256 ("null is not allowed as argument for an access parameter", N);
8257
8258 -- Standard message for all other cases (are there any?)
8259
8260 else
8261 Error_Msg_N
8262 ("null cannot be of an anonymous access type", N);
8263 end if;
8264 end if;
8265
8266 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
8267 -- assignment to a null-excluding object
8268
8269 if Ada_Version >= Ada_2005
8270 and then Can_Never_Be_Null (Typ)
8271 and then Nkind (Parent (N)) = N_Assignment_Statement
8272 then
8273 if not Inside_Init_Proc then
8274 Insert_Action
8275 (Compile_Time_Constraint_Error (N,
8276 "(Ada 2005) null not allowed in null-excluding objects??"),
8277 Make_Raise_Constraint_Error (Loc,
8278 Reason => CE_Access_Check_Failed));
8279 else
8280 Insert_Action (N,
8281 Make_Raise_Constraint_Error (Loc,
8282 Reason => CE_Access_Check_Failed));
8283 end if;
8284 end if;
8285
8286 -- In a distributed context, null for a remote access to subprogram may
8287 -- need to be replaced with a special record aggregate. In this case,
8288 -- return after having done the transformation.
8289
8290 if (Ekind (Typ) = E_Record_Type
8291 or else Is_Remote_Access_To_Subprogram_Type (Typ))
8292 and then Remote_AST_Null_Value (N, Typ)
8293 then
8294 return;
8295 end if;
8296
8297 -- The null literal takes its type from the context
8298
8299 Set_Etype (N, Typ);
8300 end Resolve_Null;
8301
8302 -----------------------
8303 -- Resolve_Op_Concat --
8304 -----------------------
8305
8306 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
8307
8308 -- We wish to avoid deep recursion, because concatenations are often
8309 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
8310 -- operands nonrecursively until we find something that is not a simple
8311 -- concatenation (A in this case). We resolve that, and then walk back
8312 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
8313 -- to do the rest of the work at each level. The Parent pointers allow
8314 -- us to avoid recursion, and thus avoid running out of memory. See also
8315 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
8316
8317 NN : Node_Id := N;
8318 Op1 : Node_Id;
8319
8320 begin
8321 -- The following code is equivalent to:
8322
8323 -- Resolve_Op_Concat_First (NN, Typ);
8324 -- Resolve_Op_Concat_Arg (N, ...);
8325 -- Resolve_Op_Concat_Rest (N, Typ);
8326
8327 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
8328 -- operand is a concatenation.
8329
8330 -- Walk down left operands
8331
8332 loop
8333 Resolve_Op_Concat_First (NN, Typ);
8334 Op1 := Left_Opnd (NN);
8335 exit when not (Nkind (Op1) = N_Op_Concat
8336 and then not Is_Array_Type (Component_Type (Typ))
8337 and then Entity (Op1) = Entity (NN));
8338 NN := Op1;
8339 end loop;
8340
8341 -- Now (given the above example) NN is A&B and Op1 is A
8342
8343 -- First resolve Op1 ...
8344
8345 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
8346
8347 -- ... then walk NN back up until we reach N (where we started), calling
8348 -- Resolve_Op_Concat_Rest along the way.
8349
8350 loop
8351 Resolve_Op_Concat_Rest (NN, Typ);
8352 exit when NN = N;
8353 NN := Parent (NN);
8354 end loop;
8355
8356 if Base_Type (Etype (N)) /= Standard_String then
8357 Check_SPARK_Restriction
8358 ("result of concatenation should have type String", N);
8359 end if;
8360 end Resolve_Op_Concat;
8361
8362 ---------------------------
8363 -- Resolve_Op_Concat_Arg --
8364 ---------------------------
8365
8366 procedure Resolve_Op_Concat_Arg
8367 (N : Node_Id;
8368 Arg : Node_Id;
8369 Typ : Entity_Id;
8370 Is_Comp : Boolean)
8371 is
8372 Btyp : constant Entity_Id := Base_Type (Typ);
8373 Ctyp : constant Entity_Id := Component_Type (Typ);
8374
8375 begin
8376 if In_Instance then
8377 if Is_Comp
8378 or else (not Is_Overloaded (Arg)
8379 and then Etype (Arg) /= Any_Composite
8380 and then Covers (Ctyp, Etype (Arg)))
8381 then
8382 Resolve (Arg, Ctyp);
8383 else
8384 Resolve (Arg, Btyp);
8385 end if;
8386
8387 -- If both Array & Array and Array & Component are visible, there is a
8388 -- potential ambiguity that must be reported.
8389
8390 elsif Has_Compatible_Type (Arg, Ctyp) then
8391 if Nkind (Arg) = N_Aggregate
8392 and then Is_Composite_Type (Ctyp)
8393 then
8394 if Is_Private_Type (Ctyp) then
8395 Resolve (Arg, Btyp);
8396
8397 -- If the operation is user-defined and not overloaded use its
8398 -- profile. The operation may be a renaming, in which case it has
8399 -- been rewritten, and we want the original profile.
8400
8401 elsif not Is_Overloaded (N)
8402 and then Comes_From_Source (Entity (Original_Node (N)))
8403 and then Ekind (Entity (Original_Node (N))) = E_Function
8404 then
8405 Resolve (Arg,
8406 Etype
8407 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
8408 return;
8409
8410 -- Otherwise an aggregate may match both the array type and the
8411 -- component type.
8412
8413 else
8414 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
8415 Set_Etype (Arg, Any_Type);
8416 end if;
8417
8418 else
8419 if Is_Overloaded (Arg)
8420 and then Has_Compatible_Type (Arg, Typ)
8421 and then Etype (Arg) /= Any_Type
8422 then
8423 declare
8424 I : Interp_Index;
8425 It : Interp;
8426 Func : Entity_Id;
8427
8428 begin
8429 Get_First_Interp (Arg, I, It);
8430 Func := It.Nam;
8431 Get_Next_Interp (I, It);
8432
8433 -- Special-case the error message when the overloading is
8434 -- caused by a function that yields an array and can be
8435 -- called without parameters.
8436
8437 if It.Nam = Func then
8438 Error_Msg_Sloc := Sloc (Func);
8439 Error_Msg_N ("ambiguous call to function#", Arg);
8440 Error_Msg_NE
8441 ("\\interpretation as call yields&", Arg, Typ);
8442 Error_Msg_NE
8443 ("\\interpretation as indexing of call yields&",
8444 Arg, Component_Type (Typ));
8445
8446 else
8447 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
8448
8449 Get_First_Interp (Arg, I, It);
8450 while Present (It.Nam) loop
8451 Error_Msg_Sloc := Sloc (It.Nam);
8452
8453 if Base_Type (It.Typ) = Btyp
8454 or else
8455 Base_Type (It.Typ) = Base_Type (Ctyp)
8456 then
8457 Error_Msg_N -- CODEFIX
8458 ("\\possible interpretation#", Arg);
8459 end if;
8460
8461 Get_Next_Interp (I, It);
8462 end loop;
8463 end if;
8464 end;
8465 end if;
8466
8467 Resolve (Arg, Component_Type (Typ));
8468
8469 if Nkind (Arg) = N_String_Literal then
8470 Set_Etype (Arg, Component_Type (Typ));
8471 end if;
8472
8473 if Arg = Left_Opnd (N) then
8474 Set_Is_Component_Left_Opnd (N);
8475 else
8476 Set_Is_Component_Right_Opnd (N);
8477 end if;
8478 end if;
8479
8480 else
8481 Resolve (Arg, Btyp);
8482 end if;
8483
8484 -- Concatenation is restricted in SPARK: each operand must be either a
8485 -- string literal, the name of a string constant, a static character or
8486 -- string expression, or another concatenation. Arg cannot be a
8487 -- concatenation here as callers of Resolve_Op_Concat_Arg call it
8488 -- separately on each final operand, past concatenation operations.
8489
8490 if Is_Character_Type (Etype (Arg)) then
8491 if not Is_Static_Expression (Arg) then
8492 Check_SPARK_Restriction
8493 ("character operand for concatenation should be static", Arg);
8494 end if;
8495
8496 elsif Is_String_Type (Etype (Arg)) then
8497 if not (Nkind_In (Arg, N_Identifier, N_Expanded_Name)
8498 and then Is_Constant_Object (Entity (Arg)))
8499 and then not Is_Static_Expression (Arg)
8500 then
8501 Check_SPARK_Restriction
8502 ("string operand for concatenation should be static", Arg);
8503 end if;
8504
8505 -- Do not issue error on an operand that is neither a character nor a
8506 -- string, as the error is issued in Resolve_Op_Concat.
8507
8508 else
8509 null;
8510 end if;
8511
8512 Check_Unset_Reference (Arg);
8513 end Resolve_Op_Concat_Arg;
8514
8515 -----------------------------
8516 -- Resolve_Op_Concat_First --
8517 -----------------------------
8518
8519 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
8520 Btyp : constant Entity_Id := Base_Type (Typ);
8521 Op1 : constant Node_Id := Left_Opnd (N);
8522 Op2 : constant Node_Id := Right_Opnd (N);
8523
8524 begin
8525 -- The parser folds an enormous sequence of concatenations of string
8526 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
8527 -- in the right operand. If the expression resolves to a predefined "&"
8528 -- operator, all is well. Otherwise, the parser's folding is wrong, so
8529 -- we give an error. See P_Simple_Expression in Par.Ch4.
8530
8531 if Nkind (Op2) = N_String_Literal
8532 and then Is_Folded_In_Parser (Op2)
8533 and then Ekind (Entity (N)) = E_Function
8534 then
8535 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
8536 and then String_Length (Strval (Op1)) = 0);
8537 Error_Msg_N ("too many user-defined concatenations", N);
8538 return;
8539 end if;
8540
8541 Set_Etype (N, Btyp);
8542
8543 if Is_Limited_Composite (Btyp) then
8544 Error_Msg_N ("concatenation not available for limited array", N);
8545 Explain_Limited_Type (Btyp, N);
8546 end if;
8547 end Resolve_Op_Concat_First;
8548
8549 ----------------------------
8550 -- Resolve_Op_Concat_Rest --
8551 ----------------------------
8552
8553 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
8554 Op1 : constant Node_Id := Left_Opnd (N);
8555 Op2 : constant Node_Id := Right_Opnd (N);
8556
8557 begin
8558 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
8559
8560 Generate_Operator_Reference (N, Typ);
8561
8562 if Is_String_Type (Typ) then
8563 Eval_Concatenation (N);
8564 end if;
8565
8566 -- If this is not a static concatenation, but the result is a string
8567 -- type (and not an array of strings) ensure that static string operands
8568 -- have their subtypes properly constructed.
8569
8570 if Nkind (N) /= N_String_Literal
8571 and then Is_Character_Type (Component_Type (Typ))
8572 then
8573 Set_String_Literal_Subtype (Op1, Typ);
8574 Set_String_Literal_Subtype (Op2, Typ);
8575 end if;
8576 end Resolve_Op_Concat_Rest;
8577
8578 ----------------------
8579 -- Resolve_Op_Expon --
8580 ----------------------
8581
8582 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
8583 B_Typ : constant Entity_Id := Base_Type (Typ);
8584
8585 begin
8586 -- Catch attempts to do fixed-point exponentiation with universal
8587 -- operands, which is a case where the illegality is not caught during
8588 -- normal operator analysis. This is not done in preanalysis mode
8589 -- since the tree is not fully decorated during preanalysis.
8590
8591 if Full_Analysis then
8592 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
8593 Error_Msg_N ("exponentiation not available for fixed point", N);
8594 return;
8595
8596 elsif Nkind (Parent (N)) in N_Op
8597 and then Is_Fixed_Point_Type (Etype (Parent (N)))
8598 and then Etype (N) = Universal_Real
8599 and then Comes_From_Source (N)
8600 then
8601 Error_Msg_N ("exponentiation not available for fixed point", N);
8602 return;
8603 end if;
8604 end if;
8605
8606 if Comes_From_Source (N)
8607 and then Ekind (Entity (N)) = E_Function
8608 and then Is_Imported (Entity (N))
8609 and then Is_Intrinsic_Subprogram (Entity (N))
8610 then
8611 Resolve_Intrinsic_Operator (N, Typ);
8612 return;
8613 end if;
8614
8615 if Etype (Left_Opnd (N)) = Universal_Integer
8616 or else Etype (Left_Opnd (N)) = Universal_Real
8617 then
8618 Check_For_Visible_Operator (N, B_Typ);
8619 end if;
8620
8621 -- We do the resolution using the base type, because intermediate values
8622 -- in expressions are always of the base type, not a subtype of it.
8623
8624 Resolve (Left_Opnd (N), B_Typ);
8625 Resolve (Right_Opnd (N), Standard_Integer);
8626
8627 -- For integer types, right argument must be in Natural range
8628
8629 if Is_Integer_Type (Typ) then
8630 Apply_Scalar_Range_Check (Right_Opnd (N), Standard_Natural);
8631 end if;
8632
8633 Check_Unset_Reference (Left_Opnd (N));
8634 Check_Unset_Reference (Right_Opnd (N));
8635
8636 Set_Etype (N, B_Typ);
8637 Generate_Operator_Reference (N, B_Typ);
8638
8639 Analyze_Dimension (N);
8640
8641 if Ada_Version >= Ada_2012 and then Has_Dimension_System (B_Typ) then
8642 -- Evaluate the exponentiation operator for dimensioned type
8643
8644 Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ);
8645 else
8646 Eval_Op_Expon (N);
8647 end if;
8648
8649 -- Set overflow checking bit. Much cleverer code needed here eventually
8650 -- and perhaps the Resolve routines should be separated for the various
8651 -- arithmetic operations, since they will need different processing. ???
8652
8653 if Nkind (N) in N_Op then
8654 if not Overflow_Checks_Suppressed (Etype (N)) then
8655 Enable_Overflow_Check (N);
8656 end if;
8657 end if;
8658 end Resolve_Op_Expon;
8659
8660 --------------------
8661 -- Resolve_Op_Not --
8662 --------------------
8663
8664 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
8665 B_Typ : Entity_Id;
8666
8667 function Parent_Is_Boolean return Boolean;
8668 -- This function determines if the parent node is a boolean operator or
8669 -- operation (comparison op, membership test, or short circuit form) and
8670 -- the not in question is the left operand of this operation. Note that
8671 -- if the not is in parens, then false is returned.
8672
8673 -----------------------
8674 -- Parent_Is_Boolean --
8675 -----------------------
8676
8677 function Parent_Is_Boolean return Boolean is
8678 begin
8679 if Paren_Count (N) /= 0 then
8680 return False;
8681
8682 else
8683 case Nkind (Parent (N)) is
8684 when N_Op_And |
8685 N_Op_Eq |
8686 N_Op_Ge |
8687 N_Op_Gt |
8688 N_Op_Le |
8689 N_Op_Lt |
8690 N_Op_Ne |
8691 N_Op_Or |
8692 N_Op_Xor |
8693 N_In |
8694 N_Not_In |
8695 N_And_Then |
8696 N_Or_Else =>
8697
8698 return Left_Opnd (Parent (N)) = N;
8699
8700 when others =>
8701 return False;
8702 end case;
8703 end if;
8704 end Parent_Is_Boolean;
8705
8706 -- Start of processing for Resolve_Op_Not
8707
8708 begin
8709 -- Predefined operations on scalar types yield the base type. On the
8710 -- other hand, logical operations on arrays yield the type of the
8711 -- arguments (and the context).
8712
8713 if Is_Array_Type (Typ) then
8714 B_Typ := Typ;
8715 else
8716 B_Typ := Base_Type (Typ);
8717 end if;
8718
8719 if Is_VMS_Operator (Entity (N)) then
8720 null;
8721
8722 -- Straightforward case of incorrect arguments
8723
8724 elsif not Valid_Boolean_Arg (Typ) then
8725 Error_Msg_N ("invalid operand type for operator&", N);
8726 Set_Etype (N, Any_Type);
8727 return;
8728
8729 -- Special case of probable missing parens
8730
8731 elsif Typ = Universal_Integer or else Typ = Any_Modular then
8732 if Parent_Is_Boolean then
8733 Error_Msg_N
8734 ("operand of not must be enclosed in parentheses",
8735 Right_Opnd (N));
8736 else
8737 Error_Msg_N
8738 ("no modular type available in this context", N);
8739 end if;
8740
8741 Set_Etype (N, Any_Type);
8742 return;
8743
8744 -- OK resolution of NOT
8745
8746 else
8747 -- Warn if non-boolean types involved. This is a case like not a < b
8748 -- where a and b are modular, where we will get (not a) < b and most
8749 -- likely not (a < b) was intended.
8750
8751 if Warn_On_Questionable_Missing_Parens
8752 and then not Is_Boolean_Type (Typ)
8753 and then Parent_Is_Boolean
8754 then
8755 Error_Msg_N ("?q?not expression should be parenthesized here!", N);
8756 end if;
8757
8758 -- Warn on double negation if checking redundant constructs
8759
8760 if Warn_On_Redundant_Constructs
8761 and then Comes_From_Source (N)
8762 and then Comes_From_Source (Right_Opnd (N))
8763 and then Root_Type (Typ) = Standard_Boolean
8764 and then Nkind (Right_Opnd (N)) = N_Op_Not
8765 then
8766 Error_Msg_N ("redundant double negation?r?", N);
8767 end if;
8768
8769 -- Complete resolution and evaluation of NOT
8770
8771 Resolve (Right_Opnd (N), B_Typ);
8772 Check_Unset_Reference (Right_Opnd (N));
8773 Set_Etype (N, B_Typ);
8774 Generate_Operator_Reference (N, B_Typ);
8775 Eval_Op_Not (N);
8776 end if;
8777 end Resolve_Op_Not;
8778
8779 -----------------------------
8780 -- Resolve_Operator_Symbol --
8781 -----------------------------
8782
8783 -- Nothing to be done, all resolved already
8784
8785 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
8786 pragma Warnings (Off, N);
8787 pragma Warnings (Off, Typ);
8788
8789 begin
8790 null;
8791 end Resolve_Operator_Symbol;
8792
8793 ----------------------------------
8794 -- Resolve_Qualified_Expression --
8795 ----------------------------------
8796
8797 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
8798 pragma Warnings (Off, Typ);
8799
8800 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
8801 Expr : constant Node_Id := Expression (N);
8802
8803 begin
8804 Resolve (Expr, Target_Typ);
8805
8806 -- Protect call to Matching_Static_Array_Bounds to avoid costly
8807 -- operation if not needed.
8808
8809 if Restriction_Check_Required (SPARK_05)
8810 and then Is_Array_Type (Target_Typ)
8811 and then Is_Array_Type (Etype (Expr))
8812 and then Etype (Expr) /= Any_Composite -- or else Expr in error
8813 and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr))
8814 then
8815 Check_SPARK_Restriction
8816 ("array types should have matching static bounds", N);
8817 end if;
8818
8819 -- A qualified expression requires an exact match of the type, class-
8820 -- wide matching is not allowed. However, if the qualifying type is
8821 -- specific and the expression has a class-wide type, it may still be
8822 -- okay, since it can be the result of the expansion of a call to a
8823 -- dispatching function, so we also have to check class-wideness of the
8824 -- type of the expression's original node.
8825
8826 if (Is_Class_Wide_Type (Target_Typ)
8827 or else
8828 (Is_Class_Wide_Type (Etype (Expr))
8829 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
8830 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
8831 then
8832 Wrong_Type (Expr, Target_Typ);
8833 end if;
8834
8835 -- If the target type is unconstrained, then we reset the type of the
8836 -- result from the type of the expression. For other cases, the actual
8837 -- subtype of the expression is the target type.
8838
8839 if Is_Composite_Type (Target_Typ)
8840 and then not Is_Constrained (Target_Typ)
8841 then
8842 Set_Etype (N, Etype (Expr));
8843 end if;
8844
8845 Analyze_Dimension (N);
8846 Eval_Qualified_Expression (N);
8847 end Resolve_Qualified_Expression;
8848
8849 ------------------------------
8850 -- Resolve_Raise_Expression --
8851 ------------------------------
8852
8853 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id) is
8854 begin
8855 if Typ = Raise_Type then
8856 Error_Msg_N ("cannot find unique type for raise expression", N);
8857 Set_Etype (N, Any_Type);
8858 else
8859 Set_Etype (N, Typ);
8860 end if;
8861 end Resolve_Raise_Expression;
8862
8863 -------------------
8864 -- Resolve_Range --
8865 -------------------
8866
8867 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
8868 L : constant Node_Id := Low_Bound (N);
8869 H : constant Node_Id := High_Bound (N);
8870
8871 function First_Last_Ref return Boolean;
8872 -- Returns True if N is of the form X'First .. X'Last where X is the
8873 -- same entity for both attributes.
8874
8875 --------------------
8876 -- First_Last_Ref --
8877 --------------------
8878
8879 function First_Last_Ref return Boolean is
8880 Lorig : constant Node_Id := Original_Node (L);
8881 Horig : constant Node_Id := Original_Node (H);
8882
8883 begin
8884 if Nkind (Lorig) = N_Attribute_Reference
8885 and then Nkind (Horig) = N_Attribute_Reference
8886 and then Attribute_Name (Lorig) = Name_First
8887 and then Attribute_Name (Horig) = Name_Last
8888 then
8889 declare
8890 PL : constant Node_Id := Prefix (Lorig);
8891 PH : constant Node_Id := Prefix (Horig);
8892 begin
8893 if Is_Entity_Name (PL)
8894 and then Is_Entity_Name (PH)
8895 and then Entity (PL) = Entity (PH)
8896 then
8897 return True;
8898 end if;
8899 end;
8900 end if;
8901
8902 return False;
8903 end First_Last_Ref;
8904
8905 -- Start of processing for Resolve_Range
8906
8907 begin
8908 Set_Etype (N, Typ);
8909 Resolve (L, Typ);
8910 Resolve (H, Typ);
8911
8912 -- Check for inappropriate range on unordered enumeration type
8913
8914 if Bad_Unordered_Enumeration_Reference (N, Typ)
8915
8916 -- Exclude X'First .. X'Last if X is the same entity for both
8917
8918 and then not First_Last_Ref
8919 then
8920 Error_Msg_Sloc := Sloc (Typ);
8921 Error_Msg_NE
8922 ("subrange of unordered enumeration type& declared#?U?", N, Typ);
8923 end if;
8924
8925 Check_Unset_Reference (L);
8926 Check_Unset_Reference (H);
8927
8928 -- We have to check the bounds for being within the base range as
8929 -- required for a non-static context. Normally this is automatic and
8930 -- done as part of evaluating expressions, but the N_Range node is an
8931 -- exception, since in GNAT we consider this node to be a subexpression,
8932 -- even though in Ada it is not. The circuit in Sem_Eval could check for
8933 -- this, but that would put the test on the main evaluation path for
8934 -- expressions.
8935
8936 Check_Non_Static_Context (L);
8937 Check_Non_Static_Context (H);
8938
8939 -- Check for an ambiguous range over character literals. This will
8940 -- happen with a membership test involving only literals.
8941
8942 if Typ = Any_Character then
8943 Ambiguous_Character (L);
8944 Set_Etype (N, Any_Type);
8945 return;
8946 end if;
8947
8948 -- If bounds are static, constant-fold them, so size computations are
8949 -- identical between front-end and back-end. Do not perform this
8950 -- transformation while analyzing generic units, as type information
8951 -- would be lost when reanalyzing the constant node in the instance.
8952
8953 if Is_Discrete_Type (Typ) and then Expander_Active then
8954 if Is_OK_Static_Expression (L) then
8955 Fold_Uint (L, Expr_Value (L), Is_Static_Expression (L));
8956 end if;
8957
8958 if Is_OK_Static_Expression (H) then
8959 Fold_Uint (H, Expr_Value (H), Is_Static_Expression (H));
8960 end if;
8961 end if;
8962 end Resolve_Range;
8963
8964 --------------------------
8965 -- Resolve_Real_Literal --
8966 --------------------------
8967
8968 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
8969 Actual_Typ : constant Entity_Id := Etype (N);
8970
8971 begin
8972 -- Special processing for fixed-point literals to make sure that the
8973 -- value is an exact multiple of small where this is required. We skip
8974 -- this for the universal real case, and also for generic types.
8975
8976 if Is_Fixed_Point_Type (Typ)
8977 and then Typ /= Universal_Fixed
8978 and then Typ /= Any_Fixed
8979 and then not Is_Generic_Type (Typ)
8980 then
8981 declare
8982 Val : constant Ureal := Realval (N);
8983 Cintr : constant Ureal := Val / Small_Value (Typ);
8984 Cint : constant Uint := UR_Trunc (Cintr);
8985 Den : constant Uint := Norm_Den (Cintr);
8986 Stat : Boolean;
8987
8988 begin
8989 -- Case of literal is not an exact multiple of the Small
8990
8991 if Den /= 1 then
8992
8993 -- For a source program literal for a decimal fixed-point type,
8994 -- this is statically illegal (RM 4.9(36)).
8995
8996 if Is_Decimal_Fixed_Point_Type (Typ)
8997 and then Actual_Typ = Universal_Real
8998 and then Comes_From_Source (N)
8999 then
9000 Error_Msg_N ("value has extraneous low order digits", N);
9001 end if;
9002
9003 -- Generate a warning if literal from source
9004
9005 if Is_Static_Expression (N)
9006 and then Warn_On_Bad_Fixed_Value
9007 then
9008 Error_Msg_N
9009 ("?b?static fixed-point value is not a multiple of Small!",
9010 N);
9011 end if;
9012
9013 -- Replace literal by a value that is the exact representation
9014 -- of a value of the type, i.e. a multiple of the small value,
9015 -- by truncation, since Machine_Rounds is false for all GNAT
9016 -- fixed-point types (RM 4.9(38)).
9017
9018 Stat := Is_Static_Expression (N);
9019 Rewrite (N,
9020 Make_Real_Literal (Sloc (N),
9021 Realval => Small_Value (Typ) * Cint));
9022
9023 Set_Is_Static_Expression (N, Stat);
9024 end if;
9025
9026 -- In all cases, set the corresponding integer field
9027
9028 Set_Corresponding_Integer_Value (N, Cint);
9029 end;
9030 end if;
9031
9032 -- Now replace the actual type by the expected type as usual
9033
9034 Set_Etype (N, Typ);
9035 Eval_Real_Literal (N);
9036 end Resolve_Real_Literal;
9037
9038 -----------------------
9039 -- Resolve_Reference --
9040 -----------------------
9041
9042 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
9043 P : constant Node_Id := Prefix (N);
9044
9045 begin
9046 -- Replace general access with specific type
9047
9048 if Ekind (Etype (N)) = E_Allocator_Type then
9049 Set_Etype (N, Base_Type (Typ));
9050 end if;
9051
9052 Resolve (P, Designated_Type (Etype (N)));
9053
9054 -- If we are taking the reference of a volatile entity, then treat it as
9055 -- a potential modification of this entity. This is too conservative,
9056 -- but necessary because remove side effects can cause transformations
9057 -- of normal assignments into reference sequences that otherwise fail to
9058 -- notice the modification.
9059
9060 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
9061 Note_Possible_Modification (P, Sure => False);
9062 end if;
9063 end Resolve_Reference;
9064
9065 --------------------------------
9066 -- Resolve_Selected_Component --
9067 --------------------------------
9068
9069 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
9070 Comp : Entity_Id;
9071 Comp1 : Entity_Id := Empty; -- prevent junk warning
9072 P : constant Node_Id := Prefix (N);
9073 S : constant Node_Id := Selector_Name (N);
9074 T : Entity_Id := Etype (P);
9075 I : Interp_Index;
9076 I1 : Interp_Index := 0; -- prevent junk warning
9077 It : Interp;
9078 It1 : Interp;
9079 Found : Boolean;
9080
9081 function Init_Component return Boolean;
9082 -- Check whether this is the initialization of a component within an
9083 -- init proc (by assignment or call to another init proc). If true,
9084 -- there is no need for a discriminant check.
9085
9086 --------------------
9087 -- Init_Component --
9088 --------------------
9089
9090 function Init_Component return Boolean is
9091 begin
9092 return Inside_Init_Proc
9093 and then Nkind (Prefix (N)) = N_Identifier
9094 and then Chars (Prefix (N)) = Name_uInit
9095 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
9096 end Init_Component;
9097
9098 -- Start of processing for Resolve_Selected_Component
9099
9100 begin
9101 if Is_Overloaded (P) then
9102
9103 -- Use the context type to select the prefix that has a selector
9104 -- of the correct name and type.
9105
9106 Found := False;
9107 Get_First_Interp (P, I, It);
9108
9109 Search : while Present (It.Typ) loop
9110 if Is_Access_Type (It.Typ) then
9111 T := Designated_Type (It.Typ);
9112 else
9113 T := It.Typ;
9114 end if;
9115
9116 -- Locate selected component. For a private prefix the selector
9117 -- can denote a discriminant.
9118
9119 if Is_Record_Type (T) or else Is_Private_Type (T) then
9120
9121 -- The visible components of a class-wide type are those of
9122 -- the root type.
9123
9124 if Is_Class_Wide_Type (T) then
9125 T := Etype (T);
9126 end if;
9127
9128 Comp := First_Entity (T);
9129 while Present (Comp) loop
9130 if Chars (Comp) = Chars (S)
9131 and then Covers (Typ, Etype (Comp))
9132 then
9133 if not Found then
9134 Found := True;
9135 I1 := I;
9136 It1 := It;
9137 Comp1 := Comp;
9138
9139 else
9140 It := Disambiguate (P, I1, I, Any_Type);
9141
9142 if It = No_Interp then
9143 Error_Msg_N
9144 ("ambiguous prefix for selected component", N);
9145 Set_Etype (N, Typ);
9146 return;
9147
9148 else
9149 It1 := It;
9150
9151 -- There may be an implicit dereference. Retrieve
9152 -- designated record type.
9153
9154 if Is_Access_Type (It1.Typ) then
9155 T := Designated_Type (It1.Typ);
9156 else
9157 T := It1.Typ;
9158 end if;
9159
9160 if Scope (Comp1) /= T then
9161
9162 -- Resolution chooses the new interpretation.
9163 -- Find the component with the right name.
9164
9165 Comp1 := First_Entity (T);
9166 while Present (Comp1)
9167 and then Chars (Comp1) /= Chars (S)
9168 loop
9169 Comp1 := Next_Entity (Comp1);
9170 end loop;
9171 end if;
9172
9173 exit Search;
9174 end if;
9175 end if;
9176 end if;
9177
9178 Comp := Next_Entity (Comp);
9179 end loop;
9180 end if;
9181
9182 Get_Next_Interp (I, It);
9183 end loop Search;
9184
9185 -- There must be a legal interpretation at this point
9186
9187 pragma Assert (Found);
9188 Resolve (P, It1.Typ);
9189 Set_Etype (N, Typ);
9190 Set_Entity_With_Checks (S, Comp1);
9191
9192 else
9193 -- Resolve prefix with its type
9194
9195 Resolve (P, T);
9196 end if;
9197
9198 -- Generate cross-reference. We needed to wait until full overloading
9199 -- resolution was complete to do this, since otherwise we can't tell if
9200 -- we are an lvalue or not.
9201
9202 if May_Be_Lvalue (N) then
9203 Generate_Reference (Entity (S), S, 'm');
9204 else
9205 Generate_Reference (Entity (S), S, 'r');
9206 end if;
9207
9208 -- If prefix is an access type, the node will be transformed into an
9209 -- explicit dereference during expansion. The type of the node is the
9210 -- designated type of that of the prefix.
9211
9212 if Is_Access_Type (Etype (P)) then
9213 T := Designated_Type (Etype (P));
9214 Check_Fully_Declared_Prefix (T, P);
9215 else
9216 T := Etype (P);
9217 end if;
9218
9219 -- Set flag for expander if discriminant check required
9220
9221 if Has_Discriminants (T)
9222 and then Ekind_In (Entity (S), E_Component, E_Discriminant)
9223 and then Present (Original_Record_Component (Entity (S)))
9224 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
9225 and then not Discriminant_Checks_Suppressed (T)
9226 and then not Init_Component
9227 then
9228 Set_Do_Discriminant_Check (N);
9229 end if;
9230
9231 if Ekind (Entity (S)) = E_Void then
9232 Error_Msg_N ("premature use of component", S);
9233 end if;
9234
9235 -- If the prefix is a record conversion, this may be a renamed
9236 -- discriminant whose bounds differ from those of the original
9237 -- one, so we must ensure that a range check is performed.
9238
9239 if Nkind (P) = N_Type_Conversion
9240 and then Ekind (Entity (S)) = E_Discriminant
9241 and then Is_Discrete_Type (Typ)
9242 then
9243 Set_Etype (N, Base_Type (Typ));
9244 end if;
9245
9246 -- Note: No Eval processing is required, because the prefix is of a
9247 -- record type, or protected type, and neither can possibly be static.
9248
9249 -- If the array type is atomic, and is packed, and we are in a left side
9250 -- context, then this is worth a warning, since we have a situation
9251 -- where the access to the component may cause extra read/writes of the
9252 -- atomic array object, which could be considered unexpected.
9253
9254 if Nkind (N) = N_Selected_Component
9255 and then (Is_Atomic (T)
9256 or else (Is_Entity_Name (Prefix (N))
9257 and then Is_Atomic (Entity (Prefix (N)))))
9258 and then Is_Packed (T)
9259 and then Is_LHS (N) = Yes
9260 then
9261 Error_Msg_N
9262 ("??assignment to component of packed atomic record", Prefix (N));
9263 Error_Msg_N
9264 ("\??may cause unexpected accesses to atomic object", Prefix (N));
9265 end if;
9266
9267 Analyze_Dimension (N);
9268 end Resolve_Selected_Component;
9269
9270 -------------------
9271 -- Resolve_Shift --
9272 -------------------
9273
9274 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
9275 B_Typ : constant Entity_Id := Base_Type (Typ);
9276 L : constant Node_Id := Left_Opnd (N);
9277 R : constant Node_Id := Right_Opnd (N);
9278
9279 begin
9280 -- We do the resolution using the base type, because intermediate values
9281 -- in expressions always are of the base type, not a subtype of it.
9282
9283 Resolve (L, B_Typ);
9284 Resolve (R, Standard_Natural);
9285
9286 Check_Unset_Reference (L);
9287 Check_Unset_Reference (R);
9288
9289 Set_Etype (N, B_Typ);
9290 Generate_Operator_Reference (N, B_Typ);
9291 Eval_Shift (N);
9292 end Resolve_Shift;
9293
9294 ---------------------------
9295 -- Resolve_Short_Circuit --
9296 ---------------------------
9297
9298 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
9299 B_Typ : constant Entity_Id := Base_Type (Typ);
9300 L : constant Node_Id := Left_Opnd (N);
9301 R : constant Node_Id := Right_Opnd (N);
9302
9303 begin
9304 -- Ensure all actions associated with the left operand (e.g.
9305 -- finalization of transient controlled objects) are fully evaluated
9306 -- locally within an expression with actions. This is particularly
9307 -- helpful for coverage analysis. However this should not happen in
9308 -- generics.
9309
9310 if Expander_Active then
9311 declare
9312 Reloc_L : constant Node_Id := Relocate_Node (L);
9313 begin
9314 Save_Interps (Old_N => L, New_N => Reloc_L);
9315
9316 Rewrite (L,
9317 Make_Expression_With_Actions (Sloc (L),
9318 Actions => New_List,
9319 Expression => Reloc_L));
9320
9321 -- Set Comes_From_Source on L to preserve warnings for unset
9322 -- reference.
9323
9324 Set_Comes_From_Source (L, Comes_From_Source (Reloc_L));
9325 end;
9326 end if;
9327
9328 Resolve (L, B_Typ);
9329 Resolve (R, B_Typ);
9330
9331 -- Check for issuing warning for always False assert/check, this happens
9332 -- when assertions are turned off, in which case the pragma Assert/Check
9333 -- was transformed into:
9334
9335 -- if False and then <condition> then ...
9336
9337 -- and we detect this pattern
9338
9339 if Warn_On_Assertion_Failure
9340 and then Is_Entity_Name (R)
9341 and then Entity (R) = Standard_False
9342 and then Nkind (Parent (N)) = N_If_Statement
9343 and then Nkind (N) = N_And_Then
9344 and then Is_Entity_Name (L)
9345 and then Entity (L) = Standard_False
9346 then
9347 declare
9348 Orig : constant Node_Id := Original_Node (Parent (N));
9349
9350 begin
9351 -- Special handling of Asssert pragma
9352
9353 if Nkind (Orig) = N_Pragma
9354 and then Pragma_Name (Orig) = Name_Assert
9355 then
9356 declare
9357 Expr : constant Node_Id :=
9358 Original_Node
9359 (Expression
9360 (First (Pragma_Argument_Associations (Orig))));
9361
9362 begin
9363 -- Don't warn if original condition is explicit False,
9364 -- since obviously the failure is expected in this case.
9365
9366 if Is_Entity_Name (Expr)
9367 and then Entity (Expr) = Standard_False
9368 then
9369 null;
9370
9371 -- Issue warning. We do not want the deletion of the
9372 -- IF/AND-THEN to take this message with it. We achieve this
9373 -- by making sure that the expanded code points to the Sloc
9374 -- of the expression, not the original pragma.
9375
9376 else
9377 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
9378 -- The source location of the expression is not usually
9379 -- the best choice here. For example, it gets located on
9380 -- the last AND keyword in a chain of boolean expressiond
9381 -- AND'ed together. It is best to put the message on the
9382 -- first character of the assertion, which is the effect
9383 -- of the First_Node call here.
9384
9385 Error_Msg_F
9386 ("?A?assertion would fail at run time!",
9387 Expression
9388 (First (Pragma_Argument_Associations (Orig))));
9389 end if;
9390 end;
9391
9392 -- Similar processing for Check pragma
9393
9394 elsif Nkind (Orig) = N_Pragma
9395 and then Pragma_Name (Orig) = Name_Check
9396 then
9397 -- Don't want to warn if original condition is explicit False
9398
9399 declare
9400 Expr : constant Node_Id :=
9401 Original_Node
9402 (Expression
9403 (Next (First (Pragma_Argument_Associations (Orig)))));
9404 begin
9405 if Is_Entity_Name (Expr)
9406 and then Entity (Expr) = Standard_False
9407 then
9408 null;
9409
9410 -- Post warning
9411
9412 else
9413 -- Again use Error_Msg_F rather than Error_Msg_N, see
9414 -- comment above for an explanation of why we do this.
9415
9416 Error_Msg_F
9417 ("?A?check would fail at run time!",
9418 Expression
9419 (Last (Pragma_Argument_Associations (Orig))));
9420 end if;
9421 end;
9422 end if;
9423 end;
9424 end if;
9425
9426 -- Continue with processing of short circuit
9427
9428 Check_Unset_Reference (L);
9429 Check_Unset_Reference (R);
9430
9431 Set_Etype (N, B_Typ);
9432 Eval_Short_Circuit (N);
9433 end Resolve_Short_Circuit;
9434
9435 -------------------
9436 -- Resolve_Slice --
9437 -------------------
9438
9439 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
9440 Drange : constant Node_Id := Discrete_Range (N);
9441 Name : constant Node_Id := Prefix (N);
9442 Array_Type : Entity_Id := Empty;
9443 Dexpr : Node_Id := Empty;
9444 Index_Type : Entity_Id;
9445
9446 begin
9447 if Is_Overloaded (Name) then
9448
9449 -- Use the context type to select the prefix that yields the correct
9450 -- array type.
9451
9452 declare
9453 I : Interp_Index;
9454 I1 : Interp_Index := 0;
9455 It : Interp;
9456 P : constant Node_Id := Prefix (N);
9457 Found : Boolean := False;
9458
9459 begin
9460 Get_First_Interp (P, I, It);
9461 while Present (It.Typ) loop
9462 if (Is_Array_Type (It.Typ)
9463 and then Covers (Typ, It.Typ))
9464 or else (Is_Access_Type (It.Typ)
9465 and then Is_Array_Type (Designated_Type (It.Typ))
9466 and then Covers (Typ, Designated_Type (It.Typ)))
9467 then
9468 if Found then
9469 It := Disambiguate (P, I1, I, Any_Type);
9470
9471 if It = No_Interp then
9472 Error_Msg_N ("ambiguous prefix for slicing", N);
9473 Set_Etype (N, Typ);
9474 return;
9475 else
9476 Found := True;
9477 Array_Type := It.Typ;
9478 I1 := I;
9479 end if;
9480 else
9481 Found := True;
9482 Array_Type := It.Typ;
9483 I1 := I;
9484 end if;
9485 end if;
9486
9487 Get_Next_Interp (I, It);
9488 end loop;
9489 end;
9490
9491 else
9492 Array_Type := Etype (Name);
9493 end if;
9494
9495 Resolve (Name, Array_Type);
9496
9497 if Is_Access_Type (Array_Type) then
9498 Apply_Access_Check (N);
9499 Array_Type := Designated_Type (Array_Type);
9500
9501 -- If the prefix is an access to an unconstrained array, we must use
9502 -- the actual subtype of the object to perform the index checks. The
9503 -- object denoted by the prefix is implicit in the node, so we build
9504 -- an explicit representation for it in order to compute the actual
9505 -- subtype.
9506
9507 if not Is_Constrained (Array_Type) then
9508 Remove_Side_Effects (Prefix (N));
9509
9510 declare
9511 Obj : constant Node_Id :=
9512 Make_Explicit_Dereference (Sloc (N),
9513 Prefix => New_Copy_Tree (Prefix (N)));
9514 begin
9515 Set_Etype (Obj, Array_Type);
9516 Set_Parent (Obj, Parent (N));
9517 Array_Type := Get_Actual_Subtype (Obj);
9518 end;
9519 end if;
9520
9521 elsif Is_Entity_Name (Name)
9522 or else Nkind (Name) = N_Explicit_Dereference
9523 or else (Nkind (Name) = N_Function_Call
9524 and then not Is_Constrained (Etype (Name)))
9525 then
9526 Array_Type := Get_Actual_Subtype (Name);
9527
9528 -- If the name is a selected component that depends on discriminants,
9529 -- build an actual subtype for it. This can happen only when the name
9530 -- itself is overloaded; otherwise the actual subtype is created when
9531 -- the selected component is analyzed.
9532
9533 elsif Nkind (Name) = N_Selected_Component
9534 and then Full_Analysis
9535 and then Depends_On_Discriminant (First_Index (Array_Type))
9536 then
9537 declare
9538 Act_Decl : constant Node_Id :=
9539 Build_Actual_Subtype_Of_Component (Array_Type, Name);
9540 begin
9541 Insert_Action (N, Act_Decl);
9542 Array_Type := Defining_Identifier (Act_Decl);
9543 end;
9544
9545 -- Maybe this should just be "else", instead of checking for the
9546 -- specific case of slice??? This is needed for the case where the
9547 -- prefix is an Image attribute, which gets expanded to a slice, and so
9548 -- has a constrained subtype which we want to use for the slice range
9549 -- check applied below (the range check won't get done if the
9550 -- unconstrained subtype of the 'Image is used).
9551
9552 elsif Nkind (Name) = N_Slice then
9553 Array_Type := Etype (Name);
9554 end if;
9555
9556 -- Obtain the type of the array index
9557
9558 if Ekind (Array_Type) = E_String_Literal_Subtype then
9559 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
9560 else
9561 Index_Type := Etype (First_Index (Array_Type));
9562 end if;
9563
9564 -- If name was overloaded, set slice type correctly now
9565
9566 Set_Etype (N, Array_Type);
9567
9568 -- Handle the generation of a range check that compares the array index
9569 -- against the discrete_range. The check is not applied to internally
9570 -- built nodes associated with the expansion of dispatch tables. Check
9571 -- that Ada.Tags has already been loaded to avoid extra dependencies on
9572 -- the unit.
9573
9574 if Tagged_Type_Expansion
9575 and then RTU_Loaded (Ada_Tags)
9576 and then Nkind (Prefix (N)) = N_Selected_Component
9577 and then Present (Entity (Selector_Name (Prefix (N))))
9578 and then Entity (Selector_Name (Prefix (N))) =
9579 RTE_Record_Component (RE_Prims_Ptr)
9580 then
9581 null;
9582
9583 -- The discrete_range is specified by a subtype indication. Create a
9584 -- shallow copy and inherit the type, parent and source location from
9585 -- the discrete_range. This ensures that the range check is inserted
9586 -- relative to the slice and that the runtime exception points to the
9587 -- proper construct.
9588
9589 elsif Is_Entity_Name (Drange) then
9590 Dexpr := New_Copy (Scalar_Range (Entity (Drange)));
9591
9592 Set_Etype (Dexpr, Etype (Drange));
9593 Set_Parent (Dexpr, Parent (Drange));
9594 Set_Sloc (Dexpr, Sloc (Drange));
9595
9596 -- The discrete_range is a regular range. Resolve the bounds and remove
9597 -- their side effects.
9598
9599 else
9600 Resolve (Drange, Base_Type (Index_Type));
9601
9602 if Nkind (Drange) = N_Range then
9603 Force_Evaluation (Low_Bound (Drange));
9604 Force_Evaluation (High_Bound (Drange));
9605
9606 Dexpr := Drange;
9607 end if;
9608 end if;
9609
9610 if Present (Dexpr) then
9611 Apply_Range_Check (Dexpr, Index_Type);
9612 end if;
9613
9614 Set_Slice_Subtype (N);
9615
9616 -- Check bad use of type with predicates
9617
9618 if Has_Predicates (Etype (Drange)) then
9619 Bad_Predicated_Subtype_Use
9620 ("subtype& has predicate, not allowed in slice",
9621 Drange, Etype (Drange));
9622
9623 -- Otherwise here is where we check suspicious indexes
9624
9625 elsif Nkind (Drange) = N_Range then
9626 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
9627 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
9628 end if;
9629
9630 Analyze_Dimension (N);
9631 Eval_Slice (N);
9632 end Resolve_Slice;
9633
9634 ----------------------------
9635 -- Resolve_String_Literal --
9636 ----------------------------
9637
9638 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
9639 C_Typ : constant Entity_Id := Component_Type (Typ);
9640 R_Typ : constant Entity_Id := Root_Type (C_Typ);
9641 Loc : constant Source_Ptr := Sloc (N);
9642 Str : constant String_Id := Strval (N);
9643 Strlen : constant Nat := String_Length (Str);
9644 Subtype_Id : Entity_Id;
9645 Need_Check : Boolean;
9646
9647 begin
9648 -- For a string appearing in a concatenation, defer creation of the
9649 -- string_literal_subtype until the end of the resolution of the
9650 -- concatenation, because the literal may be constant-folded away. This
9651 -- is a useful optimization for long concatenation expressions.
9652
9653 -- If the string is an aggregate built for a single character (which
9654 -- happens in a non-static context) or a is null string to which special
9655 -- checks may apply, we build the subtype. Wide strings must also get a
9656 -- string subtype if they come from a one character aggregate. Strings
9657 -- generated by attributes might be static, but it is often hard to
9658 -- determine whether the enclosing context is static, so we generate
9659 -- subtypes for them as well, thus losing some rarer optimizations ???
9660 -- Same for strings that come from a static conversion.
9661
9662 Need_Check :=
9663 (Strlen = 0 and then Typ /= Standard_String)
9664 or else Nkind (Parent (N)) /= N_Op_Concat
9665 or else (N /= Left_Opnd (Parent (N))
9666 and then N /= Right_Opnd (Parent (N)))
9667 or else ((Typ = Standard_Wide_String
9668 or else Typ = Standard_Wide_Wide_String)
9669 and then Nkind (Original_Node (N)) /= N_String_Literal);
9670
9671 -- If the resolving type is itself a string literal subtype, we can just
9672 -- reuse it, since there is no point in creating another.
9673
9674 if Ekind (Typ) = E_String_Literal_Subtype then
9675 Subtype_Id := Typ;
9676
9677 elsif Nkind (Parent (N)) = N_Op_Concat
9678 and then not Need_Check
9679 and then not Nkind_In (Original_Node (N), N_Character_Literal,
9680 N_Attribute_Reference,
9681 N_Qualified_Expression,
9682 N_Type_Conversion)
9683 then
9684 Subtype_Id := Typ;
9685
9686 -- Otherwise we must create a string literal subtype. Note that the
9687 -- whole idea of string literal subtypes is simply to avoid the need
9688 -- for building a full fledged array subtype for each literal.
9689
9690 else
9691 Set_String_Literal_Subtype (N, Typ);
9692 Subtype_Id := Etype (N);
9693 end if;
9694
9695 if Nkind (Parent (N)) /= N_Op_Concat
9696 or else Need_Check
9697 then
9698 Set_Etype (N, Subtype_Id);
9699 Eval_String_Literal (N);
9700 end if;
9701
9702 if Is_Limited_Composite (Typ)
9703 or else Is_Private_Composite (Typ)
9704 then
9705 Error_Msg_N ("string literal not available for private array", N);
9706 Set_Etype (N, Any_Type);
9707 return;
9708 end if;
9709
9710 -- The validity of a null string has been checked in the call to
9711 -- Eval_String_Literal.
9712
9713 if Strlen = 0 then
9714 return;
9715
9716 -- Always accept string literal with component type Any_Character, which
9717 -- occurs in error situations and in comparisons of literals, both of
9718 -- which should accept all literals.
9719
9720 elsif R_Typ = Any_Character then
9721 return;
9722
9723 -- If the type is bit-packed, then we always transform the string
9724 -- literal into a full fledged aggregate.
9725
9726 elsif Is_Bit_Packed_Array (Typ) then
9727 null;
9728
9729 -- Deal with cases of Wide_Wide_String, Wide_String, and String
9730
9731 else
9732 -- For Standard.Wide_Wide_String, or any other type whose component
9733 -- type is Standard.Wide_Wide_Character, we know that all the
9734 -- characters in the string must be acceptable, since the parser
9735 -- accepted the characters as valid character literals.
9736
9737 if R_Typ = Standard_Wide_Wide_Character then
9738 null;
9739
9740 -- For the case of Standard.String, or any other type whose component
9741 -- type is Standard.Character, we must make sure that there are no
9742 -- wide characters in the string, i.e. that it is entirely composed
9743 -- of characters in range of type Character.
9744
9745 -- If the string literal is the result of a static concatenation, the
9746 -- test has already been performed on the components, and need not be
9747 -- repeated.
9748
9749 elsif R_Typ = Standard_Character
9750 and then Nkind (Original_Node (N)) /= N_Op_Concat
9751 then
9752 for J in 1 .. Strlen loop
9753 if not In_Character_Range (Get_String_Char (Str, J)) then
9754
9755 -- If we are out of range, post error. This is one of the
9756 -- very few places that we place the flag in the middle of
9757 -- a token, right under the offending wide character. Not
9758 -- quite clear if this is right wrt wide character encoding
9759 -- sequences, but it's only an error message.
9760
9761 Error_Msg
9762 ("literal out of range of type Standard.Character",
9763 Source_Ptr (Int (Loc) + J));
9764 return;
9765 end if;
9766 end loop;
9767
9768 -- For the case of Standard.Wide_String, or any other type whose
9769 -- component type is Standard.Wide_Character, we must make sure that
9770 -- there are no wide characters in the string, i.e. that it is
9771 -- entirely composed of characters in range of type Wide_Character.
9772
9773 -- If the string literal is the result of a static concatenation,
9774 -- the test has already been performed on the components, and need
9775 -- not be repeated.
9776
9777 elsif R_Typ = Standard_Wide_Character
9778 and then Nkind (Original_Node (N)) /= N_Op_Concat
9779 then
9780 for J in 1 .. Strlen loop
9781 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
9782
9783 -- If we are out of range, post error. This is one of the
9784 -- very few places that we place the flag in the middle of
9785 -- a token, right under the offending wide character.
9786
9787 -- This is not quite right, because characters in general
9788 -- will take more than one character position ???
9789
9790 Error_Msg
9791 ("literal out of range of type Standard.Wide_Character",
9792 Source_Ptr (Int (Loc) + J));
9793 return;
9794 end if;
9795 end loop;
9796
9797 -- If the root type is not a standard character, then we will convert
9798 -- the string into an aggregate and will let the aggregate code do
9799 -- the checking. Standard Wide_Wide_Character is also OK here.
9800
9801 else
9802 null;
9803 end if;
9804
9805 -- See if the component type of the array corresponding to the string
9806 -- has compile time known bounds. If yes we can directly check
9807 -- whether the evaluation of the string will raise constraint error.
9808 -- Otherwise we need to transform the string literal into the
9809 -- corresponding character aggregate and let the aggregate code do
9810 -- the checking.
9811
9812 if Is_Standard_Character_Type (R_Typ) then
9813
9814 -- Check for the case of full range, where we are definitely OK
9815
9816 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
9817 return;
9818 end if;
9819
9820 -- Here the range is not the complete base type range, so check
9821
9822 declare
9823 Comp_Typ_Lo : constant Node_Id :=
9824 Type_Low_Bound (Component_Type (Typ));
9825 Comp_Typ_Hi : constant Node_Id :=
9826 Type_High_Bound (Component_Type (Typ));
9827
9828 Char_Val : Uint;
9829
9830 begin
9831 if Compile_Time_Known_Value (Comp_Typ_Lo)
9832 and then Compile_Time_Known_Value (Comp_Typ_Hi)
9833 then
9834 for J in 1 .. Strlen loop
9835 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
9836
9837 if Char_Val < Expr_Value (Comp_Typ_Lo)
9838 or else Char_Val > Expr_Value (Comp_Typ_Hi)
9839 then
9840 Apply_Compile_Time_Constraint_Error
9841 (N, "character out of range??",
9842 CE_Range_Check_Failed,
9843 Loc => Source_Ptr (Int (Loc) + J));
9844 end if;
9845 end loop;
9846
9847 return;
9848 end if;
9849 end;
9850 end if;
9851 end if;
9852
9853 -- If we got here we meed to transform the string literal into the
9854 -- equivalent qualified positional array aggregate. This is rather
9855 -- heavy artillery for this situation, but it is hard work to avoid.
9856
9857 declare
9858 Lits : constant List_Id := New_List;
9859 P : Source_Ptr := Loc + 1;
9860 C : Char_Code;
9861
9862 begin
9863 -- Build the character literals, we give them source locations that
9864 -- correspond to the string positions, which is a bit tricky given
9865 -- the possible presence of wide character escape sequences.
9866
9867 for J in 1 .. Strlen loop
9868 C := Get_String_Char (Str, J);
9869 Set_Character_Literal_Name (C);
9870
9871 Append_To (Lits,
9872 Make_Character_Literal (P,
9873 Chars => Name_Find,
9874 Char_Literal_Value => UI_From_CC (C)));
9875
9876 if In_Character_Range (C) then
9877 P := P + 1;
9878
9879 -- Should we have a call to Skip_Wide here ???
9880
9881 -- ??? else
9882 -- Skip_Wide (P);
9883
9884 end if;
9885 end loop;
9886
9887 Rewrite (N,
9888 Make_Qualified_Expression (Loc,
9889 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
9890 Expression =>
9891 Make_Aggregate (Loc, Expressions => Lits)));
9892
9893 Analyze_And_Resolve (N, Typ);
9894 end;
9895 end Resolve_String_Literal;
9896
9897 -----------------------------
9898 -- Resolve_Type_Conversion --
9899 -----------------------------
9900
9901 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
9902 Conv_OK : constant Boolean := Conversion_OK (N);
9903 Operand : constant Node_Id := Expression (N);
9904 Operand_Typ : constant Entity_Id := Etype (Operand);
9905 Target_Typ : constant Entity_Id := Etype (N);
9906 Rop : Node_Id;
9907 Orig_N : Node_Id;
9908 Orig_T : Node_Id;
9909
9910 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
9911 -- Set to False to suppress cases where we want to suppress the test
9912 -- for redundancy to avoid possible false positives on this warning.
9913
9914 begin
9915 if not Conv_OK
9916 and then not Valid_Conversion (N, Target_Typ, Operand)
9917 then
9918 return;
9919 end if;
9920
9921 -- If the Operand Etype is Universal_Fixed, then the conversion is
9922 -- never redundant. We need this check because by the time we have
9923 -- finished the rather complex transformation, the conversion looks
9924 -- redundant when it is not.
9925
9926 if Operand_Typ = Universal_Fixed then
9927 Test_Redundant := False;
9928
9929 -- If the operand is marked as Any_Fixed, then special processing is
9930 -- required. This is also a case where we suppress the test for a
9931 -- redundant conversion, since most certainly it is not redundant.
9932
9933 elsif Operand_Typ = Any_Fixed then
9934 Test_Redundant := False;
9935
9936 -- Mixed-mode operation involving a literal. Context must be a fixed
9937 -- type which is applied to the literal subsequently.
9938
9939 if Is_Fixed_Point_Type (Typ) then
9940 Set_Etype (Operand, Universal_Real);
9941
9942 elsif Is_Numeric_Type (Typ)
9943 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
9944 and then (Etype (Right_Opnd (Operand)) = Universal_Real
9945 or else
9946 Etype (Left_Opnd (Operand)) = Universal_Real)
9947 then
9948 -- Return if expression is ambiguous
9949
9950 if Unique_Fixed_Point_Type (N) = Any_Type then
9951 return;
9952
9953 -- If nothing else, the available fixed type is Duration
9954
9955 else
9956 Set_Etype (Operand, Standard_Duration);
9957 end if;
9958
9959 -- Resolve the real operand with largest available precision
9960
9961 if Etype (Right_Opnd (Operand)) = Universal_Real then
9962 Rop := New_Copy_Tree (Right_Opnd (Operand));
9963 else
9964 Rop := New_Copy_Tree (Left_Opnd (Operand));
9965 end if;
9966
9967 Resolve (Rop, Universal_Real);
9968
9969 -- If the operand is a literal (it could be a non-static and
9970 -- illegal exponentiation) check whether the use of Duration
9971 -- is potentially inaccurate.
9972
9973 if Nkind (Rop) = N_Real_Literal
9974 and then Realval (Rop) /= Ureal_0
9975 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
9976 then
9977 Error_Msg_N
9978 ("??universal real operand can only "
9979 & "be interpreted as Duration!", Rop);
9980 Error_Msg_N
9981 ("\??precision will be lost in the conversion!", Rop);
9982 end if;
9983
9984 elsif Is_Numeric_Type (Typ)
9985 and then Nkind (Operand) in N_Op
9986 and then Unique_Fixed_Point_Type (N) /= Any_Type
9987 then
9988 Set_Etype (Operand, Standard_Duration);
9989
9990 else
9991 Error_Msg_N ("invalid context for mixed mode operation", N);
9992 Set_Etype (Operand, Any_Type);
9993 return;
9994 end if;
9995 end if;
9996
9997 Resolve (Operand);
9998
9999 -- In SPARK, a type conversion between array types should be restricted
10000 -- to types which have matching static bounds.
10001
10002 -- Protect call to Matching_Static_Array_Bounds to avoid costly
10003 -- operation if not needed.
10004
10005 if Restriction_Check_Required (SPARK_05)
10006 and then Is_Array_Type (Target_Typ)
10007 and then Is_Array_Type (Operand_Typ)
10008 and then Operand_Typ /= Any_Composite -- or else Operand in error
10009 and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ)
10010 then
10011 Check_SPARK_Restriction
10012 ("array types should have matching static bounds", N);
10013 end if;
10014
10015 -- In formal mode, the operand of an ancestor type conversion must be an
10016 -- object (not an expression).
10017
10018 if Is_Tagged_Type (Target_Typ)
10019 and then not Is_Class_Wide_Type (Target_Typ)
10020 and then Is_Tagged_Type (Operand_Typ)
10021 and then not Is_Class_Wide_Type (Operand_Typ)
10022 and then Is_Ancestor (Target_Typ, Operand_Typ)
10023 and then not Is_SPARK_Object_Reference (Operand)
10024 then
10025 Check_SPARK_Restriction ("object required", Operand);
10026 end if;
10027
10028 Analyze_Dimension (N);
10029
10030 -- Note: we do the Eval_Type_Conversion call before applying the
10031 -- required checks for a subtype conversion. This is important, since
10032 -- both are prepared under certain circumstances to change the type
10033 -- conversion to a constraint error node, but in the case of
10034 -- Eval_Type_Conversion this may reflect an illegality in the static
10035 -- case, and we would miss the illegality (getting only a warning
10036 -- message), if we applied the type conversion checks first.
10037
10038 Eval_Type_Conversion (N);
10039
10040 -- Even when evaluation is not possible, we may be able to simplify the
10041 -- conversion or its expression. This needs to be done before applying
10042 -- checks, since otherwise the checks may use the original expression
10043 -- and defeat the simplifications. This is specifically the case for
10044 -- elimination of the floating-point Truncation attribute in
10045 -- float-to-int conversions.
10046
10047 Simplify_Type_Conversion (N);
10048
10049 -- If after evaluation we still have a type conversion, then we may need
10050 -- to apply checks required for a subtype conversion.
10051
10052 -- Skip these type conversion checks if universal fixed operands
10053 -- operands involved, since range checks are handled separately for
10054 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
10055
10056 if Nkind (N) = N_Type_Conversion
10057 and then not Is_Generic_Type (Root_Type (Target_Typ))
10058 and then Target_Typ /= Universal_Fixed
10059 and then Operand_Typ /= Universal_Fixed
10060 then
10061 Apply_Type_Conversion_Checks (N);
10062 end if;
10063
10064 -- Issue warning for conversion of simple object to its own type. We
10065 -- have to test the original nodes, since they may have been rewritten
10066 -- by various optimizations.
10067
10068 Orig_N := Original_Node (N);
10069
10070 -- Here we test for a redundant conversion if the warning mode is
10071 -- active (and was not locally reset), and we have a type conversion
10072 -- from source not appearing in a generic instance.
10073
10074 if Test_Redundant
10075 and then Nkind (Orig_N) = N_Type_Conversion
10076 and then Comes_From_Source (Orig_N)
10077 and then not In_Instance
10078 then
10079 Orig_N := Original_Node (Expression (Orig_N));
10080 Orig_T := Target_Typ;
10081
10082 -- If the node is part of a larger expression, the Target_Type
10083 -- may not be the original type of the node if the context is a
10084 -- condition. Recover original type to see if conversion is needed.
10085
10086 if Is_Boolean_Type (Orig_T)
10087 and then Nkind (Parent (N)) in N_Op
10088 then
10089 Orig_T := Etype (Parent (N));
10090 end if;
10091
10092 -- If we have an entity name, then give the warning if the entity
10093 -- is the right type, or if it is a loop parameter covered by the
10094 -- original type (that's needed because loop parameters have an
10095 -- odd subtype coming from the bounds).
10096
10097 if (Is_Entity_Name (Orig_N)
10098 and then
10099 (Etype (Entity (Orig_N)) = Orig_T
10100 or else
10101 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
10102 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
10103
10104 -- If not an entity, then type of expression must match
10105
10106 or else Etype (Orig_N) = Orig_T
10107 then
10108 -- One more check, do not give warning if the analyzed conversion
10109 -- has an expression with non-static bounds, and the bounds of the
10110 -- target are static. This avoids junk warnings in cases where the
10111 -- conversion is necessary to establish staticness, for example in
10112 -- a case statement.
10113
10114 if not Is_OK_Static_Subtype (Operand_Typ)
10115 and then Is_OK_Static_Subtype (Target_Typ)
10116 then
10117 null;
10118
10119 -- Finally, if this type conversion occurs in a context requiring
10120 -- a prefix, and the expression is a qualified expression then the
10121 -- type conversion is not redundant, since a qualified expression
10122 -- is not a prefix, whereas a type conversion is. For example, "X
10123 -- := T'(Funx(...)).Y;" is illegal because a selected component
10124 -- requires a prefix, but a type conversion makes it legal: "X :=
10125 -- T(T'(Funx(...))).Y;"
10126
10127 -- In Ada 2012, a qualified expression is a name, so this idiom is
10128 -- no longer needed, but we still suppress the warning because it
10129 -- seems unfriendly for warnings to pop up when you switch to the
10130 -- newer language version.
10131
10132 elsif Nkind (Orig_N) = N_Qualified_Expression
10133 and then Nkind_In (Parent (N), N_Attribute_Reference,
10134 N_Indexed_Component,
10135 N_Selected_Component,
10136 N_Slice,
10137 N_Explicit_Dereference)
10138 then
10139 null;
10140
10141 -- Never warn on conversion to Long_Long_Integer'Base since
10142 -- that is most likely an artifact of the extended overflow
10143 -- checking and comes from complex expanded code.
10144
10145 elsif Orig_T = Base_Type (Standard_Long_Long_Integer) then
10146 null;
10147
10148 -- Here we give the redundant conversion warning. If it is an
10149 -- entity, give the name of the entity in the message. If not,
10150 -- just mention the expression.
10151
10152 -- Shoudn't we test Warn_On_Redundant_Constructs here ???
10153
10154 else
10155 if Is_Entity_Name (Orig_N) then
10156 Error_Msg_Node_2 := Orig_T;
10157 Error_Msg_NE -- CODEFIX
10158 ("??redundant conversion, & is of type &!",
10159 N, Entity (Orig_N));
10160 else
10161 Error_Msg_NE
10162 ("??redundant conversion, expression is of type&!",
10163 N, Orig_T);
10164 end if;
10165 end if;
10166 end if;
10167 end if;
10168
10169 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
10170 -- No need to perform any interface conversion if the type of the
10171 -- expression coincides with the target type.
10172
10173 if Ada_Version >= Ada_2005
10174 and then Expander_Active
10175 and then Operand_Typ /= Target_Typ
10176 then
10177 declare
10178 Opnd : Entity_Id := Operand_Typ;
10179 Target : Entity_Id := Target_Typ;
10180
10181 begin
10182 if Is_Access_Type (Opnd) then
10183 Opnd := Designated_Type (Opnd);
10184 end if;
10185
10186 if Is_Access_Type (Target_Typ) then
10187 Target := Designated_Type (Target);
10188 end if;
10189
10190 if Opnd = Target then
10191 null;
10192
10193 -- Conversion from interface type
10194
10195 elsif Is_Interface (Opnd) then
10196
10197 -- Ada 2005 (AI-217): Handle entities from limited views
10198
10199 if From_Limited_With (Opnd) then
10200 Error_Msg_Qual_Level := 99;
10201 Error_Msg_NE -- CODEFIX
10202 ("missing WITH clause on package &", N,
10203 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
10204 Error_Msg_N
10205 ("type conversions require visibility of the full view",
10206 N);
10207
10208 elsif From_Limited_With (Target)
10209 and then not
10210 (Is_Access_Type (Target_Typ)
10211 and then Present (Non_Limited_View (Etype (Target))))
10212 then
10213 Error_Msg_Qual_Level := 99;
10214 Error_Msg_NE -- CODEFIX
10215 ("missing WITH clause on package &", N,
10216 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
10217 Error_Msg_N
10218 ("type conversions require visibility of the full view",
10219 N);
10220
10221 else
10222 Expand_Interface_Conversion (N);
10223 end if;
10224
10225 -- Conversion to interface type
10226
10227 elsif Is_Interface (Target) then
10228
10229 -- Handle subtypes
10230
10231 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
10232 Opnd := Etype (Opnd);
10233 end if;
10234
10235 if Is_Class_Wide_Type (Opnd)
10236 or else Interface_Present_In_Ancestor
10237 (Typ => Opnd,
10238 Iface => Target)
10239 then
10240 Expand_Interface_Conversion (N);
10241 else
10242 Error_Msg_Name_1 := Chars (Etype (Target));
10243 Error_Msg_Name_2 := Chars (Opnd);
10244 Error_Msg_N
10245 ("wrong interface conversion (% is not a progenitor "
10246 & "of %)", N);
10247 end if;
10248 end if;
10249 end;
10250 end if;
10251
10252 -- Ada 2012: if target type has predicates, the result requires a
10253 -- predicate check. If the context is a call to another predicate
10254 -- check we must prevent infinite recursion.
10255
10256 if Has_Predicates (Target_Typ) then
10257 if Nkind (Parent (N)) = N_Function_Call
10258 and then Present (Name (Parent (N)))
10259 and then (Is_Predicate_Function (Entity (Name (Parent (N))))
10260 or else
10261 Is_Predicate_Function_M (Entity (Name (Parent (N)))))
10262 then
10263 null;
10264
10265 else
10266 Apply_Predicate_Check (N, Target_Typ);
10267 end if;
10268 end if;
10269 end Resolve_Type_Conversion;
10270
10271 ----------------------
10272 -- Resolve_Unary_Op --
10273 ----------------------
10274
10275 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
10276 B_Typ : constant Entity_Id := Base_Type (Typ);
10277 R : constant Node_Id := Right_Opnd (N);
10278 OK : Boolean;
10279 Lo : Uint;
10280 Hi : Uint;
10281
10282 begin
10283 if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then
10284 Error_Msg_Name_1 := Chars (Typ);
10285 Check_SPARK_Restriction
10286 ("unary operator not defined for modular type%", N);
10287 end if;
10288
10289 -- Deal with intrinsic unary operators
10290
10291 if Comes_From_Source (N)
10292 and then Ekind (Entity (N)) = E_Function
10293 and then Is_Imported (Entity (N))
10294 and then Is_Intrinsic_Subprogram (Entity (N))
10295 then
10296 Resolve_Intrinsic_Unary_Operator (N, Typ);
10297 return;
10298 end if;
10299
10300 -- Deal with universal cases
10301
10302 if Etype (R) = Universal_Integer
10303 or else
10304 Etype (R) = Universal_Real
10305 then
10306 Check_For_Visible_Operator (N, B_Typ);
10307 end if;
10308
10309 Set_Etype (N, B_Typ);
10310 Resolve (R, B_Typ);
10311
10312 -- Generate warning for expressions like abs (x mod 2)
10313
10314 if Warn_On_Redundant_Constructs
10315 and then Nkind (N) = N_Op_Abs
10316 then
10317 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
10318
10319 if OK and then Hi >= Lo and then Lo >= 0 then
10320 Error_Msg_N -- CODEFIX
10321 ("?r?abs applied to known non-negative value has no effect", N);
10322 end if;
10323 end if;
10324
10325 -- Deal with reference generation
10326
10327 Check_Unset_Reference (R);
10328 Generate_Operator_Reference (N, B_Typ);
10329 Analyze_Dimension (N);
10330 Eval_Unary_Op (N);
10331
10332 -- Set overflow checking bit. Much cleverer code needed here eventually
10333 -- and perhaps the Resolve routines should be separated for the various
10334 -- arithmetic operations, since they will need different processing ???
10335
10336 if Nkind (N) in N_Op then
10337 if not Overflow_Checks_Suppressed (Etype (N)) then
10338 Enable_Overflow_Check (N);
10339 end if;
10340 end if;
10341
10342 -- Generate warning for expressions like -5 mod 3 for integers. No need
10343 -- to worry in the floating-point case, since parens do not affect the
10344 -- result so there is no point in giving in a warning.
10345
10346 declare
10347 Norig : constant Node_Id := Original_Node (N);
10348 Rorig : Node_Id;
10349 Val : Uint;
10350 HB : Uint;
10351 LB : Uint;
10352 Lval : Uint;
10353 Opnd : Node_Id;
10354
10355 begin
10356 if Warn_On_Questionable_Missing_Parens
10357 and then Comes_From_Source (Norig)
10358 and then Is_Integer_Type (Typ)
10359 and then Nkind (Norig) = N_Op_Minus
10360 then
10361 Rorig := Original_Node (Right_Opnd (Norig));
10362
10363 -- We are looking for cases where the right operand is not
10364 -- parenthesized, and is a binary operator, multiply, divide, or
10365 -- mod. These are the cases where the grouping can affect results.
10366
10367 if Paren_Count (Rorig) = 0
10368 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
10369 then
10370 -- For mod, we always give the warning, since the value is
10371 -- affected by the parenthesization (e.g. (-5) mod 315 /=
10372 -- -(5 mod 315)). But for the other cases, the only concern is
10373 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
10374 -- overflows, but (-2) * 64 does not). So we try to give the
10375 -- message only when overflow is possible.
10376
10377 if Nkind (Rorig) /= N_Op_Mod
10378 and then Compile_Time_Known_Value (R)
10379 then
10380 Val := Expr_Value (R);
10381
10382 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
10383 HB := Expr_Value (Type_High_Bound (Typ));
10384 else
10385 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
10386 end if;
10387
10388 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
10389 LB := Expr_Value (Type_Low_Bound (Typ));
10390 else
10391 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
10392 end if;
10393
10394 -- Note that the test below is deliberately excluding the
10395 -- largest negative number, since that is a potentially
10396 -- troublesome case (e.g. -2 * x, where the result is the
10397 -- largest negative integer has an overflow with 2 * x).
10398
10399 if Val > LB and then Val <= HB then
10400 return;
10401 end if;
10402 end if;
10403
10404 -- For the multiplication case, the only case we have to worry
10405 -- about is when (-a)*b is exactly the largest negative number
10406 -- so that -(a*b) can cause overflow. This can only happen if
10407 -- a is a power of 2, and more generally if any operand is a
10408 -- constant that is not a power of 2, then the parentheses
10409 -- cannot affect whether overflow occurs. We only bother to
10410 -- test the left most operand
10411
10412 -- Loop looking at left operands for one that has known value
10413
10414 Opnd := Rorig;
10415 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
10416 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
10417 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
10418
10419 -- Operand value of 0 or 1 skips warning
10420
10421 if Lval <= 1 then
10422 return;
10423
10424 -- Otherwise check power of 2, if power of 2, warn, if
10425 -- anything else, skip warning.
10426
10427 else
10428 while Lval /= 2 loop
10429 if Lval mod 2 = 1 then
10430 return;
10431 else
10432 Lval := Lval / 2;
10433 end if;
10434 end loop;
10435
10436 exit Opnd_Loop;
10437 end if;
10438 end if;
10439
10440 -- Keep looking at left operands
10441
10442 Opnd := Left_Opnd (Opnd);
10443 end loop Opnd_Loop;
10444
10445 -- For rem or "/" we can only have a problematic situation
10446 -- if the divisor has a value of minus one or one. Otherwise
10447 -- overflow is impossible (divisor > 1) or we have a case of
10448 -- division by zero in any case.
10449
10450 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
10451 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
10452 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
10453 then
10454 return;
10455 end if;
10456
10457 -- If we fall through warning should be issued
10458
10459 -- Shouldn't we test Warn_On_Questionable_Missing_Parens ???
10460
10461 Error_Msg_N
10462 ("??unary minus expression should be parenthesized here!", N);
10463 end if;
10464 end if;
10465 end;
10466 end Resolve_Unary_Op;
10467
10468 ----------------------------------
10469 -- Resolve_Unchecked_Expression --
10470 ----------------------------------
10471
10472 procedure Resolve_Unchecked_Expression
10473 (N : Node_Id;
10474 Typ : Entity_Id)
10475 is
10476 begin
10477 Resolve (Expression (N), Typ, Suppress => All_Checks);
10478 Set_Etype (N, Typ);
10479 end Resolve_Unchecked_Expression;
10480
10481 ---------------------------------------
10482 -- Resolve_Unchecked_Type_Conversion --
10483 ---------------------------------------
10484
10485 procedure Resolve_Unchecked_Type_Conversion
10486 (N : Node_Id;
10487 Typ : Entity_Id)
10488 is
10489 pragma Warnings (Off, Typ);
10490
10491 Operand : constant Node_Id := Expression (N);
10492 Opnd_Type : constant Entity_Id := Etype (Operand);
10493
10494 begin
10495 -- Resolve operand using its own type
10496
10497 Resolve (Operand, Opnd_Type);
10498 Analyze_Dimension (N);
10499 Eval_Unchecked_Conversion (N);
10500 end Resolve_Unchecked_Type_Conversion;
10501
10502 ------------------------------
10503 -- Rewrite_Operator_As_Call --
10504 ------------------------------
10505
10506 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
10507 Loc : constant Source_Ptr := Sloc (N);
10508 Actuals : constant List_Id := New_List;
10509 New_N : Node_Id;
10510
10511 begin
10512 if Nkind (N) in N_Binary_Op then
10513 Append (Left_Opnd (N), Actuals);
10514 end if;
10515
10516 Append (Right_Opnd (N), Actuals);
10517
10518 New_N :=
10519 Make_Function_Call (Sloc => Loc,
10520 Name => New_Occurrence_Of (Nam, Loc),
10521 Parameter_Associations => Actuals);
10522
10523 Preserve_Comes_From_Source (New_N, N);
10524 Preserve_Comes_From_Source (Name (New_N), N);
10525 Rewrite (N, New_N);
10526 Set_Etype (N, Etype (Nam));
10527 end Rewrite_Operator_As_Call;
10528
10529 ------------------------------
10530 -- Rewrite_Renamed_Operator --
10531 ------------------------------
10532
10533 procedure Rewrite_Renamed_Operator
10534 (N : Node_Id;
10535 Op : Entity_Id;
10536 Typ : Entity_Id)
10537 is
10538 Nam : constant Name_Id := Chars (Op);
10539 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
10540 Op_Node : Node_Id;
10541
10542 begin
10543 -- Do not perform this transformation within a pre/postcondition,
10544 -- because the expression will be re-analyzed, and the transformation
10545 -- might affect the visibility of the operator, e.g. in an instance.
10546
10547 if In_Assertion_Expr > 0 then
10548 return;
10549 end if;
10550
10551 -- Rewrite the operator node using the real operator, not its renaming.
10552 -- Exclude user-defined intrinsic operations of the same name, which are
10553 -- treated separately and rewritten as calls.
10554
10555 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
10556 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
10557 Set_Chars (Op_Node, Nam);
10558 Set_Etype (Op_Node, Etype (N));
10559 Set_Entity (Op_Node, Op);
10560 Set_Right_Opnd (Op_Node, Right_Opnd (N));
10561
10562 -- Indicate that both the original entity and its renaming are
10563 -- referenced at this point.
10564
10565 Generate_Reference (Entity (N), N);
10566 Generate_Reference (Op, N);
10567
10568 if Is_Binary then
10569 Set_Left_Opnd (Op_Node, Left_Opnd (N));
10570 end if;
10571
10572 Rewrite (N, Op_Node);
10573
10574 -- If the context type is private, add the appropriate conversions so
10575 -- that the operator is applied to the full view. This is done in the
10576 -- routines that resolve intrinsic operators.
10577
10578 if Is_Intrinsic_Subprogram (Op)
10579 and then Is_Private_Type (Typ)
10580 then
10581 case Nkind (N) is
10582 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
10583 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
10584 Resolve_Intrinsic_Operator (N, Typ);
10585
10586 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
10587 Resolve_Intrinsic_Unary_Operator (N, Typ);
10588
10589 when others =>
10590 Resolve (N, Typ);
10591 end case;
10592 end if;
10593
10594 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
10595
10596 -- Operator renames a user-defined operator of the same name. Use the
10597 -- original operator in the node, which is the one Gigi knows about.
10598
10599 Set_Entity (N, Op);
10600 Set_Is_Overloaded (N, False);
10601 end if;
10602 end Rewrite_Renamed_Operator;
10603
10604 -----------------------
10605 -- Set_Slice_Subtype --
10606 -----------------------
10607
10608 -- Build an implicit subtype declaration to represent the type delivered by
10609 -- the slice. This is an abbreviated version of an array subtype. We define
10610 -- an index subtype for the slice, using either the subtype name or the
10611 -- discrete range of the slice. To be consistent with index usage elsewhere
10612 -- we create a list header to hold the single index. This list is not
10613 -- otherwise attached to the syntax tree.
10614
10615 procedure Set_Slice_Subtype (N : Node_Id) is
10616 Loc : constant Source_Ptr := Sloc (N);
10617 Index_List : constant List_Id := New_List;
10618 Index : Node_Id;
10619 Index_Subtype : Entity_Id;
10620 Index_Type : Entity_Id;
10621 Slice_Subtype : Entity_Id;
10622 Drange : constant Node_Id := Discrete_Range (N);
10623
10624 begin
10625 Index_Type := Base_Type (Etype (Drange));
10626
10627 if Is_Entity_Name (Drange) then
10628 Index_Subtype := Entity (Drange);
10629
10630 else
10631 -- We force the evaluation of a range. This is definitely needed in
10632 -- the renamed case, and seems safer to do unconditionally. Note in
10633 -- any case that since we will create and insert an Itype referring
10634 -- to this range, we must make sure any side effect removal actions
10635 -- are inserted before the Itype definition.
10636
10637 if Nkind (Drange) = N_Range then
10638 Force_Evaluation (Low_Bound (Drange));
10639 Force_Evaluation (High_Bound (Drange));
10640
10641 -- If the discrete range is given by a subtype indication, the
10642 -- type of the slice is the base of the subtype mark.
10643
10644 elsif Nkind (Drange) = N_Subtype_Indication then
10645 declare
10646 R : constant Node_Id := Range_Expression (Constraint (Drange));
10647 begin
10648 Index_Type := Base_Type (Entity (Subtype_Mark (Drange)));
10649 Force_Evaluation (Low_Bound (R));
10650 Force_Evaluation (High_Bound (R));
10651 end;
10652 end if;
10653
10654 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
10655
10656 -- Take a new copy of Drange (where bounds have been rewritten to
10657 -- reference side-effect-free names). Using a separate tree ensures
10658 -- that further expansion (e.g. while rewriting a slice assignment
10659 -- into a FOR loop) does not attempt to remove side effects on the
10660 -- bounds again (which would cause the bounds in the index subtype
10661 -- definition to refer to temporaries before they are defined) (the
10662 -- reason is that some names are considered side effect free here
10663 -- for the subtype, but not in the context of a loop iteration
10664 -- scheme).
10665
10666 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
10667 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
10668 Set_Etype (Index_Subtype, Index_Type);
10669 Set_Size_Info (Index_Subtype, Index_Type);
10670 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
10671 end if;
10672
10673 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
10674
10675 Index := New_Occurrence_Of (Index_Subtype, Loc);
10676 Set_Etype (Index, Index_Subtype);
10677 Append (Index, Index_List);
10678
10679 Set_First_Index (Slice_Subtype, Index);
10680 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
10681 Set_Is_Constrained (Slice_Subtype, True);
10682
10683 Check_Compile_Time_Size (Slice_Subtype);
10684
10685 -- The Etype of the existing Slice node is reset to this slice subtype.
10686 -- Its bounds are obtained from its first index.
10687
10688 Set_Etype (N, Slice_Subtype);
10689
10690 -- For packed slice subtypes, freeze immediately (except in the case of
10691 -- being in a "spec expression" where we never freeze when we first see
10692 -- the expression).
10693
10694 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
10695 Freeze_Itype (Slice_Subtype, N);
10696
10697 -- For all other cases insert an itype reference in the slice's actions
10698 -- so that the itype is frozen at the proper place in the tree (i.e. at
10699 -- the point where actions for the slice are analyzed). Note that this
10700 -- is different from freezing the itype immediately, which might be
10701 -- premature (e.g. if the slice is within a transient scope). This needs
10702 -- to be done only if expansion is enabled.
10703
10704 elsif Expander_Active then
10705 Ensure_Defined (Typ => Slice_Subtype, N => N);
10706 end if;
10707 end Set_Slice_Subtype;
10708
10709 --------------------------------
10710 -- Set_String_Literal_Subtype --
10711 --------------------------------
10712
10713 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
10714 Loc : constant Source_Ptr := Sloc (N);
10715 Low_Bound : constant Node_Id :=
10716 Type_Low_Bound (Etype (First_Index (Typ)));
10717 Subtype_Id : Entity_Id;
10718
10719 begin
10720 if Nkind (N) /= N_String_Literal then
10721 return;
10722 end if;
10723
10724 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
10725 Set_String_Literal_Length (Subtype_Id, UI_From_Int
10726 (String_Length (Strval (N))));
10727 Set_Etype (Subtype_Id, Base_Type (Typ));
10728 Set_Is_Constrained (Subtype_Id);
10729 Set_Etype (N, Subtype_Id);
10730
10731 -- The low bound is set from the low bound of the corresponding index
10732 -- type. Note that we do not store the high bound in the string literal
10733 -- subtype, but it can be deduced if necessary from the length and the
10734 -- low bound.
10735
10736 if Is_OK_Static_Expression (Low_Bound) then
10737 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
10738
10739 -- If the lower bound is not static we create a range for the string
10740 -- literal, using the index type and the known length of the literal.
10741 -- The index type is not necessarily Positive, so the upper bound is
10742 -- computed as T'Val (T'Pos (Low_Bound) + L - 1).
10743
10744 else
10745 declare
10746 Index_List : constant List_Id := New_List;
10747 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
10748 High_Bound : constant Node_Id :=
10749 Make_Attribute_Reference (Loc,
10750 Attribute_Name => Name_Val,
10751 Prefix =>
10752 New_Occurrence_Of (Index_Type, Loc),
10753 Expressions => New_List (
10754 Make_Op_Add (Loc,
10755 Left_Opnd =>
10756 Make_Attribute_Reference (Loc,
10757 Attribute_Name => Name_Pos,
10758 Prefix =>
10759 New_Occurrence_Of (Index_Type, Loc),
10760 Expressions =>
10761 New_List (New_Copy_Tree (Low_Bound))),
10762 Right_Opnd =>
10763 Make_Integer_Literal (Loc,
10764 String_Length (Strval (N)) - 1))));
10765
10766 Array_Subtype : Entity_Id;
10767 Drange : Node_Id;
10768 Index : Node_Id;
10769 Index_Subtype : Entity_Id;
10770
10771 begin
10772 if Is_Integer_Type (Index_Type) then
10773 Set_String_Literal_Low_Bound
10774 (Subtype_Id, Make_Integer_Literal (Loc, 1));
10775
10776 else
10777 -- If the index type is an enumeration type, build bounds
10778 -- expression with attributes.
10779
10780 Set_String_Literal_Low_Bound
10781 (Subtype_Id,
10782 Make_Attribute_Reference (Loc,
10783 Attribute_Name => Name_First,
10784 Prefix =>
10785 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
10786 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Index_Type);
10787 end if;
10788
10789 Analyze_And_Resolve (String_Literal_Low_Bound (Subtype_Id));
10790
10791 -- Build bona fide subtype for the string, and wrap it in an
10792 -- unchecked conversion, because the backend expects the
10793 -- String_Literal_Subtype to have a static lower bound.
10794
10795 Index_Subtype :=
10796 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
10797 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
10798 Set_Scalar_Range (Index_Subtype, Drange);
10799 Set_Parent (Drange, N);
10800 Analyze_And_Resolve (Drange, Index_Type);
10801
10802 -- In the context, the Index_Type may already have a constraint,
10803 -- so use common base type on string subtype. The base type may
10804 -- be used when generating attributes of the string, for example
10805 -- in the context of a slice assignment.
10806
10807 Set_Etype (Index_Subtype, Base_Type (Index_Type));
10808 Set_Size_Info (Index_Subtype, Index_Type);
10809 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
10810
10811 Array_Subtype := Create_Itype (E_Array_Subtype, N);
10812
10813 Index := New_Occurrence_Of (Index_Subtype, Loc);
10814 Set_Etype (Index, Index_Subtype);
10815 Append (Index, Index_List);
10816
10817 Set_First_Index (Array_Subtype, Index);
10818 Set_Etype (Array_Subtype, Base_Type (Typ));
10819 Set_Is_Constrained (Array_Subtype, True);
10820
10821 Rewrite (N,
10822 Make_Unchecked_Type_Conversion (Loc,
10823 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
10824 Expression => Relocate_Node (N)));
10825 Set_Etype (N, Array_Subtype);
10826 end;
10827 end if;
10828 end Set_String_Literal_Subtype;
10829
10830 ------------------------------
10831 -- Simplify_Type_Conversion --
10832 ------------------------------
10833
10834 procedure Simplify_Type_Conversion (N : Node_Id) is
10835 begin
10836 if Nkind (N) = N_Type_Conversion then
10837 declare
10838 Operand : constant Node_Id := Expression (N);
10839 Target_Typ : constant Entity_Id := Etype (N);
10840 Opnd_Typ : constant Entity_Id := Etype (Operand);
10841
10842 begin
10843 if Is_Floating_Point_Type (Opnd_Typ)
10844 and then
10845 (Is_Integer_Type (Target_Typ)
10846 or else (Is_Fixed_Point_Type (Target_Typ)
10847 and then Conversion_OK (N)))
10848 and then Nkind (Operand) = N_Attribute_Reference
10849 and then Attribute_Name (Operand) = Name_Truncation
10850
10851 -- Special processing required if the conversion is the expression
10852 -- of a Truncation attribute reference. In this case we replace:
10853
10854 -- ityp (ftyp'Truncation (x))
10855
10856 -- by
10857
10858 -- ityp (x)
10859
10860 -- with the Float_Truncate flag set, which is more efficient.
10861
10862 then
10863 Rewrite (Operand,
10864 Relocate_Node (First (Expressions (Operand))));
10865 Set_Float_Truncate (N, True);
10866 end if;
10867 end;
10868 end if;
10869 end Simplify_Type_Conversion;
10870
10871 -----------------------------
10872 -- Unique_Fixed_Point_Type --
10873 -----------------------------
10874
10875 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
10876 T1 : Entity_Id := Empty;
10877 T2 : Entity_Id;
10878 Item : Node_Id;
10879 Scop : Entity_Id;
10880
10881 procedure Fixed_Point_Error;
10882 -- Give error messages for true ambiguity. Messages are posted on node
10883 -- N, and entities T1, T2 are the possible interpretations.
10884
10885 -----------------------
10886 -- Fixed_Point_Error --
10887 -----------------------
10888
10889 procedure Fixed_Point_Error is
10890 begin
10891 Error_Msg_N ("ambiguous universal_fixed_expression", N);
10892 Error_Msg_NE ("\\possible interpretation as}", N, T1);
10893 Error_Msg_NE ("\\possible interpretation as}", N, T2);
10894 end Fixed_Point_Error;
10895
10896 -- Start of processing for Unique_Fixed_Point_Type
10897
10898 begin
10899 -- The operations on Duration are visible, so Duration is always a
10900 -- possible interpretation.
10901
10902 T1 := Standard_Duration;
10903
10904 -- Look for fixed-point types in enclosing scopes
10905
10906 Scop := Current_Scope;
10907 while Scop /= Standard_Standard loop
10908 T2 := First_Entity (Scop);
10909 while Present (T2) loop
10910 if Is_Fixed_Point_Type (T2)
10911 and then Current_Entity (T2) = T2
10912 and then Scope (Base_Type (T2)) = Scop
10913 then
10914 if Present (T1) then
10915 Fixed_Point_Error;
10916 return Any_Type;
10917 else
10918 T1 := T2;
10919 end if;
10920 end if;
10921
10922 Next_Entity (T2);
10923 end loop;
10924
10925 Scop := Scope (Scop);
10926 end loop;
10927
10928 -- Look for visible fixed type declarations in the context
10929
10930 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
10931 while Present (Item) loop
10932 if Nkind (Item) = N_With_Clause then
10933 Scop := Entity (Name (Item));
10934 T2 := First_Entity (Scop);
10935 while Present (T2) loop
10936 if Is_Fixed_Point_Type (T2)
10937 and then Scope (Base_Type (T2)) = Scop
10938 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
10939 then
10940 if Present (T1) then
10941 Fixed_Point_Error;
10942 return Any_Type;
10943 else
10944 T1 := T2;
10945 end if;
10946 end if;
10947
10948 Next_Entity (T2);
10949 end loop;
10950 end if;
10951
10952 Next (Item);
10953 end loop;
10954
10955 if Nkind (N) = N_Real_Literal then
10956 Error_Msg_NE
10957 ("??real literal interpreted as }!", N, T1);
10958 else
10959 Error_Msg_NE
10960 ("??universal_fixed expression interpreted as }!", N, T1);
10961 end if;
10962
10963 return T1;
10964 end Unique_Fixed_Point_Type;
10965
10966 ----------------------
10967 -- Valid_Conversion --
10968 ----------------------
10969
10970 function Valid_Conversion
10971 (N : Node_Id;
10972 Target : Entity_Id;
10973 Operand : Node_Id;
10974 Report_Errs : Boolean := True) return Boolean
10975 is
10976 Target_Type : constant Entity_Id := Base_Type (Target);
10977 Opnd_Type : Entity_Id := Etype (Operand);
10978 Inc_Ancestor : Entity_Id;
10979
10980 function Conversion_Check
10981 (Valid : Boolean;
10982 Msg : String) return Boolean;
10983 -- Little routine to post Msg if Valid is False, returns Valid value
10984
10985 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id);
10986 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
10987
10988 procedure Conversion_Error_NE
10989 (Msg : String;
10990 N : Node_Or_Entity_Id;
10991 E : Node_Or_Entity_Id);
10992 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
10993
10994 function Valid_Tagged_Conversion
10995 (Target_Type : Entity_Id;
10996 Opnd_Type : Entity_Id) return Boolean;
10997 -- Specifically test for validity of tagged conversions
10998
10999 function Valid_Array_Conversion return Boolean;
11000 -- Check index and component conformance, and accessibility levels if
11001 -- the component types are anonymous access types (Ada 2005).
11002
11003 ----------------------
11004 -- Conversion_Check --
11005 ----------------------
11006
11007 function Conversion_Check
11008 (Valid : Boolean;
11009 Msg : String) return Boolean
11010 is
11011 begin
11012 if not Valid
11013
11014 -- A generic unit has already been analyzed and we have verified
11015 -- that a particular conversion is OK in that context. Since the
11016 -- instance is reanalyzed without relying on the relationships
11017 -- established during the analysis of the generic, it is possible
11018 -- to end up with inconsistent views of private types. Do not emit
11019 -- the error message in such cases. The rest of the machinery in
11020 -- Valid_Conversion still ensures the proper compatibility of
11021 -- target and operand types.
11022
11023 and then not In_Instance
11024 then
11025 Conversion_Error_N (Msg, Operand);
11026 end if;
11027
11028 return Valid;
11029 end Conversion_Check;
11030
11031 ------------------------
11032 -- Conversion_Error_N --
11033 ------------------------
11034
11035 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id) is
11036 begin
11037 if Report_Errs then
11038 Error_Msg_N (Msg, N);
11039 end if;
11040 end Conversion_Error_N;
11041
11042 -------------------------
11043 -- Conversion_Error_NE --
11044 -------------------------
11045
11046 procedure Conversion_Error_NE
11047 (Msg : String;
11048 N : Node_Or_Entity_Id;
11049 E : Node_Or_Entity_Id)
11050 is
11051 begin
11052 if Report_Errs then
11053 Error_Msg_NE (Msg, N, E);
11054 end if;
11055 end Conversion_Error_NE;
11056
11057 ----------------------------
11058 -- Valid_Array_Conversion --
11059 ----------------------------
11060
11061 function Valid_Array_Conversion return Boolean
11062 is
11063 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
11064 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
11065
11066 Opnd_Index : Node_Id;
11067 Opnd_Index_Type : Entity_Id;
11068
11069 Target_Comp_Type : constant Entity_Id :=
11070 Component_Type (Target_Type);
11071 Target_Comp_Base : constant Entity_Id :=
11072 Base_Type (Target_Comp_Type);
11073
11074 Target_Index : Node_Id;
11075 Target_Index_Type : Entity_Id;
11076
11077 begin
11078 -- Error if wrong number of dimensions
11079
11080 if
11081 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
11082 then
11083 Conversion_Error_N
11084 ("incompatible number of dimensions for conversion", Operand);
11085 return False;
11086
11087 -- Number of dimensions matches
11088
11089 else
11090 -- Loop through indexes of the two arrays
11091
11092 Target_Index := First_Index (Target_Type);
11093 Opnd_Index := First_Index (Opnd_Type);
11094 while Present (Target_Index) and then Present (Opnd_Index) loop
11095 Target_Index_Type := Etype (Target_Index);
11096 Opnd_Index_Type := Etype (Opnd_Index);
11097
11098 -- Error if index types are incompatible
11099
11100 if not (Is_Integer_Type (Target_Index_Type)
11101 and then Is_Integer_Type (Opnd_Index_Type))
11102 and then (Root_Type (Target_Index_Type)
11103 /= Root_Type (Opnd_Index_Type))
11104 then
11105 Conversion_Error_N
11106 ("incompatible index types for array conversion",
11107 Operand);
11108 return False;
11109 end if;
11110
11111 Next_Index (Target_Index);
11112 Next_Index (Opnd_Index);
11113 end loop;
11114
11115 -- If component types have same base type, all set
11116
11117 if Target_Comp_Base = Opnd_Comp_Base then
11118 null;
11119
11120 -- Here if base types of components are not the same. The only
11121 -- time this is allowed is if we have anonymous access types.
11122
11123 -- The conversion of arrays of anonymous access types can lead
11124 -- to dangling pointers. AI-392 formalizes the accessibility
11125 -- checks that must be applied to such conversions to prevent
11126 -- out-of-scope references.
11127
11128 elsif Ekind_In
11129 (Target_Comp_Base, E_Anonymous_Access_Type,
11130 E_Anonymous_Access_Subprogram_Type)
11131 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
11132 and then
11133 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
11134 then
11135 if Type_Access_Level (Target_Type) <
11136 Deepest_Type_Access_Level (Opnd_Type)
11137 then
11138 if In_Instance_Body then
11139 Error_Msg_Warn := SPARK_Mode /= On;
11140 Conversion_Error_N
11141 ("source array type has deeper accessibility "
11142 & "level than target<<", Operand);
11143 Conversion_Error_N ("\Program_Error [<<", Operand);
11144 Rewrite (N,
11145 Make_Raise_Program_Error (Sloc (N),
11146 Reason => PE_Accessibility_Check_Failed));
11147 Set_Etype (N, Target_Type);
11148 return False;
11149
11150 -- Conversion not allowed because of accessibility levels
11151
11152 else
11153 Conversion_Error_N
11154 ("source array type has deeper accessibility "
11155 & "level than target", Operand);
11156 return False;
11157 end if;
11158
11159 else
11160 null;
11161 end if;
11162
11163 -- All other cases where component base types do not match
11164
11165 else
11166 Conversion_Error_N
11167 ("incompatible component types for array conversion",
11168 Operand);
11169 return False;
11170 end if;
11171
11172 -- Check that component subtypes statically match. For numeric
11173 -- types this means that both must be either constrained or
11174 -- unconstrained. For enumeration types the bounds must match.
11175 -- All of this is checked in Subtypes_Statically_Match.
11176
11177 if not Subtypes_Statically_Match
11178 (Target_Comp_Type, Opnd_Comp_Type)
11179 then
11180 Conversion_Error_N
11181 ("component subtypes must statically match", Operand);
11182 return False;
11183 end if;
11184 end if;
11185
11186 return True;
11187 end Valid_Array_Conversion;
11188
11189 -----------------------------
11190 -- Valid_Tagged_Conversion --
11191 -----------------------------
11192
11193 function Valid_Tagged_Conversion
11194 (Target_Type : Entity_Id;
11195 Opnd_Type : Entity_Id) return Boolean
11196 is
11197 begin
11198 -- Upward conversions are allowed (RM 4.6(22))
11199
11200 if Covers (Target_Type, Opnd_Type)
11201 or else Is_Ancestor (Target_Type, Opnd_Type)
11202 then
11203 return True;
11204
11205 -- Downward conversion are allowed if the operand is class-wide
11206 -- (RM 4.6(23)).
11207
11208 elsif Is_Class_Wide_Type (Opnd_Type)
11209 and then Covers (Opnd_Type, Target_Type)
11210 then
11211 return True;
11212
11213 elsif Covers (Opnd_Type, Target_Type)
11214 or else Is_Ancestor (Opnd_Type, Target_Type)
11215 then
11216 return
11217 Conversion_Check (False,
11218 "downward conversion of tagged objects not allowed");
11219
11220 -- Ada 2005 (AI-251): The conversion to/from interface types is
11221 -- always valid
11222
11223 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
11224 return True;
11225
11226 -- If the operand is a class-wide type obtained through a limited_
11227 -- with clause, and the context includes the non-limited view, use
11228 -- it to determine whether the conversion is legal.
11229
11230 elsif Is_Class_Wide_Type (Opnd_Type)
11231 and then From_Limited_With (Opnd_Type)
11232 and then Present (Non_Limited_View (Etype (Opnd_Type)))
11233 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
11234 then
11235 return True;
11236
11237 elsif Is_Access_Type (Opnd_Type)
11238 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
11239 then
11240 return True;
11241
11242 else
11243 Conversion_Error_NE
11244 ("invalid tagged conversion, not compatible with}",
11245 N, First_Subtype (Opnd_Type));
11246 return False;
11247 end if;
11248 end Valid_Tagged_Conversion;
11249
11250 -- Start of processing for Valid_Conversion
11251
11252 begin
11253 Check_Parameterless_Call (Operand);
11254
11255 if Is_Overloaded (Operand) then
11256 declare
11257 I : Interp_Index;
11258 I1 : Interp_Index;
11259 It : Interp;
11260 It1 : Interp;
11261 N1 : Entity_Id;
11262 T1 : Entity_Id;
11263
11264 begin
11265 -- Remove procedure calls, which syntactically cannot appear in
11266 -- this context, but which cannot be removed by type checking,
11267 -- because the context does not impose a type.
11268
11269 -- When compiling for VMS, spurious ambiguities can be produced
11270 -- when arithmetic operations have a literal operand and return
11271 -- System.Address or a descendant of it. These ambiguities are
11272 -- otherwise resolved by the context, but for conversions there
11273 -- is no context type and the removal of the spurious operations
11274 -- must be done explicitly here.
11275
11276 -- The node may be labelled overloaded, but still contain only one
11277 -- interpretation because others were discarded earlier. If this
11278 -- is the case, retain the single interpretation if legal.
11279
11280 Get_First_Interp (Operand, I, It);
11281 Opnd_Type := It.Typ;
11282 Get_Next_Interp (I, It);
11283
11284 if Present (It.Typ)
11285 and then Opnd_Type /= Standard_Void_Type
11286 then
11287 -- More than one candidate interpretation is available
11288
11289 Get_First_Interp (Operand, I, It);
11290 while Present (It.Typ) loop
11291 if It.Typ = Standard_Void_Type then
11292 Remove_Interp (I);
11293 end if;
11294
11295 if Present (System_Aux_Id)
11296 and then Is_Descendent_Of_Address (It.Typ)
11297 then
11298 Remove_Interp (I);
11299 end if;
11300
11301 Get_Next_Interp (I, It);
11302 end loop;
11303 end if;
11304
11305 Get_First_Interp (Operand, I, It);
11306 I1 := I;
11307 It1 := It;
11308
11309 if No (It.Typ) then
11310 Conversion_Error_N ("illegal operand in conversion", Operand);
11311 return False;
11312 end if;
11313
11314 Get_Next_Interp (I, It);
11315
11316 if Present (It.Typ) then
11317 N1 := It1.Nam;
11318 T1 := It1.Typ;
11319 It1 := Disambiguate (Operand, I1, I, Any_Type);
11320
11321 if It1 = No_Interp then
11322 Conversion_Error_N
11323 ("ambiguous operand in conversion", Operand);
11324
11325 -- If the interpretation involves a standard operator, use
11326 -- the location of the type, which may be user-defined.
11327
11328 if Sloc (It.Nam) = Standard_Location then
11329 Error_Msg_Sloc := Sloc (It.Typ);
11330 else
11331 Error_Msg_Sloc := Sloc (It.Nam);
11332 end if;
11333
11334 Conversion_Error_N -- CODEFIX
11335 ("\\possible interpretation#!", Operand);
11336
11337 if Sloc (N1) = Standard_Location then
11338 Error_Msg_Sloc := Sloc (T1);
11339 else
11340 Error_Msg_Sloc := Sloc (N1);
11341 end if;
11342
11343 Conversion_Error_N -- CODEFIX
11344 ("\\possible interpretation#!", Operand);
11345
11346 return False;
11347 end if;
11348 end if;
11349
11350 Set_Etype (Operand, It1.Typ);
11351 Opnd_Type := It1.Typ;
11352 end;
11353 end if;
11354
11355 -- Deal with conversion of integer type to address if the pragma
11356 -- Allow_Integer_Address is in effect. We convert the conversion to
11357 -- an unchecked conversion in this case and we are all done.
11358
11359 if Address_Integer_Convert_OK (Opnd_Type, Target_Type) then
11360 Rewrite (N, Unchecked_Convert_To (Target_Type, Expression (N)));
11361 Analyze_And_Resolve (N, Target_Type);
11362 return True;
11363 end if;
11364
11365 -- If we are within a child unit, check whether the type of the
11366 -- expression has an ancestor in a parent unit, in which case it
11367 -- belongs to its derivation class even if the ancestor is private.
11368 -- See RM 7.3.1 (5.2/3).
11369
11370 Inc_Ancestor := Get_Incomplete_View_Of_Ancestor (Opnd_Type);
11371
11372 -- Numeric types
11373
11374 if Is_Numeric_Type (Target_Type) then
11375
11376 -- A universal fixed expression can be converted to any numeric type
11377
11378 if Opnd_Type = Universal_Fixed then
11379 return True;
11380
11381 -- Also no need to check when in an instance or inlined body, because
11382 -- the legality has been established when the template was analyzed.
11383 -- Furthermore, numeric conversions may occur where only a private
11384 -- view of the operand type is visible at the instantiation point.
11385 -- This results in a spurious error if we check that the operand type
11386 -- is a numeric type.
11387
11388 -- Note: in a previous version of this unit, the following tests were
11389 -- applied only for generated code (Comes_From_Source set to False),
11390 -- but in fact the test is required for source code as well, since
11391 -- this situation can arise in source code.
11392
11393 elsif In_Instance or else In_Inlined_Body then
11394 return True;
11395
11396 -- Otherwise we need the conversion check
11397
11398 else
11399 return Conversion_Check
11400 (Is_Numeric_Type (Opnd_Type)
11401 or else
11402 (Present (Inc_Ancestor)
11403 and then Is_Numeric_Type (Inc_Ancestor)),
11404 "illegal operand for numeric conversion");
11405 end if;
11406
11407 -- Array types
11408
11409 elsif Is_Array_Type (Target_Type) then
11410 if not Is_Array_Type (Opnd_Type)
11411 or else Opnd_Type = Any_Composite
11412 or else Opnd_Type = Any_String
11413 then
11414 Conversion_Error_N
11415 ("illegal operand for array conversion", Operand);
11416 return False;
11417
11418 else
11419 return Valid_Array_Conversion;
11420 end if;
11421
11422 -- Ada 2005 (AI-251): Anonymous access types where target references an
11423 -- interface type.
11424
11425 elsif Ekind_In (Target_Type, E_General_Access_Type,
11426 E_Anonymous_Access_Type)
11427 and then Is_Interface (Directly_Designated_Type (Target_Type))
11428 then
11429 -- Check the static accessibility rule of 4.6(17). Note that the
11430 -- check is not enforced when within an instance body, since the
11431 -- RM requires such cases to be caught at run time.
11432
11433 -- If the operand is a rewriting of an allocator no check is needed
11434 -- because there are no accessibility issues.
11435
11436 if Nkind (Original_Node (N)) = N_Allocator then
11437 null;
11438
11439 elsif Ekind (Target_Type) /= E_Anonymous_Access_Type then
11440 if Type_Access_Level (Opnd_Type) >
11441 Deepest_Type_Access_Level (Target_Type)
11442 then
11443 -- In an instance, this is a run-time check, but one we know
11444 -- will fail, so generate an appropriate warning. The raise
11445 -- will be generated by Expand_N_Type_Conversion.
11446
11447 if In_Instance_Body then
11448 Error_Msg_Warn := SPARK_Mode /= On;
11449 Conversion_Error_N
11450 ("cannot convert local pointer to non-local access type<<",
11451 Operand);
11452 Conversion_Error_N ("\Program_Error [<<", Operand);
11453
11454 else
11455 Conversion_Error_N
11456 ("cannot convert local pointer to non-local access type",
11457 Operand);
11458 return False;
11459 end if;
11460
11461 -- Special accessibility checks are needed in the case of access
11462 -- discriminants declared for a limited type.
11463
11464 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
11465 and then not Is_Local_Anonymous_Access (Opnd_Type)
11466 then
11467 -- When the operand is a selected access discriminant the check
11468 -- needs to be made against the level of the object denoted by
11469 -- the prefix of the selected name (Object_Access_Level handles
11470 -- checking the prefix of the operand for this case).
11471
11472 if Nkind (Operand) = N_Selected_Component
11473 and then Object_Access_Level (Operand) >
11474 Deepest_Type_Access_Level (Target_Type)
11475 then
11476 -- In an instance, this is a run-time check, but one we know
11477 -- will fail, so generate an appropriate warning. The raise
11478 -- will be generated by Expand_N_Type_Conversion.
11479
11480 if In_Instance_Body then
11481 Error_Msg_Warn := SPARK_Mode /= On;
11482 Conversion_Error_N
11483 ("cannot convert access discriminant to non-local "
11484 & "access type<<", Operand);
11485 Conversion_Error_N ("\Program_Error [<<", Operand);
11486
11487 -- Real error if not in instance body
11488
11489 else
11490 Conversion_Error_N
11491 ("cannot convert access discriminant to non-local "
11492 & "access type", Operand);
11493 return False;
11494 end if;
11495 end if;
11496
11497 -- The case of a reference to an access discriminant from
11498 -- within a limited type declaration (which will appear as
11499 -- a discriminal) is always illegal because the level of the
11500 -- discriminant is considered to be deeper than any (nameable)
11501 -- access type.
11502
11503 if Is_Entity_Name (Operand)
11504 and then not Is_Local_Anonymous_Access (Opnd_Type)
11505 and then
11506 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
11507 and then Present (Discriminal_Link (Entity (Operand)))
11508 then
11509 Conversion_Error_N
11510 ("discriminant has deeper accessibility level than target",
11511 Operand);
11512 return False;
11513 end if;
11514 end if;
11515 end if;
11516
11517 return True;
11518
11519 -- General and anonymous access types
11520
11521 elsif Ekind_In (Target_Type, E_General_Access_Type,
11522 E_Anonymous_Access_Type)
11523 and then
11524 Conversion_Check
11525 (Is_Access_Type (Opnd_Type)
11526 and then not
11527 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
11528 E_Access_Protected_Subprogram_Type),
11529 "must be an access-to-object type")
11530 then
11531 if Is_Access_Constant (Opnd_Type)
11532 and then not Is_Access_Constant (Target_Type)
11533 then
11534 Conversion_Error_N
11535 ("access-to-constant operand type not allowed", Operand);
11536 return False;
11537 end if;
11538
11539 -- Check the static accessibility rule of 4.6(17). Note that the
11540 -- check is not enforced when within an instance body, since the RM
11541 -- requires such cases to be caught at run time.
11542
11543 if Ekind (Target_Type) /= E_Anonymous_Access_Type
11544 or else Is_Local_Anonymous_Access (Target_Type)
11545 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
11546 N_Object_Declaration
11547 then
11548 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
11549 -- conversions from an anonymous access type to a named general
11550 -- access type. Such conversions are not allowed in the case of
11551 -- access parameters and stand-alone objects of an anonymous
11552 -- access type. The implicit conversion case is recognized by
11553 -- testing that Comes_From_Source is False and that it's been
11554 -- rewritten. The Comes_From_Source test isn't sufficient because
11555 -- nodes in inlined calls to predefined library routines can have
11556 -- Comes_From_Source set to False. (Is there a better way to test
11557 -- for implicit conversions???)
11558
11559 if Ada_Version >= Ada_2012
11560 and then not Comes_From_Source (N)
11561 and then N /= Original_Node (N)
11562 and then Ekind (Target_Type) = E_General_Access_Type
11563 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
11564 then
11565 if Is_Itype (Opnd_Type) then
11566
11567 -- Implicit conversions aren't allowed for objects of an
11568 -- anonymous access type, since such objects have nonstatic
11569 -- levels in Ada 2012.
11570
11571 if Nkind (Associated_Node_For_Itype (Opnd_Type)) =
11572 N_Object_Declaration
11573 then
11574 Conversion_Error_N
11575 ("implicit conversion of stand-alone anonymous "
11576 & "access object not allowed", Operand);
11577 return False;
11578
11579 -- Implicit conversions aren't allowed for anonymous access
11580 -- parameters. The "not Is_Local_Anonymous_Access_Type" test
11581 -- is done to exclude anonymous access results.
11582
11583 elsif not Is_Local_Anonymous_Access (Opnd_Type)
11584 and then Nkind_In (Associated_Node_For_Itype (Opnd_Type),
11585 N_Function_Specification,
11586 N_Procedure_Specification)
11587 then
11588 Conversion_Error_N
11589 ("implicit conversion of anonymous access formal "
11590 & "not allowed", Operand);
11591 return False;
11592
11593 -- This is a case where there's an enclosing object whose
11594 -- to which the "statically deeper than" relationship does
11595 -- not apply (such as an access discriminant selected from
11596 -- a dereference of an access parameter).
11597
11598 elsif Object_Access_Level (Operand)
11599 = Scope_Depth (Standard_Standard)
11600 then
11601 Conversion_Error_N
11602 ("implicit conversion of anonymous access value "
11603 & "not allowed", Operand);
11604 return False;
11605
11606 -- In other cases, the level of the operand's type must be
11607 -- statically less deep than that of the target type, else
11608 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
11609
11610 elsif Type_Access_Level (Opnd_Type) >
11611 Deepest_Type_Access_Level (Target_Type)
11612 then
11613 Conversion_Error_N
11614 ("implicit conversion of anonymous access value "
11615 & "violates accessibility", Operand);
11616 return False;
11617 end if;
11618 end if;
11619
11620 elsif Type_Access_Level (Opnd_Type) >
11621 Deepest_Type_Access_Level (Target_Type)
11622 then
11623 -- In an instance, this is a run-time check, but one we know
11624 -- will fail, so generate an appropriate warning. The raise
11625 -- will be generated by Expand_N_Type_Conversion.
11626
11627 if In_Instance_Body then
11628 Error_Msg_Warn := SPARK_Mode /= On;
11629 Conversion_Error_N
11630 ("cannot convert local pointer to non-local access type<<",
11631 Operand);
11632 Conversion_Error_N ("\Program_Error [<<", Operand);
11633
11634 -- If not in an instance body, this is a real error
11635
11636 else
11637 -- Avoid generation of spurious error message
11638
11639 if not Error_Posted (N) then
11640 Conversion_Error_N
11641 ("cannot convert local pointer to non-local access type",
11642 Operand);
11643 end if;
11644
11645 return False;
11646 end if;
11647
11648 -- Special accessibility checks are needed in the case of access
11649 -- discriminants declared for a limited type.
11650
11651 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
11652 and then not Is_Local_Anonymous_Access (Opnd_Type)
11653 then
11654 -- When the operand is a selected access discriminant the check
11655 -- needs to be made against the level of the object denoted by
11656 -- the prefix of the selected name (Object_Access_Level handles
11657 -- checking the prefix of the operand for this case).
11658
11659 if Nkind (Operand) = N_Selected_Component
11660 and then Object_Access_Level (Operand) >
11661 Deepest_Type_Access_Level (Target_Type)
11662 then
11663 -- In an instance, this is a run-time check, but one we know
11664 -- will fail, so generate an appropriate warning. The raise
11665 -- will be generated by Expand_N_Type_Conversion.
11666
11667 if In_Instance_Body then
11668 Error_Msg_Warn := SPARK_Mode /= On;
11669 Conversion_Error_N
11670 ("cannot convert access discriminant to non-local "
11671 & "access type<<", Operand);
11672 Conversion_Error_N ("\Program_Error [<<", Operand);
11673
11674 -- If not in an instance body, this is a real error
11675
11676 else
11677 Conversion_Error_N
11678 ("cannot convert access discriminant to non-local "
11679 & "access type", Operand);
11680 return False;
11681 end if;
11682 end if;
11683
11684 -- The case of a reference to an access discriminant from
11685 -- within a limited type declaration (which will appear as
11686 -- a discriminal) is always illegal because the level of the
11687 -- discriminant is considered to be deeper than any (nameable)
11688 -- access type.
11689
11690 if Is_Entity_Name (Operand)
11691 and then
11692 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
11693 and then Present (Discriminal_Link (Entity (Operand)))
11694 then
11695 Conversion_Error_N
11696 ("discriminant has deeper accessibility level than target",
11697 Operand);
11698 return False;
11699 end if;
11700 end if;
11701 end if;
11702
11703 -- In the presence of limited_with clauses we have to use non-limited
11704 -- views, if available.
11705
11706 Check_Limited : declare
11707 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
11708 -- Helper function to handle limited views
11709
11710 --------------------------
11711 -- Full_Designated_Type --
11712 --------------------------
11713
11714 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
11715 Desig : constant Entity_Id := Designated_Type (T);
11716
11717 begin
11718 -- Handle the limited view of a type
11719
11720 if Is_Incomplete_Type (Desig)
11721 and then From_Limited_With (Desig)
11722 and then Present (Non_Limited_View (Desig))
11723 then
11724 return Available_View (Desig);
11725 else
11726 return Desig;
11727 end if;
11728 end Full_Designated_Type;
11729
11730 -- Local Declarations
11731
11732 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
11733 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
11734
11735 Same_Base : constant Boolean :=
11736 Base_Type (Target) = Base_Type (Opnd);
11737
11738 -- Start of processing for Check_Limited
11739
11740 begin
11741 if Is_Tagged_Type (Target) then
11742 return Valid_Tagged_Conversion (Target, Opnd);
11743
11744 else
11745 if not Same_Base then
11746 Conversion_Error_NE
11747 ("target designated type not compatible with }",
11748 N, Base_Type (Opnd));
11749 return False;
11750
11751 -- Ada 2005 AI-384: legality rule is symmetric in both
11752 -- designated types. The conversion is legal (with possible
11753 -- constraint check) if either designated type is
11754 -- unconstrained.
11755
11756 elsif Subtypes_Statically_Match (Target, Opnd)
11757 or else
11758 (Has_Discriminants (Target)
11759 and then
11760 (not Is_Constrained (Opnd)
11761 or else not Is_Constrained (Target)))
11762 then
11763 -- Special case, if Value_Size has been used to make the
11764 -- sizes different, the conversion is not allowed even
11765 -- though the subtypes statically match.
11766
11767 if Known_Static_RM_Size (Target)
11768 and then Known_Static_RM_Size (Opnd)
11769 and then RM_Size (Target) /= RM_Size (Opnd)
11770 then
11771 Conversion_Error_NE
11772 ("target designated subtype not compatible with }",
11773 N, Opnd);
11774 Conversion_Error_NE
11775 ("\because sizes of the two designated subtypes differ",
11776 N, Opnd);
11777 return False;
11778
11779 -- Normal case where conversion is allowed
11780
11781 else
11782 return True;
11783 end if;
11784
11785 else
11786 Error_Msg_NE
11787 ("target designated subtype not compatible with }",
11788 N, Opnd);
11789 return False;
11790 end if;
11791 end if;
11792 end Check_Limited;
11793
11794 -- Access to subprogram types. If the operand is an access parameter,
11795 -- the type has a deeper accessibility that any master, and cannot be
11796 -- assigned. We must make an exception if the conversion is part of an
11797 -- assignment and the target is the return object of an extended return
11798 -- statement, because in that case the accessibility check takes place
11799 -- after the return.
11800
11801 elsif Is_Access_Subprogram_Type (Target_Type)
11802
11803 -- Note: this test of Opnd_Type is there to prevent entering this
11804 -- branch in the case of a remote access to subprogram type, which
11805 -- is internally represented as an E_Record_Type.
11806
11807 and then Is_Access_Type (Opnd_Type)
11808 then
11809 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
11810 and then Is_Entity_Name (Operand)
11811 and then Ekind (Entity (Operand)) = E_In_Parameter
11812 and then
11813 (Nkind (Parent (N)) /= N_Assignment_Statement
11814 or else not Is_Entity_Name (Name (Parent (N)))
11815 or else not Is_Return_Object (Entity (Name (Parent (N)))))
11816 then
11817 Conversion_Error_N
11818 ("illegal attempt to store anonymous access to subprogram",
11819 Operand);
11820 Conversion_Error_N
11821 ("\value has deeper accessibility than any master "
11822 & "(RM 3.10.2 (13))",
11823 Operand);
11824
11825 Error_Msg_NE
11826 ("\use named access type for& instead of access parameter",
11827 Operand, Entity (Operand));
11828 end if;
11829
11830 -- Check that the designated types are subtype conformant
11831
11832 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
11833 Old_Id => Designated_Type (Opnd_Type),
11834 Err_Loc => N);
11835
11836 -- Check the static accessibility rule of 4.6(20)
11837
11838 if Type_Access_Level (Opnd_Type) >
11839 Deepest_Type_Access_Level (Target_Type)
11840 then
11841 Conversion_Error_N
11842 ("operand type has deeper accessibility level than target",
11843 Operand);
11844
11845 -- Check that if the operand type is declared in a generic body,
11846 -- then the target type must be declared within that same body
11847 -- (enforces last sentence of 4.6(20)).
11848
11849 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
11850 declare
11851 O_Gen : constant Node_Id :=
11852 Enclosing_Generic_Body (Opnd_Type);
11853
11854 T_Gen : Node_Id;
11855
11856 begin
11857 T_Gen := Enclosing_Generic_Body (Target_Type);
11858 while Present (T_Gen) and then T_Gen /= O_Gen loop
11859 T_Gen := Enclosing_Generic_Body (T_Gen);
11860 end loop;
11861
11862 if T_Gen /= O_Gen then
11863 Conversion_Error_N
11864 ("target type must be declared in same generic body "
11865 & "as operand type", N);
11866 end if;
11867 end;
11868 end if;
11869
11870 return True;
11871
11872 -- Remote access to subprogram types
11873
11874 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
11875 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
11876 then
11877 -- It is valid to convert from one RAS type to another provided
11878 -- that their specification statically match.
11879
11880 -- Note: at this point, remote access to subprogram types have been
11881 -- expanded to their E_Record_Type representation, and we need to
11882 -- go back to the original access type definition using the
11883 -- Corresponding_Remote_Type attribute in order to check that the
11884 -- designated profiles match.
11885
11886 pragma Assert (Ekind (Target_Type) = E_Record_Type);
11887 pragma Assert (Ekind (Opnd_Type) = E_Record_Type);
11888
11889 Check_Subtype_Conformant
11890 (New_Id =>
11891 Designated_Type (Corresponding_Remote_Type (Target_Type)),
11892 Old_Id =>
11893 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
11894 Err_Loc =>
11895 N);
11896 return True;
11897
11898 -- If it was legal in the generic, it's legal in the instance
11899
11900 elsif In_Instance_Body then
11901 return True;
11902
11903 -- If both are tagged types, check legality of view conversions
11904
11905 elsif Is_Tagged_Type (Target_Type)
11906 and then
11907 Is_Tagged_Type (Opnd_Type)
11908 then
11909 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
11910
11911 -- Types derived from the same root type are convertible
11912
11913 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
11914 return True;
11915
11916 -- In an instance or an inlined body, there may be inconsistent views of
11917 -- the same type, or of types derived from a common root.
11918
11919 elsif (In_Instance or In_Inlined_Body)
11920 and then
11921 Root_Type (Underlying_Type (Target_Type)) =
11922 Root_Type (Underlying_Type (Opnd_Type))
11923 then
11924 return True;
11925
11926 -- Special check for common access type error case
11927
11928 elsif Ekind (Target_Type) = E_Access_Type
11929 and then Is_Access_Type (Opnd_Type)
11930 then
11931 Conversion_Error_N ("target type must be general access type!", N);
11932 Conversion_Error_NE -- CODEFIX
11933 ("add ALL to }!", N, Target_Type);
11934 return False;
11935
11936 -- Here we have a real conversion error
11937
11938 else
11939 Conversion_Error_NE
11940 ("invalid conversion, not compatible with }", N, Opnd_Type);
11941 return False;
11942 end if;
11943 end Valid_Conversion;
11944
11945 end Sem_Res;