]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/sem_res.adb
[Ada] Variable-sized node types
[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-2021, 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 Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Debug_A; use Debug_A;
31 with Einfo; use Einfo;
32 with Einfo.Entities; use Einfo.Entities;
33 with Einfo.Utils; use Einfo.Utils;
34 with Errout; use Errout;
35 with Expander; use Expander;
36 with Exp_Ch6; use Exp_Ch6;
37 with Exp_Ch7; use Exp_Ch7;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Inline; use Inline;
44 with Itypes; use Itypes;
45 with Lib; use Lib;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
49 with Nlists; use Nlists;
50 with Opt; use Opt;
51 with Output; use Output;
52 with Par_SCO; use Par_SCO;
53 with Restrict; use Restrict;
54 with Rident; use Rident;
55 with Rtsfind; use Rtsfind;
56 with Sem; use Sem;
57 with Sem_Aggr; use Sem_Aggr;
58 with Sem_Attr; use Sem_Attr;
59 with Sem_Aux; use Sem_Aux;
60 with Sem_Cat; use Sem_Cat;
61 with Sem_Ch3; use Sem_Ch3;
62 with Sem_Ch4; use Sem_Ch4;
63 with Sem_Ch6; use Sem_Ch6;
64 with Sem_Ch8; use Sem_Ch8;
65 with Sem_Ch13; use Sem_Ch13;
66 with Sem_Dim; use Sem_Dim;
67 with Sem_Disp; use Sem_Disp;
68 with Sem_Dist; use Sem_Dist;
69 with Sem_Elab; use Sem_Elab;
70 with Sem_Elim; use Sem_Elim;
71 with Sem_Eval; use Sem_Eval;
72 with Sem_Intr; use Sem_Intr;
73 with Sem_Mech; use Sem_Mech;
74 with Sem_Type; use Sem_Type;
75 with Sem_Util; use Sem_Util;
76 with Sem_Warn; use Sem_Warn;
77 with Sinfo; use Sinfo;
78 with Sinfo.Nodes; use Sinfo.Nodes;
79 with Sinfo.Utils; use Sinfo.Utils;
80 with Sinfo.CN; use Sinfo.CN;
81 with Snames; use Snames;
82 with Stand; use Stand;
83 with Stringt; use Stringt;
84 with Style; use Style;
85 with Targparm; use Targparm;
86 with Tbuild; use Tbuild;
87 with Uintp; use Uintp;
88 with Urealp; use Urealp;
89
90 package body Sem_Res is
91
92 -----------------------
93 -- Local Subprograms --
94 -----------------------
95
96 -- Second pass (top-down) type checking and overload resolution procedures
97 -- Typ is the type required by context. These procedures propagate the
98 -- type information recursively to the descendants of N. If the node is not
99 -- overloaded, its Etype is established in the first pass. If overloaded,
100 -- the Resolve routines set the correct type. For arithmetic operators, the
101 -- Etype is the base type of the context.
102
103 -- Note that Resolve_Attribute is separated off in Sem_Attr
104
105 procedure Check_Discriminant_Use (N : Node_Id);
106 -- Enforce the restrictions on the use of discriminants when constraining
107 -- a component of a discriminated type (record or concurrent type).
108
109 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
110 -- Given a node for an operator associated with type T, check that the
111 -- operator is visible. Operators all of whose operands are universal must
112 -- be checked for visibility during resolution because their type is not
113 -- determinable based on their operands.
114
115 procedure Check_Fully_Declared_Prefix
116 (Typ : Entity_Id;
117 Pref : Node_Id);
118 -- Check that the type of the prefix of a dereference is not incomplete
119
120 function Check_Infinite_Recursion (Call : Node_Id) return Boolean;
121 -- Given a call node, Call, which is known to occur immediately within the
122 -- subprogram being called, determines whether it is a detectable case of
123 -- an infinite recursion, and if so, outputs appropriate messages. Returns
124 -- True if an infinite recursion is detected, and False otherwise.
125
126 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
127 -- N is the node for a logical operator. If the operator is predefined, and
128 -- the root type of the operands is Standard.Boolean, then a check is made
129 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
130 -- the style check for Style_Check_Boolean_And_Or.
131
132 function Is_Atomic_Ref_With_Address (N : Node_Id) return Boolean;
133 -- N is either an indexed component or a selected component. This function
134 -- returns true if the prefix refers to an object that has an address
135 -- clause (the case in which we may want to issue a warning).
136
137 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
138 -- Determine whether E is an access type declared by an access declaration,
139 -- and not an (anonymous) allocator type.
140
141 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
142 -- Utility to check whether the entity for an operator is a predefined
143 -- operator, in which case the expression is left as an operator in the
144 -- tree (else it is rewritten into a call). An instance of an intrinsic
145 -- conversion operation may be given an operator name, but is not treated
146 -- like an operator. Note that an operator that is an imported back-end
147 -- builtin has convention Intrinsic, but is expected to be rewritten into
148 -- a call, so such an operator is not treated as predefined by this
149 -- predicate.
150
151 procedure Preanalyze_And_Resolve
152 (N : Node_Id;
153 T : Entity_Id;
154 With_Freezing : Boolean);
155 -- Subsidiary of public versions of Preanalyze_And_Resolve.
156
157 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
158 -- If a default expression in entry call N depends on the discriminants
159 -- of the task, it must be replaced with a reference to the discriminant
160 -- of the task being called.
161
162 procedure Resolve_Op_Concat_Arg
163 (N : Node_Id;
164 Arg : Node_Id;
165 Typ : Entity_Id;
166 Is_Comp : Boolean);
167 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
168 -- concatenation operator. The operand is either of the array type or of
169 -- the component type. If the operand is an aggregate, and the component
170 -- type is composite, this is ambiguous if component type has aggregates.
171
172 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
173 -- Does the first part of the work of Resolve_Op_Concat
174
175 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
176 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
177 -- has been resolved. See Resolve_Op_Concat for details.
178
179 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
180 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
181 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
182 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
183 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
184 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
185 procedure Resolve_Declare_Expression (N : Node_Id; Typ : Entity_Id);
186 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
187 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
188 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
189 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
190 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id);
191 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id);
192 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
193 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
194 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
195 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
196 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
197 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
198 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
199 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
200 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
201 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
202 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id);
203 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
204 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
205 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
206 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
207 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
208 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
209 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
210 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
211 procedure Resolve_Target_Name (N : Node_Id; Typ : Entity_Id);
212 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
213 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
214 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
215 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
216
217 function Operator_Kind
218 (Op_Name : Name_Id;
219 Is_Binary : Boolean) return Node_Kind;
220 -- Utility to map the name of an operator into the corresponding Node. Used
221 -- by other node rewriting procedures.
222
223 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
224 -- Resolve actuals of call, and add default expressions for missing ones.
225 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
226 -- called subprogram.
227
228 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
229 -- Called from Resolve_Call, when the prefix denotes an entry or element
230 -- of entry family. Actuals are resolved as for subprograms, and the node
231 -- is rebuilt as an entry call. Also called for protected operations. Typ
232 -- is the context type, which is used when the operation is a protected
233 -- function with no arguments, and the return value is indexed.
234
235 procedure Resolve_Implicit_Dereference (P : Node_Id);
236 -- Called when P is the prefix of an indexed component, or of a selected
237 -- component, or of a slice. If P is of an access type, we unconditionally
238 -- rewrite it as an explicit dereference. This ensures that the expander
239 -- and the code generator have a fully explicit tree to work with.
240
241 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
242 -- A call to a user-defined intrinsic operator is rewritten as a call to
243 -- the corresponding predefined operator, with suitable conversions. Note
244 -- that this applies only for intrinsic operators that denote predefined
245 -- operators, not ones that are intrinsic imports of back-end builtins.
246
247 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
248 -- Ditto, for arithmetic unary operators
249
250 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
251 -- If an operator node resolves to a call to a user-defined operator,
252 -- rewrite the node as a function call.
253
254 procedure Make_Call_Into_Operator
255 (N : Node_Id;
256 Typ : Entity_Id;
257 Op_Id : Entity_Id);
258 -- Inverse transformation: if an operator is given in functional notation,
259 -- then after resolving the node, transform into an operator node, so that
260 -- operands are resolved properly. Recall that predefined operators do not
261 -- have a full signature and special resolution rules apply.
262
263 procedure Rewrite_Renamed_Operator
264 (N : Node_Id;
265 Op : Entity_Id;
266 Typ : Entity_Id);
267 -- An operator can rename another, e.g. in an instantiation. In that
268 -- case, the proper operator node must be constructed and resolved.
269
270 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
271 -- The String_Literal_Subtype is built for all strings that are not
272 -- operands of a static concatenation operation. If the argument is not
273 -- a N_String_Literal node, then the call has no effect.
274
275 procedure Set_Slice_Subtype (N : Node_Id);
276 -- Build subtype of array type, with the range specified by the slice
277
278 procedure Simplify_Type_Conversion (N : Node_Id);
279 -- Called after N has been resolved and evaluated, but before range checks
280 -- have been applied. This rewrites the conversion into a simpler form.
281
282 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
283 -- A universal_fixed expression in an universal context is unambiguous if
284 -- there is only one applicable fixed point type. Determining whether there
285 -- is only one requires a search over all visible entities, and happens
286 -- only in very pathological cases (see 6115-006).
287
288 -------------------------
289 -- Ambiguous_Character --
290 -------------------------
291
292 procedure Ambiguous_Character (C : Node_Id) is
293 E : Entity_Id;
294
295 begin
296 if Nkind (C) = N_Character_Literal then
297 Error_Msg_N ("ambiguous character literal", C);
298
299 -- First the ones in Standard
300
301 Error_Msg_N ("\\possible interpretation: Character!", C);
302 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
303
304 -- Include Wide_Wide_Character in Ada 2005 mode
305
306 if Ada_Version >= Ada_2005 then
307 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
308 end if;
309
310 -- Now any other types that match
311
312 E := Current_Entity (C);
313 while Present (E) loop
314 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
315 E := Homonym (E);
316 end loop;
317 end if;
318 end Ambiguous_Character;
319
320 -------------------------
321 -- Analyze_And_Resolve --
322 -------------------------
323
324 procedure Analyze_And_Resolve (N : Node_Id) is
325 begin
326 Analyze (N);
327 Resolve (N);
328 end Analyze_And_Resolve;
329
330 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
331 begin
332 Analyze (N);
333 Resolve (N, Typ);
334 end Analyze_And_Resolve;
335
336 -- Versions with check(s) suppressed
337
338 procedure Analyze_And_Resolve
339 (N : Node_Id;
340 Typ : Entity_Id;
341 Suppress : Check_Id)
342 is
343 Scop : constant Entity_Id := Current_Scope;
344
345 begin
346 if Suppress = All_Checks then
347 declare
348 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
349 begin
350 Scope_Suppress.Suppress := (others => True);
351 Analyze_And_Resolve (N, Typ);
352 Scope_Suppress.Suppress := Sva;
353 end;
354
355 else
356 declare
357 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
358 begin
359 Scope_Suppress.Suppress (Suppress) := True;
360 Analyze_And_Resolve (N, Typ);
361 Scope_Suppress.Suppress (Suppress) := Svg;
362 end;
363 end if;
364
365 if Current_Scope /= Scop
366 and then Scope_Is_Transient
367 then
368 -- This can only happen if a transient scope was created for an inner
369 -- expression, which will be removed upon completion of the analysis
370 -- of an enclosing construct. The transient scope must have the
371 -- suppress status of the enclosing environment, not of this Analyze
372 -- call.
373
374 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
375 Scope_Suppress;
376 end if;
377 end Analyze_And_Resolve;
378
379 procedure Analyze_And_Resolve
380 (N : Node_Id;
381 Suppress : Check_Id)
382 is
383 Scop : constant Entity_Id := Current_Scope;
384
385 begin
386 if Suppress = All_Checks then
387 declare
388 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
389 begin
390 Scope_Suppress.Suppress := (others => True);
391 Analyze_And_Resolve (N);
392 Scope_Suppress.Suppress := Sva;
393 end;
394
395 else
396 declare
397 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
398 begin
399 Scope_Suppress.Suppress (Suppress) := True;
400 Analyze_And_Resolve (N);
401 Scope_Suppress.Suppress (Suppress) := Svg;
402 end;
403 end if;
404
405 if Current_Scope /= Scop and then Scope_Is_Transient then
406 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
407 Scope_Suppress;
408 end if;
409 end Analyze_And_Resolve;
410
411 ----------------------------
412 -- Check_Discriminant_Use --
413 ----------------------------
414
415 procedure Check_Discriminant_Use (N : Node_Id) is
416 PN : constant Node_Id := Parent (N);
417 Disc : constant Entity_Id := Entity (N);
418 P : Node_Id;
419 D : Node_Id;
420
421 begin
422 -- Any use in a spec-expression is legal
423
424 if In_Spec_Expression then
425 null;
426
427 elsif Nkind (PN) = N_Range then
428
429 -- Discriminant cannot be used to constrain a scalar type
430
431 P := Parent (PN);
432
433 if Nkind (P) = N_Range_Constraint
434 and then Nkind (Parent (P)) = N_Subtype_Indication
435 and then Nkind (Parent (Parent (P))) = N_Component_Definition
436 then
437 Error_Msg_N ("discriminant cannot constrain scalar type", N);
438
439 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
440
441 -- The following check catches the unusual case where a
442 -- discriminant appears within an index constraint that is part
443 -- of a larger expression within a constraint on a component,
444 -- e.g. "C : Int range 1 .. F (new A(1 .. D))". For now we only
445 -- check case of record components, and note that a similar check
446 -- should also apply in the case of discriminant constraints
447 -- below. ???
448
449 -- Note that the check for N_Subtype_Declaration below is to
450 -- detect the valid use of discriminants in the constraints of a
451 -- subtype declaration when this subtype declaration appears
452 -- inside the scope of a record type (which is syntactically
453 -- illegal, but which may be created as part of derived type
454 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
455 -- for more info.
456
457 if Ekind (Current_Scope) = E_Record_Type
458 and then Scope (Disc) = Current_Scope
459 and then not
460 (Nkind (Parent (P)) = N_Subtype_Indication
461 and then
462 Nkind (Parent (Parent (P))) in N_Component_Definition
463 | N_Subtype_Declaration
464 and then Paren_Count (N) = 0)
465 then
466 Error_Msg_N
467 ("discriminant must appear alone in component constraint", N);
468 return;
469 end if;
470
471 -- Detect a common error:
472
473 -- type R (D : Positive := 100) is record
474 -- Name : String (1 .. D);
475 -- end record;
476
477 -- The default value causes an object of type R to be allocated
478 -- with room for Positive'Last characters. The RM does not mandate
479 -- the allocation of the maximum size, but that is what GNAT does
480 -- so we should warn the programmer that there is a problem.
481
482 Check_Large : declare
483 SI : Node_Id;
484 T : Entity_Id;
485 TB : Node_Id;
486 CB : Entity_Id;
487
488 function Large_Storage_Type (T : Entity_Id) return Boolean;
489 -- Return True if type T has a large enough range that any
490 -- array whose index type covered the whole range of the type
491 -- would likely raise Storage_Error.
492
493 ------------------------
494 -- Large_Storage_Type --
495 ------------------------
496
497 function Large_Storage_Type (T : Entity_Id) return Boolean is
498 begin
499 -- The type is considered large if its bounds are known at
500 -- compile time and if it requires at least as many bits as
501 -- a Positive to store the possible values.
502
503 return Compile_Time_Known_Value (Type_Low_Bound (T))
504 and then Compile_Time_Known_Value (Type_High_Bound (T))
505 and then
506 Minimum_Size (T, Biased => True) >=
507 RM_Size (Standard_Positive);
508 end Large_Storage_Type;
509
510 -- Start of processing for Check_Large
511
512 begin
513 -- Check that the Disc has a large range
514
515 if not Large_Storage_Type (Etype (Disc)) then
516 goto No_Danger;
517 end if;
518
519 -- If the enclosing type is limited, we allocate only the
520 -- default value, not the maximum, and there is no need for
521 -- a warning.
522
523 if Is_Limited_Type (Scope (Disc)) then
524 goto No_Danger;
525 end if;
526
527 -- Check that it is the high bound
528
529 if N /= High_Bound (PN)
530 or else No (Discriminant_Default_Value (Disc))
531 then
532 goto No_Danger;
533 end if;
534
535 -- Check the array allows a large range at this bound. First
536 -- find the array
537
538 SI := Parent (P);
539
540 if Nkind (SI) /= N_Subtype_Indication then
541 goto No_Danger;
542 end if;
543
544 T := Entity (Subtype_Mark (SI));
545
546 if not Is_Array_Type (T) then
547 goto No_Danger;
548 end if;
549
550 -- Next, find the dimension
551
552 TB := First_Index (T);
553 CB := First (Constraints (P));
554 while True
555 and then Present (TB)
556 and then Present (CB)
557 and then CB /= PN
558 loop
559 Next_Index (TB);
560 Next (CB);
561 end loop;
562
563 if CB /= PN then
564 goto No_Danger;
565 end if;
566
567 -- Now, check the dimension has a large range
568
569 if not Large_Storage_Type (Etype (TB)) then
570 goto No_Danger;
571 end if;
572
573 -- Warn about the danger
574
575 Error_Msg_N
576 ("??creation of & object may raise Storage_Error!",
577 Scope (Disc));
578
579 <<No_Danger>>
580 null;
581
582 end Check_Large;
583 end if;
584
585 -- Legal case is in index or discriminant constraint
586
587 elsif Nkind (PN) in N_Index_Or_Discriminant_Constraint
588 | N_Discriminant_Association
589 then
590 if Paren_Count (N) > 0 then
591 Error_Msg_N
592 ("discriminant in constraint must appear alone", N);
593
594 elsif Nkind (N) = N_Expanded_Name
595 and then Comes_From_Source (N)
596 then
597 Error_Msg_N
598 ("discriminant must appear alone as a direct name", N);
599 end if;
600
601 return;
602
603 -- Otherwise, context is an expression. It should not be within (i.e. a
604 -- subexpression of) a constraint for a component.
605
606 else
607 D := PN;
608 P := Parent (PN);
609 while Nkind (P) not in
610 N_Component_Declaration | N_Subtype_Indication | N_Entry_Declaration
611 loop
612 D := P;
613 P := Parent (P);
614 exit when No (P);
615 end loop;
616
617 -- If the discriminant is used in an expression that is a bound of a
618 -- scalar type, an Itype is created and the bounds are attached to
619 -- its range, not to the original subtype indication. Such use is of
620 -- course a double fault.
621
622 if (Nkind (P) = N_Subtype_Indication
623 and then Nkind (Parent (P)) in N_Component_Definition
624 | N_Derived_Type_Definition
625 and then D = Constraint (P))
626
627 -- The constraint itself may be given by a subtype indication,
628 -- rather than by a more common discrete range.
629
630 or else (Nkind (P) = N_Subtype_Indication
631 and then
632 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
633 or else Nkind (P) = N_Entry_Declaration
634 or else Nkind (D) = N_Defining_Identifier
635 then
636 Error_Msg_N
637 ("discriminant in constraint must appear alone", N);
638 end if;
639 end if;
640 end Check_Discriminant_Use;
641
642 --------------------------------
643 -- Check_For_Visible_Operator --
644 --------------------------------
645
646 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
647 begin
648 if Is_Invisible_Operator (N, T) then
649 Error_Msg_NE -- CODEFIX
650 ("operator for} is not directly visible!", N, First_Subtype (T));
651 Error_Msg_N -- CODEFIX
652 ("use clause would make operation legal!", N);
653 end if;
654 end Check_For_Visible_Operator;
655
656 ----------------------------------
657 -- Check_Fully_Declared_Prefix --
658 ----------------------------------
659
660 procedure Check_Fully_Declared_Prefix
661 (Typ : Entity_Id;
662 Pref : Node_Id)
663 is
664 begin
665 -- Check that the designated type of the prefix of a dereference is
666 -- not an incomplete type. This cannot be done unconditionally, because
667 -- dereferences of private types are legal in default expressions. This
668 -- case is taken care of in Check_Fully_Declared, called below. There
669 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
670
671 -- This consideration also applies to similar checks for allocators,
672 -- qualified expressions, and type conversions.
673
674 -- An additional exception concerns other per-object expressions that
675 -- are not directly related to component declarations, in particular
676 -- representation pragmas for tasks. These will be per-object
677 -- expressions if they depend on discriminants or some global entity.
678 -- If the task has access discriminants, the designated type may be
679 -- incomplete at the point the expression is resolved. This resolution
680 -- takes place within the body of the initialization procedure, where
681 -- the discriminant is replaced by its discriminal.
682
683 if Is_Entity_Name (Pref)
684 and then Ekind (Entity (Pref)) = E_In_Parameter
685 then
686 null;
687
688 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
689 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
690 -- Analyze_Object_Renaming, and Freeze_Entity.
691
692 elsif Ada_Version >= Ada_2005
693 and then Is_Entity_Name (Pref)
694 and then Is_Access_Type (Etype (Pref))
695 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
696 E_Incomplete_Type
697 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
698 then
699 null;
700 else
701 Check_Fully_Declared (Typ, Parent (Pref));
702 end if;
703 end Check_Fully_Declared_Prefix;
704
705 ------------------------------
706 -- Check_Infinite_Recursion --
707 ------------------------------
708
709 function Check_Infinite_Recursion (Call : Node_Id) return Boolean is
710 function Enclosing_Declaration_Or_Statement (N : Node_Id) return Node_Id;
711 -- Return the nearest enclosing declaration or statement that houses
712 -- arbitrary node N.
713
714 function Invoked_With_Different_Arguments (N : Node_Id) return Boolean;
715 -- Determine whether call N invokes the related enclosing subprogram
716 -- with actuals that differ from the subprogram's formals.
717
718 function Is_Conditional_Statement (N : Node_Id) return Boolean;
719 -- Determine whether arbitrary node N denotes a conditional construct
720
721 function Is_Control_Flow_Statement (N : Node_Id) return Boolean;
722 -- Determine whether arbitrary node N denotes a control flow statement
723 -- or a construct that may contains such a statement.
724
725 function Is_Immediately_Within_Body (N : Node_Id) return Boolean;
726 -- Determine whether arbitrary node N appears immediately within the
727 -- statements of an entry or subprogram body.
728
729 function Is_Raise_Idiom (N : Node_Id) return Boolean;
730 -- Determine whether arbitrary node N appears immediately within the
731 -- body of an entry or subprogram, and is preceded by a single raise
732 -- statement.
733
734 function Is_Raise_Statement (N : Node_Id) return Boolean;
735 -- Determine whether arbitrary node N denotes a raise statement
736
737 function Is_Sole_Statement (N : Node_Id) return Boolean;
738 -- Determine whether arbitrary node N is the sole source statement in
739 -- the body of the enclosing subprogram.
740
741 function Preceded_By_Control_Flow_Statement (N : Node_Id) return Boolean;
742 -- Determine whether arbitrary node N is preceded by a control flow
743 -- statement.
744
745 function Within_Conditional_Statement (N : Node_Id) return Boolean;
746 -- Determine whether arbitrary node N appears within a conditional
747 -- construct.
748
749 ----------------------------------------
750 -- Enclosing_Declaration_Or_Statement --
751 ----------------------------------------
752
753 function Enclosing_Declaration_Or_Statement
754 (N : Node_Id) return Node_Id
755 is
756 Par : Node_Id;
757
758 begin
759 Par := N;
760 while Present (Par) loop
761 if Is_Declaration (Par) or else Is_Statement (Par) then
762 return Par;
763
764 -- Prevent the search from going too far
765
766 elsif Is_Body_Or_Package_Declaration (Par) then
767 exit;
768 end if;
769
770 Par := Parent (Par);
771 end loop;
772
773 return N;
774 end Enclosing_Declaration_Or_Statement;
775
776 --------------------------------------
777 -- Invoked_With_Different_Arguments --
778 --------------------------------------
779
780 function Invoked_With_Different_Arguments (N : Node_Id) return Boolean is
781 Subp : constant Entity_Id := Entity (Name (N));
782
783 Actual : Node_Id;
784 Formal : Entity_Id;
785
786 begin
787 -- Determine whether the formals of the invoked subprogram are not
788 -- used as actuals in the call.
789
790 Actual := First_Actual (Call);
791 Formal := First_Formal (Subp);
792 while Present (Actual) and then Present (Formal) loop
793
794 -- The current actual does not match the current formal
795
796 if not (Is_Entity_Name (Actual)
797 and then Entity (Actual) = Formal)
798 then
799 return True;
800 end if;
801
802 Next_Actual (Actual);
803 Next_Formal (Formal);
804 end loop;
805
806 return False;
807 end Invoked_With_Different_Arguments;
808
809 ------------------------------
810 -- Is_Conditional_Statement --
811 ------------------------------
812
813 function Is_Conditional_Statement (N : Node_Id) return Boolean is
814 begin
815 return
816 Nkind (N) in N_And_Then
817 | N_Case_Expression
818 | N_Case_Statement
819 | N_If_Expression
820 | N_If_Statement
821 | N_Or_Else;
822 end Is_Conditional_Statement;
823
824 -------------------------------
825 -- Is_Control_Flow_Statement --
826 -------------------------------
827
828 function Is_Control_Flow_Statement (N : Node_Id) return Boolean is
829 begin
830 -- It is assumed that all statements may affect the control flow in
831 -- some way. A raise statement may be expanded into a non-statement
832 -- node.
833
834 return Is_Statement (N) or else Is_Raise_Statement (N);
835 end Is_Control_Flow_Statement;
836
837 --------------------------------
838 -- Is_Immediately_Within_Body --
839 --------------------------------
840
841 function Is_Immediately_Within_Body (N : Node_Id) return Boolean is
842 HSS : constant Node_Id := Parent (N);
843
844 begin
845 return
846 Nkind (HSS) = N_Handled_Sequence_Of_Statements
847 and then Nkind (Parent (HSS)) in N_Entry_Body | N_Subprogram_Body
848 and then Is_List_Member (N)
849 and then List_Containing (N) = Statements (HSS);
850 end Is_Immediately_Within_Body;
851
852 --------------------
853 -- Is_Raise_Idiom --
854 --------------------
855
856 function Is_Raise_Idiom (N : Node_Id) return Boolean is
857 Raise_Stmt : Node_Id;
858 Stmt : Node_Id;
859
860 begin
861 if Is_Immediately_Within_Body (N) then
862
863 -- Assume that no raise statement has been seen yet
864
865 Raise_Stmt := Empty;
866
867 -- Examine the statements preceding the input node, skipping
868 -- internally-generated constructs.
869
870 Stmt := Prev (N);
871 while Present (Stmt) loop
872
873 -- Multiple raise statements violate the idiom
874
875 if Is_Raise_Statement (Stmt) then
876 if Present (Raise_Stmt) then
877 return False;
878 end if;
879
880 Raise_Stmt := Stmt;
881
882 elsif Comes_From_Source (Stmt) then
883 exit;
884 end if;
885
886 Stmt := Prev (Stmt);
887 end loop;
888
889 -- At this point the node must be preceded by a raise statement,
890 -- and the raise statement has to be the sole statement within
891 -- the enclosing entry or subprogram body.
892
893 return
894 Present (Raise_Stmt) and then Is_Sole_Statement (Raise_Stmt);
895 end if;
896
897 return False;
898 end Is_Raise_Idiom;
899
900 ------------------------
901 -- Is_Raise_Statement --
902 ------------------------
903
904 function Is_Raise_Statement (N : Node_Id) return Boolean is
905 begin
906 -- A raise statement may be transfomed into a Raise_xxx_Error node
907
908 return
909 Nkind (N) = N_Raise_Statement
910 or else Nkind (N) in N_Raise_xxx_Error;
911 end Is_Raise_Statement;
912
913 -----------------------
914 -- Is_Sole_Statement --
915 -----------------------
916
917 function Is_Sole_Statement (N : Node_Id) return Boolean is
918 Stmt : Node_Id;
919
920 begin
921 -- The input node appears within the statements of an entry or
922 -- subprogram body. Examine the statements preceding the node.
923
924 if Is_Immediately_Within_Body (N) then
925 Stmt := Prev (N);
926
927 while Present (Stmt) loop
928
929 -- The statement is preceded by another statement or a source
930 -- construct. This indicates that the node does not appear by
931 -- itself.
932
933 if Is_Control_Flow_Statement (Stmt)
934 or else Comes_From_Source (Stmt)
935 then
936 return False;
937 end if;
938
939 Stmt := Prev (Stmt);
940 end loop;
941
942 return True;
943 end if;
944
945 -- The input node is within a construct nested inside the entry or
946 -- subprogram body.
947
948 return False;
949 end Is_Sole_Statement;
950
951 ----------------------------------------
952 -- Preceded_By_Control_Flow_Statement --
953 ----------------------------------------
954
955 function Preceded_By_Control_Flow_Statement
956 (N : Node_Id) return Boolean
957 is
958 Stmt : Node_Id;
959
960 begin
961 if Is_List_Member (N) then
962 Stmt := Prev (N);
963
964 -- Examine the statements preceding the input node
965
966 while Present (Stmt) loop
967 if Is_Control_Flow_Statement (Stmt) then
968 return True;
969 end if;
970
971 Stmt := Prev (Stmt);
972 end loop;
973
974 return False;
975 end if;
976
977 -- Assume that the node is part of some control flow statement
978
979 return True;
980 end Preceded_By_Control_Flow_Statement;
981
982 ----------------------------------
983 -- Within_Conditional_Statement --
984 ----------------------------------
985
986 function Within_Conditional_Statement (N : Node_Id) return Boolean is
987 Stmt : Node_Id;
988
989 begin
990 Stmt := Parent (N);
991 while Present (Stmt) loop
992 if Is_Conditional_Statement (Stmt) then
993 return True;
994
995 -- Prevent the search from going too far
996
997 elsif Is_Body_Or_Package_Declaration (Stmt) then
998 exit;
999 end if;
1000
1001 Stmt := Parent (Stmt);
1002 end loop;
1003
1004 return False;
1005 end Within_Conditional_Statement;
1006
1007 -- Local variables
1008
1009 Call_Context : constant Node_Id :=
1010 Enclosing_Declaration_Or_Statement (Call);
1011
1012 -- Start of processing for Check_Infinite_Recursion
1013
1014 begin
1015 -- The call is assumed to be safe when the enclosing subprogram is
1016 -- invoked with actuals other than its formals.
1017 --
1018 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1019 -- begin
1020 -- ...
1021 -- Proc (A1, A2, ..., AN);
1022 -- ...
1023 -- end Proc;
1024
1025 if Invoked_With_Different_Arguments (Call) then
1026 return False;
1027
1028 -- The call is assumed to be safe when the invocation of the enclosing
1029 -- subprogram depends on a conditional statement.
1030 --
1031 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1032 -- begin
1033 -- ...
1034 -- if Some_Condition then
1035 -- Proc (F1, F2, ..., FN);
1036 -- end if;
1037 -- ...
1038 -- end Proc;
1039
1040 elsif Within_Conditional_Statement (Call) then
1041 return False;
1042
1043 -- The context of the call is assumed to be safe when the invocation of
1044 -- the enclosing subprogram is preceded by some control flow statement.
1045 --
1046 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1047 -- begin
1048 -- ...
1049 -- if Some_Condition then
1050 -- ...
1051 -- end if;
1052 -- ...
1053 -- Proc (F1, F2, ..., FN);
1054 -- ...
1055 -- end Proc;
1056
1057 elsif Preceded_By_Control_Flow_Statement (Call_Context) then
1058 return False;
1059
1060 -- Detect an idiom where the context of the call is preceded by a single
1061 -- raise statement.
1062 --
1063 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1064 -- begin
1065 -- raise ...;
1066 -- Proc (F1, F2, ..., FN);
1067 -- end Proc;
1068
1069 elsif Is_Raise_Idiom (Call_Context) then
1070 return False;
1071 end if;
1072
1073 -- At this point it is certain that infinite recursion will take place
1074 -- as long as the call is executed. Detect a case where the context of
1075 -- the call is the sole source statement within the subprogram body.
1076 --
1077 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1078 -- begin
1079 -- Proc (F1, F2, ..., FN);
1080 -- end Proc;
1081 --
1082 -- Install an explicit raise to prevent the infinite recursion.
1083
1084 if Is_Sole_Statement (Call_Context) then
1085 Error_Msg_Warn := SPARK_Mode /= On;
1086 Error_Msg_N ("!infinite recursion<<", Call);
1087 Error_Msg_N ("\!Storage_Error [<<", Call);
1088
1089 Insert_Action (Call,
1090 Make_Raise_Storage_Error (Sloc (Call),
1091 Reason => SE_Infinite_Recursion));
1092
1093 -- Otherwise infinite recursion could take place, considering other flow
1094 -- control constructs such as gotos, exit statements, etc.
1095
1096 else
1097 Error_Msg_Warn := SPARK_Mode /= On;
1098 Error_Msg_N ("!possible infinite recursion<<", Call);
1099 Error_Msg_N ("\!??Storage_Error ]<<", Call);
1100 end if;
1101
1102 return True;
1103 end Check_Infinite_Recursion;
1104
1105 ---------------------------------------
1106 -- Check_No_Direct_Boolean_Operators --
1107 ---------------------------------------
1108
1109 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
1110 begin
1111 if Scope (Entity (N)) = Standard_Standard
1112 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
1113 then
1114 -- Restriction only applies to original source code
1115
1116 if Comes_From_Source (N) then
1117 Check_Restriction (No_Direct_Boolean_Operators, N);
1118 end if;
1119 end if;
1120
1121 -- Do style check (but skip if in instance, error is on template)
1122
1123 if Style_Check then
1124 if not In_Instance then
1125 Check_Boolean_Operator (N);
1126 end if;
1127 end if;
1128 end Check_No_Direct_Boolean_Operators;
1129
1130 ------------------------------
1131 -- Check_Parameterless_Call --
1132 ------------------------------
1133
1134 procedure Check_Parameterless_Call (N : Node_Id) is
1135 Nam : Node_Id;
1136
1137 function Prefix_Is_Access_Subp return Boolean;
1138 -- If the prefix is of an access_to_subprogram type, the node must be
1139 -- rewritten as a call. Ditto if the prefix is overloaded and all its
1140 -- interpretations are access to subprograms.
1141
1142 ---------------------------
1143 -- Prefix_Is_Access_Subp --
1144 ---------------------------
1145
1146 function Prefix_Is_Access_Subp return Boolean is
1147 I : Interp_Index;
1148 It : Interp;
1149
1150 begin
1151 -- If the context is an attribute reference that can apply to
1152 -- functions, this is never a parameterless call (RM 4.1.4(6)).
1153
1154 if Nkind (Parent (N)) = N_Attribute_Reference
1155 and then Attribute_Name (Parent (N))
1156 in Name_Address | Name_Code_Address | Name_Access
1157 then
1158 return False;
1159 end if;
1160
1161 if not Is_Overloaded (N) then
1162 return
1163 Ekind (Etype (N)) = E_Subprogram_Type
1164 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1165 else
1166 Get_First_Interp (N, I, It);
1167 while Present (It.Typ) loop
1168 if Ekind (It.Typ) /= E_Subprogram_Type
1169 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1170 then
1171 return False;
1172 end if;
1173
1174 Get_Next_Interp (I, It);
1175 end loop;
1176
1177 return True;
1178 end if;
1179 end Prefix_Is_Access_Subp;
1180
1181 -- Start of processing for Check_Parameterless_Call
1182
1183 begin
1184 -- Defend against junk stuff if errors already detected
1185
1186 if Total_Errors_Detected /= 0 then
1187 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1188 return;
1189 elsif Nkind (N) in N_Has_Chars
1190 and then not Is_Valid_Name (Chars (N))
1191 then
1192 return;
1193 end if;
1194
1195 Require_Entity (N);
1196 end if;
1197
1198 -- If the context expects a value, and the name is a procedure, this is
1199 -- most likely a missing 'Access. Don't try to resolve the parameterless
1200 -- call, error will be caught when the outer call is analyzed.
1201
1202 if Is_Entity_Name (N)
1203 and then Ekind (Entity (N)) = E_Procedure
1204 and then not Is_Overloaded (N)
1205 and then
1206 Nkind (Parent (N)) in N_Parameter_Association
1207 | N_Function_Call
1208 | N_Procedure_Call_Statement
1209 then
1210 return;
1211 end if;
1212
1213 -- Rewrite as call if overloadable entity that is (or could be, in the
1214 -- overloaded case) a function call. If we know for sure that the entity
1215 -- is an enumeration literal, we do not rewrite it.
1216
1217 -- If the entity is the name of an operator, it cannot be a call because
1218 -- operators cannot have default parameters. In this case, this must be
1219 -- a string whose contents coincide with an operator name. Set the kind
1220 -- of the node appropriately.
1221
1222 if (Is_Entity_Name (N)
1223 and then Nkind (N) /= N_Operator_Symbol
1224 and then Is_Overloadable (Entity (N))
1225 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1226 or else Is_Overloaded (N)))
1227
1228 -- Rewrite as call if it is an explicit dereference of an expression of
1229 -- a subprogram access type, and the subprogram type is not that of a
1230 -- procedure or entry.
1231
1232 or else
1233 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1234
1235 -- Rewrite as call if it is a selected component which is a function,
1236 -- this is the case of a call to a protected function (which may be
1237 -- overloaded with other protected operations).
1238
1239 or else
1240 (Nkind (N) = N_Selected_Component
1241 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1242 or else
1243 (Ekind (Entity (Selector_Name (N))) in
1244 E_Entry | E_Procedure
1245 and then Is_Overloaded (Selector_Name (N)))))
1246
1247 -- If one of the above three conditions is met, rewrite as call. Apply
1248 -- the rewriting only once.
1249
1250 then
1251 if Nkind (Parent (N)) /= N_Function_Call
1252 or else N /= Name (Parent (N))
1253 then
1254
1255 -- This may be a prefixed call that was not fully analyzed, e.g.
1256 -- an actual in an instance.
1257
1258 if Ada_Version >= Ada_2005
1259 and then Nkind (N) = N_Selected_Component
1260 and then Is_Dispatching_Operation (Entity (Selector_Name (N)))
1261 then
1262 Analyze_Selected_Component (N);
1263
1264 if Nkind (N) /= N_Selected_Component then
1265 return;
1266 end if;
1267 end if;
1268
1269 -- The node is the name of the parameterless call. Preserve its
1270 -- descendants, which may be complex expressions.
1271
1272 Nam := Relocate_Node (N);
1273
1274 -- If overloaded, overload set belongs to new copy
1275
1276 Save_Interps (N, Nam);
1277
1278 -- Change node to parameterless function call (note that the
1279 -- Parameter_Associations associations field is left set to Empty,
1280 -- its normal default value since there are no parameters)
1281
1282 Change_Node (N, N_Function_Call);
1283 Set_Name (N, Nam);
1284 Set_Sloc (N, Sloc (Nam));
1285 Analyze_Call (N);
1286 end if;
1287
1288 elsif Nkind (N) = N_Parameter_Association then
1289 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1290
1291 elsif Nkind (N) = N_Operator_Symbol then
1292 Set_Etype (N, Empty);
1293 Set_Entity (N, Empty);
1294 Set_Is_Overloaded (N, False);
1295 Change_Operator_Symbol_To_String_Literal (N);
1296 Set_Etype (N, Any_String);
1297 end if;
1298 end Check_Parameterless_Call;
1299
1300 --------------------------------
1301 -- Is_Atomic_Ref_With_Address --
1302 --------------------------------
1303
1304 function Is_Atomic_Ref_With_Address (N : Node_Id) return Boolean is
1305 Pref : constant Node_Id := Prefix (N);
1306
1307 begin
1308 if not Is_Entity_Name (Pref) then
1309 return False;
1310
1311 else
1312 declare
1313 Pent : constant Entity_Id := Entity (Pref);
1314 Ptyp : constant Entity_Id := Etype (Pent);
1315 begin
1316 return not Is_Access_Type (Ptyp)
1317 and then (Is_Atomic (Ptyp) or else Is_Atomic (Pent))
1318 and then Present (Address_Clause (Pent));
1319 end;
1320 end if;
1321 end Is_Atomic_Ref_With_Address;
1322
1323 -----------------------------
1324 -- Is_Definite_Access_Type --
1325 -----------------------------
1326
1327 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1328 Btyp : constant Entity_Id := Base_Type (E);
1329 begin
1330 return Ekind (Btyp) = E_Access_Type
1331 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1332 and then Comes_From_Source (Btyp));
1333 end Is_Definite_Access_Type;
1334
1335 ----------------------
1336 -- Is_Predefined_Op --
1337 ----------------------
1338
1339 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1340 begin
1341 -- Predefined operators are intrinsic subprograms
1342
1343 if not Is_Intrinsic_Subprogram (Nam) then
1344 return False;
1345 end if;
1346
1347 -- A call to a back-end builtin is never a predefined operator
1348
1349 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1350 return False;
1351 end if;
1352
1353 return not Is_Generic_Instance (Nam)
1354 and then Chars (Nam) in Any_Operator_Name
1355 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1356 end Is_Predefined_Op;
1357
1358 -----------------------------
1359 -- Make_Call_Into_Operator --
1360 -----------------------------
1361
1362 procedure Make_Call_Into_Operator
1363 (N : Node_Id;
1364 Typ : Entity_Id;
1365 Op_Id : Entity_Id)
1366 is
1367 Op_Name : constant Name_Id := Chars (Op_Id);
1368 Act1 : Node_Id := First_Actual (N);
1369 Act2 : Node_Id := Next_Actual (Act1);
1370 Error : Boolean := False;
1371 Func : constant Entity_Id := Entity (Name (N));
1372 Is_Binary : constant Boolean := Present (Act2);
1373 Op_Node : Node_Id;
1374 Opnd_Type : Entity_Id := Empty;
1375 Orig_Type : Entity_Id := Empty;
1376 Pack : Entity_Id;
1377
1378 type Kind_Test is access function (E : Entity_Id) return Boolean;
1379
1380 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1381 -- If the operand is not universal, and the operator is given by an
1382 -- expanded name, verify that the operand has an interpretation with a
1383 -- type defined in the given scope of the operator.
1384
1385 function Type_In_P (Test : Kind_Test) return Entity_Id;
1386 -- Find a type of the given class in package Pack that contains the
1387 -- operator.
1388
1389 ---------------------------
1390 -- Operand_Type_In_Scope --
1391 ---------------------------
1392
1393 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1394 Nod : constant Node_Id := Right_Opnd (Op_Node);
1395 I : Interp_Index;
1396 It : Interp;
1397
1398 begin
1399 if not Is_Overloaded (Nod) then
1400 return Scope (Base_Type (Etype (Nod))) = S;
1401
1402 else
1403 Get_First_Interp (Nod, I, It);
1404 while Present (It.Typ) loop
1405 if Scope (Base_Type (It.Typ)) = S then
1406 return True;
1407 end if;
1408
1409 Get_Next_Interp (I, It);
1410 end loop;
1411
1412 return False;
1413 end if;
1414 end Operand_Type_In_Scope;
1415
1416 ---------------
1417 -- Type_In_P --
1418 ---------------
1419
1420 function Type_In_P (Test : Kind_Test) return Entity_Id is
1421 E : Entity_Id;
1422
1423 function In_Decl return Boolean;
1424 -- Verify that node is not part of the type declaration for the
1425 -- candidate type, which would otherwise be invisible.
1426
1427 -------------
1428 -- In_Decl --
1429 -------------
1430
1431 function In_Decl return Boolean is
1432 Decl_Node : constant Node_Id := Parent (E);
1433 N2 : Node_Id;
1434
1435 begin
1436 N2 := N;
1437
1438 if Etype (E) = Any_Type then
1439 return True;
1440
1441 elsif No (Decl_Node) then
1442 return False;
1443
1444 else
1445 while Present (N2)
1446 and then Nkind (N2) /= N_Compilation_Unit
1447 loop
1448 if N2 = Decl_Node then
1449 return True;
1450 else
1451 N2 := Parent (N2);
1452 end if;
1453 end loop;
1454
1455 return False;
1456 end if;
1457 end In_Decl;
1458
1459 -- Start of processing for Type_In_P
1460
1461 begin
1462 -- If the context type is declared in the prefix package, this is the
1463 -- desired base type.
1464
1465 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1466 return Base_Type (Typ);
1467
1468 else
1469 E := First_Entity (Pack);
1470 while Present (E) loop
1471 if Test (E) and then not In_Decl then
1472 return E;
1473 end if;
1474
1475 Next_Entity (E);
1476 end loop;
1477
1478 return Empty;
1479 end if;
1480 end Type_In_P;
1481
1482 -- Start of processing for Make_Call_Into_Operator
1483
1484 begin
1485 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1486
1487 -- Ensure that the corresponding operator has the same parent as the
1488 -- original call. This guarantees that parent traversals performed by
1489 -- the ABE mechanism succeed.
1490
1491 Set_Parent (Op_Node, Parent (N));
1492
1493 -- Binary operator
1494
1495 if Is_Binary then
1496 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1497 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1498 Save_Interps (Act1, Left_Opnd (Op_Node));
1499 Save_Interps (Act2, Right_Opnd (Op_Node));
1500 Act1 := Left_Opnd (Op_Node);
1501 Act2 := Right_Opnd (Op_Node);
1502
1503 -- Unary operator
1504
1505 else
1506 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1507 Save_Interps (Act1, Right_Opnd (Op_Node));
1508 Act1 := Right_Opnd (Op_Node);
1509 end if;
1510
1511 -- If the operator is denoted by an expanded name, and the prefix is
1512 -- not Standard, but the operator is a predefined one whose scope is
1513 -- Standard, then this is an implicit_operator, inserted as an
1514 -- interpretation by the procedure of the same name. This procedure
1515 -- overestimates the presence of implicit operators, because it does
1516 -- not examine the type of the operands. Verify now that the operand
1517 -- type appears in the given scope. If right operand is universal,
1518 -- check the other operand. In the case of concatenation, either
1519 -- argument can be the component type, so check the type of the result.
1520 -- If both arguments are literals, look for a type of the right kind
1521 -- defined in the given scope. This elaborate nonsense is brought to
1522 -- you courtesy of b33302a. The type itself must be frozen, so we must
1523 -- find the type of the proper class in the given scope.
1524
1525 -- A final wrinkle is the multiplication operator for fixed point types,
1526 -- which is defined in Standard only, and not in the scope of the
1527 -- fixed point type itself.
1528
1529 if Nkind (Name (N)) = N_Expanded_Name then
1530 Pack := Entity (Prefix (Name (N)));
1531
1532 -- If this is a package renaming, get renamed entity, which will be
1533 -- the scope of the operands if operaton is type-correct.
1534
1535 if Present (Renamed_Entity (Pack)) then
1536 Pack := Renamed_Entity (Pack);
1537 end if;
1538
1539 -- If the entity being called is defined in the given package, it is
1540 -- a renaming of a predefined operator, and known to be legal.
1541
1542 if Scope (Entity (Name (N))) = Pack
1543 and then Pack /= Standard_Standard
1544 then
1545 null;
1546
1547 -- Visibility does not need to be checked in an instance: if the
1548 -- operator was not visible in the generic it has been diagnosed
1549 -- already, else there is an implicit copy of it in the instance.
1550
1551 elsif In_Instance then
1552 null;
1553
1554 elsif Op_Name in Name_Op_Multiply | Name_Op_Divide
1555 and then Is_Fixed_Point_Type (Etype (Act1))
1556 and then Is_Fixed_Point_Type (Etype (Act2))
1557 then
1558 if Pack /= Standard_Standard then
1559 Error := True;
1560 end if;
1561
1562 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1563 -- available.
1564
1565 elsif Ada_Version >= Ada_2005
1566 and then Op_Name in Name_Op_Eq | Name_Op_Ne
1567 and then (Is_Anonymous_Access_Type (Etype (Act1))
1568 or else Is_Anonymous_Access_Type (Etype (Act2)))
1569 then
1570 null;
1571
1572 else
1573 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1574
1575 if Op_Name = Name_Op_Concat then
1576 Opnd_Type := Base_Type (Typ);
1577
1578 elsif (Scope (Opnd_Type) = Standard_Standard
1579 and then Is_Binary)
1580 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1581 and then Is_Binary
1582 and then not Comes_From_Source (Opnd_Type))
1583 then
1584 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1585 end if;
1586
1587 if Scope (Opnd_Type) = Standard_Standard then
1588
1589 -- Verify that the scope contains a type that corresponds to
1590 -- the given literal. Optimize the case where Pack is Standard.
1591
1592 if Pack /= Standard_Standard then
1593 if Opnd_Type = Universal_Integer then
1594 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1595
1596 elsif Opnd_Type = Universal_Real then
1597 Orig_Type := Type_In_P (Is_Real_Type'Access);
1598
1599 elsif Opnd_Type = Any_String then
1600 Orig_Type := Type_In_P (Is_String_Type'Access);
1601
1602 elsif Opnd_Type = Any_Access then
1603 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1604
1605 elsif Opnd_Type = Any_Composite then
1606 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1607
1608 if Present (Orig_Type) then
1609 if Has_Private_Component (Orig_Type) then
1610 Orig_Type := Empty;
1611 else
1612 Set_Etype (Act1, Orig_Type);
1613
1614 if Is_Binary then
1615 Set_Etype (Act2, Orig_Type);
1616 end if;
1617 end if;
1618 end if;
1619
1620 else
1621 Orig_Type := Empty;
1622 end if;
1623
1624 Error := No (Orig_Type);
1625 end if;
1626
1627 elsif Ekind (Opnd_Type) = E_Allocator_Type
1628 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1629 then
1630 Error := True;
1631
1632 -- If the type is defined elsewhere, and the operator is not
1633 -- defined in the given scope (by a renaming declaration, e.g.)
1634 -- then this is an error as well. If an extension of System is
1635 -- present, and the type may be defined there, Pack must be
1636 -- System itself.
1637
1638 elsif Scope (Opnd_Type) /= Pack
1639 and then Scope (Op_Id) /= Pack
1640 and then (No (System_Aux_Id)
1641 or else Scope (Opnd_Type) /= System_Aux_Id
1642 or else Pack /= Scope (System_Aux_Id))
1643 then
1644 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1645 Error := True;
1646 else
1647 Error := not Operand_Type_In_Scope (Pack);
1648 end if;
1649
1650 elsif Pack = Standard_Standard
1651 and then not Operand_Type_In_Scope (Standard_Standard)
1652 then
1653 Error := True;
1654 end if;
1655 end if;
1656
1657 if Error then
1658 Error_Msg_Node_2 := Pack;
1659 Error_Msg_NE
1660 ("& not declared in&", N, Selector_Name (Name (N)));
1661 Set_Etype (N, Any_Type);
1662 return;
1663
1664 -- Detect a mismatch between the context type and the result type
1665 -- in the named package, which is otherwise not detected if the
1666 -- operands are universal. Check is only needed if source entity is
1667 -- an operator, not a function that renames an operator.
1668
1669 elsif Nkind (Parent (N)) /= N_Type_Conversion
1670 and then Ekind (Entity (Name (N))) = E_Operator
1671 and then Is_Numeric_Type (Typ)
1672 and then not Is_Universal_Numeric_Type (Typ)
1673 and then Scope (Base_Type (Typ)) /= Pack
1674 and then not In_Instance
1675 then
1676 if Is_Fixed_Point_Type (Typ)
1677 and then Op_Name in Name_Op_Multiply | Name_Op_Divide
1678 then
1679 -- Already checked above
1680
1681 null;
1682
1683 -- Operator may be defined in an extension of System
1684
1685 elsif Present (System_Aux_Id)
1686 and then Present (Opnd_Type)
1687 and then Scope (Opnd_Type) = System_Aux_Id
1688 then
1689 null;
1690
1691 else
1692 -- Could we use Wrong_Type here??? (this would require setting
1693 -- Etype (N) to the actual type found where Typ was expected).
1694
1695 Error_Msg_NE ("expect }", N, Typ);
1696 end if;
1697 end if;
1698 end if;
1699
1700 Set_Chars (Op_Node, Op_Name);
1701
1702 if not Is_Private_Type (Etype (N)) then
1703 Set_Etype (Op_Node, Base_Type (Etype (N)));
1704 else
1705 Set_Etype (Op_Node, Etype (N));
1706 end if;
1707
1708 -- If this is a call to a function that renames a predefined equality,
1709 -- the renaming declaration provides a type that must be used to
1710 -- resolve the operands. This must be done now because resolution of
1711 -- the equality node will not resolve any remaining ambiguity, and it
1712 -- assumes that the first operand is not overloaded.
1713
1714 if Op_Name in Name_Op_Eq | Name_Op_Ne
1715 and then Ekind (Func) = E_Function
1716 and then Is_Overloaded (Act1)
1717 then
1718 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1719 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1720 end if;
1721
1722 Set_Entity (Op_Node, Op_Id);
1723 Generate_Reference (Op_Id, N, ' ');
1724
1725 -- Do rewrite setting Comes_From_Source on the result if the original
1726 -- call came from source. Although it is not strictly the case that the
1727 -- operator as such comes from the source, logically it corresponds
1728 -- exactly to the function call in the source, so it should be marked
1729 -- this way (e.g. to make sure that validity checks work fine).
1730
1731 declare
1732 CS : constant Boolean := Comes_From_Source (N);
1733 begin
1734 Rewrite (N, Op_Node);
1735 Set_Comes_From_Source (N, CS);
1736 end;
1737
1738 -- If this is an arithmetic operator and the result type is private,
1739 -- the operands and the result must be wrapped in conversion to
1740 -- expose the underlying numeric type and expand the proper checks,
1741 -- e.g. on division.
1742
1743 if Is_Private_Type (Typ) then
1744 case Nkind (N) is
1745 when N_Op_Add
1746 | N_Op_Divide
1747 | N_Op_Expon
1748 | N_Op_Mod
1749 | N_Op_Multiply
1750 | N_Op_Rem
1751 | N_Op_Subtract
1752 =>
1753 Resolve_Intrinsic_Operator (N, Typ);
1754
1755 when N_Op_Abs
1756 | N_Op_Minus
1757 | N_Op_Plus
1758 =>
1759 Resolve_Intrinsic_Unary_Operator (N, Typ);
1760
1761 when others =>
1762 Resolve (N, Typ);
1763 end case;
1764 else
1765 Resolve (N, Typ);
1766 end if;
1767 end Make_Call_Into_Operator;
1768
1769 -------------------
1770 -- Operator_Kind --
1771 -------------------
1772
1773 function Operator_Kind
1774 (Op_Name : Name_Id;
1775 Is_Binary : Boolean) return Node_Kind
1776 is
1777 Kind : Node_Kind;
1778
1779 begin
1780 -- Use CASE statement or array???
1781
1782 if Is_Binary then
1783 if Op_Name = Name_Op_And then
1784 Kind := N_Op_And;
1785 elsif Op_Name = Name_Op_Or then
1786 Kind := N_Op_Or;
1787 elsif Op_Name = Name_Op_Xor then
1788 Kind := N_Op_Xor;
1789 elsif Op_Name = Name_Op_Eq then
1790 Kind := N_Op_Eq;
1791 elsif Op_Name = Name_Op_Ne then
1792 Kind := N_Op_Ne;
1793 elsif Op_Name = Name_Op_Lt then
1794 Kind := N_Op_Lt;
1795 elsif Op_Name = Name_Op_Le then
1796 Kind := N_Op_Le;
1797 elsif Op_Name = Name_Op_Gt then
1798 Kind := N_Op_Gt;
1799 elsif Op_Name = Name_Op_Ge then
1800 Kind := N_Op_Ge;
1801 elsif Op_Name = Name_Op_Add then
1802 Kind := N_Op_Add;
1803 elsif Op_Name = Name_Op_Subtract then
1804 Kind := N_Op_Subtract;
1805 elsif Op_Name = Name_Op_Concat then
1806 Kind := N_Op_Concat;
1807 elsif Op_Name = Name_Op_Multiply then
1808 Kind := N_Op_Multiply;
1809 elsif Op_Name = Name_Op_Divide then
1810 Kind := N_Op_Divide;
1811 elsif Op_Name = Name_Op_Mod then
1812 Kind := N_Op_Mod;
1813 elsif Op_Name = Name_Op_Rem then
1814 Kind := N_Op_Rem;
1815 elsif Op_Name = Name_Op_Expon then
1816 Kind := N_Op_Expon;
1817 else
1818 raise Program_Error;
1819 end if;
1820
1821 -- Unary operators
1822
1823 else
1824 if Op_Name = Name_Op_Add then
1825 Kind := N_Op_Plus;
1826 elsif Op_Name = Name_Op_Subtract then
1827 Kind := N_Op_Minus;
1828 elsif Op_Name = Name_Op_Abs then
1829 Kind := N_Op_Abs;
1830 elsif Op_Name = Name_Op_Not then
1831 Kind := N_Op_Not;
1832 else
1833 raise Program_Error;
1834 end if;
1835 end if;
1836
1837 return Kind;
1838 end Operator_Kind;
1839
1840 ----------------------------
1841 -- Preanalyze_And_Resolve --
1842 ----------------------------
1843
1844 procedure Preanalyze_And_Resolve
1845 (N : Node_Id;
1846 T : Entity_Id;
1847 With_Freezing : Boolean)
1848 is
1849 Save_Full_Analysis : constant Boolean := Full_Analysis;
1850 Save_Must_Not_Freeze : constant Boolean := Must_Not_Freeze (N);
1851 Save_Preanalysis_Count : constant Nat :=
1852 Inside_Preanalysis_Without_Freezing;
1853 begin
1854 pragma Assert (Nkind (N) in N_Subexpr);
1855
1856 if not With_Freezing then
1857 Set_Must_Not_Freeze (N);
1858 Inside_Preanalysis_Without_Freezing :=
1859 Inside_Preanalysis_Without_Freezing + 1;
1860 end if;
1861
1862 Full_Analysis := False;
1863 Expander_Mode_Save_And_Set (False);
1864
1865 -- Normally, we suppress all checks for this preanalysis. There is no
1866 -- point in processing them now, since they will be applied properly
1867 -- and in the proper location when the default expressions reanalyzed
1868 -- and reexpanded later on. We will also have more information at that
1869 -- point for possible suppression of individual checks.
1870
1871 -- However, in SPARK mode, most expansion is suppressed, and this
1872 -- later reanalysis and reexpansion may not occur. SPARK mode does
1873 -- require the setting of checking flags for proof purposes, so we
1874 -- do the SPARK preanalysis without suppressing checks.
1875
1876 -- This special handling for SPARK mode is required for example in the
1877 -- case of Ada 2012 constructs such as quantified expressions, which are
1878 -- expanded in two separate steps.
1879
1880 if GNATprove_Mode then
1881 Analyze_And_Resolve (N, T);
1882 else
1883 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1884 end if;
1885
1886 Expander_Mode_Restore;
1887 Full_Analysis := Save_Full_Analysis;
1888 Set_Must_Not_Freeze (N, Save_Must_Not_Freeze);
1889
1890 if not With_Freezing then
1891 Inside_Preanalysis_Without_Freezing :=
1892 Inside_Preanalysis_Without_Freezing - 1;
1893 end if;
1894
1895 pragma Assert
1896 (Inside_Preanalysis_Without_Freezing = Save_Preanalysis_Count);
1897 end Preanalyze_And_Resolve;
1898
1899 ----------------------------
1900 -- Preanalyze_And_Resolve --
1901 ----------------------------
1902
1903 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1904 begin
1905 Preanalyze_And_Resolve (N, T, With_Freezing => False);
1906 end Preanalyze_And_Resolve;
1907
1908 -- Version without context type
1909
1910 procedure Preanalyze_And_Resolve (N : Node_Id) is
1911 Save_Full_Analysis : constant Boolean := Full_Analysis;
1912
1913 begin
1914 Full_Analysis := False;
1915 Expander_Mode_Save_And_Set (False);
1916
1917 Analyze (N);
1918 Resolve (N, Etype (N), Suppress => All_Checks);
1919
1920 Expander_Mode_Restore;
1921 Full_Analysis := Save_Full_Analysis;
1922 end Preanalyze_And_Resolve;
1923
1924 ------------------------------------------
1925 -- Preanalyze_With_Freezing_And_Resolve --
1926 ------------------------------------------
1927
1928 procedure Preanalyze_With_Freezing_And_Resolve
1929 (N : Node_Id;
1930 T : Entity_Id)
1931 is
1932 begin
1933 Preanalyze_And_Resolve (N, T, With_Freezing => True);
1934 end Preanalyze_With_Freezing_And_Resolve;
1935
1936 ----------------------------------
1937 -- Replace_Actual_Discriminants --
1938 ----------------------------------
1939
1940 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1941 Loc : constant Source_Ptr := Sloc (N);
1942 Tsk : Node_Id := Empty;
1943
1944 function Process_Discr (Nod : Node_Id) return Traverse_Result;
1945 -- Comment needed???
1946
1947 -------------------
1948 -- Process_Discr --
1949 -------------------
1950
1951 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1952 Ent : Entity_Id;
1953
1954 begin
1955 if Nkind (Nod) = N_Identifier then
1956 Ent := Entity (Nod);
1957
1958 if Present (Ent)
1959 and then Ekind (Ent) = E_Discriminant
1960 then
1961 Rewrite (Nod,
1962 Make_Selected_Component (Loc,
1963 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1964 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1965
1966 Set_Etype (Nod, Etype (Ent));
1967 end if;
1968
1969 end if;
1970
1971 return OK;
1972 end Process_Discr;
1973
1974 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1975
1976 -- Start of processing for Replace_Actual_Discriminants
1977
1978 begin
1979 if Expander_Active then
1980 null;
1981
1982 -- Allow the replacement of concurrent discriminants in GNATprove even
1983 -- though this is a light expansion activity. Note that generic units
1984 -- are not modified.
1985
1986 elsif GNATprove_Mode and not Inside_A_Generic then
1987 null;
1988
1989 else
1990 return;
1991 end if;
1992
1993 if Nkind (Name (N)) = N_Selected_Component then
1994 Tsk := Prefix (Name (N));
1995
1996 elsif Nkind (Name (N)) = N_Indexed_Component then
1997 Tsk := Prefix (Prefix (Name (N)));
1998 end if;
1999
2000 if Present (Tsk) then
2001 Replace_Discrs (Default);
2002 end if;
2003 end Replace_Actual_Discriminants;
2004
2005 -------------
2006 -- Resolve --
2007 -------------
2008
2009 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
2010 Ambiguous : Boolean := False;
2011 Ctx_Type : Entity_Id := Typ;
2012 Expr_Type : Entity_Id := Empty; -- prevent junk warning
2013 Err_Type : Entity_Id := Empty;
2014 Found : Boolean := False;
2015 From_Lib : Boolean;
2016 I : Interp_Index;
2017 I1 : Interp_Index := 0; -- prevent junk warning
2018 It : Interp;
2019 It1 : Interp;
2020 Seen : Entity_Id := Empty; -- prevent junk warning
2021
2022 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
2023 -- Determine whether a node comes from a predefined library unit or
2024 -- Standard.
2025
2026 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
2027 -- Try and fix up a literal so that it matches its expected type. New
2028 -- literals are manufactured if necessary to avoid cascaded errors.
2029
2030 procedure Report_Ambiguous_Argument;
2031 -- Additional diagnostics when an ambiguous call has an ambiguous
2032 -- argument (typically a controlling actual).
2033
2034 procedure Resolution_Failed;
2035 -- Called when attempt at resolving current expression fails
2036
2037 ------------------------------------
2038 -- Comes_From_Predefined_Lib_Unit --
2039 -------------------------------------
2040
2041 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
2042 begin
2043 return
2044 Sloc (Nod) = Standard_Location or else In_Predefined_Unit (Nod);
2045 end Comes_From_Predefined_Lib_Unit;
2046
2047 --------------------
2048 -- Patch_Up_Value --
2049 --------------------
2050
2051 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
2052 begin
2053 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
2054 Rewrite (N,
2055 Make_Real_Literal (Sloc (N),
2056 Realval => UR_From_Uint (Intval (N))));
2057 Set_Etype (N, Universal_Real);
2058 Set_Is_Static_Expression (N);
2059
2060 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
2061 Rewrite (N,
2062 Make_Integer_Literal (Sloc (N),
2063 Intval => UR_To_Uint (Realval (N))));
2064 Set_Etype (N, Universal_Integer);
2065 Set_Is_Static_Expression (N);
2066
2067 elsif Nkind (N) = N_String_Literal
2068 and then Is_Character_Type (Typ)
2069 then
2070 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
2071 Rewrite (N,
2072 Make_Character_Literal (Sloc (N),
2073 Chars => Name_Find,
2074 Char_Literal_Value =>
2075 UI_From_Int (Character'Pos ('A'))));
2076 Set_Etype (N, Any_Character);
2077 Set_Is_Static_Expression (N);
2078
2079 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
2080 Rewrite (N,
2081 Make_String_Literal (Sloc (N),
2082 Strval => End_String));
2083
2084 elsif Nkind (N) = N_Range then
2085 Patch_Up_Value (Low_Bound (N), Typ);
2086 Patch_Up_Value (High_Bound (N), Typ);
2087 end if;
2088 end Patch_Up_Value;
2089
2090 -------------------------------
2091 -- Report_Ambiguous_Argument --
2092 -------------------------------
2093
2094 procedure Report_Ambiguous_Argument is
2095 Arg : constant Node_Id := First (Parameter_Associations (N));
2096 I : Interp_Index;
2097 It : Interp;
2098
2099 begin
2100 if Nkind (Arg) = N_Function_Call
2101 and then Is_Entity_Name (Name (Arg))
2102 and then Is_Overloaded (Name (Arg))
2103 then
2104 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
2105
2106 -- Examine possible interpretations, and adapt the message
2107 -- for inherited subprograms declared by a type derivation.
2108
2109 Get_First_Interp (Name (Arg), I, It);
2110 while Present (It.Nam) loop
2111 Error_Msg_Sloc := Sloc (It.Nam);
2112
2113 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
2114 Error_Msg_N ("interpretation (inherited) #!", Arg);
2115 else
2116 Error_Msg_N ("interpretation #!", Arg);
2117 end if;
2118
2119 Get_Next_Interp (I, It);
2120 end loop;
2121 end if;
2122
2123 -- Additional message and hint if the ambiguity involves an Ada2020
2124 -- container aggregate.
2125
2126 Check_Ambiguous_Aggregate (N);
2127 end Report_Ambiguous_Argument;
2128
2129 -----------------------
2130 -- Resolution_Failed --
2131 -----------------------
2132
2133 procedure Resolution_Failed is
2134 begin
2135 Patch_Up_Value (N, Typ);
2136
2137 -- Set the type to the desired one to minimize cascaded errors. Note
2138 -- that this is an approximation and does not work in all cases.
2139
2140 Set_Etype (N, Typ);
2141
2142 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
2143 Set_Is_Overloaded (N, False);
2144
2145 -- The caller will return without calling the expander, so we need
2146 -- to set the analyzed flag. Note that it is fine to set Analyzed
2147 -- to True even if we are in the middle of a shallow analysis,
2148 -- (see the spec of sem for more details) since this is an error
2149 -- situation anyway, and there is no point in repeating the
2150 -- analysis later (indeed it won't work to repeat it later, since
2151 -- we haven't got a clear resolution of which entity is being
2152 -- referenced.)
2153
2154 Set_Analyzed (N, True);
2155 return;
2156 end Resolution_Failed;
2157
2158 Literal_Aspect_Map :
2159 constant array (N_Numeric_Or_String_Literal) of Aspect_Id :=
2160 (N_Integer_Literal => Aspect_Integer_Literal,
2161 N_Real_Literal => Aspect_Real_Literal,
2162 N_String_Literal => Aspect_String_Literal);
2163
2164 Named_Number_Aspect_Map : constant array (Named_Kind) of Aspect_Id :=
2165 (E_Named_Integer => Aspect_Integer_Literal,
2166 E_Named_Real => Aspect_Real_Literal);
2167
2168 -- Start of processing for Resolve
2169
2170 begin
2171 if N = Error then
2172 return;
2173 end if;
2174
2175 -- Access attribute on remote subprogram cannot be used for a non-remote
2176 -- access-to-subprogram type.
2177
2178 if Nkind (N) = N_Attribute_Reference
2179 and then Attribute_Name (N) in Name_Access
2180 | Name_Unrestricted_Access
2181 | Name_Unchecked_Access
2182 and then Comes_From_Source (N)
2183 and then Is_Entity_Name (Prefix (N))
2184 and then Is_Subprogram (Entity (Prefix (N)))
2185 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
2186 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
2187 then
2188 Error_Msg_N
2189 ("prefix must statically denote a non-remote subprogram", N);
2190 end if;
2191
2192 From_Lib := Comes_From_Predefined_Lib_Unit (N);
2193
2194 -- If the context is a Remote_Access_To_Subprogram, access attributes
2195 -- must be resolved with the corresponding fat pointer. There is no need
2196 -- to check for the attribute name since the return type of an
2197 -- attribute is never a remote type.
2198
2199 if Nkind (N) = N_Attribute_Reference
2200 and then Comes_From_Source (N)
2201 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
2202 then
2203 declare
2204 Attr : constant Attribute_Id :=
2205 Get_Attribute_Id (Attribute_Name (N));
2206 Pref : constant Node_Id := Prefix (N);
2207 Decl : Node_Id;
2208 Spec : Node_Id;
2209 Is_Remote : Boolean := True;
2210
2211 begin
2212 -- Check that Typ is a remote access-to-subprogram type
2213
2214 if Is_Remote_Access_To_Subprogram_Type (Typ) then
2215
2216 -- Prefix (N) must statically denote a remote subprogram
2217 -- declared in a package specification.
2218
2219 if Attr = Attribute_Access or else
2220 Attr = Attribute_Unchecked_Access or else
2221 Attr = Attribute_Unrestricted_Access
2222 then
2223 Decl := Unit_Declaration_Node (Entity (Pref));
2224
2225 if Nkind (Decl) = N_Subprogram_Body then
2226 Spec := Corresponding_Spec (Decl);
2227
2228 if Present (Spec) then
2229 Decl := Unit_Declaration_Node (Spec);
2230 end if;
2231 end if;
2232
2233 Spec := Parent (Decl);
2234
2235 if not Is_Entity_Name (Prefix (N))
2236 or else Nkind (Spec) /= N_Package_Specification
2237 or else
2238 not Is_Remote_Call_Interface (Defining_Entity (Spec))
2239 then
2240 Is_Remote := False;
2241 Error_Msg_N
2242 ("prefix must statically denote a remote subprogram",
2243 N);
2244 end if;
2245
2246 -- If we are generating code in distributed mode, perform
2247 -- semantic checks against corresponding remote entities.
2248
2249 if Expander_Active
2250 and then Get_PCS_Name /= Name_No_DSA
2251 then
2252 Check_Subtype_Conformant
2253 (New_Id => Entity (Prefix (N)),
2254 Old_Id => Designated_Type
2255 (Corresponding_Remote_Type (Typ)),
2256 Err_Loc => N);
2257
2258 if Is_Remote then
2259 Process_Remote_AST_Attribute (N, Typ);
2260 end if;
2261 end if;
2262 end if;
2263 end if;
2264 end;
2265 end if;
2266
2267 Debug_A_Entry ("resolving ", N);
2268
2269 if Debug_Flag_V then
2270 Write_Overloads (N);
2271 end if;
2272
2273 if Comes_From_Source (N) then
2274 if Is_Fixed_Point_Type (Typ) then
2275 Check_Restriction (No_Fixed_Point, N);
2276
2277 elsif Is_Floating_Point_Type (Typ)
2278 and then Typ /= Universal_Real
2279 and then Typ /= Any_Real
2280 then
2281 Check_Restriction (No_Floating_Point, N);
2282 end if;
2283 end if;
2284
2285 -- Return if already analyzed
2286
2287 if Analyzed (N) then
2288 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2289 Analyze_Dimension (N);
2290 return;
2291
2292 -- Any case of Any_Type as the Etype value means that we had a
2293 -- previous error.
2294
2295 elsif Etype (N) = Any_Type then
2296 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2297 return;
2298 end if;
2299
2300 Check_Parameterless_Call (N);
2301
2302 -- The resolution of an Expression_With_Actions is determined by
2303 -- its Expression, but if the node comes from source it is a
2304 -- Declare_Expression and requires scope management.
2305
2306 if Nkind (N) = N_Expression_With_Actions then
2307 if Comes_From_Source (N) and then N = Original_Node (N) then
2308 Resolve_Declare_Expression (N, Typ);
2309 else
2310 Resolve (Expression (N), Typ);
2311 end if;
2312
2313 Found := True;
2314 Expr_Type := Etype (Expression (N));
2315
2316 -- If not overloaded, then we know the type, and all that needs doing
2317 -- is to check that this type is compatible with the context.
2318
2319 elsif not Is_Overloaded (N) then
2320 Found := Covers (Typ, Etype (N));
2321 Expr_Type := Etype (N);
2322
2323 -- In the overloaded case, we must select the interpretation that
2324 -- is compatible with the context (i.e. the type passed to Resolve)
2325
2326 else
2327 -- Loop through possible interpretations
2328
2329 Get_First_Interp (N, I, It);
2330 Interp_Loop : while Present (It.Typ) loop
2331 if Debug_Flag_V then
2332 Write_Str ("Interp: ");
2333 Write_Interp (It);
2334 end if;
2335
2336 -- We are only interested in interpretations that are compatible
2337 -- with the expected type, any other interpretations are ignored.
2338
2339 if not Covers (Typ, It.Typ) then
2340 if Debug_Flag_V then
2341 Write_Str (" interpretation incompatible with context");
2342 Write_Eol;
2343 end if;
2344
2345 else
2346 -- Skip the current interpretation if it is disabled by an
2347 -- abstract operator. This action is performed only when the
2348 -- type against which we are resolving is the same as the
2349 -- type of the interpretation.
2350
2351 if Ada_Version >= Ada_2005
2352 and then It.Typ = Typ
2353 and then not Is_Universal_Numeric_Type (Typ)
2354 and then Present (It.Abstract_Op)
2355 then
2356 if Debug_Flag_V then
2357 Write_Line ("Skip.");
2358 end if;
2359
2360 goto Continue;
2361 end if;
2362
2363 -- First matching interpretation
2364
2365 if not Found then
2366 Found := True;
2367 I1 := I;
2368 Seen := It.Nam;
2369 Expr_Type := It.Typ;
2370
2371 -- Matching interpretation that is not the first, maybe an
2372 -- error, but there are some cases where preference rules are
2373 -- used to choose between the two possibilities. These and
2374 -- some more obscure cases are handled in Disambiguate.
2375
2376 else
2377 -- If the current statement is part of a predefined library
2378 -- unit, then all interpretations which come from user level
2379 -- packages should not be considered. Check previous and
2380 -- current one.
2381
2382 if From_Lib then
2383 if not Comes_From_Predefined_Lib_Unit (It.Nam) then
2384 goto Continue;
2385
2386 elsif not Comes_From_Predefined_Lib_Unit (Seen) then
2387
2388 -- Previous interpretation must be discarded
2389
2390 I1 := I;
2391 Seen := It.Nam;
2392 Expr_Type := It.Typ;
2393 Set_Entity (N, Seen);
2394 goto Continue;
2395 end if;
2396 end if;
2397
2398 -- Otherwise apply further disambiguation steps
2399
2400 Error_Msg_Sloc := Sloc (Seen);
2401 It1 := Disambiguate (N, I1, I, Typ);
2402
2403 -- Disambiguation has succeeded. Skip the remaining
2404 -- interpretations.
2405
2406 if It1 /= No_Interp then
2407 Seen := It1.Nam;
2408 Expr_Type := It1.Typ;
2409
2410 while Present (It.Typ) loop
2411 Get_Next_Interp (I, It);
2412 end loop;
2413
2414 else
2415 -- Before we issue an ambiguity complaint, check for the
2416 -- case of a subprogram call where at least one of the
2417 -- arguments is Any_Type, and if so suppress the message,
2418 -- since it is a cascaded error. This can also happen for
2419 -- a generalized indexing operation.
2420
2421 if Nkind (N) in N_Subprogram_Call
2422 or else (Nkind (N) = N_Indexed_Component
2423 and then Present (Generalized_Indexing (N)))
2424 then
2425 declare
2426 A : Node_Id;
2427 E : Node_Id;
2428
2429 begin
2430 if Nkind (N) = N_Indexed_Component then
2431 Rewrite (N, Generalized_Indexing (N));
2432 end if;
2433
2434 A := First_Actual (N);
2435 while Present (A) loop
2436 E := A;
2437
2438 if Nkind (E) = N_Parameter_Association then
2439 E := Explicit_Actual_Parameter (E);
2440 end if;
2441
2442 if Etype (E) = Any_Type then
2443 if Debug_Flag_V then
2444 Write_Str ("Any_Type in call");
2445 Write_Eol;
2446 end if;
2447
2448 exit Interp_Loop;
2449 end if;
2450
2451 Next_Actual (A);
2452 end loop;
2453 end;
2454
2455 elsif Nkind (N) in N_Binary_Op
2456 and then (Etype (Left_Opnd (N)) = Any_Type
2457 or else Etype (Right_Opnd (N)) = Any_Type)
2458 then
2459 exit Interp_Loop;
2460
2461 elsif Nkind (N) in N_Unary_Op
2462 and then Etype (Right_Opnd (N)) = Any_Type
2463 then
2464 exit Interp_Loop;
2465 end if;
2466
2467 -- Not that special case, so issue message using the flag
2468 -- Ambiguous to control printing of the header message
2469 -- only at the start of an ambiguous set.
2470
2471 if not Ambiguous then
2472 if Nkind (N) = N_Function_Call
2473 and then Nkind (Name (N)) = N_Explicit_Dereference
2474 then
2475 Error_Msg_N
2476 ("ambiguous expression (cannot resolve indirect "
2477 & "call)!", N);
2478 else
2479 Error_Msg_NE -- CODEFIX
2480 ("ambiguous expression (cannot resolve&)!",
2481 N, It.Nam);
2482 end if;
2483
2484 Ambiguous := True;
2485
2486 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2487 Error_Msg_N
2488 ("\\possible interpretation (inherited)#!", N);
2489 else
2490 Error_Msg_N -- CODEFIX
2491 ("\\possible interpretation#!", N);
2492 end if;
2493
2494 if Nkind (N) in N_Subprogram_Call
2495 and then Present (Parameter_Associations (N))
2496 then
2497 Report_Ambiguous_Argument;
2498 end if;
2499 end if;
2500
2501 Error_Msg_Sloc := Sloc (It.Nam);
2502
2503 -- By default, the error message refers to the candidate
2504 -- interpretation. But if it is a predefined operator, it
2505 -- is implicitly declared at the declaration of the type
2506 -- of the operand. Recover the sloc of that declaration
2507 -- for the error message.
2508
2509 if Nkind (N) in N_Op
2510 and then Scope (It.Nam) = Standard_Standard
2511 and then not Is_Overloaded (Right_Opnd (N))
2512 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2513 Standard_Standard
2514 then
2515 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2516
2517 if Comes_From_Source (Err_Type)
2518 and then Present (Parent (Err_Type))
2519 then
2520 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2521 end if;
2522
2523 elsif Nkind (N) in N_Binary_Op
2524 and then Scope (It.Nam) = Standard_Standard
2525 and then not Is_Overloaded (Left_Opnd (N))
2526 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2527 Standard_Standard
2528 then
2529 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2530
2531 if Comes_From_Source (Err_Type)
2532 and then Present (Parent (Err_Type))
2533 then
2534 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2535 end if;
2536
2537 -- If this is an indirect call, use the subprogram_type
2538 -- in the message, to have a meaningful location. Also
2539 -- indicate if this is an inherited operation, created
2540 -- by a type declaration.
2541
2542 elsif Nkind (N) = N_Function_Call
2543 and then Nkind (Name (N)) = N_Explicit_Dereference
2544 and then Is_Type (It.Nam)
2545 then
2546 Err_Type := It.Nam;
2547 Error_Msg_Sloc :=
2548 Sloc (Associated_Node_For_Itype (Err_Type));
2549 else
2550 Err_Type := Empty;
2551 end if;
2552
2553 if Nkind (N) in N_Op
2554 and then Scope (It.Nam) = Standard_Standard
2555 and then Present (Err_Type)
2556 then
2557 -- Special-case the message for universal_fixed
2558 -- operators, which are not declared with the type
2559 -- of the operand, but appear forever in Standard.
2560
2561 if It.Typ = Universal_Fixed
2562 and then Scope (It.Nam) = Standard_Standard
2563 then
2564 Error_Msg_N
2565 ("\\possible interpretation as universal_fixed "
2566 & "operation (RM 4.5.5 (19))", N);
2567 else
2568 Error_Msg_N
2569 ("\\possible interpretation (predefined)#!", N);
2570 end if;
2571
2572 elsif
2573 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2574 then
2575 Error_Msg_N
2576 ("\\possible interpretation (inherited)#!", N);
2577 else
2578 Error_Msg_N -- CODEFIX
2579 ("\\possible interpretation#!", N);
2580 end if;
2581
2582 end if;
2583 end if;
2584
2585 -- We have a matching interpretation, Expr_Type is the type
2586 -- from this interpretation, and Seen is the entity.
2587
2588 -- For an operator, just set the entity name. The type will be
2589 -- set by the specific operator resolution routine.
2590
2591 if Nkind (N) in N_Op then
2592 Set_Entity (N, Seen);
2593 Generate_Reference (Seen, N);
2594
2595 elsif Nkind (N) in N_Case_Expression
2596 | N_Character_Literal
2597 | N_Delta_Aggregate
2598 | N_If_Expression
2599 then
2600 Set_Etype (N, Expr_Type);
2601
2602 -- AI05-0139-2: Expression is overloaded because type has
2603 -- implicit dereference. The context may be the one that
2604 -- requires implicit dereferemce.
2605
2606 elsif Has_Implicit_Dereference (Expr_Type) then
2607 Set_Etype (N, Expr_Type);
2608 Set_Is_Overloaded (N, False);
2609
2610 -- If the expression is an entity, generate a reference
2611 -- to it, as this is not done for an overloaded construct
2612 -- during analysis.
2613
2614 if Is_Entity_Name (N)
2615 and then Comes_From_Source (N)
2616 then
2617 Generate_Reference (Entity (N), N);
2618
2619 -- Examine access discriminants of entity type,
2620 -- to check whether one of them yields the
2621 -- expected type.
2622
2623 declare
2624 Disc : Entity_Id :=
2625 First_Discriminant (Etype (Entity (N)));
2626
2627 begin
2628 while Present (Disc) loop
2629 exit when Is_Access_Type (Etype (Disc))
2630 and then Has_Implicit_Dereference (Disc)
2631 and then Designated_Type (Etype (Disc)) = Typ;
2632
2633 Next_Discriminant (Disc);
2634 end loop;
2635
2636 if Present (Disc) then
2637 Build_Explicit_Dereference (N, Disc);
2638 end if;
2639 end;
2640 end if;
2641
2642 exit Interp_Loop;
2643
2644 elsif Is_Overloaded (N)
2645 and then Present (It.Nam)
2646 and then Ekind (It.Nam) = E_Discriminant
2647 and then Has_Implicit_Dereference (It.Nam)
2648 then
2649 -- If the node is a general indexing, the dereference is
2650 -- is inserted when resolving the rewritten form, else
2651 -- insert it now.
2652
2653 if Nkind (N) /= N_Indexed_Component
2654 or else No (Generalized_Indexing (N))
2655 then
2656 Build_Explicit_Dereference (N, It.Nam);
2657 end if;
2658
2659 -- For an explicit dereference, attribute reference, range,
2660 -- short-circuit form (which is not an operator node), or call
2661 -- with a name that is an explicit dereference, there is
2662 -- nothing to be done at this point.
2663
2664 elsif Nkind (N) in N_Attribute_Reference
2665 | N_And_Then
2666 | N_Explicit_Dereference
2667 | N_Identifier
2668 | N_Indexed_Component
2669 | N_Or_Else
2670 | N_Range
2671 | N_Selected_Component
2672 | N_Slice
2673 or else Nkind (Name (N)) = N_Explicit_Dereference
2674 then
2675 null;
2676
2677 -- For procedure or function calls, set the type of the name,
2678 -- and also the entity pointer for the prefix.
2679
2680 elsif Nkind (N) in N_Subprogram_Call
2681 and then Is_Entity_Name (Name (N))
2682 then
2683 Set_Etype (Name (N), Expr_Type);
2684 Set_Entity (Name (N), Seen);
2685 Generate_Reference (Seen, Name (N));
2686
2687 elsif Nkind (N) = N_Function_Call
2688 and then Nkind (Name (N)) = N_Selected_Component
2689 then
2690 Set_Etype (Name (N), Expr_Type);
2691 Set_Entity (Selector_Name (Name (N)), Seen);
2692 Generate_Reference (Seen, Selector_Name (Name (N)));
2693
2694 -- For all other cases, just set the type of the Name
2695
2696 else
2697 Set_Etype (Name (N), Expr_Type);
2698 end if;
2699
2700 end if;
2701
2702 <<Continue>>
2703
2704 -- Move to next interpretation
2705
2706 exit Interp_Loop when No (It.Typ);
2707
2708 Get_Next_Interp (I, It);
2709 end loop Interp_Loop;
2710 end if;
2711
2712 -- At this stage Found indicates whether or not an acceptable
2713 -- interpretation exists. If not, then we have an error, except that if
2714 -- the context is Any_Type as a result of some other error, then we
2715 -- suppress the error report.
2716
2717 if not Found then
2718 if Typ /= Any_Type then
2719
2720 -- If type we are looking for is Void, then this is the procedure
2721 -- call case, and the error is simply that what we gave is not a
2722 -- procedure name (we think of procedure calls as expressions with
2723 -- types internally, but the user doesn't think of them this way).
2724
2725 if Typ = Standard_Void_Type then
2726
2727 -- Special case message if function used as a procedure
2728
2729 if Nkind (N) = N_Procedure_Call_Statement
2730 and then Is_Entity_Name (Name (N))
2731 and then Ekind (Entity (Name (N))) = E_Function
2732 then
2733 Error_Msg_NE
2734 ("cannot use call to function & as a statement",
2735 Name (N), Entity (Name (N)));
2736 Error_Msg_N
2737 ("\return value of a function call cannot be ignored",
2738 Name (N));
2739
2740 -- Otherwise give general message (not clear what cases this
2741 -- covers, but no harm in providing for them).
2742
2743 else
2744 Error_Msg_N ("expect procedure name in procedure call", N);
2745 end if;
2746
2747 Found := True;
2748
2749 -- Otherwise we do have a subexpression with the wrong type
2750
2751 -- Check for the case of an allocator which uses an access type
2752 -- instead of the designated type. This is a common error and we
2753 -- specialize the message, posting an error on the operand of the
2754 -- allocator, complaining that we expected the designated type of
2755 -- the allocator.
2756
2757 elsif Nkind (N) = N_Allocator
2758 and then Is_Access_Type (Typ)
2759 and then Is_Access_Type (Etype (N))
2760 and then Designated_Type (Etype (N)) = Typ
2761 then
2762 Wrong_Type (Expression (N), Designated_Type (Typ));
2763 Found := True;
2764
2765 -- Check for view mismatch on Null in instances, for which the
2766 -- view-swapping mechanism has no identifier.
2767
2768 elsif (In_Instance or else In_Inlined_Body)
2769 and then (Nkind (N) = N_Null)
2770 and then Is_Private_Type (Typ)
2771 and then Is_Access_Type (Full_View (Typ))
2772 then
2773 Resolve (N, Full_View (Typ));
2774 Set_Etype (N, Typ);
2775 return;
2776
2777 -- Check for an aggregate. Sometimes we can get bogus aggregates
2778 -- from misuse of parentheses, and we are about to complain about
2779 -- the aggregate without even looking inside it.
2780
2781 -- Instead, if we have an aggregate of type Any_Composite, then
2782 -- analyze and resolve the component fields, and then only issue
2783 -- another message if we get no errors doing this (otherwise
2784 -- assume that the errors in the aggregate caused the problem).
2785
2786 elsif Nkind (N) = N_Aggregate
2787 and then Etype (N) = Any_Composite
2788 then
2789 if Ada_Version >= Ada_2020
2790 and then Has_Aspect (Typ, Aspect_Aggregate)
2791 then
2792 Resolve_Container_Aggregate (N, Typ);
2793
2794 if Expander_Active then
2795 Expand (N);
2796 end if;
2797 return;
2798 end if;
2799
2800 -- Disable expansion in any case. If there is a type mismatch
2801 -- it may be fatal to try to expand the aggregate. The flag
2802 -- would otherwise be set to false when the error is posted.
2803
2804 Expander_Active := False;
2805
2806 declare
2807 procedure Check_Aggr (Aggr : Node_Id);
2808 -- Check one aggregate, and set Found to True if we have a
2809 -- definite error in any of its elements
2810
2811 procedure Check_Elmt (Aelmt : Node_Id);
2812 -- Check one element of aggregate and set Found to True if
2813 -- we definitely have an error in the element.
2814
2815 ----------------
2816 -- Check_Aggr --
2817 ----------------
2818
2819 procedure Check_Aggr (Aggr : Node_Id) is
2820 Elmt : Node_Id;
2821
2822 begin
2823 if Present (Expressions (Aggr)) then
2824 Elmt := First (Expressions (Aggr));
2825 while Present (Elmt) loop
2826 Check_Elmt (Elmt);
2827 Next (Elmt);
2828 end loop;
2829 end if;
2830
2831 if Present (Component_Associations (Aggr)) then
2832 Elmt := First (Component_Associations (Aggr));
2833 while Present (Elmt) loop
2834
2835 -- If this is a default-initialized component, then
2836 -- there is nothing to check. The box will be
2837 -- replaced by the appropriate call during late
2838 -- expansion.
2839
2840 if Nkind (Elmt) /= N_Iterated_Component_Association
2841 and then not Box_Present (Elmt)
2842 then
2843 Check_Elmt (Expression (Elmt));
2844 end if;
2845
2846 Next (Elmt);
2847 end loop;
2848 end if;
2849 end Check_Aggr;
2850
2851 ----------------
2852 -- Check_Elmt --
2853 ----------------
2854
2855 procedure Check_Elmt (Aelmt : Node_Id) is
2856 begin
2857 -- If we have a nested aggregate, go inside it (to
2858 -- attempt a naked analyze-resolve of the aggregate can
2859 -- cause undesirable cascaded errors). Do not resolve
2860 -- expression if it needs a type from context, as for
2861 -- integer * fixed expression.
2862
2863 if Nkind (Aelmt) = N_Aggregate then
2864 Check_Aggr (Aelmt);
2865
2866 else
2867 Analyze (Aelmt);
2868
2869 if not Is_Overloaded (Aelmt)
2870 and then Etype (Aelmt) /= Any_Fixed
2871 then
2872 Resolve (Aelmt);
2873 end if;
2874
2875 if Etype (Aelmt) = Any_Type then
2876 Found := True;
2877 end if;
2878 end if;
2879 end Check_Elmt;
2880
2881 begin
2882 Check_Aggr (N);
2883 end;
2884 end if;
2885
2886 -- Rewrite Literal as a call if the corresponding literal aspect
2887 -- is set.
2888
2889 if (Nkind (N) in N_Numeric_Or_String_Literal
2890 and then
2891 Present
2892 (Find_Aspect (Typ, Literal_Aspect_Map (Nkind (N)))))
2893 or else
2894 (Nkind (N) = N_Identifier
2895 and then Is_Named_Number (Entity (N))
2896 and then
2897 Present
2898 (Find_Aspect
2899 (Typ, Named_Number_Aspect_Map (Ekind (Entity (N))))))
2900 then
2901 declare
2902 Lit_Aspect : constant Aspect_Id :=
2903 (if Nkind (N) = N_Identifier
2904 then Named_Number_Aspect_Map (Ekind (Entity (N)))
2905 else Literal_Aspect_Map (Nkind (N)));
2906
2907 Loc : constant Source_Ptr := Sloc (N);
2908
2909 Callee : Entity_Id :=
2910 Entity (Expression (Find_Aspect (Typ, Lit_Aspect)));
2911
2912 Name : constant Node_Id :=
2913 Make_Identifier (Loc, Chars (Callee));
2914
2915 Param1 : Node_Id;
2916 Param2 : Node_Id;
2917 Params : List_Id;
2918 Call : Node_Id;
2919 Expr : Node_Id;
2920
2921 begin
2922 if Nkind (N) = N_Identifier then
2923 Expr := Expression (Declaration_Node (Entity (N)));
2924
2925 if Ekind (Entity (N)) = E_Named_Integer then
2926 UI_Image (Expr_Value (Expr), Decimal);
2927 Start_String;
2928 Store_String_Chars
2929 (UI_Image_Buffer (1 .. UI_Image_Length));
2930 Param1 := Make_String_Literal (Loc, End_String);
2931 Params := New_List (Param1);
2932
2933 else
2934 UI_Image (Norm_Num (Expr_Value_R (Expr)), Decimal);
2935 Start_String;
2936 Store_String_Chars
2937 (UI_Image_Buffer (1 .. UI_Image_Length));
2938 Param1 := Make_String_Literal (Loc, End_String);
2939
2940 -- Note: Set_Etype is called below on Param1
2941
2942 UI_Image (Norm_Den (Expr_Value_R (Expr)), Decimal);
2943 Start_String;
2944 Store_String_Chars
2945 (UI_Image_Buffer (1 .. UI_Image_Length));
2946 Param2 := Make_String_Literal (Loc, End_String);
2947 Set_Etype (Param2, Standard_String);
2948
2949 Params := New_List (Param1, Param2);
2950
2951 if Present (Related_Expression (Callee)) then
2952 Callee := Related_Expression (Callee);
2953 else
2954 Error_Msg_NE
2955 ("cannot resolve & for a named real", N, Callee);
2956 return;
2957 end if;
2958 end if;
2959
2960 elsif Nkind (N) = N_String_Literal then
2961 Param1 := Make_String_Literal (Loc, Strval (N));
2962 Params := New_List (Param1);
2963 else
2964 Param1 :=
2965 Make_String_Literal
2966 (Loc, String_From_Numeric_Literal (N));
2967 Params := New_List (Param1);
2968 end if;
2969
2970 Call :=
2971 Make_Function_Call
2972 (Sloc => Loc,
2973 Name => Name,
2974 Parameter_Associations => Params);
2975
2976 Set_Entity (Name, Callee);
2977 Set_Is_Overloaded (Name, False);
2978
2979 if Lit_Aspect = Aspect_String_Literal then
2980 Set_Etype (Param1, Standard_Wide_Wide_String);
2981 else
2982 Set_Etype (Param1, Standard_String);
2983 end if;
2984
2985 Set_Etype (Call, Etype (Callee));
2986
2987 -- Conversion needed in case of an inherited aspect
2988 -- of a derived type.
2989 --
2990 -- ??? Need to do something different here for downward
2991 -- tagged conversion case (which is only possible in the
2992 -- case of a null extension); the current call to
2993 -- Convert_To results in an error message about an illegal
2994 -- downward conversion.
2995
2996 Call := Convert_To (Typ, Call);
2997
2998 Rewrite (N, Call);
2999 end;
3000
3001 Analyze_And_Resolve (N, Typ);
3002 return;
3003 end if;
3004
3005 -- Looks like we have a type error, but check for special case
3006 -- of Address wanted, integer found, with the configuration pragma
3007 -- Allow_Integer_Address active. If we have this case, introduce
3008 -- an unchecked conversion to allow the integer expression to be
3009 -- treated as an Address. The reverse case of integer wanted,
3010 -- Address found, is treated in an analogous manner.
3011
3012 if Address_Integer_Convert_OK (Typ, Etype (N)) then
3013 Rewrite (N, Unchecked_Convert_To (Typ, Relocate_Node (N)));
3014 Analyze_And_Resolve (N, Typ);
3015 return;
3016
3017 -- Under relaxed RM semantics silently replace occurrences of null
3018 -- by System.Null_Address.
3019
3020 elsif Null_To_Null_Address_Convert_OK (N, Typ) then
3021 Replace_Null_By_Null_Address (N);
3022 Analyze_And_Resolve (N, Typ);
3023 return;
3024 end if;
3025
3026 -- That special Allow_Integer_Address check did not apply, so we
3027 -- have a real type error. If an error message was issued already,
3028 -- Found got reset to True, so if it's still False, issue standard
3029 -- Wrong_Type message.
3030
3031 if not Found then
3032 if Is_Overloaded (N) and then Nkind (N) = N_Function_Call then
3033 declare
3034 Subp_Name : Node_Id;
3035
3036 begin
3037 if Is_Entity_Name (Name (N)) then
3038 Subp_Name := Name (N);
3039
3040 elsif Nkind (Name (N)) = N_Selected_Component then
3041
3042 -- Protected operation: retrieve operation name
3043
3044 Subp_Name := Selector_Name (Name (N));
3045
3046 else
3047 raise Program_Error;
3048 end if;
3049
3050 Error_Msg_Node_2 := Typ;
3051 Error_Msg_NE
3052 ("no visible interpretation of& matches expected type&",
3053 N, Subp_Name);
3054 end;
3055
3056 if All_Errors_Mode then
3057 declare
3058 Index : Interp_Index;
3059 It : Interp;
3060
3061 begin
3062 Error_Msg_N ("\\possible interpretations:", N);
3063
3064 Get_First_Interp (Name (N), Index, It);
3065 while Present (It.Nam) loop
3066 Error_Msg_Sloc := Sloc (It.Nam);
3067 Error_Msg_Node_2 := It.Nam;
3068 Error_Msg_NE
3069 ("\\ type& for & declared#", N, It.Typ);
3070 Get_Next_Interp (Index, It);
3071 end loop;
3072 end;
3073
3074 else
3075 Error_Msg_N ("\use -gnatf for details", N);
3076 end if;
3077
3078 else
3079 Wrong_Type (N, Typ);
3080 end if;
3081 end if;
3082 end if;
3083
3084 Resolution_Failed;
3085 return;
3086
3087 -- Test if we have more than one interpretation for the context
3088
3089 elsif Ambiguous then
3090 Resolution_Failed;
3091 return;
3092
3093 -- Only one interpretation
3094
3095 else
3096 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
3097 -- the "+" on T is abstract, and the operands are of universal type,
3098 -- the above code will have (incorrectly) resolved the "+" to the
3099 -- universal one in Standard. Therefore check for this case and give
3100 -- an error. We can't do this earlier, because it would cause legal
3101 -- cases to get errors (when some other type has an abstract "+").
3102
3103 if Ada_Version >= Ada_2005
3104 and then Nkind (N) in N_Op
3105 and then Is_Overloaded (N)
3106 and then Is_Universal_Numeric_Type (Etype (Entity (N)))
3107 then
3108 Get_First_Interp (N, I, It);
3109 while Present (It.Typ) loop
3110 if Present (It.Abstract_Op) and then
3111 Etype (It.Abstract_Op) = Typ
3112 then
3113 Error_Msg_NE
3114 ("cannot call abstract subprogram &!", N, It.Abstract_Op);
3115 return;
3116 end if;
3117
3118 Get_Next_Interp (I, It);
3119 end loop;
3120 end if;
3121
3122 -- Here we have an acceptable interpretation for the context
3123
3124 -- Propagate type information and normalize tree for various
3125 -- predefined operations. If the context only imposes a class of
3126 -- types, rather than a specific type, propagate the actual type
3127 -- downward.
3128
3129 if Typ = Any_Integer or else
3130 Typ = Any_Boolean or else
3131 Typ = Any_Modular or else
3132 Typ = Any_Real or else
3133 Typ = Any_Discrete
3134 then
3135 Ctx_Type := Expr_Type;
3136
3137 -- Any_Fixed is legal in a real context only if a specific fixed-
3138 -- point type is imposed. If Norman Cohen can be confused by this,
3139 -- it deserves a separate message.
3140
3141 if Typ = Any_Real
3142 and then Expr_Type = Any_Fixed
3143 then
3144 Error_Msg_N ("illegal context for mixed mode operation", N);
3145 Set_Etype (N, Universal_Real);
3146 Ctx_Type := Universal_Real;
3147 end if;
3148 end if;
3149
3150 -- A user-defined operator is transformed into a function call at
3151 -- this point, so that further processing knows that operators are
3152 -- really operators (i.e. are predefined operators). User-defined
3153 -- operators that are intrinsic are just renamings of the predefined
3154 -- ones, and need not be turned into calls either, but if they rename
3155 -- a different operator, we must transform the node accordingly.
3156 -- Instantiations of Unchecked_Conversion are intrinsic but are
3157 -- treated as functions, even if given an operator designator.
3158
3159 if Nkind (N) in N_Op
3160 and then Present (Entity (N))
3161 and then Ekind (Entity (N)) /= E_Operator
3162 then
3163 if not Is_Predefined_Op (Entity (N)) then
3164 Rewrite_Operator_As_Call (N, Entity (N));
3165
3166 elsif Present (Alias (Entity (N)))
3167 and then
3168 Nkind (Parent (Parent (Entity (N)))) =
3169 N_Subprogram_Renaming_Declaration
3170 then
3171 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
3172
3173 -- If the node is rewritten, it will be fully resolved in
3174 -- Rewrite_Renamed_Operator.
3175
3176 if Analyzed (N) then
3177 return;
3178 end if;
3179 end if;
3180 end if;
3181
3182 case N_Subexpr'(Nkind (N)) is
3183 when N_Aggregate =>
3184 Resolve_Aggregate (N, Ctx_Type);
3185
3186 when N_Allocator =>
3187 Resolve_Allocator (N, Ctx_Type);
3188
3189 when N_Short_Circuit =>
3190 Resolve_Short_Circuit (N, Ctx_Type);
3191
3192 when N_Attribute_Reference =>
3193 Resolve_Attribute (N, Ctx_Type);
3194
3195 when N_Case_Expression =>
3196 Resolve_Case_Expression (N, Ctx_Type);
3197
3198 when N_Character_Literal =>
3199 Resolve_Character_Literal (N, Ctx_Type);
3200
3201 when N_Delta_Aggregate =>
3202 Resolve_Delta_Aggregate (N, Ctx_Type);
3203
3204 when N_Expanded_Name =>
3205 Resolve_Entity_Name (N, Ctx_Type);
3206
3207 when N_Explicit_Dereference =>
3208 Resolve_Explicit_Dereference (N, Ctx_Type);
3209
3210 when N_Expression_With_Actions =>
3211 Resolve_Expression_With_Actions (N, Ctx_Type);
3212
3213 when N_Extension_Aggregate =>
3214 Resolve_Extension_Aggregate (N, Ctx_Type);
3215
3216 when N_Function_Call =>
3217 Resolve_Call (N, Ctx_Type);
3218
3219 when N_Identifier =>
3220 Resolve_Entity_Name (N, Ctx_Type);
3221
3222 when N_If_Expression =>
3223 Resolve_If_Expression (N, Ctx_Type);
3224
3225 when N_Indexed_Component =>
3226 Resolve_Indexed_Component (N, Ctx_Type);
3227
3228 when N_Integer_Literal =>
3229 Resolve_Integer_Literal (N, Ctx_Type);
3230
3231 when N_Membership_Test =>
3232 Resolve_Membership_Op (N, Ctx_Type);
3233
3234 when N_Null =>
3235 Resolve_Null (N, Ctx_Type);
3236
3237 when N_Op_And
3238 | N_Op_Or
3239 | N_Op_Xor
3240 =>
3241 Resolve_Logical_Op (N, Ctx_Type);
3242
3243 when N_Op_Eq
3244 | N_Op_Ne
3245 =>
3246 Resolve_Equality_Op (N, Ctx_Type);
3247
3248 when N_Op_Ge
3249 | N_Op_Gt
3250 | N_Op_Le
3251 | N_Op_Lt
3252 =>
3253 Resolve_Comparison_Op (N, Ctx_Type);
3254
3255 when N_Op_Not =>
3256 Resolve_Op_Not (N, Ctx_Type);
3257
3258 when N_Op_Add
3259 | N_Op_Divide
3260 | N_Op_Mod
3261 | N_Op_Multiply
3262 | N_Op_Rem
3263 | N_Op_Subtract
3264 =>
3265 Resolve_Arithmetic_Op (N, Ctx_Type);
3266
3267 when N_Op_Concat =>
3268 Resolve_Op_Concat (N, Ctx_Type);
3269
3270 when N_Op_Expon =>
3271 Resolve_Op_Expon (N, Ctx_Type);
3272
3273 when N_Op_Abs
3274 | N_Op_Minus
3275 | N_Op_Plus
3276 =>
3277 Resolve_Unary_Op (N, Ctx_Type);
3278
3279 when N_Op_Shift =>
3280 Resolve_Shift (N, Ctx_Type);
3281
3282 when N_Procedure_Call_Statement =>
3283 Resolve_Call (N, Ctx_Type);
3284
3285 when N_Operator_Symbol =>
3286 Resolve_Operator_Symbol (N, Ctx_Type);
3287
3288 when N_Qualified_Expression =>
3289 Resolve_Qualified_Expression (N, Ctx_Type);
3290
3291 -- Why is the following null, needs a comment ???
3292
3293 when N_Quantified_Expression =>
3294 null;
3295
3296 when N_Raise_Expression =>
3297 Resolve_Raise_Expression (N, Ctx_Type);
3298
3299 when N_Raise_xxx_Error =>
3300 Set_Etype (N, Ctx_Type);
3301
3302 when N_Range =>
3303 Resolve_Range (N, Ctx_Type);
3304
3305 when N_Real_Literal =>
3306 Resolve_Real_Literal (N, Ctx_Type);
3307
3308 when N_Reference =>
3309 Resolve_Reference (N, Ctx_Type);
3310
3311 when N_Selected_Component =>
3312 Resolve_Selected_Component (N, Ctx_Type);
3313
3314 when N_Slice =>
3315 Resolve_Slice (N, Ctx_Type);
3316
3317 when N_String_Literal =>
3318 Resolve_String_Literal (N, Ctx_Type);
3319
3320 when N_Target_Name =>
3321 Resolve_Target_Name (N, Ctx_Type);
3322
3323 when N_Type_Conversion =>
3324 Resolve_Type_Conversion (N, Ctx_Type);
3325
3326 when N_Unchecked_Expression =>
3327 Resolve_Unchecked_Expression (N, Ctx_Type);
3328
3329 when N_Unchecked_Type_Conversion =>
3330 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
3331 end case;
3332
3333 -- Mark relevant use-type and use-package clauses as effective using
3334 -- the original node because constant folding may have occured and
3335 -- removed references that need to be examined.
3336
3337 if Nkind (Original_Node (N)) in N_Op then
3338 Mark_Use_Clauses (Original_Node (N));
3339 end if;
3340
3341 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
3342 -- expression of an anonymous access type that occurs in the context
3343 -- of a named general access type, except when the expression is that
3344 -- of a membership test. This ensures proper legality checking in
3345 -- terms of allowed conversions (expressions that would be illegal to
3346 -- convert implicitly are allowed in membership tests).
3347
3348 if Ada_Version >= Ada_2012
3349 and then Ekind (Base_Type (Ctx_Type)) = E_General_Access_Type
3350 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
3351 and then Nkind (Parent (N)) not in N_Membership_Test
3352 then
3353 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
3354 Analyze_And_Resolve (N, Ctx_Type);
3355 end if;
3356
3357 -- If the subexpression was replaced by a non-subexpression, then
3358 -- all we do is to expand it. The only legitimate case we know of
3359 -- is converting procedure call statement to entry call statements,
3360 -- but there may be others, so we are making this test general.
3361
3362 if Nkind (N) not in N_Subexpr then
3363 Debug_A_Exit ("resolving ", N, " (done)");
3364 Expand (N);
3365 return;
3366 end if;
3367
3368 -- The expression is definitely NOT overloaded at this point, so
3369 -- we reset the Is_Overloaded flag to avoid any confusion when
3370 -- reanalyzing the node.
3371
3372 Set_Is_Overloaded (N, False);
3373
3374 -- Freeze expression type, entity if it is a name, and designated
3375 -- type if it is an allocator (RM 13.14(10,11,13)).
3376
3377 -- Now that the resolution of the type of the node is complete, and
3378 -- we did not detect an error, we can expand this node. We skip the
3379 -- expand call if we are in a default expression, see section
3380 -- "Handling of Default Expressions" in Sem spec.
3381
3382 Debug_A_Exit ("resolving ", N, " (done)");
3383
3384 -- We unconditionally freeze the expression, even if we are in
3385 -- default expression mode (the Freeze_Expression routine tests this
3386 -- flag and only freezes static types if it is set).
3387
3388 -- Ada 2012 (AI05-177): The declaration of an expression function
3389 -- does not cause freezing, but we never reach here in that case.
3390 -- Here we are resolving the corresponding expanded body, so we do
3391 -- need to perform normal freezing.
3392
3393 -- As elsewhere we do not emit freeze node within a generic. We make
3394 -- an exception for entities that are expressions, only to detect
3395 -- misuses of deferred constants and preserve the output of various
3396 -- tests.
3397
3398 if not Inside_A_Generic or else Is_Entity_Name (N) then
3399 Freeze_Expression (N);
3400 end if;
3401
3402 -- Now we can do the expansion
3403
3404 Expand (N);
3405 end if;
3406 end Resolve;
3407
3408 -------------
3409 -- Resolve --
3410 -------------
3411
3412 -- Version with check(s) suppressed
3413
3414 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
3415 begin
3416 if Suppress = All_Checks then
3417 declare
3418 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
3419 begin
3420 Scope_Suppress.Suppress := (others => True);
3421 Resolve (N, Typ);
3422 Scope_Suppress.Suppress := Sva;
3423 end;
3424
3425 else
3426 declare
3427 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
3428 begin
3429 Scope_Suppress.Suppress (Suppress) := True;
3430 Resolve (N, Typ);
3431 Scope_Suppress.Suppress (Suppress) := Svg;
3432 end;
3433 end if;
3434 end Resolve;
3435
3436 -------------
3437 -- Resolve --
3438 -------------
3439
3440 -- Version with implicit type
3441
3442 procedure Resolve (N : Node_Id) is
3443 begin
3444 Resolve (N, Etype (N));
3445 end Resolve;
3446
3447 ---------------------
3448 -- Resolve_Actuals --
3449 ---------------------
3450
3451 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
3452 Loc : constant Source_Ptr := Sloc (N);
3453 A : Node_Id;
3454 A_Id : Entity_Id;
3455 A_Typ : Entity_Id := Empty; -- init to avoid warning
3456 F : Entity_Id;
3457 F_Typ : Entity_Id;
3458 Prev : Node_Id := Empty;
3459 Orig_A : Node_Id;
3460 Real_F : Entity_Id := Empty; -- init to avoid warning
3461
3462 Real_Subp : Entity_Id;
3463 -- If the subprogram being called is an inherited operation for
3464 -- a formal derived type in an instance, Real_Subp is the subprogram
3465 -- that will be called. It may have different formal names than the
3466 -- operation of the formal in the generic, so after actual is resolved
3467 -- the name of the actual in a named association must carry the name
3468 -- of the actual of the subprogram being called.
3469
3470 procedure Check_Aliased_Parameter;
3471 -- Check rules on aliased parameters and related accessibility rules
3472 -- in (RM 3.10.2 (10.2-10.4)).
3473
3474 procedure Check_Argument_Order;
3475 -- Performs a check for the case where the actuals are all simple
3476 -- identifiers that correspond to the formal names, but in the wrong
3477 -- order, which is considered suspicious and cause for a warning.
3478
3479 procedure Check_Prefixed_Call;
3480 -- If the original node is an overloaded call in prefix notation,
3481 -- insert an 'Access or a dereference as needed over the first actual.
3482 -- Try_Object_Operation has already verified that there is a valid
3483 -- interpretation, but the form of the actual can only be determined
3484 -- once the primitive operation is identified.
3485
3486 procedure Flag_Effectively_Volatile_Objects (Expr : Node_Id);
3487 -- Emit an error concerning the illegal usage of an effectively volatile
3488 -- object for reading in interfering context (SPARK RM 7.1.3(10)).
3489
3490 procedure Insert_Default;
3491 -- If the actual is missing in a call, insert in the actuals list
3492 -- an instance of the default expression. The insertion is always
3493 -- a named association.
3494
3495 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
3496 -- Check whether T1 and T2, or their full views, are derived from a
3497 -- common type. Used to enforce the restrictions on array conversions
3498 -- of AI95-00246.
3499
3500 function Static_Concatenation (N : Node_Id) return Boolean;
3501 -- Predicate to determine whether an actual that is a concatenation
3502 -- will be evaluated statically and does not need a transient scope.
3503 -- This must be determined before the actual is resolved and expanded
3504 -- because if needed the transient scope must be introduced earlier.
3505
3506 -----------------------------
3507 -- Check_Aliased_Parameter --
3508 -----------------------------
3509
3510 procedure Check_Aliased_Parameter is
3511 Nominal_Subt : Entity_Id;
3512
3513 begin
3514 if Is_Aliased (F) then
3515 if Is_Tagged_Type (A_Typ) then
3516 null;
3517
3518 elsif Is_Aliased_View (A) then
3519 if Is_Constr_Subt_For_U_Nominal (A_Typ) then
3520 Nominal_Subt := Base_Type (A_Typ);
3521 else
3522 Nominal_Subt := A_Typ;
3523 end if;
3524
3525 if Subtypes_Statically_Match (F_Typ, Nominal_Subt) then
3526 null;
3527
3528 -- In a generic body assume the worst for generic formals:
3529 -- they can have a constrained partial view (AI05-041).
3530
3531 elsif Has_Discriminants (F_Typ)
3532 and then not Is_Constrained (F_Typ)
3533 and then not Object_Type_Has_Constrained_Partial_View
3534 (Typ => F_Typ, Scop => Current_Scope)
3535 then
3536 null;
3537
3538 else
3539 Error_Msg_NE ("untagged actual does not statically match "
3540 & "aliased formal&", A, F);
3541 end if;
3542
3543 else
3544 Error_Msg_NE ("actual for aliased formal& must be "
3545 & "aliased object", A, F);
3546 end if;
3547
3548 if Ekind (Nam) = E_Procedure then
3549 null;
3550
3551 elsif Ekind (Etype (Nam)) = E_Anonymous_Access_Type then
3552 if Nkind (Parent (N)) = N_Type_Conversion
3553 and then Type_Access_Level (Etype (Parent (N)))
3554 < Static_Accessibility_Level (A, Object_Decl_Level)
3555 then
3556 Error_Msg_N ("aliased actual has wrong accessibility", A);
3557 end if;
3558
3559 elsif Nkind (Parent (N)) = N_Qualified_Expression
3560 and then Nkind (Parent (Parent (N))) = N_Allocator
3561 and then Type_Access_Level (Etype (Parent (Parent (N))))
3562 < Static_Accessibility_Level (A, Object_Decl_Level)
3563 then
3564 Error_Msg_N
3565 ("aliased actual in allocator has wrong accessibility", A);
3566 end if;
3567 end if;
3568 end Check_Aliased_Parameter;
3569
3570 --------------------------
3571 -- Check_Argument_Order --
3572 --------------------------
3573
3574 procedure Check_Argument_Order is
3575 begin
3576 -- Nothing to do if no parameters, or original node is neither a
3577 -- function call nor a procedure call statement (happens in the
3578 -- operator-transformed-to-function call case), or the call is to an
3579 -- operator symbol (which is usually in infix form), or the call does
3580 -- not come from source, or this warning is off.
3581
3582 if not Warn_On_Parameter_Order
3583 or else No (Parameter_Associations (N))
3584 or else Nkind (Original_Node (N)) not in N_Subprogram_Call
3585 or else (Nkind (Name (N)) = N_Identifier
3586 and then Present (Entity (Name (N)))
3587 and then Nkind (Entity (Name (N))) =
3588 N_Defining_Operator_Symbol)
3589 or else not Comes_From_Source (N)
3590 then
3591 return;
3592 end if;
3593
3594 declare
3595 Nargs : constant Nat := List_Length (Parameter_Associations (N));
3596
3597 begin
3598 -- Nothing to do if only one parameter
3599
3600 if Nargs < 2 then
3601 return;
3602 end if;
3603
3604 -- Here if at least two arguments
3605
3606 declare
3607 Actuals : array (1 .. Nargs) of Node_Id;
3608 Actual : Node_Id;
3609 Formal : Node_Id;
3610
3611 Wrong_Order : Boolean := False;
3612 -- Set True if an out of order case is found
3613
3614 begin
3615 -- Collect identifier names of actuals, fail if any actual is
3616 -- not a simple identifier, and record max length of name.
3617
3618 Actual := First (Parameter_Associations (N));
3619 for J in Actuals'Range loop
3620 if Nkind (Actual) /= N_Identifier then
3621 return;
3622 else
3623 Actuals (J) := Actual;
3624 Next (Actual);
3625 end if;
3626 end loop;
3627
3628 -- If we got this far, all actuals are identifiers and the list
3629 -- of their names is stored in the Actuals array.
3630
3631 Formal := First_Formal (Nam);
3632 for J in Actuals'Range loop
3633
3634 -- If we ran out of formals, that's odd, probably an error
3635 -- which will be detected elsewhere, but abandon the search.
3636
3637 if No (Formal) then
3638 return;
3639 end if;
3640
3641 -- If name matches and is in order OK
3642
3643 if Chars (Formal) = Chars (Actuals (J)) then
3644 null;
3645
3646 else
3647 -- If no match, see if it is elsewhere in list and if so
3648 -- flag potential wrong order if type is compatible.
3649
3650 for K in Actuals'Range loop
3651 if Chars (Formal) = Chars (Actuals (K))
3652 and then
3653 Has_Compatible_Type (Actuals (K), Etype (Formal))
3654 then
3655 Wrong_Order := True;
3656 goto Continue;
3657 end if;
3658 end loop;
3659
3660 -- No match
3661
3662 return;
3663 end if;
3664
3665 <<Continue>> Next_Formal (Formal);
3666 end loop;
3667
3668 -- If Formals left over, also probably an error, skip warning
3669
3670 if Present (Formal) then
3671 return;
3672 end if;
3673
3674 -- Here we give the warning if something was out of order
3675
3676 if Wrong_Order then
3677 Error_Msg_N
3678 ("?P?actuals for this call may be in wrong order", N);
3679 end if;
3680 end;
3681 end;
3682 end Check_Argument_Order;
3683
3684 -------------------------
3685 -- Check_Prefixed_Call --
3686 -------------------------
3687
3688 procedure Check_Prefixed_Call is
3689 Act : constant Node_Id := First_Actual (N);
3690 A_Type : constant Entity_Id := Etype (Act);
3691 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3692 Orig : constant Node_Id := Original_Node (N);
3693 New_A : Node_Id;
3694
3695 begin
3696 -- Check whether the call is a prefixed call, with or without
3697 -- additional actuals.
3698
3699 if Nkind (Orig) = N_Selected_Component
3700 or else
3701 (Nkind (Orig) = N_Indexed_Component
3702 and then Nkind (Prefix (Orig)) = N_Selected_Component
3703 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3704 and then Is_Entity_Name (Act)
3705 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3706 then
3707 if Is_Access_Type (A_Type)
3708 and then not Is_Access_Type (F_Type)
3709 then
3710 -- Introduce dereference on object in prefix
3711
3712 New_A :=
3713 Make_Explicit_Dereference (Sloc (Act),
3714 Prefix => Relocate_Node (Act));
3715 Rewrite (Act, New_A);
3716 Analyze (Act);
3717
3718 elsif Is_Access_Type (F_Type)
3719 and then not Is_Access_Type (A_Type)
3720 then
3721 -- Introduce an implicit 'Access in prefix
3722
3723 if not Is_Aliased_View (Act) then
3724 Error_Msg_NE
3725 ("object in prefixed call to& must be aliased "
3726 & "(RM 4.1.3 (13 1/2))",
3727 Prefix (Act), Nam);
3728 end if;
3729
3730 Rewrite (Act,
3731 Make_Attribute_Reference (Loc,
3732 Attribute_Name => Name_Access,
3733 Prefix => Relocate_Node (Act)));
3734 end if;
3735
3736 Analyze (Act);
3737 end if;
3738 end Check_Prefixed_Call;
3739
3740 ---------------------------------------
3741 -- Flag_Effectively_Volatile_Objects --
3742 ---------------------------------------
3743
3744 procedure Flag_Effectively_Volatile_Objects (Expr : Node_Id) is
3745 function Flag_Object (N : Node_Id) return Traverse_Result;
3746 -- Determine whether arbitrary node N denotes an effectively volatile
3747 -- object for reading and if it does, emit an error.
3748
3749 -----------------
3750 -- Flag_Object --
3751 -----------------
3752
3753 function Flag_Object (N : Node_Id) return Traverse_Result is
3754 Id : Entity_Id;
3755
3756 begin
3757 case Nkind (N) is
3758
3759 -- Do not consider object name appearing in the prefix of
3760 -- attribute Address as a read.
3761
3762 when N_Attribute_Reference =>
3763
3764 -- Prefix of attribute Address denotes an object, program
3765 -- unit, or label; none of them needs to be flagged here.
3766
3767 if Attribute_Name (N) = Name_Address then
3768 return Skip;
3769 end if;
3770
3771 -- Do not consider nested function calls because they have
3772 -- already been processed during their own resolution.
3773
3774 when N_Function_Call =>
3775 return Skip;
3776
3777 when N_Identifier | N_Expanded_Name =>
3778 Id := Entity (N);
3779
3780 if Present (Id)
3781 and then Is_Object (Id)
3782 and then Is_Effectively_Volatile_For_Reading (Id)
3783 then
3784 Error_Msg_N
3785 ("volatile object cannot appear in this context"
3786 & " (SPARK RM 7.1.3(10))", N);
3787 end if;
3788
3789 return Skip;
3790
3791 when others =>
3792 null;
3793 end case;
3794
3795 return OK;
3796 end Flag_Object;
3797
3798 procedure Flag_Objects is new Traverse_Proc (Flag_Object);
3799
3800 -- Start of processing for Flag_Effectively_Volatile_Objects
3801
3802 begin
3803 Flag_Objects (Expr);
3804 end Flag_Effectively_Volatile_Objects;
3805
3806 --------------------
3807 -- Insert_Default --
3808 --------------------
3809
3810 procedure Insert_Default is
3811 Actval : Node_Id;
3812 Assoc : Node_Id;
3813
3814 begin
3815 -- Missing argument in call, nothing to insert
3816
3817 if No (Default_Value (F)) then
3818 return;
3819
3820 else
3821 -- Note that we do a full New_Copy_Tree, so that any associated
3822 -- Itypes are properly copied. This may not be needed any more,
3823 -- but it does no harm as a safety measure. Defaults of a generic
3824 -- formal may be out of bounds of the corresponding actual (see
3825 -- cc1311b) and an additional check may be required.
3826
3827 Actval :=
3828 New_Copy_Tree
3829 (Default_Value (F),
3830 New_Scope => Current_Scope,
3831 New_Sloc => Loc);
3832
3833 -- Propagate dimension information, if any.
3834
3835 Copy_Dimensions (Default_Value (F), Actval);
3836
3837 if Is_Concurrent_Type (Scope (Nam))
3838 and then Has_Discriminants (Scope (Nam))
3839 then
3840 Replace_Actual_Discriminants (N, Actval);
3841 end if;
3842
3843 if Is_Overloadable (Nam)
3844 and then Present (Alias (Nam))
3845 then
3846 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3847 and then not Is_Tagged_Type (Etype (F))
3848 then
3849 -- If default is a real literal, do not introduce a
3850 -- conversion whose effect may depend on the run-time
3851 -- size of universal real.
3852
3853 if Nkind (Actval) = N_Real_Literal then
3854 Set_Etype (Actval, Base_Type (Etype (F)));
3855 else
3856 Actval := Unchecked_Convert_To (Etype (F), Actval);
3857 end if;
3858 end if;
3859
3860 if Is_Scalar_Type (Etype (F)) then
3861 Enable_Range_Check (Actval);
3862 end if;
3863
3864 Set_Parent (Actval, N);
3865
3866 -- Resolve aggregates with their base type, to avoid scope
3867 -- anomalies: the subtype was first built in the subprogram
3868 -- declaration, and the current call may be nested.
3869
3870 if Nkind (Actval) = N_Aggregate then
3871 Analyze_And_Resolve (Actval, Etype (F));
3872 else
3873 Analyze_And_Resolve (Actval, Etype (Actval));
3874 end if;
3875
3876 else
3877 Set_Parent (Actval, N);
3878
3879 -- See note above concerning aggregates
3880
3881 if Nkind (Actval) = N_Aggregate
3882 and then Has_Discriminants (Etype (Actval))
3883 then
3884 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3885
3886 -- Resolve entities with their own type, which may differ from
3887 -- the type of a reference in a generic context (the view
3888 -- swapping mechanism did not anticipate the re-analysis of
3889 -- default values in calls).
3890
3891 elsif Is_Entity_Name (Actval) then
3892 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3893
3894 else
3895 Analyze_And_Resolve (Actval, Etype (Actval));
3896 end if;
3897 end if;
3898
3899 -- If default is a tag indeterminate function call, propagate tag
3900 -- to obtain proper dispatching.
3901
3902 if Is_Controlling_Formal (F)
3903 and then Nkind (Default_Value (F)) = N_Function_Call
3904 then
3905 Set_Is_Controlling_Actual (Actval);
3906 end if;
3907 end if;
3908
3909 -- If the default expression raises constraint error, then just
3910 -- silently replace it with an N_Raise_Constraint_Error node, since
3911 -- we already gave the warning on the subprogram spec. If node is
3912 -- already a Raise_Constraint_Error leave as is, to prevent loops in
3913 -- the warnings removal machinery.
3914
3915 if Raises_Constraint_Error (Actval)
3916 and then Nkind (Actval) /= N_Raise_Constraint_Error
3917 then
3918 Rewrite (Actval,
3919 Make_Raise_Constraint_Error (Loc,
3920 Reason => CE_Range_Check_Failed));
3921
3922 Set_Raises_Constraint_Error (Actval);
3923 Set_Etype (Actval, Etype (F));
3924 end if;
3925
3926 Assoc :=
3927 Make_Parameter_Association (Loc,
3928 Explicit_Actual_Parameter => Actval,
3929 Selector_Name => Make_Identifier (Loc, Chars (F)));
3930
3931 -- Case of insertion is first named actual
3932
3933 if No (Prev)
3934 or else Nkind (Parent (Prev)) /= N_Parameter_Association
3935 then
3936 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3937 Set_First_Named_Actual (N, Actval);
3938
3939 if No (Prev) then
3940 if No (Parameter_Associations (N)) then
3941 Set_Parameter_Associations (N, New_List (Assoc));
3942 else
3943 Append (Assoc, Parameter_Associations (N));
3944 end if;
3945
3946 else
3947 Insert_After (Prev, Assoc);
3948 end if;
3949
3950 -- Case of insertion is not first named actual
3951
3952 else
3953 Set_Next_Named_Actual
3954 (Assoc, Next_Named_Actual (Parent (Prev)));
3955 Set_Next_Named_Actual (Parent (Prev), Actval);
3956 Append (Assoc, Parameter_Associations (N));
3957 end if;
3958
3959 Mark_Rewrite_Insertion (Assoc);
3960 Mark_Rewrite_Insertion (Actval);
3961
3962 Prev := Actval;
3963 end Insert_Default;
3964
3965 -------------------
3966 -- Same_Ancestor --
3967 -------------------
3968
3969 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3970 FT1 : Entity_Id := T1;
3971 FT2 : Entity_Id := T2;
3972
3973 begin
3974 if Is_Private_Type (T1)
3975 and then Present (Full_View (T1))
3976 then
3977 FT1 := Full_View (T1);
3978 end if;
3979
3980 if Is_Private_Type (T2)
3981 and then Present (Full_View (T2))
3982 then
3983 FT2 := Full_View (T2);
3984 end if;
3985
3986 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3987 end Same_Ancestor;
3988
3989 --------------------------
3990 -- Static_Concatenation --
3991 --------------------------
3992
3993 function Static_Concatenation (N : Node_Id) return Boolean is
3994 begin
3995 case Nkind (N) is
3996 when N_String_Literal =>
3997 return True;
3998
3999 when N_Op_Concat =>
4000
4001 -- Concatenation is static when both operands are static and
4002 -- the concatenation operator is a predefined one.
4003
4004 return Scope (Entity (N)) = Standard_Standard
4005 and then
4006 Static_Concatenation (Left_Opnd (N))
4007 and then
4008 Static_Concatenation (Right_Opnd (N));
4009
4010 when others =>
4011 if Is_Entity_Name (N) then
4012 declare
4013 Ent : constant Entity_Id := Entity (N);
4014 begin
4015 return Ekind (Ent) = E_Constant
4016 and then Present (Constant_Value (Ent))
4017 and then
4018 Is_OK_Static_Expression (Constant_Value (Ent));
4019 end;
4020
4021 else
4022 return False;
4023 end if;
4024 end case;
4025 end Static_Concatenation;
4026
4027 -- Start of processing for Resolve_Actuals
4028
4029 begin
4030 Check_Argument_Order;
4031
4032 if Is_Overloadable (Nam)
4033 and then Is_Inherited_Operation (Nam)
4034 and then In_Instance
4035 and then Present (Alias (Nam))
4036 and then Present (Overridden_Operation (Alias (Nam)))
4037 then
4038 Real_Subp := Alias (Nam);
4039 else
4040 Real_Subp := Empty;
4041 end if;
4042
4043 if Present (First_Actual (N)) then
4044 Check_Prefixed_Call;
4045 end if;
4046
4047 A := First_Actual (N);
4048 F := First_Formal (Nam);
4049
4050 if Present (Real_Subp) then
4051 Real_F := First_Formal (Real_Subp);
4052 end if;
4053
4054 while Present (F) loop
4055 if No (A) and then Needs_No_Actuals (Nam) then
4056 null;
4057
4058 -- If we have an error in any actual or formal, indicated by a type
4059 -- of Any_Type, then abandon resolution attempt, and set result type
4060 -- to Any_Type. Skip this if the actual is a Raise_Expression, whose
4061 -- type is imposed from context.
4062
4063 elsif (Present (A) and then Etype (A) = Any_Type)
4064 or else Etype (F) = Any_Type
4065 then
4066 if Nkind (A) /= N_Raise_Expression then
4067 Set_Etype (N, Any_Type);
4068 return;
4069 end if;
4070 end if;
4071
4072 -- Case where actual is present
4073
4074 -- If the actual is an entity, generate a reference to it now. We
4075 -- do this before the actual is resolved, because a formal of some
4076 -- protected subprogram, or a task discriminant, will be rewritten
4077 -- during expansion, and the source entity reference may be lost.
4078
4079 if Present (A)
4080 and then Is_Entity_Name (A)
4081 and then Comes_From_Source (A)
4082 then
4083 -- Annotate the tree by creating a variable reference marker when
4084 -- the actual denotes a variable reference, in case the reference
4085 -- is folded or optimized away. The variable reference marker is
4086 -- automatically saved for later examination by the ABE Processing
4087 -- phase. The status of the reference is set as follows:
4088
4089 -- status mode
4090 -- read IN, IN OUT
4091 -- write IN OUT, OUT
4092
4093 if Needs_Variable_Reference_Marker
4094 (N => A,
4095 Calls_OK => True)
4096 then
4097 Build_Variable_Reference_Marker
4098 (N => A,
4099 Read => Ekind (F) /= E_Out_Parameter,
4100 Write => Ekind (F) /= E_In_Parameter);
4101 end if;
4102
4103 Orig_A := Entity (A);
4104
4105 if Present (Orig_A) then
4106 if Is_Formal (Orig_A)
4107 and then Ekind (F) /= E_In_Parameter
4108 then
4109 Generate_Reference (Orig_A, A, 'm');
4110
4111 elsif not Is_Overloaded (A) then
4112 if Ekind (F) /= E_Out_Parameter then
4113 Generate_Reference (Orig_A, A);
4114
4115 -- RM 6.4.1(12): For an out parameter that is passed by
4116 -- copy, the formal parameter object is created, and:
4117
4118 -- * For an access type, the formal parameter is initialized
4119 -- from the value of the actual, without checking that the
4120 -- value satisfies any constraint, any predicate, or any
4121 -- exclusion of the null value.
4122
4123 -- * For a scalar type that has the Default_Value aspect
4124 -- specified, the formal parameter is initialized from the
4125 -- value of the actual, without checking that the value
4126 -- satisfies any constraint or any predicate.
4127 -- I do not understand why this case is included??? this is
4128 -- not a case where an OUT parameter is treated as IN OUT.
4129
4130 -- * For a composite type with discriminants or that has
4131 -- implicit initial values for any subcomponents, the
4132 -- behavior is as for an in out parameter passed by copy.
4133
4134 -- Hence for these cases we generate the read reference now
4135 -- (the write reference will be generated later by
4136 -- Note_Possible_Modification).
4137
4138 elsif Is_By_Copy_Type (Etype (F))
4139 and then
4140 (Is_Access_Type (Etype (F))
4141 or else
4142 (Is_Scalar_Type (Etype (F))
4143 and then
4144 Present (Default_Aspect_Value (Etype (F))))
4145 or else
4146 (Is_Composite_Type (Etype (F))
4147 and then (Has_Discriminants (Etype (F))
4148 or else Is_Partially_Initialized_Type
4149 (Etype (F)))))
4150 then
4151 Generate_Reference (Orig_A, A);
4152 end if;
4153 end if;
4154 end if;
4155 end if;
4156
4157 if Present (A)
4158 and then (Nkind (Parent (A)) /= N_Parameter_Association
4159 or else Chars (Selector_Name (Parent (A))) = Chars (F))
4160 then
4161 -- If style checking mode on, check match of formal name
4162
4163 if Style_Check then
4164 if Nkind (Parent (A)) = N_Parameter_Association then
4165 Check_Identifier (Selector_Name (Parent (A)), F);
4166 end if;
4167 end if;
4168
4169 -- If the formal is Out or In_Out, do not resolve and expand the
4170 -- conversion, because it is subsequently expanded into explicit
4171 -- temporaries and assignments. However, the object of the
4172 -- conversion can be resolved. An exception is the case of tagged
4173 -- type conversion with a class-wide actual. In that case we want
4174 -- the tag check to occur and no temporary will be needed (no
4175 -- representation change can occur) and the parameter is passed by
4176 -- reference, so we go ahead and resolve the type conversion.
4177 -- Another exception is the case of reference to component or
4178 -- subcomponent of a bit-packed array, in which case we want to
4179 -- defer expansion to the point the in and out assignments are
4180 -- performed.
4181
4182 if Ekind (F) /= E_In_Parameter
4183 and then Nkind (A) = N_Type_Conversion
4184 and then not Is_Class_Wide_Type (Etype (Expression (A)))
4185 and then not Is_Interface (Etype (A))
4186 then
4187 declare
4188 Expr_Typ : constant Entity_Id := Etype (Expression (A));
4189
4190 begin
4191 -- Check RM 4.6 (24.2/2)
4192
4193 if Is_Array_Type (Etype (F))
4194 and then Is_View_Conversion (A)
4195 then
4196 -- In a view conversion, the conversion must be legal in
4197 -- both directions, and thus both component types must be
4198 -- aliased, or neither (4.6 (8)).
4199
4200 -- Check RM 4.6 (24.8/2)
4201
4202 if Has_Aliased_Components (Expr_Typ) /=
4203 Has_Aliased_Components (Etype (F))
4204 then
4205 -- This normally illegal conversion is legal in an
4206 -- expanded instance body because of RM 12.3(11).
4207 -- At runtime, conversion must create a new object.
4208
4209 if not In_Instance then
4210 Error_Msg_N
4211 ("both component types in a view conversion must"
4212 & " be aliased, or neither", A);
4213 end if;
4214
4215 -- Check RM 4.6 (24/3)
4216
4217 elsif not Same_Ancestor (Etype (F), Expr_Typ) then
4218 -- Check view conv between unrelated by ref array
4219 -- types.
4220
4221 if Is_By_Reference_Type (Etype (F))
4222 or else Is_By_Reference_Type (Expr_Typ)
4223 then
4224 Error_Msg_N
4225 ("view conversion between unrelated by reference "
4226 & "array types not allowed ('A'I-00246)", A);
4227
4228 -- In Ada 2005 mode, check view conversion component
4229 -- type cannot be private, tagged, or volatile. Note
4230 -- that we only apply this to source conversions. The
4231 -- generated code can contain conversions which are
4232 -- not subject to this test, and we cannot extract the
4233 -- component type in such cases since it is not
4234 -- present.
4235
4236 elsif Comes_From_Source (A)
4237 and then Ada_Version >= Ada_2005
4238 then
4239 declare
4240 Comp_Type : constant Entity_Id :=
4241 Component_Type (Expr_Typ);
4242 begin
4243 if (Is_Private_Type (Comp_Type)
4244 and then not Is_Generic_Type (Comp_Type))
4245 or else Is_Tagged_Type (Comp_Type)
4246 or else Is_Volatile (Comp_Type)
4247 then
4248 Error_Msg_N
4249 ("component type of a view conversion " &
4250 "cannot be private, tagged, or volatile" &
4251 " (RM 4.6 (24))",
4252 Expression (A));
4253 end if;
4254 end;
4255 end if;
4256 end if;
4257
4258 -- AI12-0074 & AI12-0377
4259 -- Check 6.4.1: If the mode is out, the actual parameter is
4260 -- a view conversion, and the type of the formal parameter
4261 -- is a scalar type, then either:
4262 -- - the target and operand type both do not have the
4263 -- Default_Value aspect specified; or
4264 -- - the target and operand type both have the
4265 -- Default_Value aspect specified, and there shall exist
4266 -- a type (other than a root numeric type) that is an
4267 -- ancestor of both the target type and the operand
4268 -- type.
4269
4270 elsif Ekind (F) = E_Out_Parameter
4271 and then Is_Scalar_Type (Etype (F))
4272 then
4273 if Has_Default_Aspect (Etype (F)) /=
4274 Has_Default_Aspect (Expr_Typ)
4275 then
4276 Error_Msg_N
4277 ("view conversion requires Default_Value on both " &
4278 "types (RM 6.4.1)", A);
4279 elsif Has_Default_Aspect (Expr_Typ)
4280 and then not Same_Ancestor (Etype (F), Expr_Typ)
4281 then
4282 Error_Msg_N
4283 ("view conversion between unrelated types with "
4284 & "Default_Value not allowed (RM 6.4.1)", A);
4285 end if;
4286 end if;
4287 end;
4288
4289 -- Resolve expression if conversion is all OK
4290
4291 if (Conversion_OK (A)
4292 or else Valid_Conversion (A, Etype (A), Expression (A)))
4293 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
4294 then
4295 Resolve (Expression (A));
4296 end if;
4297
4298 -- If the actual is a function call that returns a limited
4299 -- unconstrained object that needs finalization, create a
4300 -- transient scope for it, so that it can receive the proper
4301 -- finalization list.
4302
4303 elsif Expander_Active
4304 and then Nkind (A) = N_Function_Call
4305 and then Is_Limited_Record (Etype (F))
4306 and then not Is_Constrained (Etype (F))
4307 and then (Needs_Finalization (Etype (F))
4308 or else Has_Task (Etype (F)))
4309 then
4310 Establish_Transient_Scope (A, Manage_Sec_Stack => False);
4311 Resolve (A, Etype (F));
4312
4313 -- A small optimization: if one of the actuals is a concatenation
4314 -- create a block around a procedure call to recover stack space.
4315 -- This alleviates stack usage when several procedure calls in
4316 -- the same statement list use concatenation. We do not perform
4317 -- this wrapping for code statements, where the argument is a
4318 -- static string, and we want to preserve warnings involving
4319 -- sequences of such statements.
4320
4321 elsif Expander_Active
4322 and then Nkind (A) = N_Op_Concat
4323 and then Nkind (N) = N_Procedure_Call_Statement
4324 and then not (Is_Intrinsic_Subprogram (Nam)
4325 and then Chars (Nam) = Name_Asm)
4326 and then not Static_Concatenation (A)
4327 then
4328 Establish_Transient_Scope (A, Manage_Sec_Stack => False);
4329 Resolve (A, Etype (F));
4330
4331 else
4332 if Nkind (A) = N_Type_Conversion
4333 and then Is_Array_Type (Etype (F))
4334 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
4335 and then
4336 (Is_Limited_Type (Etype (F))
4337 or else Is_Limited_Type (Etype (Expression (A))))
4338 then
4339 Error_Msg_N
4340 ("conversion between unrelated limited array types not "
4341 & "allowed ('A'I-00246)", A);
4342
4343 if Is_Limited_Type (Etype (F)) then
4344 Explain_Limited_Type (Etype (F), A);
4345 end if;
4346
4347 if Is_Limited_Type (Etype (Expression (A))) then
4348 Explain_Limited_Type (Etype (Expression (A)), A);
4349 end if;
4350 end if;
4351
4352 -- (Ada 2005: AI-251): If the actual is an allocator whose
4353 -- directly designated type is a class-wide interface, we build
4354 -- an anonymous access type to use it as the type of the
4355 -- allocator. Later, when the subprogram call is expanded, if
4356 -- the interface has a secondary dispatch table the expander
4357 -- will add a type conversion to force the correct displacement
4358 -- of the pointer.
4359
4360 if Nkind (A) = N_Allocator then
4361 declare
4362 DDT : constant Entity_Id :=
4363 Directly_Designated_Type (Base_Type (Etype (F)));
4364
4365 begin
4366 -- Displace the pointer to the object to reference its
4367 -- secondary dispatch table.
4368
4369 if Is_Class_Wide_Type (DDT)
4370 and then Is_Interface (DDT)
4371 then
4372 Rewrite (A, Convert_To (Etype (F), Relocate_Node (A)));
4373 Analyze_And_Resolve (A, Etype (F),
4374 Suppress => Access_Check);
4375 end if;
4376
4377 -- Ada 2005, AI-162:If the actual is an allocator, the
4378 -- innermost enclosing statement is the master of the
4379 -- created object. This needs to be done with expansion
4380 -- enabled only, otherwise the transient scope will not
4381 -- be removed in the expansion of the wrapped construct.
4382
4383 if Expander_Active
4384 and then (Needs_Finalization (DDT)
4385 or else Has_Task (DDT))
4386 then
4387 Establish_Transient_Scope
4388 (A, Manage_Sec_Stack => False);
4389 end if;
4390 end;
4391
4392 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4393 Check_Restriction (No_Access_Parameter_Allocators, A);
4394 end if;
4395 end if;
4396
4397 -- (Ada 2005): The call may be to a primitive operation of a
4398 -- tagged synchronized type, declared outside of the type. In
4399 -- this case the controlling actual must be converted to its
4400 -- corresponding record type, which is the formal type. The
4401 -- actual may be a subtype, either because of a constraint or
4402 -- because it is a generic actual, so use base type to locate
4403 -- concurrent type.
4404
4405 F_Typ := Base_Type (Etype (F));
4406
4407 if Is_Tagged_Type (F_Typ)
4408 and then (Is_Concurrent_Type (F_Typ)
4409 or else Is_Concurrent_Record_Type (F_Typ))
4410 then
4411 -- If the actual is overloaded, look for an interpretation
4412 -- that has a synchronized type.
4413
4414 if not Is_Overloaded (A) then
4415 A_Typ := Base_Type (Etype (A));
4416
4417 else
4418 declare
4419 Index : Interp_Index;
4420 It : Interp;
4421
4422 begin
4423 Get_First_Interp (A, Index, It);
4424 while Present (It.Typ) loop
4425 if Is_Concurrent_Type (It.Typ)
4426 or else Is_Concurrent_Record_Type (It.Typ)
4427 then
4428 A_Typ := Base_Type (It.Typ);
4429 exit;
4430 end if;
4431
4432 Get_Next_Interp (Index, It);
4433 end loop;
4434 end;
4435 end if;
4436
4437 declare
4438 Full_A_Typ : Entity_Id;
4439
4440 begin
4441 if Present (Full_View (A_Typ)) then
4442 Full_A_Typ := Base_Type (Full_View (A_Typ));
4443 else
4444 Full_A_Typ := A_Typ;
4445 end if;
4446
4447 -- Tagged synchronized type (case 1): the actual is a
4448 -- concurrent type.
4449
4450 if Is_Concurrent_Type (A_Typ)
4451 and then Corresponding_Record_Type (A_Typ) = F_Typ
4452 then
4453 Rewrite (A,
4454 Unchecked_Convert_To
4455 (Corresponding_Record_Type (A_Typ), A));
4456 Resolve (A, Etype (F));
4457
4458 -- Tagged synchronized type (case 2): the formal is a
4459 -- concurrent type.
4460
4461 elsif Ekind (Full_A_Typ) = E_Record_Type
4462 and then Present
4463 (Corresponding_Concurrent_Type (Full_A_Typ))
4464 and then Is_Concurrent_Type (F_Typ)
4465 and then Present (Corresponding_Record_Type (F_Typ))
4466 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
4467 then
4468 Resolve (A, Corresponding_Record_Type (F_Typ));
4469
4470 -- Common case
4471
4472 else
4473 Resolve (A, Etype (F));
4474 end if;
4475 end;
4476
4477 -- Not a synchronized operation
4478
4479 else
4480 Resolve (A, Etype (F));
4481 end if;
4482 end if;
4483
4484 A_Typ := Etype (A);
4485 F_Typ := Etype (F);
4486
4487 -- An actual cannot be an untagged formal incomplete type
4488
4489 if Ekind (A_Typ) = E_Incomplete_Type
4490 and then not Is_Tagged_Type (A_Typ)
4491 and then Is_Generic_Type (A_Typ)
4492 then
4493 Error_Msg_N
4494 ("invalid use of untagged formal incomplete type", A);
4495 end if;
4496
4497 -- has warnings suppressed, then we reset Never_Set_In_Source for
4498 -- the calling entity. The reason for this is to catch cases like
4499 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
4500 -- uses trickery to modify an IN parameter.
4501
4502 if Ekind (F) = E_In_Parameter
4503 and then Is_Entity_Name (A)
4504 and then Present (Entity (A))
4505 and then Ekind (Entity (A)) = E_Variable
4506 and then Has_Warnings_Off (F_Typ)
4507 then
4508 Set_Never_Set_In_Source (Entity (A), False);
4509 end if;
4510
4511 -- Perform error checks for IN and IN OUT parameters
4512
4513 if Ekind (F) /= E_Out_Parameter then
4514
4515 -- Check unset reference. For scalar parameters, it is clearly
4516 -- wrong to pass an uninitialized value as either an IN or
4517 -- IN-OUT parameter. For composites, it is also clearly an
4518 -- error to pass a completely uninitialized value as an IN
4519 -- parameter, but the case of IN OUT is trickier. We prefer
4520 -- not to give a warning here. For example, suppose there is
4521 -- a routine that sets some component of a record to False.
4522 -- It is perfectly reasonable to make this IN-OUT and allow
4523 -- either initialized or uninitialized records to be passed
4524 -- in this case.
4525
4526 -- For partially initialized composite values, we also avoid
4527 -- warnings, since it is quite likely that we are passing a
4528 -- partially initialized value and only the initialized fields
4529 -- will in fact be read in the subprogram.
4530
4531 if Is_Scalar_Type (A_Typ)
4532 or else (Ekind (F) = E_In_Parameter
4533 and then not Is_Partially_Initialized_Type (A_Typ))
4534 then
4535 Check_Unset_Reference (A);
4536 end if;
4537
4538 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
4539 -- actual to a nested call, since this constitutes a reading of
4540 -- the parameter, which is not allowed.
4541
4542 if Ada_Version = Ada_83
4543 and then Is_Entity_Name (A)
4544 and then Ekind (Entity (A)) = E_Out_Parameter
4545 then
4546 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
4547 end if;
4548 end if;
4549
4550 -- In -gnatd.q mode, forget that a given array is constant when
4551 -- it is passed as an IN parameter to a foreign-convention
4552 -- subprogram. This is in case the subprogram evilly modifies the
4553 -- object. Of course, correct code would use IN OUT.
4554
4555 if Debug_Flag_Dot_Q
4556 and then Ekind (F) = E_In_Parameter
4557 and then Has_Foreign_Convention (Nam)
4558 and then Is_Array_Type (F_Typ)
4559 and then Nkind (A) in N_Has_Entity
4560 and then Present (Entity (A))
4561 then
4562 Set_Is_True_Constant (Entity (A), False);
4563 end if;
4564
4565 -- Case of OUT or IN OUT parameter
4566
4567 if Ekind (F) /= E_In_Parameter then
4568
4569 -- For an Out parameter, check for useless assignment. Note
4570 -- that we can't set Last_Assignment this early, because we may
4571 -- kill current values in Resolve_Call, and that call would
4572 -- clobber the Last_Assignment field.
4573
4574 -- Note: call Warn_On_Useless_Assignment before doing the check
4575 -- below for Is_OK_Variable_For_Out_Formal so that the setting
4576 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
4577 -- reflects the last assignment, not this one.
4578
4579 if Ekind (F) = E_Out_Parameter then
4580 if Warn_On_Modified_As_Out_Parameter (F)
4581 and then Is_Entity_Name (A)
4582 and then Present (Entity (A))
4583 and then Comes_From_Source (N)
4584 then
4585 Warn_On_Useless_Assignment (Entity (A), A);
4586 end if;
4587 end if;
4588
4589 -- Validate the form of the actual. Note that the call to
4590 -- Is_OK_Variable_For_Out_Formal generates the required
4591 -- reference in this case.
4592
4593 -- A call to an initialization procedure for an aggregate
4594 -- component may initialize a nested component of a constant
4595 -- designated object. In this context the object is variable.
4596
4597 if not Is_OK_Variable_For_Out_Formal (A)
4598 and then not Is_Init_Proc (Nam)
4599 then
4600 Error_Msg_NE ("actual for& must be a variable", A, F);
4601
4602 if Is_Subprogram (Current_Scope) then
4603 if Is_Invariant_Procedure (Current_Scope)
4604 or else Is_Partial_Invariant_Procedure (Current_Scope)
4605 then
4606 Error_Msg_N
4607 ("function used in invariant cannot modify its "
4608 & "argument", F);
4609
4610 elsif Is_Predicate_Function (Current_Scope) then
4611 Error_Msg_N
4612 ("function used in predicate cannot modify its "
4613 & "argument", F);
4614 end if;
4615 end if;
4616 end if;
4617
4618 -- What's the following about???
4619
4620 if Is_Entity_Name (A) then
4621 Kill_Checks (Entity (A));
4622 else
4623 Kill_All_Checks;
4624 end if;
4625 end if;
4626
4627 if A_Typ = Any_Type then
4628 Set_Etype (N, Any_Type);
4629 return;
4630 end if;
4631
4632 -- Apply appropriate constraint/predicate checks for IN [OUT] case
4633
4634 if Ekind (F) in E_In_Parameter | E_In_Out_Parameter then
4635
4636 -- Apply predicate tests except in certain special cases. Note
4637 -- that it might be more consistent to apply these only when
4638 -- expansion is active (in Exp_Ch6.Expand_Actuals), as we do
4639 -- for the outbound predicate tests ??? In any case indicate
4640 -- the function being called, for better warnings if the call
4641 -- leads to an infinite recursion.
4642
4643 if Predicate_Tests_On_Arguments (Nam) then
4644 Apply_Predicate_Check (A, F_Typ, Nam);
4645 end if;
4646
4647 -- Apply required constraint checks
4648
4649 if Is_Scalar_Type (A_Typ) then
4650 Apply_Scalar_Range_Check (A, F_Typ);
4651
4652 elsif Is_Array_Type (A_Typ) then
4653 Apply_Length_Check (A, F_Typ);
4654
4655 elsif Is_Record_Type (F_Typ)
4656 and then Has_Discriminants (F_Typ)
4657 and then Is_Constrained (F_Typ)
4658 and then (not Is_Derived_Type (F_Typ)
4659 or else Comes_From_Source (Nam))
4660 then
4661 Apply_Discriminant_Check (A, F_Typ);
4662
4663 -- For view conversions of a discriminated object, apply
4664 -- check to object itself, the conversion alreay has the
4665 -- proper type.
4666
4667 if Nkind (A) = N_Type_Conversion
4668 and then Is_Constrained (Etype (Expression (A)))
4669 then
4670 Apply_Discriminant_Check (Expression (A), F_Typ);
4671 end if;
4672
4673 elsif Is_Access_Type (F_Typ)
4674 and then Is_Array_Type (Designated_Type (F_Typ))
4675 and then Is_Constrained (Designated_Type (F_Typ))
4676 then
4677 Apply_Length_Check (A, F_Typ);
4678
4679 elsif Is_Access_Type (F_Typ)
4680 and then Has_Discriminants (Designated_Type (F_Typ))
4681 and then Is_Constrained (Designated_Type (F_Typ))
4682 then
4683 Apply_Discriminant_Check (A, F_Typ);
4684
4685 else
4686 Apply_Range_Check (A, F_Typ);
4687 end if;
4688
4689 -- Ada 2005 (AI-231): Note that the controlling parameter case
4690 -- already existed in Ada 95, which is partially checked
4691 -- elsewhere (see Checks), and we don't want the warning
4692 -- message to differ.
4693
4694 if Is_Access_Type (F_Typ)
4695 and then Can_Never_Be_Null (F_Typ)
4696 and then Known_Null (A)
4697 then
4698 if Is_Controlling_Formal (F) then
4699 Apply_Compile_Time_Constraint_Error
4700 (N => A,
4701 Msg => "null value not allowed here??",
4702 Reason => CE_Access_Check_Failed);
4703
4704 elsif Ada_Version >= Ada_2005 then
4705 Apply_Compile_Time_Constraint_Error
4706 (N => A,
4707 Msg => "(Ada 2005) NULL not allowed in "
4708 & "null-excluding formal??",
4709 Reason => CE_Null_Not_Allowed);
4710 end if;
4711 end if;
4712 end if;
4713
4714 -- Checks for OUT parameters and IN OUT parameters
4715
4716 if Ekind (F) in E_Out_Parameter | E_In_Out_Parameter then
4717
4718 -- If there is a type conversion, make sure the return value
4719 -- meets the constraints of the variable before the conversion.
4720
4721 if Nkind (A) = N_Type_Conversion then
4722 if Is_Scalar_Type (A_Typ) then
4723
4724 -- Special case here tailored to Exp_Ch6.Is_Legal_Copy,
4725 -- which would prevent the check from being generated.
4726 -- This is for Starlet only though, so long obsolete.
4727
4728 if Mechanism (F) = By_Reference
4729 and then Ekind (Nam) = E_Procedure
4730 and then Is_Valued_Procedure (Nam)
4731 then
4732 null;
4733 else
4734 Apply_Scalar_Range_Check
4735 (Expression (A), Etype (Expression (A)), A_Typ);
4736 end if;
4737
4738 -- In addition the return value must meet the constraints
4739 -- of the object type (see the comment below).
4740
4741 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4742
4743 else
4744 Apply_Range_Check
4745 (Expression (A), Etype (Expression (A)), A_Typ);
4746 end if;
4747
4748 -- If no conversion, apply scalar range checks and length check
4749 -- based on the subtype of the actual (NOT that of the formal).
4750 -- This indicates that the check takes place on return from the
4751 -- call. During expansion the required constraint checks are
4752 -- inserted. In GNATprove mode, in the absence of expansion,
4753 -- the flag indicates that the returned value is valid.
4754
4755 else
4756 if Is_Scalar_Type (F_Typ) then
4757 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
4758
4759 elsif Is_Array_Type (F_Typ)
4760 and then Ekind (F) = E_Out_Parameter
4761 then
4762 Apply_Length_Check (A, F_Typ);
4763
4764 else
4765 Apply_Range_Check (A, A_Typ, F_Typ);
4766 end if;
4767 end if;
4768
4769 -- Note: we do not apply the predicate checks for the case of
4770 -- OUT and IN OUT parameters. They are instead applied in the
4771 -- Expand_Actuals routine in Exp_Ch6.
4772 end if;
4773
4774 -- An actual associated with an access parameter is implicitly
4775 -- converted to the anonymous access type of the formal and must
4776 -- satisfy the legality checks for access conversions.
4777
4778 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4779 if not Valid_Conversion (A, F_Typ, A) then
4780 Error_Msg_N
4781 ("invalid implicit conversion for access parameter", A);
4782 end if;
4783
4784 -- If the actual is an access selected component of a variable,
4785 -- the call may modify its designated object. It is reasonable
4786 -- to treat this as a potential modification of the enclosing
4787 -- record, to prevent spurious warnings that it should be
4788 -- declared as a constant, because intuitively programmers
4789 -- regard the designated subcomponent as part of the record.
4790
4791 if Nkind (A) = N_Selected_Component
4792 and then Is_Entity_Name (Prefix (A))
4793 and then not Is_Constant_Object (Entity (Prefix (A)))
4794 then
4795 Note_Possible_Modification (A, Sure => False);
4796 end if;
4797 end if;
4798
4799 -- Check illegal cases of atomic/volatile/VFA actual (RM C.6(12))
4800
4801 if (Is_By_Reference_Type (Etype (F)) or else Is_Aliased (F))
4802 and then Comes_From_Source (N)
4803 then
4804 if Is_Atomic_Object (A)
4805 and then not Is_Atomic (Etype (F))
4806 then
4807 Error_Msg_NE
4808 ("cannot pass atomic object to nonatomic formal&",
4809 A, F);
4810 Error_Msg_N
4811 ("\which is passed by reference (RM C.6(12))", A);
4812
4813 elsif Is_Volatile_Object_Ref (A)
4814 and then not Is_Volatile (Etype (F))
4815 then
4816 Error_Msg_NE
4817 ("cannot pass volatile object to nonvolatile formal&",
4818 A, F);
4819 Error_Msg_N
4820 ("\which is passed by reference (RM C.6(12))", A);
4821
4822 elsif Is_Volatile_Full_Access_Object_Ref (A)
4823 and then not Is_Volatile_Full_Access (Etype (F))
4824 then
4825 Error_Msg_NE
4826 ("cannot pass full access object to nonfull access "
4827 & "formal&", A, F);
4828 Error_Msg_N
4829 ("\which is passed by reference (RM C.6(12))", A);
4830 end if;
4831
4832 -- Check for nonatomic subcomponent of a full access object
4833 -- in Ada 2020 (RM C.6 (12)).
4834
4835 if Ada_Version >= Ada_2020
4836 and then Is_Subcomponent_Of_Full_Access_Object (A)
4837 and then not Is_Atomic_Object (A)
4838 then
4839 Error_Msg_N
4840 ("cannot pass nonatomic subcomponent of full access "
4841 & "object", A);
4842 Error_Msg_NE
4843 ("\to formal & which is passed by reference (RM C.6(12))",
4844 A, F);
4845 end if;
4846 end if;
4847
4848 -- Check that subprograms don't have improper controlling
4849 -- arguments (RM 3.9.2 (9)).
4850
4851 -- A primitive operation may have an access parameter of an
4852 -- incomplete tagged type, but a dispatching call is illegal
4853 -- if the type is still incomplete.
4854
4855 if Is_Controlling_Formal (F) then
4856 Set_Is_Controlling_Actual (A);
4857
4858 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4859 declare
4860 Desig : constant Entity_Id := Designated_Type (Etype (F));
4861 begin
4862 if Ekind (Desig) = E_Incomplete_Type
4863 and then No (Full_View (Desig))
4864 and then No (Non_Limited_View (Desig))
4865 then
4866 Error_Msg_NE
4867 ("premature use of incomplete type& "
4868 & "in dispatching call", A, Desig);
4869 end if;
4870 end;
4871 end if;
4872
4873 elsif Nkind (A) = N_Explicit_Dereference then
4874 Validate_Remote_Access_To_Class_Wide_Type (A);
4875 end if;
4876
4877 -- Apply legality rule 3.9.2 (9/1)
4878
4879 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
4880 and then not Is_Class_Wide_Type (F_Typ)
4881 and then not Is_Controlling_Formal (F)
4882 and then not In_Instance
4883 then
4884 Error_Msg_N ("class-wide argument not allowed here!", A);
4885
4886 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4887 Error_Msg_Node_2 := F_Typ;
4888 Error_Msg_NE
4889 ("& is not a dispatching operation of &!", A, Nam);
4890 end if;
4891
4892 -- Apply the checks described in 3.10.2(27): if the context is a
4893 -- specific access-to-object, the actual cannot be class-wide.
4894 -- Use base type to exclude access_to_subprogram cases.
4895
4896 elsif Is_Access_Type (A_Typ)
4897 and then Is_Access_Type (F_Typ)
4898 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
4899 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
4900 or else (Nkind (A) = N_Attribute_Reference
4901 and then
4902 Is_Class_Wide_Type (Etype (Prefix (A)))))
4903 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
4904 and then not Is_Controlling_Formal (F)
4905
4906 -- Disable these checks for call to imported C++ subprograms
4907
4908 and then not
4909 (Is_Entity_Name (Name (N))
4910 and then Is_Imported (Entity (Name (N)))
4911 and then Convention (Entity (Name (N))) = Convention_CPP)
4912 then
4913 Error_Msg_N
4914 ("access to class-wide argument not allowed here!", A);
4915
4916 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
4917 Error_Msg_Node_2 := Designated_Type (F_Typ);
4918 Error_Msg_NE
4919 ("& is not a dispatching operation of &!", A, Nam);
4920 end if;
4921 end if;
4922
4923 Check_Aliased_Parameter;
4924
4925 Eval_Actual (A);
4926
4927 -- If it is a named association, treat the selector_name as a
4928 -- proper identifier, and mark the corresponding entity.
4929
4930 if Nkind (Parent (A)) = N_Parameter_Association
4931
4932 -- Ignore reference in SPARK mode, as it refers to an entity not
4933 -- in scope at the point of reference, so the reference should
4934 -- be ignored for computing effects of subprograms.
4935
4936 and then not GNATprove_Mode
4937 then
4938 -- If subprogram is overridden, use name of formal that
4939 -- is being called.
4940
4941 if Present (Real_Subp) then
4942 Set_Entity (Selector_Name (Parent (A)), Real_F);
4943 Set_Etype (Selector_Name (Parent (A)), Etype (Real_F));
4944
4945 else
4946 Set_Entity (Selector_Name (Parent (A)), F);
4947 Generate_Reference (F, Selector_Name (Parent (A)));
4948 Set_Etype (Selector_Name (Parent (A)), F_Typ);
4949 Generate_Reference (F_Typ, N, ' ');
4950 end if;
4951 end if;
4952
4953 Prev := A;
4954
4955 if Ekind (F) /= E_Out_Parameter then
4956 Check_Unset_Reference (A);
4957 end if;
4958
4959 -- The following checks are only relevant when SPARK_Mode is on as
4960 -- they are not standard Ada legality rule. Internally generated
4961 -- temporaries are ignored.
4962
4963 if SPARK_Mode = On and then Comes_From_Source (A) then
4964
4965 -- An effectively volatile object for reading may act as an
4966 -- actual when the corresponding formal is of a non-scalar
4967 -- effectively volatile type for reading (SPARK RM 7.1.3(10)).
4968
4969 if not Is_Scalar_Type (Etype (F))
4970 and then Is_Effectively_Volatile_For_Reading (Etype (F))
4971 then
4972 null;
4973
4974 -- An effectively volatile object for reading may act as an
4975 -- actual in a call to an instance of Unchecked_Conversion.
4976 -- (SPARK RM 7.1.3(10)).
4977
4978 elsif Is_Unchecked_Conversion_Instance (Nam) then
4979 null;
4980
4981 -- The actual denotes an object
4982
4983 elsif Is_Effectively_Volatile_Object_For_Reading (A) then
4984 Error_Msg_N
4985 ("volatile object cannot act as actual in a call (SPARK "
4986 & "RM 7.1.3(10))", A);
4987
4988 -- Otherwise the actual denotes an expression. Inspect the
4989 -- expression and flag each effectively volatile object
4990 -- for reading as illegal because it apprears within an
4991 -- interfering context. Note that this is usually done in
4992 -- Resolve_Entity_Name, but when the effectively volatile
4993 -- object for reading appears as an actual in a call, the
4994 -- call must be resolved first.
4995
4996 else
4997 Flag_Effectively_Volatile_Objects (A);
4998 end if;
4999
5000 -- An effectively volatile variable cannot act as an actual
5001 -- parameter in a procedure call when the variable has enabled
5002 -- property Effective_Reads and the corresponding formal is of
5003 -- mode IN (SPARK RM 7.1.3(10)).
5004
5005 if Ekind (Nam) = E_Procedure
5006 and then Ekind (F) = E_In_Parameter
5007 and then Is_Entity_Name (A)
5008 then
5009 A_Id := Entity (A);
5010
5011 if Ekind (A_Id) = E_Variable
5012 and then Is_Effectively_Volatile_For_Reading (Etype (A_Id))
5013 and then Effective_Reads_Enabled (A_Id)
5014 then
5015 Error_Msg_NE
5016 ("effectively volatile variable & cannot appear as "
5017 & "actual in procedure call", A, A_Id);
5018
5019 Error_Msg_Name_1 := Name_Effective_Reads;
5020 Error_Msg_N ("\\variable has enabled property %", A);
5021 Error_Msg_N ("\\corresponding formal has mode IN", A);
5022 end if;
5023 end if;
5024 end if;
5025
5026 -- A formal parameter of a specific tagged type whose related
5027 -- subprogram is subject to pragma Extensions_Visible with value
5028 -- "False" cannot act as an actual in a subprogram with value
5029 -- "True" (SPARK RM 6.1.7(3)).
5030
5031 if Is_EVF_Expression (A)
5032 and then Extensions_Visible_Status (Nam) =
5033 Extensions_Visible_True
5034 then
5035 Error_Msg_N
5036 ("formal parameter cannot act as actual parameter when "
5037 & "Extensions_Visible is False", A);
5038 Error_Msg_NE
5039 ("\subprogram & has Extensions_Visible True", A, Nam);
5040 end if;
5041
5042 -- The actual parameter of a Ghost subprogram whose formal is of
5043 -- mode IN OUT or OUT must be a Ghost variable (SPARK RM 6.9(12)).
5044
5045 if Comes_From_Source (Nam)
5046 and then Is_Ghost_Entity (Nam)
5047 and then Ekind (F) in E_In_Out_Parameter | E_Out_Parameter
5048 and then Is_Entity_Name (A)
5049 and then Present (Entity (A))
5050 and then not Is_Ghost_Entity (Entity (A))
5051 then
5052 Error_Msg_NE
5053 ("non-ghost variable & cannot appear as actual in call to "
5054 & "ghost procedure", A, Entity (A));
5055
5056 if Ekind (F) = E_In_Out_Parameter then
5057 Error_Msg_N ("\corresponding formal has mode `IN OUT`", A);
5058 else
5059 Error_Msg_N ("\corresponding formal has mode OUT", A);
5060 end if;
5061 end if;
5062
5063 -- (AI12-0397): The target of a subprogram call that occurs within
5064 -- the expression of an Default_Initial_Condition aspect and has
5065 -- an actual that is the current instance of the type must be
5066 -- either a primitive of the type or a class-wide subprogram,
5067 -- because the type of the current instance in such an aspect is
5068 -- considered to be a notional formal derived type whose only
5069 -- operations correspond to the primitives of the enclosing type.
5070 -- Nonprimitives can be called, but the current instance must be
5071 -- converted rather than passed directly. Note that a current
5072 -- instance of a type with DIC will occur as a reference to an
5073 -- in-mode formal of an enclosing DIC procedure or partial DIC
5074 -- procedure. (It seems that this check should perhaps also apply
5075 -- to calls within Type_Invariant'Class, but not Type_Invariant,
5076 -- aspects???)
5077
5078 if Nkind (A) = N_Identifier
5079 and then Ekind (Entity (A)) = E_In_Parameter
5080
5081 and then Is_Subprogram (Scope (Entity (A)))
5082 and then Is_DIC_Procedure (Scope (Entity (A)))
5083
5084 -- We check Comes_From_Source to exclude inherited primitives
5085 -- from being flagged, because such subprograms turn out to not
5086 -- always have the Is_Primitive flag set. ???
5087
5088 and then Comes_From_Source (Nam)
5089
5090 and then not Is_Primitive (Nam)
5091 and then not Is_Class_Wide_Type (Etype (F))
5092 then
5093 Error_Msg_NE
5094 ("call to nonprimitive & with current instance not allowed " &
5095 "for aspect", A, Nam);
5096 end if;
5097
5098 Next_Actual (A);
5099
5100 -- Case where actual is not present
5101
5102 else
5103 Insert_Default;
5104 end if;
5105
5106 Next_Formal (F);
5107
5108 if Present (Real_Subp) then
5109 Next_Formal (Real_F);
5110 end if;
5111 end loop;
5112 end Resolve_Actuals;
5113
5114 -----------------------
5115 -- Resolve_Allocator --
5116 -----------------------
5117
5118 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
5119 Desig_T : constant Entity_Id := Designated_Type (Typ);
5120 E : constant Node_Id := Expression (N);
5121 Subtyp : Entity_Id;
5122 Discrim : Entity_Id;
5123 Constr : Node_Id;
5124 Aggr : Node_Id;
5125 Assoc : Node_Id := Empty;
5126 Disc_Exp : Node_Id;
5127
5128 procedure Check_Allocator_Discrim_Accessibility
5129 (Disc_Exp : Node_Id;
5130 Alloc_Typ : Entity_Id);
5131 -- Check that accessibility level associated with an access discriminant
5132 -- initialized in an allocator by the expression Disc_Exp is not deeper
5133 -- than the level of the allocator type Alloc_Typ. An error message is
5134 -- issued if this condition is violated. Specialized checks are done for
5135 -- the cases of a constraint expression which is an access attribute or
5136 -- an access discriminant.
5137
5138 procedure Check_Allocator_Discrim_Accessibility_Exprs
5139 (Curr_Exp : Node_Id;
5140 Alloc_Typ : Entity_Id);
5141 -- Dispatch checks performed by Check_Allocator_Discrim_Accessibility
5142 -- across all expressions within a given conditional expression.
5143
5144 function In_Dispatching_Context return Boolean;
5145 -- If the allocator is an actual in a call, it is allowed to be class-
5146 -- wide when the context is not because it is a controlling actual.
5147
5148 -------------------------------------------
5149 -- Check_Allocator_Discrim_Accessibility --
5150 -------------------------------------------
5151
5152 procedure Check_Allocator_Discrim_Accessibility
5153 (Disc_Exp : Node_Id;
5154 Alloc_Typ : Entity_Id)
5155 is
5156 begin
5157 if Type_Access_Level (Etype (Disc_Exp)) >
5158 Deepest_Type_Access_Level (Alloc_Typ)
5159 then
5160 Error_Msg_N
5161 ("operand type has deeper level than allocator type", Disc_Exp);
5162
5163 -- When the expression is an Access attribute the level of the prefix
5164 -- object must not be deeper than that of the allocator's type.
5165
5166 elsif Nkind (Disc_Exp) = N_Attribute_Reference
5167 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
5168 Attribute_Access
5169 and then Static_Accessibility_Level
5170 (Disc_Exp, Zero_On_Dynamic_Level)
5171 > Deepest_Type_Access_Level (Alloc_Typ)
5172 then
5173 Error_Msg_N
5174 ("prefix of attribute has deeper level than allocator type",
5175 Disc_Exp);
5176
5177 -- When the expression is an access discriminant the check is against
5178 -- the level of the prefix object.
5179
5180 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
5181 and then Nkind (Disc_Exp) = N_Selected_Component
5182 and then Static_Accessibility_Level
5183 (Disc_Exp, Zero_On_Dynamic_Level)
5184 > Deepest_Type_Access_Level (Alloc_Typ)
5185 then
5186 Error_Msg_N
5187 ("access discriminant has deeper level than allocator type",
5188 Disc_Exp);
5189
5190 -- All other cases are legal
5191
5192 else
5193 null;
5194 end if;
5195 end Check_Allocator_Discrim_Accessibility;
5196
5197 -------------------------------------------------
5198 -- Check_Allocator_Discrim_Accessibility_Exprs --
5199 -------------------------------------------------
5200
5201 procedure Check_Allocator_Discrim_Accessibility_Exprs
5202 (Curr_Exp : Node_Id;
5203 Alloc_Typ : Entity_Id)
5204 is
5205 Alt : Node_Id;
5206 Expr : Node_Id;
5207 Disc_Exp : constant Node_Id := Original_Node (Curr_Exp);
5208 begin
5209 -- When conditional expressions are constant folded we know at
5210 -- compile time which expression to check - so don't bother with
5211 -- the rest of the cases.
5212
5213 if Nkind (Curr_Exp) = N_Attribute_Reference then
5214 Check_Allocator_Discrim_Accessibility (Curr_Exp, Alloc_Typ);
5215
5216 -- Non-constant-folded if expressions
5217
5218 elsif Nkind (Disc_Exp) = N_If_Expression then
5219 -- Check both expressions if they are still present in the face
5220 -- of expansion.
5221
5222 Expr := Next (First (Expressions (Disc_Exp)));
5223 if Present (Expr) then
5224 Check_Allocator_Discrim_Accessibility_Exprs (Expr, Alloc_Typ);
5225 Next (Expr);
5226 if Present (Expr) then
5227 Check_Allocator_Discrim_Accessibility_Exprs
5228 (Expr, Alloc_Typ);
5229 end if;
5230 end if;
5231
5232 -- Non-constant-folded case expressions
5233
5234 elsif Nkind (Disc_Exp) = N_Case_Expression then
5235 -- Check all alternatives
5236
5237 Alt := First (Alternatives (Disc_Exp));
5238 while Present (Alt) loop
5239 Check_Allocator_Discrim_Accessibility_Exprs
5240 (Expression (Alt), Alloc_Typ);
5241
5242 Next (Alt);
5243 end loop;
5244
5245 -- Base case, check the accessibility of the original node of the
5246 -- expression.
5247
5248 else
5249 Check_Allocator_Discrim_Accessibility (Disc_Exp, Alloc_Typ);
5250 end if;
5251 end Check_Allocator_Discrim_Accessibility_Exprs;
5252
5253 ----------------------------
5254 -- In_Dispatching_Context --
5255 ----------------------------
5256
5257 function In_Dispatching_Context return Boolean is
5258 Par : constant Node_Id := Parent (N);
5259
5260 begin
5261 return Nkind (Par) in N_Subprogram_Call
5262 and then Is_Entity_Name (Name (Par))
5263 and then Is_Dispatching_Operation (Entity (Name (Par)));
5264 end In_Dispatching_Context;
5265
5266 -- Start of processing for Resolve_Allocator
5267
5268 begin
5269 -- Replace general access with specific type
5270
5271 if Ekind (Etype (N)) = E_Allocator_Type then
5272 Set_Etype (N, Base_Type (Typ));
5273 end if;
5274
5275 if Is_Abstract_Type (Typ) then
5276 Error_Msg_N ("type of allocator cannot be abstract", N);
5277 end if;
5278
5279 -- For qualified expression, resolve the expression using the given
5280 -- subtype (nothing to do for type mark, subtype indication)
5281
5282 if Nkind (E) = N_Qualified_Expression then
5283 if Is_Class_Wide_Type (Etype (E))
5284 and then not Is_Class_Wide_Type (Desig_T)
5285 and then not In_Dispatching_Context
5286 then
5287 Error_Msg_N
5288 ("class-wide allocator not allowed for this access type", N);
5289 end if;
5290
5291 -- Do a full resolution to apply constraint and predicate checks
5292
5293 Resolve_Qualified_Expression (E, Etype (E));
5294 Check_Unset_Reference (Expression (E));
5295
5296 -- Allocators generated by the build-in-place expansion mechanism
5297 -- are explicitly marked as coming from source but do not need to be
5298 -- checked for limited initialization. To exclude this case, ensure
5299 -- that the parent of the allocator is a source node.
5300 -- The return statement constructed for an Expression_Function does
5301 -- not come from source but requires a limited check.
5302
5303 if Is_Limited_Type (Etype (E))
5304 and then Comes_From_Source (N)
5305 and then
5306 (Comes_From_Source (Parent (N))
5307 or else
5308 (Ekind (Current_Scope) = E_Function
5309 and then Nkind (Original_Node (Unit_Declaration_Node
5310 (Current_Scope))) = N_Expression_Function))
5311 and then not In_Instance_Body
5312 then
5313 if not OK_For_Limited_Init (Etype (E), Expression (E)) then
5314 if Nkind (Parent (N)) = N_Assignment_Statement then
5315 Error_Msg_N
5316 ("illegal expression for initialized allocator of a "
5317 & "limited type (RM 7.5 (2.7/2))", N);
5318 else
5319 Error_Msg_N
5320 ("initialization not allowed for limited types", N);
5321 end if;
5322
5323 Explain_Limited_Type (Etype (E), N);
5324 end if;
5325 end if;
5326
5327 -- Calls to build-in-place functions are not currently supported in
5328 -- allocators for access types associated with a simple storage pool.
5329 -- Supporting such allocators may require passing additional implicit
5330 -- parameters to build-in-place functions (or a significant revision
5331 -- of the current b-i-p implementation to unify the handling for
5332 -- multiple kinds of storage pools). ???
5333
5334 if Is_Limited_View (Desig_T)
5335 and then Nkind (Expression (E)) = N_Function_Call
5336 then
5337 declare
5338 Pool : constant Entity_Id :=
5339 Associated_Storage_Pool (Root_Type (Typ));
5340 begin
5341 if Present (Pool)
5342 and then
5343 Present (Get_Rep_Pragma
5344 (Etype (Pool), Name_Simple_Storage_Pool_Type))
5345 then
5346 Error_Msg_N
5347 ("limited function calls not yet supported in simple "
5348 & "storage pool allocators", Expression (E));
5349 end if;
5350 end;
5351 end if;
5352
5353 -- A special accessibility check is needed for allocators that
5354 -- constrain access discriminants. The level of the type of the
5355 -- expression used to constrain an access discriminant cannot be
5356 -- deeper than the type of the allocator (in contrast to access
5357 -- parameters, where the level of the actual can be arbitrary).
5358
5359 -- We can't use Valid_Conversion to perform this check because in
5360 -- general the type of the allocator is unrelated to the type of
5361 -- the access discriminant.
5362
5363 if Ekind (Typ) /= E_Anonymous_Access_Type
5364 or else Is_Local_Anonymous_Access (Typ)
5365 then
5366 Subtyp := Entity (Subtype_Mark (E));
5367
5368 Aggr := Original_Node (Expression (E));
5369
5370 if Has_Discriminants (Subtyp)
5371 and then Nkind (Aggr) in N_Aggregate | N_Extension_Aggregate
5372 then
5373 Discrim := First_Discriminant (Base_Type (Subtyp));
5374
5375 -- Get the first component expression of the aggregate
5376
5377 if Present (Expressions (Aggr)) then
5378 Disc_Exp := First (Expressions (Aggr));
5379
5380 elsif Present (Component_Associations (Aggr)) then
5381 Assoc := First (Component_Associations (Aggr));
5382
5383 if Present (Assoc) then
5384 Disc_Exp := Expression (Assoc);
5385 else
5386 Disc_Exp := Empty;
5387 end if;
5388
5389 else
5390 Disc_Exp := Empty;
5391 end if;
5392
5393 while Present (Discrim) and then Present (Disc_Exp) loop
5394 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
5395 Check_Allocator_Discrim_Accessibility_Exprs
5396 (Disc_Exp, Typ);
5397 end if;
5398
5399 Next_Discriminant (Discrim);
5400
5401 if Present (Discrim) then
5402 if Present (Assoc) then
5403 Next (Assoc);
5404 Disc_Exp := Expression (Assoc);
5405
5406 elsif Present (Next (Disc_Exp)) then
5407 Next (Disc_Exp);
5408
5409 else
5410 Assoc := First (Component_Associations (Aggr));
5411
5412 if Present (Assoc) then
5413 Disc_Exp := Expression (Assoc);
5414 else
5415 Disc_Exp := Empty;
5416 end if;
5417 end if;
5418 end if;
5419 end loop;
5420 end if;
5421 end if;
5422
5423 -- For a subtype mark or subtype indication, freeze the subtype
5424
5425 else
5426 Freeze_Expression (E);
5427
5428 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
5429 Error_Msg_N
5430 ("initialization required for access-to-constant allocator", N);
5431 end if;
5432
5433 -- A special accessibility check is needed for allocators that
5434 -- constrain access discriminants. The level of the type of the
5435 -- expression used to constrain an access discriminant cannot be
5436 -- deeper than the type of the allocator (in contrast to access
5437 -- parameters, where the level of the actual can be arbitrary).
5438 -- We can't use Valid_Conversion to perform this check because
5439 -- in general the type of the allocator is unrelated to the type
5440 -- of the access discriminant.
5441
5442 if Nkind (Original_Node (E)) = N_Subtype_Indication
5443 and then (Ekind (Typ) /= E_Anonymous_Access_Type
5444 or else Is_Local_Anonymous_Access (Typ))
5445 then
5446 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
5447
5448 if Has_Discriminants (Subtyp) then
5449 Discrim := First_Discriminant (Base_Type (Subtyp));
5450 Constr := First (Constraints (Constraint (Original_Node (E))));
5451 while Present (Discrim) and then Present (Constr) loop
5452 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
5453 if Nkind (Constr) = N_Discriminant_Association then
5454 Disc_Exp := Expression (Constr);
5455 else
5456 Disc_Exp := Constr;
5457 end if;
5458
5459 Check_Allocator_Discrim_Accessibility_Exprs
5460 (Disc_Exp, Typ);
5461 end if;
5462
5463 Next_Discriminant (Discrim);
5464 Next (Constr);
5465 end loop;
5466 end if;
5467 end if;
5468 end if;
5469
5470 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
5471 -- check that the level of the type of the created object is not deeper
5472 -- than the level of the allocator's access type, since extensions can
5473 -- now occur at deeper levels than their ancestor types. This is a
5474 -- static accessibility level check; a run-time check is also needed in
5475 -- the case of an initialized allocator with a class-wide argument (see
5476 -- Expand_Allocator_Expression).
5477
5478 if Ada_Version >= Ada_2005
5479 and then Is_Class_Wide_Type (Desig_T)
5480 then
5481 declare
5482 Exp_Typ : Entity_Id;
5483
5484 begin
5485 if Nkind (E) = N_Qualified_Expression then
5486 Exp_Typ := Etype (E);
5487 elsif Nkind (E) = N_Subtype_Indication then
5488 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
5489 else
5490 Exp_Typ := Entity (E);
5491 end if;
5492
5493 if Type_Access_Level (Exp_Typ) >
5494 Deepest_Type_Access_Level (Typ)
5495 then
5496 if In_Instance_Body then
5497 Error_Msg_Warn := SPARK_Mode /= On;
5498 Error_Msg_N
5499 ("type in allocator has deeper level than designated "
5500 & "class-wide type<<", E);
5501 Error_Msg_N ("\Program_Error [<<", E);
5502
5503 Rewrite (N,
5504 Make_Raise_Program_Error (Sloc (N),
5505 Reason => PE_Accessibility_Check_Failed));
5506 Set_Etype (N, Typ);
5507
5508 -- Do not apply Ada 2005 accessibility checks on a class-wide
5509 -- allocator if the type given in the allocator is a formal
5510 -- type or within a formal package. A run-time check will be
5511 -- performed in the instance.
5512
5513 elsif not Is_Generic_Type (Exp_Typ)
5514 and then not In_Generic_Formal_Package (Exp_Typ)
5515 then
5516 Error_Msg_N
5517 ("type in allocator has deeper level than designated "
5518 & "class-wide type", E);
5519 end if;
5520 end if;
5521 end;
5522 end if;
5523
5524 -- Check for allocation from an empty storage pool. But do not complain
5525 -- if it's a return statement for a build-in-place function, because the
5526 -- allocator is there just in case the caller uses an allocator. If the
5527 -- caller does use an allocator, it will be caught at the call site.
5528
5529 if No_Pool_Assigned (Typ)
5530 and then not Alloc_For_BIP_Return (N)
5531 then
5532 Error_Msg_N ("allocation from empty storage pool!", N);
5533
5534 -- If the context is an unchecked conversion, as may happen within an
5535 -- inlined subprogram, the allocator is being resolved with its own
5536 -- anonymous type. In that case, if the target type has a specific
5537 -- storage pool, it must be inherited explicitly by the allocator type.
5538
5539 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
5540 and then No (Associated_Storage_Pool (Typ))
5541 then
5542 Set_Associated_Storage_Pool
5543 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
5544 end if;
5545
5546 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
5547 Check_Restriction (No_Anonymous_Allocators, N);
5548 end if;
5549
5550 -- Check that an allocator with task parts isn't for a nested access
5551 -- type when restriction No_Task_Hierarchy applies.
5552
5553 if not Is_Library_Level_Entity (Base_Type (Typ))
5554 and then Has_Task (Base_Type (Desig_T))
5555 then
5556 Check_Restriction (No_Task_Hierarchy, N);
5557 end if;
5558
5559 -- An illegal allocator may be rewritten as a raise Program_Error
5560 -- statement.
5561
5562 if Nkind (N) = N_Allocator then
5563
5564 -- Avoid coextension processing for an allocator that is the
5565 -- expansion of a build-in-place function call.
5566
5567 if Nkind (Original_Node (N)) = N_Allocator
5568 and then Nkind (Expression (Original_Node (N))) =
5569 N_Qualified_Expression
5570 and then Nkind (Expression (Expression (Original_Node (N)))) =
5571 N_Function_Call
5572 and then Is_Expanded_Build_In_Place_Call
5573 (Expression (Expression (Original_Node (N))))
5574 then
5575 null; -- b-i-p function call case
5576
5577 else
5578 -- An anonymous access discriminant is the definition of a
5579 -- coextension.
5580
5581 if Ekind (Typ) = E_Anonymous_Access_Type
5582 and then Nkind (Associated_Node_For_Itype (Typ)) =
5583 N_Discriminant_Specification
5584 then
5585 declare
5586 Discr : constant Entity_Id :=
5587 Defining_Identifier (Associated_Node_For_Itype (Typ));
5588
5589 begin
5590 Check_Restriction (No_Coextensions, N);
5591
5592 -- Ada 2012 AI05-0052: If the designated type of the
5593 -- allocator is limited, then the allocator shall not
5594 -- be used to define the value of an access discriminant
5595 -- unless the discriminated type is immutably limited.
5596
5597 if Ada_Version >= Ada_2012
5598 and then Is_Limited_Type (Desig_T)
5599 and then not Is_Limited_View (Scope (Discr))
5600 then
5601 Error_Msg_N
5602 ("only immutably limited types can have anonymous "
5603 & "access discriminants designating a limited type",
5604 N);
5605 end if;
5606 end;
5607
5608 -- Avoid marking an allocator as a dynamic coextension if it is
5609 -- within a static construct.
5610
5611 if not Is_Static_Coextension (N) then
5612 Set_Is_Dynamic_Coextension (N);
5613
5614 -- Finalization and deallocation of coextensions utilizes an
5615 -- approximate implementation which does not directly adhere
5616 -- to the semantic rules. Warn on potential issues involving
5617 -- coextensions.
5618
5619 if Is_Controlled (Desig_T) then
5620 Error_Msg_N
5621 ("??coextension will not be finalized when its "
5622 & "associated owner is deallocated or finalized", N);
5623 else
5624 Error_Msg_N
5625 ("??coextension will not be deallocated when its "
5626 & "associated owner is deallocated", N);
5627 end if;
5628 end if;
5629
5630 -- Cleanup for potential static coextensions
5631
5632 else
5633 Set_Is_Dynamic_Coextension (N, False);
5634 Set_Is_Static_Coextension (N, False);
5635
5636 -- Anonymous access-to-controlled objects are not finalized on
5637 -- time because this involves run-time ownership and currently
5638 -- this property is not available. In rare cases the object may
5639 -- not be finalized at all. Warn on potential issues involving
5640 -- anonymous access-to-controlled objects.
5641
5642 if Ekind (Typ) = E_Anonymous_Access_Type
5643 and then Is_Controlled_Active (Desig_T)
5644 then
5645 Error_Msg_N
5646 ("??object designated by anonymous access object might "
5647 & "not be finalized until its enclosing library unit "
5648 & "goes out of scope", N);
5649 Error_Msg_N ("\use named access type instead", N);
5650 end if;
5651 end if;
5652 end if;
5653 end if;
5654
5655 -- Report a simple error: if the designated object is a local task,
5656 -- its body has not been seen yet, and its activation will fail an
5657 -- elaboration check.
5658
5659 if Is_Task_Type (Desig_T)
5660 and then Scope (Base_Type (Desig_T)) = Current_Scope
5661 and then Is_Compilation_Unit (Current_Scope)
5662 and then Ekind (Current_Scope) = E_Package
5663 and then not In_Package_Body (Current_Scope)
5664 then
5665 Error_Msg_Warn := SPARK_Mode /= On;
5666 Error_Msg_N ("cannot activate task before body seen<<", N);
5667 Error_Msg_N ("\Program_Error [<<", N);
5668 end if;
5669
5670 -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a
5671 -- type with a task component on a subpool. This action must raise
5672 -- Program_Error at runtime.
5673
5674 if Ada_Version >= Ada_2012
5675 and then Nkind (N) = N_Allocator
5676 and then Present (Subpool_Handle_Name (N))
5677 and then Has_Task (Desig_T)
5678 then
5679 Error_Msg_Warn := SPARK_Mode /= On;
5680 Error_Msg_N ("cannot allocate task on subpool<<", N);
5681 Error_Msg_N ("\Program_Error [<<", N);
5682
5683 Rewrite (N,
5684 Make_Raise_Program_Error (Sloc (N),
5685 Reason => PE_Explicit_Raise));
5686 Set_Etype (N, Typ);
5687 end if;
5688 end Resolve_Allocator;
5689
5690 ---------------------------
5691 -- Resolve_Arithmetic_Op --
5692 ---------------------------
5693
5694 -- Used for resolving all arithmetic operators except exponentiation
5695
5696 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
5697 L : constant Node_Id := Left_Opnd (N);
5698 R : constant Node_Id := Right_Opnd (N);
5699 TL : constant Entity_Id := Base_Type (Etype (L));
5700 TR : constant Entity_Id := Base_Type (Etype (R));
5701 T : Entity_Id;
5702 Rop : Node_Id;
5703
5704 B_Typ : constant Entity_Id := Base_Type (Typ);
5705 -- We do the resolution using the base type, because intermediate values
5706 -- in expressions always are of the base type, not a subtype of it.
5707
5708 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
5709 -- Returns True if N is in a context that expects "any real type"
5710
5711 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
5712 -- Return True iff given type is Integer or universal real/integer
5713
5714 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
5715 -- Choose type of integer literal in fixed-point operation to conform
5716 -- to available fixed-point type. T is the type of the other operand,
5717 -- which is needed to determine the expected type of N.
5718
5719 procedure Set_Operand_Type (N : Node_Id);
5720 -- Set operand type to T if universal
5721
5722 -------------------------------
5723 -- Expected_Type_Is_Any_Real --
5724 -------------------------------
5725
5726 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
5727 begin
5728 -- N is the expression after "delta" in a fixed_point_definition;
5729 -- see RM-3.5.9(6):
5730
5731 return Nkind (Parent (N)) in N_Ordinary_Fixed_Point_Definition
5732 | N_Decimal_Fixed_Point_Definition
5733
5734 -- N is one of the bounds in a real_range_specification;
5735 -- see RM-3.5.7(5):
5736
5737 | N_Real_Range_Specification
5738
5739 -- N is the expression of a delta_constraint;
5740 -- see RM-J.3(3):
5741
5742 | N_Delta_Constraint;
5743 end Expected_Type_Is_Any_Real;
5744
5745 -----------------------------
5746 -- Is_Integer_Or_Universal --
5747 -----------------------------
5748
5749 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
5750 T : Entity_Id;
5751 Index : Interp_Index;
5752 It : Interp;
5753
5754 begin
5755 if not Is_Overloaded (N) then
5756 T := Etype (N);
5757 return Base_Type (T) = Base_Type (Standard_Integer)
5758 or else Is_Universal_Numeric_Type (T);
5759 else
5760 Get_First_Interp (N, Index, It);
5761 while Present (It.Typ) loop
5762 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
5763 or else Is_Universal_Numeric_Type (It.Typ)
5764 then
5765 return True;
5766 end if;
5767
5768 Get_Next_Interp (Index, It);
5769 end loop;
5770 end if;
5771
5772 return False;
5773 end Is_Integer_Or_Universal;
5774
5775 ----------------------------
5776 -- Set_Mixed_Mode_Operand --
5777 ----------------------------
5778
5779 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
5780 Index : Interp_Index;
5781 It : Interp;
5782
5783 begin
5784 if Universal_Interpretation (N) = Universal_Integer then
5785
5786 -- A universal integer literal is resolved as standard integer
5787 -- except in the case of a fixed-point result, where we leave it
5788 -- as universal (to be handled by Exp_Fixd later on)
5789
5790 if Is_Fixed_Point_Type (T) then
5791 Resolve (N, Universal_Integer);
5792 else
5793 Resolve (N, Standard_Integer);
5794 end if;
5795
5796 elsif Universal_Interpretation (N) = Universal_Real
5797 and then (T = Base_Type (Standard_Integer)
5798 or else Is_Universal_Numeric_Type (T))
5799 then
5800 -- A universal real can appear in a fixed-type context. We resolve
5801 -- the literal with that context, even though this might raise an
5802 -- exception prematurely (the other operand may be zero).
5803
5804 Resolve (N, B_Typ);
5805
5806 elsif Etype (N) = Base_Type (Standard_Integer)
5807 and then T = Universal_Real
5808 and then Is_Overloaded (N)
5809 then
5810 -- Integer arg in mixed-mode operation. Resolve with universal
5811 -- type, in case preference rule must be applied.
5812
5813 Resolve (N, Universal_Integer);
5814
5815 elsif Etype (N) = T and then B_Typ /= Universal_Fixed then
5816
5817 -- If the operand is part of a fixed multiplication operation,
5818 -- a conversion will be applied to each operand, so resolve it
5819 -- with its own type.
5820
5821 if Nkind (Parent (N)) in N_Op_Divide | N_Op_Multiply then
5822 Resolve (N);
5823
5824 else
5825 -- Not a mixed-mode operation, resolve with context
5826
5827 Resolve (N, B_Typ);
5828 end if;
5829
5830 elsif Etype (N) = Any_Fixed then
5831
5832 -- N may itself be a mixed-mode operation, so use context type
5833
5834 Resolve (N, B_Typ);
5835
5836 elsif Is_Fixed_Point_Type (T)
5837 and then B_Typ = Universal_Fixed
5838 and then Is_Overloaded (N)
5839 then
5840 -- Must be (fixed * fixed) operation, operand must have one
5841 -- compatible interpretation.
5842
5843 Resolve (N, Any_Fixed);
5844
5845 elsif Is_Fixed_Point_Type (B_Typ)
5846 and then (T = Universal_Real or else Is_Fixed_Point_Type (T))
5847 and then Is_Overloaded (N)
5848 then
5849 -- C * F(X) in a fixed context, where C is a real literal or a
5850 -- fixed-point expression. F must have either a fixed type
5851 -- interpretation or an integer interpretation, but not both.
5852
5853 Get_First_Interp (N, Index, It);
5854 while Present (It.Typ) loop
5855 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
5856 if Analyzed (N) then
5857 Error_Msg_N ("ambiguous operand in fixed operation", N);
5858 else
5859 Resolve (N, Standard_Integer);
5860 end if;
5861
5862 elsif Is_Fixed_Point_Type (It.Typ) then
5863 if Analyzed (N) then
5864 Error_Msg_N ("ambiguous operand in fixed operation", N);
5865 else
5866 Resolve (N, It.Typ);
5867 end if;
5868 end if;
5869
5870 Get_Next_Interp (Index, It);
5871 end loop;
5872
5873 -- Reanalyze the literal with the fixed type of the context. If
5874 -- context is Universal_Fixed, we are within a conversion, leave
5875 -- the literal as a universal real because there is no usable
5876 -- fixed type, and the target of the conversion plays no role in
5877 -- the resolution.
5878
5879 declare
5880 Op2 : Node_Id;
5881 T2 : Entity_Id;
5882
5883 begin
5884 if N = L then
5885 Op2 := R;
5886 else
5887 Op2 := L;
5888 end if;
5889
5890 if B_Typ = Universal_Fixed
5891 and then Nkind (Op2) = N_Real_Literal
5892 then
5893 T2 := Universal_Real;
5894 else
5895 T2 := B_Typ;
5896 end if;
5897
5898 Set_Analyzed (Op2, False);
5899 Resolve (Op2, T2);
5900 end;
5901
5902 -- A universal real conditional expression can appear in a fixed-type
5903 -- context and must be resolved with that context to facilitate the
5904 -- code generation in the back end. However, If the context is
5905 -- Universal_fixed (i.e. as an operand of a multiplication/division
5906 -- involving a fixed-point operand) the conditional expression must
5907 -- resolve to a unique visible fixed_point type, normally Duration.
5908
5909 elsif Nkind (N) in N_Case_Expression | N_If_Expression
5910 and then Etype (N) = Universal_Real
5911 and then Is_Fixed_Point_Type (B_Typ)
5912 then
5913 if B_Typ = Universal_Fixed then
5914 Resolve (N, Unique_Fixed_Point_Type (N));
5915
5916 else
5917 Resolve (N, B_Typ);
5918 end if;
5919
5920 else
5921 Resolve (N);
5922 end if;
5923 end Set_Mixed_Mode_Operand;
5924
5925 ----------------------
5926 -- Set_Operand_Type --
5927 ----------------------
5928
5929 procedure Set_Operand_Type (N : Node_Id) is
5930 begin
5931 if Is_Universal_Numeric_Type (Etype (N)) then
5932 Set_Etype (N, T);
5933 end if;
5934 end Set_Operand_Type;
5935
5936 -- Start of processing for Resolve_Arithmetic_Op
5937
5938 begin
5939 if Comes_From_Source (N)
5940 and then Ekind (Entity (N)) = E_Function
5941 and then Is_Imported (Entity (N))
5942 and then Is_Intrinsic_Subprogram (Entity (N))
5943 then
5944 Resolve_Intrinsic_Operator (N, Typ);
5945 return;
5946
5947 -- Special-case for mixed-mode universal expressions or fixed point type
5948 -- operation: each argument is resolved separately. The same treatment
5949 -- is required if one of the operands of a fixed point operation is
5950 -- universal real, since in this case we don't do a conversion to a
5951 -- specific fixed-point type (instead the expander handles the case).
5952
5953 -- Set the type of the node to its universal interpretation because
5954 -- legality checks on an exponentiation operand need the context.
5955
5956 elsif Is_Universal_Numeric_Type (B_Typ)
5957 and then Present (Universal_Interpretation (L))
5958 and then Present (Universal_Interpretation (R))
5959 then
5960 Set_Etype (N, B_Typ);
5961 Resolve (L, Universal_Interpretation (L));
5962 Resolve (R, Universal_Interpretation (R));
5963
5964 elsif (B_Typ = Universal_Real
5965 or else Etype (N) = Universal_Fixed
5966 or else (Etype (N) = Any_Fixed
5967 and then Is_Fixed_Point_Type (B_Typ))
5968 or else (Is_Fixed_Point_Type (B_Typ)
5969 and then (Is_Integer_Or_Universal (L)
5970 or else
5971 Is_Integer_Or_Universal (R))))
5972 and then Nkind (N) in N_Op_Multiply | N_Op_Divide
5973 then
5974 if TL = Universal_Integer or else TR = Universal_Integer then
5975 Check_For_Visible_Operator (N, B_Typ);
5976 end if;
5977
5978 -- If context is a fixed type and one operand is integer, the other
5979 -- is resolved with the type of the context.
5980
5981 if Is_Fixed_Point_Type (B_Typ)
5982 and then (Base_Type (TL) = Base_Type (Standard_Integer)
5983 or else TL = Universal_Integer)
5984 then
5985 Resolve (R, B_Typ);
5986 Resolve (L, TL);
5987
5988 elsif Is_Fixed_Point_Type (B_Typ)
5989 and then (Base_Type (TR) = Base_Type (Standard_Integer)
5990 or else TR = Universal_Integer)
5991 then
5992 Resolve (L, B_Typ);
5993 Resolve (R, TR);
5994
5995 -- If both operands are universal and the context is a floating
5996 -- point type, the operands are resolved to the type of the context.
5997
5998 elsif Is_Floating_Point_Type (B_Typ) then
5999 Resolve (L, B_Typ);
6000 Resolve (R, B_Typ);
6001
6002 else
6003 Set_Mixed_Mode_Operand (L, TR);
6004 Set_Mixed_Mode_Operand (R, TL);
6005 end if;
6006
6007 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
6008 -- multiplying operators from being used when the expected type is
6009 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
6010 -- some cases where the expected type is actually Any_Real;
6011 -- Expected_Type_Is_Any_Real takes care of that case.
6012
6013 if Etype (N) = Universal_Fixed
6014 or else Etype (N) = Any_Fixed
6015 then
6016 if B_Typ = Universal_Fixed
6017 and then not Expected_Type_Is_Any_Real (N)
6018 and then Nkind (Parent (N)) not in
6019 N_Type_Conversion | N_Unchecked_Type_Conversion
6020 then
6021 Error_Msg_N ("type cannot be determined from context!", N);
6022 Error_Msg_N ("\explicit conversion to result type required", N);
6023
6024 Set_Etype (L, Any_Type);
6025 Set_Etype (R, Any_Type);
6026
6027 else
6028 if Ada_Version = Ada_83
6029 and then Etype (N) = Universal_Fixed
6030 and then Nkind (Parent (N)) not in
6031 N_Type_Conversion | N_Unchecked_Type_Conversion
6032 then
6033 Error_Msg_N
6034 ("(Ada 83) fixed-point operation needs explicit "
6035 & "conversion", N);
6036 end if;
6037
6038 -- The expected type is "any real type" in contexts like
6039
6040 -- type T is delta <universal_fixed-expression> ...
6041
6042 -- in which case we need to set the type to Universal_Real
6043 -- so that static expression evaluation will work properly.
6044
6045 if Expected_Type_Is_Any_Real (N) then
6046 Set_Etype (N, Universal_Real);
6047 else
6048 Set_Etype (N, B_Typ);
6049 end if;
6050 end if;
6051
6052 elsif Is_Fixed_Point_Type (B_Typ)
6053 and then (Is_Integer_Or_Universal (L)
6054 or else Nkind (L) = N_Real_Literal
6055 or else Nkind (R) = N_Real_Literal
6056 or else Is_Integer_Or_Universal (R))
6057 then
6058 Set_Etype (N, B_Typ);
6059
6060 elsif Etype (N) = Any_Fixed then
6061
6062 -- If no previous errors, this is only possible if one operand is
6063 -- overloaded and the context is universal. Resolve as such.
6064
6065 Set_Etype (N, B_Typ);
6066 end if;
6067
6068 else
6069 if Is_Universal_Numeric_Type (TL)
6070 and then
6071 Is_Universal_Numeric_Type (TR)
6072 then
6073 Check_For_Visible_Operator (N, B_Typ);
6074 end if;
6075
6076 -- If the context is Universal_Fixed and the operands are also
6077 -- universal fixed, this is an error, unless there is only one
6078 -- applicable fixed_point type (usually Duration).
6079
6080 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
6081 T := Unique_Fixed_Point_Type (N);
6082
6083 if T = Any_Type then
6084 Set_Etype (N, T);
6085 return;
6086 else
6087 Resolve (L, T);
6088 Resolve (R, T);
6089 end if;
6090
6091 else
6092 Resolve (L, B_Typ);
6093 Resolve (R, B_Typ);
6094 end if;
6095
6096 -- If one of the arguments was resolved to a non-universal type.
6097 -- label the result of the operation itself with the same type.
6098 -- Do the same for the universal argument, if any.
6099
6100 T := Intersect_Types (L, R);
6101 Set_Etype (N, Base_Type (T));
6102 Set_Operand_Type (L);
6103 Set_Operand_Type (R);
6104 end if;
6105
6106 Generate_Operator_Reference (N, Typ);
6107 Analyze_Dimension (N);
6108 Eval_Arithmetic_Op (N);
6109
6110 -- Set overflow and division checking bit
6111
6112 if Nkind (N) in N_Op then
6113 if not Overflow_Checks_Suppressed (Etype (N)) then
6114 Enable_Overflow_Check (N);
6115 end if;
6116
6117 -- Give warning if explicit division by zero
6118
6119 if Nkind (N) in N_Op_Divide | N_Op_Rem | N_Op_Mod
6120 and then not Division_Checks_Suppressed (Etype (N))
6121 then
6122 Rop := Right_Opnd (N);
6123
6124 if Compile_Time_Known_Value (Rop)
6125 and then ((Is_Integer_Type (Etype (Rop))
6126 and then Expr_Value (Rop) = Uint_0)
6127 or else
6128 (Is_Real_Type (Etype (Rop))
6129 and then Expr_Value_R (Rop) = Ureal_0))
6130 then
6131 -- Specialize the warning message according to the operation.
6132 -- When SPARK_Mode is On, force a warning instead of an error
6133 -- in that case, as this likely corresponds to deactivated
6134 -- code. The following warnings are for the case
6135
6136 case Nkind (N) is
6137 when N_Op_Divide =>
6138
6139 -- For division, we have two cases, for float division
6140 -- of an unconstrained float type, on a machine where
6141 -- Machine_Overflows is false, we don't get an exception
6142 -- at run-time, but rather an infinity or Nan. The Nan
6143 -- case is pretty obscure, so just warn about infinities.
6144
6145 if Is_Floating_Point_Type (Typ)
6146 and then not Is_Constrained (Typ)
6147 and then not Machine_Overflows_On_Target
6148 then
6149 Error_Msg_N
6150 ("float division by zero, may generate "
6151 & "'+'/'- infinity??", Right_Opnd (N));
6152
6153 -- For all other cases, we get a Constraint_Error
6154
6155 else
6156 Apply_Compile_Time_Constraint_Error
6157 (N, "division by zero??", CE_Divide_By_Zero,
6158 Loc => Sloc (Right_Opnd (N)),
6159 Warn => SPARK_Mode = On);
6160 end if;
6161
6162 when N_Op_Rem =>
6163 Apply_Compile_Time_Constraint_Error
6164 (N, "rem with zero divisor??", CE_Divide_By_Zero,
6165 Loc => Sloc (Right_Opnd (N)),
6166 Warn => SPARK_Mode = On);
6167
6168 when N_Op_Mod =>
6169 Apply_Compile_Time_Constraint_Error
6170 (N, "mod with zero divisor??", CE_Divide_By_Zero,
6171 Loc => Sloc (Right_Opnd (N)),
6172 Warn => SPARK_Mode = On);
6173
6174 -- Division by zero can only happen with division, rem,
6175 -- and mod operations.
6176
6177 when others =>
6178 raise Program_Error;
6179 end case;
6180
6181 -- In GNATprove mode, we enable the division check so that
6182 -- GNATprove will issue a message if it cannot be proved.
6183
6184 if GNATprove_Mode then
6185 Activate_Division_Check (N);
6186 end if;
6187
6188 -- Otherwise just set the flag to check at run time
6189
6190 else
6191 Activate_Division_Check (N);
6192 end if;
6193 end if;
6194
6195 -- If Restriction No_Implicit_Conditionals is active, then it is
6196 -- violated if either operand can be negative for mod, or for rem
6197 -- if both operands can be negative.
6198
6199 if Restriction_Check_Required (No_Implicit_Conditionals)
6200 and then Nkind (N) in N_Op_Rem | N_Op_Mod
6201 then
6202 declare
6203 Lo : Uint;
6204 Hi : Uint;
6205 OK : Boolean;
6206
6207 LNeg : Boolean;
6208 RNeg : Boolean;
6209 -- Set if corresponding operand might be negative
6210
6211 begin
6212 Determine_Range
6213 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
6214 LNeg := (not OK) or else Lo < 0;
6215
6216 Determine_Range
6217 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
6218 RNeg := (not OK) or else Lo < 0;
6219
6220 -- Check if we will be generating conditionals. There are two
6221 -- cases where that can happen, first for REM, the only case
6222 -- is largest negative integer mod -1, where the division can
6223 -- overflow, but we still have to give the right result. The
6224 -- front end generates a test for this annoying case. Here we
6225 -- just test if both operands can be negative (that's what the
6226 -- expander does, so we match its logic here).
6227
6228 -- The second case is mod where either operand can be negative.
6229 -- In this case, the back end has to generate additional tests.
6230
6231 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
6232 or else
6233 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
6234 then
6235 Check_Restriction (No_Implicit_Conditionals, N);
6236 end if;
6237 end;
6238 end if;
6239 end if;
6240
6241 Check_Unset_Reference (L);
6242 Check_Unset_Reference (R);
6243 end Resolve_Arithmetic_Op;
6244
6245 ------------------
6246 -- Resolve_Call --
6247 ------------------
6248
6249 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
6250 Loc : constant Source_Ptr := Sloc (N);
6251 Subp : constant Node_Id := Name (N);
6252 Body_Id : Entity_Id;
6253 I : Interp_Index;
6254 It : Interp;
6255 Nam : Entity_Id;
6256 Nam_Decl : Node_Id;
6257 Nam_UA : Entity_Id;
6258 Norm_OK : Boolean;
6259 Rtype : Entity_Id;
6260 Scop : Entity_Id;
6261
6262 begin
6263 -- Preserve relevant elaboration-related attributes of the context which
6264 -- are no longer available or very expensive to recompute once analysis,
6265 -- resolution, and expansion are over.
6266
6267 Mark_Elaboration_Attributes
6268 (N_Id => N,
6269 Checks => True,
6270 Modes => True,
6271 Warnings => True);
6272
6273 -- The context imposes a unique interpretation with type Typ on a
6274 -- procedure or function call. Find the entity of the subprogram that
6275 -- yields the expected type, and propagate the corresponding formal
6276 -- constraints on the actuals. The caller has established that an
6277 -- interpretation exists, and emitted an error if not unique.
6278
6279 -- First deal with the case of a call to an access-to-subprogram,
6280 -- dereference made explicit in Analyze_Call.
6281
6282 if Ekind (Etype (Subp)) = E_Subprogram_Type then
6283 if not Is_Overloaded (Subp) then
6284 Nam := Etype (Subp);
6285
6286 else
6287 -- Find the interpretation whose type (a subprogram type) has a
6288 -- return type that is compatible with the context. Analysis of
6289 -- the node has established that one exists.
6290
6291 Nam := Empty;
6292
6293 Get_First_Interp (Subp, I, It);
6294 while Present (It.Typ) loop
6295 if Covers (Typ, Etype (It.Typ)) then
6296 Nam := It.Typ;
6297 exit;
6298 end if;
6299
6300 Get_Next_Interp (I, It);
6301 end loop;
6302
6303 if No (Nam) then
6304 raise Program_Error;
6305 end if;
6306 end if;
6307
6308 -- If the prefix is not an entity, then resolve it
6309
6310 if not Is_Entity_Name (Subp) then
6311 Resolve (Subp, Nam);
6312 end if;
6313
6314 -- For an indirect call, we always invalidate checks, since we do not
6315 -- know whether the subprogram is local or global. Yes we could do
6316 -- better here, e.g. by knowing that there are no local subprograms,
6317 -- but it does not seem worth the effort. Similarly, we kill all
6318 -- knowledge of current constant values.
6319
6320 Kill_Current_Values;
6321
6322 -- If this is a procedure call which is really an entry call, do
6323 -- the conversion of the procedure call to an entry call. Protected
6324 -- operations use the same circuitry because the name in the call
6325 -- can be an arbitrary expression with special resolution rules.
6326
6327 elsif Nkind (Subp) in N_Selected_Component | N_Indexed_Component
6328 or else (Is_Entity_Name (Subp) and then Is_Entry (Entity (Subp)))
6329 then
6330 Resolve_Entry_Call (N, Typ);
6331
6332 if Legacy_Elaboration_Checks then
6333 Check_Elab_Call (N);
6334 end if;
6335
6336 -- Annotate the tree by creating a call marker in case the original
6337 -- call is transformed by expansion. The call marker is automatically
6338 -- saved for later examination by the ABE Processing phase.
6339
6340 Build_Call_Marker (N);
6341
6342 -- Kill checks and constant values, as above for indirect case
6343 -- Who knows what happens when another task is activated?
6344
6345 Kill_Current_Values;
6346 return;
6347
6348 -- Normal subprogram call with name established in Resolve
6349
6350 elsif not Is_Type (Entity (Subp)) then
6351 Nam := Entity (Subp);
6352 Set_Entity_With_Checks (Subp, Nam);
6353
6354 -- Otherwise we must have the case of an overloaded call
6355
6356 else
6357 pragma Assert (Is_Overloaded (Subp));
6358
6359 -- Initialize Nam to prevent warning (we know it will be assigned
6360 -- in the loop below, but the compiler does not know that).
6361
6362 Nam := Empty;
6363
6364 Get_First_Interp (Subp, I, It);
6365 while Present (It.Typ) loop
6366 if Covers (Typ, It.Typ) then
6367 Nam := It.Nam;
6368 Set_Entity_With_Checks (Subp, Nam);
6369 exit;
6370 end if;
6371
6372 Get_Next_Interp (I, It);
6373 end loop;
6374 end if;
6375
6376 -- Check that a call to Current_Task does not occur in an entry body
6377
6378 if Is_RTE (Nam, RE_Current_Task) then
6379 declare
6380 P : Node_Id;
6381
6382 begin
6383 P := N;
6384 loop
6385 P := Parent (P);
6386
6387 -- Exclude calls that occur within the default of a formal
6388 -- parameter of the entry, since those are evaluated outside
6389 -- of the body.
6390
6391 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
6392
6393 if Nkind (P) = N_Entry_Body
6394 or else (Nkind (P) = N_Subprogram_Body
6395 and then Is_Entry_Barrier_Function (P))
6396 then
6397 Rtype := Etype (N);
6398 Error_Msg_Warn := SPARK_Mode /= On;
6399 Error_Msg_NE
6400 ("& should not be used in entry body (RM C.7(17))<<",
6401 N, Nam);
6402 Error_Msg_NE ("\Program_Error [<<", N, Nam);
6403 Rewrite (N,
6404 Make_Raise_Program_Error (Loc,
6405 Reason => PE_Current_Task_In_Entry_Body));
6406 Set_Etype (N, Rtype);
6407 return;
6408 end if;
6409 end loop;
6410 end;
6411 end if;
6412
6413 -- Check that a procedure call does not occur in the context of the
6414 -- entry call statement of a conditional or timed entry call. Note that
6415 -- the case of a call to a subprogram renaming of an entry will also be
6416 -- rejected. The test for N not being an N_Entry_Call_Statement is
6417 -- defensive, covering the possibility that the processing of entry
6418 -- calls might reach this point due to later modifications of the code
6419 -- above.
6420
6421 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6422 and then Nkind (N) /= N_Entry_Call_Statement
6423 and then Entry_Call_Statement (Parent (N)) = N
6424 then
6425 if Ada_Version < Ada_2005 then
6426 Error_Msg_N ("entry call required in select statement", N);
6427
6428 -- Ada 2005 (AI-345): If a procedure_call_statement is used
6429 -- for a procedure_or_entry_call, the procedure_name or
6430 -- procedure_prefix of the procedure_call_statement shall denote
6431 -- an entry renamed by a procedure, or (a view of) a primitive
6432 -- subprogram of a limited interface whose first parameter is
6433 -- a controlling parameter.
6434
6435 elsif Nkind (N) = N_Procedure_Call_Statement
6436 and then not Is_Renamed_Entry (Nam)
6437 and then not Is_Controlling_Limited_Procedure (Nam)
6438 then
6439 Error_Msg_N
6440 ("entry call or dispatching primitive of interface required", N);
6441 end if;
6442 end if;
6443
6444 -- Check that this is not a call to a protected procedure or entry from
6445 -- within a protected function.
6446
6447 Check_Internal_Protected_Use (N, Nam);
6448
6449 -- Freeze the subprogram name if not in a spec-expression. Note that
6450 -- we freeze procedure calls as well as function calls. Procedure calls
6451 -- are not frozen according to the rules (RM 13.14(14)) because it is
6452 -- impossible to have a procedure call to a non-frozen procedure in
6453 -- pure Ada, but in the code that we generate in the expander, this
6454 -- rule needs extending because we can generate procedure calls that
6455 -- need freezing.
6456
6457 -- In Ada 2012, expression functions may be called within pre/post
6458 -- conditions of subsequent functions or expression functions. Such
6459 -- calls do not freeze when they appear within generated bodies,
6460 -- (including the body of another expression function) which would
6461 -- place the freeze node in the wrong scope. An expression function
6462 -- is frozen in the usual fashion, by the appearance of a real body,
6463 -- or at the end of a declarative part. However an implicit call to
6464 -- an expression function may appear when it is part of a default
6465 -- expression in a call to an initialization procedure, and must be
6466 -- frozen now, even if the body is inserted at a later point.
6467 -- Otherwise, the call freezes the expression if expander is active,
6468 -- for example as part of an object declaration.
6469
6470 if Is_Entity_Name (Subp)
6471 and then not In_Spec_Expression
6472 and then not Is_Expression_Function_Or_Completion (Current_Scope)
6473 and then
6474 (not Is_Expression_Function_Or_Completion (Entity (Subp))
6475 or else Expander_Active)
6476 then
6477 if Is_Expression_Function (Entity (Subp)) then
6478
6479 -- Force freeze of expression function in call
6480
6481 Set_Comes_From_Source (Subp, True);
6482 Set_Must_Not_Freeze (Subp, False);
6483 end if;
6484
6485 Freeze_Expression (Subp);
6486 end if;
6487
6488 -- For a predefined operator, the type of the result is the type imposed
6489 -- by context, except for a predefined operation on universal fixed.
6490 -- Otherwise the type of the call is the type returned by the subprogram
6491 -- being called.
6492
6493 if Is_Predefined_Op (Nam) then
6494 if Etype (N) /= Universal_Fixed then
6495 Set_Etype (N, Typ);
6496 end if;
6497
6498 -- If the subprogram returns an array type, and the context requires the
6499 -- component type of that array type, the node is really an indexing of
6500 -- the parameterless call. Resolve as such. A pathological case occurs
6501 -- when the type of the component is an access to the array type. In
6502 -- this case the call is truly ambiguous. If the call is to an intrinsic
6503 -- subprogram, it can't be an indexed component. This check is necessary
6504 -- because if it's Unchecked_Conversion, and we have "type T_Ptr is
6505 -- access T;" and "type T is array (...) of T_Ptr;" (i.e. an array of
6506 -- pointers to the same array), the compiler gets confused and does an
6507 -- infinite recursion.
6508
6509 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
6510 and then
6511 ((Is_Array_Type (Etype (Nam))
6512 and then Covers (Typ, Component_Type (Etype (Nam))))
6513 or else
6514 (Is_Access_Type (Etype (Nam))
6515 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6516 and then
6517 Covers (Typ, Component_Type (Designated_Type (Etype (Nam))))
6518 and then not Is_Intrinsic_Subprogram (Entity (Subp))))
6519 then
6520 declare
6521 Index_Node : Node_Id;
6522 New_Subp : Node_Id;
6523 Ret_Type : constant Entity_Id := Etype (Nam);
6524
6525 begin
6526 -- If this is a parameterless call there is no ambiguity and the
6527 -- call has the type of the function.
6528
6529 if No (First_Actual (N)) then
6530 Set_Etype (N, Etype (Nam));
6531
6532 if Present (First_Formal (Nam)) then
6533 Resolve_Actuals (N, Nam);
6534 end if;
6535
6536 -- Annotate the tree by creating a call marker in case the
6537 -- original call is transformed by expansion. The call marker
6538 -- is automatically saved for later examination by the ABE
6539 -- Processing phase.
6540
6541 Build_Call_Marker (N);
6542
6543 elsif Is_Access_Type (Ret_Type)
6544
6545 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
6546 then
6547 Error_Msg_N
6548 ("cannot disambiguate function call and indexing", N);
6549 else
6550 New_Subp := Relocate_Node (Subp);
6551
6552 -- The called entity may be an explicit dereference, in which
6553 -- case there is no entity to set.
6554
6555 if Nkind (New_Subp) /= N_Explicit_Dereference then
6556 Set_Entity (Subp, Nam);
6557 end if;
6558
6559 if (Is_Array_Type (Ret_Type)
6560 and then Component_Type (Ret_Type) /= Any_Type)
6561 or else
6562 (Is_Access_Type (Ret_Type)
6563 and then
6564 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
6565 then
6566 if Needs_No_Actuals (Nam) then
6567
6568 -- Indexed call to a parameterless function
6569
6570 Index_Node :=
6571 Make_Indexed_Component (Loc,
6572 Prefix =>
6573 Make_Function_Call (Loc, Name => New_Subp),
6574 Expressions => Parameter_Associations (N));
6575 else
6576 -- An Ada 2005 prefixed call to a primitive operation
6577 -- whose first parameter is the prefix. This prefix was
6578 -- prepended to the parameter list, which is actually a
6579 -- list of indexes. Remove the prefix in order to build
6580 -- the proper indexed component.
6581
6582 Index_Node :=
6583 Make_Indexed_Component (Loc,
6584 Prefix =>
6585 Make_Function_Call (Loc,
6586 Name => New_Subp,
6587 Parameter_Associations =>
6588 New_List
6589 (Remove_Head (Parameter_Associations (N)))),
6590 Expressions => Parameter_Associations (N));
6591 end if;
6592
6593 -- Preserve the parenthesis count of the node
6594
6595 Set_Paren_Count (Index_Node, Paren_Count (N));
6596
6597 -- Since we are correcting a node classification error made
6598 -- by the parser, we call Replace rather than Rewrite.
6599
6600 Replace (N, Index_Node);
6601
6602 Set_Etype (Prefix (N), Ret_Type);
6603 Set_Etype (N, Typ);
6604
6605 if Legacy_Elaboration_Checks then
6606 Check_Elab_Call (Prefix (N));
6607 end if;
6608
6609 -- Annotate the tree by creating a call marker in case
6610 -- the original call is transformed by expansion. The call
6611 -- marker is automatically saved for later examination by
6612 -- the ABE Processing phase.
6613
6614 Build_Call_Marker (Prefix (N));
6615
6616 Resolve_Indexed_Component (N, Typ);
6617 end if;
6618 end if;
6619
6620 return;
6621 end;
6622
6623 else
6624 -- If the called function is not declared in the main unit and it
6625 -- returns the limited view of type then use the available view (as
6626 -- is done in Try_Object_Operation) to prevent back-end confusion;
6627 -- for the function entity itself. The call must appear in a context
6628 -- where the nonlimited view is available. If the function entity is
6629 -- in the extended main unit then no action is needed, because the
6630 -- back end handles this case. In either case the type of the call
6631 -- is the nonlimited view.
6632
6633 if From_Limited_With (Etype (Nam))
6634 and then Present (Available_View (Etype (Nam)))
6635 then
6636 Set_Etype (N, Available_View (Etype (Nam)));
6637
6638 if not In_Extended_Main_Code_Unit (Nam) then
6639 Set_Etype (Nam, Available_View (Etype (Nam)));
6640 end if;
6641
6642 else
6643 Set_Etype (N, Etype (Nam));
6644 end if;
6645 end if;
6646
6647 -- In the case where the call is to an overloaded subprogram, Analyze
6648 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
6649 -- such a case Normalize_Actuals needs to be called once more to order
6650 -- the actuals correctly. Otherwise the call will have the ordering
6651 -- given by the last overloaded subprogram whether this is the correct
6652 -- one being called or not.
6653
6654 if Is_Overloaded (Subp) then
6655 Normalize_Actuals (N, Nam, False, Norm_OK);
6656 pragma Assert (Norm_OK);
6657 end if;
6658
6659 -- In any case, call is fully resolved now. Reset Overload flag, to
6660 -- prevent subsequent overload resolution if node is analyzed again
6661
6662 Set_Is_Overloaded (Subp, False);
6663 Set_Is_Overloaded (N, False);
6664
6665 -- A Ghost entity must appear in a specific context
6666
6667 if Is_Ghost_Entity (Nam) and then Comes_From_Source (N) then
6668 Check_Ghost_Context (Nam, N);
6669 end if;
6670
6671 -- If we are calling the current subprogram from immediately within its
6672 -- body, then that is the case where we can sometimes detect cases of
6673 -- infinite recursion statically. Do not try this in case restriction
6674 -- No_Recursion is in effect anyway, and do it only for source calls.
6675
6676 if Comes_From_Source (N) then
6677 Scop := Current_Scope;
6678
6679 -- Issue warning for possible infinite recursion in the absence
6680 -- of the No_Recursion restriction.
6681
6682 if Same_Or_Aliased_Subprograms (Nam, Scop)
6683 and then not Restriction_Active (No_Recursion)
6684 and then not Is_Static_Function (Scop)
6685 and then Check_Infinite_Recursion (N)
6686 then
6687 -- Here we detected and flagged an infinite recursion, so we do
6688 -- not need to test the case below for further warnings. Also we
6689 -- are all done if we now have a raise SE node.
6690
6691 if Nkind (N) = N_Raise_Storage_Error then
6692 return;
6693 end if;
6694
6695 -- If call is to immediately containing subprogram, then check for
6696 -- the case of a possible run-time detectable infinite recursion.
6697
6698 else
6699 Scope_Loop : while Scop /= Standard_Standard loop
6700 if Same_Or_Aliased_Subprograms (Nam, Scop) then
6701
6702 -- Ada 202x (AI12-0075): Static functions are never allowed
6703 -- to make a recursive call, as specified by 6.8(5.4/5).
6704
6705 if Is_Static_Function (Scop) then
6706 Error_Msg_N
6707 ("recursive call not allowed in static expression "
6708 & "function", N);
6709
6710 Set_Error_Posted (Scop);
6711
6712 exit Scope_Loop;
6713 end if;
6714
6715 -- Although in general case, recursion is not statically
6716 -- checkable, the case of calling an immediately containing
6717 -- subprogram is easy to catch.
6718
6719 if not Is_Ignored_Ghost_Entity (Nam) then
6720 Check_Restriction (No_Recursion, N);
6721 end if;
6722
6723 -- If the recursive call is to a parameterless subprogram,
6724 -- then even if we can't statically detect infinite
6725 -- recursion, this is pretty suspicious, and we output a
6726 -- warning. Furthermore, we will try later to detect some
6727 -- cases here at run time by expanding checking code (see
6728 -- Detect_Infinite_Recursion in package Exp_Ch6).
6729
6730 -- If the recursive call is within a handler, do not emit a
6731 -- warning, because this is a common idiom: loop until input
6732 -- is correct, catch illegal input in handler and restart.
6733
6734 if No (First_Formal (Nam))
6735 and then Etype (Nam) = Standard_Void_Type
6736 and then not Error_Posted (N)
6737 and then Nkind (Parent (N)) /= N_Exception_Handler
6738 then
6739 -- For the case of a procedure call. We give the message
6740 -- only if the call is the first statement in a sequence
6741 -- of statements, or if all previous statements are
6742 -- simple assignments. This is simply a heuristic to
6743 -- decrease false positives, without losing too many good
6744 -- warnings. The idea is that these previous statements
6745 -- may affect global variables the procedure depends on.
6746 -- We also exclude raise statements, that may arise from
6747 -- constraint checks and are probably unrelated to the
6748 -- intended control flow.
6749
6750 if Nkind (N) = N_Procedure_Call_Statement
6751 and then Is_List_Member (N)
6752 then
6753 declare
6754 P : Node_Id;
6755 begin
6756 P := Prev (N);
6757 while Present (P) loop
6758 if Nkind (P) not in N_Assignment_Statement
6759 | N_Raise_Constraint_Error
6760 then
6761 exit Scope_Loop;
6762 end if;
6763
6764 Prev (P);
6765 end loop;
6766 end;
6767 end if;
6768
6769 -- Do not give warning if we are in a conditional context
6770
6771 declare
6772 K : constant Node_Kind := Nkind (Parent (N));
6773 begin
6774 if (K = N_Loop_Statement
6775 and then Present (Iteration_Scheme (Parent (N))))
6776 or else K = N_If_Statement
6777 or else K = N_Elsif_Part
6778 or else K = N_Case_Statement_Alternative
6779 then
6780 exit Scope_Loop;
6781 end if;
6782 end;
6783
6784 -- Here warning is to be issued
6785
6786 Set_Has_Recursive_Call (Nam);
6787 Error_Msg_Warn := SPARK_Mode /= On;
6788 Error_Msg_N ("possible infinite recursion<<!", N);
6789 Error_Msg_N ("\Storage_Error ]<<!", N);
6790 end if;
6791
6792 exit Scope_Loop;
6793 end if;
6794
6795 Scop := Scope (Scop);
6796 end loop Scope_Loop;
6797 end if;
6798 end if;
6799
6800 -- Check obsolescent reference to Ada.Characters.Handling subprogram
6801
6802 Check_Obsolescent_2005_Entity (Nam, Subp);
6803
6804 -- If subprogram name is a predefined operator, it was given in
6805 -- functional notation. Replace call node with operator node, so
6806 -- that actuals can be resolved appropriately.
6807
6808 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
6809 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
6810 return;
6811
6812 elsif Present (Alias (Nam))
6813 and then Is_Predefined_Op (Alias (Nam))
6814 then
6815 Resolve_Actuals (N, Nam);
6816 Make_Call_Into_Operator (N, Typ, Alias (Nam));
6817 return;
6818 end if;
6819
6820 -- Create a transient scope if the resulting type requires it
6821
6822 -- There are several notable exceptions:
6823
6824 -- a) In init procs, the transient scope overhead is not needed, and is
6825 -- even incorrect when the call is a nested initialization call for a
6826 -- component whose expansion may generate adjust calls. However, if the
6827 -- call is some other procedure call within an initialization procedure
6828 -- (for example a call to Create_Task in the init_proc of the task
6829 -- run-time record) a transient scope must be created around this call.
6830
6831 -- b) Enumeration literal pseudo-calls need no transient scope
6832
6833 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
6834 -- functions) do not use the secondary stack even though the return
6835 -- type may be unconstrained.
6836
6837 -- d) Calls to a build-in-place function, since such functions may
6838 -- allocate their result directly in a target object, and cases where
6839 -- the result does get allocated in the secondary stack are checked for
6840 -- within the specialized Exp_Ch6 procedures for expanding those
6841 -- build-in-place calls.
6842
6843 -- e) Calls to inlinable expression functions do not use the secondary
6844 -- stack (since the call will be replaced by its returned object).
6845
6846 -- f) If the subprogram is marked Inline_Always, then even if it returns
6847 -- an unconstrained type the call does not require use of the secondary
6848 -- stack. However, inlining will only take place if the body to inline
6849 -- is already present. It may not be available if e.g. the subprogram is
6850 -- declared in a child instance.
6851
6852 -- g) If the subprogram is a static expression function and the call is
6853 -- a static call (the actuals are all static expressions), then we never
6854 -- want to create a transient scope (this could occur in the case of a
6855 -- static string-returning call).
6856
6857 if Is_Inlined (Nam)
6858 and then Has_Pragma_Inline (Nam)
6859 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
6860 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
6861 then
6862 null;
6863
6864 elsif Ekind (Nam) = E_Enumeration_Literal
6865 or else Is_Build_In_Place_Function (Nam)
6866 or else Is_Intrinsic_Subprogram (Nam)
6867 or else Is_Inlinable_Expression_Function (Nam)
6868 or else Is_Static_Function_Call (N)
6869 then
6870 null;
6871
6872 -- A return statement from an ignored Ghost function does not use the
6873 -- secondary stack (or any other one).
6874
6875 elsif Expander_Active
6876 and then Ekind (Nam) in E_Function | E_Subprogram_Type
6877 and then Requires_Transient_Scope (Etype (Nam))
6878 and then not Is_Ignored_Ghost_Entity (Nam)
6879 then
6880 Establish_Transient_Scope (N, Manage_Sec_Stack => True);
6881
6882 -- If the call appears within the bounds of a loop, it will be
6883 -- rewritten and reanalyzed, nothing left to do here.
6884
6885 if Nkind (N) /= N_Function_Call then
6886 return;
6887 end if;
6888 end if;
6889
6890 -- A protected function cannot be called within the definition of the
6891 -- enclosing protected type, unless it is part of a pre/postcondition
6892 -- on another protected operation. This may appear in the entry wrapper
6893 -- created for an entry with preconditions.
6894
6895 if Is_Protected_Type (Scope (Nam))
6896 and then In_Open_Scopes (Scope (Nam))
6897 and then not Has_Completion (Scope (Nam))
6898 and then not In_Spec_Expression
6899 and then not Is_Entry_Wrapper (Current_Scope)
6900 then
6901 Error_Msg_NE
6902 ("& cannot be called before end of protected definition", N, Nam);
6903 end if;
6904
6905 -- Propagate interpretation to actuals, and add default expressions
6906 -- where needed.
6907
6908 if Present (First_Formal (Nam)) then
6909 Resolve_Actuals (N, Nam);
6910
6911 -- Overloaded literals are rewritten as function calls, for purpose of
6912 -- resolution. After resolution, we can replace the call with the
6913 -- literal itself.
6914
6915 elsif Ekind (Nam) = E_Enumeration_Literal then
6916 Copy_Node (Subp, N);
6917 Resolve_Entity_Name (N, Typ);
6918
6919 -- Avoid validation, since it is a static function call
6920
6921 Generate_Reference (Nam, Subp);
6922 return;
6923 end if;
6924
6925 -- If the subprogram is not global, then kill all saved values and
6926 -- checks. This is a bit conservative, since in many cases we could do
6927 -- better, but it is not worth the effort. Similarly, we kill constant
6928 -- values. However we do not need to do this for internal entities
6929 -- (unless they are inherited user-defined subprograms), since they
6930 -- are not in the business of molesting local values.
6931
6932 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
6933 -- kill all checks and values for calls to global subprograms. This
6934 -- takes care of the case where an access to a local subprogram is
6935 -- taken, and could be passed directly or indirectly and then called
6936 -- from almost any context.
6937
6938 -- Note: we do not do this step till after resolving the actuals. That
6939 -- way we still take advantage of the current value information while
6940 -- scanning the actuals.
6941
6942 -- We suppress killing values if we are processing the nodes associated
6943 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
6944 -- type kills all the values as part of analyzing the code that
6945 -- initializes the dispatch tables.
6946
6947 if Inside_Freezing_Actions = 0
6948 and then (not Is_Library_Level_Entity (Nam)
6949 or else Suppress_Value_Tracking_On_Call
6950 (Nearest_Dynamic_Scope (Current_Scope)))
6951 and then (Comes_From_Source (Nam)
6952 or else (Present (Alias (Nam))
6953 and then Comes_From_Source (Alias (Nam))))
6954 then
6955 Kill_Current_Values;
6956 end if;
6957
6958 -- If we are warning about unread OUT parameters, this is the place to
6959 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
6960 -- after the above call to Kill_Current_Values (since that call clears
6961 -- the Last_Assignment field of all local variables).
6962
6963 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
6964 and then Comes_From_Source (N)
6965 and then In_Extended_Main_Source_Unit (N)
6966 then
6967 declare
6968 F : Entity_Id;
6969 A : Node_Id;
6970
6971 begin
6972 F := First_Formal (Nam);
6973 A := First_Actual (N);
6974 while Present (F) and then Present (A) loop
6975 if Ekind (F) in E_Out_Parameter | E_In_Out_Parameter
6976 and then Warn_On_Modified_As_Out_Parameter (F)
6977 and then Is_Entity_Name (A)
6978 and then Present (Entity (A))
6979 and then Comes_From_Source (N)
6980 and then Safe_To_Capture_Value (N, Entity (A))
6981 then
6982 Set_Last_Assignment (Entity (A), A);
6983 end if;
6984
6985 Next_Formal (F);
6986 Next_Actual (A);
6987 end loop;
6988 end;
6989 end if;
6990
6991 -- If the subprogram is a primitive operation, check whether or not
6992 -- it is a correct dispatching call.
6993
6994 if Is_Overloadable (Nam)
6995 and then Is_Dispatching_Operation (Nam)
6996 then
6997 Check_Dispatching_Call (N);
6998
6999 elsif Ekind (Nam) /= E_Subprogram_Type
7000 and then Is_Abstract_Subprogram (Nam)
7001 and then not In_Instance
7002 then
7003 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
7004 end if;
7005
7006 -- If this is a dispatching call, generate the appropriate reference,
7007 -- for better source navigation in GNAT Studio.
7008
7009 if Is_Overloadable (Nam)
7010 and then Present (Controlling_Argument (N))
7011 then
7012 Generate_Reference (Nam, Subp, 'R');
7013
7014 -- Normal case, not a dispatching call: generate a call reference
7015
7016 else
7017 Generate_Reference (Nam, Subp, 's');
7018 end if;
7019
7020 if Is_Intrinsic_Subprogram (Nam) then
7021 Check_Intrinsic_Call (N);
7022 end if;
7023
7024 -- Check for violation of restriction No_Specific_Termination_Handlers
7025 -- and warn on a potentially blocking call to Abort_Task.
7026
7027 if Restriction_Check_Required (No_Specific_Termination_Handlers)
7028 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
7029 or else
7030 Is_RTE (Nam, RE_Specific_Handler))
7031 then
7032 Check_Restriction (No_Specific_Termination_Handlers, N);
7033
7034 elsif Is_RTE (Nam, RE_Abort_Task) then
7035 Check_Potentially_Blocking_Operation (N);
7036 end if;
7037
7038 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
7039 -- timing event violates restriction No_Relative_Delay (AI-0211). We
7040 -- need to check the second argument to determine whether it is an
7041 -- absolute or relative timing event.
7042
7043 if Restriction_Check_Required (No_Relative_Delay)
7044 and then Is_RTE (Nam, RE_Set_Handler)
7045 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
7046 then
7047 Check_Restriction (No_Relative_Delay, N);
7048 end if;
7049
7050 -- Issue an error for a call to an eliminated subprogram. This routine
7051 -- will not perform the check if the call appears within a default
7052 -- expression.
7053
7054 Check_For_Eliminated_Subprogram (Subp, Nam);
7055
7056 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
7057 -- class-wide and the call dispatches on result in a context that does
7058 -- not provide a tag, the call raises Program_Error.
7059
7060 if Nkind (N) = N_Function_Call
7061 and then In_Instance
7062 and then Is_Generic_Actual_Type (Typ)
7063 and then Is_Class_Wide_Type (Typ)
7064 and then Has_Controlling_Result (Nam)
7065 and then Nkind (Parent (N)) = N_Object_Declaration
7066 then
7067 -- Verify that none of the formals are controlling
7068
7069 declare
7070 Call_OK : Boolean := False;
7071 F : Entity_Id;
7072
7073 begin
7074 F := First_Formal (Nam);
7075 while Present (F) loop
7076 if Is_Controlling_Formal (F) then
7077 Call_OK := True;
7078 exit;
7079 end if;
7080
7081 Next_Formal (F);
7082 end loop;
7083
7084 if not Call_OK then
7085 Error_Msg_Warn := SPARK_Mode /= On;
7086 Error_Msg_N ("!cannot determine tag of result<<", N);
7087 Error_Msg_N ("\Program_Error [<<!", N);
7088 Insert_Action (N,
7089 Make_Raise_Program_Error (Sloc (N),
7090 Reason => PE_Explicit_Raise));
7091 end if;
7092 end;
7093 end if;
7094
7095 -- Check for calling a function with OUT or IN OUT parameter when the
7096 -- calling context (us right now) is not Ada 2012, so does not allow
7097 -- OUT or IN OUT parameters in function calls. Functions declared in
7098 -- a predefined unit are OK, as they may be called indirectly from a
7099 -- user-declared instantiation.
7100
7101 if Ada_Version < Ada_2012
7102 and then Ekind (Nam) = E_Function
7103 and then Has_Out_Or_In_Out_Parameter (Nam)
7104 and then not In_Predefined_Unit (Nam)
7105 then
7106 Error_Msg_NE ("& has at least one OUT or `IN OUT` parameter", N, Nam);
7107 Error_Msg_N ("\call to this function only allowed in Ada 2012", N);
7108 end if;
7109
7110 -- Check the dimensions of the actuals in the call. For function calls,
7111 -- propagate the dimensions from the returned type to N.
7112
7113 Analyze_Dimension_Call (N, Nam);
7114
7115 -- All done, evaluate call and deal with elaboration issues
7116
7117 Eval_Call (N);
7118
7119 if Legacy_Elaboration_Checks then
7120 Check_Elab_Call (N);
7121 end if;
7122
7123 -- Annotate the tree by creating a call marker in case the original call
7124 -- is transformed by expansion. The call marker is automatically saved
7125 -- for later examination by the ABE Processing phase.
7126
7127 Build_Call_Marker (N);
7128
7129 Mark_Use_Clauses (Subp);
7130
7131 Warn_On_Overlapping_Actuals (Nam, N);
7132
7133 -- Ada 202x (AI12-0075): If the call is a static call to a static
7134 -- expression function, then we want to "inline" the call, replacing
7135 -- it with the folded static result. This is not done if the checking
7136 -- for a potentially static expression is enabled or if an error has
7137 -- been posted on the call (which may be due to the check for recursive
7138 -- calls, in which case we don't want to fall into infinite recursion
7139 -- when doing the inlining).
7140
7141 if not Checking_Potentially_Static_Expression
7142 and then Is_Static_Function_Call (N)
7143 and then not Is_Intrinsic_Subprogram (Ultimate_Alias (Nam))
7144 and then not Error_Posted (Ultimate_Alias (Nam))
7145 then
7146 Inline_Static_Function_Call (N, Ultimate_Alias (Nam));
7147
7148 -- In GNATprove mode, expansion is disabled, but we want to inline some
7149 -- subprograms to facilitate formal verification. Indirect calls through
7150 -- a subprogram type or within a generic cannot be inlined. Inlining is
7151 -- performed only for calls subject to SPARK_Mode on.
7152
7153 elsif GNATprove_Mode
7154 and then SPARK_Mode = On
7155 and then Is_Overloadable (Nam)
7156 and then not Inside_A_Generic
7157 then
7158 Nam_UA := Ultimate_Alias (Nam);
7159 Nam_Decl := Unit_Declaration_Node (Nam_UA);
7160
7161 if Nkind (Nam_Decl) = N_Subprogram_Declaration then
7162 Body_Id := Corresponding_Body (Nam_Decl);
7163
7164 -- Nothing to do if the subprogram is not eligible for inlining in
7165 -- GNATprove mode, or inlining is disabled with switch -gnatdm
7166
7167 if not Is_Inlined_Always (Nam_UA)
7168 or else not Can_Be_Inlined_In_GNATprove_Mode (Nam_UA, Body_Id)
7169 or else Debug_Flag_M
7170 then
7171 null;
7172
7173 -- Calls cannot be inlined inside assertions, as GNATprove treats
7174 -- assertions as logic expressions. Only issue a message when the
7175 -- body has been seen, otherwise this leads to spurious messages
7176 -- on expression functions.
7177
7178 elsif In_Assertion_Expr /= 0 then
7179 Cannot_Inline
7180 ("cannot inline & (in assertion expression)?", N, Nam_UA,
7181 Suppress_Info => No (Body_Id));
7182
7183 -- Calls cannot be inlined inside default expressions
7184
7185 elsif In_Default_Expr then
7186 Cannot_Inline
7187 ("cannot inline & (in default expression)?", N, Nam_UA);
7188
7189 -- Calls cannot be inlined inside quantified expressions, which
7190 -- are left in expression form for GNATprove. Since these
7191 -- expressions are only preanalyzed, we need to detect the failure
7192 -- to inline outside of the case for Full_Analysis below.
7193
7194 elsif In_Quantified_Expression (N) then
7195 Cannot_Inline
7196 ("cannot inline & (in quantified expression)?", N, Nam_UA);
7197
7198 -- Inlining should not be performed during preanalysis
7199
7200 elsif Full_Analysis then
7201
7202 -- Do not inline calls inside expression functions or functions
7203 -- generated by the front end for subtype predicates, as this
7204 -- would prevent interpreting them as logical formulas in
7205 -- GNATprove. Only issue a message when the body has been seen,
7206 -- otherwise this leads to spurious messages on callees that
7207 -- are themselves expression functions.
7208
7209 if Present (Current_Subprogram)
7210 and then
7211 (Is_Expression_Function_Or_Completion (Current_Subprogram)
7212 or else Is_Predicate_Function (Current_Subprogram)
7213 or else Is_Invariant_Procedure (Current_Subprogram)
7214 or else Is_DIC_Procedure (Current_Subprogram))
7215 then
7216 if Present (Body_Id)
7217 and then Present (Body_To_Inline (Nam_Decl))
7218 then
7219 if Is_Predicate_Function (Current_Subprogram) then
7220 Cannot_Inline
7221 ("cannot inline & (inside predicate)?",
7222 N, Nam_UA);
7223
7224 elsif Is_Invariant_Procedure (Current_Subprogram) then
7225 Cannot_Inline
7226 ("cannot inline & (inside invariant)?",
7227 N, Nam_UA);
7228
7229 elsif Is_DIC_Procedure (Current_Subprogram) then
7230 Cannot_Inline
7231 ("cannot inline & (inside Default_Initial_Condition)?",
7232 N, Nam_UA);
7233
7234 else
7235 Cannot_Inline
7236 ("cannot inline & (inside expression function)?",
7237 N, Nam_UA);
7238 end if;
7239 end if;
7240
7241 -- Cannot inline a call inside the definition of a record type,
7242 -- typically inside the constraints of the type. Calls in
7243 -- default expressions are also not inlined, but this is
7244 -- filtered out above when testing In_Default_Expr.
7245
7246 elsif Is_Record_Type (Current_Scope) then
7247 Cannot_Inline
7248 ("cannot inline & (inside record type)?", N, Nam_UA);
7249
7250 -- With the one-pass inlining technique, a call cannot be
7251 -- inlined if the corresponding body has not been seen yet.
7252
7253 elsif No (Body_Id) then
7254 Cannot_Inline
7255 ("cannot inline & (body not seen yet)?", N, Nam_UA);
7256
7257 -- Nothing to do if there is no body to inline, indicating that
7258 -- the subprogram is not suitable for inlining in GNATprove
7259 -- mode.
7260
7261 elsif No (Body_To_Inline (Nam_Decl)) then
7262 null;
7263
7264 -- Calls cannot be inlined inside potentially unevaluated
7265 -- expressions, as this would create complex actions inside
7266 -- expressions, that are not handled by GNATprove.
7267
7268 elsif Is_Potentially_Unevaluated (N) then
7269 Cannot_Inline
7270 ("cannot inline & (in potentially unevaluated context)?",
7271 N, Nam_UA);
7272
7273 -- Calls cannot be inlined inside the conditions of while
7274 -- loops, as this would create complex actions inside
7275 -- the condition, that are not handled by GNATprove.
7276
7277 elsif In_While_Loop_Condition (N) then
7278 Cannot_Inline
7279 ("cannot inline & (in while loop condition)?", N, Nam_UA);
7280
7281 -- Do not inline calls which would possibly lead to missing a
7282 -- type conversion check on an input parameter.
7283
7284 elsif not Call_Can_Be_Inlined_In_GNATprove_Mode (N, Nam) then
7285 Cannot_Inline
7286 ("cannot inline & (possible check on input parameters)?",
7287 N, Nam_UA);
7288
7289 -- Otherwise, inline the call, issuing an info message when
7290 -- -gnatd_f is set.
7291
7292 else
7293 if Debug_Flag_Underscore_F then
7294 Error_Msg_NE
7295 ("info: analyzing call to & in context?", N, Nam_UA);
7296 end if;
7297
7298 Expand_Inlined_Call (N, Nam_UA, Nam);
7299 end if;
7300 end if;
7301 end if;
7302 end if;
7303 end Resolve_Call;
7304
7305 -----------------------------
7306 -- Resolve_Case_Expression --
7307 -----------------------------
7308
7309 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
7310 Alt : Node_Id;
7311 Alt_Expr : Node_Id;
7312 Alt_Typ : Entity_Id;
7313 Is_Dyn : Boolean;
7314
7315 begin
7316 Alt := First (Alternatives (N));
7317 while Present (Alt) loop
7318 Alt_Expr := Expression (Alt);
7319
7320 if Error_Posted (Alt_Expr) then
7321 return;
7322 end if;
7323
7324 Resolve (Alt_Expr, Typ);
7325 Alt_Typ := Etype (Alt_Expr);
7326
7327 -- When the expression is of a scalar subtype different from the
7328 -- result subtype, then insert a conversion to ensure the generation
7329 -- of a constraint check.
7330
7331 if Is_Scalar_Type (Alt_Typ) and then Alt_Typ /= Typ then
7332 Rewrite (Alt_Expr, Convert_To (Typ, Alt_Expr));
7333 Analyze_And_Resolve (Alt_Expr, Typ);
7334 end if;
7335
7336 Next (Alt);
7337 end loop;
7338
7339 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
7340 -- dynamically tagged must be known statically.
7341
7342 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
7343 Alt := First (Alternatives (N));
7344 Is_Dyn := Is_Dynamically_Tagged (Expression (Alt));
7345
7346 while Present (Alt) loop
7347 if Is_Dynamically_Tagged (Expression (Alt)) /= Is_Dyn then
7348 Error_Msg_N
7349 ("all or none of the dependent expressions can be "
7350 & "dynamically tagged", N);
7351 end if;
7352
7353 Next (Alt);
7354 end loop;
7355 end if;
7356
7357 Set_Etype (N, Typ);
7358 Eval_Case_Expression (N);
7359 Analyze_Dimension (N);
7360 end Resolve_Case_Expression;
7361
7362 -------------------------------
7363 -- Resolve_Character_Literal --
7364 -------------------------------
7365
7366 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
7367 B_Typ : constant Entity_Id := Base_Type (Typ);
7368 C : Entity_Id;
7369
7370 begin
7371 -- Verify that the character does belong to the type of the context
7372
7373 Set_Etype (N, B_Typ);
7374 Eval_Character_Literal (N);
7375
7376 -- Wide_Wide_Character literals must always be defined, since the set
7377 -- of wide wide character literals is complete, i.e. if a character
7378 -- literal is accepted by the parser, then it is OK for wide wide
7379 -- character (out of range character literals are rejected).
7380
7381 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
7382 return;
7383
7384 -- Always accept character literal for type Any_Character, which
7385 -- occurs in error situations and in comparisons of literals, both
7386 -- of which should accept all literals.
7387
7388 elsif B_Typ = Any_Character then
7389 return;
7390
7391 -- For Standard.Character or a type derived from it, check that the
7392 -- literal is in range.
7393
7394 elsif Root_Type (B_Typ) = Standard_Character then
7395 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
7396 return;
7397 end if;
7398
7399 -- For Standard.Wide_Character or a type derived from it, check that the
7400 -- literal is in range.
7401
7402 elsif Root_Type (B_Typ) = Standard_Wide_Character then
7403 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
7404 return;
7405 end if;
7406
7407 -- If the entity is already set, this has already been resolved in a
7408 -- generic context, or comes from expansion. Nothing else to do.
7409
7410 elsif Present (Entity (N)) then
7411 return;
7412
7413 -- Otherwise we have a user defined character type, and we can use the
7414 -- standard visibility mechanisms to locate the referenced entity.
7415
7416 else
7417 C := Current_Entity (N);
7418 while Present (C) loop
7419 if Etype (C) = B_Typ then
7420 Set_Entity_With_Checks (N, C);
7421 Generate_Reference (C, N);
7422 return;
7423 end if;
7424
7425 C := Homonym (C);
7426 end loop;
7427 end if;
7428
7429 -- If we fall through, then the literal does not match any of the
7430 -- entries of the enumeration type. This isn't just a constraint error
7431 -- situation, it is an illegality (see RM 4.2).
7432
7433 Error_Msg_NE
7434 ("character not defined for }", N, First_Subtype (B_Typ));
7435 end Resolve_Character_Literal;
7436
7437 ---------------------------
7438 -- Resolve_Comparison_Op --
7439 ---------------------------
7440
7441 -- Context requires a boolean type, and plays no role in resolution.
7442 -- Processing identical to that for equality operators. The result type is
7443 -- the base type, which matters when pathological subtypes of booleans with
7444 -- limited ranges are used.
7445
7446 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
7447 L : constant Node_Id := Left_Opnd (N);
7448 R : constant Node_Id := Right_Opnd (N);
7449 T : Entity_Id;
7450
7451 begin
7452 -- If this is an intrinsic operation which is not predefined, use the
7453 -- types of its declared arguments to resolve the possibly overloaded
7454 -- operands. Otherwise the operands are unambiguous and specify the
7455 -- expected type.
7456
7457 if Scope (Entity (N)) /= Standard_Standard then
7458 T := Etype (First_Entity (Entity (N)));
7459
7460 else
7461 T := Find_Unique_Type (L, R);
7462
7463 if T = Any_Fixed then
7464 T := Unique_Fixed_Point_Type (L);
7465 end if;
7466 end if;
7467
7468 Set_Etype (N, Base_Type (Typ));
7469 Generate_Reference (T, N, ' ');
7470
7471 -- Skip remaining processing if already set to Any_Type
7472
7473 if T = Any_Type then
7474 return;
7475 end if;
7476
7477 -- Deal with other error cases
7478
7479 if T = Any_String or else
7480 T = Any_Composite or else
7481 T = Any_Character
7482 then
7483 if T = Any_Character then
7484 Ambiguous_Character (L);
7485 else
7486 Error_Msg_N ("ambiguous operands for comparison", N);
7487 end if;
7488
7489 Set_Etype (N, Any_Type);
7490 return;
7491 end if;
7492
7493 -- Resolve the operands if types OK
7494
7495 Resolve (L, T);
7496 Resolve (R, T);
7497 Check_Unset_Reference (L);
7498 Check_Unset_Reference (R);
7499 Generate_Operator_Reference (N, T);
7500 Check_Low_Bound_Tested (N);
7501
7502 -- Check comparison on unordered enumeration
7503
7504 if Bad_Unordered_Enumeration_Reference (N, Etype (L)) then
7505 Error_Msg_Sloc := Sloc (Etype (L));
7506 Error_Msg_NE
7507 ("comparison on unordered enumeration type& declared#?U?",
7508 N, Etype (L));
7509 end if;
7510
7511 Analyze_Dimension (N);
7512
7513 Eval_Relational_Op (N);
7514 end Resolve_Comparison_Op;
7515
7516 --------------------------------
7517 -- Resolve_Declare_Expression --
7518 --------------------------------
7519
7520 procedure Resolve_Declare_Expression
7521 (N : Node_Id;
7522 Typ : Entity_Id)
7523 is
7524 Decl : Node_Id;
7525 Need_Transient_Scope : Boolean := False;
7526 begin
7527 -- Install the scope created for local declarations, if
7528 -- any. The syntax allows a Declare_Expression with no
7529 -- declarations, in analogy with block statements.
7530 -- Note that that scope has no explicit declaration, but
7531 -- appears as the scope of all entities declared therein.
7532
7533 Decl := First (Actions (N));
7534 while Present (Decl) loop
7535 exit when Nkind (Decl)
7536 in N_Object_Declaration | N_Object_Renaming_Declaration;
7537 Next (Decl);
7538 end loop;
7539
7540 if Present (Decl) then
7541
7542 -- Need to establish a transient scope in case Expression (N)
7543 -- requires actions to be wrapped.
7544
7545 declare
7546 Node : Node_Id;
7547 begin
7548 Node := First (Actions (N));
7549 while Present (Node) loop
7550 if Nkind (Node) = N_Object_Declaration
7551 and then Is_Type (Etype (Defining_Identifier (Node)))
7552 and then Requires_Transient_Scope
7553 (Etype (Defining_Identifier (Node)))
7554 then
7555 Need_Transient_Scope := True;
7556 exit;
7557 end if;
7558
7559 Next (Node);
7560 end loop;
7561 end;
7562
7563 if Need_Transient_Scope then
7564 Establish_Transient_Scope (Decl, True);
7565 else
7566 Push_Scope (Scope (Defining_Identifier (Decl)));
7567 end if;
7568
7569 declare
7570 E : Entity_Id := First_Entity (Current_Scope);
7571 begin
7572 while Present (E) loop
7573 Set_Current_Entity (E);
7574 Set_Is_Immediately_Visible (E);
7575 Next_Entity (E);
7576 end loop;
7577 end;
7578
7579 Resolve (Expression (N), Typ);
7580 End_Scope;
7581
7582 else
7583 Resolve (Expression (N), Typ);
7584 end if;
7585 end Resolve_Declare_Expression;
7586
7587 -----------------------------------------
7588 -- Resolve_Discrete_Subtype_Indication --
7589 -----------------------------------------
7590
7591 procedure Resolve_Discrete_Subtype_Indication
7592 (N : Node_Id;
7593 Typ : Entity_Id)
7594 is
7595 R : Node_Id;
7596 S : Entity_Id;
7597
7598 begin
7599 Analyze (Subtype_Mark (N));
7600 S := Entity (Subtype_Mark (N));
7601
7602 if Nkind (Constraint (N)) /= N_Range_Constraint then
7603 Error_Msg_N ("expect range constraint for discrete type", N);
7604 Set_Etype (N, Any_Type);
7605
7606 else
7607 R := Range_Expression (Constraint (N));
7608
7609 if R = Error then
7610 return;
7611 end if;
7612
7613 Analyze (R);
7614
7615 if Base_Type (S) /= Base_Type (Typ) then
7616 Error_Msg_NE
7617 ("expect subtype of }", N, First_Subtype (Typ));
7618
7619 -- Rewrite the constraint as a range of Typ
7620 -- to allow compilation to proceed further.
7621
7622 Set_Etype (N, Typ);
7623 Rewrite (Low_Bound (R),
7624 Make_Attribute_Reference (Sloc (Low_Bound (R)),
7625 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
7626 Attribute_Name => Name_First));
7627 Rewrite (High_Bound (R),
7628 Make_Attribute_Reference (Sloc (High_Bound (R)),
7629 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
7630 Attribute_Name => Name_First));
7631
7632 else
7633 Resolve (R, Typ);
7634 Set_Etype (N, Etype (R));
7635
7636 -- Additionally, we must check that the bounds are compatible
7637 -- with the given subtype, which might be different from the
7638 -- type of the context.
7639
7640 Apply_Range_Check (R, S);
7641
7642 -- ??? If the above check statically detects a Constraint_Error
7643 -- it replaces the offending bound(s) of the range R with a
7644 -- Constraint_Error node. When the itype which uses these bounds
7645 -- is frozen the resulting call to Duplicate_Subexpr generates
7646 -- a new temporary for the bounds.
7647
7648 -- Unfortunately there are other itypes that are also made depend
7649 -- on these bounds, so when Duplicate_Subexpr is called they get
7650 -- a forward reference to the newly created temporaries and Gigi
7651 -- aborts on such forward references. This is probably sign of a
7652 -- more fundamental problem somewhere else in either the order of
7653 -- itype freezing or the way certain itypes are constructed.
7654
7655 -- To get around this problem we call Remove_Side_Effects right
7656 -- away if either bounds of R are a Constraint_Error.
7657
7658 declare
7659 L : constant Node_Id := Low_Bound (R);
7660 H : constant Node_Id := High_Bound (R);
7661
7662 begin
7663 if Nkind (L) = N_Raise_Constraint_Error then
7664 Remove_Side_Effects (L);
7665 end if;
7666
7667 if Nkind (H) = N_Raise_Constraint_Error then
7668 Remove_Side_Effects (H);
7669 end if;
7670 end;
7671
7672 Check_Unset_Reference (Low_Bound (R));
7673 Check_Unset_Reference (High_Bound (R));
7674 end if;
7675 end if;
7676 end Resolve_Discrete_Subtype_Indication;
7677
7678 -------------------------
7679 -- Resolve_Entity_Name --
7680 -------------------------
7681
7682 -- Used to resolve identifiers and expanded names
7683
7684 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
7685 function Is_Assignment_Or_Object_Expression
7686 (Context : Node_Id;
7687 Expr : Node_Id) return Boolean;
7688 -- Determine whether node Context denotes an assignment statement or an
7689 -- object declaration whose expression is node Expr.
7690
7691 function Is_Attribute_Expression (Expr : Node_Id) return Boolean;
7692 -- Determine whether Expr is part of an N_Attribute_Reference
7693 -- expression.
7694
7695 ----------------------------------------
7696 -- Is_Assignment_Or_Object_Expression --
7697 ----------------------------------------
7698
7699 function Is_Assignment_Or_Object_Expression
7700 (Context : Node_Id;
7701 Expr : Node_Id) return Boolean
7702 is
7703 begin
7704 if Nkind (Context) in
7705 N_Assignment_Statement | N_Object_Declaration
7706 and then Expression (Context) = Expr
7707 then
7708 return True;
7709
7710 -- Check whether a construct that yields a name is the expression of
7711 -- an assignment statement or an object declaration.
7712
7713 elsif (Nkind (Context) in N_Attribute_Reference
7714 | N_Explicit_Dereference
7715 | N_Indexed_Component
7716 | N_Selected_Component
7717 | N_Slice
7718 and then Prefix (Context) = Expr)
7719 or else
7720 (Nkind (Context) in N_Type_Conversion
7721 | N_Unchecked_Type_Conversion
7722 and then Expression (Context) = Expr)
7723 then
7724 return
7725 Is_Assignment_Or_Object_Expression
7726 (Context => Parent (Context),
7727 Expr => Context);
7728
7729 -- Otherwise the context is not an assignment statement or an object
7730 -- declaration.
7731
7732 else
7733 return False;
7734 end if;
7735 end Is_Assignment_Or_Object_Expression;
7736
7737 -----------------------------
7738 -- Is_Attribute_Expression --
7739 -----------------------------
7740
7741 function Is_Attribute_Expression (Expr : Node_Id) return Boolean is
7742 N : Node_Id := Expr;
7743 begin
7744 while Present (N) loop
7745 if Nkind (N) = N_Attribute_Reference then
7746 return True;
7747 end if;
7748
7749 N := Parent (N);
7750 end loop;
7751
7752 return False;
7753 end Is_Attribute_Expression;
7754
7755 -- Local variables
7756
7757 E : constant Entity_Id := Entity (N);
7758 Par : Node_Id;
7759
7760 -- Start of processing for Resolve_Entity_Name
7761
7762 begin
7763 -- If garbage from errors, set to Any_Type and return
7764
7765 if No (E) and then Total_Errors_Detected /= 0 then
7766 Set_Etype (N, Any_Type);
7767 return;
7768 end if;
7769
7770 -- Replace named numbers by corresponding literals. Note that this is
7771 -- the one case where Resolve_Entity_Name must reset the Etype, since
7772 -- it is currently marked as universal.
7773
7774 if Ekind (E) = E_Named_Integer then
7775 Set_Etype (N, Typ);
7776 Eval_Named_Integer (N);
7777
7778 elsif Ekind (E) = E_Named_Real then
7779 Set_Etype (N, Typ);
7780 Eval_Named_Real (N);
7781
7782 -- For enumeration literals, we need to make sure that a proper style
7783 -- check is done, since such literals are overloaded, and thus we did
7784 -- not do a style check during the first phase of analysis.
7785
7786 elsif Ekind (E) = E_Enumeration_Literal then
7787 Set_Entity_With_Checks (N, E);
7788 Eval_Entity_Name (N);
7789
7790 -- Case of (sub)type name appearing in a context where an expression
7791 -- is expected. This is legal if occurrence is a current instance.
7792 -- See RM 8.6 (17/3).
7793
7794 elsif Is_Type (E) then
7795 if Is_Current_Instance (N) then
7796 null;
7797
7798 -- Any other use is an error
7799
7800 else
7801 Error_Msg_N
7802 ("invalid use of subtype mark in expression or call", N);
7803 end if;
7804
7805 -- Check discriminant use if entity is discriminant in current scope,
7806 -- i.e. discriminant of record or concurrent type currently being
7807 -- analyzed. Uses in corresponding body are unrestricted.
7808
7809 elsif Ekind (E) = E_Discriminant
7810 and then Scope (E) = Current_Scope
7811 and then not Has_Completion (Current_Scope)
7812 then
7813 Check_Discriminant_Use (N);
7814
7815 -- A parameterless generic function cannot appear in a context that
7816 -- requires resolution.
7817
7818 elsif Ekind (E) = E_Generic_Function then
7819 Error_Msg_N ("illegal use of generic function", N);
7820
7821 -- In Ada 83 an OUT parameter cannot be read, but attributes of
7822 -- array types (i.e. bounds and length) are legal.
7823
7824 elsif Ekind (E) = E_Out_Parameter
7825 and then (Is_Scalar_Type (Etype (E))
7826 or else not Is_Attribute_Expression (Parent (N)))
7827
7828 and then (Nkind (Parent (N)) in N_Op
7829 or else Nkind (Parent (N)) = N_Explicit_Dereference
7830 or else Is_Assignment_Or_Object_Expression
7831 (Context => Parent (N),
7832 Expr => N))
7833 then
7834 if Ada_Version = Ada_83 then
7835 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
7836 end if;
7837
7838 -- In all other cases, just do the possible static evaluation
7839
7840 else
7841 -- A deferred constant that appears in an expression must have a
7842 -- completion, unless it has been removed by in-place expansion of
7843 -- an aggregate. A constant that is a renaming does not need
7844 -- initialization.
7845
7846 if Ekind (E) = E_Constant
7847 and then Comes_From_Source (E)
7848 and then No (Constant_Value (E))
7849 and then Is_Frozen (Etype (E))
7850 and then not In_Spec_Expression
7851 and then not Is_Imported (E)
7852 and then Nkind (Parent (E)) /= N_Object_Renaming_Declaration
7853 then
7854 if No_Initialization (Parent (E))
7855 or else (Present (Full_View (E))
7856 and then No_Initialization (Parent (Full_View (E))))
7857 then
7858 null;
7859 else
7860 Error_Msg_N
7861 ("deferred constant is frozen before completion", N);
7862 end if;
7863 end if;
7864
7865 Eval_Entity_Name (N);
7866 end if;
7867
7868 Par := Parent (N);
7869
7870 -- When the entity appears in a parameter association, retrieve the
7871 -- related subprogram call.
7872
7873 if Nkind (Par) = N_Parameter_Association then
7874 Par := Parent (Par);
7875 end if;
7876
7877 if Comes_From_Source (N) then
7878
7879 -- The following checks are only relevant when SPARK_Mode is on as
7880 -- they are not standard Ada legality rules.
7881
7882 if SPARK_Mode = On then
7883
7884 -- An effectively volatile object for reading must appear in
7885 -- non-interfering context (SPARK RM 7.1.3(10)).
7886
7887 if Is_Object (E)
7888 and then Is_Effectively_Volatile_For_Reading (E)
7889 and then not Is_OK_Volatile_Context (Par, N)
7890 then
7891 SPARK_Msg_N
7892 ("volatile object cannot appear in this context "
7893 & "(SPARK RM 7.1.3(10))", N);
7894 end if;
7895
7896 -- Check for possible elaboration issues with respect to reads of
7897 -- variables. The act of renaming the variable is not considered a
7898 -- read as it simply establishes an alias.
7899
7900 if Legacy_Elaboration_Checks
7901 and then Ekind (E) = E_Variable
7902 and then Dynamic_Elaboration_Checks
7903 and then Nkind (Par) /= N_Object_Renaming_Declaration
7904 then
7905 Check_Elab_Call (N);
7906 end if;
7907 end if;
7908
7909 -- The variable may eventually become a constituent of a single
7910 -- protected/task type. Record the reference now and verify its
7911 -- legality when analyzing the contract of the variable
7912 -- (SPARK RM 9.3).
7913
7914 if Ekind (E) = E_Variable then
7915 Record_Possible_Part_Of_Reference (E, N);
7916 end if;
7917
7918 -- A Ghost entity must appear in a specific context
7919
7920 if Is_Ghost_Entity (E) then
7921 Check_Ghost_Context (E, N);
7922 end if;
7923 end if;
7924
7925 -- We may be resolving an entity within expanded code, so a reference to
7926 -- an entity should be ignored when calculating effective use clauses to
7927 -- avoid inappropriate marking.
7928
7929 if Comes_From_Source (N) then
7930 Mark_Use_Clauses (E);
7931 end if;
7932 end Resolve_Entity_Name;
7933
7934 -------------------
7935 -- Resolve_Entry --
7936 -------------------
7937
7938 procedure Resolve_Entry (Entry_Name : Node_Id) is
7939 Loc : constant Source_Ptr := Sloc (Entry_Name);
7940 Nam : Entity_Id;
7941 New_N : Node_Id;
7942 S : Entity_Id;
7943 Tsk : Entity_Id;
7944 E_Name : Node_Id;
7945 Index : Node_Id;
7946
7947 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
7948 -- If the bounds of the entry family being called depend on task
7949 -- discriminants, build a new index subtype where a discriminant is
7950 -- replaced with the value of the discriminant of the target task.
7951 -- The target task is the prefix of the entry name in the call.
7952
7953 -----------------------
7954 -- Actual_Index_Type --
7955 -----------------------
7956
7957 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
7958 Typ : constant Entity_Id := Entry_Index_Type (E);
7959 Tsk : constant Entity_Id := Scope (E);
7960 Lo : constant Node_Id := Type_Low_Bound (Typ);
7961 Hi : constant Node_Id := Type_High_Bound (Typ);
7962 New_T : Entity_Id;
7963
7964 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
7965 -- If the bound is given by a discriminant, replace with a reference
7966 -- to the discriminant of the same name in the target task. If the
7967 -- entry name is the target of a requeue statement and the entry is
7968 -- in the current protected object, the bound to be used is the
7969 -- discriminal of the object (see Apply_Range_Check for details of
7970 -- the transformation).
7971
7972 -----------------------------
7973 -- Actual_Discriminant_Ref --
7974 -----------------------------
7975
7976 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
7977 Typ : constant Entity_Id := Etype (Bound);
7978 Ref : Node_Id;
7979
7980 begin
7981 Remove_Side_Effects (Bound);
7982
7983 if not Is_Entity_Name (Bound)
7984 or else Ekind (Entity (Bound)) /= E_Discriminant
7985 then
7986 return Bound;
7987
7988 elsif Is_Protected_Type (Tsk)
7989 and then In_Open_Scopes (Tsk)
7990 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
7991 then
7992 -- Note: here Bound denotes a discriminant of the corresponding
7993 -- record type tskV, whose discriminal is a formal of the
7994 -- init-proc tskVIP. What we want is the body discriminal,
7995 -- which is associated to the discriminant of the original
7996 -- concurrent type tsk.
7997
7998 return New_Occurrence_Of
7999 (Find_Body_Discriminal (Entity (Bound)), Loc);
8000
8001 else
8002 Ref :=
8003 Make_Selected_Component (Loc,
8004 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
8005 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
8006 Analyze (Ref);
8007 Resolve (Ref, Typ);
8008 return Ref;
8009 end if;
8010 end Actual_Discriminant_Ref;
8011
8012 -- Start of processing for Actual_Index_Type
8013
8014 begin
8015 if not Has_Discriminants (Tsk)
8016 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
8017 then
8018 return Entry_Index_Type (E);
8019
8020 else
8021 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
8022 Set_Etype (New_T, Base_Type (Typ));
8023 Set_Size_Info (New_T, Typ);
8024 Set_RM_Size (New_T, RM_Size (Typ));
8025 Set_Scalar_Range (New_T,
8026 Make_Range (Sloc (Entry_Name),
8027 Low_Bound => Actual_Discriminant_Ref (Lo),
8028 High_Bound => Actual_Discriminant_Ref (Hi)));
8029
8030 return New_T;
8031 end if;
8032 end Actual_Index_Type;
8033
8034 -- Start of processing for Resolve_Entry
8035
8036 begin
8037 -- Find name of entry being called, and resolve prefix of name with its
8038 -- own type. The prefix can be overloaded, and the name and signature of
8039 -- the entry must be taken into account.
8040
8041 if Nkind (Entry_Name) = N_Indexed_Component then
8042
8043 -- Case of dealing with entry family within the current tasks
8044
8045 E_Name := Prefix (Entry_Name);
8046
8047 else
8048 E_Name := Entry_Name;
8049 end if;
8050
8051 if Is_Entity_Name (E_Name) then
8052
8053 -- Entry call to an entry (or entry family) in the current task. This
8054 -- is legal even though the task will deadlock. Rewrite as call to
8055 -- current task.
8056
8057 -- This can also be a call to an entry in an enclosing task. If this
8058 -- is a single task, we have to retrieve its name, because the scope
8059 -- of the entry is the task type, not the object. If the enclosing
8060 -- task is a task type, the identity of the task is given by its own
8061 -- self variable.
8062
8063 -- Finally this can be a requeue on an entry of the same task or
8064 -- protected object.
8065
8066 S := Scope (Entity (E_Name));
8067
8068 for J in reverse 0 .. Scope_Stack.Last loop
8069 if Is_Task_Type (Scope_Stack.Table (J).Entity)
8070 and then not Comes_From_Source (S)
8071 then
8072 -- S is an enclosing task or protected object. The concurrent
8073 -- declaration has been converted into a type declaration, and
8074 -- the object itself has an object declaration that follows
8075 -- the type in the same declarative part.
8076
8077 Tsk := Next_Entity (S);
8078 while Etype (Tsk) /= S loop
8079 Next_Entity (Tsk);
8080 end loop;
8081
8082 S := Tsk;
8083 exit;
8084
8085 elsif S = Scope_Stack.Table (J).Entity then
8086
8087 -- Call to current task. Will be transformed into call to Self
8088
8089 exit;
8090
8091 end if;
8092 end loop;
8093
8094 New_N :=
8095 Make_Selected_Component (Loc,
8096 Prefix => New_Occurrence_Of (S, Loc),
8097 Selector_Name =>
8098 New_Occurrence_Of (Entity (E_Name), Loc));
8099 Rewrite (E_Name, New_N);
8100 Analyze (E_Name);
8101
8102 elsif Nkind (Entry_Name) = N_Selected_Component
8103 and then Is_Overloaded (Prefix (Entry_Name))
8104 then
8105 -- Use the entry name (which must be unique at this point) to find
8106 -- the prefix that returns the corresponding task/protected type.
8107
8108 declare
8109 Pref : constant Node_Id := Prefix (Entry_Name);
8110 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
8111 I : Interp_Index;
8112 It : Interp;
8113
8114 begin
8115 Get_First_Interp (Pref, I, It);
8116 while Present (It.Typ) loop
8117 if Scope (Ent) = It.Typ then
8118 Set_Etype (Pref, It.Typ);
8119 exit;
8120 end if;
8121
8122 Get_Next_Interp (I, It);
8123 end loop;
8124 end;
8125 end if;
8126
8127 if Nkind (Entry_Name) = N_Selected_Component then
8128 Resolve (Prefix (Entry_Name));
8129 Resolve_Implicit_Dereference (Prefix (Entry_Name));
8130
8131 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8132 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
8133 Resolve (Prefix (Prefix (Entry_Name)));
8134 Resolve_Implicit_Dereference (Prefix (Prefix (Entry_Name)));
8135
8136 -- We do not resolve the prefix because an Entry_Family has no type,
8137 -- although it has the semantics of an array since it can be indexed.
8138 -- In order to perform the associated range check, we would need to
8139 -- build an array type on the fly and set it on the prefix, but this
8140 -- would be wasteful since only the index type matters. Therefore we
8141 -- attach this index type directly, so that Actual_Index_Expression
8142 -- can pick it up later in order to generate the range check.
8143
8144 Set_Etype (Prefix (Entry_Name), Actual_Index_Type (Nam));
8145
8146 Index := First (Expressions (Entry_Name));
8147 Resolve (Index, Entry_Index_Type (Nam));
8148
8149 -- Generate a reference for the index when it denotes an entity
8150
8151 if Is_Entity_Name (Index) then
8152 Generate_Reference (Entity (Index), Nam);
8153 end if;
8154
8155 -- Up to this point the expression could have been the actual in a
8156 -- simple entry call, and be given by a named association.
8157
8158 if Nkind (Index) = N_Parameter_Association then
8159 Error_Msg_N ("expect expression for entry index", Index);
8160 else
8161 Apply_Scalar_Range_Check (Index, Etype (Prefix (Entry_Name)));
8162 end if;
8163 end if;
8164 end Resolve_Entry;
8165
8166 ------------------------
8167 -- Resolve_Entry_Call --
8168 ------------------------
8169
8170 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
8171 Entry_Name : constant Node_Id := Name (N);
8172 Loc : constant Source_Ptr := Sloc (Entry_Name);
8173
8174 Nam : Entity_Id;
8175 Norm_OK : Boolean;
8176 Obj : Node_Id;
8177 Was_Over : Boolean;
8178
8179 begin
8180 -- We kill all checks here, because it does not seem worth the effort to
8181 -- do anything better, an entry call is a big operation.
8182
8183 Kill_All_Checks;
8184
8185 -- Processing of the name is similar for entry calls and protected
8186 -- operation calls. Once the entity is determined, we can complete
8187 -- the resolution of the actuals.
8188
8189 -- The selector may be overloaded, in the case of a protected object
8190 -- with overloaded functions. The type of the context is used for
8191 -- resolution.
8192
8193 if Nkind (Entry_Name) = N_Selected_Component
8194 and then Is_Overloaded (Selector_Name (Entry_Name))
8195 and then Typ /= Standard_Void_Type
8196 then
8197 declare
8198 I : Interp_Index;
8199 It : Interp;
8200
8201 begin
8202 Get_First_Interp (Selector_Name (Entry_Name), I, It);
8203 while Present (It.Typ) loop
8204 if Covers (Typ, It.Typ) then
8205 Set_Entity (Selector_Name (Entry_Name), It.Nam);
8206 Set_Etype (Entry_Name, It.Typ);
8207
8208 Generate_Reference (It.Typ, N, ' ');
8209 end if;
8210
8211 Get_Next_Interp (I, It);
8212 end loop;
8213 end;
8214 end if;
8215
8216 Resolve_Entry (Entry_Name);
8217
8218 if Nkind (Entry_Name) = N_Selected_Component then
8219
8220 -- Simple entry or protected operation call
8221
8222 Nam := Entity (Selector_Name (Entry_Name));
8223 Obj := Prefix (Entry_Name);
8224
8225 if Is_Subprogram (Nam) then
8226 Check_For_Eliminated_Subprogram (Entry_Name, Nam);
8227 end if;
8228
8229 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
8230
8231 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8232
8233 -- Call to member of entry family
8234
8235 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
8236 Obj := Prefix (Prefix (Entry_Name));
8237 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
8238 end if;
8239
8240 -- We cannot in general check the maximum depth of protected entry calls
8241 -- at compile time. But we can tell that any protected entry call at all
8242 -- violates a specified nesting depth of zero.
8243
8244 if Is_Protected_Type (Scope (Nam)) then
8245 Check_Restriction (Max_Entry_Queue_Length, N);
8246 end if;
8247
8248 -- Use context type to disambiguate a protected function that can be
8249 -- called without actuals and that returns an array type, and where the
8250 -- argument list may be an indexing of the returned value.
8251
8252 if Ekind (Nam) = E_Function
8253 and then Needs_No_Actuals (Nam)
8254 and then Present (Parameter_Associations (N))
8255 and then
8256 ((Is_Array_Type (Etype (Nam))
8257 and then Covers (Typ, Component_Type (Etype (Nam))))
8258
8259 or else (Is_Access_Type (Etype (Nam))
8260 and then Is_Array_Type (Designated_Type (Etype (Nam)))
8261 and then
8262 Covers
8263 (Typ,
8264 Component_Type (Designated_Type (Etype (Nam))))))
8265 then
8266 declare
8267 Index_Node : Node_Id;
8268
8269 begin
8270 Index_Node :=
8271 Make_Indexed_Component (Loc,
8272 Prefix =>
8273 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
8274 Expressions => Parameter_Associations (N));
8275
8276 -- Since we are correcting a node classification error made by the
8277 -- parser, we call Replace rather than Rewrite.
8278
8279 Replace (N, Index_Node);
8280 Set_Etype (Prefix (N), Etype (Nam));
8281 Set_Etype (N, Typ);
8282 Resolve_Indexed_Component (N, Typ);
8283 return;
8284 end;
8285 end if;
8286
8287 if Is_Entry (Nam)
8288 and then Present (Contract_Wrapper (Nam))
8289 and then Current_Scope /= Contract_Wrapper (Nam)
8290 then
8291 -- Note the entity being called before rewriting the call, so that
8292 -- it appears used at this point.
8293
8294 Generate_Reference (Nam, Entry_Name, 'r');
8295
8296 -- Rewrite as call to the precondition wrapper, adding the task
8297 -- object to the list of actuals. If the call is to a member of an
8298 -- entry family, include the index as well.
8299
8300 declare
8301 New_Call : Node_Id;
8302 New_Actuals : List_Id;
8303
8304 begin
8305 New_Actuals := New_List (Obj);
8306
8307 if Nkind (Entry_Name) = N_Indexed_Component then
8308 Append_To (New_Actuals,
8309 New_Copy_Tree (First (Expressions (Entry_Name))));
8310 end if;
8311
8312 Append_List (Parameter_Associations (N), New_Actuals);
8313 New_Call :=
8314 Make_Procedure_Call_Statement (Loc,
8315 Name =>
8316 New_Occurrence_Of (Contract_Wrapper (Nam), Loc),
8317 Parameter_Associations => New_Actuals);
8318 Rewrite (N, New_Call);
8319
8320 -- Preanalyze and resolve new call. Current procedure is called
8321 -- from Resolve_Call, after which expansion will take place.
8322
8323 Preanalyze_And_Resolve (N);
8324 return;
8325 end;
8326 end if;
8327
8328 -- The operation name may have been overloaded. Order the actuals
8329 -- according to the formals of the resolved entity, and set the return
8330 -- type to that of the operation.
8331
8332 if Was_Over then
8333 Normalize_Actuals (N, Nam, False, Norm_OK);
8334 pragma Assert (Norm_OK);
8335 Set_Etype (N, Etype (Nam));
8336
8337 -- Reset the Is_Overloaded flag, since resolution is now completed
8338
8339 -- Simple entry call
8340
8341 if Nkind (Entry_Name) = N_Selected_Component then
8342 Set_Is_Overloaded (Selector_Name (Entry_Name), False);
8343
8344 -- Call to a member of an entry family
8345
8346 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8347 Set_Is_Overloaded (Selector_Name (Prefix (Entry_Name)), False);
8348 end if;
8349 end if;
8350
8351 Resolve_Actuals (N, Nam);
8352 Check_Internal_Protected_Use (N, Nam);
8353
8354 -- Create a call reference to the entry
8355
8356 Generate_Reference (Nam, Entry_Name, 's');
8357
8358 if Is_Entry (Nam) then
8359 Check_Potentially_Blocking_Operation (N);
8360 end if;
8361
8362 -- Verify that a procedure call cannot masquerade as an entry
8363 -- call where an entry call is expected.
8364
8365 if Ekind (Nam) = E_Procedure then
8366 if Nkind (Parent (N)) = N_Entry_Call_Alternative
8367 and then N = Entry_Call_Statement (Parent (N))
8368 then
8369 Error_Msg_N ("entry call required in select statement", N);
8370
8371 elsif Nkind (Parent (N)) = N_Triggering_Alternative
8372 and then N = Triggering_Statement (Parent (N))
8373 then
8374 Error_Msg_N ("triggering statement cannot be procedure call", N);
8375
8376 elsif Ekind (Scope (Nam)) = E_Task_Type
8377 and then not In_Open_Scopes (Scope (Nam))
8378 then
8379 Error_Msg_N ("task has no entry with this name", Entry_Name);
8380 end if;
8381 end if;
8382
8383 -- After resolution, entry calls and protected procedure calls are
8384 -- changed into entry calls, for expansion. The structure of the node
8385 -- does not change, so it can safely be done in place. Protected
8386 -- function calls must keep their structure because they are
8387 -- subexpressions.
8388
8389 if Ekind (Nam) /= E_Function then
8390
8391 -- A protected operation that is not a function may modify the
8392 -- corresponding object, and cannot apply to a constant. If this
8393 -- is an internal call, the prefix is the type itself.
8394
8395 if Is_Protected_Type (Scope (Nam))
8396 and then not Is_Variable (Obj)
8397 and then (not Is_Entity_Name (Obj)
8398 or else not Is_Type (Entity (Obj)))
8399 then
8400 Error_Msg_N
8401 ("prefix of protected procedure or entry call must be variable",
8402 Entry_Name);
8403 end if;
8404
8405 declare
8406 Entry_Call : Node_Id;
8407
8408 begin
8409 Entry_Call :=
8410 Make_Entry_Call_Statement (Loc,
8411 Name => Entry_Name,
8412 Parameter_Associations => Parameter_Associations (N));
8413
8414 -- Inherit relevant attributes from the original call
8415
8416 Set_First_Named_Actual
8417 (Entry_Call, First_Named_Actual (N));
8418
8419 Set_Is_Elaboration_Checks_OK_Node
8420 (Entry_Call, Is_Elaboration_Checks_OK_Node (N));
8421
8422 Set_Is_Elaboration_Warnings_OK_Node
8423 (Entry_Call, Is_Elaboration_Warnings_OK_Node (N));
8424
8425 Set_Is_SPARK_Mode_On_Node
8426 (Entry_Call, Is_SPARK_Mode_On_Node (N));
8427
8428 Rewrite (N, Entry_Call);
8429 Set_Analyzed (N, True);
8430 end;
8431
8432 -- Protected functions can return on the secondary stack, in which case
8433 -- we must trigger the transient scope mechanism.
8434
8435 elsif Expander_Active
8436 and then Requires_Transient_Scope (Etype (Nam))
8437 then
8438 Establish_Transient_Scope (N, Manage_Sec_Stack => True);
8439 end if;
8440
8441 -- Now we know that this is not a call to a function that returns an
8442 -- array type; moreover, we know the name of the called entry. Detect
8443 -- overlapping actuals, just like for a subprogram call.
8444
8445 Warn_On_Overlapping_Actuals (Nam, N);
8446
8447 end Resolve_Entry_Call;
8448
8449 -------------------------
8450 -- Resolve_Equality_Op --
8451 -------------------------
8452
8453 -- Both arguments must have the same type, and the boolean context does
8454 -- not participate in the resolution. The first pass verifies that the
8455 -- interpretation is not ambiguous, and the type of the left argument is
8456 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
8457 -- are strings or aggregates, allocators, or Null, they are ambiguous even
8458 -- though they carry a single (universal) type. Diagnose this case here.
8459
8460 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
8461 L : constant Node_Id := Left_Opnd (N);
8462 R : constant Node_Id := Right_Opnd (N);
8463 T : Entity_Id := Find_Unique_Type (L, R);
8464
8465 procedure Check_If_Expression (Cond : Node_Id);
8466 -- The resolution rule for if expressions requires that each such must
8467 -- have a unique type. This means that if several dependent expressions
8468 -- are of a non-null anonymous access type, and the context does not
8469 -- impose an expected type (as can be the case in an equality operation)
8470 -- the expression must be rejected.
8471
8472 procedure Explain_Redundancy (N : Node_Id);
8473 -- Attempt to explain the nature of a redundant comparison with True. If
8474 -- the expression N is too complex, this routine issues a general error
8475 -- message.
8476
8477 function Find_Unique_Access_Type return Entity_Id;
8478 -- In the case of allocators and access attributes, the context must
8479 -- provide an indication of the specific access type to be used. If
8480 -- one operand is of such a "generic" access type, check whether there
8481 -- is a specific visible access type that has the same designated type.
8482 -- This is semantically dubious, and of no interest to any real code,
8483 -- but c48008a makes it all worthwhile.
8484
8485 function Suspicious_Prio_For_Equality return Boolean;
8486 -- Returns True iff the parent node is a and/or/xor operation that
8487 -- could be the cause of confused priorities. Note that if the not is
8488 -- in parens, then False is returned.
8489
8490 -------------------------
8491 -- Check_If_Expression --
8492 -------------------------
8493
8494 procedure Check_If_Expression (Cond : Node_Id) is
8495 Then_Expr : Node_Id;
8496 Else_Expr : Node_Id;
8497
8498 begin
8499 if Nkind (Cond) = N_If_Expression then
8500 Then_Expr := Next (First (Expressions (Cond)));
8501 Else_Expr := Next (Then_Expr);
8502
8503 if Nkind (Then_Expr) /= N_Null
8504 and then Nkind (Else_Expr) /= N_Null
8505 then
8506 Error_Msg_N ("cannot determine type of if expression", Cond);
8507 end if;
8508 end if;
8509 end Check_If_Expression;
8510
8511 ------------------------
8512 -- Explain_Redundancy --
8513 ------------------------
8514
8515 procedure Explain_Redundancy (N : Node_Id) is
8516 Error : Name_Id;
8517 Val : Node_Id;
8518 Val_Id : Entity_Id;
8519
8520 begin
8521 Val := N;
8522
8523 -- Strip the operand down to an entity
8524
8525 loop
8526 if Nkind (Val) = N_Selected_Component then
8527 Val := Selector_Name (Val);
8528 else
8529 exit;
8530 end if;
8531 end loop;
8532
8533 -- The construct denotes an entity
8534
8535 if Is_Entity_Name (Val) and then Present (Entity (Val)) then
8536 Val_Id := Entity (Val);
8537
8538 -- Do not generate an error message when the comparison is done
8539 -- against the enumeration literal Standard.True.
8540
8541 if Ekind (Val_Id) /= E_Enumeration_Literal then
8542
8543 -- Build a customized error message
8544
8545 Name_Len := 0;
8546 Add_Str_To_Name_Buffer ("?r?");
8547
8548 if Ekind (Val_Id) = E_Component then
8549 Add_Str_To_Name_Buffer ("component ");
8550
8551 elsif Ekind (Val_Id) = E_Constant then
8552 Add_Str_To_Name_Buffer ("constant ");
8553
8554 elsif Ekind (Val_Id) = E_Discriminant then
8555 Add_Str_To_Name_Buffer ("discriminant ");
8556
8557 elsif Is_Formal (Val_Id) then
8558 Add_Str_To_Name_Buffer ("parameter ");
8559
8560 elsif Ekind (Val_Id) = E_Variable then
8561 Add_Str_To_Name_Buffer ("variable ");
8562 end if;
8563
8564 Add_Str_To_Name_Buffer ("& is always True!");
8565 Error := Name_Find;
8566
8567 Error_Msg_NE (Get_Name_String (Error), Val, Val_Id);
8568 end if;
8569
8570 -- The construct is too complex to disect, issue a general message
8571
8572 else
8573 Error_Msg_N ("?r?expression is always True!", Val);
8574 end if;
8575 end Explain_Redundancy;
8576
8577 -----------------------------
8578 -- Find_Unique_Access_Type --
8579 -----------------------------
8580
8581 function Find_Unique_Access_Type return Entity_Id is
8582 Acc : Entity_Id;
8583 E : Entity_Id;
8584 S : Entity_Id;
8585
8586 begin
8587 if Ekind (Etype (R)) in E_Allocator_Type | E_Access_Attribute_Type
8588 then
8589 Acc := Designated_Type (Etype (R));
8590
8591 elsif Ekind (Etype (L)) in E_Allocator_Type | E_Access_Attribute_Type
8592 then
8593 Acc := Designated_Type (Etype (L));
8594 else
8595 return Empty;
8596 end if;
8597
8598 S := Current_Scope;
8599 while S /= Standard_Standard loop
8600 E := First_Entity (S);
8601 while Present (E) loop
8602 if Is_Type (E)
8603 and then Is_Access_Type (E)
8604 and then Ekind (E) /= E_Allocator_Type
8605 and then Designated_Type (E) = Base_Type (Acc)
8606 then
8607 return E;
8608 end if;
8609
8610 Next_Entity (E);
8611 end loop;
8612
8613 S := Scope (S);
8614 end loop;
8615
8616 return Empty;
8617 end Find_Unique_Access_Type;
8618
8619 ----------------------------------
8620 -- Suspicious_Prio_For_Equality --
8621 ----------------------------------
8622
8623 function Suspicious_Prio_For_Equality return Boolean is
8624 Par : constant Node_Id := Parent (N);
8625
8626 begin
8627 -- Check if parent node is one of and/or/xor, not parenthesized
8628 -- explicitly, and its own parent is not of this kind. Otherwise,
8629 -- it's a case of chained Boolean conditions which is likely well
8630 -- parenthesized.
8631
8632 if Nkind (Par) in N_Op_And | N_Op_Or | N_Op_Xor
8633 and then Paren_Count (N) = 0
8634 and then Nkind (Parent (Par)) not in N_Op_And | N_Op_Or | N_Op_Xor
8635 then
8636 declare
8637 Compar : Node_Id :=
8638 (if Left_Opnd (Par) = N then
8639 Right_Opnd (Par)
8640 else
8641 Left_Opnd (Par));
8642 begin
8643 -- Compar may have been rewritten, for example from (a /= b)
8644 -- into not (a = b). Use the Original_Node instead.
8645
8646 Compar := Original_Node (Compar);
8647
8648 -- If the other argument of the and/or/xor is also a
8649 -- comparison, or another and/or/xor then most likely
8650 -- the priorities are correctly set.
8651
8652 return Nkind (Compar) not in N_Op_Boolean;
8653 end;
8654
8655 else
8656 return False;
8657 end if;
8658 end Suspicious_Prio_For_Equality;
8659
8660 -- Start of processing for Resolve_Equality_Op
8661
8662 begin
8663 Set_Etype (N, Base_Type (Typ));
8664 Generate_Reference (T, N, ' ');
8665
8666 if T = Any_Fixed then
8667 T := Unique_Fixed_Point_Type (L);
8668 end if;
8669
8670 if T /= Any_Type then
8671 if T = Any_String or else
8672 T = Any_Composite or else
8673 T = Any_Character
8674 then
8675 if T = Any_Character then
8676 Ambiguous_Character (L);
8677 else
8678 Error_Msg_N ("ambiguous operands for equality", N);
8679 end if;
8680
8681 Set_Etype (N, Any_Type);
8682 return;
8683
8684 elsif T = Any_Access
8685 or else Ekind (T) in E_Allocator_Type | E_Access_Attribute_Type
8686 then
8687 T := Find_Unique_Access_Type;
8688
8689 if No (T) then
8690 Error_Msg_N ("ambiguous operands for equality", N);
8691 Set_Etype (N, Any_Type);
8692 return;
8693 end if;
8694
8695 -- If expressions must have a single type, and if the context does
8696 -- not impose one the dependent expressions cannot be anonymous
8697 -- access types.
8698
8699 -- Why no similar processing for case expressions???
8700
8701 elsif Ada_Version >= Ada_2012
8702 and then Is_Anonymous_Access_Type (Etype (L))
8703 and then Is_Anonymous_Access_Type (Etype (R))
8704 then
8705 Check_If_Expression (L);
8706 Check_If_Expression (R);
8707 end if;
8708
8709 Resolve (L, T);
8710 Resolve (R, T);
8711
8712 -- If the unique type is a class-wide type then it will be expanded
8713 -- into a dispatching call to the predefined primitive. Therefore we
8714 -- check here for potential violation of such restriction.
8715
8716 if Is_Class_Wide_Type (T) then
8717 Check_Restriction (No_Dispatching_Calls, N);
8718 end if;
8719
8720 -- Only warn for redundant equality comparison to True for objects
8721 -- (e.g. "X = True") and operations (e.g. "(X < Y) = True"). For
8722 -- other expressions, it may be a matter of preference to write
8723 -- "Expr = True" or "Expr".
8724
8725 if Warn_On_Redundant_Constructs
8726 and then Comes_From_Source (N)
8727 and then Comes_From_Source (R)
8728 and then Is_Entity_Name (R)
8729 and then Entity (R) = Standard_True
8730 and then
8731 ((Is_Entity_Name (L) and then Is_Object (Entity (L)))
8732 or else
8733 Nkind (L) in N_Op)
8734 then
8735 Error_Msg_N -- CODEFIX
8736 ("?r?comparison with True is redundant!", N);
8737 Explain_Redundancy (Original_Node (R));
8738 end if;
8739
8740 -- Warn on a (in)equality between boolean values which is not
8741 -- parenthesized when the parent expression is one of and/or/xor, as
8742 -- this is interpreted as (a = b) op c where most likely a = (b op c)
8743 -- was intended. Do not generate a warning in generic instances, as
8744 -- the problematic expression may be implicitly parenthesized in
8745 -- the generic itself if one of the operators is a generic formal.
8746 -- Also do not generate a warning for generated equality, for
8747 -- example from rewritting a membership test.
8748
8749 if Warn_On_Questionable_Missing_Parens
8750 and then not In_Instance
8751 and then Comes_From_Source (N)
8752 and then Is_Boolean_Type (T)
8753 and then Suspicious_Prio_For_Equality
8754 then
8755 Error_Msg_N ("?q?equality should be parenthesized here!", N);
8756 end if;
8757
8758 -- If the equality is overloaded and the operands have resolved
8759 -- properly, set the proper equality operator on the node. The
8760 -- current setting is the first one found during analysis, which
8761 -- is not necessarily the one to which the node has resolved.
8762
8763 if Is_Overloaded (N) then
8764 declare
8765 I : Interp_Index;
8766 It : Interp;
8767
8768 begin
8769 Get_First_Interp (N, I, It);
8770
8771 -- If the equality is user-defined, the type of the operands
8772 -- matches that of the formals. For a predefined operator,
8773 -- it is the scope that matters, given that the predefined
8774 -- equality has Any_Type formals. In either case the result
8775 -- type (most often Boolean) must match the context. The scope
8776 -- is either that of the type, if there is a generated equality
8777 -- (when there is an equality for the component type), or else
8778 -- Standard otherwise.
8779
8780 while Present (It.Typ) loop
8781 if Etype (It.Nam) = Typ
8782 and then
8783 (Etype (First_Entity (It.Nam)) = Etype (L)
8784 or else Scope (It.Nam) = Standard_Standard
8785 or else Scope (It.Nam) = Scope (T))
8786 then
8787 Set_Entity (N, It.Nam);
8788
8789 Set_Is_Overloaded (N, False);
8790 exit;
8791 end if;
8792
8793 Get_Next_Interp (I, It);
8794 end loop;
8795
8796 -- If expansion is active and this is an inherited operation,
8797 -- replace it with its ancestor. This must not be done during
8798 -- preanalysis because the type may not be frozen yet, as when
8799 -- the context is a precondition or postcondition.
8800
8801 if Present (Alias (Entity (N))) and then Expander_Active then
8802 Set_Entity (N, Alias (Entity (N)));
8803 end if;
8804 end;
8805 end if;
8806
8807 Check_Unset_Reference (L);
8808 Check_Unset_Reference (R);
8809 Generate_Operator_Reference (N, T);
8810 Check_Low_Bound_Tested (N);
8811
8812 -- If this is an inequality, it may be the implicit inequality
8813 -- created for a user-defined operation, in which case the corres-
8814 -- ponding equality operation is not intrinsic, and the operation
8815 -- cannot be constant-folded. Else fold.
8816
8817 if Nkind (N) = N_Op_Eq
8818 or else Comes_From_Source (Entity (N))
8819 or else Ekind (Entity (N)) = E_Operator
8820 or else Is_Intrinsic_Subprogram
8821 (Corresponding_Equality (Entity (N)))
8822 then
8823 Analyze_Dimension (N);
8824 Eval_Relational_Op (N);
8825
8826 elsif Nkind (N) = N_Op_Ne
8827 and then Is_Abstract_Subprogram (Entity (N))
8828 then
8829 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
8830 end if;
8831
8832 -- Ada 2005: If one operand is an anonymous access type, convert the
8833 -- other operand to it, to ensure that the underlying types match in
8834 -- the back-end. Same for access_to_subprogram, and the conversion
8835 -- verifies that the types are subtype conformant.
8836
8837 -- We apply the same conversion in the case one of the operands is a
8838 -- private subtype of the type of the other.
8839
8840 -- Why the Expander_Active test here ???
8841
8842 if Expander_Active
8843 and then
8844 (Ekind (T) in E_Anonymous_Access_Type
8845 | E_Anonymous_Access_Subprogram_Type
8846 or else Is_Private_Type (T))
8847 then
8848 if Etype (L) /= T then
8849 Rewrite (L,
8850 Make_Unchecked_Type_Conversion (Sloc (L),
8851 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
8852 Expression => Relocate_Node (L)));
8853 Analyze_And_Resolve (L, T);
8854 end if;
8855
8856 if (Etype (R)) /= T then
8857 Rewrite (R,
8858 Make_Unchecked_Type_Conversion (Sloc (R),
8859 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
8860 Expression => Relocate_Node (R)));
8861 Analyze_And_Resolve (R, T);
8862 end if;
8863 end if;
8864 end if;
8865 end Resolve_Equality_Op;
8866
8867 ----------------------------------
8868 -- Resolve_Explicit_Dereference --
8869 ----------------------------------
8870
8871 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
8872 Loc : constant Source_Ptr := Sloc (N);
8873 New_N : Node_Id;
8874 P : constant Node_Id := Prefix (N);
8875
8876 P_Typ : Entity_Id;
8877 -- The candidate prefix type, if overloaded
8878
8879 I : Interp_Index;
8880 It : Interp;
8881
8882 begin
8883 Check_Fully_Declared_Prefix (Typ, P);
8884 P_Typ := Empty;
8885
8886 -- A useful optimization: check whether the dereference denotes an
8887 -- element of a container, and if so rewrite it as a call to the
8888 -- corresponding Element function.
8889
8890 -- Disabled for now, on advice of ARG. A more restricted form of the
8891 -- predicate might be acceptable ???
8892
8893 -- if Is_Container_Element (N) then
8894 -- return;
8895 -- end if;
8896
8897 if Is_Overloaded (P) then
8898
8899 -- Use the context type to select the prefix that has the correct
8900 -- designated type. Keep the first match, which will be the inner-
8901 -- most.
8902
8903 Get_First_Interp (P, I, It);
8904
8905 while Present (It.Typ) loop
8906 if Is_Access_Type (It.Typ)
8907 and then Covers (Typ, Designated_Type (It.Typ))
8908 then
8909 if No (P_Typ) then
8910 P_Typ := It.Typ;
8911 end if;
8912
8913 -- Remove access types that do not match, but preserve access
8914 -- to subprogram interpretations, in case a further dereference
8915 -- is needed (see below).
8916
8917 elsif Ekind (It.Typ) /= E_Access_Subprogram_Type then
8918 Remove_Interp (I);
8919 end if;
8920
8921 Get_Next_Interp (I, It);
8922 end loop;
8923
8924 if Present (P_Typ) then
8925 Resolve (P, P_Typ);
8926 Set_Etype (N, Designated_Type (P_Typ));
8927
8928 else
8929 -- If no interpretation covers the designated type of the prefix,
8930 -- this is the pathological case where not all implementations of
8931 -- the prefix allow the interpretation of the node as a call. Now
8932 -- that the expected type is known, Remove other interpretations
8933 -- from prefix, rewrite it as a call, and resolve again, so that
8934 -- the proper call node is generated.
8935
8936 Get_First_Interp (P, I, It);
8937 while Present (It.Typ) loop
8938 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
8939 Remove_Interp (I);
8940 end if;
8941
8942 Get_Next_Interp (I, It);
8943 end loop;
8944
8945 New_N :=
8946 Make_Function_Call (Loc,
8947 Name =>
8948 Make_Explicit_Dereference (Loc,
8949 Prefix => P),
8950 Parameter_Associations => New_List);
8951
8952 Save_Interps (N, New_N);
8953 Rewrite (N, New_N);
8954 Analyze_And_Resolve (N, Typ);
8955 return;
8956 end if;
8957
8958 -- If not overloaded, resolve P with its own type
8959
8960 else
8961 Resolve (P);
8962 end if;
8963
8964 -- If the prefix might be null, add an access check
8965
8966 if Is_Access_Type (Etype (P))
8967 and then not Can_Never_Be_Null (Etype (P))
8968 then
8969 Apply_Access_Check (N);
8970 end if;
8971
8972 -- If the designated type is a packed unconstrained array type, and the
8973 -- explicit dereference is not in the context of an attribute reference,
8974 -- then we must compute and set the actual subtype, since it is needed
8975 -- by Gigi. The reason we exclude the attribute case is that this is
8976 -- handled fine by Gigi, and in fact we use such attributes to build the
8977 -- actual subtype. We also exclude generated code (which builds actual
8978 -- subtypes directly if they are needed).
8979
8980 if Is_Packed_Array (Etype (N))
8981 and then not Is_Constrained (Etype (N))
8982 and then Nkind (Parent (N)) /= N_Attribute_Reference
8983 and then Comes_From_Source (N)
8984 then
8985 Set_Etype (N, Get_Actual_Subtype (N));
8986 end if;
8987
8988 Analyze_Dimension (N);
8989
8990 -- Note: No Eval processing is required for an explicit dereference,
8991 -- because such a name can never be static.
8992
8993 end Resolve_Explicit_Dereference;
8994
8995 -------------------------------------
8996 -- Resolve_Expression_With_Actions --
8997 -------------------------------------
8998
8999 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
9000
9001 function OK_For_Static (Act : Node_Id) return Boolean;
9002 -- True if Act is an action of a declare_expression that is allowed in a
9003 -- static declare_expression.
9004
9005 function All_OK_For_Static return Boolean;
9006 -- True if all actions of N are allowed in a static declare_expression.
9007
9008 function Get_Literal (Expr : Node_Id) return Node_Id;
9009 -- Expr is an expression with compile-time-known value. This returns the
9010 -- literal node that reprsents that value.
9011
9012 function OK_For_Static (Act : Node_Id) return Boolean is
9013 begin
9014 case Nkind (Act) is
9015 when N_Object_Declaration =>
9016 if Constant_Present (Act)
9017 and then Is_Static_Expression (Expression (Act))
9018 then
9019 return True;
9020 end if;
9021
9022 when N_Object_Renaming_Declaration =>
9023 if Statically_Names_Object (Name (Act)) then
9024 return True;
9025 end if;
9026
9027 when others =>
9028 -- No other declarations, nor even pragmas, are allowed in a
9029 -- declare expression, so if we see something else, it must be
9030 -- an internally generated expression_with_actions.
9031 null;
9032 end case;
9033
9034 return False;
9035 end OK_For_Static;
9036
9037 function All_OK_For_Static return Boolean is
9038 Act : Node_Id := First (Actions (N));
9039 begin
9040 while Present (Act) loop
9041 if not OK_For_Static (Act) then
9042 return False;
9043 end if;
9044
9045 Next (Act);
9046 end loop;
9047
9048 return True;
9049 end All_OK_For_Static;
9050
9051 function Get_Literal (Expr : Node_Id) return Node_Id is
9052 pragma Assert (Compile_Time_Known_Value (Expr));
9053 Result : Node_Id;
9054 begin
9055 case Nkind (Expr) is
9056 when N_Has_Entity =>
9057 if Ekind (Entity (Expr)) = E_Enumeration_Literal then
9058 Result := Expr;
9059 else
9060 Result := Constant_Value (Entity (Expr));
9061 end if;
9062 when N_Numeric_Or_String_Literal =>
9063 Result := Expr;
9064 when others =>
9065 raise Program_Error;
9066 end case;
9067
9068 pragma Assert
9069 (Nkind (Result) in N_Numeric_Or_String_Literal
9070 or else Ekind (Entity (Result)) = E_Enumeration_Literal);
9071 return Result;
9072 end Get_Literal;
9073
9074 Loc : constant Source_Ptr := Sloc (N);
9075
9076 begin
9077 Set_Etype (N, Typ);
9078
9079 if Is_Empty_List (Actions (N)) then
9080 pragma Assert (All_OK_For_Static); null;
9081 end if;
9082
9083 -- If the value of the expression is known at compile time, and all
9084 -- of the actions (if any) are suitable, then replace the declare
9085 -- expression with its expression. This allows the declare expression
9086 -- as a whole to be static if appropriate. See AI12-0368.
9087
9088 if Compile_Time_Known_Value (Expression (N)) then
9089 if Is_Empty_List (Actions (N)) then
9090 Rewrite (N, Expression (N));
9091 elsif All_OK_For_Static then
9092 Rewrite
9093 (N, New_Copy_Tree
9094 (Get_Literal (Expression (N)), New_Sloc => Loc));
9095 end if;
9096 end if;
9097 end Resolve_Expression_With_Actions;
9098
9099 ----------------------------------
9100 -- Resolve_Generalized_Indexing --
9101 ----------------------------------
9102
9103 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id) is
9104 Indexing : constant Node_Id := Generalized_Indexing (N);
9105 begin
9106 Rewrite (N, Indexing);
9107 Resolve (N, Typ);
9108 end Resolve_Generalized_Indexing;
9109
9110 ---------------------------
9111 -- Resolve_If_Expression --
9112 ---------------------------
9113
9114 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id) is
9115 procedure Apply_Check (Expr : Node_Id);
9116 -- When a dependent expression is of a subtype different from
9117 -- the context subtype, then insert a qualification to ensure
9118 -- the generation of a constraint check. This was previously
9119 -- for scalar types. For array types apply a length check, given
9120 -- that the context in general allows sliding, while a qualified
9121 -- expression forces equality of bounds.
9122
9123 Result_Type : Entity_Id := Typ;
9124 -- So in most cases the type of the If_Expression and of its
9125 -- dependent expressions is that of the context. However, if
9126 -- the expression is the index of an Indexed_Component, we must
9127 -- ensure that a proper index check is applied, rather than a
9128 -- range check on the index type (which might be discriminant
9129 -- dependent). In this case we resolve with the base type of the
9130 -- index type, and the index check is generated in the resolution
9131 -- of the indexed_component above.
9132
9133 -----------------
9134 -- Apply_Check --
9135 -----------------
9136
9137 procedure Apply_Check (Expr : Node_Id) is
9138 Expr_Typ : constant Entity_Id := Etype (Expr);
9139 Loc : constant Source_Ptr := Sloc (Expr);
9140
9141 begin
9142 if Expr_Typ = Typ
9143 or else Is_Tagged_Type (Typ)
9144 or else Is_Access_Type (Typ)
9145 or else not Is_Constrained (Typ)
9146 or else Inside_A_Generic
9147 then
9148 null;
9149
9150 elsif Is_Array_Type (Typ) then
9151 Apply_Length_Check (Expr, Typ);
9152
9153 else
9154 Rewrite (Expr,
9155 Make_Qualified_Expression (Loc,
9156 Subtype_Mark => New_Occurrence_Of (Result_Type, Loc),
9157 Expression => Relocate_Node (Expr)));
9158
9159 Analyze_And_Resolve (Expr, Result_Type);
9160 end if;
9161 end Apply_Check;
9162
9163 -- Local variables
9164
9165 Condition : constant Node_Id := First (Expressions (N));
9166 Else_Expr : Node_Id;
9167 Then_Expr : Node_Id;
9168
9169 -- Start of processing for Resolve_If_Expression
9170
9171 begin
9172 -- Defend against malformed expressions
9173
9174 if No (Condition) then
9175 return;
9176 end if;
9177
9178 if Nkind (Parent (N)) = N_Indexed_Component
9179 or else Nkind (Parent (Parent (N))) = N_Indexed_Component
9180 then
9181 Result_Type := Base_Type (Typ);
9182 end if;
9183
9184 Then_Expr := Next (Condition);
9185
9186 if No (Then_Expr) then
9187 return;
9188 end if;
9189
9190 Else_Expr := Next (Then_Expr);
9191
9192 Resolve (Condition, Any_Boolean);
9193 Resolve (Then_Expr, Result_Type);
9194 Apply_Check (Then_Expr);
9195
9196 -- If ELSE expression present, just resolve using the determined type
9197 -- If type is universal, resolve to any member of the class.
9198
9199 if Present (Else_Expr) then
9200 if Typ = Universal_Integer then
9201 Resolve (Else_Expr, Any_Integer);
9202
9203 elsif Typ = Universal_Real then
9204 Resolve (Else_Expr, Any_Real);
9205
9206 else
9207 Resolve (Else_Expr, Result_Type);
9208 end if;
9209
9210 Apply_Check (Else_Expr);
9211
9212 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
9213 -- dynamically tagged must be known statically.
9214
9215 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
9216 if Is_Dynamically_Tagged (Then_Expr) /=
9217 Is_Dynamically_Tagged (Else_Expr)
9218 then
9219 Error_Msg_N ("all or none of the dependent expressions "
9220 & "can be dynamically tagged", N);
9221 end if;
9222 end if;
9223
9224 -- If no ELSE expression is present, root type must be Standard.Boolean
9225 -- and we provide a Standard.True result converted to the appropriate
9226 -- Boolean type (in case it is a derived boolean type).
9227
9228 elsif Root_Type (Typ) = Standard_Boolean then
9229 Else_Expr :=
9230 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
9231 Analyze_And_Resolve (Else_Expr, Result_Type);
9232 Append_To (Expressions (N), Else_Expr);
9233
9234 else
9235 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
9236 Append_To (Expressions (N), Error);
9237 end if;
9238
9239 Set_Etype (N, Result_Type);
9240
9241 if not Error_Posted (N) then
9242 Eval_If_Expression (N);
9243 end if;
9244
9245 Analyze_Dimension (N);
9246 end Resolve_If_Expression;
9247
9248 ----------------------------------
9249 -- Resolve_Implicit_Dereference --
9250 ----------------------------------
9251
9252 procedure Resolve_Implicit_Dereference (P : Node_Id) is
9253 Desig_Typ : Entity_Id;
9254
9255 begin
9256 -- In an instance the proper view may not always be correct for
9257 -- private types, see e.g. Sem_Type.Covers for similar handling.
9258
9259 if Is_Private_Type (Etype (P))
9260 and then Present (Full_View (Etype (P)))
9261 and then Is_Access_Type (Full_View (Etype (P)))
9262 and then In_Instance
9263 then
9264 Set_Etype (P, Full_View (Etype (P)));
9265 end if;
9266
9267 if Is_Access_Type (Etype (P)) then
9268 Desig_Typ := Implicitly_Designated_Type (Etype (P));
9269 Insert_Explicit_Dereference (P);
9270 Analyze_And_Resolve (P, Desig_Typ);
9271 end if;
9272 end Resolve_Implicit_Dereference;
9273
9274 -------------------------------
9275 -- Resolve_Indexed_Component --
9276 -------------------------------
9277
9278 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
9279 Name : constant Node_Id := Prefix (N);
9280 Expr : Node_Id;
9281 Array_Type : Entity_Id := Empty; -- to prevent junk warning
9282 Index : Node_Id;
9283
9284 begin
9285 if Present (Generalized_Indexing (N)) then
9286 Resolve_Generalized_Indexing (N, Typ);
9287 return;
9288 end if;
9289
9290 if Is_Overloaded (Name) then
9291
9292 -- Use the context type to select the prefix that yields the correct
9293 -- component type.
9294
9295 declare
9296 I : Interp_Index;
9297 It : Interp;
9298 I1 : Interp_Index := 0;
9299 P : constant Node_Id := Prefix (N);
9300 Found : Boolean := False;
9301
9302 begin
9303 Get_First_Interp (P, I, It);
9304 while Present (It.Typ) loop
9305 if (Is_Array_Type (It.Typ)
9306 and then Covers (Typ, Component_Type (It.Typ)))
9307 or else (Is_Access_Type (It.Typ)
9308 and then Is_Array_Type (Designated_Type (It.Typ))
9309 and then
9310 Covers
9311 (Typ,
9312 Component_Type (Designated_Type (It.Typ))))
9313 then
9314 if Found then
9315 It := Disambiguate (P, I1, I, Any_Type);
9316
9317 if It = No_Interp then
9318 Error_Msg_N ("ambiguous prefix for indexing", N);
9319 Set_Etype (N, Typ);
9320 return;
9321
9322 else
9323 Found := True;
9324 Array_Type := It.Typ;
9325 I1 := I;
9326 end if;
9327
9328 else
9329 Found := True;
9330 Array_Type := It.Typ;
9331 I1 := I;
9332 end if;
9333 end if;
9334
9335 Get_Next_Interp (I, It);
9336 end loop;
9337 end;
9338
9339 else
9340 Array_Type := Etype (Name);
9341 end if;
9342
9343 Resolve (Name, Array_Type);
9344 Array_Type := Get_Actual_Subtype_If_Available (Name);
9345
9346 -- If the prefix's type is an access type, get to the real array type.
9347 -- Note: we do not apply an access check because an explicit dereference
9348 -- will be introduced later, and the check will happen there.
9349
9350 if Is_Access_Type (Array_Type) then
9351 Array_Type := Implicitly_Designated_Type (Array_Type);
9352 end if;
9353
9354 -- If name was overloaded, set component type correctly now.
9355 -- If a misplaced call to an entry family (which has no index types)
9356 -- return. Error will be diagnosed from calling context.
9357
9358 if Is_Array_Type (Array_Type) then
9359 Set_Etype (N, Component_Type (Array_Type));
9360 else
9361 return;
9362 end if;
9363
9364 Index := First_Index (Array_Type);
9365 Expr := First (Expressions (N));
9366
9367 -- The prefix may have resolved to a string literal, in which case its
9368 -- etype has a special representation. This is only possible currently
9369 -- if the prefix is a static concatenation, written in functional
9370 -- notation.
9371
9372 if Ekind (Array_Type) = E_String_Literal_Subtype then
9373 Resolve (Expr, Standard_Positive);
9374
9375 else
9376 while Present (Index) and then Present (Expr) loop
9377 Resolve (Expr, Etype (Index));
9378 Check_Unset_Reference (Expr);
9379
9380 Apply_Scalar_Range_Check (Expr, Etype (Index));
9381
9382 Next_Index (Index);
9383 Next (Expr);
9384 end loop;
9385 end if;
9386
9387 Resolve_Implicit_Dereference (Prefix (N));
9388 Analyze_Dimension (N);
9389
9390 -- Do not generate the warning on suspicious index if we are analyzing
9391 -- package Ada.Tags; otherwise we will report the warning with the
9392 -- Prims_Ptr field of the dispatch table.
9393
9394 if Scope (Etype (Prefix (N))) = Standard_Standard
9395 or else not
9396 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
9397 Ada_Tags)
9398 then
9399 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
9400 Eval_Indexed_Component (N);
9401 end if;
9402
9403 -- If the array type is atomic and the component is not, then this is
9404 -- worth a warning before Ada 2020, since we have a situation where the
9405 -- access to the component may cause extra read/writes of the atomic
9406 -- object, or partial word accesses, both of which may be unexpected.
9407
9408 if Nkind (N) = N_Indexed_Component
9409 and then Is_Atomic_Ref_With_Address (N)
9410 and then not (Has_Atomic_Components (Array_Type)
9411 or else (Is_Entity_Name (Prefix (N))
9412 and then Has_Atomic_Components
9413 (Entity (Prefix (N)))))
9414 and then not Is_Atomic (Component_Type (Array_Type))
9415 and then Ada_Version < Ada_2020
9416 then
9417 Error_Msg_N
9418 ("??access to non-atomic component of atomic array", Prefix (N));
9419 Error_Msg_N
9420 ("??\may cause unexpected accesses to atomic object", Prefix (N));
9421 end if;
9422 end Resolve_Indexed_Component;
9423
9424 -----------------------------
9425 -- Resolve_Integer_Literal --
9426 -----------------------------
9427
9428 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
9429 begin
9430 Set_Etype (N, Typ);
9431 Eval_Integer_Literal (N);
9432 end Resolve_Integer_Literal;
9433
9434 --------------------------------
9435 -- Resolve_Intrinsic_Operator --
9436 --------------------------------
9437
9438 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
9439 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
9440 Op : Entity_Id;
9441 Arg1 : Node_Id;
9442 Arg2 : Node_Id;
9443
9444 function Convert_Operand (Opnd : Node_Id) return Node_Id;
9445 -- If the operand is a literal, it cannot be the expression in a
9446 -- conversion. Use a qualified expression instead.
9447
9448 ---------------------
9449 -- Convert_Operand --
9450 ---------------------
9451
9452 function Convert_Operand (Opnd : Node_Id) return Node_Id is
9453 Loc : constant Source_Ptr := Sloc (Opnd);
9454 Res : Node_Id;
9455
9456 begin
9457 if Nkind (Opnd) in N_Integer_Literal | N_Real_Literal then
9458 Res :=
9459 Make_Qualified_Expression (Loc,
9460 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
9461 Expression => Relocate_Node (Opnd));
9462 Analyze (Res);
9463
9464 else
9465 Res := Unchecked_Convert_To (Btyp, Opnd);
9466 end if;
9467
9468 return Res;
9469 end Convert_Operand;
9470
9471 -- Start of processing for Resolve_Intrinsic_Operator
9472
9473 begin
9474 -- We must preserve the original entity in a generic setting, so that
9475 -- the legality of the operation can be verified in an instance.
9476
9477 if not Expander_Active then
9478 return;
9479 end if;
9480
9481 Op := Entity (N);
9482 while Scope (Op) /= Standard_Standard loop
9483 Op := Homonym (Op);
9484 pragma Assert (Present (Op));
9485 end loop;
9486
9487 Set_Entity (N, Op);
9488 Set_Is_Overloaded (N, False);
9489
9490 -- If the result or operand types are private, rewrite with unchecked
9491 -- conversions on the operands and the result, to expose the proper
9492 -- underlying numeric type.
9493
9494 if Is_Private_Type (Typ)
9495 or else Is_Private_Type (Etype (Left_Opnd (N)))
9496 or else Is_Private_Type (Etype (Right_Opnd (N)))
9497 then
9498 Arg1 := Convert_Operand (Left_Opnd (N));
9499
9500 if Nkind (N) = N_Op_Expon then
9501 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
9502 else
9503 Arg2 := Convert_Operand (Right_Opnd (N));
9504 end if;
9505
9506 if Nkind (Arg1) = N_Type_Conversion then
9507 Save_Interps (Left_Opnd (N), Expression (Arg1));
9508 end if;
9509
9510 if Nkind (Arg2) = N_Type_Conversion then
9511 Save_Interps (Right_Opnd (N), Expression (Arg2));
9512 end if;
9513
9514 Set_Left_Opnd (N, Arg1);
9515 Set_Right_Opnd (N, Arg2);
9516
9517 Set_Etype (N, Btyp);
9518 Rewrite (N, Unchecked_Convert_To (Typ, N));
9519 Resolve (N, Typ);
9520
9521 elsif Typ /= Etype (Left_Opnd (N))
9522 or else Typ /= Etype (Right_Opnd (N))
9523 then
9524 -- Add explicit conversion where needed, and save interpretations in
9525 -- case operands are overloaded.
9526
9527 Arg1 := Convert_To (Typ, Left_Opnd (N));
9528 Arg2 := Convert_To (Typ, Right_Opnd (N));
9529
9530 if Nkind (Arg1) = N_Type_Conversion then
9531 Save_Interps (Left_Opnd (N), Expression (Arg1));
9532 else
9533 Save_Interps (Left_Opnd (N), Arg1);
9534 end if;
9535
9536 if Nkind (Arg2) = N_Type_Conversion then
9537 Save_Interps (Right_Opnd (N), Expression (Arg2));
9538 else
9539 Save_Interps (Right_Opnd (N), Arg2);
9540 end if;
9541
9542 Rewrite (Left_Opnd (N), Arg1);
9543 Rewrite (Right_Opnd (N), Arg2);
9544 Analyze (Arg1);
9545 Analyze (Arg2);
9546 Resolve_Arithmetic_Op (N, Typ);
9547
9548 else
9549 Resolve_Arithmetic_Op (N, Typ);
9550 end if;
9551 end Resolve_Intrinsic_Operator;
9552
9553 --------------------------------------
9554 -- Resolve_Intrinsic_Unary_Operator --
9555 --------------------------------------
9556
9557 procedure Resolve_Intrinsic_Unary_Operator
9558 (N : Node_Id;
9559 Typ : Entity_Id)
9560 is
9561 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
9562 Op : Entity_Id;
9563 Arg2 : Node_Id;
9564
9565 begin
9566 Op := Entity (N);
9567 while Scope (Op) /= Standard_Standard loop
9568 Op := Homonym (Op);
9569 pragma Assert (Present (Op));
9570 end loop;
9571
9572 Set_Entity (N, Op);
9573
9574 if Is_Private_Type (Typ) then
9575 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
9576 Save_Interps (Right_Opnd (N), Expression (Arg2));
9577
9578 Set_Right_Opnd (N, Arg2);
9579
9580 Set_Etype (N, Btyp);
9581 Rewrite (N, Unchecked_Convert_To (Typ, N));
9582 Resolve (N, Typ);
9583
9584 else
9585 Resolve_Unary_Op (N, Typ);
9586 end if;
9587 end Resolve_Intrinsic_Unary_Operator;
9588
9589 ------------------------
9590 -- Resolve_Logical_Op --
9591 ------------------------
9592
9593 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
9594 B_Typ : Entity_Id;
9595
9596 begin
9597 Check_No_Direct_Boolean_Operators (N);
9598
9599 -- Predefined operations on scalar types yield the base type. On the
9600 -- other hand, logical operations on arrays yield the type of the
9601 -- arguments (and the context).
9602
9603 if Is_Array_Type (Typ) then
9604 B_Typ := Typ;
9605 else
9606 B_Typ := Base_Type (Typ);
9607 end if;
9608
9609 -- The following test is required because the operands of the operation
9610 -- may be literals, in which case the resulting type appears to be
9611 -- compatible with a signed integer type, when in fact it is compatible
9612 -- only with modular types. If the context itself is universal, the
9613 -- operation is illegal.
9614
9615 if not Valid_Boolean_Arg (Typ) then
9616 Error_Msg_N ("invalid context for logical operation", N);
9617 Set_Etype (N, Any_Type);
9618 return;
9619
9620 elsif Typ = Any_Modular then
9621 Error_Msg_N
9622 ("no modular type available in this context", N);
9623 Set_Etype (N, Any_Type);
9624 return;
9625
9626 elsif Is_Modular_Integer_Type (Typ)
9627 and then Etype (Left_Opnd (N)) = Universal_Integer
9628 and then Etype (Right_Opnd (N)) = Universal_Integer
9629 then
9630 Check_For_Visible_Operator (N, B_Typ);
9631 end if;
9632
9633 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
9634 -- is active and the result type is standard Boolean (do not mess with
9635 -- ops that return a nonstandard Boolean type, because something strange
9636 -- is going on).
9637
9638 -- Note: you might expect this replacement to be done during expansion,
9639 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
9640 -- is used, no part of the right operand of an "and" or "or" operator
9641 -- should be executed if the left operand would short-circuit the
9642 -- evaluation of the corresponding "and then" or "or else". If we left
9643 -- the replacement to expansion time, then run-time checks associated
9644 -- with such operands would be evaluated unconditionally, due to being
9645 -- before the condition prior to the rewriting as short-circuit forms
9646 -- during expansion.
9647
9648 if Short_Circuit_And_Or
9649 and then B_Typ = Standard_Boolean
9650 and then Nkind (N) in N_Op_And | N_Op_Or
9651 then
9652 -- Mark the corresponding putative SCO operator as truly a logical
9653 -- (and short-circuit) operator.
9654
9655 if Generate_SCO and then Comes_From_Source (N) then
9656 Set_SCO_Logical_Operator (N);
9657 end if;
9658
9659 if Nkind (N) = N_Op_And then
9660 Rewrite (N,
9661 Make_And_Then (Sloc (N),
9662 Left_Opnd => Relocate_Node (Left_Opnd (N)),
9663 Right_Opnd => Relocate_Node (Right_Opnd (N))));
9664 Analyze_And_Resolve (N, B_Typ);
9665
9666 -- Case of OR changed to OR ELSE
9667
9668 else
9669 Rewrite (N,
9670 Make_Or_Else (Sloc (N),
9671 Left_Opnd => Relocate_Node (Left_Opnd (N)),
9672 Right_Opnd => Relocate_Node (Right_Opnd (N))));
9673 Analyze_And_Resolve (N, B_Typ);
9674 end if;
9675
9676 -- Return now, since analysis of the rewritten ops will take care of
9677 -- other reference bookkeeping and expression folding.
9678
9679 return;
9680 end if;
9681
9682 Resolve (Left_Opnd (N), B_Typ);
9683 Resolve (Right_Opnd (N), B_Typ);
9684
9685 Check_Unset_Reference (Left_Opnd (N));
9686 Check_Unset_Reference (Right_Opnd (N));
9687
9688 Set_Etype (N, B_Typ);
9689 Generate_Operator_Reference (N, B_Typ);
9690 Eval_Logical_Op (N);
9691 end Resolve_Logical_Op;
9692
9693 ---------------------------
9694 -- Resolve_Membership_Op --
9695 ---------------------------
9696
9697 -- The context can only be a boolean type, and does not determine the
9698 -- arguments. Arguments should be unambiguous, but the preference rule for
9699 -- universal types applies.
9700
9701 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
9702 pragma Assert (Is_Boolean_Type (Typ));
9703
9704 L : constant Node_Id := Left_Opnd (N);
9705 R : constant Node_Id := Right_Opnd (N);
9706 T : Entity_Id;
9707
9708 procedure Resolve_Set_Membership;
9709 -- Analysis has determined a unique type for the left operand. Use it as
9710 -- the basis to resolve the disjuncts.
9711
9712 ----------------------------
9713 -- Resolve_Set_Membership --
9714 ----------------------------
9715
9716 procedure Resolve_Set_Membership is
9717 Alt : Node_Id;
9718
9719 begin
9720 -- If the left operand is overloaded, find type compatible with not
9721 -- overloaded alternative of the right operand.
9722
9723 Alt := First (Alternatives (N));
9724 if Is_Overloaded (L) then
9725 T := Empty;
9726 while Present (Alt) loop
9727 if not Is_Overloaded (Alt) then
9728 T := Intersect_Types (L, Alt);
9729 exit;
9730 else
9731 Next (Alt);
9732 end if;
9733 end loop;
9734
9735 -- Unclear how to resolve expression if all alternatives are also
9736 -- overloaded.
9737
9738 if No (T) then
9739 Error_Msg_N ("ambiguous expression", N);
9740 end if;
9741
9742 else
9743 T := Intersect_Types (L, Alt);
9744 end if;
9745
9746 Resolve (L, T);
9747
9748 Alt := First (Alternatives (N));
9749 while Present (Alt) loop
9750
9751 -- Alternative is an expression, a range
9752 -- or a subtype mark.
9753
9754 if not Is_Entity_Name (Alt)
9755 or else not Is_Type (Entity (Alt))
9756 then
9757 Resolve (Alt, T);
9758 end if;
9759
9760 Next (Alt);
9761 end loop;
9762
9763 -- Check for duplicates for discrete case
9764
9765 if Is_Discrete_Type (T) then
9766 declare
9767 type Ent is record
9768 Alt : Node_Id;
9769 Val : Uint;
9770 end record;
9771
9772 Alts : array (0 .. List_Length (Alternatives (N))) of Ent;
9773 Nalts : Nat;
9774
9775 begin
9776 -- Loop checking duplicates. This is quadratic, but giant sets
9777 -- are unlikely in this context so it's a reasonable choice.
9778
9779 Nalts := 0;
9780 Alt := First (Alternatives (N));
9781 while Present (Alt) loop
9782 if Is_OK_Static_Expression (Alt)
9783 and then Nkind (Alt) in N_Integer_Literal
9784 | N_Character_Literal
9785 | N_Has_Entity
9786 then
9787 Nalts := Nalts + 1;
9788 Alts (Nalts) := (Alt, Expr_Value (Alt));
9789
9790 for J in 1 .. Nalts - 1 loop
9791 if Alts (J).Val = Alts (Nalts).Val then
9792 Error_Msg_Sloc := Sloc (Alts (J).Alt);
9793 Error_Msg_N ("duplicate of value given#??", Alt);
9794 end if;
9795 end loop;
9796 end if;
9797
9798 Next (Alt);
9799 end loop;
9800 end;
9801 end if;
9802
9803 -- RM 4.5.2 (28.1/3) specifies that for types other than records or
9804 -- limited types, evaluation of a membership test uses the predefined
9805 -- equality for the type. This may be confusing to users, and the
9806 -- following warning appears useful for the most common case.
9807
9808 if Is_Scalar_Type (Etype (L))
9809 and then Present (Get_User_Defined_Eq (Etype (L)))
9810 then
9811 Error_Msg_NE
9812 ("membership test on& uses predefined equality?", N, Etype (L));
9813 Error_Msg_N
9814 ("\even if user-defined equality exists (RM 4.5.2 (28.1/3)?", N);
9815 end if;
9816 end Resolve_Set_Membership;
9817
9818 -- Start of processing for Resolve_Membership_Op
9819
9820 begin
9821 if L = Error or else R = Error then
9822 return;
9823 end if;
9824
9825 if Present (Alternatives (N)) then
9826 Resolve_Set_Membership;
9827 goto SM_Exit;
9828
9829 elsif not Is_Overloaded (R)
9830 and then Is_Universal_Numeric_Type (Etype (R))
9831 and then Is_Overloaded (L)
9832 then
9833 T := Etype (R);
9834
9835 -- Ada 2005 (AI-251): Support the following case:
9836
9837 -- type I is interface;
9838 -- type T is tagged ...
9839
9840 -- function Test (O : I'Class) is
9841 -- begin
9842 -- return O in T'Class.
9843 -- end Test;
9844
9845 -- In this case we have nothing else to do. The membership test will be
9846 -- done at run time.
9847
9848 elsif Ada_Version >= Ada_2005
9849 and then Is_Class_Wide_Type (Etype (L))
9850 and then Is_Interface (Etype (L))
9851 and then not Is_Interface (Etype (R))
9852 then
9853 return;
9854 else
9855 T := Intersect_Types (L, R);
9856 end if;
9857
9858 -- If mixed-mode operations are present and operands are all literal,
9859 -- the only interpretation involves Duration, which is probably not
9860 -- the intention of the programmer.
9861
9862 if T = Any_Fixed then
9863 T := Unique_Fixed_Point_Type (N);
9864
9865 if T = Any_Type then
9866 return;
9867 end if;
9868 end if;
9869
9870 Resolve (L, T);
9871 Check_Unset_Reference (L);
9872
9873 if Nkind (R) = N_Range
9874 and then not Is_Scalar_Type (T)
9875 then
9876 Error_Msg_N ("scalar type required for range", R);
9877 end if;
9878
9879 if Is_Entity_Name (R) then
9880 Freeze_Expression (R);
9881 else
9882 Resolve (R, T);
9883 Check_Unset_Reference (R);
9884 end if;
9885
9886 -- Here after resolving membership operation
9887
9888 <<SM_Exit>>
9889
9890 Eval_Membership_Op (N);
9891 end Resolve_Membership_Op;
9892
9893 ------------------
9894 -- Resolve_Null --
9895 ------------------
9896
9897 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
9898 Loc : constant Source_Ptr := Sloc (N);
9899
9900 begin
9901 -- Handle restriction against anonymous null access values This
9902 -- restriction can be turned off using -gnatdj.
9903
9904 -- Ada 2005 (AI-231): Remove restriction
9905
9906 if Ada_Version < Ada_2005
9907 and then not Debug_Flag_J
9908 and then Ekind (Typ) = E_Anonymous_Access_Type
9909 and then Comes_From_Source (N)
9910 then
9911 -- In the common case of a call which uses an explicitly null value
9912 -- for an access parameter, give specialized error message.
9913
9914 if Nkind (Parent (N)) in N_Subprogram_Call then
9915 Error_Msg_N
9916 ("NULL is not allowed as argument for an access parameter", N);
9917
9918 -- Standard message for all other cases (are there any?)
9919
9920 else
9921 Error_Msg_N
9922 ("NULL cannot be of an anonymous access type", N);
9923 end if;
9924 end if;
9925
9926 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
9927 -- assignment to a null-excluding object.
9928
9929 if Ada_Version >= Ada_2005
9930 and then Can_Never_Be_Null (Typ)
9931 and then Nkind (Parent (N)) = N_Assignment_Statement
9932 then
9933 if Inside_Init_Proc then
9934
9935 -- Decide whether to generate an if_statement around our
9936 -- null-excluding check to avoid them on certain internal object
9937 -- declarations by looking at the type the current Init_Proc
9938 -- belongs to.
9939
9940 -- Generate:
9941 -- if T1b_skip_null_excluding_check then
9942 -- [constraint_error "access check failed"]
9943 -- end if;
9944
9945 if Needs_Conditional_Null_Excluding_Check
9946 (Etype (First_Formal (Enclosing_Init_Proc)))
9947 then
9948 Insert_Action (N,
9949 Make_If_Statement (Loc,
9950 Condition =>
9951 Make_Identifier (Loc,
9952 New_External_Name
9953 (Chars (Typ), "_skip_null_excluding_check")),
9954 Then_Statements =>
9955 New_List (
9956 Make_Raise_Constraint_Error (Loc,
9957 Reason => CE_Access_Check_Failed))));
9958
9959 -- Otherwise, simply create the check
9960
9961 else
9962 Insert_Action (N,
9963 Make_Raise_Constraint_Error (Loc,
9964 Reason => CE_Access_Check_Failed));
9965 end if;
9966 else
9967 Insert_Action
9968 (Compile_Time_Constraint_Error (N,
9969 "(Ada 2005) NULL not allowed in null-excluding objects??"),
9970 Make_Raise_Constraint_Error (Loc,
9971 Reason => CE_Access_Check_Failed));
9972 end if;
9973 end if;
9974
9975 -- In a distributed context, null for a remote access to subprogram may
9976 -- need to be replaced with a special record aggregate. In this case,
9977 -- return after having done the transformation.
9978
9979 if (Ekind (Typ) = E_Record_Type
9980 or else Is_Remote_Access_To_Subprogram_Type (Typ))
9981 and then Remote_AST_Null_Value (N, Typ)
9982 then
9983 return;
9984 end if;
9985
9986 -- The null literal takes its type from the context
9987
9988 Set_Etype (N, Typ);
9989 end Resolve_Null;
9990
9991 -----------------------
9992 -- Resolve_Op_Concat --
9993 -----------------------
9994
9995 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
9996
9997 -- We wish to avoid deep recursion, because concatenations are often
9998 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
9999 -- operands nonrecursively until we find something that is not a simple
10000 -- concatenation (A in this case). We resolve that, and then walk back
10001 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
10002 -- to do the rest of the work at each level. The Parent pointers allow
10003 -- us to avoid recursion, and thus avoid running out of memory. See also
10004 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
10005
10006 NN : Node_Id := N;
10007 Op1 : Node_Id;
10008
10009 begin
10010 -- The following code is equivalent to:
10011
10012 -- Resolve_Op_Concat_First (NN, Typ);
10013 -- Resolve_Op_Concat_Arg (N, ...);
10014 -- Resolve_Op_Concat_Rest (N, Typ);
10015
10016 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
10017 -- operand is a concatenation.
10018
10019 -- Walk down left operands
10020
10021 loop
10022 Resolve_Op_Concat_First (NN, Typ);
10023 Op1 := Left_Opnd (NN);
10024 exit when not (Nkind (Op1) = N_Op_Concat
10025 and then not Is_Array_Type (Component_Type (Typ))
10026 and then Entity (Op1) = Entity (NN));
10027 NN := Op1;
10028 end loop;
10029
10030 -- Now (given the above example) NN is A&B and Op1 is A
10031
10032 -- First resolve Op1 ...
10033
10034 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
10035
10036 -- ... then walk NN back up until we reach N (where we started), calling
10037 -- Resolve_Op_Concat_Rest along the way.
10038
10039 loop
10040 Resolve_Op_Concat_Rest (NN, Typ);
10041 exit when NN = N;
10042 NN := Parent (NN);
10043 end loop;
10044 end Resolve_Op_Concat;
10045
10046 ---------------------------
10047 -- Resolve_Op_Concat_Arg --
10048 ---------------------------
10049
10050 procedure Resolve_Op_Concat_Arg
10051 (N : Node_Id;
10052 Arg : Node_Id;
10053 Typ : Entity_Id;
10054 Is_Comp : Boolean)
10055 is
10056 Btyp : constant Entity_Id := Base_Type (Typ);
10057 Ctyp : constant Entity_Id := Component_Type (Typ);
10058
10059 begin
10060 if In_Instance then
10061 if Is_Comp
10062 or else (not Is_Overloaded (Arg)
10063 and then Etype (Arg) /= Any_Composite
10064 and then Covers (Ctyp, Etype (Arg)))
10065 then
10066 Resolve (Arg, Ctyp);
10067 else
10068 Resolve (Arg, Btyp);
10069 end if;
10070
10071 -- If both Array & Array and Array & Component are visible, there is a
10072 -- potential ambiguity that must be reported.
10073
10074 elsif Has_Compatible_Type (Arg, Ctyp) then
10075 if Nkind (Arg) = N_Aggregate
10076 and then Is_Composite_Type (Ctyp)
10077 then
10078 if Is_Private_Type (Ctyp) then
10079 Resolve (Arg, Btyp);
10080
10081 -- If the operation is user-defined and not overloaded use its
10082 -- profile. The operation may be a renaming, in which case it has
10083 -- been rewritten, and we want the original profile.
10084
10085 elsif not Is_Overloaded (N)
10086 and then Comes_From_Source (Entity (Original_Node (N)))
10087 and then Ekind (Entity (Original_Node (N))) = E_Function
10088 then
10089 Resolve (Arg,
10090 Etype
10091 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
10092 return;
10093
10094 -- Otherwise an aggregate may match both the array type and the
10095 -- component type.
10096
10097 else
10098 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
10099 Set_Etype (Arg, Any_Type);
10100 end if;
10101
10102 else
10103 if Is_Overloaded (Arg)
10104 and then Has_Compatible_Type (Arg, Typ)
10105 and then Etype (Arg) /= Any_Type
10106 then
10107 declare
10108 I : Interp_Index;
10109 It : Interp;
10110 Func : Entity_Id;
10111
10112 begin
10113 Get_First_Interp (Arg, I, It);
10114 Func := It.Nam;
10115 Get_Next_Interp (I, It);
10116
10117 -- Special-case the error message when the overloading is
10118 -- caused by a function that yields an array and can be
10119 -- called without parameters.
10120
10121 if It.Nam = Func then
10122 Error_Msg_Sloc := Sloc (Func);
10123 Error_Msg_N ("ambiguous call to function#", Arg);
10124 Error_Msg_NE
10125 ("\\interpretation as call yields&", Arg, Typ);
10126 Error_Msg_NE
10127 ("\\interpretation as indexing of call yields&",
10128 Arg, Component_Type (Typ));
10129
10130 else
10131 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
10132
10133 Get_First_Interp (Arg, I, It);
10134 while Present (It.Nam) loop
10135 Error_Msg_Sloc := Sloc (It.Nam);
10136
10137 if Base_Type (It.Typ) = Btyp
10138 or else
10139 Base_Type (It.Typ) = Base_Type (Ctyp)
10140 then
10141 Error_Msg_N -- CODEFIX
10142 ("\\possible interpretation#", Arg);
10143 end if;
10144
10145 Get_Next_Interp (I, It);
10146 end loop;
10147 end if;
10148 end;
10149 end if;
10150
10151 Resolve (Arg, Component_Type (Typ));
10152
10153 if Nkind (Arg) = N_String_Literal then
10154 Set_Etype (Arg, Component_Type (Typ));
10155 end if;
10156
10157 if Arg = Left_Opnd (N) then
10158 Set_Is_Component_Left_Opnd (N);
10159 else
10160 Set_Is_Component_Right_Opnd (N);
10161 end if;
10162 end if;
10163
10164 else
10165 Resolve (Arg, Btyp);
10166 end if;
10167
10168 Check_Unset_Reference (Arg);
10169 end Resolve_Op_Concat_Arg;
10170
10171 -----------------------------
10172 -- Resolve_Op_Concat_First --
10173 -----------------------------
10174
10175 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
10176 Btyp : constant Entity_Id := Base_Type (Typ);
10177 Op1 : constant Node_Id := Left_Opnd (N);
10178 Op2 : constant Node_Id := Right_Opnd (N);
10179
10180 begin
10181 -- The parser folds an enormous sequence of concatenations of string
10182 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
10183 -- in the right operand. If the expression resolves to a predefined "&"
10184 -- operator, all is well. Otherwise, the parser's folding is wrong, so
10185 -- we give an error. See P_Simple_Expression in Par.Ch4.
10186
10187 if Nkind (Op2) = N_String_Literal
10188 and then Is_Folded_In_Parser (Op2)
10189 and then Ekind (Entity (N)) = E_Function
10190 then
10191 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
10192 and then String_Length (Strval (Op1)) = 0);
10193 Error_Msg_N ("too many user-defined concatenations", N);
10194 return;
10195 end if;
10196
10197 Set_Etype (N, Btyp);
10198
10199 if Is_Limited_Composite (Btyp) then
10200 Error_Msg_N ("concatenation not available for limited array", N);
10201 Explain_Limited_Type (Btyp, N);
10202 end if;
10203 end Resolve_Op_Concat_First;
10204
10205 ----------------------------
10206 -- Resolve_Op_Concat_Rest --
10207 ----------------------------
10208
10209 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
10210 Op1 : constant Node_Id := Left_Opnd (N);
10211 Op2 : constant Node_Id := Right_Opnd (N);
10212
10213 begin
10214 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
10215
10216 Generate_Operator_Reference (N, Typ);
10217
10218 if Is_String_Type (Typ) then
10219 Eval_Concatenation (N);
10220 end if;
10221
10222 -- If this is not a static concatenation, but the result is a string
10223 -- type (and not an array of strings) ensure that static string operands
10224 -- have their subtypes properly constructed.
10225
10226 if Nkind (N) /= N_String_Literal
10227 and then Is_Character_Type (Component_Type (Typ))
10228 then
10229 Set_String_Literal_Subtype (Op1, Typ);
10230 Set_String_Literal_Subtype (Op2, Typ);
10231 end if;
10232 end Resolve_Op_Concat_Rest;
10233
10234 ----------------------
10235 -- Resolve_Op_Expon --
10236 ----------------------
10237
10238 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
10239 B_Typ : constant Entity_Id := Base_Type (Typ);
10240
10241 begin
10242 -- Catch attempts to do fixed-point exponentiation with universal
10243 -- operands, which is a case where the illegality is not caught during
10244 -- normal operator analysis. This is not done in preanalysis mode
10245 -- since the tree is not fully decorated during preanalysis.
10246
10247 if Full_Analysis then
10248 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
10249 Error_Msg_N ("exponentiation not available for fixed point", N);
10250 return;
10251
10252 elsif Nkind (Parent (N)) in N_Op
10253 and then Present (Etype (Parent (N)))
10254 and then Is_Fixed_Point_Type (Etype (Parent (N)))
10255 and then Etype (N) = Universal_Real
10256 and then Comes_From_Source (N)
10257 then
10258 Error_Msg_N ("exponentiation not available for fixed point", N);
10259 return;
10260 end if;
10261 end if;
10262
10263 if Comes_From_Source (N)
10264 and then Ekind (Entity (N)) = E_Function
10265 and then Is_Imported (Entity (N))
10266 and then Is_Intrinsic_Subprogram (Entity (N))
10267 then
10268 Resolve_Intrinsic_Operator (N, Typ);
10269 return;
10270 end if;
10271
10272 if Is_Universal_Numeric_Type (Etype (Left_Opnd (N))) then
10273 Check_For_Visible_Operator (N, B_Typ);
10274 end if;
10275
10276 -- We do the resolution using the base type, because intermediate values
10277 -- in expressions are always of the base type, not a subtype of it.
10278
10279 Resolve (Left_Opnd (N), B_Typ);
10280 Resolve (Right_Opnd (N), Standard_Integer);
10281
10282 -- For integer types, right argument must be in Natural range
10283
10284 if Is_Integer_Type (Typ) then
10285 Apply_Scalar_Range_Check (Right_Opnd (N), Standard_Natural);
10286 end if;
10287
10288 Check_Unset_Reference (Left_Opnd (N));
10289 Check_Unset_Reference (Right_Opnd (N));
10290
10291 Set_Etype (N, B_Typ);
10292 Generate_Operator_Reference (N, B_Typ);
10293
10294 Analyze_Dimension (N);
10295
10296 if Ada_Version >= Ada_2012 and then Has_Dimension_System (B_Typ) then
10297 -- Evaluate the exponentiation operator for dimensioned type
10298
10299 Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ);
10300 else
10301 Eval_Op_Expon (N);
10302 end if;
10303
10304 -- Set overflow checking bit. Much cleverer code needed here eventually
10305 -- and perhaps the Resolve routines should be separated for the various
10306 -- arithmetic operations, since they will need different processing. ???
10307
10308 if Nkind (N) in N_Op then
10309 if not Overflow_Checks_Suppressed (Etype (N)) then
10310 Enable_Overflow_Check (N);
10311 end if;
10312 end if;
10313 end Resolve_Op_Expon;
10314
10315 --------------------
10316 -- Resolve_Op_Not --
10317 --------------------
10318
10319 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
10320 function Parent_Is_Boolean return Boolean;
10321 -- This function determines if the parent node is a boolean operator or
10322 -- operation (comparison op, membership test, or short circuit form) and
10323 -- the not in question is the left operand of this operation. Note that
10324 -- if the not is in parens, then false is returned.
10325
10326 -----------------------
10327 -- Parent_Is_Boolean --
10328 -----------------------
10329
10330 function Parent_Is_Boolean return Boolean is
10331 begin
10332 return Paren_Count (N) = 0
10333 and then Nkind (Parent (N)) in N_Membership_Test
10334 | N_Op_Boolean
10335 | N_Short_Circuit
10336 and then Left_Opnd (Parent (N)) = N;
10337 end Parent_Is_Boolean;
10338
10339 -- Local variables
10340
10341 B_Typ : Entity_Id;
10342
10343 -- Start of processing for Resolve_Op_Not
10344
10345 begin
10346 -- Predefined operations on scalar types yield the base type. On the
10347 -- other hand, logical operations on arrays yield the type of the
10348 -- arguments (and the context).
10349
10350 if Is_Array_Type (Typ) then
10351 B_Typ := Typ;
10352 else
10353 B_Typ := Base_Type (Typ);
10354 end if;
10355
10356 -- Straightforward case of incorrect arguments
10357
10358 if not Valid_Boolean_Arg (Typ) then
10359 Error_Msg_N ("invalid operand type for operator&", N);
10360 Set_Etype (N, Any_Type);
10361 return;
10362
10363 -- Special case of probable missing parens
10364
10365 elsif Typ = Universal_Integer or else Typ = Any_Modular then
10366 if Parent_Is_Boolean then
10367 Error_Msg_N
10368 ("operand of NOT must be enclosed in parentheses",
10369 Right_Opnd (N));
10370 else
10371 Error_Msg_N
10372 ("no modular type available in this context", N);
10373 end if;
10374
10375 Set_Etype (N, Any_Type);
10376 return;
10377
10378 -- OK resolution of NOT
10379
10380 else
10381 -- Warn if non-boolean types involved. This is a case like not a < b
10382 -- where a and b are modular, where we will get (not a) < b and most
10383 -- likely not (a < b) was intended.
10384
10385 if Warn_On_Questionable_Missing_Parens
10386 and then not Is_Boolean_Type (Typ)
10387 and then Parent_Is_Boolean
10388 then
10389 Error_Msg_N ("?q?not expression should be parenthesized here!", N);
10390 end if;
10391
10392 -- Warn on double negation if checking redundant constructs
10393
10394 if Warn_On_Redundant_Constructs
10395 and then Comes_From_Source (N)
10396 and then Comes_From_Source (Right_Opnd (N))
10397 and then Root_Type (Typ) = Standard_Boolean
10398 and then Nkind (Right_Opnd (N)) = N_Op_Not
10399 then
10400 Error_Msg_N ("redundant double negation?r?", N);
10401 end if;
10402
10403 -- Complete resolution and evaluation of NOT
10404 -- If argument is an equality and expected type is boolean, that
10405 -- expected type has no effect on resolution, and there are
10406 -- special rules for resolution of Eq, Neq in the presence of
10407 -- overloaded operands, so we directly call its resolution routines.
10408
10409 declare
10410 Opnd : constant Node_Id := Right_Opnd (N);
10411 Op_Id : Entity_Id;
10412
10413 begin
10414 if B_Typ = Standard_Boolean
10415 and then Nkind (Opnd) in N_Op_Eq | N_Op_Ne
10416 and then Is_Overloaded (Opnd)
10417 then
10418 Resolve_Equality_Op (Opnd, B_Typ);
10419 Op_Id := Entity (Opnd);
10420
10421 if Ekind (Op_Id) = E_Function
10422 and then not Is_Intrinsic_Subprogram (Op_Id)
10423 then
10424 Rewrite_Operator_As_Call (Opnd, Op_Id);
10425 end if;
10426
10427 if not Inside_A_Generic or else Is_Entity_Name (Opnd) then
10428 Freeze_Expression (Opnd);
10429 end if;
10430
10431 Expand (Opnd);
10432
10433 else
10434 Resolve (Opnd, B_Typ);
10435 end if;
10436
10437 Check_Unset_Reference (Opnd);
10438 end;
10439
10440 Set_Etype (N, B_Typ);
10441 Generate_Operator_Reference (N, B_Typ);
10442 Eval_Op_Not (N);
10443 end if;
10444 end Resolve_Op_Not;
10445
10446 -----------------------------
10447 -- Resolve_Operator_Symbol --
10448 -----------------------------
10449
10450 -- Nothing to be done, all resolved already
10451
10452 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
10453 pragma Warnings (Off, N);
10454 pragma Warnings (Off, Typ);
10455
10456 begin
10457 null;
10458 end Resolve_Operator_Symbol;
10459
10460 ----------------------------------
10461 -- Resolve_Qualified_Expression --
10462 ----------------------------------
10463
10464 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
10465 pragma Warnings (Off, Typ);
10466
10467 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
10468 Expr : constant Node_Id := Expression (N);
10469
10470 begin
10471 Resolve (Expr, Target_Typ);
10472
10473 -- A qualified expression requires an exact match of the type, class-
10474 -- wide matching is not allowed. However, if the qualifying type is
10475 -- specific and the expression has a class-wide type, it may still be
10476 -- okay, since it can be the result of the expansion of a call to a
10477 -- dispatching function, so we also have to check class-wideness of the
10478 -- type of the expression's original node.
10479
10480 if (Is_Class_Wide_Type (Target_Typ)
10481 or else
10482 (Is_Class_Wide_Type (Etype (Expr))
10483 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
10484 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
10485 then
10486 Wrong_Type (Expr, Target_Typ);
10487 end if;
10488
10489 -- If the target type is unconstrained, then we reset the type of the
10490 -- result from the type of the expression. For other cases, the actual
10491 -- subtype of the expression is the target type. But we avoid doing it
10492 -- for an allocator since this is not needed and might be problematic.
10493
10494 if Is_Composite_Type (Target_Typ)
10495 and then not Is_Constrained (Target_Typ)
10496 and then Nkind (Parent (N)) /= N_Allocator
10497 then
10498 Set_Etype (N, Etype (Expr));
10499 end if;
10500
10501 Analyze_Dimension (N);
10502 Eval_Qualified_Expression (N);
10503
10504 -- If we still have a qualified expression after the static evaluation,
10505 -- then apply a scalar range check if needed. The reason that we do this
10506 -- after the Eval call is that otherwise, the application of the range
10507 -- check may convert an illegal static expression and result in warning
10508 -- rather than giving an error (e.g Integer'(Integer'Last + 1)).
10509
10510 if Nkind (N) = N_Qualified_Expression
10511 and then Is_Scalar_Type (Target_Typ)
10512 then
10513 Apply_Scalar_Range_Check (Expr, Target_Typ);
10514 end if;
10515
10516 -- AI12-0100: Once the qualified expression is resolved, check whether
10517 -- operand statisfies a static predicate of the target subtype, if any.
10518 -- In the static expression case, a predicate check failure is an error.
10519
10520 if Has_Predicates (Target_Typ) then
10521 Check_Expression_Against_Static_Predicate
10522 (Expr, Target_Typ, Static_Failure_Is_Error => True);
10523 end if;
10524 end Resolve_Qualified_Expression;
10525
10526 ------------------------------
10527 -- Resolve_Raise_Expression --
10528 ------------------------------
10529
10530 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id) is
10531 begin
10532 if Typ = Raise_Type then
10533 Error_Msg_N ("cannot find unique type for raise expression", N);
10534 Set_Etype (N, Any_Type);
10535 else
10536 Set_Etype (N, Typ);
10537 end if;
10538 end Resolve_Raise_Expression;
10539
10540 -------------------
10541 -- Resolve_Range --
10542 -------------------
10543
10544 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
10545 L : constant Node_Id := Low_Bound (N);
10546 H : constant Node_Id := High_Bound (N);
10547
10548 function First_Last_Ref return Boolean;
10549 -- Returns True if N is of the form X'First .. X'Last where X is the
10550 -- same entity for both attributes.
10551
10552 --------------------
10553 -- First_Last_Ref --
10554 --------------------
10555
10556 function First_Last_Ref return Boolean is
10557 Lorig : constant Node_Id := Original_Node (L);
10558 Horig : constant Node_Id := Original_Node (H);
10559
10560 begin
10561 if Nkind (Lorig) = N_Attribute_Reference
10562 and then Nkind (Horig) = N_Attribute_Reference
10563 and then Attribute_Name (Lorig) = Name_First
10564 and then Attribute_Name (Horig) = Name_Last
10565 then
10566 declare
10567 PL : constant Node_Id := Prefix (Lorig);
10568 PH : constant Node_Id := Prefix (Horig);
10569 begin
10570 return Is_Entity_Name (PL)
10571 and then Is_Entity_Name (PH)
10572 and then Entity (PL) = Entity (PH);
10573 end;
10574 end if;
10575
10576 return False;
10577 end First_Last_Ref;
10578
10579 -- Start of processing for Resolve_Range
10580
10581 begin
10582 Set_Etype (N, Typ);
10583
10584 Resolve (L, Typ);
10585 Resolve (H, Typ);
10586
10587 -- Reanalyze the lower bound after both bounds have been analyzed, so
10588 -- that the range is known to be static or not by now. This may trigger
10589 -- more compile-time evaluation, which is useful for static analysis
10590 -- with GNATprove. This is not needed for compilation or static analysis
10591 -- with CodePeer, as full expansion does that evaluation then.
10592
10593 if GNATprove_Mode then
10594 Set_Analyzed (L, False);
10595 Resolve (L, Typ);
10596 end if;
10597
10598 -- Check for inappropriate range on unordered enumeration type
10599
10600 if Bad_Unordered_Enumeration_Reference (N, Typ)
10601
10602 -- Exclude X'First .. X'Last if X is the same entity for both
10603
10604 and then not First_Last_Ref
10605 then
10606 Error_Msg_Sloc := Sloc (Typ);
10607 Error_Msg_NE
10608 ("subrange of unordered enumeration type& declared#?U?", N, Typ);
10609 end if;
10610
10611 Check_Unset_Reference (L);
10612 Check_Unset_Reference (H);
10613
10614 -- We have to check the bounds for being within the base range as
10615 -- required for a non-static context. Normally this is automatic and
10616 -- done as part of evaluating expressions, but the N_Range node is an
10617 -- exception, since in GNAT we consider this node to be a subexpression,
10618 -- even though in Ada it is not. The circuit in Sem_Eval could check for
10619 -- this, but that would put the test on the main evaluation path for
10620 -- expressions.
10621
10622 Check_Non_Static_Context (L);
10623 Check_Non_Static_Context (H);
10624
10625 -- Check for an ambiguous range over character literals. This will
10626 -- happen with a membership test involving only literals.
10627
10628 if Typ = Any_Character then
10629 Ambiguous_Character (L);
10630 Set_Etype (N, Any_Type);
10631 return;
10632 end if;
10633
10634 -- If bounds are static, constant-fold them, so size computations are
10635 -- identical between front-end and back-end. Do not perform this
10636 -- transformation while analyzing generic units, as type information
10637 -- would be lost when reanalyzing the constant node in the instance.
10638
10639 if Is_Discrete_Type (Typ) and then Expander_Active then
10640 if Is_OK_Static_Expression (L) then
10641 Fold_Uint (L, Expr_Value (L), Static => True);
10642 end if;
10643
10644 if Is_OK_Static_Expression (H) then
10645 Fold_Uint (H, Expr_Value (H), Static => True);
10646 end if;
10647 end if;
10648 end Resolve_Range;
10649
10650 --------------------------
10651 -- Resolve_Real_Literal --
10652 --------------------------
10653
10654 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
10655 Actual_Typ : constant Entity_Id := Etype (N);
10656
10657 begin
10658 -- Special processing for fixed-point literals to make sure that the
10659 -- value is an exact multiple of small where this is required. We skip
10660 -- this for the universal real case, and also for generic types.
10661
10662 if Is_Fixed_Point_Type (Typ)
10663 and then Typ /= Universal_Fixed
10664 and then Typ /= Any_Fixed
10665 and then not Is_Generic_Type (Typ)
10666 then
10667 declare
10668 Val : constant Ureal := Realval (N);
10669 Cintr : constant Ureal := Val / Small_Value (Typ);
10670 Cint : constant Uint := UR_Trunc (Cintr);
10671 Den : constant Uint := Norm_Den (Cintr);
10672 Stat : Boolean;
10673
10674 begin
10675 -- Case of literal is not an exact multiple of the Small
10676
10677 if Den /= 1 then
10678
10679 -- For a source program literal for a decimal fixed-point type,
10680 -- this is statically illegal (RM 4.9(36)).
10681
10682 if Is_Decimal_Fixed_Point_Type (Typ)
10683 and then Actual_Typ = Universal_Real
10684 and then Comes_From_Source (N)
10685 then
10686 Error_Msg_N ("value has extraneous low order digits", N);
10687 end if;
10688
10689 -- Generate a warning if literal from source
10690
10691 if Is_OK_Static_Expression (N)
10692 and then Warn_On_Bad_Fixed_Value
10693 then
10694 Error_Msg_N
10695 ("?b?static fixed-point value is not a multiple of Small!",
10696 N);
10697 end if;
10698
10699 -- Replace literal by a value that is the exact representation
10700 -- of a value of the type, i.e. a multiple of the small value,
10701 -- by truncation, since Machine_Rounds is false for all GNAT
10702 -- fixed-point types (RM 4.9(38)).
10703
10704 Stat := Is_OK_Static_Expression (N);
10705 Rewrite (N,
10706 Make_Real_Literal (Sloc (N),
10707 Realval => Small_Value (Typ) * Cint));
10708
10709 Set_Is_Static_Expression (N, Stat);
10710 end if;
10711
10712 -- In all cases, set the corresponding integer field
10713
10714 Set_Corresponding_Integer_Value (N, Cint);
10715 end;
10716 end if;
10717
10718 -- Now replace the actual type by the expected type as usual
10719
10720 Set_Etype (N, Typ);
10721 Eval_Real_Literal (N);
10722 end Resolve_Real_Literal;
10723
10724 -----------------------
10725 -- Resolve_Reference --
10726 -----------------------
10727
10728 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
10729 P : constant Node_Id := Prefix (N);
10730
10731 begin
10732 -- Replace general access with specific type
10733
10734 if Ekind (Etype (N)) = E_Allocator_Type then
10735 Set_Etype (N, Base_Type (Typ));
10736 end if;
10737
10738 Resolve (P, Designated_Type (Etype (N)));
10739
10740 -- If we are taking the reference of a volatile entity, then treat it as
10741 -- a potential modification of this entity. This is too conservative,
10742 -- but necessary because remove side effects can cause transformations
10743 -- of normal assignments into reference sequences that otherwise fail to
10744 -- notice the modification.
10745
10746 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
10747 Note_Possible_Modification (P, Sure => False);
10748 end if;
10749 end Resolve_Reference;
10750
10751 --------------------------------
10752 -- Resolve_Selected_Component --
10753 --------------------------------
10754
10755 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
10756 Comp : Entity_Id;
10757 Comp1 : Entity_Id := Empty; -- prevent junk warning
10758 P : constant Node_Id := Prefix (N);
10759 S : constant Node_Id := Selector_Name (N);
10760 T : Entity_Id := Etype (P);
10761 I : Interp_Index;
10762 I1 : Interp_Index := 0; -- prevent junk warning
10763 It : Interp;
10764 It1 : Interp;
10765 Found : Boolean;
10766
10767 function Init_Component return Boolean;
10768 -- Check whether this is the initialization of a component within an
10769 -- init proc (by assignment or call to another init proc). If true,
10770 -- there is no need for a discriminant check.
10771
10772 --------------------
10773 -- Init_Component --
10774 --------------------
10775
10776 function Init_Component return Boolean is
10777 begin
10778 return Inside_Init_Proc
10779 and then Nkind (Prefix (N)) = N_Identifier
10780 and then Chars (Prefix (N)) = Name_uInit
10781 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
10782 end Init_Component;
10783
10784 -- Start of processing for Resolve_Selected_Component
10785
10786 begin
10787 if Is_Overloaded (P) then
10788
10789 -- Use the context type to select the prefix that has a selector
10790 -- of the correct name and type.
10791
10792 Found := False;
10793 Get_First_Interp (P, I, It);
10794
10795 Search : while Present (It.Typ) loop
10796 if Is_Access_Type (It.Typ) then
10797 T := Designated_Type (It.Typ);
10798 else
10799 T := It.Typ;
10800 end if;
10801
10802 -- Locate selected component. For a private prefix the selector
10803 -- can denote a discriminant.
10804
10805 if Is_Record_Type (T) or else Is_Private_Type (T) then
10806
10807 -- The visible components of a class-wide type are those of
10808 -- the root type.
10809
10810 if Is_Class_Wide_Type (T) then
10811 T := Etype (T);
10812 end if;
10813
10814 Comp := First_Entity (T);
10815 while Present (Comp) loop
10816 if Chars (Comp) = Chars (S)
10817 and then Covers (Typ, Etype (Comp))
10818 then
10819 if not Found then
10820 Found := True;
10821 I1 := I;
10822 It1 := It;
10823 Comp1 := Comp;
10824
10825 else
10826 It := Disambiguate (P, I1, I, Any_Type);
10827
10828 if It = No_Interp then
10829 Error_Msg_N
10830 ("ambiguous prefix for selected component", N);
10831 Set_Etype (N, Typ);
10832 return;
10833
10834 else
10835 It1 := It;
10836
10837 -- There may be an implicit dereference. Retrieve
10838 -- designated record type.
10839
10840 if Is_Access_Type (It1.Typ) then
10841 T := Designated_Type (It1.Typ);
10842 else
10843 T := It1.Typ;
10844 end if;
10845
10846 if Scope (Comp1) /= T then
10847
10848 -- Resolution chooses the new interpretation.
10849 -- Find the component with the right name.
10850
10851 Comp1 := First_Entity (T);
10852 while Present (Comp1)
10853 and then Chars (Comp1) /= Chars (S)
10854 loop
10855 Next_Entity (Comp1);
10856 end loop;
10857 end if;
10858
10859 exit Search;
10860 end if;
10861 end if;
10862 end if;
10863
10864 Next_Entity (Comp);
10865 end loop;
10866 end if;
10867
10868 Get_Next_Interp (I, It);
10869 end loop Search;
10870
10871 -- There must be a legal interpretation at this point
10872
10873 pragma Assert (Found);
10874 Resolve (P, It1.Typ);
10875
10876 -- In general the expected type is the type of the context, not the
10877 -- type of the candidate selected component.
10878
10879 Set_Etype (N, Typ);
10880 Set_Entity_With_Checks (S, Comp1);
10881
10882 -- The type of the context and that of the component are
10883 -- compatible and in general identical, but if they are anonymous
10884 -- access-to-subprogram types, the relevant type is that of the
10885 -- component. This matters in Unnest_Subprograms mode, where the
10886 -- relevant context is the one in which the type is declared, not
10887 -- the point of use. This determines what activation record to use.
10888
10889 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
10890 Set_Etype (N, Etype (Comp1));
10891
10892 -- When the type of the component is an access to a class-wide type
10893 -- the relevant type is that of the component (since in such case we
10894 -- may need to generate implicit type conversions or dispatching
10895 -- calls).
10896
10897 elsif Is_Access_Type (Typ)
10898 and then not Is_Class_Wide_Type (Designated_Type (Typ))
10899 and then Is_Class_Wide_Type (Designated_Type (Etype (Comp1)))
10900 then
10901 Set_Etype (N, Etype (Comp1));
10902 end if;
10903
10904 else
10905 -- Resolve prefix with its type
10906
10907 Resolve (P, T);
10908 end if;
10909
10910 -- Generate cross-reference. We needed to wait until full overloading
10911 -- resolution was complete to do this, since otherwise we can't tell if
10912 -- we are an lvalue or not.
10913
10914 if May_Be_Lvalue (N) then
10915 Generate_Reference (Entity (S), S, 'm');
10916 else
10917 Generate_Reference (Entity (S), S, 'r');
10918 end if;
10919
10920 -- If the prefix's type is an access type, get to the real record type.
10921 -- Note: we do not apply an access check because an explicit dereference
10922 -- will be introduced later, and the check will happen there.
10923
10924 if Is_Access_Type (Etype (P)) then
10925 T := Implicitly_Designated_Type (Etype (P));
10926 Check_Fully_Declared_Prefix (T, P);
10927
10928 else
10929 T := Etype (P);
10930
10931 -- If the prefix is an entity it may have a deferred reference set
10932 -- during analysis of the selected component. After resolution we
10933 -- can transform it into a proper reference. This prevents spurious
10934 -- warnings on useless assignments when the same selected component
10935 -- is the actual for an out parameter in a subsequent call.
10936
10937 if Is_Entity_Name (P)
10938 and then Has_Deferred_Reference (Entity (P))
10939 then
10940 if May_Be_Lvalue (N) then
10941 Generate_Reference (Entity (P), P, 'm');
10942 else
10943 Generate_Reference (Entity (P), P, 'r');
10944 end if;
10945 end if;
10946 end if;
10947
10948 -- Set flag for expander if discriminant check required on a component
10949 -- appearing within a variant.
10950
10951 if Has_Discriminants (T)
10952 and then Ekind (Entity (S)) = E_Component
10953 and then Present (Original_Record_Component (Entity (S)))
10954 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
10955 and then
10956 Is_Declared_Within_Variant (Original_Record_Component (Entity (S)))
10957 and then not Discriminant_Checks_Suppressed (T)
10958 and then not Init_Component
10959 then
10960 Set_Do_Discriminant_Check (N);
10961 end if;
10962
10963 if Ekind (Entity (S)) = E_Void then
10964 Error_Msg_N ("premature use of component", S);
10965 end if;
10966
10967 -- If the prefix is a record conversion, this may be a renamed
10968 -- discriminant whose bounds differ from those of the original
10969 -- one, so we must ensure that a range check is performed.
10970
10971 if Nkind (P) = N_Type_Conversion
10972 and then Ekind (Entity (S)) = E_Discriminant
10973 and then Is_Discrete_Type (Typ)
10974 then
10975 Set_Etype (N, Base_Type (Typ));
10976 end if;
10977
10978 -- Eval_Selected_Component may e.g. fold statically known discriminants.
10979
10980 Eval_Selected_Component (N);
10981
10982 if Nkind (N) = N_Selected_Component then
10983
10984 -- If the record type is atomic and the component is not, then this
10985 -- is worth a warning before Ada 2020, since we have a situation
10986 -- where the access to the component may cause extra read/writes of
10987 -- the atomic object, or partial word accesses, both of which may be
10988 -- unexpected.
10989
10990 if Is_Atomic_Ref_With_Address (N)
10991 and then not Is_Atomic (Entity (S))
10992 and then not Is_Atomic (Etype (Entity (S)))
10993 and then Ada_Version < Ada_2020
10994 then
10995 Error_Msg_N
10996 ("??access to non-atomic component of atomic record",
10997 Prefix (N));
10998 Error_Msg_N
10999 ("\??may cause unexpected accesses to atomic object",
11000 Prefix (N));
11001 end if;
11002
11003 Resolve_Implicit_Dereference (Prefix (N));
11004 Analyze_Dimension (N);
11005 end if;
11006 end Resolve_Selected_Component;
11007
11008 -------------------
11009 -- Resolve_Shift --
11010 -------------------
11011
11012 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
11013 B_Typ : constant Entity_Id := Base_Type (Typ);
11014 L : constant Node_Id := Left_Opnd (N);
11015 R : constant Node_Id := Right_Opnd (N);
11016
11017 begin
11018 -- We do the resolution using the base type, because intermediate values
11019 -- in expressions always are of the base type, not a subtype of it.
11020
11021 Resolve (L, B_Typ);
11022 Resolve (R, Standard_Natural);
11023
11024 Check_Unset_Reference (L);
11025 Check_Unset_Reference (R);
11026
11027 Set_Etype (N, B_Typ);
11028 Generate_Operator_Reference (N, B_Typ);
11029 Eval_Shift (N);
11030 end Resolve_Shift;
11031
11032 ---------------------------
11033 -- Resolve_Short_Circuit --
11034 ---------------------------
11035
11036 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
11037 B_Typ : constant Entity_Id := Base_Type (Typ);
11038 L : constant Node_Id := Left_Opnd (N);
11039 R : constant Node_Id := Right_Opnd (N);
11040
11041 begin
11042 -- Ensure all actions associated with the left operand (e.g.
11043 -- finalization of transient objects) are fully evaluated locally within
11044 -- an expression with actions. This is particularly helpful for coverage
11045 -- analysis. However this should not happen in generics or if option
11046 -- Minimize_Expression_With_Actions is set.
11047
11048 if Expander_Active and not Minimize_Expression_With_Actions then
11049 declare
11050 Reloc_L : constant Node_Id := Relocate_Node (L);
11051 begin
11052 Save_Interps (Old_N => L, New_N => Reloc_L);
11053
11054 Rewrite (L,
11055 Make_Expression_With_Actions (Sloc (L),
11056 Actions => New_List,
11057 Expression => Reloc_L));
11058
11059 -- Set Comes_From_Source on L to preserve warnings for unset
11060 -- reference.
11061
11062 Preserve_Comes_From_Source (L, Reloc_L);
11063 end;
11064 end if;
11065
11066 Resolve (L, B_Typ);
11067 Resolve (R, B_Typ);
11068
11069 -- Check for issuing warning for always False assert/check, this happens
11070 -- when assertions are turned off, in which case the pragma Assert/Check
11071 -- was transformed into:
11072
11073 -- if False and then <condition> then ...
11074
11075 -- and we detect this pattern
11076
11077 if Warn_On_Assertion_Failure
11078 and then Is_Entity_Name (R)
11079 and then Entity (R) = Standard_False
11080 and then Nkind (Parent (N)) = N_If_Statement
11081 and then Nkind (N) = N_And_Then
11082 and then Is_Entity_Name (L)
11083 and then Entity (L) = Standard_False
11084 then
11085 declare
11086 Orig : constant Node_Id := Original_Node (Parent (N));
11087
11088 begin
11089 -- Special handling of Asssert pragma
11090
11091 if Nkind (Orig) = N_Pragma
11092 and then Pragma_Name (Orig) = Name_Assert
11093 then
11094 declare
11095 Expr : constant Node_Id :=
11096 Original_Node
11097 (Expression
11098 (First (Pragma_Argument_Associations (Orig))));
11099
11100 begin
11101 -- Don't warn if original condition is explicit False,
11102 -- since obviously the failure is expected in this case.
11103
11104 if Is_Entity_Name (Expr)
11105 and then Entity (Expr) = Standard_False
11106 then
11107 null;
11108
11109 -- Issue warning. We do not want the deletion of the
11110 -- IF/AND-THEN to take this message with it. We achieve this
11111 -- by making sure that the expanded code points to the Sloc
11112 -- of the expression, not the original pragma.
11113
11114 else
11115 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
11116 -- The source location of the expression is not usually
11117 -- the best choice here. For example, it gets located on
11118 -- the last AND keyword in a chain of boolean expressiond
11119 -- AND'ed together. It is best to put the message on the
11120 -- first character of the assertion, which is the effect
11121 -- of the First_Node call here.
11122
11123 Error_Msg_F
11124 ("?A?assertion would fail at run time!",
11125 Expression
11126 (First (Pragma_Argument_Associations (Orig))));
11127 end if;
11128 end;
11129
11130 -- Similar processing for Check pragma
11131
11132 elsif Nkind (Orig) = N_Pragma
11133 and then Pragma_Name (Orig) = Name_Check
11134 then
11135 -- Don't want to warn if original condition is explicit False
11136
11137 declare
11138 Expr : constant Node_Id :=
11139 Original_Node
11140 (Expression
11141 (Next (First (Pragma_Argument_Associations (Orig)))));
11142 begin
11143 if Is_Entity_Name (Expr)
11144 and then Entity (Expr) = Standard_False
11145 then
11146 null;
11147
11148 -- Post warning
11149
11150 else
11151 -- Again use Error_Msg_F rather than Error_Msg_N, see
11152 -- comment above for an explanation of why we do this.
11153
11154 Error_Msg_F
11155 ("?A?check would fail at run time!",
11156 Expression
11157 (Last (Pragma_Argument_Associations (Orig))));
11158 end if;
11159 end;
11160 end if;
11161 end;
11162 end if;
11163
11164 -- Continue with processing of short circuit
11165
11166 Check_Unset_Reference (L);
11167 Check_Unset_Reference (R);
11168
11169 Set_Etype (N, B_Typ);
11170 Eval_Short_Circuit (N);
11171 end Resolve_Short_Circuit;
11172
11173 -------------------
11174 -- Resolve_Slice --
11175 -------------------
11176
11177 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
11178 Drange : constant Node_Id := Discrete_Range (N);
11179 Name : constant Node_Id := Prefix (N);
11180 Array_Type : Entity_Id := Empty;
11181 Dexpr : Node_Id := Empty;
11182 Index_Type : Entity_Id;
11183
11184 begin
11185 if Is_Overloaded (Name) then
11186
11187 -- Use the context type to select the prefix that yields the correct
11188 -- array type.
11189
11190 declare
11191 I : Interp_Index;
11192 I1 : Interp_Index := 0;
11193 It : Interp;
11194 P : constant Node_Id := Prefix (N);
11195 Found : Boolean := False;
11196
11197 begin
11198 Get_First_Interp (P, I, It);
11199 while Present (It.Typ) loop
11200 if (Is_Array_Type (It.Typ)
11201 and then Covers (Typ, It.Typ))
11202 or else (Is_Access_Type (It.Typ)
11203 and then Is_Array_Type (Designated_Type (It.Typ))
11204 and then Covers (Typ, Designated_Type (It.Typ)))
11205 then
11206 if Found then
11207 It := Disambiguate (P, I1, I, Any_Type);
11208
11209 if It = No_Interp then
11210 Error_Msg_N ("ambiguous prefix for slicing", N);
11211 Set_Etype (N, Typ);
11212 return;
11213 else
11214 Found := True;
11215 Array_Type := It.Typ;
11216 I1 := I;
11217 end if;
11218 else
11219 Found := True;
11220 Array_Type := It.Typ;
11221 I1 := I;
11222 end if;
11223 end if;
11224
11225 Get_Next_Interp (I, It);
11226 end loop;
11227 end;
11228
11229 else
11230 Array_Type := Etype (Name);
11231 end if;
11232
11233 Resolve (Name, Array_Type);
11234
11235 -- If the prefix's type is an access type, get to the real array type.
11236 -- Note: we do not apply an access check because an explicit dereference
11237 -- will be introduced later, and the check will happen there.
11238
11239 if Is_Access_Type (Array_Type) then
11240 Array_Type := Implicitly_Designated_Type (Array_Type);
11241
11242 -- If the prefix is an access to an unconstrained array, we must use
11243 -- the actual subtype of the object to perform the index checks. The
11244 -- object denoted by the prefix is implicit in the node, so we build
11245 -- an explicit representation for it in order to compute the actual
11246 -- subtype.
11247
11248 if not Is_Constrained (Array_Type) then
11249 Remove_Side_Effects (Prefix (N));
11250
11251 declare
11252 Obj : constant Node_Id :=
11253 Make_Explicit_Dereference (Sloc (N),
11254 Prefix => New_Copy_Tree (Prefix (N)));
11255 begin
11256 Set_Etype (Obj, Array_Type);
11257 Set_Parent (Obj, Parent (N));
11258 Array_Type := Get_Actual_Subtype (Obj);
11259 end;
11260 end if;
11261
11262 elsif Is_Entity_Name (Name)
11263 or else Nkind (Name) = N_Explicit_Dereference
11264 or else (Nkind (Name) = N_Function_Call
11265 and then not Is_Constrained (Etype (Name)))
11266 then
11267 Array_Type := Get_Actual_Subtype (Name);
11268
11269 -- If the name is a selected component that depends on discriminants,
11270 -- build an actual subtype for it. This can happen only when the name
11271 -- itself is overloaded; otherwise the actual subtype is created when
11272 -- the selected component is analyzed.
11273
11274 elsif Nkind (Name) = N_Selected_Component
11275 and then Full_Analysis
11276 and then Depends_On_Discriminant (First_Index (Array_Type))
11277 then
11278 declare
11279 Act_Decl : constant Node_Id :=
11280 Build_Actual_Subtype_Of_Component (Array_Type, Name);
11281 begin
11282 Insert_Action (N, Act_Decl);
11283 Array_Type := Defining_Identifier (Act_Decl);
11284 end;
11285
11286 -- Maybe this should just be "else", instead of checking for the
11287 -- specific case of slice??? This is needed for the case where the
11288 -- prefix is an Image attribute, which gets expanded to a slice, and so
11289 -- has a constrained subtype which we want to use for the slice range
11290 -- check applied below (the range check won't get done if the
11291 -- unconstrained subtype of the 'Image is used).
11292
11293 elsif Nkind (Name) = N_Slice then
11294 Array_Type := Etype (Name);
11295 end if;
11296
11297 -- Obtain the type of the array index
11298
11299 if Ekind (Array_Type) = E_String_Literal_Subtype then
11300 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
11301 else
11302 Index_Type := Etype (First_Index (Array_Type));
11303 end if;
11304
11305 -- If name was overloaded, set slice type correctly now
11306
11307 Set_Etype (N, Array_Type);
11308
11309 -- Handle the generation of a range check that compares the array index
11310 -- against the discrete_range. The check is not applied to internally
11311 -- built nodes associated with the expansion of dispatch tables. Check
11312 -- that Ada.Tags has already been loaded to avoid extra dependencies on
11313 -- the unit.
11314
11315 if Tagged_Type_Expansion
11316 and then RTU_Loaded (Ada_Tags)
11317 and then Nkind (Prefix (N)) = N_Selected_Component
11318 and then Present (Entity (Selector_Name (Prefix (N))))
11319 and then Entity (Selector_Name (Prefix (N))) =
11320 RTE_Record_Component (RE_Prims_Ptr)
11321 then
11322 null;
11323
11324 -- The discrete_range is specified by a subtype indication. Create a
11325 -- shallow copy and inherit the type, parent and source location from
11326 -- the discrete_range. This ensures that the range check is inserted
11327 -- relative to the slice and that the runtime exception points to the
11328 -- proper construct.
11329
11330 elsif Is_Entity_Name (Drange) then
11331 Dexpr := New_Copy (Scalar_Range (Entity (Drange)));
11332
11333 Set_Etype (Dexpr, Etype (Drange));
11334 Set_Parent (Dexpr, Parent (Drange));
11335 Set_Sloc (Dexpr, Sloc (Drange));
11336
11337 -- The discrete_range is a regular range. Resolve the bounds and remove
11338 -- their side effects.
11339
11340 else
11341 Resolve (Drange, Base_Type (Index_Type));
11342
11343 if Nkind (Drange) = N_Range then
11344 Force_Evaluation (Low_Bound (Drange));
11345 Force_Evaluation (High_Bound (Drange));
11346
11347 Dexpr := Drange;
11348 end if;
11349 end if;
11350
11351 if Present (Dexpr) then
11352 Apply_Range_Check (Dexpr, Index_Type);
11353 end if;
11354
11355 Set_Slice_Subtype (N);
11356
11357 -- Check bad use of type with predicates
11358
11359 declare
11360 Subt : Entity_Id;
11361
11362 begin
11363 if Nkind (Drange) = N_Subtype_Indication
11364 and then Has_Predicates (Entity (Subtype_Mark (Drange)))
11365 then
11366 Subt := Entity (Subtype_Mark (Drange));
11367 else
11368 Subt := Etype (Drange);
11369 end if;
11370
11371 if Has_Predicates (Subt) then
11372 Bad_Predicated_Subtype_Use
11373 ("subtype& has predicate, not allowed in slice", Drange, Subt);
11374 end if;
11375 end;
11376
11377 -- Otherwise here is where we check suspicious indexes
11378
11379 if Nkind (Drange) = N_Range then
11380 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
11381 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
11382 end if;
11383
11384 Resolve_Implicit_Dereference (Prefix (N));
11385 Analyze_Dimension (N);
11386 Eval_Slice (N);
11387 end Resolve_Slice;
11388
11389 ----------------------------
11390 -- Resolve_String_Literal --
11391 ----------------------------
11392
11393 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
11394 C_Typ : constant Entity_Id := Component_Type (Typ);
11395 R_Typ : constant Entity_Id := Root_Type (C_Typ);
11396 Loc : constant Source_Ptr := Sloc (N);
11397 Str : constant String_Id := Strval (N);
11398 Strlen : constant Nat := String_Length (Str);
11399 Subtype_Id : Entity_Id;
11400 Need_Check : Boolean;
11401
11402 begin
11403 -- For a string appearing in a concatenation, defer creation of the
11404 -- string_literal_subtype until the end of the resolution of the
11405 -- concatenation, because the literal may be constant-folded away. This
11406 -- is a useful optimization for long concatenation expressions.
11407
11408 -- If the string is an aggregate built for a single character (which
11409 -- happens in a non-static context) or a is null string to which special
11410 -- checks may apply, we build the subtype. Wide strings must also get a
11411 -- string subtype if they come from a one character aggregate. Strings
11412 -- generated by attributes might be static, but it is often hard to
11413 -- determine whether the enclosing context is static, so we generate
11414 -- subtypes for them as well, thus losing some rarer optimizations ???
11415 -- Same for strings that come from a static conversion.
11416
11417 Need_Check :=
11418 (Strlen = 0 and then Typ /= Standard_String)
11419 or else Nkind (Parent (N)) /= N_Op_Concat
11420 or else (N /= Left_Opnd (Parent (N))
11421 and then N /= Right_Opnd (Parent (N)))
11422 or else ((Typ = Standard_Wide_String
11423 or else Typ = Standard_Wide_Wide_String)
11424 and then Nkind (Original_Node (N)) /= N_String_Literal);
11425
11426 -- If the resolving type is itself a string literal subtype, we can just
11427 -- reuse it, since there is no point in creating another.
11428
11429 if Ekind (Typ) = E_String_Literal_Subtype then
11430 Subtype_Id := Typ;
11431
11432 elsif Nkind (Parent (N)) = N_Op_Concat
11433 and then not Need_Check
11434 and then Nkind (Original_Node (N)) not in N_Character_Literal
11435 | N_Attribute_Reference
11436 | N_Qualified_Expression
11437 | N_Type_Conversion
11438 then
11439 Subtype_Id := Typ;
11440
11441 -- Do not generate a string literal subtype for the default expression
11442 -- of a formal parameter in GNATprove mode. This is because the string
11443 -- subtype is associated with the freezing actions of the subprogram,
11444 -- however freezing is disabled in GNATprove mode and as a result the
11445 -- subtype is unavailable.
11446
11447 elsif GNATprove_Mode
11448 and then Nkind (Parent (N)) = N_Parameter_Specification
11449 then
11450 Subtype_Id := Typ;
11451
11452 -- Otherwise we must create a string literal subtype. Note that the
11453 -- whole idea of string literal subtypes is simply to avoid the need
11454 -- for building a full fledged array subtype for each literal.
11455
11456 else
11457 Set_String_Literal_Subtype (N, Typ);
11458 Subtype_Id := Etype (N);
11459 end if;
11460
11461 if Nkind (Parent (N)) /= N_Op_Concat
11462 or else Need_Check
11463 then
11464 Set_Etype (N, Subtype_Id);
11465 Eval_String_Literal (N);
11466 end if;
11467
11468 if Is_Limited_Composite (Typ)
11469 or else Is_Private_Composite (Typ)
11470 then
11471 Error_Msg_N ("string literal not available for private array", N);
11472 Set_Etype (N, Any_Type);
11473 return;
11474 end if;
11475
11476 -- The validity of a null string has been checked in the call to
11477 -- Eval_String_Literal.
11478
11479 if Strlen = 0 then
11480 return;
11481
11482 -- Always accept string literal with component type Any_Character, which
11483 -- occurs in error situations and in comparisons of literals, both of
11484 -- which should accept all literals.
11485
11486 elsif R_Typ = Any_Character then
11487 return;
11488
11489 -- If the type is bit-packed, then we always transform the string
11490 -- literal into a full fledged aggregate.
11491
11492 elsif Is_Bit_Packed_Array (Typ) then
11493 null;
11494
11495 -- Deal with cases of Wide_Wide_String, Wide_String, and String
11496
11497 else
11498 -- For Standard.Wide_Wide_String, or any other type whose component
11499 -- type is Standard.Wide_Wide_Character, we know that all the
11500 -- characters in the string must be acceptable, since the parser
11501 -- accepted the characters as valid character literals.
11502
11503 if R_Typ = Standard_Wide_Wide_Character then
11504 null;
11505
11506 -- For the case of Standard.String, or any other type whose component
11507 -- type is Standard.Character, we must make sure that there are no
11508 -- wide characters in the string, i.e. that it is entirely composed
11509 -- of characters in range of type Character.
11510
11511 -- If the string literal is the result of a static concatenation, the
11512 -- test has already been performed on the components, and need not be
11513 -- repeated.
11514
11515 elsif R_Typ = Standard_Character
11516 and then Nkind (Original_Node (N)) /= N_Op_Concat
11517 then
11518 for J in 1 .. Strlen loop
11519 if not In_Character_Range (Get_String_Char (Str, J)) then
11520
11521 -- If we are out of range, post error. This is one of the
11522 -- very few places that we place the flag in the middle of
11523 -- a token, right under the offending wide character. Not
11524 -- quite clear if this is right wrt wide character encoding
11525 -- sequences, but it's only an error message.
11526
11527 Error_Msg
11528 ("literal out of range of type Standard.Character",
11529 Source_Ptr (Int (Loc) + J));
11530 return;
11531 end if;
11532 end loop;
11533
11534 -- For the case of Standard.Wide_String, or any other type whose
11535 -- component type is Standard.Wide_Character, we must make sure that
11536 -- there are no wide characters in the string, i.e. that it is
11537 -- entirely composed of characters in range of type Wide_Character.
11538
11539 -- If the string literal is the result of a static concatenation,
11540 -- the test has already been performed on the components, and need
11541 -- not be repeated.
11542
11543 elsif R_Typ = Standard_Wide_Character
11544 and then Nkind (Original_Node (N)) /= N_Op_Concat
11545 then
11546 for J in 1 .. Strlen loop
11547 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
11548
11549 -- If we are out of range, post error. This is one of the
11550 -- very few places that we place the flag in the middle of
11551 -- a token, right under the offending wide character.
11552
11553 -- This is not quite right, because characters in general
11554 -- will take more than one character position ???
11555
11556 Error_Msg
11557 ("literal out of range of type Standard.Wide_Character",
11558 Source_Ptr (Int (Loc) + J));
11559 return;
11560 end if;
11561 end loop;
11562
11563 -- If the root type is not a standard character, then we will convert
11564 -- the string into an aggregate and will let the aggregate code do
11565 -- the checking. Standard Wide_Wide_Character is also OK here.
11566
11567 else
11568 null;
11569 end if;
11570
11571 -- See if the component type of the array corresponding to the string
11572 -- has compile time known bounds. If yes we can directly check
11573 -- whether the evaluation of the string will raise constraint error.
11574 -- Otherwise we need to transform the string literal into the
11575 -- corresponding character aggregate and let the aggregate code do
11576 -- the checking. We use the same transformation if the component
11577 -- type has a static predicate, which will be applied to each
11578 -- character when the aggregate is resolved.
11579
11580 if Is_Standard_Character_Type (R_Typ) then
11581
11582 -- Check for the case of full range, where we are definitely OK
11583
11584 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
11585 return;
11586 end if;
11587
11588 -- Here the range is not the complete base type range, so check
11589
11590 declare
11591 Comp_Typ_Lo : constant Node_Id :=
11592 Type_Low_Bound (Component_Type (Typ));
11593 Comp_Typ_Hi : constant Node_Id :=
11594 Type_High_Bound (Component_Type (Typ));
11595
11596 Char_Val : Int;
11597
11598 begin
11599 if Compile_Time_Known_Value (Comp_Typ_Lo)
11600 and then Compile_Time_Known_Value (Comp_Typ_Hi)
11601 then
11602 for J in 1 .. Strlen loop
11603 Char_Val := Int (Get_String_Char (Str, J));
11604
11605 if Char_Val < Expr_Value (Comp_Typ_Lo)
11606 or else Char_Val > Expr_Value (Comp_Typ_Hi)
11607 then
11608 Apply_Compile_Time_Constraint_Error
11609 (N, "character out of range??",
11610 CE_Range_Check_Failed,
11611 Loc => Source_Ptr (Int (Loc) + J));
11612 end if;
11613 end loop;
11614
11615 if not Has_Static_Predicate (C_Typ) then
11616 return;
11617 end if;
11618 end if;
11619 end;
11620 end if;
11621 end if;
11622
11623 -- If we got here we meed to transform the string literal into the
11624 -- equivalent qualified positional array aggregate. This is rather
11625 -- heavy artillery for this situation, but it is hard work to avoid.
11626
11627 declare
11628 Lits : constant List_Id := New_List;
11629 P : Source_Ptr := Loc + 1;
11630 C : Char_Code;
11631
11632 begin
11633 -- Build the character literals, we give them source locations that
11634 -- correspond to the string positions, which is a bit tricky given
11635 -- the possible presence of wide character escape sequences.
11636
11637 for J in 1 .. Strlen loop
11638 C := Get_String_Char (Str, J);
11639 Set_Character_Literal_Name (C);
11640
11641 Append_To (Lits,
11642 Make_Character_Literal (P,
11643 Chars => Name_Find,
11644 Char_Literal_Value => UI_From_CC (C)));
11645
11646 if In_Character_Range (C) then
11647 P := P + 1;
11648
11649 -- Should we have a call to Skip_Wide here ???
11650
11651 -- ??? else
11652 -- Skip_Wide (P);
11653
11654 end if;
11655 end loop;
11656
11657 Rewrite (N,
11658 Make_Qualified_Expression (Loc,
11659 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
11660 Expression =>
11661 Make_Aggregate (Loc, Expressions => Lits)));
11662
11663 Analyze_And_Resolve (N, Typ);
11664 end;
11665 end Resolve_String_Literal;
11666
11667 -------------------------
11668 -- Resolve_Target_Name --
11669 -------------------------
11670
11671 procedure Resolve_Target_Name (N : Node_Id; Typ : Entity_Id) is
11672 begin
11673 Set_Etype (N, Typ);
11674 end Resolve_Target_Name;
11675
11676 -----------------------------
11677 -- Resolve_Type_Conversion --
11678 -----------------------------
11679
11680 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
11681 Conv_OK : constant Boolean := Conversion_OK (N);
11682 Operand : constant Node_Id := Expression (N);
11683 Operand_Typ : constant Entity_Id := Etype (Operand);
11684 Target_Typ : constant Entity_Id := Etype (N);
11685 Rop : Node_Id;
11686 Orig_N : Node_Id;
11687 Orig_T : Node_Id;
11688
11689 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
11690 -- Set to False to suppress cases where we want to suppress the test
11691 -- for redundancy to avoid possible false positives on this warning.
11692
11693 begin
11694 if not Conv_OK
11695 and then not Valid_Conversion (N, Target_Typ, Operand)
11696 then
11697 return;
11698 end if;
11699
11700 -- If the Operand Etype is Universal_Fixed, then the conversion is
11701 -- never redundant. We need this check because by the time we have
11702 -- finished the rather complex transformation, the conversion looks
11703 -- redundant when it is not.
11704
11705 if Operand_Typ = Universal_Fixed then
11706 Test_Redundant := False;
11707
11708 -- If the operand is marked as Any_Fixed, then special processing is
11709 -- required. This is also a case where we suppress the test for a
11710 -- redundant conversion, since most certainly it is not redundant.
11711
11712 elsif Operand_Typ = Any_Fixed then
11713 Test_Redundant := False;
11714
11715 -- Mixed-mode operation involving a literal. Context must be a fixed
11716 -- type which is applied to the literal subsequently.
11717
11718 -- Multiplication and division involving two fixed type operands must
11719 -- yield a universal real because the result is computed in arbitrary
11720 -- precision.
11721
11722 if Is_Fixed_Point_Type (Typ)
11723 and then Nkind (Operand) in N_Op_Divide | N_Op_Multiply
11724 and then Etype (Left_Opnd (Operand)) = Any_Fixed
11725 and then Etype (Right_Opnd (Operand)) = Any_Fixed
11726 then
11727 Set_Etype (Operand, Universal_Real);
11728
11729 elsif Is_Numeric_Type (Typ)
11730 and then Nkind (Operand) in N_Op_Multiply | N_Op_Divide
11731 and then (Etype (Right_Opnd (Operand)) = Universal_Real
11732 or else
11733 Etype (Left_Opnd (Operand)) = Universal_Real)
11734 then
11735 -- Return if expression is ambiguous
11736
11737 if Unique_Fixed_Point_Type (N) = Any_Type then
11738 return;
11739
11740 -- If nothing else, the available fixed type is Duration
11741
11742 else
11743 Set_Etype (Operand, Standard_Duration);
11744 end if;
11745
11746 -- Resolve the real operand with largest available precision
11747
11748 if Etype (Right_Opnd (Operand)) = Universal_Real then
11749 Rop := New_Copy_Tree (Right_Opnd (Operand));
11750 else
11751 Rop := New_Copy_Tree (Left_Opnd (Operand));
11752 end if;
11753
11754 Resolve (Rop, Universal_Real);
11755
11756 -- If the operand is a literal (it could be a non-static and
11757 -- illegal exponentiation) check whether the use of Duration
11758 -- is potentially inaccurate.
11759
11760 if Nkind (Rop) = N_Real_Literal
11761 and then Realval (Rop) /= Ureal_0
11762 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
11763 then
11764 Error_Msg_N
11765 ("??universal real operand can only "
11766 & "be interpreted as Duration!", Rop);
11767 Error_Msg_N
11768 ("\??precision will be lost in the conversion!", Rop);
11769 end if;
11770
11771 elsif Is_Numeric_Type (Typ)
11772 and then Nkind (Operand) in N_Op
11773 and then Unique_Fixed_Point_Type (N) /= Any_Type
11774 then
11775 Set_Etype (Operand, Standard_Duration);
11776
11777 else
11778 Error_Msg_N ("invalid context for mixed mode operation", N);
11779 Set_Etype (Operand, Any_Type);
11780 return;
11781 end if;
11782 end if;
11783
11784 Resolve (Operand);
11785
11786 Analyze_Dimension (N);
11787
11788 -- Note: we do the Eval_Type_Conversion call before applying the
11789 -- required checks for a subtype conversion. This is important, since
11790 -- both are prepared under certain circumstances to change the type
11791 -- conversion to a constraint error node, but in the case of
11792 -- Eval_Type_Conversion this may reflect an illegality in the static
11793 -- case, and we would miss the illegality (getting only a warning
11794 -- message), if we applied the type conversion checks first.
11795
11796 Eval_Type_Conversion (N);
11797
11798 -- Even when evaluation is not possible, we may be able to simplify the
11799 -- conversion or its expression. This needs to be done before applying
11800 -- checks, since otherwise the checks may use the original expression
11801 -- and defeat the simplifications. This is specifically the case for
11802 -- elimination of the floating-point Truncation attribute in
11803 -- float-to-int conversions.
11804
11805 Simplify_Type_Conversion (N);
11806
11807 -- If after evaluation we still have a type conversion, then we may need
11808 -- to apply checks required for a subtype conversion. But skip them if
11809 -- universal fixed operands are involved, since range checks are handled
11810 -- separately for these cases, after the expansion done by Exp_Fixd.
11811
11812 if Nkind (N) = N_Type_Conversion
11813 and then not Is_Generic_Type (Root_Type (Target_Typ))
11814 and then Target_Typ /= Universal_Fixed
11815 and then Etype (Operand) /= Universal_Fixed
11816 then
11817 Apply_Type_Conversion_Checks (N);
11818 end if;
11819
11820 -- Issue warning for conversion of simple object to its own type. We
11821 -- have to test the original nodes, since they may have been rewritten
11822 -- by various optimizations.
11823
11824 Orig_N := Original_Node (N);
11825
11826 -- Here we test for a redundant conversion if the warning mode is
11827 -- active (and was not locally reset), and we have a type conversion
11828 -- from source not appearing in a generic instance.
11829
11830 if Test_Redundant
11831 and then Nkind (Orig_N) = N_Type_Conversion
11832 and then Comes_From_Source (Orig_N)
11833 and then not In_Instance
11834 then
11835 Orig_N := Original_Node (Expression (Orig_N));
11836 Orig_T := Target_Typ;
11837
11838 -- If the node is part of a larger expression, the Target_Type
11839 -- may not be the original type of the node if the context is a
11840 -- condition. Recover original type to see if conversion is needed.
11841
11842 if Is_Boolean_Type (Orig_T)
11843 and then Nkind (Parent (N)) in N_Op
11844 then
11845 Orig_T := Etype (Parent (N));
11846 end if;
11847
11848 -- If we have an entity name, then give the warning if the entity
11849 -- is the right type, or if it is a loop parameter covered by the
11850 -- original type (that's needed because loop parameters have an
11851 -- odd subtype coming from the bounds).
11852
11853 if (Is_Entity_Name (Orig_N)
11854 and then Present (Entity (Orig_N))
11855 and then
11856 (Etype (Entity (Orig_N)) = Orig_T
11857 or else
11858 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
11859 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
11860
11861 -- If not an entity, then type of expression must match
11862
11863 or else Etype (Orig_N) = Orig_T
11864 then
11865 -- One more check, do not give warning if the analyzed conversion
11866 -- has an expression with non-static bounds, and the bounds of the
11867 -- target are static. This avoids junk warnings in cases where the
11868 -- conversion is necessary to establish staticness, for example in
11869 -- a case statement.
11870
11871 if not Is_OK_Static_Subtype (Operand_Typ)
11872 and then Is_OK_Static_Subtype (Target_Typ)
11873 then
11874 null;
11875
11876 -- Finally, if this type conversion occurs in a context requiring
11877 -- a prefix, and the expression is a qualified expression then the
11878 -- type conversion is not redundant, since a qualified expression
11879 -- is not a prefix, whereas a type conversion is. For example, "X
11880 -- := T'(Funx(...)).Y;" is illegal because a selected component
11881 -- requires a prefix, but a type conversion makes it legal: "X :=
11882 -- T(T'(Funx(...))).Y;"
11883
11884 -- In Ada 2012, a qualified expression is a name, so this idiom is
11885 -- no longer needed, but we still suppress the warning because it
11886 -- seems unfriendly for warnings to pop up when you switch to the
11887 -- newer language version.
11888
11889 elsif Nkind (Orig_N) = N_Qualified_Expression
11890 and then Nkind (Parent (N)) in N_Attribute_Reference
11891 | N_Indexed_Component
11892 | N_Selected_Component
11893 | N_Slice
11894 | N_Explicit_Dereference
11895 then
11896 null;
11897
11898 -- Never warn on conversion to Long_Long_Integer'Base since
11899 -- that is most likely an artifact of the extended overflow
11900 -- checking and comes from complex expanded code.
11901
11902 elsif Orig_T = Base_Type (Standard_Long_Long_Integer) then
11903 null;
11904
11905 -- Here we give the redundant conversion warning. If it is an
11906 -- entity, give the name of the entity in the message. If not,
11907 -- just mention the expression.
11908
11909 else
11910 if Is_Entity_Name (Orig_N) then
11911 Error_Msg_Node_2 := Orig_T;
11912 Error_Msg_NE -- CODEFIX
11913 ("?r?redundant conversion, & is of type &!",
11914 N, Entity (Orig_N));
11915 else
11916 Error_Msg_NE
11917 ("?r?redundant conversion, expression is of type&!",
11918 N, Orig_T);
11919 end if;
11920 end if;
11921 end if;
11922 end if;
11923
11924 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
11925 -- No need to perform any interface conversion if the type of the
11926 -- expression coincides with the target type.
11927
11928 if Ada_Version >= Ada_2005
11929 and then Expander_Active
11930 and then Operand_Typ /= Target_Typ
11931 then
11932 declare
11933 Opnd : Entity_Id := Operand_Typ;
11934 Target : Entity_Id := Target_Typ;
11935
11936 begin
11937 -- If the type of the operand is a limited view, use nonlimited
11938 -- view when available. If it is a class-wide type, recover the
11939 -- class-wide type of the nonlimited view.
11940
11941 if From_Limited_With (Opnd)
11942 and then Has_Non_Limited_View (Opnd)
11943 then
11944 Opnd := Non_Limited_View (Opnd);
11945 Set_Etype (Expression (N), Opnd);
11946 end if;
11947
11948 -- It seems that Non_Limited_View should also be applied for
11949 -- Target when it has a limited view, but that leads to missing
11950 -- error checks on interface conversions further below. ???
11951
11952 if Is_Access_Type (Opnd) then
11953 Opnd := Designated_Type (Opnd);
11954
11955 -- If the type of the operand is a limited view, use nonlimited
11956 -- view when available. If it is a class-wide type, recover the
11957 -- class-wide type of the nonlimited view.
11958
11959 if From_Limited_With (Opnd)
11960 and then Has_Non_Limited_View (Opnd)
11961 then
11962 Opnd := Non_Limited_View (Opnd);
11963 end if;
11964 end if;
11965
11966 if Is_Access_Type (Target_Typ) then
11967 Target := Designated_Type (Target);
11968
11969 -- If the target type is a limited view, use nonlimited view
11970 -- when available.
11971
11972 if From_Limited_With (Target)
11973 and then Has_Non_Limited_View (Target)
11974 then
11975 Target := Non_Limited_View (Target);
11976 end if;
11977 end if;
11978
11979 if Opnd = Target then
11980 null;
11981
11982 -- Conversion from interface type
11983
11984 -- It seems that it would be better for the error checks below
11985 -- to be performed as part of Validate_Conversion (and maybe some
11986 -- of the error checks above could be moved as well?). ???
11987
11988 elsif Is_Interface (Opnd) then
11989
11990 -- Ada 2005 (AI-217): Handle entities from limited views
11991
11992 if From_Limited_With (Opnd) then
11993 Error_Msg_Qual_Level := 99;
11994 Error_Msg_NE -- CODEFIX
11995 ("missing WITH clause on package &", N,
11996 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
11997 Error_Msg_N
11998 ("type conversions require visibility of the full view",
11999 N);
12000
12001 elsif From_Limited_With (Target)
12002 and then not
12003 (Is_Access_Type (Target_Typ)
12004 and then Present (Non_Limited_View (Etype (Target))))
12005 then
12006 Error_Msg_Qual_Level := 99;
12007 Error_Msg_NE -- CODEFIX
12008 ("missing WITH clause on package &", N,
12009 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
12010 Error_Msg_N
12011 ("type conversions require visibility of the full view",
12012 N);
12013
12014 else
12015 Expand_Interface_Conversion (N);
12016 end if;
12017
12018 -- Conversion to interface type
12019
12020 elsif Is_Interface (Target) then
12021
12022 -- Handle subtypes
12023
12024 if Ekind (Opnd) in E_Protected_Subtype | E_Task_Subtype then
12025 Opnd := Etype (Opnd);
12026 end if;
12027
12028 if Is_Class_Wide_Type (Opnd)
12029 or else Interface_Present_In_Ancestor
12030 (Typ => Opnd,
12031 Iface => Target)
12032 then
12033 Expand_Interface_Conversion (N);
12034 else
12035 Error_Msg_Name_1 := Chars (Etype (Target));
12036 Error_Msg_Name_2 := Chars (Opnd);
12037 Error_Msg_N
12038 ("wrong interface conversion (% is not a progenitor "
12039 & "of %)", N);
12040 end if;
12041 end if;
12042 end;
12043 end if;
12044
12045 -- Ada 2012: Once the type conversion is resolved, check whether the
12046 -- operand statisfies a static predicate of the target subtype, if any.
12047 -- In the static expression case, a predicate check failure is an error.
12048
12049 if Has_Predicates (Target_Typ) then
12050 Check_Expression_Against_Static_Predicate
12051 (N, Target_Typ, Static_Failure_Is_Error => True);
12052 end if;
12053
12054 -- If at this stage we have a fixed to integer conversion, make sure the
12055 -- Do_Range_Check flag is set, because such conversions in general need
12056 -- a range check. We only need this if expansion is off, see above why.
12057
12058 if Nkind (N) = N_Type_Conversion
12059 and then not Expander_Active
12060 and then Is_Integer_Type (Target_Typ)
12061 and then Is_Fixed_Point_Type (Operand_Typ)
12062 and then not Range_Checks_Suppressed (Target_Typ)
12063 and then not Range_Checks_Suppressed (Operand_Typ)
12064 then
12065 Set_Do_Range_Check (Operand);
12066 end if;
12067
12068 -- Generating C code a type conversion of an access to constrained
12069 -- array type to access to unconstrained array type involves building
12070 -- a fat pointer which in general cannot be generated on the fly. We
12071 -- remove side effects in order to store the result of the conversion
12072 -- into a temporary.
12073
12074 if Modify_Tree_For_C
12075 and then Nkind (N) = N_Type_Conversion
12076 and then Nkind (Parent (N)) /= N_Object_Declaration
12077 and then Is_Access_Type (Etype (N))
12078 and then Is_Array_Type (Designated_Type (Etype (N)))
12079 and then not Is_Constrained (Designated_Type (Etype (N)))
12080 and then Is_Constrained (Designated_Type (Etype (Expression (N))))
12081 then
12082 Remove_Side_Effects (N);
12083 end if;
12084 end Resolve_Type_Conversion;
12085
12086 ----------------------
12087 -- Resolve_Unary_Op --
12088 ----------------------
12089
12090 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
12091 B_Typ : constant Entity_Id := Base_Type (Typ);
12092 R : constant Node_Id := Right_Opnd (N);
12093 OK : Boolean;
12094 Lo : Uint;
12095 Hi : Uint;
12096
12097 begin
12098 -- Deal with intrinsic unary operators
12099
12100 if Comes_From_Source (N)
12101 and then Ekind (Entity (N)) = E_Function
12102 and then Is_Imported (Entity (N))
12103 and then Is_Intrinsic_Subprogram (Entity (N))
12104 then
12105 Resolve_Intrinsic_Unary_Operator (N, Typ);
12106 return;
12107 end if;
12108
12109 -- Deal with universal cases
12110
12111 if Is_Universal_Numeric_Type (Etype (R)) then
12112 Check_For_Visible_Operator (N, B_Typ);
12113 end if;
12114
12115 Set_Etype (N, B_Typ);
12116 Resolve (R, B_Typ);
12117
12118 -- Generate warning for negative literal of a modular type, unless it is
12119 -- enclosed directly in a type qualification or a type conversion, as it
12120 -- is likely not what the user intended. We don't issue the warning for
12121 -- the common use of -1 to denote OxFFFF_FFFF...
12122
12123 if Warn_On_Suspicious_Modulus_Value
12124 and then Nkind (N) = N_Op_Minus
12125 and then Nkind (R) = N_Integer_Literal
12126 and then Is_Modular_Integer_Type (B_Typ)
12127 and then Nkind (Parent (N)) not in N_Qualified_Expression
12128 | N_Type_Conversion
12129 and then Expr_Value (R) > Uint_1
12130 then
12131 Error_Msg_N
12132 ("?M?negative literal of modular type is in fact positive", N);
12133 Error_Msg_Uint_1 := (-Expr_Value (R)) mod Modulus (B_Typ);
12134 Error_Msg_Uint_2 := Expr_Value (R);
12135 Error_Msg_N ("\do you really mean^ when writing -^ '?", N);
12136 Error_Msg_N
12137 ("\if you do, use qualification to avoid this warning", N);
12138 end if;
12139
12140 -- Generate warning for expressions like abs (x mod 2)
12141
12142 if Warn_On_Redundant_Constructs
12143 and then Nkind (N) = N_Op_Abs
12144 then
12145 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
12146
12147 if OK and then Hi >= Lo and then Lo >= 0 then
12148 Error_Msg_N -- CODEFIX
12149 ("?r?abs applied to known non-negative value has no effect", N);
12150 end if;
12151 end if;
12152
12153 -- Deal with reference generation
12154
12155 Check_Unset_Reference (R);
12156 Generate_Operator_Reference (N, B_Typ);
12157 Analyze_Dimension (N);
12158 Eval_Unary_Op (N);
12159
12160 -- Set overflow checking bit. Much cleverer code needed here eventually
12161 -- and perhaps the Resolve routines should be separated for the various
12162 -- arithmetic operations, since they will need different processing ???
12163
12164 if Nkind (N) in N_Op then
12165 if not Overflow_Checks_Suppressed (Etype (N)) then
12166 Enable_Overflow_Check (N);
12167 end if;
12168 end if;
12169
12170 -- Generate warning for expressions like -5 mod 3 for integers. No need
12171 -- to worry in the floating-point case, since parens do not affect the
12172 -- result so there is no point in giving in a warning.
12173
12174 declare
12175 Norig : constant Node_Id := Original_Node (N);
12176 Rorig : Node_Id;
12177 Val : Uint;
12178 HB : Uint;
12179 LB : Uint;
12180 Lval : Uint;
12181 Opnd : Node_Id;
12182
12183 begin
12184 if Warn_On_Questionable_Missing_Parens
12185 and then Comes_From_Source (Norig)
12186 and then Is_Integer_Type (Typ)
12187 and then Nkind (Norig) = N_Op_Minus
12188 then
12189 Rorig := Original_Node (Right_Opnd (Norig));
12190
12191 -- We are looking for cases where the right operand is not
12192 -- parenthesized, and is a binary operator, multiply, divide, or
12193 -- mod. These are the cases where the grouping can affect results.
12194
12195 if Paren_Count (Rorig) = 0
12196 and then Nkind (Rorig) in N_Op_Mod | N_Op_Multiply | N_Op_Divide
12197 then
12198 -- For mod, we always give the warning, since the value is
12199 -- affected by the parenthesization (e.g. (-5) mod 315 /=
12200 -- -(5 mod 315)). But for the other cases, the only concern is
12201 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
12202 -- overflows, but (-2) * 64 does not). So we try to give the
12203 -- message only when overflow is possible.
12204
12205 if Nkind (Rorig) /= N_Op_Mod
12206 and then Compile_Time_Known_Value (R)
12207 then
12208 Val := Expr_Value (R);
12209
12210 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
12211 HB := Expr_Value (Type_High_Bound (Typ));
12212 else
12213 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
12214 end if;
12215
12216 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
12217 LB := Expr_Value (Type_Low_Bound (Typ));
12218 else
12219 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
12220 end if;
12221
12222 -- Note that the test below is deliberately excluding the
12223 -- largest negative number, since that is a potentially
12224 -- troublesome case (e.g. -2 * x, where the result is the
12225 -- largest negative integer has an overflow with 2 * x).
12226
12227 if Val > LB and then Val <= HB then
12228 return;
12229 end if;
12230 end if;
12231
12232 -- For the multiplication case, the only case we have to worry
12233 -- about is when (-a)*b is exactly the largest negative number
12234 -- so that -(a*b) can cause overflow. This can only happen if
12235 -- a is a power of 2, and more generally if any operand is a
12236 -- constant that is not a power of 2, then the parentheses
12237 -- cannot affect whether overflow occurs. We only bother to
12238 -- test the left most operand
12239
12240 -- Loop looking at left operands for one that has known value
12241
12242 Opnd := Rorig;
12243 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
12244 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
12245 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
12246
12247 -- Operand value of 0 or 1 skips warning
12248
12249 if Lval <= 1 then
12250 return;
12251
12252 -- Otherwise check power of 2, if power of 2, warn, if
12253 -- anything else, skip warning.
12254
12255 else
12256 while Lval /= 2 loop
12257 if Lval mod 2 = 1 then
12258 return;
12259 else
12260 Lval := Lval / 2;
12261 end if;
12262 end loop;
12263
12264 exit Opnd_Loop;
12265 end if;
12266 end if;
12267
12268 -- Keep looking at left operands
12269
12270 Opnd := Left_Opnd (Opnd);
12271 end loop Opnd_Loop;
12272
12273 -- For rem or "/" we can only have a problematic situation
12274 -- if the divisor has a value of minus one or one. Otherwise
12275 -- overflow is impossible (divisor > 1) or we have a case of
12276 -- division by zero in any case.
12277
12278 if Nkind (Rorig) in N_Op_Divide | N_Op_Rem
12279 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
12280 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
12281 then
12282 return;
12283 end if;
12284
12285 -- If we fall through warning should be issued
12286
12287 -- Shouldn't we test Warn_On_Questionable_Missing_Parens ???
12288
12289 Error_Msg_N
12290 ("??unary minus expression should be parenthesized here!", N);
12291 end if;
12292 end if;
12293 end;
12294 end Resolve_Unary_Op;
12295
12296 ----------------------------------
12297 -- Resolve_Unchecked_Expression --
12298 ----------------------------------
12299
12300 procedure Resolve_Unchecked_Expression
12301 (N : Node_Id;
12302 Typ : Entity_Id)
12303 is
12304 begin
12305 Resolve (Expression (N), Typ, Suppress => All_Checks);
12306 Set_Etype (N, Typ);
12307 end Resolve_Unchecked_Expression;
12308
12309 ---------------------------------------
12310 -- Resolve_Unchecked_Type_Conversion --
12311 ---------------------------------------
12312
12313 procedure Resolve_Unchecked_Type_Conversion
12314 (N : Node_Id;
12315 Typ : Entity_Id)
12316 is
12317 pragma Warnings (Off, Typ);
12318
12319 Operand : constant Node_Id := Expression (N);
12320 Opnd_Type : constant Entity_Id := Etype (Operand);
12321
12322 begin
12323 -- Resolve operand using its own type
12324
12325 Resolve (Operand, Opnd_Type);
12326
12327 -- If the expression is a conversion to universal integer of an
12328 -- an expression with an integer type, then we can eliminate the
12329 -- intermediate conversion to universal integer.
12330
12331 if Nkind (Operand) = N_Type_Conversion
12332 and then Entity (Subtype_Mark (Operand)) = Universal_Integer
12333 and then Is_Integer_Type (Etype (Expression (Operand)))
12334 then
12335 Rewrite (Operand, Relocate_Node (Expression (Operand)));
12336 Analyze_And_Resolve (Operand);
12337 end if;
12338
12339 -- In an inlined context, the unchecked conversion may be applied
12340 -- to a literal, in which case its type is the type of the context.
12341 -- (In other contexts conversions cannot apply to literals).
12342
12343 if In_Inlined_Body
12344 and then (Opnd_Type = Any_Character or else
12345 Opnd_Type = Any_Integer or else
12346 Opnd_Type = Any_Real)
12347 then
12348 Set_Etype (Operand, Typ);
12349 end if;
12350
12351 Analyze_Dimension (N);
12352 Eval_Unchecked_Conversion (N);
12353 end Resolve_Unchecked_Type_Conversion;
12354
12355 ------------------------------
12356 -- Rewrite_Operator_As_Call --
12357 ------------------------------
12358
12359 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
12360 Loc : constant Source_Ptr := Sloc (N);
12361 Actuals : constant List_Id := New_List;
12362 New_N : Node_Id;
12363
12364 begin
12365 if Nkind (N) in N_Binary_Op then
12366 Append (Left_Opnd (N), Actuals);
12367 end if;
12368
12369 Append (Right_Opnd (N), Actuals);
12370
12371 New_N :=
12372 Make_Function_Call (Sloc => Loc,
12373 Name => New_Occurrence_Of (Nam, Loc),
12374 Parameter_Associations => Actuals);
12375
12376 Preserve_Comes_From_Source (New_N, N);
12377 Preserve_Comes_From_Source (Name (New_N), N);
12378 Rewrite (N, New_N);
12379 Set_Etype (N, Etype (Nam));
12380 end Rewrite_Operator_As_Call;
12381
12382 ------------------------------
12383 -- Rewrite_Renamed_Operator --
12384 ------------------------------
12385
12386 procedure Rewrite_Renamed_Operator
12387 (N : Node_Id;
12388 Op : Entity_Id;
12389 Typ : Entity_Id)
12390 is
12391 Nam : constant Name_Id := Chars (Op);
12392 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
12393 Op_Node : Node_Id;
12394
12395 begin
12396 -- Do not perform this transformation within a pre/postcondition,
12397 -- because the expression will be reanalyzed, and the transformation
12398 -- might affect the visibility of the operator, e.g. in an instance.
12399 -- Note that fully analyzed and expanded pre/postconditions appear as
12400 -- pragma Check equivalents.
12401
12402 if In_Pre_Post_Condition (N) then
12403 return;
12404 end if;
12405
12406 -- Likewise when an expression function is being preanalyzed, since the
12407 -- expression will be reanalyzed as part of the generated body.
12408
12409 if In_Spec_Expression then
12410 declare
12411 S : constant Entity_Id := Current_Scope_No_Loops;
12412 begin
12413 if Ekind (S) = E_Function
12414 and then Nkind (Original_Node (Unit_Declaration_Node (S))) =
12415 N_Expression_Function
12416 then
12417 return;
12418 end if;
12419 end;
12420 end if;
12421
12422 -- Rewrite the operator node using the real operator, not its renaming.
12423 -- Exclude user-defined intrinsic operations of the same name, which are
12424 -- treated separately and rewritten as calls.
12425
12426 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
12427 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
12428 Set_Chars (Op_Node, Nam);
12429 Set_Etype (Op_Node, Etype (N));
12430 Set_Entity (Op_Node, Op);
12431 Set_Right_Opnd (Op_Node, Right_Opnd (N));
12432
12433 -- Indicate that both the original entity and its renaming are
12434 -- referenced at this point.
12435
12436 Generate_Reference (Entity (N), N);
12437 Generate_Reference (Op, N);
12438
12439 if Is_Binary then
12440 Set_Left_Opnd (Op_Node, Left_Opnd (N));
12441 end if;
12442
12443 Rewrite (N, Op_Node);
12444
12445 -- If the context type is private, add the appropriate conversions so
12446 -- that the operator is applied to the full view. This is done in the
12447 -- routines that resolve intrinsic operators.
12448
12449 if Is_Intrinsic_Subprogram (Op) and then Is_Private_Type (Typ) then
12450 case Nkind (N) is
12451 when N_Op_Add
12452 | N_Op_Divide
12453 | N_Op_Expon
12454 | N_Op_Mod
12455 | N_Op_Multiply
12456 | N_Op_Rem
12457 | N_Op_Subtract
12458 =>
12459 Resolve_Intrinsic_Operator (N, Typ);
12460
12461 when N_Op_Abs
12462 | N_Op_Minus
12463 | N_Op_Plus
12464 =>
12465 Resolve_Intrinsic_Unary_Operator (N, Typ);
12466
12467 when others =>
12468 Resolve (N, Typ);
12469 end case;
12470 end if;
12471
12472 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
12473
12474 -- Operator renames a user-defined operator of the same name. Use the
12475 -- original operator in the node, which is the one Gigi knows about.
12476
12477 Set_Entity (N, Op);
12478 Set_Is_Overloaded (N, False);
12479 end if;
12480 end Rewrite_Renamed_Operator;
12481
12482 -----------------------
12483 -- Set_Slice_Subtype --
12484 -----------------------
12485
12486 -- Build an implicit subtype declaration to represent the type delivered by
12487 -- the slice. This is an abbreviated version of an array subtype. We define
12488 -- an index subtype for the slice, using either the subtype name or the
12489 -- discrete range of the slice. To be consistent with index usage elsewhere
12490 -- we create a list header to hold the single index. This list is not
12491 -- otherwise attached to the syntax tree.
12492
12493 procedure Set_Slice_Subtype (N : Node_Id) is
12494 Loc : constant Source_Ptr := Sloc (N);
12495 Index_List : constant List_Id := New_List;
12496 Index : Node_Id;
12497 Index_Subtype : Entity_Id;
12498 Index_Type : Entity_Id;
12499 Slice_Subtype : Entity_Id;
12500 Drange : constant Node_Id := Discrete_Range (N);
12501
12502 begin
12503 Index_Type := Base_Type (Etype (Drange));
12504
12505 if Is_Entity_Name (Drange) then
12506 Index_Subtype := Entity (Drange);
12507
12508 else
12509 -- We force the evaluation of a range. This is definitely needed in
12510 -- the renamed case, and seems safer to do unconditionally. Note in
12511 -- any case that since we will create and insert an Itype referring
12512 -- to this range, we must make sure any side effect removal actions
12513 -- are inserted before the Itype definition.
12514
12515 if Nkind (Drange) = N_Range then
12516 Force_Evaluation (Low_Bound (Drange));
12517 Force_Evaluation (High_Bound (Drange));
12518
12519 -- If the discrete range is given by a subtype indication, the
12520 -- type of the slice is the base of the subtype mark.
12521
12522 elsif Nkind (Drange) = N_Subtype_Indication then
12523 declare
12524 R : constant Node_Id := Range_Expression (Constraint (Drange));
12525 begin
12526 Index_Type := Base_Type (Entity (Subtype_Mark (Drange)));
12527 Force_Evaluation (Low_Bound (R));
12528 Force_Evaluation (High_Bound (R));
12529 end;
12530 end if;
12531
12532 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
12533
12534 -- Take a new copy of Drange (where bounds have been rewritten to
12535 -- reference side-effect-free names). Using a separate tree ensures
12536 -- that further expansion (e.g. while rewriting a slice assignment
12537 -- into a FOR loop) does not attempt to remove side effects on the
12538 -- bounds again (which would cause the bounds in the index subtype
12539 -- definition to refer to temporaries before they are defined) (the
12540 -- reason is that some names are considered side effect free here
12541 -- for the subtype, but not in the context of a loop iteration
12542 -- scheme).
12543
12544 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
12545 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
12546 Set_Etype (Index_Subtype, Index_Type);
12547 Set_Size_Info (Index_Subtype, Index_Type);
12548 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
12549 Set_Is_Constrained (Index_Subtype);
12550 end if;
12551
12552 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
12553
12554 Index := New_Occurrence_Of (Index_Subtype, Loc);
12555 Set_Etype (Index, Index_Subtype);
12556 Append (Index, Index_List);
12557
12558 Set_First_Index (Slice_Subtype, Index);
12559 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
12560 Set_Is_Constrained (Slice_Subtype, True);
12561
12562 Check_Compile_Time_Size (Slice_Subtype);
12563
12564 -- The Etype of the existing Slice node is reset to this slice subtype.
12565 -- Its bounds are obtained from its first index.
12566
12567 Set_Etype (N, Slice_Subtype);
12568
12569 -- For bit-packed slice subtypes, freeze immediately (except in the case
12570 -- of being in a "spec expression" where we never freeze when we first
12571 -- see the expression).
12572
12573 if Is_Bit_Packed_Array (Slice_Subtype) and not In_Spec_Expression then
12574 Freeze_Itype (Slice_Subtype, N);
12575
12576 -- For all other cases insert an itype reference in the slice's actions
12577 -- so that the itype is frozen at the proper place in the tree (i.e. at
12578 -- the point where actions for the slice are analyzed). Note that this
12579 -- is different from freezing the itype immediately, which might be
12580 -- premature (e.g. if the slice is within a transient scope). This needs
12581 -- to be done only if expansion is enabled, or in GNATprove mode to
12582 -- capture the associated run-time exceptions if any.
12583
12584 elsif Expander_Active or GNATprove_Mode then
12585 Ensure_Defined (Typ => Slice_Subtype, N => N);
12586 end if;
12587 end Set_Slice_Subtype;
12588
12589 --------------------------------
12590 -- Set_String_Literal_Subtype --
12591 --------------------------------
12592
12593 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
12594 Loc : constant Source_Ptr := Sloc (N);
12595 Low_Bound : constant Node_Id :=
12596 Type_Low_Bound (Etype (First_Index (Typ)));
12597 Subtype_Id : Entity_Id;
12598
12599 begin
12600 if Nkind (N) /= N_String_Literal then
12601 return;
12602 end if;
12603
12604 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
12605 Set_String_Literal_Length (Subtype_Id, UI_From_Int
12606 (String_Length (Strval (N))));
12607 Set_Etype (Subtype_Id, Base_Type (Typ));
12608 Set_Is_Constrained (Subtype_Id);
12609 Set_Etype (N, Subtype_Id);
12610
12611 -- The low bound is set from the low bound of the corresponding index
12612 -- type. Note that we do not store the high bound in the string literal
12613 -- subtype, but it can be deduced if necessary from the length and the
12614 -- low bound.
12615
12616 if Is_OK_Static_Expression (Low_Bound) then
12617 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
12618
12619 -- If the lower bound is not static we create a range for the string
12620 -- literal, using the index type and the known length of the literal.
12621 -- If the length is 1, then the upper bound is set to a mere copy of
12622 -- the lower bound; or else, if the index type is a signed integer,
12623 -- then the upper bound is computed as Low_Bound + L - 1; otherwise,
12624 -- the upper bound is computed as T'Val (T'Pos (Low_Bound) + L - 1).
12625
12626 else
12627 declare
12628 Length : constant Nat := String_Length (Strval (N));
12629 Index_List : constant List_Id := New_List;
12630 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
12631 Array_Subtype : Entity_Id;
12632 Drange : Node_Id;
12633 High_Bound : Node_Id;
12634 Index : Node_Id;
12635 Index_Subtype : Entity_Id;
12636
12637 begin
12638 if Length = 1 then
12639 High_Bound := New_Copy_Tree (Low_Bound);
12640
12641 elsif Is_Signed_Integer_Type (Index_Type) then
12642 High_Bound :=
12643 Make_Op_Add (Loc,
12644 Left_Opnd => New_Copy_Tree (Low_Bound),
12645 Right_Opnd => Make_Integer_Literal (Loc, Length - 1));
12646
12647 else
12648 High_Bound :=
12649 Make_Attribute_Reference (Loc,
12650 Attribute_Name => Name_Val,
12651 Prefix =>
12652 New_Occurrence_Of (Index_Type, Loc),
12653 Expressions => New_List (
12654 Make_Op_Add (Loc,
12655 Left_Opnd =>
12656 Make_Attribute_Reference (Loc,
12657 Attribute_Name => Name_Pos,
12658 Prefix =>
12659 New_Occurrence_Of (Index_Type, Loc),
12660 Expressions =>
12661 New_List (New_Copy_Tree (Low_Bound))),
12662 Right_Opnd =>
12663 Make_Integer_Literal (Loc, Length - 1))));
12664 end if;
12665
12666 if Is_Integer_Type (Index_Type) then
12667 Set_String_Literal_Low_Bound
12668 (Subtype_Id, Make_Integer_Literal (Loc, 1));
12669
12670 else
12671 -- If the index type is an enumeration type, build bounds
12672 -- expression with attributes.
12673
12674 Set_String_Literal_Low_Bound
12675 (Subtype_Id,
12676 Make_Attribute_Reference (Loc,
12677 Attribute_Name => Name_First,
12678 Prefix =>
12679 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
12680 end if;
12681
12682 Analyze_And_Resolve
12683 (String_Literal_Low_Bound (Subtype_Id), Base_Type (Index_Type));
12684
12685 -- Build bona fide subtype for the string, and wrap it in an
12686 -- unchecked conversion, because the back end expects the
12687 -- String_Literal_Subtype to have a static lower bound.
12688
12689 Index_Subtype :=
12690 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
12691 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
12692 Set_Scalar_Range (Index_Subtype, Drange);
12693 Set_Parent (Drange, N);
12694 Analyze_And_Resolve (Drange, Index_Type);
12695
12696 -- In this context, the Index_Type may already have a constraint,
12697 -- so use common base type on string subtype. The base type may
12698 -- be used when generating attributes of the string, for example
12699 -- in the context of a slice assignment.
12700
12701 Set_Etype (Index_Subtype, Base_Type (Index_Type));
12702 Set_Size_Info (Index_Subtype, Index_Type);
12703 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
12704
12705 Array_Subtype := Create_Itype (E_Array_Subtype, N);
12706
12707 Index := New_Occurrence_Of (Index_Subtype, Loc);
12708 Set_Etype (Index, Index_Subtype);
12709 Append (Index, Index_List);
12710
12711 Set_First_Index (Array_Subtype, Index);
12712 Set_Etype (Array_Subtype, Base_Type (Typ));
12713 Set_Is_Constrained (Array_Subtype, True);
12714
12715 Rewrite (N,
12716 Make_Unchecked_Type_Conversion (Loc,
12717 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
12718 Expression => Relocate_Node (N)));
12719 Set_Etype (N, Array_Subtype);
12720 end;
12721 end if;
12722 end Set_String_Literal_Subtype;
12723
12724 ------------------------------
12725 -- Simplify_Type_Conversion --
12726 ------------------------------
12727
12728 procedure Simplify_Type_Conversion (N : Node_Id) is
12729 begin
12730 if Nkind (N) = N_Type_Conversion then
12731 declare
12732 Operand : constant Node_Id := Expression (N);
12733 Target_Typ : constant Entity_Id := Etype (N);
12734 Opnd_Typ : constant Entity_Id := Etype (Operand);
12735
12736 begin
12737 -- Special processing if the conversion is the expression of a
12738 -- Rounding or Truncation attribute reference. In this case we
12739 -- replace:
12740
12741 -- ityp (ftyp'Rounding (x)) or ityp (ftyp'Truncation (x))
12742
12743 -- by
12744
12745 -- ityp (x)
12746
12747 -- with the Float_Truncate flag set to False or True respectively,
12748 -- which is more efficient. We reuse Rounding for Machine_Rounding
12749 -- as System.Fat_Gen, which is a permissible behavior.
12750
12751 if Is_Floating_Point_Type (Opnd_Typ)
12752 and then
12753 (Is_Integer_Type (Target_Typ)
12754 or else (Is_Fixed_Point_Type (Target_Typ)
12755 and then Conversion_OK (N)))
12756 and then Nkind (Operand) = N_Attribute_Reference
12757 and then Attribute_Name (Operand) in Name_Rounding
12758 | Name_Machine_Rounding
12759 | Name_Truncation
12760 then
12761 declare
12762 Truncate : constant Boolean :=
12763 Attribute_Name (Operand) = Name_Truncation;
12764 begin
12765 Rewrite (Operand,
12766 Relocate_Node (First (Expressions (Operand))));
12767 Set_Float_Truncate (N, Truncate);
12768 end;
12769
12770 -- Special processing for the conversion of an integer literal to
12771 -- a dynamic type: we first convert the literal to the root type
12772 -- and then convert the result to the target type, the goal being
12773 -- to avoid doing range checks in universal integer.
12774
12775 elsif Is_Integer_Type (Target_Typ)
12776 and then not Is_Generic_Type (Root_Type (Target_Typ))
12777 and then Nkind (Operand) = N_Integer_Literal
12778 and then Opnd_Typ = Universal_Integer
12779 then
12780 Convert_To_And_Rewrite (Root_Type (Target_Typ), Operand);
12781 Analyze_And_Resolve (Operand);
12782
12783 -- If the expression is a conversion to universal integer of an
12784 -- an expression with an integer type, then we can eliminate the
12785 -- intermediate conversion to universal integer.
12786
12787 elsif Nkind (Operand) = N_Type_Conversion
12788 and then Entity (Subtype_Mark (Operand)) = Universal_Integer
12789 and then Is_Integer_Type (Etype (Expression (Operand)))
12790 then
12791 Rewrite (Operand, Relocate_Node (Expression (Operand)));
12792 Analyze_And_Resolve (Operand);
12793 end if;
12794 end;
12795 end if;
12796 end Simplify_Type_Conversion;
12797
12798 -----------------------------
12799 -- Unique_Fixed_Point_Type --
12800 -----------------------------
12801
12802 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
12803 procedure Fixed_Point_Error (T1 : Entity_Id; T2 : Entity_Id);
12804 -- Give error messages for true ambiguity. Messages are posted on node
12805 -- N, and entities T1, T2 are the possible interpretations.
12806
12807 -----------------------
12808 -- Fixed_Point_Error --
12809 -----------------------
12810
12811 procedure Fixed_Point_Error (T1 : Entity_Id; T2 : Entity_Id) is
12812 begin
12813 Error_Msg_N ("ambiguous universal_fixed_expression", N);
12814 Error_Msg_NE ("\\possible interpretation as}", N, T1);
12815 Error_Msg_NE ("\\possible interpretation as}", N, T2);
12816 end Fixed_Point_Error;
12817
12818 -- Local variables
12819
12820 ErrN : Node_Id;
12821 Item : Node_Id;
12822 Scop : Entity_Id;
12823 T1 : Entity_Id;
12824 T2 : Entity_Id;
12825
12826 -- Start of processing for Unique_Fixed_Point_Type
12827
12828 begin
12829 -- The operations on Duration are visible, so Duration is always a
12830 -- possible interpretation.
12831
12832 T1 := Standard_Duration;
12833
12834 -- Look for fixed-point types in enclosing scopes
12835
12836 Scop := Current_Scope;
12837 while Scop /= Standard_Standard loop
12838 T2 := First_Entity (Scop);
12839 while Present (T2) loop
12840 if Is_Fixed_Point_Type (T2)
12841 and then Current_Entity (T2) = T2
12842 and then Scope (Base_Type (T2)) = Scop
12843 then
12844 if Present (T1) then
12845 Fixed_Point_Error (T1, T2);
12846 return Any_Type;
12847 else
12848 T1 := T2;
12849 end if;
12850 end if;
12851
12852 Next_Entity (T2);
12853 end loop;
12854
12855 Scop := Scope (Scop);
12856 end loop;
12857
12858 -- Look for visible fixed type declarations in the context
12859
12860 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
12861 while Present (Item) loop
12862 if Nkind (Item) = N_With_Clause then
12863 Scop := Entity (Name (Item));
12864 T2 := First_Entity (Scop);
12865 while Present (T2) loop
12866 if Is_Fixed_Point_Type (T2)
12867 and then Scope (Base_Type (T2)) = Scop
12868 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
12869 then
12870 if Present (T1) then
12871 Fixed_Point_Error (T1, T2);
12872 return Any_Type;
12873 else
12874 T1 := T2;
12875 end if;
12876 end if;
12877
12878 Next_Entity (T2);
12879 end loop;
12880 end if;
12881
12882 Next (Item);
12883 end loop;
12884
12885 if Nkind (N) = N_Real_Literal then
12886 Error_Msg_NE ("??real literal interpreted as }!", N, T1);
12887
12888 else
12889 -- When the context is a type conversion, issue the warning on the
12890 -- expression of the conversion because it is the actual operation.
12891
12892 if Nkind (N) in N_Type_Conversion | N_Unchecked_Type_Conversion then
12893 ErrN := Expression (N);
12894 else
12895 ErrN := N;
12896 end if;
12897
12898 Error_Msg_NE
12899 ("??universal_fixed expression interpreted as }!", ErrN, T1);
12900 end if;
12901
12902 return T1;
12903 end Unique_Fixed_Point_Type;
12904
12905 ----------------------
12906 -- Valid_Conversion --
12907 ----------------------
12908
12909 function Valid_Conversion
12910 (N : Node_Id;
12911 Target : Entity_Id;
12912 Operand : Node_Id;
12913 Report_Errs : Boolean := True) return Boolean
12914 is
12915 Target_Type : constant Entity_Id := Base_Type (Target);
12916 Opnd_Type : Entity_Id := Etype (Operand);
12917 Inc_Ancestor : Entity_Id;
12918
12919 function Conversion_Check
12920 (Valid : Boolean;
12921 Msg : String) return Boolean;
12922 -- Little routine to post Msg if Valid is False, returns Valid value
12923
12924 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id);
12925 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
12926
12927 procedure Conversion_Error_NE
12928 (Msg : String;
12929 N : Node_Or_Entity_Id;
12930 E : Node_Or_Entity_Id);
12931 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
12932
12933 function In_Instance_Code return Boolean;
12934 -- Return True if expression is within an instance but is not in one of
12935 -- the actuals of the instantiation. Type conversions within an instance
12936 -- are not rechecked because type visbility may lead to spurious errors,
12937 -- but conversions in an actual for a formal object must be checked.
12938
12939 function Is_Discrim_Of_Bad_Access_Conversion_Argument
12940 (Expr : Node_Id) return Boolean;
12941 -- Implicit anonymous-to-named access type conversions are not allowed
12942 -- if the "statically deeper than" relationship does not apply to the
12943 -- type of the conversion operand. See RM 8.6(28.1) and AARM 8.6(28.d).
12944 -- We deal with most such cases elsewhere so that we can emit more
12945 -- specific error messages (e.g., if the operand is an access parameter
12946 -- or a saooaaat (stand-alone object of an anonymous access type)), but
12947 -- here is where we catch the case where the operand is an access
12948 -- discriminant selected from a dereference of another such "bad"
12949 -- conversion argument.
12950
12951 function Valid_Tagged_Conversion
12952 (Target_Type : Entity_Id;
12953 Opnd_Type : Entity_Id) return Boolean;
12954 -- Specifically test for validity of tagged conversions
12955
12956 function Valid_Array_Conversion return Boolean;
12957 -- Check index and component conformance, and accessibility levels if
12958 -- the component types are anonymous access types (Ada 2005).
12959
12960 ----------------------
12961 -- Conversion_Check --
12962 ----------------------
12963
12964 function Conversion_Check
12965 (Valid : Boolean;
12966 Msg : String) return Boolean
12967 is
12968 begin
12969 if not Valid
12970
12971 -- A generic unit has already been analyzed and we have verified
12972 -- that a particular conversion is OK in that context. Since the
12973 -- instance is reanalyzed without relying on the relationships
12974 -- established during the analysis of the generic, it is possible
12975 -- to end up with inconsistent views of private types. Do not emit
12976 -- the error message in such cases. The rest of the machinery in
12977 -- Valid_Conversion still ensures the proper compatibility of
12978 -- target and operand types.
12979
12980 and then not In_Instance_Code
12981 then
12982 Conversion_Error_N (Msg, Operand);
12983 end if;
12984
12985 return Valid;
12986 end Conversion_Check;
12987
12988 ------------------------
12989 -- Conversion_Error_N --
12990 ------------------------
12991
12992 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id) is
12993 begin
12994 if Report_Errs then
12995 Error_Msg_N (Msg, N);
12996 end if;
12997 end Conversion_Error_N;
12998
12999 -------------------------
13000 -- Conversion_Error_NE --
13001 -------------------------
13002
13003 procedure Conversion_Error_NE
13004 (Msg : String;
13005 N : Node_Or_Entity_Id;
13006 E : Node_Or_Entity_Id)
13007 is
13008 begin
13009 if Report_Errs then
13010 Error_Msg_NE (Msg, N, E);
13011 end if;
13012 end Conversion_Error_NE;
13013
13014 ----------------------
13015 -- In_Instance_Code --
13016 ----------------------
13017
13018 function In_Instance_Code return Boolean is
13019 Par : Node_Id;
13020
13021 begin
13022 if not In_Instance then
13023 return False;
13024
13025 else
13026 Par := Parent (N);
13027 while Present (Par) loop
13028
13029 -- The expression is part of an actual object if it appears in
13030 -- the generated object declaration in the instance.
13031
13032 if Nkind (Par) = N_Object_Declaration
13033 and then Present (Corresponding_Generic_Association (Par))
13034 then
13035 return False;
13036
13037 else
13038 exit when
13039 Nkind (Par) in N_Statement_Other_Than_Procedure_Call
13040 or else Nkind (Par) in N_Subprogram_Call
13041 or else Nkind (Par) in N_Declaration;
13042 end if;
13043
13044 Par := Parent (Par);
13045 end loop;
13046
13047 -- Otherwise the expression appears within the instantiated unit
13048
13049 return True;
13050 end if;
13051 end In_Instance_Code;
13052
13053 --------------------------------------------------
13054 -- Is_Discrim_Of_Bad_Access_Conversion_Argument --
13055 --------------------------------------------------
13056
13057 function Is_Discrim_Of_Bad_Access_Conversion_Argument
13058 (Expr : Node_Id) return Boolean
13059 is
13060 Exp_Type : Entity_Id := Base_Type (Etype (Expr));
13061 pragma Assert (Is_Access_Type (Exp_Type));
13062
13063 Associated_Node : Node_Id;
13064 Deref_Prefix : Node_Id;
13065 begin
13066 if not Is_Anonymous_Access_Type (Exp_Type) then
13067 return False;
13068 end if;
13069
13070 pragma Assert (Is_Itype (Exp_Type));
13071 Associated_Node := Associated_Node_For_Itype (Exp_Type);
13072
13073 if Nkind (Associated_Node) /= N_Discriminant_Specification then
13074 return False; -- not the type of an access discriminant
13075 end if;
13076
13077 -- return False if Expr not of form <prefix>.all.Some_Component
13078
13079 if (Nkind (Expr) /= N_Selected_Component)
13080 or else (Nkind (Prefix (Expr)) /= N_Explicit_Dereference)
13081 then
13082 -- conditional expressions, declare expressions ???
13083 return False;
13084 end if;
13085
13086 Deref_Prefix := Prefix (Prefix (Expr));
13087 Exp_Type := Base_Type (Etype (Deref_Prefix));
13088
13089 -- The "statically deeper relationship" does not apply
13090 -- to generic formal access types, so a prefix of such
13091 -- a type is a "bad" prefix.
13092
13093 if Is_Generic_Formal (Exp_Type) then
13094 return True;
13095
13096 -- The "statically deeper relationship" does apply to
13097 -- any other named access type.
13098
13099 elsif not Is_Anonymous_Access_Type (Exp_Type) then
13100 return False;
13101 end if;
13102
13103 pragma Assert (Is_Itype (Exp_Type));
13104 Associated_Node := Associated_Node_For_Itype (Exp_Type);
13105
13106 -- The "statically deeper relationship" applies to some
13107 -- anonymous access types and not to others. Return
13108 -- True for the cases where it does not apply. Also check
13109 -- recursively for the
13110 -- <prefix>.all.Access_Discrim.all.Access_Discrim case,
13111 -- where the correct result depends on <prefix>.
13112
13113 return Nkind (Associated_Node) in
13114 N_Procedure_Specification | -- access parameter
13115 N_Function_Specification | -- access parameter
13116 N_Object_Declaration -- saooaaat
13117 or else Is_Discrim_Of_Bad_Access_Conversion_Argument (Deref_Prefix);
13118 end Is_Discrim_Of_Bad_Access_Conversion_Argument;
13119
13120 ----------------------------
13121 -- Valid_Array_Conversion --
13122 ----------------------------
13123
13124 function Valid_Array_Conversion return Boolean is
13125 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
13126 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
13127
13128 Opnd_Index : Node_Id;
13129 Opnd_Index_Type : Entity_Id;
13130
13131 Target_Comp_Type : constant Entity_Id :=
13132 Component_Type (Target_Type);
13133 Target_Comp_Base : constant Entity_Id :=
13134 Base_Type (Target_Comp_Type);
13135
13136 Target_Index : Node_Id;
13137 Target_Index_Type : Entity_Id;
13138
13139 begin
13140 -- Error if wrong number of dimensions
13141
13142 if
13143 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
13144 then
13145 Conversion_Error_N
13146 ("incompatible number of dimensions for conversion", Operand);
13147 return False;
13148
13149 -- Number of dimensions matches
13150
13151 else
13152 -- Loop through indexes of the two arrays
13153
13154 Target_Index := First_Index (Target_Type);
13155 Opnd_Index := First_Index (Opnd_Type);
13156 while Present (Target_Index) and then Present (Opnd_Index) loop
13157 Target_Index_Type := Etype (Target_Index);
13158 Opnd_Index_Type := Etype (Opnd_Index);
13159
13160 -- Error if index types are incompatible
13161
13162 if not (Is_Integer_Type (Target_Index_Type)
13163 and then Is_Integer_Type (Opnd_Index_Type))
13164 and then (Root_Type (Target_Index_Type)
13165 /= Root_Type (Opnd_Index_Type))
13166 then
13167 Conversion_Error_N
13168 ("incompatible index types for array conversion",
13169 Operand);
13170 return False;
13171 end if;
13172
13173 Next_Index (Target_Index);
13174 Next_Index (Opnd_Index);
13175 end loop;
13176
13177 -- If component types have same base type, all set
13178
13179 if Target_Comp_Base = Opnd_Comp_Base then
13180 null;
13181
13182 -- Here if base types of components are not the same. The only
13183 -- time this is allowed is if we have anonymous access types.
13184
13185 -- The conversion of arrays of anonymous access types can lead
13186 -- to dangling pointers. AI-392 formalizes the accessibility
13187 -- checks that must be applied to such conversions to prevent
13188 -- out-of-scope references.
13189
13190 elsif Ekind (Target_Comp_Base) in
13191 E_Anonymous_Access_Type
13192 | E_Anonymous_Access_Subprogram_Type
13193 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
13194 and then
13195 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
13196 then
13197 if Type_Access_Level (Target_Type) <
13198 Deepest_Type_Access_Level (Opnd_Type)
13199 then
13200 if In_Instance_Body then
13201 Error_Msg_Warn := SPARK_Mode /= On;
13202 Conversion_Error_N
13203 ("source array type has deeper accessibility "
13204 & "level than target<<", Operand);
13205 Conversion_Error_N ("\Program_Error [<<", Operand);
13206 Rewrite (N,
13207 Make_Raise_Program_Error (Sloc (N),
13208 Reason => PE_Accessibility_Check_Failed));
13209 Set_Etype (N, Target_Type);
13210 return False;
13211
13212 -- Conversion not allowed because of accessibility levels
13213
13214 else
13215 Conversion_Error_N
13216 ("source array type has deeper accessibility "
13217 & "level than target", Operand);
13218 return False;
13219 end if;
13220
13221 else
13222 null;
13223 end if;
13224
13225 -- All other cases where component base types do not match
13226
13227 else
13228 Conversion_Error_N
13229 ("incompatible component types for array conversion",
13230 Operand);
13231 return False;
13232 end if;
13233
13234 -- Check that component subtypes statically match. For numeric
13235 -- types this means that both must be either constrained or
13236 -- unconstrained. For enumeration types the bounds must match.
13237 -- All of this is checked in Subtypes_Statically_Match.
13238
13239 if not Subtypes_Statically_Match
13240 (Target_Comp_Type, Opnd_Comp_Type)
13241 then
13242 Conversion_Error_N
13243 ("component subtypes must statically match", Operand);
13244 return False;
13245 end if;
13246 end if;
13247
13248 return True;
13249 end Valid_Array_Conversion;
13250
13251 -----------------------------
13252 -- Valid_Tagged_Conversion --
13253 -----------------------------
13254
13255 function Valid_Tagged_Conversion
13256 (Target_Type : Entity_Id;
13257 Opnd_Type : Entity_Id) return Boolean
13258 is
13259 begin
13260 -- Upward conversions are allowed (RM 4.6(22))
13261
13262 if Covers (Target_Type, Opnd_Type)
13263 or else Is_Ancestor (Target_Type, Opnd_Type)
13264 then
13265 return True;
13266
13267 -- Downward conversion are allowed if the operand is class-wide
13268 -- (RM 4.6(23)).
13269
13270 elsif Is_Class_Wide_Type (Opnd_Type)
13271 and then Covers (Opnd_Type, Target_Type)
13272 then
13273 return True;
13274
13275 elsif Covers (Opnd_Type, Target_Type)
13276 or else Is_Ancestor (Opnd_Type, Target_Type)
13277 then
13278 return
13279 Conversion_Check (False,
13280 "downward conversion of tagged objects not allowed");
13281
13282 -- Ada 2005 (AI-251): The conversion to/from interface types is
13283 -- always valid. The types involved may be class-wide (sub)types.
13284
13285 elsif Is_Interface (Etype (Base_Type (Target_Type)))
13286 or else Is_Interface (Etype (Base_Type (Opnd_Type)))
13287 then
13288 return True;
13289
13290 -- If the operand is a class-wide type obtained through a limited_
13291 -- with clause, and the context includes the nonlimited view, use
13292 -- it to determine whether the conversion is legal.
13293
13294 elsif Is_Class_Wide_Type (Opnd_Type)
13295 and then From_Limited_With (Opnd_Type)
13296 and then Present (Non_Limited_View (Etype (Opnd_Type)))
13297 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
13298 then
13299 return True;
13300
13301 elsif Is_Access_Type (Opnd_Type)
13302 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
13303 then
13304 return True;
13305
13306 else
13307 Conversion_Error_NE
13308 ("invalid tagged conversion, not compatible with}",
13309 N, First_Subtype (Opnd_Type));
13310 return False;
13311 end if;
13312 end Valid_Tagged_Conversion;
13313
13314 -- Start of processing for Valid_Conversion
13315
13316 begin
13317 Check_Parameterless_Call (Operand);
13318
13319 if Is_Overloaded (Operand) then
13320 declare
13321 I : Interp_Index;
13322 I1 : Interp_Index;
13323 It : Interp;
13324 It1 : Interp;
13325 N1 : Entity_Id;
13326 T1 : Entity_Id;
13327
13328 begin
13329 -- Remove procedure calls, which syntactically cannot appear in
13330 -- this context, but which cannot be removed by type checking,
13331 -- because the context does not impose a type.
13332
13333 -- The node may be labelled overloaded, but still contain only one
13334 -- interpretation because others were discarded earlier. If this
13335 -- is the case, retain the single interpretation if legal.
13336
13337 Get_First_Interp (Operand, I, It);
13338 Opnd_Type := It.Typ;
13339 Get_Next_Interp (I, It);
13340
13341 if Present (It.Typ)
13342 and then Opnd_Type /= Standard_Void_Type
13343 then
13344 -- More than one candidate interpretation is available
13345
13346 Get_First_Interp (Operand, I, It);
13347 while Present (It.Typ) loop
13348 if It.Typ = Standard_Void_Type then
13349 Remove_Interp (I);
13350 end if;
13351
13352 -- When compiling for a system where Address is of a visible
13353 -- integer type, spurious ambiguities can be produced when
13354 -- arithmetic operations have a literal operand and return
13355 -- System.Address or a descendant of it. These ambiguities
13356 -- are usually resolved by the context, but for conversions
13357 -- there is no context type and the removal of the spurious
13358 -- operations must be done explicitly here.
13359
13360 if not Address_Is_Private
13361 and then Is_Descendant_Of_Address (It.Typ)
13362 then
13363 Remove_Interp (I);
13364 end if;
13365
13366 Get_Next_Interp (I, It);
13367 end loop;
13368 end if;
13369
13370 Get_First_Interp (Operand, I, It);
13371 I1 := I;
13372 It1 := It;
13373
13374 if No (It.Typ) then
13375 Conversion_Error_N ("illegal operand in conversion", Operand);
13376 return False;
13377 end if;
13378
13379 Get_Next_Interp (I, It);
13380
13381 if Present (It.Typ) then
13382 N1 := It1.Nam;
13383 T1 := It1.Typ;
13384 It1 := Disambiguate (Operand, I1, I, Any_Type);
13385
13386 if It1 = No_Interp then
13387 Conversion_Error_N
13388 ("ambiguous operand in conversion", Operand);
13389
13390 -- If the interpretation involves a standard operator, use
13391 -- the location of the type, which may be user-defined.
13392
13393 if Sloc (It.Nam) = Standard_Location then
13394 Error_Msg_Sloc := Sloc (It.Typ);
13395 else
13396 Error_Msg_Sloc := Sloc (It.Nam);
13397 end if;
13398
13399 Conversion_Error_N -- CODEFIX
13400 ("\\possible interpretation#!", Operand);
13401
13402 if Sloc (N1) = Standard_Location then
13403 Error_Msg_Sloc := Sloc (T1);
13404 else
13405 Error_Msg_Sloc := Sloc (N1);
13406 end if;
13407
13408 Conversion_Error_N -- CODEFIX
13409 ("\\possible interpretation#!", Operand);
13410
13411 return False;
13412 end if;
13413 end if;
13414
13415 Set_Etype (Operand, It1.Typ);
13416 Opnd_Type := It1.Typ;
13417 end;
13418 end if;
13419
13420 -- Deal with conversion of integer type to address if the pragma
13421 -- Allow_Integer_Address is in effect. We convert the conversion to
13422 -- an unchecked conversion in this case and we are all done.
13423
13424 if Address_Integer_Convert_OK (Opnd_Type, Target_Type) then
13425 Rewrite (N, Unchecked_Convert_To (Target_Type, Expression (N)));
13426 Analyze_And_Resolve (N, Target_Type);
13427 return True;
13428 end if;
13429
13430 -- If we are within a child unit, check whether the type of the
13431 -- expression has an ancestor in a parent unit, in which case it
13432 -- belongs to its derivation class even if the ancestor is private.
13433 -- See RM 7.3.1 (5.2/3).
13434
13435 Inc_Ancestor := Get_Incomplete_View_Of_Ancestor (Opnd_Type);
13436
13437 -- Numeric types
13438
13439 if Is_Numeric_Type (Target_Type) then
13440
13441 -- A universal fixed expression can be converted to any numeric type
13442
13443 if Opnd_Type = Universal_Fixed then
13444 return True;
13445
13446 -- Also no need to check when in an instance or inlined body, because
13447 -- the legality has been established when the template was analyzed.
13448 -- Furthermore, numeric conversions may occur where only a private
13449 -- view of the operand type is visible at the instantiation point.
13450 -- This results in a spurious error if we check that the operand type
13451 -- is a numeric type.
13452
13453 -- Note: in a previous version of this unit, the following tests were
13454 -- applied only for generated code (Comes_From_Source set to False),
13455 -- but in fact the test is required for source code as well, since
13456 -- this situation can arise in source code.
13457
13458 elsif In_Instance_Code or else In_Inlined_Body then
13459 return True;
13460
13461 -- Otherwise we need the conversion check
13462
13463 else
13464 return Conversion_Check
13465 (Is_Numeric_Type (Opnd_Type)
13466 or else
13467 (Present (Inc_Ancestor)
13468 and then Is_Numeric_Type (Inc_Ancestor)),
13469 "illegal operand for numeric conversion");
13470 end if;
13471
13472 -- Array types
13473
13474 elsif Is_Array_Type (Target_Type) then
13475 if not Is_Array_Type (Opnd_Type)
13476 or else Opnd_Type = Any_Composite
13477 or else Opnd_Type = Any_String
13478 then
13479 Conversion_Error_N
13480 ("illegal operand for array conversion", Operand);
13481 return False;
13482
13483 else
13484 return Valid_Array_Conversion;
13485 end if;
13486
13487 -- Ada 2005 (AI-251): Internally generated conversions of access to
13488 -- interface types added to force the displacement of the pointer to
13489 -- reference the corresponding dispatch table.
13490
13491 elsif not Comes_From_Source (N)
13492 and then Is_Access_Type (Target_Type)
13493 and then Is_Interface (Designated_Type (Target_Type))
13494 then
13495 return True;
13496
13497 -- Ada 2005 (AI-251): Anonymous access types where target references an
13498 -- interface type.
13499
13500 elsif Is_Access_Type (Opnd_Type)
13501 and then Ekind (Target_Type) in
13502 E_General_Access_Type | E_Anonymous_Access_Type
13503 and then Is_Interface (Directly_Designated_Type (Target_Type))
13504 then
13505 -- Check the static accessibility rule of 4.6(17). Note that the
13506 -- check is not enforced when within an instance body, since the
13507 -- RM requires such cases to be caught at run time.
13508
13509 -- If the operand is a rewriting of an allocator no check is needed
13510 -- because there are no accessibility issues.
13511
13512 if Nkind (Original_Node (N)) = N_Allocator then
13513 null;
13514
13515 elsif Ekind (Target_Type) /= E_Anonymous_Access_Type then
13516 if Type_Access_Level (Opnd_Type) >
13517 Deepest_Type_Access_Level (Target_Type)
13518 then
13519 -- In an instance, this is a run-time check, but one we know
13520 -- will fail, so generate an appropriate warning. The raise
13521 -- will be generated by Expand_N_Type_Conversion.
13522
13523 if In_Instance_Body then
13524 Error_Msg_Warn := SPARK_Mode /= On;
13525 Conversion_Error_N
13526 ("cannot convert local pointer to non-local access type<<",
13527 Operand);
13528 Conversion_Error_N ("\Program_Error [<<", Operand);
13529
13530 else
13531 Conversion_Error_N
13532 ("cannot convert local pointer to non-local access type",
13533 Operand);
13534 return False;
13535 end if;
13536
13537 -- Special accessibility checks are needed in the case of access
13538 -- discriminants declared for a limited type.
13539
13540 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
13541 and then not Is_Local_Anonymous_Access (Opnd_Type)
13542 then
13543 -- When the operand is a selected access discriminant the check
13544 -- needs to be made against the level of the object denoted by
13545 -- the prefix of the selected name (Accessibility_Level handles
13546 -- checking the prefix of the operand for this case).
13547
13548 if Nkind (Operand) = N_Selected_Component
13549 and then Static_Accessibility_Level
13550 (Operand, Zero_On_Dynamic_Level)
13551 > Deepest_Type_Access_Level (Target_Type)
13552 then
13553 -- In an instance, this is a run-time check, but one we know
13554 -- will fail, so generate an appropriate warning. The raise
13555 -- will be generated by Expand_N_Type_Conversion.
13556
13557 if In_Instance_Body then
13558 Error_Msg_Warn := SPARK_Mode /= On;
13559 Conversion_Error_N
13560 ("cannot convert access discriminant to non-local "
13561 & "access type<<", Operand);
13562 Conversion_Error_N ("\Program_Error [<<", Operand);
13563
13564 -- Real error if not in instance body
13565
13566 else
13567 Conversion_Error_N
13568 ("cannot convert access discriminant to non-local "
13569 & "access type", Operand);
13570 return False;
13571 end if;
13572 end if;
13573
13574 -- The case of a reference to an access discriminant from
13575 -- within a limited type declaration (which will appear as
13576 -- a discriminal) is always illegal because the level of the
13577 -- discriminant is considered to be deeper than any (nameable)
13578 -- access type.
13579
13580 if Is_Entity_Name (Operand)
13581 and then not Is_Local_Anonymous_Access (Opnd_Type)
13582 and then
13583 Ekind (Entity (Operand)) in E_In_Parameter | E_Constant
13584 and then Present (Discriminal_Link (Entity (Operand)))
13585 then
13586 Conversion_Error_N
13587 ("discriminant has deeper accessibility level than target",
13588 Operand);
13589 return False;
13590 end if;
13591 end if;
13592 end if;
13593
13594 return True;
13595
13596 -- General and anonymous access types
13597
13598 elsif Ekind (Target_Type) in
13599 E_General_Access_Type | E_Anonymous_Access_Type
13600 and then
13601 Conversion_Check
13602 (Is_Access_Type (Opnd_Type)
13603 and then
13604 Ekind (Opnd_Type) not in
13605 E_Access_Subprogram_Type |
13606 E_Access_Protected_Subprogram_Type,
13607 "must be an access-to-object type")
13608 then
13609 if Is_Access_Constant (Opnd_Type)
13610 and then not Is_Access_Constant (Target_Type)
13611 then
13612 Conversion_Error_N
13613 ("access-to-constant operand type not allowed", Operand);
13614 return False;
13615 end if;
13616
13617 -- Check the static accessibility rule of 4.6(17). Note that the
13618 -- check is not enforced when within an instance body, since the RM
13619 -- requires such cases to be caught at run time.
13620
13621 if Ekind (Target_Type) /= E_Anonymous_Access_Type
13622 or else Is_Local_Anonymous_Access (Target_Type)
13623 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
13624 N_Object_Declaration
13625 then
13626 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
13627 -- conversions from an anonymous access type to a named general
13628 -- access type. Such conversions are not allowed in the case of
13629 -- access parameters and stand-alone objects of an anonymous
13630 -- access type. The implicit conversion case is recognized by
13631 -- testing that Comes_From_Source is False and that it's been
13632 -- rewritten. The Comes_From_Source test isn't sufficient because
13633 -- nodes in inlined calls to predefined library routines can have
13634 -- Comes_From_Source set to False. (Is there a better way to test
13635 -- for implicit conversions???).
13636 --
13637 -- Do not treat a rewritten 'Old attribute reference like other
13638 -- rewrite substitutions. This makes a difference, for example,
13639 -- in the case where we are generating the expansion of a
13640 -- membership test of the form
13641 -- Saooaaat'Old in Named_Access_Type
13642 -- because in this case Valid_Conversion needs to return True
13643 -- (otherwise the expansion will be False - see the call site
13644 -- in exp_ch4.adb).
13645
13646 if Ada_Version >= Ada_2012
13647 and then not Comes_From_Source (N)
13648 and then Is_Rewrite_Substitution (N)
13649 and then not Is_Attribute_Old (Original_Node (N))
13650 and then Ekind (Base_Type (Target_Type)) = E_General_Access_Type
13651 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
13652 then
13653 if Is_Itype (Opnd_Type) then
13654
13655 -- Implicit conversions aren't allowed for objects of an
13656 -- anonymous access type, since such objects have nonstatic
13657 -- levels in Ada 2012.
13658
13659 if Nkind (Associated_Node_For_Itype (Opnd_Type)) =
13660 N_Object_Declaration
13661 then
13662 Conversion_Error_N
13663 ("implicit conversion of stand-alone anonymous "
13664 & "access object not allowed", Operand);
13665 return False;
13666
13667 -- Implicit conversions aren't allowed for anonymous access
13668 -- parameters. We exclude anonymous access results as well
13669 -- as universal_access "=".
13670
13671 elsif not Is_Local_Anonymous_Access (Opnd_Type)
13672 and then Nkind (Associated_Node_For_Itype (Opnd_Type)) in
13673 N_Function_Specification |
13674 N_Procedure_Specification
13675 and then Nkind (Parent (N)) not in N_Op_Eq | N_Op_Ne
13676 then
13677 Conversion_Error_N
13678 ("implicit conversion of anonymous access parameter "
13679 & "not allowed", Operand);
13680 return False;
13681
13682 -- Detect access discriminant values that are illegal
13683 -- implicit anonymous-to-named access conversion operands.
13684
13685 elsif Is_Discrim_Of_Bad_Access_Conversion_Argument (Operand)
13686 then
13687 Conversion_Error_N
13688 ("implicit conversion of anonymous access value "
13689 & "not allowed", Operand);
13690 return False;
13691
13692 -- In other cases, the level of the operand's type must be
13693 -- statically less deep than that of the target type, else
13694 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
13695
13696 elsif Type_Access_Level (Opnd_Type) >
13697 Deepest_Type_Access_Level (Target_Type)
13698 then
13699 Conversion_Error_N
13700 ("implicit conversion of anonymous access value "
13701 & "violates accessibility", Operand);
13702 return False;
13703 end if;
13704 end if;
13705
13706 -- Check if the operand is deeper than the target type, taking
13707 -- care to avoid the case where we are converting a result of a
13708 -- function returning an anonymous access type since the "master
13709 -- of the call" would be target type of the conversion unless
13710 -- the target type is anonymous access as well - see RM 3.10.2
13711 -- (10.3/3).
13712
13713 elsif Type_Access_Level (Opnd_Type) >
13714 Deepest_Type_Access_Level (Target_Type)
13715 and then (Nkind (Associated_Node_For_Itype (Opnd_Type)) /=
13716 N_Function_Specification
13717 or else Ekind (Target_Type) in
13718 Anonymous_Access_Kind)
13719
13720 -- Check we are not in a return value ???
13721
13722 and then (not In_Return_Value (N)
13723 or else
13724 Nkind (Associated_Node_For_Itype (Target_Type))
13725 = N_Component_Declaration)
13726 then
13727 -- In an instance, this is a run-time check, but one we know
13728 -- will fail, so generate an appropriate warning. The raise
13729 -- will be generated by Expand_N_Type_Conversion.
13730
13731 if In_Instance_Body then
13732 Error_Msg_Warn := SPARK_Mode /= On;
13733 Conversion_Error_N
13734 ("cannot convert local pointer to non-local access type<<",
13735 Operand);
13736 Conversion_Error_N ("\Program_Error [<<", Operand);
13737
13738 -- If not in an instance body, this is a real error
13739
13740 else
13741 -- Avoid generation of spurious error message
13742
13743 if not Error_Posted (N) then
13744 Conversion_Error_N
13745 ("cannot convert local pointer to non-local access type",
13746 Operand);
13747 end if;
13748
13749 return False;
13750 end if;
13751
13752 -- Special accessibility checks are needed in the case of access
13753 -- discriminants declared for a limited type.
13754
13755 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
13756 and then not Is_Local_Anonymous_Access (Opnd_Type)
13757 then
13758 -- When the operand is a selected access discriminant the check
13759 -- needs to be made against the level of the object denoted by
13760 -- the prefix of the selected name (Accessibility_Level handles
13761 -- checking the prefix of the operand for this case).
13762
13763 if Nkind (Operand) = N_Selected_Component
13764 and then Static_Accessibility_Level
13765 (Operand, Zero_On_Dynamic_Level)
13766 > Deepest_Type_Access_Level (Target_Type)
13767 then
13768 -- In an instance, this is a run-time check, but one we know
13769 -- will fail, so generate an appropriate warning. The raise
13770 -- will be generated by Expand_N_Type_Conversion.
13771
13772 if In_Instance_Body then
13773 Error_Msg_Warn := SPARK_Mode /= On;
13774 Conversion_Error_N
13775 ("cannot convert access discriminant to non-local "
13776 & "access type<<", Operand);
13777 Conversion_Error_N ("\Program_Error [<<", Operand);
13778
13779 -- If not in an instance body, this is a real error
13780
13781 else
13782 Conversion_Error_N
13783 ("cannot convert access discriminant to non-local "
13784 & "access type", Operand);
13785 return False;
13786 end if;
13787 end if;
13788
13789 -- The case of a reference to an access discriminant from
13790 -- within a limited type declaration (which will appear as
13791 -- a discriminal) is always illegal because the level of the
13792 -- discriminant is considered to be deeper than any (nameable)
13793 -- access type.
13794
13795 if Is_Entity_Name (Operand)
13796 and then
13797 Ekind (Entity (Operand)) in E_In_Parameter | E_Constant
13798 and then Present (Discriminal_Link (Entity (Operand)))
13799 then
13800 Conversion_Error_N
13801 ("discriminant has deeper accessibility level than target",
13802 Operand);
13803 return False;
13804 end if;
13805 end if;
13806 end if;
13807
13808 -- In the presence of limited_with clauses we have to use nonlimited
13809 -- views, if available.
13810
13811 Check_Limited : declare
13812 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
13813 -- Helper function to handle limited views
13814
13815 --------------------------
13816 -- Full_Designated_Type --
13817 --------------------------
13818
13819 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
13820 Desig : constant Entity_Id := Designated_Type (T);
13821
13822 begin
13823 -- Handle the limited view of a type
13824
13825 if From_Limited_With (Desig)
13826 and then Has_Non_Limited_View (Desig)
13827 then
13828 return Available_View (Desig);
13829 else
13830 return Desig;
13831 end if;
13832 end Full_Designated_Type;
13833
13834 -- Local Declarations
13835
13836 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
13837 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
13838
13839 Same_Base : constant Boolean :=
13840 Base_Type (Target) = Base_Type (Opnd);
13841
13842 -- Start of processing for Check_Limited
13843
13844 begin
13845 if Is_Tagged_Type (Target) then
13846 return Valid_Tagged_Conversion (Target, Opnd);
13847
13848 else
13849 if not Same_Base then
13850 Conversion_Error_NE
13851 ("target designated type not compatible with }",
13852 N, Base_Type (Opnd));
13853 return False;
13854
13855 -- Ada 2005 AI-384: legality rule is symmetric in both
13856 -- designated types. The conversion is legal (with possible
13857 -- constraint check) if either designated type is
13858 -- unconstrained.
13859
13860 elsif Subtypes_Statically_Match (Target, Opnd)
13861 or else
13862 (Has_Discriminants (Target)
13863 and then
13864 (not Is_Constrained (Opnd)
13865 or else not Is_Constrained (Target)))
13866 then
13867 -- Special case, if Value_Size has been used to make the
13868 -- sizes different, the conversion is not allowed even
13869 -- though the subtypes statically match.
13870
13871 if Known_Static_RM_Size (Target)
13872 and then Known_Static_RM_Size (Opnd)
13873 and then RM_Size (Target) /= RM_Size (Opnd)
13874 then
13875 Conversion_Error_NE
13876 ("target designated subtype not compatible with }",
13877 N, Opnd);
13878 Conversion_Error_NE
13879 ("\because sizes of the two designated subtypes differ",
13880 N, Opnd);
13881 return False;
13882
13883 -- Normal case where conversion is allowed
13884
13885 else
13886 return True;
13887 end if;
13888
13889 else
13890 Error_Msg_NE
13891 ("target designated subtype not compatible with }",
13892 N, Opnd);
13893 return False;
13894 end if;
13895 end if;
13896 end Check_Limited;
13897
13898 -- Access to subprogram types. If the operand is an access parameter,
13899 -- the type has a deeper accessibility that any master, and cannot be
13900 -- assigned. We must make an exception if the conversion is part of an
13901 -- assignment and the target is the return object of an extended return
13902 -- statement, because in that case the accessibility check takes place
13903 -- after the return.
13904
13905 elsif Is_Access_Subprogram_Type (Target_Type)
13906
13907 -- Note: this test of Opnd_Type is there to prevent entering this
13908 -- branch in the case of a remote access to subprogram type, which
13909 -- is internally represented as an E_Record_Type.
13910
13911 and then Is_Access_Type (Opnd_Type)
13912 then
13913 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
13914 and then Is_Entity_Name (Operand)
13915 and then Ekind (Entity (Operand)) = E_In_Parameter
13916 and then
13917 (Nkind (Parent (N)) /= N_Assignment_Statement
13918 or else not Is_Entity_Name (Name (Parent (N)))
13919 or else not Is_Return_Object (Entity (Name (Parent (N)))))
13920 then
13921 Conversion_Error_N
13922 ("illegal attempt to store anonymous access to subprogram",
13923 Operand);
13924 Conversion_Error_N
13925 ("\value has deeper accessibility than any master "
13926 & "(RM 3.10.2 (13))",
13927 Operand);
13928
13929 Error_Msg_NE
13930 ("\use named access type for& instead of access parameter",
13931 Operand, Entity (Operand));
13932 end if;
13933
13934 -- Check that the designated types are subtype conformant
13935
13936 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
13937 Old_Id => Designated_Type (Opnd_Type),
13938 Err_Loc => N);
13939
13940 -- Check the static accessibility rule of 4.6(20)
13941
13942 if Type_Access_Level (Opnd_Type) >
13943 Deepest_Type_Access_Level (Target_Type)
13944 then
13945 Conversion_Error_N
13946 ("operand type has deeper accessibility level than target",
13947 Operand);
13948
13949 -- Check that if the operand type is declared in a generic body,
13950 -- then the target type must be declared within that same body
13951 -- (enforces last sentence of 4.6(20)).
13952
13953 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
13954 declare
13955 O_Gen : constant Node_Id :=
13956 Enclosing_Generic_Body (Opnd_Type);
13957
13958 T_Gen : Node_Id;
13959
13960 begin
13961 T_Gen := Enclosing_Generic_Body (Target_Type);
13962 while Present (T_Gen) and then T_Gen /= O_Gen loop
13963 T_Gen := Enclosing_Generic_Body (T_Gen);
13964 end loop;
13965
13966 if T_Gen /= O_Gen then
13967 Conversion_Error_N
13968 ("target type must be declared in same generic body "
13969 & "as operand type", N);
13970 end if;
13971 end;
13972 end if;
13973
13974 return True;
13975
13976 -- Remote access to subprogram types
13977
13978 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
13979 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
13980 then
13981 -- It is valid to convert from one RAS type to another provided
13982 -- that their specification statically match.
13983
13984 -- Note: at this point, remote access to subprogram types have been
13985 -- expanded to their E_Record_Type representation, and we need to
13986 -- go back to the original access type definition using the
13987 -- Corresponding_Remote_Type attribute in order to check that the
13988 -- designated profiles match.
13989
13990 pragma Assert (Ekind (Target_Type) = E_Record_Type);
13991 pragma Assert (Ekind (Opnd_Type) = E_Record_Type);
13992
13993 Check_Subtype_Conformant
13994 (New_Id =>
13995 Designated_Type (Corresponding_Remote_Type (Target_Type)),
13996 Old_Id =>
13997 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
13998 Err_Loc =>
13999 N);
14000 return True;
14001
14002 -- If it was legal in the generic, it's legal in the instance
14003
14004 elsif In_Instance_Body then
14005 return True;
14006
14007 -- If both are tagged types, check legality of view conversions
14008
14009 elsif Is_Tagged_Type (Target_Type)
14010 and then
14011 Is_Tagged_Type (Opnd_Type)
14012 then
14013 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
14014
14015 -- Types derived from the same root type are convertible
14016
14017 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
14018 return True;
14019
14020 -- In an instance or an inlined body, there may be inconsistent views of
14021 -- the same type, or of types derived from a common root.
14022
14023 elsif (In_Instance or In_Inlined_Body)
14024 and then
14025 Root_Type (Underlying_Type (Target_Type)) =
14026 Root_Type (Underlying_Type (Opnd_Type))
14027 then
14028 return True;
14029
14030 -- Special check for common access type error case
14031
14032 elsif Ekind (Target_Type) = E_Access_Type
14033 and then Is_Access_Type (Opnd_Type)
14034 then
14035 Conversion_Error_N ("target type must be general access type!", N);
14036 Conversion_Error_NE -- CODEFIX
14037 ("\add ALL to }!", N, Target_Type);
14038 return False;
14039
14040 -- Here we have a real conversion error
14041
14042 else
14043 -- Check for missing regular with_clause when only a limited view of
14044 -- target is available.
14045
14046 if From_Limited_With (Opnd_Type) and then In_Package_Body then
14047 Conversion_Error_NE
14048 ("invalid conversion, not compatible with limited view of }",
14049 N, Opnd_Type);
14050 Conversion_Error_NE
14051 ("\add with_clause for& to current unit!", N, Scope (Opnd_Type));
14052
14053 elsif Is_Access_Type (Opnd_Type)
14054 and then From_Limited_With (Designated_Type (Opnd_Type))
14055 and then In_Package_Body
14056 then
14057 Conversion_Error_NE
14058 ("invalid conversion, not compatible with }", N, Opnd_Type);
14059 Conversion_Error_NE
14060 ("\add with_clause for& to current unit!",
14061 N, Scope (Designated_Type (Opnd_Type)));
14062
14063 else
14064 Conversion_Error_NE
14065 ("invalid conversion, not compatible with }", N, Opnd_Type);
14066 end if;
14067
14068 return False;
14069 end if;
14070 end Valid_Conversion;
14071
14072 end Sem_Res;