]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/sem_res.adb
27c5ae0e5a0eeea4c3e1c2efcc9f934b44aec012
[thirdparty/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-2020, 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_Ch6; use Exp_Ch6;
34 with Exp_Ch7; use Exp_Ch7;
35 with Exp_Disp; use Exp_Disp;
36 with Exp_Tss; use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Freeze; use Freeze;
39 with Ghost; use Ghost;
40 with Inline; use Inline;
41 with Itypes; use Itypes;
42 with Lib; use Lib;
43 with Lib.Xref; use Lib.Xref;
44 with Namet; use Namet;
45 with Nmake; use Nmake;
46 with Nlists; use Nlists;
47 with Opt; use Opt;
48 with Output; use Output;
49 with Par_SCO; use Par_SCO;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
53 with Sem; use Sem;
54 with Sem_Aggr; use Sem_Aggr;
55 with Sem_Attr; use Sem_Attr;
56 with Sem_Aux; use Sem_Aux;
57 with Sem_Cat; use Sem_Cat;
58 with Sem_Ch3; use Sem_Ch3;
59 with Sem_Ch4; use Sem_Ch4;
60 with Sem_Ch6; use Sem_Ch6;
61 with Sem_Ch8; use Sem_Ch8;
62 with Sem_Ch13; use Sem_Ch13;
63 with Sem_Dim; use Sem_Dim;
64 with Sem_Disp; use Sem_Disp;
65 with Sem_Dist; use Sem_Dist;
66 with Sem_Elab; use Sem_Elab;
67 with Sem_Elim; use Sem_Elim;
68 with Sem_Eval; use Sem_Eval;
69 with Sem_Intr; use Sem_Intr;
70 with Sem_Mech; use Sem_Mech;
71 with Sem_Type; use Sem_Type;
72 with Sem_Util; use Sem_Util;
73 with Sem_Warn; use Sem_Warn;
74 with Sinfo; use Sinfo;
75 with Sinfo.CN; use Sinfo.CN;
76 with Snames; use Snames;
77 with Stand; use Stand;
78 with Stringt; use Stringt;
79 with Style; use Style;
80 with Targparm; use Targparm;
81 with Tbuild; use Tbuild;
82 with Uintp; use Uintp;
83 with Urealp; use Urealp;
84
85 package body Sem_Res is
86
87 -----------------------
88 -- Local Subprograms --
89 -----------------------
90
91 -- Second pass (top-down) type checking and overload resolution procedures
92 -- Typ is the type required by context. These procedures propagate the
93 -- type information recursively to the descendants of N. If the node is not
94 -- overloaded, its Etype is established in the first pass. If overloaded,
95 -- the Resolve routines set the correct type. For arithmetic operators, the
96 -- Etype is the base type of the context.
97
98 -- Note that Resolve_Attribute is separated off in Sem_Attr
99
100 procedure Check_Discriminant_Use (N : Node_Id);
101 -- Enforce the restrictions on the use of discriminants when constraining
102 -- a component of a discriminated type (record or concurrent type).
103
104 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
105 -- Given a node for an operator associated with type T, check that the
106 -- operator is visible. Operators all of whose operands are universal must
107 -- be checked for visibility during resolution because their type is not
108 -- determinable based on their operands.
109
110 procedure Check_Fully_Declared_Prefix
111 (Typ : Entity_Id;
112 Pref : Node_Id);
113 -- Check that the type of the prefix of a dereference is not incomplete
114
115 function Check_Infinite_Recursion (Call : Node_Id) return Boolean;
116 -- Given a call node, Call, which is known to occur immediately within the
117 -- subprogram being called, determines whether it is a detectable case of
118 -- an infinite recursion, and if so, outputs appropriate messages. Returns
119 -- True if an infinite recursion is detected, and False otherwise.
120
121 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
122 -- N is the node for a logical operator. If the operator is predefined, and
123 -- the root type of the operands is Standard.Boolean, then a check is made
124 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
125 -- the style check for Style_Check_Boolean_And_Or.
126
127 function Is_Atomic_Ref_With_Address (N : Node_Id) return Boolean;
128 -- N is either an indexed component or a selected component. This function
129 -- returns true if the prefix refers to an object that has an address
130 -- clause (the case in which we may want to issue a warning).
131
132 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
133 -- Determine whether E is an access type declared by an access declaration,
134 -- and not an (anonymous) allocator type.
135
136 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
137 -- Utility to check whether the entity for an operator is a predefined
138 -- operator, in which case the expression is left as an operator in the
139 -- tree (else it is rewritten into a call). An instance of an intrinsic
140 -- conversion operation may be given an operator name, but is not treated
141 -- like an operator. Note that an operator that is an imported back-end
142 -- builtin has convention Intrinsic, but is expected to be rewritten into
143 -- a call, so such an operator is not treated as predefined by this
144 -- predicate.
145
146 procedure Preanalyze_And_Resolve
147 (N : Node_Id;
148 T : Entity_Id;
149 With_Freezing : Boolean);
150 -- Subsidiary of public versions of Preanalyze_And_Resolve.
151
152 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
153 -- If a default expression in entry call N depends on the discriminants
154 -- of the task, it must be replaced with a reference to the discriminant
155 -- of the task being called.
156
157 procedure Resolve_Op_Concat_Arg
158 (N : Node_Id;
159 Arg : Node_Id;
160 Typ : Entity_Id;
161 Is_Comp : Boolean);
162 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
163 -- concatenation operator. The operand is either of the array type or of
164 -- the component type. If the operand is an aggregate, and the component
165 -- type is composite, this is ambiguous if component type has aggregates.
166
167 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
168 -- Does the first part of the work of Resolve_Op_Concat
169
170 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
171 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
172 -- has been resolved. See Resolve_Op_Concat for details.
173
174 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
175 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
176 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
177 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
178 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
179 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
180 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
181 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
182 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
183 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
184 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id);
185 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id);
186 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
187 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
188 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
189 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
190 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
191 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
192 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
193 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
194 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
195 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
196 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id);
197 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
198 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
199 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
200 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
201 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
202 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
203 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
204 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
205 procedure Resolve_Target_Name (N : Node_Id; Typ : Entity_Id);
206 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
207 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
208 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
209 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
210
211 function Operator_Kind
212 (Op_Name : Name_Id;
213 Is_Binary : Boolean) return Node_Kind;
214 -- Utility to map the name of an operator into the corresponding Node. Used
215 -- by other node rewriting procedures.
216
217 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
218 -- Resolve actuals of call, and add default expressions for missing ones.
219 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
220 -- called subprogram.
221
222 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
223 -- Called from Resolve_Call, when the prefix denotes an entry or element
224 -- of entry family. Actuals are resolved as for subprograms, and the node
225 -- is rebuilt as an entry call. Also called for protected operations. Typ
226 -- is the context type, which is used when the operation is a protected
227 -- function with no arguments, and the return value is indexed.
228
229 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
230 -- A call to a user-defined intrinsic operator is rewritten as a call to
231 -- the corresponding predefined operator, with suitable conversions. Note
232 -- that this applies only for intrinsic operators that denote predefined
233 -- operators, not ones that are intrinsic imports of back-end builtins.
234
235 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
236 -- Ditto, for arithmetic unary operators
237
238 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
239 -- If an operator node resolves to a call to a user-defined operator,
240 -- rewrite the node as a function call.
241
242 procedure Make_Call_Into_Operator
243 (N : Node_Id;
244 Typ : Entity_Id;
245 Op_Id : Entity_Id);
246 -- Inverse transformation: if an operator is given in functional notation,
247 -- then after resolving the node, transform into an operator node, so that
248 -- operands are resolved properly. Recall that predefined operators do not
249 -- have a full signature and special resolution rules apply.
250
251 procedure Rewrite_Renamed_Operator
252 (N : Node_Id;
253 Op : Entity_Id;
254 Typ : Entity_Id);
255 -- An operator can rename another, e.g. in an instantiation. In that
256 -- case, the proper operator node must be constructed and resolved.
257
258 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
259 -- The String_Literal_Subtype is built for all strings that are not
260 -- operands of a static concatenation operation. If the argument is not
261 -- a N_String_Literal node, then the call has no effect.
262
263 procedure Set_Slice_Subtype (N : Node_Id);
264 -- Build subtype of array type, with the range specified by the slice
265
266 procedure Simplify_Type_Conversion (N : Node_Id);
267 -- Called after N has been resolved and evaluated, but before range checks
268 -- have been applied. This rewrites the conversion into a simpler form.
269
270 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
271 -- A universal_fixed expression in an universal context is unambiguous if
272 -- there is only one applicable fixed point type. Determining whether there
273 -- is only one requires a search over all visible entities, and happens
274 -- only in very pathological cases (see 6115-006).
275
276 -------------------------
277 -- Ambiguous_Character --
278 -------------------------
279
280 procedure Ambiguous_Character (C : Node_Id) is
281 E : Entity_Id;
282
283 begin
284 if Nkind (C) = N_Character_Literal then
285 Error_Msg_N ("ambiguous character literal", C);
286
287 -- First the ones in Standard
288
289 Error_Msg_N ("\\possible interpretation: Character!", C);
290 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
291
292 -- Include Wide_Wide_Character in Ada 2005 mode
293
294 if Ada_Version >= Ada_2005 then
295 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
296 end if;
297
298 -- Now any other types that match
299
300 E := Current_Entity (C);
301 while Present (E) loop
302 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
303 E := Homonym (E);
304 end loop;
305 end if;
306 end Ambiguous_Character;
307
308 -------------------------
309 -- Analyze_And_Resolve --
310 -------------------------
311
312 procedure Analyze_And_Resolve (N : Node_Id) is
313 begin
314 Analyze (N);
315 Resolve (N);
316 end Analyze_And_Resolve;
317
318 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
319 begin
320 Analyze (N);
321 Resolve (N, Typ);
322 end Analyze_And_Resolve;
323
324 -- Versions with check(s) suppressed
325
326 procedure Analyze_And_Resolve
327 (N : Node_Id;
328 Typ : Entity_Id;
329 Suppress : Check_Id)
330 is
331 Scop : constant Entity_Id := Current_Scope;
332
333 begin
334 if Suppress = All_Checks then
335 declare
336 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
337 begin
338 Scope_Suppress.Suppress := (others => True);
339 Analyze_And_Resolve (N, Typ);
340 Scope_Suppress.Suppress := Sva;
341 end;
342
343 else
344 declare
345 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
346 begin
347 Scope_Suppress.Suppress (Suppress) := True;
348 Analyze_And_Resolve (N, Typ);
349 Scope_Suppress.Suppress (Suppress) := Svg;
350 end;
351 end if;
352
353 if Current_Scope /= Scop
354 and then Scope_Is_Transient
355 then
356 -- This can only happen if a transient scope was created for an inner
357 -- expression, which will be removed upon completion of the analysis
358 -- of an enclosing construct. The transient scope must have the
359 -- suppress status of the enclosing environment, not of this Analyze
360 -- call.
361
362 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
363 Scope_Suppress;
364 end if;
365 end Analyze_And_Resolve;
366
367 procedure Analyze_And_Resolve
368 (N : Node_Id;
369 Suppress : Check_Id)
370 is
371 Scop : constant Entity_Id := Current_Scope;
372
373 begin
374 if Suppress = All_Checks then
375 declare
376 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
377 begin
378 Scope_Suppress.Suppress := (others => True);
379 Analyze_And_Resolve (N);
380 Scope_Suppress.Suppress := Sva;
381 end;
382
383 else
384 declare
385 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
386 begin
387 Scope_Suppress.Suppress (Suppress) := True;
388 Analyze_And_Resolve (N);
389 Scope_Suppress.Suppress (Suppress) := Svg;
390 end;
391 end if;
392
393 if Current_Scope /= Scop and then Scope_Is_Transient then
394 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
395 Scope_Suppress;
396 end if;
397 end Analyze_And_Resolve;
398
399 ----------------------------
400 -- Check_Discriminant_Use --
401 ----------------------------
402
403 procedure Check_Discriminant_Use (N : Node_Id) is
404 PN : constant Node_Id := Parent (N);
405 Disc : constant Entity_Id := Entity (N);
406 P : Node_Id;
407 D : Node_Id;
408
409 begin
410 -- Any use in a spec-expression is legal
411
412 if In_Spec_Expression then
413 null;
414
415 elsif Nkind (PN) = N_Range then
416
417 -- Discriminant cannot be used to constrain a scalar type
418
419 P := Parent (PN);
420
421 if Nkind (P) = N_Range_Constraint
422 and then Nkind (Parent (P)) = N_Subtype_Indication
423 and then Nkind (Parent (Parent (P))) = N_Component_Definition
424 then
425 Error_Msg_N ("discriminant cannot constrain scalar type", N);
426
427 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
428
429 -- The following check catches the unusual case where a
430 -- discriminant appears within an index constraint that is part
431 -- of a larger expression within a constraint on a component,
432 -- e.g. "C : Int range 1 .. F (new A(1 .. D))". For now we only
433 -- check case of record components, and note that a similar check
434 -- should also apply in the case of discriminant constraints
435 -- below. ???
436
437 -- Note that the check for N_Subtype_Declaration below is to
438 -- detect the valid use of discriminants in the constraints of a
439 -- subtype declaration when this subtype declaration appears
440 -- inside the scope of a record type (which is syntactically
441 -- illegal, but which may be created as part of derived type
442 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
443 -- for more info.
444
445 if Ekind (Current_Scope) = E_Record_Type
446 and then Scope (Disc) = Current_Scope
447 and then not
448 (Nkind (Parent (P)) = N_Subtype_Indication
449 and then
450 Nkind_In (Parent (Parent (P)), N_Component_Definition,
451 N_Subtype_Declaration)
452 and then Paren_Count (N) = 0)
453 then
454 Error_Msg_N
455 ("discriminant must appear alone in component constraint", N);
456 return;
457 end if;
458
459 -- Detect a common error:
460
461 -- type R (D : Positive := 100) is record
462 -- Name : String (1 .. D);
463 -- end record;
464
465 -- The default value causes an object of type R to be allocated
466 -- with room for Positive'Last characters. The RM does not mandate
467 -- the allocation of the maximum size, but that is what GNAT does
468 -- so we should warn the programmer that there is a problem.
469
470 Check_Large : declare
471 SI : Node_Id;
472 T : Entity_Id;
473 TB : Node_Id;
474 CB : Entity_Id;
475
476 function Large_Storage_Type (T : Entity_Id) return Boolean;
477 -- Return True if type T has a large enough range that any
478 -- array whose index type covered the whole range of the type
479 -- would likely raise Storage_Error.
480
481 ------------------------
482 -- Large_Storage_Type --
483 ------------------------
484
485 function Large_Storage_Type (T : Entity_Id) return Boolean is
486 begin
487 -- The type is considered large if its bounds are known at
488 -- compile time and if it requires at least as many bits as
489 -- a Positive to store the possible values.
490
491 return Compile_Time_Known_Value (Type_Low_Bound (T))
492 and then Compile_Time_Known_Value (Type_High_Bound (T))
493 and then
494 Minimum_Size (T, Biased => True) >=
495 RM_Size (Standard_Positive);
496 end Large_Storage_Type;
497
498 -- Start of processing for Check_Large
499
500 begin
501 -- Check that the Disc has a large range
502
503 if not Large_Storage_Type (Etype (Disc)) then
504 goto No_Danger;
505 end if;
506
507 -- If the enclosing type is limited, we allocate only the
508 -- default value, not the maximum, and there is no need for
509 -- a warning.
510
511 if Is_Limited_Type (Scope (Disc)) then
512 goto No_Danger;
513 end if;
514
515 -- Check that it is the high bound
516
517 if N /= High_Bound (PN)
518 or else No (Discriminant_Default_Value (Disc))
519 then
520 goto No_Danger;
521 end if;
522
523 -- Check the array allows a large range at this bound. First
524 -- find the array
525
526 SI := Parent (P);
527
528 if Nkind (SI) /= N_Subtype_Indication then
529 goto No_Danger;
530 end if;
531
532 T := Entity (Subtype_Mark (SI));
533
534 if not Is_Array_Type (T) then
535 goto No_Danger;
536 end if;
537
538 -- Next, find the dimension
539
540 TB := First_Index (T);
541 CB := First (Constraints (P));
542 while True
543 and then Present (TB)
544 and then Present (CB)
545 and then CB /= PN
546 loop
547 Next_Index (TB);
548 Next (CB);
549 end loop;
550
551 if CB /= PN then
552 goto No_Danger;
553 end if;
554
555 -- Now, check the dimension has a large range
556
557 if not Large_Storage_Type (Etype (TB)) then
558 goto No_Danger;
559 end if;
560
561 -- Warn about the danger
562
563 Error_Msg_N
564 ("??creation of & object may raise Storage_Error!",
565 Scope (Disc));
566
567 <<No_Danger>>
568 null;
569
570 end Check_Large;
571 end if;
572
573 -- Legal case is in index or discriminant constraint
574
575 elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint,
576 N_Discriminant_Association)
577 then
578 if Paren_Count (N) > 0 then
579 Error_Msg_N
580 ("discriminant in constraint must appear alone", N);
581
582 elsif Nkind (N) = N_Expanded_Name
583 and then Comes_From_Source (N)
584 then
585 Error_Msg_N
586 ("discriminant must appear alone as a direct name", N);
587 end if;
588
589 return;
590
591 -- Otherwise, context is an expression. It should not be within (i.e. a
592 -- subexpression of) a constraint for a component.
593
594 else
595 D := PN;
596 P := Parent (PN);
597 while not Nkind_In (P, N_Component_Declaration,
598 N_Subtype_Indication,
599 N_Entry_Declaration)
600 loop
601 D := P;
602 P := Parent (P);
603 exit when No (P);
604 end loop;
605
606 -- If the discriminant is used in an expression that is a bound of a
607 -- scalar type, an Itype is created and the bounds are attached to
608 -- its range, not to the original subtype indication. Such use is of
609 -- course a double fault.
610
611 if (Nkind (P) = N_Subtype_Indication
612 and then Nkind_In (Parent (P), N_Component_Definition,
613 N_Derived_Type_Definition)
614 and then D = Constraint (P))
615
616 -- The constraint itself may be given by a subtype indication,
617 -- rather than by a more common discrete range.
618
619 or else (Nkind (P) = N_Subtype_Indication
620 and then
621 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
622 or else Nkind (P) = N_Entry_Declaration
623 or else Nkind (D) = N_Defining_Identifier
624 then
625 Error_Msg_N
626 ("discriminant in constraint must appear alone", N);
627 end if;
628 end if;
629 end Check_Discriminant_Use;
630
631 --------------------------------
632 -- Check_For_Visible_Operator --
633 --------------------------------
634
635 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
636 begin
637 if Is_Invisible_Operator (N, T) then
638 Error_Msg_NE -- CODEFIX
639 ("operator for} is not directly visible!", N, First_Subtype (T));
640 Error_Msg_N -- CODEFIX
641 ("use clause would make operation legal!", N);
642 end if;
643 end Check_For_Visible_Operator;
644
645 ----------------------------------
646 -- Check_Fully_Declared_Prefix --
647 ----------------------------------
648
649 procedure Check_Fully_Declared_Prefix
650 (Typ : Entity_Id;
651 Pref : Node_Id)
652 is
653 begin
654 -- Check that the designated type of the prefix of a dereference is
655 -- not an incomplete type. This cannot be done unconditionally, because
656 -- dereferences of private types are legal in default expressions. This
657 -- case is taken care of in Check_Fully_Declared, called below. There
658 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
659
660 -- This consideration also applies to similar checks for allocators,
661 -- qualified expressions, and type conversions.
662
663 -- An additional exception concerns other per-object expressions that
664 -- are not directly related to component declarations, in particular
665 -- representation pragmas for tasks. These will be per-object
666 -- expressions if they depend on discriminants or some global entity.
667 -- If the task has access discriminants, the designated type may be
668 -- incomplete at the point the expression is resolved. This resolution
669 -- takes place within the body of the initialization procedure, where
670 -- the discriminant is replaced by its discriminal.
671
672 if Is_Entity_Name (Pref)
673 and then Ekind (Entity (Pref)) = E_In_Parameter
674 then
675 null;
676
677 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
678 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
679 -- Analyze_Object_Renaming, and Freeze_Entity.
680
681 elsif Ada_Version >= Ada_2005
682 and then Is_Entity_Name (Pref)
683 and then Is_Access_Type (Etype (Pref))
684 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
685 E_Incomplete_Type
686 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
687 then
688 null;
689 else
690 Check_Fully_Declared (Typ, Parent (Pref));
691 end if;
692 end Check_Fully_Declared_Prefix;
693
694 ------------------------------
695 -- Check_Infinite_Recursion --
696 ------------------------------
697
698 function Check_Infinite_Recursion (Call : Node_Id) return Boolean is
699 function Enclosing_Declaration_Or_Statement (N : Node_Id) return Node_Id;
700 -- Return the nearest enclosing declaration or statement that houses
701 -- arbitrary node N.
702
703 function Invoked_With_Different_Arguments (N : Node_Id) return Boolean;
704 -- Determine whether call N invokes the related enclosing subprogram
705 -- with actuals that differ from the subprogram's formals.
706
707 function Is_Conditional_Statement (N : Node_Id) return Boolean;
708 -- Determine whether arbitrary node N denotes a conditional construct
709
710 function Is_Control_Flow_Statement (N : Node_Id) return Boolean;
711 -- Determine whether arbitrary node N denotes a control flow statement
712 -- or a construct that may contains such a statement.
713
714 function Is_Immediately_Within_Body (N : Node_Id) return Boolean;
715 -- Determine whether arbitrary node N appears immediately within the
716 -- statements of an entry or subprogram body.
717
718 function Is_Raise_Idiom (N : Node_Id) return Boolean;
719 -- Determine whether arbitrary node N appears immediately within the
720 -- body of an entry or subprogram, and is preceded by a single raise
721 -- statement.
722
723 function Is_Raise_Statement (N : Node_Id) return Boolean;
724 -- Determine whether arbitrary node N denotes a raise statement
725
726 function Is_Sole_Statement (N : Node_Id) return Boolean;
727 -- Determine whether arbitrary node N is the sole source statement in
728 -- the body of the enclosing subprogram.
729
730 function Preceded_By_Control_Flow_Statement (N : Node_Id) return Boolean;
731 -- Determine whether arbitrary node N is preceded by a control flow
732 -- statement.
733
734 function Within_Conditional_Statement (N : Node_Id) return Boolean;
735 -- Determine whether arbitrary node N appears within a conditional
736 -- construct.
737
738 ----------------------------------------
739 -- Enclosing_Declaration_Or_Statement --
740 ----------------------------------------
741
742 function Enclosing_Declaration_Or_Statement
743 (N : Node_Id) return Node_Id
744 is
745 Par : Node_Id;
746
747 begin
748 Par := N;
749 while Present (Par) loop
750 if Is_Declaration (Par) or else Is_Statement (Par) then
751 return Par;
752
753 -- Prevent the search from going too far
754
755 elsif Is_Body_Or_Package_Declaration (Par) then
756 exit;
757 end if;
758
759 Par := Parent (Par);
760 end loop;
761
762 return N;
763 end Enclosing_Declaration_Or_Statement;
764
765 --------------------------------------
766 -- Invoked_With_Different_Arguments --
767 --------------------------------------
768
769 function Invoked_With_Different_Arguments (N : Node_Id) return Boolean is
770 Subp : constant Entity_Id := Entity (Name (N));
771
772 Actual : Node_Id;
773 Formal : Entity_Id;
774
775 begin
776 -- Determine whether the formals of the invoked subprogram are not
777 -- used as actuals in the call.
778
779 Actual := First_Actual (Call);
780 Formal := First_Formal (Subp);
781 while Present (Actual) and then Present (Formal) loop
782
783 -- The current actual does not match the current formal
784
785 if not (Is_Entity_Name (Actual)
786 and then Entity (Actual) = Formal)
787 then
788 return True;
789 end if;
790
791 Next_Actual (Actual);
792 Next_Formal (Formal);
793 end loop;
794
795 return False;
796 end Invoked_With_Different_Arguments;
797
798 ------------------------------
799 -- Is_Conditional_Statement --
800 ------------------------------
801
802 function Is_Conditional_Statement (N : Node_Id) return Boolean is
803 begin
804 return
805 Nkind_In (N, N_And_Then,
806 N_Case_Expression,
807 N_Case_Statement,
808 N_If_Expression,
809 N_If_Statement,
810 N_Or_Else);
811 end Is_Conditional_Statement;
812
813 -------------------------------
814 -- Is_Control_Flow_Statement --
815 -------------------------------
816
817 function Is_Control_Flow_Statement (N : Node_Id) return Boolean is
818 begin
819 -- It is assumed that all statements may affect the control flow in
820 -- some way. A raise statement may be expanded into a non-statement
821 -- node.
822
823 return Is_Statement (N) or else Is_Raise_Statement (N);
824 end Is_Control_Flow_Statement;
825
826 --------------------------------
827 -- Is_Immediately_Within_Body --
828 --------------------------------
829
830 function Is_Immediately_Within_Body (N : Node_Id) return Boolean is
831 HSS : constant Node_Id := Parent (N);
832
833 begin
834 return
835 Nkind (HSS) = N_Handled_Sequence_Of_Statements
836 and then Nkind_In (Parent (HSS), N_Entry_Body, N_Subprogram_Body)
837 and then Is_List_Member (N)
838 and then List_Containing (N) = Statements (HSS);
839 end Is_Immediately_Within_Body;
840
841 --------------------
842 -- Is_Raise_Idiom --
843 --------------------
844
845 function Is_Raise_Idiom (N : Node_Id) return Boolean is
846 Raise_Stmt : Node_Id;
847 Stmt : Node_Id;
848
849 begin
850 if Is_Immediately_Within_Body (N) then
851
852 -- Assume that no raise statement has been seen yet
853
854 Raise_Stmt := Empty;
855
856 -- Examine the statements preceding the input node, skipping
857 -- internally-generated constructs.
858
859 Stmt := Prev (N);
860 while Present (Stmt) loop
861
862 -- Multiple raise statements violate the idiom
863
864 if Is_Raise_Statement (Stmt) then
865 if Present (Raise_Stmt) then
866 return False;
867 end if;
868
869 Raise_Stmt := Stmt;
870
871 elsif Comes_From_Source (Stmt) then
872 exit;
873 end if;
874
875 Stmt := Prev (Stmt);
876 end loop;
877
878 -- At this point the node must be preceded by a raise statement,
879 -- and the raise statement has to be the sole statement within
880 -- the enclosing entry or subprogram body.
881
882 return
883 Present (Raise_Stmt) and then Is_Sole_Statement (Raise_Stmt);
884 end if;
885
886 return False;
887 end Is_Raise_Idiom;
888
889 ------------------------
890 -- Is_Raise_Statement --
891 ------------------------
892
893 function Is_Raise_Statement (N : Node_Id) return Boolean is
894 begin
895 -- A raise statement may be transfomed into a Raise_xxx_Error node
896
897 return
898 Nkind (N) = N_Raise_Statement
899 or else Nkind (N) in N_Raise_xxx_Error;
900 end Is_Raise_Statement;
901
902 -----------------------
903 -- Is_Sole_Statement --
904 -----------------------
905
906 function Is_Sole_Statement (N : Node_Id) return Boolean is
907 Stmt : Node_Id;
908
909 begin
910 -- The input node appears within the statements of an entry or
911 -- subprogram body. Examine the statements preceding the node.
912
913 if Is_Immediately_Within_Body (N) then
914 Stmt := Prev (N);
915
916 while Present (Stmt) loop
917
918 -- The statement is preceded by another statement or a source
919 -- construct. This indicates that the node does not appear by
920 -- itself.
921
922 if Is_Control_Flow_Statement (Stmt)
923 or else Comes_From_Source (Stmt)
924 then
925 return False;
926 end if;
927
928 Stmt := Prev (Stmt);
929 end loop;
930
931 return True;
932 end if;
933
934 -- The input node is within a construct nested inside the entry or
935 -- subprogram body.
936
937 return False;
938 end Is_Sole_Statement;
939
940 ----------------------------------------
941 -- Preceded_By_Control_Flow_Statement --
942 ----------------------------------------
943
944 function Preceded_By_Control_Flow_Statement
945 (N : Node_Id) return Boolean
946 is
947 Stmt : Node_Id;
948
949 begin
950 if Is_List_Member (N) then
951 Stmt := Prev (N);
952
953 -- Examine the statements preceding the input node
954
955 while Present (Stmt) loop
956 if Is_Control_Flow_Statement (Stmt) then
957 return True;
958 end if;
959
960 Stmt := Prev (Stmt);
961 end loop;
962
963 return False;
964 end if;
965
966 -- Assume that the node is part of some control flow statement
967
968 return True;
969 end Preceded_By_Control_Flow_Statement;
970
971 ----------------------------------
972 -- Within_Conditional_Statement --
973 ----------------------------------
974
975 function Within_Conditional_Statement (N : Node_Id) return Boolean is
976 Stmt : Node_Id;
977
978 begin
979 Stmt := Parent (N);
980 while Present (Stmt) loop
981 if Is_Conditional_Statement (Stmt) then
982 return True;
983
984 -- Prevent the search from going too far
985
986 elsif Is_Body_Or_Package_Declaration (Stmt) then
987 exit;
988 end if;
989
990 Stmt := Parent (Stmt);
991 end loop;
992
993 return False;
994 end Within_Conditional_Statement;
995
996 -- Local variables
997
998 Call_Context : constant Node_Id :=
999 Enclosing_Declaration_Or_Statement (Call);
1000
1001 -- Start of processing for Check_Infinite_Recursion
1002
1003 begin
1004 -- The call is assumed to be safe when the enclosing subprogram is
1005 -- invoked with actuals other than its formals.
1006 --
1007 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1008 -- begin
1009 -- ...
1010 -- Proc (A1, A2, ..., AN);
1011 -- ...
1012 -- end Proc;
1013
1014 if Invoked_With_Different_Arguments (Call) then
1015 return False;
1016
1017 -- The call is assumed to be safe when the invocation of the enclosing
1018 -- subprogram depends on a conditional statement.
1019 --
1020 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1021 -- begin
1022 -- ...
1023 -- if Some_Condition then
1024 -- Proc (F1, F2, ..., FN);
1025 -- end if;
1026 -- ...
1027 -- end Proc;
1028
1029 elsif Within_Conditional_Statement (Call) then
1030 return False;
1031
1032 -- The context of the call is assumed to be safe when the invocation of
1033 -- the enclosing subprogram is preceded by some control flow statement.
1034 --
1035 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1036 -- begin
1037 -- ...
1038 -- if Some_Condition then
1039 -- ...
1040 -- end if;
1041 -- ...
1042 -- Proc (F1, F2, ..., FN);
1043 -- ...
1044 -- end Proc;
1045
1046 elsif Preceded_By_Control_Flow_Statement (Call_Context) then
1047 return False;
1048
1049 -- Detect an idiom where the context of the call is preceded by a single
1050 -- raise statement.
1051 --
1052 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1053 -- begin
1054 -- raise ...;
1055 -- Proc (F1, F2, ..., FN);
1056 -- end Proc;
1057
1058 elsif Is_Raise_Idiom (Call_Context) then
1059 return False;
1060 end if;
1061
1062 -- At this point it is certain that infinite recursion will take place
1063 -- as long as the call is executed. Detect a case where the context of
1064 -- the call is the sole source statement within the subprogram body.
1065 --
1066 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1067 -- begin
1068 -- Proc (F1, F2, ..., FN);
1069 -- end Proc;
1070 --
1071 -- Install an explicit raise to prevent the infinite recursion.
1072
1073 if Is_Sole_Statement (Call_Context) then
1074 Error_Msg_Warn := SPARK_Mode /= On;
1075 Error_Msg_N ("!infinite recursion<<", Call);
1076 Error_Msg_N ("\!Storage_Error [<<", Call);
1077
1078 Insert_Action (Call,
1079 Make_Raise_Storage_Error (Sloc (Call),
1080 Reason => SE_Infinite_Recursion));
1081
1082 -- Otherwise infinite recursion could take place, considering other flow
1083 -- control constructs such as gotos, exit statements, etc.
1084
1085 else
1086 Error_Msg_Warn := SPARK_Mode /= On;
1087 Error_Msg_N ("!possible infinite recursion<<", Call);
1088 Error_Msg_N ("\!??Storage_Error ]<<", Call);
1089 end if;
1090
1091 return True;
1092 end Check_Infinite_Recursion;
1093
1094 ---------------------------------------
1095 -- Check_No_Direct_Boolean_Operators --
1096 ---------------------------------------
1097
1098 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
1099 begin
1100 if Scope (Entity (N)) = Standard_Standard
1101 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
1102 then
1103 -- Restriction only applies to original source code
1104
1105 if Comes_From_Source (N) then
1106 Check_Restriction (No_Direct_Boolean_Operators, N);
1107 end if;
1108 end if;
1109
1110 -- Do style check (but skip if in instance, error is on template)
1111
1112 if Style_Check then
1113 if not In_Instance then
1114 Check_Boolean_Operator (N);
1115 end if;
1116 end if;
1117 end Check_No_Direct_Boolean_Operators;
1118
1119 ------------------------------
1120 -- Check_Parameterless_Call --
1121 ------------------------------
1122
1123 procedure Check_Parameterless_Call (N : Node_Id) is
1124 Nam : Node_Id;
1125
1126 function Prefix_Is_Access_Subp return Boolean;
1127 -- If the prefix is of an access_to_subprogram type, the node must be
1128 -- rewritten as a call. Ditto if the prefix is overloaded and all its
1129 -- interpretations are access to subprograms.
1130
1131 ---------------------------
1132 -- Prefix_Is_Access_Subp --
1133 ---------------------------
1134
1135 function Prefix_Is_Access_Subp return Boolean is
1136 I : Interp_Index;
1137 It : Interp;
1138
1139 begin
1140 -- If the context is an attribute reference that can apply to
1141 -- functions, this is never a parameterless call (RM 4.1.4(6)).
1142
1143 if Nkind (Parent (N)) = N_Attribute_Reference
1144 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
1145 Name_Code_Address,
1146 Name_Access)
1147 then
1148 return False;
1149 end if;
1150
1151 if not Is_Overloaded (N) then
1152 return
1153 Ekind (Etype (N)) = E_Subprogram_Type
1154 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1155 else
1156 Get_First_Interp (N, I, It);
1157 while Present (It.Typ) loop
1158 if Ekind (It.Typ) /= E_Subprogram_Type
1159 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1160 then
1161 return False;
1162 end if;
1163
1164 Get_Next_Interp (I, It);
1165 end loop;
1166
1167 return True;
1168 end if;
1169 end Prefix_Is_Access_Subp;
1170
1171 -- Start of processing for Check_Parameterless_Call
1172
1173 begin
1174 -- Defend against junk stuff if errors already detected
1175
1176 if Total_Errors_Detected /= 0 then
1177 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1178 return;
1179 elsif Nkind (N) in N_Has_Chars
1180 and then not Is_Valid_Name (Chars (N))
1181 then
1182 return;
1183 end if;
1184
1185 Require_Entity (N);
1186 end if;
1187
1188 -- If the context expects a value, and the name is a procedure, this is
1189 -- most likely a missing 'Access. Don't try to resolve the parameterless
1190 -- call, error will be caught when the outer call is analyzed.
1191
1192 if Is_Entity_Name (N)
1193 and then Ekind (Entity (N)) = E_Procedure
1194 and then not Is_Overloaded (N)
1195 and then
1196 Nkind_In (Parent (N), N_Parameter_Association,
1197 N_Function_Call,
1198 N_Procedure_Call_Statement)
1199 then
1200 return;
1201 end if;
1202
1203 -- Rewrite as call if overloadable entity that is (or could be, in the
1204 -- overloaded case) a function call. If we know for sure that the entity
1205 -- is an enumeration literal, we do not rewrite it.
1206
1207 -- If the entity is the name of an operator, it cannot be a call because
1208 -- operators cannot have default parameters. In this case, this must be
1209 -- a string whose contents coincide with an operator name. Set the kind
1210 -- of the node appropriately.
1211
1212 if (Is_Entity_Name (N)
1213 and then Nkind (N) /= N_Operator_Symbol
1214 and then Is_Overloadable (Entity (N))
1215 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1216 or else Is_Overloaded (N)))
1217
1218 -- Rewrite as call if it is an explicit dereference of an expression of
1219 -- a subprogram access type, and the subprogram type is not that of a
1220 -- procedure or entry.
1221
1222 or else
1223 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1224
1225 -- Rewrite as call if it is a selected component which is a function,
1226 -- this is the case of a call to a protected function (which may be
1227 -- overloaded with other protected operations).
1228
1229 or else
1230 (Nkind (N) = N_Selected_Component
1231 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1232 or else
1233 (Ekind_In (Entity (Selector_Name (N)), E_Entry,
1234 E_Procedure)
1235 and then Is_Overloaded (Selector_Name (N)))))
1236
1237 -- If one of the above three conditions is met, rewrite as call. Apply
1238 -- the rewriting only once.
1239
1240 then
1241 if Nkind (Parent (N)) /= N_Function_Call
1242 or else N /= Name (Parent (N))
1243 then
1244
1245 -- This may be a prefixed call that was not fully analyzed, e.g.
1246 -- an actual in an instance.
1247
1248 if Ada_Version >= Ada_2005
1249 and then Nkind (N) = N_Selected_Component
1250 and then Is_Dispatching_Operation (Entity (Selector_Name (N)))
1251 then
1252 Analyze_Selected_Component (N);
1253
1254 if Nkind (N) /= N_Selected_Component then
1255 return;
1256 end if;
1257 end if;
1258
1259 -- The node is the name of the parameterless call. Preserve its
1260 -- descendants, which may be complex expressions.
1261
1262 Nam := Relocate_Node (N);
1263
1264 -- If overloaded, overload set belongs to new copy
1265
1266 Save_Interps (N, Nam);
1267
1268 -- Change node to parameterless function call (note that the
1269 -- Parameter_Associations associations field is left set to Empty,
1270 -- its normal default value since there are no parameters)
1271
1272 Change_Node (N, N_Function_Call);
1273 Set_Name (N, Nam);
1274 Set_Sloc (N, Sloc (Nam));
1275 Analyze_Call (N);
1276 end if;
1277
1278 elsif Nkind (N) = N_Parameter_Association then
1279 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1280
1281 elsif Nkind (N) = N_Operator_Symbol then
1282 Change_Operator_Symbol_To_String_Literal (N);
1283 Set_Is_Overloaded (N, False);
1284 Set_Etype (N, Any_String);
1285 end if;
1286 end Check_Parameterless_Call;
1287
1288 --------------------------------
1289 -- Is_Atomic_Ref_With_Address --
1290 --------------------------------
1291
1292 function Is_Atomic_Ref_With_Address (N : Node_Id) return Boolean is
1293 Pref : constant Node_Id := Prefix (N);
1294
1295 begin
1296 if not Is_Entity_Name (Pref) then
1297 return False;
1298
1299 else
1300 declare
1301 Pent : constant Entity_Id := Entity (Pref);
1302 Ptyp : constant Entity_Id := Etype (Pent);
1303 begin
1304 return not Is_Access_Type (Ptyp)
1305 and then (Is_Atomic (Ptyp) or else Is_Atomic (Pent))
1306 and then Present (Address_Clause (Pent));
1307 end;
1308 end if;
1309 end Is_Atomic_Ref_With_Address;
1310
1311 -----------------------------
1312 -- Is_Definite_Access_Type --
1313 -----------------------------
1314
1315 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1316 Btyp : constant Entity_Id := Base_Type (E);
1317 begin
1318 return Ekind (Btyp) = E_Access_Type
1319 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1320 and then Comes_From_Source (Btyp));
1321 end Is_Definite_Access_Type;
1322
1323 ----------------------
1324 -- Is_Predefined_Op --
1325 ----------------------
1326
1327 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1328 begin
1329 -- Predefined operators are intrinsic subprograms
1330
1331 if not Is_Intrinsic_Subprogram (Nam) then
1332 return False;
1333 end if;
1334
1335 -- A call to a back-end builtin is never a predefined operator
1336
1337 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1338 return False;
1339 end if;
1340
1341 return not Is_Generic_Instance (Nam)
1342 and then Chars (Nam) in Any_Operator_Name
1343 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1344 end Is_Predefined_Op;
1345
1346 -----------------------------
1347 -- Make_Call_Into_Operator --
1348 -----------------------------
1349
1350 procedure Make_Call_Into_Operator
1351 (N : Node_Id;
1352 Typ : Entity_Id;
1353 Op_Id : Entity_Id)
1354 is
1355 Op_Name : constant Name_Id := Chars (Op_Id);
1356 Act1 : Node_Id := First_Actual (N);
1357 Act2 : Node_Id := Next_Actual (Act1);
1358 Error : Boolean := False;
1359 Func : constant Entity_Id := Entity (Name (N));
1360 Is_Binary : constant Boolean := Present (Act2);
1361 Op_Node : Node_Id;
1362 Opnd_Type : Entity_Id := Empty;
1363 Orig_Type : Entity_Id := Empty;
1364 Pack : Entity_Id;
1365
1366 type Kind_Test is access function (E : Entity_Id) return Boolean;
1367
1368 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1369 -- If the operand is not universal, and the operator is given by an
1370 -- expanded name, verify that the operand has an interpretation with a
1371 -- type defined in the given scope of the operator.
1372
1373 function Type_In_P (Test : Kind_Test) return Entity_Id;
1374 -- Find a type of the given class in package Pack that contains the
1375 -- operator.
1376
1377 ---------------------------
1378 -- Operand_Type_In_Scope --
1379 ---------------------------
1380
1381 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1382 Nod : constant Node_Id := Right_Opnd (Op_Node);
1383 I : Interp_Index;
1384 It : Interp;
1385
1386 begin
1387 if not Is_Overloaded (Nod) then
1388 return Scope (Base_Type (Etype (Nod))) = S;
1389
1390 else
1391 Get_First_Interp (Nod, I, It);
1392 while Present (It.Typ) loop
1393 if Scope (Base_Type (It.Typ)) = S then
1394 return True;
1395 end if;
1396
1397 Get_Next_Interp (I, It);
1398 end loop;
1399
1400 return False;
1401 end if;
1402 end Operand_Type_In_Scope;
1403
1404 ---------------
1405 -- Type_In_P --
1406 ---------------
1407
1408 function Type_In_P (Test : Kind_Test) return Entity_Id is
1409 E : Entity_Id;
1410
1411 function In_Decl return Boolean;
1412 -- Verify that node is not part of the type declaration for the
1413 -- candidate type, which would otherwise be invisible.
1414
1415 -------------
1416 -- In_Decl --
1417 -------------
1418
1419 function In_Decl return Boolean is
1420 Decl_Node : constant Node_Id := Parent (E);
1421 N2 : Node_Id;
1422
1423 begin
1424 N2 := N;
1425
1426 if Etype (E) = Any_Type then
1427 return True;
1428
1429 elsif No (Decl_Node) then
1430 return False;
1431
1432 else
1433 while Present (N2)
1434 and then Nkind (N2) /= N_Compilation_Unit
1435 loop
1436 if N2 = Decl_Node then
1437 return True;
1438 else
1439 N2 := Parent (N2);
1440 end if;
1441 end loop;
1442
1443 return False;
1444 end if;
1445 end In_Decl;
1446
1447 -- Start of processing for Type_In_P
1448
1449 begin
1450 -- If the context type is declared in the prefix package, this is the
1451 -- desired base type.
1452
1453 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1454 return Base_Type (Typ);
1455
1456 else
1457 E := First_Entity (Pack);
1458 while Present (E) loop
1459 if Test (E) and then not In_Decl then
1460 return E;
1461 end if;
1462
1463 Next_Entity (E);
1464 end loop;
1465
1466 return Empty;
1467 end if;
1468 end Type_In_P;
1469
1470 -- Start of processing for Make_Call_Into_Operator
1471
1472 begin
1473 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1474
1475 -- Ensure that the corresponding operator has the same parent as the
1476 -- original call. This guarantees that parent traversals performed by
1477 -- the ABE mechanism succeed.
1478
1479 Set_Parent (Op_Node, Parent (N));
1480
1481 -- Binary operator
1482
1483 if Is_Binary then
1484 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1485 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1486 Save_Interps (Act1, Left_Opnd (Op_Node));
1487 Save_Interps (Act2, Right_Opnd (Op_Node));
1488 Act1 := Left_Opnd (Op_Node);
1489 Act2 := Right_Opnd (Op_Node);
1490
1491 -- Unary operator
1492
1493 else
1494 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1495 Save_Interps (Act1, Right_Opnd (Op_Node));
1496 Act1 := Right_Opnd (Op_Node);
1497 end if;
1498
1499 -- If the operator is denoted by an expanded name, and the prefix is
1500 -- not Standard, but the operator is a predefined one whose scope is
1501 -- Standard, then this is an implicit_operator, inserted as an
1502 -- interpretation by the procedure of the same name. This procedure
1503 -- overestimates the presence of implicit operators, because it does
1504 -- not examine the type of the operands. Verify now that the operand
1505 -- type appears in the given scope. If right operand is universal,
1506 -- check the other operand. In the case of concatenation, either
1507 -- argument can be the component type, so check the type of the result.
1508 -- If both arguments are literals, look for a type of the right kind
1509 -- defined in the given scope. This elaborate nonsense is brought to
1510 -- you courtesy of b33302a. The type itself must be frozen, so we must
1511 -- find the type of the proper class in the given scope.
1512
1513 -- A final wrinkle is the multiplication operator for fixed point types,
1514 -- which is defined in Standard only, and not in the scope of the
1515 -- fixed point type itself.
1516
1517 if Nkind (Name (N)) = N_Expanded_Name then
1518 Pack := Entity (Prefix (Name (N)));
1519
1520 -- If this is a package renaming, get renamed entity, which will be
1521 -- the scope of the operands if operaton is type-correct.
1522
1523 if Present (Renamed_Entity (Pack)) then
1524 Pack := Renamed_Entity (Pack);
1525 end if;
1526
1527 -- If the entity being called is defined in the given package, it is
1528 -- a renaming of a predefined operator, and known to be legal.
1529
1530 if Scope (Entity (Name (N))) = Pack
1531 and then Pack /= Standard_Standard
1532 then
1533 null;
1534
1535 -- Visibility does not need to be checked in an instance: if the
1536 -- operator was not visible in the generic it has been diagnosed
1537 -- already, else there is an implicit copy of it in the instance.
1538
1539 elsif In_Instance then
1540 null;
1541
1542 elsif Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide)
1543 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1544 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1545 then
1546 if Pack /= Standard_Standard then
1547 Error := True;
1548 end if;
1549
1550 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1551 -- available.
1552
1553 elsif Ada_Version >= Ada_2005
1554 and then Nam_In (Op_Name, Name_Op_Eq, Name_Op_Ne)
1555 and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1556 then
1557 null;
1558
1559 else
1560 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1561
1562 if Op_Name = Name_Op_Concat then
1563 Opnd_Type := Base_Type (Typ);
1564
1565 elsif (Scope (Opnd_Type) = Standard_Standard
1566 and then Is_Binary)
1567 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1568 and then Is_Binary
1569 and then not Comes_From_Source (Opnd_Type))
1570 then
1571 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1572 end if;
1573
1574 if Scope (Opnd_Type) = Standard_Standard then
1575
1576 -- Verify that the scope contains a type that corresponds to
1577 -- the given literal. Optimize the case where Pack is Standard.
1578
1579 if Pack /= Standard_Standard then
1580 if Opnd_Type = Universal_Integer then
1581 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1582
1583 elsif Opnd_Type = Universal_Real then
1584 Orig_Type := Type_In_P (Is_Real_Type'Access);
1585
1586 elsif Opnd_Type = Any_String then
1587 Orig_Type := Type_In_P (Is_String_Type'Access);
1588
1589 elsif Opnd_Type = Any_Access then
1590 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1591
1592 elsif Opnd_Type = Any_Composite then
1593 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1594
1595 if Present (Orig_Type) then
1596 if Has_Private_Component (Orig_Type) then
1597 Orig_Type := Empty;
1598 else
1599 Set_Etype (Act1, Orig_Type);
1600
1601 if Is_Binary then
1602 Set_Etype (Act2, Orig_Type);
1603 end if;
1604 end if;
1605 end if;
1606
1607 else
1608 Orig_Type := Empty;
1609 end if;
1610
1611 Error := No (Orig_Type);
1612 end if;
1613
1614 elsif Ekind (Opnd_Type) = E_Allocator_Type
1615 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1616 then
1617 Error := True;
1618
1619 -- If the type is defined elsewhere, and the operator is not
1620 -- defined in the given scope (by a renaming declaration, e.g.)
1621 -- then this is an error as well. If an extension of System is
1622 -- present, and the type may be defined there, Pack must be
1623 -- System itself.
1624
1625 elsif Scope (Opnd_Type) /= Pack
1626 and then Scope (Op_Id) /= Pack
1627 and then (No (System_Aux_Id)
1628 or else Scope (Opnd_Type) /= System_Aux_Id
1629 or else Pack /= Scope (System_Aux_Id))
1630 then
1631 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1632 Error := True;
1633 else
1634 Error := not Operand_Type_In_Scope (Pack);
1635 end if;
1636
1637 elsif Pack = Standard_Standard
1638 and then not Operand_Type_In_Scope (Standard_Standard)
1639 then
1640 Error := True;
1641 end if;
1642 end if;
1643
1644 if Error then
1645 Error_Msg_Node_2 := Pack;
1646 Error_Msg_NE
1647 ("& not declared in&", N, Selector_Name (Name (N)));
1648 Set_Etype (N, Any_Type);
1649 return;
1650
1651 -- Detect a mismatch between the context type and the result type
1652 -- in the named package, which is otherwise not detected if the
1653 -- operands are universal. Check is only needed if source entity is
1654 -- an operator, not a function that renames an operator.
1655
1656 elsif Nkind (Parent (N)) /= N_Type_Conversion
1657 and then Ekind (Entity (Name (N))) = E_Operator
1658 and then Is_Numeric_Type (Typ)
1659 and then not Is_Universal_Numeric_Type (Typ)
1660 and then Scope (Base_Type (Typ)) /= Pack
1661 and then not In_Instance
1662 then
1663 if Is_Fixed_Point_Type (Typ)
1664 and then Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide)
1665 then
1666 -- Already checked above
1667
1668 null;
1669
1670 -- Operator may be defined in an extension of System
1671
1672 elsif Present (System_Aux_Id)
1673 and then Present (Opnd_Type)
1674 and then Scope (Opnd_Type) = System_Aux_Id
1675 then
1676 null;
1677
1678 else
1679 -- Could we use Wrong_Type here??? (this would require setting
1680 -- Etype (N) to the actual type found where Typ was expected).
1681
1682 Error_Msg_NE ("expect }", N, Typ);
1683 end if;
1684 end if;
1685 end if;
1686
1687 Set_Chars (Op_Node, Op_Name);
1688
1689 if not Is_Private_Type (Etype (N)) then
1690 Set_Etype (Op_Node, Base_Type (Etype (N)));
1691 else
1692 Set_Etype (Op_Node, Etype (N));
1693 end if;
1694
1695 -- If this is a call to a function that renames a predefined equality,
1696 -- the renaming declaration provides a type that must be used to
1697 -- resolve the operands. This must be done now because resolution of
1698 -- the equality node will not resolve any remaining ambiguity, and it
1699 -- assumes that the first operand is not overloaded.
1700
1701 if Nam_In (Op_Name, Name_Op_Eq, Name_Op_Ne)
1702 and then Ekind (Func) = E_Function
1703 and then Is_Overloaded (Act1)
1704 then
1705 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1706 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1707 end if;
1708
1709 Set_Entity (Op_Node, Op_Id);
1710 Generate_Reference (Op_Id, N, ' ');
1711
1712 -- Do rewrite setting Comes_From_Source on the result if the original
1713 -- call came from source. Although it is not strictly the case that the
1714 -- operator as such comes from the source, logically it corresponds
1715 -- exactly to the function call in the source, so it should be marked
1716 -- this way (e.g. to make sure that validity checks work fine).
1717
1718 declare
1719 CS : constant Boolean := Comes_From_Source (N);
1720 begin
1721 Rewrite (N, Op_Node);
1722 Set_Comes_From_Source (N, CS);
1723 end;
1724
1725 -- If this is an arithmetic operator and the result type is private,
1726 -- the operands and the result must be wrapped in conversion to
1727 -- expose the underlying numeric type and expand the proper checks,
1728 -- e.g. on division.
1729
1730 if Is_Private_Type (Typ) then
1731 case Nkind (N) is
1732 when N_Op_Add
1733 | N_Op_Divide
1734 | N_Op_Expon
1735 | N_Op_Mod
1736 | N_Op_Multiply
1737 | N_Op_Rem
1738 | N_Op_Subtract
1739 =>
1740 Resolve_Intrinsic_Operator (N, Typ);
1741
1742 when N_Op_Abs
1743 | N_Op_Minus
1744 | N_Op_Plus
1745 =>
1746 Resolve_Intrinsic_Unary_Operator (N, Typ);
1747
1748 when others =>
1749 Resolve (N, Typ);
1750 end case;
1751 else
1752 Resolve (N, Typ);
1753 end if;
1754
1755 -- If in ASIS_Mode, propagate operand types to original actuals of
1756 -- function call, which would otherwise not be fully resolved. If
1757 -- the call has already been constant-folded, nothing to do. We
1758 -- relocate the operand nodes rather than copy them, to preserve
1759 -- original_node pointers, given that the operands themselves may
1760 -- have been rewritten. If the call was itself a rewriting of an
1761 -- operator node, nothing to do.
1762
1763 if ASIS_Mode
1764 and then Nkind (N) in N_Op
1765 and then Nkind (Original_Node (N)) = N_Function_Call
1766 then
1767 declare
1768 L : Node_Id;
1769 R : constant Node_Id := Right_Opnd (N);
1770
1771 Old_First : constant Node_Id :=
1772 First (Parameter_Associations (Original_Node (N)));
1773 Old_Sec : Node_Id;
1774
1775 begin
1776 if Is_Binary then
1777 L := Left_Opnd (N);
1778 Old_Sec := Next (Old_First);
1779
1780 -- If the original call has named associations, replace the
1781 -- explicit actual parameter in the association with the proper
1782 -- resolved operand.
1783
1784 if Nkind (Old_First) = N_Parameter_Association then
1785 if Chars (Selector_Name (Old_First)) =
1786 Chars (First_Entity (Op_Id))
1787 then
1788 Rewrite (Explicit_Actual_Parameter (Old_First),
1789 Relocate_Node (L));
1790 else
1791 Rewrite (Explicit_Actual_Parameter (Old_First),
1792 Relocate_Node (R));
1793 end if;
1794
1795 else
1796 Rewrite (Old_First, Relocate_Node (L));
1797 end if;
1798
1799 if Nkind (Old_Sec) = N_Parameter_Association then
1800 if Chars (Selector_Name (Old_Sec)) =
1801 Chars (First_Entity (Op_Id))
1802 then
1803 Rewrite (Explicit_Actual_Parameter (Old_Sec),
1804 Relocate_Node (L));
1805 else
1806 Rewrite (Explicit_Actual_Parameter (Old_Sec),
1807 Relocate_Node (R));
1808 end if;
1809
1810 else
1811 Rewrite (Old_Sec, Relocate_Node (R));
1812 end if;
1813
1814 else
1815 if Nkind (Old_First) = N_Parameter_Association then
1816 Rewrite (Explicit_Actual_Parameter (Old_First),
1817 Relocate_Node (R));
1818 else
1819 Rewrite (Old_First, Relocate_Node (R));
1820 end if;
1821 end if;
1822 end;
1823
1824 Set_Parent (Original_Node (N), Parent (N));
1825 end if;
1826 end Make_Call_Into_Operator;
1827
1828 -------------------
1829 -- Operator_Kind --
1830 -------------------
1831
1832 function Operator_Kind
1833 (Op_Name : Name_Id;
1834 Is_Binary : Boolean) return Node_Kind
1835 is
1836 Kind : Node_Kind;
1837
1838 begin
1839 -- Use CASE statement or array???
1840
1841 if Is_Binary then
1842 if Op_Name = Name_Op_And then
1843 Kind := N_Op_And;
1844 elsif Op_Name = Name_Op_Or then
1845 Kind := N_Op_Or;
1846 elsif Op_Name = Name_Op_Xor then
1847 Kind := N_Op_Xor;
1848 elsif Op_Name = Name_Op_Eq then
1849 Kind := N_Op_Eq;
1850 elsif Op_Name = Name_Op_Ne then
1851 Kind := N_Op_Ne;
1852 elsif Op_Name = Name_Op_Lt then
1853 Kind := N_Op_Lt;
1854 elsif Op_Name = Name_Op_Le then
1855 Kind := N_Op_Le;
1856 elsif Op_Name = Name_Op_Gt then
1857 Kind := N_Op_Gt;
1858 elsif Op_Name = Name_Op_Ge then
1859 Kind := N_Op_Ge;
1860 elsif Op_Name = Name_Op_Add then
1861 Kind := N_Op_Add;
1862 elsif Op_Name = Name_Op_Subtract then
1863 Kind := N_Op_Subtract;
1864 elsif Op_Name = Name_Op_Concat then
1865 Kind := N_Op_Concat;
1866 elsif Op_Name = Name_Op_Multiply then
1867 Kind := N_Op_Multiply;
1868 elsif Op_Name = Name_Op_Divide then
1869 Kind := N_Op_Divide;
1870 elsif Op_Name = Name_Op_Mod then
1871 Kind := N_Op_Mod;
1872 elsif Op_Name = Name_Op_Rem then
1873 Kind := N_Op_Rem;
1874 elsif Op_Name = Name_Op_Expon then
1875 Kind := N_Op_Expon;
1876 else
1877 raise Program_Error;
1878 end if;
1879
1880 -- Unary operators
1881
1882 else
1883 if Op_Name = Name_Op_Add then
1884 Kind := N_Op_Plus;
1885 elsif Op_Name = Name_Op_Subtract then
1886 Kind := N_Op_Minus;
1887 elsif Op_Name = Name_Op_Abs then
1888 Kind := N_Op_Abs;
1889 elsif Op_Name = Name_Op_Not then
1890 Kind := N_Op_Not;
1891 else
1892 raise Program_Error;
1893 end if;
1894 end if;
1895
1896 return Kind;
1897 end Operator_Kind;
1898
1899 ----------------------------
1900 -- Preanalyze_And_Resolve --
1901 ----------------------------
1902
1903 procedure Preanalyze_And_Resolve
1904 (N : Node_Id;
1905 T : Entity_Id;
1906 With_Freezing : Boolean)
1907 is
1908 Save_Full_Analysis : constant Boolean := Full_Analysis;
1909 Save_Must_Not_Freeze : constant Boolean := Must_Not_Freeze (N);
1910 Save_Preanalysis_Count : constant Nat :=
1911 Inside_Preanalysis_Without_Freezing;
1912 begin
1913 pragma Assert (Nkind (N) in N_Subexpr);
1914
1915 if not With_Freezing then
1916 Set_Must_Not_Freeze (N);
1917 Inside_Preanalysis_Without_Freezing :=
1918 Inside_Preanalysis_Without_Freezing + 1;
1919 end if;
1920
1921 Full_Analysis := False;
1922 Expander_Mode_Save_And_Set (False);
1923
1924 -- Normally, we suppress all checks for this preanalysis. There is no
1925 -- point in processing them now, since they will be applied properly
1926 -- and in the proper location when the default expressions reanalyzed
1927 -- and reexpanded later on. We will also have more information at that
1928 -- point for possible suppression of individual checks.
1929
1930 -- However, in SPARK mode, most expansion is suppressed, and this
1931 -- later reanalysis and reexpansion may not occur. SPARK mode does
1932 -- require the setting of checking flags for proof purposes, so we
1933 -- do the SPARK preanalysis without suppressing checks.
1934
1935 -- This special handling for SPARK mode is required for example in the
1936 -- case of Ada 2012 constructs such as quantified expressions, which are
1937 -- expanded in two separate steps.
1938
1939 if GNATprove_Mode then
1940 Analyze_And_Resolve (N, T);
1941 else
1942 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1943 end if;
1944
1945 Expander_Mode_Restore;
1946 Full_Analysis := Save_Full_Analysis;
1947 Set_Must_Not_Freeze (N, Save_Must_Not_Freeze);
1948
1949 if not With_Freezing then
1950 Inside_Preanalysis_Without_Freezing :=
1951 Inside_Preanalysis_Without_Freezing - 1;
1952 end if;
1953
1954 pragma Assert
1955 (Inside_Preanalysis_Without_Freezing = Save_Preanalysis_Count);
1956 end Preanalyze_And_Resolve;
1957
1958 ----------------------------
1959 -- Preanalyze_And_Resolve --
1960 ----------------------------
1961
1962 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1963 begin
1964 Preanalyze_And_Resolve (N, T, With_Freezing => False);
1965 end Preanalyze_And_Resolve;
1966
1967 -- Version without context type
1968
1969 procedure Preanalyze_And_Resolve (N : Node_Id) is
1970 Save_Full_Analysis : constant Boolean := Full_Analysis;
1971
1972 begin
1973 Full_Analysis := False;
1974 Expander_Mode_Save_And_Set (False);
1975
1976 Analyze (N);
1977 Resolve (N, Etype (N), Suppress => All_Checks);
1978
1979 Expander_Mode_Restore;
1980 Full_Analysis := Save_Full_Analysis;
1981 end Preanalyze_And_Resolve;
1982
1983 ------------------------------------------
1984 -- Preanalyze_With_Freezing_And_Resolve --
1985 ------------------------------------------
1986
1987 procedure Preanalyze_With_Freezing_And_Resolve
1988 (N : Node_Id;
1989 T : Entity_Id)
1990 is
1991 begin
1992 Preanalyze_And_Resolve (N, T, With_Freezing => True);
1993 end Preanalyze_With_Freezing_And_Resolve;
1994
1995 ----------------------------------
1996 -- Replace_Actual_Discriminants --
1997 ----------------------------------
1998
1999 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
2000 Loc : constant Source_Ptr := Sloc (N);
2001 Tsk : Node_Id := Empty;
2002
2003 function Process_Discr (Nod : Node_Id) return Traverse_Result;
2004 -- Comment needed???
2005
2006 -------------------
2007 -- Process_Discr --
2008 -------------------
2009
2010 function Process_Discr (Nod : Node_Id) return Traverse_Result is
2011 Ent : Entity_Id;
2012
2013 begin
2014 if Nkind (Nod) = N_Identifier then
2015 Ent := Entity (Nod);
2016
2017 if Present (Ent)
2018 and then Ekind (Ent) = E_Discriminant
2019 then
2020 Rewrite (Nod,
2021 Make_Selected_Component (Loc,
2022 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
2023 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
2024
2025 Set_Etype (Nod, Etype (Ent));
2026 end if;
2027
2028 end if;
2029
2030 return OK;
2031 end Process_Discr;
2032
2033 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
2034
2035 -- Start of processing for Replace_Actual_Discriminants
2036
2037 begin
2038 if Expander_Active then
2039 null;
2040
2041 -- Allow the replacement of concurrent discriminants in GNATprove even
2042 -- though this is a light expansion activity. Note that generic units
2043 -- are not modified.
2044
2045 elsif GNATprove_Mode and not Inside_A_Generic then
2046 null;
2047
2048 else
2049 return;
2050 end if;
2051
2052 if Nkind (Name (N)) = N_Selected_Component then
2053 Tsk := Prefix (Name (N));
2054
2055 elsif Nkind (Name (N)) = N_Indexed_Component then
2056 Tsk := Prefix (Prefix (Name (N)));
2057 end if;
2058
2059 if Present (Tsk) then
2060 Replace_Discrs (Default);
2061 end if;
2062 end Replace_Actual_Discriminants;
2063
2064 -------------
2065 -- Resolve --
2066 -------------
2067
2068 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
2069 Ambiguous : Boolean := False;
2070 Ctx_Type : Entity_Id := Typ;
2071 Expr_Type : Entity_Id := Empty; -- prevent junk warning
2072 Err_Type : Entity_Id := Empty;
2073 Found : Boolean := False;
2074 From_Lib : Boolean;
2075 I : Interp_Index;
2076 I1 : Interp_Index := 0; -- prevent junk warning
2077 It : Interp;
2078 It1 : Interp;
2079 Seen : Entity_Id := Empty; -- prevent junk warning
2080
2081 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
2082 -- Determine whether a node comes from a predefined library unit or
2083 -- Standard.
2084
2085 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
2086 -- Try and fix up a literal so that it matches its expected type. New
2087 -- literals are manufactured if necessary to avoid cascaded errors.
2088
2089 procedure Report_Ambiguous_Argument;
2090 -- Additional diagnostics when an ambiguous call has an ambiguous
2091 -- argument (typically a controlling actual).
2092
2093 procedure Resolution_Failed;
2094 -- Called when attempt at resolving current expression fails
2095
2096 ------------------------------------
2097 -- Comes_From_Predefined_Lib_Unit --
2098 -------------------------------------
2099
2100 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
2101 begin
2102 return
2103 Sloc (Nod) = Standard_Location or else In_Predefined_Unit (Nod);
2104 end Comes_From_Predefined_Lib_Unit;
2105
2106 --------------------
2107 -- Patch_Up_Value --
2108 --------------------
2109
2110 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
2111 begin
2112 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
2113 Rewrite (N,
2114 Make_Real_Literal (Sloc (N),
2115 Realval => UR_From_Uint (Intval (N))));
2116 Set_Etype (N, Universal_Real);
2117 Set_Is_Static_Expression (N);
2118
2119 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
2120 Rewrite (N,
2121 Make_Integer_Literal (Sloc (N),
2122 Intval => UR_To_Uint (Realval (N))));
2123 Set_Etype (N, Universal_Integer);
2124 Set_Is_Static_Expression (N);
2125
2126 elsif Nkind (N) = N_String_Literal
2127 and then Is_Character_Type (Typ)
2128 then
2129 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
2130 Rewrite (N,
2131 Make_Character_Literal (Sloc (N),
2132 Chars => Name_Find,
2133 Char_Literal_Value =>
2134 UI_From_Int (Character'Pos ('A'))));
2135 Set_Etype (N, Any_Character);
2136 Set_Is_Static_Expression (N);
2137
2138 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
2139 Rewrite (N,
2140 Make_String_Literal (Sloc (N),
2141 Strval => End_String));
2142
2143 elsif Nkind (N) = N_Range then
2144 Patch_Up_Value (Low_Bound (N), Typ);
2145 Patch_Up_Value (High_Bound (N), Typ);
2146 end if;
2147 end Patch_Up_Value;
2148
2149 -------------------------------
2150 -- Report_Ambiguous_Argument --
2151 -------------------------------
2152
2153 procedure Report_Ambiguous_Argument is
2154 Arg : constant Node_Id := First (Parameter_Associations (N));
2155 I : Interp_Index;
2156 It : Interp;
2157
2158 begin
2159 if Nkind (Arg) = N_Function_Call
2160 and then Is_Entity_Name (Name (Arg))
2161 and then Is_Overloaded (Name (Arg))
2162 then
2163 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
2164
2165 -- Could use comments on what is going on here???
2166
2167 Get_First_Interp (Name (Arg), I, It);
2168 while Present (It.Nam) loop
2169 Error_Msg_Sloc := Sloc (It.Nam);
2170
2171 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
2172 Error_Msg_N ("interpretation (inherited) #!", Arg);
2173 else
2174 Error_Msg_N ("interpretation #!", Arg);
2175 end if;
2176
2177 Get_Next_Interp (I, It);
2178 end loop;
2179 end if;
2180 end Report_Ambiguous_Argument;
2181
2182 -----------------------
2183 -- Resolution_Failed --
2184 -----------------------
2185
2186 procedure Resolution_Failed is
2187 begin
2188 Patch_Up_Value (N, Typ);
2189
2190 -- Set the type to the desired one to minimize cascaded errors. Note
2191 -- that this is an approximation and does not work in all cases.
2192
2193 Set_Etype (N, Typ);
2194
2195 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
2196 Set_Is_Overloaded (N, False);
2197
2198 -- The caller will return without calling the expander, so we need
2199 -- to set the analyzed flag. Note that it is fine to set Analyzed
2200 -- to True even if we are in the middle of a shallow analysis,
2201 -- (see the spec of sem for more details) since this is an error
2202 -- situation anyway, and there is no point in repeating the
2203 -- analysis later (indeed it won't work to repeat it later, since
2204 -- we haven't got a clear resolution of which entity is being
2205 -- referenced.)
2206
2207 Set_Analyzed (N, True);
2208 return;
2209 end Resolution_Failed;
2210
2211 -- Start of processing for Resolve
2212
2213 begin
2214 if N = Error then
2215 return;
2216 end if;
2217
2218 -- Access attribute on remote subprogram cannot be used for a non-remote
2219 -- access-to-subprogram type.
2220
2221 if Nkind (N) = N_Attribute_Reference
2222 and then Nam_In (Attribute_Name (N), Name_Access,
2223 Name_Unrestricted_Access,
2224 Name_Unchecked_Access)
2225 and then Comes_From_Source (N)
2226 and then Is_Entity_Name (Prefix (N))
2227 and then Is_Subprogram (Entity (Prefix (N)))
2228 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
2229 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
2230 then
2231 Error_Msg_N
2232 ("prefix must statically denote a non-remote subprogram", N);
2233 end if;
2234
2235 From_Lib := Comes_From_Predefined_Lib_Unit (N);
2236
2237 -- If the context is a Remote_Access_To_Subprogram, access attributes
2238 -- must be resolved with the corresponding fat pointer. There is no need
2239 -- to check for the attribute name since the return type of an
2240 -- attribute is never a remote type.
2241
2242 if Nkind (N) = N_Attribute_Reference
2243 and then Comes_From_Source (N)
2244 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
2245 then
2246 declare
2247 Attr : constant Attribute_Id :=
2248 Get_Attribute_Id (Attribute_Name (N));
2249 Pref : constant Node_Id := Prefix (N);
2250 Decl : Node_Id;
2251 Spec : Node_Id;
2252 Is_Remote : Boolean := True;
2253
2254 begin
2255 -- Check that Typ is a remote access-to-subprogram type
2256
2257 if Is_Remote_Access_To_Subprogram_Type (Typ) then
2258
2259 -- Prefix (N) must statically denote a remote subprogram
2260 -- declared in a package specification.
2261
2262 if Attr = Attribute_Access or else
2263 Attr = Attribute_Unchecked_Access or else
2264 Attr = Attribute_Unrestricted_Access
2265 then
2266 Decl := Unit_Declaration_Node (Entity (Pref));
2267
2268 if Nkind (Decl) = N_Subprogram_Body then
2269 Spec := Corresponding_Spec (Decl);
2270
2271 if Present (Spec) then
2272 Decl := Unit_Declaration_Node (Spec);
2273 end if;
2274 end if;
2275
2276 Spec := Parent (Decl);
2277
2278 if not Is_Entity_Name (Prefix (N))
2279 or else Nkind (Spec) /= N_Package_Specification
2280 or else
2281 not Is_Remote_Call_Interface (Defining_Entity (Spec))
2282 then
2283 Is_Remote := False;
2284 Error_Msg_N
2285 ("prefix must statically denote a remote subprogram ",
2286 N);
2287 end if;
2288
2289 -- If we are generating code in distributed mode, perform
2290 -- semantic checks against corresponding remote entities.
2291
2292 if Expander_Active
2293 and then Get_PCS_Name /= Name_No_DSA
2294 then
2295 Check_Subtype_Conformant
2296 (New_Id => Entity (Prefix (N)),
2297 Old_Id => Designated_Type
2298 (Corresponding_Remote_Type (Typ)),
2299 Err_Loc => N);
2300
2301 if Is_Remote then
2302 Process_Remote_AST_Attribute (N, Typ);
2303 end if;
2304 end if;
2305 end if;
2306 end if;
2307 end;
2308 end if;
2309
2310 Debug_A_Entry ("resolving ", N);
2311
2312 if Debug_Flag_V then
2313 Write_Overloads (N);
2314 end if;
2315
2316 if Comes_From_Source (N) then
2317 if Is_Fixed_Point_Type (Typ) then
2318 Check_Restriction (No_Fixed_Point, N);
2319
2320 elsif Is_Floating_Point_Type (Typ)
2321 and then Typ /= Universal_Real
2322 and then Typ /= Any_Real
2323 then
2324 Check_Restriction (No_Floating_Point, N);
2325 end if;
2326 end if;
2327
2328 -- Return if already analyzed
2329
2330 if Analyzed (N) then
2331 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2332 Analyze_Dimension (N);
2333 return;
2334
2335 -- Any case of Any_Type as the Etype value means that we had a
2336 -- previous error.
2337
2338 elsif Etype (N) = Any_Type then
2339 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2340 return;
2341 end if;
2342
2343 Check_Parameterless_Call (N);
2344
2345 -- The resolution of an Expression_With_Actions is determined by
2346 -- its Expression.
2347
2348 if Nkind (N) = N_Expression_With_Actions then
2349 Resolve (Expression (N), Typ);
2350
2351 Found := True;
2352 Expr_Type := Etype (Expression (N));
2353
2354 -- If not overloaded, then we know the type, and all that needs doing
2355 -- is to check that this type is compatible with the context.
2356
2357 elsif not Is_Overloaded (N) then
2358 Found := Covers (Typ, Etype (N));
2359 Expr_Type := Etype (N);
2360
2361 -- In the overloaded case, we must select the interpretation that
2362 -- is compatible with the context (i.e. the type passed to Resolve)
2363
2364 else
2365 -- Loop through possible interpretations
2366
2367 Get_First_Interp (N, I, It);
2368 Interp_Loop : while Present (It.Typ) loop
2369 if Debug_Flag_V then
2370 Write_Str ("Interp: ");
2371 Write_Interp (It);
2372 end if;
2373
2374 -- We are only interested in interpretations that are compatible
2375 -- with the expected type, any other interpretations are ignored.
2376
2377 if not Covers (Typ, It.Typ) then
2378 if Debug_Flag_V then
2379 Write_Str (" interpretation incompatible with context");
2380 Write_Eol;
2381 end if;
2382
2383 else
2384 -- Skip the current interpretation if it is disabled by an
2385 -- abstract operator. This action is performed only when the
2386 -- type against which we are resolving is the same as the
2387 -- type of the interpretation.
2388
2389 if Ada_Version >= Ada_2005
2390 and then It.Typ = Typ
2391 and then Typ /= Universal_Integer
2392 and then Typ /= Universal_Real
2393 and then Present (It.Abstract_Op)
2394 then
2395 if Debug_Flag_V then
2396 Write_Line ("Skip.");
2397 end if;
2398
2399 goto Continue;
2400 end if;
2401
2402 -- First matching interpretation
2403
2404 if not Found then
2405 Found := True;
2406 I1 := I;
2407 Seen := It.Nam;
2408 Expr_Type := It.Typ;
2409
2410 -- Matching interpretation that is not the first, maybe an
2411 -- error, but there are some cases where preference rules are
2412 -- used to choose between the two possibilities. These and
2413 -- some more obscure cases are handled in Disambiguate.
2414
2415 else
2416 -- If the current statement is part of a predefined library
2417 -- unit, then all interpretations which come from user level
2418 -- packages should not be considered. Check previous and
2419 -- current one.
2420
2421 if From_Lib then
2422 if not Comes_From_Predefined_Lib_Unit (It.Nam) then
2423 goto Continue;
2424
2425 elsif not Comes_From_Predefined_Lib_Unit (Seen) then
2426
2427 -- Previous interpretation must be discarded
2428
2429 I1 := I;
2430 Seen := It.Nam;
2431 Expr_Type := It.Typ;
2432 Set_Entity (N, Seen);
2433 goto Continue;
2434 end if;
2435 end if;
2436
2437 -- Otherwise apply further disambiguation steps
2438
2439 Error_Msg_Sloc := Sloc (Seen);
2440 It1 := Disambiguate (N, I1, I, Typ);
2441
2442 -- Disambiguation has succeeded. Skip the remaining
2443 -- interpretations.
2444
2445 if It1 /= No_Interp then
2446 Seen := It1.Nam;
2447 Expr_Type := It1.Typ;
2448
2449 while Present (It.Typ) loop
2450 Get_Next_Interp (I, It);
2451 end loop;
2452
2453 else
2454 -- Before we issue an ambiguity complaint, check for the
2455 -- case of a subprogram call where at least one of the
2456 -- arguments is Any_Type, and if so suppress the message,
2457 -- since it is a cascaded error. This can also happen for
2458 -- a generalized indexing operation.
2459
2460 if Nkind (N) in N_Subprogram_Call
2461 or else (Nkind (N) = N_Indexed_Component
2462 and then Present (Generalized_Indexing (N)))
2463 then
2464 declare
2465 A : Node_Id;
2466 E : Node_Id;
2467
2468 begin
2469 if Nkind (N) = N_Indexed_Component then
2470 Rewrite (N, Generalized_Indexing (N));
2471 end if;
2472
2473 A := First_Actual (N);
2474 while Present (A) loop
2475 E := A;
2476
2477 if Nkind (E) = N_Parameter_Association then
2478 E := Explicit_Actual_Parameter (E);
2479 end if;
2480
2481 if Etype (E) = Any_Type then
2482 if Debug_Flag_V then
2483 Write_Str ("Any_Type in call");
2484 Write_Eol;
2485 end if;
2486
2487 exit Interp_Loop;
2488 end if;
2489
2490 Next_Actual (A);
2491 end loop;
2492 end;
2493
2494 elsif Nkind (N) in N_Binary_Op
2495 and then (Etype (Left_Opnd (N)) = Any_Type
2496 or else Etype (Right_Opnd (N)) = Any_Type)
2497 then
2498 exit Interp_Loop;
2499
2500 elsif Nkind (N) in N_Unary_Op
2501 and then Etype (Right_Opnd (N)) = Any_Type
2502 then
2503 exit Interp_Loop;
2504 end if;
2505
2506 -- Not that special case, so issue message using the flag
2507 -- Ambiguous to control printing of the header message
2508 -- only at the start of an ambiguous set.
2509
2510 if not Ambiguous then
2511 if Nkind (N) = N_Function_Call
2512 and then Nkind (Name (N)) = N_Explicit_Dereference
2513 then
2514 Error_Msg_N
2515 ("ambiguous expression (cannot resolve indirect "
2516 & "call)!", N);
2517 else
2518 Error_Msg_NE -- CODEFIX
2519 ("ambiguous expression (cannot resolve&)!",
2520 N, It.Nam);
2521 end if;
2522
2523 Ambiguous := True;
2524
2525 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2526 Error_Msg_N
2527 ("\\possible interpretation (inherited)#!", N);
2528 else
2529 Error_Msg_N -- CODEFIX
2530 ("\\possible interpretation#!", N);
2531 end if;
2532
2533 if Nkind (N) in N_Subprogram_Call
2534 and then Present (Parameter_Associations (N))
2535 then
2536 Report_Ambiguous_Argument;
2537 end if;
2538 end if;
2539
2540 Error_Msg_Sloc := Sloc (It.Nam);
2541
2542 -- By default, the error message refers to the candidate
2543 -- interpretation. But if it is a predefined operator, it
2544 -- is implicitly declared at the declaration of the type
2545 -- of the operand. Recover the sloc of that declaration
2546 -- for the error message.
2547
2548 if Nkind (N) in N_Op
2549 and then Scope (It.Nam) = Standard_Standard
2550 and then not Is_Overloaded (Right_Opnd (N))
2551 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2552 Standard_Standard
2553 then
2554 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2555
2556 if Comes_From_Source (Err_Type)
2557 and then Present (Parent (Err_Type))
2558 then
2559 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2560 end if;
2561
2562 elsif Nkind (N) in N_Binary_Op
2563 and then Scope (It.Nam) = Standard_Standard
2564 and then not Is_Overloaded (Left_Opnd (N))
2565 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2566 Standard_Standard
2567 then
2568 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2569
2570 if Comes_From_Source (Err_Type)
2571 and then Present (Parent (Err_Type))
2572 then
2573 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2574 end if;
2575
2576 -- If this is an indirect call, use the subprogram_type
2577 -- in the message, to have a meaningful location. Also
2578 -- indicate if this is an inherited operation, created
2579 -- by a type declaration.
2580
2581 elsif Nkind (N) = N_Function_Call
2582 and then Nkind (Name (N)) = N_Explicit_Dereference
2583 and then Is_Type (It.Nam)
2584 then
2585 Err_Type := It.Nam;
2586 Error_Msg_Sloc :=
2587 Sloc (Associated_Node_For_Itype (Err_Type));
2588 else
2589 Err_Type := Empty;
2590 end if;
2591
2592 if Nkind (N) in N_Op
2593 and then Scope (It.Nam) = Standard_Standard
2594 and then Present (Err_Type)
2595 then
2596 -- Special-case the message for universal_fixed
2597 -- operators, which are not declared with the type
2598 -- of the operand, but appear forever in Standard.
2599
2600 if It.Typ = Universal_Fixed
2601 and then Scope (It.Nam) = Standard_Standard
2602 then
2603 Error_Msg_N
2604 ("\\possible interpretation as universal_fixed "
2605 & "operation (RM 4.5.5 (19))", N);
2606 else
2607 Error_Msg_N
2608 ("\\possible interpretation (predefined)#!", N);
2609 end if;
2610
2611 elsif
2612 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2613 then
2614 Error_Msg_N
2615 ("\\possible interpretation (inherited)#!", N);
2616 else
2617 Error_Msg_N -- CODEFIX
2618 ("\\possible interpretation#!", N);
2619 end if;
2620
2621 end if;
2622 end if;
2623
2624 -- We have a matching interpretation, Expr_Type is the type
2625 -- from this interpretation, and Seen is the entity.
2626
2627 -- For an operator, just set the entity name. The type will be
2628 -- set by the specific operator resolution routine.
2629
2630 if Nkind (N) in N_Op then
2631 Set_Entity (N, Seen);
2632 Generate_Reference (Seen, N);
2633
2634 elsif Nkind_In (N, N_Case_Expression,
2635 N_Character_Literal,
2636 N_Delta_Aggregate,
2637 N_If_Expression)
2638 then
2639 Set_Etype (N, Expr_Type);
2640
2641 -- AI05-0139-2: Expression is overloaded because type has
2642 -- implicit dereference. The context may be the one that
2643 -- requires implicit dereferemce.
2644
2645 elsif Has_Implicit_Dereference (Expr_Type) then
2646 Set_Etype (N, Expr_Type);
2647 Set_Is_Overloaded (N, False);
2648
2649 -- If the expression is an entity, generate a reference
2650 -- to it, as this is not done for an overloaded construct
2651 -- during analysis.
2652
2653 if Is_Entity_Name (N)
2654 and then Comes_From_Source (N)
2655 then
2656 Generate_Reference (Entity (N), N);
2657
2658 -- Examine access discriminants of entity type,
2659 -- to check whether one of them yields the
2660 -- expected type.
2661
2662 declare
2663 Disc : Entity_Id :=
2664 First_Discriminant (Etype (Entity (N)));
2665
2666 begin
2667 while Present (Disc) loop
2668 exit when Is_Access_Type (Etype (Disc))
2669 and then Has_Implicit_Dereference (Disc)
2670 and then Designated_Type (Etype (Disc)) = Typ;
2671
2672 Next_Discriminant (Disc);
2673 end loop;
2674
2675 if Present (Disc) then
2676 Build_Explicit_Dereference (N, Disc);
2677 end if;
2678 end;
2679 end if;
2680
2681 exit Interp_Loop;
2682
2683 elsif Is_Overloaded (N)
2684 and then Present (It.Nam)
2685 and then Ekind (It.Nam) = E_Discriminant
2686 and then Has_Implicit_Dereference (It.Nam)
2687 then
2688 -- If the node is a general indexing, the dereference is
2689 -- is inserted when resolving the rewritten form, else
2690 -- insert it now.
2691
2692 if Nkind (N) /= N_Indexed_Component
2693 or else No (Generalized_Indexing (N))
2694 then
2695 Build_Explicit_Dereference (N, It.Nam);
2696 end if;
2697
2698 -- For an explicit dereference, attribute reference, range,
2699 -- short-circuit form (which is not an operator node), or call
2700 -- with a name that is an explicit dereference, there is
2701 -- nothing to be done at this point.
2702
2703 elsif Nkind_In (N, N_Attribute_Reference,
2704 N_And_Then,
2705 N_Explicit_Dereference,
2706 N_Identifier,
2707 N_Indexed_Component,
2708 N_Or_Else,
2709 N_Range,
2710 N_Selected_Component,
2711 N_Slice)
2712 or else Nkind (Name (N)) = N_Explicit_Dereference
2713 then
2714 null;
2715
2716 -- For procedure or function calls, set the type of the name,
2717 -- and also the entity pointer for the prefix.
2718
2719 elsif Nkind (N) in N_Subprogram_Call
2720 and then Is_Entity_Name (Name (N))
2721 then
2722 Set_Etype (Name (N), Expr_Type);
2723 Set_Entity (Name (N), Seen);
2724 Generate_Reference (Seen, Name (N));
2725
2726 elsif Nkind (N) = N_Function_Call
2727 and then Nkind (Name (N)) = N_Selected_Component
2728 then
2729 Set_Etype (Name (N), Expr_Type);
2730 Set_Entity (Selector_Name (Name (N)), Seen);
2731 Generate_Reference (Seen, Selector_Name (Name (N)));
2732
2733 -- For all other cases, just set the type of the Name
2734
2735 else
2736 Set_Etype (Name (N), Expr_Type);
2737 end if;
2738
2739 end if;
2740
2741 <<Continue>>
2742
2743 -- Move to next interpretation
2744
2745 exit Interp_Loop when No (It.Typ);
2746
2747 Get_Next_Interp (I, It);
2748 end loop Interp_Loop;
2749 end if;
2750
2751 -- At this stage Found indicates whether or not an acceptable
2752 -- interpretation exists. If not, then we have an error, except that if
2753 -- the context is Any_Type as a result of some other error, then we
2754 -- suppress the error report.
2755
2756 if not Found then
2757 if Typ /= Any_Type then
2758
2759 -- If type we are looking for is Void, then this is the procedure
2760 -- call case, and the error is simply that what we gave is not a
2761 -- procedure name (we think of procedure calls as expressions with
2762 -- types internally, but the user doesn't think of them this way).
2763
2764 if Typ = Standard_Void_Type then
2765
2766 -- Special case message if function used as a procedure
2767
2768 if Nkind (N) = N_Procedure_Call_Statement
2769 and then Is_Entity_Name (Name (N))
2770 and then Ekind (Entity (Name (N))) = E_Function
2771 then
2772 Error_Msg_NE
2773 ("cannot use call to function & as a statement",
2774 Name (N), Entity (Name (N)));
2775 Error_Msg_N
2776 ("\return value of a function call cannot be ignored",
2777 Name (N));
2778
2779 -- Otherwise give general message (not clear what cases this
2780 -- covers, but no harm in providing for them).
2781
2782 else
2783 Error_Msg_N ("expect procedure name in procedure call", N);
2784 end if;
2785
2786 Found := True;
2787
2788 -- Otherwise we do have a subexpression with the wrong type
2789
2790 -- Check for the case of an allocator which uses an access type
2791 -- instead of the designated type. This is a common error and we
2792 -- specialize the message, posting an error on the operand of the
2793 -- allocator, complaining that we expected the designated type of
2794 -- the allocator.
2795
2796 elsif Nkind (N) = N_Allocator
2797 and then Is_Access_Type (Typ)
2798 and then Is_Access_Type (Etype (N))
2799 and then Designated_Type (Etype (N)) = Typ
2800 then
2801 Wrong_Type (Expression (N), Designated_Type (Typ));
2802 Found := True;
2803
2804 -- Check for view mismatch on Null in instances, for which the
2805 -- view-swapping mechanism has no identifier.
2806
2807 elsif (In_Instance or else In_Inlined_Body)
2808 and then (Nkind (N) = N_Null)
2809 and then Is_Private_Type (Typ)
2810 and then Is_Access_Type (Full_View (Typ))
2811 then
2812 Resolve (N, Full_View (Typ));
2813 Set_Etype (N, Typ);
2814 return;
2815
2816 -- Check for an aggregate. Sometimes we can get bogus aggregates
2817 -- from misuse of parentheses, and we are about to complain about
2818 -- the aggregate without even looking inside it.
2819
2820 -- Instead, if we have an aggregate of type Any_Composite, then
2821 -- analyze and resolve the component fields, and then only issue
2822 -- another message if we get no errors doing this (otherwise
2823 -- assume that the errors in the aggregate caused the problem).
2824
2825 elsif Nkind (N) = N_Aggregate
2826 and then Etype (N) = Any_Composite
2827 then
2828 -- Disable expansion in any case. If there is a type mismatch
2829 -- it may be fatal to try to expand the aggregate. The flag
2830 -- would otherwise be set to false when the error is posted.
2831
2832 Expander_Active := False;
2833
2834 declare
2835 procedure Check_Aggr (Aggr : Node_Id);
2836 -- Check one aggregate, and set Found to True if we have a
2837 -- definite error in any of its elements
2838
2839 procedure Check_Elmt (Aelmt : Node_Id);
2840 -- Check one element of aggregate and set Found to True if
2841 -- we definitely have an error in the element.
2842
2843 ----------------
2844 -- Check_Aggr --
2845 ----------------
2846
2847 procedure Check_Aggr (Aggr : Node_Id) is
2848 Elmt : Node_Id;
2849
2850 begin
2851 if Present (Expressions (Aggr)) then
2852 Elmt := First (Expressions (Aggr));
2853 while Present (Elmt) loop
2854 Check_Elmt (Elmt);
2855 Next (Elmt);
2856 end loop;
2857 end if;
2858
2859 if Present (Component_Associations (Aggr)) then
2860 Elmt := First (Component_Associations (Aggr));
2861 while Present (Elmt) loop
2862
2863 -- If this is a default-initialized component, then
2864 -- there is nothing to check. The box will be
2865 -- replaced by the appropriate call during late
2866 -- expansion.
2867
2868 if Nkind (Elmt) /= N_Iterated_Component_Association
2869 and then not Box_Present (Elmt)
2870 then
2871 Check_Elmt (Expression (Elmt));
2872 end if;
2873
2874 Next (Elmt);
2875 end loop;
2876 end if;
2877 end Check_Aggr;
2878
2879 ----------------
2880 -- Check_Elmt --
2881 ----------------
2882
2883 procedure Check_Elmt (Aelmt : Node_Id) is
2884 begin
2885 -- If we have a nested aggregate, go inside it (to
2886 -- attempt a naked analyze-resolve of the aggregate can
2887 -- cause undesirable cascaded errors). Do not resolve
2888 -- expression if it needs a type from context, as for
2889 -- integer * fixed expression.
2890
2891 if Nkind (Aelmt) = N_Aggregate then
2892 Check_Aggr (Aelmt);
2893
2894 else
2895 Analyze (Aelmt);
2896
2897 if not Is_Overloaded (Aelmt)
2898 and then Etype (Aelmt) /= Any_Fixed
2899 then
2900 Resolve (Aelmt);
2901 end if;
2902
2903 if Etype (Aelmt) = Any_Type then
2904 Found := True;
2905 end if;
2906 end if;
2907 end Check_Elmt;
2908
2909 begin
2910 Check_Aggr (N);
2911 end;
2912 end if;
2913
2914 -- Looks like we have a type error, but check for special case
2915 -- of Address wanted, integer found, with the configuration pragma
2916 -- Allow_Integer_Address active. If we have this case, introduce
2917 -- an unchecked conversion to allow the integer expression to be
2918 -- treated as an Address. The reverse case of integer wanted,
2919 -- Address found, is treated in an analogous manner.
2920
2921 if Address_Integer_Convert_OK (Typ, Etype (N)) then
2922 Rewrite (N, Unchecked_Convert_To (Typ, Relocate_Node (N)));
2923 Analyze_And_Resolve (N, Typ);
2924 return;
2925
2926 -- Under relaxed RM semantics silently replace occurrences of null
2927 -- by System.Address_Null.
2928
2929 elsif Null_To_Null_Address_Convert_OK (N, Typ) then
2930 Replace_Null_By_Null_Address (N);
2931 Analyze_And_Resolve (N, Typ);
2932 return;
2933 end if;
2934
2935 -- That special Allow_Integer_Address check did not apply, so we
2936 -- have a real type error. If an error message was issued already,
2937 -- Found got reset to True, so if it's still False, issue standard
2938 -- Wrong_Type message.
2939
2940 if not Found then
2941 if Is_Overloaded (N) and then Nkind (N) = N_Function_Call then
2942 declare
2943 Subp_Name : Node_Id;
2944
2945 begin
2946 if Is_Entity_Name (Name (N)) then
2947 Subp_Name := Name (N);
2948
2949 elsif Nkind (Name (N)) = N_Selected_Component then
2950
2951 -- Protected operation: retrieve operation name
2952
2953 Subp_Name := Selector_Name (Name (N));
2954
2955 else
2956 raise Program_Error;
2957 end if;
2958
2959 Error_Msg_Node_2 := Typ;
2960 Error_Msg_NE
2961 ("no visible interpretation of& matches expected type&",
2962 N, Subp_Name);
2963 end;
2964
2965 if All_Errors_Mode then
2966 declare
2967 Index : Interp_Index;
2968 It : Interp;
2969
2970 begin
2971 Error_Msg_N ("\\possible interpretations:", N);
2972
2973 Get_First_Interp (Name (N), Index, It);
2974 while Present (It.Nam) loop
2975 Error_Msg_Sloc := Sloc (It.Nam);
2976 Error_Msg_Node_2 := It.Nam;
2977 Error_Msg_NE
2978 ("\\ type& for & declared#", N, It.Typ);
2979 Get_Next_Interp (Index, It);
2980 end loop;
2981 end;
2982
2983 else
2984 Error_Msg_N ("\use -gnatf for details", N);
2985 end if;
2986
2987 else
2988 Wrong_Type (N, Typ);
2989 end if;
2990 end if;
2991 end if;
2992
2993 Resolution_Failed;
2994 return;
2995
2996 -- Test if we have more than one interpretation for the context
2997
2998 elsif Ambiguous then
2999 Resolution_Failed;
3000 return;
3001
3002 -- Only one intepretation
3003
3004 else
3005 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
3006 -- the "+" on T is abstract, and the operands are of universal type,
3007 -- the above code will have (incorrectly) resolved the "+" to the
3008 -- universal one in Standard. Therefore check for this case and give
3009 -- an error. We can't do this earlier, because it would cause legal
3010 -- cases to get errors (when some other type has an abstract "+").
3011
3012 if Ada_Version >= Ada_2005
3013 and then Nkind (N) in N_Op
3014 and then Is_Overloaded (N)
3015 and then Is_Universal_Numeric_Type (Etype (Entity (N)))
3016 then
3017 Get_First_Interp (N, I, It);
3018 while Present (It.Typ) loop
3019 if Present (It.Abstract_Op) and then
3020 Etype (It.Abstract_Op) = Typ
3021 then
3022 Error_Msg_NE
3023 ("cannot call abstract subprogram &!", N, It.Abstract_Op);
3024 return;
3025 end if;
3026
3027 Get_Next_Interp (I, It);
3028 end loop;
3029 end if;
3030
3031 -- Here we have an acceptable interpretation for the context
3032
3033 -- Propagate type information and normalize tree for various
3034 -- predefined operations. If the context only imposes a class of
3035 -- types, rather than a specific type, propagate the actual type
3036 -- downward.
3037
3038 if Typ = Any_Integer or else
3039 Typ = Any_Boolean or else
3040 Typ = Any_Modular or else
3041 Typ = Any_Real or else
3042 Typ = Any_Discrete
3043 then
3044 Ctx_Type := Expr_Type;
3045
3046 -- Any_Fixed is legal in a real context only if a specific fixed-
3047 -- point type is imposed. If Norman Cohen can be confused by this,
3048 -- it deserves a separate message.
3049
3050 if Typ = Any_Real
3051 and then Expr_Type = Any_Fixed
3052 then
3053 Error_Msg_N ("illegal context for mixed mode operation", N);
3054 Set_Etype (N, Universal_Real);
3055 Ctx_Type := Universal_Real;
3056 end if;
3057 end if;
3058
3059 -- A user-defined operator is transformed into a function call at
3060 -- this point, so that further processing knows that operators are
3061 -- really operators (i.e. are predefined operators). User-defined
3062 -- operators that are intrinsic are just renamings of the predefined
3063 -- ones, and need not be turned into calls either, but if they rename
3064 -- a different operator, we must transform the node accordingly.
3065 -- Instantiations of Unchecked_Conversion are intrinsic but are
3066 -- treated as functions, even if given an operator designator.
3067
3068 if Nkind (N) in N_Op
3069 and then Present (Entity (N))
3070 and then Ekind (Entity (N)) /= E_Operator
3071 then
3072 if not Is_Predefined_Op (Entity (N)) then
3073 Rewrite_Operator_As_Call (N, Entity (N));
3074
3075 elsif Present (Alias (Entity (N)))
3076 and then
3077 Nkind (Parent (Parent (Entity (N)))) =
3078 N_Subprogram_Renaming_Declaration
3079 then
3080 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
3081
3082 -- If the node is rewritten, it will be fully resolved in
3083 -- Rewrite_Renamed_Operator.
3084
3085 if Analyzed (N) then
3086 return;
3087 end if;
3088 end if;
3089 end if;
3090
3091 case N_Subexpr'(Nkind (N)) is
3092 when N_Aggregate =>
3093 Resolve_Aggregate (N, Ctx_Type);
3094
3095 when N_Allocator =>
3096 Resolve_Allocator (N, Ctx_Type);
3097
3098 when N_Short_Circuit =>
3099 Resolve_Short_Circuit (N, Ctx_Type);
3100
3101 when N_Attribute_Reference =>
3102 Resolve_Attribute (N, Ctx_Type);
3103
3104 when N_Case_Expression =>
3105 Resolve_Case_Expression (N, Ctx_Type);
3106
3107 when N_Character_Literal =>
3108 Resolve_Character_Literal (N, Ctx_Type);
3109
3110 when N_Delta_Aggregate =>
3111 Resolve_Delta_Aggregate (N, Ctx_Type);
3112
3113 when N_Expanded_Name =>
3114 Resolve_Entity_Name (N, Ctx_Type);
3115
3116 when N_Explicit_Dereference =>
3117 Resolve_Explicit_Dereference (N, Ctx_Type);
3118
3119 when N_Expression_With_Actions =>
3120 Resolve_Expression_With_Actions (N, Ctx_Type);
3121
3122 when N_Extension_Aggregate =>
3123 Resolve_Extension_Aggregate (N, Ctx_Type);
3124
3125 when N_Function_Call =>
3126 Resolve_Call (N, Ctx_Type);
3127
3128 when N_Identifier =>
3129 Resolve_Entity_Name (N, Ctx_Type);
3130
3131 when N_If_Expression =>
3132 Resolve_If_Expression (N, Ctx_Type);
3133
3134 when N_Indexed_Component =>
3135 Resolve_Indexed_Component (N, Ctx_Type);
3136
3137 when N_Integer_Literal =>
3138 Resolve_Integer_Literal (N, Ctx_Type);
3139
3140 when N_Membership_Test =>
3141 Resolve_Membership_Op (N, Ctx_Type);
3142
3143 when N_Null =>
3144 Resolve_Null (N, Ctx_Type);
3145
3146 when N_Op_And
3147 | N_Op_Or
3148 | N_Op_Xor
3149 =>
3150 Resolve_Logical_Op (N, Ctx_Type);
3151
3152 when N_Op_Eq
3153 | N_Op_Ne
3154 =>
3155 Resolve_Equality_Op (N, Ctx_Type);
3156
3157 when N_Op_Ge
3158 | N_Op_Gt
3159 | N_Op_Le
3160 | N_Op_Lt
3161 =>
3162 Resolve_Comparison_Op (N, Ctx_Type);
3163
3164 when N_Op_Not =>
3165 Resolve_Op_Not (N, Ctx_Type);
3166
3167 when N_Op_Add
3168 | N_Op_Divide
3169 | N_Op_Mod
3170 | N_Op_Multiply
3171 | N_Op_Rem
3172 | N_Op_Subtract
3173 =>
3174 Resolve_Arithmetic_Op (N, Ctx_Type);
3175
3176 when N_Op_Concat =>
3177 Resolve_Op_Concat (N, Ctx_Type);
3178
3179 when N_Op_Expon =>
3180 Resolve_Op_Expon (N, Ctx_Type);
3181
3182 when N_Op_Abs
3183 | N_Op_Minus
3184 | N_Op_Plus
3185 =>
3186 Resolve_Unary_Op (N, Ctx_Type);
3187
3188 when N_Op_Shift =>
3189 Resolve_Shift (N, Ctx_Type);
3190
3191 when N_Procedure_Call_Statement =>
3192 Resolve_Call (N, Ctx_Type);
3193
3194 when N_Operator_Symbol =>
3195 Resolve_Operator_Symbol (N, Ctx_Type);
3196
3197 when N_Qualified_Expression =>
3198 Resolve_Qualified_Expression (N, Ctx_Type);
3199
3200 -- Why is the following null, needs a comment ???
3201
3202 when N_Quantified_Expression =>
3203 null;
3204
3205 when N_Raise_Expression =>
3206 Resolve_Raise_Expression (N, Ctx_Type);
3207
3208 when N_Raise_xxx_Error =>
3209 Set_Etype (N, Ctx_Type);
3210
3211 when N_Range =>
3212 Resolve_Range (N, Ctx_Type);
3213
3214 when N_Real_Literal =>
3215 Resolve_Real_Literal (N, Ctx_Type);
3216
3217 when N_Reference =>
3218 Resolve_Reference (N, Ctx_Type);
3219
3220 when N_Selected_Component =>
3221 Resolve_Selected_Component (N, Ctx_Type);
3222
3223 when N_Slice =>
3224 Resolve_Slice (N, Ctx_Type);
3225
3226 when N_String_Literal =>
3227 Resolve_String_Literal (N, Ctx_Type);
3228
3229 when N_Target_Name =>
3230 Resolve_Target_Name (N, Ctx_Type);
3231
3232 when N_Type_Conversion =>
3233 Resolve_Type_Conversion (N, Ctx_Type);
3234
3235 when N_Unchecked_Expression =>
3236 Resolve_Unchecked_Expression (N, Ctx_Type);
3237
3238 when N_Unchecked_Type_Conversion =>
3239 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
3240 end case;
3241
3242 -- Mark relevant use-type and use-package clauses as effective using
3243 -- the original node because constant folding may have occured and
3244 -- removed references that need to be examined.
3245
3246 if Nkind (Original_Node (N)) in N_Op then
3247 Mark_Use_Clauses (Original_Node (N));
3248 end if;
3249
3250 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
3251 -- expression of an anonymous access type that occurs in the context
3252 -- of a named general access type, except when the expression is that
3253 -- of a membership test. This ensures proper legality checking in
3254 -- terms of allowed conversions (expressions that would be illegal to
3255 -- convert implicitly are allowed in membership tests).
3256
3257 if Ada_Version >= Ada_2012
3258 and then Ekind (Base_Type (Ctx_Type)) = E_General_Access_Type
3259 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
3260 and then Nkind (Parent (N)) not in N_Membership_Test
3261 then
3262 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
3263 Analyze_And_Resolve (N, Ctx_Type);
3264 end if;
3265
3266 -- If the subexpression was replaced by a non-subexpression, then
3267 -- all we do is to expand it. The only legitimate case we know of
3268 -- is converting procedure call statement to entry call statements,
3269 -- but there may be others, so we are making this test general.
3270
3271 if Nkind (N) not in N_Subexpr then
3272 Debug_A_Exit ("resolving ", N, " (done)");
3273 Expand (N);
3274 return;
3275 end if;
3276
3277 -- The expression is definitely NOT overloaded at this point, so
3278 -- we reset the Is_Overloaded flag to avoid any confusion when
3279 -- reanalyzing the node.
3280
3281 Set_Is_Overloaded (N, False);
3282
3283 -- Freeze expression type, entity if it is a name, and designated
3284 -- type if it is an allocator (RM 13.14(10,11,13)).
3285
3286 -- Now that the resolution of the type of the node is complete, and
3287 -- we did not detect an error, we can expand this node. We skip the
3288 -- expand call if we are in a default expression, see section
3289 -- "Handling of Default Expressions" in Sem spec.
3290
3291 Debug_A_Exit ("resolving ", N, " (done)");
3292
3293 -- We unconditionally freeze the expression, even if we are in
3294 -- default expression mode (the Freeze_Expression routine tests this
3295 -- flag and only freezes static types if it is set).
3296
3297 -- Ada 2012 (AI05-177): The declaration of an expression function
3298 -- does not cause freezing, but we never reach here in that case.
3299 -- Here we are resolving the corresponding expanded body, so we do
3300 -- need to perform normal freezing.
3301
3302 -- As elsewhere we do not emit freeze node within a generic. We make
3303 -- an exception for entities that are expressions, only to detect
3304 -- misuses of deferred constants and preserve the output of various
3305 -- tests.
3306
3307 if not Inside_A_Generic or else Is_Entity_Name (N) then
3308 Freeze_Expression (N);
3309 end if;
3310
3311 -- Now we can do the expansion
3312
3313 Expand (N);
3314 end if;
3315 end Resolve;
3316
3317 -------------
3318 -- Resolve --
3319 -------------
3320
3321 -- Version with check(s) suppressed
3322
3323 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
3324 begin
3325 if Suppress = All_Checks then
3326 declare
3327 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
3328 begin
3329 Scope_Suppress.Suppress := (others => True);
3330 Resolve (N, Typ);
3331 Scope_Suppress.Suppress := Sva;
3332 end;
3333
3334 else
3335 declare
3336 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
3337 begin
3338 Scope_Suppress.Suppress (Suppress) := True;
3339 Resolve (N, Typ);
3340 Scope_Suppress.Suppress (Suppress) := Svg;
3341 end;
3342 end if;
3343 end Resolve;
3344
3345 -------------
3346 -- Resolve --
3347 -------------
3348
3349 -- Version with implicit type
3350
3351 procedure Resolve (N : Node_Id) is
3352 begin
3353 Resolve (N, Etype (N));
3354 end Resolve;
3355
3356 ---------------------
3357 -- Resolve_Actuals --
3358 ---------------------
3359
3360 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
3361 Loc : constant Source_Ptr := Sloc (N);
3362 A : Node_Id;
3363 A_Id : Entity_Id;
3364 A_Typ : Entity_Id := Empty; -- init to avoid warning
3365 F : Entity_Id;
3366 F_Typ : Entity_Id;
3367 Prev : Node_Id := Empty;
3368 Orig_A : Node_Id;
3369 Real_F : Entity_Id := Empty; -- init to avoid warning
3370
3371 Real_Subp : Entity_Id;
3372 -- If the subprogram being called is an inherited operation for
3373 -- a formal derived type in an instance, Real_Subp is the subprogram
3374 -- that will be called. It may have different formal names than the
3375 -- operation of the formal in the generic, so after actual is resolved
3376 -- the name of the actual in a named association must carry the name
3377 -- of the actual of the subprogram being called.
3378
3379 procedure Check_Aliased_Parameter;
3380 -- Check rules on aliased parameters and related accessibility rules
3381 -- in (RM 3.10.2 (10.2-10.4)).
3382
3383 procedure Check_Argument_Order;
3384 -- Performs a check for the case where the actuals are all simple
3385 -- identifiers that correspond to the formal names, but in the wrong
3386 -- order, which is considered suspicious and cause for a warning.
3387
3388 procedure Check_Prefixed_Call;
3389 -- If the original node is an overloaded call in prefix notation,
3390 -- insert an 'Access or a dereference as needed over the first actual.
3391 -- Try_Object_Operation has already verified that there is a valid
3392 -- interpretation, but the form of the actual can only be determined
3393 -- once the primitive operation is identified.
3394
3395 procedure Flag_Effectively_Volatile_Objects (Expr : Node_Id);
3396 -- Emit an error concerning the illegal usage of an effectively volatile
3397 -- object in interfering context (SPARK RM 7.13(12)).
3398
3399 procedure Insert_Default;
3400 -- If the actual is missing in a call, insert in the actuals list
3401 -- an instance of the default expression. The insertion is always
3402 -- a named association.
3403
3404 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
3405 -- Check whether T1 and T2, or their full views, are derived from a
3406 -- common type. Used to enforce the restrictions on array conversions
3407 -- of AI95-00246.
3408
3409 function Static_Concatenation (N : Node_Id) return Boolean;
3410 -- Predicate to determine whether an actual that is a concatenation
3411 -- will be evaluated statically and does not need a transient scope.
3412 -- This must be determined before the actual is resolved and expanded
3413 -- because if needed the transient scope must be introduced earlier.
3414
3415 -----------------------------
3416 -- Check_Aliased_Parameter --
3417 -----------------------------
3418
3419 procedure Check_Aliased_Parameter is
3420 Nominal_Subt : Entity_Id;
3421
3422 begin
3423 if Is_Aliased (F) then
3424 if Is_Tagged_Type (A_Typ) then
3425 null;
3426
3427 elsif Is_Aliased_View (A) then
3428 if Is_Constr_Subt_For_U_Nominal (A_Typ) then
3429 Nominal_Subt := Base_Type (A_Typ);
3430 else
3431 Nominal_Subt := A_Typ;
3432 end if;
3433
3434 if Subtypes_Statically_Match (F_Typ, Nominal_Subt) then
3435 null;
3436
3437 -- In a generic body assume the worst for generic formals:
3438 -- they can have a constrained partial view (AI05-041).
3439
3440 elsif Has_Discriminants (F_Typ)
3441 and then not Is_Constrained (F_Typ)
3442 and then not Has_Constrained_Partial_View (F_Typ)
3443 and then not Is_Generic_Type (F_Typ)
3444 then
3445 null;
3446
3447 else
3448 Error_Msg_NE ("untagged actual does not match "
3449 & "aliased formal&", A, F);
3450 end if;
3451
3452 else
3453 Error_Msg_NE ("actual for aliased formal& must be "
3454 & "aliased object", A, F);
3455 end if;
3456
3457 if Ekind (Nam) = E_Procedure then
3458 null;
3459
3460 elsif Ekind (Etype (Nam)) = E_Anonymous_Access_Type then
3461 if Nkind (Parent (N)) = N_Type_Conversion
3462 and then Type_Access_Level (Etype (Parent (N))) <
3463 Object_Access_Level (A)
3464 then
3465 Error_Msg_N ("aliased actual has wrong accessibility", A);
3466 end if;
3467
3468 elsif Nkind (Parent (N)) = N_Qualified_Expression
3469 and then Nkind (Parent (Parent (N))) = N_Allocator
3470 and then Type_Access_Level (Etype (Parent (Parent (N)))) <
3471 Object_Access_Level (A)
3472 then
3473 Error_Msg_N
3474 ("aliased actual in allocator has wrong accessibility", A);
3475 end if;
3476 end if;
3477 end Check_Aliased_Parameter;
3478
3479 --------------------------
3480 -- Check_Argument_Order --
3481 --------------------------
3482
3483 procedure Check_Argument_Order is
3484 begin
3485 -- Nothing to do if no parameters, or original node is neither a
3486 -- function call nor a procedure call statement (happens in the
3487 -- operator-transformed-to-function call case), or the call is to an
3488 -- operator symbol (which is usually in infix form), or the call does
3489 -- not come from source, or this warning is off.
3490
3491 if not Warn_On_Parameter_Order
3492 or else No (Parameter_Associations (N))
3493 or else Nkind (Original_Node (N)) not in N_Subprogram_Call
3494 or else (Nkind (Name (N)) = N_Identifier
3495 and then Present (Entity (Name (N)))
3496 and then Nkind (Entity (Name (N))) =
3497 N_Defining_Operator_Symbol)
3498 or else not Comes_From_Source (N)
3499 then
3500 return;
3501 end if;
3502
3503 declare
3504 Nargs : constant Nat := List_Length (Parameter_Associations (N));
3505
3506 begin
3507 -- Nothing to do if only one parameter
3508
3509 if Nargs < 2 then
3510 return;
3511 end if;
3512
3513 -- Here if at least two arguments
3514
3515 declare
3516 Actuals : array (1 .. Nargs) of Node_Id;
3517 Actual : Node_Id;
3518 Formal : Node_Id;
3519
3520 Wrong_Order : Boolean := False;
3521 -- Set True if an out of order case is found
3522
3523 begin
3524 -- Collect identifier names of actuals, fail if any actual is
3525 -- not a simple identifier, and record max length of name.
3526
3527 Actual := First (Parameter_Associations (N));
3528 for J in Actuals'Range loop
3529 if Nkind (Actual) /= N_Identifier then
3530 return;
3531 else
3532 Actuals (J) := Actual;
3533 Next (Actual);
3534 end if;
3535 end loop;
3536
3537 -- If we got this far, all actuals are identifiers and the list
3538 -- of their names is stored in the Actuals array.
3539
3540 Formal := First_Formal (Nam);
3541 for J in Actuals'Range loop
3542
3543 -- If we ran out of formals, that's odd, probably an error
3544 -- which will be detected elsewhere, but abandon the search.
3545
3546 if No (Formal) then
3547 return;
3548 end if;
3549
3550 -- If name matches and is in order OK
3551
3552 if Chars (Formal) = Chars (Actuals (J)) then
3553 null;
3554
3555 else
3556 -- If no match, see if it is elsewhere in list and if so
3557 -- flag potential wrong order if type is compatible.
3558
3559 for K in Actuals'Range loop
3560 if Chars (Formal) = Chars (Actuals (K))
3561 and then
3562 Has_Compatible_Type (Actuals (K), Etype (Formal))
3563 then
3564 Wrong_Order := True;
3565 goto Continue;
3566 end if;
3567 end loop;
3568
3569 -- No match
3570
3571 return;
3572 end if;
3573
3574 <<Continue>> Next_Formal (Formal);
3575 end loop;
3576
3577 -- If Formals left over, also probably an error, skip warning
3578
3579 if Present (Formal) then
3580 return;
3581 end if;
3582
3583 -- Here we give the warning if something was out of order
3584
3585 if Wrong_Order then
3586 Error_Msg_N
3587 ("?P?actuals for this call may be in wrong order", N);
3588 end if;
3589 end;
3590 end;
3591 end Check_Argument_Order;
3592
3593 -------------------------
3594 -- Check_Prefixed_Call --
3595 -------------------------
3596
3597 procedure Check_Prefixed_Call is
3598 Act : constant Node_Id := First_Actual (N);
3599 A_Type : constant Entity_Id := Etype (Act);
3600 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3601 Orig : constant Node_Id := Original_Node (N);
3602 New_A : Node_Id;
3603
3604 begin
3605 -- Check whether the call is a prefixed call, with or without
3606 -- additional actuals.
3607
3608 if Nkind (Orig) = N_Selected_Component
3609 or else
3610 (Nkind (Orig) = N_Indexed_Component
3611 and then Nkind (Prefix (Orig)) = N_Selected_Component
3612 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3613 and then Is_Entity_Name (Act)
3614 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3615 then
3616 if Is_Access_Type (A_Type)
3617 and then not Is_Access_Type (F_Type)
3618 then
3619 -- Introduce dereference on object in prefix
3620
3621 New_A :=
3622 Make_Explicit_Dereference (Sloc (Act),
3623 Prefix => Relocate_Node (Act));
3624 Rewrite (Act, New_A);
3625 Analyze (Act);
3626
3627 elsif Is_Access_Type (F_Type)
3628 and then not Is_Access_Type (A_Type)
3629 then
3630 -- Introduce an implicit 'Access in prefix
3631
3632 if not Is_Aliased_View (Act) then
3633 Error_Msg_NE
3634 ("object in prefixed call to& must be aliased "
3635 & "(RM 4.1.3 (13 1/2))",
3636 Prefix (Act), Nam);
3637 end if;
3638
3639 Rewrite (Act,
3640 Make_Attribute_Reference (Loc,
3641 Attribute_Name => Name_Access,
3642 Prefix => Relocate_Node (Act)));
3643 end if;
3644
3645 Analyze (Act);
3646 end if;
3647 end Check_Prefixed_Call;
3648
3649 ---------------------------------------
3650 -- Flag_Effectively_Volatile_Objects --
3651 ---------------------------------------
3652
3653 procedure Flag_Effectively_Volatile_Objects (Expr : Node_Id) is
3654 function Flag_Object (N : Node_Id) return Traverse_Result;
3655 -- Determine whether arbitrary node N denotes an effectively volatile
3656 -- object and if it does, emit an error.
3657
3658 -----------------
3659 -- Flag_Object --
3660 -----------------
3661
3662 function Flag_Object (N : Node_Id) return Traverse_Result is
3663 Id : Entity_Id;
3664
3665 begin
3666 -- Do not consider nested function calls because they have already
3667 -- been processed during their own resolution.
3668
3669 if Nkind (N) = N_Function_Call then
3670 return Skip;
3671
3672 elsif Is_Entity_Name (N) and then Present (Entity (N)) then
3673 Id := Entity (N);
3674
3675 if Is_Object (Id)
3676 and then Is_Effectively_Volatile (Id)
3677 and then (Async_Writers_Enabled (Id)
3678 or else Effective_Reads_Enabled (Id))
3679 then
3680 Error_Msg_N
3681 ("volatile object cannot appear in this context (SPARK "
3682 & "RM 7.1.3(11))", N);
3683 return Skip;
3684 end if;
3685 end if;
3686
3687 return OK;
3688 end Flag_Object;
3689
3690 procedure Flag_Objects is new Traverse_Proc (Flag_Object);
3691
3692 -- Start of processing for Flag_Effectively_Volatile_Objects
3693
3694 begin
3695 Flag_Objects (Expr);
3696 end Flag_Effectively_Volatile_Objects;
3697
3698 --------------------
3699 -- Insert_Default --
3700 --------------------
3701
3702 procedure Insert_Default is
3703 Actval : Node_Id;
3704 Assoc : Node_Id;
3705
3706 begin
3707 -- Missing argument in call, nothing to insert
3708
3709 if No (Default_Value (F)) then
3710 return;
3711
3712 else
3713 -- Note that we do a full New_Copy_Tree, so that any associated
3714 -- Itypes are properly copied. This may not be needed any more,
3715 -- but it does no harm as a safety measure. Defaults of a generic
3716 -- formal may be out of bounds of the corresponding actual (see
3717 -- cc1311b) and an additional check may be required.
3718
3719 Actval :=
3720 New_Copy_Tree
3721 (Default_Value (F),
3722 New_Scope => Current_Scope,
3723 New_Sloc => Loc);
3724
3725 -- Propagate dimension information, if any.
3726
3727 Copy_Dimensions (Default_Value (F), Actval);
3728
3729 if Is_Concurrent_Type (Scope (Nam))
3730 and then Has_Discriminants (Scope (Nam))
3731 then
3732 Replace_Actual_Discriminants (N, Actval);
3733 end if;
3734
3735 if Is_Overloadable (Nam)
3736 and then Present (Alias (Nam))
3737 then
3738 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3739 and then not Is_Tagged_Type (Etype (F))
3740 then
3741 -- If default is a real literal, do not introduce a
3742 -- conversion whose effect may depend on the run-time
3743 -- size of universal real.
3744
3745 if Nkind (Actval) = N_Real_Literal then
3746 Set_Etype (Actval, Base_Type (Etype (F)));
3747 else
3748 Actval := Unchecked_Convert_To (Etype (F), Actval);
3749 end if;
3750 end if;
3751
3752 if Is_Scalar_Type (Etype (F)) then
3753 Enable_Range_Check (Actval);
3754 end if;
3755
3756 Set_Parent (Actval, N);
3757
3758 -- Resolve aggregates with their base type, to avoid scope
3759 -- anomalies: the subtype was first built in the subprogram
3760 -- declaration, and the current call may be nested.
3761
3762 if Nkind (Actval) = N_Aggregate then
3763 Analyze_And_Resolve (Actval, Etype (F));
3764 else
3765 Analyze_And_Resolve (Actval, Etype (Actval));
3766 end if;
3767
3768 else
3769 Set_Parent (Actval, N);
3770
3771 -- See note above concerning aggregates
3772
3773 if Nkind (Actval) = N_Aggregate
3774 and then Has_Discriminants (Etype (Actval))
3775 then
3776 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3777
3778 -- Resolve entities with their own type, which may differ from
3779 -- the type of a reference in a generic context (the view
3780 -- swapping mechanism did not anticipate the re-analysis of
3781 -- default values in calls).
3782
3783 elsif Is_Entity_Name (Actval) then
3784 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3785
3786 else
3787 Analyze_And_Resolve (Actval, Etype (Actval));
3788 end if;
3789 end if;
3790
3791 -- If default is a tag indeterminate function call, propagate tag
3792 -- to obtain proper dispatching.
3793
3794 if Is_Controlling_Formal (F)
3795 and then Nkind (Default_Value (F)) = N_Function_Call
3796 then
3797 Set_Is_Controlling_Actual (Actval);
3798 end if;
3799 end if;
3800
3801 -- If the default expression raises constraint error, then just
3802 -- silently replace it with an N_Raise_Constraint_Error node, since
3803 -- we already gave the warning on the subprogram spec. If node is
3804 -- already a Raise_Constraint_Error leave as is, to prevent loops in
3805 -- the warnings removal machinery.
3806
3807 if Raises_Constraint_Error (Actval)
3808 and then Nkind (Actval) /= N_Raise_Constraint_Error
3809 then
3810 Rewrite (Actval,
3811 Make_Raise_Constraint_Error (Loc,
3812 Reason => CE_Range_Check_Failed));
3813
3814 Set_Raises_Constraint_Error (Actval);
3815 Set_Etype (Actval, Etype (F));
3816 end if;
3817
3818 Assoc :=
3819 Make_Parameter_Association (Loc,
3820 Explicit_Actual_Parameter => Actval,
3821 Selector_Name => Make_Identifier (Loc, Chars (F)));
3822
3823 -- Case of insertion is first named actual
3824
3825 if No (Prev)
3826 or else Nkind (Parent (Prev)) /= N_Parameter_Association
3827 then
3828 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3829 Set_First_Named_Actual (N, Actval);
3830
3831 if No (Prev) then
3832 if No (Parameter_Associations (N)) then
3833 Set_Parameter_Associations (N, New_List (Assoc));
3834 else
3835 Append (Assoc, Parameter_Associations (N));
3836 end if;
3837
3838 else
3839 Insert_After (Prev, Assoc);
3840 end if;
3841
3842 -- Case of insertion is not first named actual
3843
3844 else
3845 Set_Next_Named_Actual
3846 (Assoc, Next_Named_Actual (Parent (Prev)));
3847 Set_Next_Named_Actual (Parent (Prev), Actval);
3848 Append (Assoc, Parameter_Associations (N));
3849 end if;
3850
3851 Mark_Rewrite_Insertion (Assoc);
3852 Mark_Rewrite_Insertion (Actval);
3853
3854 Prev := Actval;
3855 end Insert_Default;
3856
3857 -------------------
3858 -- Same_Ancestor --
3859 -------------------
3860
3861 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3862 FT1 : Entity_Id := T1;
3863 FT2 : Entity_Id := T2;
3864
3865 begin
3866 if Is_Private_Type (T1)
3867 and then Present (Full_View (T1))
3868 then
3869 FT1 := Full_View (T1);
3870 end if;
3871
3872 if Is_Private_Type (T2)
3873 and then Present (Full_View (T2))
3874 then
3875 FT2 := Full_View (T2);
3876 end if;
3877
3878 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3879 end Same_Ancestor;
3880
3881 --------------------------
3882 -- Static_Concatenation --
3883 --------------------------
3884
3885 function Static_Concatenation (N : Node_Id) return Boolean is
3886 begin
3887 case Nkind (N) is
3888 when N_String_Literal =>
3889 return True;
3890
3891 when N_Op_Concat =>
3892
3893 -- Concatenation is static when both operands are static and
3894 -- the concatenation operator is a predefined one.
3895
3896 return Scope (Entity (N)) = Standard_Standard
3897 and then
3898 Static_Concatenation (Left_Opnd (N))
3899 and then
3900 Static_Concatenation (Right_Opnd (N));
3901
3902 when others =>
3903 if Is_Entity_Name (N) then
3904 declare
3905 Ent : constant Entity_Id := Entity (N);
3906 begin
3907 return Ekind (Ent) = E_Constant
3908 and then Present (Constant_Value (Ent))
3909 and then
3910 Is_OK_Static_Expression (Constant_Value (Ent));
3911 end;
3912
3913 else
3914 return False;
3915 end if;
3916 end case;
3917 end Static_Concatenation;
3918
3919 -- Start of processing for Resolve_Actuals
3920
3921 begin
3922 Check_Argument_Order;
3923
3924 if Is_Overloadable (Nam)
3925 and then Is_Inherited_Operation (Nam)
3926 and then In_Instance
3927 and then Present (Alias (Nam))
3928 and then Present (Overridden_Operation (Alias (Nam)))
3929 then
3930 Real_Subp := Alias (Nam);
3931 else
3932 Real_Subp := Empty;
3933 end if;
3934
3935 if Present (First_Actual (N)) then
3936 Check_Prefixed_Call;
3937 end if;
3938
3939 A := First_Actual (N);
3940 F := First_Formal (Nam);
3941
3942 if Present (Real_Subp) then
3943 Real_F := First_Formal (Real_Subp);
3944 end if;
3945
3946 while Present (F) loop
3947 if No (A) and then Needs_No_Actuals (Nam) then
3948 null;
3949
3950 -- If we have an error in any actual or formal, indicated by a type
3951 -- of Any_Type, then abandon resolution attempt, and set result type
3952 -- to Any_Type. Skip this if the actual is a Raise_Expression, whose
3953 -- type is imposed from context.
3954
3955 elsif (Present (A) and then Etype (A) = Any_Type)
3956 or else Etype (F) = Any_Type
3957 then
3958 if Nkind (A) /= N_Raise_Expression then
3959 Set_Etype (N, Any_Type);
3960 return;
3961 end if;
3962 end if;
3963
3964 -- Case where actual is present
3965
3966 -- If the actual is an entity, generate a reference to it now. We
3967 -- do this before the actual is resolved, because a formal of some
3968 -- protected subprogram, or a task discriminant, will be rewritten
3969 -- during expansion, and the source entity reference may be lost.
3970
3971 if Present (A)
3972 and then Is_Entity_Name (A)
3973 and then Comes_From_Source (A)
3974 then
3975 -- Annotate the tree by creating a variable reference marker when
3976 -- the actual denotes a variable reference, in case the reference
3977 -- is folded or optimized away. The variable reference marker is
3978 -- automatically saved for later examination by the ABE Processing
3979 -- phase. The status of the reference is set as follows:
3980
3981 -- status mode
3982 -- read IN, IN OUT
3983 -- write IN OUT, OUT
3984
3985 if Needs_Variable_Reference_Marker
3986 (N => A,
3987 Calls_OK => True)
3988 then
3989 Build_Variable_Reference_Marker
3990 (N => A,
3991 Read => Ekind (F) /= E_Out_Parameter,
3992 Write => Ekind (F) /= E_In_Parameter);
3993 end if;
3994
3995 Orig_A := Entity (A);
3996
3997 if Present (Orig_A) then
3998 if Is_Formal (Orig_A)
3999 and then Ekind (F) /= E_In_Parameter
4000 then
4001 Generate_Reference (Orig_A, A, 'm');
4002
4003 elsif not Is_Overloaded (A) then
4004 if Ekind (F) /= E_Out_Parameter then
4005 Generate_Reference (Orig_A, A);
4006
4007 -- RM 6.4.1(12): For an out parameter that is passed by
4008 -- copy, the formal parameter object is created, and:
4009
4010 -- * For an access type, the formal parameter is initialized
4011 -- from the value of the actual, without checking that the
4012 -- value satisfies any constraint, any predicate, or any
4013 -- exclusion of the null value.
4014
4015 -- * For a scalar type that has the Default_Value aspect
4016 -- specified, the formal parameter is initialized from the
4017 -- value of the actual, without checking that the value
4018 -- satisfies any constraint or any predicate.
4019 -- I do not understand why this case is included??? this is
4020 -- not a case where an OUT parameter is treated as IN OUT.
4021
4022 -- * For a composite type with discriminants or that has
4023 -- implicit initial values for any subcomponents, the
4024 -- behavior is as for an in out parameter passed by copy.
4025
4026 -- Hence for these cases we generate the read reference now
4027 -- (the write reference will be generated later by
4028 -- Note_Possible_Modification).
4029
4030 elsif Is_By_Copy_Type (Etype (F))
4031 and then
4032 (Is_Access_Type (Etype (F))
4033 or else
4034 (Is_Scalar_Type (Etype (F))
4035 and then
4036 Present (Default_Aspect_Value (Etype (F))))
4037 or else
4038 (Is_Composite_Type (Etype (F))
4039 and then (Has_Discriminants (Etype (F))
4040 or else Is_Partially_Initialized_Type
4041 (Etype (F)))))
4042 then
4043 Generate_Reference (Orig_A, A);
4044 end if;
4045 end if;
4046 end if;
4047 end if;
4048
4049 if Present (A)
4050 and then (Nkind (Parent (A)) /= N_Parameter_Association
4051 or else Chars (Selector_Name (Parent (A))) = Chars (F))
4052 then
4053 -- If style checking mode on, check match of formal name
4054
4055 if Style_Check then
4056 if Nkind (Parent (A)) = N_Parameter_Association then
4057 Check_Identifier (Selector_Name (Parent (A)), F);
4058 end if;
4059 end if;
4060
4061 -- If the formal is Out or In_Out, do not resolve and expand the
4062 -- conversion, because it is subsequently expanded into explicit
4063 -- temporaries and assignments. However, the object of the
4064 -- conversion can be resolved. An exception is the case of tagged
4065 -- type conversion with a class-wide actual. In that case we want
4066 -- the tag check to occur and no temporary will be needed (no
4067 -- representation change can occur) and the parameter is passed by
4068 -- reference, so we go ahead and resolve the type conversion.
4069 -- Another exception is the case of reference to component or
4070 -- subcomponent of a bit-packed array, in which case we want to
4071 -- defer expansion to the point the in and out assignments are
4072 -- performed.
4073
4074 if Ekind (F) /= E_In_Parameter
4075 and then Nkind (A) = N_Type_Conversion
4076 and then not Is_Class_Wide_Type (Etype (Expression (A)))
4077 and then not Is_Interface (Etype (A))
4078 then
4079 if Ekind (F) = E_In_Out_Parameter
4080 and then Is_Array_Type (Etype (F))
4081 then
4082 -- In a view conversion, the conversion must be legal in
4083 -- both directions, and thus both component types must be
4084 -- aliased, or neither (4.6 (8)).
4085
4086 -- The extra rule in 4.6 (24.9.2) seems unduly restrictive:
4087 -- the privacy requirement should not apply to generic
4088 -- types, and should be checked in an instance. ARG query
4089 -- is in order ???
4090
4091 if Has_Aliased_Components (Etype (Expression (A))) /=
4092 Has_Aliased_Components (Etype (F))
4093 then
4094 Error_Msg_N
4095 ("both component types in a view conversion must be"
4096 & " aliased, or neither", A);
4097
4098 -- Comment here??? what set of cases???
4099
4100 elsif
4101 not Same_Ancestor (Etype (F), Etype (Expression (A)))
4102 then
4103 -- Check view conv between unrelated by ref array types
4104
4105 if Is_By_Reference_Type (Etype (F))
4106 or else Is_By_Reference_Type (Etype (Expression (A)))
4107 then
4108 Error_Msg_N
4109 ("view conversion between unrelated by reference "
4110 & "array types not allowed (\'A'I-00246)", A);
4111
4112 -- In Ada 2005 mode, check view conversion component
4113 -- type cannot be private, tagged, or volatile. Note
4114 -- that we only apply this to source conversions. The
4115 -- generated code can contain conversions which are
4116 -- not subject to this test, and we cannot extract the
4117 -- component type in such cases since it is not present.
4118
4119 elsif Comes_From_Source (A)
4120 and then Ada_Version >= Ada_2005
4121 then
4122 declare
4123 Comp_Type : constant Entity_Id :=
4124 Component_Type
4125 (Etype (Expression (A)));
4126 begin
4127 if (Is_Private_Type (Comp_Type)
4128 and then not Is_Generic_Type (Comp_Type))
4129 or else Is_Tagged_Type (Comp_Type)
4130 or else Is_Volatile (Comp_Type)
4131 then
4132 Error_Msg_N
4133 ("component type of a view conversion cannot"
4134 & " be private, tagged, or volatile"
4135 & " (RM 4.6 (24))",
4136 Expression (A));
4137 end if;
4138 end;
4139 end if;
4140 end if;
4141 end if;
4142
4143 -- Resolve expression if conversion is all OK
4144
4145 if (Conversion_OK (A)
4146 or else Valid_Conversion (A, Etype (A), Expression (A)))
4147 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
4148 then
4149 Resolve (Expression (A));
4150 end if;
4151
4152 -- If the actual is a function call that returns a limited
4153 -- unconstrained object that needs finalization, create a
4154 -- transient scope for it, so that it can receive the proper
4155 -- finalization list.
4156
4157 elsif Expander_Active
4158 and then Nkind (A) = N_Function_Call
4159 and then Is_Limited_Record (Etype (F))
4160 and then not Is_Constrained (Etype (F))
4161 and then (Needs_Finalization (Etype (F))
4162 or else Has_Task (Etype (F)))
4163 then
4164 Establish_Transient_Scope (A, Manage_Sec_Stack => False);
4165 Resolve (A, Etype (F));
4166
4167 -- A small optimization: if one of the actuals is a concatenation
4168 -- create a block around a procedure call to recover stack space.
4169 -- This alleviates stack usage when several procedure calls in
4170 -- the same statement list use concatenation. We do not perform
4171 -- this wrapping for code statements, where the argument is a
4172 -- static string, and we want to preserve warnings involving
4173 -- sequences of such statements.
4174
4175 elsif Expander_Active
4176 and then Nkind (A) = N_Op_Concat
4177 and then Nkind (N) = N_Procedure_Call_Statement
4178 and then not (Is_Intrinsic_Subprogram (Nam)
4179 and then Chars (Nam) = Name_Asm)
4180 and then not Static_Concatenation (A)
4181 then
4182 Establish_Transient_Scope (A, Manage_Sec_Stack => False);
4183 Resolve (A, Etype (F));
4184
4185 else
4186 if Nkind (A) = N_Type_Conversion
4187 and then Is_Array_Type (Etype (F))
4188 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
4189 and then
4190 (Is_Limited_Type (Etype (F))
4191 or else Is_Limited_Type (Etype (Expression (A))))
4192 then
4193 Error_Msg_N
4194 ("conversion between unrelated limited array types not "
4195 & "allowed ('A'I-00246)", A);
4196
4197 if Is_Limited_Type (Etype (F)) then
4198 Explain_Limited_Type (Etype (F), A);
4199 end if;
4200
4201 if Is_Limited_Type (Etype (Expression (A))) then
4202 Explain_Limited_Type (Etype (Expression (A)), A);
4203 end if;
4204 end if;
4205
4206 -- (Ada 2005: AI-251): If the actual is an allocator whose
4207 -- directly designated type is a class-wide interface, we build
4208 -- an anonymous access type to use it as the type of the
4209 -- allocator. Later, when the subprogram call is expanded, if
4210 -- the interface has a secondary dispatch table the expander
4211 -- will add a type conversion to force the correct displacement
4212 -- of the pointer.
4213
4214 if Nkind (A) = N_Allocator then
4215 declare
4216 DDT : constant Entity_Id :=
4217 Directly_Designated_Type (Base_Type (Etype (F)));
4218
4219 begin
4220 -- Displace the pointer to the object to reference its
4221 -- secondary dispatch table.
4222
4223 if Is_Class_Wide_Type (DDT)
4224 and then Is_Interface (DDT)
4225 then
4226 Rewrite (A, Convert_To (Etype (F), Relocate_Node (A)));
4227 Analyze_And_Resolve (A, Etype (F),
4228 Suppress => Access_Check);
4229 end if;
4230
4231 -- Ada 2005, AI-162:If the actual is an allocator, the
4232 -- innermost enclosing statement is the master of the
4233 -- created object. This needs to be done with expansion
4234 -- enabled only, otherwise the transient scope will not
4235 -- be removed in the expansion of the wrapped construct.
4236
4237 if Expander_Active
4238 and then (Needs_Finalization (DDT)
4239 or else Has_Task (DDT))
4240 then
4241 Establish_Transient_Scope
4242 (A, Manage_Sec_Stack => False);
4243 end if;
4244 end;
4245
4246 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4247 Check_Restriction (No_Access_Parameter_Allocators, A);
4248 end if;
4249 end if;
4250
4251 -- (Ada 2005): The call may be to a primitive operation of a
4252 -- tagged synchronized type, declared outside of the type. In
4253 -- this case the controlling actual must be converted to its
4254 -- corresponding record type, which is the formal type. The
4255 -- actual may be a subtype, either because of a constraint or
4256 -- because it is a generic actual, so use base type to locate
4257 -- concurrent type.
4258
4259 F_Typ := Base_Type (Etype (F));
4260
4261 if Is_Tagged_Type (F_Typ)
4262 and then (Is_Concurrent_Type (F_Typ)
4263 or else Is_Concurrent_Record_Type (F_Typ))
4264 then
4265 -- If the actual is overloaded, look for an interpretation
4266 -- that has a synchronized type.
4267
4268 if not Is_Overloaded (A) then
4269 A_Typ := Base_Type (Etype (A));
4270
4271 else
4272 declare
4273 Index : Interp_Index;
4274 It : Interp;
4275
4276 begin
4277 Get_First_Interp (A, Index, It);
4278 while Present (It.Typ) loop
4279 if Is_Concurrent_Type (It.Typ)
4280 or else Is_Concurrent_Record_Type (It.Typ)
4281 then
4282 A_Typ := Base_Type (It.Typ);
4283 exit;
4284 end if;
4285
4286 Get_Next_Interp (Index, It);
4287 end loop;
4288 end;
4289 end if;
4290
4291 declare
4292 Full_A_Typ : Entity_Id;
4293
4294 begin
4295 if Present (Full_View (A_Typ)) then
4296 Full_A_Typ := Base_Type (Full_View (A_Typ));
4297 else
4298 Full_A_Typ := A_Typ;
4299 end if;
4300
4301 -- Tagged synchronized type (case 1): the actual is a
4302 -- concurrent type.
4303
4304 if Is_Concurrent_Type (A_Typ)
4305 and then Corresponding_Record_Type (A_Typ) = F_Typ
4306 then
4307 Rewrite (A,
4308 Unchecked_Convert_To
4309 (Corresponding_Record_Type (A_Typ), A));
4310 Resolve (A, Etype (F));
4311
4312 -- Tagged synchronized type (case 2): the formal is a
4313 -- concurrent type.
4314
4315 elsif Ekind (Full_A_Typ) = E_Record_Type
4316 and then Present
4317 (Corresponding_Concurrent_Type (Full_A_Typ))
4318 and then Is_Concurrent_Type (F_Typ)
4319 and then Present (Corresponding_Record_Type (F_Typ))
4320 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
4321 then
4322 Resolve (A, Corresponding_Record_Type (F_Typ));
4323
4324 -- Common case
4325
4326 else
4327 Resolve (A, Etype (F));
4328 end if;
4329 end;
4330
4331 -- Not a synchronized operation
4332
4333 else
4334 Resolve (A, Etype (F));
4335 end if;
4336 end if;
4337
4338 A_Typ := Etype (A);
4339 F_Typ := Etype (F);
4340
4341 -- An actual cannot be an untagged formal incomplete type
4342
4343 if Ekind (A_Typ) = E_Incomplete_Type
4344 and then not Is_Tagged_Type (A_Typ)
4345 and then Is_Generic_Type (A_Typ)
4346 then
4347 Error_Msg_N
4348 ("invalid use of untagged formal incomplete type", A);
4349 end if;
4350
4351 if Comes_From_Source (Original_Node (N))
4352 and then Nkind_In (Original_Node (N), N_Function_Call,
4353 N_Procedure_Call_Statement)
4354 then
4355 -- In formal mode, check that actual parameters matching
4356 -- formals of tagged types are objects (or ancestor type
4357 -- conversions of objects), not general expressions.
4358
4359 if Is_Actual_Tagged_Parameter (A) then
4360 if Is_SPARK_05_Object_Reference (A) then
4361 null;
4362
4363 elsif Nkind (A) = N_Type_Conversion then
4364 declare
4365 Operand : constant Node_Id := Expression (A);
4366 Operand_Typ : constant Entity_Id := Etype (Operand);
4367 Target_Typ : constant Entity_Id := A_Typ;
4368
4369 begin
4370 if not Is_SPARK_05_Object_Reference (Operand) then
4371 Check_SPARK_05_Restriction
4372 ("object required", Operand);
4373
4374 -- In formal mode, the only view conversions are those
4375 -- involving ancestor conversion of an extended type.
4376
4377 elsif not
4378 (Is_Tagged_Type (Target_Typ)
4379 and then not Is_Class_Wide_Type (Target_Typ)
4380 and then Is_Tagged_Type (Operand_Typ)
4381 and then not Is_Class_Wide_Type (Operand_Typ)
4382 and then Is_Ancestor (Target_Typ, Operand_Typ))
4383 then
4384 if Ekind_In
4385 (F, E_Out_Parameter, E_In_Out_Parameter)
4386 then
4387 Check_SPARK_05_Restriction
4388 ("ancestor conversion is the only permitted "
4389 & "view conversion", A);
4390 else
4391 Check_SPARK_05_Restriction
4392 ("ancestor conversion required", A);
4393 end if;
4394
4395 else
4396 null;
4397 end if;
4398 end;
4399
4400 else
4401 Check_SPARK_05_Restriction ("object required", A);
4402 end if;
4403
4404 -- In formal mode, the only view conversions are those
4405 -- involving ancestor conversion of an extended type.
4406
4407 elsif Nkind (A) = N_Type_Conversion
4408 and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
4409 then
4410 Check_SPARK_05_Restriction
4411 ("ancestor conversion is the only permitted view "
4412 & "conversion", A);
4413 end if;
4414 end if;
4415
4416 -- has warnings suppressed, then we reset Never_Set_In_Source for
4417 -- the calling entity. The reason for this is to catch cases like
4418 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
4419 -- uses trickery to modify an IN parameter.
4420
4421 if Ekind (F) = E_In_Parameter
4422 and then Is_Entity_Name (A)
4423 and then Present (Entity (A))
4424 and then Ekind (Entity (A)) = E_Variable
4425 and then Has_Warnings_Off (F_Typ)
4426 then
4427 Set_Never_Set_In_Source (Entity (A), False);
4428 end if;
4429
4430 -- Perform error checks for IN and IN OUT parameters
4431
4432 if Ekind (F) /= E_Out_Parameter then
4433
4434 -- Check unset reference. For scalar parameters, it is clearly
4435 -- wrong to pass an uninitialized value as either an IN or
4436 -- IN-OUT parameter. For composites, it is also clearly an
4437 -- error to pass a completely uninitialized value as an IN
4438 -- parameter, but the case of IN OUT is trickier. We prefer
4439 -- not to give a warning here. For example, suppose there is
4440 -- a routine that sets some component of a record to False.
4441 -- It is perfectly reasonable to make this IN-OUT and allow
4442 -- either initialized or uninitialized records to be passed
4443 -- in this case.
4444
4445 -- For partially initialized composite values, we also avoid
4446 -- warnings, since it is quite likely that we are passing a
4447 -- partially initialized value and only the initialized fields
4448 -- will in fact be read in the subprogram.
4449
4450 if Is_Scalar_Type (A_Typ)
4451 or else (Ekind (F) = E_In_Parameter
4452 and then not Is_Partially_Initialized_Type (A_Typ))
4453 then
4454 Check_Unset_Reference (A);
4455 end if;
4456
4457 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
4458 -- actual to a nested call, since this constitutes a reading of
4459 -- the parameter, which is not allowed.
4460
4461 if Ada_Version = Ada_83
4462 and then Is_Entity_Name (A)
4463 and then Ekind (Entity (A)) = E_Out_Parameter
4464 then
4465 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
4466 end if;
4467 end if;
4468
4469 -- In -gnatd.q mode, forget that a given array is constant when
4470 -- it is passed as an IN parameter to a foreign-convention
4471 -- subprogram. This is in case the subprogram evilly modifies the
4472 -- object. Of course, correct code would use IN OUT.
4473
4474 if Debug_Flag_Dot_Q
4475 and then Ekind (F) = E_In_Parameter
4476 and then Has_Foreign_Convention (Nam)
4477 and then Is_Array_Type (F_Typ)
4478 and then Nkind (A) in N_Has_Entity
4479 and then Present (Entity (A))
4480 then
4481 Set_Is_True_Constant (Entity (A), False);
4482 end if;
4483
4484 -- Case of OUT or IN OUT parameter
4485
4486 if Ekind (F) /= E_In_Parameter then
4487
4488 -- For an Out parameter, check for useless assignment. Note
4489 -- that we can't set Last_Assignment this early, because we may
4490 -- kill current values in Resolve_Call, and that call would
4491 -- clobber the Last_Assignment field.
4492
4493 -- Note: call Warn_On_Useless_Assignment before doing the check
4494 -- below for Is_OK_Variable_For_Out_Formal so that the setting
4495 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
4496 -- reflects the last assignment, not this one.
4497
4498 if Ekind (F) = E_Out_Parameter then
4499 if Warn_On_Modified_As_Out_Parameter (F)
4500 and then Is_Entity_Name (A)
4501 and then Present (Entity (A))
4502 and then Comes_From_Source (N)
4503 then
4504 Warn_On_Useless_Assignment (Entity (A), A);
4505 end if;
4506 end if;
4507
4508 -- Validate the form of the actual. Note that the call to
4509 -- Is_OK_Variable_For_Out_Formal generates the required
4510 -- reference in this case.
4511
4512 -- A call to an initialization procedure for an aggregate
4513 -- component may initialize a nested component of a constant
4514 -- designated object. In this context the object is variable.
4515
4516 if not Is_OK_Variable_For_Out_Formal (A)
4517 and then not Is_Init_Proc (Nam)
4518 then
4519 Error_Msg_NE ("actual for& must be a variable", A, F);
4520
4521 if Is_Subprogram (Current_Scope) then
4522 if Is_Invariant_Procedure (Current_Scope)
4523 or else Is_Partial_Invariant_Procedure (Current_Scope)
4524 then
4525 Error_Msg_N
4526 ("function used in invariant cannot modify its "
4527 & "argument", F);
4528
4529 elsif Is_Predicate_Function (Current_Scope) then
4530 Error_Msg_N
4531 ("function used in predicate cannot modify its "
4532 & "argument", F);
4533 end if;
4534 end if;
4535 end if;
4536
4537 -- What's the following about???
4538
4539 if Is_Entity_Name (A) then
4540 Kill_Checks (Entity (A));
4541 else
4542 Kill_All_Checks;
4543 end if;
4544 end if;
4545
4546 if A_Typ = Any_Type then
4547 Set_Etype (N, Any_Type);
4548 return;
4549 end if;
4550
4551 -- Apply appropriate constraint/predicate checks for IN [OUT] case
4552
4553 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
4554
4555 -- Apply predicate tests except in certain special cases. Note
4556 -- that it might be more consistent to apply these only when
4557 -- expansion is active (in Exp_Ch6.Expand_Actuals), as we do
4558 -- for the outbound predicate tests ??? In any case indicate
4559 -- the function being called, for better warnings if the call
4560 -- leads to an infinite recursion.
4561
4562 if Predicate_Tests_On_Arguments (Nam) then
4563 Apply_Predicate_Check (A, F_Typ, Nam);
4564 end if;
4565
4566 -- Apply required constraint checks
4567
4568 if Is_Scalar_Type (A_Typ) then
4569 Apply_Scalar_Range_Check (A, F_Typ);
4570
4571 elsif Is_Array_Type (A_Typ) then
4572 Apply_Length_Check (A, F_Typ);
4573
4574 elsif Is_Record_Type (F_Typ)
4575 and then Has_Discriminants (F_Typ)
4576 and then Is_Constrained (F_Typ)
4577 and then (not Is_Derived_Type (F_Typ)
4578 or else Comes_From_Source (Nam))
4579 then
4580 Apply_Discriminant_Check (A, F_Typ);
4581
4582 -- For view conversions of a discriminated object, apply
4583 -- check to object itself, the conversion alreay has the
4584 -- proper type.
4585
4586 if Nkind (A) = N_Type_Conversion
4587 and then Is_Constrained (Etype (Expression (A)))
4588 then
4589 Apply_Discriminant_Check (Expression (A), F_Typ);
4590 end if;
4591
4592 elsif Is_Access_Type (F_Typ)
4593 and then Is_Array_Type (Designated_Type (F_Typ))
4594 and then Is_Constrained (Designated_Type (F_Typ))
4595 then
4596 Apply_Length_Check (A, F_Typ);
4597
4598 elsif Is_Access_Type (F_Typ)
4599 and then Has_Discriminants (Designated_Type (F_Typ))
4600 and then Is_Constrained (Designated_Type (F_Typ))
4601 then
4602 Apply_Discriminant_Check (A, F_Typ);
4603
4604 else
4605 Apply_Range_Check (A, F_Typ);
4606 end if;
4607
4608 -- Ada 2005 (AI-231): Note that the controlling parameter case
4609 -- already existed in Ada 95, which is partially checked
4610 -- elsewhere (see Checks), and we don't want the warning
4611 -- message to differ.
4612
4613 if Is_Access_Type (F_Typ)
4614 and then Can_Never_Be_Null (F_Typ)
4615 and then Known_Null (A)
4616 then
4617 if Is_Controlling_Formal (F) then
4618 Apply_Compile_Time_Constraint_Error
4619 (N => A,
4620 Msg => "null value not allowed here??",
4621 Reason => CE_Access_Check_Failed);
4622
4623 elsif Ada_Version >= Ada_2005 then
4624 Apply_Compile_Time_Constraint_Error
4625 (N => A,
4626 Msg => "(Ada 2005) null not allowed in "
4627 & "null-excluding formal??",
4628 Reason => CE_Null_Not_Allowed);
4629 end if;
4630 end if;
4631 end if;
4632
4633 -- Checks for OUT parameters and IN OUT parameters
4634
4635 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
4636
4637 -- If there is a type conversion, make sure the return value
4638 -- meets the constraints of the variable before the conversion.
4639
4640 if Nkind (A) = N_Type_Conversion then
4641 if Is_Scalar_Type (A_Typ) then
4642
4643 -- Special case here tailored to Exp_Ch6.Is_Legal_Copy,
4644 -- which would prevent the check from being generated.
4645 -- This is for Starlet only though, so long obsolete.
4646
4647 if Mechanism (F) = By_Reference
4648 and then Is_Valued_Procedure (Nam)
4649 then
4650 null;
4651 else
4652 Apply_Scalar_Range_Check
4653 (Expression (A), Etype (Expression (A)), A_Typ);
4654 end if;
4655
4656 -- In addition the return value must meet the constraints
4657 -- of the object type (see the comment below).
4658
4659 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4660
4661 else
4662 Apply_Range_Check
4663 (Expression (A), Etype (Expression (A)), A_Typ);
4664 end if;
4665
4666 -- If no conversion, apply scalar range checks and length check
4667 -- based on the subtype of the actual (NOT that of the formal).
4668 -- This indicates that the check takes place on return from the
4669 -- call. During expansion the required constraint checks are
4670 -- inserted. In GNATprove mode, in the absence of expansion,
4671 -- the flag indicates that the returned value is valid.
4672
4673 else
4674 if Is_Scalar_Type (F_Typ) then
4675 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4676
4677 elsif Is_Array_Type (F_Typ)
4678 and then Ekind (F) = E_Out_Parameter
4679 then
4680 Apply_Length_Check (A, F_Typ);
4681
4682 else
4683 Apply_Range_Check (A, A_Typ, F_Typ);
4684 end if;
4685 end if;
4686
4687 -- Note: we do not apply the predicate checks for the case of
4688 -- OUT and IN OUT parameters. They are instead applied in the
4689 -- Expand_Actuals routine in Exp_Ch6.
4690 end if;
4691
4692 -- An actual associated with an access parameter is implicitly
4693 -- converted to the anonymous access type of the formal and must
4694 -- satisfy the legality checks for access conversions.
4695
4696 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4697 if not Valid_Conversion (A, F_Typ, A) then
4698 Error_Msg_N
4699 ("invalid implicit conversion for access parameter", A);
4700 end if;
4701
4702 -- If the actual is an access selected component of a variable,
4703 -- the call may modify its designated object. It is reasonable
4704 -- to treat this as a potential modification of the enclosing
4705 -- record, to prevent spurious warnings that it should be
4706 -- declared as a constant, because intuitively programmers
4707 -- regard the designated subcomponent as part of the record.
4708
4709 if Nkind (A) = N_Selected_Component
4710 and then Is_Entity_Name (Prefix (A))
4711 and then not Is_Constant_Object (Entity (Prefix (A)))
4712 then
4713 Note_Possible_Modification (A, Sure => False);
4714 end if;
4715 end if;
4716
4717 -- Check illegal cases of atomic/volatile actual (RM C.6(12,13))
4718
4719 if (Is_By_Reference_Type (Etype (F)) or else Is_Aliased (F))
4720 and then Comes_From_Source (N)
4721 then
4722 if Is_Atomic_Object (A)
4723 and then not Is_Atomic (Etype (F))
4724 then
4725 Error_Msg_NE
4726 ("cannot pass atomic object to nonatomic formal&",
4727 A, F);
4728 Error_Msg_N
4729 ("\which is passed by reference (RM C.6(12))", A);
4730
4731 elsif Is_Volatile_Object (A)
4732 and then not Is_Volatile (Etype (F))
4733 then
4734 Error_Msg_NE
4735 ("cannot pass volatile object to nonvolatile formal&",
4736 A, F);
4737 Error_Msg_N
4738 ("\which is passed by reference (RM C.6(12))", A);
4739 end if;
4740
4741 if Ada_Version >= Ada_2020
4742 and then Is_Subcomponent_Of_Atomic_Object (A)
4743 and then not Is_Atomic_Object (A)
4744 then
4745 Error_Msg_N
4746 ("cannot pass nonatomic subcomponent of atomic object",
4747 A);
4748 Error_Msg_NE
4749 ("\to formal & which is passed by reference (RM C.6(13))",
4750 A, F);
4751 end if;
4752 end if;
4753
4754 -- Check that subprograms don't have improper controlling
4755 -- arguments (RM 3.9.2 (9)).
4756
4757 -- A primitive operation may have an access parameter of an
4758 -- incomplete tagged type, but a dispatching call is illegal
4759 -- if the type is still incomplete.
4760
4761 if Is_Controlling_Formal (F) then
4762 Set_Is_Controlling_Actual (A);
4763
4764 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4765 declare
4766 Desig : constant Entity_Id := Designated_Type (Etype (F));
4767 begin
4768 if Ekind (Desig) = E_Incomplete_Type
4769 and then No (Full_View (Desig))
4770 and then No (Non_Limited_View (Desig))
4771 then
4772 Error_Msg_NE
4773 ("premature use of incomplete type& "
4774 & "in dispatching call", A, Desig);
4775 end if;
4776 end;
4777 end if;
4778
4779 elsif Nkind (A) = N_Explicit_Dereference then
4780 Validate_Remote_Access_To_Class_Wide_Type (A);
4781 end if;
4782
4783 -- Apply legality rule 3.9.2 (9/1)
4784
4785 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
4786 and then not Is_Class_Wide_Type (F_Typ)
4787 and then not Is_Controlling_Formal (F)
4788 and then not In_Instance
4789 then
4790 Error_Msg_N ("class-wide argument not allowed here!", A);
4791
4792 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4793 Error_Msg_Node_2 := F_Typ;
4794 Error_Msg_NE
4795 ("& is not a dispatching operation of &!", A, Nam);
4796 end if;
4797
4798 -- Apply the checks described in 3.10.2(27): if the context is a
4799 -- specific access-to-object, the actual cannot be class-wide.
4800 -- Use base type to exclude access_to_subprogram cases.
4801
4802 elsif Is_Access_Type (A_Typ)
4803 and then Is_Access_Type (F_Typ)
4804 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
4805 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
4806 or else (Nkind (A) = N_Attribute_Reference
4807 and then
4808 Is_Class_Wide_Type (Etype (Prefix (A)))))
4809 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
4810 and then not Is_Controlling_Formal (F)
4811
4812 -- Disable these checks for call to imported C++ subprograms
4813
4814 and then not
4815 (Is_Entity_Name (Name (N))
4816 and then Is_Imported (Entity (Name (N)))
4817 and then Convention (Entity (Name (N))) = Convention_CPP)
4818 then
4819 Error_Msg_N
4820 ("access to class-wide argument not allowed here!", A);
4821
4822 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4823 Error_Msg_Node_2 := Designated_Type (F_Typ);
4824 Error_Msg_NE
4825 ("& is not a dispatching operation of &!", A, Nam);
4826 end if;
4827 end if;
4828
4829 Check_Aliased_Parameter;
4830
4831 Eval_Actual (A);
4832
4833 -- If it is a named association, treat the selector_name as a
4834 -- proper identifier, and mark the corresponding entity.
4835
4836 if Nkind (Parent (A)) = N_Parameter_Association
4837
4838 -- Ignore reference in SPARK mode, as it refers to an entity not
4839 -- in scope at the point of reference, so the reference should
4840 -- be ignored for computing effects of subprograms.
4841
4842 and then not GNATprove_Mode
4843 then
4844 -- If subprogram is overridden, use name of formal that
4845 -- is being called.
4846
4847 if Present (Real_Subp) then
4848 Set_Entity (Selector_Name (Parent (A)), Real_F);
4849 Set_Etype (Selector_Name (Parent (A)), Etype (Real_F));
4850
4851 else
4852 Set_Entity (Selector_Name (Parent (A)), F);
4853 Generate_Reference (F, Selector_Name (Parent (A)));
4854 Set_Etype (Selector_Name (Parent (A)), F_Typ);
4855 Generate_Reference (F_Typ, N, ' ');
4856 end if;
4857 end if;
4858
4859 Prev := A;
4860
4861 if Ekind (F) /= E_Out_Parameter then
4862 Check_Unset_Reference (A);
4863 end if;
4864
4865 -- The following checks are only relevant when SPARK_Mode is on as
4866 -- they are not standard Ada legality rule. Internally generated
4867 -- temporaries are ignored.
4868
4869 if SPARK_Mode = On and then Comes_From_Source (A) then
4870
4871 -- An effectively volatile object may act as an actual when the
4872 -- corresponding formal is of a non-scalar effectively volatile
4873 -- type (SPARK RM 7.1.3(11)).
4874
4875 if not Is_Scalar_Type (Etype (F))
4876 and then Is_Effectively_Volatile (Etype (F))
4877 then
4878 null;
4879
4880 -- An effectively volatile object may act as an actual in a
4881 -- call to an instance of Unchecked_Conversion.
4882 -- (SPARK RM 7.1.3(11)).
4883
4884 elsif Is_Unchecked_Conversion_Instance (Nam) then
4885 null;
4886
4887 -- The actual denotes an object
4888
4889 elsif Is_Effectively_Volatile_Object (A) then
4890 Error_Msg_N
4891 ("volatile object cannot act as actual in a call (SPARK "
4892 & "RM 7.1.3(11))", A);
4893
4894 -- Otherwise the actual denotes an expression. Inspect the
4895 -- expression and flag each effectively volatile object with
4896 -- enabled property Async_Writers or Effective_Reads as illegal
4897 -- because it apprears within an interfering context. Note that
4898 -- this is usually done in Resolve_Entity_Name, but when the
4899 -- effectively volatile object appears as an actual in a call,
4900 -- the call must be resolved first.
4901
4902 else
4903 Flag_Effectively_Volatile_Objects (A);
4904 end if;
4905
4906 -- An effectively volatile variable cannot act as an actual
4907 -- parameter in a procedure call when the variable has enabled
4908 -- property Effective_Reads and the corresponding formal is of
4909 -- mode IN (SPARK RM 7.1.3(10)).
4910
4911 if Ekind (Nam) = E_Procedure
4912 and then Ekind (F) = E_In_Parameter
4913 and then Is_Entity_Name (A)
4914 then
4915 A_Id := Entity (A);
4916
4917 if Ekind (A_Id) = E_Variable
4918 and then Is_Effectively_Volatile (Etype (A_Id))
4919 and then Effective_Reads_Enabled (A_Id)
4920 then
4921 Error_Msg_NE
4922 ("effectively volatile variable & cannot appear as "
4923 & "actual in procedure call", A, A_Id);
4924
4925 Error_Msg_Name_1 := Name_Effective_Reads;
4926 Error_Msg_N ("\\variable has enabled property %", A);
4927 Error_Msg_N ("\\corresponding formal has mode IN", A);
4928 end if;
4929 end if;
4930 end if;
4931
4932 -- A formal parameter of a specific tagged type whose related
4933 -- subprogram is subject to pragma Extensions_Visible with value
4934 -- "False" cannot act as an actual in a subprogram with value
4935 -- "True" (SPARK RM 6.1.7(3)).
4936
4937 if Is_EVF_Expression (A)
4938 and then Extensions_Visible_Status (Nam) =
4939 Extensions_Visible_True
4940 then
4941 Error_Msg_N
4942 ("formal parameter cannot act as actual parameter when "
4943 & "Extensions_Visible is False", A);
4944 Error_Msg_NE
4945 ("\subprogram & has Extensions_Visible True", A, Nam);
4946 end if;
4947
4948 -- The actual parameter of a Ghost subprogram whose formal is of
4949 -- mode IN OUT or OUT must be a Ghost variable (SPARK RM 6.9(12)).
4950
4951 if Comes_From_Source (Nam)
4952 and then Is_Ghost_Entity (Nam)
4953 and then Ekind_In (F, E_In_Out_Parameter, E_Out_Parameter)
4954 and then Is_Entity_Name (A)
4955 and then Present (Entity (A))
4956 and then not Is_Ghost_Entity (Entity (A))
4957 then
4958 Error_Msg_NE
4959 ("non-ghost variable & cannot appear as actual in call to "
4960 & "ghost procedure", A, Entity (A));
4961
4962 if Ekind (F) = E_In_Out_Parameter then
4963 Error_Msg_N ("\corresponding formal has mode `IN OUT`", A);
4964 else
4965 Error_Msg_N ("\corresponding formal has mode OUT", A);
4966 end if;
4967 end if;
4968
4969 Next_Actual (A);
4970
4971 -- Case where actual is not present
4972
4973 else
4974 Insert_Default;
4975 end if;
4976
4977 Next_Formal (F);
4978
4979 if Present (Real_Subp) then
4980 Next_Formal (Real_F);
4981 end if;
4982 end loop;
4983 end Resolve_Actuals;
4984
4985 -----------------------
4986 -- Resolve_Allocator --
4987 -----------------------
4988
4989 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
4990 Desig_T : constant Entity_Id := Designated_Type (Typ);
4991 E : constant Node_Id := Expression (N);
4992 Subtyp : Entity_Id;
4993 Discrim : Entity_Id;
4994 Constr : Node_Id;
4995 Aggr : Node_Id;
4996 Assoc : Node_Id := Empty;
4997 Disc_Exp : Node_Id;
4998
4999 procedure Check_Allocator_Discrim_Accessibility
5000 (Disc_Exp : Node_Id;
5001 Alloc_Typ : Entity_Id);
5002 -- Check that accessibility level associated with an access discriminant
5003 -- initialized in an allocator by the expression Disc_Exp is not deeper
5004 -- than the level of the allocator type Alloc_Typ. An error message is
5005 -- issued if this condition is violated. Specialized checks are done for
5006 -- the cases of a constraint expression which is an access attribute or
5007 -- an access discriminant.
5008
5009 procedure Check_Allocator_Discrim_Accessibility_Exprs
5010 (Curr_Exp : Node_Id;
5011 Alloc_Typ : Entity_Id);
5012 -- Dispatch checks performed by Check_Allocator_Discrim_Accessibility
5013 -- across all expressions within a given conditional expression.
5014
5015 function In_Dispatching_Context return Boolean;
5016 -- If the allocator is an actual in a call, it is allowed to be class-
5017 -- wide when the context is not because it is a controlling actual.
5018
5019 -------------------------------------------
5020 -- Check_Allocator_Discrim_Accessibility --
5021 -------------------------------------------
5022
5023 procedure Check_Allocator_Discrim_Accessibility
5024 (Disc_Exp : Node_Id;
5025 Alloc_Typ : Entity_Id)
5026 is
5027 begin
5028 if Type_Access_Level (Etype (Disc_Exp)) >
5029 Deepest_Type_Access_Level (Alloc_Typ)
5030 then
5031 Error_Msg_N
5032 ("operand type has deeper level than allocator type", Disc_Exp);
5033
5034 -- When the expression is an Access attribute the level of the prefix
5035 -- object must not be deeper than that of the allocator's type.
5036
5037 elsif Nkind (Disc_Exp) = N_Attribute_Reference
5038 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
5039 Attribute_Access
5040 and then Object_Access_Level (Prefix (Disc_Exp)) >
5041 Deepest_Type_Access_Level (Alloc_Typ)
5042 then
5043 Error_Msg_N
5044 ("prefix of attribute has deeper level than allocator type",
5045 Disc_Exp);
5046
5047 -- When the expression is an access discriminant the check is against
5048 -- the level of the prefix object.
5049
5050 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
5051 and then Nkind (Disc_Exp) = N_Selected_Component
5052 and then Object_Access_Level (Prefix (Disc_Exp)) >
5053 Deepest_Type_Access_Level (Alloc_Typ)
5054 then
5055 Error_Msg_N
5056 ("access discriminant has deeper level than allocator type",
5057 Disc_Exp);
5058
5059 -- All other cases are legal
5060
5061 else
5062 null;
5063 end if;
5064 end Check_Allocator_Discrim_Accessibility;
5065
5066 -------------------------------------------------
5067 -- Check_Allocator_Discrim_Accessibility_Exprs --
5068 -------------------------------------------------
5069
5070 procedure Check_Allocator_Discrim_Accessibility_Exprs
5071 (Curr_Exp : Node_Id;
5072 Alloc_Typ : Entity_Id)
5073 is
5074 Alt : Node_Id;
5075 Expr : Node_Id;
5076 Disc_Exp : constant Node_Id := Original_Node (Curr_Exp);
5077 begin
5078 -- When conditional expressions are constant folded we know at
5079 -- compile time which expression to check - so don't bother with
5080 -- the rest of the cases.
5081
5082 if Nkind (Curr_Exp) = N_Attribute_Reference then
5083 Check_Allocator_Discrim_Accessibility (Curr_Exp, Alloc_Typ);
5084
5085 -- Non-constant-folded if expressions
5086
5087 elsif Nkind (Disc_Exp) = N_If_Expression then
5088 -- Check both expressions if they are still present in the face
5089 -- of expansion.
5090
5091 Expr := Next (First (Expressions (Disc_Exp)));
5092 if Present (Expr) then
5093 Check_Allocator_Discrim_Accessibility_Exprs (Expr, Alloc_Typ);
5094 Expr := Next (Expr);
5095 if Present (Expr) then
5096 Check_Allocator_Discrim_Accessibility_Exprs
5097 (Expr, Alloc_Typ);
5098 end if;
5099 end if;
5100
5101 -- Non-constant-folded case expressions
5102
5103 elsif Nkind (Disc_Exp) = N_Case_Expression then
5104 -- Check all alternatives
5105
5106 Alt := First (Alternatives (Disc_Exp));
5107 while Present (Alt) loop
5108 Check_Allocator_Discrim_Accessibility_Exprs
5109 (Expression (Alt), Alloc_Typ);
5110
5111 Next (Alt);
5112 end loop;
5113
5114 -- Base case, check the accessibility of the original node of the
5115 -- expression.
5116
5117 else
5118 Check_Allocator_Discrim_Accessibility (Disc_Exp, Alloc_Typ);
5119 end if;
5120 end Check_Allocator_Discrim_Accessibility_Exprs;
5121
5122 ----------------------------
5123 -- In_Dispatching_Context --
5124 ----------------------------
5125
5126 function In_Dispatching_Context return Boolean is
5127 Par : constant Node_Id := Parent (N);
5128
5129 begin
5130 return Nkind (Par) in N_Subprogram_Call
5131 and then Is_Entity_Name (Name (Par))
5132 and then Is_Dispatching_Operation (Entity (Name (Par)));
5133 end In_Dispatching_Context;
5134
5135 -- Start of processing for Resolve_Allocator
5136
5137 begin
5138 -- Replace general access with specific type
5139
5140 if Ekind (Etype (N)) = E_Allocator_Type then
5141 Set_Etype (N, Base_Type (Typ));
5142 end if;
5143
5144 if Is_Abstract_Type (Typ) then
5145 Error_Msg_N ("type of allocator cannot be abstract", N);
5146 end if;
5147
5148 -- For qualified expression, resolve the expression using the given
5149 -- subtype (nothing to do for type mark, subtype indication)
5150
5151 if Nkind (E) = N_Qualified_Expression then
5152 if Is_Class_Wide_Type (Etype (E))
5153 and then not Is_Class_Wide_Type (Desig_T)
5154 and then not In_Dispatching_Context
5155 then
5156 Error_Msg_N
5157 ("class-wide allocator not allowed for this access type", N);
5158 end if;
5159
5160 Resolve (Expression (E), Etype (E));
5161 Check_Non_Static_Context (Expression (E));
5162 Check_Unset_Reference (Expression (E));
5163
5164 -- Allocators generated by the build-in-place expansion mechanism
5165 -- are explicitly marked as coming from source but do not need to be
5166 -- checked for limited initialization. To exclude this case, ensure
5167 -- that the parent of the allocator is a source node.
5168 -- The return statement constructed for an Expression_Function does
5169 -- not come from source but requires a limited check.
5170
5171 if Is_Limited_Type (Etype (E))
5172 and then Comes_From_Source (N)
5173 and then
5174 (Comes_From_Source (Parent (N))
5175 or else
5176 (Ekind (Current_Scope) = E_Function
5177 and then Nkind (Original_Node (Unit_Declaration_Node
5178 (Current_Scope))) = N_Expression_Function))
5179 and then not In_Instance_Body
5180 then
5181 if not OK_For_Limited_Init (Etype (E), Expression (E)) then
5182 if Nkind (Parent (N)) = N_Assignment_Statement then
5183 Error_Msg_N
5184 ("illegal expression for initialized allocator of a "
5185 & "limited type (RM 7.5 (2.7/2))", N);
5186 else
5187 Error_Msg_N
5188 ("initialization not allowed for limited types", N);
5189 end if;
5190
5191 Explain_Limited_Type (Etype (E), N);
5192 end if;
5193 end if;
5194
5195 -- A qualified expression requires an exact match of the type. Class-
5196 -- wide matching is not allowed.
5197
5198 if (Is_Class_Wide_Type (Etype (Expression (E)))
5199 or else Is_Class_Wide_Type (Etype (E)))
5200 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
5201 then
5202 Wrong_Type (Expression (E), Etype (E));
5203 end if;
5204
5205 -- Calls to build-in-place functions are not currently supported in
5206 -- allocators for access types associated with a simple storage pool.
5207 -- Supporting such allocators may require passing additional implicit
5208 -- parameters to build-in-place functions (or a significant revision
5209 -- of the current b-i-p implementation to unify the handling for
5210 -- multiple kinds of storage pools). ???
5211
5212 if Is_Limited_View (Desig_T)
5213 and then Nkind (Expression (E)) = N_Function_Call
5214 then
5215 declare
5216 Pool : constant Entity_Id :=
5217 Associated_Storage_Pool (Root_Type (Typ));
5218 begin
5219 if Present (Pool)
5220 and then
5221 Present (Get_Rep_Pragma
5222 (Etype (Pool), Name_Simple_Storage_Pool_Type))
5223 then
5224 Error_Msg_N
5225 ("limited function calls not yet supported in simple "
5226 & "storage pool allocators", Expression (E));
5227 end if;
5228 end;
5229 end if;
5230
5231 -- A special accessibility check is needed for allocators that
5232 -- constrain access discriminants. The level of the type of the
5233 -- expression used to constrain an access discriminant cannot be
5234 -- deeper than the type of the allocator (in contrast to access
5235 -- parameters, where the level of the actual can be arbitrary).
5236
5237 -- We can't use Valid_Conversion to perform this check because in
5238 -- general the type of the allocator is unrelated to the type of
5239 -- the access discriminant.
5240
5241 if Ekind (Typ) /= E_Anonymous_Access_Type
5242 or else Is_Local_Anonymous_Access (Typ)
5243 then
5244 Subtyp := Entity (Subtype_Mark (E));
5245
5246 Aggr := Original_Node (Expression (E));
5247
5248 if Has_Discriminants (Subtyp)
5249 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
5250 then
5251 Discrim := First_Discriminant (Base_Type (Subtyp));
5252
5253 -- Get the first component expression of the aggregate
5254
5255 if Present (Expressions (Aggr)) then
5256 Disc_Exp := First (Expressions (Aggr));
5257
5258 elsif Present (Component_Associations (Aggr)) then
5259 Assoc := First (Component_Associations (Aggr));
5260
5261 if Present (Assoc) then
5262 Disc_Exp := Expression (Assoc);
5263 else
5264 Disc_Exp := Empty;
5265 end if;
5266
5267 else
5268 Disc_Exp := Empty;
5269 end if;
5270
5271 while Present (Discrim) and then Present (Disc_Exp) loop
5272 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
5273 Check_Allocator_Discrim_Accessibility_Exprs
5274 (Disc_Exp, Typ);
5275 end if;
5276
5277 Next_Discriminant (Discrim);
5278
5279 if Present (Discrim) then
5280 if Present (Assoc) then
5281 Next (Assoc);
5282 Disc_Exp := Expression (Assoc);
5283
5284 elsif Present (Next (Disc_Exp)) then
5285 Next (Disc_Exp);
5286
5287 else
5288 Assoc := First (Component_Associations (Aggr));
5289
5290 if Present (Assoc) then
5291 Disc_Exp := Expression (Assoc);
5292 else
5293 Disc_Exp := Empty;
5294 end if;
5295 end if;
5296 end if;
5297 end loop;
5298 end if;
5299 end if;
5300
5301 -- For a subtype mark or subtype indication, freeze the subtype
5302
5303 else
5304 Freeze_Expression (E);
5305
5306 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
5307 Error_Msg_N
5308 ("initialization required for access-to-constant allocator", N);
5309 end if;
5310
5311 -- A special accessibility check is needed for allocators that
5312 -- constrain access discriminants. The level of the type of the
5313 -- expression used to constrain an access discriminant cannot be
5314 -- deeper than the type of the allocator (in contrast to access
5315 -- parameters, where the level of the actual can be arbitrary).
5316 -- We can't use Valid_Conversion to perform this check because
5317 -- in general the type of the allocator is unrelated to the type
5318 -- of the access discriminant.
5319
5320 if Nkind (Original_Node (E)) = N_Subtype_Indication
5321 and then (Ekind (Typ) /= E_Anonymous_Access_Type
5322 or else Is_Local_Anonymous_Access (Typ))
5323 then
5324 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
5325
5326 if Has_Discriminants (Subtyp) then
5327 Discrim := First_Discriminant (Base_Type (Subtyp));
5328 Constr := First (Constraints (Constraint (Original_Node (E))));
5329 while Present (Discrim) and then Present (Constr) loop
5330 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
5331 if Nkind (Constr) = N_Discriminant_Association then
5332 Disc_Exp := Expression (Constr);
5333 else
5334 Disc_Exp := Constr;
5335 end if;
5336
5337 Check_Allocator_Discrim_Accessibility_Exprs
5338 (Disc_Exp, Typ);
5339 end if;
5340
5341 Next_Discriminant (Discrim);
5342 Next (Constr);
5343 end loop;
5344 end if;
5345 end if;
5346 end if;
5347
5348 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
5349 -- check that the level of the type of the created object is not deeper
5350 -- than the level of the allocator's access type, since extensions can
5351 -- now occur at deeper levels than their ancestor types. This is a
5352 -- static accessibility level check; a run-time check is also needed in
5353 -- the case of an initialized allocator with a class-wide argument (see
5354 -- Expand_Allocator_Expression).
5355
5356 if Ada_Version >= Ada_2005
5357 and then Is_Class_Wide_Type (Desig_T)
5358 then
5359 declare
5360 Exp_Typ : Entity_Id;
5361
5362 begin
5363 if Nkind (E) = N_Qualified_Expression then
5364 Exp_Typ := Etype (E);
5365 elsif Nkind (E) = N_Subtype_Indication then
5366 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
5367 else
5368 Exp_Typ := Entity (E);
5369 end if;
5370
5371 if Type_Access_Level (Exp_Typ) >
5372 Deepest_Type_Access_Level (Typ)
5373 then
5374 if In_Instance_Body then
5375 Error_Msg_Warn := SPARK_Mode /= On;
5376 Error_Msg_N
5377 ("type in allocator has deeper level than designated "
5378 & "class-wide type<<", E);
5379 Error_Msg_N ("\Program_Error [<<", E);
5380
5381 Rewrite (N,
5382 Make_Raise_Program_Error (Sloc (N),
5383 Reason => PE_Accessibility_Check_Failed));
5384 Set_Etype (N, Typ);
5385
5386 -- Do not apply Ada 2005 accessibility checks on a class-wide
5387 -- allocator if the type given in the allocator is a formal
5388 -- type. A run-time check will be performed in the instance.
5389
5390 elsif not Is_Generic_Type (Exp_Typ) then
5391 Error_Msg_N
5392 ("type in allocator has deeper level than designated "
5393 & "class-wide type", E);
5394 end if;
5395 end if;
5396 end;
5397 end if;
5398
5399 -- Check for allocation from an empty storage pool. But do not complain
5400 -- if it's a return statement for a build-in-place function, because the
5401 -- allocator is there just in case the caller uses an allocator. If the
5402 -- caller does use an allocator, it will be caught at the call site.
5403
5404 if No_Pool_Assigned (Typ)
5405 and then not Alloc_For_BIP_Return (N)
5406 then
5407 Error_Msg_N ("allocation from empty storage pool!", N);
5408
5409 -- If the context is an unchecked conversion, as may happen within an
5410 -- inlined subprogram, the allocator is being resolved with its own
5411 -- anonymous type. In that case, if the target type has a specific
5412 -- storage pool, it must be inherited explicitly by the allocator type.
5413
5414 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
5415 and then No (Associated_Storage_Pool (Typ))
5416 then
5417 Set_Associated_Storage_Pool
5418 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
5419 end if;
5420
5421 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
5422 Check_Restriction (No_Anonymous_Allocators, N);
5423 end if;
5424
5425 -- Check that an allocator with task parts isn't for a nested access
5426 -- type when restriction No_Task_Hierarchy applies.
5427
5428 if not Is_Library_Level_Entity (Base_Type (Typ))
5429 and then Has_Task (Base_Type (Desig_T))
5430 then
5431 Check_Restriction (No_Task_Hierarchy, N);
5432 end if;
5433
5434 -- An illegal allocator may be rewritten as a raise Program_Error
5435 -- statement.
5436
5437 if Nkind (N) = N_Allocator then
5438
5439 -- Avoid coextension processing for an allocator that is the
5440 -- expansion of a build-in-place function call.
5441
5442 if Nkind (Original_Node (N)) = N_Allocator
5443 and then Nkind (Expression (Original_Node (N))) =
5444 N_Qualified_Expression
5445 and then Nkind (Expression (Expression (Original_Node (N)))) =
5446 N_Function_Call
5447 and then Is_Expanded_Build_In_Place_Call
5448 (Expression (Expression (Original_Node (N))))
5449 then
5450 null; -- b-i-p function call case
5451
5452 else
5453 -- An anonymous access discriminant is the definition of a
5454 -- coextension.
5455
5456 if Ekind (Typ) = E_Anonymous_Access_Type
5457 and then Nkind (Associated_Node_For_Itype (Typ)) =
5458 N_Discriminant_Specification
5459 then
5460 declare
5461 Discr : constant Entity_Id :=
5462 Defining_Identifier (Associated_Node_For_Itype (Typ));
5463
5464 begin
5465 Check_Restriction (No_Coextensions, N);
5466
5467 -- Ada 2012 AI05-0052: If the designated type of the
5468 -- allocator is limited, then the allocator shall not
5469 -- be used to define the value of an access discriminant
5470 -- unless the discriminated type is immutably limited.
5471
5472 if Ada_Version >= Ada_2012
5473 and then Is_Limited_Type (Desig_T)
5474 and then not Is_Limited_View (Scope (Discr))
5475 then
5476 Error_Msg_N
5477 ("only immutably limited types can have anonymous "
5478 & "access discriminants designating a limited type",
5479 N);
5480 end if;
5481 end;
5482
5483 -- Avoid marking an allocator as a dynamic coextension if it is
5484 -- within a static construct.
5485
5486 if not Is_Static_Coextension (N) then
5487 Set_Is_Dynamic_Coextension (N);
5488
5489 -- Finalization and deallocation of coextensions utilizes an
5490 -- approximate implementation which does not directly adhere
5491 -- to the semantic rules. Warn on potential issues involving
5492 -- coextensions.
5493
5494 if Is_Controlled (Desig_T) then
5495 Error_Msg_N
5496 ("??coextension will not be finalized when its "
5497 & "associated owner is deallocated or finalized", N);
5498 else
5499 Error_Msg_N
5500 ("??coextension will not be deallocated when its "
5501 & "associated owner is deallocated", N);
5502 end if;
5503 end if;
5504
5505 -- Cleanup for potential static coextensions
5506
5507 else
5508 Set_Is_Dynamic_Coextension (N, False);
5509 Set_Is_Static_Coextension (N, False);
5510
5511 -- Anonymous access-to-controlled objects are not finalized on
5512 -- time because this involves run-time ownership and currently
5513 -- this property is not available. In rare cases the object may
5514 -- not be finalized at all. Warn on potential issues involving
5515 -- anonymous access-to-controlled objects.
5516
5517 if Ekind (Typ) = E_Anonymous_Access_Type
5518 and then Is_Controlled_Active (Desig_T)
5519 then
5520 Error_Msg_N
5521 ("??object designated by anonymous access object might "
5522 & "not be finalized until its enclosing library unit "
5523 & "goes out of scope", N);
5524 Error_Msg_N ("\use named access type instead", N);
5525 end if;
5526 end if;
5527 end if;
5528 end if;
5529
5530 -- Report a simple error: if the designated object is a local task,
5531 -- its body has not been seen yet, and its activation will fail an
5532 -- elaboration check.
5533
5534 if Is_Task_Type (Desig_T)
5535 and then Scope (Base_Type (Desig_T)) = Current_Scope
5536 and then Is_Compilation_Unit (Current_Scope)
5537 and then Ekind (Current_Scope) = E_Package
5538 and then not In_Package_Body (Current_Scope)
5539 then
5540 Error_Msg_Warn := SPARK_Mode /= On;
5541 Error_Msg_N ("cannot activate task before body seen<<", N);
5542 Error_Msg_N ("\Program_Error [<<", N);
5543 end if;
5544
5545 -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a
5546 -- type with a task component on a subpool. This action must raise
5547 -- Program_Error at runtime.
5548
5549 if Ada_Version >= Ada_2012
5550 and then Nkind (N) = N_Allocator
5551 and then Present (Subpool_Handle_Name (N))
5552 and then Has_Task (Desig_T)
5553 then
5554 Error_Msg_Warn := SPARK_Mode /= On;
5555 Error_Msg_N ("cannot allocate task on subpool<<", N);
5556 Error_Msg_N ("\Program_Error [<<", N);
5557
5558 Rewrite (N,
5559 Make_Raise_Program_Error (Sloc (N),
5560 Reason => PE_Explicit_Raise));
5561 Set_Etype (N, Typ);
5562 end if;
5563 end Resolve_Allocator;
5564
5565 ---------------------------
5566 -- Resolve_Arithmetic_Op --
5567 ---------------------------
5568
5569 -- Used for resolving all arithmetic operators except exponentiation
5570
5571 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
5572 L : constant Node_Id := Left_Opnd (N);
5573 R : constant Node_Id := Right_Opnd (N);
5574 TL : constant Entity_Id := Base_Type (Etype (L));
5575 TR : constant Entity_Id := Base_Type (Etype (R));
5576 T : Entity_Id;
5577 Rop : Node_Id;
5578
5579 B_Typ : constant Entity_Id := Base_Type (Typ);
5580 -- We do the resolution using the base type, because intermediate values
5581 -- in expressions always are of the base type, not a subtype of it.
5582
5583 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
5584 -- Returns True if N is in a context that expects "any real type"
5585
5586 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
5587 -- Return True iff given type is Integer or universal real/integer
5588
5589 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
5590 -- Choose type of integer literal in fixed-point operation to conform
5591 -- to available fixed-point type. T is the type of the other operand,
5592 -- which is needed to determine the expected type of N.
5593
5594 procedure Set_Operand_Type (N : Node_Id);
5595 -- Set operand type to T if universal
5596
5597 -------------------------------
5598 -- Expected_Type_Is_Any_Real --
5599 -------------------------------
5600
5601 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
5602 begin
5603 -- N is the expression after "delta" in a fixed_point_definition;
5604 -- see RM-3.5.9(6):
5605
5606 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
5607 N_Decimal_Fixed_Point_Definition,
5608
5609 -- N is one of the bounds in a real_range_specification;
5610 -- see RM-3.5.7(5):
5611
5612 N_Real_Range_Specification,
5613
5614 -- N is the expression of a delta_constraint;
5615 -- see RM-J.3(3):
5616
5617 N_Delta_Constraint);
5618 end Expected_Type_Is_Any_Real;
5619
5620 -----------------------------
5621 -- Is_Integer_Or_Universal --
5622 -----------------------------
5623
5624 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
5625 T : Entity_Id;
5626 Index : Interp_Index;
5627 It : Interp;
5628
5629 begin
5630 if not Is_Overloaded (N) then
5631 T := Etype (N);
5632 return Base_Type (T) = Base_Type (Standard_Integer)
5633 or else T = Universal_Integer
5634 or else T = Universal_Real;
5635 else
5636 Get_First_Interp (N, Index, It);
5637 while Present (It.Typ) loop
5638 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
5639 or else It.Typ = Universal_Integer
5640 or else It.Typ = Universal_Real
5641 then
5642 return True;
5643 end if;
5644
5645 Get_Next_Interp (Index, It);
5646 end loop;
5647 end if;
5648
5649 return False;
5650 end Is_Integer_Or_Universal;
5651
5652 ----------------------------
5653 -- Set_Mixed_Mode_Operand --
5654 ----------------------------
5655
5656 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
5657 Index : Interp_Index;
5658 It : Interp;
5659
5660 begin
5661 if Universal_Interpretation (N) = Universal_Integer then
5662
5663 -- A universal integer literal is resolved as standard integer
5664 -- except in the case of a fixed-point result, where we leave it
5665 -- as universal (to be handled by Exp_Fixd later on)
5666
5667 if Is_Fixed_Point_Type (T) then
5668 Resolve (N, Universal_Integer);
5669 else
5670 Resolve (N, Standard_Integer);
5671 end if;
5672
5673 elsif Universal_Interpretation (N) = Universal_Real
5674 and then (T = Base_Type (Standard_Integer)
5675 or else T = Universal_Integer
5676 or else T = Universal_Real)
5677 then
5678 -- A universal real can appear in a fixed-type context. We resolve
5679 -- the literal with that context, even though this might raise an
5680 -- exception prematurely (the other operand may be zero).
5681
5682 Resolve (N, B_Typ);
5683
5684 elsif Etype (N) = Base_Type (Standard_Integer)
5685 and then T = Universal_Real
5686 and then Is_Overloaded (N)
5687 then
5688 -- Integer arg in mixed-mode operation. Resolve with universal
5689 -- type, in case preference rule must be applied.
5690
5691 Resolve (N, Universal_Integer);
5692
5693 elsif Etype (N) = T and then B_Typ /= Universal_Fixed then
5694
5695 -- If the operand is part of a fixed multiplication operation,
5696 -- a conversion will be applied to each operand, so resolve it
5697 -- with its own type.
5698
5699 if Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply) then
5700 Resolve (N);
5701
5702 else
5703 -- Not a mixed-mode operation, resolve with context
5704
5705 Resolve (N, B_Typ);
5706 end if;
5707
5708 elsif Etype (N) = Any_Fixed then
5709
5710 -- N may itself be a mixed-mode operation, so use context type
5711
5712 Resolve (N, B_Typ);
5713
5714 elsif Is_Fixed_Point_Type (T)
5715 and then B_Typ = Universal_Fixed
5716 and then Is_Overloaded (N)
5717 then
5718 -- Must be (fixed * fixed) operation, operand must have one
5719 -- compatible interpretation.
5720
5721 Resolve (N, Any_Fixed);
5722
5723 elsif Is_Fixed_Point_Type (B_Typ)
5724 and then (T = Universal_Real or else Is_Fixed_Point_Type (T))
5725 and then Is_Overloaded (N)
5726 then
5727 -- C * F(X) in a fixed context, where C is a real literal or a
5728 -- fixed-point expression. F must have either a fixed type
5729 -- interpretation or an integer interpretation, but not both.
5730
5731 Get_First_Interp (N, Index, It);
5732 while Present (It.Typ) loop
5733 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
5734 if Analyzed (N) then
5735 Error_Msg_N ("ambiguous operand in fixed operation", N);
5736 else
5737 Resolve (N, Standard_Integer);
5738 end if;
5739
5740 elsif Is_Fixed_Point_Type (It.Typ) then
5741 if Analyzed (N) then
5742 Error_Msg_N ("ambiguous operand in fixed operation", N);
5743 else
5744 Resolve (N, It.Typ);
5745 end if;
5746 end if;
5747
5748 Get_Next_Interp (Index, It);
5749 end loop;
5750
5751 -- Reanalyze the literal with the fixed type of the context. If
5752 -- context is Universal_Fixed, we are within a conversion, leave
5753 -- the literal as a universal real because there is no usable
5754 -- fixed type, and the target of the conversion plays no role in
5755 -- the resolution.
5756
5757 declare
5758 Op2 : Node_Id;
5759 T2 : Entity_Id;
5760
5761 begin
5762 if N = L then
5763 Op2 := R;
5764 else
5765 Op2 := L;
5766 end if;
5767
5768 if B_Typ = Universal_Fixed
5769 and then Nkind (Op2) = N_Real_Literal
5770 then
5771 T2 := Universal_Real;
5772 else
5773 T2 := B_Typ;
5774 end if;
5775
5776 Set_Analyzed (Op2, False);
5777 Resolve (Op2, T2);
5778 end;
5779
5780 -- A universal real conditional expression can appear in a fixed-type
5781 -- context and must be resolved with that context to facilitate the
5782 -- code generation in the back end. However, If the context is
5783 -- Universal_fixed (i.e. as an operand of a multiplication/division
5784 -- involving a fixed-point operand) the conditional expression must
5785 -- resolve to a unique visible fixed_point type, normally Duration.
5786
5787 elsif Nkind_In (N, N_Case_Expression, N_If_Expression)
5788 and then Etype (N) = Universal_Real
5789 and then Is_Fixed_Point_Type (B_Typ)
5790 then
5791 if B_Typ = Universal_Fixed then
5792 Resolve (N, Unique_Fixed_Point_Type (N));
5793
5794 else
5795 Resolve (N, B_Typ);
5796 end if;
5797
5798 else
5799 Resolve (N);
5800 end if;
5801 end Set_Mixed_Mode_Operand;
5802
5803 ----------------------
5804 -- Set_Operand_Type --
5805 ----------------------
5806
5807 procedure Set_Operand_Type (N : Node_Id) is
5808 begin
5809 if Etype (N) = Universal_Integer
5810 or else Etype (N) = Universal_Real
5811 then
5812 Set_Etype (N, T);
5813 end if;
5814 end Set_Operand_Type;
5815
5816 -- Start of processing for Resolve_Arithmetic_Op
5817
5818 begin
5819 if Comes_From_Source (N)
5820 and then Ekind (Entity (N)) = E_Function
5821 and then Is_Imported (Entity (N))
5822 and then Is_Intrinsic_Subprogram (Entity (N))
5823 then
5824 Resolve_Intrinsic_Operator (N, Typ);
5825 return;
5826
5827 -- Special-case for mixed-mode universal expressions or fixed point type
5828 -- operation: each argument is resolved separately. The same treatment
5829 -- is required if one of the operands of a fixed point operation is
5830 -- universal real, since in this case we don't do a conversion to a
5831 -- specific fixed-point type (instead the expander handles the case).
5832
5833 -- Set the type of the node to its universal interpretation because
5834 -- legality checks on an exponentiation operand need the context.
5835
5836 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
5837 and then Present (Universal_Interpretation (L))
5838 and then Present (Universal_Interpretation (R))
5839 then
5840 Set_Etype (N, B_Typ);
5841 Resolve (L, Universal_Interpretation (L));
5842 Resolve (R, Universal_Interpretation (R));
5843
5844 elsif (B_Typ = Universal_Real
5845 or else Etype (N) = Universal_Fixed
5846 or else (Etype (N) = Any_Fixed
5847 and then Is_Fixed_Point_Type (B_Typ))
5848 or else (Is_Fixed_Point_Type (B_Typ)
5849 and then (Is_Integer_Or_Universal (L)
5850 or else
5851 Is_Integer_Or_Universal (R))))
5852 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
5853 then
5854 if TL = Universal_Integer or else TR = Universal_Integer then
5855 Check_For_Visible_Operator (N, B_Typ);
5856 end if;
5857
5858 -- If context is a fixed type and one operand is integer, the other
5859 -- is resolved with the type of the context.
5860
5861 if Is_Fixed_Point_Type (B_Typ)
5862 and then (Base_Type (TL) = Base_Type (Standard_Integer)
5863 or else TL = Universal_Integer)
5864 then
5865 Resolve (R, B_Typ);
5866 Resolve (L, TL);
5867
5868 elsif Is_Fixed_Point_Type (B_Typ)
5869 and then (Base_Type (TR) = Base_Type (Standard_Integer)
5870 or else TR = Universal_Integer)
5871 then
5872 Resolve (L, B_Typ);
5873 Resolve (R, TR);
5874
5875 -- If both operands are universal and the context is a floating
5876 -- point type, the operands are resolved to the type of the context.
5877
5878 elsif Is_Floating_Point_Type (B_Typ) then
5879 Resolve (L, B_Typ);
5880 Resolve (R, B_Typ);
5881
5882 else
5883 Set_Mixed_Mode_Operand (L, TR);
5884 Set_Mixed_Mode_Operand (R, TL);
5885 end if;
5886
5887 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
5888 -- multiplying operators from being used when the expected type is
5889 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
5890 -- some cases where the expected type is actually Any_Real;
5891 -- Expected_Type_Is_Any_Real takes care of that case.
5892
5893 if Etype (N) = Universal_Fixed
5894 or else Etype (N) = Any_Fixed
5895 then
5896 if B_Typ = Universal_Fixed
5897 and then not Expected_Type_Is_Any_Real (N)
5898 and then not Nkind_In (Parent (N), N_Type_Conversion,
5899 N_Unchecked_Type_Conversion)
5900 then
5901 Error_Msg_N ("type cannot be determined from context!", N);
5902 Error_Msg_N ("\explicit conversion to result type required", N);
5903
5904 Set_Etype (L, Any_Type);
5905 Set_Etype (R, Any_Type);
5906
5907 else
5908 if Ada_Version = Ada_83
5909 and then Etype (N) = Universal_Fixed
5910 and then not
5911 Nkind_In (Parent (N), N_Type_Conversion,
5912 N_Unchecked_Type_Conversion)
5913 then
5914 Error_Msg_N
5915 ("(Ada 83) fixed-point operation needs explicit "
5916 & "conversion", N);
5917 end if;
5918
5919 -- The expected type is "any real type" in contexts like
5920
5921 -- type T is delta <universal_fixed-expression> ...
5922
5923 -- in which case we need to set the type to Universal_Real
5924 -- so that static expression evaluation will work properly.
5925
5926 if Expected_Type_Is_Any_Real (N) then
5927 Set_Etype (N, Universal_Real);
5928 else
5929 Set_Etype (N, B_Typ);
5930 end if;
5931 end if;
5932
5933 elsif Is_Fixed_Point_Type (B_Typ)
5934 and then (Is_Integer_Or_Universal (L)
5935 or else Nkind (L) = N_Real_Literal
5936 or else Nkind (R) = N_Real_Literal
5937 or else Is_Integer_Or_Universal (R))
5938 then
5939 Set_Etype (N, B_Typ);
5940
5941 elsif Etype (N) = Any_Fixed then
5942
5943 -- If no previous errors, this is only possible if one operand is
5944 -- overloaded and the context is universal. Resolve as such.
5945
5946 Set_Etype (N, B_Typ);
5947 end if;
5948
5949 else
5950 if (TL = Universal_Integer or else TL = Universal_Real)
5951 and then
5952 (TR = Universal_Integer or else TR = Universal_Real)
5953 then
5954 Check_For_Visible_Operator (N, B_Typ);
5955 end if;
5956
5957 -- If the context is Universal_Fixed and the operands are also
5958 -- universal fixed, this is an error, unless there is only one
5959 -- applicable fixed_point type (usually Duration).
5960
5961 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
5962 T := Unique_Fixed_Point_Type (N);
5963
5964 if T = Any_Type then
5965 Set_Etype (N, T);
5966 return;
5967 else
5968 Resolve (L, T);
5969 Resolve (R, T);
5970 end if;
5971
5972 else
5973 Resolve (L, B_Typ);
5974 Resolve (R, B_Typ);
5975 end if;
5976
5977 -- If one of the arguments was resolved to a non-universal type.
5978 -- label the result of the operation itself with the same type.
5979 -- Do the same for the universal argument, if any.
5980
5981 T := Intersect_Types (L, R);
5982 Set_Etype (N, Base_Type (T));
5983 Set_Operand_Type (L);
5984 Set_Operand_Type (R);
5985 end if;
5986
5987 Generate_Operator_Reference (N, Typ);
5988 Analyze_Dimension (N);
5989 Eval_Arithmetic_Op (N);
5990
5991 -- In SPARK, a multiplication or division with operands of fixed point
5992 -- types must be qualified or explicitly converted to identify the
5993 -- result type.
5994
5995 if (Is_Fixed_Point_Type (Etype (L))
5996 or else Is_Fixed_Point_Type (Etype (R)))
5997 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
5998 and then
5999 not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion)
6000 then
6001 Check_SPARK_05_Restriction
6002 ("operation should be qualified or explicitly converted", N);
6003 end if;
6004
6005 -- Set overflow and division checking bit
6006
6007 if Nkind (N) in N_Op then
6008 if not Overflow_Checks_Suppressed (Etype (N)) then
6009 Enable_Overflow_Check (N);
6010 end if;
6011
6012 -- Give warning if explicit division by zero
6013
6014 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
6015 and then not Division_Checks_Suppressed (Etype (N))
6016 then
6017 Rop := Right_Opnd (N);
6018
6019 if Compile_Time_Known_Value (Rop)
6020 and then ((Is_Integer_Type (Etype (Rop))
6021 and then Expr_Value (Rop) = Uint_0)
6022 or else
6023 (Is_Real_Type (Etype (Rop))
6024 and then Expr_Value_R (Rop) = Ureal_0))
6025 then
6026 -- Specialize the warning message according to the operation.
6027 -- When SPARK_Mode is On, force a warning instead of an error
6028 -- in that case, as this likely corresponds to deactivated
6029 -- code. The following warnings are for the case
6030
6031 case Nkind (N) is
6032 when N_Op_Divide =>
6033
6034 -- For division, we have two cases, for float division
6035 -- of an unconstrained float type, on a machine where
6036 -- Machine_Overflows is false, we don't get an exception
6037 -- at run-time, but rather an infinity or Nan. The Nan
6038 -- case is pretty obscure, so just warn about infinities.
6039
6040 if Is_Floating_Point_Type (Typ)
6041 and then not Is_Constrained (Typ)
6042 and then not Machine_Overflows_On_Target
6043 then
6044 Error_Msg_N
6045 ("float division by zero, may generate "
6046 & "'+'/'- infinity??", Right_Opnd (N));
6047
6048 -- For all other cases, we get a Constraint_Error
6049
6050 else
6051 Apply_Compile_Time_Constraint_Error
6052 (N, "division by zero??", CE_Divide_By_Zero,
6053 Loc => Sloc (Right_Opnd (N)),
6054 Warn => SPARK_Mode = On);
6055 end if;
6056
6057 when N_Op_Rem =>
6058 Apply_Compile_Time_Constraint_Error
6059 (N, "rem with zero divisor??", CE_Divide_By_Zero,
6060 Loc => Sloc (Right_Opnd (N)),
6061 Warn => SPARK_Mode = On);
6062
6063 when N_Op_Mod =>
6064 Apply_Compile_Time_Constraint_Error
6065 (N, "mod with zero divisor??", CE_Divide_By_Zero,
6066 Loc => Sloc (Right_Opnd (N)),
6067 Warn => SPARK_Mode = On);
6068
6069 -- Division by zero can only happen with division, rem,
6070 -- and mod operations.
6071
6072 when others =>
6073 raise Program_Error;
6074 end case;
6075
6076 -- In GNATprove mode, we enable the division check so that
6077 -- GNATprove will issue a message if it cannot be proved.
6078
6079 if GNATprove_Mode then
6080 Activate_Division_Check (N);
6081 end if;
6082
6083 -- Otherwise just set the flag to check at run time
6084
6085 else
6086 Activate_Division_Check (N);
6087 end if;
6088 end if;
6089
6090 -- If Restriction No_Implicit_Conditionals is active, then it is
6091 -- violated if either operand can be negative for mod, or for rem
6092 -- if both operands can be negative.
6093
6094 if Restriction_Check_Required (No_Implicit_Conditionals)
6095 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
6096 then
6097 declare
6098 Lo : Uint;
6099 Hi : Uint;
6100 OK : Boolean;
6101
6102 LNeg : Boolean;
6103 RNeg : Boolean;
6104 -- Set if corresponding operand might be negative
6105
6106 begin
6107 Determine_Range
6108 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
6109 LNeg := (not OK) or else Lo < 0;
6110
6111 Determine_Range
6112 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
6113 RNeg := (not OK) or else Lo < 0;
6114
6115 -- Check if we will be generating conditionals. There are two
6116 -- cases where that can happen, first for REM, the only case
6117 -- is largest negative integer mod -1, where the division can
6118 -- overflow, but we still have to give the right result. The
6119 -- front end generates a test for this annoying case. Here we
6120 -- just test if both operands can be negative (that's what the
6121 -- expander does, so we match its logic here).
6122
6123 -- The second case is mod where either operand can be negative.
6124 -- In this case, the back end has to generate additional tests.
6125
6126 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
6127 or else
6128 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
6129 then
6130 Check_Restriction (No_Implicit_Conditionals, N);
6131 end if;
6132 end;
6133 end if;
6134 end if;
6135
6136 Check_Unset_Reference (L);
6137 Check_Unset_Reference (R);
6138 end Resolve_Arithmetic_Op;
6139
6140 ------------------
6141 -- Resolve_Call --
6142 ------------------
6143
6144 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
6145 function Same_Or_Aliased_Subprograms
6146 (S : Entity_Id;
6147 E : Entity_Id) return Boolean;
6148 -- Returns True if the subprogram entity S is the same as E or else
6149 -- S is an alias of E.
6150
6151 ---------------------------------
6152 -- Same_Or_Aliased_Subprograms --
6153 ---------------------------------
6154
6155 function Same_Or_Aliased_Subprograms
6156 (S : Entity_Id;
6157 E : Entity_Id) return Boolean
6158 is
6159 Subp_Alias : constant Entity_Id := Alias (S);
6160 begin
6161 return S = E or else (Present (Subp_Alias) and then Subp_Alias = E);
6162 end Same_Or_Aliased_Subprograms;
6163
6164 -- Local variables
6165
6166 Loc : constant Source_Ptr := Sloc (N);
6167 Subp : constant Node_Id := Name (N);
6168 Body_Id : Entity_Id;
6169 I : Interp_Index;
6170 It : Interp;
6171 Nam : Entity_Id;
6172 Nam_Decl : Node_Id;
6173 Nam_UA : Entity_Id;
6174 Norm_OK : Boolean;
6175 Rtype : Entity_Id;
6176 Scop : Entity_Id;
6177
6178 -- Start of processing for Resolve_Call
6179
6180 begin
6181 -- Preserve relevant elaboration-related attributes of the context which
6182 -- are no longer available or very expensive to recompute once analysis,
6183 -- resolution, and expansion are over.
6184
6185 Mark_Elaboration_Attributes
6186 (N_Id => N,
6187 Checks => True,
6188 Modes => True,
6189 Warnings => True);
6190
6191 -- The context imposes a unique interpretation with type Typ on a
6192 -- procedure or function call. Find the entity of the subprogram that
6193 -- yields the expected type, and propagate the corresponding formal
6194 -- constraints on the actuals. The caller has established that an
6195 -- interpretation exists, and emitted an error if not unique.
6196
6197 -- First deal with the case of a call to an access-to-subprogram,
6198 -- dereference made explicit in Analyze_Call.
6199
6200 if Ekind (Etype (Subp)) = E_Subprogram_Type then
6201 if not Is_Overloaded (Subp) then
6202 Nam := Etype (Subp);
6203
6204 else
6205 -- Find the interpretation whose type (a subprogram type) has a
6206 -- return type that is compatible with the context. Analysis of
6207 -- the node has established that one exists.
6208
6209 Nam := Empty;
6210
6211 Get_First_Interp (Subp, I, It);
6212 while Present (It.Typ) loop
6213 if Covers (Typ, Etype (It.Typ)) then
6214 Nam := It.Typ;
6215 exit;
6216 end if;
6217
6218 Get_Next_Interp (I, It);
6219 end loop;
6220
6221 if No (Nam) then
6222 raise Program_Error;
6223 end if;
6224 end if;
6225
6226 -- If the prefix is not an entity, then resolve it
6227
6228 if not Is_Entity_Name (Subp) then
6229 Resolve (Subp, Nam);
6230 end if;
6231
6232 -- For an indirect call, we always invalidate checks, since we do not
6233 -- know whether the subprogram is local or global. Yes we could do
6234 -- better here, e.g. by knowing that there are no local subprograms,
6235 -- but it does not seem worth the effort. Similarly, we kill all
6236 -- knowledge of current constant values.
6237
6238 Kill_Current_Values;
6239
6240 -- If this is a procedure call which is really an entry call, do
6241 -- the conversion of the procedure call to an entry call. Protected
6242 -- operations use the same circuitry because the name in the call
6243 -- can be an arbitrary expression with special resolution rules.
6244
6245 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
6246 or else (Is_Entity_Name (Subp)
6247 and then Ekind_In (Entity (Subp), E_Entry, E_Entry_Family))
6248 then
6249 Resolve_Entry_Call (N, Typ);
6250
6251 if Legacy_Elaboration_Checks then
6252 Check_Elab_Call (N);
6253 end if;
6254
6255 -- Annotate the tree by creating a call marker in case the original
6256 -- call is transformed by expansion. The call marker is automatically
6257 -- saved for later examination by the ABE Processing phase.
6258
6259 Build_Call_Marker (N);
6260
6261 -- Kill checks and constant values, as above for indirect case
6262 -- Who knows what happens when another task is activated?
6263
6264 Kill_Current_Values;
6265 return;
6266
6267 -- Normal subprogram call with name established in Resolve
6268
6269 elsif not (Is_Type (Entity (Subp))) then
6270 Nam := Entity (Subp);
6271 Set_Entity_With_Checks (Subp, Nam);
6272
6273 -- Otherwise we must have the case of an overloaded call
6274
6275 else
6276 pragma Assert (Is_Overloaded (Subp));
6277
6278 -- Initialize Nam to prevent warning (we know it will be assigned
6279 -- in the loop below, but the compiler does not know that).
6280
6281 Nam := Empty;
6282
6283 Get_First_Interp (Subp, I, It);
6284 while Present (It.Typ) loop
6285 if Covers (Typ, It.Typ) then
6286 Nam := It.Nam;
6287 Set_Entity_With_Checks (Subp, Nam);
6288 exit;
6289 end if;
6290
6291 Get_Next_Interp (I, It);
6292 end loop;
6293 end if;
6294
6295 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
6296 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
6297 and then Nkind (Subp) /= N_Explicit_Dereference
6298 and then Present (Parameter_Associations (N))
6299 then
6300 -- The prefix is a parameterless function call that returns an access
6301 -- to subprogram. If parameters are present in the current call, add
6302 -- add an explicit dereference. We use the base type here because
6303 -- within an instance these may be subtypes.
6304
6305 -- The dereference is added either in Analyze_Call or here. Should
6306 -- be consolidated ???
6307
6308 Set_Is_Overloaded (Subp, False);
6309 Set_Etype (Subp, Etype (Nam));
6310 Insert_Explicit_Dereference (Subp);
6311 Nam := Designated_Type (Etype (Nam));
6312 Resolve (Subp, Nam);
6313 end if;
6314
6315 -- Check that a call to Current_Task does not occur in an entry body
6316
6317 if Is_RTE (Nam, RE_Current_Task) then
6318 declare
6319 P : Node_Id;
6320
6321 begin
6322 P := N;
6323 loop
6324 P := Parent (P);
6325
6326 -- Exclude calls that occur within the default of a formal
6327 -- parameter of the entry, since those are evaluated outside
6328 -- of the body.
6329
6330 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
6331
6332 if Nkind (P) = N_Entry_Body
6333 or else (Nkind (P) = N_Subprogram_Body
6334 and then Is_Entry_Barrier_Function (P))
6335 then
6336 Rtype := Etype (N);
6337 Error_Msg_Warn := SPARK_Mode /= On;
6338 Error_Msg_NE
6339 ("& should not be used in entry body (RM C.7(17))<<",
6340 N, Nam);
6341 Error_Msg_NE ("\Program_Error [<<", N, Nam);
6342 Rewrite (N,
6343 Make_Raise_Program_Error (Loc,
6344 Reason => PE_Current_Task_In_Entry_Body));
6345 Set_Etype (N, Rtype);
6346 return;
6347 end if;
6348 end loop;
6349 end;
6350 end if;
6351
6352 -- Check that a procedure call does not occur in the context of the
6353 -- entry call statement of a conditional or timed entry call. Note that
6354 -- the case of a call to a subprogram renaming of an entry will also be
6355 -- rejected. The test for N not being an N_Entry_Call_Statement is
6356 -- defensive, covering the possibility that the processing of entry
6357 -- calls might reach this point due to later modifications of the code
6358 -- above.
6359
6360 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6361 and then Nkind (N) /= N_Entry_Call_Statement
6362 and then Entry_Call_Statement (Parent (N)) = N
6363 then
6364 if Ada_Version < Ada_2005 then
6365 Error_Msg_N ("entry call required in select statement", N);
6366
6367 -- Ada 2005 (AI-345): If a procedure_call_statement is used
6368 -- for a procedure_or_entry_call, the procedure_name or
6369 -- procedure_prefix of the procedure_call_statement shall denote
6370 -- an entry renamed by a procedure, or (a view of) a primitive
6371 -- subprogram of a limited interface whose first parameter is
6372 -- a controlling parameter.
6373
6374 elsif Nkind (N) = N_Procedure_Call_Statement
6375 and then not Is_Renamed_Entry (Nam)
6376 and then not Is_Controlling_Limited_Procedure (Nam)
6377 then
6378 Error_Msg_N
6379 ("entry call or dispatching primitive of interface required", N);
6380 end if;
6381 end if;
6382
6383 -- If the SPARK_05 restriction is active, we are not allowed
6384 -- to have a call to a subprogram before we see its completion.
6385
6386 if not Has_Completion (Nam)
6387 and then Restriction_Check_Required (SPARK_05)
6388
6389 -- Don't flag strange internal calls
6390
6391 and then Comes_From_Source (N)
6392 and then Comes_From_Source (Nam)
6393
6394 -- Only flag calls in extended main source
6395
6396 and then In_Extended_Main_Source_Unit (Nam)
6397 and then In_Extended_Main_Source_Unit (N)
6398
6399 -- Exclude enumeration literals from this processing
6400
6401 and then Ekind (Nam) /= E_Enumeration_Literal
6402 then
6403 Check_SPARK_05_Restriction
6404 ("call to subprogram cannot appear before its body", N);
6405 end if;
6406
6407 -- Check that this is not a call to a protected procedure or entry from
6408 -- within a protected function.
6409
6410 Check_Internal_Protected_Use (N, Nam);
6411
6412 -- Freeze the subprogram name if not in a spec-expression. Note that
6413 -- we freeze procedure calls as well as function calls. Procedure calls
6414 -- are not frozen according to the rules (RM 13.14(14)) because it is
6415 -- impossible to have a procedure call to a non-frozen procedure in
6416 -- pure Ada, but in the code that we generate in the expander, this
6417 -- rule needs extending because we can generate procedure calls that
6418 -- need freezing.
6419
6420 -- In Ada 2012, expression functions may be called within pre/post
6421 -- conditions of subsequent functions or expression functions. Such
6422 -- calls do not freeze when they appear within generated bodies,
6423 -- (including the body of another expression function) which would
6424 -- place the freeze node in the wrong scope. An expression function
6425 -- is frozen in the usual fashion, by the appearance of a real body,
6426 -- or at the end of a declarative part. However an implicit call to
6427 -- an expression function may appear when it is part of a default
6428 -- expression in a call to an initialization procedure, and must be
6429 -- frozen now, even if the body is inserted at a later point.
6430 -- Otherwise, the call freezes the expression if expander is active,
6431 -- for example as part of an object declaration.
6432
6433 if Is_Entity_Name (Subp)
6434 and then not In_Spec_Expression
6435 and then not Is_Expression_Function_Or_Completion (Current_Scope)
6436 and then
6437 (not Is_Expression_Function_Or_Completion (Entity (Subp))
6438 or else Expander_Active)
6439 then
6440 if Is_Expression_Function (Entity (Subp)) then
6441
6442 -- Force freeze of expression function in call
6443
6444 Set_Comes_From_Source (Subp, True);
6445 Set_Must_Not_Freeze (Subp, False);
6446 end if;
6447
6448 Freeze_Expression (Subp);
6449 end if;
6450
6451 -- For a predefined operator, the type of the result is the type imposed
6452 -- by context, except for a predefined operation on universal fixed.
6453 -- Otherwise the type of the call is the type returned by the subprogram
6454 -- being called.
6455
6456 if Is_Predefined_Op (Nam) then
6457 if Etype (N) /= Universal_Fixed then
6458 Set_Etype (N, Typ);
6459 end if;
6460
6461 -- If the subprogram returns an array type, and the context requires the
6462 -- component type of that array type, the node is really an indexing of
6463 -- the parameterless call. Resolve as such. A pathological case occurs
6464 -- when the type of the component is an access to the array type. In
6465 -- this case the call is truly ambiguous. If the call is to an intrinsic
6466 -- subprogram, it can't be an indexed component. This check is necessary
6467 -- because if it's Unchecked_Conversion, and we have "type T_Ptr is
6468 -- access T;" and "type T is array (...) of T_Ptr;" (i.e. an array of
6469 -- pointers to the same array), the compiler gets confused and does an
6470 -- infinite recursion.
6471
6472 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
6473 and then
6474 ((Is_Array_Type (Etype (Nam))
6475 and then Covers (Typ, Component_Type (Etype (Nam))))
6476 or else
6477 (Is_Access_Type (Etype (Nam))
6478 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6479 and then
6480 Covers (Typ, Component_Type (Designated_Type (Etype (Nam))))
6481 and then not Is_Intrinsic_Subprogram (Entity (Subp))))
6482 then
6483 declare
6484 Index_Node : Node_Id;
6485 New_Subp : Node_Id;
6486 Ret_Type : constant Entity_Id := Etype (Nam);
6487
6488 begin
6489 -- If this is a parameterless call there is no ambiguity and the
6490 -- call has the type of the function.
6491
6492 if No (First_Actual (N)) then
6493 Set_Etype (N, Etype (Nam));
6494
6495 if Present (First_Formal (Nam)) then
6496 Resolve_Actuals (N, Nam);
6497 end if;
6498
6499 -- Annotate the tree by creating a call marker in case the
6500 -- original call is transformed by expansion. The call marker
6501 -- is automatically saved for later examination by the ABE
6502 -- Processing phase.
6503
6504 Build_Call_Marker (N);
6505
6506 elsif Is_Access_Type (Ret_Type)
6507
6508 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
6509 then
6510 Error_Msg_N
6511 ("cannot disambiguate function call and indexing", N);
6512 else
6513 New_Subp := Relocate_Node (Subp);
6514
6515 -- The called entity may be an explicit dereference, in which
6516 -- case there is no entity to set.
6517
6518 if Nkind (New_Subp) /= N_Explicit_Dereference then
6519 Set_Entity (Subp, Nam);
6520 end if;
6521
6522 if (Is_Array_Type (Ret_Type)
6523 and then Component_Type (Ret_Type) /= Any_Type)
6524 or else
6525 (Is_Access_Type (Ret_Type)
6526 and then
6527 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
6528 then
6529 if Needs_No_Actuals (Nam) then
6530
6531 -- Indexed call to a parameterless function
6532
6533 Index_Node :=
6534 Make_Indexed_Component (Loc,
6535 Prefix =>
6536 Make_Function_Call (Loc, Name => New_Subp),
6537 Expressions => Parameter_Associations (N));
6538 else
6539 -- An Ada 2005 prefixed call to a primitive operation
6540 -- whose first parameter is the prefix. This prefix was
6541 -- prepended to the parameter list, which is actually a
6542 -- list of indexes. Remove the prefix in order to build
6543 -- the proper indexed component.
6544
6545 Index_Node :=
6546 Make_Indexed_Component (Loc,
6547 Prefix =>
6548 Make_Function_Call (Loc,
6549 Name => New_Subp,
6550 Parameter_Associations =>
6551 New_List
6552 (Remove_Head (Parameter_Associations (N)))),
6553 Expressions => Parameter_Associations (N));
6554 end if;
6555
6556 -- Preserve the parenthesis count of the node
6557
6558 Set_Paren_Count (Index_Node, Paren_Count (N));
6559
6560 -- Since we are correcting a node classification error made
6561 -- by the parser, we call Replace rather than Rewrite.
6562
6563 Replace (N, Index_Node);
6564
6565 Set_Etype (Prefix (N), Ret_Type);
6566 Set_Etype (N, Typ);
6567 Resolve_Indexed_Component (N, Typ);
6568
6569 if Legacy_Elaboration_Checks then
6570 Check_Elab_Call (Prefix (N));
6571 end if;
6572
6573 -- Annotate the tree by creating a call marker in case
6574 -- the original call is transformed by expansion. The call
6575 -- marker is automatically saved for later examination by
6576 -- the ABE Processing phase.
6577
6578 Build_Call_Marker (Prefix (N));
6579 end if;
6580 end if;
6581
6582 return;
6583 end;
6584
6585 else
6586 -- If the called function is not declared in the main unit and it
6587 -- returns the limited view of type then use the available view (as
6588 -- is done in Try_Object_Operation) to prevent back-end confusion;
6589 -- for the function entity itself. The call must appear in a context
6590 -- where the nonlimited view is available. If the function entity is
6591 -- in the extended main unit then no action is needed, because the
6592 -- back end handles this case. In either case the type of the call
6593 -- is the nonlimited view.
6594
6595 if From_Limited_With (Etype (Nam))
6596 and then Present (Available_View (Etype (Nam)))
6597 then
6598 Set_Etype (N, Available_View (Etype (Nam)));
6599
6600 if not In_Extended_Main_Code_Unit (Nam) then
6601 Set_Etype (Nam, Available_View (Etype (Nam)));
6602 end if;
6603
6604 else
6605 Set_Etype (N, Etype (Nam));
6606 end if;
6607 end if;
6608
6609 -- In the case where the call is to an overloaded subprogram, Analyze
6610 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
6611 -- such a case Normalize_Actuals needs to be called once more to order
6612 -- the actuals correctly. Otherwise the call will have the ordering
6613 -- given by the last overloaded subprogram whether this is the correct
6614 -- one being called or not.
6615
6616 if Is_Overloaded (Subp) then
6617 Normalize_Actuals (N, Nam, False, Norm_OK);
6618 pragma Assert (Norm_OK);
6619 end if;
6620
6621 -- In any case, call is fully resolved now. Reset Overload flag, to
6622 -- prevent subsequent overload resolution if node is analyzed again
6623
6624 Set_Is_Overloaded (Subp, False);
6625 Set_Is_Overloaded (N, False);
6626
6627 -- A Ghost entity must appear in a specific context
6628
6629 if Is_Ghost_Entity (Nam) and then Comes_From_Source (N) then
6630 Check_Ghost_Context (Nam, N);
6631 end if;
6632
6633 -- If we are calling the current subprogram from immediately within its
6634 -- body, then that is the case where we can sometimes detect cases of
6635 -- infinite recursion statically. Do not try this in case restriction
6636 -- No_Recursion is in effect anyway, and do it only for source calls.
6637
6638 if Comes_From_Source (N) then
6639 Scop := Current_Scope;
6640
6641 -- Check violation of SPARK_05 restriction which does not permit
6642 -- a subprogram body to contain a call to the subprogram directly.
6643
6644 if Restriction_Check_Required (SPARK_05)
6645 and then Same_Or_Aliased_Subprograms (Nam, Scop)
6646 then
6647 Check_SPARK_05_Restriction
6648 ("subprogram may not contain direct call to itself", N);
6649 end if;
6650
6651 -- Issue warning for possible infinite recursion in the absence
6652 -- of the No_Recursion restriction.
6653
6654 if Same_Or_Aliased_Subprograms (Nam, Scop)
6655 and then not Restriction_Active (No_Recursion)
6656 and then Check_Infinite_Recursion (N)
6657 then
6658 -- Here we detected and flagged an infinite recursion, so we do
6659 -- not need to test the case below for further warnings. Also we
6660 -- are all done if we now have a raise SE node.
6661
6662 if Nkind (N) = N_Raise_Storage_Error then
6663 return;
6664 end if;
6665
6666 -- If call is to immediately containing subprogram, then check for
6667 -- the case of a possible run-time detectable infinite recursion.
6668
6669 else
6670 Scope_Loop : while Scop /= Standard_Standard loop
6671 if Same_Or_Aliased_Subprograms (Nam, Scop) then
6672
6673 -- Although in general case, recursion is not statically
6674 -- checkable, the case of calling an immediately containing
6675 -- subprogram is easy to catch.
6676
6677 if not Is_Ignored_Ghost_Entity (Nam) then
6678 Check_Restriction (No_Recursion, N);
6679 end if;
6680
6681 -- If the recursive call is to a parameterless subprogram,
6682 -- then even if we can't statically detect infinite
6683 -- recursion, this is pretty suspicious, and we output a
6684 -- warning. Furthermore, we will try later to detect some
6685 -- cases here at run time by expanding checking code (see
6686 -- Detect_Infinite_Recursion in package Exp_Ch6).
6687
6688 -- If the recursive call is within a handler, do not emit a
6689 -- warning, because this is a common idiom: loop until input
6690 -- is correct, catch illegal input in handler and restart.
6691
6692 if No (First_Formal (Nam))
6693 and then Etype (Nam) = Standard_Void_Type
6694 and then not Error_Posted (N)
6695 and then Nkind (Parent (N)) /= N_Exception_Handler
6696 then
6697 -- For the case of a procedure call. We give the message
6698 -- only if the call is the first statement in a sequence
6699 -- of statements, or if all previous statements are
6700 -- simple assignments. This is simply a heuristic to
6701 -- decrease false positives, without losing too many good
6702 -- warnings. The idea is that these previous statements
6703 -- may affect global variables the procedure depends on.
6704 -- We also exclude raise statements, that may arise from
6705 -- constraint checks and are probably unrelated to the
6706 -- intended control flow.
6707
6708 if Nkind (N) = N_Procedure_Call_Statement
6709 and then Is_List_Member (N)
6710 then
6711 declare
6712 P : Node_Id;
6713 begin
6714 P := Prev (N);
6715 while Present (P) loop
6716 if not Nkind_In (P, N_Assignment_Statement,
6717 N_Raise_Constraint_Error)
6718 then
6719 exit Scope_Loop;
6720 end if;
6721
6722 Prev (P);
6723 end loop;
6724 end;
6725 end if;
6726
6727 -- Do not give warning if we are in a conditional context
6728
6729 declare
6730 K : constant Node_Kind := Nkind (Parent (N));
6731 begin
6732 if (K = N_Loop_Statement
6733 and then Present (Iteration_Scheme (Parent (N))))
6734 or else K = N_If_Statement
6735 or else K = N_Elsif_Part
6736 or else K = N_Case_Statement_Alternative
6737 then
6738 exit Scope_Loop;
6739 end if;
6740 end;
6741
6742 -- Here warning is to be issued
6743
6744 Set_Has_Recursive_Call (Nam);
6745 Error_Msg_Warn := SPARK_Mode /= On;
6746 Error_Msg_N ("possible infinite recursion<<!", N);
6747 Error_Msg_N ("\Storage_Error ]<<!", N);
6748 end if;
6749
6750 exit Scope_Loop;
6751 end if;
6752
6753 Scop := Scope (Scop);
6754 end loop Scope_Loop;
6755 end if;
6756 end if;
6757
6758 -- Check obsolescent reference to Ada.Characters.Handling subprogram
6759
6760 Check_Obsolescent_2005_Entity (Nam, Subp);
6761
6762 -- If subprogram name is a predefined operator, it was given in
6763 -- functional notation. Replace call node with operator node, so
6764 -- that actuals can be resolved appropriately.
6765
6766 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
6767 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
6768 return;
6769
6770 elsif Present (Alias (Nam))
6771 and then Is_Predefined_Op (Alias (Nam))
6772 then
6773 Resolve_Actuals (N, Nam);
6774 Make_Call_Into_Operator (N, Typ, Alias (Nam));
6775 return;
6776 end if;
6777
6778 -- Create a transient scope if the resulting type requires it
6779
6780 -- There are several notable exceptions:
6781
6782 -- a) In init procs, the transient scope overhead is not needed, and is
6783 -- even incorrect when the call is a nested initialization call for a
6784 -- component whose expansion may generate adjust calls. However, if the
6785 -- call is some other procedure call within an initialization procedure
6786 -- (for example a call to Create_Task in the init_proc of the task
6787 -- run-time record) a transient scope must be created around this call.
6788
6789 -- b) Enumeration literal pseudo-calls need no transient scope
6790
6791 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
6792 -- functions) do not use the secondary stack even though the return
6793 -- type may be unconstrained.
6794
6795 -- d) Calls to a build-in-place function, since such functions may
6796 -- allocate their result directly in a target object, and cases where
6797 -- the result does get allocated in the secondary stack are checked for
6798 -- within the specialized Exp_Ch6 procedures for expanding those
6799 -- build-in-place calls.
6800
6801 -- e) Calls to inlinable expression functions do not use the secondary
6802 -- stack (since the call will be replaced by its returned object).
6803
6804 -- f) If the subprogram is marked Inline_Always, then even if it returns
6805 -- an unconstrained type the call does not require use of the secondary
6806 -- stack. However, inlining will only take place if the body to inline
6807 -- is already present. It may not be available if e.g. the subprogram is
6808 -- declared in a child instance.
6809
6810 if Is_Inlined (Nam)
6811 and then Has_Pragma_Inline (Nam)
6812 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
6813 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
6814 then
6815 null;
6816
6817 elsif Ekind (Nam) = E_Enumeration_Literal
6818 or else Is_Build_In_Place_Function (Nam)
6819 or else Is_Intrinsic_Subprogram (Nam)
6820 or else Is_Inlinable_Expression_Function (Nam)
6821 then
6822 null;
6823
6824 -- A return statement from an ignored Ghost function does not use the
6825 -- secondary stack (or any other one).
6826
6827 elsif Expander_Active
6828 and then Ekind_In (Nam, E_Function, E_Subprogram_Type)
6829 and then Requires_Transient_Scope (Etype (Nam))
6830 and then not Is_Ignored_Ghost_Entity (Nam)
6831 then
6832 Establish_Transient_Scope (N, Manage_Sec_Stack => True);
6833
6834 -- If the call appears within the bounds of a loop, it will be
6835 -- rewritten and reanalyzed, nothing left to do here.
6836
6837 if Nkind (N) /= N_Function_Call then
6838 return;
6839 end if;
6840 end if;
6841
6842 -- A protected function cannot be called within the definition of the
6843 -- enclosing protected type, unless it is part of a pre/postcondition
6844 -- on another protected operation. This may appear in the entry wrapper
6845 -- created for an entry with preconditions.
6846
6847 if Is_Protected_Type (Scope (Nam))
6848 and then In_Open_Scopes (Scope (Nam))
6849 and then not Has_Completion (Scope (Nam))
6850 and then not In_Spec_Expression
6851 and then not Is_Entry_Wrapper (Current_Scope)
6852 then
6853 Error_Msg_NE
6854 ("& cannot be called before end of protected definition", N, Nam);
6855 end if;
6856
6857 -- Propagate interpretation to actuals, and add default expressions
6858 -- where needed.
6859
6860 if Present (First_Formal (Nam)) then
6861 Resolve_Actuals (N, Nam);
6862
6863 -- Overloaded literals are rewritten as function calls, for purpose of
6864 -- resolution. After resolution, we can replace the call with the
6865 -- literal itself.
6866
6867 elsif Ekind (Nam) = E_Enumeration_Literal then
6868 Copy_Node (Subp, N);
6869 Resolve_Entity_Name (N, Typ);
6870
6871 -- Avoid validation, since it is a static function call
6872
6873 Generate_Reference (Nam, Subp);
6874 return;
6875 end if;
6876
6877 -- If the subprogram is not global, then kill all saved values and
6878 -- checks. This is a bit conservative, since in many cases we could do
6879 -- better, but it is not worth the effort. Similarly, we kill constant
6880 -- values. However we do not need to do this for internal entities
6881 -- (unless they are inherited user-defined subprograms), since they
6882 -- are not in the business of molesting local values.
6883
6884 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
6885 -- kill all checks and values for calls to global subprograms. This
6886 -- takes care of the case where an access to a local subprogram is
6887 -- taken, and could be passed directly or indirectly and then called
6888 -- from almost any context.
6889
6890 -- Note: we do not do this step till after resolving the actuals. That
6891 -- way we still take advantage of the current value information while
6892 -- scanning the actuals.
6893
6894 -- We suppress killing values if we are processing the nodes associated
6895 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
6896 -- type kills all the values as part of analyzing the code that
6897 -- initializes the dispatch tables.
6898
6899 if Inside_Freezing_Actions = 0
6900 and then (not Is_Library_Level_Entity (Nam)
6901 or else Suppress_Value_Tracking_On_Call
6902 (Nearest_Dynamic_Scope (Current_Scope)))
6903 and then (Comes_From_Source (Nam)
6904 or else (Present (Alias (Nam))
6905 and then Comes_From_Source (Alias (Nam))))
6906 then
6907 Kill_Current_Values;
6908 end if;
6909
6910 -- If we are warning about unread OUT parameters, this is the place to
6911 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
6912 -- after the above call to Kill_Current_Values (since that call clears
6913 -- the Last_Assignment field of all local variables).
6914
6915 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
6916 and then Comes_From_Source (N)
6917 and then In_Extended_Main_Source_Unit (N)
6918 then
6919 declare
6920 F : Entity_Id;
6921 A : Node_Id;
6922
6923 begin
6924 F := First_Formal (Nam);
6925 A := First_Actual (N);
6926 while Present (F) and then Present (A) loop
6927 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
6928 and then Warn_On_Modified_As_Out_Parameter (F)
6929 and then Is_Entity_Name (A)
6930 and then Present (Entity (A))
6931 and then Comes_From_Source (N)
6932 and then Safe_To_Capture_Value (N, Entity (A))
6933 then
6934 Set_Last_Assignment (Entity (A), A);
6935 end if;
6936
6937 Next_Formal (F);
6938 Next_Actual (A);
6939 end loop;
6940 end;
6941 end if;
6942
6943 -- If the subprogram is a primitive operation, check whether or not
6944 -- it is a correct dispatching call.
6945
6946 if Is_Overloadable (Nam)
6947 and then Is_Dispatching_Operation (Nam)
6948 then
6949 Check_Dispatching_Call (N);
6950
6951 elsif Ekind (Nam) /= E_Subprogram_Type
6952 and then Is_Abstract_Subprogram (Nam)
6953 and then not In_Instance
6954 then
6955 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
6956 end if;
6957
6958 -- If this is a dispatching call, generate the appropriate reference,
6959 -- for better source navigation in GNAT Studio.
6960
6961 if Is_Overloadable (Nam)
6962 and then Present (Controlling_Argument (N))
6963 then
6964 Generate_Reference (Nam, Subp, 'R');
6965
6966 -- Normal case, not a dispatching call: generate a call reference
6967
6968 else
6969 Generate_Reference (Nam, Subp, 's');
6970 end if;
6971
6972 if Is_Intrinsic_Subprogram (Nam) then
6973 Check_Intrinsic_Call (N);
6974 end if;
6975
6976 -- Check for violation of restriction No_Specific_Termination_Handlers
6977 -- and warn on a potentially blocking call to Abort_Task.
6978
6979 if Restriction_Check_Required (No_Specific_Termination_Handlers)
6980 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
6981 or else
6982 Is_RTE (Nam, RE_Specific_Handler))
6983 then
6984 Check_Restriction (No_Specific_Termination_Handlers, N);
6985
6986 elsif Is_RTE (Nam, RE_Abort_Task) then
6987 Check_Potentially_Blocking_Operation (N);
6988 end if;
6989
6990 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
6991 -- timing event violates restriction No_Relative_Delay (AI-0211). We
6992 -- need to check the second argument to determine whether it is an
6993 -- absolute or relative timing event.
6994
6995 if Restriction_Check_Required (No_Relative_Delay)
6996 and then Is_RTE (Nam, RE_Set_Handler)
6997 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
6998 then
6999 Check_Restriction (No_Relative_Delay, N);
7000 end if;
7001
7002 -- Issue an error for a call to an eliminated subprogram. This routine
7003 -- will not perform the check if the call appears within a default
7004 -- expression.
7005
7006 Check_For_Eliminated_Subprogram (Subp, Nam);
7007
7008 -- In formal mode, the primitive operations of a tagged type or type
7009 -- extension do not include functions that return the tagged type.
7010
7011 if Nkind (N) = N_Function_Call
7012 and then Is_Tagged_Type (Etype (N))
7013 and then Is_Entity_Name (Name (N))
7014 and then Is_Inherited_Operation_For_Type (Entity (Name (N)), Etype (N))
7015 then
7016 Check_SPARK_05_Restriction ("function not inherited", N);
7017 end if;
7018
7019 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
7020 -- class-wide and the call dispatches on result in a context that does
7021 -- not provide a tag, the call raises Program_Error.
7022
7023 if Nkind (N) = N_Function_Call
7024 and then In_Instance
7025 and then Is_Generic_Actual_Type (Typ)
7026 and then Is_Class_Wide_Type (Typ)
7027 and then Has_Controlling_Result (Nam)
7028 and then Nkind (Parent (N)) = N_Object_Declaration
7029 then
7030 -- Verify that none of the formals are controlling
7031
7032 declare
7033 Call_OK : Boolean := False;
7034 F : Entity_Id;
7035
7036 begin
7037 F := First_Formal (Nam);
7038 while Present (F) loop
7039 if Is_Controlling_Formal (F) then
7040 Call_OK := True;
7041 exit;
7042 end if;
7043
7044 Next_Formal (F);
7045 end loop;
7046
7047 if not Call_OK then
7048 Error_Msg_Warn := SPARK_Mode /= On;
7049 Error_Msg_N ("!cannot determine tag of result<<", N);
7050 Error_Msg_N ("\Program_Error [<<!", N);
7051 Insert_Action (N,
7052 Make_Raise_Program_Error (Sloc (N),
7053 Reason => PE_Explicit_Raise));
7054 end if;
7055 end;
7056 end if;
7057
7058 -- Check for calling a function with OUT or IN OUT parameter when the
7059 -- calling context (us right now) is not Ada 2012, so does not allow
7060 -- OUT or IN OUT parameters in function calls. Functions declared in
7061 -- a predefined unit are OK, as they may be called indirectly from a
7062 -- user-declared instantiation.
7063
7064 if Ada_Version < Ada_2012
7065 and then Ekind (Nam) = E_Function
7066 and then Has_Out_Or_In_Out_Parameter (Nam)
7067 and then not In_Predefined_Unit (Nam)
7068 then
7069 Error_Msg_NE ("& has at least one OUT or `IN OUT` parameter", N, Nam);
7070 Error_Msg_N ("\call to this function only allowed in Ada 2012", N);
7071 end if;
7072
7073 -- Check the dimensions of the actuals in the call. For function calls,
7074 -- propagate the dimensions from the returned type to N.
7075
7076 Analyze_Dimension_Call (N, Nam);
7077
7078 -- All done, evaluate call and deal with elaboration issues
7079
7080 Eval_Call (N);
7081
7082 if Legacy_Elaboration_Checks then
7083 Check_Elab_Call (N);
7084 end if;
7085
7086 -- Annotate the tree by creating a call marker in case the original call
7087 -- is transformed by expansion. The call marker is automatically saved
7088 -- for later examination by the ABE Processing phase.
7089
7090 Build_Call_Marker (N);
7091
7092 Mark_Use_Clauses (Subp);
7093
7094 Warn_On_Overlapping_Actuals (Nam, N);
7095
7096 -- In GNATprove mode, expansion is disabled, but we want to inline some
7097 -- subprograms to facilitate formal verification. Indirect calls through
7098 -- a subprogram type or within a generic cannot be inlined. Inlining is
7099 -- performed only for calls subject to SPARK_Mode on.
7100
7101 if GNATprove_Mode
7102 and then SPARK_Mode = On
7103 and then Is_Overloadable (Nam)
7104 and then not Inside_A_Generic
7105 then
7106 Nam_UA := Ultimate_Alias (Nam);
7107 Nam_Decl := Unit_Declaration_Node (Nam_UA);
7108
7109 if Nkind (Nam_Decl) = N_Subprogram_Declaration then
7110 Body_Id := Corresponding_Body (Nam_Decl);
7111
7112 -- Nothing to do if the subprogram is not eligible for inlining in
7113 -- GNATprove mode, or inlining is disabled with switch -gnatdm
7114
7115 if not Is_Inlined_Always (Nam_UA)
7116 or else not Can_Be_Inlined_In_GNATprove_Mode (Nam_UA, Body_Id)
7117 or else Debug_Flag_M
7118 then
7119 null;
7120
7121 -- Calls cannot be inlined inside assertions, as GNATprove treats
7122 -- assertions as logic expressions. Only issue a message when the
7123 -- body has been seen, otherwise this leads to spurious messages
7124 -- on expression functions.
7125
7126 elsif In_Assertion_Expr /= 0 then
7127 if Present (Body_Id) then
7128 Cannot_Inline
7129 ("cannot inline & (in assertion expression)?", N, Nam_UA);
7130 end if;
7131
7132 -- Calls cannot be inlined inside default expressions
7133
7134 elsif In_Default_Expr then
7135 Cannot_Inline
7136 ("cannot inline & (in default expression)?", N, Nam_UA);
7137
7138 -- Calls cannot be inlined inside quantified expressions, which
7139 -- are left in expression form for GNATprove. Since these
7140 -- expressions are only preanalyzed, we need to detect the failure
7141 -- to inline outside of the case for Full_Analysis below.
7142
7143 elsif In_Quantified_Expression (N) then
7144 Cannot_Inline
7145 ("cannot inline & (in quantified expression)?", N, Nam_UA);
7146
7147 -- Inlining should not be performed during preanalysis
7148
7149 elsif Full_Analysis then
7150
7151 -- Do not inline calls inside expression functions or functions
7152 -- generated by the front end for subtype predicates, as this
7153 -- would prevent interpreting them as logical formulas in
7154 -- GNATprove. Only issue a message when the body has been seen,
7155 -- otherwise this leads to spurious messages on callees that
7156 -- are themselves expression functions.
7157
7158 if Present (Current_Subprogram)
7159 and then
7160 (Is_Expression_Function_Or_Completion (Current_Subprogram)
7161 or else Is_Predicate_Function (Current_Subprogram)
7162 or else Is_Invariant_Procedure (Current_Subprogram)
7163 or else Is_DIC_Procedure (Current_Subprogram))
7164 then
7165 if Present (Body_Id)
7166 and then Present (Body_To_Inline (Nam_Decl))
7167 then
7168 if Is_Predicate_Function (Current_Subprogram) then
7169 Cannot_Inline
7170 ("cannot inline & (inside predicate)?",
7171 N, Nam_UA);
7172
7173 elsif Is_Invariant_Procedure (Current_Subprogram) then
7174 Cannot_Inline
7175 ("cannot inline & (inside invariant)?",
7176 N, Nam_UA);
7177
7178 elsif Is_DIC_Procedure (Current_Subprogram) then
7179 Cannot_Inline
7180 ("cannot inline & (inside Default_Initial_Condition)?",
7181 N, Nam_UA);
7182
7183 else
7184 Cannot_Inline
7185 ("cannot inline & (inside expression function)?",
7186 N, Nam_UA);
7187 end if;
7188 end if;
7189
7190 -- Cannot inline a call inside the definition of a record type,
7191 -- typically inside the constraints of the type. Calls in
7192 -- default expressions are also not inlined, but this is
7193 -- filtered out above when testing In_Default_Expr.
7194
7195 elsif Is_Record_Type (Current_Scope) then
7196 Cannot_Inline
7197 ("cannot inline & (inside record type)?", N, Nam_UA);
7198
7199 -- With the one-pass inlining technique, a call cannot be
7200 -- inlined if the corresponding body has not been seen yet.
7201
7202 elsif No (Body_Id) then
7203 Cannot_Inline
7204 ("cannot inline & (body not seen yet)?", N, Nam_UA);
7205
7206 -- Nothing to do if there is no body to inline, indicating that
7207 -- the subprogram is not suitable for inlining in GNATprove
7208 -- mode.
7209
7210 elsif No (Body_To_Inline (Nam_Decl)) then
7211 null;
7212
7213 -- Calls cannot be inlined inside potentially unevaluated
7214 -- expressions, as this would create complex actions inside
7215 -- expressions, that are not handled by GNATprove.
7216
7217 elsif Is_Potentially_Unevaluated (N) then
7218 Cannot_Inline
7219 ("cannot inline & (in potentially unevaluated context)?",
7220 N, Nam_UA);
7221
7222 -- Calls cannot be inlined inside the conditions of while
7223 -- loops, as this would create complex actions inside
7224 -- the condition, that are not handled by GNATprove.
7225
7226 elsif In_While_Loop_Condition (N) then
7227 Cannot_Inline
7228 ("cannot inline & (in while loop condition)?", N, Nam_UA);
7229
7230 -- Do not inline calls which would possibly lead to missing a
7231 -- type conversion check on an input parameter.
7232
7233 elsif not Call_Can_Be_Inlined_In_GNATprove_Mode (N, Nam) then
7234 Cannot_Inline
7235 ("cannot inline & (possible check on input parameters)?",
7236 N, Nam_UA);
7237
7238 -- Otherwise, inline the call, issuing an info message when
7239 -- -gnatd_f is set.
7240
7241 else
7242 if Debug_Flag_Underscore_F then
7243 Error_Msg_NE
7244 ("info: analyzing call to & in context?", N, Nam_UA);
7245 end if;
7246
7247 Expand_Inlined_Call (N, Nam_UA, Nam);
7248 end if;
7249 end if;
7250 end if;
7251 end if;
7252 end Resolve_Call;
7253
7254 -----------------------------
7255 -- Resolve_Case_Expression --
7256 -----------------------------
7257
7258 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
7259 Alt : Node_Id;
7260 Alt_Expr : Node_Id;
7261 Alt_Typ : Entity_Id;
7262 Is_Dyn : Boolean;
7263
7264 begin
7265 Alt := First (Alternatives (N));
7266 while Present (Alt) loop
7267 Alt_Expr := Expression (Alt);
7268
7269 if Error_Posted (Alt_Expr) then
7270 return;
7271 end if;
7272
7273 Resolve (Alt_Expr, Typ);
7274 Alt_Typ := Etype (Alt_Expr);
7275
7276 -- When the expression is of a scalar subtype different from the
7277 -- result subtype, then insert a conversion to ensure the generation
7278 -- of a constraint check.
7279
7280 if Is_Scalar_Type (Alt_Typ) and then Alt_Typ /= Typ then
7281 Rewrite (Alt_Expr, Convert_To (Typ, Alt_Expr));
7282 Analyze_And_Resolve (Alt_Expr, Typ);
7283 end if;
7284
7285 Next (Alt);
7286 end loop;
7287
7288 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
7289 -- dynamically tagged must be known statically.
7290
7291 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
7292 Alt := First (Alternatives (N));
7293 Is_Dyn := Is_Dynamically_Tagged (Expression (Alt));
7294
7295 while Present (Alt) loop
7296 if Is_Dynamically_Tagged (Expression (Alt)) /= Is_Dyn then
7297 Error_Msg_N
7298 ("all or none of the dependent expressions can be "
7299 & "dynamically tagged", N);
7300 end if;
7301
7302 Next (Alt);
7303 end loop;
7304 end if;
7305
7306 Set_Etype (N, Typ);
7307 Eval_Case_Expression (N);
7308 Analyze_Dimension (N);
7309 end Resolve_Case_Expression;
7310
7311 -------------------------------
7312 -- Resolve_Character_Literal --
7313 -------------------------------
7314
7315 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
7316 B_Typ : constant Entity_Id := Base_Type (Typ);
7317 C : Entity_Id;
7318
7319 begin
7320 -- Verify that the character does belong to the type of the context
7321
7322 Set_Etype (N, B_Typ);
7323 Eval_Character_Literal (N);
7324
7325 -- Wide_Wide_Character literals must always be defined, since the set
7326 -- of wide wide character literals is complete, i.e. if a character
7327 -- literal is accepted by the parser, then it is OK for wide wide
7328 -- character (out of range character literals are rejected).
7329
7330 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
7331 return;
7332
7333 -- Always accept character literal for type Any_Character, which
7334 -- occurs in error situations and in comparisons of literals, both
7335 -- of which should accept all literals.
7336
7337 elsif B_Typ = Any_Character then
7338 return;
7339
7340 -- For Standard.Character or a type derived from it, check that the
7341 -- literal is in range.
7342
7343 elsif Root_Type (B_Typ) = Standard_Character then
7344 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
7345 return;
7346 end if;
7347
7348 -- For Standard.Wide_Character or a type derived from it, check that the
7349 -- literal is in range.
7350
7351 elsif Root_Type (B_Typ) = Standard_Wide_Character then
7352 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
7353 return;
7354 end if;
7355
7356 -- If the entity is already set, this has already been resolved in a
7357 -- generic context, or comes from expansion. Nothing else to do.
7358
7359 elsif Present (Entity (N)) then
7360 return;
7361
7362 -- Otherwise we have a user defined character type, and we can use the
7363 -- standard visibility mechanisms to locate the referenced entity.
7364
7365 else
7366 C := Current_Entity (N);
7367 while Present (C) loop
7368 if Etype (C) = B_Typ then
7369 Set_Entity_With_Checks (N, C);
7370 Generate_Reference (C, N);
7371 return;
7372 end if;
7373
7374 C := Homonym (C);
7375 end loop;
7376 end if;
7377
7378 -- If we fall through, then the literal does not match any of the
7379 -- entries of the enumeration type. This isn't just a constraint error
7380 -- situation, it is an illegality (see RM 4.2).
7381
7382 Error_Msg_NE
7383 ("character not defined for }", N, First_Subtype (B_Typ));
7384 end Resolve_Character_Literal;
7385
7386 ---------------------------
7387 -- Resolve_Comparison_Op --
7388 ---------------------------
7389
7390 -- Context requires a boolean type, and plays no role in resolution.
7391 -- Processing identical to that for equality operators. The result type is
7392 -- the base type, which matters when pathological subtypes of booleans with
7393 -- limited ranges are used.
7394
7395 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
7396 L : constant Node_Id := Left_Opnd (N);
7397 R : constant Node_Id := Right_Opnd (N);
7398 T : Entity_Id;
7399
7400 begin
7401 -- If this is an intrinsic operation which is not predefined, use the
7402 -- types of its declared arguments to resolve the possibly overloaded
7403 -- operands. Otherwise the operands are unambiguous and specify the
7404 -- expected type.
7405
7406 if Scope (Entity (N)) /= Standard_Standard then
7407 T := Etype (First_Entity (Entity (N)));
7408
7409 else
7410 T := Find_Unique_Type (L, R);
7411
7412 if T = Any_Fixed then
7413 T := Unique_Fixed_Point_Type (L);
7414 end if;
7415 end if;
7416
7417 Set_Etype (N, Base_Type (Typ));
7418 Generate_Reference (T, N, ' ');
7419
7420 -- Skip remaining processing if already set to Any_Type
7421
7422 if T = Any_Type then
7423 return;
7424 end if;
7425
7426 -- Deal with other error cases
7427
7428 if T = Any_String or else
7429 T = Any_Composite or else
7430 T = Any_Character
7431 then
7432 if T = Any_Character then
7433 Ambiguous_Character (L);
7434 else
7435 Error_Msg_N ("ambiguous operands for comparison", N);
7436 end if;
7437
7438 Set_Etype (N, Any_Type);
7439 return;
7440 end if;
7441
7442 -- Resolve the operands if types OK
7443
7444 Resolve (L, T);
7445 Resolve (R, T);
7446 Check_Unset_Reference (L);
7447 Check_Unset_Reference (R);
7448 Generate_Operator_Reference (N, T);
7449 Check_Low_Bound_Tested (N);
7450
7451 -- In SPARK, ordering operators <, <=, >, >= are not defined for Boolean
7452 -- types or array types except String.
7453
7454 if Is_Boolean_Type (T) then
7455 Check_SPARK_05_Restriction
7456 ("comparison is not defined on Boolean type", N);
7457
7458 elsif Is_Array_Type (T)
7459 and then Base_Type (T) /= Standard_String
7460 then
7461 Check_SPARK_05_Restriction
7462 ("comparison is not defined on array types other than String", N);
7463 end if;
7464
7465 -- Check comparison on unordered enumeration
7466
7467 if Bad_Unordered_Enumeration_Reference (N, Etype (L)) then
7468 Error_Msg_Sloc := Sloc (Etype (L));
7469 Error_Msg_NE
7470 ("comparison on unordered enumeration type& declared#?U?",
7471 N, Etype (L));
7472 end if;
7473
7474 Analyze_Dimension (N);
7475
7476 -- Evaluate the relation (note we do this after the above check since
7477 -- this Eval call may change N to True/False. Skip this evaluation
7478 -- inside assertions, in order to keep assertions as written by users
7479 -- for tools that rely on these, e.g. GNATprove for loop invariants.
7480 -- Except evaluation is still performed even inside assertions for
7481 -- comparisons between values of universal type, which are useless
7482 -- for static analysis tools, and not supported even by GNATprove.
7483
7484 if In_Assertion_Expr = 0
7485 or else (Is_Universal_Numeric_Type (Etype (L))
7486 and then
7487 Is_Universal_Numeric_Type (Etype (R)))
7488 then
7489 Eval_Relational_Op (N);
7490 end if;
7491 end Resolve_Comparison_Op;
7492
7493 -----------------------------------------
7494 -- Resolve_Discrete_Subtype_Indication --
7495 -----------------------------------------
7496
7497 procedure Resolve_Discrete_Subtype_Indication
7498 (N : Node_Id;
7499 Typ : Entity_Id)
7500 is
7501 R : Node_Id;
7502 S : Entity_Id;
7503
7504 begin
7505 Analyze (Subtype_Mark (N));
7506 S := Entity (Subtype_Mark (N));
7507
7508 if Nkind (Constraint (N)) /= N_Range_Constraint then
7509 Error_Msg_N ("expect range constraint for discrete type", N);
7510 Set_Etype (N, Any_Type);
7511
7512 else
7513 R := Range_Expression (Constraint (N));
7514
7515 if R = Error then
7516 return;
7517 end if;
7518
7519 Analyze (R);
7520
7521 if Base_Type (S) /= Base_Type (Typ) then
7522 Error_Msg_NE
7523 ("expect subtype of }", N, First_Subtype (Typ));
7524
7525 -- Rewrite the constraint as a range of Typ
7526 -- to allow compilation to proceed further.
7527
7528 Set_Etype (N, Typ);
7529 Rewrite (Low_Bound (R),
7530 Make_Attribute_Reference (Sloc (Low_Bound (R)),
7531 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
7532 Attribute_Name => Name_First));
7533 Rewrite (High_Bound (R),
7534 Make_Attribute_Reference (Sloc (High_Bound (R)),
7535 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
7536 Attribute_Name => Name_First));
7537
7538 else
7539 Resolve (R, Typ);
7540 Set_Etype (N, Etype (R));
7541
7542 -- Additionally, we must check that the bounds are compatible
7543 -- with the given subtype, which might be different from the
7544 -- type of the context.
7545
7546 Apply_Range_Check (R, S);
7547
7548 -- ??? If the above check statically detects a Constraint_Error
7549 -- it replaces the offending bound(s) of the range R with a
7550 -- Constraint_Error node. When the itype which uses these bounds
7551 -- is frozen the resulting call to Duplicate_Subexpr generates
7552 -- a new temporary for the bounds.
7553
7554 -- Unfortunately there are other itypes that are also made depend
7555 -- on these bounds, so when Duplicate_Subexpr is called they get
7556 -- a forward reference to the newly created temporaries and Gigi
7557 -- aborts on such forward references. This is probably sign of a
7558 -- more fundamental problem somewhere else in either the order of
7559 -- itype freezing or the way certain itypes are constructed.
7560
7561 -- To get around this problem we call Remove_Side_Effects right
7562 -- away if either bounds of R are a Constraint_Error.
7563
7564 declare
7565 L : constant Node_Id := Low_Bound (R);
7566 H : constant Node_Id := High_Bound (R);
7567
7568 begin
7569 if Nkind (L) = N_Raise_Constraint_Error then
7570 Remove_Side_Effects (L);
7571 end if;
7572
7573 if Nkind (H) = N_Raise_Constraint_Error then
7574 Remove_Side_Effects (H);
7575 end if;
7576 end;
7577
7578 Check_Unset_Reference (Low_Bound (R));
7579 Check_Unset_Reference (High_Bound (R));
7580 end if;
7581 end if;
7582 end Resolve_Discrete_Subtype_Indication;
7583
7584 -------------------------
7585 -- Resolve_Entity_Name --
7586 -------------------------
7587
7588 -- Used to resolve identifiers and expanded names
7589
7590 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
7591 function Is_Assignment_Or_Object_Expression
7592 (Context : Node_Id;
7593 Expr : Node_Id) return Boolean;
7594 -- Determine whether node Context denotes an assignment statement or an
7595 -- object declaration whose expression is node Expr.
7596
7597 ----------------------------------------
7598 -- Is_Assignment_Or_Object_Expression --
7599 ----------------------------------------
7600
7601 function Is_Assignment_Or_Object_Expression
7602 (Context : Node_Id;
7603 Expr : Node_Id) return Boolean
7604 is
7605 begin
7606 if Nkind_In (Context, N_Assignment_Statement,
7607 N_Object_Declaration)
7608 and then Expression (Context) = Expr
7609 then
7610 return True;
7611
7612 -- Check whether a construct that yields a name is the expression of
7613 -- an assignment statement or an object declaration.
7614
7615 elsif (Nkind_In (Context, N_Attribute_Reference,
7616 N_Explicit_Dereference,
7617 N_Indexed_Component,
7618 N_Selected_Component,
7619 N_Slice)
7620 and then Prefix (Context) = Expr)
7621 or else
7622 (Nkind_In (Context, N_Type_Conversion,
7623 N_Unchecked_Type_Conversion)
7624 and then Expression (Context) = Expr)
7625 then
7626 return
7627 Is_Assignment_Or_Object_Expression
7628 (Context => Parent (Context),
7629 Expr => Context);
7630
7631 -- Otherwise the context is not an assignment statement or an object
7632 -- declaration.
7633
7634 else
7635 return False;
7636 end if;
7637 end Is_Assignment_Or_Object_Expression;
7638
7639 -- Local variables
7640
7641 E : constant Entity_Id := Entity (N);
7642 Par : Node_Id;
7643
7644 -- Start of processing for Resolve_Entity_Name
7645
7646 begin
7647 -- If garbage from errors, set to Any_Type and return
7648
7649 if No (E) and then Total_Errors_Detected /= 0 then
7650 Set_Etype (N, Any_Type);
7651 return;
7652 end if;
7653
7654 -- Replace named numbers by corresponding literals. Note that this is
7655 -- the one case where Resolve_Entity_Name must reset the Etype, since
7656 -- it is currently marked as universal.
7657
7658 if Ekind (E) = E_Named_Integer then
7659 Set_Etype (N, Typ);
7660 Eval_Named_Integer (N);
7661
7662 elsif Ekind (E) = E_Named_Real then
7663 Set_Etype (N, Typ);
7664 Eval_Named_Real (N);
7665
7666 -- For enumeration literals, we need to make sure that a proper style
7667 -- check is done, since such literals are overloaded, and thus we did
7668 -- not do a style check during the first phase of analysis.
7669
7670 elsif Ekind (E) = E_Enumeration_Literal then
7671 Set_Entity_With_Checks (N, E);
7672 Eval_Entity_Name (N);
7673
7674 -- Case of (sub)type name appearing in a context where an expression
7675 -- is expected. This is legal if occurrence is a current instance.
7676 -- See RM 8.6 (17/3).
7677
7678 elsif Is_Type (E) then
7679 if Is_Current_Instance (N) then
7680 null;
7681
7682 -- Any other use is an error
7683
7684 else
7685 Error_Msg_N
7686 ("invalid use of subtype mark in expression or call", N);
7687 end if;
7688
7689 -- Check discriminant use if entity is discriminant in current scope,
7690 -- i.e. discriminant of record or concurrent type currently being
7691 -- analyzed. Uses in corresponding body are unrestricted.
7692
7693 elsif Ekind (E) = E_Discriminant
7694 and then Scope (E) = Current_Scope
7695 and then not Has_Completion (Current_Scope)
7696 then
7697 Check_Discriminant_Use (N);
7698
7699 -- A parameterless generic function cannot appear in a context that
7700 -- requires resolution.
7701
7702 elsif Ekind (E) = E_Generic_Function then
7703 Error_Msg_N ("illegal use of generic function", N);
7704
7705 -- In Ada 83 an OUT parameter cannot be read, but attributes of
7706 -- array types (i.e. bounds and length) are legal.
7707
7708 elsif Ekind (E) = E_Out_Parameter
7709 and then (Nkind (Parent (N)) /= N_Attribute_Reference
7710 or else Is_Scalar_Type (Etype (E)))
7711
7712 and then (Nkind (Parent (N)) in N_Op
7713 or else Nkind (Parent (N)) = N_Explicit_Dereference
7714 or else Is_Assignment_Or_Object_Expression
7715 (Context => Parent (N),
7716 Expr => N))
7717 then
7718 if Ada_Version = Ada_83 then
7719 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
7720 end if;
7721
7722 -- In all other cases, just do the possible static evaluation
7723
7724 else
7725 -- A deferred constant that appears in an expression must have a
7726 -- completion, unless it has been removed by in-place expansion of
7727 -- an aggregate. A constant that is a renaming does not need
7728 -- initialization.
7729
7730 if Ekind (E) = E_Constant
7731 and then Comes_From_Source (E)
7732 and then No (Constant_Value (E))
7733 and then Is_Frozen (Etype (E))
7734 and then not In_Spec_Expression
7735 and then not Is_Imported (E)
7736 and then Nkind (Parent (E)) /= N_Object_Renaming_Declaration
7737 then
7738 if No_Initialization (Parent (E))
7739 or else (Present (Full_View (E))
7740 and then No_Initialization (Parent (Full_View (E))))
7741 then
7742 null;
7743 else
7744 Error_Msg_N
7745 ("deferred constant is frozen before completion", N);
7746 end if;
7747 end if;
7748
7749 Eval_Entity_Name (N);
7750 end if;
7751
7752 Par := Parent (N);
7753
7754 -- When the entity appears in a parameter association, retrieve the
7755 -- related subprogram call.
7756
7757 if Nkind (Par) = N_Parameter_Association then
7758 Par := Parent (Par);
7759 end if;
7760
7761 if Comes_From_Source (N) then
7762
7763 -- The following checks are only relevant when SPARK_Mode is on as
7764 -- they are not standard Ada legality rules.
7765
7766 if SPARK_Mode = On then
7767
7768 -- An effectively volatile object subject to enabled properties
7769 -- Async_Writers or Effective_Reads must appear in non-interfering
7770 -- context (SPARK RM 7.1.3(12)).
7771
7772 if Is_Object (E)
7773 and then Is_Effectively_Volatile (E)
7774 and then (Async_Writers_Enabled (E)
7775 or else Effective_Reads_Enabled (E))
7776 and then not Is_OK_Volatile_Context (Par, N)
7777 then
7778 SPARK_Msg_N
7779 ("volatile object cannot appear in this context "
7780 & "(SPARK RM 7.1.3(12))", N);
7781 end if;
7782
7783 -- Check for possible elaboration issues with respect to reads of
7784 -- variables. The act of renaming the variable is not considered a
7785 -- read as it simply establishes an alias.
7786
7787 if Legacy_Elaboration_Checks
7788 and then Ekind (E) = E_Variable
7789 and then Dynamic_Elaboration_Checks
7790 and then Nkind (Par) /= N_Object_Renaming_Declaration
7791 then
7792 Check_Elab_Call (N);
7793 end if;
7794 end if;
7795
7796 -- The variable may eventually become a constituent of a single
7797 -- protected/task type. Record the reference now and verify its
7798 -- legality when analyzing the contract of the variable
7799 -- (SPARK RM 9.3).
7800
7801 if Ekind (E) = E_Variable then
7802 Record_Possible_Part_Of_Reference (E, N);
7803 end if;
7804
7805 -- A Ghost entity must appear in a specific context
7806
7807 if Is_Ghost_Entity (E) then
7808 Check_Ghost_Context (E, N);
7809 end if;
7810 end if;
7811
7812 -- We may be resolving an entity within expanded code, so a reference to
7813 -- an entity should be ignored when calculating effective use clauses to
7814 -- avoid inappropriate marking.
7815
7816 if Comes_From_Source (N) then
7817 Mark_Use_Clauses (E);
7818 end if;
7819 end Resolve_Entity_Name;
7820
7821 -------------------
7822 -- Resolve_Entry --
7823 -------------------
7824
7825 procedure Resolve_Entry (Entry_Name : Node_Id) is
7826 Loc : constant Source_Ptr := Sloc (Entry_Name);
7827 Nam : Entity_Id;
7828 New_N : Node_Id;
7829 S : Entity_Id;
7830 Tsk : Entity_Id;
7831 E_Name : Node_Id;
7832 Index : Node_Id;
7833
7834 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
7835 -- If the bounds of the entry family being called depend on task
7836 -- discriminants, build a new index subtype where a discriminant is
7837 -- replaced with the value of the discriminant of the target task.
7838 -- The target task is the prefix of the entry name in the call.
7839
7840 -----------------------
7841 -- Actual_Index_Type --
7842 -----------------------
7843
7844 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
7845 Typ : constant Entity_Id := Entry_Index_Type (E);
7846 Tsk : constant Entity_Id := Scope (E);
7847 Lo : constant Node_Id := Type_Low_Bound (Typ);
7848 Hi : constant Node_Id := Type_High_Bound (Typ);
7849 New_T : Entity_Id;
7850
7851 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
7852 -- If the bound is given by a discriminant, replace with a reference
7853 -- to the discriminant of the same name in the target task. If the
7854 -- entry name is the target of a requeue statement and the entry is
7855 -- in the current protected object, the bound to be used is the
7856 -- discriminal of the object (see Apply_Range_Checks for details of
7857 -- the transformation).
7858
7859 -----------------------------
7860 -- Actual_Discriminant_Ref --
7861 -----------------------------
7862
7863 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
7864 Typ : constant Entity_Id := Etype (Bound);
7865 Ref : Node_Id;
7866
7867 begin
7868 Remove_Side_Effects (Bound);
7869
7870 if not Is_Entity_Name (Bound)
7871 or else Ekind (Entity (Bound)) /= E_Discriminant
7872 then
7873 return Bound;
7874
7875 elsif Is_Protected_Type (Tsk)
7876 and then In_Open_Scopes (Tsk)
7877 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
7878 then
7879 -- Note: here Bound denotes a discriminant of the corresponding
7880 -- record type tskV, whose discriminal is a formal of the
7881 -- init-proc tskVIP. What we want is the body discriminal,
7882 -- which is associated to the discriminant of the original
7883 -- concurrent type tsk.
7884
7885 return New_Occurrence_Of
7886 (Find_Body_Discriminal (Entity (Bound)), Loc);
7887
7888 else
7889 Ref :=
7890 Make_Selected_Component (Loc,
7891 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
7892 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
7893 Analyze (Ref);
7894 Resolve (Ref, Typ);
7895 return Ref;
7896 end if;
7897 end Actual_Discriminant_Ref;
7898
7899 -- Start of processing for Actual_Index_Type
7900
7901 begin
7902 if not Has_Discriminants (Tsk)
7903 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
7904 then
7905 return Entry_Index_Type (E);
7906
7907 else
7908 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
7909 Set_Etype (New_T, Base_Type (Typ));
7910 Set_Size_Info (New_T, Typ);
7911 Set_RM_Size (New_T, RM_Size (Typ));
7912 Set_Scalar_Range (New_T,
7913 Make_Range (Sloc (Entry_Name),
7914 Low_Bound => Actual_Discriminant_Ref (Lo),
7915 High_Bound => Actual_Discriminant_Ref (Hi)));
7916
7917 return New_T;
7918 end if;
7919 end Actual_Index_Type;
7920
7921 -- Start of processing for Resolve_Entry
7922
7923 begin
7924 -- Find name of entry being called, and resolve prefix of name with its
7925 -- own type. The prefix can be overloaded, and the name and signature of
7926 -- the entry must be taken into account.
7927
7928 if Nkind (Entry_Name) = N_Indexed_Component then
7929
7930 -- Case of dealing with entry family within the current tasks
7931
7932 E_Name := Prefix (Entry_Name);
7933
7934 else
7935 E_Name := Entry_Name;
7936 end if;
7937
7938 if Is_Entity_Name (E_Name) then
7939
7940 -- Entry call to an entry (or entry family) in the current task. This
7941 -- is legal even though the task will deadlock. Rewrite as call to
7942 -- current task.
7943
7944 -- This can also be a call to an entry in an enclosing task. If this
7945 -- is a single task, we have to retrieve its name, because the scope
7946 -- of the entry is the task type, not the object. If the enclosing
7947 -- task is a task type, the identity of the task is given by its own
7948 -- self variable.
7949
7950 -- Finally this can be a requeue on an entry of the same task or
7951 -- protected object.
7952
7953 S := Scope (Entity (E_Name));
7954
7955 for J in reverse 0 .. Scope_Stack.Last loop
7956 if Is_Task_Type (Scope_Stack.Table (J).Entity)
7957 and then not Comes_From_Source (S)
7958 then
7959 -- S is an enclosing task or protected object. The concurrent
7960 -- declaration has been converted into a type declaration, and
7961 -- the object itself has an object declaration that follows
7962 -- the type in the same declarative part.
7963
7964 Tsk := Next_Entity (S);
7965 while Etype (Tsk) /= S loop
7966 Next_Entity (Tsk);
7967 end loop;
7968
7969 S := Tsk;
7970 exit;
7971
7972 elsif S = Scope_Stack.Table (J).Entity then
7973
7974 -- Call to current task. Will be transformed into call to Self
7975
7976 exit;
7977
7978 end if;
7979 end loop;
7980
7981 New_N :=
7982 Make_Selected_Component (Loc,
7983 Prefix => New_Occurrence_Of (S, Loc),
7984 Selector_Name =>
7985 New_Occurrence_Of (Entity (E_Name), Loc));
7986 Rewrite (E_Name, New_N);
7987 Analyze (E_Name);
7988
7989 elsif Nkind (Entry_Name) = N_Selected_Component
7990 and then Is_Overloaded (Prefix (Entry_Name))
7991 then
7992 -- Use the entry name (which must be unique at this point) to find
7993 -- the prefix that returns the corresponding task/protected type.
7994
7995 declare
7996 Pref : constant Node_Id := Prefix (Entry_Name);
7997 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
7998 I : Interp_Index;
7999 It : Interp;
8000
8001 begin
8002 Get_First_Interp (Pref, I, It);
8003 while Present (It.Typ) loop
8004 if Scope (Ent) = It.Typ then
8005 Set_Etype (Pref, It.Typ);
8006 exit;
8007 end if;
8008
8009 Get_Next_Interp (I, It);
8010 end loop;
8011 end;
8012 end if;
8013
8014 if Nkind (Entry_Name) = N_Selected_Component then
8015 Resolve (Prefix (Entry_Name));
8016
8017 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8018 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
8019 Resolve (Prefix (Prefix (Entry_Name)));
8020 Index := First (Expressions (Entry_Name));
8021 Resolve (Index, Entry_Index_Type (Nam));
8022
8023 -- Generate a reference for the index when it denotes an entity
8024
8025 if Is_Entity_Name (Index) then
8026 Generate_Reference (Entity (Index), Nam);
8027 end if;
8028
8029 -- Up to this point the expression could have been the actual in a
8030 -- simple entry call, and be given by a named association.
8031
8032 if Nkind (Index) = N_Parameter_Association then
8033 Error_Msg_N ("expect expression for entry index", Index);
8034 else
8035 Apply_Range_Check (Index, Actual_Index_Type (Nam));
8036 end if;
8037 end if;
8038 end Resolve_Entry;
8039
8040 ------------------------
8041 -- Resolve_Entry_Call --
8042 ------------------------
8043
8044 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
8045 Entry_Name : constant Node_Id := Name (N);
8046 Loc : constant Source_Ptr := Sloc (Entry_Name);
8047
8048 Nam : Entity_Id;
8049 Norm_OK : Boolean;
8050 Obj : Node_Id;
8051 Was_Over : Boolean;
8052
8053 begin
8054 -- We kill all checks here, because it does not seem worth the effort to
8055 -- do anything better, an entry call is a big operation.
8056
8057 Kill_All_Checks;
8058
8059 -- Processing of the name is similar for entry calls and protected
8060 -- operation calls. Once the entity is determined, we can complete
8061 -- the resolution of the actuals.
8062
8063 -- The selector may be overloaded, in the case of a protected object
8064 -- with overloaded functions. The type of the context is used for
8065 -- resolution.
8066
8067 if Nkind (Entry_Name) = N_Selected_Component
8068 and then Is_Overloaded (Selector_Name (Entry_Name))
8069 and then Typ /= Standard_Void_Type
8070 then
8071 declare
8072 I : Interp_Index;
8073 It : Interp;
8074
8075 begin
8076 Get_First_Interp (Selector_Name (Entry_Name), I, It);
8077 while Present (It.Typ) loop
8078 if Covers (Typ, It.Typ) then
8079 Set_Entity (Selector_Name (Entry_Name), It.Nam);
8080 Set_Etype (Entry_Name, It.Typ);
8081
8082 Generate_Reference (It.Typ, N, ' ');
8083 end if;
8084
8085 Get_Next_Interp (I, It);
8086 end loop;
8087 end;
8088 end if;
8089
8090 Resolve_Entry (Entry_Name);
8091
8092 if Nkind (Entry_Name) = N_Selected_Component then
8093
8094 -- Simple entry or protected operation call
8095
8096 Nam := Entity (Selector_Name (Entry_Name));
8097 Obj := Prefix (Entry_Name);
8098
8099 if Is_Subprogram (Nam) then
8100 Check_For_Eliminated_Subprogram (Entry_Name, Nam);
8101 end if;
8102
8103 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
8104
8105 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8106
8107 -- Call to member of entry family
8108
8109 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
8110 Obj := Prefix (Prefix (Entry_Name));
8111 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
8112 end if;
8113
8114 -- We cannot in general check the maximum depth of protected entry calls
8115 -- at compile time. But we can tell that any protected entry call at all
8116 -- violates a specified nesting depth of zero.
8117
8118 if Is_Protected_Type (Scope (Nam)) then
8119 Check_Restriction (Max_Entry_Queue_Length, N);
8120 end if;
8121
8122 -- Use context type to disambiguate a protected function that can be
8123 -- called without actuals and that returns an array type, and where the
8124 -- argument list may be an indexing of the returned value.
8125
8126 if Ekind (Nam) = E_Function
8127 and then Needs_No_Actuals (Nam)
8128 and then Present (Parameter_Associations (N))
8129 and then
8130 ((Is_Array_Type (Etype (Nam))
8131 and then Covers (Typ, Component_Type (Etype (Nam))))
8132
8133 or else (Is_Access_Type (Etype (Nam))
8134 and then Is_Array_Type (Designated_Type (Etype (Nam)))
8135 and then
8136 Covers
8137 (Typ,
8138 Component_Type (Designated_Type (Etype (Nam))))))
8139 then
8140 declare
8141 Index_Node : Node_Id;
8142
8143 begin
8144 Index_Node :=
8145 Make_Indexed_Component (Loc,
8146 Prefix =>
8147 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
8148 Expressions => Parameter_Associations (N));
8149
8150 -- Since we are correcting a node classification error made by the
8151 -- parser, we call Replace rather than Rewrite.
8152
8153 Replace (N, Index_Node);
8154 Set_Etype (Prefix (N), Etype (Nam));
8155 Set_Etype (N, Typ);
8156 Resolve_Indexed_Component (N, Typ);
8157 return;
8158 end;
8159 end if;
8160
8161 if Ekind_In (Nam, E_Entry, E_Entry_Family)
8162 and then Present (Contract_Wrapper (Nam))
8163 and then Current_Scope /= Contract_Wrapper (Nam)
8164 then
8165 -- Note the entity being called before rewriting the call, so that
8166 -- it appears used at this point.
8167
8168 Generate_Reference (Nam, Entry_Name, 'r');
8169
8170 -- Rewrite as call to the precondition wrapper, adding the task
8171 -- object to the list of actuals. If the call is to a member of an
8172 -- entry family, include the index as well.
8173
8174 declare
8175 New_Call : Node_Id;
8176 New_Actuals : List_Id;
8177
8178 begin
8179 New_Actuals := New_List (Obj);
8180
8181 if Nkind (Entry_Name) = N_Indexed_Component then
8182 Append_To (New_Actuals,
8183 New_Copy_Tree (First (Expressions (Entry_Name))));
8184 end if;
8185
8186 Append_List (Parameter_Associations (N), New_Actuals);
8187 New_Call :=
8188 Make_Procedure_Call_Statement (Loc,
8189 Name =>
8190 New_Occurrence_Of (Contract_Wrapper (Nam), Loc),
8191 Parameter_Associations => New_Actuals);
8192 Rewrite (N, New_Call);
8193
8194 -- Preanalyze and resolve new call. Current procedure is called
8195 -- from Resolve_Call, after which expansion will take place.
8196
8197 Preanalyze_And_Resolve (N);
8198 return;
8199 end;
8200 end if;
8201
8202 -- The operation name may have been overloaded. Order the actuals
8203 -- according to the formals of the resolved entity, and set the return
8204 -- type to that of the operation.
8205
8206 if Was_Over then
8207 Normalize_Actuals (N, Nam, False, Norm_OK);
8208 pragma Assert (Norm_OK);
8209 Set_Etype (N, Etype (Nam));
8210
8211 -- Reset the Is_Overloaded flag, since resolution is now completed
8212
8213 -- Simple entry call
8214
8215 if Nkind (Entry_Name) = N_Selected_Component then
8216 Set_Is_Overloaded (Selector_Name (Entry_Name), False);
8217
8218 -- Call to a member of an entry family
8219
8220 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8221 Set_Is_Overloaded (Selector_Name (Prefix (Entry_Name)), False);
8222 end if;
8223 end if;
8224
8225 Resolve_Actuals (N, Nam);
8226 Check_Internal_Protected_Use (N, Nam);
8227
8228 -- Create a call reference to the entry
8229
8230 Generate_Reference (Nam, Entry_Name, 's');
8231
8232 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
8233 Check_Potentially_Blocking_Operation (N);
8234 end if;
8235
8236 -- Verify that a procedure call cannot masquerade as an entry
8237 -- call where an entry call is expected.
8238
8239 if Ekind (Nam) = E_Procedure then
8240 if Nkind (Parent (N)) = N_Entry_Call_Alternative
8241 and then N = Entry_Call_Statement (Parent (N))
8242 then
8243 Error_Msg_N ("entry call required in select statement", N);
8244
8245 elsif Nkind (Parent (N)) = N_Triggering_Alternative
8246 and then N = Triggering_Statement (Parent (N))
8247 then
8248 Error_Msg_N ("triggering statement cannot be procedure call", N);
8249
8250 elsif Ekind (Scope (Nam)) = E_Task_Type
8251 and then not In_Open_Scopes (Scope (Nam))
8252 then
8253 Error_Msg_N ("task has no entry with this name", Entry_Name);
8254 end if;
8255 end if;
8256
8257 -- After resolution, entry calls and protected procedure calls are
8258 -- changed into entry calls, for expansion. The structure of the node
8259 -- does not change, so it can safely be done in place. Protected
8260 -- function calls must keep their structure because they are
8261 -- subexpressions.
8262
8263 if Ekind (Nam) /= E_Function then
8264
8265 -- A protected operation that is not a function may modify the
8266 -- corresponding object, and cannot apply to a constant. If this
8267 -- is an internal call, the prefix is the type itself.
8268
8269 if Is_Protected_Type (Scope (Nam))
8270 and then not Is_Variable (Obj)
8271 and then (not Is_Entity_Name (Obj)
8272 or else not Is_Type (Entity (Obj)))
8273 then
8274 Error_Msg_N
8275 ("prefix of protected procedure or entry call must be variable",
8276 Entry_Name);
8277 end if;
8278
8279 declare
8280 Entry_Call : Node_Id;
8281
8282 begin
8283 Entry_Call :=
8284 Make_Entry_Call_Statement (Loc,
8285 Name => Entry_Name,
8286 Parameter_Associations => Parameter_Associations (N));
8287
8288 -- Inherit relevant attributes from the original call
8289
8290 Set_First_Named_Actual
8291 (Entry_Call, First_Named_Actual (N));
8292
8293 Set_Is_Elaboration_Checks_OK_Node
8294 (Entry_Call, Is_Elaboration_Checks_OK_Node (N));
8295
8296 Set_Is_Elaboration_Warnings_OK_Node
8297 (Entry_Call, Is_Elaboration_Warnings_OK_Node (N));
8298
8299 Set_Is_SPARK_Mode_On_Node
8300 (Entry_Call, Is_SPARK_Mode_On_Node (N));
8301
8302 Rewrite (N, Entry_Call);
8303 Set_Analyzed (N, True);
8304 end;
8305
8306 -- Protected functions can return on the secondary stack, in which case
8307 -- we must trigger the transient scope mechanism.
8308
8309 elsif Expander_Active
8310 and then Requires_Transient_Scope (Etype (Nam))
8311 then
8312 Establish_Transient_Scope (N, Manage_Sec_Stack => True);
8313 end if;
8314 end Resolve_Entry_Call;
8315
8316 -------------------------
8317 -- Resolve_Equality_Op --
8318 -------------------------
8319
8320 -- Both arguments must have the same type, and the boolean context does
8321 -- not participate in the resolution. The first pass verifies that the
8322 -- interpretation is not ambiguous, and the type of the left argument is
8323 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
8324 -- are strings or aggregates, allocators, or Null, they are ambiguous even
8325 -- though they carry a single (universal) type. Diagnose this case here.
8326
8327 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
8328 L : constant Node_Id := Left_Opnd (N);
8329 R : constant Node_Id := Right_Opnd (N);
8330 T : Entity_Id := Find_Unique_Type (L, R);
8331
8332 procedure Check_If_Expression (Cond : Node_Id);
8333 -- The resolution rule for if expressions requires that each such must
8334 -- have a unique type. This means that if several dependent expressions
8335 -- are of a non-null anonymous access type, and the context does not
8336 -- impose an expected type (as can be the case in an equality operation)
8337 -- the expression must be rejected.
8338
8339 procedure Explain_Redundancy (N : Node_Id);
8340 -- Attempt to explain the nature of a redundant comparison with True. If
8341 -- the expression N is too complex, this routine issues a general error
8342 -- message.
8343
8344 function Find_Unique_Access_Type return Entity_Id;
8345 -- In the case of allocators and access attributes, the context must
8346 -- provide an indication of the specific access type to be used. If
8347 -- one operand is of such a "generic" access type, check whether there
8348 -- is a specific visible access type that has the same designated type.
8349 -- This is semantically dubious, and of no interest to any real code,
8350 -- but c48008a makes it all worthwhile.
8351
8352 -------------------------
8353 -- Check_If_Expression --
8354 -------------------------
8355
8356 procedure Check_If_Expression (Cond : Node_Id) is
8357 Then_Expr : Node_Id;
8358 Else_Expr : Node_Id;
8359
8360 begin
8361 if Nkind (Cond) = N_If_Expression then
8362 Then_Expr := Next (First (Expressions (Cond)));
8363 Else_Expr := Next (Then_Expr);
8364
8365 if Nkind (Then_Expr) /= N_Null
8366 and then Nkind (Else_Expr) /= N_Null
8367 then
8368 Error_Msg_N ("cannot determine type of if expression", Cond);
8369 end if;
8370 end if;
8371 end Check_If_Expression;
8372
8373 ------------------------
8374 -- Explain_Redundancy --
8375 ------------------------
8376
8377 procedure Explain_Redundancy (N : Node_Id) is
8378 Error : Name_Id;
8379 Val : Node_Id;
8380 Val_Id : Entity_Id;
8381
8382 begin
8383 Val := N;
8384
8385 -- Strip the operand down to an entity
8386
8387 loop
8388 if Nkind (Val) = N_Selected_Component then
8389 Val := Selector_Name (Val);
8390 else
8391 exit;
8392 end if;
8393 end loop;
8394
8395 -- The construct denotes an entity
8396
8397 if Is_Entity_Name (Val) and then Present (Entity (Val)) then
8398 Val_Id := Entity (Val);
8399
8400 -- Do not generate an error message when the comparison is done
8401 -- against the enumeration literal Standard.True.
8402
8403 if Ekind (Val_Id) /= E_Enumeration_Literal then
8404
8405 -- Build a customized error message
8406
8407 Name_Len := 0;
8408 Add_Str_To_Name_Buffer ("?r?");
8409
8410 if Ekind (Val_Id) = E_Component then
8411 Add_Str_To_Name_Buffer ("component ");
8412
8413 elsif Ekind (Val_Id) = E_Constant then
8414 Add_Str_To_Name_Buffer ("constant ");
8415
8416 elsif Ekind (Val_Id) = E_Discriminant then
8417 Add_Str_To_Name_Buffer ("discriminant ");
8418
8419 elsif Is_Formal (Val_Id) then
8420 Add_Str_To_Name_Buffer ("parameter ");
8421
8422 elsif Ekind (Val_Id) = E_Variable then
8423 Add_Str_To_Name_Buffer ("variable ");
8424 end if;
8425
8426 Add_Str_To_Name_Buffer ("& is always True!");
8427 Error := Name_Find;
8428
8429 Error_Msg_NE (Get_Name_String (Error), Val, Val_Id);
8430 end if;
8431
8432 -- The construct is too complex to disect, issue a general message
8433
8434 else
8435 Error_Msg_N ("?r?expression is always True!", Val);
8436 end if;
8437 end Explain_Redundancy;
8438
8439 -----------------------------
8440 -- Find_Unique_Access_Type --
8441 -----------------------------
8442
8443 function Find_Unique_Access_Type return Entity_Id is
8444 Acc : Entity_Id;
8445 E : Entity_Id;
8446 S : Entity_Id;
8447
8448 begin
8449 if Ekind_In (Etype (R), E_Allocator_Type,
8450 E_Access_Attribute_Type)
8451 then
8452 Acc := Designated_Type (Etype (R));
8453
8454 elsif Ekind_In (Etype (L), E_Allocator_Type,
8455 E_Access_Attribute_Type)
8456 then
8457 Acc := Designated_Type (Etype (L));
8458 else
8459 return Empty;
8460 end if;
8461
8462 S := Current_Scope;
8463 while S /= Standard_Standard loop
8464 E := First_Entity (S);
8465 while Present (E) loop
8466 if Is_Type (E)
8467 and then Is_Access_Type (E)
8468 and then Ekind (E) /= E_Allocator_Type
8469 and then Designated_Type (E) = Base_Type (Acc)
8470 then
8471 return E;
8472 end if;
8473
8474 Next_Entity (E);
8475 end loop;
8476
8477 S := Scope (S);
8478 end loop;
8479
8480 return Empty;
8481 end Find_Unique_Access_Type;
8482
8483 -- Start of processing for Resolve_Equality_Op
8484
8485 begin
8486 Set_Etype (N, Base_Type (Typ));
8487 Generate_Reference (T, N, ' ');
8488
8489 if T = Any_Fixed then
8490 T := Unique_Fixed_Point_Type (L);
8491 end if;
8492
8493 if T /= Any_Type then
8494 if T = Any_String or else
8495 T = Any_Composite or else
8496 T = Any_Character
8497 then
8498 if T = Any_Character then
8499 Ambiguous_Character (L);
8500 else
8501 Error_Msg_N ("ambiguous operands for equality", N);
8502 end if;
8503
8504 Set_Etype (N, Any_Type);
8505 return;
8506
8507 elsif T = Any_Access
8508 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
8509 then
8510 T := Find_Unique_Access_Type;
8511
8512 if No (T) then
8513 Error_Msg_N ("ambiguous operands for equality", N);
8514 Set_Etype (N, Any_Type);
8515 return;
8516 end if;
8517
8518 -- If expressions must have a single type, and if the context does
8519 -- not impose one the dependent expressions cannot be anonymous
8520 -- access types.
8521
8522 -- Why no similar processing for case expressions???
8523
8524 elsif Ada_Version >= Ada_2012
8525 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
8526 E_Anonymous_Access_Subprogram_Type)
8527 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
8528 E_Anonymous_Access_Subprogram_Type)
8529 then
8530 Check_If_Expression (L);
8531 Check_If_Expression (R);
8532 end if;
8533
8534 Resolve (L, T);
8535 Resolve (R, T);
8536
8537 -- In SPARK, equality operators = and /= for array types other than
8538 -- String are only defined when, for each index position, the
8539 -- operands have equal static bounds.
8540
8541 if Is_Array_Type (T) then
8542
8543 -- Protect call to Matching_Static_Array_Bounds to avoid costly
8544 -- operation if not needed.
8545
8546 if Restriction_Check_Required (SPARK_05)
8547 and then Base_Type (T) /= Standard_String
8548 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
8549 and then Etype (L) /= Any_Composite -- or else L in error
8550 and then Etype (R) /= Any_Composite -- or else R in error
8551 and then not Matching_Static_Array_Bounds (Etype (L), Etype (R))
8552 then
8553 Check_SPARK_05_Restriction
8554 ("array types should have matching static bounds", N);
8555 end if;
8556 end if;
8557
8558 -- If the unique type is a class-wide type then it will be expanded
8559 -- into a dispatching call to the predefined primitive. Therefore we
8560 -- check here for potential violation of such restriction.
8561
8562 if Is_Class_Wide_Type (T) then
8563 Check_Restriction (No_Dispatching_Calls, N);
8564 end if;
8565
8566 -- Only warn for redundant equality comparison to True for objects
8567 -- (e.g. "X = True") and operations (e.g. "(X < Y) = True"). For
8568 -- other expressions, it may be a matter of preference to write
8569 -- "Expr = True" or "Expr".
8570
8571 if Warn_On_Redundant_Constructs
8572 and then Comes_From_Source (N)
8573 and then Comes_From_Source (R)
8574 and then Is_Entity_Name (R)
8575 and then Entity (R) = Standard_True
8576 and then
8577 ((Is_Entity_Name (L) and then Is_Object (Entity (L)))
8578 or else
8579 Nkind (L) in N_Op)
8580 then
8581 Error_Msg_N -- CODEFIX
8582 ("?r?comparison with True is redundant!", N);
8583 Explain_Redundancy (Original_Node (R));
8584 end if;
8585
8586 -- If the equality is overloaded and the operands have resolved
8587 -- properly, set the proper equality operator on the node. The
8588 -- current setting is the first one found during analysis, which
8589 -- is not necessarily the one to which the node has resolved.
8590
8591 if Is_Overloaded (N) then
8592 declare
8593 I : Interp_Index;
8594 It : Interp;
8595
8596 begin
8597 Get_First_Interp (N, I, It);
8598
8599 -- If the equality is user-defined, the type of the operands
8600 -- matches that of the formals. For a predefined operator,
8601 -- it is the scope that matters, given that the predefined
8602 -- equality has Any_Type formals. In either case the result
8603 -- type (most often Boolean) must match the context. The scope
8604 -- is either that of the type, if there is a generated equality
8605 -- (when there is an equality for the component type), or else
8606 -- Standard otherwise.
8607
8608 while Present (It.Typ) loop
8609 if Etype (It.Nam) = Typ
8610 and then
8611 (Etype (First_Entity (It.Nam)) = Etype (L)
8612 or else Scope (It.Nam) = Standard_Standard
8613 or else Scope (It.Nam) = Scope (T))
8614 then
8615 Set_Entity (N, It.Nam);
8616
8617 Set_Is_Overloaded (N, False);
8618 exit;
8619 end if;
8620
8621 Get_Next_Interp (I, It);
8622 end loop;
8623
8624 -- If expansion is active and this is an inherited operation,
8625 -- replace it with its ancestor. This must not be done during
8626 -- preanalysis because the type may not be frozen yet, as when
8627 -- the context is a precondition or postcondition.
8628
8629 if Present (Alias (Entity (N))) and then Expander_Active then
8630 Set_Entity (N, Alias (Entity (N)));
8631 end if;
8632 end;
8633 end if;
8634
8635 Check_Unset_Reference (L);
8636 Check_Unset_Reference (R);
8637 Generate_Operator_Reference (N, T);
8638 Check_Low_Bound_Tested (N);
8639
8640 -- If this is an inequality, it may be the implicit inequality
8641 -- created for a user-defined operation, in which case the corres-
8642 -- ponding equality operation is not intrinsic, and the operation
8643 -- cannot be constant-folded. Else fold.
8644
8645 if Nkind (N) = N_Op_Eq
8646 or else Comes_From_Source (Entity (N))
8647 or else Ekind (Entity (N)) = E_Operator
8648 or else Is_Intrinsic_Subprogram
8649 (Corresponding_Equality (Entity (N)))
8650 then
8651 Analyze_Dimension (N);
8652 Eval_Relational_Op (N);
8653
8654 elsif Nkind (N) = N_Op_Ne
8655 and then Is_Abstract_Subprogram (Entity (N))
8656 then
8657 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
8658 end if;
8659
8660 -- Ada 2005: If one operand is an anonymous access type, convert the
8661 -- other operand to it, to ensure that the underlying types match in
8662 -- the back-end. Same for access_to_subprogram, and the conversion
8663 -- verifies that the types are subtype conformant.
8664
8665 -- We apply the same conversion in the case one of the operands is a
8666 -- private subtype of the type of the other.
8667
8668 -- Why the Expander_Active test here ???
8669
8670 if Expander_Active
8671 and then
8672 (Ekind_In (T, E_Anonymous_Access_Type,
8673 E_Anonymous_Access_Subprogram_Type)
8674 or else Is_Private_Type (T))
8675 then
8676 if Etype (L) /= T then
8677 Rewrite (L,
8678 Make_Unchecked_Type_Conversion (Sloc (L),
8679 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
8680 Expression => Relocate_Node (L)));
8681 Analyze_And_Resolve (L, T);
8682 end if;
8683
8684 if (Etype (R)) /= T then
8685 Rewrite (R,
8686 Make_Unchecked_Type_Conversion (Sloc (R),
8687 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
8688 Expression => Relocate_Node (R)));
8689 Analyze_And_Resolve (R, T);
8690 end if;
8691 end if;
8692 end if;
8693 end Resolve_Equality_Op;
8694
8695 ----------------------------------
8696 -- Resolve_Explicit_Dereference --
8697 ----------------------------------
8698
8699 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
8700 Loc : constant Source_Ptr := Sloc (N);
8701 New_N : Node_Id;
8702 P : constant Node_Id := Prefix (N);
8703
8704 P_Typ : Entity_Id;
8705 -- The candidate prefix type, if overloaded
8706
8707 I : Interp_Index;
8708 It : Interp;
8709
8710 begin
8711 Check_Fully_Declared_Prefix (Typ, P);
8712 P_Typ := Empty;
8713
8714 -- A useful optimization: check whether the dereference denotes an
8715 -- element of a container, and if so rewrite it as a call to the
8716 -- corresponding Element function.
8717
8718 -- Disabled for now, on advice of ARG. A more restricted form of the
8719 -- predicate might be acceptable ???
8720
8721 -- if Is_Container_Element (N) then
8722 -- return;
8723 -- end if;
8724
8725 if Is_Overloaded (P) then
8726
8727 -- Use the context type to select the prefix that has the correct
8728 -- designated type. Keep the first match, which will be the inner-
8729 -- most.
8730
8731 Get_First_Interp (P, I, It);
8732
8733 while Present (It.Typ) loop
8734 if Is_Access_Type (It.Typ)
8735 and then Covers (Typ, Designated_Type (It.Typ))
8736 then
8737 if No (P_Typ) then
8738 P_Typ := It.Typ;
8739 end if;
8740
8741 -- Remove access types that do not match, but preserve access
8742 -- to subprogram interpretations, in case a further dereference
8743 -- is needed (see below).
8744
8745 elsif Ekind (It.Typ) /= E_Access_Subprogram_Type then
8746 Remove_Interp (I);
8747 end if;
8748
8749 Get_Next_Interp (I, It);
8750 end loop;
8751
8752 if Present (P_Typ) then
8753 Resolve (P, P_Typ);
8754 Set_Etype (N, Designated_Type (P_Typ));
8755
8756 else
8757 -- If no interpretation covers the designated type of the prefix,
8758 -- this is the pathological case where not all implementations of
8759 -- the prefix allow the interpretation of the node as a call. Now
8760 -- that the expected type is known, Remove other interpretations
8761 -- from prefix, rewrite it as a call, and resolve again, so that
8762 -- the proper call node is generated.
8763
8764 Get_First_Interp (P, I, It);
8765 while Present (It.Typ) loop
8766 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
8767 Remove_Interp (I);
8768 end if;
8769
8770 Get_Next_Interp (I, It);
8771 end loop;
8772
8773 New_N :=
8774 Make_Function_Call (Loc,
8775 Name =>
8776 Make_Explicit_Dereference (Loc,
8777 Prefix => P),
8778 Parameter_Associations => New_List);
8779
8780 Save_Interps (N, New_N);
8781 Rewrite (N, New_N);
8782 Analyze_And_Resolve (N, Typ);
8783 return;
8784 end if;
8785
8786 -- If not overloaded, resolve P with its own type
8787
8788 else
8789 Resolve (P);
8790 end if;
8791
8792 -- If the prefix might be null, add an access check
8793
8794 if Is_Access_Type (Etype (P))
8795 and then not Can_Never_Be_Null (Etype (P))
8796 then
8797 Apply_Access_Check (N);
8798 end if;
8799
8800 -- If the designated type is a packed unconstrained array type, and the
8801 -- explicit dereference is not in the context of an attribute reference,
8802 -- then we must compute and set the actual subtype, since it is needed
8803 -- by Gigi. The reason we exclude the attribute case is that this is
8804 -- handled fine by Gigi, and in fact we use such attributes to build the
8805 -- actual subtype. We also exclude generated code (which builds actual
8806 -- subtypes directly if they are needed).
8807
8808 if Is_Array_Type (Etype (N))
8809 and then Is_Packed (Etype (N))
8810 and then not Is_Constrained (Etype (N))
8811 and then Nkind (Parent (N)) /= N_Attribute_Reference
8812 and then Comes_From_Source (N)
8813 then
8814 Set_Etype (N, Get_Actual_Subtype (N));
8815 end if;
8816
8817 Analyze_Dimension (N);
8818
8819 -- Note: No Eval processing is required for an explicit dereference,
8820 -- because such a name can never be static.
8821
8822 end Resolve_Explicit_Dereference;
8823
8824 -------------------------------------
8825 -- Resolve_Expression_With_Actions --
8826 -------------------------------------
8827
8828 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
8829 begin
8830 Set_Etype (N, Typ);
8831
8832 -- If N has no actions, and its expression has been constant folded,
8833 -- then rewrite N as just its expression. Note, we can't do this in
8834 -- the general case of Is_Empty_List (Actions (N)) as this would cause
8835 -- Expression (N) to be expanded again.
8836
8837 if Is_Empty_List (Actions (N))
8838 and then Compile_Time_Known_Value (Expression (N))
8839 then
8840 Rewrite (N, Expression (N));
8841 end if;
8842 end Resolve_Expression_With_Actions;
8843
8844 ----------------------------------
8845 -- Resolve_Generalized_Indexing --
8846 ----------------------------------
8847
8848 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id) is
8849 Indexing : constant Node_Id := Generalized_Indexing (N);
8850 Call : Node_Id;
8851 Indexes : List_Id;
8852 Pref : Node_Id;
8853
8854 begin
8855 -- In ASIS mode, propagate the information about the indexes back to
8856 -- to the original indexing node. The generalized indexing is either
8857 -- a function call, or a dereference of one. The actuals include the
8858 -- prefix of the original node, which is the container expression.
8859
8860 if ASIS_Mode then
8861 Resolve (Indexing, Typ);
8862 Set_Etype (N, Etype (Indexing));
8863 Set_Is_Overloaded (N, False);
8864
8865 Call := Indexing;
8866 while Nkind_In (Call, N_Explicit_Dereference, N_Selected_Component)
8867 loop
8868 Call := Prefix (Call);
8869 end loop;
8870
8871 if Nkind (Call) = N_Function_Call then
8872 Indexes := New_Copy_List (Parameter_Associations (Call));
8873 Pref := Remove_Head (Indexes);
8874 Set_Expressions (N, Indexes);
8875
8876 -- If expression is to be reanalyzed, reset Generalized_Indexing
8877 -- to recreate call node, as is the case when the expression is
8878 -- part of an expression function.
8879
8880 if In_Spec_Expression then
8881 Set_Generalized_Indexing (N, Empty);
8882 end if;
8883
8884 Set_Prefix (N, Pref);
8885 end if;
8886
8887 else
8888 Rewrite (N, Indexing);
8889 Resolve (N, Typ);
8890 end if;
8891 end Resolve_Generalized_Indexing;
8892
8893 ---------------------------
8894 -- Resolve_If_Expression --
8895 ---------------------------
8896
8897 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id) is
8898 procedure Apply_Check (Expr : Node_Id);
8899 -- When a dependent expression is of a subtype different from
8900 -- the context subtype, then insert a qualification to ensure
8901 -- the generation of a constraint check. This was previously
8902 -- for scalar types. For array types apply a length check, given
8903 -- that the context in general allows sliding, while a qualified
8904 -- expression forces equality of bounds.
8905
8906 -----------------
8907 -- Apply_Check --
8908 -----------------
8909
8910 procedure Apply_Check (Expr : Node_Id) is
8911 Expr_Typ : constant Entity_Id := Etype (Expr);
8912 Loc : constant Source_Ptr := Sloc (Expr);
8913
8914 begin
8915 if Expr_Typ = Typ
8916 or else Is_Tagged_Type (Typ)
8917 or else Is_Access_Type (Typ)
8918 or else not Is_Constrained (Typ)
8919 or else Inside_A_Generic
8920 then
8921 null;
8922
8923 elsif Is_Array_Type (Typ) then
8924 Apply_Length_Check (Expr, Typ);
8925
8926 else
8927 Rewrite (Expr,
8928 Make_Qualified_Expression (Loc,
8929 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
8930 Expression => Relocate_Node (Expr)));
8931
8932 Analyze_And_Resolve (Expr, Typ);
8933 end if;
8934 end Apply_Check;
8935
8936 -- Local variables
8937
8938 Condition : constant Node_Id := First (Expressions (N));
8939 Else_Expr : Node_Id;
8940 Then_Expr : Node_Id;
8941
8942 -- Start of processing for Resolve_If_Expression
8943
8944 begin
8945 -- Defend against malformed expressions
8946
8947 if No (Condition) then
8948 return;
8949 end if;
8950
8951 Then_Expr := Next (Condition);
8952
8953 if No (Then_Expr) then
8954 return;
8955 end if;
8956
8957 Else_Expr := Next (Then_Expr);
8958
8959 Resolve (Condition, Any_Boolean);
8960 Resolve (Then_Expr, Typ);
8961 Apply_Check (Then_Expr);
8962
8963 -- If ELSE expression present, just resolve using the determined type
8964 -- If type is universal, resolve to any member of the class.
8965
8966 if Present (Else_Expr) then
8967 if Typ = Universal_Integer then
8968 Resolve (Else_Expr, Any_Integer);
8969
8970 elsif Typ = Universal_Real then
8971 Resolve (Else_Expr, Any_Real);
8972
8973 else
8974 Resolve (Else_Expr, Typ);
8975 end if;
8976
8977 Apply_Check (Else_Expr);
8978
8979 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
8980 -- dynamically tagged must be known statically.
8981
8982 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
8983 if Is_Dynamically_Tagged (Then_Expr) /=
8984 Is_Dynamically_Tagged (Else_Expr)
8985 then
8986 Error_Msg_N ("all or none of the dependent expressions "
8987 & "can be dynamically tagged", N);
8988 end if;
8989 end if;
8990
8991 -- If no ELSE expression is present, root type must be Standard.Boolean
8992 -- and we provide a Standard.True result converted to the appropriate
8993 -- Boolean type (in case it is a derived boolean type).
8994
8995 elsif Root_Type (Typ) = Standard_Boolean then
8996 Else_Expr :=
8997 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
8998 Analyze_And_Resolve (Else_Expr, Typ);
8999 Append_To (Expressions (N), Else_Expr);
9000
9001 else
9002 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
9003 Append_To (Expressions (N), Error);
9004 end if;
9005
9006 Set_Etype (N, Typ);
9007
9008 if not Error_Posted (N) then
9009 Eval_If_Expression (N);
9010 end if;
9011
9012 Analyze_Dimension (N);
9013 end Resolve_If_Expression;
9014
9015 -------------------------------
9016 -- Resolve_Indexed_Component --
9017 -------------------------------
9018
9019 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
9020 Name : constant Node_Id := Prefix (N);
9021 Expr : Node_Id;
9022 Array_Type : Entity_Id := Empty; -- to prevent junk warning
9023 Index : Node_Id;
9024
9025 begin
9026 if Present (Generalized_Indexing (N)) then
9027 Resolve_Generalized_Indexing (N, Typ);
9028 return;
9029 end if;
9030
9031 if Is_Overloaded (Name) then
9032
9033 -- Use the context type to select the prefix that yields the correct
9034 -- component type.
9035
9036 declare
9037 I : Interp_Index;
9038 It : Interp;
9039 I1 : Interp_Index := 0;
9040 P : constant Node_Id := Prefix (N);
9041 Found : Boolean := False;
9042
9043 begin
9044 Get_First_Interp (P, I, It);
9045 while Present (It.Typ) loop
9046 if (Is_Array_Type (It.Typ)
9047 and then Covers (Typ, Component_Type (It.Typ)))
9048 or else (Is_Access_Type (It.Typ)
9049 and then Is_Array_Type (Designated_Type (It.Typ))
9050 and then
9051 Covers
9052 (Typ,
9053 Component_Type (Designated_Type (It.Typ))))
9054 then
9055 if Found then
9056 It := Disambiguate (P, I1, I, Any_Type);
9057
9058 if It = No_Interp then
9059 Error_Msg_N ("ambiguous prefix for indexing", N);
9060 Set_Etype (N, Typ);
9061 return;
9062
9063 else
9064 Found := True;
9065 Array_Type := It.Typ;
9066 I1 := I;
9067 end if;
9068
9069 else
9070 Found := True;
9071 Array_Type := It.Typ;
9072 I1 := I;
9073 end if;
9074 end if;
9075
9076 Get_Next_Interp (I, It);
9077 end loop;
9078 end;
9079
9080 else
9081 Array_Type := Etype (Name);
9082 end if;
9083
9084 Resolve (Name, Array_Type);
9085 Array_Type := Get_Actual_Subtype_If_Available (Name);
9086
9087 -- If prefix is access type, dereference to get real array type.
9088 -- Note: we do not apply an access check because the expander always
9089 -- introduces an explicit dereference, and the check will happen there.
9090
9091 if Is_Access_Type (Array_Type) then
9092 Array_Type := Designated_Type (Array_Type);
9093 end if;
9094
9095 -- If name was overloaded, set component type correctly now
9096 -- If a misplaced call to an entry family (which has no index types)
9097 -- return. Error will be diagnosed from calling context.
9098
9099 if Is_Array_Type (Array_Type) then
9100 Set_Etype (N, Component_Type (Array_Type));
9101 else
9102 return;
9103 end if;
9104
9105 Index := First_Index (Array_Type);
9106 Expr := First (Expressions (N));
9107
9108 -- The prefix may have resolved to a string literal, in which case its
9109 -- etype has a special representation. This is only possible currently
9110 -- if the prefix is a static concatenation, written in functional
9111 -- notation.
9112
9113 if Ekind (Array_Type) = E_String_Literal_Subtype then
9114 Resolve (Expr, Standard_Positive);
9115
9116 else
9117 while Present (Index) and Present (Expr) loop
9118 Resolve (Expr, Etype (Index));
9119 Check_Unset_Reference (Expr);
9120
9121 if Is_Scalar_Type (Etype (Expr)) then
9122 Apply_Scalar_Range_Check (Expr, Etype (Index));
9123 else
9124 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
9125 end if;
9126
9127 Next_Index (Index);
9128 Next (Expr);
9129 end loop;
9130 end if;
9131
9132 Analyze_Dimension (N);
9133
9134 -- Do not generate the warning on suspicious index if we are analyzing
9135 -- package Ada.Tags; otherwise we will report the warning with the
9136 -- Prims_Ptr field of the dispatch table.
9137
9138 if Scope (Etype (Prefix (N))) = Standard_Standard
9139 or else not
9140 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
9141 Ada_Tags)
9142 then
9143 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
9144 Eval_Indexed_Component (N);
9145 end if;
9146
9147 -- If the array type is atomic, and the component is not atomic, then
9148 -- this is worth a warning, since we have a situation where the access
9149 -- to the component may cause extra read/writes of the atomic array
9150 -- object, or partial word accesses, which could be unexpected.
9151
9152 if Nkind (N) = N_Indexed_Component
9153 and then Is_Atomic_Ref_With_Address (N)
9154 and then not (Has_Atomic_Components (Array_Type)
9155 or else (Is_Entity_Name (Prefix (N))
9156 and then Has_Atomic_Components
9157 (Entity (Prefix (N)))))
9158 and then not Is_Atomic (Component_Type (Array_Type))
9159 then
9160 Error_Msg_N
9161 ("??access to non-atomic component of atomic array", Prefix (N));
9162 Error_Msg_N
9163 ("??\may cause unexpected accesses to atomic object", Prefix (N));
9164 end if;
9165 end Resolve_Indexed_Component;
9166
9167 -----------------------------
9168 -- Resolve_Integer_Literal --
9169 -----------------------------
9170
9171 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
9172 begin
9173 Set_Etype (N, Typ);
9174 Eval_Integer_Literal (N);
9175 end Resolve_Integer_Literal;
9176
9177 --------------------------------
9178 -- Resolve_Intrinsic_Operator --
9179 --------------------------------
9180
9181 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
9182 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
9183 Op : Entity_Id;
9184 Arg1 : Node_Id;
9185 Arg2 : Node_Id;
9186
9187 function Convert_Operand (Opnd : Node_Id) return Node_Id;
9188 -- If the operand is a literal, it cannot be the expression in a
9189 -- conversion. Use a qualified expression instead.
9190
9191 ---------------------
9192 -- Convert_Operand --
9193 ---------------------
9194
9195 function Convert_Operand (Opnd : Node_Id) return Node_Id is
9196 Loc : constant Source_Ptr := Sloc (Opnd);
9197 Res : Node_Id;
9198
9199 begin
9200 if Nkind_In (Opnd, N_Integer_Literal, N_Real_Literal) then
9201 Res :=
9202 Make_Qualified_Expression (Loc,
9203 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
9204 Expression => Relocate_Node (Opnd));
9205 Analyze (Res);
9206
9207 else
9208 Res := Unchecked_Convert_To (Btyp, Opnd);
9209 end if;
9210
9211 return Res;
9212 end Convert_Operand;
9213
9214 -- Start of processing for Resolve_Intrinsic_Operator
9215
9216 begin
9217 -- We must preserve the original entity in a generic setting, so that
9218 -- the legality of the operation can be verified in an instance.
9219
9220 if not Expander_Active then
9221 return;
9222 end if;
9223
9224 Op := Entity (N);
9225 while Scope (Op) /= Standard_Standard loop
9226 Op := Homonym (Op);
9227 pragma Assert (Present (Op));
9228 end loop;
9229
9230 Set_Entity (N, Op);
9231 Set_Is_Overloaded (N, False);
9232
9233 -- If the result or operand types are private, rewrite with unchecked
9234 -- conversions on the operands and the result, to expose the proper
9235 -- underlying numeric type.
9236
9237 if Is_Private_Type (Typ)
9238 or else Is_Private_Type (Etype (Left_Opnd (N)))
9239 or else Is_Private_Type (Etype (Right_Opnd (N)))
9240 then
9241 Arg1 := Convert_Operand (Left_Opnd (N));
9242
9243 if Nkind (N) = N_Op_Expon then
9244 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
9245 else
9246 Arg2 := Convert_Operand (Right_Opnd (N));
9247 end if;
9248
9249 if Nkind (Arg1) = N_Type_Conversion then
9250 Save_Interps (Left_Opnd (N), Expression (Arg1));
9251 end if;
9252
9253 if Nkind (Arg2) = N_Type_Conversion then
9254 Save_Interps (Right_Opnd (N), Expression (Arg2));
9255 end if;
9256
9257 Set_Left_Opnd (N, Arg1);
9258 Set_Right_Opnd (N, Arg2);
9259
9260 Set_Etype (N, Btyp);
9261 Rewrite (N, Unchecked_Convert_To (Typ, N));
9262 Resolve (N, Typ);
9263
9264 elsif Typ /= Etype (Left_Opnd (N))
9265 or else Typ /= Etype (Right_Opnd (N))
9266 then
9267 -- Add explicit conversion where needed, and save interpretations in
9268 -- case operands are overloaded.
9269
9270 Arg1 := Convert_To (Typ, Left_Opnd (N));
9271 Arg2 := Convert_To (Typ, Right_Opnd (N));
9272
9273 if Nkind (Arg1) = N_Type_Conversion then
9274 Save_Interps (Left_Opnd (N), Expression (Arg1));
9275 else
9276 Save_Interps (Left_Opnd (N), Arg1);
9277 end if;
9278
9279 if Nkind (Arg2) = N_Type_Conversion then
9280 Save_Interps (Right_Opnd (N), Expression (Arg2));
9281 else
9282 Save_Interps (Right_Opnd (N), Arg2);
9283 end if;
9284
9285 Rewrite (Left_Opnd (N), Arg1);
9286 Rewrite (Right_Opnd (N), Arg2);
9287 Analyze (Arg1);
9288 Analyze (Arg2);
9289 Resolve_Arithmetic_Op (N, Typ);
9290
9291 else
9292 Resolve_Arithmetic_Op (N, Typ);
9293 end if;
9294 end Resolve_Intrinsic_Operator;
9295
9296 --------------------------------------
9297 -- Resolve_Intrinsic_Unary_Operator --
9298 --------------------------------------
9299
9300 procedure Resolve_Intrinsic_Unary_Operator
9301 (N : Node_Id;
9302 Typ : Entity_Id)
9303 is
9304 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
9305 Op : Entity_Id;
9306 Arg2 : Node_Id;
9307
9308 begin
9309 Op := Entity (N);
9310 while Scope (Op) /= Standard_Standard loop
9311 Op := Homonym (Op);
9312 pragma Assert (Present (Op));
9313 end loop;
9314
9315 Set_Entity (N, Op);
9316
9317 if Is_Private_Type (Typ) then
9318 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
9319 Save_Interps (Right_Opnd (N), Expression (Arg2));
9320
9321 Set_Right_Opnd (N, Arg2);
9322
9323 Set_Etype (N, Btyp);
9324 Rewrite (N, Unchecked_Convert_To (Typ, N));
9325 Resolve (N, Typ);
9326
9327 else
9328 Resolve_Unary_Op (N, Typ);
9329 end if;
9330 end Resolve_Intrinsic_Unary_Operator;
9331
9332 ------------------------
9333 -- Resolve_Logical_Op --
9334 ------------------------
9335
9336 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
9337 B_Typ : Entity_Id;
9338
9339 begin
9340 Check_No_Direct_Boolean_Operators (N);
9341
9342 -- Predefined operations on scalar types yield the base type. On the
9343 -- other hand, logical operations on arrays yield the type of the
9344 -- arguments (and the context).
9345
9346 if Is_Array_Type (Typ) then
9347 B_Typ := Typ;
9348 else
9349 B_Typ := Base_Type (Typ);
9350 end if;
9351
9352 -- The following test is required because the operands of the operation
9353 -- may be literals, in which case the resulting type appears to be
9354 -- compatible with a signed integer type, when in fact it is compatible
9355 -- only with modular types. If the context itself is universal, the
9356 -- operation is illegal.
9357
9358 if not Valid_Boolean_Arg (Typ) then
9359 Error_Msg_N ("invalid context for logical operation", N);
9360 Set_Etype (N, Any_Type);
9361 return;
9362
9363 elsif Typ = Any_Modular then
9364 Error_Msg_N
9365 ("no modular type available in this context", N);
9366 Set_Etype (N, Any_Type);
9367 return;
9368
9369 elsif Is_Modular_Integer_Type (Typ)
9370 and then Etype (Left_Opnd (N)) = Universal_Integer
9371 and then Etype (Right_Opnd (N)) = Universal_Integer
9372 then
9373 Check_For_Visible_Operator (N, B_Typ);
9374 end if;
9375
9376 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
9377 -- is active and the result type is standard Boolean (do not mess with
9378 -- ops that return a nonstandard Boolean type, because something strange
9379 -- is going on).
9380
9381 -- Note: you might expect this replacement to be done during expansion,
9382 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
9383 -- is used, no part of the right operand of an "and" or "or" operator
9384 -- should be executed if the left operand would short-circuit the
9385 -- evaluation of the corresponding "and then" or "or else". If we left
9386 -- the replacement to expansion time, then run-time checks associated
9387 -- with such operands would be evaluated unconditionally, due to being
9388 -- before the condition prior to the rewriting as short-circuit forms
9389 -- during expansion.
9390
9391 if Short_Circuit_And_Or
9392 and then B_Typ = Standard_Boolean
9393 and then Nkind_In (N, N_Op_And, N_Op_Or)
9394 then
9395 -- Mark the corresponding putative SCO operator as truly a logical
9396 -- (and short-circuit) operator.
9397
9398 if Generate_SCO and then Comes_From_Source (N) then
9399 Set_SCO_Logical_Operator (N);
9400 end if;
9401
9402 if Nkind (N) = N_Op_And then
9403 Rewrite (N,
9404 Make_And_Then (Sloc (N),
9405 Left_Opnd => Relocate_Node (Left_Opnd (N)),
9406 Right_Opnd => Relocate_Node (Right_Opnd (N))));
9407 Analyze_And_Resolve (N, B_Typ);
9408
9409 -- Case of OR changed to OR ELSE
9410
9411 else
9412 Rewrite (N,
9413 Make_Or_Else (Sloc (N),
9414 Left_Opnd => Relocate_Node (Left_Opnd (N)),
9415 Right_Opnd => Relocate_Node (Right_Opnd (N))));
9416 Analyze_And_Resolve (N, B_Typ);
9417 end if;
9418
9419 -- Return now, since analysis of the rewritten ops will take care of
9420 -- other reference bookkeeping and expression folding.
9421
9422 return;
9423 end if;
9424
9425 Resolve (Left_Opnd (N), B_Typ);
9426 Resolve (Right_Opnd (N), B_Typ);
9427
9428 Check_Unset_Reference (Left_Opnd (N));
9429 Check_Unset_Reference (Right_Opnd (N));
9430
9431 Set_Etype (N, B_Typ);
9432 Generate_Operator_Reference (N, B_Typ);
9433 Eval_Logical_Op (N);
9434
9435 -- In SPARK, logical operations AND, OR and XOR for arrays are defined
9436 -- only when both operands have same static lower and higher bounds. Of
9437 -- course the types have to match, so only check if operands are
9438 -- compatible and the node itself has no errors.
9439
9440 if Is_Array_Type (B_Typ)
9441 and then Nkind (N) in N_Binary_Op
9442 then
9443 declare
9444 Left_Typ : constant Node_Id := Etype (Left_Opnd (N));
9445 Right_Typ : constant Node_Id := Etype (Right_Opnd (N));
9446
9447 begin
9448 -- Protect call to Matching_Static_Array_Bounds to avoid costly
9449 -- operation if not needed.
9450
9451 if Restriction_Check_Required (SPARK_05)
9452 and then Base_Type (Left_Typ) = Base_Type (Right_Typ)
9453 and then Left_Typ /= Any_Composite -- or Left_Opnd in error
9454 and then Right_Typ /= Any_Composite -- or Right_Opnd in error
9455 and then not Matching_Static_Array_Bounds (Left_Typ, Right_Typ)
9456 then
9457 Check_SPARK_05_Restriction
9458 ("array types should have matching static bounds", N);
9459 end if;
9460 end;
9461 end if;
9462 end Resolve_Logical_Op;
9463
9464 ---------------------------
9465 -- Resolve_Membership_Op --
9466 ---------------------------
9467
9468 -- The context can only be a boolean type, and does not determine the
9469 -- arguments. Arguments should be unambiguous, but the preference rule for
9470 -- universal types applies.
9471
9472 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
9473 pragma Warnings (Off, Typ);
9474
9475 L : constant Node_Id := Left_Opnd (N);
9476 R : constant Node_Id := Right_Opnd (N);
9477 T : Entity_Id;
9478
9479 procedure Resolve_Set_Membership;
9480 -- Analysis has determined a unique type for the left operand. Use it to
9481 -- resolve the disjuncts.
9482
9483 ----------------------------
9484 -- Resolve_Set_Membership --
9485 ----------------------------
9486
9487 procedure Resolve_Set_Membership is
9488 Alt : Node_Id;
9489 Ltyp : Entity_Id;
9490
9491 begin
9492 -- If the left operand is overloaded, find type compatible with not
9493 -- overloaded alternative of the right operand.
9494
9495 if Is_Overloaded (L) then
9496 Ltyp := Empty;
9497 Alt := First (Alternatives (N));
9498 while Present (Alt) loop
9499 if not Is_Overloaded (Alt) then
9500 Ltyp := Intersect_Types (L, Alt);
9501 exit;
9502 else
9503 Next (Alt);
9504 end if;
9505 end loop;
9506
9507 -- Unclear how to resolve expression if all alternatives are also
9508 -- overloaded.
9509
9510 if No (Ltyp) then
9511 Error_Msg_N ("ambiguous expression", N);
9512 end if;
9513
9514 else
9515 Ltyp := Etype (L);
9516 end if;
9517
9518 Resolve (L, Ltyp);
9519
9520 Alt := First (Alternatives (N));
9521 while Present (Alt) loop
9522
9523 -- Alternative is an expression, a range
9524 -- or a subtype mark.
9525
9526 if not Is_Entity_Name (Alt)
9527 or else not Is_Type (Entity (Alt))
9528 then
9529 Resolve (Alt, Ltyp);
9530 end if;
9531
9532 Next (Alt);
9533 end loop;
9534
9535 -- Check for duplicates for discrete case
9536
9537 if Is_Discrete_Type (Ltyp) then
9538 declare
9539 type Ent is record
9540 Alt : Node_Id;
9541 Val : Uint;
9542 end record;
9543
9544 Alts : array (0 .. List_Length (Alternatives (N))) of Ent;
9545 Nalts : Nat;
9546
9547 begin
9548 -- Loop checking duplicates. This is quadratic, but giant sets
9549 -- are unlikely in this context so it's a reasonable choice.
9550
9551 Nalts := 0;
9552 Alt := First (Alternatives (N));
9553 while Present (Alt) loop
9554 if Is_OK_Static_Expression (Alt)
9555 and then (Nkind_In (Alt, N_Integer_Literal,
9556 N_Character_Literal)
9557 or else Nkind (Alt) in N_Has_Entity)
9558 then
9559 Nalts := Nalts + 1;
9560 Alts (Nalts) := (Alt, Expr_Value (Alt));
9561
9562 for J in 1 .. Nalts - 1 loop
9563 if Alts (J).Val = Alts (Nalts).Val then
9564 Error_Msg_Sloc := Sloc (Alts (J).Alt);
9565 Error_Msg_N ("duplicate of value given#??", Alt);
9566 end if;
9567 end loop;
9568 end if;
9569
9570 Alt := Next (Alt);
9571 end loop;
9572 end;
9573 end if;
9574
9575 -- RM 4.5.2 (28.1/3) specifies that for types other than records or
9576 -- limited types, evaluation of a membership test uses the predefined
9577 -- equality for the type. This may be confusing to users, and the
9578 -- following warning appears useful for the most common case.
9579
9580 if Is_Scalar_Type (Ltyp)
9581 and then Present (Get_User_Defined_Eq (Ltyp))
9582 then
9583 Error_Msg_NE
9584 ("membership test on& uses predefined equality?", N, Ltyp);
9585 Error_Msg_N
9586 ("\even if user-defined equality exists (RM 4.5.2 (28.1/3)?", N);
9587 end if;
9588 end Resolve_Set_Membership;
9589
9590 -- Start of processing for Resolve_Membership_Op
9591
9592 begin
9593 if L = Error or else R = Error then
9594 return;
9595 end if;
9596
9597 if Present (Alternatives (N)) then
9598 Resolve_Set_Membership;
9599 goto SM_Exit;
9600
9601 elsif not Is_Overloaded (R)
9602 and then
9603 (Etype (R) = Universal_Integer
9604 or else
9605 Etype (R) = Universal_Real)
9606 and then Is_Overloaded (L)
9607 then
9608 T := Etype (R);
9609
9610 -- Ada 2005 (AI-251): Support the following case:
9611
9612 -- type I is interface;
9613 -- type T is tagged ...
9614
9615 -- function Test (O : I'Class) is
9616 -- begin
9617 -- return O in T'Class.
9618 -- end Test;
9619
9620 -- In this case we have nothing else to do. The membership test will be
9621 -- done at run time.
9622
9623 elsif Ada_Version >= Ada_2005
9624 and then Is_Class_Wide_Type (Etype (L))
9625 and then Is_Interface (Etype (L))
9626 and then not Is_Interface (Etype (R))
9627 then
9628 return;
9629 else
9630 T := Intersect_Types (L, R);
9631 end if;
9632
9633 -- If mixed-mode operations are present and operands are all literal,
9634 -- the only interpretation involves Duration, which is probably not
9635 -- the intention of the programmer.
9636
9637 if T = Any_Fixed then
9638 T := Unique_Fixed_Point_Type (N);
9639
9640 if T = Any_Type then
9641 return;
9642 end if;
9643 end if;
9644
9645 Resolve (L, T);
9646 Check_Unset_Reference (L);
9647
9648 if Nkind (R) = N_Range
9649 and then not Is_Scalar_Type (T)
9650 then
9651 Error_Msg_N ("scalar type required for range", R);
9652 end if;
9653
9654 if Is_Entity_Name (R) then
9655 Freeze_Expression (R);
9656 else
9657 Resolve (R, T);
9658 Check_Unset_Reference (R);
9659 end if;
9660
9661 -- Here after resolving membership operation
9662
9663 <<SM_Exit>>
9664
9665 Eval_Membership_Op (N);
9666 end Resolve_Membership_Op;
9667
9668 ------------------
9669 -- Resolve_Null --
9670 ------------------
9671
9672 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
9673 Loc : constant Source_Ptr := Sloc (N);
9674
9675 begin
9676 -- Handle restriction against anonymous null access values This
9677 -- restriction can be turned off using -gnatdj.
9678
9679 -- Ada 2005 (AI-231): Remove restriction
9680
9681 if Ada_Version < Ada_2005
9682 and then not Debug_Flag_J
9683 and then Ekind (Typ) = E_Anonymous_Access_Type
9684 and then Comes_From_Source (N)
9685 then
9686 -- In the common case of a call which uses an explicitly null value
9687 -- for an access parameter, give specialized error message.
9688
9689 if Nkind (Parent (N)) in N_Subprogram_Call then
9690 Error_Msg_N
9691 ("null is not allowed as argument for an access parameter", N);
9692
9693 -- Standard message for all other cases (are there any?)
9694
9695 else
9696 Error_Msg_N
9697 ("null cannot be of an anonymous access type", N);
9698 end if;
9699 end if;
9700
9701 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
9702 -- assignment to a null-excluding object.
9703
9704 if Ada_Version >= Ada_2005
9705 and then Can_Never_Be_Null (Typ)
9706 and then Nkind (Parent (N)) = N_Assignment_Statement
9707 then
9708 if Inside_Init_Proc then
9709
9710 -- Decide whether to generate an if_statement around our
9711 -- null-excluding check to avoid them on certain internal object
9712 -- declarations by looking at the type the current Init_Proc
9713 -- belongs to.
9714
9715 -- Generate:
9716 -- if T1b_skip_null_excluding_check then
9717 -- [constraint_error "access check failed"]
9718 -- end if;
9719
9720 if Needs_Conditional_Null_Excluding_Check
9721 (Etype (First_Formal (Enclosing_Init_Proc)))
9722 then
9723 Insert_Action (N,
9724 Make_If_Statement (Loc,
9725 Condition =>
9726 Make_Identifier (Loc,
9727 New_External_Name
9728 (Chars (Typ), "_skip_null_excluding_check")),
9729 Then_Statements =>
9730 New_List (
9731 Make_Raise_Constraint_Error (Loc,
9732 Reason => CE_Access_Check_Failed))));
9733
9734 -- Otherwise, simply create the check
9735
9736 else
9737 Insert_Action (N,
9738 Make_Raise_Constraint_Error (Loc,
9739 Reason => CE_Access_Check_Failed));
9740 end if;
9741 else
9742 Insert_Action
9743 (Compile_Time_Constraint_Error (N,
9744 "(Ada 2005) null not allowed in null-excluding objects??"),
9745 Make_Raise_Constraint_Error (Loc,
9746 Reason => CE_Access_Check_Failed));
9747 end if;
9748 end if;
9749
9750 -- In a distributed context, null for a remote access to subprogram may
9751 -- need to be replaced with a special record aggregate. In this case,
9752 -- return after having done the transformation.
9753
9754 if (Ekind (Typ) = E_Record_Type
9755 or else Is_Remote_Access_To_Subprogram_Type (Typ))
9756 and then Remote_AST_Null_Value (N, Typ)
9757 then
9758 return;
9759 end if;
9760
9761 -- The null literal takes its type from the context
9762
9763 Set_Etype (N, Typ);
9764 end Resolve_Null;
9765
9766 -----------------------
9767 -- Resolve_Op_Concat --
9768 -----------------------
9769
9770 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
9771
9772 -- We wish to avoid deep recursion, because concatenations are often
9773 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
9774 -- operands nonrecursively until we find something that is not a simple
9775 -- concatenation (A in this case). We resolve that, and then walk back
9776 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
9777 -- to do the rest of the work at each level. The Parent pointers allow
9778 -- us to avoid recursion, and thus avoid running out of memory. See also
9779 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
9780
9781 NN : Node_Id := N;
9782 Op1 : Node_Id;
9783
9784 begin
9785 -- The following code is equivalent to:
9786
9787 -- Resolve_Op_Concat_First (NN, Typ);
9788 -- Resolve_Op_Concat_Arg (N, ...);
9789 -- Resolve_Op_Concat_Rest (N, Typ);
9790
9791 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
9792 -- operand is a concatenation.
9793
9794 -- Walk down left operands
9795
9796 loop
9797 Resolve_Op_Concat_First (NN, Typ);
9798 Op1 := Left_Opnd (NN);
9799 exit when not (Nkind (Op1) = N_Op_Concat
9800 and then not Is_Array_Type (Component_Type (Typ))
9801 and then Entity (Op1) = Entity (NN));
9802 NN := Op1;
9803 end loop;
9804
9805 -- Now (given the above example) NN is A&B and Op1 is A
9806
9807 -- First resolve Op1 ...
9808
9809 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
9810
9811 -- ... then walk NN back up until we reach N (where we started), calling
9812 -- Resolve_Op_Concat_Rest along the way.
9813
9814 loop
9815 Resolve_Op_Concat_Rest (NN, Typ);
9816 exit when NN = N;
9817 NN := Parent (NN);
9818 end loop;
9819
9820 if Base_Type (Etype (N)) /= Standard_String then
9821 Check_SPARK_05_Restriction
9822 ("result of concatenation should have type String", N);
9823 end if;
9824 end Resolve_Op_Concat;
9825
9826 ---------------------------
9827 -- Resolve_Op_Concat_Arg --
9828 ---------------------------
9829
9830 procedure Resolve_Op_Concat_Arg
9831 (N : Node_Id;
9832 Arg : Node_Id;
9833 Typ : Entity_Id;
9834 Is_Comp : Boolean)
9835 is
9836 Btyp : constant Entity_Id := Base_Type (Typ);
9837 Ctyp : constant Entity_Id := Component_Type (Typ);
9838
9839 begin
9840 if In_Instance then
9841 if Is_Comp
9842 or else (not Is_Overloaded (Arg)
9843 and then Etype (Arg) /= Any_Composite
9844 and then Covers (Ctyp, Etype (Arg)))
9845 then
9846 Resolve (Arg, Ctyp);
9847 else
9848 Resolve (Arg, Btyp);
9849 end if;
9850
9851 -- If both Array & Array and Array & Component are visible, there is a
9852 -- potential ambiguity that must be reported.
9853
9854 elsif Has_Compatible_Type (Arg, Ctyp) then
9855 if Nkind (Arg) = N_Aggregate
9856 and then Is_Composite_Type (Ctyp)
9857 then
9858 if Is_Private_Type (Ctyp) then
9859 Resolve (Arg, Btyp);
9860
9861 -- If the operation is user-defined and not overloaded use its
9862 -- profile. The operation may be a renaming, in which case it has
9863 -- been rewritten, and we want the original profile.
9864
9865 elsif not Is_Overloaded (N)
9866 and then Comes_From_Source (Entity (Original_Node (N)))
9867 and then Ekind (Entity (Original_Node (N))) = E_Function
9868 then
9869 Resolve (Arg,
9870 Etype
9871 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
9872 return;
9873
9874 -- Otherwise an aggregate may match both the array type and the
9875 -- component type.
9876
9877 else
9878 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
9879 Set_Etype (Arg, Any_Type);
9880 end if;
9881
9882 else
9883 if Is_Overloaded (Arg)
9884 and then Has_Compatible_Type (Arg, Typ)
9885 and then Etype (Arg) /= Any_Type
9886 then
9887 declare
9888 I : Interp_Index;
9889 It : Interp;
9890 Func : Entity_Id;
9891
9892 begin
9893 Get_First_Interp (Arg, I, It);
9894 Func := It.Nam;
9895 Get_Next_Interp (I, It);
9896
9897 -- Special-case the error message when the overloading is
9898 -- caused by a function that yields an array and can be
9899 -- called without parameters.
9900
9901 if It.Nam = Func then
9902 Error_Msg_Sloc := Sloc (Func);
9903 Error_Msg_N ("ambiguous call to function#", Arg);
9904 Error_Msg_NE
9905 ("\\interpretation as call yields&", Arg, Typ);
9906 Error_Msg_NE
9907 ("\\interpretation as indexing of call yields&",
9908 Arg, Component_Type (Typ));
9909
9910 else
9911 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
9912
9913 Get_First_Interp (Arg, I, It);
9914 while Present (It.Nam) loop
9915 Error_Msg_Sloc := Sloc (It.Nam);
9916
9917 if Base_Type (It.Typ) = Btyp
9918 or else
9919 Base_Type (It.Typ) = Base_Type (Ctyp)
9920 then
9921 Error_Msg_N -- CODEFIX
9922 ("\\possible interpretation#", Arg);
9923 end if;
9924
9925 Get_Next_Interp (I, It);
9926 end loop;
9927 end if;
9928 end;
9929 end if;
9930
9931 Resolve (Arg, Component_Type (Typ));
9932
9933 if Nkind (Arg) = N_String_Literal then
9934 Set_Etype (Arg, Component_Type (Typ));
9935 end if;
9936
9937 if Arg = Left_Opnd (N) then
9938 Set_Is_Component_Left_Opnd (N);
9939 else
9940 Set_Is_Component_Right_Opnd (N);
9941 end if;
9942 end if;
9943
9944 else
9945 Resolve (Arg, Btyp);
9946 end if;
9947
9948 -- Concatenation is restricted in SPARK: each operand must be either a
9949 -- string literal, the name of a string constant, a static character or
9950 -- string expression, or another concatenation. Arg cannot be a
9951 -- concatenation here as callers of Resolve_Op_Concat_Arg call it
9952 -- separately on each final operand, past concatenation operations.
9953
9954 if Is_Character_Type (Etype (Arg)) then
9955 if not Is_OK_Static_Expression (Arg) then
9956 Check_SPARK_05_Restriction
9957 ("character operand for concatenation should be static", Arg);
9958 end if;
9959
9960 elsif Is_String_Type (Etype (Arg)) then
9961 if not (Nkind_In (Arg, N_Identifier, N_Expanded_Name)
9962 and then Is_Constant_Object (Entity (Arg)))
9963 and then not Is_OK_Static_Expression (Arg)
9964 then
9965 Check_SPARK_05_Restriction
9966 ("string operand for concatenation should be static", Arg);
9967 end if;
9968
9969 -- Do not issue error on an operand that is neither a character nor a
9970 -- string, as the error is issued in Resolve_Op_Concat.
9971
9972 else
9973 null;
9974 end if;
9975
9976 Check_Unset_Reference (Arg);
9977 end Resolve_Op_Concat_Arg;
9978
9979 -----------------------------
9980 -- Resolve_Op_Concat_First --
9981 -----------------------------
9982
9983 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
9984 Btyp : constant Entity_Id := Base_Type (Typ);
9985 Op1 : constant Node_Id := Left_Opnd (N);
9986 Op2 : constant Node_Id := Right_Opnd (N);
9987
9988 begin
9989 -- The parser folds an enormous sequence of concatenations of string
9990 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
9991 -- in the right operand. If the expression resolves to a predefined "&"
9992 -- operator, all is well. Otherwise, the parser's folding is wrong, so
9993 -- we give an error. See P_Simple_Expression in Par.Ch4.
9994
9995 if Nkind (Op2) = N_String_Literal
9996 and then Is_Folded_In_Parser (Op2)
9997 and then Ekind (Entity (N)) = E_Function
9998 then
9999 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
10000 and then String_Length (Strval (Op1)) = 0);
10001 Error_Msg_N ("too many user-defined concatenations", N);
10002 return;
10003 end if;
10004
10005 Set_Etype (N, Btyp);
10006
10007 if Is_Limited_Composite (Btyp) then
10008 Error_Msg_N ("concatenation not available for limited array", N);
10009 Explain_Limited_Type (Btyp, N);
10010 end if;
10011 end Resolve_Op_Concat_First;
10012
10013 ----------------------------
10014 -- Resolve_Op_Concat_Rest --
10015 ----------------------------
10016
10017 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
10018 Op1 : constant Node_Id := Left_Opnd (N);
10019 Op2 : constant Node_Id := Right_Opnd (N);
10020
10021 begin
10022 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
10023
10024 Generate_Operator_Reference (N, Typ);
10025
10026 if Is_String_Type (Typ) then
10027 Eval_Concatenation (N);
10028 end if;
10029
10030 -- If this is not a static concatenation, but the result is a string
10031 -- type (and not an array of strings) ensure that static string operands
10032 -- have their subtypes properly constructed.
10033
10034 if Nkind (N) /= N_String_Literal
10035 and then Is_Character_Type (Component_Type (Typ))
10036 then
10037 Set_String_Literal_Subtype (Op1, Typ);
10038 Set_String_Literal_Subtype (Op2, Typ);
10039 end if;
10040 end Resolve_Op_Concat_Rest;
10041
10042 ----------------------
10043 -- Resolve_Op_Expon --
10044 ----------------------
10045
10046 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
10047 B_Typ : constant Entity_Id := Base_Type (Typ);
10048
10049 begin
10050 -- Catch attempts to do fixed-point exponentiation with universal
10051 -- operands, which is a case where the illegality is not caught during
10052 -- normal operator analysis. This is not done in preanalysis mode
10053 -- since the tree is not fully decorated during preanalysis.
10054
10055 if Full_Analysis then
10056 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
10057 Error_Msg_N ("exponentiation not available for fixed point", N);
10058 return;
10059
10060 elsif Nkind (Parent (N)) in N_Op
10061 and then Present (Etype (Parent (N)))
10062 and then Is_Fixed_Point_Type (Etype (Parent (N)))
10063 and then Etype (N) = Universal_Real
10064 and then Comes_From_Source (N)
10065 then
10066 Error_Msg_N ("exponentiation not available for fixed point", N);
10067 return;
10068 end if;
10069 end if;
10070
10071 if Comes_From_Source (N)
10072 and then Ekind (Entity (N)) = E_Function
10073 and then Is_Imported (Entity (N))
10074 and then Is_Intrinsic_Subprogram (Entity (N))
10075 then
10076 Resolve_Intrinsic_Operator (N, Typ);
10077 return;
10078 end if;
10079
10080 if Etype (Left_Opnd (N)) = Universal_Integer
10081 or else Etype (Left_Opnd (N)) = Universal_Real
10082 then
10083 Check_For_Visible_Operator (N, B_Typ);
10084 end if;
10085
10086 -- We do the resolution using the base type, because intermediate values
10087 -- in expressions are always of the base type, not a subtype of it.
10088
10089 Resolve (Left_Opnd (N), B_Typ);
10090 Resolve (Right_Opnd (N), Standard_Integer);
10091
10092 -- For integer types, right argument must be in Natural range
10093
10094 if Is_Integer_Type (Typ) then
10095 Apply_Scalar_Range_Check (Right_Opnd (N), Standard_Natural);
10096 end if;
10097
10098 Check_Unset_Reference (Left_Opnd (N));
10099 Check_Unset_Reference (Right_Opnd (N));
10100
10101 Set_Etype (N, B_Typ);
10102 Generate_Operator_Reference (N, B_Typ);
10103
10104 Analyze_Dimension (N);
10105
10106 if Ada_Version >= Ada_2012 and then Has_Dimension_System (B_Typ) then
10107 -- Evaluate the exponentiation operator for dimensioned type
10108
10109 Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ);
10110 else
10111 Eval_Op_Expon (N);
10112 end if;
10113
10114 -- Set overflow checking bit. Much cleverer code needed here eventually
10115 -- and perhaps the Resolve routines should be separated for the various
10116 -- arithmetic operations, since they will need different processing. ???
10117
10118 if Nkind (N) in N_Op then
10119 if not Overflow_Checks_Suppressed (Etype (N)) then
10120 Enable_Overflow_Check (N);
10121 end if;
10122 end if;
10123 end Resolve_Op_Expon;
10124
10125 --------------------
10126 -- Resolve_Op_Not --
10127 --------------------
10128
10129 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
10130 B_Typ : Entity_Id;
10131
10132 function Parent_Is_Boolean return Boolean;
10133 -- This function determines if the parent node is a boolean operator or
10134 -- operation (comparison op, membership test, or short circuit form) and
10135 -- the not in question is the left operand of this operation. Note that
10136 -- if the not is in parens, then false is returned.
10137
10138 -----------------------
10139 -- Parent_Is_Boolean --
10140 -----------------------
10141
10142 function Parent_Is_Boolean return Boolean is
10143 begin
10144 if Paren_Count (N) /= 0 then
10145 return False;
10146
10147 else
10148 case Nkind (Parent (N)) is
10149 when N_And_Then
10150 | N_In
10151 | N_Not_In
10152 | N_Op_And
10153 | N_Op_Eq
10154 | N_Op_Ge
10155 | N_Op_Gt
10156 | N_Op_Le
10157 | N_Op_Lt
10158 | N_Op_Ne
10159 | N_Op_Or
10160 | N_Op_Xor
10161 | N_Or_Else
10162 =>
10163 return Left_Opnd (Parent (N)) = N;
10164
10165 when others =>
10166 return False;
10167 end case;
10168 end if;
10169 end Parent_Is_Boolean;
10170
10171 -- Start of processing for Resolve_Op_Not
10172
10173 begin
10174 -- Predefined operations on scalar types yield the base type. On the
10175 -- other hand, logical operations on arrays yield the type of the
10176 -- arguments (and the context).
10177
10178 if Is_Array_Type (Typ) then
10179 B_Typ := Typ;
10180 else
10181 B_Typ := Base_Type (Typ);
10182 end if;
10183
10184 -- Straightforward case of incorrect arguments
10185
10186 if not Valid_Boolean_Arg (Typ) then
10187 Error_Msg_N ("invalid operand type for operator&", N);
10188 Set_Etype (N, Any_Type);
10189 return;
10190
10191 -- Special case of probable missing parens
10192
10193 elsif Typ = Universal_Integer or else Typ = Any_Modular then
10194 if Parent_Is_Boolean then
10195 Error_Msg_N
10196 ("operand of not must be enclosed in parentheses",
10197 Right_Opnd (N));
10198 else
10199 Error_Msg_N
10200 ("no modular type available in this context", N);
10201 end if;
10202
10203 Set_Etype (N, Any_Type);
10204 return;
10205
10206 -- OK resolution of NOT
10207
10208 else
10209 -- Warn if non-boolean types involved. This is a case like not a < b
10210 -- where a and b are modular, where we will get (not a) < b and most
10211 -- likely not (a < b) was intended.
10212
10213 if Warn_On_Questionable_Missing_Parens
10214 and then not Is_Boolean_Type (Typ)
10215 and then Parent_Is_Boolean
10216 then
10217 Error_Msg_N ("?q?not expression should be parenthesized here!", N);
10218 end if;
10219
10220 -- Warn on double negation if checking redundant constructs
10221
10222 if Warn_On_Redundant_Constructs
10223 and then Comes_From_Source (N)
10224 and then Comes_From_Source (Right_Opnd (N))
10225 and then Root_Type (Typ) = Standard_Boolean
10226 and then Nkind (Right_Opnd (N)) = N_Op_Not
10227 then
10228 Error_Msg_N ("redundant double negation?r?", N);
10229 end if;
10230
10231 -- Complete resolution and evaluation of NOT
10232 -- If argument is an equality and expected type is boolean, that
10233 -- expected type has no effect on resolution, and there are
10234 -- special rules for resolution of Eq, Neq in the presence of
10235 -- overloaded operands, so we directly call its resolution routines.
10236
10237 declare
10238 Opnd : constant Node_Id := Right_Opnd (N);
10239 Op_Id : Entity_Id;
10240
10241 begin
10242 if B_Typ = Standard_Boolean
10243 and then Nkind_In (Opnd, N_Op_Eq, N_Op_Ne)
10244 and then Is_Overloaded (Opnd)
10245 then
10246 Resolve_Equality_Op (Opnd, B_Typ);
10247 Op_Id := Entity (Opnd);
10248
10249 if Ekind (Op_Id) = E_Function
10250 and then not Is_Intrinsic_Subprogram (Op_Id)
10251 then
10252 Rewrite_Operator_As_Call (Opnd, Op_Id);
10253 end if;
10254
10255 if not Inside_A_Generic or else Is_Entity_Name (Opnd) then
10256 Freeze_Expression (Opnd);
10257 end if;
10258
10259 Expand (Opnd);
10260
10261 else
10262 Resolve (Opnd, B_Typ);
10263 end if;
10264
10265 Check_Unset_Reference (Opnd);
10266 end;
10267
10268 Set_Etype (N, B_Typ);
10269 Generate_Operator_Reference (N, B_Typ);
10270 Eval_Op_Not (N);
10271 end if;
10272 end Resolve_Op_Not;
10273
10274 -----------------------------
10275 -- Resolve_Operator_Symbol --
10276 -----------------------------
10277
10278 -- Nothing to be done, all resolved already
10279
10280 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
10281 pragma Warnings (Off, N);
10282 pragma Warnings (Off, Typ);
10283
10284 begin
10285 null;
10286 end Resolve_Operator_Symbol;
10287
10288 ----------------------------------
10289 -- Resolve_Qualified_Expression --
10290 ----------------------------------
10291
10292 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
10293 pragma Warnings (Off, Typ);
10294
10295 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
10296 Expr : constant Node_Id := Expression (N);
10297
10298 begin
10299 Resolve (Expr, Target_Typ);
10300
10301 -- Protect call to Matching_Static_Array_Bounds to avoid costly
10302 -- operation if not needed.
10303
10304 if Restriction_Check_Required (SPARK_05)
10305 and then Is_Array_Type (Target_Typ)
10306 and then Is_Array_Type (Etype (Expr))
10307 and then Etype (Expr) /= Any_Composite -- or else Expr in error
10308 and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr))
10309 then
10310 Check_SPARK_05_Restriction
10311 ("array types should have matching static bounds", N);
10312 end if;
10313
10314 -- A qualified expression requires an exact match of the type, class-
10315 -- wide matching is not allowed. However, if the qualifying type is
10316 -- specific and the expression has a class-wide type, it may still be
10317 -- okay, since it can be the result of the expansion of a call to a
10318 -- dispatching function, so we also have to check class-wideness of the
10319 -- type of the expression's original node.
10320
10321 if (Is_Class_Wide_Type (Target_Typ)
10322 or else
10323 (Is_Class_Wide_Type (Etype (Expr))
10324 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
10325 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
10326 then
10327 Wrong_Type (Expr, Target_Typ);
10328 end if;
10329
10330 -- If the target type is unconstrained, then we reset the type of the
10331 -- result from the type of the expression. For other cases, the actual
10332 -- subtype of the expression is the target type.
10333
10334 if Is_Composite_Type (Target_Typ)
10335 and then not Is_Constrained (Target_Typ)
10336 then
10337 Set_Etype (N, Etype (Expr));
10338 end if;
10339
10340 Analyze_Dimension (N);
10341 Eval_Qualified_Expression (N);
10342
10343 -- If we still have a qualified expression after the static evaluation,
10344 -- then apply a scalar range check if needed. The reason that we do this
10345 -- after the Eval call is that otherwise, the application of the range
10346 -- check may convert an illegal static expression and result in warning
10347 -- rather than giving an error (e.g Integer'(Integer'Last + 1)).
10348
10349 if Nkind (N) = N_Qualified_Expression and then Is_Scalar_Type (Typ) then
10350 Apply_Scalar_Range_Check (Expr, Typ);
10351 end if;
10352
10353 -- Finally, check whether a predicate applies to the target type. This
10354 -- comes from AI12-0100. As for type conversions, check the enclosing
10355 -- context to prevent an infinite expansion.
10356
10357 if Has_Predicates (Target_Typ) then
10358 if Nkind (Parent (N)) = N_Function_Call
10359 and then Present (Name (Parent (N)))
10360 and then (Is_Predicate_Function (Entity (Name (Parent (N))))
10361 or else
10362 Is_Predicate_Function_M (Entity (Name (Parent (N)))))
10363 then
10364 null;
10365
10366 -- In the case of a qualified expression in an allocator, the check
10367 -- is applied when expanding the allocator, so avoid redundant check.
10368
10369 elsif Nkind (N) = N_Qualified_Expression
10370 and then Nkind (Parent (N)) /= N_Allocator
10371 then
10372 Apply_Predicate_Check (N, Target_Typ);
10373 end if;
10374 end if;
10375 end Resolve_Qualified_Expression;
10376
10377 ------------------------------
10378 -- Resolve_Raise_Expression --
10379 ------------------------------
10380
10381 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id) is
10382 begin
10383 if Typ = Raise_Type then
10384 Error_Msg_N ("cannot find unique type for raise expression", N);
10385 Set_Etype (N, Any_Type);
10386 else
10387 Set_Etype (N, Typ);
10388 end if;
10389 end Resolve_Raise_Expression;
10390
10391 -------------------
10392 -- Resolve_Range --
10393 -------------------
10394
10395 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
10396 L : constant Node_Id := Low_Bound (N);
10397 H : constant Node_Id := High_Bound (N);
10398
10399 function First_Last_Ref return Boolean;
10400 -- Returns True if N is of the form X'First .. X'Last where X is the
10401 -- same entity for both attributes.
10402
10403 --------------------
10404 -- First_Last_Ref --
10405 --------------------
10406
10407 function First_Last_Ref return Boolean is
10408 Lorig : constant Node_Id := Original_Node (L);
10409 Horig : constant Node_Id := Original_Node (H);
10410
10411 begin
10412 if Nkind (Lorig) = N_Attribute_Reference
10413 and then Nkind (Horig) = N_Attribute_Reference
10414 and then Attribute_Name (Lorig) = Name_First
10415 and then Attribute_Name (Horig) = Name_Last
10416 then
10417 declare
10418 PL : constant Node_Id := Prefix (Lorig);
10419 PH : constant Node_Id := Prefix (Horig);
10420 begin
10421 if Is_Entity_Name (PL)
10422 and then Is_Entity_Name (PH)
10423 and then Entity (PL) = Entity (PH)
10424 then
10425 return True;
10426 end if;
10427 end;
10428 end if;
10429
10430 return False;
10431 end First_Last_Ref;
10432
10433 -- Start of processing for Resolve_Range
10434
10435 begin
10436 Set_Etype (N, Typ);
10437
10438 -- The lower bound should be in Typ. The higher bound can be in Typ's
10439 -- base type if the range is null. It may still be invalid if it is
10440 -- higher than the lower bound. This is checked later in the context in
10441 -- which the range appears.
10442
10443 Resolve (L, Typ);
10444 Resolve (H, Base_Type (Typ));
10445
10446 -- Reanalyze the lower bound after both bounds have been analyzed, so
10447 -- that the range is known to be static or not by now. This may trigger
10448 -- more compile-time evaluation, which is useful for static analysis
10449 -- with GNATprove. This is not needed for compilation or static analysis
10450 -- with CodePeer, as full expansion does that evaluation then.
10451
10452 if GNATprove_Mode then
10453 Set_Analyzed (L, False);
10454 Resolve (L, Typ);
10455 end if;
10456
10457 -- Check for inappropriate range on unordered enumeration type
10458
10459 if Bad_Unordered_Enumeration_Reference (N, Typ)
10460
10461 -- Exclude X'First .. X'Last if X is the same entity for both
10462
10463 and then not First_Last_Ref
10464 then
10465 Error_Msg_Sloc := Sloc (Typ);
10466 Error_Msg_NE
10467 ("subrange of unordered enumeration type& declared#?U?", N, Typ);
10468 end if;
10469
10470 Check_Unset_Reference (L);
10471 Check_Unset_Reference (H);
10472
10473 -- We have to check the bounds for being within the base range as
10474 -- required for a non-static context. Normally this is automatic and
10475 -- done as part of evaluating expressions, but the N_Range node is an
10476 -- exception, since in GNAT we consider this node to be a subexpression,
10477 -- even though in Ada it is not. The circuit in Sem_Eval could check for
10478 -- this, but that would put the test on the main evaluation path for
10479 -- expressions.
10480
10481 Check_Non_Static_Context (L);
10482 Check_Non_Static_Context (H);
10483
10484 -- Check for an ambiguous range over character literals. This will
10485 -- happen with a membership test involving only literals.
10486
10487 if Typ = Any_Character then
10488 Ambiguous_Character (L);
10489 Set_Etype (N, Any_Type);
10490 return;
10491 end if;
10492
10493 -- If bounds are static, constant-fold them, so size computations are
10494 -- identical between front-end and back-end. Do not perform this
10495 -- transformation while analyzing generic units, as type information
10496 -- would be lost when reanalyzing the constant node in the instance.
10497
10498 if Is_Discrete_Type (Typ) and then Expander_Active then
10499 if Is_OK_Static_Expression (L) then
10500 Fold_Uint (L, Expr_Value (L), Is_OK_Static_Expression (L));
10501 end if;
10502
10503 if Is_OK_Static_Expression (H) then
10504 Fold_Uint (H, Expr_Value (H), Is_OK_Static_Expression (H));
10505 end if;
10506 end if;
10507 end Resolve_Range;
10508
10509 --------------------------
10510 -- Resolve_Real_Literal --
10511 --------------------------
10512
10513 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
10514 Actual_Typ : constant Entity_Id := Etype (N);
10515
10516 begin
10517 -- Special processing for fixed-point literals to make sure that the
10518 -- value is an exact multiple of small where this is required. We skip
10519 -- this for the universal real case, and also for generic types.
10520
10521 if Is_Fixed_Point_Type (Typ)
10522 and then Typ /= Universal_Fixed
10523 and then Typ /= Any_Fixed
10524 and then not Is_Generic_Type (Typ)
10525 then
10526 declare
10527 Val : constant Ureal := Realval (N);
10528 Cintr : constant Ureal := Val / Small_Value (Typ);
10529 Cint : constant Uint := UR_Trunc (Cintr);
10530 Den : constant Uint := Norm_Den (Cintr);
10531 Stat : Boolean;
10532
10533 begin
10534 -- Case of literal is not an exact multiple of the Small
10535
10536 if Den /= 1 then
10537
10538 -- For a source program literal for a decimal fixed-point type,
10539 -- this is statically illegal (RM 4.9(36)).
10540
10541 if Is_Decimal_Fixed_Point_Type (Typ)
10542 and then Actual_Typ = Universal_Real
10543 and then Comes_From_Source (N)
10544 then
10545 Error_Msg_N ("value has extraneous low order digits", N);
10546 end if;
10547
10548 -- Generate a warning if literal from source
10549
10550 if Is_OK_Static_Expression (N)
10551 and then Warn_On_Bad_Fixed_Value
10552 then
10553 Error_Msg_N
10554 ("?b?static fixed-point value is not a multiple of Small!",
10555 N);
10556 end if;
10557
10558 -- Replace literal by a value that is the exact representation
10559 -- of a value of the type, i.e. a multiple of the small value,
10560 -- by truncation, since Machine_Rounds is false for all GNAT
10561 -- fixed-point types (RM 4.9(38)).
10562
10563 Stat := Is_OK_Static_Expression (N);
10564 Rewrite (N,
10565 Make_Real_Literal (Sloc (N),
10566 Realval => Small_Value (Typ) * Cint));
10567
10568 Set_Is_Static_Expression (N, Stat);
10569 end if;
10570
10571 -- In all cases, set the corresponding integer field
10572
10573 Set_Corresponding_Integer_Value (N, Cint);
10574 end;
10575 end if;
10576
10577 -- Now replace the actual type by the expected type as usual
10578
10579 Set_Etype (N, Typ);
10580 Eval_Real_Literal (N);
10581 end Resolve_Real_Literal;
10582
10583 -----------------------
10584 -- Resolve_Reference --
10585 -----------------------
10586
10587 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
10588 P : constant Node_Id := Prefix (N);
10589
10590 begin
10591 -- Replace general access with specific type
10592
10593 if Ekind (Etype (N)) = E_Allocator_Type then
10594 Set_Etype (N, Base_Type (Typ));
10595 end if;
10596
10597 Resolve (P, Designated_Type (Etype (N)));
10598
10599 -- If we are taking the reference of a volatile entity, then treat it as
10600 -- a potential modification of this entity. This is too conservative,
10601 -- but necessary because remove side effects can cause transformations
10602 -- of normal assignments into reference sequences that otherwise fail to
10603 -- notice the modification.
10604
10605 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
10606 Note_Possible_Modification (P, Sure => False);
10607 end if;
10608 end Resolve_Reference;
10609
10610 --------------------------------
10611 -- Resolve_Selected_Component --
10612 --------------------------------
10613
10614 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
10615 Comp : Entity_Id;
10616 Comp1 : Entity_Id := Empty; -- prevent junk warning
10617 P : constant Node_Id := Prefix (N);
10618 S : constant Node_Id := Selector_Name (N);
10619 T : Entity_Id := Etype (P);
10620 I : Interp_Index;
10621 I1 : Interp_Index := 0; -- prevent junk warning
10622 It : Interp;
10623 It1 : Interp;
10624 Found : Boolean;
10625
10626 function Init_Component return Boolean;
10627 -- Check whether this is the initialization of a component within an
10628 -- init proc (by assignment or call to another init proc). If true,
10629 -- there is no need for a discriminant check.
10630
10631 --------------------
10632 -- Init_Component --
10633 --------------------
10634
10635 function Init_Component return Boolean is
10636 begin
10637 return Inside_Init_Proc
10638 and then Nkind (Prefix (N)) = N_Identifier
10639 and then Chars (Prefix (N)) = Name_uInit
10640 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
10641 end Init_Component;
10642
10643 -- Start of processing for Resolve_Selected_Component
10644
10645 begin
10646 if Is_Overloaded (P) then
10647
10648 -- Use the context type to select the prefix that has a selector
10649 -- of the correct name and type.
10650
10651 Found := False;
10652 Get_First_Interp (P, I, It);
10653
10654 Search : while Present (It.Typ) loop
10655 if Is_Access_Type (It.Typ) then
10656 T := Designated_Type (It.Typ);
10657 else
10658 T := It.Typ;
10659 end if;
10660
10661 -- Locate selected component. For a private prefix the selector
10662 -- can denote a discriminant.
10663
10664 if Is_Record_Type (T) or else Is_Private_Type (T) then
10665
10666 -- The visible components of a class-wide type are those of
10667 -- the root type.
10668
10669 if Is_Class_Wide_Type (T) then
10670 T := Etype (T);
10671 end if;
10672
10673 Comp := First_Entity (T);
10674 while Present (Comp) loop
10675 if Chars (Comp) = Chars (S)
10676 and then Covers (Typ, Etype (Comp))
10677 then
10678 if not Found then
10679 Found := True;
10680 I1 := I;
10681 It1 := It;
10682 Comp1 := Comp;
10683
10684 else
10685 It := Disambiguate (P, I1, I, Any_Type);
10686
10687 if It = No_Interp then
10688 Error_Msg_N
10689 ("ambiguous prefix for selected component", N);
10690 Set_Etype (N, Typ);
10691 return;
10692
10693 else
10694 It1 := It;
10695
10696 -- There may be an implicit dereference. Retrieve
10697 -- designated record type.
10698
10699 if Is_Access_Type (It1.Typ) then
10700 T := Designated_Type (It1.Typ);
10701 else
10702 T := It1.Typ;
10703 end if;
10704
10705 if Scope (Comp1) /= T then
10706
10707 -- Resolution chooses the new interpretation.
10708 -- Find the component with the right name.
10709
10710 Comp1 := First_Entity (T);
10711 while Present (Comp1)
10712 and then Chars (Comp1) /= Chars (S)
10713 loop
10714 Comp1 := Next_Entity (Comp1);
10715 end loop;
10716 end if;
10717
10718 exit Search;
10719 end if;
10720 end if;
10721 end if;
10722
10723 Comp := Next_Entity (Comp);
10724 end loop;
10725 end if;
10726
10727 Get_Next_Interp (I, It);
10728 end loop Search;
10729
10730 -- There must be a legal interpretation at this point
10731
10732 pragma Assert (Found);
10733 Resolve (P, It1.Typ);
10734
10735 -- In general the expected type is the type of the context, not the
10736 -- type of the candidate selected component.
10737
10738 Set_Etype (N, Typ);
10739 Set_Entity_With_Checks (S, Comp1);
10740
10741 -- The type of the context and that of the component are
10742 -- compatible and in general identical, but if they are anonymous
10743 -- access-to-subprogram types, the relevant type is that of the
10744 -- component. This matters in Unnest_Subprograms mode, where the
10745 -- relevant context is the one in which the type is declared, not
10746 -- the point of use. This determines what activation record to use.
10747
10748 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
10749 Set_Etype (N, Etype (Comp1));
10750
10751 -- When the type of the component is an access to a class-wide type
10752 -- the relevant type is that of the component (since in such case we
10753 -- may need to generate implicit type conversions or dispatching
10754 -- calls).
10755
10756 elsif Is_Access_Type (Typ)
10757 and then not Is_Class_Wide_Type (Designated_Type (Typ))
10758 and then Is_Class_Wide_Type (Designated_Type (Etype (Comp1)))
10759 then
10760 Set_Etype (N, Etype (Comp1));
10761 end if;
10762
10763 else
10764 -- Resolve prefix with its type
10765
10766 Resolve (P, T);
10767 end if;
10768
10769 -- Generate cross-reference. We needed to wait until full overloading
10770 -- resolution was complete to do this, since otherwise we can't tell if
10771 -- we are an lvalue or not.
10772
10773 if May_Be_Lvalue (N) then
10774 Generate_Reference (Entity (S), S, 'm');
10775 else
10776 Generate_Reference (Entity (S), S, 'r');
10777 end if;
10778
10779 -- If prefix is an access type, the node will be transformed into an
10780 -- explicit dereference during expansion. The type of the node is the
10781 -- designated type of that of the prefix.
10782
10783 if Is_Access_Type (Etype (P)) then
10784 T := Designated_Type (Etype (P));
10785 Check_Fully_Declared_Prefix (T, P);
10786
10787 else
10788 T := Etype (P);
10789
10790 -- If the prefix is an entity it may have a deferred reference set
10791 -- during analysis of the selected component. After resolution we
10792 -- can transform it into a proper reference. This prevents spurious
10793 -- warnings on useless assignments when the same selected component
10794 -- is the actual for an out parameter in a subsequent call.
10795
10796 if Is_Entity_Name (P)
10797 and then Has_Deferred_Reference (Entity (P))
10798 then
10799 if May_Be_Lvalue (N) then
10800 Generate_Reference (Entity (P), P, 'm');
10801 else
10802 Generate_Reference (Entity (P), P, 'r');
10803 end if;
10804 end if;
10805 end if;
10806
10807 -- Set flag for expander if discriminant check required on a component
10808 -- appearing within a variant.
10809
10810 if Has_Discriminants (T)
10811 and then Ekind (Entity (S)) = E_Component
10812 and then Present (Original_Record_Component (Entity (S)))
10813 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
10814 and then
10815 Is_Declared_Within_Variant (Original_Record_Component (Entity (S)))
10816 and then not Discriminant_Checks_Suppressed (T)
10817 and then not Init_Component
10818 then
10819 Set_Do_Discriminant_Check (N);
10820 end if;
10821
10822 if Ekind (Entity (S)) = E_Void then
10823 Error_Msg_N ("premature use of component", S);
10824 end if;
10825
10826 -- If the prefix is a record conversion, this may be a renamed
10827 -- discriminant whose bounds differ from those of the original
10828 -- one, so we must ensure that a range check is performed.
10829
10830 if Nkind (P) = N_Type_Conversion
10831 and then Ekind (Entity (S)) = E_Discriminant
10832 and then Is_Discrete_Type (Typ)
10833 then
10834 Set_Etype (N, Base_Type (Typ));
10835 end if;
10836
10837 -- Note: No Eval processing is required, because the prefix is of a
10838 -- record type, or protected type, and neither can possibly be static.
10839
10840 -- If the record type is atomic, and the component is non-atomic, then
10841 -- this is worth a warning, since we have a situation where the access
10842 -- to the component may cause extra read/writes of the atomic array
10843 -- object, or partial word accesses, both of which may be unexpected.
10844
10845 if Nkind (N) = N_Selected_Component
10846 and then Is_Atomic_Ref_With_Address (N)
10847 and then not Is_Atomic (Entity (S))
10848 and then not Is_Atomic (Etype (Entity (S)))
10849 then
10850 Error_Msg_N
10851 ("??access to non-atomic component of atomic record",
10852 Prefix (N));
10853 Error_Msg_N
10854 ("\??may cause unexpected accesses to atomic object",
10855 Prefix (N));
10856 end if;
10857
10858 Analyze_Dimension (N);
10859 end Resolve_Selected_Component;
10860
10861 -------------------
10862 -- Resolve_Shift --
10863 -------------------
10864
10865 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
10866 B_Typ : constant Entity_Id := Base_Type (Typ);
10867 L : constant Node_Id := Left_Opnd (N);
10868 R : constant Node_Id := Right_Opnd (N);
10869
10870 begin
10871 -- We do the resolution using the base type, because intermediate values
10872 -- in expressions always are of the base type, not a subtype of it.
10873
10874 Resolve (L, B_Typ);
10875 Resolve (R, Standard_Natural);
10876
10877 Check_Unset_Reference (L);
10878 Check_Unset_Reference (R);
10879
10880 Set_Etype (N, B_Typ);
10881 Generate_Operator_Reference (N, B_Typ);
10882 Eval_Shift (N);
10883 end Resolve_Shift;
10884
10885 ---------------------------
10886 -- Resolve_Short_Circuit --
10887 ---------------------------
10888
10889 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
10890 B_Typ : constant Entity_Id := Base_Type (Typ);
10891 L : constant Node_Id := Left_Opnd (N);
10892 R : constant Node_Id := Right_Opnd (N);
10893
10894 begin
10895 -- Ensure all actions associated with the left operand (e.g.
10896 -- finalization of transient objects) are fully evaluated locally within
10897 -- an expression with actions. This is particularly helpful for coverage
10898 -- analysis. However this should not happen in generics or if option
10899 -- Minimize_Expression_With_Actions is set.
10900
10901 if Expander_Active and not Minimize_Expression_With_Actions then
10902 declare
10903 Reloc_L : constant Node_Id := Relocate_Node (L);
10904 begin
10905 Save_Interps (Old_N => L, New_N => Reloc_L);
10906
10907 Rewrite (L,
10908 Make_Expression_With_Actions (Sloc (L),
10909 Actions => New_List,
10910 Expression => Reloc_L));
10911
10912 -- Set Comes_From_Source on L to preserve warnings for unset
10913 -- reference.
10914
10915 Set_Comes_From_Source (L, Comes_From_Source (Reloc_L));
10916 end;
10917 end if;
10918
10919 Resolve (L, B_Typ);
10920 Resolve (R, B_Typ);
10921
10922 -- Check for issuing warning for always False assert/check, this happens
10923 -- when assertions are turned off, in which case the pragma Assert/Check
10924 -- was transformed into:
10925
10926 -- if False and then <condition> then ...
10927
10928 -- and we detect this pattern
10929
10930 if Warn_On_Assertion_Failure
10931 and then Is_Entity_Name (R)
10932 and then Entity (R) = Standard_False
10933 and then Nkind (Parent (N)) = N_If_Statement
10934 and then Nkind (N) = N_And_Then
10935 and then Is_Entity_Name (L)
10936 and then Entity (L) = Standard_False
10937 then
10938 declare
10939 Orig : constant Node_Id := Original_Node (Parent (N));
10940
10941 begin
10942 -- Special handling of Asssert pragma
10943
10944 if Nkind (Orig) = N_Pragma
10945 and then Pragma_Name (Orig) = Name_Assert
10946 then
10947 declare
10948 Expr : constant Node_Id :=
10949 Original_Node
10950 (Expression
10951 (First (Pragma_Argument_Associations (Orig))));
10952
10953 begin
10954 -- Don't warn if original condition is explicit False,
10955 -- since obviously the failure is expected in this case.
10956
10957 if Is_Entity_Name (Expr)
10958 and then Entity (Expr) = Standard_False
10959 then
10960 null;
10961
10962 -- Issue warning. We do not want the deletion of the
10963 -- IF/AND-THEN to take this message with it. We achieve this
10964 -- by making sure that the expanded code points to the Sloc
10965 -- of the expression, not the original pragma.
10966
10967 else
10968 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
10969 -- The source location of the expression is not usually
10970 -- the best choice here. For example, it gets located on
10971 -- the last AND keyword in a chain of boolean expressiond
10972 -- AND'ed together. It is best to put the message on the
10973 -- first character of the assertion, which is the effect
10974 -- of the First_Node call here.
10975
10976 Error_Msg_F
10977 ("?A?assertion would fail at run time!",
10978 Expression
10979 (First (Pragma_Argument_Associations (Orig))));
10980 end if;
10981 end;
10982
10983 -- Similar processing for Check pragma
10984
10985 elsif Nkind (Orig) = N_Pragma
10986 and then Pragma_Name (Orig) = Name_Check
10987 then
10988 -- Don't want to warn if original condition is explicit False
10989
10990 declare
10991 Expr : constant Node_Id :=
10992 Original_Node
10993 (Expression
10994 (Next (First (Pragma_Argument_Associations (Orig)))));
10995 begin
10996 if Is_Entity_Name (Expr)
10997 and then Entity (Expr) = Standard_False
10998 then
10999 null;
11000
11001 -- Post warning
11002
11003 else
11004 -- Again use Error_Msg_F rather than Error_Msg_N, see
11005 -- comment above for an explanation of why we do this.
11006
11007 Error_Msg_F
11008 ("?A?check would fail at run time!",
11009 Expression
11010 (Last (Pragma_Argument_Associations (Orig))));
11011 end if;
11012 end;
11013 end if;
11014 end;
11015 end if;
11016
11017 -- Continue with processing of short circuit
11018
11019 Check_Unset_Reference (L);
11020 Check_Unset_Reference (R);
11021
11022 Set_Etype (N, B_Typ);
11023 Eval_Short_Circuit (N);
11024 end Resolve_Short_Circuit;
11025
11026 -------------------
11027 -- Resolve_Slice --
11028 -------------------
11029
11030 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
11031 Drange : constant Node_Id := Discrete_Range (N);
11032 Name : constant Node_Id := Prefix (N);
11033 Array_Type : Entity_Id := Empty;
11034 Dexpr : Node_Id := Empty;
11035 Index_Type : Entity_Id;
11036
11037 begin
11038 if Is_Overloaded (Name) then
11039
11040 -- Use the context type to select the prefix that yields the correct
11041 -- array type.
11042
11043 declare
11044 I : Interp_Index;
11045 I1 : Interp_Index := 0;
11046 It : Interp;
11047 P : constant Node_Id := Prefix (N);
11048 Found : Boolean := False;
11049
11050 begin
11051 Get_First_Interp (P, I, It);
11052 while Present (It.Typ) loop
11053 if (Is_Array_Type (It.Typ)
11054 and then Covers (Typ, It.Typ))
11055 or else (Is_Access_Type (It.Typ)
11056 and then Is_Array_Type (Designated_Type (It.Typ))
11057 and then Covers (Typ, Designated_Type (It.Typ)))
11058 then
11059 if Found then
11060 It := Disambiguate (P, I1, I, Any_Type);
11061
11062 if It = No_Interp then
11063 Error_Msg_N ("ambiguous prefix for slicing", N);
11064 Set_Etype (N, Typ);
11065 return;
11066 else
11067 Found := True;
11068 Array_Type := It.Typ;
11069 I1 := I;
11070 end if;
11071 else
11072 Found := True;
11073 Array_Type := It.Typ;
11074 I1 := I;
11075 end if;
11076 end if;
11077
11078 Get_Next_Interp (I, It);
11079 end loop;
11080 end;
11081
11082 else
11083 Array_Type := Etype (Name);
11084 end if;
11085
11086 Resolve (Name, Array_Type);
11087
11088 if Is_Access_Type (Array_Type) then
11089 Apply_Access_Check (N);
11090 Array_Type := Designated_Type (Array_Type);
11091
11092 -- If the prefix is an access to an unconstrained array, we must use
11093 -- the actual subtype of the object to perform the index checks. The
11094 -- object denoted by the prefix is implicit in the node, so we build
11095 -- an explicit representation for it in order to compute the actual
11096 -- subtype.
11097
11098 if not Is_Constrained (Array_Type) then
11099 Remove_Side_Effects (Prefix (N));
11100
11101 declare
11102 Obj : constant Node_Id :=
11103 Make_Explicit_Dereference (Sloc (N),
11104 Prefix => New_Copy_Tree (Prefix (N)));
11105 begin
11106 Set_Etype (Obj, Array_Type);
11107 Set_Parent (Obj, Parent (N));
11108 Array_Type := Get_Actual_Subtype (Obj);
11109 end;
11110 end if;
11111
11112 elsif Is_Entity_Name (Name)
11113 or else Nkind (Name) = N_Explicit_Dereference
11114 or else (Nkind (Name) = N_Function_Call
11115 and then not Is_Constrained (Etype (Name)))
11116 then
11117 Array_Type := Get_Actual_Subtype (Name);
11118
11119 -- If the name is a selected component that depends on discriminants,
11120 -- build an actual subtype for it. This can happen only when the name
11121 -- itself is overloaded; otherwise the actual subtype is created when
11122 -- the selected component is analyzed.
11123
11124 elsif Nkind (Name) = N_Selected_Component
11125 and then Full_Analysis
11126 and then Depends_On_Discriminant (First_Index (Array_Type))
11127 then
11128 declare
11129 Act_Decl : constant Node_Id :=
11130 Build_Actual_Subtype_Of_Component (Array_Type, Name);
11131 begin
11132 Insert_Action (N, Act_Decl);
11133 Array_Type := Defining_Identifier (Act_Decl);
11134 end;
11135
11136 -- Maybe this should just be "else", instead of checking for the
11137 -- specific case of slice??? This is needed for the case where the
11138 -- prefix is an Image attribute, which gets expanded to a slice, and so
11139 -- has a constrained subtype which we want to use for the slice range
11140 -- check applied below (the range check won't get done if the
11141 -- unconstrained subtype of the 'Image is used).
11142
11143 elsif Nkind (Name) = N_Slice then
11144 Array_Type := Etype (Name);
11145 end if;
11146
11147 -- Obtain the type of the array index
11148
11149 if Ekind (Array_Type) = E_String_Literal_Subtype then
11150 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
11151 else
11152 Index_Type := Etype (First_Index (Array_Type));
11153 end if;
11154
11155 -- If name was overloaded, set slice type correctly now
11156
11157 Set_Etype (N, Array_Type);
11158
11159 -- Handle the generation of a range check that compares the array index
11160 -- against the discrete_range. The check is not applied to internally
11161 -- built nodes associated with the expansion of dispatch tables. Check
11162 -- that Ada.Tags has already been loaded to avoid extra dependencies on
11163 -- the unit.
11164
11165 if Tagged_Type_Expansion
11166 and then RTU_Loaded (Ada_Tags)
11167 and then Nkind (Prefix (N)) = N_Selected_Component
11168 and then Present (Entity (Selector_Name (Prefix (N))))
11169 and then Entity (Selector_Name (Prefix (N))) =
11170 RTE_Record_Component (RE_Prims_Ptr)
11171 then
11172 null;
11173
11174 -- The discrete_range is specified by a subtype indication. Create a
11175 -- shallow copy and inherit the type, parent and source location from
11176 -- the discrete_range. This ensures that the range check is inserted
11177 -- relative to the slice and that the runtime exception points to the
11178 -- proper construct.
11179
11180 elsif Is_Entity_Name (Drange) then
11181 Dexpr := New_Copy (Scalar_Range (Entity (Drange)));
11182
11183 Set_Etype (Dexpr, Etype (Drange));
11184 Set_Parent (Dexpr, Parent (Drange));
11185 Set_Sloc (Dexpr, Sloc (Drange));
11186
11187 -- The discrete_range is a regular range. Resolve the bounds and remove
11188 -- their side effects.
11189
11190 else
11191 Resolve (Drange, Base_Type (Index_Type));
11192
11193 if Nkind (Drange) = N_Range then
11194 Force_Evaluation (Low_Bound (Drange));
11195 Force_Evaluation (High_Bound (Drange));
11196
11197 Dexpr := Drange;
11198 end if;
11199 end if;
11200
11201 if Present (Dexpr) then
11202 Apply_Range_Check (Dexpr, Index_Type);
11203 end if;
11204
11205 Set_Slice_Subtype (N);
11206
11207 -- Check bad use of type with predicates
11208
11209 declare
11210 Subt : Entity_Id;
11211
11212 begin
11213 if Nkind (Drange) = N_Subtype_Indication
11214 and then Has_Predicates (Entity (Subtype_Mark (Drange)))
11215 then
11216 Subt := Entity (Subtype_Mark (Drange));
11217 else
11218 Subt := Etype (Drange);
11219 end if;
11220
11221 if Has_Predicates (Subt) then
11222 Bad_Predicated_Subtype_Use
11223 ("subtype& has predicate, not allowed in slice", Drange, Subt);
11224 end if;
11225 end;
11226
11227 -- Otherwise here is where we check suspicious indexes
11228
11229 if Nkind (Drange) = N_Range then
11230 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
11231 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
11232 end if;
11233
11234 Analyze_Dimension (N);
11235 Eval_Slice (N);
11236 end Resolve_Slice;
11237
11238 ----------------------------
11239 -- Resolve_String_Literal --
11240 ----------------------------
11241
11242 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
11243 C_Typ : constant Entity_Id := Component_Type (Typ);
11244 R_Typ : constant Entity_Id := Root_Type (C_Typ);
11245 Loc : constant Source_Ptr := Sloc (N);
11246 Str : constant String_Id := Strval (N);
11247 Strlen : constant Nat := String_Length (Str);
11248 Subtype_Id : Entity_Id;
11249 Need_Check : Boolean;
11250
11251 begin
11252 -- For a string appearing in a concatenation, defer creation of the
11253 -- string_literal_subtype until the end of the resolution of the
11254 -- concatenation, because the literal may be constant-folded away. This
11255 -- is a useful optimization for long concatenation expressions.
11256
11257 -- If the string is an aggregate built for a single character (which
11258 -- happens in a non-static context) or a is null string to which special
11259 -- checks may apply, we build the subtype. Wide strings must also get a
11260 -- string subtype if they come from a one character aggregate. Strings
11261 -- generated by attributes might be static, but it is often hard to
11262 -- determine whether the enclosing context is static, so we generate
11263 -- subtypes for them as well, thus losing some rarer optimizations ???
11264 -- Same for strings that come from a static conversion.
11265
11266 Need_Check :=
11267 (Strlen = 0 and then Typ /= Standard_String)
11268 or else Nkind (Parent (N)) /= N_Op_Concat
11269 or else (N /= Left_Opnd (Parent (N))
11270 and then N /= Right_Opnd (Parent (N)))
11271 or else ((Typ = Standard_Wide_String
11272 or else Typ = Standard_Wide_Wide_String)
11273 and then Nkind (Original_Node (N)) /= N_String_Literal);
11274
11275 -- If the resolving type is itself a string literal subtype, we can just
11276 -- reuse it, since there is no point in creating another.
11277
11278 if Ekind (Typ) = E_String_Literal_Subtype then
11279 Subtype_Id := Typ;
11280
11281 elsif Nkind (Parent (N)) = N_Op_Concat
11282 and then not Need_Check
11283 and then not Nkind_In (Original_Node (N), N_Character_Literal,
11284 N_Attribute_Reference,
11285 N_Qualified_Expression,
11286 N_Type_Conversion)
11287 then
11288 Subtype_Id := Typ;
11289
11290 -- Do not generate a string literal subtype for the default expression
11291 -- of a formal parameter in GNATprove mode. This is because the string
11292 -- subtype is associated with the freezing actions of the subprogram,
11293 -- however freezing is disabled in GNATprove mode and as a result the
11294 -- subtype is unavailable.
11295
11296 elsif GNATprove_Mode
11297 and then Nkind (Parent (N)) = N_Parameter_Specification
11298 then
11299 Subtype_Id := Typ;
11300
11301 -- Otherwise we must create a string literal subtype. Note that the
11302 -- whole idea of string literal subtypes is simply to avoid the need
11303 -- for building a full fledged array subtype for each literal.
11304
11305 else
11306 Set_String_Literal_Subtype (N, Typ);
11307 Subtype_Id := Etype (N);
11308 end if;
11309
11310 if Nkind (Parent (N)) /= N_Op_Concat
11311 or else Need_Check
11312 then
11313 Set_Etype (N, Subtype_Id);
11314 Eval_String_Literal (N);
11315 end if;
11316
11317 if Is_Limited_Composite (Typ)
11318 or else Is_Private_Composite (Typ)
11319 then
11320 Error_Msg_N ("string literal not available for private array", N);
11321 Set_Etype (N, Any_Type);
11322 return;
11323 end if;
11324
11325 -- The validity of a null string has been checked in the call to
11326 -- Eval_String_Literal.
11327
11328 if Strlen = 0 then
11329 return;
11330
11331 -- Always accept string literal with component type Any_Character, which
11332 -- occurs in error situations and in comparisons of literals, both of
11333 -- which should accept all literals.
11334
11335 elsif R_Typ = Any_Character then
11336 return;
11337
11338 -- If the type is bit-packed, then we always transform the string
11339 -- literal into a full fledged aggregate.
11340
11341 elsif Is_Bit_Packed_Array (Typ) then
11342 null;
11343
11344 -- Deal with cases of Wide_Wide_String, Wide_String, and String
11345
11346 else
11347 -- For Standard.Wide_Wide_String, or any other type whose component
11348 -- type is Standard.Wide_Wide_Character, we know that all the
11349 -- characters in the string must be acceptable, since the parser
11350 -- accepted the characters as valid character literals.
11351
11352 if R_Typ = Standard_Wide_Wide_Character then
11353 null;
11354
11355 -- For the case of Standard.String, or any other type whose component
11356 -- type is Standard.Character, we must make sure that there are no
11357 -- wide characters in the string, i.e. that it is entirely composed
11358 -- of characters in range of type Character.
11359
11360 -- If the string literal is the result of a static concatenation, the
11361 -- test has already been performed on the components, and need not be
11362 -- repeated.
11363
11364 elsif R_Typ = Standard_Character
11365 and then Nkind (Original_Node (N)) /= N_Op_Concat
11366 then
11367 for J in 1 .. Strlen loop
11368 if not In_Character_Range (Get_String_Char (Str, J)) then
11369
11370 -- If we are out of range, post error. This is one of the
11371 -- very few places that we place the flag in the middle of
11372 -- a token, right under the offending wide character. Not
11373 -- quite clear if this is right wrt wide character encoding
11374 -- sequences, but it's only an error message.
11375
11376 Error_Msg
11377 ("literal out of range of type Standard.Character",
11378 Source_Ptr (Int (Loc) + J));
11379 return;
11380 end if;
11381 end loop;
11382
11383 -- For the case of Standard.Wide_String, or any other type whose
11384 -- component type is Standard.Wide_Character, we must make sure that
11385 -- there are no wide characters in the string, i.e. that it is
11386 -- entirely composed of characters in range of type Wide_Character.
11387
11388 -- If the string literal is the result of a static concatenation,
11389 -- the test has already been performed on the components, and need
11390 -- not be repeated.
11391
11392 elsif R_Typ = Standard_Wide_Character
11393 and then Nkind (Original_Node (N)) /= N_Op_Concat
11394 then
11395 for J in 1 .. Strlen loop
11396 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
11397
11398 -- If we are out of range, post error. This is one of the
11399 -- very few places that we place the flag in the middle of
11400 -- a token, right under the offending wide character.
11401
11402 -- This is not quite right, because characters in general
11403 -- will take more than one character position ???
11404
11405 Error_Msg
11406 ("literal out of range of type Standard.Wide_Character",
11407 Source_Ptr (Int (Loc) + J));
11408 return;
11409 end if;
11410 end loop;
11411
11412 -- If the root type is not a standard character, then we will convert
11413 -- the string into an aggregate and will let the aggregate code do
11414 -- the checking. Standard Wide_Wide_Character is also OK here.
11415
11416 else
11417 null;
11418 end if;
11419
11420 -- See if the component type of the array corresponding to the string
11421 -- has compile time known bounds. If yes we can directly check
11422 -- whether the evaluation of the string will raise constraint error.
11423 -- Otherwise we need to transform the string literal into the
11424 -- corresponding character aggregate and let the aggregate code do
11425 -- the checking. We use the same transformation if the component
11426 -- type has a static predicate, which will be applied to each
11427 -- character when the aggregate is resolved.
11428
11429 if Is_Standard_Character_Type (R_Typ) then
11430
11431 -- Check for the case of full range, where we are definitely OK
11432
11433 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
11434 return;
11435 end if;
11436
11437 -- Here the range is not the complete base type range, so check
11438
11439 declare
11440 Comp_Typ_Lo : constant Node_Id :=
11441 Type_Low_Bound (Component_Type (Typ));
11442 Comp_Typ_Hi : constant Node_Id :=
11443 Type_High_Bound (Component_Type (Typ));
11444
11445 Char_Val : Uint;
11446
11447 begin
11448 if Compile_Time_Known_Value (Comp_Typ_Lo)
11449 and then Compile_Time_Known_Value (Comp_Typ_Hi)
11450 then
11451 for J in 1 .. Strlen loop
11452 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
11453
11454 if Char_Val < Expr_Value (Comp_Typ_Lo)
11455 or else Char_Val > Expr_Value (Comp_Typ_Hi)
11456 then
11457 Apply_Compile_Time_Constraint_Error
11458 (N, "character out of range??",
11459 CE_Range_Check_Failed,
11460 Loc => Source_Ptr (Int (Loc) + J));
11461 end if;
11462 end loop;
11463
11464 if not Has_Static_Predicate (C_Typ) then
11465 return;
11466 end if;
11467 end if;
11468 end;
11469 end if;
11470 end if;
11471
11472 -- If we got here we meed to transform the string literal into the
11473 -- equivalent qualified positional array aggregate. This is rather
11474 -- heavy artillery for this situation, but it is hard work to avoid.
11475
11476 declare
11477 Lits : constant List_Id := New_List;
11478 P : Source_Ptr := Loc + 1;
11479 C : Char_Code;
11480
11481 begin
11482 -- Build the character literals, we give them source locations that
11483 -- correspond to the string positions, which is a bit tricky given
11484 -- the possible presence of wide character escape sequences.
11485
11486 for J in 1 .. Strlen loop
11487 C := Get_String_Char (Str, J);
11488 Set_Character_Literal_Name (C);
11489
11490 Append_To (Lits,
11491 Make_Character_Literal (P,
11492 Chars => Name_Find,
11493 Char_Literal_Value => UI_From_CC (C)));
11494
11495 if In_Character_Range (C) then
11496 P := P + 1;
11497
11498 -- Should we have a call to Skip_Wide here ???
11499
11500 -- ??? else
11501 -- Skip_Wide (P);
11502
11503 end if;
11504 end loop;
11505
11506 Rewrite (N,
11507 Make_Qualified_Expression (Loc,
11508 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
11509 Expression =>
11510 Make_Aggregate (Loc, Expressions => Lits)));
11511
11512 Analyze_And_Resolve (N, Typ);
11513 end;
11514 end Resolve_String_Literal;
11515
11516 -------------------------
11517 -- Resolve_Target_Name --
11518 -------------------------
11519
11520 procedure Resolve_Target_Name (N : Node_Id; Typ : Entity_Id) is
11521 begin
11522 Set_Etype (N, Typ);
11523 end Resolve_Target_Name;
11524
11525 -----------------------------
11526 -- Resolve_Type_Conversion --
11527 -----------------------------
11528
11529 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
11530 Conv_OK : constant Boolean := Conversion_OK (N);
11531 Operand : constant Node_Id := Expression (N);
11532 Operand_Typ : constant Entity_Id := Etype (Operand);
11533 Target_Typ : constant Entity_Id := Etype (N);
11534 Rop : Node_Id;
11535 Orig_N : Node_Id;
11536 Orig_T : Node_Id;
11537
11538 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
11539 -- Set to False to suppress cases where we want to suppress the test
11540 -- for redundancy to avoid possible false positives on this warning.
11541
11542 begin
11543 if not Conv_OK
11544 and then not Valid_Conversion (N, Target_Typ, Operand)
11545 then
11546 return;
11547 end if;
11548
11549 -- If the Operand Etype is Universal_Fixed, then the conversion is
11550 -- never redundant. We need this check because by the time we have
11551 -- finished the rather complex transformation, the conversion looks
11552 -- redundant when it is not.
11553
11554 if Operand_Typ = Universal_Fixed then
11555 Test_Redundant := False;
11556
11557 -- If the operand is marked as Any_Fixed, then special processing is
11558 -- required. This is also a case where we suppress the test for a
11559 -- redundant conversion, since most certainly it is not redundant.
11560
11561 elsif Operand_Typ = Any_Fixed then
11562 Test_Redundant := False;
11563
11564 -- Mixed-mode operation involving a literal. Context must be a fixed
11565 -- type which is applied to the literal subsequently.
11566
11567 -- Multiplication and division involving two fixed type operands must
11568 -- yield a universal real because the result is computed in arbitrary
11569 -- precision.
11570
11571 if Is_Fixed_Point_Type (Typ)
11572 and then Nkind_In (Operand, N_Op_Divide, N_Op_Multiply)
11573 and then Etype (Left_Opnd (Operand)) = Any_Fixed
11574 and then Etype (Right_Opnd (Operand)) = Any_Fixed
11575 then
11576 Set_Etype (Operand, Universal_Real);
11577
11578 elsif Is_Numeric_Type (Typ)
11579 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
11580 and then (Etype (Right_Opnd (Operand)) = Universal_Real
11581 or else
11582 Etype (Left_Opnd (Operand)) = Universal_Real)
11583 then
11584 -- Return if expression is ambiguous
11585
11586 if Unique_Fixed_Point_Type (N) = Any_Type then
11587 return;
11588
11589 -- If nothing else, the available fixed type is Duration
11590
11591 else
11592 Set_Etype (Operand, Standard_Duration);
11593 end if;
11594
11595 -- Resolve the real operand with largest available precision
11596
11597 if Etype (Right_Opnd (Operand)) = Universal_Real then
11598 Rop := New_Copy_Tree (Right_Opnd (Operand));
11599 else
11600 Rop := New_Copy_Tree (Left_Opnd (Operand));
11601 end if;
11602
11603 Resolve (Rop, Universal_Real);
11604
11605 -- If the operand is a literal (it could be a non-static and
11606 -- illegal exponentiation) check whether the use of Duration
11607 -- is potentially inaccurate.
11608
11609 if Nkind (Rop) = N_Real_Literal
11610 and then Realval (Rop) /= Ureal_0
11611 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
11612 then
11613 Error_Msg_N
11614 ("??universal real operand can only "
11615 & "be interpreted as Duration!", Rop);
11616 Error_Msg_N
11617 ("\??precision will be lost in the conversion!", Rop);
11618 end if;
11619
11620 elsif Is_Numeric_Type (Typ)
11621 and then Nkind (Operand) in N_Op
11622 and then Unique_Fixed_Point_Type (N) /= Any_Type
11623 then
11624 Set_Etype (Operand, Standard_Duration);
11625
11626 else
11627 Error_Msg_N ("invalid context for mixed mode operation", N);
11628 Set_Etype (Operand, Any_Type);
11629 return;
11630 end if;
11631 end if;
11632
11633 Resolve (Operand);
11634
11635 -- In SPARK, a type conversion between array types should be restricted
11636 -- to types which have matching static bounds.
11637
11638 -- Protect call to Matching_Static_Array_Bounds to avoid costly
11639 -- operation if not needed.
11640
11641 if Restriction_Check_Required (SPARK_05)
11642 and then Is_Array_Type (Target_Typ)
11643 and then Is_Array_Type (Operand_Typ)
11644 and then Operand_Typ /= Any_Composite -- or else Operand in error
11645 and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ)
11646 then
11647 Check_SPARK_05_Restriction
11648 ("array types should have matching static bounds", N);
11649 end if;
11650
11651 -- In formal mode, the operand of an ancestor type conversion must be an
11652 -- object (not an expression).
11653
11654 if Is_Tagged_Type (Target_Typ)
11655 and then not Is_Class_Wide_Type (Target_Typ)
11656 and then Is_Tagged_Type (Operand_Typ)
11657 and then not Is_Class_Wide_Type (Operand_Typ)
11658 and then Is_Ancestor (Target_Typ, Operand_Typ)
11659 and then not Is_SPARK_05_Object_Reference (Operand)
11660 then
11661 Check_SPARK_05_Restriction ("object required", Operand);
11662 end if;
11663
11664 Analyze_Dimension (N);
11665
11666 -- Note: we do the Eval_Type_Conversion call before applying the
11667 -- required checks for a subtype conversion. This is important, since
11668 -- both are prepared under certain circumstances to change the type
11669 -- conversion to a constraint error node, but in the case of
11670 -- Eval_Type_Conversion this may reflect an illegality in the static
11671 -- case, and we would miss the illegality (getting only a warning
11672 -- message), if we applied the type conversion checks first.
11673
11674 Eval_Type_Conversion (N);
11675
11676 -- Even when evaluation is not possible, we may be able to simplify the
11677 -- conversion or its expression. This needs to be done before applying
11678 -- checks, since otherwise the checks may use the original expression
11679 -- and defeat the simplifications. This is specifically the case for
11680 -- elimination of the floating-point Truncation attribute in
11681 -- float-to-int conversions.
11682
11683 Simplify_Type_Conversion (N);
11684
11685 -- If after evaluation we still have a type conversion, then we may need
11686 -- to apply checks required for a subtype conversion.
11687
11688 -- Skip these type conversion checks if universal fixed operands
11689 -- operands involved, since range checks are handled separately for
11690 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
11691
11692 if Nkind (N) = N_Type_Conversion
11693 and then not Is_Generic_Type (Root_Type (Target_Typ))
11694 and then Target_Typ /= Universal_Fixed
11695 and then Operand_Typ /= Universal_Fixed
11696 then
11697 Apply_Type_Conversion_Checks (N);
11698 end if;
11699
11700 -- Issue warning for conversion of simple object to its own type. We
11701 -- have to test the original nodes, since they may have been rewritten
11702 -- by various optimizations.
11703
11704 Orig_N := Original_Node (N);
11705
11706 -- Here we test for a redundant conversion if the warning mode is
11707 -- active (and was not locally reset), and we have a type conversion
11708 -- from source not appearing in a generic instance.
11709
11710 if Test_Redundant
11711 and then Nkind (Orig_N) = N_Type_Conversion
11712 and then Comes_From_Source (Orig_N)
11713 and then not In_Instance
11714 then
11715 Orig_N := Original_Node (Expression (Orig_N));
11716 Orig_T := Target_Typ;
11717
11718 -- If the node is part of a larger expression, the Target_Type
11719 -- may not be the original type of the node if the context is a
11720 -- condition. Recover original type to see if conversion is needed.
11721
11722 if Is_Boolean_Type (Orig_T)
11723 and then Nkind (Parent (N)) in N_Op
11724 then
11725 Orig_T := Etype (Parent (N));
11726 end if;
11727
11728 -- If we have an entity name, then give the warning if the entity
11729 -- is the right type, or if it is a loop parameter covered by the
11730 -- original type (that's needed because loop parameters have an
11731 -- odd subtype coming from the bounds).
11732
11733 if (Is_Entity_Name (Orig_N)
11734 and then
11735 (Etype (Entity (Orig_N)) = Orig_T
11736 or else
11737 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
11738 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
11739
11740 -- If not an entity, then type of expression must match
11741
11742 or else Etype (Orig_N) = Orig_T
11743 then
11744 -- One more check, do not give warning if the analyzed conversion
11745 -- has an expression with non-static bounds, and the bounds of the
11746 -- target are static. This avoids junk warnings in cases where the
11747 -- conversion is necessary to establish staticness, for example in
11748 -- a case statement.
11749
11750 if not Is_OK_Static_Subtype (Operand_Typ)
11751 and then Is_OK_Static_Subtype (Target_Typ)
11752 then
11753 null;
11754
11755 -- Finally, if this type conversion occurs in a context requiring
11756 -- a prefix, and the expression is a qualified expression then the
11757 -- type conversion is not redundant, since a qualified expression
11758 -- is not a prefix, whereas a type conversion is. For example, "X
11759 -- := T'(Funx(...)).Y;" is illegal because a selected component
11760 -- requires a prefix, but a type conversion makes it legal: "X :=
11761 -- T(T'(Funx(...))).Y;"
11762
11763 -- In Ada 2012, a qualified expression is a name, so this idiom is
11764 -- no longer needed, but we still suppress the warning because it
11765 -- seems unfriendly for warnings to pop up when you switch to the
11766 -- newer language version.
11767
11768 elsif Nkind (Orig_N) = N_Qualified_Expression
11769 and then Nkind_In (Parent (N), N_Attribute_Reference,
11770 N_Indexed_Component,
11771 N_Selected_Component,
11772 N_Slice,
11773 N_Explicit_Dereference)
11774 then
11775 null;
11776
11777 -- Never warn on conversion to Long_Long_Integer'Base since
11778 -- that is most likely an artifact of the extended overflow
11779 -- checking and comes from complex expanded code.
11780
11781 elsif Orig_T = Base_Type (Standard_Long_Long_Integer) then
11782 null;
11783
11784 -- Here we give the redundant conversion warning. If it is an
11785 -- entity, give the name of the entity in the message. If not,
11786 -- just mention the expression.
11787
11788 -- Shoudn't we test Warn_On_Redundant_Constructs here ???
11789
11790 else
11791 if Is_Entity_Name (Orig_N) then
11792 Error_Msg_Node_2 := Orig_T;
11793 Error_Msg_NE -- CODEFIX
11794 ("??redundant conversion, & is of type &!",
11795 N, Entity (Orig_N));
11796 else
11797 Error_Msg_NE
11798 ("??redundant conversion, expression is of type&!",
11799 N, Orig_T);
11800 end if;
11801 end if;
11802 end if;
11803 end if;
11804
11805 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
11806 -- No need to perform any interface conversion if the type of the
11807 -- expression coincides with the target type.
11808
11809 if Ada_Version >= Ada_2005
11810 and then Expander_Active
11811 and then Operand_Typ /= Target_Typ
11812 then
11813 declare
11814 Opnd : Entity_Id := Operand_Typ;
11815 Target : Entity_Id := Target_Typ;
11816
11817 begin
11818 -- If the type of the operand is a limited view, use nonlimited
11819 -- view when available. If it is a class-wide type, recover the
11820 -- class-wide type of the nonlimited view.
11821
11822 if From_Limited_With (Opnd)
11823 and then Has_Non_Limited_View (Opnd)
11824 then
11825 Opnd := Non_Limited_View (Opnd);
11826 Set_Etype (Expression (N), Opnd);
11827 end if;
11828
11829 -- It seems that Non_Limited_View should also be applied for
11830 -- Target when it has a limited view, but that leads to missing
11831 -- error checks on interface conversions further below. ???
11832
11833 if Is_Access_Type (Opnd) then
11834 Opnd := Designated_Type (Opnd);
11835
11836 -- If the type of the operand is a limited view, use nonlimited
11837 -- view when available. If it is a class-wide type, recover the
11838 -- class-wide type of the nonlimited view.
11839
11840 if From_Limited_With (Opnd)
11841 and then Has_Non_Limited_View (Opnd)
11842 then
11843 Opnd := Non_Limited_View (Opnd);
11844 end if;
11845 end if;
11846
11847 if Is_Access_Type (Target_Typ) then
11848 Target := Designated_Type (Target);
11849
11850 -- If the target type is a limited view, use nonlimited view
11851 -- when available.
11852
11853 if From_Limited_With (Target)
11854 and then Has_Non_Limited_View (Target)
11855 then
11856 Target := Non_Limited_View (Target);
11857 end if;
11858 end if;
11859
11860 if Opnd = Target then
11861 null;
11862
11863 -- Conversion from interface type
11864
11865 -- It seems that it would be better for the error checks below
11866 -- to be performed as part of Validate_Conversion (and maybe some
11867 -- of the error checks above could be moved as well?). ???
11868
11869 elsif Is_Interface (Opnd) then
11870
11871 -- Ada 2005 (AI-217): Handle entities from limited views
11872
11873 if From_Limited_With (Opnd) then
11874 Error_Msg_Qual_Level := 99;
11875 Error_Msg_NE -- CODEFIX
11876 ("missing WITH clause on package &", N,
11877 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
11878 Error_Msg_N
11879 ("type conversions require visibility of the full view",
11880 N);
11881
11882 elsif From_Limited_With (Target)
11883 and then not
11884 (Is_Access_Type (Target_Typ)
11885 and then Present (Non_Limited_View (Etype (Target))))
11886 then
11887 Error_Msg_Qual_Level := 99;
11888 Error_Msg_NE -- CODEFIX
11889 ("missing WITH clause on package &", N,
11890 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
11891 Error_Msg_N
11892 ("type conversions require visibility of the full view",
11893 N);
11894
11895 else
11896 Expand_Interface_Conversion (N);
11897 end if;
11898
11899 -- Conversion to interface type
11900
11901 elsif Is_Interface (Target) then
11902
11903 -- Handle subtypes
11904
11905 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
11906 Opnd := Etype (Opnd);
11907 end if;
11908
11909 if Is_Class_Wide_Type (Opnd)
11910 or else Interface_Present_In_Ancestor
11911 (Typ => Opnd,
11912 Iface => Target)
11913 then
11914 Expand_Interface_Conversion (N);
11915 else
11916 Error_Msg_Name_1 := Chars (Etype (Target));
11917 Error_Msg_Name_2 := Chars (Opnd);
11918 Error_Msg_N
11919 ("wrong interface conversion (% is not a progenitor "
11920 & "of %)", N);
11921 end if;
11922 end if;
11923 end;
11924 end if;
11925
11926 -- Ada 2012: once the type conversion is resolved, check whether the
11927 -- operand statisfies the static predicate of the target type.
11928
11929 if Has_Predicates (Target_Typ) then
11930 Check_Expression_Against_Static_Predicate (N, Target_Typ);
11931 end if;
11932
11933 -- If at this stage we have a real to integer conversion, make sure that
11934 -- the Do_Range_Check flag is set, because such conversions in general
11935 -- need a range check. We only need this if expansion is off.
11936 -- In GNATprove mode, we only do that when converting from fixed-point
11937 -- (as floating-point to integer conversions are now handled in
11938 -- GNATprove mode).
11939
11940 if Nkind (N) = N_Type_Conversion
11941 and then not Expander_Active
11942 and then Is_Integer_Type (Target_Typ)
11943 and then (Is_Fixed_Point_Type (Operand_Typ)
11944 or else (not GNATprove_Mode
11945 and then Is_Floating_Point_Type (Operand_Typ)))
11946 and then not Range_Checks_Suppressed (Target_Typ)
11947 and then not Range_Checks_Suppressed (Operand_Typ)
11948 then
11949 Set_Do_Range_Check (Operand);
11950 end if;
11951
11952 -- Generating C code a type conversion of an access to constrained
11953 -- array type to access to unconstrained array type involves building
11954 -- a fat pointer which in general cannot be generated on the fly. We
11955 -- remove side effects in order to store the result of the conversion
11956 -- into a temporary.
11957
11958 if Modify_Tree_For_C
11959 and then Nkind (N) = N_Type_Conversion
11960 and then Nkind (Parent (N)) /= N_Object_Declaration
11961 and then Is_Access_Type (Etype (N))
11962 and then Is_Array_Type (Designated_Type (Etype (N)))
11963 and then not Is_Constrained (Designated_Type (Etype (N)))
11964 and then Is_Constrained (Designated_Type (Etype (Expression (N))))
11965 then
11966 Remove_Side_Effects (N);
11967 end if;
11968 end Resolve_Type_Conversion;
11969
11970 ----------------------
11971 -- Resolve_Unary_Op --
11972 ----------------------
11973
11974 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
11975 B_Typ : constant Entity_Id := Base_Type (Typ);
11976 R : constant Node_Id := Right_Opnd (N);
11977 OK : Boolean;
11978 Lo : Uint;
11979 Hi : Uint;
11980
11981 begin
11982 if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then
11983 Error_Msg_Name_1 := Chars (Typ);
11984 Check_SPARK_05_Restriction
11985 ("unary operator not defined for modular type%", N);
11986 end if;
11987
11988 -- Deal with intrinsic unary operators
11989
11990 if Comes_From_Source (N)
11991 and then Ekind (Entity (N)) = E_Function
11992 and then Is_Imported (Entity (N))
11993 and then Is_Intrinsic_Subprogram (Entity (N))
11994 then
11995 Resolve_Intrinsic_Unary_Operator (N, Typ);
11996 return;
11997 end if;
11998
11999 -- Deal with universal cases
12000
12001 if Etype (R) = Universal_Integer
12002 or else
12003 Etype (R) = Universal_Real
12004 then
12005 Check_For_Visible_Operator (N, B_Typ);
12006 end if;
12007
12008 Set_Etype (N, B_Typ);
12009 Resolve (R, B_Typ);
12010
12011 -- Generate warning for expressions like abs (x mod 2)
12012
12013 if Warn_On_Redundant_Constructs
12014 and then Nkind (N) = N_Op_Abs
12015 then
12016 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
12017
12018 if OK and then Hi >= Lo and then Lo >= 0 then
12019 Error_Msg_N -- CODEFIX
12020 ("?r?abs applied to known non-negative value has no effect", N);
12021 end if;
12022 end if;
12023
12024 -- Deal with reference generation
12025
12026 Check_Unset_Reference (R);
12027 Generate_Operator_Reference (N, B_Typ);
12028 Analyze_Dimension (N);
12029 Eval_Unary_Op (N);
12030
12031 -- Set overflow checking bit. Much cleverer code needed here eventually
12032 -- and perhaps the Resolve routines should be separated for the various
12033 -- arithmetic operations, since they will need different processing ???
12034
12035 if Nkind (N) in N_Op then
12036 if not Overflow_Checks_Suppressed (Etype (N)) then
12037 Enable_Overflow_Check (N);
12038 end if;
12039 end if;
12040
12041 -- Generate warning for expressions like -5 mod 3 for integers. No need
12042 -- to worry in the floating-point case, since parens do not affect the
12043 -- result so there is no point in giving in a warning.
12044
12045 declare
12046 Norig : constant Node_Id := Original_Node (N);
12047 Rorig : Node_Id;
12048 Val : Uint;
12049 HB : Uint;
12050 LB : Uint;
12051 Lval : Uint;
12052 Opnd : Node_Id;
12053
12054 begin
12055 if Warn_On_Questionable_Missing_Parens
12056 and then Comes_From_Source (Norig)
12057 and then Is_Integer_Type (Typ)
12058 and then Nkind (Norig) = N_Op_Minus
12059 then
12060 Rorig := Original_Node (Right_Opnd (Norig));
12061
12062 -- We are looking for cases where the right operand is not
12063 -- parenthesized, and is a binary operator, multiply, divide, or
12064 -- mod. These are the cases where the grouping can affect results.
12065
12066 if Paren_Count (Rorig) = 0
12067 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
12068 then
12069 -- For mod, we always give the warning, since the value is
12070 -- affected by the parenthesization (e.g. (-5) mod 315 /=
12071 -- -(5 mod 315)). But for the other cases, the only concern is
12072 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
12073 -- overflows, but (-2) * 64 does not). So we try to give the
12074 -- message only when overflow is possible.
12075
12076 if Nkind (Rorig) /= N_Op_Mod
12077 and then Compile_Time_Known_Value (R)
12078 then
12079 Val := Expr_Value (R);
12080
12081 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
12082 HB := Expr_Value (Type_High_Bound (Typ));
12083 else
12084 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
12085 end if;
12086
12087 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
12088 LB := Expr_Value (Type_Low_Bound (Typ));
12089 else
12090 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
12091 end if;
12092
12093 -- Note that the test below is deliberately excluding the
12094 -- largest negative number, since that is a potentially
12095 -- troublesome case (e.g. -2 * x, where the result is the
12096 -- largest negative integer has an overflow with 2 * x).
12097
12098 if Val > LB and then Val <= HB then
12099 return;
12100 end if;
12101 end if;
12102
12103 -- For the multiplication case, the only case we have to worry
12104 -- about is when (-a)*b is exactly the largest negative number
12105 -- so that -(a*b) can cause overflow. This can only happen if
12106 -- a is a power of 2, and more generally if any operand is a
12107 -- constant that is not a power of 2, then the parentheses
12108 -- cannot affect whether overflow occurs. We only bother to
12109 -- test the left most operand
12110
12111 -- Loop looking at left operands for one that has known value
12112
12113 Opnd := Rorig;
12114 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
12115 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
12116 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
12117
12118 -- Operand value of 0 or 1 skips warning
12119
12120 if Lval <= 1 then
12121 return;
12122
12123 -- Otherwise check power of 2, if power of 2, warn, if
12124 -- anything else, skip warning.
12125
12126 else
12127 while Lval /= 2 loop
12128 if Lval mod 2 = 1 then
12129 return;
12130 else
12131 Lval := Lval / 2;
12132 end if;
12133 end loop;
12134
12135 exit Opnd_Loop;
12136 end if;
12137 end if;
12138
12139 -- Keep looking at left operands
12140
12141 Opnd := Left_Opnd (Opnd);
12142 end loop Opnd_Loop;
12143
12144 -- For rem or "/" we can only have a problematic situation
12145 -- if the divisor has a value of minus one or one. Otherwise
12146 -- overflow is impossible (divisor > 1) or we have a case of
12147 -- division by zero in any case.
12148
12149 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
12150 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
12151 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
12152 then
12153 return;
12154 end if;
12155
12156 -- If we fall through warning should be issued
12157
12158 -- Shouldn't we test Warn_On_Questionable_Missing_Parens ???
12159
12160 Error_Msg_N
12161 ("??unary minus expression should be parenthesized here!", N);
12162 end if;
12163 end if;
12164 end;
12165 end Resolve_Unary_Op;
12166
12167 ----------------------------------
12168 -- Resolve_Unchecked_Expression --
12169 ----------------------------------
12170
12171 procedure Resolve_Unchecked_Expression
12172 (N : Node_Id;
12173 Typ : Entity_Id)
12174 is
12175 begin
12176 Resolve (Expression (N), Typ, Suppress => All_Checks);
12177 Set_Etype (N, Typ);
12178 end Resolve_Unchecked_Expression;
12179
12180 ---------------------------------------
12181 -- Resolve_Unchecked_Type_Conversion --
12182 ---------------------------------------
12183
12184 procedure Resolve_Unchecked_Type_Conversion
12185 (N : Node_Id;
12186 Typ : Entity_Id)
12187 is
12188 pragma Warnings (Off, Typ);
12189
12190 Operand : constant Node_Id := Expression (N);
12191 Opnd_Type : constant Entity_Id := Etype (Operand);
12192
12193 begin
12194 -- Resolve operand using its own type
12195
12196 Resolve (Operand, Opnd_Type);
12197
12198 -- In an inlined context, the unchecked conversion may be applied
12199 -- to a literal, in which case its type is the type of the context.
12200 -- (In other contexts conversions cannot apply to literals).
12201
12202 if In_Inlined_Body
12203 and then (Opnd_Type = Any_Character or else
12204 Opnd_Type = Any_Integer or else
12205 Opnd_Type = Any_Real)
12206 then
12207 Set_Etype (Operand, Typ);
12208 end if;
12209
12210 Analyze_Dimension (N);
12211 Eval_Unchecked_Conversion (N);
12212 end Resolve_Unchecked_Type_Conversion;
12213
12214 ------------------------------
12215 -- Rewrite_Operator_As_Call --
12216 ------------------------------
12217
12218 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
12219 Loc : constant Source_Ptr := Sloc (N);
12220 Actuals : constant List_Id := New_List;
12221 New_N : Node_Id;
12222
12223 begin
12224 if Nkind (N) in N_Binary_Op then
12225 Append (Left_Opnd (N), Actuals);
12226 end if;
12227
12228 Append (Right_Opnd (N), Actuals);
12229
12230 New_N :=
12231 Make_Function_Call (Sloc => Loc,
12232 Name => New_Occurrence_Of (Nam, Loc),
12233 Parameter_Associations => Actuals);
12234
12235 Preserve_Comes_From_Source (New_N, N);
12236 Preserve_Comes_From_Source (Name (New_N), N);
12237 Rewrite (N, New_N);
12238 Set_Etype (N, Etype (Nam));
12239 end Rewrite_Operator_As_Call;
12240
12241 ------------------------------
12242 -- Rewrite_Renamed_Operator --
12243 ------------------------------
12244
12245 procedure Rewrite_Renamed_Operator
12246 (N : Node_Id;
12247 Op : Entity_Id;
12248 Typ : Entity_Id)
12249 is
12250 Nam : constant Name_Id := Chars (Op);
12251 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
12252 Op_Node : Node_Id;
12253
12254 begin
12255 -- Do not perform this transformation within a pre/postcondition,
12256 -- because the expression will be reanalyzed, and the transformation
12257 -- might affect the visibility of the operator, e.g. in an instance.
12258 -- Note that fully analyzed and expanded pre/postconditions appear as
12259 -- pragma Check equivalents.
12260
12261 if In_Pre_Post_Condition (N) then
12262 return;
12263 end if;
12264
12265 -- Likewise when an expression function is being preanalyzed, since the
12266 -- expression will be reanalyzed as part of the generated body.
12267
12268 if In_Spec_Expression then
12269 declare
12270 S : constant Entity_Id := Current_Scope_No_Loops;
12271 begin
12272 if Ekind (S) = E_Function
12273 and then Nkind (Original_Node (Unit_Declaration_Node (S))) =
12274 N_Expression_Function
12275 then
12276 return;
12277 end if;
12278 end;
12279 end if;
12280
12281 -- Rewrite the operator node using the real operator, not its renaming.
12282 -- Exclude user-defined intrinsic operations of the same name, which are
12283 -- treated separately and rewritten as calls.
12284
12285 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
12286 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
12287 Set_Chars (Op_Node, Nam);
12288 Set_Etype (Op_Node, Etype (N));
12289 Set_Entity (Op_Node, Op);
12290 Set_Right_Opnd (Op_Node, Right_Opnd (N));
12291
12292 -- Indicate that both the original entity and its renaming are
12293 -- referenced at this point.
12294
12295 Generate_Reference (Entity (N), N);
12296 Generate_Reference (Op, N);
12297
12298 if Is_Binary then
12299 Set_Left_Opnd (Op_Node, Left_Opnd (N));
12300 end if;
12301
12302 Rewrite (N, Op_Node);
12303
12304 -- If the context type is private, add the appropriate conversions so
12305 -- that the operator is applied to the full view. This is done in the
12306 -- routines that resolve intrinsic operators.
12307
12308 if Is_Intrinsic_Subprogram (Op) and then Is_Private_Type (Typ) then
12309 case Nkind (N) is
12310 when N_Op_Add
12311 | N_Op_Divide
12312 | N_Op_Expon
12313 | N_Op_Mod
12314 | N_Op_Multiply
12315 | N_Op_Rem
12316 | N_Op_Subtract
12317 =>
12318 Resolve_Intrinsic_Operator (N, Typ);
12319
12320 when N_Op_Abs
12321 | N_Op_Minus
12322 | N_Op_Plus
12323 =>
12324 Resolve_Intrinsic_Unary_Operator (N, Typ);
12325
12326 when others =>
12327 Resolve (N, Typ);
12328 end case;
12329 end if;
12330
12331 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
12332
12333 -- Operator renames a user-defined operator of the same name. Use the
12334 -- original operator in the node, which is the one Gigi knows about.
12335
12336 Set_Entity (N, Op);
12337 Set_Is_Overloaded (N, False);
12338 end if;
12339 end Rewrite_Renamed_Operator;
12340
12341 -----------------------
12342 -- Set_Slice_Subtype --
12343 -----------------------
12344
12345 -- Build an implicit subtype declaration to represent the type delivered by
12346 -- the slice. This is an abbreviated version of an array subtype. We define
12347 -- an index subtype for the slice, using either the subtype name or the
12348 -- discrete range of the slice. To be consistent with index usage elsewhere
12349 -- we create a list header to hold the single index. This list is not
12350 -- otherwise attached to the syntax tree.
12351
12352 procedure Set_Slice_Subtype (N : Node_Id) is
12353 Loc : constant Source_Ptr := Sloc (N);
12354 Index_List : constant List_Id := New_List;
12355 Index : Node_Id;
12356 Index_Subtype : Entity_Id;
12357 Index_Type : Entity_Id;
12358 Slice_Subtype : Entity_Id;
12359 Drange : constant Node_Id := Discrete_Range (N);
12360
12361 begin
12362 Index_Type := Base_Type (Etype (Drange));
12363
12364 if Is_Entity_Name (Drange) then
12365 Index_Subtype := Entity (Drange);
12366
12367 else
12368 -- We force the evaluation of a range. This is definitely needed in
12369 -- the renamed case, and seems safer to do unconditionally. Note in
12370 -- any case that since we will create and insert an Itype referring
12371 -- to this range, we must make sure any side effect removal actions
12372 -- are inserted before the Itype definition.
12373
12374 if Nkind (Drange) = N_Range then
12375 Force_Evaluation (Low_Bound (Drange));
12376 Force_Evaluation (High_Bound (Drange));
12377
12378 -- If the discrete range is given by a subtype indication, the
12379 -- type of the slice is the base of the subtype mark.
12380
12381 elsif Nkind (Drange) = N_Subtype_Indication then
12382 declare
12383 R : constant Node_Id := Range_Expression (Constraint (Drange));
12384 begin
12385 Index_Type := Base_Type (Entity (Subtype_Mark (Drange)));
12386 Force_Evaluation (Low_Bound (R));
12387 Force_Evaluation (High_Bound (R));
12388 end;
12389 end if;
12390
12391 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
12392
12393 -- Take a new copy of Drange (where bounds have been rewritten to
12394 -- reference side-effect-free names). Using a separate tree ensures
12395 -- that further expansion (e.g. while rewriting a slice assignment
12396 -- into a FOR loop) does not attempt to remove side effects on the
12397 -- bounds again (which would cause the bounds in the index subtype
12398 -- definition to refer to temporaries before they are defined) (the
12399 -- reason is that some names are considered side effect free here
12400 -- for the subtype, but not in the context of a loop iteration
12401 -- scheme).
12402
12403 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
12404 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
12405 Set_Etype (Index_Subtype, Index_Type);
12406 Set_Size_Info (Index_Subtype, Index_Type);
12407 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
12408 Set_Is_Constrained (Index_Subtype);
12409 end if;
12410
12411 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
12412
12413 Index := New_Occurrence_Of (Index_Subtype, Loc);
12414 Set_Etype (Index, Index_Subtype);
12415 Append (Index, Index_List);
12416
12417 Set_First_Index (Slice_Subtype, Index);
12418 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
12419 Set_Is_Constrained (Slice_Subtype, True);
12420
12421 Check_Compile_Time_Size (Slice_Subtype);
12422
12423 -- The Etype of the existing Slice node is reset to this slice subtype.
12424 -- Its bounds are obtained from its first index.
12425
12426 Set_Etype (N, Slice_Subtype);
12427
12428 -- For bit-packed slice subtypes, freeze immediately (except in the case
12429 -- of being in a "spec expression" where we never freeze when we first
12430 -- see the expression).
12431
12432 if Is_Bit_Packed_Array (Slice_Subtype) and not In_Spec_Expression then
12433 Freeze_Itype (Slice_Subtype, N);
12434
12435 -- For all other cases insert an itype reference in the slice's actions
12436 -- so that the itype is frozen at the proper place in the tree (i.e. at
12437 -- the point where actions for the slice are analyzed). Note that this
12438 -- is different from freezing the itype immediately, which might be
12439 -- premature (e.g. if the slice is within a transient scope). This needs
12440 -- to be done only if expansion is enabled.
12441
12442 elsif Expander_Active then
12443 Ensure_Defined (Typ => Slice_Subtype, N => N);
12444 end if;
12445 end Set_Slice_Subtype;
12446
12447 --------------------------------
12448 -- Set_String_Literal_Subtype --
12449 --------------------------------
12450
12451 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
12452 Loc : constant Source_Ptr := Sloc (N);
12453 Low_Bound : constant Node_Id :=
12454 Type_Low_Bound (Etype (First_Index (Typ)));
12455 Subtype_Id : Entity_Id;
12456
12457 begin
12458 if Nkind (N) /= N_String_Literal then
12459 return;
12460 end if;
12461
12462 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
12463 Set_String_Literal_Length (Subtype_Id, UI_From_Int
12464 (String_Length (Strval (N))));
12465 Set_Etype (Subtype_Id, Base_Type (Typ));
12466 Set_Is_Constrained (Subtype_Id);
12467 Set_Etype (N, Subtype_Id);
12468
12469 -- The low bound is set from the low bound of the corresponding index
12470 -- type. Note that we do not store the high bound in the string literal
12471 -- subtype, but it can be deduced if necessary from the length and the
12472 -- low bound.
12473
12474 if Is_OK_Static_Expression (Low_Bound) then
12475 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
12476
12477 -- If the lower bound is not static we create a range for the string
12478 -- literal, using the index type and the known length of the literal.
12479 -- If the length is 1, then the upper bound is set to a mere copy of
12480 -- the lower bound; or else, if the index type is a signed integer,
12481 -- then the upper bound is computed as Low_Bound + L - 1; otherwise,
12482 -- the upper bound is computed as T'Val (T'Pos (Low_Bound) + L - 1).
12483
12484 else
12485 declare
12486 Length : constant Nat := String_Length (Strval (N));
12487 Index_List : constant List_Id := New_List;
12488 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
12489 Array_Subtype : Entity_Id;
12490 Drange : Node_Id;
12491 High_Bound : Node_Id;
12492 Index : Node_Id;
12493 Index_Subtype : Entity_Id;
12494
12495 begin
12496 if Length = 1 then
12497 High_Bound := New_Copy_Tree (Low_Bound);
12498
12499 elsif Is_Signed_Integer_Type (Index_Type) then
12500 High_Bound :=
12501 Make_Op_Add (Loc,
12502 Left_Opnd => New_Copy_Tree (Low_Bound),
12503 Right_Opnd => Make_Integer_Literal (Loc, Length - 1));
12504
12505 else
12506 High_Bound :=
12507 Make_Attribute_Reference (Loc,
12508 Attribute_Name => Name_Val,
12509 Prefix =>
12510 New_Occurrence_Of (Index_Type, Loc),
12511 Expressions => New_List (
12512 Make_Op_Add (Loc,
12513 Left_Opnd =>
12514 Make_Attribute_Reference (Loc,
12515 Attribute_Name => Name_Pos,
12516 Prefix =>
12517 New_Occurrence_Of (Index_Type, Loc),
12518 Expressions =>
12519 New_List (New_Copy_Tree (Low_Bound))),
12520 Right_Opnd =>
12521 Make_Integer_Literal (Loc, Length - 1))));
12522 end if;
12523
12524 if Is_Integer_Type (Index_Type) then
12525 Set_String_Literal_Low_Bound
12526 (Subtype_Id, Make_Integer_Literal (Loc, 1));
12527
12528 else
12529 -- If the index type is an enumeration type, build bounds
12530 -- expression with attributes.
12531
12532 Set_String_Literal_Low_Bound
12533 (Subtype_Id,
12534 Make_Attribute_Reference (Loc,
12535 Attribute_Name => Name_First,
12536 Prefix =>
12537 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
12538 end if;
12539
12540 Analyze_And_Resolve
12541 (String_Literal_Low_Bound (Subtype_Id), Base_Type (Index_Type));
12542
12543 -- Build bona fide subtype for the string, and wrap it in an
12544 -- unchecked conversion, because the back end expects the
12545 -- String_Literal_Subtype to have a static lower bound.
12546
12547 Index_Subtype :=
12548 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
12549 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
12550 Set_Scalar_Range (Index_Subtype, Drange);
12551 Set_Parent (Drange, N);
12552 Analyze_And_Resolve (Drange, Index_Type);
12553
12554 -- In this context, the Index_Type may already have a constraint,
12555 -- so use common base type on string subtype. The base type may
12556 -- be used when generating attributes of the string, for example
12557 -- in the context of a slice assignment.
12558
12559 Set_Etype (Index_Subtype, Base_Type (Index_Type));
12560 Set_Size_Info (Index_Subtype, Index_Type);
12561 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
12562
12563 Array_Subtype := Create_Itype (E_Array_Subtype, N);
12564
12565 Index := New_Occurrence_Of (Index_Subtype, Loc);
12566 Set_Etype (Index, Index_Subtype);
12567 Append (Index, Index_List);
12568
12569 Set_First_Index (Array_Subtype, Index);
12570 Set_Etype (Array_Subtype, Base_Type (Typ));
12571 Set_Is_Constrained (Array_Subtype, True);
12572
12573 Rewrite (N,
12574 Make_Unchecked_Type_Conversion (Loc,
12575 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
12576 Expression => Relocate_Node (N)));
12577 Set_Etype (N, Array_Subtype);
12578 end;
12579 end if;
12580 end Set_String_Literal_Subtype;
12581
12582 ------------------------------
12583 -- Simplify_Type_Conversion --
12584 ------------------------------
12585
12586 procedure Simplify_Type_Conversion (N : Node_Id) is
12587 begin
12588 if Nkind (N) = N_Type_Conversion then
12589 declare
12590 Operand : constant Node_Id := Expression (N);
12591 Target_Typ : constant Entity_Id := Etype (N);
12592 Opnd_Typ : constant Entity_Id := Etype (Operand);
12593
12594 begin
12595 -- Special processing if the conversion is the expression of a
12596 -- Rounding or Truncation attribute reference. In this case we
12597 -- replace:
12598
12599 -- ityp (ftyp'Rounding (x)) or ityp (ftyp'Truncation (x))
12600
12601 -- by
12602
12603 -- ityp (x)
12604
12605 -- with the Float_Truncate flag set to False or True respectively,
12606 -- which is more efficient. We reuse Rounding for Machine_Rounding
12607 -- as System.Fat_Gen, which is a permissible behavior.
12608
12609 if Is_Floating_Point_Type (Opnd_Typ)
12610 and then
12611 (Is_Integer_Type (Target_Typ)
12612 or else (Is_Fixed_Point_Type (Target_Typ)
12613 and then Conversion_OK (N)))
12614 and then Nkind (Operand) = N_Attribute_Reference
12615 and then Nam_In (Attribute_Name (Operand), Name_Rounding,
12616 Name_Machine_Rounding,
12617 Name_Truncation)
12618 then
12619 declare
12620 Truncate : constant Boolean :=
12621 Attribute_Name (Operand) = Name_Truncation;
12622 begin
12623 Rewrite (Operand,
12624 Relocate_Node (First (Expressions (Operand))));
12625 Set_Float_Truncate (N, Truncate);
12626 end;
12627
12628 -- Special processing for the conversion of an integer literal to
12629 -- a dynamic type: we first convert the literal to the root type
12630 -- and then convert the result to the target type, the goal being
12631 -- to avoid doing range checks in universal integer.
12632
12633 elsif Is_Integer_Type (Target_Typ)
12634 and then not Is_Generic_Type (Root_Type (Target_Typ))
12635 and then Nkind (Operand) = N_Integer_Literal
12636 and then Opnd_Typ = Universal_Integer
12637 then
12638 Convert_To_And_Rewrite (Root_Type (Target_Typ), Operand);
12639 Analyze_And_Resolve (Operand);
12640
12641 -- If the expression is a conversion to universal integer of an
12642 -- an expression with an integer type, then we can eliminate the
12643 -- intermediate conversion to universal integer.
12644
12645 elsif Nkind (Operand) = N_Type_Conversion
12646 and then Entity (Subtype_Mark (Operand)) = Universal_Integer
12647 and then Is_Integer_Type (Etype (Expression (Operand)))
12648 then
12649 Rewrite (Operand, Relocate_Node (Expression (Operand)));
12650 Analyze_And_Resolve (Operand);
12651 end if;
12652 end;
12653 end if;
12654 end Simplify_Type_Conversion;
12655
12656 -----------------------------
12657 -- Unique_Fixed_Point_Type --
12658 -----------------------------
12659
12660 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
12661 procedure Fixed_Point_Error (T1 : Entity_Id; T2 : Entity_Id);
12662 -- Give error messages for true ambiguity. Messages are posted on node
12663 -- N, and entities T1, T2 are the possible interpretations.
12664
12665 -----------------------
12666 -- Fixed_Point_Error --
12667 -----------------------
12668
12669 procedure Fixed_Point_Error (T1 : Entity_Id; T2 : Entity_Id) is
12670 begin
12671 Error_Msg_N ("ambiguous universal_fixed_expression", N);
12672 Error_Msg_NE ("\\possible interpretation as}", N, T1);
12673 Error_Msg_NE ("\\possible interpretation as}", N, T2);
12674 end Fixed_Point_Error;
12675
12676 -- Local variables
12677
12678 ErrN : Node_Id;
12679 Item : Node_Id;
12680 Scop : Entity_Id;
12681 T1 : Entity_Id;
12682 T2 : Entity_Id;
12683
12684 -- Start of processing for Unique_Fixed_Point_Type
12685
12686 begin
12687 -- The operations on Duration are visible, so Duration is always a
12688 -- possible interpretation.
12689
12690 T1 := Standard_Duration;
12691
12692 -- Look for fixed-point types in enclosing scopes
12693
12694 Scop := Current_Scope;
12695 while Scop /= Standard_Standard loop
12696 T2 := First_Entity (Scop);
12697 while Present (T2) loop
12698 if Is_Fixed_Point_Type (T2)
12699 and then Current_Entity (T2) = T2
12700 and then Scope (Base_Type (T2)) = Scop
12701 then
12702 if Present (T1) then
12703 Fixed_Point_Error (T1, T2);
12704 return Any_Type;
12705 else
12706 T1 := T2;
12707 end if;
12708 end if;
12709
12710 Next_Entity (T2);
12711 end loop;
12712
12713 Scop := Scope (Scop);
12714 end loop;
12715
12716 -- Look for visible fixed type declarations in the context
12717
12718 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
12719 while Present (Item) loop
12720 if Nkind (Item) = N_With_Clause then
12721 Scop := Entity (Name (Item));
12722 T2 := First_Entity (Scop);
12723 while Present (T2) loop
12724 if Is_Fixed_Point_Type (T2)
12725 and then Scope (Base_Type (T2)) = Scop
12726 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
12727 then
12728 if Present (T1) then
12729 Fixed_Point_Error (T1, T2);
12730 return Any_Type;
12731 else
12732 T1 := T2;
12733 end if;
12734 end if;
12735
12736 Next_Entity (T2);
12737 end loop;
12738 end if;
12739
12740 Next (Item);
12741 end loop;
12742
12743 if Nkind (N) = N_Real_Literal then
12744 Error_Msg_NE ("??real literal interpreted as }!", N, T1);
12745
12746 else
12747 -- When the context is a type conversion, issue the warning on the
12748 -- expression of the conversion because it is the actual operation.
12749
12750 if Nkind_In (N, N_Type_Conversion, N_Unchecked_Type_Conversion) then
12751 ErrN := Expression (N);
12752 else
12753 ErrN := N;
12754 end if;
12755
12756 Error_Msg_NE
12757 ("??universal_fixed expression interpreted as }!", ErrN, T1);
12758 end if;
12759
12760 return T1;
12761 end Unique_Fixed_Point_Type;
12762
12763 ----------------------
12764 -- Valid_Conversion --
12765 ----------------------
12766
12767 function Valid_Conversion
12768 (N : Node_Id;
12769 Target : Entity_Id;
12770 Operand : Node_Id;
12771 Report_Errs : Boolean := True) return Boolean
12772 is
12773 Target_Type : constant Entity_Id := Base_Type (Target);
12774 Opnd_Type : Entity_Id := Etype (Operand);
12775 Inc_Ancestor : Entity_Id;
12776
12777 function Conversion_Check
12778 (Valid : Boolean;
12779 Msg : String) return Boolean;
12780 -- Little routine to post Msg if Valid is False, returns Valid value
12781
12782 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id);
12783 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
12784
12785 procedure Conversion_Error_NE
12786 (Msg : String;
12787 N : Node_Or_Entity_Id;
12788 E : Node_Or_Entity_Id);
12789 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
12790
12791 function In_Instance_Code return Boolean;
12792 -- Return True if expression is within an instance but is not in one of
12793 -- the actuals of the instantiation. Type conversions within an instance
12794 -- are not rechecked because type visbility may lead to spurious errors,
12795 -- but conversions in an actual for a formal object must be checked.
12796
12797 function Valid_Tagged_Conversion
12798 (Target_Type : Entity_Id;
12799 Opnd_Type : Entity_Id) return Boolean;
12800 -- Specifically test for validity of tagged conversions
12801
12802 function Valid_Array_Conversion return Boolean;
12803 -- Check index and component conformance, and accessibility levels if
12804 -- the component types are anonymous access types (Ada 2005).
12805
12806 ----------------------
12807 -- Conversion_Check --
12808 ----------------------
12809
12810 function Conversion_Check
12811 (Valid : Boolean;
12812 Msg : String) return Boolean
12813 is
12814 begin
12815 if not Valid
12816
12817 -- A generic unit has already been analyzed and we have verified
12818 -- that a particular conversion is OK in that context. Since the
12819 -- instance is reanalyzed without relying on the relationships
12820 -- established during the analysis of the generic, it is possible
12821 -- to end up with inconsistent views of private types. Do not emit
12822 -- the error message in such cases. The rest of the machinery in
12823 -- Valid_Conversion still ensures the proper compatibility of
12824 -- target and operand types.
12825
12826 and then not In_Instance_Code
12827 then
12828 Conversion_Error_N (Msg, Operand);
12829 end if;
12830
12831 return Valid;
12832 end Conversion_Check;
12833
12834 ------------------------
12835 -- Conversion_Error_N --
12836 ------------------------
12837
12838 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id) is
12839 begin
12840 if Report_Errs then
12841 Error_Msg_N (Msg, N);
12842 end if;
12843 end Conversion_Error_N;
12844
12845 -------------------------
12846 -- Conversion_Error_NE --
12847 -------------------------
12848
12849 procedure Conversion_Error_NE
12850 (Msg : String;
12851 N : Node_Or_Entity_Id;
12852 E : Node_Or_Entity_Id)
12853 is
12854 begin
12855 if Report_Errs then
12856 Error_Msg_NE (Msg, N, E);
12857 end if;
12858 end Conversion_Error_NE;
12859
12860 ----------------------
12861 -- In_Instance_Code --
12862 ----------------------
12863
12864 function In_Instance_Code return Boolean is
12865 Par : Node_Id;
12866
12867 begin
12868 if not In_Instance then
12869 return False;
12870
12871 else
12872 Par := Parent (N);
12873 while Present (Par) loop
12874
12875 -- The expression is part of an actual object if it appears in
12876 -- the generated object declaration in the instance.
12877
12878 if Nkind (Par) = N_Object_Declaration
12879 and then Present (Corresponding_Generic_Association (Par))
12880 then
12881 return False;
12882
12883 else
12884 exit when
12885 Nkind (Par) in N_Statement_Other_Than_Procedure_Call
12886 or else Nkind (Par) in N_Subprogram_Call
12887 or else Nkind (Par) in N_Declaration;
12888 end if;
12889
12890 Par := Parent (Par);
12891 end loop;
12892
12893 -- Otherwise the expression appears within the instantiated unit
12894
12895 return True;
12896 end if;
12897 end In_Instance_Code;
12898
12899 ----------------------------
12900 -- Valid_Array_Conversion --
12901 ----------------------------
12902
12903 function Valid_Array_Conversion return Boolean is
12904 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
12905 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
12906
12907 Opnd_Index : Node_Id;
12908 Opnd_Index_Type : Entity_Id;
12909
12910 Target_Comp_Type : constant Entity_Id :=
12911 Component_Type (Target_Type);
12912 Target_Comp_Base : constant Entity_Id :=
12913 Base_Type (Target_Comp_Type);
12914
12915 Target_Index : Node_Id;
12916 Target_Index_Type : Entity_Id;
12917
12918 begin
12919 -- Error if wrong number of dimensions
12920
12921 if
12922 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
12923 then
12924 Conversion_Error_N
12925 ("incompatible number of dimensions for conversion", Operand);
12926 return False;
12927
12928 -- Number of dimensions matches
12929
12930 else
12931 -- Loop through indexes of the two arrays
12932
12933 Target_Index := First_Index (Target_Type);
12934 Opnd_Index := First_Index (Opnd_Type);
12935 while Present (Target_Index) and then Present (Opnd_Index) loop
12936 Target_Index_Type := Etype (Target_Index);
12937 Opnd_Index_Type := Etype (Opnd_Index);
12938
12939 -- Error if index types are incompatible
12940
12941 if not (Is_Integer_Type (Target_Index_Type)
12942 and then Is_Integer_Type (Opnd_Index_Type))
12943 and then (Root_Type (Target_Index_Type)
12944 /= Root_Type (Opnd_Index_Type))
12945 then
12946 Conversion_Error_N
12947 ("incompatible index types for array conversion",
12948 Operand);
12949 return False;
12950 end if;
12951
12952 Next_Index (Target_Index);
12953 Next_Index (Opnd_Index);
12954 end loop;
12955
12956 -- If component types have same base type, all set
12957
12958 if Target_Comp_Base = Opnd_Comp_Base then
12959 null;
12960
12961 -- Here if base types of components are not the same. The only
12962 -- time this is allowed is if we have anonymous access types.
12963
12964 -- The conversion of arrays of anonymous access types can lead
12965 -- to dangling pointers. AI-392 formalizes the accessibility
12966 -- checks that must be applied to such conversions to prevent
12967 -- out-of-scope references.
12968
12969 elsif Ekind_In
12970 (Target_Comp_Base, E_Anonymous_Access_Type,
12971 E_Anonymous_Access_Subprogram_Type)
12972 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
12973 and then
12974 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
12975 then
12976 if Type_Access_Level (Target_Type) <
12977 Deepest_Type_Access_Level (Opnd_Type)
12978 then
12979 if In_Instance_Body then
12980 Error_Msg_Warn := SPARK_Mode /= On;
12981 Conversion_Error_N
12982 ("source array type has deeper accessibility "
12983 & "level than target<<", Operand);
12984 Conversion_Error_N ("\Program_Error [<<", Operand);
12985 Rewrite (N,
12986 Make_Raise_Program_Error (Sloc (N),
12987 Reason => PE_Accessibility_Check_Failed));
12988 Set_Etype (N, Target_Type);
12989 return False;
12990
12991 -- Conversion not allowed because of accessibility levels
12992
12993 else
12994 Conversion_Error_N
12995 ("source array type has deeper accessibility "
12996 & "level than target", Operand);
12997 return False;
12998 end if;
12999
13000 else
13001 null;
13002 end if;
13003
13004 -- All other cases where component base types do not match
13005
13006 else
13007 Conversion_Error_N
13008 ("incompatible component types for array conversion",
13009 Operand);
13010 return False;
13011 end if;
13012
13013 -- Check that component subtypes statically match. For numeric
13014 -- types this means that both must be either constrained or
13015 -- unconstrained. For enumeration types the bounds must match.
13016 -- All of this is checked in Subtypes_Statically_Match.
13017
13018 if not Subtypes_Statically_Match
13019 (Target_Comp_Type, Opnd_Comp_Type)
13020 then
13021 Conversion_Error_N
13022 ("component subtypes must statically match", Operand);
13023 return False;
13024 end if;
13025 end if;
13026
13027 return True;
13028 end Valid_Array_Conversion;
13029
13030 -----------------------------
13031 -- Valid_Tagged_Conversion --
13032 -----------------------------
13033
13034 function Valid_Tagged_Conversion
13035 (Target_Type : Entity_Id;
13036 Opnd_Type : Entity_Id) return Boolean
13037 is
13038 begin
13039 -- Upward conversions are allowed (RM 4.6(22))
13040
13041 if Covers (Target_Type, Opnd_Type)
13042 or else Is_Ancestor (Target_Type, Opnd_Type)
13043 then
13044 return True;
13045
13046 -- Downward conversion are allowed if the operand is class-wide
13047 -- (RM 4.6(23)).
13048
13049 elsif Is_Class_Wide_Type (Opnd_Type)
13050 and then Covers (Opnd_Type, Target_Type)
13051 then
13052 return True;
13053
13054 elsif Covers (Opnd_Type, Target_Type)
13055 or else Is_Ancestor (Opnd_Type, Target_Type)
13056 then
13057 return
13058 Conversion_Check (False,
13059 "downward conversion of tagged objects not allowed");
13060
13061 -- Ada 2005 (AI-251): The conversion to/from interface types is
13062 -- always valid. The types involved may be class-wide (sub)types.
13063
13064 elsif Is_Interface (Etype (Base_Type (Target_Type)))
13065 or else Is_Interface (Etype (Base_Type (Opnd_Type)))
13066 then
13067 return True;
13068
13069 -- If the operand is a class-wide type obtained through a limited_
13070 -- with clause, and the context includes the nonlimited view, use
13071 -- it to determine whether the conversion is legal.
13072
13073 elsif Is_Class_Wide_Type (Opnd_Type)
13074 and then From_Limited_With (Opnd_Type)
13075 and then Present (Non_Limited_View (Etype (Opnd_Type)))
13076 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
13077 then
13078 return True;
13079
13080 elsif Is_Access_Type (Opnd_Type)
13081 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
13082 then
13083 return True;
13084
13085 else
13086 Conversion_Error_NE
13087 ("invalid tagged conversion, not compatible with}",
13088 N, First_Subtype (Opnd_Type));
13089 return False;
13090 end if;
13091 end Valid_Tagged_Conversion;
13092
13093 -- Start of processing for Valid_Conversion
13094
13095 begin
13096 Check_Parameterless_Call (Operand);
13097
13098 if Is_Overloaded (Operand) then
13099 declare
13100 I : Interp_Index;
13101 I1 : Interp_Index;
13102 It : Interp;
13103 It1 : Interp;
13104 N1 : Entity_Id;
13105 T1 : Entity_Id;
13106
13107 begin
13108 -- Remove procedure calls, which syntactically cannot appear in
13109 -- this context, but which cannot be removed by type checking,
13110 -- because the context does not impose a type.
13111
13112 -- The node may be labelled overloaded, but still contain only one
13113 -- interpretation because others were discarded earlier. If this
13114 -- is the case, retain the single interpretation if legal.
13115
13116 Get_First_Interp (Operand, I, It);
13117 Opnd_Type := It.Typ;
13118 Get_Next_Interp (I, It);
13119
13120 if Present (It.Typ)
13121 and then Opnd_Type /= Standard_Void_Type
13122 then
13123 -- More than one candidate interpretation is available
13124
13125 Get_First_Interp (Operand, I, It);
13126 while Present (It.Typ) loop
13127 if It.Typ = Standard_Void_Type then
13128 Remove_Interp (I);
13129 end if;
13130
13131 -- When compiling for a system where Address is of a visible
13132 -- integer type, spurious ambiguities can be produced when
13133 -- arithmetic operations have a literal operand and return
13134 -- System.Address or a descendant of it. These ambiguities
13135 -- are usually resolved by the context, but for conversions
13136 -- there is no context type and the removal of the spurious
13137 -- operations must be done explicitly here.
13138
13139 if not Address_Is_Private
13140 and then Is_Descendant_Of_Address (It.Typ)
13141 then
13142 Remove_Interp (I);
13143 end if;
13144
13145 Get_Next_Interp (I, It);
13146 end loop;
13147 end if;
13148
13149 Get_First_Interp (Operand, I, It);
13150 I1 := I;
13151 It1 := It;
13152
13153 if No (It.Typ) then
13154 Conversion_Error_N ("illegal operand in conversion", Operand);
13155 return False;
13156 end if;
13157
13158 Get_Next_Interp (I, It);
13159
13160 if Present (It.Typ) then
13161 N1 := It1.Nam;
13162 T1 := It1.Typ;
13163 It1 := Disambiguate (Operand, I1, I, Any_Type);
13164
13165 if It1 = No_Interp then
13166 Conversion_Error_N
13167 ("ambiguous operand in conversion", Operand);
13168
13169 -- If the interpretation involves a standard operator, use
13170 -- the location of the type, which may be user-defined.
13171
13172 if Sloc (It.Nam) = Standard_Location then
13173 Error_Msg_Sloc := Sloc (It.Typ);
13174 else
13175 Error_Msg_Sloc := Sloc (It.Nam);
13176 end if;
13177
13178 Conversion_Error_N -- CODEFIX
13179 ("\\possible interpretation#!", Operand);
13180
13181 if Sloc (N1) = Standard_Location then
13182 Error_Msg_Sloc := Sloc (T1);
13183 else
13184 Error_Msg_Sloc := Sloc (N1);
13185 end if;
13186
13187 Conversion_Error_N -- CODEFIX
13188 ("\\possible interpretation#!", Operand);
13189
13190 return False;
13191 end if;
13192 end if;
13193
13194 Set_Etype (Operand, It1.Typ);
13195 Opnd_Type := It1.Typ;
13196 end;
13197 end if;
13198
13199 -- Deal with conversion of integer type to address if the pragma
13200 -- Allow_Integer_Address is in effect. We convert the conversion to
13201 -- an unchecked conversion in this case and we are all done.
13202
13203 if Address_Integer_Convert_OK (Opnd_Type, Target_Type) then
13204 Rewrite (N, Unchecked_Convert_To (Target_Type, Expression (N)));
13205 Analyze_And_Resolve (N, Target_Type);
13206 return True;
13207 end if;
13208
13209 -- If we are within a child unit, check whether the type of the
13210 -- expression has an ancestor in a parent unit, in which case it
13211 -- belongs to its derivation class even if the ancestor is private.
13212 -- See RM 7.3.1 (5.2/3).
13213
13214 Inc_Ancestor := Get_Incomplete_View_Of_Ancestor (Opnd_Type);
13215
13216 -- Numeric types
13217
13218 if Is_Numeric_Type (Target_Type) then
13219
13220 -- A universal fixed expression can be converted to any numeric type
13221
13222 if Opnd_Type = Universal_Fixed then
13223 return True;
13224
13225 -- Also no need to check when in an instance or inlined body, because
13226 -- the legality has been established when the template was analyzed.
13227 -- Furthermore, numeric conversions may occur where only a private
13228 -- view of the operand type is visible at the instantiation point.
13229 -- This results in a spurious error if we check that the operand type
13230 -- is a numeric type.
13231
13232 -- Note: in a previous version of this unit, the following tests were
13233 -- applied only for generated code (Comes_From_Source set to False),
13234 -- but in fact the test is required for source code as well, since
13235 -- this situation can arise in source code.
13236
13237 elsif In_Instance_Code or else In_Inlined_Body then
13238 return True;
13239
13240 -- Otherwise we need the conversion check
13241
13242 else
13243 return Conversion_Check
13244 (Is_Numeric_Type (Opnd_Type)
13245 or else
13246 (Present (Inc_Ancestor)
13247 and then Is_Numeric_Type (Inc_Ancestor)),
13248 "illegal operand for numeric conversion");
13249 end if;
13250
13251 -- Array types
13252
13253 elsif Is_Array_Type (Target_Type) then
13254 if not Is_Array_Type (Opnd_Type)
13255 or else Opnd_Type = Any_Composite
13256 or else Opnd_Type = Any_String
13257 then
13258 Conversion_Error_N
13259 ("illegal operand for array conversion", Operand);
13260 return False;
13261
13262 else
13263 return Valid_Array_Conversion;
13264 end if;
13265
13266 -- Ada 2005 (AI-251): Internally generated conversions of access to
13267 -- interface types added to force the displacement of the pointer to
13268 -- reference the corresponding dispatch table.
13269
13270 elsif not Comes_From_Source (N)
13271 and then Is_Access_Type (Target_Type)
13272 and then Is_Interface (Designated_Type (Target_Type))
13273 then
13274 return True;
13275
13276 -- Ada 2005 (AI-251): Anonymous access types where target references an
13277 -- interface type.
13278
13279 elsif Is_Access_Type (Opnd_Type)
13280 and then Ekind_In (Target_Type, E_General_Access_Type,
13281 E_Anonymous_Access_Type)
13282 and then Is_Interface (Directly_Designated_Type (Target_Type))
13283 then
13284 -- Check the static accessibility rule of 4.6(17). Note that the
13285 -- check is not enforced when within an instance body, since the
13286 -- RM requires such cases to be caught at run time.
13287
13288 -- If the operand is a rewriting of an allocator no check is needed
13289 -- because there are no accessibility issues.
13290
13291 if Nkind (Original_Node (N)) = N_Allocator then
13292 null;
13293
13294 elsif Ekind (Target_Type) /= E_Anonymous_Access_Type then
13295 if Type_Access_Level (Opnd_Type) >
13296 Deepest_Type_Access_Level (Target_Type)
13297 then
13298 -- In an instance, this is a run-time check, but one we know
13299 -- will fail, so generate an appropriate warning. The raise
13300 -- will be generated by Expand_N_Type_Conversion.
13301
13302 if In_Instance_Body then
13303 Error_Msg_Warn := SPARK_Mode /= On;
13304 Conversion_Error_N
13305 ("cannot convert local pointer to non-local access type<<",
13306 Operand);
13307 Conversion_Error_N ("\Program_Error [<<", Operand);
13308
13309 else
13310 Conversion_Error_N
13311 ("cannot convert local pointer to non-local access type",
13312 Operand);
13313 return False;
13314 end if;
13315
13316 -- Special accessibility checks are needed in the case of access
13317 -- discriminants declared for a limited type.
13318
13319 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
13320 and then not Is_Local_Anonymous_Access (Opnd_Type)
13321 then
13322 -- When the operand is a selected access discriminant the check
13323 -- needs to be made against the level of the object denoted by
13324 -- the prefix of the selected name (Object_Access_Level handles
13325 -- checking the prefix of the operand for this case).
13326
13327 if Nkind (Operand) = N_Selected_Component
13328 and then Object_Access_Level (Operand) >
13329 Deepest_Type_Access_Level (Target_Type)
13330 then
13331 -- In an instance, this is a run-time check, but one we know
13332 -- will fail, so generate an appropriate warning. The raise
13333 -- will be generated by Expand_N_Type_Conversion.
13334
13335 if In_Instance_Body then
13336 Error_Msg_Warn := SPARK_Mode /= On;
13337 Conversion_Error_N
13338 ("cannot convert access discriminant to non-local "
13339 & "access type<<", Operand);
13340 Conversion_Error_N ("\Program_Error [<<", Operand);
13341
13342 -- Real error if not in instance body
13343
13344 else
13345 Conversion_Error_N
13346 ("cannot convert access discriminant to non-local "
13347 & "access type", Operand);
13348 return False;
13349 end if;
13350 end if;
13351
13352 -- The case of a reference to an access discriminant from
13353 -- within a limited type declaration (which will appear as
13354 -- a discriminal) is always illegal because the level of the
13355 -- discriminant is considered to be deeper than any (nameable)
13356 -- access type.
13357
13358 if Is_Entity_Name (Operand)
13359 and then not Is_Local_Anonymous_Access (Opnd_Type)
13360 and then
13361 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
13362 and then Present (Discriminal_Link (Entity (Operand)))
13363 then
13364 Conversion_Error_N
13365 ("discriminant has deeper accessibility level than target",
13366 Operand);
13367 return False;
13368 end if;
13369 end if;
13370 end if;
13371
13372 return True;
13373
13374 -- General and anonymous access types
13375
13376 elsif Ekind_In (Target_Type, E_General_Access_Type,
13377 E_Anonymous_Access_Type)
13378 and then
13379 Conversion_Check
13380 (Is_Access_Type (Opnd_Type)
13381 and then not
13382 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
13383 E_Access_Protected_Subprogram_Type),
13384 "must be an access-to-object type")
13385 then
13386 if Is_Access_Constant (Opnd_Type)
13387 and then not Is_Access_Constant (Target_Type)
13388 then
13389 Conversion_Error_N
13390 ("access-to-constant operand type not allowed", Operand);
13391 return False;
13392 end if;
13393
13394 -- Check the static accessibility rule of 4.6(17). Note that the
13395 -- check is not enforced when within an instance body, since the RM
13396 -- requires such cases to be caught at run time.
13397
13398 if Ekind (Target_Type) /= E_Anonymous_Access_Type
13399 or else Is_Local_Anonymous_Access (Target_Type)
13400 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
13401 N_Object_Declaration
13402 then
13403 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
13404 -- conversions from an anonymous access type to a named general
13405 -- access type. Such conversions are not allowed in the case of
13406 -- access parameters and stand-alone objects of an anonymous
13407 -- access type. The implicit conversion case is recognized by
13408 -- testing that Comes_From_Source is False and that it's been
13409 -- rewritten. The Comes_From_Source test isn't sufficient because
13410 -- nodes in inlined calls to predefined library routines can have
13411 -- Comes_From_Source set to False. (Is there a better way to test
13412 -- for implicit conversions???)
13413
13414 if Ada_Version >= Ada_2012
13415 and then not Comes_From_Source (N)
13416 and then Is_Rewrite_Substitution (N)
13417 and then Ekind (Base_Type (Target_Type)) = E_General_Access_Type
13418 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
13419 then
13420 if Is_Itype (Opnd_Type) then
13421
13422 -- Implicit conversions aren't allowed for objects of an
13423 -- anonymous access type, since such objects have nonstatic
13424 -- levels in Ada 2012.
13425
13426 if Nkind (Associated_Node_For_Itype (Opnd_Type)) =
13427 N_Object_Declaration
13428 then
13429 Conversion_Error_N
13430 ("implicit conversion of stand-alone anonymous "
13431 & "access object not allowed", Operand);
13432 return False;
13433
13434 -- Implicit conversions aren't allowed for anonymous access
13435 -- parameters. The "not Is_Local_Anonymous_Access_Type" test
13436 -- is done to exclude anonymous access results.
13437
13438 elsif not Is_Local_Anonymous_Access (Opnd_Type)
13439 and then Nkind_In (Associated_Node_For_Itype (Opnd_Type),
13440 N_Function_Specification,
13441 N_Procedure_Specification)
13442 then
13443 Conversion_Error_N
13444 ("implicit conversion of anonymous access formal "
13445 & "not allowed", Operand);
13446 return False;
13447
13448 -- This is a case where there's an enclosing object whose
13449 -- to which the "statically deeper than" relationship does
13450 -- not apply (such as an access discriminant selected from
13451 -- a dereference of an access parameter).
13452
13453 elsif Object_Access_Level (Operand)
13454 = Scope_Depth (Standard_Standard)
13455 then
13456 Conversion_Error_N
13457 ("implicit conversion of anonymous access value "
13458 & "not allowed", Operand);
13459 return False;
13460
13461 -- In other cases, the level of the operand's type must be
13462 -- statically less deep than that of the target type, else
13463 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
13464
13465 elsif Type_Access_Level (Opnd_Type) >
13466 Deepest_Type_Access_Level (Target_Type)
13467 then
13468 Conversion_Error_N
13469 ("implicit conversion of anonymous access value "
13470 & "violates accessibility", Operand);
13471 return False;
13472 end if;
13473 end if;
13474
13475 elsif Type_Access_Level (Opnd_Type) >
13476 Deepest_Type_Access_Level (Target_Type)
13477 then
13478 -- In an instance, this is a run-time check, but one we know
13479 -- will fail, so generate an appropriate warning. The raise
13480 -- will be generated by Expand_N_Type_Conversion.
13481
13482 if In_Instance_Body then
13483 Error_Msg_Warn := SPARK_Mode /= On;
13484 Conversion_Error_N
13485 ("cannot convert local pointer to non-local access type<<",
13486 Operand);
13487 Conversion_Error_N ("\Program_Error [<<", Operand);
13488
13489 -- If not in an instance body, this is a real error
13490
13491 else
13492 -- Avoid generation of spurious error message
13493
13494 if not Error_Posted (N) then
13495 Conversion_Error_N
13496 ("cannot convert local pointer to non-local access type",
13497 Operand);
13498 end if;
13499
13500 return False;
13501 end if;
13502
13503 -- Special accessibility checks are needed in the case of access
13504 -- discriminants declared for a limited type.
13505
13506 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
13507 and then not Is_Local_Anonymous_Access (Opnd_Type)
13508 then
13509 -- When the operand is a selected access discriminant the check
13510 -- needs to be made against the level of the object denoted by
13511 -- the prefix of the selected name (Object_Access_Level handles
13512 -- checking the prefix of the operand for this case).
13513
13514 if Nkind (Operand) = N_Selected_Component
13515 and then Object_Access_Level (Operand) >
13516 Deepest_Type_Access_Level (Target_Type)
13517 then
13518 -- In an instance, this is a run-time check, but one we know
13519 -- will fail, so generate an appropriate warning. The raise
13520 -- will be generated by Expand_N_Type_Conversion.
13521
13522 if In_Instance_Body then
13523 Error_Msg_Warn := SPARK_Mode /= On;
13524 Conversion_Error_N
13525 ("cannot convert access discriminant to non-local "
13526 & "access type<<", Operand);
13527 Conversion_Error_N ("\Program_Error [<<", Operand);
13528
13529 -- If not in an instance body, this is a real error
13530
13531 else
13532 Conversion_Error_N
13533 ("cannot convert access discriminant to non-local "
13534 & "access type", Operand);
13535 return False;
13536 end if;
13537 end if;
13538
13539 -- The case of a reference to an access discriminant from
13540 -- within a limited type declaration (which will appear as
13541 -- a discriminal) is always illegal because the level of the
13542 -- discriminant is considered to be deeper than any (nameable)
13543 -- access type.
13544
13545 if Is_Entity_Name (Operand)
13546 and then
13547 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
13548 and then Present (Discriminal_Link (Entity (Operand)))
13549 then
13550 Conversion_Error_N
13551 ("discriminant has deeper accessibility level than target",
13552 Operand);
13553 return False;
13554 end if;
13555 end if;
13556 end if;
13557
13558 -- In the presence of limited_with clauses we have to use nonlimited
13559 -- views, if available.
13560
13561 Check_Limited : declare
13562 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
13563 -- Helper function to handle limited views
13564
13565 --------------------------
13566 -- Full_Designated_Type --
13567 --------------------------
13568
13569 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
13570 Desig : constant Entity_Id := Designated_Type (T);
13571
13572 begin
13573 -- Handle the limited view of a type
13574
13575 if From_Limited_With (Desig)
13576 and then Has_Non_Limited_View (Desig)
13577 then
13578 return Available_View (Desig);
13579 else
13580 return Desig;
13581 end if;
13582 end Full_Designated_Type;
13583
13584 -- Local Declarations
13585
13586 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
13587 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
13588
13589 Same_Base : constant Boolean :=
13590 Base_Type (Target) = Base_Type (Opnd);
13591
13592 -- Start of processing for Check_Limited
13593
13594 begin
13595 if Is_Tagged_Type (Target) then
13596 return Valid_Tagged_Conversion (Target, Opnd);
13597
13598 else
13599 if not Same_Base then
13600 Conversion_Error_NE
13601 ("target designated type not compatible with }",
13602 N, Base_Type (Opnd));
13603 return False;
13604
13605 -- Ada 2005 AI-384: legality rule is symmetric in both
13606 -- designated types. The conversion is legal (with possible
13607 -- constraint check) if either designated type is
13608 -- unconstrained.
13609
13610 elsif Subtypes_Statically_Match (Target, Opnd)
13611 or else
13612 (Has_Discriminants (Target)
13613 and then
13614 (not Is_Constrained (Opnd)
13615 or else not Is_Constrained (Target)))
13616 then
13617 -- Special case, if Value_Size has been used to make the
13618 -- sizes different, the conversion is not allowed even
13619 -- though the subtypes statically match.
13620
13621 if Known_Static_RM_Size (Target)
13622 and then Known_Static_RM_Size (Opnd)
13623 and then RM_Size (Target) /= RM_Size (Opnd)
13624 then
13625 Conversion_Error_NE
13626 ("target designated subtype not compatible with }",
13627 N, Opnd);
13628 Conversion_Error_NE
13629 ("\because sizes of the two designated subtypes differ",
13630 N, Opnd);
13631 return False;
13632
13633 -- Normal case where conversion is allowed
13634
13635 else
13636 return True;
13637 end if;
13638
13639 else
13640 Error_Msg_NE
13641 ("target designated subtype not compatible with }",
13642 N, Opnd);
13643 return False;
13644 end if;
13645 end if;
13646 end Check_Limited;
13647
13648 -- Access to subprogram types. If the operand is an access parameter,
13649 -- the type has a deeper accessibility that any master, and cannot be
13650 -- assigned. We must make an exception if the conversion is part of an
13651 -- assignment and the target is the return object of an extended return
13652 -- statement, because in that case the accessibility check takes place
13653 -- after the return.
13654
13655 elsif Is_Access_Subprogram_Type (Target_Type)
13656
13657 -- Note: this test of Opnd_Type is there to prevent entering this
13658 -- branch in the case of a remote access to subprogram type, which
13659 -- is internally represented as an E_Record_Type.
13660
13661 and then Is_Access_Type (Opnd_Type)
13662 then
13663 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
13664 and then Is_Entity_Name (Operand)
13665 and then Ekind (Entity (Operand)) = E_In_Parameter
13666 and then
13667 (Nkind (Parent (N)) /= N_Assignment_Statement
13668 or else not Is_Entity_Name (Name (Parent (N)))
13669 or else not Is_Return_Object (Entity (Name (Parent (N)))))
13670 then
13671 Conversion_Error_N
13672 ("illegal attempt to store anonymous access to subprogram",
13673 Operand);
13674 Conversion_Error_N
13675 ("\value has deeper accessibility than any master "
13676 & "(RM 3.10.2 (13))",
13677 Operand);
13678
13679 Error_Msg_NE
13680 ("\use named access type for& instead of access parameter",
13681 Operand, Entity (Operand));
13682 end if;
13683
13684 -- Check that the designated types are subtype conformant
13685
13686 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
13687 Old_Id => Designated_Type (Opnd_Type),
13688 Err_Loc => N);
13689
13690 -- Check the static accessibility rule of 4.6(20)
13691
13692 if Type_Access_Level (Opnd_Type) >
13693 Deepest_Type_Access_Level (Target_Type)
13694 then
13695 Conversion_Error_N
13696 ("operand type has deeper accessibility level than target",
13697 Operand);
13698
13699 -- Check that if the operand type is declared in a generic body,
13700 -- then the target type must be declared within that same body
13701 -- (enforces last sentence of 4.6(20)).
13702
13703 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
13704 declare
13705 O_Gen : constant Node_Id :=
13706 Enclosing_Generic_Body (Opnd_Type);
13707
13708 T_Gen : Node_Id;
13709
13710 begin
13711 T_Gen := Enclosing_Generic_Body (Target_Type);
13712 while Present (T_Gen) and then T_Gen /= O_Gen loop
13713 T_Gen := Enclosing_Generic_Body (T_Gen);
13714 end loop;
13715
13716 if T_Gen /= O_Gen then
13717 Conversion_Error_N
13718 ("target type must be declared in same generic body "
13719 & "as operand type", N);
13720 end if;
13721 end;
13722 end if;
13723
13724 return True;
13725
13726 -- Remote access to subprogram types
13727
13728 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
13729 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
13730 then
13731 -- It is valid to convert from one RAS type to another provided
13732 -- that their specification statically match.
13733
13734 -- Note: at this point, remote access to subprogram types have been
13735 -- expanded to their E_Record_Type representation, and we need to
13736 -- go back to the original access type definition using the
13737 -- Corresponding_Remote_Type attribute in order to check that the
13738 -- designated profiles match.
13739
13740 pragma Assert (Ekind (Target_Type) = E_Record_Type);
13741 pragma Assert (Ekind (Opnd_Type) = E_Record_Type);
13742
13743 Check_Subtype_Conformant
13744 (New_Id =>
13745 Designated_Type (Corresponding_Remote_Type (Target_Type)),
13746 Old_Id =>
13747 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
13748 Err_Loc =>
13749 N);
13750 return True;
13751
13752 -- If it was legal in the generic, it's legal in the instance
13753
13754 elsif In_Instance_Body then
13755 return True;
13756
13757 -- If both are tagged types, check legality of view conversions
13758
13759 elsif Is_Tagged_Type (Target_Type)
13760 and then
13761 Is_Tagged_Type (Opnd_Type)
13762 then
13763 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
13764
13765 -- Types derived from the same root type are convertible
13766
13767 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
13768 return True;
13769
13770 -- In an instance or an inlined body, there may be inconsistent views of
13771 -- the same type, or of types derived from a common root.
13772
13773 elsif (In_Instance or In_Inlined_Body)
13774 and then
13775 Root_Type (Underlying_Type (Target_Type)) =
13776 Root_Type (Underlying_Type (Opnd_Type))
13777 then
13778 return True;
13779
13780 -- Special check for common access type error case
13781
13782 elsif Ekind (Target_Type) = E_Access_Type
13783 and then Is_Access_Type (Opnd_Type)
13784 then
13785 Conversion_Error_N ("target type must be general access type!", N);
13786 Conversion_Error_NE -- CODEFIX
13787 ("add ALL to }!", N, Target_Type);
13788 return False;
13789
13790 -- Here we have a real conversion error
13791
13792 else
13793 -- Check for missing regular with_clause when only a limited view of
13794 -- target is available.
13795
13796 if From_Limited_With (Opnd_Type) and then In_Package_Body then
13797 Conversion_Error_NE
13798 ("invalid conversion, not compatible with limited view of }",
13799 N, Opnd_Type);
13800 Conversion_Error_NE
13801 ("\add with_clause for& to current unit!", N, Scope (Opnd_Type));
13802
13803 elsif Is_Access_Type (Opnd_Type)
13804 and then From_Limited_With (Designated_Type (Opnd_Type))
13805 and then In_Package_Body
13806 then
13807 Conversion_Error_NE
13808 ("invalid conversion, not compatible with }", N, Opnd_Type);
13809 Conversion_Error_NE
13810 ("\add with_clause for& to current unit!",
13811 N, Scope (Designated_Type (Opnd_Type)));
13812
13813 else
13814 Conversion_Error_NE
13815 ("invalid conversion, not compatible with }", N, Opnd_Type);
13816 end if;
13817
13818 return False;
13819 end if;
13820 end Valid_Conversion;
13821
13822 end Sem_Res;