Debian Codegraph @

gcc/ada/sem_res.adb

raw
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-2025, 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 Accessibility; use Accessibility;
27 with Aspects; use Aspects;
28 with Atree; use Atree;
29 with Checks; use Checks;
30 with Debug; use Debug;
31 with Debug_A; use Debug_A;
32 with Einfo; use Einfo;
33 with Einfo.Entities; use Einfo.Entities;
34 with Einfo.Utils; use Einfo.Utils;
35 with Elists; use Elists;
36 with Errout; use Errout;
37 with Expander; use Expander;
38 with Exp_Ch6; use Exp_Ch6;
39 with Exp_Ch7; use Exp_Ch7;
40 with Exp_Disp; use Exp_Disp;
41 with Exp_Tss; use Exp_Tss;
42 with Exp_Util; use Exp_Util;
43 with Freeze; use Freeze;
44 with Ghost; use Ghost;
45 with Inline; use Inline;
46 with Itypes; use Itypes;
47 with Lib; use Lib;
48 with Lib.Xref; use Lib.Xref;
49 with Local_Restrict;
50 with Mutably_Tagged; use Mutably_Tagged;
51 with Namet; use Namet;
52 with Nmake; use Nmake;
53 with Nlists; use Nlists;
54 with Opt; use Opt;
55 with Output; use Output;
56 with Par_SCO; use Par_SCO;
57 with Restrict; use Restrict;
58 with Rident; use Rident;
59 with Rtsfind; use Rtsfind;
60 with Sem; use Sem;
61 with Sem_Aggr; use Sem_Aggr;
62 with Sem_Attr; use Sem_Attr;
63 with Sem_Aux; use Sem_Aux;
64 with Sem_Case; use Sem_Case;
65 with Sem_Cat; use Sem_Cat;
66 with Sem_Ch3; use Sem_Ch3;
67 with Sem_Ch4; use Sem_Ch4;
68 with Sem_Ch5; use Sem_Ch5;
69 with Sem_Ch6; use Sem_Ch6;
70 with Sem_Ch8; use Sem_Ch8;
71 with Sem_Ch13; use Sem_Ch13;
72 with Sem_Dim; use Sem_Dim;
73 with Sem_Disp; use Sem_Disp;
74 with Sem_Dist; use Sem_Dist;
75 with Sem_Elab; use Sem_Elab;
76 with Sem_Elim; use Sem_Elim;
77 with Sem_Eval; use Sem_Eval;
78 with Sem_Intr; use Sem_Intr;
79 with Sem_Mech; use Sem_Mech;
80 with Sem_Type; use Sem_Type;
81 with Sem_Util; use Sem_Util;
82 with Sem_Warn; use Sem_Warn;
83 with Sinfo; use Sinfo;
84 with Sinfo.Nodes; use Sinfo.Nodes;
85 with Sinfo.Utils; use Sinfo.Utils;
86 with Sinfo.CN; use Sinfo.CN;
87 with Sinput; use Sinput;
88 with Snames; use Snames;
89 with Stand; use Stand;
90 with Stringt; use Stringt;
91 with Strub; use Strub;
92 with Style; use Style;
93 with Targparm; use Targparm;
94 with Tbuild; use Tbuild;
95 with Uintp; use Uintp;
96 with Urealp; use Urealp;
97 with Warnsw; use Warnsw;
98
99 package body Sem_Res is
100
101 -----------------------
102 -- Local Subprograms --
103 -----------------------
104
105 -- Second pass (top-down) type checking and overload resolution procedures
106 -- Typ is the type required by context. These procedures propagate the
107 -- type information recursively to the descendants of N. If the node is not
108 -- overloaded, its Etype is established in the first pass. If overloaded,
109 -- the Resolve routines set the correct type. For arithmetic operators, the
110 -- Etype is the base type of the context.
111
112 -- Note that Resolve_Attribute is separated off in Sem_Attr
113
114 function Has_Applicable_User_Defined_Literal
115 (N : Node_Id;
116 Typ : Entity_Id) return Boolean;
117 -- Check whether N is a literal or a named number, and whether Typ has a
118 -- user-defined literal aspect that may apply to N. In this case, replace
119 -- N with a call to the corresponding function and return True.
120
121 procedure Check_Discriminant_Use (N : Node_Id);
122 -- Enforce the restrictions on the use of discriminants when constraining
123 -- a component of a discriminated type (record or concurrent type).
124
125 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
126 -- Given a node for an operator associated with type T, check that the
127 -- operator is visible. Operators all of whose operands are universal must
128 -- be checked for visibility during resolution because their type is not
129 -- determinable based on their operands.
130
131 procedure Check_Fully_Declared_Prefix
132 (Typ : Entity_Id;
133 Pref : Node_Id);
134 -- Check that the type of the prefix of a dereference is not incomplete
135
136 function Check_Infinite_Recursion (Call : Node_Id) return Boolean;
137 -- Given a call node, Call, which is known to occur immediately within the
138 -- subprogram being called, determines whether it is a detectable case of
139 -- an infinite recursion, and if so, outputs appropriate messages. Returns
140 -- True if an infinite recursion is detected, and False otherwise.
141
142 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
143 -- N is the node for a logical operator. If the operator is predefined, and
144 -- the root type of the operands is Standard.Boolean, then a check is made
145 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
146 -- the style check for Style_Check_Boolean_And_Or.
147
148 function Is_Atomic_Non_VFA_Ref_With_Address (N : Node_Id) return Boolean;
149 -- N is either an indexed component or a selected component. Return true
150 -- if the prefix denotes an Atomic but not Volatile_Full_Access object that
151 -- has an address clause (the case in which we may want to give a warning).
152
153 function Is_Definite_Access_Type (E : N_Entity_Id) return Boolean;
154 -- Determine whether E is an access type declared by an access declaration,
155 -- and not an (anonymous) allocator type.
156
157 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
158 -- Utility to check whether the entity for an operator is a predefined
159 -- operator, in which case the expression is left as an operator in the
160 -- tree (else it is rewritten into a call). An instance of an intrinsic
161 -- conversion operation may be given an operator name, but is not treated
162 -- like an operator. Note that an operator that is an imported back-end
163 -- builtin has convention Intrinsic, but is expected to be rewritten into
164 -- a call, so such an operator is not treated as predefined by this
165 -- predicate.
166
167 function Original_Implementation_Base_Type
168 (Id : Entity_Id) return Entity_Id;
169 -- Like Implementation_Base_Type, but looks at Original_Node.
170
171 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
172 -- If a default expression in entry call N depends on the discriminants
173 -- of the task, it must be replaced with a reference to the discriminant
174 -- of the task being called.
175
176 procedure Report_Error_N
177 (Msg : String;
178 N : Node_Or_Entity_Id;
179 Report_Errs : Boolean := True);
180 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
181
182 procedure Report_Error_NE
183 (Msg : String;
184 N : Node_Or_Entity_Id;
185 E : Node_Or_Entity_Id;
186 Report_Errs : Boolean := True);
187 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
188
189 procedure Report_Interpretation
190 (N : Node_Id;
191 Nam : Entity_Id;
192 Typ : Entity_Id);
193 -- Report that Nam is an interpretation for node N. When calling a
194 -- standard operator, use the location of the type Typ to report the
195 -- interpretation, as it may be user-defined thus therefore more
196 -- user-friendly.
197
198 procedure Resolve_Dependent_Expression
199 (N : Node_Id;
200 Expr : Node_Id;
201 Typ : Entity_Id);
202 -- Internal procedure to resolve the dependent expression Expr of the
203 -- conditional expression N with type Typ.
204
205 procedure Resolve_Op_Concat_Arg
206 (N : Node_Id;
207 Arg : Node_Id;
208 Typ : Entity_Id;
209 Is_Comp : Boolean);
210 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
211 -- concatenation operator. The operand is either of the array type or of
212 -- the component type. If the operand is an aggregate, and the component
213 -- type is composite, this is ambiguous if component type has aggregates.
214
215 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
216 -- Does the first part of the work of Resolve_Op_Concat
217
218 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
219 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
220 -- has been resolved. See Resolve_Op_Concat for details.
221
222 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
223 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
224 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
225 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
226 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
227 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
228 procedure Resolve_Declare_Expression (N : Node_Id; Typ : Entity_Id);
229 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
230 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
231 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
232 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
233 procedure Resolve_External_Initializer (N : Node_Id; Typ : Entity_Id);
234 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id);
235 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id);
236 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
237 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
238 procedure Resolve_Interpolated_String_Literal
239 (N : Node_Id;
240 Typ : Entity_Id);
241 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
242 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
243 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
244 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
245 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
246 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
247 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
248 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
249 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id);
250 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
251 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
252 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
253 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
254 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
255 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
256 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
257 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
258 procedure Resolve_Target_Name (N : Node_Id; Typ : Entity_Id);
259 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
260 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
261 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
262
263 function Operator_Kind
264 (Op_Name : Name_Id;
265 Is_Binary : Boolean) return Node_Kind;
266 -- Utility to map the name of an operator into the corresponding Node. Used
267 -- by other node rewriting procedures.
268
269 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
270 -- Resolve actuals of call, and add default expressions for missing ones.
271 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
272 -- called subprogram.
273
274 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
275 -- Called from Resolve_Call, when the prefix denotes an entry or element
276 -- of entry family. Actuals are resolved as for subprograms, and the node
277 -- is rebuilt as an entry call. Also called for protected operations. Typ
278 -- is the context type, which is used when the operation is a protected
279 -- function with no arguments, and the return value is indexed.
280
281 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
282 -- A call to a user-defined intrinsic operator is rewritten as a call to
283 -- the corresponding predefined operator, with suitable conversions. Note
284 -- that this applies only for intrinsic operators that denote predefined
285 -- operators, not ones that are intrinsic imports of back-end builtins.
286
287 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
288 -- Ditto, for arithmetic unary operators
289
290 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
291 -- If an operator node resolves to a call to a user-defined operator,
292 -- rewrite the node as a function call.
293
294 procedure Make_Call_Into_Operator
295 (N : Node_Id;
296 Typ : Entity_Id;
297 Op_Id : Entity_Id);
298 -- Inverse transformation: if an operator is given in functional notation,
299 -- then after resolving the node, transform into an operator node, so that
300 -- operands are resolved properly. Recall that predefined operators do not
301 -- have a full signature and special resolution rules apply.
302
303 procedure Rewrite_Renamed_Operator
304 (N : Node_Id;
305 Op : Entity_Id;
306 Typ : Entity_Id);
307 -- An operator can rename another, e.g. in an instantiation. In that
308 -- case, the proper operator node must be constructed and resolved.
309
310 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
311 -- The String_Literal_Subtype is built for all strings that are not
312 -- operands of a static concatenation operation. It is also built for
313 -- expressions generated by the expansion of the External_Initialization
314 -- aspect. If the argument is not an N_String_Literal node or an
315 -- N_External_Initializer node, then the call has no effect.
316
317 procedure Set_Slice_Subtype (N : Node_Id);
318 -- Build subtype of array type, with the range specified by the slice
319
320 procedure Simplify_Type_Conversion (N : Node_Id);
321 -- Called after N has been resolved and evaluated, but before range checks
322 -- have been applied. This rewrites the conversion into a simpler form.
323
324 function Try_User_Defined_Literal
325 (N : Node_Id;
326 Typ : Entity_Id) return Boolean;
327 -- If the node is a literal or a named number or a conditional expression
328 -- whose dependent expressions are all literals or named numbers, and the
329 -- context type has a user-defined literal aspect, then rewrite the node
330 -- or its leaf nodes as calls to the corresponding function, which plays
331 -- the role of an implicit conversion.
332
333 function Try_User_Defined_Literal_For_Operator
334 (N : Node_Id;
335 Typ : Entity_Id) return Boolean;
336 -- If an operator node has a literal operand, check whether the type of the
337 -- context, or that of the other operand has a user-defined literal aspect
338 -- that can be applied to the literal to resolve the node. If such aspect
339 -- exists, replace literal with a call to the corresponding function and
340 -- return True, return false otherwise.
341
342 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
343 -- A universal_fixed expression in an universal context is unambiguous if
344 -- there is only one applicable fixed point type. Determining whether there
345 -- is only one requires a search over all visible entities, and happens
346 -- only in very pathological cases (see 6115-006).
347
348 -------------------------
349 -- Ambiguous_Character --
350 -------------------------
351
352 procedure Ambiguous_Character (C : Node_Id) is
353 E : Entity_Id;
354
355 begin
356 if Nkind (C) = N_Character_Literal then
357 Error_Msg_N ("ambiguous character literal", C);
358
359 -- First the ones in Standard
360
361 Error_Msg_N ("\\possible interpretation: Character!", C);
362 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
363
364 -- Include Wide_Wide_Character in Ada 2005 mode
365
366 if Ada_Version >= Ada_2005 then
367 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
368 end if;
369
370 -- Now any other types that match
371
372 E := Current_Entity (C);
373 while Present (E) loop
374 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
375 E := Homonym (E);
376 end loop;
377 end if;
378 end Ambiguous_Character;
379
380 -------------------------
381 -- Analyze_And_Resolve --
382 -------------------------
383
384 procedure Analyze_And_Resolve (N : Node_Id) is
385 begin
386 Analyze (N);
387 Resolve (N);
388 end Analyze_And_Resolve;
389
390 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
391 begin
392 Analyze (N);
393 Resolve (N, Typ);
394 end Analyze_And_Resolve;
395
396 -- Versions with check(s) suppressed
397
398 procedure Analyze_And_Resolve
399 (N : Node_Id;
400 Typ : Entity_Id;
401 Suppress : Check_Id)
402 is
403 Scop : constant Entity_Id := Current_Scope;
404
405 begin
406 if Suppress = All_Checks then
407 declare
408 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
409 begin
410 Scope_Suppress.Suppress := (others => True);
411 Analyze_And_Resolve (N, Typ);
412 Scope_Suppress.Suppress := Sva;
413 end;
414
415 else
416 declare
417 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
418 begin
419 Scope_Suppress.Suppress (Suppress) := True;
420 Analyze_And_Resolve (N, Typ);
421 Scope_Suppress.Suppress (Suppress) := Svg;
422 end;
423 end if;
424
425 if Current_Scope /= Scop
426 and then Scope_Is_Transient
427 then
428 -- This can only happen if a transient scope was created for an inner
429 -- expression, which will be removed upon completion of the analysis
430 -- of an enclosing construct. The transient scope must have the
431 -- suppress status of the enclosing environment, not of this Analyze
432 -- call.
433
434 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
435 Scope_Suppress;
436 end if;
437 end Analyze_And_Resolve;
438
439 procedure Analyze_And_Resolve
440 (N : Node_Id;
441 Suppress : Check_Id)
442 is
443 Scop : constant Entity_Id := Current_Scope;
444
445 begin
446 if Suppress = All_Checks then
447 declare
448 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
449 begin
450 Scope_Suppress.Suppress := (others => True);
451 Analyze_And_Resolve (N);
452 Scope_Suppress.Suppress := Sva;
453 end;
454
455 else
456 declare
457 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
458 begin
459 Scope_Suppress.Suppress (Suppress) := True;
460 Analyze_And_Resolve (N);
461 Scope_Suppress.Suppress (Suppress) := Svg;
462 end;
463 end if;
464
465 if Current_Scope /= Scop and then Scope_Is_Transient then
466 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
467 Scope_Suppress;
468 end if;
469 end Analyze_And_Resolve;
470
471 -------------------------------------
472 -- Has_Applicable_User_Defined_Literal --
473 -------------------------------------
474
475 function Has_Applicable_User_Defined_Literal
476 (N : Node_Id;
477 Typ : Entity_Id) return Boolean
478 is
479 Loc : constant Source_Ptr := Sloc (N);
480 Literal_Aspect_Map :
481 constant array (N_Numeric_Or_String_Literal) of Aspect_Id :=
482 (N_Integer_Literal => Aspect_Integer_Literal,
483 N_Interpolated_String_Literal => Aspect_String_Literal,
484 N_Real_Literal => Aspect_Real_Literal,
485 N_String_Literal => Aspect_String_Literal);
486
487 Named_Number_Aspect_Map : constant array (Named_Kind) of Aspect_Id :=
488 (E_Named_Integer => Aspect_Integer_Literal,
489 E_Named_Real => Aspect_Real_Literal);
490
491 Lit_Aspect : Aspect_Id;
492
493 Callee : Entity_Id;
494 Name : Node_Id;
495 Param1 : Node_Id;
496 Param2 : Node_Id;
497 Params : List_Id;
498 Call : Node_Id;
499 Expr : Node_Id;
500
501 begin
502 if (Nkind (N) in N_Numeric_Or_String_Literal
503 | N_Interpolated_String_Literal
504 and then Present
505 (Find_Aspect (Typ, Literal_Aspect_Map (Nkind (N)))))
506 or else
507 (Nkind (N) = N_Identifier
508 and then Is_Named_Number (Entity (N))
509 and then
510 Present
511 (Find_Aspect
512 (Typ, Named_Number_Aspect_Map (Ekind (Entity (N))))))
513 then
514 Lit_Aspect :=
515 (if Nkind (N) = N_Identifier
516 then Named_Number_Aspect_Map (Ekind (Entity (N)))
517 else Literal_Aspect_Map (Nkind (N)));
518 Callee :=
519 Entity (Expression (Find_Aspect (Typ, Lit_Aspect)));
520 Name := Make_Identifier (Loc, Chars (Callee));
521
522 -- It seems that we shouldn't need to retrieve the corresponding
523 -- primitive for a derived type at this point, because it should
524 -- have been determined earlier by Inherit_Nonoverridable_Aspects,
525 -- but the wrapper function created for a literal function when the
526 -- type is frozen gets created too late, so we search again at this
527 -- point. Would be nice to find a way to avoid this. ???
528
529 if Is_Derived_Type (Typ) then
530 Callee :=
531 Corresponding_Op_Of_Derived_Type
532 (Ancestor_Op => Callee,
533 Descendant_Type => Base_Type (Typ));
534 end if;
535
536 -- Handle an identifier that denotes a named number.
537
538 if Nkind (N) = N_Identifier then
539 Expr := Expression (Declaration_Node (Entity (N)));
540
541 if Ekind (Entity (N)) = E_Named_Integer then
542 UI_Image (Expr_Value (Expr), Decimal);
543 Start_String;
544 Store_String_Chars
545 (UI_Image_Buffer (1 .. UI_Image_Length));
546 Param1 := Make_String_Literal (Loc, End_String);
547 Params := New_List (Param1);
548
549 else
550 UI_Image (Norm_Num (Expr_Value_R (Expr)), Decimal);
551 Start_String;
552
553 if UR_Is_Negative (Expr_Value_R (Expr)) then
554 Store_String_Chars ("-");
555 end if;
556
557 Store_String_Chars
558 (UI_Image_Buffer (1 .. UI_Image_Length));
559 Param1 := Make_String_Literal (Loc, End_String);
560
561 -- Note: Set_Etype is called below on Param1
562
563 UI_Image (Norm_Den (Expr_Value_R (Expr)), Decimal);
564 Start_String;
565 Store_String_Chars
566 (UI_Image_Buffer (1 .. UI_Image_Length));
567 Param2 := Make_String_Literal (Loc, End_String);
568 Set_Etype (Param2, Standard_String);
569
570 Params := New_List (Param1, Param2);
571
572 if Present (Related_Expression (Callee)) then
573 Callee := Related_Expression (Callee);
574 else
575 Error_Msg_NE
576 ("cannot resolve & for a named real", N, Callee);
577 return False;
578 end if;
579 end if;
580
581 elsif Nkind (N) = N_String_Literal then
582 Param1 := Make_String_Literal (Loc, Strval (N));
583 Params := New_List (Param1);
584
585 elsif Nkind (N) = N_Interpolated_String_Literal then
586 Param1 := New_Copy_Tree (N);
587 Params := New_List (Param1);
588
589 else
590 Param1 :=
591 Make_String_Literal
592 (Loc, String_From_Numeric_Literal (N));
593 Params := New_List (Param1);
594 end if;
595
596 Call :=
597 Make_Function_Call
598 (Sloc => Loc,
599 Name => Name,
600 Parameter_Associations => Params);
601
602 Set_Entity (Name, Callee);
603 Set_Is_Overloaded (Name, False);
604
605 if Lit_Aspect = Aspect_String_Literal then
606 Set_Etype (Param1, Standard_Wide_Wide_String);
607 else
608 Set_Etype (Param1, Standard_String);
609 end if;
610
611 Set_Etype (Call, Etype (Callee));
612
613 -- Conversion not needed if the result type of the call is class-wide
614 -- or if the result type matches the context type.
615
616 if not Is_Class_Wide_Type (Typ)
617 and then Base_Type (Etype (Call)) /= Base_Type (Typ)
618 then
619 -- Conversion may be needed in case of an inherited
620 -- aspect of a derived type. For a null extension, we
621 -- use a null extension aggregate instead because the
622 -- downward type conversion would be illegal.
623
624 if Is_Null_Extension_Of
625 (Descendant => Typ,
626 Ancestor => Etype (Call))
627 then
628 Call := Make_Extension_Aggregate (Loc,
629 Ancestor_Part => Call,
630 Null_Record_Present => True);
631 else
632 Call := Convert_To (Typ, Call);
633 end if;
634 end if;
635
636 Rewrite (N, Call);
637
638 Analyze_And_Resolve (N, Typ);
639 return True;
640
641 else
642 return False;
643 end if;
644 end Has_Applicable_User_Defined_Literal;
645
646 ----------------------------
647 -- Check_Discriminant_Use --
648 ----------------------------
649
650 procedure Check_Discriminant_Use (N : Node_Id) is
651 PN : constant Node_Id := Parent (N);
652 Disc : constant Entity_Id := Entity (N);
653 P : Node_Id;
654 D : Node_Id;
655
656 begin
657 -- Any use in a spec-expression is legal
658
659 if In_Spec_Expression then
660 null;
661
662 elsif Nkind (PN) = N_Range then
663
664 -- Discriminant cannot be used to constrain a scalar type
665
666 P := Parent (PN);
667
668 if Nkind (P) = N_Range_Constraint
669 and then Nkind (Parent (P)) = N_Subtype_Indication
670 and then Nkind (Parent (Parent (P))) = N_Component_Definition
671 then
672 Error_Msg_N ("discriminant cannot constrain scalar type", N);
673
674 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
675
676 -- The following check catches the unusual case where a
677 -- discriminant appears within an index constraint that is part
678 -- of a larger expression within a constraint on a component,
679 -- e.g. "C : Int range 1 .. F (new A(1 .. D))". For now we only
680 -- check case of record components, and note that a similar check
681 -- should also apply in the case of discriminant constraints
682 -- below. ???
683
684 -- Note that the check for N_Subtype_Declaration below is to
685 -- detect the valid use of discriminants in the constraints of a
686 -- subtype declaration when this subtype declaration appears
687 -- inside the scope of a record type (which is syntactically
688 -- illegal, but which may be created as part of derived type
689 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
690 -- for more info.
691
692 if Ekind (Current_Scope) = E_Record_Type
693 and then Scope (Disc) = Current_Scope
694 and then not
695 (Nkind (Parent (P)) = N_Subtype_Indication
696 and then
697 Nkind (Parent (Parent (P))) in N_Component_Definition
698 | N_Subtype_Declaration
699 and then Paren_Count (N) = 0)
700 then
701 Error_Msg_N
702 ("discriminant must appear alone in component constraint", N);
703 return;
704 end if;
705
706 -- Detect a common error:
707
708 -- type R (D : Positive := 100) is record
709 -- Name : String (1 .. D);
710 -- end record;
711
712 -- The default value causes an object of type R to be allocated
713 -- with room for Positive'Last characters. The RM does not mandate
714 -- the allocation of the maximum size, but that is what GNAT does
715 -- so we should warn the programmer that there is a problem.
716
717 Check_Large : declare
718 SI : Node_Id;
719 T : Entity_Id;
720 TB : Node_Id;
721 CB : Entity_Id;
722
723 function Large_Storage_Type (T : Entity_Id) return Boolean;
724 -- Return True if type T has a large enough range that any
725 -- array whose index type covered the whole range of the type
726 -- would likely raise Storage_Error.
727
728 ------------------------
729 -- Large_Storage_Type --
730 ------------------------
731
732 function Large_Storage_Type (T : Entity_Id) return Boolean is
733 begin
734 -- The type is considered large if its bounds are known at
735 -- compile time and if it requires at least as many bits as
736 -- a Positive to store the possible values.
737
738 return Compile_Time_Known_Value (Type_Low_Bound (T))
739 and then Compile_Time_Known_Value (Type_High_Bound (T))
740 and then
741 Minimum_Size (T, Biased => True) >=
742 RM_Size (Standard_Positive);
743 end Large_Storage_Type;
744
745 -- Start of processing for Check_Large
746
747 begin
748 -- Check that the Disc has a large range
749
750 if not Large_Storage_Type (Etype (Disc)) then
751 goto No_Danger;
752 end if;
753
754 -- If the enclosing type is limited, we allocate only the
755 -- default value, not the maximum, and there is no need for
756 -- a warning.
757
758 if Is_Limited_Type (Scope (Disc)) then
759 goto No_Danger;
760 end if;
761
762 -- Check that it is the high bound
763
764 if N /= High_Bound (PN)
765 or else No (Discriminant_Default_Value (Disc))
766 then
767 goto No_Danger;
768 end if;
769
770 -- Check the array allows a large range at this bound. First
771 -- find the array
772
773 SI := Parent (P);
774
775 if Nkind (SI) /= N_Subtype_Indication then
776 goto No_Danger;
777 end if;
778
779 T := Entity (Subtype_Mark (SI));
780
781 if not Is_Array_Type (T) then
782 goto No_Danger;
783 end if;
784
785 -- Next, find the dimension
786
787 TB := First_Index (T);
788 CB := First (Constraints (P));
789 while True
790 and then Present (TB)
791 and then Present (CB)
792 and then CB /= PN
793 loop
794 Next_Index (TB);
795 Next (CB);
796 end loop;
797
798 if CB /= PN then
799 goto No_Danger;
800 end if;
801
802 -- Now, check the dimension has a large range
803
804 if not Large_Storage_Type (Etype (TB)) then
805 goto No_Danger;
806 end if;
807
808 -- Warn about the danger
809
810 Error_Msg_N
811 ("??creation of & object may raise Storage_Error!",
812 Scope (Disc));
813
814 <<No_Danger>>
815 null;
816
817 end Check_Large;
818 end if;
819
820 -- Legal case is in index or discriminant constraint
821
822 elsif Nkind (PN) in N_Index_Or_Discriminant_Constraint
823 | N_Discriminant_Association
824 then
825 if Paren_Count (N) > 0 then
826 Error_Msg_N
827 ("discriminant in constraint must appear alone", N);
828
829 elsif Nkind (N) = N_Expanded_Name
830 and then Comes_From_Source (N)
831 then
832 Error_Msg_N
833 ("discriminant must appear alone as a direct name", N);
834 end if;
835
836 return;
837
838 -- Otherwise, context is an expression. It should not be within (i.e. a
839 -- subexpression of) a constraint for a component.
840
841 else
842 D := PN;
843 P := Parent (PN);
844 while Nkind (P) not in
845 N_Component_Declaration | N_Subtype_Indication | N_Entry_Declaration
846 loop
847 D := P;
848 P := Parent (P);
849 exit when No (P);
850 end loop;
851
852 -- If the discriminant is used in an expression that is a bound of a
853 -- scalar type, an Itype is created and the bounds are attached to
854 -- its range, not to the original subtype indication. Such use is of
855 -- course a double fault.
856
857 if (Nkind (P) = N_Subtype_Indication
858 and then Nkind (Parent (P)) in N_Component_Definition
859 | N_Derived_Type_Definition
860 and then D = Constraint (P))
861
862 -- The constraint itself may be given by a subtype indication,
863 -- rather than by a more common discrete range.
864
865 or else (Nkind (P) = N_Subtype_Indication
866 and then
867 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
868 or else Nkind (P) = N_Entry_Declaration
869 or else Nkind (D) = N_Defining_Identifier
870 then
871 Error_Msg_N
872 ("discriminant in constraint must appear alone", N);
873 end if;
874 end if;
875 end Check_Discriminant_Use;
876
877 --------------------------------
878 -- Check_For_Visible_Operator --
879 --------------------------------
880
881 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
882 begin
883 if Comes_From_Source (N)
884 and then not Is_Visible_Operator (Original_Node (N), T)
885 and then not Error_Posted (N)
886 then
887 Error_Msg_NE -- CODEFIX
888 ("operator for} is not directly visible!", N, First_Subtype (T));
889 Error_Msg_N -- CODEFIX
890 ("use clause would make operation legal!", N);
891 end if;
892 end Check_For_Visible_Operator;
893
894 ---------------------------------
895 -- Check_Fully_Declared_Prefix --
896 ---------------------------------
897
898 procedure Check_Fully_Declared_Prefix
899 (Typ : Entity_Id;
900 Pref : Node_Id)
901 is
902 begin
903 -- Check that the designated type of the prefix of a dereference is
904 -- not an incomplete type. This cannot be done unconditionally, because
905 -- dereferences of private types are legal in default expressions. This
906 -- case is taken care of in Check_Fully_Declared, called below. There
907 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
908
909 -- This consideration also applies to similar checks for allocators,
910 -- qualified expressions, and type conversions.
911
912 -- An additional exception concerns other per-object expressions that
913 -- are not directly related to component declarations, in particular
914 -- representation pragmas for tasks. These will be per-object
915 -- expressions if they depend on discriminants or some global entity.
916 -- If the task has access discriminants, the designated type may be
917 -- incomplete at the point the expression is resolved. This resolution
918 -- takes place within the body of the initialization procedure, where
919 -- the discriminant is replaced by its discriminal.
920
921 if Is_Entity_Name (Pref)
922 and then Ekind (Entity (Pref)) = E_In_Parameter
923 then
924 null;
925
926 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
927 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
928 -- Analyze_Object_Renaming, and Freeze_Entity.
929
930 elsif Ada_Version >= Ada_2005
931 and then Is_Entity_Name (Pref)
932 and then Is_Access_Type (Etype (Pref))
933 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
934 E_Incomplete_Type
935 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
936 then
937 null;
938 else
939 Check_Fully_Declared (Typ, Parent (Pref));
940 end if;
941 end Check_Fully_Declared_Prefix;
942
943 ------------------------------
944 -- Check_Infinite_Recursion --
945 ------------------------------
946
947 function Check_Infinite_Recursion (Call : Node_Id) return Boolean is
948 function Invoked_With_Different_Arguments (N : Node_Id) return Boolean;
949 -- Determine whether call N invokes the related enclosing subprogram
950 -- with actuals that differ from the subprogram's formals.
951
952 function Is_Conditional_Statement (N : Node_Id) return Boolean;
953 -- Determine whether arbitrary node N denotes a conditional construct
954
955 function Is_Control_Flow_Statement (N : Node_Id) return Boolean;
956 -- Determine whether arbitrary node N denotes a control flow statement
957 -- or a construct that may contains such a statement.
958
959 function Is_Immediately_Within_Body (N : Node_Id) return Boolean;
960 -- Determine whether arbitrary node N appears immediately within the
961 -- statements of an entry or subprogram body.
962
963 function Is_Raise_Idiom (N : Node_Id) return Boolean;
964 -- Determine whether arbitrary node N appears immediately within the
965 -- body of an entry or subprogram, and is preceded by a single raise
966 -- statement.
967
968 function Is_Raise_Statement (N : Node_Id) return Boolean;
969 -- Determine whether arbitrary node N denotes a raise statement
970
971 function Is_Sole_Statement (N : Node_Id) return Boolean;
972 -- Determine whether arbitrary node N is the sole source statement in
973 -- the body of the enclosing subprogram.
974
975 function Preceded_By_Control_Flow_Statement (N : Node_Id) return Boolean;
976 -- Determine whether arbitrary node N is preceded by a control flow
977 -- statement.
978
979 function Within_Conditional_Statement (N : Node_Id) return Boolean;
980 -- Determine whether arbitrary node N appears within a conditional
981 -- construct.
982
983 --------------------------------------
984 -- Invoked_With_Different_Arguments --
985 --------------------------------------
986
987 function Invoked_With_Different_Arguments (N : Node_Id) return Boolean is
988 Subp : constant Entity_Id := Get_Called_Entity (N);
989
990 Actual : Node_Id;
991 Formal : Entity_Id;
992
993 begin
994 -- Determine whether the formals of the invoked subprogram are not
995 -- used as actuals in the call.
996
997 Actual := First_Actual (N);
998 Formal := First_Formal (Subp);
999 while Present (Actual) and then Present (Formal) loop
1000
1001 -- The current actual does not match the current formal
1002
1003 if not (Is_Entity_Name (Actual)
1004 and then Entity (Actual) = Formal)
1005 then
1006 return True;
1007 end if;
1008
1009 Next_Actual (Actual);
1010 Next_Formal (Formal);
1011 end loop;
1012
1013 return False;
1014 end Invoked_With_Different_Arguments;
1015
1016 ------------------------------
1017 -- Is_Conditional_Statement --
1018 ------------------------------
1019
1020 function Is_Conditional_Statement (N : Node_Id) return Boolean is
1021 begin
1022 return
1023 Nkind (N) in N_And_Then
1024 | N_Case_Expression
1025 | N_Case_Statement
1026 | N_If_Expression
1027 | N_If_Statement
1028 | N_Or_Else;
1029 end Is_Conditional_Statement;
1030
1031 -------------------------------
1032 -- Is_Control_Flow_Statement --
1033 -------------------------------
1034
1035 function Is_Control_Flow_Statement (N : Node_Id) return Boolean is
1036 begin
1037 -- It is assumed that all statements may affect the control flow in
1038 -- some way. A raise statement may be expanded into a non-statement
1039 -- node.
1040
1041 return Is_Statement (N) or else Is_Raise_Statement (N);
1042 end Is_Control_Flow_Statement;
1043
1044 --------------------------------
1045 -- Is_Immediately_Within_Body --
1046 --------------------------------
1047
1048 function Is_Immediately_Within_Body (N : Node_Id) return Boolean is
1049 HSS : constant Node_Id := Parent (N);
1050
1051 begin
1052 return
1053 Nkind (HSS) = N_Handled_Sequence_Of_Statements
1054 and then Nkind (Parent (HSS)) in N_Entry_Body | N_Subprogram_Body
1055 and then Is_List_Member (N)
1056 and then List_Containing (N) = Statements (HSS);
1057 end Is_Immediately_Within_Body;
1058
1059 --------------------
1060 -- Is_Raise_Idiom --
1061 --------------------
1062
1063 function Is_Raise_Idiom (N : Node_Id) return Boolean is
1064 Raise_Stmt : Node_Id;
1065 Stmt : Node_Id;
1066
1067 begin
1068 if Is_Immediately_Within_Body (N) then
1069
1070 -- Assume that no raise statement has been seen yet
1071
1072 Raise_Stmt := Empty;
1073
1074 -- Examine the statements preceding the input node, skipping
1075 -- internally-generated constructs.
1076
1077 Stmt := Prev (N);
1078 while Present (Stmt) loop
1079
1080 -- Multiple raise statements violate the idiom
1081
1082 if Is_Raise_Statement (Stmt) then
1083 if Present (Raise_Stmt) then
1084 return False;
1085 end if;
1086
1087 Raise_Stmt := Stmt;
1088
1089 elsif Comes_From_Source (Stmt) then
1090 exit;
1091 end if;
1092
1093 Stmt := Prev (Stmt);
1094 end loop;
1095
1096 -- At this point the node must be preceded by a raise statement,
1097 -- and the raise statement has to be the sole statement within
1098 -- the enclosing entry or subprogram body.
1099
1100 return
1101 Present (Raise_Stmt) and then Is_Sole_Statement (Raise_Stmt);
1102 end if;
1103
1104 return False;
1105 end Is_Raise_Idiom;
1106
1107 ------------------------
1108 -- Is_Raise_Statement --
1109 ------------------------
1110
1111 function Is_Raise_Statement (N : Node_Id) return Boolean is
1112 begin
1113 -- A raise statement may be transfomed into a Raise_xxx_Error node
1114
1115 return
1116 Nkind (N) = N_Raise_Statement
1117 or else Nkind (N) in N_Raise_xxx_Error;
1118 end Is_Raise_Statement;
1119
1120 -----------------------
1121 -- Is_Sole_Statement --
1122 -----------------------
1123
1124 function Is_Sole_Statement (N : Node_Id) return Boolean is
1125 Stmt : Node_Id;
1126
1127 begin
1128 -- The input node appears within the statements of an entry or
1129 -- subprogram body. Examine the statements preceding the node.
1130
1131 if Is_Immediately_Within_Body (N) then
1132 Stmt := Prev (N);
1133
1134 while Present (Stmt) loop
1135
1136 -- The statement is preceded by another statement or a source
1137 -- construct. This indicates that the node does not appear by
1138 -- itself.
1139
1140 if Is_Control_Flow_Statement (Stmt)
1141 or else Comes_From_Source (Stmt)
1142 then
1143 return False;
1144 end if;
1145
1146 Stmt := Prev (Stmt);
1147 end loop;
1148
1149 return True;
1150 end if;
1151
1152 -- The input node is within a construct nested inside the entry or
1153 -- subprogram body.
1154
1155 return False;
1156 end Is_Sole_Statement;
1157
1158 ----------------------------------------
1159 -- Preceded_By_Control_Flow_Statement --
1160 ----------------------------------------
1161
1162 function Preceded_By_Control_Flow_Statement
1163 (N : Node_Id) return Boolean
1164 is
1165 Stmt : Node_Id;
1166
1167 begin
1168 if Is_List_Member (N) then
1169 Stmt := Prev (N);
1170
1171 -- Examine the statements preceding the input node
1172
1173 while Present (Stmt) loop
1174 if Is_Control_Flow_Statement (Stmt) then
1175 return True;
1176 end if;
1177
1178 Stmt := Prev (Stmt);
1179 end loop;
1180
1181 return False;
1182 end if;
1183
1184 -- Assume that the node is part of some control flow statement
1185
1186 return True;
1187 end Preceded_By_Control_Flow_Statement;
1188
1189 ----------------------------------
1190 -- Within_Conditional_Statement --
1191 ----------------------------------
1192
1193 function Within_Conditional_Statement (N : Node_Id) return Boolean is
1194 Stmt : Node_Id;
1195
1196 begin
1197 Stmt := Parent (N);
1198 while Present (Stmt) loop
1199 if Is_Conditional_Statement (Stmt) then
1200 return True;
1201
1202 -- Prevent the search from going too far
1203
1204 elsif Is_Body_Or_Package_Declaration (Stmt) then
1205 exit;
1206 end if;
1207
1208 Stmt := Parent (Stmt);
1209 end loop;
1210
1211 return False;
1212 end Within_Conditional_Statement;
1213
1214 -- Local variables
1215
1216 Call_Context : constant Node_Id :=
1217 Enclosing_Declaration_Or_Statement (Call);
1218
1219 -- Start of processing for Check_Infinite_Recursion
1220
1221 begin
1222 -- The call is assumed to be safe when the enclosing subprogram is
1223 -- invoked with actuals other than its formals.
1224 --
1225 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1226 -- begin
1227 -- ...
1228 -- Proc (A1, A2, ..., AN);
1229 -- ...
1230 -- end Proc;
1231
1232 if Invoked_With_Different_Arguments (Call) then
1233 return False;
1234
1235 -- The call is assumed to be safe when the invocation of the enclosing
1236 -- subprogram depends on a conditional statement.
1237 --
1238 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1239 -- begin
1240 -- ...
1241 -- if Some_Condition then
1242 -- Proc (F1, F2, ..., FN);
1243 -- end if;
1244 -- ...
1245 -- end Proc;
1246
1247 elsif Within_Conditional_Statement (Call) then
1248 return False;
1249
1250 -- The context of the call is assumed to be safe when the invocation of
1251 -- the enclosing subprogram is preceded by some control flow statement.
1252 --
1253 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1254 -- begin
1255 -- ...
1256 -- if Some_Condition then
1257 -- ...
1258 -- end if;
1259 -- ...
1260 -- Proc (F1, F2, ..., FN);
1261 -- ...
1262 -- end Proc;
1263
1264 elsif Preceded_By_Control_Flow_Statement (Call_Context) then
1265 return False;
1266
1267 -- Detect an idiom where the context of the call is preceded by a single
1268 -- raise statement.
1269 --
1270 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1271 -- begin
1272 -- raise ...;
1273 -- Proc (F1, F2, ..., FN);
1274 -- end Proc;
1275
1276 elsif Is_Raise_Idiom (Call_Context) then
1277 return False;
1278 end if;
1279
1280 -- At this point it is certain that infinite recursion will take place
1281 -- as long as the call is executed. Detect a case where the context of
1282 -- the call is the sole source statement within the subprogram body.
1283 --
1284 -- procedure Proc (F1 : ...; F2 : ...; ...; FN : ...) is
1285 -- begin
1286 -- Proc (F1, F2, ..., FN);
1287 -- end Proc;
1288 --
1289 -- Install an explicit raise to prevent the infinite recursion.
1290
1291 if Is_Sole_Statement (Call_Context) then
1292 Error_Msg_Warn := SPARK_Mode /= On;
1293 Error_Msg_N ("!infinite recursion<<", Call);
1294 Error_Msg_N ("\!Storage_Error [<<", Call);
1295
1296 Insert_Action (Call,
1297 Make_Raise_Storage_Error (Sloc (Call),
1298 Reason => SE_Infinite_Recursion));
1299
1300 -- Otherwise infinite recursion could take place, considering other flow
1301 -- control constructs such as gotos, exit statements, etc.
1302
1303 else
1304 Error_Msg_Warn := SPARK_Mode /= On;
1305 Error_Msg_N ("!possible infinite recursion<<", Call);
1306 Error_Msg_N ("\!??Storage_Error ]<<", Call);
1307 end if;
1308
1309 return True;
1310 end Check_Infinite_Recursion;
1311
1312 ---------------------------------------
1313 -- Check_No_Direct_Boolean_Operators --
1314 ---------------------------------------
1315
1316 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
1317 begin
1318 if Scope (Entity (N)) = Standard_Standard
1319 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
1320 then
1321 -- Restriction only applies to original source code
1322
1323 if Comes_From_Source (N) then
1324 Check_Restriction (No_Direct_Boolean_Operators, N);
1325 end if;
1326 end if;
1327
1328 -- Do style check (but skip if in instance, error is on template)
1329
1330 if Style_Check then
1331 if not In_Instance then
1332 Check_Boolean_Operator (N);
1333 end if;
1334 end if;
1335 end Check_No_Direct_Boolean_Operators;
1336
1337 ------------------------------
1338 -- Check_Parameterless_Call --
1339 ------------------------------
1340
1341 procedure Check_Parameterless_Call (N : Node_Id) is
1342 Nam : Node_Id;
1343
1344 function Prefix_Is_Access_Subp return Boolean;
1345 -- If the prefix is of an access_to_subprogram type, the node must be
1346 -- rewritten as a call. Ditto if the prefix is overloaded and all its
1347 -- interpretations are access to subprograms.
1348
1349 ---------------------------
1350 -- Prefix_Is_Access_Subp --
1351 ---------------------------
1352
1353 function Prefix_Is_Access_Subp return Boolean is
1354 I : Interp_Index;
1355 It : Interp;
1356
1357 begin
1358 -- If the context is an attribute reference that can apply to
1359 -- functions, this is never a parameterless call (RM 4.1.4(6)).
1360
1361 if Nkind (Parent (N)) = N_Attribute_Reference
1362 and then Attribute_Name (Parent (N))
1363 in Name_Address | Name_Code_Address | Name_Access
1364 then
1365 return False;
1366 end if;
1367
1368 if not Is_Overloaded (N) then
1369 return
1370 Ekind (Etype (N)) = E_Subprogram_Type
1371 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1372 else
1373 Get_First_Interp (N, I, It);
1374 while Present (It.Typ) loop
1375 if Ekind (It.Typ) /= E_Subprogram_Type
1376 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1377 then
1378 return False;
1379 end if;
1380
1381 Get_Next_Interp (I, It);
1382 end loop;
1383
1384 return True;
1385 end if;
1386 end Prefix_Is_Access_Subp;
1387
1388 -- Start of processing for Check_Parameterless_Call
1389
1390 begin
1391 -- Defend against junk stuff if errors already detected
1392
1393 if Total_Errors_Detected /= 0 then
1394 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1395 return;
1396 elsif Nkind (N) in N_Has_Chars
1397 and then not Is_Valid_Name (Chars (N))
1398 then
1399 return;
1400 end if;
1401
1402 Require_Entity (N);
1403 end if;
1404
1405 -- If the context expects a value, and the name is a procedure, this is
1406 -- most likely a missing 'Access. Don't try to resolve the parameterless
1407 -- call, error will be caught when the outer call is analyzed.
1408
1409 if Is_Entity_Name (N)
1410 and then Ekind (Entity (N)) = E_Procedure
1411 and then not Is_Overloaded (N)
1412 and then
1413 Nkind (Parent (N)) in N_Parameter_Association
1414 | N_Function_Call
1415 | N_Procedure_Call_Statement
1416 then
1417 return;
1418 end if;
1419
1420 -- Rewrite as call if overloadable entity that is (or could be, in the
1421 -- overloaded case) a function call. If we know for sure that the entity
1422 -- is an enumeration literal, we do not rewrite it.
1423
1424 -- If the entity is the name of an operator, it cannot be a call because
1425 -- operators cannot have default parameters. In this case, this must be
1426 -- a string whose contents coincide with an operator name. Set the kind
1427 -- of the node appropriately.
1428
1429 if (Is_Entity_Name (N)
1430 and then Nkind (N) /= N_Operator_Symbol
1431 and then Is_Overloadable (Entity (N))
1432 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1433 or else Is_Overloaded (N)))
1434
1435 -- Rewrite as call if it is an explicit dereference of an expression of
1436 -- a subprogram access type, and the subprogram type is not that of a
1437 -- procedure or entry.
1438
1439 or else
1440 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1441
1442 -- Rewrite as call if it is a selected component which is a function,
1443 -- this is the case of a call to a protected function (which may be
1444 -- overloaded with other protected operations).
1445
1446 or else
1447 (Nkind (N) = N_Selected_Component
1448 and then (Ekind (Entity (Selector_Name (N))) = E_Function
1449 or else
1450 (Ekind (Entity (Selector_Name (N))) in
1451 E_Entry | E_Procedure
1452 and then Is_Overloaded (Selector_Name (N)))))
1453
1454 -- If one of the above three conditions is met, rewrite as call. Apply
1455 -- the rewriting only once.
1456
1457 then
1458 if Nkind (Parent (N)) /= N_Function_Call
1459 or else N /= Name (Parent (N))
1460 then
1461
1462 -- This may be a prefixed call that was not fully analyzed, e.g.
1463 -- an actual in an instance.
1464
1465 if Ada_Version >= Ada_2005
1466 and then Nkind (N) = N_Selected_Component
1467 and then Is_Dispatching_Operation (Entity (Selector_Name (N)))
1468 then
1469 Analyze_Selected_Component (N);
1470
1471 if Nkind (N) /= N_Selected_Component then
1472 return;
1473 end if;
1474 end if;
1475
1476 -- The node is the name of the parameterless call. Preserve its
1477 -- descendants, which may be complex expressions.
1478
1479 Nam := Relocate_Node (N);
1480
1481 -- If overloaded, overload set belongs to new copy
1482
1483 Save_Interps (N, Nam);
1484
1485 -- Change node to parameterless function call (note that the
1486 -- Parameter_Associations associations field is left set to Empty,
1487 -- its normal default value since there are no parameters)
1488
1489 Change_Node (N, N_Function_Call);
1490 Set_Name (N, Nam);
1491 Set_Sloc (N, Sloc (Nam));
1492 Analyze_Call (N);
1493 end if;
1494
1495 elsif Nkind (N) = N_Parameter_Association then
1496 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1497
1498 elsif Nkind (N) = N_Operator_Symbol then
1499 Set_Etype (N, Empty);
1500 Set_Entity (N, Empty);
1501 Set_Is_Overloaded (N, False);
1502 Change_Operator_Symbol_To_String_Literal (N);
1503 Set_Etype (N, Any_String);
1504 end if;
1505 end Check_Parameterless_Call;
1506
1507 ----------------------------------------
1508 -- Is_Atomic_Non_VFA_Ref_With_Address --
1509 ----------------------------------------
1510
1511 function Is_Atomic_Non_VFA_Ref_With_Address (N : Node_Id) return Boolean is
1512 Pref : constant Node_Id := Prefix (N);
1513
1514 function Is_Atomic_Non_VFA (E : Entity_Id) return Boolean;
1515 -- Return true if E is Atomic but not Volatile_Full_Access
1516
1517 -----------------------
1518 -- Is_Atomic_Non_VFA --
1519 -----------------------
1520
1521 function Is_Atomic_Non_VFA (E : Entity_Id) return Boolean is
1522 begin
1523 return Is_Atomic (E) and then not Is_Volatile_Full_Access (E);
1524 end Is_Atomic_Non_VFA;
1525
1526 begin
1527 if Is_Entity_Name (Pref) then
1528 declare
1529 Pent : constant Entity_Id := Entity (Pref);
1530 Ptyp : constant Entity_Id := Etype (Pent);
1531
1532 begin
1533 return not Is_Access_Type (Ptyp)
1534 and then (Is_Atomic_Non_VFA (Ptyp)
1535 or else Is_Atomic_Non_VFA (Pent))
1536 and then Present (Address_Clause (Pent));
1537 end;
1538
1539 else
1540 return False;
1541 end if;
1542 end Is_Atomic_Non_VFA_Ref_With_Address;
1543
1544 -----------------------------
1545 -- Is_Definite_Access_Type --
1546 -----------------------------
1547
1548 function Is_Definite_Access_Type (E : N_Entity_Id) return Boolean is
1549 Btyp : constant Entity_Id := Base_Type (E);
1550 begin
1551 return Ekind (Btyp) = E_Access_Type
1552 or else (Ekind (Btyp) = E_Access_Subprogram_Type
1553 and then Comes_From_Source (Btyp));
1554 end Is_Definite_Access_Type;
1555
1556 ----------------------
1557 -- Is_Predefined_Op --
1558 ----------------------
1559
1560 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1561 begin
1562 -- Predefined operators are intrinsic subprograms
1563
1564 if not Is_Intrinsic_Subprogram (Nam) then
1565 return False;
1566 end if;
1567
1568 -- A call to a back-end builtin is never a predefined operator
1569
1570 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1571 return False;
1572 end if;
1573
1574 return not Is_Generic_Instance (Nam)
1575 and then Chars (Nam) in Any_Operator_Name
1576 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
1577 end Is_Predefined_Op;
1578
1579 -----------------------------
1580 -- Make_Call_Into_Operator --
1581 -----------------------------
1582
1583 procedure Make_Call_Into_Operator
1584 (N : Node_Id;
1585 Typ : Entity_Id;
1586 Op_Id : Entity_Id)
1587 is
1588 Op_Name : constant Name_Id := Chars (Op_Id);
1589 Act1 : Node_Id := First_Actual (N);
1590 Act2 : Node_Id := Next_Actual (Act1);
1591 Error : Boolean := False;
1592 Func : constant Entity_Id := Entity (Name (N));
1593 Is_Binary : constant Boolean := Present (Act2);
1594 Op_Node : Node_Id;
1595 Opnd_Type : Entity_Id := Empty;
1596 Orig_Type : Entity_Id := Empty;
1597 Pack : Entity_Id;
1598
1599 type Kind_Test is access function (E : N_Entity_Id) return Boolean;
1600
1601 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1602 -- If the operand is not universal, and the operator is given by an
1603 -- expanded name, verify that the operand has an interpretation with a
1604 -- type defined in the given scope of the operator.
1605
1606 function Type_In_P (Test : not null Kind_Test) return Entity_Id;
1607 -- Find a type of the given class in package Pack that contains the
1608 -- operator.
1609
1610 ---------------------------
1611 -- Operand_Type_In_Scope --
1612 ---------------------------
1613
1614 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1615 Nod : constant Node_Id := Right_Opnd (Op_Node);
1616 I : Interp_Index;
1617 It : Interp;
1618
1619 begin
1620 if not Is_Overloaded (Nod) then
1621 return Scope (Base_Type (Etype (Nod))) = S;
1622
1623 else
1624 Get_First_Interp (Nod, I, It);
1625 while Present (It.Typ) loop
1626 if Scope (Base_Type (It.Typ)) = S then
1627 return True;
1628 end if;
1629
1630 Get_Next_Interp (I, It);
1631 end loop;
1632
1633 return False;
1634 end if;
1635 end Operand_Type_In_Scope;
1636
1637 ---------------
1638 -- Type_In_P --
1639 ---------------
1640
1641 function Type_In_P (Test : not null Kind_Test) return Entity_Id is
1642 E : Entity_Id;
1643
1644 function In_Decl return Boolean;
1645 -- Verify that node is not part of the type declaration for the
1646 -- candidate type, which would otherwise be invisible.
1647
1648 -------------
1649 -- In_Decl --
1650 -------------
1651
1652 function In_Decl return Boolean is
1653 Decl_Node : constant Node_Id := Parent (E);
1654 Context : Node_Id;
1655
1656 begin
1657 if Etype (E) = Any_Type then
1658 return True;
1659
1660 elsif No (Decl_Node) then
1661 return False;
1662
1663 else
1664 Context := N;
1665
1666 while Present (Context)
1667 and then Nkind (Context) /= N_Compilation_Unit
1668 loop
1669 if Context = Decl_Node then
1670 return True;
1671 else
1672 Context := Parent (Context);
1673 end if;
1674 end loop;
1675
1676 return False;
1677 end if;
1678 end In_Decl;
1679
1680 -- Start of processing for Type_In_P
1681
1682 begin
1683 -- If the context type is declared in the prefix package, this is the
1684 -- desired base type.
1685
1686 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
1687 return Base_Type (Typ);
1688
1689 else
1690 E := First_Entity (Pack);
1691 while Present (E) loop
1692 if Test (E) and then not In_Decl then
1693 return E;
1694 end if;
1695
1696 Next_Entity (E);
1697 end loop;
1698
1699 return Empty;
1700 end if;
1701 end Type_In_P;
1702
1703 -- Start of processing for Make_Call_Into_Operator
1704
1705 begin
1706 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1707
1708 -- Preserve the Comes_From_Source flag on the result if the original
1709 -- call came from source. Although it is not strictly the case that the
1710 -- operator as such comes from the source, logically it corresponds
1711 -- exactly to the function call in the source, so it should be marked
1712 -- this way (e.g. to make sure that validity checks work fine).
1713
1714 Preserve_Comes_From_Source (Op_Node, N);
1715
1716 -- Ensure that the corresponding operator has the same parent as the
1717 -- original call. This guarantees that parent traversals performed by
1718 -- the ABE mechanism succeed.
1719
1720 Set_Parent (Op_Node, Parent (N));
1721
1722 -- Binary operator
1723
1724 if Is_Binary then
1725 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1726 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1727 Save_Interps (Act1, Left_Opnd (Op_Node));
1728 Save_Interps (Act2, Right_Opnd (Op_Node));
1729 Act1 := Left_Opnd (Op_Node);
1730 Act2 := Right_Opnd (Op_Node);
1731
1732 -- Unary operator
1733
1734 else
1735 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1736 Save_Interps (Act1, Right_Opnd (Op_Node));
1737 Act1 := Right_Opnd (Op_Node);
1738 end if;
1739
1740 -- If the operator is denoted by an expanded name, and the prefix is
1741 -- not Standard, but the operator is a predefined one whose scope is
1742 -- Standard, then this is an implicit_operator, inserted as an
1743 -- interpretation by the procedure of the same name. This procedure
1744 -- overestimates the presence of implicit operators, because it does
1745 -- not examine the type of the operands. Verify now that the operand
1746 -- type appears in the given scope. If right operand is universal,
1747 -- check the other operand. In the case of concatenation, either
1748 -- argument can be the component type, so check the type of the result.
1749 -- If both arguments are literals, look for a type of the right kind
1750 -- defined in the given scope. This elaborate nonsense is brought to
1751 -- you courtesy of b33302a. The type itself must be frozen, so we must
1752 -- find the type of the proper class in the given scope.
1753
1754 -- A final wrinkle is the multiplication operator for fixed point types,
1755 -- which is defined in Standard only, and not in the scope of the
1756 -- fixed point type itself.
1757
1758 if Nkind (Name (N)) = N_Expanded_Name then
1759 Pack := Entity (Prefix (Name (N)));
1760
1761 -- If this is a package renaming, get renamed entity, which will be
1762 -- the scope of the operands if operaton is type-correct.
1763
1764 if Present (Renamed_Entity (Pack)) then
1765 Pack := Renamed_Entity (Pack);
1766 end if;
1767
1768 -- If the entity being called is defined in the given package, it is
1769 -- a renaming of a predefined operator, and known to be legal.
1770
1771 if Scope (Entity (Name (N))) = Pack
1772 and then Pack /= Standard_Standard
1773 then
1774 null;
1775
1776 -- Visibility does not need to be checked in an instance: if the
1777 -- operator was not visible in the generic it has been diagnosed
1778 -- already, else there is an implicit copy of it in the instance.
1779
1780 elsif In_Instance then
1781 null;
1782
1783 elsif Op_Name in Name_Op_Multiply | Name_Op_Divide
1784 and then Is_Fixed_Point_Type (Etype (Act1))
1785 and then Is_Fixed_Point_Type (Etype (Act2))
1786 then
1787 if Pack /= Standard_Standard then
1788 Error := True;
1789 end if;
1790
1791 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
1792 -- available.
1793
1794 elsif Ada_Version >= Ada_2005
1795 and then Op_Name in Name_Op_Eq | Name_Op_Ne
1796 and then (Is_Anonymous_Access_Type (Etype (Act1))
1797 or else Is_Anonymous_Access_Type (Etype (Act2)))
1798 then
1799 null;
1800
1801 else
1802 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1803
1804 if Op_Name = Name_Op_Concat then
1805 Opnd_Type := Base_Type (Typ);
1806
1807 elsif (Scope (Opnd_Type) = Standard_Standard
1808 and then Is_Binary)
1809 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1810 and then Is_Binary
1811 and then not Comes_From_Source (Opnd_Type))
1812 then
1813 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1814 end if;
1815
1816 if Scope (Opnd_Type) = Standard_Standard then
1817
1818 -- Verify that the scope contains a type that corresponds to
1819 -- the given literal. Optimize the case where Pack is Standard.
1820
1821 if Pack /= Standard_Standard then
1822 if Opnd_Type = Universal_Integer then
1823 Orig_Type := Type_In_P (Is_Integer_Type'Access);
1824
1825 elsif Opnd_Type = Universal_Real then
1826 Orig_Type := Type_In_P (Is_Real_Type'Access);
1827
1828 elsif Opnd_Type = Universal_Access then
1829 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
1830
1831 elsif Opnd_Type = Any_String then
1832 Orig_Type := Type_In_P (Is_String_Type'Access);
1833
1834 elsif Opnd_Type = Any_Composite then
1835 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1836
1837 if Present (Orig_Type) then
1838 if Has_Private_Component (Orig_Type) then
1839 Orig_Type := Empty;
1840 else
1841 Set_Etype (Act1, Orig_Type);
1842
1843 if Is_Binary then
1844 Set_Etype (Act2, Orig_Type);
1845 end if;
1846 end if;
1847 end if;
1848
1849 else
1850 Orig_Type := Empty;
1851 end if;
1852
1853 Error := No (Orig_Type);
1854 end if;
1855
1856 elsif Ekind (Opnd_Type) = E_Allocator_Type
1857 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1858 then
1859 Error := True;
1860
1861 -- If the type is defined elsewhere, and the operator is not
1862 -- defined in the given scope (by a renaming declaration, e.g.)
1863 -- then this is an error as well. If an extension of System is
1864 -- present, and the type may be defined there, Pack must be
1865 -- System itself.
1866
1867 elsif Scope (Opnd_Type) /= Pack
1868 and then Scope (Op_Id) /= Pack
1869 and then (No (System_Aux_Id)
1870 or else Scope (Opnd_Type) /= System_Aux_Id
1871 or else Pack /= Scope (System_Aux_Id))
1872 then
1873 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1874 Error := True;
1875 else
1876 Error := not Operand_Type_In_Scope (Pack);
1877 end if;
1878
1879 elsif Pack = Standard_Standard
1880 and then not Operand_Type_In_Scope (Standard_Standard)
1881 then
1882 Error := True;
1883 end if;
1884 end if;
1885
1886 if Error then
1887 Error_Msg_Node_2 := Pack;
1888 Error_Msg_NE
1889 ("& not declared in&", N, Selector_Name (Name (N)));
1890 Set_Etype (N, Any_Type);
1891 return;
1892
1893 -- Detect a mismatch between the context type and the result type
1894 -- in the named package, which is otherwise not detected if the
1895 -- operands are universal. Check is only needed if source entity is
1896 -- an operator, not a function that renames an operator.
1897
1898 elsif Nkind (Parent (N)) /= N_Type_Conversion
1899 and then Ekind (Entity (Name (N))) = E_Operator
1900 and then Is_Numeric_Type (Typ)
1901 and then not Is_Universal_Numeric_Type (Typ)
1902 and then Scope (Base_Type (Typ)) /= Pack
1903 and then not In_Instance
1904 then
1905 if Is_Fixed_Point_Type (Typ)
1906 and then Op_Name in Name_Op_Multiply | Name_Op_Divide
1907 then
1908 -- Already checked above
1909
1910 null;
1911
1912 -- Operator may be defined in an extension of System
1913
1914 elsif Present (System_Aux_Id)
1915 and then Present (Opnd_Type)
1916 and then Scope (Opnd_Type) = System_Aux_Id
1917 then
1918 null;
1919
1920 else
1921 -- Could we use Wrong_Type here??? (this would require setting
1922 -- Etype (N) to the actual type found where Typ was expected).
1923
1924 Error_Msg_NE ("expect }", N, Typ);
1925 end if;
1926 end if;
1927 end if;
1928
1929 Set_Chars (Op_Node, Op_Name);
1930
1931 if not Is_Private_Type (Etype (N)) then
1932 Set_Etype (Op_Node, Base_Type (Etype (N)));
1933 else
1934 Set_Etype (Op_Node, Etype (N));
1935 end if;
1936
1937 -- If this is a call to a function that renames a predefined equality,
1938 -- the renaming declaration provides a type that must be used to
1939 -- resolve the operands. This must be done now because resolution of
1940 -- the equality node will not resolve any remaining ambiguity, and it
1941 -- assumes that the first operand is not overloaded.
1942
1943 if Op_Name in Name_Op_Eq | Name_Op_Ne
1944 and then Ekind (Func) = E_Function
1945 and then Is_Overloaded (Act1)
1946 then
1947 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1948 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1949 end if;
1950
1951 Set_Entity (Op_Node, Op_Id);
1952 Generate_Reference (Op_Id, N, ' ');
1953
1954 Rewrite (N, Op_Node);
1955
1956 -- If this is an arithmetic operator and the result type is private,
1957 -- the operands and the result must be wrapped in conversion to
1958 -- expose the underlying numeric type and expand the proper checks,
1959 -- e.g. on division.
1960
1961 if Is_Private_Type (Typ) then
1962 case Nkind (N) is
1963 when N_Op_Add
1964 | N_Op_Divide
1965 | N_Op_Expon
1966 | N_Op_Mod
1967 | N_Op_Multiply
1968 | N_Op_Rem
1969 | N_Op_Subtract
1970 =>
1971 Resolve_Intrinsic_Operator (N, Typ);
1972
1973 when N_Op_Abs
1974 | N_Op_Minus
1975 | N_Op_Plus
1976 =>
1977 Resolve_Intrinsic_Unary_Operator (N, Typ);
1978
1979 when others =>
1980 Resolve (N, Typ);
1981 end case;
1982 else
1983 Resolve (N, Typ);
1984 end if;
1985 end Make_Call_Into_Operator;
1986
1987 -------------------
1988 -- Operator_Kind --
1989 -------------------
1990
1991 function Operator_Kind
1992 (Op_Name : Name_Id;
1993 Is_Binary : Boolean) return Node_Kind
1994 is
1995 Kind : Node_Kind;
1996
1997 begin
1998 -- Use CASE statement or array???
1999
2000 if Is_Binary then
2001 if Op_Name = Name_Op_And then
2002 Kind := N_Op_And;
2003 elsif Op_Name = Name_Op_Or then
2004 Kind := N_Op_Or;
2005 elsif Op_Name = Name_Op_Xor then
2006 Kind := N_Op_Xor;
2007 elsif Op_Name = Name_Op_Eq then
2008 Kind := N_Op_Eq;
2009 elsif Op_Name = Name_Op_Ne then
2010 Kind := N_Op_Ne;
2011 elsif Op_Name = Name_Op_Lt then
2012 Kind := N_Op_Lt;
2013 elsif Op_Name = Name_Op_Le then
2014 Kind := N_Op_Le;
2015 elsif Op_Name = Name_Op_Gt then
2016 Kind := N_Op_Gt;
2017 elsif Op_Name = Name_Op_Ge then
2018 Kind := N_Op_Ge;
2019 elsif Op_Name = Name_Op_Add then
2020 Kind := N_Op_Add;
2021 elsif Op_Name = Name_Op_Subtract then
2022 Kind := N_Op_Subtract;
2023 elsif Op_Name = Name_Op_Concat then
2024 Kind := N_Op_Concat;
2025 elsif Op_Name = Name_Op_Multiply then
2026 Kind := N_Op_Multiply;
2027 elsif Op_Name = Name_Op_Divide then
2028 Kind := N_Op_Divide;
2029 elsif Op_Name = Name_Op_Mod then
2030 Kind := N_Op_Mod;
2031 elsif Op_Name = Name_Op_Rem then
2032 Kind := N_Op_Rem;
2033 elsif Op_Name = Name_Op_Expon then
2034 Kind := N_Op_Expon;
2035 else
2036 raise Program_Error;
2037 end if;
2038
2039 -- Unary operators
2040
2041 else
2042 if Op_Name = Name_Op_Add then
2043 Kind := N_Op_Plus;
2044 elsif Op_Name = Name_Op_Subtract then
2045 Kind := N_Op_Minus;
2046 elsif Op_Name = Name_Op_Abs then
2047 Kind := N_Op_Abs;
2048 elsif Op_Name = Name_Op_Not then
2049 Kind := N_Op_Not;
2050 else
2051 raise Program_Error;
2052 end if;
2053 end if;
2054
2055 return Kind;
2056 end Operator_Kind;
2057
2058 ---------------------------------------
2059 -- Original_Implementation_Base_Type --
2060 ---------------------------------------
2061
2062 function Original_Implementation_Base_Type
2063 (Id : Entity_Id) return Entity_Id
2064 is
2065 IBT : constant Entity_Id := Implementation_Base_Type (Id);
2066 IBT_Decl : constant Node_Id := Parent (IBT);
2067 Parent_Id : Node_Id;
2068 begin
2069 if Nkind (IBT_Decl) = N_Full_Type_Declaration
2070 and then Original_Node (IBT_Decl) /= IBT_Decl
2071 and then Nkind (Original_Node (IBT_Decl)) =
2072 N_Full_Type_Declaration
2073 and then Nkind (Type_Definition (Original_Node (IBT_Decl)))
2074 = N_Derived_Type_Definition
2075 then
2076 Parent_Id := Subtype_Indication (Type_Definition
2077 (Original_Node (IBT_Decl)));
2078
2079 if Nkind (Parent_Id) = N_Subtype_Indication then
2080 Parent_Id := Subtype_Mark (Parent_Id);
2081 end if;
2082
2083 return Original_Implementation_Base_Type
2084 (Etype (Parent_Id));
2085 else
2086 return IBT;
2087 end if;
2088 end Original_Implementation_Base_Type;
2089
2090 ----------------------------
2091 -- Preanalyze_And_Resolve --
2092 ----------------------------
2093
2094 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
2095 Save_Full_Analysis : constant Boolean := Full_Analysis;
2096
2097 begin
2098 pragma Assert (Nkind (N) in N_Subexpr);
2099
2100 Full_Analysis := False;
2101 Expander_Mode_Save_And_Set (False);
2102
2103 -- See also Preanalyze_And_Resolve in sem.adb for similar handling
2104
2105 -- Normally, we suppress all checks for this preanalysis. There is no
2106 -- point in processing them now, since they will be applied properly
2107 -- and in the proper location when the default expressions reanalyzed
2108 -- and reexpanded later on. We will also have more information at that
2109 -- point for possible suppression of individual checks.
2110
2111 -- However, in GNATprove mode, most expansion is suppressed, and this
2112 -- later reanalysis and reexpansion may not occur. GNATprove mode does
2113 -- require the setting of checking flags for proof purposes, so we
2114 -- do the GNATprove preanalysis without suppressing checks.
2115
2116 -- This special handling for SPARK mode is required for example in the
2117 -- case of Ada 2012 constructs such as quantified expressions, which are
2118 -- expanded in two separate steps.
2119
2120 -- We also do not want to suppress checks if we are not dealing
2121 -- with a default expression. One such case that is known to reach
2122 -- this point is the expression of an expression function.
2123
2124 if GNATprove_Mode or Nkind (Parent (N)) = N_Simple_Return_Statement then
2125 Analyze_And_Resolve (N, T);
2126 else
2127 Analyze_And_Resolve (N, T, Suppress => All_Checks);
2128 end if;
2129
2130 Expander_Mode_Restore;
2131 Full_Analysis := Save_Full_Analysis;
2132 end Preanalyze_And_Resolve;
2133
2134 ----------------------------
2135 -- Preanalyze_And_Resolve --
2136 ----------------------------
2137
2138 -- Version without context type
2139
2140 procedure Preanalyze_And_Resolve (N : Node_Id) is
2141 Save_Full_Analysis : constant Boolean := Full_Analysis;
2142
2143 begin
2144 Full_Analysis := False;
2145 Expander_Mode_Save_And_Set (False);
2146
2147 Analyze (N);
2148 Resolve (N, Etype (N), Suppress => All_Checks);
2149
2150 Expander_Mode_Restore;
2151 Full_Analysis := Save_Full_Analysis;
2152 end Preanalyze_And_Resolve;
2153
2154 ----------------------------------
2155 -- Replace_Actual_Discriminants --
2156 ----------------------------------
2157
2158 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
2159 Loc : constant Source_Ptr := Sloc (N);
2160 Tsk : Node_Id := Empty;
2161
2162 function Process_Discr (Nod : Node_Id) return Traverse_Result;
2163 -- Comment needed???
2164
2165 -------------------
2166 -- Process_Discr --
2167 -------------------
2168
2169 function Process_Discr (Nod : Node_Id) return Traverse_Result is
2170 Ent : Entity_Id;
2171
2172 begin
2173 if Nkind (Nod) = N_Identifier then
2174 Ent := Entity (Nod);
2175
2176 if Present (Ent)
2177 and then Ekind (Ent) = E_Discriminant
2178 then
2179 Rewrite (Nod,
2180 Make_Selected_Component (Loc,
2181 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
2182 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
2183
2184 Set_Etype (Nod, Etype (Ent));
2185 end if;
2186
2187 end if;
2188
2189 return OK;
2190 end Process_Discr;
2191
2192 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
2193
2194 -- Start of processing for Replace_Actual_Discriminants
2195
2196 begin
2197 if Expander_Active then
2198 null;
2199
2200 -- Allow the replacement of concurrent discriminants in GNATprove even
2201 -- though this is a light expansion activity. Note that generic units
2202 -- are not modified.
2203
2204 elsif GNATprove_Mode and not Inside_A_Generic then
2205 null;
2206
2207 else
2208 return;
2209 end if;
2210
2211 if Nkind (Name (N)) = N_Selected_Component then
2212 Tsk := Prefix (Name (N));
2213
2214 elsif Nkind (Name (N)) = N_Indexed_Component then
2215 Tsk := Prefix (Prefix (Name (N)));
2216 end if;
2217
2218 if Present (Tsk) then
2219 Replace_Discrs (Default);
2220 end if;
2221 end Replace_Actual_Discriminants;
2222
2223 --------------------
2224 -- Report_Error_N --
2225 --------------------
2226
2227 procedure Report_Error_N
2228 (Msg : String;
2229 N : Node_Or_Entity_Id;
2230 Report_Errs : Boolean := True) is
2231 begin
2232 if Report_Errs then
2233 Error_Msg_N (Msg, N);
2234 end if;
2235 end Report_Error_N;
2236
2237 ---------------------
2238 -- Report_Error_NE --
2239 ---------------------
2240
2241 procedure Report_Error_NE
2242 (Msg : String;
2243 N : Node_Or_Entity_Id;
2244 E : Node_Or_Entity_Id;
2245 Report_Errs : Boolean := True) is
2246 begin
2247 if Report_Errs then
2248 Error_Msg_NE (Msg, N, E);
2249 end if;
2250 end Report_Error_NE;
2251
2252 ---------------------------
2253 -- Report_Interpretation --
2254 ---------------------------
2255
2256 procedure Report_Interpretation
2257 (N : Node_Id;
2258 Nam : Entity_Id;
2259 Typ : Entity_Id)
2260 is
2261 Insert_Location : constant String := "#";
2262 Is_Inherited : constant Boolean :=
2263 Nkind (Parent (Nam)) = N_Full_Type_Declaration;
2264 Text_Base : constant String := "\\possible interpretation";
2265 Text_Inherited : constant String := " (inherited)";
2266 Uncond_Msg : constant String := "!";
2267
2268 begin
2269 -- If the interpretation involves a standard operator, we use the
2270 -- location of the type, which may be user-defined.
2271
2272 if Sloc (Nam) = Standard_Location then
2273 Error_Msg_Sloc := Sloc (Typ);
2274 else
2275 Error_Msg_Sloc := Sloc (Nam);
2276 end if;
2277
2278 if Is_Inherited then
2279 Error_Msg_N
2280 (Text_Base & Text_Inherited & Insert_Location & Uncond_Msg, N);
2281 else
2282 Error_Msg_N -- CODEFIX
2283 (Text_Base & Insert_Location & Uncond_Msg, N);
2284 end if;
2285 end Report_Interpretation;
2286
2287 -------------
2288 -- Resolve --
2289 -------------
2290
2291 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
2292 Ambiguous : Boolean := False;
2293 Ctx_Type : Entity_Id := Typ;
2294 Expr_Type : Entity_Id := Empty; -- prevent junk warning
2295 Err_Type : Entity_Id := Empty;
2296 Found : Boolean := False;
2297 From_Lib : Boolean;
2298 I : Interp_Index;
2299 I1 : Interp_Index := 0; -- prevent junk warning
2300 It : Interp;
2301 It1 : Interp;
2302 Seen : Entity_Id := Empty; -- prevent junk warning
2303
2304 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
2305 -- Determine whether a node comes from a predefined library unit or
2306 -- Standard.
2307
2308 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
2309 -- Try and fix up a literal so that it matches its expected type. New
2310 -- literals are manufactured if necessary to avoid cascaded errors.
2311
2312 procedure Report_Ambiguous_Argument;
2313 -- Additional diagnostics when an ambiguous call has an ambiguous
2314 -- argument (typically a controlling actual).
2315
2316 procedure Resolution_Failed;
2317 -- Called when attempt at resolving current expression fails
2318
2319 ------------------------------------
2320 -- Comes_From_Predefined_Lib_Unit --
2321 -------------------------------------
2322
2323 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
2324 begin
2325 return
2326 Sloc (Nod) = Standard_Location or else In_Predefined_Unit (Nod);
2327 end Comes_From_Predefined_Lib_Unit;
2328
2329 --------------------
2330 -- Patch_Up_Value --
2331 --------------------
2332
2333 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
2334 begin
2335 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
2336 Rewrite (N,
2337 Make_Real_Literal (Sloc (N),
2338 Realval => UR_From_Uint (Intval (N))));
2339 Set_Etype (N, Universal_Real);
2340 Set_Is_Static_Expression (N);
2341
2342 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
2343 Rewrite (N,
2344 Make_Integer_Literal (Sloc (N),
2345 Intval => UR_To_Uint (Realval (N))));
2346 Set_Etype (N, Universal_Integer);
2347 Set_Is_Static_Expression (N);
2348
2349 elsif Nkind (N) = N_String_Literal
2350 and then Is_Character_Type (Typ)
2351 then
2352 Set_Character_Literal_Name (Get_Char_Code ('A'));
2353 Rewrite (N,
2354 Make_Character_Literal (Sloc (N),
2355 Chars => Name_Find,
2356 Char_Literal_Value =>
2357 UI_From_CC (Get_Char_Code ('A'))));
2358 Set_Etype (N, Any_Character);
2359 Set_Is_Static_Expression (N);
2360
2361 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
2362 Rewrite (N,
2363 Make_String_Literal (Sloc (N),
2364 Strval => End_String));
2365
2366 elsif Nkind (N) = N_Range then
2367 Patch_Up_Value (Low_Bound (N), Typ);
2368 Patch_Up_Value (High_Bound (N), Typ);
2369 end if;
2370 end Patch_Up_Value;
2371
2372 -------------------------------
2373 -- Report_Ambiguous_Argument --
2374 -------------------------------
2375
2376 procedure Report_Ambiguous_Argument is
2377 Arg : constant Node_Id := First (Parameter_Associations (N));
2378 I : Interp_Index;
2379 It : Interp;
2380
2381 begin
2382 if Nkind (Arg) = N_Function_Call
2383 and then Is_Entity_Name (Name (Arg))
2384 and then Is_Overloaded (Name (Arg))
2385 then
2386 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
2387
2388 -- Examine possible interpretations, and adapt the message
2389 -- for inherited subprograms declared by a type derivation.
2390
2391 Get_First_Interp (Name (Arg), I, It);
2392 while Present (It.Nam) loop
2393 Report_Interpretation (Arg, It.Nam, It.Typ);
2394 Get_Next_Interp (I, It);
2395 end loop;
2396 end if;
2397
2398 -- Additional message and hint if the ambiguity involves an Ada 2022
2399 -- container aggregate.
2400
2401 Check_Ambiguous_Aggregate (N);
2402 end Report_Ambiguous_Argument;
2403
2404 -----------------------
2405 -- Resolution_Failed --
2406 -----------------------
2407
2408 procedure Resolution_Failed is
2409 begin
2410 Patch_Up_Value (N, Typ);
2411
2412 -- Set the type to the desired one to minimize cascaded errors. Note
2413 -- that this is an approximation and does not work in all cases.
2414
2415 Set_Etype (N, Typ);
2416
2417 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
2418 Set_Is_Overloaded (N, False);
2419
2420 -- The caller will return without calling the expander, so we need
2421 -- to set the analyzed flag. Note that it is fine to set Analyzed
2422 -- to True even if we are in the middle of a shallow analysis,
2423 -- (see the spec of sem for more details) since this is an error
2424 -- situation anyway, and there is no point in repeating the
2425 -- analysis later (indeed it won't work to repeat it later, since
2426 -- we haven't got a clear resolution of which entity is being
2427 -- referenced.)
2428
2429 Set_Analyzed (N, True);
2430 return;
2431 end Resolution_Failed;
2432
2433 -- Start of processing for Resolve
2434
2435 begin
2436 if N = Error then
2437 return;
2438 end if;
2439
2440 -- Access attribute on remote subprogram cannot be used for a non-remote
2441 -- access-to-subprogram type.
2442
2443 if Nkind (N) = N_Attribute_Reference
2444 and then Attribute_Name (N) in Name_Access
2445 | Name_Unrestricted_Access
2446 | Name_Unchecked_Access
2447 and then Comes_From_Source (N)
2448 and then Is_Entity_Name (Prefix (N))
2449 and then Is_Subprogram (Entity (Prefix (N)))
2450 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
2451 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
2452 then
2453 Error_Msg_N
2454 ("prefix must statically denote a non-remote subprogram", N);
2455 end if;
2456
2457 -- If the context is a Remote_Access_To_Subprogram, access attributes
2458 -- must be resolved with the corresponding fat pointer. There is no need
2459 -- to check for the attribute name since the return type of an
2460 -- attribute is never a remote type.
2461
2462 if Nkind (N) = N_Attribute_Reference
2463 and then Comes_From_Source (N)
2464 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
2465 then
2466 declare
2467 Attr : constant Attribute_Id :=
2468 Get_Attribute_Id (Attribute_Name (N));
2469 Pref : constant Node_Id := Prefix (N);
2470 Decl : Node_Id;
2471 Spec : Node_Id;
2472 Is_Remote : Boolean := True;
2473
2474 begin
2475 -- Check that Typ is a remote access-to-subprogram type
2476
2477 if Is_Remote_Access_To_Subprogram_Type (Typ) then
2478
2479 -- Prefix (N) must statically denote a remote subprogram
2480 -- declared in a package specification.
2481
2482 if Attr = Attribute_Access or else
2483 Attr = Attribute_Unchecked_Access or else
2484 Attr = Attribute_Unrestricted_Access
2485 then
2486 Decl := Unit_Declaration_Node (Entity (Pref));
2487
2488 if Nkind (Decl) = N_Subprogram_Body then
2489 Spec := Corresponding_Spec (Decl);
2490
2491 if Present (Spec) then
2492 Decl := Unit_Declaration_Node (Spec);
2493 end if;
2494 end if;
2495
2496 Spec := Parent (Decl);
2497
2498 if not Is_Entity_Name (Prefix (N))
2499 or else Nkind (Spec) /= N_Package_Specification
2500 or else
2501 not Is_Remote_Call_Interface (Defining_Entity (Spec))
2502 then
2503 Is_Remote := False;
2504 Error_Msg_N
2505 ("prefix must statically denote a remote subprogram",
2506 N);
2507 end if;
2508
2509 -- If we are generating code in distributed mode, perform
2510 -- semantic checks against corresponding remote entities.
2511
2512 if Expander_Active
2513 and then Get_PCS_Name /= Name_No_DSA
2514 then
2515 Check_Subtype_Conformant
2516 (New_Id => Entity (Prefix (N)),
2517 Old_Id => Designated_Type
2518 (Corresponding_Remote_Type (Typ)),
2519 Err_Loc => N);
2520
2521 if Is_Remote then
2522 Process_Remote_AST_Attribute (N, Typ);
2523 end if;
2524 end if;
2525 end if;
2526 end if;
2527 end;
2528 end if;
2529
2530 Debug_A_Entry ("resolving ", N);
2531
2532 if Debug_Flag_V then
2533 Write_Overloads (N);
2534 end if;
2535
2536 if Comes_From_Source (N) then
2537 if Is_Fixed_Point_Type (Typ) then
2538 Check_Restriction (No_Fixed_Point, N);
2539
2540 elsif Is_Floating_Point_Type (Typ)
2541 and then Typ /= Universal_Real
2542 and then Typ /= Any_Real
2543 then
2544 Check_Restriction (No_Floating_Point, N);
2545 end if;
2546 end if;
2547
2548 -- Return if already analyzed
2549
2550 if Analyzed (N) then
2551 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
2552 Analyze_Dimension (N);
2553 return;
2554
2555 -- Any case of Any_Type as the Etype value means that we had a
2556 -- previous error.
2557
2558 elsif Etype (N) = Any_Type then
2559 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
2560 return;
2561 end if;
2562
2563 Check_Parameterless_Call (N);
2564
2565 -- The resolution of an Expression_With_Actions is determined by
2566 -- its Expression, but if the node comes from source it is a
2567 -- Declare_Expression and requires scope management.
2568
2569 if Nkind (N) = N_Expression_With_Actions then
2570 if Comes_From_Source (N) and then not Is_Rewrite_Substitution (N) then
2571 Resolve_Declare_Expression (N, Typ);
2572 else
2573 Resolve (Expression (N), Typ);
2574 end if;
2575
2576 Found := True;
2577 Expr_Type := Etype (Expression (N));
2578
2579 -- The resolution of a conditional expression that is the operand of a
2580 -- type conversion is determined by the conversion (RM 4.5.7(10/3)).
2581
2582 elsif Nkind (N) in N_Case_Expression | N_If_Expression
2583 and then Nkind (Parent (N)) = N_Type_Conversion
2584 then
2585 Found := True;
2586 Expr_Type := Etype (Parent (N));
2587
2588 -- If not overloaded, then we know the type, and all that needs doing
2589 -- is to check that this type is compatible with the context. But note
2590 -- that we may have an operator with no interpretation in Ada 2022 for
2591 -- the case of possible user-defined literals as operands.
2592
2593 elsif not Is_Overloaded (N) then
2594 if Nkind (N) in N_Op and then No (Entity (N)) then
2595 pragma Assert (Ada_Version >= Ada_2022);
2596 Found := False;
2597 elsif not Comes_From_Source (N) and then
2598 Original_Implementation_Base_Type (Typ) =
2599 Original_Implementation_Base_Type (Etype (N))
2600 then
2601 -- Ignore privacy for streaming or Put_Image routines
2602 Found := True;
2603 else
2604 Found := Covers (Typ, Etype (N));
2605 end if;
2606
2607 Expr_Type := Etype (N);
2608
2609 -- In the overloaded case, we must select the interpretation that
2610 -- is compatible with the context (i.e. the type passed to Resolve)
2611
2612 else
2613 From_Lib := Comes_From_Predefined_Lib_Unit (N);
2614
2615 -- Loop through possible interpretations
2616
2617 Get_First_Interp (N, I, It);
2618 Interp_Loop : while Present (It.Typ) loop
2619 if Debug_Flag_V then
2620 Write_Str ("Interp: ");
2621 Write_Interp (It);
2622 end if;
2623
2624 -- We are only interested in interpretations that are compatible
2625 -- with the expected type, any other interpretations are ignored.
2626
2627 if not Covers (Typ, It.Typ) then
2628 if Debug_Flag_V then
2629 Write_Str (" interpretation incompatible with context");
2630 Write_Eol;
2631 end if;
2632
2633 else
2634 -- Skip the current interpretation if it is disabled by an
2635 -- abstract operator. This action is performed only when the
2636 -- type against which we are resolving is the same as the
2637 -- type of the interpretation.
2638
2639 if Ada_Version >= Ada_2005
2640 and then It.Typ = Typ
2641 and then not Is_Universal_Numeric_Type (Typ)
2642 and then Present (It.Abstract_Op)
2643 then
2644 if Debug_Flag_V then
2645 Write_Line ("Skip.");
2646 end if;
2647
2648 goto Continue;
2649 end if;
2650
2651 -- First matching interpretation
2652
2653 if not Found then
2654 Found := True;
2655 I1 := I;
2656 Seen := It.Nam;
2657 Expr_Type := It.Typ;
2658
2659 -- Matching interpretation that is not the first, maybe an
2660 -- error, but there are some cases where preference rules are
2661 -- used to choose between the two possibilities. These and
2662 -- some more obscure cases are handled in Disambiguate.
2663
2664 else
2665 -- If the current statement is part of a predefined library
2666 -- unit, then all interpretations which come from user level
2667 -- packages should not be considered. Check previous and
2668 -- current one.
2669
2670 if From_Lib then
2671 if not Comes_From_Predefined_Lib_Unit (It.Nam) then
2672 goto Continue;
2673
2674 elsif not Comes_From_Predefined_Lib_Unit (Seen) then
2675
2676 -- Previous interpretation must be discarded
2677
2678 I1 := I;
2679 Seen := It.Nam;
2680 Expr_Type := It.Typ;
2681 Set_Entity (N, Seen);
2682 goto Continue;
2683 end if;
2684 end if;
2685
2686 -- Otherwise apply further disambiguation steps
2687
2688 Error_Msg_Sloc := Sloc (Seen);
2689 It1 := Disambiguate (N, I1, I, Typ);
2690
2691 -- Disambiguation has succeeded. Skip the remaining
2692 -- interpretations.
2693
2694 if It1 /= No_Interp then
2695 Seen := It1.Nam;
2696 Expr_Type := It1.Typ;
2697
2698 while Present (It.Typ) loop
2699 Get_Next_Interp (I, It);
2700 end loop;
2701
2702 else
2703 -- Before we issue an ambiguity complaint, check for the
2704 -- case of a subprogram call where at least one of the
2705 -- arguments is Any_Type, and if so suppress the message,
2706 -- since it is a cascaded error. This can also happen for
2707 -- a generalized indexing operation.
2708
2709 if Nkind (N) in N_Subprogram_Call
2710 or else (Nkind (N) = N_Indexed_Component
2711 and then Present (Generalized_Indexing (N)))
2712 then
2713 declare
2714 A : Node_Id;
2715 E : Node_Id;
2716
2717 begin
2718 if Nkind (N) = N_Indexed_Component then
2719 Rewrite (N, Generalized_Indexing (N));
2720 end if;
2721
2722 A := First_Actual (N);
2723 while Present (A) loop
2724 E := A;
2725
2726 if Nkind (E) = N_Parameter_Association then
2727 E := Explicit_Actual_Parameter (E);
2728 end if;
2729
2730 if Etype (E) = Any_Type then
2731 if Debug_Flag_V then
2732 Write_Str ("Any_Type in call");
2733 Write_Eol;
2734 end if;
2735
2736 exit Interp_Loop;
2737 end if;
2738
2739 Next_Actual (A);
2740 end loop;
2741 end;
2742
2743 elsif Nkind (N) in N_Binary_Op
2744 and then (Etype (Left_Opnd (N)) = Any_Type
2745 or else Etype (Right_Opnd (N)) = Any_Type)
2746 then
2747 exit Interp_Loop;
2748
2749 elsif Nkind (N) in N_Unary_Op
2750 and then Etype (Right_Opnd (N)) = Any_Type
2751 then
2752 exit Interp_Loop;
2753 end if;
2754
2755 -- Not that special case, so issue message using the flag
2756 -- Ambiguous to control printing of the header message
2757 -- only at the start of an ambiguous set.
2758
2759 if not Ambiguous then
2760 if Nkind (N) = N_Function_Call
2761 and then Nkind (Name (N)) = N_Explicit_Dereference
2762 then
2763 Error_Msg_N
2764 ("ambiguous expression (cannot resolve indirect "
2765 & "call)!", N);
2766 else
2767 Error_Msg_NE -- CODEFIX
2768 ("ambiguous expression (cannot resolve&)!",
2769 N, It.Nam);
2770 end if;
2771
2772 Ambiguous := True;
2773
2774 Report_Interpretation
2775 (N => N,
2776 Nam => Seen,
2777 Typ => Etype (Seen));
2778
2779 if Nkind (N) in N_Subprogram_Call
2780 and then Present (Parameter_Associations (N))
2781 then
2782 Report_Ambiguous_Argument;
2783 end if;
2784 end if;
2785
2786 Error_Msg_Sloc := Sloc (It.Nam);
2787
2788 -- By default, the error message refers to the candidate
2789 -- interpretation. But if it is a predefined operator, it
2790 -- is implicitly declared at the declaration of the type
2791 -- of the operand. Recover the sloc of that declaration
2792 -- for the error message.
2793
2794 if Nkind (N) in N_Op
2795 and then Scope (It.Nam) = Standard_Standard
2796 and then not Is_Overloaded (Right_Opnd (N))
2797 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2798 Standard_Standard
2799 then
2800 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2801
2802 if Comes_From_Source (Err_Type)
2803 and then Present (Parent (Err_Type))
2804 then
2805 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2806 end if;
2807
2808 elsif Nkind (N) in N_Binary_Op
2809 and then Scope (It.Nam) = Standard_Standard
2810 and then not Is_Overloaded (Left_Opnd (N))
2811 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2812 Standard_Standard
2813 then
2814 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2815
2816 if Comes_From_Source (Err_Type)
2817 and then Present (Parent (Err_Type))
2818 then
2819 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2820 end if;
2821
2822 -- If this is an indirect call, use the subprogram_type
2823 -- in the message, to have a meaningful location. Also
2824 -- indicate if this is an inherited operation, created
2825 -- by a type declaration.
2826
2827 elsif Nkind (N) = N_Function_Call
2828 and then Nkind (Name (N)) = N_Explicit_Dereference
2829 and then Is_Type (It.Nam)
2830 then
2831 Err_Type := It.Nam;
2832 Error_Msg_Sloc :=
2833 Sloc (Associated_Node_For_Itype (Err_Type));
2834 else
2835 Err_Type := Empty;
2836 end if;
2837
2838 if Nkind (N) in N_Op
2839 and then Scope (It.Nam) = Standard_Standard
2840 and then Present (Err_Type)
2841 then
2842 -- Special-case the message for universal_fixed
2843 -- operators, which are not declared with the type
2844 -- of the operand, but appear forever in Standard.
2845
2846 if It.Typ = Universal_Fixed
2847 and then Scope (It.Nam) = Standard_Standard
2848 then
2849 Error_Msg_N
2850 ("\\possible interpretation as universal_fixed "
2851 & "operation (RM 4.5.5 (19))", N);
2852 else
2853 Error_Msg_N
2854 ("\\possible interpretation (predefined)#!", N);
2855 end if;
2856
2857 elsif
2858 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2859 then
2860 Report_Interpretation (N, It.Nam, It.Typ);
2861
2862 else
2863 Error_Msg_N -- CODEFIX
2864 ("\\possible interpretation#!", N);
2865 end if;
2866
2867 end if;
2868 end if;
2869
2870 -- We have a matching interpretation, Expr_Type is the type
2871 -- from this interpretation, and Seen is the entity.
2872
2873 -- For an operator, just set the entity name. The type will be
2874 -- set by the specific operator resolution routine.
2875
2876 if Nkind (N) in N_Op then
2877 Set_Entity (N, Seen);
2878 Generate_Reference (Seen, N);
2879
2880 elsif Nkind (N) in N_Case_Expression
2881 | N_Character_Literal
2882 | N_Delta_Aggregate
2883 | N_If_Expression
2884 then
2885 Set_Etype (N, Expr_Type);
2886
2887 -- AI05-0139-2: Expression is overloaded because type has
2888 -- implicit dereference. The context may be the one that
2889 -- requires implicit dereferemce.
2890
2891 elsif Has_Implicit_Dereference (Expr_Type) then
2892 Set_Etype (N, Expr_Type);
2893 Set_Is_Overloaded (N, False);
2894
2895 -- If the expression is an entity, generate a reference
2896 -- to it, as this is not done for an overloaded construct
2897 -- during analysis.
2898
2899 if Is_Entity_Name (N)
2900 and then Comes_From_Source (N)
2901 then
2902 Generate_Reference (Entity (N), N);
2903
2904 -- Examine access discriminants of entity type,
2905 -- to check whether one of them yields the
2906 -- expected type.
2907
2908 declare
2909 Disc : Entity_Id :=
2910 First_Discriminant (Etype (Entity (N)));
2911
2912 begin
2913 while Present (Disc) loop
2914 exit when Is_Access_Type (Etype (Disc))
2915 and then Has_Implicit_Dereference (Disc)
2916 and then Designated_Type (Etype (Disc)) = Typ;
2917
2918 Next_Discriminant (Disc);
2919 end loop;
2920
2921 if Present (Disc) then
2922 Build_Explicit_Dereference (N, Disc);
2923 end if;
2924 end;
2925 end if;
2926
2927 exit Interp_Loop;
2928
2929 elsif Is_Overloaded (N)
2930 and then Present (It.Nam)
2931 and then Ekind (It.Nam) = E_Discriminant
2932 and then Has_Implicit_Dereference (It.Nam)
2933 then
2934 -- If the node is a general indexing, the dereference is
2935 -- is inserted when resolving the rewritten form, else
2936 -- insert it now.
2937
2938 if Nkind (N) /= N_Indexed_Component
2939 or else No (Generalized_Indexing (N))
2940 then
2941 Build_Explicit_Dereference (N, It.Nam);
2942 end if;
2943
2944 -- For an explicit dereference, attribute reference, range,
2945 -- short-circuit form (which is not an operator node), or call
2946 -- with a name that is an explicit dereference, there is
2947 -- nothing to be done at this point.
2948
2949 elsif Nkind (N) in N_Attribute_Reference
2950 | N_And_Then
2951 | N_Explicit_Dereference
2952 | N_Identifier
2953 | N_Indexed_Component
2954 | N_Or_Else
2955 | N_Range
2956 | N_Selected_Component
2957 | N_Slice
2958 or else Nkind (Name (N)) = N_Explicit_Dereference
2959 then
2960 null;
2961
2962 -- For procedure or function calls, set the type of the name,
2963 -- and also the entity pointer for the prefix.
2964
2965 elsif Nkind (N) in N_Subprogram_Call
2966 and then Is_Entity_Name (Name (N))
2967 then
2968 Set_Etype (Name (N), Expr_Type);
2969 Set_Entity (Name (N), Seen);
2970 Generate_Reference (Seen, Name (N));
2971
2972 elsif Nkind (N) = N_Function_Call
2973 and then Nkind (Name (N)) = N_Selected_Component
2974 then
2975 Set_Etype (Name (N), Expr_Type);
2976 Set_Entity (Selector_Name (Name (N)), Seen);
2977 Generate_Reference (Seen, Selector_Name (Name (N)));
2978
2979 -- For all other cases, just set the type of the Name
2980
2981 else
2982 Set_Etype (Name (N), Expr_Type);
2983 end if;
2984
2985 end if;
2986
2987 <<Continue>>
2988
2989 -- Move to next interpretation
2990
2991 exit Interp_Loop when No (It.Typ);
2992
2993 Get_Next_Interp (I, It);
2994 end loop Interp_Loop;
2995 end if;
2996
2997 -- At this stage Found indicates whether or not an acceptable
2998 -- interpretation exists. If not, then we have an error, except that if
2999 -- the context is Any_Type as a result of some other error, then we
3000 -- suppress the error report.
3001
3002 if not Found then
3003 if Typ /= Any_Type then
3004
3005 -- If type we are looking for is Void, then this is the procedure
3006 -- call case, and the error is simply that what we gave is not a
3007 -- procedure name (we think of procedure calls as expressions with
3008 -- types internally, but the user doesn't think of them this way).
3009
3010 if Typ = Standard_Void_Type then
3011
3012 -- Special case message if function used as a procedure
3013
3014 if Nkind (N) = N_Procedure_Call_Statement
3015 and then Is_Entity_Name (Name (N))
3016 and then Ekind (Entity (Name (N))) = E_Function
3017 then
3018 Error_Msg_NE
3019 ("cannot use call to function & as a statement",
3020 Name (N), Entity (Name (N)));
3021 Error_Msg_N
3022 ("\return value of a function call cannot be ignored",
3023 Name (N));
3024
3025 -- Otherwise give general message (not clear what cases this
3026 -- covers, but no harm in providing for them).
3027
3028 else
3029 Error_Msg_N ("expect procedure name in procedure call", N);
3030 end if;
3031
3032 Found := True;
3033
3034 -- Otherwise we do have a subexpression with the wrong type
3035
3036 -- Check for the case of an allocator which uses an access type
3037 -- instead of the designated type. This is a common error and we
3038 -- specialize the message, posting an error on the operand of the
3039 -- allocator, complaining that we expected the designated type of
3040 -- the allocator.
3041
3042 elsif Nkind (N) = N_Allocator
3043 and then Is_Access_Type (Typ)
3044 and then Is_Access_Type (Etype (N))
3045 and then Designated_Type (Etype (N)) = Typ
3046 then
3047 Wrong_Type (Expression (N), Designated_Type (Typ));
3048 Found := True;
3049
3050 -- Check for view mismatch on Null in instances, for which the
3051 -- view-swapping mechanism has no identifier.
3052
3053 elsif (In_Instance or else In_Inlined_Body)
3054 and then Nkind (N) = N_Null
3055 and then Is_Private_Type (Typ)
3056 and then Is_Access_Type (Full_View (Typ))
3057 then
3058 Resolve (N, Full_View (Typ));
3059 Set_Etype (N, Typ);
3060 return;
3061
3062 -- Check for an aggregate. Sometimes we can get bogus aggregates
3063 -- from misuse of parentheses, and we are about to complain about
3064 -- the aggregate without even looking inside it.
3065
3066 -- Instead, if we have an aggregate of type Any_Composite, then
3067 -- analyze and resolve the component fields, and then only issue
3068 -- another message if we get no errors doing this (otherwise
3069 -- assume that the errors in the aggregate caused the problem).
3070
3071 elsif Nkind (N) = N_Aggregate
3072 and then Etype (N) = Any_Composite
3073 then
3074 if Ada_Version >= Ada_2022
3075 and then Has_Aspect (Typ, Aspect_Aggregate)
3076 then
3077 Resolve_Container_Aggregate (N, Typ);
3078
3079 if Expander_Active then
3080 Expand (N);
3081 end if;
3082 return;
3083 end if;
3084
3085 -- Disable expansion in any case. If there is a type mismatch
3086 -- it may be fatal to try to expand the aggregate. The flag
3087 -- would otherwise be set to false when the error is posted.
3088
3089 Expander_Active := False;
3090
3091 declare
3092 procedure Check_Aggr (Aggr : Node_Id);
3093 -- Check one aggregate, and set Found to True if we have a
3094 -- definite error in any of its elements
3095
3096 procedure Check_Elmt (Aelmt : Node_Id);
3097 -- Check one element of aggregate and set Found to True if
3098 -- we definitely have an error in the element.
3099
3100 ----------------
3101 -- Check_Aggr --
3102 ----------------
3103
3104 procedure Check_Aggr (Aggr : Node_Id) is
3105 Elmt : Node_Id;
3106
3107 begin
3108 if Present (Expressions (Aggr)) then
3109 Elmt := First (Expressions (Aggr));
3110 while Present (Elmt) loop
3111 Check_Elmt (Elmt);
3112 Next (Elmt);
3113 end loop;
3114 end if;
3115
3116 if Present (Component_Associations (Aggr)) then
3117 Elmt := First (Component_Associations (Aggr));
3118 while Present (Elmt) loop
3119
3120 -- If this is a default-initialized component, then
3121 -- there is nothing to check. The box will be
3122 -- replaced by the appropriate call during late
3123 -- expansion.
3124
3125 if Nkind (Elmt) /= N_Iterated_Component_Association
3126 and then not Box_Present (Elmt)
3127 then
3128 Check_Elmt (Expression (Elmt));
3129 end if;
3130
3131 Next (Elmt);
3132 end loop;
3133 end if;
3134 end Check_Aggr;
3135
3136 ----------------
3137 -- Check_Elmt --
3138 ----------------
3139
3140 procedure Check_Elmt (Aelmt : Node_Id) is
3141 begin
3142 -- If we have a nested aggregate, go inside it (to
3143 -- attempt a naked analyze-resolve of the aggregate can
3144 -- cause undesirable cascaded errors). Do not resolve
3145 -- expression if it needs a type from context, as for
3146 -- integer * fixed expression.
3147
3148 if Nkind (Aelmt) = N_Aggregate then
3149 Check_Aggr (Aelmt);
3150
3151 else
3152 Analyze (Aelmt);
3153
3154 if not Is_Overloaded (Aelmt)
3155 and then Etype (Aelmt) /= Any_Fixed
3156 then
3157 Resolve (Aelmt);
3158 end if;
3159
3160 if Etype (Aelmt) = Any_Type then
3161 Found := True;
3162 end if;
3163 end if;
3164 end Check_Elmt;
3165
3166 begin
3167 Check_Aggr (N);
3168 end;
3169 end if;
3170
3171 -- Check whether the node is a literal or a named number or a
3172 -- conditional expression whose dependent expressions are all
3173 -- literals or named numbers.
3174
3175 if Try_User_Defined_Literal (N, Typ) then
3176 return;
3177 end if;
3178
3179 -- Looks like we have a type error, but check for special case
3180 -- of Address wanted, integer found, with the configuration pragma
3181 -- Allow_Integer_Address active. If we have this case, introduce
3182 -- an unchecked conversion to allow the integer expression to be
3183 -- treated as an Address. The reverse case of integer wanted,
3184 -- Address found, is treated in an analogous manner.
3185
3186 if Address_Integer_Convert_OK (Typ, Etype (N)) then
3187 Rewrite (N, Unchecked_Convert_To (Typ, Relocate_Node (N)));
3188 Analyze_And_Resolve (N, Typ);
3189 return;
3190
3191 -- Under relaxed RM semantics silently replace occurrences of null
3192 -- by System.Null_Address.
3193
3194 elsif Null_To_Null_Address_Convert_OK (N, Typ) then
3195 Replace_Null_By_Null_Address (N);
3196 Analyze_And_Resolve (N, Typ);
3197 return;
3198 end if;
3199
3200 -- That special Allow_Integer_Address check did not apply, so we
3201 -- have a real type error. If an error message was issued already,
3202 -- Found got reset to True, so if it's still False, issue standard
3203 -- Wrong_Type message.
3204
3205 if not Found then
3206 if Is_Overloaded (N) and then Nkind (N) = N_Function_Call then
3207 declare
3208 Subp_Name : Node_Id;
3209
3210 begin
3211 if Is_Entity_Name (Name (N)) then
3212 Subp_Name := Name (N);
3213
3214 elsif Nkind (Name (N)) = N_Selected_Component then
3215
3216 -- Protected operation: retrieve operation name
3217
3218 Subp_Name := Selector_Name (Name (N));
3219
3220 else
3221 raise Program_Error;
3222 end if;
3223
3224 Error_Msg_Node_2 := Typ;
3225 Error_Msg_NE
3226 ("no visible interpretation of& matches expected type&",
3227 N, Subp_Name);
3228 end;
3229
3230 if All_Errors_Mode then
3231 declare
3232 Index : Interp_Index;
3233 It : Interp;
3234
3235 begin
3236 Error_Msg_N ("\\possible interpretations:", N);
3237
3238 Get_First_Interp (Name (N), Index, It);
3239 while Present (It.Nam) loop
3240 Error_Msg_Sloc := Sloc (It.Nam);
3241 Error_Msg_Node_2 := It.Nam;
3242 Error_Msg_NE
3243 ("\\ type& for & declared#", N, It.Typ);
3244 Get_Next_Interp (Index, It);
3245 end loop;
3246 end;
3247
3248 else
3249 Error_Msg_N ("\use -gnatf for details", N);
3250 end if;
3251
3252 -- Recognize the case of a quantified expression being mistaken
3253 -- for an iterated component association because the user
3254 -- forgot the "all" or "some" keyword after "for". Because the
3255 -- error message starts with "missing ALL", we automatically
3256 -- benefit from the associated CODEFIX, which requires that
3257 -- the message is located on the identifier following "for"
3258 -- in order for the CODEFIX to insert "all" in the right place.
3259
3260 elsif Nkind (N) = N_Aggregate
3261 and then List_Length (Component_Associations (N)) = 1
3262 and then Nkind (First (Component_Associations (N)))
3263 = N_Iterated_Component_Association
3264 and then Is_Boolean_Type (Typ)
3265 then
3266 if Present
3267 (Iterator_Specification
3268 (First (Component_Associations (N))))
3269 then
3270 Error_Msg_N -- CODEFIX
3271 ("missing ALL or SOME in quantified expression",
3272 Defining_Identifier
3273 (Iterator_Specification
3274 (First (Component_Associations (N)))));
3275 else
3276 Error_Msg_N -- CODEFIX
3277 ("missing ALL or SOME in quantified expression",
3278 Defining_Identifier
3279 (First (Component_Associations (N))));
3280 end if;
3281
3282 -- For an operator with no interpretation, check whether one of
3283 -- its operands may be a user-defined literal.
3284
3285 elsif Nkind (N) in N_Op and then No (Entity (N)) then
3286 if Try_User_Defined_Literal_For_Operator (N, Typ) then
3287 return;
3288 else
3289 Unresolved_Operator (N);
3290 end if;
3291
3292 else
3293 Wrong_Type (N, Typ);
3294 end if;
3295 end if;
3296 end if;
3297
3298 Resolution_Failed;
3299 return;
3300
3301 -- Test if we have more than one interpretation for the context
3302
3303 elsif Ambiguous then
3304 Resolution_Failed;
3305 return;
3306
3307 -- Only one interpretation
3308
3309 else
3310 -- Prevent implicit conversions between access-to-subprogram types
3311 -- with different strub modes. Explicit conversions are acceptable in
3312 -- some circumstances. We don't have to be concerned about data or
3313 -- access-to-data types. Conversions between data types can safely
3314 -- drop or add strub attributes from types, because strub effects are
3315 -- associated with the locations rather than values. E.g., converting
3316 -- a hypothetical Strub_Integer variable to Integer would load the
3317 -- value from the variable, enabling stack scrabbing for the
3318 -- enclosing subprogram, and then convert the value to Integer. As
3319 -- for conversions between access-to-data types, that's no different
3320 -- from any other case of type punning.
3321
3322 if Is_Access_Type (Typ)
3323 and then Ekind (Designated_Type (Typ)) = E_Subprogram_Type
3324 and then Is_Access_Type (Expr_Type)
3325 and then Ekind (Designated_Type (Expr_Type)) = E_Subprogram_Type
3326 then
3327 Check_Same_Strub_Mode
3328 (Designated_Type (Typ), Designated_Type (Expr_Type));
3329 end if;
3330
3331 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
3332 -- the "+" on T is abstract, and the operands are of universal type,
3333 -- the above code will have (incorrectly) resolved the "+" to the
3334 -- universal one in Standard. Therefore check for this case and give
3335 -- an error. We can't do this earlier, because it would cause legal
3336 -- cases to get errors (when some other type has an abstract "+").
3337
3338 if Ada_Version >= Ada_2005
3339 and then Nkind (N) in N_Op
3340 and then Is_Overloaded (N)
3341 and then Is_Universal_Numeric_Type (Etype (Entity (N)))
3342 then
3343 Get_First_Interp (N, I, It);
3344 while Present (It.Typ) loop
3345 if Present (It.Abstract_Op)
3346 and then Etype (It.Abstract_Op) = Typ
3347 then
3348 Nondispatching_Call_To_Abstract_Operation
3349 (N, It.Abstract_Op);
3350 return;
3351 end if;
3352
3353 Get_Next_Interp (I, It);
3354 end loop;
3355 end if;
3356
3357 -- Here we have an acceptable interpretation for the context
3358
3359 -- Propagate type information and normalize tree for various
3360 -- predefined operations. If the context only imposes a class of
3361 -- types, rather than a specific type, propagate the actual type
3362 -- downward.
3363
3364 if Typ = Any_Integer or else
3365 Typ = Any_Boolean or else
3366 Typ = Any_Modular or else
3367 Typ = Any_Real or else
3368 Typ = Any_Discrete
3369 then
3370 Ctx_Type := Expr_Type;
3371
3372 -- Any_Fixed is legal in a real context only if a specific fixed-
3373 -- point type is imposed. If Norman Cohen can be confused by this,
3374 -- it deserves a separate message.
3375
3376 if Typ = Any_Real
3377 and then Expr_Type = Any_Fixed
3378 then
3379 Error_Msg_N ("illegal context for mixed mode operation", N);
3380 Set_Etype (N, Universal_Real);
3381 Ctx_Type := Universal_Real;
3382 end if;
3383 end if;
3384
3385 -- A user-defined operator is transformed into a function call at
3386 -- this point, so that further processing knows that operators are
3387 -- really operators (i.e. are predefined operators). User-defined
3388 -- operators that are intrinsic are just renamings of the predefined
3389 -- ones, and need not be turned into calls either, but if they rename
3390 -- a different operator, we must transform the node accordingly.
3391 -- Instantiations of Unchecked_Conversion are intrinsic but are
3392 -- treated as functions, even if given an operator designator.
3393
3394 if Nkind (N) in N_Op
3395 and then Present (Entity (N))
3396 and then Ekind (Entity (N)) /= E_Operator
3397 then
3398 if not Is_Predefined_Op (Entity (N)) then
3399 Rewrite_Operator_As_Call (N, Entity (N));
3400
3401 elsif Present (Alias (Entity (N)))
3402 and then
3403 Nkind (Parent (Parent (Entity (N)))) =
3404 N_Subprogram_Renaming_Declaration
3405 then
3406 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
3407
3408 -- If the node is rewritten, it will be fully resolved in
3409 -- Rewrite_Renamed_Operator.
3410
3411 if Analyzed (N) then
3412 return;
3413 end if;
3414 end if;
3415 end if;
3416
3417 case N_Subexpr'(Nkind (N)) is
3418 when N_Aggregate =>
3419 Resolve_Aggregate (N, Ctx_Type);
3420
3421 when N_Allocator =>
3422 Resolve_Allocator (N, Ctx_Type);
3423
3424 when N_Short_Circuit =>
3425 Resolve_Short_Circuit (N, Ctx_Type);
3426
3427 when N_Attribute_Reference =>
3428 Resolve_Attribute (N, Ctx_Type);
3429
3430 when N_Case_Expression =>
3431 Resolve_Case_Expression (N, Ctx_Type);
3432
3433 when N_Character_Literal =>
3434 Resolve_Character_Literal (N, Ctx_Type);
3435
3436 when N_Delta_Aggregate =>
3437 Resolve_Delta_Aggregate (N, Ctx_Type);
3438
3439 when N_Expanded_Name =>
3440 Resolve_Entity_Name (N, Ctx_Type);
3441
3442 when N_Explicit_Dereference =>
3443 Resolve_Explicit_Dereference (N, Ctx_Type);
3444
3445 when N_Expression_With_Actions =>
3446 Resolve_Expression_With_Actions (N, Ctx_Type);
3447
3448 when N_Extension_Aggregate =>
3449 Resolve_Extension_Aggregate (N, Ctx_Type);
3450
3451 when N_External_Initializer =>
3452 Resolve_External_Initializer (N, Ctx_Type);
3453
3454 when N_Function_Call =>
3455 Resolve_Call (N, Ctx_Type);
3456
3457 when N_Identifier =>
3458 Resolve_Entity_Name (N, Ctx_Type);
3459
3460 when N_If_Expression =>
3461 Resolve_If_Expression (N, Ctx_Type);
3462
3463 when N_Indexed_Component =>
3464 Resolve_Indexed_Component (N, Ctx_Type);
3465
3466 when N_Integer_Literal =>
3467 Resolve_Integer_Literal (N, Ctx_Type);
3468
3469 when N_Membership_Test =>
3470 Resolve_Membership_Op (N, Ctx_Type);
3471
3472 when N_Null =>
3473 Resolve_Null (N, Ctx_Type);
3474
3475 when N_Op_And
3476 | N_Op_Or
3477 | N_Op_Xor
3478 =>
3479 Resolve_Logical_Op (N, Ctx_Type);
3480
3481 when N_Op_Eq
3482 | N_Op_Ne
3483 =>
3484 Resolve_Equality_Op (N, Ctx_Type);
3485
3486 when N_Op_Ge
3487 | N_Op_Gt
3488 | N_Op_Le
3489 | N_Op_Lt
3490 =>
3491 Resolve_Comparison_Op (N, Ctx_Type);
3492
3493 when N_Op_Not =>
3494 Resolve_Op_Not (N, Ctx_Type);
3495
3496 when N_Op_Add
3497 | N_Op_Divide
3498 | N_Op_Mod
3499 | N_Op_Multiply
3500 | N_Op_Rem
3501 | N_Op_Subtract
3502 =>
3503 Resolve_Arithmetic_Op (N, Ctx_Type);
3504
3505 when N_Op_Concat =>
3506 Resolve_Op_Concat (N, Ctx_Type);
3507
3508 when N_Op_Expon =>
3509 Resolve_Op_Expon (N, Ctx_Type);
3510
3511 when N_Op_Abs
3512 | N_Op_Minus
3513 | N_Op_Plus
3514 =>
3515 Resolve_Unary_Op (N, Ctx_Type);
3516
3517 when N_Op_Shift =>
3518 Resolve_Shift (N, Ctx_Type);
3519
3520 when N_Procedure_Call_Statement =>
3521 Resolve_Call (N, Ctx_Type);
3522
3523 when N_Operator_Symbol =>
3524 Resolve_Operator_Symbol (N, Ctx_Type);
3525
3526 when N_Qualified_Expression =>
3527 Resolve_Qualified_Expression (N, Ctx_Type);
3528
3529 -- Why is the following null, needs a comment ???
3530
3531 when N_Quantified_Expression =>
3532 null;
3533
3534 when N_Raise_Expression =>
3535 Resolve_Raise_Expression (N, Ctx_Type);
3536
3537 when N_Raise_xxx_Error =>
3538 Set_Etype (N, Ctx_Type);
3539
3540 when N_Range =>
3541 Resolve_Range (N, Ctx_Type);
3542
3543 when N_Real_Literal =>
3544 Resolve_Real_Literal (N, Ctx_Type);
3545
3546 when N_Reference =>
3547 Resolve_Reference (N, Ctx_Type);
3548
3549 when N_Selected_Component =>
3550 Resolve_Selected_Component (N, Ctx_Type);
3551
3552 when N_Slice =>
3553 Resolve_Slice (N, Ctx_Type);
3554
3555 when N_String_Literal =>
3556 Resolve_String_Literal (N, Ctx_Type);
3557
3558 when N_Interpolated_String_Literal =>
3559 Resolve_Interpolated_String_Literal (N, Ctx_Type);
3560
3561 when N_Target_Name =>
3562 Resolve_Target_Name (N, Ctx_Type);
3563
3564 when N_Type_Conversion =>
3565 Resolve_Type_Conversion (N, Ctx_Type);
3566
3567 when N_Unchecked_Type_Conversion =>
3568 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
3569 end case;
3570
3571 -- Mark relevant use-type and use-package clauses as effective using
3572 -- the original node because constant folding may have occurred and
3573 -- removed references that need to be examined.
3574
3575 if Nkind (Original_Node (N)) in N_Op then
3576 Mark_Use_Clauses (Original_Node (N));
3577 end if;
3578
3579 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
3580 -- expression of an anonymous access type that occurs in the context
3581 -- of a named general access type, except when the expression is that
3582 -- of a membership test. This ensures proper legality checking in
3583 -- terms of allowed conversions (expressions that would be illegal to
3584 -- convert implicitly are allowed in membership tests).
3585
3586 if Ada_Version >= Ada_2012
3587 and then Ekind (Base_Type (Ctx_Type)) = E_General_Access_Type
3588 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
3589 and then Nkind (Parent (N)) not in N_Membership_Test
3590 then
3591 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
3592 Analyze_And_Resolve (N, Ctx_Type);
3593 end if;
3594
3595 -- If the subexpression was replaced by a non-subexpression, then
3596 -- all we do is to expand it. The only legitimate case we know of
3597 -- is converting procedure call statement to entry call statements,
3598 -- but there may be others, so we are making this test general.
3599
3600 if Nkind (N) not in N_Subexpr then
3601 Debug_A_Exit ("resolving ", N, " (done)");
3602 Expand (N);
3603 return;
3604 end if;
3605
3606 -- The expression is definitely NOT overloaded at this point, so
3607 -- we reset the Is_Overloaded flag to avoid any confusion when
3608 -- reanalyzing the node.
3609
3610 Set_Is_Overloaded (N, False);
3611
3612 -- Freeze expression type, entity if it is a name, and designated
3613 -- type if it is an allocator (RM 13.14(10,11,13)).
3614
3615 -- Now that the resolution of the type of the node is complete, and
3616 -- we did not detect an error, we can expand this node. We skip the
3617 -- expand call if we are in a default expression, see section
3618 -- "Handling of Default Expressions" in Sem spec.
3619
3620 Debug_A_Exit ("resolving ", N, " (done)");
3621
3622 -- We unconditionally freeze the expression, even if we are in
3623 -- default expression mode (the Freeze_Expression routine tests this
3624 -- flag and only freezes static types if it is set).
3625
3626 -- Ada 2012 (AI05-177): The declaration of an expression function
3627 -- does not cause freezing, but we never reach here in that case.
3628 -- Here we are resolving the corresponding expanded body, so we do
3629 -- need to perform normal freezing.
3630
3631 -- As elsewhere we do not emit freeze node within a generic.
3632
3633 if not Inside_A_Generic then
3634 Freeze_Expression (N);
3635 end if;
3636
3637 -- Now we can do the expansion
3638
3639 Expand (N);
3640 end if;
3641 end Resolve;
3642
3643 -------------
3644 -- Resolve --
3645 -------------
3646
3647 -- Version with check(s) suppressed
3648
3649 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
3650 begin
3651 if Suppress = All_Checks then
3652 declare
3653 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
3654 begin
3655 Scope_Suppress.Suppress := (others => True);
3656 Resolve (N, Typ);
3657 Scope_Suppress.Suppress := Sva;
3658 end;
3659
3660 else
3661 declare
3662 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
3663 begin
3664 Scope_Suppress.Suppress (Suppress) := True;
3665 Resolve (N, Typ);
3666 Scope_Suppress.Suppress (Suppress) := Svg;
3667 end;
3668 end if;
3669 end Resolve;
3670
3671 -------------
3672 -- Resolve --
3673 -------------
3674
3675 -- Version with implicit type
3676
3677 procedure Resolve (N : Node_Id) is
3678 begin
3679 Resolve (N, Etype (N));
3680 end Resolve;
3681
3682 ---------------------
3683 -- Resolve_Actuals --
3684 ---------------------
3685
3686 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
3687 Loc : constant Source_Ptr := Sloc (N);
3688 A : Node_Id;
3689 A_Typ : Entity_Id := Empty; -- init to avoid warning
3690 F : Entity_Id;
3691 F_Typ : Entity_Id;
3692 Prev : Node_Id := Empty;
3693 Orig_A : Node_Id;
3694 Real_F : Entity_Id := Empty; -- init to avoid warning
3695
3696 Real_Subp : Entity_Id;
3697 -- If the subprogram being called is an inherited operation for
3698 -- a formal derived type in an instance, Real_Subp is the subprogram
3699 -- that will be called. It may have different formal names than the
3700 -- operation of the formal in the generic, so after actual is resolved
3701 -- the name of the actual in a named association must carry the name
3702 -- of the actual of the subprogram being called.
3703
3704 procedure Check_Aliased_Parameter;
3705 -- Check rules on aliased parameters and related accessibility rules
3706 -- in (RM 3.10.2 (10.2-10.4)).
3707
3708 procedure Check_Argument_Order;
3709 -- Performs a check for the case where the actuals are all simple
3710 -- identifiers that correspond to the formal names, but in the wrong
3711 -- order, which is considered suspicious and cause for a warning.
3712
3713 procedure Check_Prefixed_Call;
3714 -- If the original node is an overloaded call in prefix notation,
3715 -- insert an 'Access or a dereference as needed over the first actual.
3716 -- Try_Object_Operation has already verified that there is a valid
3717 -- interpretation, but the form of the actual can only be determined
3718 -- once the primitive operation is identified.
3719
3720 procedure Insert_Default;
3721 -- If the actual is missing in a call, insert in the actuals list
3722 -- an instance of the default expression. The insertion is always
3723 -- a named association.
3724
3725 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
3726 -- Check whether T1 and T2, or their full views, are derived from a
3727 -- common type. Used to enforce the restrictions on array conversions
3728 -- of AI95-00246.
3729
3730 function Static_Concatenation (N : Node_Id) return Boolean;
3731 -- Predicate to determine whether an actual that is a concatenation
3732 -- will be evaluated statically and does not need a transient scope.
3733 -- This must be determined before the actual is resolved and expanded
3734 -- because if needed the transient scope must be introduced earlier.
3735
3736 -----------------------------
3737 -- Check_Aliased_Parameter --
3738 -----------------------------
3739
3740 procedure Check_Aliased_Parameter is
3741 Nominal_Subt : Entity_Id;
3742
3743 begin
3744 if Is_Aliased (F) then
3745 if Is_Tagged_Type (A_Typ) then
3746 null;
3747
3748 elsif Is_Aliased_View (A) then
3749 if Is_Constr_Subt_For_U_Nominal (A_Typ) then
3750 Nominal_Subt := Base_Type (A_Typ);
3751 else
3752 Nominal_Subt := A_Typ;
3753 end if;
3754
3755 if Subtypes_Statically_Match (F_Typ, Nominal_Subt) then
3756 null;
3757
3758 -- In a generic body assume the worst for generic formals:
3759 -- they can have a constrained partial view (AI05-041).
3760
3761 elsif Has_Discriminants (F_Typ)
3762 and then not Is_Constrained (F_Typ)
3763 and then not Object_Type_Has_Constrained_Partial_View
3764 (Typ => F_Typ, Scop => Current_Scope)
3765 then
3766 null;
3767
3768 else
3769 Error_Msg_NE ("untagged actual does not statically match "
3770 & "aliased formal&", A, F);
3771 end if;
3772
3773 else
3774 Error_Msg_NE ("actual for aliased formal& must be "
3775 & "aliased object", A, F);
3776 end if;
3777
3778 if Ekind (Nam) = E_Procedure then
3779 null;
3780
3781 elsif Ekind (Etype (Nam)) = E_Anonymous_Access_Type then
3782 if Nkind (Parent (N)) = N_Type_Conversion
3783 and then Type_Access_Level (Etype (Parent (N)))
3784 < Static_Accessibility_Level (A, Object_Decl_Level)
3785 then
3786 Error_Msg_N ("aliased actual has wrong accessibility", A);
3787 end if;
3788
3789 elsif Nkind (Parent (N)) = N_Qualified_Expression
3790 and then Nkind (Parent (Parent (N))) = N_Allocator
3791 and then Type_Access_Level (Etype (Parent (Parent (N))))
3792 < Static_Accessibility_Level (A, Object_Decl_Level)
3793 then
3794 Error_Msg_N
3795 ("aliased actual in allocator has wrong accessibility", A);
3796 end if;
3797 end if;
3798 end Check_Aliased_Parameter;
3799
3800 --------------------------
3801 -- Check_Argument_Order --
3802 --------------------------
3803
3804 procedure Check_Argument_Order is
3805 begin
3806 -- Nothing to do if no parameters, or original node is neither a
3807 -- function call nor a procedure call statement (happens in the
3808 -- operator-transformed-to-function call case), or the call is to an
3809 -- operator symbol (which is usually in infix form), or the call does
3810 -- not come from source, or this warning is off.
3811
3812 if not Warn_On_Parameter_Order
3813 or else No (Parameter_Associations (N))
3814 or else Nkind (Original_Node (N)) not in N_Subprogram_Call
3815 or else (Nkind (Name (N)) = N_Identifier
3816 and then Present (Entity (Name (N)))
3817 and then Nkind (Entity (Name (N))) =
3818 N_Defining_Operator_Symbol)
3819 or else not Comes_From_Source (N)
3820 then
3821 return;
3822 end if;
3823
3824 declare
3825 Nargs : constant Nat := List_Length (Parameter_Associations (N));
3826
3827 begin
3828 -- Nothing to do if only one parameter
3829
3830 if Nargs < 2 then
3831 return;
3832 end if;
3833
3834 -- Here if at least two arguments
3835
3836 declare
3837 Actuals : array (1 .. Nargs) of Node_Id;
3838 Actual : Node_Id;
3839 Formal : Node_Id;
3840
3841 Wrong_Order : Boolean := False;
3842 -- Set True if an out of order case is found
3843
3844 begin
3845 -- Collect identifier names of actuals, fail if any actual is
3846 -- not a simple identifier, and record max length of name.
3847
3848 Actual := First (Parameter_Associations (N));
3849 for J in Actuals'Range loop
3850 if Nkind (Actual) /= N_Identifier then
3851 return;
3852 else
3853 Actuals (J) := Actual;
3854 Next (Actual);
3855 end if;
3856 end loop;
3857
3858 -- If we got this far, all actuals are identifiers and the list
3859 -- of their names is stored in the Actuals array.
3860
3861 Formal := First_Formal (Nam);
3862 for J in Actuals'Range loop
3863
3864 -- If we ran out of formals, that's odd, probably an error
3865 -- which will be detected elsewhere, but abandon the search.
3866
3867 if No (Formal) then
3868 return;
3869 end if;
3870
3871 -- If name matches and is in order OK
3872
3873 if Chars (Formal) = Chars (Actuals (J)) then
3874 null;
3875
3876 else
3877 -- If no match, see if it is elsewhere in list and if so
3878 -- flag potential wrong order if type is compatible.
3879
3880 for K in Actuals'Range loop
3881 if Chars (Formal) = Chars (Actuals (K))
3882 and then
3883 Has_Compatible_Type (Actuals (K), Etype (Formal))
3884 then
3885 Wrong_Order := True;
3886 goto Continue;
3887 end if;
3888 end loop;
3889
3890 -- No match
3891
3892 return;
3893 end if;
3894
3895 <<Continue>> Next_Formal (Formal);
3896 end loop;
3897
3898 -- If Formals left over, also probably an error, skip warning
3899
3900 if Present (Formal) then
3901 return;
3902 end if;
3903
3904 -- Here we give the warning if something was out of order
3905
3906 if Wrong_Order then
3907 Error_Msg_N
3908 ("?.p?actuals for this call may be in wrong order", N);
3909 end if;
3910 end;
3911 end;
3912 end Check_Argument_Order;
3913
3914 -------------------------
3915 -- Check_Prefixed_Call --
3916 -------------------------
3917
3918 procedure Check_Prefixed_Call is
3919 Actual : constant Node_Id := First_Actual (N);
3920 Actual_Type : constant Entity_Id := Etype (Actual);
3921 Formal_Type : constant Entity_Id := Etype (First_Formal (Nam));
3922 New_Actual : Node_Id;
3923
3924 begin
3925 -- Check whether the call is a prefixed call, with or without
3926 -- additional actuals.
3927
3928 if Is_Expanded_Prefixed_Call (N) then
3929
3930 -- Introduce dereference on object in prefix
3931
3932 if Is_Access_Type (Actual_Type)
3933 and then not Is_Access_Type (Formal_Type)
3934 then
3935 New_Actual :=
3936 Make_Explicit_Dereference (Sloc (Actual),
3937 Prefix => Relocate_Node (Actual));
3938 Rewrite (Actual, New_Actual);
3939 Analyze (Actual);
3940
3941 -- Conversely, if the formal is an access parameter and the object
3942 -- is not an access type or a reference type (i.e. a type with the
3943 -- Implicit_Dereference aspect specified), add an implicit 'Access
3944 -- to the prefix. Its analysis will check that the object is
3945 -- aliased.
3946
3947 elsif Is_Access_Type (Formal_Type)
3948 and then not Is_Access_Type (Actual_Type)
3949 and then (not Has_Implicit_Dereference (Actual_Type)
3950 or else
3951 not Is_Access_Type
3952 (Designated_Type
3953 (Etype (Get_Reference_Discriminant (Actual_Type)))))
3954 then
3955 Rewrite (Actual,
3956 Make_Attribute_Reference (Loc,
3957 Attribute_Name => Name_Access,
3958 Prefix => Relocate_Node (Actual)));
3959 Analyze (Actual);
3960 end if;
3961 end if;
3962 end Check_Prefixed_Call;
3963
3964 --------------------
3965 -- Insert_Default --
3966 --------------------
3967
3968 procedure Insert_Default is
3969 Actval : Node_Id;
3970 Assoc : Node_Id;
3971
3972 begin
3973 -- Missing argument in call, nothing to insert
3974
3975 if No (Default_Value (F)) then
3976 return;
3977
3978 else
3979 -- Note that we do a full New_Copy_Tree, so that any associated
3980 -- Itypes are properly copied. This may not be needed any more,
3981 -- but it does no harm as a safety measure. Defaults of a generic
3982 -- formal may be out of bounds of the corresponding actual (see
3983 -- cc1311b) and an additional check may be required.
3984
3985 Actval :=
3986 New_Copy_Tree
3987 (Default_Value (F),
3988 New_Scope => Current_Scope,
3989 New_Sloc => Loc);
3990
3991 -- Propagate dimension information, if any.
3992
3993 Copy_Dimensions (Default_Value (F), Actval);
3994
3995 if Is_Concurrent_Type (Scope (Nam))
3996 and then Has_Discriminants (Scope (Nam))
3997 then
3998 Replace_Actual_Discriminants (N, Actval);
3999 end if;
4000
4001 if Is_Overloadable (Nam)
4002 and then Present (Alias (Nam))
4003 then
4004 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
4005 and then not Is_Tagged_Type (Etype (F))
4006 then
4007 -- If default is a real literal, do not introduce a
4008 -- conversion whose effect may depend on the run-time
4009 -- size of universal real.
4010
4011 if Nkind (Actval) = N_Real_Literal then
4012 Set_Etype (Actval, Base_Type (Etype (F)));
4013 else
4014 Actval := Unchecked_Convert_To (Etype (F), Actval);
4015 end if;
4016 end if;
4017
4018 if Is_Scalar_Type (Etype (F)) then
4019 Enable_Range_Check (Actval);
4020 end if;
4021
4022 Set_Parent (Actval, N);
4023
4024 -- Resolve aggregates with their base type, to avoid scope
4025 -- anomalies: the subtype was first built in the subprogram
4026 -- declaration, and the current call may be nested.
4027
4028 if Nkind (Actval) = N_Aggregate then
4029 Analyze_And_Resolve (Actval, Etype (F));
4030 else
4031 Analyze_And_Resolve (Actval, Etype (Actval));
4032 end if;
4033
4034 else
4035 Set_Parent (Actval, N);
4036
4037 -- See note above concerning aggregates
4038
4039 if Nkind (Actval) = N_Aggregate
4040 and then Has_Discriminants (Etype (Actval))
4041 then
4042 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
4043
4044 -- Resolve entities with their own type, which may differ from
4045 -- the type of a reference in a generic context because of the
4046 -- trick used in Save_Global_References.Set_Global_Type to set
4047 -- full views forcefully, which did not anticipate the need to
4048 -- re-analyze default values in calls.
4049
4050 elsif Is_Entity_Name (Actval) then
4051 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
4052
4053 -- Ditto for calls whose name is an entity, for the same reason
4054
4055 elsif Nkind (Actval) = N_Function_Call
4056 and then Is_Entity_Name (Name (Actval))
4057 then
4058 Analyze_And_Resolve (Actval, Etype (Entity (Name (Actval))));
4059
4060 else
4061 Analyze_And_Resolve (Actval, Etype (Actval));
4062 end if;
4063 end if;
4064
4065 -- If default is a tag indeterminate function call, propagate tag
4066 -- to obtain proper dispatching.
4067
4068 if Is_Controlling_Formal (F)
4069 and then Nkind (Default_Value (F)) = N_Function_Call
4070 then
4071 Set_Is_Controlling_Actual (Actval);
4072 end if;
4073 end if;
4074
4075 -- If the default expression raises constraint error, then just
4076 -- silently replace it with an N_Raise_Constraint_Error node, since
4077 -- we already gave the warning on the subprogram spec. If node is
4078 -- already a Raise_Constraint_Error leave as is, to prevent loops in
4079 -- the warnings removal machinery.
4080
4081 if Raises_Constraint_Error (Actval)
4082 and then Nkind (Actval) /= N_Raise_Constraint_Error
4083 then
4084 Rewrite (Actval,
4085 Make_Raise_Constraint_Error (Loc,
4086 Reason => CE_Range_Check_Failed));
4087
4088 Set_Raises_Constraint_Error (Actval);
4089 Set_Etype (Actval, Etype (F));
4090 end if;
4091
4092 Assoc :=
4093 Make_Parameter_Association (Loc,
4094 Explicit_Actual_Parameter => Actval,
4095 Selector_Name => Make_Identifier (Loc, Chars (F)));
4096
4097 -- Case of insertion is first named actual
4098
4099 if No (Prev)
4100 or else Nkind (Parent (Prev)) /= N_Parameter_Association
4101 then
4102 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
4103 Set_First_Named_Actual (N, Actval);
4104
4105 if No (Prev) then
4106 if No (Parameter_Associations (N)) then
4107 Set_Parameter_Associations (N, New_List (Assoc));
4108 else
4109 Append (Assoc, Parameter_Associations (N));
4110 end if;
4111
4112 else
4113 Insert_After (Prev, Assoc);
4114 end if;
4115
4116 -- Case of insertion is not first named actual
4117
4118 else
4119 Set_Next_Named_Actual
4120 (Assoc, Next_Named_Actual (Parent (Prev)));
4121 Set_Next_Named_Actual (Parent (Prev), Actval);
4122 Append (Assoc, Parameter_Associations (N));
4123 end if;
4124
4125 Mark_Rewrite_Insertion (Assoc);
4126 Mark_Rewrite_Insertion (Actval);
4127
4128 Prev := Actval;
4129 end Insert_Default;
4130
4131 -------------------
4132 -- Same_Ancestor --
4133 -------------------
4134
4135 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
4136 FT1 : Entity_Id := T1;
4137 FT2 : Entity_Id := T2;
4138
4139 begin
4140 if Is_Private_Type (T1)
4141 and then Present (Full_View (T1))
4142 then
4143 FT1 := Full_View (T1);
4144 end if;
4145
4146 if Is_Private_Type (T2)
4147 and then Present (Full_View (T2))
4148 then
4149 FT2 := Full_View (T2);
4150 end if;
4151
4152 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
4153 end Same_Ancestor;
4154
4155 --------------------------
4156 -- Static_Concatenation --
4157 --------------------------
4158
4159 function Static_Concatenation (N : Node_Id) return Boolean is
4160 begin
4161 case Nkind (N) is
4162 when N_String_Literal =>
4163 return True;
4164
4165 when N_Op_Concat =>
4166
4167 -- Concatenation is static when both operands are static and
4168 -- the concatenation operator is a predefined one.
4169
4170 return Scope (Entity (N)) = Standard_Standard
4171 and then
4172 Static_Concatenation (Left_Opnd (N))
4173 and then
4174 Static_Concatenation (Right_Opnd (N));
4175
4176 when others =>
4177 if Is_Entity_Name (N) then
4178 declare
4179 Ent : constant Entity_Id := Entity (N);
4180 begin
4181 return Ekind (Ent) = E_Constant
4182 and then Present (Constant_Value (Ent))
4183 and then
4184 Is_OK_Static_Expression (Constant_Value (Ent));
4185 end;
4186
4187 else
4188 return False;
4189 end if;
4190 end case;
4191 end Static_Concatenation;
4192
4193 -- Start of processing for Resolve_Actuals
4194
4195 begin
4196 Check_Argument_Order;
4197
4198 if Is_Overloadable (Nam)
4199 and then Is_Inherited_Operation (Nam)
4200 and then In_Instance
4201 and then Present (Alias (Nam))
4202 and then Present (Overridden_Operation (Alias (Nam)))
4203 then
4204 Real_Subp := Alias (Nam);
4205 else
4206 Real_Subp := Empty;
4207 end if;
4208
4209 if Present (First_Actual (N)) then
4210 Check_Prefixed_Call;
4211 end if;
4212
4213 A := First_Actual (N);
4214 F := First_Formal (Nam);
4215
4216 if Present (Real_Subp) then
4217 Real_F := First_Formal (Real_Subp);
4218 end if;
4219
4220 while Present (F) loop
4221 if No (A) and then Needs_No_Actuals (Nam) then
4222 null;
4223
4224 -- If we have an error in any formal or actual, indicated by a type
4225 -- of Any_Type, then abandon resolution attempt, and set result type
4226 -- to Any_Type.
4227
4228 elsif Etype (F) = Any_Type then
4229 Set_Etype (N, Any_Type);
4230 return;
4231
4232 elsif Present (A) and then Etype (A) = Any_Type then
4233 -- For the peculiar case of a user-defined comparison or equality
4234 -- operator that does not return a boolean type, the operands may
4235 -- have been ambiguous for the predefined operator and, therefore,
4236 -- marked with Any_Type. Since the operation has been resolved to
4237 -- the user-defined operator, that is irrelevant, so reset Etype.
4238
4239 if Nkind (Original_Node (N)) in N_Op_Compare
4240 and then not Is_Boolean_Type (Etype (N))
4241 then
4242 Set_Etype (A, Etype (F));
4243
4244 -- Also skip this if the actual is a Raise_Expression, whose type
4245 -- is imposed from context.
4246
4247 elsif Nkind (A) = N_Raise_Expression then
4248 null;
4249
4250 else
4251 Set_Etype (N, Any_Type);
4252 return;
4253 end if;
4254 end if;
4255
4256 -- Case where actual is present
4257
4258 -- If the actual is an entity, generate a reference to it now. We
4259 -- do this before the actual is resolved, because a formal of some
4260 -- protected subprogram, or a task discriminant, will be rewritten
4261 -- during expansion, and the source entity reference may be lost.
4262
4263 if Present (A)
4264 and then Is_Entity_Name (A)
4265 and then Comes_From_Source (A)
4266 then
4267 -- Annotate the tree by creating a variable reference marker when
4268 -- the actual denotes a variable reference, in case the reference
4269 -- is folded or optimized away. The variable reference marker is
4270 -- automatically saved for later examination by the ABE Processing
4271 -- phase. The status of the reference is set as follows:
4272
4273 -- status mode
4274 -- read IN, IN OUT
4275 -- write IN OUT, OUT
4276
4277 if Needs_Variable_Reference_Marker
4278 (N => A,
4279 Calls_OK => True)
4280 then
4281 Build_Variable_Reference_Marker
4282 (N => A,
4283 Read => Ekind (F) /= E_Out_Parameter,
4284 Write => Ekind (F) /= E_In_Parameter);
4285 end if;
4286
4287 Orig_A := Entity (A);
4288
4289 if Present (Orig_A) then
4290 if Is_Formal (Orig_A)
4291 and then Ekind (F) /= E_In_Parameter
4292 then
4293 Generate_Reference (Orig_A, A, 'm');
4294
4295 elsif not Is_Overloaded (A) then
4296 if Ekind (F) /= E_Out_Parameter then
4297 Generate_Reference (Orig_A, A);
4298
4299 -- RM 6.4.1(12): For an out parameter that is passed by
4300 -- copy, the formal parameter object is created, and:
4301
4302 -- * For an access type, the formal parameter is initialized
4303 -- from the value of the actual, without checking that the
4304 -- value satisfies any constraint, any predicate, or any
4305 -- exclusion of the null value.
4306
4307 -- * For a scalar type that has the Default_Value aspect
4308 -- specified, the formal parameter is initialized from the
4309 -- value of the actual, without checking that the value
4310 -- satisfies any constraint or any predicate.
4311 -- I do not understand why this case is included??? this is
4312 -- not a case where an OUT parameter is treated as IN OUT.
4313
4314 -- * For a composite type with discriminants or that has
4315 -- implicit initial values for any subcomponents, the
4316 -- behavior is as for an in out parameter passed by copy.
4317
4318 -- Hence for these cases we generate the read reference now
4319 -- (the write reference will be generated later by
4320 -- Note_Possible_Modification).
4321
4322 elsif Is_By_Copy_Type (Etype (F))
4323 and then
4324 (Is_Access_Type (Etype (F))
4325 or else
4326 (Is_Scalar_Type (Etype (F))
4327 and then
4328 Present (Default_Aspect_Value (Etype (F))))
4329 or else
4330 (Is_Composite_Type (Etype (F))
4331 and then (Has_Discriminants (Etype (F))
4332 or else Is_Partially_Initialized_Type
4333 (Etype (F)))))
4334 then
4335 Generate_Reference (Orig_A, A);
4336 end if;
4337 end if;
4338 end if;
4339 end if;
4340
4341 if Present (A)
4342 and then (Nkind (Parent (A)) /= N_Parameter_Association
4343 or else Chars (Selector_Name (Parent (A))) = Chars (F))
4344 then
4345 -- If style checking mode on, check match of formal name
4346
4347 if Style_Check then
4348 if Nkind (Parent (A)) = N_Parameter_Association then
4349 Check_Identifier (Selector_Name (Parent (A)), F);
4350 end if;
4351 end if;
4352
4353 -- If the formal is Out or In_Out, do not resolve and expand the
4354 -- conversion, because it is subsequently expanded into explicit
4355 -- temporaries and assignments. However, the object of the
4356 -- conversion can be resolved. An exception is the case of tagged
4357 -- type conversion with a class-wide actual. In that case we want
4358 -- the tag check to occur and no temporary will be needed (no
4359 -- representation change can occur) and the parameter is passed by
4360 -- reference, so we go ahead and resolve the type conversion.
4361 -- Another exception is the case of reference to component or
4362 -- subcomponent of a bit-packed array, in which case we want to
4363 -- defer expansion to the point the in and out assignments are
4364 -- performed.
4365
4366 if Ekind (F) /= E_In_Parameter
4367 and then Nkind (A) = N_Type_Conversion
4368 and then not Is_Class_Wide_Type (Etype (Expression (A)))
4369 and then not Is_Interface (Etype (A))
4370 then
4371 declare
4372 Expr_Typ : constant Entity_Id := Etype (Expression (A));
4373
4374 begin
4375 -- Check RM 4.6 (24.2/2)
4376
4377 if Is_Array_Type (Etype (F))
4378 and then Is_View_Conversion (A)
4379 then
4380 -- In a view conversion, the conversion must be legal in
4381 -- both directions, and thus both component types must be
4382 -- aliased, or neither (4.6 (8)).
4383
4384 -- Check RM 4.6 (24.8/2)
4385
4386 if Has_Aliased_Components (Expr_Typ) /=
4387 Has_Aliased_Components (Etype (F))
4388 then
4389 -- This normally illegal conversion is legal in an
4390 -- expanded instance body because of RM 12.3(11).
4391 -- At runtime, conversion must create a new object.
4392
4393 if not In_Instance then
4394 Error_Msg_N
4395 ("both component types in a view conversion must"
4396 & " be aliased, or neither", A);
4397 end if;
4398
4399 -- Check RM 4.6 (24/3)
4400
4401 elsif not Same_Ancestor (Etype (F), Expr_Typ) then
4402 -- Check view conv between unrelated by ref array
4403 -- types.
4404
4405 if Is_By_Reference_Type (Etype (F))
4406 or else Is_By_Reference_Type (Expr_Typ)
4407 then
4408 Error_Msg_N
4409 ("view conversion between unrelated by reference "
4410 & "array types not allowed ('A'I-00246)", A);
4411
4412 -- In Ada 2005 mode, check view conversion component
4413 -- type cannot be private, tagged, or volatile. Note
4414 -- that we only apply this to source conversions. The
4415 -- generated code can contain conversions which are
4416 -- not subject to this test, and we cannot extract the
4417 -- component type in such cases since it is not
4418 -- present.
4419
4420 elsif Comes_From_Source (A)
4421 and then Ada_Version >= Ada_2005
4422 then
4423 declare
4424 Comp_Type : constant Entity_Id :=
4425 Component_Type (Expr_Typ);
4426 begin
4427 if (Is_Private_Type (Comp_Type)
4428 and then not Is_Generic_Type (Comp_Type))
4429 or else Is_Tagged_Type (Comp_Type)
4430 or else Is_Volatile (Comp_Type)
4431 then
4432 Error_Msg_N
4433 ("component type of a view conversion " &
4434 "cannot be private, tagged, or volatile" &
4435 " (RM 4.6 (24))",
4436 Expression (A));
4437 end if;
4438 end;
4439 end if;
4440 end if;
4441
4442 -- AI12-0074 & AI12-0377
4443 -- Check 6.4.1: If the mode is out, the actual parameter is
4444 -- a view conversion, and the type of the formal parameter
4445 -- is a scalar type, then either:
4446 -- - the target and operand type both do not have the
4447 -- Default_Value aspect specified; or
4448 -- - the target and operand type both have the
4449 -- Default_Value aspect specified, and there shall exist
4450 -- a type (other than a root numeric type) that is an
4451 -- ancestor of both the target type and the operand
4452 -- type.
4453
4454 elsif Ekind (F) = E_Out_Parameter
4455 and then Is_Scalar_Type (Etype (F))
4456 then
4457 if Has_Default_Aspect (Etype (F)) /=
4458 Has_Default_Aspect (Expr_Typ)
4459 then
4460 Error_Msg_N
4461 ("view conversion requires Default_Value on both " &
4462 "types (RM 6.4.1)", A);
4463 elsif Has_Default_Aspect (Expr_Typ)
4464 and then not Same_Ancestor (Etype (F), Expr_Typ)
4465 then
4466 Error_Msg_N
4467 ("view conversion between unrelated types with "
4468 & "Default_Value not allowed (RM 6.4.1)", A);
4469 end if;
4470 end if;
4471 end;
4472
4473 -- Resolve expression if conversion is all OK
4474
4475 if (Conversion_OK (A)
4476 or else Valid_Conversion (A, Etype (A), Expression (A)))
4477 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
4478 then
4479 Resolve (Expression (A));
4480 end if;
4481
4482 -- In GNATprove mode, add a range check flag on scalar
4483 -- conversions for IN OUT parameters. The check may be
4484 -- needed on entry from the call.
4485
4486 if GNATprove_Mode
4487 and then Ekind (F) = E_In_Out_Parameter
4488 and then Is_Scalar_Type (Etype (F))
4489 then
4490 Set_Do_Range_Check (Expression (A));
4491 end if;
4492
4493 -- If the actual is a function call that returns a limited
4494 -- unconstrained object that needs finalization, create a
4495 -- transient scope for it, so that it can receive the proper
4496 -- finalization list.
4497
4498 elsif Expander_Active
4499 and then Nkind (A) = N_Function_Call
4500 and then Is_Limited_Record (Etype (F))
4501 and then not Is_Constrained (Etype (F))
4502 and then (Needs_Finalization (Etype (F))
4503 or else Has_Task (Etype (F)))
4504 then
4505 Establish_Transient_Scope (A, Manage_Sec_Stack => False);
4506 Resolve (A, Etype (F));
4507
4508 -- A small optimization: if one of the actuals is a concatenation
4509 -- create a block around a procedure call to recover stack space.
4510 -- This alleviates stack usage when several procedure calls in
4511 -- the same statement list use concatenation. We do not perform
4512 -- this wrapping for code statements, where the argument is a
4513 -- static string, and we want to preserve warnings involving
4514 -- sequences of such statements.
4515
4516 elsif Expander_Active
4517 and then Nkind (A) = N_Op_Concat
4518 and then Nkind (N) = N_Procedure_Call_Statement
4519 and then not (Is_Intrinsic_Subprogram (Nam)
4520 and then Chars (Nam) = Name_Asm)
4521 and then not Static_Concatenation (A)
4522 then
4523 Establish_Transient_Scope (A, Manage_Sec_Stack => False);
4524 Resolve (A, Etype (F));
4525
4526 else
4527 if Nkind (A) = N_Type_Conversion
4528 and then Is_Array_Type (Etype (F))
4529 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
4530 and then
4531 (Is_Limited_Type (Etype (F))
4532 or else Is_Limited_Type (Etype (Expression (A))))
4533 then
4534 Error_Msg_N
4535 ("conversion between unrelated limited array types not "
4536 & "allowed ('A'I-00246)", A);
4537
4538 if Is_Limited_Type (Etype (F)) then
4539 Explain_Limited_Type (Etype (F), A);
4540 end if;
4541
4542 if Is_Limited_Type (Etype (Expression (A))) then
4543 Explain_Limited_Type (Etype (Expression (A)), A);
4544 end if;
4545 end if;
4546
4547 -- (Ada 2005: AI-251): If the actual is an allocator whose
4548 -- directly designated type is a class-wide interface, we build
4549 -- an anonymous access type to use it as the type of the
4550 -- allocator. Later, when the subprogram call is expanded, if
4551 -- the interface has a secondary dispatch table the expander
4552 -- will add a type conversion to force the correct displacement
4553 -- of the pointer.
4554
4555 if Nkind (A) = N_Allocator then
4556 declare
4557 DDT : constant Entity_Id :=
4558 Directly_Designated_Type (Base_Type (Etype (F)));
4559
4560 begin
4561 -- Displace the pointer to the object to reference its
4562 -- secondary dispatch table.
4563
4564 if Is_Class_Wide_Type (DDT)
4565 and then Is_Interface (DDT)
4566 then
4567 Rewrite (A, Convert_To (Etype (F), Relocate_Node (A)));
4568 Analyze_And_Resolve (A, Etype (F),
4569 Suppress => Access_Check);
4570 end if;
4571
4572 -- Ada 2005, AI-162:If the actual is an allocator, the
4573 -- innermost enclosing statement is the master of the
4574 -- created object. This needs to be done with expansion
4575 -- enabled only, otherwise the transient scope will not
4576 -- be removed in the expansion of the wrapped construct.
4577
4578 if Expander_Active
4579 and then (Needs_Finalization (DDT)
4580 or else Has_Task (DDT))
4581 then
4582 Establish_Transient_Scope
4583 (A, Manage_Sec_Stack => False);
4584 end if;
4585 end;
4586
4587 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4588 Check_Restriction (No_Access_Parameter_Allocators, A);
4589 end if;
4590 end if;
4591
4592 -- (Ada 2005): The call may be to a primitive operation of a
4593 -- tagged synchronized type, declared outside of the type. In
4594 -- this case the controlling actual must be converted to its
4595 -- corresponding record type, which is the formal type. The
4596 -- actual may be a subtype, either because of a constraint or
4597 -- because it is a generic actual, so use base type to locate
4598 -- concurrent type.
4599
4600 F_Typ := Base_Type (Etype (F));
4601
4602 if Is_Tagged_Type (F_Typ)
4603 and then (Is_Concurrent_Type (F_Typ)
4604 or else Is_Concurrent_Record_Type (F_Typ))
4605 then
4606 -- If the actual is overloaded, look for an interpretation
4607 -- that has a synchronized type.
4608
4609 if not Is_Overloaded (A) then
4610 A_Typ := Base_Type (Etype (A));
4611
4612 else
4613 declare
4614 Index : Interp_Index;
4615 It : Interp;
4616
4617 begin
4618 Get_First_Interp (A, Index, It);
4619 while Present (It.Typ) loop
4620 if Is_Concurrent_Type (It.Typ)
4621 or else Is_Concurrent_Record_Type (It.Typ)
4622 then
4623 A_Typ := Base_Type (It.Typ);
4624 exit;
4625 end if;
4626
4627 Get_Next_Interp (Index, It);
4628 end loop;
4629 end;
4630 end if;
4631
4632 declare
4633 Full_A_Typ : Entity_Id;
4634
4635 begin
4636 if Present (Full_View (A_Typ)) then
4637 Full_A_Typ := Base_Type (Full_View (A_Typ));
4638 else
4639 Full_A_Typ := A_Typ;
4640 end if;
4641
4642 -- Tagged synchronized type (case 1): the actual is a
4643 -- concurrent type.
4644
4645 if Is_Concurrent_Type (A_Typ)
4646 and then Corresponding_Record_Type (A_Typ) = F_Typ
4647 then
4648 Rewrite (A,
4649 Unchecked_Convert_To
4650 (Corresponding_Record_Type (A_Typ), A));
4651 Resolve (A, Etype (F));
4652
4653 -- Tagged synchronized type (case 2): the formal is a
4654 -- concurrent type.
4655
4656 elsif Ekind (Full_A_Typ) = E_Record_Type
4657 and then Present
4658 (Corresponding_Concurrent_Type (Full_A_Typ))
4659 and then Is_Concurrent_Type (F_Typ)
4660 and then Present (Corresponding_Record_Type (F_Typ))
4661 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
4662 then
4663 Resolve (A, Corresponding_Record_Type (F_Typ));
4664
4665 -- Common case
4666
4667 else
4668 Resolve (A, Etype (F));
4669 end if;
4670 end;
4671
4672 -- Not a synchronized operation
4673
4674 else
4675 Resolve (A, Etype (F));
4676 end if;
4677 end if;
4678
4679 A_Typ := Etype (A);
4680 F_Typ := Etype (F);
4681
4682 -- If A_Typ is complete and F_Typ is not, then adjust F_Typ
4683
4684 if Ekind (F_Typ) = E_Incomplete_Type
4685 and then Present (Full_View (F_Typ))
4686 and then not Is_Incomplete_Type (A_Typ)
4687 then
4688 F_Typ := Full_View (F_Typ);
4689 end if;
4690
4691 -- An actual cannot be of an untagged incomplete view; the result
4692 -- object of a function call cannot be of an incomplete view
4693 -- (RM 3.10.1(10)).
4694
4695 if Ekind (A_Typ) = E_Incomplete_Type
4696 and then (Nkind (A) = N_Function_Call
4697 or else not Is_Tagged_Type (A_Typ))
4698 then
4699 -- No error if the call is placed in the initializing
4700 -- expression of a component of the full-view of the
4701 -- incomplete type. For example:
4702
4703 -- type T;
4704 -- function F (Obj : T) return Integer;
4705
4706 -- type T is record
4707 -- Data : Integer := F (T);
4708 -- end record;
4709
4710 if Present (Full_View (A_Typ))
4711 and then Full_View (A_Typ) = Current_Scope
4712 and then In_Spec_Expression
4713 and then In_Default_Expr
4714 then
4715 null;
4716
4717 -- No error if the call is performed in pre/postconditions, and
4718 -- it is an incomplete type of a limited-with clause.
4719
4720 elsif From_Limited_With (A_Typ)
4721 and then Present (Non_Limited_View (A_Typ))
4722 and then Is_Subprogram (Current_Scope)
4723 and then
4724 (In_Spec_Expression
4725 or else
4726 Present (Class_Preconditions_Subprogram (Current_Scope)))
4727 then
4728 null;
4729
4730 elsif Is_Generic_Type (A_Typ) then
4731 if Is_Tagged_Type (A_Typ) then
4732 Error_Msg_N
4733 ("invalid use of tagged formal incomplete type", A);
4734 else
4735 Error_Msg_N
4736 ("invalid use of untagged formal incomplete type", A);
4737 end if;
4738 else
4739 Check_Fully_Declared (A_Typ, A);
4740 end if;
4741 end if;
4742
4743 -- For mode IN, if actual is an entity, and the type of the formal
4744 -- has warnings suppressed, then we reset Never_Set_In_Source for
4745 -- the calling entity. The reason for this is to catch cases like
4746 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
4747 -- uses trickery to modify an IN parameter.
4748
4749 if Ekind (F) = E_In_Parameter
4750 and then Is_Entity_Name (A)
4751 and then Present (Entity (A))
4752 and then Ekind (Entity (A)) = E_Variable
4753 and then Has_Warnings_Off (F_Typ)
4754 then
4755 Set_Never_Set_In_Source (Entity (A), False);
4756 end if;
4757
4758 -- Perform error checks for IN and IN OUT parameters
4759
4760 if Ekind (F) /= E_Out_Parameter then
4761
4762 -- Check unset reference. For scalar parameters, it is clearly
4763 -- wrong to pass an uninitialized value as either an IN or
4764 -- IN-OUT parameter. For composites, it is also clearly an
4765 -- error to pass a completely uninitialized value as an IN
4766 -- parameter, but the case of IN OUT is trickier. We prefer
4767 -- not to give a warning here. For example, suppose there is
4768 -- a routine that sets some component of a record to False.
4769 -- It is perfectly reasonable to make this IN-OUT and allow
4770 -- either initialized or uninitialized records to be passed
4771 -- in this case.
4772
4773 -- For partially initialized composite values, we also avoid
4774 -- warnings, since it is quite likely that we are passing a
4775 -- partially initialized value and only the initialized fields
4776 -- will in fact be read in the subprogram.
4777
4778 if Is_Scalar_Type (A_Typ)
4779 or else (Ekind (F) = E_In_Parameter
4780 and then not Is_Partially_Initialized_Type (A_Typ))
4781 then
4782 Check_Unset_Reference (A);
4783 end if;
4784
4785 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
4786 -- actual to a nested call, since this constitutes a reading of
4787 -- the parameter, which is not allowed.
4788
4789 if Ada_Version = Ada_83
4790 and then Is_Entity_Name (A)
4791 and then Ekind (Entity (A)) = E_Out_Parameter
4792 then
4793 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
4794 end if;
4795 end if;
4796
4797 -- In -gnatd.q mode, forget that a given array is constant when
4798 -- it is passed as an IN parameter to a foreign-convention
4799 -- subprogram. This is in case the subprogram evilly modifies the
4800 -- object. Of course, correct code would use IN OUT.
4801
4802 if Debug_Flag_Dot_Q
4803 and then Ekind (F) = E_In_Parameter
4804 and then Has_Foreign_Convention (Nam)
4805 and then Is_Array_Type (F_Typ)
4806 and then Nkind (A) in N_Has_Entity
4807 and then Present (Entity (A))
4808 then
4809 Set_Is_True_Constant (Entity (A), False);
4810 end if;
4811
4812 -- Case of OUT or IN OUT parameter
4813
4814 if Ekind (F) /= E_In_Parameter then
4815
4816 -- For an Out parameter, check for useless assignment. Note
4817 -- that we can't set Last_Assignment this early, because we may
4818 -- kill current values in Resolve_Call, and that call would
4819 -- clobber the Last_Assignment field.
4820
4821 -- Note: call Warn_On_Useless_Assignment before doing the check
4822 -- below for Is_OK_Variable_For_Out_Formal so that the setting
4823 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
4824 -- reflects the last assignment, not this one.
4825
4826 if Ekind (F) = E_Out_Parameter then
4827 if Warn_On_Modified_As_Out_Parameter (F)
4828 and then Is_Entity_Name (A)
4829 and then Present (Entity (A))
4830 and then Comes_From_Source (N)
4831 then
4832 Warn_On_Useless_Assignment (Entity (A), A);
4833 end if;
4834 end if;
4835
4836 -- Validate the form of the actual. Note that the call to
4837 -- Is_OK_Variable_For_Out_Formal generates the required
4838 -- reference in this case.
4839
4840 -- A call to an initialization procedure for an aggregate
4841 -- component may initialize a nested component of a constant
4842 -- designated object. In this context the object is variable.
4843
4844 if not Is_OK_Variable_For_Out_Formal (A)
4845 and then not Is_Init_Proc (Nam)
4846 then
4847 Error_Msg_NE ("actual for& must be a variable", A, F);
4848
4849 if Is_Subprogram (Current_Scope) then
4850 if Is_Invariant_Procedure (Current_Scope)
4851 or else Is_Partial_Invariant_Procedure (Current_Scope)
4852 then
4853 Error_Msg_N
4854 ("function used in invariant cannot modify its "
4855 & "argument", F);
4856
4857 elsif Is_Predicate_Function (Current_Scope) then
4858 Error_Msg_N
4859 ("function used in predicate cannot modify its "
4860 & "argument", F);
4861 end if;
4862 end if;
4863 end if;
4864
4865 -- What's the following about???
4866
4867 if Is_Entity_Name (A) then
4868 Kill_Checks (Entity (A));
4869 else
4870 Kill_All_Checks;
4871 end if;
4872 end if;
4873
4874 if A_Typ = Any_Type then
4875 Set_Etype (N, Any_Type);
4876 return;
4877 end if;
4878
4879 -- Apply appropriate constraint/predicate checks for IN [OUT] case
4880
4881 if Ekind (F) in E_In_Parameter | E_In_Out_Parameter then
4882
4883 -- Apply predicate tests except in certain special cases. Note
4884 -- that it might be more consistent to apply these only when
4885 -- expansion is active (in Exp_Ch6.Expand_Actuals), as we do
4886 -- for the outbound predicate tests ??? In any case indicate
4887 -- the function being called, for better warnings if the call
4888 -- leads to an infinite recursion.
4889
4890 if Predicate_Tests_On_Arguments (Nam) then
4891 Apply_Predicate_Check (A, F_Typ, Fun => Nam);
4892 end if;
4893
4894 -- Apply required constraint checks
4895
4896 if Is_Scalar_Type (A_Typ) then
4897 Apply_Scalar_Range_Check (A, F_Typ);
4898
4899 elsif Is_Array_Type (A_Typ) then
4900 Apply_Length_Check (A, F_Typ);
4901
4902 elsif Is_Record_Type (F_Typ)
4903 and then Has_Discriminants (F_Typ)
4904 and then Is_Constrained (F_Typ)
4905 and then (not Is_Derived_Type (F_Typ)
4906 or else Comes_From_Source (Nam))
4907 then
4908 Apply_Discriminant_Check (A, F_Typ);
4909
4910 -- For view conversions of a discriminated object, apply
4911 -- check to object itself, the conversion alreay has the
4912 -- proper type.
4913
4914 if Nkind (A) = N_Type_Conversion
4915 and then Is_Constrained (Etype (Expression (A)))
4916 then
4917 Apply_Discriminant_Check (Expression (A), F_Typ);
4918 end if;
4919
4920 elsif Is_Access_Type (F_Typ)
4921 and then Is_Array_Type (Designated_Type (F_Typ))
4922 and then Is_Constrained (Designated_Type (F_Typ))
4923 then
4924 Apply_Length_Check (A, F_Typ);
4925
4926 elsif Is_Access_Type (F_Typ)
4927 and then Has_Discriminants (Designated_Type (F_Typ))
4928 and then Is_Constrained (Designated_Type (F_Typ))
4929 then
4930 Apply_Discriminant_Check (A, F_Typ);
4931
4932 else
4933 Apply_Range_Check (A, F_Typ);
4934 end if;
4935
4936 -- Ada 2005 (AI-231): Note that the controlling parameter case
4937 -- already existed in Ada 95, which is partially checked
4938 -- elsewhere (see Checks), and we don't want the warning
4939 -- message to differ.
4940
4941 if Is_Access_Type (F_Typ)
4942 and then Can_Never_Be_Null (F_Typ)
4943 and then Known_Null (A)
4944 then
4945 if Is_Controlling_Formal (F) then
4946 Apply_Compile_Time_Constraint_Error
4947 (N => A,
4948 Msg => "null value not allowed here??",
4949 Reason => CE_Access_Check_Failed);
4950
4951 elsif Ada_Version >= Ada_2005 then
4952 Apply_Compile_Time_Constraint_Error
4953 (N => A,
4954 Msg => "(Ada 2005) NULL not allowed in "
4955 & "null-excluding formal??",
4956 Reason => CE_Null_Not_Allowed);
4957 end if;
4958 end if;
4959
4960 -- In a prefixed call, if the prefix is an access type
4961 -- it cannot be null.
4962
4963 if Is_Access_Type (F_Typ)
4964 and then A = First_Actual (N)
4965 and then Is_Expanded_Prefixed_Call (N)
4966 then
4967 if not Is_Access_Type (A_Typ)
4968 and then not Is_Aliased_View (A)
4969 then
4970 Error_Msg_NE
4971 ("object in prefixed call to& must be aliased "
4972 & "(RM 4.1.3 (13 1/2))",
4973 A, Nam);
4974 end if;
4975
4976 if not Debug_Flag_Underscore_PP
4977 and then
4978 (Is_Controlling_Formal (F)
4979 or else Is_Class_Wide_Type (Designated_Type (F_Typ)))
4980 then
4981 Install_Null_Excluding_Check (A);
4982 end if;
4983 end if;
4984 end if;
4985
4986 -- Checks for OUT parameters and IN OUT parameters
4987
4988 if Ekind (F) in E_Out_Parameter | E_In_Out_Parameter then
4989
4990 -- If there is a type conversion, make sure the return value
4991 -- meets the constraints of the variable before the conversion.
4992
4993 if Nkind (A) = N_Type_Conversion then
4994 if Is_Scalar_Type (A_Typ) then
4995
4996 -- Special case here tailored to Exp_Ch6.Is_Legal_Copy,
4997 -- which would prevent the check from being generated.
4998 -- This is for Starlet only though, so long obsolete.
4999
5000 if Mechanism (F) = By_Reference
5001 and then Ekind (Nam) = E_Procedure
5002 and then Is_Valued_Procedure (Nam)
5003 then
5004 null;
5005 else
5006 Apply_Scalar_Range_Check
5007 (Expression (A), Etype (Expression (A)), A_Typ);
5008 end if;
5009
5010 -- In addition the return value must meet the constraints
5011 -- of the object type (see the comment below).
5012
5013 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
5014
5015 else
5016 Apply_Range_Check
5017 (Expression (A), Etype (Expression (A)), A_Typ);
5018 end if;
5019
5020 -- If no conversion, apply scalar range checks and length check
5021 -- based on the subtype of the actual (NOT that of the formal).
5022 -- This indicates that the check takes place on return from the
5023 -- call. During expansion the required constraint checks are
5024 -- inserted. In GNATprove mode, in the absence of expansion,
5025 -- the flag indicates that the returned value is valid.
5026
5027 else
5028 if Is_Scalar_Type (F_Typ) then
5029 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
5030
5031 elsif Is_Array_Type (F_Typ)
5032 and then Ekind (F) = E_Out_Parameter
5033 then
5034 Apply_Length_Check (A, F_Typ);
5035
5036 else
5037 Apply_Range_Check (A, A_Typ, F_Typ);
5038 end if;
5039 end if;
5040
5041 -- Note: we do not apply the predicate checks for the case of
5042 -- OUT and IN OUT parameters. They are instead applied in the
5043 -- Expand_Actuals routine in Exp_Ch6.
5044 end if;
5045
5046 -- If the formal is of an unconstrained array subtype with fixed
5047 -- lower bound, then sliding to that bound may be needed.
5048
5049 if Is_Fixed_Lower_Bound_Array_Subtype (F_Typ) then
5050 Expand_Sliding_Conversion (A, F_Typ);
5051 end if;
5052
5053 -- An actual associated with an access parameter is implicitly
5054 -- converted to the anonymous access type of the formal and must
5055 -- satisfy the legality checks for access conversions.
5056
5057 if Ekind (F_Typ) = E_Anonymous_Access_Type then
5058 if not Valid_Conversion (A, F_Typ, A) then
5059 Error_Msg_N
5060 ("invalid implicit conversion for access parameter", A);
5061 end if;
5062
5063 -- If the actual is an access selected component of a variable,
5064 -- the call may modify its designated object. It is reasonable
5065 -- to treat this as a potential modification of the enclosing
5066 -- record, to prevent spurious warnings that it should be
5067 -- declared as a constant, because intuitively programmers
5068 -- regard the designated subcomponent as part of the record.
5069
5070 if Nkind (A) = N_Selected_Component
5071 and then Is_Entity_Name (Prefix (A))
5072 and then not Is_Constant_Object (Entity (Prefix (A)))
5073 then
5074 Note_Possible_Modification (A, Sure => False);
5075 end if;
5076 end if;
5077
5078 -- Check illegal cases of atomic/volatile/VFA actual (RM C.6(12))
5079
5080 if (Is_By_Reference_Type (F_Typ) or else Is_Aliased (F))
5081 and then Comes_From_Source (N)
5082 then
5083 if Is_Atomic_Object (A)
5084 and then not Is_Atomic (F_Typ)
5085 then
5086 Error_Msg_NE
5087 ("cannot pass atomic object to nonatomic formal&",
5088 A, F);
5089 Error_Msg_N
5090 ("\which is passed by reference (RM C.6(12))", A);
5091
5092 elsif Is_Volatile_Object_Ref (A)
5093 and then not Is_Volatile (F_Typ)
5094 then
5095 Error_Msg_NE
5096 ("cannot pass volatile object to nonvolatile formal&",
5097 A, F);
5098 Error_Msg_N
5099 ("\which is passed by reference (RM C.6(12))", A);
5100
5101 elsif Is_Volatile_Full_Access_Object_Ref (A)
5102 and then not Is_Volatile_Full_Access (F_Typ)
5103 then
5104 Error_Msg_NE
5105 ("cannot pass full access object to nonfull access "
5106 & "formal&", A, F);
5107 Error_Msg_N
5108 ("\which is passed by reference (RM C.6(12))", A);
5109 end if;
5110
5111 -- Check for nonatomic subcomponent of a full access object
5112 -- in Ada 2022 (RM C.6 (12)).
5113
5114 if Ada_Version >= Ada_2022
5115 and then Is_Subcomponent_Of_Full_Access_Object (A)
5116 and then not Is_Atomic_Object (A)
5117 then
5118 Error_Msg_N
5119 ("cannot pass nonatomic subcomponent of full access "
5120 & "object", A);
5121 Error_Msg_NE
5122 ("\to formal & which is passed by reference (RM C.6(12))",
5123 A, F);
5124 end if;
5125 end if;
5126
5127 -- Check that subprograms don't have improper controlling
5128 -- arguments (RM 3.9.2 (9)).
5129
5130 -- A primitive operation may have an access parameter of an
5131 -- incomplete tagged type, but a dispatching call is illegal
5132 -- if the type is still incomplete.
5133
5134 if Is_Controlling_Formal (F) then
5135 Set_Is_Controlling_Actual (A);
5136
5137 if Ekind (F_Typ) = E_Anonymous_Access_Type then
5138 declare
5139 Desig : constant Entity_Id := Designated_Type (F_Typ);
5140 begin
5141 if Ekind (Desig) = E_Incomplete_Type
5142 and then No (Full_View (Desig))
5143 and then No (Non_Limited_View (Desig))
5144 then
5145 Error_Msg_NE
5146 ("premature use of incomplete type& "
5147 & "in dispatching call", A, Desig);
5148 end if;
5149 end;
5150 end if;
5151
5152 elsif Nkind (A) = N_Explicit_Dereference then
5153 Validate_Remote_Access_To_Class_Wide_Type (A);
5154 end if;
5155
5156 -- Apply legality rule 3.9.2 (9/1)
5157
5158 -- Skip this check on helpers and indirect-call wrappers built to
5159 -- support class-wide preconditions.
5160
5161 -- We make special exception here for mutably tagged types and
5162 -- related calls to their initialization procedures.
5163
5164 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
5165 and then not Is_Class_Wide_Type (F_Typ)
5166 and then not Is_Controlling_Formal (F)
5167 and then not In_Instance
5168 and then (not Is_Subprogram (Nam)
5169 or else No (Class_Preconditions_Subprogram (Nam)))
5170
5171 -- Ignore mutably tagged types and their use in calls to init
5172 -- procs.
5173
5174 and then not Is_Mutably_Tagged_CW_Equivalent_Type (A_Typ)
5175 and then not Is_Init_Proc (Nam)
5176 then
5177 Error_Msg_N ("class-wide argument not allowed here!", A);
5178
5179 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
5180 Error_Msg_Node_2 := F_Typ;
5181 Error_Msg_NE
5182 ("& is not a dispatching operation of &!", A, Nam);
5183 end if;
5184
5185 -- Apply the checks described in 3.10.2(27): if the context is a
5186 -- specific access-to-object, the actual cannot be class-wide.
5187 -- Use base type to exclude access_to_subprogram cases.
5188
5189 elsif Is_Access_Type (A_Typ)
5190 and then Is_Access_Type (F_Typ)
5191 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
5192 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
5193 or else (Nkind (A) = N_Attribute_Reference
5194 and then
5195 Is_Class_Wide_Type (Etype (Prefix (A)))))
5196 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
5197 and then not Is_Controlling_Formal (F)
5198
5199 -- Disable these checks for call to imported C++ subprograms
5200
5201 and then not
5202 (Is_Entity_Name (Name (N))
5203 and then Is_Imported (Entity (Name (N)))
5204 and then Convention (Entity (Name (N))) = Convention_CPP)
5205 then
5206 Error_Msg_N
5207 ("access to class-wide argument not allowed here!", A);
5208
5209 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
5210 Error_Msg_Node_2 := Designated_Type (F_Typ);
5211 Error_Msg_NE
5212 ("& is not a dispatching operation of &!", A, Nam);
5213 end if;
5214 end if;
5215
5216 Check_Aliased_Parameter;
5217
5218 Eval_Actual (A);
5219
5220 -- If it is a named association, treat the selector_name as a
5221 -- proper identifier, and mark the corresponding entity.
5222
5223 if Nkind (Parent (A)) = N_Parameter_Association
5224
5225 -- Ignore reference in SPARK mode, as it refers to an entity not
5226 -- in scope at the point of reference, so the reference should
5227 -- be ignored for computing effects of subprograms.
5228
5229 and then not GNATprove_Mode
5230 then
5231 -- If subprogram is overridden, use name of formal that
5232 -- is being called.
5233
5234 if Present (Real_Subp) then
5235 Set_Entity (Selector_Name (Parent (A)), Real_F);
5236 Set_Etype (Selector_Name (Parent (A)), Etype (Real_F));
5237
5238 else
5239 Set_Entity (Selector_Name (Parent (A)), F);
5240 Generate_Reference (F, Selector_Name (Parent (A)));
5241 Set_Etype (Selector_Name (Parent (A)), F_Typ);
5242 Generate_Reference (F_Typ, N, ' ');
5243 end if;
5244 end if;
5245
5246 Prev := A;
5247
5248 if Ekind (F) /= E_Out_Parameter then
5249 Check_Unset_Reference (A);
5250 end if;
5251
5252 -- A formal parameter of a specific tagged type whose related
5253 -- subprogram is subject to pragma Extensions_Visible with value
5254 -- "False" cannot act as an actual in a subprogram with value
5255 -- "True" (SPARK RM 6.1.7(3)).
5256
5257 -- No check needed for helpers and indirect-call wrappers built to
5258 -- support class-wide preconditions.
5259
5260 if Is_EVF_Expression (A)
5261 and then Extensions_Visible_Status (Nam) =
5262 Extensions_Visible_True
5263 and then not
5264 (Is_Subprogram (Current_Scope)
5265 and then
5266 Present (Class_Preconditions_Subprogram (Current_Scope)))
5267 then
5268 Error_Msg_N
5269 ("formal parameter cannot act as actual parameter when "
5270 & "Extensions_Visible is False", A);
5271 Error_Msg_NE
5272 ("\subprogram & has Extensions_Visible True", A, Nam);
5273 end if;
5274
5275 -- The actual parameter of a Ghost subprogram whose formal is of
5276 -- mode IN OUT or OUT must be a Ghost variable (SPARK RM 6.9(12)).
5277
5278 if Comes_From_Source (Nam)
5279 and then Is_Ghost_Entity (Nam)
5280 and then Ekind (F) in E_In_Out_Parameter | E_Out_Parameter
5281 and then Is_Entity_Name (A)
5282 and then Present (Entity (A))
5283 and then not Is_Ghost_Entity (Entity (A))
5284 then
5285 Error_Msg_NE
5286 ("non-ghost variable & cannot appear as actual in call to "
5287 & "ghost procedure", A, Entity (A));
5288
5289 if Ekind (F) = E_In_Out_Parameter then
5290 Error_Msg_N ("\corresponding formal has mode `IN OUT`", A);
5291 else
5292 Error_Msg_N ("\corresponding formal has mode OUT", A);
5293 end if;
5294 end if;
5295
5296 -- (AI12-0397): The target of a subprogram call that occurs within
5297 -- the expression of an Default_Initial_Condition aspect and has
5298 -- an actual that is the current instance of the type must be
5299 -- either a primitive of the type or a class-wide subprogram,
5300 -- because the type of the current instance in such an aspect is
5301 -- considered to be a notional formal derived type whose only
5302 -- operations correspond to the primitives of the enclosing type.
5303 -- Nonprimitives can be called, but the current instance must be
5304 -- converted rather than passed directly. Note that a current
5305 -- instance of a type with DIC will occur as a reference to an
5306 -- in-mode formal of an enclosing DIC procedure or partial DIC
5307 -- procedure. (It seems that this check should perhaps also apply
5308 -- to calls within Type_Invariant'Class, but not Type_Invariant,
5309 -- aspects???)
5310
5311 if Nkind (A) = N_Identifier
5312 and then Ekind (Entity (A)) = E_In_Parameter
5313
5314 and then Is_Subprogram (Scope (Entity (A)))
5315 and then Is_DIC_Procedure (Scope (Entity (A)))
5316
5317 -- We check Comes_From_Source to exclude inherited primitives
5318 -- from being flagged, because such subprograms turn out to not
5319 -- always have the Is_Primitive flag set. ???
5320
5321 and then Comes_From_Source (Nam)
5322
5323 and then not Is_Primitive (Nam)
5324 and then not Is_Class_Wide_Type (F_Typ)
5325 then
5326 Error_Msg_NE
5327 ("call to nonprimitive & with current instance not allowed " &
5328 "for aspect", A, Nam);
5329 end if;
5330
5331 Next_Actual (A);
5332
5333 -- Case where actual is not present
5334
5335 else
5336 Insert_Default;
5337 end if;
5338
5339 Next_Formal (F);
5340
5341 if Present (Real_Subp) then
5342 Next_Formal (Real_F);
5343 end if;
5344 end loop;
5345 end Resolve_Actuals;
5346
5347 -----------------------
5348 -- Resolve_Allocator --
5349 -----------------------
5350
5351 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
5352 Desig_T : constant Entity_Id := Designated_Type (Typ);
5353 E : constant Node_Id := Expression (N);
5354 Subtyp : Entity_Id;
5355 Discrim : Entity_Id;
5356 Constr : Node_Id;
5357 Aggr : Node_Id;
5358 Assoc : Node_Id := Empty;
5359 Disc_Exp : Node_Id;
5360
5361 procedure Check_Allocator_Discrim_Accessibility
5362 (Disc_Exp : Node_Id;
5363 Alloc_Typ : Entity_Id);
5364 -- Check that accessibility level associated with an access discriminant
5365 -- initialized in an allocator by the expression Disc_Exp is not deeper
5366 -- than the level of the allocator type Alloc_Typ. An error message is
5367 -- issued if this condition is violated. Specialized checks are done for
5368 -- the cases of a constraint expression which is an access attribute or
5369 -- an access discriminant.
5370
5371 procedure Check_Allocator_Discrim_Accessibility_Exprs
5372 (Curr_Exp : Node_Id;
5373 Alloc_Typ : Entity_Id);
5374 -- Dispatch checks performed by Check_Allocator_Discrim_Accessibility
5375 -- across all expressions within a given conditional expression.
5376
5377 function In_Dispatching_Context return Boolean;
5378 -- If the allocator is an actual in a call, it is allowed to be class-
5379 -- wide when the context is not because it is a controlling actual.
5380
5381 -------------------------------------------
5382 -- Check_Allocator_Discrim_Accessibility --
5383 -------------------------------------------
5384
5385 procedure Check_Allocator_Discrim_Accessibility
5386 (Disc_Exp : Node_Id;
5387 Alloc_Typ : Entity_Id)
5388 is
5389 begin
5390 if Type_Access_Level (Etype (Disc_Exp)) >
5391 Deepest_Type_Access_Level (Alloc_Typ)
5392 then
5393 Error_Msg_N
5394 ("operand type has deeper level than allocator type", Disc_Exp);
5395
5396 -- When the expression is an Access attribute the level of the prefix
5397 -- object must not be deeper than that of the allocator's type.
5398
5399 elsif Nkind (Disc_Exp) = N_Attribute_Reference
5400 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
5401 Attribute_Access
5402 and then Static_Accessibility_Level
5403 (Disc_Exp, Zero_On_Dynamic_Level)
5404 > Deepest_Type_Access_Level (Alloc_Typ)
5405 then
5406 Error_Msg_N
5407 ("prefix of attribute has deeper level than allocator type",
5408 Disc_Exp);
5409
5410 -- When the expression is an access discriminant the check is against
5411 -- the level of the prefix object.
5412
5413 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
5414 and then Nkind (Disc_Exp) = N_Selected_Component
5415 and then Static_Accessibility_Level
5416 (Disc_Exp, Zero_On_Dynamic_Level)
5417 > Deepest_Type_Access_Level (Alloc_Typ)
5418 then
5419 Error_Msg_N
5420 ("access discriminant has deeper level than allocator type",
5421 Disc_Exp);
5422
5423 -- All other cases are legal
5424
5425 else
5426 null;
5427 end if;
5428 end Check_Allocator_Discrim_Accessibility;
5429
5430 -------------------------------------------------
5431 -- Check_Allocator_Discrim_Accessibility_Exprs --
5432 -------------------------------------------------
5433
5434 procedure Check_Allocator_Discrim_Accessibility_Exprs
5435 (Curr_Exp : Node_Id;
5436 Alloc_Typ : Entity_Id)
5437 is
5438 Alt : Node_Id;
5439 Expr : Node_Id;
5440 Disc_Exp : constant Node_Id := Original_Node (Curr_Exp);
5441 begin
5442 -- When conditional expressions are constant folded we know at
5443 -- compile time which expression to check - so don't bother with
5444 -- the rest of the cases.
5445
5446 if Nkind (Curr_Exp) = N_Attribute_Reference then
5447 Check_Allocator_Discrim_Accessibility (Curr_Exp, Alloc_Typ);
5448
5449 -- Non-constant-folded if expressions
5450
5451 elsif Nkind (Disc_Exp) = N_If_Expression then
5452 -- Check both expressions if they are still present in the face
5453 -- of expansion.
5454
5455 Expr := Next (First (Expressions (Disc_Exp)));
5456 if Present (Expr) then
5457 Check_Allocator_Discrim_Accessibility_Exprs (Expr, Alloc_Typ);
5458 Next (Expr);
5459 if Present (Expr) then
5460 Check_Allocator_Discrim_Accessibility_Exprs
5461 (Expr, Alloc_Typ);
5462 end if;
5463 end if;
5464
5465 -- Non-constant-folded case expressions
5466
5467 elsif Nkind (Disc_Exp) = N_Case_Expression then
5468 -- Check all alternatives
5469
5470 Alt := First (Alternatives (Disc_Exp));
5471 while Present (Alt) loop
5472 Check_Allocator_Discrim_Accessibility_Exprs
5473 (Expression (Alt), Alloc_Typ);
5474
5475 Next (Alt);
5476 end loop;
5477
5478 -- Base case, check the accessibility of the original node of the
5479 -- expression.
5480
5481 else
5482 Check_Allocator_Discrim_Accessibility (Disc_Exp, Alloc_Typ);
5483 end if;
5484 end Check_Allocator_Discrim_Accessibility_Exprs;
5485
5486 ----------------------------
5487 -- In_Dispatching_Context --
5488 ----------------------------
5489
5490 function In_Dispatching_Context return Boolean is
5491 Par : constant Node_Id := Parent (N);
5492
5493 begin
5494 return Nkind (Par) in N_Subprogram_Call
5495 and then Is_Entity_Name (Name (Par))
5496 and then Is_Dispatching_Operation (Entity (Name (Par)));
5497 end In_Dispatching_Context;
5498
5499 -- Start of processing for Resolve_Allocator
5500
5501 begin
5502 -- Replace general access with specific type
5503
5504 if Ekind (Etype (N)) = E_Allocator_Type then
5505 Set_Etype (N, Base_Type (Typ));
5506 end if;
5507
5508 if Is_Abstract_Type (Typ) then
5509 Error_Msg_N ("type of allocator cannot be abstract", N);
5510 end if;
5511
5512 -- For qualified expression, resolve the expression using the given
5513 -- subtype (nothing to do for type mark, subtype indication)
5514
5515 if Nkind (E) = N_Qualified_Expression then
5516 if Is_Class_Wide_Type (Etype (E))
5517 and then not Is_Class_Wide_Type (Desig_T)
5518 and then not In_Dispatching_Context
5519 then
5520 Error_Msg_N
5521 ("class-wide allocator not allowed for this access type", N);
5522 end if;
5523
5524 -- Do a full resolution to apply constraint and predicate checks
5525
5526 Resolve_Qualified_Expression (E, Etype (E));
5527 Check_Unset_Reference (Expression (E));
5528
5529 -- Allocators generated by the build-in-place expansion mechanism
5530 -- are explicitly marked as coming from source but do not need to be
5531 -- checked for limited initialization. To exclude this case, ensure
5532 -- that the parent of the allocator is a source node.
5533 -- The return statement constructed for an Expression_Function does
5534 -- not come from source but requires a limited check.
5535
5536 if Is_Limited_Type (Etype (E))
5537 and then Comes_From_Source (N)
5538 and then
5539 (Comes_From_Source (Parent (N))
5540 or else
5541 (Ekind (Current_Scope) = E_Function
5542 and then Nkind (Original_Node (Unit_Declaration_Node
5543 (Current_Scope))) = N_Expression_Function))
5544 and then not In_Instance_Body
5545 then
5546 if not OK_For_Limited_Init (Etype (E), Expression (E)) then
5547 if Nkind (Parent (N)) = N_Assignment_Statement then
5548 Error_Msg_N
5549 ("illegal expression for initialized allocator of a "
5550 & "limited type (RM 7.5 (2.7/2))", N);
5551 else
5552 Error_Msg_N
5553 ("initialization not allowed for limited types", N);
5554 end if;
5555
5556 Explain_Limited_Type (Etype (E), N);
5557 end if;
5558 end if;
5559
5560 -- Calls to build-in-place functions are not currently supported in
5561 -- allocators for access types associated with a simple storage pool.
5562 -- Supporting such allocators may require passing additional implicit
5563 -- parameters to build-in-place functions (or a significant revision
5564 -- of the current b-i-p implementation to unify the handling for
5565 -- multiple kinds of storage pools). ???
5566
5567 if Is_Inherently_Limited_Type (Desig_T)
5568 and then Nkind (Expression (E)) = N_Function_Call
5569 then
5570 declare
5571 Pool : constant Entity_Id :=
5572 Associated_Storage_Pool (Root_Type (Typ));
5573 begin
5574 if Present (Pool)
5575 and then
5576 Present (Get_Rep_Pragma
5577 (Etype (Pool), Name_Simple_Storage_Pool_Type))
5578 then
5579 Error_Msg_N
5580 ("limited function calls not yet supported in simple "
5581 & "storage pool allocators", Expression (E));
5582 end if;
5583 end;
5584 end if;
5585
5586 -- A special accessibility check is needed for allocators that
5587 -- constrain access discriminants. The level of the type of the
5588 -- expression used to constrain an access discriminant cannot be
5589 -- deeper than the type of the allocator (in contrast to access
5590 -- parameters, where the level of the actual can be arbitrary).
5591
5592 -- We can't use Valid_Conversion to perform this check because in
5593 -- general the type of the allocator is unrelated to the type of
5594 -- the access discriminant.
5595
5596 if Ekind (Typ) /= E_Anonymous_Access_Type
5597 or else Is_Local_Anonymous_Access (Typ)
5598 then
5599 Subtyp := Entity (Subtype_Mark (E));
5600
5601 Aggr := Original_Node (Expression (E));
5602
5603 if Has_Discriminants (Subtyp)
5604 and then Nkind (Aggr) in N_Aggregate | N_Extension_Aggregate
5605 then
5606 Discrim := First_Discriminant (Base_Type (Subtyp));
5607
5608 -- Get the first component expression of the aggregate
5609
5610 if Present (Expressions (Aggr)) then
5611 Disc_Exp := First (Expressions (Aggr));
5612
5613 elsif Present (Component_Associations (Aggr)) then
5614 Assoc := First (Component_Associations (Aggr));
5615
5616 if Present (Assoc) then
5617 Disc_Exp := Expression (Assoc);
5618 else
5619 Disc_Exp := Empty;
5620 end if;
5621
5622 else
5623 Disc_Exp := Empty;
5624 end if;
5625
5626 while Present (Discrim) and then Present (Disc_Exp) loop
5627 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
5628 Check_Allocator_Discrim_Accessibility_Exprs
5629 (Disc_Exp, Typ);
5630 end if;
5631
5632 Next_Discriminant (Discrim);
5633
5634 if Present (Discrim) then
5635 if Present (Assoc) then
5636 Next (Assoc);
5637 Disc_Exp := Expression (Assoc);
5638
5639 elsif Present (Next (Disc_Exp)) then
5640 Next (Disc_Exp);
5641
5642 else
5643 Assoc := First (Component_Associations (Aggr));
5644
5645 if Present (Assoc) then
5646 Disc_Exp := Expression (Assoc);
5647 else
5648 Disc_Exp := Empty;
5649 end if;
5650 end if;
5651 end if;
5652 end loop;
5653 end if;
5654 end if;
5655
5656 -- For a subtype mark or subtype indication, freeze the subtype
5657
5658 else
5659 Freeze_Expression (E);
5660
5661 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
5662 Error_Msg_N
5663 ("initialization required for access-to-constant allocator", N);
5664 end if;
5665
5666 -- A special accessibility check is needed for allocators that
5667 -- constrain access discriminants. The level of the type of the
5668 -- expression used to constrain an access discriminant cannot be
5669 -- deeper than the type of the allocator (in contrast to access
5670 -- parameters, where the level of the actual can be arbitrary).
5671 -- We can't use Valid_Conversion to perform this check because
5672 -- in general the type of the allocator is unrelated to the type
5673 -- of the access discriminant.
5674
5675 if Nkind (Original_Node (E)) = N_Subtype_Indication
5676 and then (Ekind (Typ) /= E_Anonymous_Access_Type
5677 or else Is_Local_Anonymous_Access (Typ))
5678 then
5679 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
5680
5681 if Has_Discriminants (Subtyp) then
5682 Discrim := First_Discriminant (Base_Type (Subtyp));
5683 Constr := First (Constraints (Constraint (Original_Node (E))));
5684 while Present (Discrim) and then Present (Constr) loop
5685 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
5686 if Nkind (Constr) = N_Discriminant_Association then
5687 Disc_Exp := Expression (Constr);
5688 else
5689 Disc_Exp := Constr;
5690 end if;
5691
5692 Check_Allocator_Discrim_Accessibility_Exprs
5693 (Disc_Exp, Typ);
5694 end if;
5695
5696 Next_Discriminant (Discrim);
5697 Next (Constr);
5698 end loop;
5699 end if;
5700 end if;
5701 end if;
5702
5703 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
5704 -- check that the level of the type of the created object is not deeper
5705 -- than the level of the allocator's access type, since extensions can
5706 -- now occur at deeper levels than their ancestor types. This is a
5707 -- static accessibility level check; a run-time check is also needed in
5708 -- the case of an initialized allocator with a class-wide argument (see
5709 -- Expand_Allocator_Expression).
5710
5711 if Ada_Version >= Ada_2005
5712 and then Is_Class_Wide_Type (Desig_T)
5713 then
5714 declare
5715 Exp_Typ : Entity_Id;
5716
5717 begin
5718 if Nkind (E) = N_Qualified_Expression then
5719 Exp_Typ := Etype (E);
5720 elsif Nkind (E) = N_Subtype_Indication then
5721 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
5722 else
5723 Exp_Typ := Entity (E);
5724 end if;
5725
5726 if Type_Access_Level (Exp_Typ) >
5727 Deepest_Type_Access_Level (Typ)
5728 then
5729 if In_Instance_Body then
5730 Error_Msg_Warn := SPARK_Mode /= On;
5731 Error_Msg_N
5732 ("type in allocator has deeper level than designated "
5733 & "class-wide type<<", E);
5734 Error_Msg_N ("\Program_Error [<<", E);
5735
5736 Rewrite (N,
5737 Make_Raise_Program_Error (Sloc (N),
5738 Reason => PE_Accessibility_Check_Failed));
5739 Set_Etype (N, Typ);
5740
5741 -- Do not apply Ada 2005 accessibility checks on a class-wide
5742 -- allocator if the type given in the allocator is a formal
5743 -- type or within a formal package. A run-time check will be
5744 -- performed in the instance.
5745
5746 elsif not Is_Generic_Type (Exp_Typ)
5747 and then not In_Generic_Formal_Package (Exp_Typ)
5748 then
5749 Error_Msg_N
5750 ("type in allocator has deeper level than designated "
5751 & "class-wide type", E);
5752 end if;
5753 end if;
5754 end;
5755 end if;
5756
5757 -- Check for allocation from an empty storage pool. But do not complain
5758 -- if it's a return statement for a build-in-place function, because the
5759 -- allocator is there just in case the caller uses an allocator. If the
5760 -- caller does use an allocator, it will be caught at the call site.
5761
5762 if No_Pool_Assigned (Typ)
5763 and then not For_Special_Return_Object (N)
5764 then
5765 Error_Msg_N ("allocation from empty storage pool!", N);
5766
5767 -- If the context is an unchecked conversion, as may happen within an
5768 -- inlined subprogram, the allocator is being resolved with its own
5769 -- anonymous type. In that case, if the target type has a specific
5770 -- storage pool, it must be inherited explicitly by the allocator type.
5771
5772 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
5773 and then No (Associated_Storage_Pool (Typ))
5774 then
5775 Set_Associated_Storage_Pool
5776 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
5777 end if;
5778
5779 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
5780 Check_Restriction (No_Anonymous_Allocators, N);
5781 end if;
5782
5783 -- Check that an allocator with task parts isn't for a nested access
5784 -- type when restriction No_Task_Hierarchy applies.
5785
5786 if not Is_Library_Level_Entity (Base_Type (Typ))
5787 and then Has_Task (Base_Type (Desig_T))
5788 then
5789 Check_Restriction (No_Task_Hierarchy, N);
5790 end if;
5791
5792 -- An illegal allocator may be rewritten as a raise Program_Error
5793 -- statement.
5794
5795 if Nkind (N) = N_Allocator then
5796
5797 -- Avoid coextension processing for an allocator that is the
5798 -- expansion of a build-in-place function call.
5799
5800 if Nkind (Original_Node (N)) = N_Allocator
5801 and then Nkind (Expression (Original_Node (N))) =
5802 N_Qualified_Expression
5803 and then Nkind (Expression (Expression (Original_Node (N)))) =
5804 N_Function_Call
5805 and then Is_Expanded_Build_In_Place_Call
5806 (Expression (Expression (Original_Node (N))))
5807 then
5808 null; -- b-i-p function call case
5809
5810 else
5811 -- An anonymous access discriminant is the definition of a
5812 -- coextension.
5813
5814 if Ekind (Typ) = E_Anonymous_Access_Type
5815 and then Nkind (Associated_Node_For_Itype (Typ)) =
5816 N_Discriminant_Specification
5817 then
5818 Check_Restriction (No_Coextensions, N);
5819
5820 declare
5821 Discr : constant Entity_Id :=
5822 Defining_Identifier (Associated_Node_For_Itype (Typ));
5823
5824 begin
5825 -- Ada 2012 AI05-0052: If the designated type of the
5826 -- allocator is limited, then the allocator shall not
5827 -- be used to define the value of an access discriminant
5828 -- unless the discriminated type is immutably limited.
5829
5830 if Ada_Version >= Ada_2012
5831 and then Is_Limited_Type (Desig_T)
5832 and then not Is_Inherently_Limited_Type (Scope (Discr))
5833 then
5834 Error_Msg_N
5835 ("only immutably limited types can have anonymous "
5836 & "access discriminants designating a limited type",
5837 N);
5838 end if;
5839 end;
5840
5841 -- Avoid marking an allocator as a dynamic coextension if it is
5842 -- within a static construct.
5843
5844 if not Is_Static_Coextension (N) then
5845 Set_Is_Dynamic_Coextension (N);
5846
5847 -- Finalization and deallocation of coextensions utilizes an
5848 -- approximate implementation which does not directly adhere
5849 -- to the semantic rules. Warn on potential issues involving
5850 -- coextensions.
5851
5852 if Is_Controlled (Desig_T) then
5853 Error_Msg_N
5854 ("??coextension will not be finalized when its "
5855 & "associated owner is deallocated or finalized", N);
5856 else
5857 Error_Msg_N
5858 ("??coextension will not be deallocated when its "
5859 & "associated owner is deallocated", N);
5860 end if;
5861 end if;
5862
5863 -- Cleanup for potential static coextensions
5864
5865 else
5866 Set_Is_Dynamic_Coextension (N, False);
5867 Set_Is_Static_Coextension (N, False);
5868
5869 -- Objects allocated through anonymous access types are not
5870 -- finalized on time because this involves run-time ownership
5871 -- and currently this property is not available. In rare cases
5872 -- the object might not be finalized at all. Warn on potential
5873 -- issues involving anonymous access-to-controlled types.
5874
5875 if Ekind (Typ) = E_Anonymous_Access_Type
5876 and then Is_Controlled_Active (Desig_T)
5877 then
5878 Error_Msg_N
5879 ("??object designated by anonymous access value might "
5880 & "not be finalized until its enclosing library unit "
5881 & "goes out of scope, or not be finalized at all", N);
5882 Error_Msg_N ("\use named access type instead", N);
5883 end if;
5884 end if;
5885 end if;
5886 end if;
5887
5888 -- Report a simple error: if the designated object is a local task,
5889 -- its body has not been seen yet, and its activation will fail an
5890 -- elaboration check.
5891
5892 if Is_Task_Type (Desig_T)
5893 and then Scope (Base_Type (Desig_T)) = Current_Scope
5894 and then Is_Compilation_Unit (Current_Scope)
5895 and then Ekind (Current_Scope) = E_Package
5896 and then not In_Package_Body (Current_Scope)
5897 then
5898 Error_Msg_Warn := SPARK_Mode /= On;
5899 Error_Msg_N ("cannot activate task before body seen<<", N);
5900 Error_Msg_N ("\Program_Error [<<", N);
5901 end if;
5902
5903 -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a
5904 -- type with a task component on a subpool. This action must raise
5905 -- Program_Error at runtime.
5906
5907 if Ada_Version >= Ada_2012
5908 and then Nkind (N) = N_Allocator
5909 and then Present (Subpool_Handle_Name (N))
5910 and then Has_Task (Desig_T)
5911 then
5912 Error_Msg_Warn := SPARK_Mode /= On;
5913 Error_Msg_N ("cannot allocate task on subpool<<", N);
5914 Error_Msg_N ("\Program_Error [<<", N);
5915
5916 Rewrite (N,
5917 Make_Raise_Program_Error (Sloc (N),
5918 Reason => PE_Explicit_Raise));
5919 Set_Etype (N, Typ);
5920 end if;
5921 end Resolve_Allocator;
5922
5923 ---------------------------
5924 -- Resolve_Arithmetic_Op --
5925 ---------------------------
5926
5927 -- Used for resolving all arithmetic operators except exponentiation
5928
5929 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
5930 L : constant Node_Id := Left_Opnd (N);
5931 R : constant Node_Id := Right_Opnd (N);
5932 TL : constant Entity_Id := Base_Type (Etype (L));
5933 TR : constant Entity_Id := Base_Type (Etype (R));
5934 T : Entity_Id;
5935 Rop : Node_Id;
5936
5937 B_Typ : constant Entity_Id := Base_Type (Typ);
5938 -- We do the resolution using the base type, because intermediate values
5939 -- in expressions always are of the base type, not a subtype of it.
5940
5941 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
5942 -- Returns True if N is in a context that expects "any real type"
5943
5944 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
5945 -- Return True iff given type is Integer or universal real/integer
5946
5947 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
5948 -- Choose type of integer literal in fixed-point operation to conform
5949 -- to available fixed-point type. T is the type of the other operand,
5950 -- which is needed to determine the expected type of N.
5951
5952 procedure Set_Operand_Type (N : Node_Id);
5953 -- Set operand type to T if universal
5954
5955 -------------------------------
5956 -- Expected_Type_Is_Any_Real --
5957 -------------------------------
5958
5959 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
5960 begin
5961 -- N is the expression after "delta" in a fixed_point_definition;
5962 -- see RM-3.5.9(6):
5963
5964 return Nkind (Parent (N)) in N_Ordinary_Fixed_Point_Definition
5965 | N_Decimal_Fixed_Point_Definition
5966
5967 -- N is one of the bounds in a real_range_specification;
5968 -- see RM-3.5.7(5):
5969
5970 | N_Real_Range_Specification
5971
5972 -- N is the expression of a delta_constraint;
5973 -- see RM-J.3(3):
5974
5975 | N_Delta_Constraint;
5976 end Expected_Type_Is_Any_Real;
5977
5978 -----------------------------
5979 -- Is_Integer_Or_Universal --
5980 -----------------------------
5981
5982 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
5983 T : Entity_Id;
5984 Index : Interp_Index;
5985 It : Interp;
5986
5987 begin
5988 if not Is_Overloaded (N) then
5989 T := Etype (N);
5990 return Base_Type (T) = Base_Type (Standard_Integer)
5991 or else Is_Universal_Numeric_Type (T);
5992 else
5993 Get_First_Interp (N, Index, It);
5994 while Present (It.Typ) loop
5995 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
5996 or else Is_Universal_Numeric_Type (It.Typ)
5997 then
5998 return True;
5999 end if;
6000
6001 Get_Next_Interp (Index, It);
6002 end loop;
6003 end if;
6004
6005 return False;
6006 end Is_Integer_Or_Universal;
6007
6008 ----------------------------
6009 -- Set_Mixed_Mode_Operand --
6010 ----------------------------
6011
6012 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
6013 Index : Interp_Index;
6014 It : Interp;
6015
6016 begin
6017 if Universal_Interpretation (N) = Universal_Integer then
6018
6019 -- A universal integer literal is resolved as standard integer
6020 -- except in the case of a fixed-point result, where we leave it
6021 -- as universal (to be handled by Exp_Fixd later on)
6022
6023 if Is_Fixed_Point_Type (T) then
6024 Resolve (N, Universal_Integer);
6025 else
6026 Resolve (N, Standard_Integer);
6027 end if;
6028
6029 elsif Universal_Interpretation (N) = Universal_Real
6030 and then (T = Base_Type (Standard_Integer)
6031 or else Is_Universal_Numeric_Type (T))
6032 then
6033 -- A universal real can appear in a fixed-type context. We resolve
6034 -- the literal with that context, even though this might raise an
6035 -- exception prematurely (the other operand may be zero).
6036
6037 Resolve (N, B_Typ);
6038
6039 elsif Etype (N) = Base_Type (Standard_Integer)
6040 and then T = Universal_Real
6041 and then Is_Overloaded (N)
6042 then
6043 -- Integer arg in mixed-mode operation. Resolve with universal
6044 -- type, in case preference rule must be applied.
6045
6046 Resolve (N, Universal_Integer);
6047
6048 elsif Etype (N) = T and then B_Typ /= Universal_Fixed then
6049
6050 -- If the operand is part of a fixed multiplication operation,
6051 -- a conversion will be applied to each operand, so resolve it
6052 -- with its own type.
6053
6054 if Nkind (Parent (N)) in N_Op_Divide | N_Op_Multiply then
6055 Resolve (N);
6056
6057 else
6058 -- Not a mixed-mode operation, resolve with context
6059
6060 Resolve (N, B_Typ);
6061 end if;
6062
6063 elsif Etype (N) = Any_Fixed then
6064
6065 -- N may itself be a mixed-mode operation, so use context type
6066
6067 Resolve (N, B_Typ);
6068
6069 elsif Is_Fixed_Point_Type (T)
6070 and then B_Typ = Universal_Fixed
6071 and then Is_Overloaded (N)
6072 then
6073 -- Must be (fixed * fixed) operation, operand must have one
6074 -- compatible interpretation.
6075
6076 Resolve (N, Any_Fixed);
6077
6078 elsif Is_Fixed_Point_Type (B_Typ)
6079 and then (T = Universal_Real or else Is_Fixed_Point_Type (T))
6080 and then Is_Overloaded (N)
6081 then
6082 -- C * F(X) in a fixed context, where C is a real literal or a
6083 -- fixed-point expression. F must have either a fixed type
6084 -- interpretation or an integer interpretation, but not both.
6085
6086 Get_First_Interp (N, Index, It);
6087 while Present (It.Typ) loop
6088 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
6089 if Analyzed (N) then
6090 Error_Msg_N ("ambiguous operand in fixed operation", N);
6091 else
6092 Resolve (N, Standard_Integer);
6093 end if;
6094
6095 elsif Is_Fixed_Point_Type (It.Typ) then
6096 if Analyzed (N) then
6097 Error_Msg_N ("ambiguous operand in fixed operation", N);
6098 elsif It.Typ = Any_Fixed then
6099 Resolve (N, B_Typ);
6100 else
6101 Resolve (N, It.Typ);
6102 end if;
6103 end if;
6104
6105 Get_Next_Interp (Index, It);
6106 end loop;
6107
6108 -- Reanalyze the literal with the fixed type of the context. If
6109 -- context is Universal_Fixed, we are within a conversion, leave
6110 -- the literal as a universal real because there is no usable
6111 -- fixed type, and the target of the conversion plays no role in
6112 -- the resolution.
6113
6114 declare
6115 Op2 : Node_Id;
6116 T2 : Entity_Id;
6117
6118 begin
6119 if N = L then
6120 Op2 := R;
6121 else
6122 Op2 := L;
6123 end if;
6124
6125 if B_Typ = Universal_Fixed
6126 and then Nkind (Op2) = N_Real_Literal
6127 then
6128 T2 := Universal_Real;
6129 else
6130 T2 := B_Typ;
6131 end if;
6132
6133 Set_Analyzed (Op2, False);
6134 Resolve (Op2, T2);
6135 end;
6136
6137 -- A universal real conditional expression can appear in a fixed-type
6138 -- context and must be resolved with that context to facilitate the
6139 -- code generation in the back end. However, If the context is
6140 -- Universal_fixed (i.e. as an operand of a multiplication/division
6141 -- involving a fixed-point operand) the conditional expression must
6142 -- resolve to a unique visible fixed_point type, normally Duration.
6143
6144 elsif Nkind (N) in N_Case_Expression | N_If_Expression
6145 and then Etype (N) = Universal_Real
6146 and then Is_Fixed_Point_Type (B_Typ)
6147 then
6148 if B_Typ = Universal_Fixed then
6149 Resolve (N, Unique_Fixed_Point_Type (N));
6150
6151 else
6152 Resolve (N, B_Typ);
6153 end if;
6154
6155 else
6156 Resolve (N);
6157 end if;
6158 end Set_Mixed_Mode_Operand;
6159
6160 ----------------------
6161 -- Set_Operand_Type --
6162 ----------------------
6163
6164 procedure Set_Operand_Type (N : Node_Id) is
6165 begin
6166 if Is_Universal_Numeric_Type (Etype (N)) then
6167 Set_Etype (N, T);
6168 end if;
6169 end Set_Operand_Type;
6170
6171 -- Start of processing for Resolve_Arithmetic_Op
6172
6173 begin
6174 if Ekind (Entity (N)) = E_Function
6175 and then Is_Imported (Entity (N))
6176 and then Is_Intrinsic_Subprogram (Entity (N))
6177 then
6178 Generate_Reference (Entity (N), N);
6179 Resolve_Intrinsic_Operator (N, Typ);
6180 return;
6181
6182 -- Special-case for mixed-mode universal expressions or fixed point type
6183 -- operation: each argument is resolved separately. The same treatment
6184 -- is required if one of the operands of a fixed point operation is
6185 -- universal real, since in this case we don't do a conversion to a
6186 -- specific fixed-point type (instead the expander handles the case).
6187
6188 -- Set the type of the node to its universal interpretation because
6189 -- legality checks on an exponentiation operand need the context.
6190
6191 elsif Is_Universal_Numeric_Type (B_Typ)
6192 and then Present (Universal_Interpretation (L))
6193 and then Present (Universal_Interpretation (R))
6194 then
6195 Set_Etype (N, B_Typ);
6196 Resolve (L, Universal_Interpretation (L));
6197 Resolve (R, Universal_Interpretation (R));
6198
6199 elsif (B_Typ = Universal_Real
6200 or else Etype (N) = Universal_Fixed
6201 or else (Etype (N) = Any_Fixed
6202 and then Is_Fixed_Point_Type (B_Typ))
6203 or else (Is_Fixed_Point_Type (B_Typ)
6204 and then (Is_Integer_Or_Universal (L)
6205 or else
6206 Is_Integer_Or_Universal (R))))
6207 and then Nkind (N) in N_Op_Multiply | N_Op_Divide
6208 then
6209 if TL = Universal_Integer or else TR = Universal_Integer then
6210 Check_For_Visible_Operator (N, B_Typ);
6211 end if;
6212
6213 -- If context is a fixed type and one operand is integer, the other
6214 -- is resolved with the type of the context.
6215
6216 if Is_Fixed_Point_Type (B_Typ)
6217 and then (Base_Type (TL) = Base_Type (Standard_Integer)
6218 or else TL = Universal_Integer)
6219 then
6220 Resolve (R, B_Typ);
6221 Resolve (L, TL);
6222
6223 elsif Is_Fixed_Point_Type (B_Typ)
6224 and then (Base_Type (TR) = Base_Type (Standard_Integer)
6225 or else TR = Universal_Integer)
6226 then
6227 Resolve (L, B_Typ);
6228 Resolve (R, TR);
6229
6230 -- If both operands are universal and the context is a floating
6231 -- point type, the operands are resolved to the type of the context.
6232
6233 elsif Is_Floating_Point_Type (B_Typ) then
6234 Resolve (L, B_Typ);
6235 Resolve (R, B_Typ);
6236
6237 else
6238 Set_Mixed_Mode_Operand (L, TR);
6239 Set_Mixed_Mode_Operand (R, TL);
6240 end if;
6241
6242 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
6243 -- multiplying operators from being used when the expected type is
6244 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
6245 -- some cases where the expected type is actually Any_Real;
6246 -- Expected_Type_Is_Any_Real takes care of that case.
6247
6248 if Etype (N) = Universal_Fixed
6249 or else Etype (N) = Any_Fixed
6250 then
6251 if B_Typ = Universal_Fixed
6252 and then not Expected_Type_Is_Any_Real (N)
6253 and then Nkind (Parent (N)) not in
6254 N_Type_Conversion | N_Unchecked_Type_Conversion
6255 then
6256 Error_Msg_N ("type cannot be determined from context!", N);
6257 Error_Msg_N ("\explicit conversion to result type required", N);
6258
6259 Set_Etype (L, Any_Type);
6260 Set_Etype (R, Any_Type);
6261
6262 else
6263 if Ada_Version = Ada_83
6264 and then Etype (N) = Universal_Fixed
6265 and then Nkind (Parent (N)) not in
6266 N_Type_Conversion | N_Unchecked_Type_Conversion
6267 then
6268 Error_Msg_N
6269 ("(Ada 83) fixed-point operation needs explicit "
6270 & "conversion", N);
6271 end if;
6272
6273 -- The expected type is "any real type" in contexts like
6274
6275 -- type T is delta <universal_fixed-expression> ...
6276
6277 -- in which case we need to set the type to Universal_Real
6278 -- so that static expression evaluation will work properly.
6279
6280 if Expected_Type_Is_Any_Real (N) then
6281 Set_Etype (N, Universal_Real);
6282 else
6283 Set_Etype (N, B_Typ);
6284 end if;
6285 end if;
6286
6287 elsif Is_Fixed_Point_Type (B_Typ)
6288 and then (Is_Integer_Or_Universal (L)
6289 or else Nkind (L) = N_Real_Literal
6290 or else Nkind (R) = N_Real_Literal
6291 or else Is_Integer_Or_Universal (R))
6292 then
6293 Set_Etype (N, B_Typ);
6294
6295 elsif Etype (N) = Any_Fixed then
6296
6297 -- If no previous errors, this is only possible if one operand is
6298 -- overloaded and the context is universal. Resolve as such.
6299
6300 Set_Etype (N, B_Typ);
6301 end if;
6302
6303 else
6304 if Is_Universal_Numeric_Type (TL)
6305 and then
6306 Is_Universal_Numeric_Type (TR)
6307 then
6308 Check_For_Visible_Operator (N, B_Typ);
6309 end if;
6310
6311 -- If the context is Universal_Fixed and the operands are also
6312 -- universal fixed, this is an error, unless there is only one
6313 -- applicable fixed_point type (usually Duration).
6314
6315 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
6316 T := Unique_Fixed_Point_Type (N);
6317
6318 if T = Any_Type then
6319 Set_Etype (N, T);
6320 return;
6321 else
6322 Resolve (L, T);
6323 Resolve (R, T);
6324 end if;
6325
6326 else
6327 Resolve (L, B_Typ);
6328 Resolve (R, B_Typ);
6329 end if;
6330
6331 -- If one of the arguments was resolved to a non-universal type.
6332 -- label the result of the operation itself with the same type.
6333 -- Do the same for the universal argument, if any.
6334
6335 T := Intersect_Types (L, R);
6336 Set_Etype (N, Base_Type (T));
6337 Set_Operand_Type (L);
6338 Set_Operand_Type (R);
6339 end if;
6340
6341 Generate_Operator_Reference (N, Typ);
6342 Analyze_Dimension (N);
6343 Eval_Arithmetic_Op (N);
6344
6345 -- Set overflow and division checking bit
6346
6347 if Nkind (N) in N_Op then
6348 if not Overflow_Checks_Suppressed (Etype (N)) then
6349 Enable_Overflow_Check (N);
6350 end if;
6351
6352 -- Give warning if explicit division by zero
6353
6354 if Nkind (N) in N_Op_Divide | N_Op_Rem | N_Op_Mod
6355 and then not Division_Checks_Suppressed (Etype (N))
6356 then
6357 Rop := Right_Opnd (N);
6358
6359 if Compile_Time_Known_Value (Rop)
6360 and then ((Is_Integer_Type (Etype (Rop))
6361 and then Expr_Value (Rop) = Uint_0)
6362 or else
6363 (Is_Real_Type (Etype (Rop))
6364 and then Expr_Value_R (Rop) = Ureal_0))
6365 then
6366 -- Specialize the warning message according to the operation.
6367 -- When SPARK_Mode is On, force a warning instead of an error
6368 -- in that case, as this likely corresponds to deactivated
6369 -- code. The following warnings are for the case
6370
6371 case Nkind (N) is
6372 when N_Op_Divide =>
6373
6374 -- For division, we have two cases, for float division
6375 -- of an unconstrained float type, on a machine where
6376 -- Machine_Overflows is false, we don't get an exception
6377 -- at run-time, but rather an infinity or Nan. The Nan
6378 -- case is pretty obscure, so just warn about infinities.
6379
6380 if Is_Floating_Point_Type (Typ)
6381 and then not Is_Constrained (Typ)
6382 and then not Machine_Overflows_On_Target
6383 then
6384 Error_Msg_N
6385 ("float division by zero, may generate "
6386 & "'+'/'- infinity??", Right_Opnd (N));
6387
6388 -- For all other cases, we get a Constraint_Error
6389
6390 else
6391 Apply_Compile_Time_Constraint_Error
6392 (N, "division by zero??", CE_Divide_By_Zero,
6393 Loc => Sloc (Right_Opnd (N)),
6394 Warn => SPARK_Mode = On);
6395 end if;
6396
6397 when N_Op_Rem =>
6398 Apply_Compile_Time_Constraint_Error
6399 (N, "rem with zero divisor??", CE_Divide_By_Zero,
6400 Loc => Sloc (Right_Opnd (N)),
6401 Warn => SPARK_Mode = On);
6402
6403 when N_Op_Mod =>
6404 Apply_Compile_Time_Constraint_Error
6405 (N, "mod with zero divisor??", CE_Divide_By_Zero,
6406 Loc => Sloc (Right_Opnd (N)),
6407 Warn => SPARK_Mode = On);
6408
6409 -- Division by zero can only happen with division, rem,
6410 -- and mod operations.
6411
6412 when others =>
6413 raise Program_Error;
6414 end case;
6415
6416 -- Otherwise just set the flag to check at run time
6417
6418 else
6419 Activate_Division_Check (N);
6420 end if;
6421 end if;
6422
6423 -- If Restriction No_Implicit_Conditionals is active, then it is
6424 -- violated if either operand can be negative for mod, or for rem
6425 -- if both operands can be negative.
6426
6427 if Restriction_Check_Required (No_Implicit_Conditionals)
6428 and then Nkind (N) in N_Op_Rem | N_Op_Mod
6429 then
6430 declare
6431 Lo : Uint;
6432 Hi : Uint;
6433 OK : Boolean;
6434
6435 LNeg : Boolean;
6436 RNeg : Boolean;
6437 -- Set if corresponding operand might be negative
6438
6439 begin
6440 Determine_Range
6441 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
6442 LNeg := not OK or else Lo < 0;
6443
6444 Determine_Range
6445 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
6446 RNeg := not OK or else Lo < 0;
6447
6448 -- Check if we will be generating conditionals. There are two
6449 -- cases where that can happen, first for REM, the only case
6450 -- is largest negative integer mod -1, where the division can
6451 -- overflow, but we still have to give the right result. The
6452 -- front end generates a test for this annoying case. Here we
6453 -- just test if both operands can be negative (that's what the
6454 -- expander does, so we match its logic here).
6455
6456 -- The second case is mod where either operand can be negative.
6457 -- In this case, the back end has to generate additional tests.
6458
6459 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
6460 or else
6461 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
6462 then
6463 Check_Restriction (No_Implicit_Conditionals, N);
6464 end if;
6465 end;
6466 end if;
6467 end if;
6468
6469 Check_Unset_Reference (L);
6470 Check_Unset_Reference (R);
6471 end Resolve_Arithmetic_Op;
6472
6473 ------------------
6474 -- Resolve_Call --
6475 ------------------
6476
6477 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
6478 Loc : constant Source_Ptr := Sloc (N);
6479 Subp : constant Node_Id := Name (N);
6480 Body_Id : Entity_Id;
6481 I : Interp_Index;
6482 It : Interp;
6483 Nam : Entity_Id;
6484 Nam_Decl : Node_Id;
6485 Nam_UA : Entity_Id;
6486 Norm_OK : Boolean;
6487 Rtype : Entity_Id;
6488 Scop : Entity_Id;
6489
6490 begin
6491 -- Preserve relevant elaboration-related attributes of the context which
6492 -- are no longer available or very expensive to recompute once analysis,
6493 -- resolution, and expansion are over.
6494
6495 Mark_Elaboration_Attributes
6496 (N_Id => N,
6497 Checks => True,
6498 Modes => True,
6499 Warnings => True);
6500
6501 -- The context imposes a unique interpretation with type Typ on a
6502 -- procedure or function call. Find the entity of the subprogram that
6503 -- yields the expected type, and propagate the corresponding formal
6504 -- constraints on the actuals. The caller has established that an
6505 -- interpretation exists, and emitted an error if not unique.
6506
6507 -- First deal with the case of a call to an access-to-subprogram,
6508 -- dereference made explicit in Analyze_Call.
6509
6510 if Ekind (Etype (Subp)) = E_Subprogram_Type then
6511 if not Is_Overloaded (Subp) then
6512 Nam := Etype (Subp);
6513
6514 else
6515 -- Find the interpretation whose type (a subprogram type) has a
6516 -- return type that is compatible with the context. Analysis of
6517 -- the node has established that one exists.
6518
6519 Nam := Empty;
6520
6521 Get_First_Interp (Subp, I, It);
6522 while Present (It.Typ) loop
6523 if Covers (Typ, Etype (It.Typ)) then
6524 Nam := It.Typ;
6525 exit;
6526 end if;
6527
6528 Get_Next_Interp (I, It);
6529 end loop;
6530
6531 if No (Nam) then
6532 raise Program_Error;
6533 end if;
6534 end if;
6535
6536 -- If the prefix is not an entity, then resolve it
6537
6538 if not Is_Entity_Name (Subp) then
6539 Resolve (Subp, Nam);
6540 end if;
6541
6542 -- For an indirect call, we always invalidate checks, since we do not
6543 -- know whether the subprogram is local or global. Yes we could do
6544 -- better here, e.g. by knowing that there are no local subprograms,
6545 -- but it does not seem worth the effort. Similarly, we kill all
6546 -- knowledge of current constant values.
6547
6548 Kill_Current_Values;
6549
6550 -- If this is a procedure call which is really an entry call, do
6551 -- the conversion of the procedure call to an entry call. Protected
6552 -- operations use the same circuitry because the name in the call
6553 -- can be an arbitrary expression with special resolution rules.
6554
6555 elsif Nkind (Subp) in N_Selected_Component | N_Indexed_Component
6556 or else (Is_Entity_Name (Subp) and then Is_Entry (Entity (Subp)))
6557 then
6558 Resolve_Entry_Call (N, Typ);
6559
6560 if Legacy_Elaboration_Checks then
6561 Check_Elab_Call (N);
6562 end if;
6563
6564 -- Annotate the tree by creating a call marker in case the original
6565 -- call is transformed by expansion. The call marker is automatically
6566 -- saved for later examination by the ABE Processing phase.
6567
6568 Build_Call_Marker (N);
6569
6570 -- Kill checks and constant values, as above for indirect case
6571 -- Who knows what happens when another task is activated?
6572
6573 Kill_Current_Values;
6574 return;
6575
6576 -- Normal subprogram call with name established in Resolve
6577
6578 elsif not Is_Type (Entity (Subp)) then
6579 Nam := Entity (Subp);
6580 Set_Entity_With_Checks (Subp, Nam);
6581
6582 -- Otherwise we must have the case of an overloaded call
6583
6584 else
6585 pragma Assert (Is_Overloaded (Subp));
6586
6587 -- Initialize Nam to prevent warning (we know it will be assigned
6588 -- in the loop below, but the compiler does not know that).
6589
6590 Nam := Empty;
6591
6592 Get_First_Interp (Subp, I, It);
6593 while Present (It.Typ) loop
6594 if Covers (Typ, It.Typ) then
6595 Nam := It.Nam;
6596 Set_Entity_With_Checks (Subp, Nam);
6597 exit;
6598 end if;
6599
6600 Get_Next_Interp (I, It);
6601 end loop;
6602 end if;
6603
6604 -- Check that a call to Current_Task does not occur in an entry body
6605
6606 if Is_RTE (Nam, RE_Current_Task) then
6607 declare
6608 P : Node_Id;
6609
6610 begin
6611 P := N;
6612 loop
6613 P := Parent (P);
6614
6615 -- Exclude calls that occur within the default of a formal
6616 -- parameter of the entry, since those are evaluated outside
6617 -- of the body.
6618
6619 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
6620
6621 if Nkind (P) = N_Entry_Body
6622 or else (Nkind (P) = N_Subprogram_Body
6623 and then Is_Entry_Barrier_Function (P))
6624 then
6625 Rtype := Etype (N);
6626 Error_Msg_Warn := SPARK_Mode /= On;
6627 Error_Msg_NE
6628 ("& should not be used in entry body (RM C.7(17))<<",
6629 N, Nam);
6630 Error_Msg_NE ("\Program_Error [<<", N, Nam);
6631 Rewrite (N,
6632 Make_Raise_Program_Error (Loc,
6633 Reason => PE_Current_Task_In_Entry_Body));
6634 Set_Etype (N, Rtype);
6635 return;
6636 end if;
6637 end loop;
6638 end;
6639 end if;
6640
6641 -- Check that a procedure call does not occur in the context of the
6642 -- entry call statement of a conditional or timed entry call. Note that
6643 -- the case of a call to a subprogram renaming of an entry will also be
6644 -- rejected. The test for N not being an N_Entry_Call_Statement is
6645 -- defensive, covering the possibility that the processing of entry
6646 -- calls might reach this point due to later modifications of the code
6647 -- above.
6648
6649 if Nkind (Parent (N)) = N_Entry_Call_Alternative
6650 and then Nkind (N) /= N_Entry_Call_Statement
6651 and then Entry_Call_Statement (Parent (N)) = N
6652 then
6653 if Ada_Version < Ada_2005 then
6654 Error_Msg_N ("entry call required in select statement", N);
6655
6656 -- Ada 2005 (AI-345): If a procedure_call_statement is used
6657 -- for a procedure_or_entry_call, the procedure_name or
6658 -- procedure_prefix of the procedure_call_statement shall denote
6659 -- an entry renamed by a procedure, or (a view of) a primitive
6660 -- subprogram of a limited interface whose first parameter is
6661 -- a controlling parameter.
6662
6663 elsif Nkind (N) = N_Procedure_Call_Statement
6664 and then not Is_Renamed_Entry (Nam)
6665 and then not Is_Controlling_Limited_Procedure (Nam)
6666 then
6667 Error_Msg_N
6668 ("entry call or dispatching primitive of interface required", N);
6669 end if;
6670 end if;
6671
6672 -- Check that this is not a call to a protected procedure or entry from
6673 -- within a protected function.
6674
6675 Check_Internal_Protected_Use (N, Nam);
6676
6677 -- Freeze the subprogram name if not in a spec-expression. Note that
6678 -- we freeze procedure calls as well as function calls. Procedure calls
6679 -- are not frozen according to the rules (RM 13.14(14)) because it is
6680 -- impossible to have a procedure call to a non-frozen procedure in
6681 -- pure Ada, but in the code that we generate in the expander, this
6682 -- rule needs extending because we can generate procedure calls that
6683 -- need freezing.
6684
6685 -- In Ada 2012, expression functions may be called within pre/post
6686 -- conditions of subsequent functions or expression functions. Such
6687 -- calls do not freeze when they appear within generated bodies,
6688 -- (including the body of another expression function) which would
6689 -- place the freeze node in the wrong scope. An expression function
6690 -- is frozen in the usual fashion, by the appearance of a real body,
6691 -- or at the end of a declarative part. However an implicit call to
6692 -- an expression function may appear when it is part of a default
6693 -- expression in a call to an initialization procedure, and must be
6694 -- frozen now, even if the body is inserted at a later point.
6695 -- Otherwise, the call freezes the expression if expander is active,
6696 -- for example as part of an object declaration.
6697
6698 if Is_Entity_Name (Subp)
6699 and then not In_Spec_Expression
6700 and then not Is_Expression_Function_Or_Completion (Current_Scope)
6701 and then not (Chars (Current_Scope) = Name_uWrapped_Statements
6702 and then Is_Expression_Function_Or_Completion
6703 (Scope (Current_Scope)))
6704 and then
6705 (not Is_Expression_Function_Or_Completion (Entity (Subp))
6706 or else Expander_Active)
6707 then
6708 if Is_Expression_Function (Entity (Subp)) then
6709
6710 -- Force freeze of expression function in call
6711
6712 Set_Comes_From_Source (Subp, True);
6713 Set_Must_Not_Freeze (Subp, False);
6714 end if;
6715
6716 Freeze_Expression (Subp);
6717 end if;
6718
6719 -- For a predefined operator, the type of the result is the type imposed
6720 -- by context, except for a predefined operation on universal fixed.
6721 -- Otherwise the type of the call is the type returned by the subprogram
6722 -- being called.
6723
6724 if Is_Predefined_Op (Nam) then
6725 if Etype (N) /= Universal_Fixed then
6726 Set_Etype (N, Typ);
6727 end if;
6728
6729 -- If the subprogram returns an array type, and the context requires the
6730 -- component type of that array type, the node is really an indexing of
6731 -- the parameterless call. Resolve as such. A pathological case occurs
6732 -- when the type of the component is an access to the array type. In
6733 -- this case the call is truly ambiguous. If the call is to an intrinsic
6734 -- subprogram, it can't be an indexed component. This check is necessary
6735 -- because if it's Unchecked_Conversion, and we have "type T_Ptr is
6736 -- access T;" and "type T is array (...) of T_Ptr;" (i.e. an array of
6737 -- pointers to the same array), the compiler gets confused and does an
6738 -- infinite recursion.
6739
6740 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
6741 and then
6742 ((Is_Array_Type (Etype (Nam))
6743 and then Covers (Typ, Component_Type (Etype (Nam))))
6744 or else
6745 (Is_Access_Type (Etype (Nam))
6746 and then Is_Array_Type (Designated_Type (Etype (Nam)))
6747 and then
6748 Covers (Typ, Component_Type (Designated_Type (Etype (Nam))))
6749 and then not Is_Intrinsic_Subprogram (Entity (Subp))))
6750 then
6751 declare
6752 Index_Node : Node_Id;
6753 New_Subp : Node_Id;
6754 Ret_Type : constant Entity_Id := Etype (Nam);
6755
6756 begin
6757 -- If this is a parameterless call there is no ambiguity and the
6758 -- call has the type of the function.
6759
6760 if No (First_Actual (N)) then
6761 Set_Etype (N, Etype (Nam));
6762
6763 if Present (First_Formal (Nam)) then
6764 Resolve_Actuals (N, Nam);
6765 end if;
6766
6767 -- Annotate the tree by creating a call marker in case the
6768 -- original call is transformed by expansion. The call marker
6769 -- is automatically saved for later examination by the ABE
6770 -- Processing phase.
6771
6772 Build_Call_Marker (N);
6773
6774 elsif Is_Access_Type (Ret_Type)
6775
6776 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
6777 then
6778 Error_Msg_N
6779 ("cannot disambiguate function call and indexing", N);
6780 else
6781 New_Subp := Relocate_Node (Subp);
6782
6783 -- The called entity may be an explicit dereference, in which
6784 -- case there is no entity to set.
6785
6786 if Nkind (New_Subp) /= N_Explicit_Dereference then
6787 Set_Entity (Subp, Nam);
6788 end if;
6789
6790 if (Is_Array_Type (Ret_Type)
6791 and then Component_Type (Ret_Type) /= Any_Type)
6792 or else
6793 (Is_Access_Type (Ret_Type)
6794 and then
6795 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
6796 then
6797 if Needs_No_Actuals (Nam) then
6798
6799 -- Indexed call to a parameterless function
6800
6801 Index_Node :=
6802 Make_Indexed_Component (Loc,
6803 Prefix =>
6804 Make_Function_Call (Loc, Name => New_Subp),
6805 Expressions => Parameter_Associations (N));
6806 else
6807 -- An Ada 2005 prefixed call to a primitive operation
6808 -- whose first parameter is the prefix. This prefix was
6809 -- prepended to the parameter list, which is actually a
6810 -- list of indexes. Remove the prefix in order to build
6811 -- the proper indexed component.
6812
6813 Index_Node :=
6814 Make_Indexed_Component (Loc,
6815 Prefix =>
6816 Make_Function_Call (Loc,
6817 Name => New_Subp,
6818 Parameter_Associations =>
6819 New_List
6820 (Remove_Head (Parameter_Associations (N)))),
6821 Expressions => Parameter_Associations (N));
6822 end if;
6823
6824 -- Preserve the parenthesis count of the node
6825
6826 Set_Paren_Count (Index_Node, Paren_Count (N));
6827
6828 -- Since we are correcting a node classification error made
6829 -- by the parser, we call Replace rather than Rewrite.
6830
6831 Replace (N, Index_Node);
6832
6833 Set_Etype (Prefix (N), Ret_Type);
6834 Set_Etype (N, Typ);
6835
6836 if Legacy_Elaboration_Checks then
6837 Check_Elab_Call (Prefix (N));
6838 end if;
6839
6840 -- Annotate the tree by creating a call marker in case
6841 -- the original call is transformed by expansion. The call
6842 -- marker is automatically saved for later examination by
6843 -- the ABE Processing phase.
6844
6845 Build_Call_Marker (Prefix (N));
6846
6847 Resolve_Indexed_Component (N, Typ);
6848 end if;
6849 end if;
6850
6851 return;
6852 end;
6853
6854 else
6855 -- If the called function is not declared in the main unit and it
6856 -- returns the limited view of type then use the available view (as
6857 -- is done in Try_Object_Operation) to prevent back-end confusion;
6858 -- for the function entity itself. The call must appear in a context
6859 -- where the nonlimited view is available. If the function entity is
6860 -- in the extended main unit then no action is needed, because the
6861 -- back end handles this case. In either case the type of the call
6862 -- is the nonlimited view.
6863
6864 if From_Limited_With (Etype (Nam))
6865 and then Present (Available_View (Etype (Nam)))
6866 then
6867 Set_Etype (N, Available_View (Etype (Nam)));
6868
6869 if not In_Extended_Main_Code_Unit (Nam) then
6870 Set_Etype (Nam, Available_View (Etype (Nam)));
6871 end if;
6872
6873 else
6874 Set_Etype (N, Etype (Nam));
6875 end if;
6876 end if;
6877
6878 -- In the case where the call is to an overloaded subprogram, Analyze
6879 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
6880 -- such a case Normalize_Actuals needs to be called once more to order
6881 -- the actuals correctly. Otherwise the call will have the ordering
6882 -- given by the last overloaded subprogram whether this is the correct
6883 -- one being called or not.
6884
6885 if Is_Overloaded (Subp) then
6886 Normalize_Actuals (N, Nam, False, Norm_OK);
6887 pragma Assert (Norm_OK);
6888 end if;
6889
6890 -- In any case, call is fully resolved now. Reset Overload flag, to
6891 -- prevent subsequent overload resolution if node is analyzed again
6892
6893 Set_Is_Overloaded (Subp, False);
6894 Set_Is_Overloaded (N, False);
6895
6896 -- A Ghost entity must appear in a specific context
6897
6898 if Is_Ghost_Entity (Nam) and then Comes_From_Source (N) then
6899 Check_Ghost_Context (Nam, N);
6900 end if;
6901
6902 if Is_Entity_Name (Subp) then
6903 Local_Restrict.Check_Call
6904 (Call => N, Callee => Ultimate_Alias (Nam));
6905 else
6906 Local_Restrict.Check_Call (Call => N);
6907 end if;
6908
6909 -- If we are calling the current subprogram from immediately within its
6910 -- body, then that is the case where we can sometimes detect cases of
6911 -- infinite recursion statically. Do not try this in case restriction
6912 -- No_Recursion is in effect anyway, and do it only for source calls.
6913
6914 if Comes_From_Source (N) then
6915 Scop := Current_Scope;
6916
6917 -- Issue warning for possible infinite recursion in the absence
6918 -- of the No_Recursion restriction.
6919
6920 if Same_Or_Aliased_Subprograms (Nam, Scop)
6921 and then not Restriction_Active (No_Recursion)
6922 and then not Is_Static_Function (Scop)
6923 and then Check_Infinite_Recursion (N)
6924 then
6925 -- Here we detected and flagged an infinite recursion, so we do
6926 -- not need to test the case below for further warnings. Also we
6927 -- are all done if we now have a raise SE node.
6928
6929 if Nkind (N) = N_Raise_Storage_Error then
6930 return;
6931 end if;
6932
6933 -- If call is to immediately containing subprogram, then check for
6934 -- the case of a possible run-time detectable infinite recursion.
6935
6936 else
6937 Scope_Loop : while Scop /= Standard_Standard loop
6938 if Same_Or_Aliased_Subprograms (Nam, Scop) then
6939
6940 -- Ada 2022 (AI12-0075): Static functions are never allowed
6941 -- to make a recursive call, as specified by 6.8(5.4/5).
6942
6943 if Is_Static_Function (Scop) then
6944 Error_Msg_N
6945 ("recursive call not allowed in static expression "
6946 & "function", N);
6947
6948 Set_Error_Posted (Scop);
6949
6950 exit Scope_Loop;
6951 end if;
6952
6953 -- Although in general case, recursion is not statically
6954 -- checkable, the case of calling an immediately containing
6955 -- subprogram is easy to catch.
6956
6957 if not Is_Ignored_Ghost_Entity (Nam) then
6958 Check_Restriction (No_Recursion, N);
6959 end if;
6960
6961 -- If the recursive call is to a parameterless subprogram,
6962 -- then even if we can't statically detect infinite
6963 -- recursion, this is pretty suspicious, and we output a
6964 -- warning. Furthermore, we will try later to detect some
6965 -- cases here at run time by expanding checking code (see
6966 -- Detect_Infinite_Recursion in package Exp_Ch6).
6967
6968 -- If the recursive call is within a handler, do not emit a
6969 -- warning, because this is a common idiom: loop until input
6970 -- is correct, catch illegal input in handler and restart.
6971
6972 if No (First_Formal (Nam))
6973 and then Etype (Nam) = Standard_Void_Type
6974 and then not Error_Posted (N)
6975 and then Nkind (Parent (N)) /= N_Exception_Handler
6976 then
6977 -- For the case of a procedure call. We give the message
6978 -- only if the call is the first statement in a sequence
6979 -- of statements, or if all previous statements are
6980 -- simple assignments. This is simply a heuristic to
6981 -- decrease false positives, without losing too many good
6982 -- warnings. The idea is that these previous statements
6983 -- may affect global variables the procedure depends on.
6984 -- We also exclude raise statements, that may arise from
6985 -- constraint checks and are probably unrelated to the
6986 -- intended control flow.
6987
6988 if Nkind (N) = N_Procedure_Call_Statement
6989 and then Is_List_Member (N)
6990 then
6991 declare
6992 P : Node_Id;
6993 begin
6994 P := Prev (N);
6995 while Present (P) loop
6996 if Nkind (P) not in N_Assignment_Statement
6997 | N_Raise_Constraint_Error
6998 then
6999 exit Scope_Loop;
7000 end if;
7001
7002 Prev (P);
7003 end loop;
7004 end;
7005 end if;
7006
7007 -- Do not give warning if we are in a conditional context
7008
7009 declare
7010 K : constant Node_Kind := Nkind (Parent (N));
7011 begin
7012 if (K = N_Loop_Statement
7013 and then Present (Iteration_Scheme (Parent (N))))
7014 or else K = N_If_Statement
7015 or else K = N_Elsif_Part
7016 or else K = N_Case_Statement_Alternative
7017 then
7018 exit Scope_Loop;
7019 end if;
7020 end;
7021
7022 -- Here warning is to be issued
7023
7024 Set_Has_Recursive_Call (Nam);
7025 Error_Msg_Warn := SPARK_Mode /= On;
7026 Error_Msg_N ("possible infinite recursion<<!", N);
7027 Error_Msg_N ("\Storage_Error ]<<!", N);
7028 end if;
7029
7030 exit Scope_Loop;
7031 end if;
7032
7033 Scop := Scope (Scop);
7034 end loop Scope_Loop;
7035 end if;
7036 end if;
7037
7038 -- Check obsolescent reference to Ada.Characters.Handling subprogram
7039
7040 Check_Obsolescent_2005_Entity (Nam, Subp);
7041
7042 -- If subprogram name is a predefined operator, it was given in
7043 -- functional notation. Replace call node with operator node, so
7044 -- that actuals can be resolved appropriately.
7045
7046 if Ekind (Nam) = E_Operator or else Is_Predefined_Op (Nam) then
7047 Make_Call_Into_Operator (N, Typ, Nam);
7048 return;
7049
7050 elsif Present (Alias (Nam)) and then Is_Predefined_Op (Alias (Nam)) then
7051 Resolve_Actuals (N, Nam);
7052 Make_Call_Into_Operator (N, Typ, Alias (Nam));
7053 return;
7054 end if;
7055
7056 -- Create a transient scope if the expander is active and the resulting
7057 -- type requires it.
7058
7059 -- There are several notable exceptions:
7060
7061 -- a) Intrinsic subprograms (Unchecked_Conversion and source info
7062 -- functions) do not use the secondary stack even though the return
7063 -- type may be unconstrained.
7064
7065 -- b) Subprograms that are ignored ghost entities do not return anything
7066
7067 -- c) Calls to a build-in-place function, since such functions may
7068 -- allocate their result directly in a target object, and cases where
7069 -- the result does get allocated in the secondary stack are checked for
7070 -- within the specialized Exp_Ch6 procedures for expanding those
7071 -- build-in-place calls.
7072
7073 -- d) Calls to inlinable expression functions do not use the secondary
7074 -- stack (since the call will be replaced by its returned object).
7075
7076 -- e) If the subprogram is marked Inline, then even if it returns
7077 -- an unconstrained type the call does not require use of the secondary
7078 -- stack. However, inlining will only take place if the body to inline
7079 -- is already present. It may not be available if e.g. the subprogram is
7080 -- declared in a child instance.
7081
7082 -- f) If the subprogram is a static expression function and the call is
7083 -- a static call (the actuals are all static expressions), then we never
7084 -- want to create a transient scope (this could occur in the case of a
7085 -- static string-returning call).
7086
7087 -- g) If the call is the expression of a simple return statement that
7088 -- returns on the same stack, since it will be handled as a tail call
7089 -- by Expand_Simple_Function_Return.
7090
7091 if Expander_Active
7092 and then Ekind (Nam) in E_Function | E_Subprogram_Type
7093 and then Requires_Transient_Scope (Etype (Nam))
7094 and then not Is_Intrinsic_Subprogram (Nam)
7095 and then not Is_Ignored_Ghost_Entity (Nam)
7096 and then not Is_Build_In_Place_Function (Nam)
7097 and then not Is_Inlinable_Expression_Function (Nam)
7098 and then not (Is_Inlined (Nam)
7099 and then Has_Pragma_Inline (Nam)
7100 and then Nkind (Unit_Declaration_Node (Nam)) =
7101 N_Subprogram_Declaration
7102 and then
7103 Present (Body_To_Inline (Unit_Declaration_Node (Nam))))
7104 and then not Is_Static_Function_Call (N)
7105 and then not (Nkind (Parent (N)) = N_Simple_Return_Statement
7106 and then
7107 Needs_Secondary_Stack
7108 (Etype
7109 (Return_Applies_To
7110 (Return_Statement_Entity (Parent (N))))) =
7111 Needs_Secondary_Stack (Etype (Nam)))
7112 then
7113 Establish_Transient_Scope (N, Needs_Secondary_Stack (Etype (Nam)));
7114
7115 -- If the call appears within the bounds of a loop, it will be
7116 -- rewritten and reanalyzed, nothing left to do here.
7117
7118 if Nkind (N) /= N_Function_Call then
7119 return;
7120 end if;
7121 end if;
7122
7123 -- A protected function cannot be called within the definition of the
7124 -- enclosing protected type, unless it is part of a pre/postcondition
7125 -- on another protected operation. This may appear in the entry wrapper
7126 -- created for an entry with preconditions.
7127
7128 if Is_Protected_Type (Scope (Nam))
7129 and then In_Open_Scopes (Scope (Nam))
7130 and then not Has_Completion (Scope (Nam))
7131 and then not In_Spec_Expression
7132 and then not Is_Entry_Wrapper (Current_Scope)
7133 then
7134 Error_Msg_NE
7135 ("& cannot be called before end of protected definition", N, Nam);
7136 end if;
7137
7138 -- Propagate interpretation to actuals, and add default expressions
7139 -- where needed.
7140
7141 if Present (First_Formal (Nam)) then
7142 Resolve_Actuals (N, Nam);
7143
7144 -- Overloaded literals are rewritten as function calls, for purpose of
7145 -- resolution. After resolution, we can replace the call with the
7146 -- literal itself.
7147
7148 elsif Ekind (Nam) = E_Enumeration_Literal then
7149 Copy_Node (Subp, N);
7150 Resolve_Entity_Name (N, Typ);
7151
7152 -- Avoid validation, since it is a static function call
7153
7154 Generate_Reference (Nam, Subp);
7155 return;
7156 end if;
7157
7158 -- If the subprogram is not global, then kill all saved values and
7159 -- checks. This is a bit conservative, since in many cases we could do
7160 -- better, but it is not worth the effort. Similarly, we kill constant
7161 -- values. However we do not need to do this for internal entities
7162 -- (unless they are inherited user-defined subprograms), since they
7163 -- are not in the business of molesting local values.
7164
7165 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
7166 -- kill all checks and values for calls to global subprograms. This
7167 -- takes care of the case where an access to a local subprogram is
7168 -- taken, and could be passed directly or indirectly and then called
7169 -- from almost any context.
7170
7171 -- Note: we do not do this step till after resolving the actuals. That
7172 -- way we still take advantage of the current value information while
7173 -- scanning the actuals.
7174
7175 -- We suppress killing values if we are processing the nodes associated
7176 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
7177 -- type kills all the values as part of analyzing the code that
7178 -- initializes the dispatch tables.
7179
7180 if Inside_Freezing_Actions = 0
7181 and then (not Is_Library_Level_Entity (Nam)
7182 or else Suppress_Value_Tracking_On_Call
7183 (Nearest_Dynamic_Scope (Current_Scope)))
7184 and then (Comes_From_Source (Nam)
7185 or else (Present (Alias (Nam))
7186 and then Comes_From_Source (Alias (Nam))))
7187 then
7188 Kill_Current_Values;
7189 end if;
7190
7191 -- If we are warning about unread OUT parameters, this is the place to
7192 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
7193 -- after the above call to Kill_Current_Values (since that call clears
7194 -- the Last_Assignment field of all local variables).
7195
7196 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
7197 and then Comes_From_Source (N)
7198 and then In_Extended_Main_Source_Unit (N)
7199 then
7200 declare
7201 F : Entity_Id;
7202 A : Node_Id;
7203
7204 begin
7205 F := First_Formal (Nam);
7206 A := First_Actual (N);
7207 while Present (F) and then Present (A) loop
7208 if Ekind (F) in E_Out_Parameter | E_In_Out_Parameter
7209 and then Warn_On_Modified_As_Out_Parameter (F)
7210 and then Is_Entity_Name (A)
7211 and then Present (Entity (A))
7212 and then Comes_From_Source (N)
7213 and then Safe_To_Capture_Value (N, Entity (A))
7214 then
7215 Set_Last_Assignment (Entity (A), A);
7216 end if;
7217
7218 Next_Formal (F);
7219 Next_Actual (A);
7220 end loop;
7221 end;
7222 end if;
7223
7224 -- If the subprogram is a primitive operation, check whether or not
7225 -- it is a correct dispatching call.
7226
7227 if Is_Overloadable (Nam) and then Is_Dispatching_Operation (Nam) then
7228 Check_Dispatching_Call (N);
7229
7230 -- If the subprogram is an abstract operation, then flag an error
7231
7232 elsif Is_Overloadable (Nam) and then Is_Abstract_Subprogram (Nam) then
7233 Nondispatching_Call_To_Abstract_Operation (N, Nam);
7234 end if;
7235
7236 -- If this is a dispatching call, generate the appropriate reference,
7237 -- for better source navigation in GNAT Studio.
7238
7239 if Is_Overloadable (Nam) and then Present (Controlling_Argument (N)) then
7240 Generate_Reference (Nam, Subp, 'R');
7241
7242 -- Normal case, not a dispatching call: generate a call reference
7243
7244 else
7245 Generate_Reference (Nam, Subp, 's');
7246 end if;
7247
7248 if Is_Intrinsic_Subprogram (Nam) then
7249 Check_Intrinsic_Call (N);
7250 end if;
7251
7252 -- Check for violation of restriction No_Specific_Termination_Handlers
7253 -- and warn on a potentially blocking call to Abort_Task.
7254
7255 if Restriction_Check_Required (No_Specific_Termination_Handlers)
7256 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
7257 or else
7258 Is_RTE (Nam, RE_Specific_Handler))
7259 then
7260 Check_Restriction (No_Specific_Termination_Handlers, N);
7261
7262 elsif Is_RTE (Nam, RE_Abort_Task) then
7263 Check_Potentially_Blocking_Operation (N);
7264 end if;
7265
7266 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
7267 -- timing event violates restriction No_Relative_Delay (AI-0211). We
7268 -- need to check the second argument to determine whether it is an
7269 -- absolute or relative timing event.
7270
7271 if Restriction_Check_Required (No_Relative_Delay)
7272 and then Is_RTE (Nam, RE_Set_Handler)
7273 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
7274 then
7275 Check_Restriction (No_Relative_Delay, N);
7276 end if;
7277
7278 -- Issue an error for a call to an eliminated subprogram. This routine
7279 -- will not perform the check if the call appears within a default
7280 -- expression.
7281
7282 Check_For_Eliminated_Subprogram (Subp, Nam);
7283
7284 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
7285 -- class-wide and the call dispatches on result in a context that does
7286 -- not provide a tag, the call raises Program_Error.
7287
7288 if Nkind (N) = N_Function_Call
7289 and then In_Instance
7290 and then Is_Generic_Actual_Type (Typ)
7291 and then Is_Class_Wide_Type (Typ)
7292 and then Has_Controlling_Result (Nam)
7293 and then Nkind (Parent (N)) = N_Object_Declaration
7294 then
7295 -- Verify that none of the formals are controlling
7296
7297 declare
7298 Call_OK : Boolean := False;
7299 F : Entity_Id;
7300
7301 begin
7302 F := First_Formal (Nam);
7303 while Present (F) loop
7304 if Is_Controlling_Formal (F) then
7305 Call_OK := True;
7306 exit;
7307 end if;
7308
7309 Next_Formal (F);
7310 end loop;
7311
7312 if not Call_OK then
7313 Error_Msg_Warn := SPARK_Mode /= On;
7314 Error_Msg_N ("!cannot determine tag of result<<", N);
7315 Error_Msg_N ("\Program_Error [<<!", N);
7316 Insert_Action (N,
7317 Make_Raise_Program_Error (Sloc (N),
7318 Reason => PE_Explicit_Raise));
7319 end if;
7320 end;
7321 end if;
7322
7323 -- Check for calling a function with OUT or IN OUT parameter when the
7324 -- calling context (us right now) is not Ada 2012, so does not allow
7325 -- OUT or IN OUT parameters in function calls. Functions declared in
7326 -- a predefined unit are OK, as they may be called indirectly from a
7327 -- user-declared instantiation.
7328
7329 if Ada_Version < Ada_2012
7330 and then Ekind (Nam) = E_Function
7331 and then Has_Out_Or_In_Out_Parameter (Nam)
7332 and then not In_Predefined_Unit (Nam)
7333 then
7334 Error_Msg_NE ("& has at least one OUT or `IN OUT` parameter", N, Nam);
7335 Error_Msg_N ("\call to this function only allowed in Ada 2012", N);
7336 end if;
7337
7338 -- Check the dimensions of the actuals in the call. For function calls,
7339 -- propagate the dimensions from the returned type to N.
7340
7341 Analyze_Dimension_Call (N, Nam);
7342
7343 -- Check unreachable code after calls to procedures with No_Return
7344
7345 if Ekind (Nam) = E_Procedure and then No_Return (Nam) then
7346 Check_Unreachable_Code (N);
7347 end if;
7348
7349 -- All done, evaluate call and deal with elaboration issues
7350
7351 Eval_Call (N);
7352
7353 if Legacy_Elaboration_Checks then
7354 Check_Elab_Call (N);
7355 end if;
7356
7357 -- Annotate the tree by creating a call marker in case the original call
7358 -- is transformed by expansion. The call marker is automatically saved
7359 -- for later examination by the ABE Processing phase.
7360
7361 Build_Call_Marker (N);
7362
7363 Mark_Use_Clauses (Subp);
7364
7365 Warn_On_Overlapping_Actuals (Nam, N);
7366
7367 -- Ada 2022 (AI12-0075): If the call is a static call to a static
7368 -- expression function, then we want to "inline" the call, replacing
7369 -- it with the folded static result. This is not done if the checking
7370 -- for a potentially static expression is enabled or if an error has
7371 -- been posted on the call (which may be due to the check for recursive
7372 -- calls, in which case we don't want to fall into infinite recursion
7373 -- when doing the inlining).
7374
7375 if not Checking_Potentially_Static_Expression
7376 and then Is_Static_Function_Call (N)
7377 and then not Is_Intrinsic_Subprogram (Ultimate_Alias (Nam))
7378 and then not Error_Posted (Ultimate_Alias (Nam))
7379 then
7380 Inline_Static_Function_Call (N, Ultimate_Alias (Nam));
7381
7382 -- In GNATprove mode, expansion is disabled, but we want to inline some
7383 -- subprograms to facilitate formal verification. Indirect calls through
7384 -- a subprogram type or within a generic cannot be inlined. Inlining is
7385 -- performed only for calls subject to SPARK_Mode => On.
7386
7387 elsif GNATprove_Mode
7388 and then SPARK_Mode = On
7389 and then Is_Overloadable (Nam)
7390 and then not Inside_A_Generic
7391 then
7392 Nam_UA := Ultimate_Alias (Nam);
7393 Nam_Decl := Unit_Declaration_Node (Nam_UA);
7394
7395 if Nkind (Nam_Decl) = N_Subprogram_Declaration then
7396 Body_Id := Corresponding_Body (Nam_Decl);
7397
7398 -- Nothing to do if the subprogram is not inlined (because it is
7399 -- recursive, directly or indirectly), or is not eligible for
7400 -- inlining GNATprove mode (because of properties of the
7401 -- subprogram itself), or inlining has been disabled with switch
7402 -- -gnatdm.
7403
7404 if not Is_Inlined (Nam_UA)
7405 or else not Can_Be_Inlined_In_GNATprove_Mode (Nam_UA, Body_Id)
7406 or else Debug_Flag_M
7407 then
7408 null;
7409
7410 -- Calls cannot be inlined inside assertions, as GNATprove treats
7411 -- assertions as logic expressions. Only issue a message when the
7412 -- body has been seen, otherwise this leads to spurious messages
7413 -- on expression functions.
7414
7415 elsif In_Assertion_Expr /= 0 then
7416 Cannot_Inline
7417 ("cannot inline & (in assertion expression)?", N, Nam_UA,
7418 Suppress_Info => No (Body_Id));
7419
7420 -- Calls cannot be inlined inside default expressions
7421
7422 elsif In_Default_Expr then
7423 Cannot_Inline
7424 ("cannot inline & (in default expression)?", N, Nam_UA);
7425
7426 -- Calls cannot be inlined inside potentially unevaluated
7427 -- expressions, as this would create complex actions inside
7428 -- expressions, that are not handled by GNATprove.
7429
7430 elsif Is_Potentially_Unevaluated (N) then
7431 Cannot_Inline
7432 ("cannot inline & (in potentially unevaluated context)?",
7433 N, Nam_UA);
7434
7435 -- Calls are not inlined inside the loop_parameter_specification
7436 -- or iterator_specification of the quantified expression, as they
7437 -- are only preanalyzed. Calls in the predicate part are handled
7438 -- by the previous test on potentially unevaluated expressions.
7439
7440 elsif In_Quantified_Expression (N) then
7441 Cannot_Inline
7442 ("cannot inline & (in quantified expression)?", N, Nam_UA);
7443
7444 -- Inlining should not be performed during preanalysis
7445
7446 elsif Full_Analysis then
7447
7448 -- Do not inline calls inside expression functions or functions
7449 -- generated by the front end for subtype predicates, as this
7450 -- would prevent interpreting them as logical formulas in
7451 -- GNATprove. Only issue a message when the body has been seen,
7452 -- otherwise this leads to spurious messages on callees that
7453 -- are themselves expression functions.
7454
7455 if Present (Current_Subprogram)
7456 and then
7457 (Is_Expression_Function_Or_Completion (Current_Subprogram)
7458 or else Is_Predicate_Function (Current_Subprogram)
7459 or else Is_Invariant_Procedure (Current_Subprogram)
7460 or else Is_DIC_Procedure (Current_Subprogram))
7461 then
7462 declare
7463 Issue_Msg : constant Boolean :=
7464 Present (Body_Id)
7465 and then Present (Body_To_Inline (Nam_Decl));
7466 begin
7467 if Is_Predicate_Function (Current_Subprogram) then
7468 Cannot_Inline
7469 ("cannot inline & (inside predicate)?",
7470 N, Nam_UA, Suppress_Info => not Issue_Msg);
7471
7472 elsif Is_Invariant_Procedure (Current_Subprogram) then
7473 Cannot_Inline
7474 ("cannot inline & (inside invariant)?",
7475 N, Nam_UA, Suppress_Info => not Issue_Msg);
7476
7477 elsif Is_DIC_Procedure (Current_Subprogram) then
7478 Cannot_Inline
7479 ("cannot inline & (inside Default_Initial_Condition)?",
7480 N, Nam_UA, Suppress_Info => not Issue_Msg);
7481
7482 else
7483 Cannot_Inline
7484 ("cannot inline & (inside expression function)?",
7485 N, Nam_UA, Suppress_Info => not Issue_Msg);
7486 end if;
7487 end;
7488
7489 -- Cannot inline a call inside the definition of a record type,
7490 -- typically inside the constraints of the type. Calls in
7491 -- default expressions are also not inlined, but this is
7492 -- filtered out above when testing In_Default_Expr.
7493
7494 elsif Is_Record_Type (Current_Scope) then
7495 Cannot_Inline
7496 ("cannot inline & (inside record type)?", N, Nam_UA);
7497
7498 -- With the one-pass inlining technique, a call cannot be
7499 -- inlined if the corresponding body has not been seen yet.
7500
7501 elsif No (Body_Id) then
7502 Cannot_Inline
7503 ("cannot inline & (body not seen yet)?", N, Nam_UA);
7504
7505 -- Nothing to do if there is no body to inline, indicating that
7506 -- the subprogram is not suitable for inlining in GNATprove
7507 -- mode.
7508
7509 elsif No (Body_To_Inline (Nam_Decl)) then
7510 null;
7511
7512 -- Calls cannot be inlined inside the conditions of while
7513 -- loops, as this would create complex actions inside
7514 -- the condition, that are not handled by GNATprove.
7515
7516 elsif In_Statement_Condition_With_Actions (N) then
7517 Cannot_Inline
7518 ("cannot inline & (in while loop condition)?", N, Nam_UA);
7519
7520 -- Do not inline calls which would possibly lead to missing a
7521 -- type conversion check on an input parameter or a memory leak
7522 -- on an output parameter.
7523
7524 elsif not Call_Can_Be_Inlined_In_GNATprove_Mode (N, Nam) then
7525 Cannot_Inline
7526 ("cannot inline & (possible check on parameters)?",
7527 N, Nam_UA);
7528
7529 -- Otherwise, inline the call, issuing an info message when
7530 -- -gnatd_f is set.
7531
7532 else
7533 if Debug_Flag_Underscore_F then
7534 Error_Msg_NE
7535 ("info: analyzing call to & in context?", N, Nam_UA);
7536 end if;
7537
7538 Expand_Inlined_Call (N, Nam_UA, Nam);
7539 end if;
7540 end if;
7541 end if;
7542 end if;
7543 end Resolve_Call;
7544
7545 -----------------------------
7546 -- Resolve_Case_Expression --
7547 -----------------------------
7548
7549 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
7550 Alt : Node_Id;
7551 Alt_Expr : Node_Id;
7552 Alt_Typ : Entity_Id;
7553 Is_Dyn : Boolean;
7554
7555 begin
7556 Alt := First (Alternatives (N));
7557 while Present (Alt) loop
7558 Alt_Expr := Expression (Alt);
7559
7560 if Error_Posted (Alt_Expr) then
7561 return;
7562 end if;
7563
7564 Resolve_Dependent_Expression (N, Alt_Expr, Typ);
7565
7566 Check_Unset_Reference (Alt_Expr);
7567 Alt_Typ := Etype (Alt_Expr);
7568
7569 -- When the expression is of a scalar subtype different from the
7570 -- result subtype, then insert a conversion to ensure the generation
7571 -- of a constraint check.
7572
7573 if Is_Scalar_Type (Alt_Typ) and then Alt_Typ /= Typ then
7574 Rewrite (Alt_Expr, Convert_To (Typ, Alt_Expr));
7575 Analyze_And_Resolve (Alt_Expr, Typ);
7576
7577 elsif Is_Array_Type (Typ) then
7578 Apply_Length_Check (Alt_Expr, Typ);
7579 end if;
7580
7581 Next (Alt);
7582 end loop;
7583
7584 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
7585 -- dynamically tagged must be known statically.
7586
7587 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
7588 Alt := First (Alternatives (N));
7589 Is_Dyn := Is_Dynamically_Tagged (Expression (Alt));
7590
7591 while Present (Alt) loop
7592 if Is_Dynamically_Tagged (Expression (Alt)) /= Is_Dyn then
7593 Error_Msg_N
7594 ("all or none of the dependent expressions can be "
7595 & "dynamically tagged", N);
7596 end if;
7597
7598 Next (Alt);
7599 end loop;
7600 end if;
7601
7602 Set_Etype (N, Typ);
7603 Eval_Case_Expression (N);
7604 Analyze_Dimension (N);
7605 end Resolve_Case_Expression;
7606
7607 -------------------------------
7608 -- Resolve_Character_Literal --
7609 -------------------------------
7610
7611 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
7612 B_Typ : constant Entity_Id := Base_Type (Typ);
7613 C : Entity_Id;
7614
7615 begin
7616 -- Verify that the character does belong to the type of the context
7617
7618 Set_Etype (N, B_Typ);
7619 Eval_Character_Literal (N);
7620
7621 -- Wide_Wide_Character literals must always be defined, since the set
7622 -- of wide wide character literals is complete, i.e. if a character
7623 -- literal is accepted by the parser, then it is OK for wide wide
7624 -- character (out of range character literals are rejected).
7625
7626 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
7627 return;
7628
7629 -- Always accept character literal for type Any_Character, which
7630 -- occurs in error situations and in comparisons of literals, both
7631 -- of which should accept all literals.
7632
7633 elsif B_Typ = Any_Character then
7634 return;
7635
7636 -- For Standard.Character or a type derived from it, check that the
7637 -- literal is in range.
7638
7639 elsif Root_Type (B_Typ) = Standard_Character then
7640 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
7641 return;
7642 end if;
7643
7644 -- For Standard.Wide_Character or a type derived from it, check that the
7645 -- literal is in range.
7646
7647 elsif Root_Type (B_Typ) = Standard_Wide_Character then
7648 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
7649 return;
7650 end if;
7651
7652 -- If the entity is already set, this has already been resolved in a
7653 -- generic context, or comes from expansion. Nothing else to do.
7654
7655 elsif Present (Entity (N)) then
7656 return;
7657
7658 -- Otherwise we have a user defined character type, and we can use the
7659 -- standard visibility mechanisms to locate the referenced entity.
7660
7661 else
7662 C := Current_Entity (N);
7663 while Present (C) loop
7664 if Etype (C) = B_Typ then
7665 Set_Entity_With_Checks (N, C);
7666 Generate_Reference (C, N);
7667 return;
7668 end if;
7669
7670 C := Homonym (C);
7671 end loop;
7672 end if;
7673
7674 -- If we fall through, then the literal does not match any of the
7675 -- entries of the enumeration type. This isn't just a constraint error
7676 -- situation, it is an illegality (see RM 4.2).
7677
7678 Error_Msg_NE
7679 ("character not defined for }", N, First_Subtype (B_Typ));
7680 end Resolve_Character_Literal;
7681
7682 ---------------------------
7683 -- Resolve_Comparison_Op --
7684 ---------------------------
7685
7686 -- The operands must have compatible types and the boolean context does not
7687 -- participate in the resolution. The first pass verifies that the operands
7688 -- are not ambiguous and sets their type correctly, or to Any_Type in case
7689 -- of ambiguity. If both operands are strings or aggregates, then they are
7690 -- ambiguous even if they carry a single (universal) type.
7691
7692 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
7693 L : constant Node_Id := Left_Opnd (N);
7694 R : constant Node_Id := Right_Opnd (N);
7695
7696 T : Entity_Id := Find_Unique_Type (L, R);
7697
7698 begin
7699 if T = Any_Fixed then
7700 T := Unique_Fixed_Point_Type (L);
7701 end if;
7702
7703 Set_Etype (N, Base_Type (Typ));
7704 Generate_Reference (T, N, ' ');
7705
7706 if T = Any_Type then
7707 -- Deal with explicit ambiguity of operands
7708
7709 if Is_Overloaded (L) or else Is_Overloaded (R) then
7710 Ambiguous_Operands (N);
7711 end if;
7712
7713 return;
7714 end if;
7715
7716 -- Deal with other error cases
7717
7718 if T = Any_String or else
7719 T = Any_Composite or else
7720 T = Any_Character
7721 then
7722 if T = Any_Character then
7723 Ambiguous_Character (L);
7724 else
7725 Error_Msg_N ("ambiguous operands for comparison", N);
7726 end if;
7727
7728 Set_Etype (N, Any_Type);
7729 return;
7730 end if;
7731
7732 -- Resolve the operands if types OK
7733
7734 Resolve (L, T);
7735 Resolve (R, T);
7736 Set_Compare_Type (N, T);
7737 Check_Unset_Reference (L);
7738 Check_Unset_Reference (R);
7739 Generate_Operator_Reference (N, T);
7740 Check_Low_Bound_Tested (N);
7741
7742 -- Check comparison on unordered enumeration
7743
7744 if Bad_Unordered_Enumeration_Reference (N, Etype (L)) then
7745 Error_Msg_Sloc := Sloc (Etype (L));
7746 Error_Msg_NE
7747 ("comparison on unordered enumeration type& declared#?.u?",
7748 N, Etype (L));
7749 end if;
7750
7751 Analyze_Dimension (N);
7752
7753 Eval_Relational_Op (N);
7754 end Resolve_Comparison_Op;
7755
7756 --------------------------------
7757 -- Resolve_Declare_Expression --
7758 --------------------------------
7759
7760 procedure Resolve_Declare_Expression
7761 (N : Node_Id;
7762 Typ : Entity_Id)
7763 is
7764 Expr : constant Node_Id := Expression (N);
7765
7766 Decl : Node_Id;
7767 Local : Entity_Id := Empty;
7768
7769 function Replace_Local (N : Node_Id) return Traverse_Result;
7770 -- Use a tree traversal to replace each occurrence of the name of
7771 -- a local object declared in the construct, with the corresponding
7772 -- entity. This replaces the usual way to perform name capture by
7773 -- visibility, because it is not possible to place on the scope
7774 -- stack the fake scope created for the analysis of the local
7775 -- declarations; such a scope conflicts with the transient scopes
7776 -- that may be generated if the expression includes function calls
7777 -- requiring finalization.
7778
7779 -------------------
7780 -- Replace_Local --
7781 -------------------
7782
7783 function Replace_Local (N : Node_Id) return Traverse_Result is
7784 begin
7785 -- The identifier may be the prefix of a selected component,
7786 -- but not a selector name, because the local entities do not
7787 -- have a scope that can be named: a selected component whose
7788 -- selector is a homonym of a local entity must denote some
7789 -- global entity.
7790
7791 if Nkind (N) = N_Identifier
7792 and then Chars (N) = Chars (Local)
7793 and then No (Entity (N))
7794 and then
7795 (Nkind (Parent (N)) /= N_Selected_Component
7796 or else N = Prefix (Parent (N)))
7797 then
7798 Set_Entity (N, Local);
7799 Set_Etype (N, Etype (Local));
7800 end if;
7801
7802 return OK;
7803 end Replace_Local;
7804
7805 procedure Replace_Local_Ref is new Traverse_Proc (Replace_Local);
7806
7807 -- Start of processing for Resolve_Declare_Expression
7808
7809 begin
7810 Decl := First (Actions (N));
7811
7812 while Present (Decl) loop
7813 if Nkind (Decl) in
7814 N_Object_Declaration | N_Object_Renaming_Declaration
7815 and then Comes_From_Source (Defining_Identifier (Decl))
7816 then
7817 Local := Defining_Identifier (Decl);
7818 Replace_Local_Ref (Expr);
7819
7820 -- Traverse the expression to replace references to local
7821 -- variables that occur within declarations of the
7822 -- declare_expression.
7823
7824 declare
7825 D : Node_Id := Next (Decl);
7826 begin
7827 while Present (D) loop
7828 Replace_Local_Ref (D);
7829 Next (D);
7830 end loop;
7831 end;
7832 end if;
7833
7834 Next (Decl);
7835 end loop;
7836
7837 -- The end of the declarative list is a freeze point for the
7838 -- local declarations.
7839
7840 if Present (Local) then
7841 Decl := Parent (Local);
7842 Freeze_All (First_Entity (Scope (Local)), Decl);
7843 end if;
7844
7845 Resolve (Expr, Typ);
7846 Check_Unset_Reference (Expr);
7847 end Resolve_Declare_Expression;
7848
7849 -----------------------------------
7850 -- Resolve_Dependent_Expression --
7851 -----------------------------------
7852
7853 procedure Resolve_Dependent_Expression
7854 (N : Node_Id;
7855 Expr : Node_Id;
7856 Typ : Entity_Id)
7857 is
7858 begin
7859 -- RM 4.5.7(8/3) says that the expected type of dependent expressions is
7860 -- that of the conditional expression but RM 4.5.7(10/3) forces the type
7861 -- of the conditional expression without changing the expected type (the
7862 -- expected type of the operand of a type conversion is any type), so we
7863 -- may have a gap between these two types that is bridged by the dynamic
7864 -- semantics specified by RM 4.5.7(20/3) with the associated legality
7865 -- rule RM 4.5.7(16/3) that will be automatically enforced.
7866
7867 if Nkind (Parent (N)) = N_Type_Conversion
7868 and then Nkind (Expr) /= N_Raise_Expression
7869 then
7870 Convert_To_And_Rewrite (Typ, Expr);
7871 Analyze_And_Resolve (Expr);
7872 else
7873 Resolve (Expr, Typ);
7874 end if;
7875 end Resolve_Dependent_Expression;
7876
7877 -----------------------------------------
7878 -- Resolve_Discrete_Subtype_Indication --
7879 -----------------------------------------
7880
7881 procedure Resolve_Discrete_Subtype_Indication
7882 (N : Node_Id;
7883 Typ : Entity_Id)
7884 is
7885 R : Node_Id;
7886 S : Entity_Id;
7887
7888 begin
7889 Analyze (Subtype_Mark (N));
7890 S := Entity (Subtype_Mark (N));
7891
7892 if Nkind (Constraint (N)) /= N_Range_Constraint then
7893 Error_Msg_N ("expect range constraint for discrete type", N);
7894 Set_Etype (N, Any_Type);
7895
7896 else
7897 R := Range_Expression (Constraint (N));
7898
7899 if R = Error then
7900 return;
7901 end if;
7902
7903 Analyze (R);
7904
7905 if Base_Type (S) /= Base_Type (Typ) then
7906 Error_Msg_NE
7907 ("expect subtype of }", N, First_Subtype (Typ));
7908
7909 -- Rewrite the constraint as a range of Typ
7910 -- to allow compilation to proceed further.
7911
7912 Set_Etype (N, Typ);
7913 Rewrite (Low_Bound (R),
7914 Make_Attribute_Reference (Sloc (Low_Bound (R)),
7915 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
7916 Attribute_Name => Name_First));
7917 Rewrite (High_Bound (R),
7918 Make_Attribute_Reference (Sloc (High_Bound (R)),
7919 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
7920 Attribute_Name => Name_First));
7921
7922 else
7923 Resolve (R, Typ);
7924 Set_Etype (N, Etype (R));
7925
7926 -- Additionally, we must check that the bounds are compatible
7927 -- with the given subtype, which might be different from the
7928 -- type of the context.
7929
7930 Apply_Range_Check (R, S);
7931
7932 -- ??? If the above check statically detects a Constraint_Error
7933 -- it replaces the offending bound(s) of the range R with a
7934 -- Constraint_Error node. When the itype which uses these bounds
7935 -- is frozen the resulting call to Duplicate_Subexpr generates
7936 -- a new temporary for the bounds.
7937
7938 -- Unfortunately there are other itypes that are also made depend
7939 -- on these bounds, so when Duplicate_Subexpr is called they get
7940 -- a forward reference to the newly created temporaries and Gigi
7941 -- aborts on such forward references. This is probably sign of a
7942 -- more fundamental problem somewhere else in either the order of
7943 -- itype freezing or the way certain itypes are constructed.
7944
7945 -- To get around this problem we call Remove_Side_Effects right
7946 -- away if either bounds of R are a Constraint_Error.
7947
7948 declare
7949 L : constant Node_Id := Low_Bound (R);
7950 H : constant Node_Id := High_Bound (R);
7951
7952 begin
7953 if Nkind (L) = N_Raise_Constraint_Error then
7954 Remove_Side_Effects (L);
7955 end if;
7956
7957 if Nkind (H) = N_Raise_Constraint_Error then
7958 Remove_Side_Effects (H);
7959 end if;
7960 end;
7961
7962 Check_Unset_Reference (Low_Bound (R));
7963 Check_Unset_Reference (High_Bound (R));
7964 end if;
7965 end if;
7966 end Resolve_Discrete_Subtype_Indication;
7967
7968 -------------------------
7969 -- Resolve_Entity_Name --
7970 -------------------------
7971
7972 -- Used to resolve identifiers and expanded names
7973
7974 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
7975 function Is_Assignment_Or_Object_Expression
7976 (Context : Node_Id;
7977 Expr : Node_Id) return Boolean;
7978 -- Determine whether node Context denotes an assignment statement or an
7979 -- object declaration whose expression is node Expr.
7980
7981 function Is_Attribute_Expression (Expr : Node_Id) return Boolean;
7982 -- Determine whether Expr is part of an N_Attribute_Reference
7983 -- expression.
7984
7985 ----------------------------------------
7986 -- Is_Assignment_Or_Object_Expression --
7987 ----------------------------------------
7988
7989 function Is_Assignment_Or_Object_Expression
7990 (Context : Node_Id;
7991 Expr : Node_Id) return Boolean
7992 is
7993 begin
7994 if Nkind (Context) in N_Assignment_Statement | N_Object_Declaration
7995 and then Expression (Context) = Expr
7996 then
7997 return True;
7998
7999 -- Check whether a construct that yields a name is the expression of
8000 -- an assignment statement or an object declaration.
8001
8002 elsif (Nkind (Context) in N_Attribute_Reference
8003 | N_Explicit_Dereference
8004 | N_Indexed_Component
8005 | N_Selected_Component
8006 | N_Slice
8007 and then Prefix (Context) = Expr)
8008 or else
8009 (Nkind (Context) in N_Type_Conversion
8010 | N_Unchecked_Type_Conversion
8011 and then Expression (Context) = Expr)
8012 then
8013 return
8014 Is_Assignment_Or_Object_Expression
8015 (Context => Parent (Context),
8016 Expr => Context);
8017
8018 -- Otherwise the context is not an assignment statement or an object
8019 -- declaration.
8020
8021 else
8022 return False;
8023 end if;
8024 end Is_Assignment_Or_Object_Expression;
8025
8026 -----------------------------
8027 -- Is_Attribute_Expression --
8028 -----------------------------
8029
8030 function Is_Attribute_Expression (Expr : Node_Id) return Boolean is
8031 N : Node_Id := Expr;
8032 begin
8033 while Present (N) loop
8034 if Nkind (N) = N_Attribute_Reference then
8035 return True;
8036
8037 -- Prevent the search from going too far
8038
8039 elsif Is_Body_Or_Package_Declaration (N) then
8040 return False;
8041 end if;
8042
8043 N := Parent (N);
8044 end loop;
8045
8046 return False;
8047 end Is_Attribute_Expression;
8048
8049 -- Local variables
8050
8051 E : constant Entity_Id := Entity (N);
8052 Par : Node_Id;
8053
8054 -- Start of processing for Resolve_Entity_Name
8055
8056 begin
8057 -- If garbage from errors, set to Any_Type and return
8058
8059 if No (E) and then Total_Errors_Detected /= 0 then
8060 Set_Etype (N, Any_Type);
8061 return;
8062 end if;
8063
8064 -- Replace named numbers by corresponding literals. Note that this is
8065 -- the one case where Resolve_Entity_Name must reset the Etype, since
8066 -- it is currently marked as universal.
8067
8068 if Ekind (E) = E_Named_Integer then
8069 Set_Etype (N, Typ);
8070 Eval_Named_Integer (N);
8071
8072 elsif Ekind (E) = E_Named_Real then
8073 Set_Etype (N, Typ);
8074 Eval_Named_Real (N);
8075
8076 -- For enumeration literals, we need to make sure that a proper style
8077 -- check is done, since such literals are overloaded, and thus we did
8078 -- not do a style check during the first phase of analysis.
8079
8080 elsif Ekind (E) = E_Enumeration_Literal then
8081 Set_Entity_With_Checks (N, E);
8082 Eval_Entity_Name (N);
8083
8084 -- Case of (sub)type name appearing in a context where an expression
8085 -- is expected. This is legal if occurrence is a current instance.
8086 -- See RM 8.6 (17/3). It is also legal if the expression is
8087 -- part of a choice pattern for a case stmt/expr having a
8088 -- non-discrete selecting expression.
8089
8090 elsif Is_Type (E) then
8091 if Is_Current_Instance (N) or else Is_Case_Choice_Pattern (N) then
8092 null;
8093
8094 -- Any other use is an error
8095
8096 else
8097 Error_Msg_N
8098 ("invalid use of subtype mark in expression or call", N);
8099 end if;
8100
8101 -- Check discriminant use if entity is discriminant in current scope,
8102 -- i.e. discriminant of record or concurrent type currently being
8103 -- analyzed. Uses in corresponding body are unrestricted.
8104
8105 elsif Ekind (E) = E_Discriminant
8106 and then Scope (E) = Current_Scope
8107 and then not Has_Completion (Current_Scope)
8108 then
8109 Check_Discriminant_Use (N);
8110
8111 -- A parameterless generic function cannot appear in a context that
8112 -- requires resolution.
8113
8114 elsif Ekind (E) = E_Generic_Function then
8115 Error_Msg_N ("illegal use of generic function", N);
8116
8117 -- In Ada 83 an OUT parameter cannot be read, but attributes of
8118 -- array types (i.e. bounds and length) are legal.
8119
8120 elsif Ekind (E) = E_Out_Parameter
8121 and then (Is_Scalar_Type (Etype (E))
8122 or else not Is_Attribute_Expression (Parent (N)))
8123
8124 and then (Nkind (Parent (N)) in N_Op
8125 or else Nkind (Parent (N)) = N_Explicit_Dereference
8126 or else Is_Assignment_Or_Object_Expression
8127 (Context => Parent (N),
8128 Expr => N))
8129 then
8130 if Ada_Version = Ada_83 then
8131 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
8132 end if;
8133
8134 -- In all other cases, just do the possible static evaluation
8135
8136 else
8137 -- A deferred constant that appears in an expression must have a
8138 -- completion, unless it has been removed by in-place expansion of
8139 -- an aggregate. A constant that is a renaming does not need
8140 -- initialization.
8141
8142 if Ekind (E) = E_Constant
8143 and then Comes_From_Source (E)
8144 and then No (Constant_Value (E))
8145 and then Is_Frozen (Etype (E))
8146 and then not Preanalysis_Active
8147 and then not Is_Imported (E)
8148 and then Nkind (Parent (E)) /= N_Object_Renaming_Declaration
8149 then
8150 if No_Initialization (Parent (E))
8151 or else (Present (Full_View (E))
8152 and then No_Initialization (Parent (Full_View (E))))
8153 then
8154 null;
8155 else
8156 Error_Msg_N
8157 ("deferred constant is frozen before completion", N);
8158 end if;
8159 end if;
8160
8161 Eval_Entity_Name (N);
8162 end if;
8163
8164 Par := Parent (N);
8165
8166 -- When the entity appears in a parameter association, retrieve the
8167 -- related subprogram call.
8168
8169 if Nkind (Par) = N_Parameter_Association then
8170 Par := Parent (Par);
8171 end if;
8172
8173 if Comes_From_Source (N) then
8174
8175 -- The following checks are only relevant when SPARK_Mode is On as
8176 -- they are not standard Ada legality rules.
8177
8178 if SPARK_Mode = On then
8179
8180 -- Check for possible elaboration issues with respect to reads of
8181 -- variables. The act of renaming the variable is not considered a
8182 -- read as it simply establishes an alias.
8183
8184 if Legacy_Elaboration_Checks
8185 and then Ekind (E) = E_Variable
8186 and then Dynamic_Elaboration_Checks
8187 and then Nkind (Par) /= N_Object_Renaming_Declaration
8188 then
8189 Check_Elab_Call (N);
8190 end if;
8191 end if;
8192
8193 -- The variable may eventually become a constituent of a single
8194 -- protected/task type. Record the reference now and verify its
8195 -- legality when analyzing the contract of the variable
8196 -- (SPARK RM 9.3).
8197
8198 if Ekind (E) = E_Variable then
8199 Record_Possible_Part_Of_Reference (E, N);
8200 end if;
8201
8202 -- A Ghost entity must appear in a specific context
8203
8204 if Is_Ghost_Entity (E) then
8205 Check_Ghost_Context (E, N);
8206 end if;
8207
8208 -- We may be resolving an entity within expanded code, so a reference
8209 -- to an entity should be ignored when calculating effective use
8210 -- clauses to avoid inappropriate marking.
8211
8212 Mark_Use_Clauses (E);
8213 end if;
8214 end Resolve_Entity_Name;
8215
8216 -------------------
8217 -- Resolve_Entry --
8218 -------------------
8219
8220 procedure Resolve_Entry (Entry_Name : Node_Id) is
8221 Loc : constant Source_Ptr := Sloc (Entry_Name);
8222 Nam : Entity_Id;
8223 New_N : Node_Id;
8224 S : Entity_Id;
8225 Tsk : Entity_Id;
8226 E_Name : Node_Id;
8227 Index : Node_Id;
8228
8229 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
8230 -- If the bounds of the entry family being called depend on task
8231 -- discriminants, build a new index subtype where a discriminant is
8232 -- replaced with the value of the discriminant of the target task.
8233 -- The target task is the prefix of the entry name in the call.
8234
8235 -----------------------
8236 -- Actual_Index_Type --
8237 -----------------------
8238
8239 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
8240 Typ : constant Entity_Id := Entry_Index_Type (E);
8241 Tsk : constant Entity_Id := Scope (E);
8242 Lo : constant Node_Id := Type_Low_Bound (Typ);
8243 Hi : constant Node_Id := Type_High_Bound (Typ);
8244 New_T : Entity_Id;
8245
8246 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
8247 -- If the bound is given by a discriminant, replace with a reference
8248 -- to the discriminant of the same name in the target task. If the
8249 -- entry name is the target of a requeue statement and the entry is
8250 -- in the current protected object, the bound to be used is the
8251 -- discriminal of the object (see Apply_Range_Check for details of
8252 -- the transformation).
8253
8254 -----------------------------
8255 -- Actual_Discriminant_Ref --
8256 -----------------------------
8257
8258 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
8259 Typ : constant Entity_Id := Etype (Bound);
8260 Ref : Node_Id;
8261
8262 begin
8263 Remove_Side_Effects (Bound);
8264
8265 if not Is_Entity_Name (Bound)
8266 or else Ekind (Entity (Bound)) /= E_Discriminant
8267 then
8268 return Bound;
8269
8270 elsif Is_Protected_Type (Tsk)
8271 and then In_Open_Scopes (Tsk)
8272 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
8273 then
8274 -- Note: here Bound denotes a discriminant of the corresponding
8275 -- record type tskV, whose discriminal is a formal of the
8276 -- init-proc tskVIP. What we want is the body discriminal,
8277 -- which is associated to the discriminant of the original
8278 -- concurrent type tsk.
8279
8280 return New_Occurrence_Of
8281 (Find_Body_Discriminal (Entity (Bound)), Loc);
8282
8283 else
8284 Ref :=
8285 Make_Selected_Component (Loc,
8286 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
8287 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
8288 Analyze (Ref);
8289 Resolve (Ref, Typ);
8290 return Ref;
8291 end if;
8292 end Actual_Discriminant_Ref;
8293
8294 -- Start of processing for Actual_Index_Type
8295
8296 begin
8297 if not Has_Discriminants (Tsk)
8298 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
8299 then
8300 return Entry_Index_Type (E);
8301
8302 else
8303 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
8304 Set_Etype (New_T, Base_Type (Typ));
8305 Set_Size_Info (New_T, Typ);
8306 Set_RM_Size (New_T, RM_Size (Typ));
8307 Set_Scalar_Range (New_T,
8308 Make_Range (Sloc (Entry_Name),
8309 Low_Bound => Actual_Discriminant_Ref (Lo),
8310 High_Bound => Actual_Discriminant_Ref (Hi)));
8311
8312 return New_T;
8313 end if;
8314 end Actual_Index_Type;
8315
8316 -- Start of processing for Resolve_Entry
8317
8318 begin
8319 -- Find name of entry being called, and resolve prefix of name with its
8320 -- own type. The prefix can be overloaded, and the name and signature of
8321 -- the entry must be taken into account.
8322
8323 if Nkind (Entry_Name) = N_Indexed_Component then
8324
8325 -- Case of dealing with entry family within the current tasks
8326
8327 E_Name := Prefix (Entry_Name);
8328
8329 else
8330 E_Name := Entry_Name;
8331 end if;
8332
8333 if Is_Entity_Name (E_Name) then
8334
8335 -- Entry call to an entry (or entry family) in the current task. This
8336 -- is legal even though the task will deadlock. Rewrite as call to
8337 -- current task.
8338
8339 -- This can also be a call to an entry in an enclosing task. If this
8340 -- is a single task, we have to retrieve its name, because the scope
8341 -- of the entry is the task type, not the object. If the enclosing
8342 -- task is a task type, the identity of the task is given by its own
8343 -- self variable.
8344
8345 -- Finally this can be a requeue on an entry of the same task or
8346 -- protected object.
8347
8348 S := Scope (Entity (E_Name));
8349
8350 for J in reverse 0 .. Scope_Stack.Last loop
8351 if Is_Task_Type (Scope_Stack.Table (J).Entity)
8352 and then not Comes_From_Source (S)
8353 then
8354 -- S is an enclosing task or protected object. The concurrent
8355 -- declaration has been converted into a type declaration, and
8356 -- the object itself has an object declaration that follows
8357 -- the type in the same declarative part.
8358
8359 Tsk := Next_Entity (S);
8360 while Etype (Tsk) /= S loop
8361 Next_Entity (Tsk);
8362 end loop;
8363
8364 S := Tsk;
8365 exit;
8366
8367 elsif S = Scope_Stack.Table (J).Entity then
8368
8369 -- Call to current task. Will be transformed into call to Self
8370
8371 exit;
8372
8373 end if;
8374 end loop;
8375
8376 New_N :=
8377 Make_Selected_Component (Loc,
8378 Prefix => New_Occurrence_Of (S, Loc),
8379 Selector_Name =>
8380 New_Occurrence_Of (Entity (E_Name), Loc));
8381 Rewrite (E_Name, New_N);
8382 Analyze (E_Name);
8383
8384 elsif Nkind (Entry_Name) = N_Selected_Component
8385 and then Is_Overloaded (Prefix (Entry_Name))
8386 then
8387 -- Use the entry name (which must be unique at this point) to find
8388 -- the prefix that returns the corresponding task/protected type.
8389
8390 declare
8391 Pref : constant Node_Id := Prefix (Entry_Name);
8392 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
8393 I : Interp_Index;
8394 It : Interp;
8395
8396 begin
8397 Get_First_Interp (Pref, I, It);
8398 while Present (It.Typ) loop
8399 if Scope (Ent) = It.Typ then
8400 Set_Etype (Pref, It.Typ);
8401 exit;
8402 end if;
8403
8404 Get_Next_Interp (I, It);
8405 end loop;
8406 end;
8407 end if;
8408
8409 if Nkind (Entry_Name) = N_Selected_Component then
8410 Resolve (Prefix (Entry_Name));
8411 Resolve_Implicit_Dereference (Prefix (Entry_Name));
8412
8413 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8414 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
8415 Resolve (Prefix (Prefix (Entry_Name)));
8416 Resolve_Implicit_Dereference (Prefix (Prefix (Entry_Name)));
8417
8418 -- We do not resolve the prefix because an Entry_Family has no type,
8419 -- although it has the semantics of an array since it can be indexed.
8420 -- In order to perform the associated range check, we would need to
8421 -- build an array type on the fly and set it on the prefix, but this
8422 -- would be wasteful since only the index type matters. Therefore we
8423 -- attach this index type directly, so that Actual_Index_Expression
8424 -- can pick it up later in order to generate the range check.
8425
8426 Set_Etype (Prefix (Entry_Name), Actual_Index_Type (Nam));
8427
8428 Index := First (Expressions (Entry_Name));
8429 Resolve (Index, Entry_Index_Type (Nam));
8430
8431 -- Generate a reference for the index when it denotes an entity
8432
8433 if Is_Entity_Name (Index) then
8434 Generate_Reference (Entity (Index), Nam);
8435 end if;
8436
8437 -- Up to this point the expression could have been the actual in a
8438 -- simple entry call, and be given by a named association.
8439
8440 if Nkind (Index) = N_Parameter_Association then
8441 Error_Msg_N ("expect expression for entry index", Index);
8442 else
8443 Apply_Scalar_Range_Check (Index, Etype (Prefix (Entry_Name)));
8444 end if;
8445 end if;
8446 end Resolve_Entry;
8447
8448 ------------------------
8449 -- Resolve_Entry_Call --
8450 ------------------------
8451
8452 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
8453 Entry_Name : constant Node_Id := Name (N);
8454 Loc : constant Source_Ptr := Sloc (Entry_Name);
8455
8456 Nam : Entity_Id;
8457 Norm_OK : Boolean;
8458 Obj : Node_Id;
8459 Was_Over : Boolean;
8460
8461 begin
8462 -- We kill all checks here, because it does not seem worth the effort to
8463 -- do anything better, an entry call is a big operation.
8464
8465 Kill_All_Checks;
8466
8467 -- Processing of the name is similar for entry calls and protected
8468 -- operation calls. Once the entity is determined, we can complete
8469 -- the resolution of the actuals.
8470
8471 -- The selector may be overloaded, in the case of a protected object
8472 -- with overloaded functions. The type of the context is used for
8473 -- resolution.
8474
8475 if Nkind (Entry_Name) = N_Selected_Component
8476 and then Is_Overloaded (Selector_Name (Entry_Name))
8477 and then Typ /= Standard_Void_Type
8478 then
8479 declare
8480 I : Interp_Index;
8481 It : Interp;
8482
8483 begin
8484 Get_First_Interp (Selector_Name (Entry_Name), I, It);
8485 while Present (It.Typ) loop
8486 if Covers (Typ, It.Typ) then
8487 Set_Entity (Selector_Name (Entry_Name), It.Nam);
8488 Set_Etype (Entry_Name, It.Typ);
8489
8490 Generate_Reference (It.Typ, N, ' ');
8491 end if;
8492
8493 Get_Next_Interp (I, It);
8494 end loop;
8495 end;
8496 end if;
8497
8498 Resolve_Entry (Entry_Name);
8499
8500 if Nkind (Entry_Name) = N_Selected_Component then
8501
8502 -- Simple entry or protected operation call
8503
8504 Nam := Entity (Selector_Name (Entry_Name));
8505 Obj := Prefix (Entry_Name);
8506
8507 if Is_Subprogram (Nam) then
8508 Check_For_Eliminated_Subprogram (Entry_Name, Nam);
8509 end if;
8510
8511 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
8512
8513 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8514
8515 -- Call to member of entry family
8516
8517 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
8518 Obj := Prefix (Prefix (Entry_Name));
8519 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
8520 end if;
8521
8522 -- We cannot in general check the maximum depth of protected entry calls
8523 -- at compile time. But we can tell that any protected entry call at all
8524 -- violates a specified nesting depth of zero.
8525
8526 if Is_Protected_Type (Scope (Nam)) then
8527 Check_Restriction (Max_Entry_Queue_Length, N);
8528 end if;
8529
8530 -- Use context type to disambiguate a protected function that can be
8531 -- called without actuals and that returns an array type, and where the
8532 -- argument list may be an indexing of the returned value.
8533
8534 if Ekind (Nam) = E_Function
8535 and then Needs_No_Actuals (Nam)
8536 and then Present (Parameter_Associations (N))
8537 and then
8538 ((Is_Array_Type (Etype (Nam))
8539 and then Covers (Typ, Component_Type (Etype (Nam))))
8540
8541 or else (Is_Access_Type (Etype (Nam))
8542 and then Is_Array_Type (Designated_Type (Etype (Nam)))
8543 and then
8544 Covers
8545 (Typ,
8546 Component_Type (Designated_Type (Etype (Nam))))))
8547 then
8548 declare
8549 Index_Node : Node_Id;
8550
8551 begin
8552 Index_Node :=
8553 Make_Indexed_Component (Loc,
8554 Prefix =>
8555 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
8556 Expressions => Parameter_Associations (N));
8557
8558 -- Since we are correcting a node classification error made by the
8559 -- parser, we call Replace rather than Rewrite.
8560
8561 Replace (N, Index_Node);
8562 Set_Etype (Prefix (N), Etype (Nam));
8563 Set_Etype (N, Typ);
8564 Resolve_Indexed_Component (N, Typ);
8565 return;
8566 end;
8567 end if;
8568
8569 if Is_Entry (Nam)
8570 and then Present (Contract_Wrapper (Nam))
8571 and then Current_Scope /= Contract_Wrapper (Nam)
8572 and then Current_Scope /= Wrapped_Statements (Contract_Wrapper (Nam))
8573 then
8574 -- Note the entity being called before rewriting the call, so that
8575 -- it appears used at this point.
8576
8577 Generate_Reference (Nam, Entry_Name, 'r');
8578
8579 -- Rewrite as call to the precondition wrapper, adding the task
8580 -- object to the list of actuals. If the call is to a member of an
8581 -- entry family, include the index as well.
8582
8583 declare
8584 New_Call : Node_Id;
8585 New_Actuals : List_Id;
8586
8587 begin
8588 New_Actuals := New_List (Obj);
8589
8590 if Nkind (Entry_Name) = N_Indexed_Component then
8591 Append_To (New_Actuals,
8592 New_Copy_Tree (First (Expressions (Entry_Name))));
8593 end if;
8594
8595 Append_List (Parameter_Associations (N), New_Actuals);
8596 New_Call :=
8597 Make_Procedure_Call_Statement (Loc,
8598 Name =>
8599 New_Occurrence_Of (Contract_Wrapper (Nam), Loc),
8600 Parameter_Associations => New_Actuals);
8601 Rewrite (N, New_Call);
8602
8603 -- Preanalyze and resolve new call. Current procedure is called
8604 -- from Resolve_Call, after which expansion will take place.
8605
8606 Preanalyze_And_Resolve (N);
8607 return;
8608 end;
8609 end if;
8610
8611 -- The operation name may have been overloaded. Order the actuals
8612 -- according to the formals of the resolved entity, and set the return
8613 -- type to that of the operation.
8614
8615 if Was_Over then
8616 Normalize_Actuals (N, Nam, False, Norm_OK);
8617 pragma Assert (Norm_OK);
8618 Set_Etype (N, Etype (Nam));
8619
8620 -- Reset the Is_Overloaded flag, since resolution is now completed
8621
8622 -- Simple entry call
8623
8624 if Nkind (Entry_Name) = N_Selected_Component then
8625 Set_Is_Overloaded (Selector_Name (Entry_Name), False);
8626
8627 -- Call to a member of an entry family
8628
8629 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
8630 Set_Is_Overloaded (Selector_Name (Prefix (Entry_Name)), False);
8631 end if;
8632 end if;
8633
8634 Resolve_Actuals (N, Nam);
8635 Check_Internal_Protected_Use (N, Nam);
8636
8637 -- Create a call reference to the entry
8638
8639 Generate_Reference (Nam, Entry_Name, 's');
8640
8641 if Is_Entry (Nam) then
8642 Check_Potentially_Blocking_Operation (N);
8643 end if;
8644
8645 -- Verify that a procedure call cannot masquerade as an entry
8646 -- call where an entry call is expected.
8647
8648 if Ekind (Nam) = E_Procedure then
8649 if Nkind (Parent (N)) = N_Entry_Call_Alternative
8650 and then N = Entry_Call_Statement (Parent (N))
8651 then
8652 Error_Msg_N ("entry call required in select statement", N);
8653
8654 elsif Nkind (Parent (N)) = N_Triggering_Alternative
8655 and then N = Triggering_Statement (Parent (N))
8656 then
8657 Error_Msg_N ("triggering statement cannot be procedure call", N);
8658
8659 elsif Ekind (Scope (Nam)) = E_Task_Type
8660 and then not In_Open_Scopes (Scope (Nam))
8661 then
8662 Error_Msg_N ("task has no entry with this name", Entry_Name);
8663 end if;
8664 end if;
8665
8666 -- After resolution, entry calls and protected procedure calls are
8667 -- changed into entry calls, for expansion. The structure of the node
8668 -- does not change, so it can safely be done in place. Protected
8669 -- function calls must keep their structure because they are
8670 -- subexpressions.
8671
8672 if Ekind (Nam) /= E_Function then
8673
8674 -- A protected operation that is not a function may modify the
8675 -- corresponding object, and cannot apply to a constant. If this
8676 -- is an internal call, the prefix is the type itself.
8677
8678 if Is_Protected_Type (Scope (Nam))
8679 and then not Is_Variable (Obj)
8680 and then (not Is_Entity_Name (Obj)
8681 or else not Is_Type (Entity (Obj)))
8682 then
8683 Error_Msg_N
8684 ("prefix of protected procedure or entry call must be variable",
8685 Entry_Name);
8686 end if;
8687
8688 declare
8689 Entry_Call : Node_Id;
8690
8691 begin
8692 Entry_Call :=
8693 Make_Entry_Call_Statement (Loc,
8694 Name => Entry_Name,
8695 Parameter_Associations => Parameter_Associations (N));
8696
8697 -- Inherit relevant attributes from the original call
8698
8699 Set_First_Named_Actual
8700 (Entry_Call, First_Named_Actual (N));
8701
8702 Set_Is_Elaboration_Checks_OK_Node
8703 (Entry_Call, Is_Elaboration_Checks_OK_Node (N));
8704
8705 Set_Is_Elaboration_Warnings_OK_Node
8706 (Entry_Call, Is_Elaboration_Warnings_OK_Node (N));
8707
8708 Set_Is_SPARK_Mode_On_Node
8709 (Entry_Call, Is_SPARK_Mode_On_Node (N));
8710
8711 Rewrite (N, Entry_Call);
8712 Set_Analyzed (N, True);
8713 end;
8714
8715 -- Protected functions can return on the secondary stack, in which case
8716 -- we must trigger the transient scope mechanism.
8717
8718 elsif Expander_Active
8719 and then Requires_Transient_Scope (Etype (Nam))
8720 then
8721 Establish_Transient_Scope (N, Needs_Secondary_Stack (Etype (Nam)));
8722 end if;
8723
8724 -- Now we know that this is not a call to a function that returns an
8725 -- array type; moreover, we know the name of the called entry. Detect
8726 -- overlapping actuals, just like for a subprogram call.
8727
8728 Warn_On_Overlapping_Actuals (Nam, N);
8729 end Resolve_Entry_Call;
8730
8731 -------------------------
8732 -- Resolve_Equality_Op --
8733 -------------------------
8734
8735 -- The operands must have compatible types and the boolean context does not
8736 -- participate in the resolution. The first pass verifies that the operands
8737 -- are not ambiguous and sets their type correctly, or to Any_Type in case
8738 -- of ambiguity. If both operands are strings, aggregates, allocators, or
8739 -- null, they are ambiguous even if they carry a single (universal) type.
8740
8741 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
8742 L : constant Node_Id := Left_Opnd (N);
8743 R : constant Node_Id := Right_Opnd (N);
8744
8745 Implicit_NE_For_User_Defined_Operator : constant Boolean :=
8746 Nkind (N) = N_Op_Ne
8747 and then Ekind (Entity (N)) = E_Function
8748 and then not Comes_From_Source (Entity (N))
8749 and then not
8750 Is_Intrinsic_Subprogram (Corresponding_Equality (Entity (N)));
8751 -- Whether this is a call to the implicit inequality operator created
8752 -- for a user-defined operator that is not an intrinsic subprogram, in
8753 -- which case we need to skip some processing.
8754
8755 T : Entity_Id := Find_Unique_Type (L, R);
8756
8757 procedure Check_Access_Attribute (N : Node_Id);
8758 -- For any object, '[Unchecked_]Access of such object can never be
8759 -- passed as an operand to the Universal_Access equality operators.
8760 -- This is so because the expected type for Obj'Access in a call to
8761 -- these operators, whose formals are of type Universal_Access, is
8762 -- Universal_Access, and Universal_Access does not have a designated
8763 -- type. For more details, see RM 3.10.2(2/2) and 6.4.1(3).
8764
8765 procedure Check_Designated_Object_Types (T1, T2 : Entity_Id);
8766 -- Check RM 4.5.2(9.6/2) on the given designated object types
8767
8768 procedure Check_Designated_Subprogram_Types (T1, T2 : Entity_Id);
8769 -- Check RM 4.5.2(9.7/2) on the given designated subprogram types
8770
8771 procedure Check_If_Expression (Cond : Node_Id);
8772 -- The resolution rule for if expressions requires that each such must
8773 -- have a unique type. This means that if several dependent expressions
8774 -- are of a non-null anonymous access type, and the context does not
8775 -- impose an expected type (as can be the case in an equality operation)
8776 -- the expression must be rejected.
8777
8778 procedure Explain_Redundancy (N : Node_Id);
8779 -- Attempt to explain the nature of a redundant comparison with True. If
8780 -- the expression N is too complex, this routine issues a general error
8781 -- message.
8782
8783 function Find_Unique_Access_Type return Entity_Id;
8784 -- In the case of allocators and access attributes, the context must
8785 -- provide an indication of the specific access type to be used. If
8786 -- one operand is of such a "generic" access type, check whether there
8787 -- is a specific visible access type that has the same designated type.
8788 -- This is semantically dubious, and of no interest to any real code,
8789 -- but c48008a makes it all worthwhile.
8790
8791 function Suspicious_Prio_For_Equality return Boolean;
8792 -- Returns True iff the parent node is a and/or/xor operation that
8793 -- could be the cause of confused priorities. Note that if the not is
8794 -- in parens, then False is returned.
8795
8796 ----------------------------
8797 -- Check_Access_Attribute --
8798 ----------------------------
8799
8800 procedure Check_Access_Attribute (N : Node_Id) is
8801 begin
8802 if Nkind (N) = N_Attribute_Reference
8803 and then Attribute_Name (N) in Name_Access | Name_Unchecked_Access
8804 then
8805 Error_Msg_N
8806 ("access attribute cannot be used as actual for "
8807 & "universal_access equality", N);
8808 end if;
8809 end Check_Access_Attribute;
8810
8811 -----------------------------------
8812 -- Check_Designated_Object_Types --
8813 -----------------------------------
8814
8815 procedure Check_Designated_Object_Types (T1, T2 : Entity_Id) is
8816 begin
8817 if (Is_Elementary_Type (T1) or else Is_Array_Type (T1))
8818 and then (Base_Type (T1) /= Base_Type (T2)
8819 or else not Subtypes_Statically_Match (T1, T2))
8820 then
8821 Error_Msg_N
8822 ("designated subtypes for universal_access equality "
8823 & "do not statically match (RM 4.5.2(9.6/2)", N);
8824 Error_Msg_NE ("\left operand has}!", N, Etype (L));
8825 Error_Msg_NE ("\right operand has}!", N, Etype (R));
8826 end if;
8827 end Check_Designated_Object_Types;
8828
8829 ---------------------------------------
8830 -- Check_Designated_Subprogram_Types --
8831 ---------------------------------------
8832
8833 procedure Check_Designated_Subprogram_Types (T1, T2 : Entity_Id) is
8834 begin
8835 if not Subtype_Conformant (T1, T2) then
8836 Error_Msg_N
8837 ("designated subtypes for universal_access equality "
8838 & "not subtype conformant (RM 4.5.2(9.7/2)", N);
8839 Error_Msg_NE ("\left operand has}!", N, Etype (L));
8840 Error_Msg_NE ("\right operand has}!", N, Etype (R));
8841 end if;
8842 end Check_Designated_Subprogram_Types;
8843
8844 -------------------------
8845 -- Check_If_Expression --
8846 -------------------------
8847
8848 procedure Check_If_Expression (Cond : Node_Id) is
8849 Then_Expr : Node_Id;
8850 Else_Expr : Node_Id;
8851
8852 begin
8853 if Nkind (Cond) = N_If_Expression then
8854 Then_Expr := Next (First (Expressions (Cond)));
8855 Else_Expr := Next (Then_Expr);
8856
8857 if Nkind (Then_Expr) /= N_Null
8858 and then Nkind (Else_Expr) /= N_Null
8859 then
8860 Error_Msg_N ("cannot determine type of if expression", Cond);
8861 end if;
8862 end if;
8863 end Check_If_Expression;
8864
8865 ------------------------
8866 -- Explain_Redundancy --
8867 ------------------------
8868
8869 procedure Explain_Redundancy (N : Node_Id) is
8870 Error : Name_Id;
8871 Val : Node_Id;
8872 Val_Id : Entity_Id;
8873
8874 begin
8875 Val := N;
8876
8877 -- Strip the operand down to an entity
8878
8879 loop
8880 if Nkind (Val) = N_Selected_Component then
8881 Val := Selector_Name (Val);
8882 else
8883 exit;
8884 end if;
8885 end loop;
8886
8887 -- The construct denotes an entity
8888
8889 if Is_Entity_Name (Val) and then Present (Entity (Val)) then
8890 Val_Id := Entity (Val);
8891
8892 -- Do not generate an error message when the comparison is done
8893 -- against the enumeration literal Standard.True.
8894
8895 if Ekind (Val_Id) /= E_Enumeration_Literal then
8896
8897 -- Build a customized error message
8898
8899 Name_Len := 0;
8900 Add_Str_To_Name_Buffer ("?r?");
8901
8902 if Ekind (Val_Id) = E_Component then
8903 Add_Str_To_Name_Buffer ("component ");
8904
8905 elsif Ekind (Val_Id) = E_Constant then
8906 Add_Str_To_Name_Buffer ("constant ");
8907
8908 elsif Ekind (Val_Id) = E_Discriminant then
8909 Add_Str_To_Name_Buffer ("discriminant ");
8910
8911 elsif Is_Formal (Val_Id) then
8912 Add_Str_To_Name_Buffer ("parameter ");
8913
8914 elsif Ekind (Val_Id) = E_Variable then
8915 Add_Str_To_Name_Buffer ("variable ");
8916 end if;
8917
8918 Add_Str_To_Name_Buffer ("& is always True!");
8919 Error := Name_Find;
8920
8921 Error_Msg_NE (Get_Name_String (Error), Val, Val_Id);
8922 end if;
8923
8924 -- The construct is too complex to disect, issue a general message
8925
8926 else
8927 Error_Msg_N ("?r?expression is always True!", Val);
8928 end if;
8929 end Explain_Redundancy;
8930
8931 -----------------------------
8932 -- Find_Unique_Access_Type --
8933 -----------------------------
8934
8935 function Find_Unique_Access_Type return Entity_Id is
8936 Acc : Entity_Id;
8937 E : Entity_Id;
8938 S : Entity_Id;
8939
8940 begin
8941 if Ekind (Etype (R)) in E_Allocator_Type | E_Access_Attribute_Type
8942 then
8943 Acc := Designated_Type (Etype (R));
8944
8945 elsif Ekind (Etype (L)) in E_Allocator_Type | E_Access_Attribute_Type
8946 then
8947 Acc := Designated_Type (Etype (L));
8948 else
8949 return Empty;
8950 end if;
8951
8952 S := Current_Scope;
8953 while S /= Standard_Standard loop
8954 E := First_Entity (S);
8955 while Present (E) loop
8956 if Is_Type (E)
8957 and then Is_Access_Type (E)
8958 and then Ekind (E) /= E_Allocator_Type
8959 and then Designated_Type (E) = Base_Type (Acc)
8960 then
8961 return E;
8962 end if;
8963
8964 Next_Entity (E);
8965 end loop;
8966
8967 S := Scope (S);
8968 end loop;
8969
8970 return Empty;
8971 end Find_Unique_Access_Type;
8972
8973 ----------------------------------
8974 -- Suspicious_Prio_For_Equality --
8975 ----------------------------------
8976
8977 function Suspicious_Prio_For_Equality return Boolean is
8978 Par : constant Node_Id := Parent (N);
8979
8980 begin
8981 -- Check if parent node is one of and/or/xor, not parenthesized
8982 -- explicitly, and its own parent is not of this kind. Otherwise,
8983 -- it's a case of chained Boolean conditions which is likely well
8984 -- parenthesized.
8985
8986 if Nkind (Par) in N_Op_And | N_Op_Or | N_Op_Xor
8987 and then Paren_Count (N) = 0
8988 and then Nkind (Parent (Par)) not in N_Op_And | N_Op_Or | N_Op_Xor
8989 then
8990 declare
8991 Compar : Node_Id :=
8992 (if Left_Opnd (Par) = N then
8993 Right_Opnd (Par)
8994 else
8995 Left_Opnd (Par));
8996 begin
8997 -- Compar may have been rewritten, for example from (a /= b)
8998 -- into not (a = b). Use the Original_Node instead.
8999
9000 Compar := Original_Node (Compar);
9001
9002 -- If the other argument of the and/or/xor is also a
9003 -- comparison, or another and/or/xor then most likely
9004 -- the priorities are correctly set.
9005
9006 return Nkind (Compar) not in N_Op_Boolean;
9007 end;
9008
9009 else
9010 return False;
9011 end if;
9012 end Suspicious_Prio_For_Equality;
9013
9014 -- Start of processing for Resolve_Equality_Op
9015
9016 begin
9017 if T = Any_Fixed then
9018 T := Unique_Fixed_Point_Type (L);
9019 end if;
9020
9021 Set_Etype (N, Base_Type (Typ));
9022 Generate_Reference (T, N, ' ');
9023
9024 if T = Any_Type then
9025 -- Deal with explicit ambiguity of operands, unless this is a call
9026 -- to the implicit inequality operator created for a user-defined
9027 -- operator that is not an intrinsic subprogram, since the common
9028 -- resolution of operands done here does not apply to it.
9029
9030 if not Implicit_NE_For_User_Defined_Operator
9031 and then (Is_Overloaded (L) or else Is_Overloaded (R))
9032 then
9033 Ambiguous_Operands (N);
9034 end if;
9035
9036 else
9037
9038 -- For Ada 2022, check for user-defined literals when the type has
9039 -- the appropriate aspect.
9040
9041 if Has_Applicable_User_Defined_Literal (L, Etype (R)) then
9042 Resolve (L, Etype (R));
9043 Set_Etype (N, Standard_Boolean);
9044 end if;
9045
9046 if Has_Applicable_User_Defined_Literal (R, Etype (L)) then
9047 Resolve (R, Etype (L));
9048 Set_Etype (N, Standard_Boolean);
9049 end if;
9050
9051 -- Deal with other error cases
9052
9053 if T = Any_String or else
9054 T = Any_Composite or else
9055 T = Any_Character
9056 then
9057 if T = Any_Character then
9058 Ambiguous_Character (L);
9059 else
9060 Error_Msg_N ("ambiguous operands for equality", N);
9061 end if;
9062
9063 Set_Etype (N, Any_Type);
9064 return;
9065
9066 elsif T = Universal_Access
9067 or else Ekind (T) in E_Allocator_Type | E_Access_Attribute_Type
9068 then
9069 T := Find_Unique_Access_Type;
9070
9071 if No (T) then
9072 Error_Msg_N ("ambiguous operands for equality", N);
9073 Set_Etype (N, Any_Type);
9074 return;
9075 end if;
9076
9077 -- If expressions must have a single type, and if the context does
9078 -- not impose one the dependent expressions cannot be anonymous
9079 -- access types.
9080
9081 -- Why no similar processing for case expressions???
9082
9083 elsif Ada_Version >= Ada_2012
9084 and then Is_Anonymous_Access_Type (Etype (L))
9085 and then Is_Anonymous_Access_Type (Etype (R))
9086 then
9087 Check_If_Expression (L);
9088 Check_If_Expression (R);
9089 end if;
9090
9091 -- RM 4.5.2(9.5/2): At least one of the operands of the equality
9092 -- operators for universal_access shall be of type universal_access,
9093 -- or both shall be of access-to-object types, or both shall be of
9094 -- access-to-subprogram types (RM 4.5.2(9.5/2)).
9095
9096 if Is_Anonymous_Access_Type (T)
9097 and then Etype (L) /= Universal_Access
9098 and then Etype (R) /= Universal_Access
9099 then
9100 -- RM 4.5.2(9.6/2): When both are of access-to-object types, the
9101 -- designated types shall be the same or one shall cover the other
9102 -- and if the designated types are elementary or array types, then
9103 -- the designated subtypes shall statically match.
9104
9105 if Is_Access_Object_Type (Etype (L))
9106 and then Is_Access_Object_Type (Etype (R))
9107 then
9108 Check_Designated_Object_Types
9109 (Designated_Type (Etype (L)), Designated_Type (Etype (R)));
9110
9111 -- RM 4.5.2(9.7/2): When both are of access-to-subprogram types,
9112 -- the designated profiles shall be subtype conformant.
9113
9114 elsif Is_Access_Subprogram_Type (Etype (L))
9115 and then Is_Access_Subprogram_Type (Etype (R))
9116 then
9117 Check_Designated_Subprogram_Types
9118 (Designated_Type (Etype (L)), Designated_Type (Etype (R)));
9119 end if;
9120 end if;
9121
9122 -- Check another case of equality operators for universal_access
9123
9124 if Is_Anonymous_Access_Type (T) and then Comes_From_Source (N) then
9125 Check_Access_Attribute (L);
9126 Check_Access_Attribute (R);
9127 end if;
9128
9129 Resolve (L, T);
9130 Resolve (R, T);
9131 Set_Compare_Type (N, T);
9132
9133 -- AI12-0413: user-defined primitive equality of an untagged record
9134 -- type hides the predefined equality operator, including within a
9135 -- generic, and if it is declared abstract, results in an illegal
9136 -- instance if the operator is used in the spec, or in the raising
9137 -- of Program_Error if used in the body of an instance.
9138
9139 if Nkind (N) = N_Op_Eq
9140 and then In_Instance
9141 and then Ada_Version >= Ada_2012
9142 then
9143 declare
9144 U : constant Entity_Id := Underlying_Type (T);
9145
9146 Eq : Entity_Id;
9147
9148 begin
9149 if Present (U)
9150 and then Is_Record_Type (U)
9151 and then not Is_Tagged_Type (U)
9152 then
9153 Eq := Get_User_Defined_Equality (T);
9154
9155 if Present (Eq) then
9156 if Is_Abstract_Subprogram (Eq) then
9157 Nondispatching_Call_To_Abstract_Operation (N, Eq);
9158 else
9159 Rewrite_Operator_As_Call (N, Eq);
9160 end if;
9161
9162 return;
9163 end if;
9164 end if;
9165 end;
9166 end if;
9167
9168 -- If the unique type is a class-wide type then it will be expanded
9169 -- into a dispatching call to the predefined primitive. Therefore we
9170 -- check here for potential violation of such restriction.
9171
9172 if Is_Class_Wide_Type (T) then
9173 Check_Restriction (No_Dispatching_Calls, N);
9174 end if;
9175
9176 -- Only warn for redundant equality comparison to True for objects
9177 -- (e.g. "X = True") and operations (e.g. "(X < Y) = True"). For
9178 -- other expressions, it may be a matter of preference to write
9179 -- "Expr = True" or "Expr".
9180
9181 if Warn_On_Redundant_Constructs
9182 and then Comes_From_Source (N)
9183 and then Comes_From_Source (R)
9184 and then Is_Entity_Name (R)
9185 and then Entity (R) = Standard_True
9186 and then
9187 ((Is_Entity_Name (L) and then Is_Object (Entity (L)))
9188 or else
9189 Nkind (L) in N_Op)
9190 then
9191 Error_Msg_N -- CODEFIX
9192 ("?r?comparison with True is redundant!", N);
9193 Explain_Redundancy (Original_Node (R));
9194 end if;
9195
9196 -- Warn on a (in)equality between boolean values which is not
9197 -- parenthesized when the parent expression is one of and/or/xor, as
9198 -- this is interpreted as (a = b) op c where most likely a = (b op c)
9199 -- was intended. Do not generate a warning in generic instances, as
9200 -- the problematic expression may be implicitly parenthesized in
9201 -- the generic itself if one of the operators is a generic formal.
9202 -- Also do not generate a warning for generated equality, for
9203 -- example from rewritting a membership test.
9204
9205 if Warn_On_Questionable_Missing_Parens
9206 and then not In_Instance
9207 and then Comes_From_Source (N)
9208 and then Is_Boolean_Type (T)
9209 and then Suspicious_Prio_For_Equality
9210 then
9211 Error_Msg_N ("?q?equality should be parenthesized here!", N);
9212 end if;
9213
9214 Check_Unset_Reference (L);
9215 Check_Unset_Reference (R);
9216 Generate_Operator_Reference (N, T);
9217 Check_Low_Bound_Tested (N);
9218
9219 -- Unless this is a call to the implicit inequality operator created
9220 -- for a user-defined operator that is not an intrinsic subprogram,
9221 -- try to fold the operation.
9222
9223 if not Implicit_NE_For_User_Defined_Operator then
9224 Analyze_Dimension (N);
9225 Eval_Relational_Op (N);
9226
9227 elsif Nkind (N) = N_Op_Ne
9228 and then Is_Abstract_Subprogram (Entity (N))
9229 then
9230 Nondispatching_Call_To_Abstract_Operation (N, Entity (N));
9231 end if;
9232 end if;
9233 end Resolve_Equality_Op;
9234
9235 ----------------------------------
9236 -- Resolve_Explicit_Dereference --
9237 ----------------------------------
9238
9239 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
9240 Loc : constant Source_Ptr := Sloc (N);
9241 New_N : Node_Id;
9242 P : constant Node_Id := Prefix (N);
9243
9244 P_Typ : Entity_Id;
9245 -- The candidate prefix type, if overloaded
9246
9247 I : Interp_Index;
9248 It : Interp;
9249
9250 begin
9251 Check_Fully_Declared_Prefix (Typ, P);
9252 P_Typ := Empty;
9253
9254 -- A useful optimization: check whether the dereference denotes an
9255 -- element of a container, and if so rewrite it as a call to the
9256 -- corresponding Element function.
9257
9258 -- Disabled for now, on advice of ARG. A more restricted form of the
9259 -- predicate might be acceptable ???
9260
9261 -- if Is_Container_Element (N) then
9262 -- return;
9263 -- end if;
9264
9265 if Is_Overloaded (P) then
9266
9267 -- Use the context type to select the prefix that has the correct
9268 -- designated type. Keep the first match, which will be the inner-
9269 -- most.
9270
9271 Get_First_Interp (P, I, It);
9272
9273 while Present (It.Typ) loop
9274 if Is_Access_Type (It.Typ)
9275 and then Covers (Typ, Designated_Type (It.Typ))
9276 then
9277 if No (P_Typ) then
9278 P_Typ := It.Typ;
9279 end if;
9280
9281 -- Remove access types that do not match, but preserve access
9282 -- to subprogram interpretations, in case a further dereference
9283 -- is needed (see below).
9284
9285 elsif Ekind (It.Typ) /= E_Access_Subprogram_Type then
9286 Remove_Interp (I);
9287 end if;
9288
9289 Get_Next_Interp (I, It);
9290 end loop;
9291
9292 if Present (P_Typ) then
9293 Resolve (P, P_Typ);
9294 Set_Etype (N, Designated_Type (P_Typ));
9295
9296 else
9297 -- If no interpretation covers the designated type of the prefix,
9298 -- this is the pathological case where not all implementations of
9299 -- the prefix allow the interpretation of the node as a call. Now
9300 -- that the expected type is known, Remove other interpretations
9301 -- from prefix, rewrite it as a call, and resolve again, so that
9302 -- the proper call node is generated.
9303
9304 Get_First_Interp (P, I, It);
9305 while Present (It.Typ) loop
9306 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
9307 Remove_Interp (I);
9308 end if;
9309
9310 Get_Next_Interp (I, It);
9311 end loop;
9312
9313 New_N :=
9314 Make_Function_Call (Loc,
9315 Name =>
9316 Make_Explicit_Dereference (Loc,
9317 Prefix => P),
9318 Parameter_Associations => New_List);
9319
9320 Save_Interps (N, New_N);
9321 Rewrite (N, New_N);
9322 Analyze_And_Resolve (N, Typ);
9323 return;
9324 end if;
9325
9326 -- If not overloaded, resolve P with its own type
9327
9328 else
9329 Resolve (P);
9330 end if;
9331
9332 -- If the prefix might be null, add an access check
9333
9334 if Is_Access_Type (Etype (P))
9335 and then not Can_Never_Be_Null (Etype (P))
9336 then
9337 Apply_Access_Check (N);
9338 end if;
9339
9340 -- If the designated type is a packed unconstrained array type, and the
9341 -- explicit dereference is not in the context of an attribute reference,
9342 -- then we must compute and set the actual subtype, since it is needed
9343 -- by Gigi. The reason we exclude the attribute case is that this is
9344 -- handled fine by Gigi, and in fact we use such attributes to build the
9345 -- actual subtype. We also exclude generated code (which builds actual
9346 -- subtypes directly if they are needed).
9347
9348 if Is_Packed_Array (Etype (N))
9349 and then not Is_Constrained (Etype (N))
9350 and then Nkind (Parent (N)) /= N_Attribute_Reference
9351 and then Comes_From_Source (N)
9352 then
9353 Set_Etype (N, Get_Actual_Subtype (N));
9354 end if;
9355
9356 Analyze_Dimension (N);
9357
9358 -- Note: No Eval processing is required for an explicit dereference,
9359 -- because such a name can never be static.
9360
9361 end Resolve_Explicit_Dereference;
9362
9363 -------------------------------------
9364 -- Resolve_Expression_With_Actions --
9365 -------------------------------------
9366
9367 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
9368
9369 function OK_For_Static (Act : Node_Id) return Boolean;
9370 -- True if Act is an action of a declare_expression that is allowed in a
9371 -- static declare_expression.
9372
9373 function All_OK_For_Static return Boolean;
9374 -- True if all actions of N are allowed in a static declare_expression.
9375
9376 function Get_Literal (Expr : Node_Id) return Node_Id;
9377 -- Expr is an expression with compile-time-known value. This returns the
9378 -- literal node that reprsents that value.
9379
9380 -------------------
9381 -- OK_For_Static --
9382 -------------------
9383
9384 function OK_For_Static (Act : Node_Id) return Boolean is
9385 begin
9386 case Nkind (Act) is
9387 when N_Object_Declaration =>
9388 if Constant_Present (Act)
9389 and then Is_Static_Expression (Expression (Act))
9390 then
9391 return True;
9392 end if;
9393
9394 when N_Object_Renaming_Declaration =>
9395 if Statically_Names_Object (Name (Act)) then
9396 return True;
9397 end if;
9398
9399 when others =>
9400 -- No other declarations, nor even pragmas, are allowed in a
9401 -- declare expression, so if we see something else, it must be
9402 -- an internally generated expression_with_actions.
9403 null;
9404 end case;
9405
9406 return False;
9407 end OK_For_Static;
9408
9409 -----------------------
9410 -- All_OK_For_Static --
9411 -----------------------
9412
9413 function All_OK_For_Static return Boolean is
9414 Act : Node_Id := First (Actions (N));
9415 begin
9416 while Present (Act) loop
9417 if not OK_For_Static (Act) then
9418 return False;
9419 end if;
9420
9421 Next (Act);
9422 end loop;
9423
9424 return True;
9425 end All_OK_For_Static;
9426
9427 -----------------
9428 -- Get_Literal --
9429 -----------------
9430
9431 function Get_Literal (Expr : Node_Id) return Node_Id is
9432 pragma Assert (Compile_Time_Known_Value (Expr));
9433 Result : Node_Id;
9434 begin
9435 case Nkind (Expr) is
9436 when N_Has_Entity =>
9437 if Ekind (Entity (Expr)) = E_Enumeration_Literal then
9438 Result := Expr;
9439 else
9440 Result := Constant_Value (Entity (Expr));
9441 end if;
9442 when N_Numeric_Or_String_Literal =>
9443 Result := Expr;
9444 when others =>
9445 raise Program_Error;
9446 end case;
9447
9448 pragma Assert
9449 (Nkind (Result) in N_Numeric_Or_String_Literal
9450 or else Ekind (Entity (Result)) = E_Enumeration_Literal);
9451 return Result;
9452 end Get_Literal;
9453
9454 -- Local variables
9455
9456 Loc : constant Source_Ptr := Sloc (N);
9457
9458 -- Start of processing for Resolve_Expression_With_Actions
9459
9460 begin
9461 Set_Etype (N, Typ);
9462
9463 if Is_Empty_List (Actions (N)) then
9464 pragma Assert (All_OK_For_Static); null;
9465 end if;
9466
9467 -- If the value of the expression is known at compile time, and all
9468 -- of the actions (if any) are suitable, then replace the declare
9469 -- expression with its expression. This allows the declare expression
9470 -- as a whole to be static if appropriate. See AI12-0368.
9471
9472 if Compile_Time_Known_Value (Expression (N)) then
9473 if Is_Empty_List (Actions (N)) then
9474 Rewrite (N, Expression (N));
9475 elsif All_OK_For_Static then
9476 Rewrite
9477 (N, New_Copy_Tree
9478 (Get_Literal (Expression (N)), New_Sloc => Loc));
9479 end if;
9480 end if;
9481 end Resolve_Expression_With_Actions;
9482
9483 ----------------------------------
9484 -- Resolve_External_Initializer --
9485 ----------------------------------
9486
9487 procedure Resolve_External_Initializer (N : Node_Id; Typ : Entity_Id) is
9488 begin
9489 Set_String_Literal_Subtype (N, Typ);
9490 end Resolve_External_Initializer;
9491
9492 ----------------------------------
9493 -- Resolve_Generalized_Indexing --
9494 ----------------------------------
9495
9496 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id) is
9497 Indexing : constant Node_Id := Generalized_Indexing (N);
9498 begin
9499 Rewrite (N, Indexing);
9500 Resolve (N, Typ);
9501 end Resolve_Generalized_Indexing;
9502
9503 ---------------------------
9504 -- Resolve_If_Expression --
9505 ---------------------------
9506
9507 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id) is
9508 Condition : constant Node_Id := First (Expressions (N));
9509
9510 procedure Apply_Check (Expr : Node_Id; Result_Type : Entity_Id);
9511 -- When a dependent expression is of a subtype different from
9512 -- the context subtype, then insert a qualification to ensure
9513 -- the generation of a constraint check. This was previously
9514 -- for scalar types. For array types apply a length check, given
9515 -- that the context in general allows sliding, while a qualified
9516 -- expression forces equality of bounds.
9517
9518 -----------------
9519 -- Apply_Check --
9520 -----------------
9521
9522 procedure Apply_Check (Expr : Node_Id; Result_Type : Entity_Id) is
9523 Expr_Typ : constant Entity_Id := Etype (Expr);
9524 Loc : constant Source_Ptr := Sloc (Expr);
9525
9526 begin
9527 if Expr_Typ = Typ
9528 or else Is_Tagged_Type (Typ)
9529 or else Is_Access_Type (Typ)
9530 or else not Is_Constrained (Typ)
9531 or else Inside_A_Generic
9532 then
9533 null;
9534
9535 elsif Is_Array_Type (Typ) then
9536 Apply_Length_Check (Expr, Typ);
9537
9538 else
9539 Rewrite (Expr,
9540 Make_Qualified_Expression (Loc,
9541 Subtype_Mark => New_Occurrence_Of (Result_Type, Loc),
9542 Expression => Relocate_Node (Expr)));
9543
9544 Analyze_And_Resolve (Expr, Result_Type);
9545 end if;
9546 end Apply_Check;
9547
9548 -- Local variables
9549
9550 Else_Expr : Node_Id;
9551 Then_Expr : Node_Id;
9552
9553 Result_Type : Entity_Id;
9554 -- So in most cases the type of the if_expression and of its
9555 -- dependent expressions is that of the context. However, if
9556 -- the expression is the index of an Indexed_Component, we must
9557 -- ensure that a proper index check is applied, rather than a
9558 -- range check on the index type (which might be discriminant
9559 -- dependent). In this case we resolve with the base type of the
9560 -- index type, and the index check is generated in the resolution
9561 -- of the indexed_component above.
9562
9563 -- Start of processing for Resolve_If_Expression
9564
9565 begin
9566 -- Defend against malformed expressions
9567
9568 if No (Condition) then
9569 return;
9570 end if;
9571
9572 if Present (Parent (N))
9573 and then (Nkind (Parent (N)) = N_Indexed_Component
9574 or else Nkind (Parent (Parent (N))) = N_Indexed_Component)
9575 then
9576 Result_Type := Base_Type (Typ);
9577
9578 else
9579 Result_Type := Typ;
9580 end if;
9581
9582 Then_Expr := Next (Condition);
9583
9584 if No (Then_Expr) then
9585 return;
9586 end if;
9587
9588 Resolve (Condition, Any_Boolean);
9589 Check_Unset_Reference (Condition);
9590
9591 Resolve_Dependent_Expression (N, Then_Expr, Result_Type);
9592
9593 Check_Unset_Reference (Then_Expr);
9594 Apply_Check (Then_Expr, Result_Type);
9595
9596 Else_Expr := Next (Then_Expr);
9597
9598 -- If ELSE expression present, just resolve using the determined type
9599
9600 if Present (Else_Expr) then
9601 Resolve_Dependent_Expression (N, Else_Expr, Result_Type);
9602
9603 Check_Unset_Reference (Else_Expr);
9604 Apply_Check (Else_Expr, Result_Type);
9605
9606 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
9607 -- dynamically tagged must be known statically.
9608
9609 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
9610 if Is_Dynamically_Tagged (Then_Expr) /=
9611 Is_Dynamically_Tagged (Else_Expr)
9612 then
9613 Error_Msg_N ("all or none of the dependent expressions "
9614 & "can be dynamically tagged", N);
9615 end if;
9616 end if;
9617
9618 -- If no ELSE expression is present, root type must be Standard.Boolean
9619 -- and we provide a Standard.True result converted to the appropriate
9620 -- Boolean type (in case it is a derived boolean type).
9621
9622 elsif Root_Type (Typ) = Standard_Boolean then
9623 Else_Expr :=
9624 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
9625 Analyze_And_Resolve (Else_Expr, Result_Type);
9626 Append_To (Expressions (N), Else_Expr);
9627
9628 else
9629 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
9630 Append_To (Expressions (N), Error);
9631 end if;
9632
9633 Set_Etype (N, Result_Type);
9634
9635 if not Error_Posted (N) then
9636 Eval_If_Expression (N);
9637 end if;
9638
9639 Analyze_Dimension (N);
9640 end Resolve_If_Expression;
9641
9642 ----------------------------------
9643 -- Resolve_Implicit_Dereference --
9644 ----------------------------------
9645
9646 procedure Resolve_Implicit_Dereference (P : Node_Id) is
9647 Desig_Typ : Entity_Id;
9648
9649 begin
9650 if Is_Access_Type (Etype (P)) then
9651 Desig_Typ := Implicitly_Designated_Type (Etype (P));
9652 Insert_Explicit_Dereference (P);
9653 Analyze_And_Resolve (P, Desig_Typ);
9654 end if;
9655 end Resolve_Implicit_Dereference;
9656
9657 -------------------------------
9658 -- Resolve_Indexed_Component --
9659 -------------------------------
9660
9661 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
9662 Pref : constant Node_Id := Prefix (N);
9663 Expr : Node_Id;
9664 Array_Type : Entity_Id := Empty; -- to prevent junk warning
9665 Index : Node_Id;
9666
9667 begin
9668 if Present (Generalized_Indexing (N)) then
9669 Resolve_Generalized_Indexing (N, Typ);
9670 return;
9671 end if;
9672
9673 if Is_Overloaded (Pref) then
9674
9675 -- Use the context type to select the prefix that yields the correct
9676 -- component type.
9677
9678 declare
9679 I : Interp_Index;
9680 It : Interp;
9681 I1 : Interp_Index := 0;
9682 Found : Boolean := False;
9683
9684 begin
9685 Get_First_Interp (Pref, I, It);
9686 while Present (It.Typ) loop
9687 if (Is_Array_Type (It.Typ)
9688 and then Covers (Typ, Component_Type (It.Typ)))
9689 or else (Is_Access_Type (It.Typ)
9690 and then Is_Array_Type (Designated_Type (It.Typ))
9691 and then
9692 Covers
9693 (Typ,
9694 Component_Type (Designated_Type (It.Typ))))
9695 then
9696 if Found then
9697 It := Disambiguate (Pref, I1, I, Any_Type);
9698
9699 if It = No_Interp then
9700 Error_Msg_N ("ambiguous prefix for indexing", N);
9701 Set_Etype (N, Typ);
9702 return;
9703
9704 else
9705 Found := True;
9706 Array_Type := It.Typ;
9707 I1 := I;
9708 end if;
9709
9710 else
9711 Found := True;
9712 Array_Type := It.Typ;
9713 I1 := I;
9714 end if;
9715 end if;
9716
9717 Get_Next_Interp (I, It);
9718 end loop;
9719 end;
9720
9721 else
9722 Array_Type := Etype (Pref);
9723 end if;
9724
9725 Resolve (Pref, Array_Type);
9726 Array_Type := Get_Actual_Subtype_If_Available (Pref);
9727
9728 -- If the prefix's type is an access type, get to the real array type.
9729 -- Note: we do not apply an access check because an explicit dereference
9730 -- will be introduced later, and the check will happen there.
9731
9732 if Is_Access_Type (Array_Type) then
9733 Array_Type := Implicitly_Designated_Type (Array_Type);
9734 end if;
9735
9736 -- If name was overloaded, set component type correctly now.
9737 -- If a misplaced call to an entry family (which has no index types)
9738 -- return. Error will be diagnosed from calling context.
9739
9740 if Is_Array_Type (Array_Type) then
9741 Set_Etype (N, Component_Type (Array_Type));
9742 else
9743 return;
9744 end if;
9745
9746 Index := First_Index (Array_Type);
9747 Expr := First (Expressions (N));
9748
9749 -- The prefix may have resolved to a string literal, in which case its
9750 -- etype has a special representation. This is only possible currently
9751 -- if the prefix is a static concatenation, written in functional
9752 -- notation.
9753
9754 if Ekind (Array_Type) = E_String_Literal_Subtype then
9755 Resolve (Expr, Standard_Positive);
9756
9757 else
9758 while Present (Index) and then Present (Expr) loop
9759 Resolve (Expr, Etype (Index));
9760 Check_Unset_Reference (Expr);
9761
9762 if not Kill_Range_Check (N) then
9763 Apply_Scalar_Range_Check (Expr, Etype (Index));
9764 end if;
9765
9766 Next_Index (Index);
9767 Next (Expr);
9768 end loop;
9769 end if;
9770
9771 Resolve_Implicit_Dereference (Pref);
9772 Analyze_Dimension (N);
9773
9774 -- Do not generate the warning on suspicious index if we are analyzing
9775 -- package Ada.Tags; otherwise we will report the warning with the
9776 -- Prims_Ptr field of the dispatch table.
9777
9778 if Scope (Etype (Pref)) = Standard_Standard
9779 or else not
9780 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Pref))), Ada_Tags)
9781 then
9782 Warn_On_Suspicious_Index (Pref, First (Expressions (N)));
9783 Eval_Indexed_Component (N);
9784 end if;
9785
9786 -- If the array type is atomic and the component is not, then this is
9787 -- worth a warning before Ada 2022, since we have a situation where the
9788 -- access to the component may cause extra read/writes of the atomic
9789 -- object, or partial word accesses, both of which may be unexpected.
9790
9791 if Nkind (N) = N_Indexed_Component
9792 and then Is_Atomic_Non_VFA_Ref_With_Address (N)
9793 and then not (Has_Atomic_Components (Array_Type)
9794 or else (Is_Entity_Name (Pref)
9795 and then Has_Atomic_Components
9796 (Entity (Pref))))
9797 and then not Is_Atomic (Component_Type (Array_Type))
9798 and then Ada_Version < Ada_2022
9799 then
9800 Error_Msg_N
9801 ("??access to non-atomic component of atomic array", Pref);
9802 Error_Msg_N
9803 ("??\may cause unexpected accesses to atomic object", Pref);
9804 end if;
9805 end Resolve_Indexed_Component;
9806
9807 -----------------------------
9808 -- Resolve_Integer_Literal --
9809 -----------------------------
9810
9811 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
9812 begin
9813 Set_Etype (N, Typ);
9814 Eval_Integer_Literal (N);
9815 end Resolve_Integer_Literal;
9816
9817 -----------------------------------------
9818 -- Resolve_Interpolated_String_Literal --
9819 -----------------------------------------
9820
9821 procedure Resolve_Interpolated_String_Literal (N : Node_Id; Typ : Entity_Id)
9822 is
9823 Str_Elem : Node_Id;
9824
9825 begin
9826 Str_Elem := First (Expressions (N));
9827 pragma Assert (Nkind (Str_Elem) = N_String_Literal);
9828
9829 while Present (Str_Elem) loop
9830
9831 -- Resolve string elements using the context type; for interpolated
9832 -- expressions there is no need to check if their type has a suitable
9833 -- image function because under Ada 2022 all the types have such
9834 -- function available.
9835
9836 if Nkind (Str_Elem) = N_String_Literal
9837 and then Is_Interpolated_String_Literal (Str_Elem)
9838 then
9839 Resolve (Str_Elem, Typ);
9840
9841 -- Must have been rejected during analysis
9842
9843 elsif Nkind (Str_Elem) in N_Character_Literal
9844 | N_Integer_Literal
9845 | N_Real_Literal
9846 | N_String_Literal
9847 then
9848 pragma Assert (Error_Posted (Str_Elem));
9849 end if;
9850
9851 Next (Str_Elem);
9852 end loop;
9853
9854 Set_Etype (N, Typ);
9855 end Resolve_Interpolated_String_Literal;
9856
9857 --------------------------------
9858 -- Resolve_Intrinsic_Operator --
9859 --------------------------------
9860
9861 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
9862 Is_Stoele_Mod : constant Boolean :=
9863 Nkind (N) = N_Op_Mod
9864 and then Is_RTE (First_Subtype (Typ), RE_Storage_Offset)
9865 and then Is_RTE (Etype (Left_Opnd (N)), RE_Address);
9866 -- True if this is the special mod operator of System.Storage_Elements,
9867 -- which needs to be resolved to the type of the left operand in order
9868 -- to implement the correct semantics.
9869
9870 Btyp : constant Entity_Id :=
9871 (if Is_Stoele_Mod
9872 then Implementation_Base_Type (Etype (Left_Opnd (N)))
9873 else Implementation_Base_Type (Typ));
9874 -- The base type to be used for the operator
9875
9876 function Convert_Operand (Opnd : Node_Id) return Node_Id;
9877 -- If the operand is a literal, it cannot be the expression in a
9878 -- conversion. Use a qualified expression instead.
9879
9880 ---------------------
9881 -- Convert_Operand --
9882 ---------------------
9883
9884 function Convert_Operand (Opnd : Node_Id) return Node_Id is
9885 Loc : constant Source_Ptr := Sloc (Opnd);
9886 Res : Node_Id;
9887
9888 begin
9889 if Nkind (Opnd) in N_Integer_Literal | N_Real_Literal then
9890 Res :=
9891 Make_Qualified_Expression (Loc,
9892 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
9893 Expression => Relocate_Node (Opnd));
9894 Analyze (Res);
9895
9896 else
9897 Res := Unchecked_Convert_To (Btyp, Opnd);
9898 end if;
9899
9900 return Res;
9901 end Convert_Operand;
9902
9903 -- Local variables
9904
9905 Arg1 : Node_Id;
9906 Arg2 : Node_Id;
9907 Op : Entity_Id;
9908
9909 -- Start of processing for Resolve_Intrinsic_Operator
9910
9911 begin
9912 -- We must preserve the original entity in a generic setting, so that
9913 -- the legality of the operation can be verified in an instance.
9914
9915 if not Expander_Active then
9916 return;
9917 end if;
9918
9919 case N_Binary_Op'(Nkind (N)) is
9920 when N_Op_Add =>
9921 Op := Standard_Op_Add;
9922 when N_Op_Expon =>
9923 Op := Standard_Op_Expon;
9924 when N_Op_Subtract =>
9925 Op := Standard_Op_Subtract;
9926 when N_Op_Divide =>
9927 Op := Standard_Op_Divide;
9928 when N_Op_Mod =>
9929 Op := Standard_Op_Mod;
9930 when N_Op_Multiply =>
9931 Op := Standard_Op_Multiply;
9932 when N_Op_Rem =>
9933 Op := Standard_Op_Rem;
9934
9935 -- Non-arithmetic operators are handled elsewhere
9936
9937 when N_Op_Boolean
9938 | N_Op_Concat
9939 | N_Op_Shift
9940 =>
9941 raise Program_Error;
9942 end case;
9943
9944 Set_Entity (N, Op);
9945 Set_Is_Overloaded (N, False);
9946
9947 -- If the result or operand types are private, rewrite with unchecked
9948 -- conversions on the operands and the result, to expose the proper
9949 -- underlying numeric type. Likewise for the special mod operator of
9950 -- System.Storage_Elements, to expose the modified base type.
9951
9952 if Is_Private_Type (Typ)
9953 or else Is_Private_Type (Etype (Left_Opnd (N)))
9954 or else Is_Private_Type (Etype (Right_Opnd (N)))
9955 or else Is_Stoele_Mod
9956 then
9957 Arg1 := Convert_Operand (Left_Opnd (N));
9958
9959 if Nkind (N) = N_Op_Expon then
9960 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
9961 else
9962 Arg2 := Convert_Operand (Right_Opnd (N));
9963 end if;
9964
9965 if Nkind (Arg1) = N_Type_Conversion then
9966 Save_Interps (Left_Opnd (N), Expression (Arg1));
9967 end if;
9968
9969 if Nkind (Arg2) = N_Type_Conversion then
9970 Save_Interps (Right_Opnd (N), Expression (Arg2));
9971 end if;
9972
9973 Set_Left_Opnd (N, Arg1);
9974 Set_Right_Opnd (N, Arg2);
9975
9976 Set_Etype (N, Btyp);
9977 Rewrite (N, Unchecked_Convert_To (Typ, N));
9978 Resolve (N, Typ);
9979
9980 elsif Typ /= Etype (Left_Opnd (N))
9981 or else Typ /= Etype (Right_Opnd (N))
9982 then
9983 -- Add explicit conversion where needed, and save interpretations in
9984 -- case operands are overloaded.
9985
9986 Arg1 := Convert_To (Typ, Left_Opnd (N));
9987 Arg2 := Convert_To (Typ, Right_Opnd (N));
9988
9989 if Nkind (Arg1) = N_Type_Conversion then
9990 Save_Interps (Left_Opnd (N), Expression (Arg1));
9991 else
9992 Save_Interps (Left_Opnd (N), Arg1);
9993 end if;
9994
9995 if Nkind (Arg2) = N_Type_Conversion then
9996 Save_Interps (Right_Opnd (N), Expression (Arg2));
9997 else
9998 Save_Interps (Right_Opnd (N), Arg2);
9999 end if;
10000
10001 Rewrite (Left_Opnd (N), Arg1);
10002 Rewrite (Right_Opnd (N), Arg2);
10003 Analyze (Arg1);
10004 Analyze (Arg2);
10005 Resolve_Arithmetic_Op (N, Typ);
10006
10007 else
10008 Resolve_Arithmetic_Op (N, Typ);
10009 end if;
10010 end Resolve_Intrinsic_Operator;
10011
10012 --------------------------------------
10013 -- Resolve_Intrinsic_Unary_Operator --
10014 --------------------------------------
10015
10016 procedure Resolve_Intrinsic_Unary_Operator
10017 (N : Node_Id;
10018 Typ : Entity_Id)
10019 is
10020 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
10021 Op : Entity_Id;
10022 Arg2 : Node_Id;
10023
10024 begin
10025 -- We must preserve the original entity in a generic setting, so that
10026 -- the legality of the operation can be verified in an instance.
10027
10028 if not Expander_Active then
10029 return;
10030 end if;
10031
10032 case N_Unary_Op'(Nkind (N)) is
10033 when N_Op_Abs =>
10034 Op := Standard_Op_Abs;
10035 when N_Op_Minus =>
10036 Op := Standard_Op_Minus;
10037 when N_Op_Plus =>
10038 Op := Standard_Op_Plus;
10039
10040 -- Non-arithmetic operators are handled elsewhere
10041
10042 when N_Op_Not =>
10043 raise Program_Error;
10044 end case;
10045
10046 Set_Entity (N, Op);
10047
10048 if Is_Private_Type (Typ) then
10049 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
10050 Save_Interps (Right_Opnd (N), Expression (Arg2));
10051
10052 Set_Right_Opnd (N, Arg2);
10053
10054 Set_Etype (N, Btyp);
10055 Rewrite (N, Unchecked_Convert_To (Typ, N));
10056 Resolve (N, Typ);
10057
10058 else
10059 Resolve_Unary_Op (N, Typ);
10060 end if;
10061 end Resolve_Intrinsic_Unary_Operator;
10062
10063 ------------------------
10064 -- Resolve_Logical_Op --
10065 ------------------------
10066
10067 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
10068 B_Typ : Entity_Id;
10069
10070 begin
10071 Check_No_Direct_Boolean_Operators (N);
10072
10073 -- Predefined operations on scalar types yield the base type. On the
10074 -- other hand, logical operations on arrays yield the type of the
10075 -- arguments (and the context).
10076
10077 if Is_Array_Type (Typ) then
10078 B_Typ := Typ;
10079 else
10080 B_Typ := Base_Type (Typ);
10081 end if;
10082
10083 -- The following test is required because the operands of the operation
10084 -- may be literals, in which case the resulting type appears to be
10085 -- compatible with a signed integer type, when in fact it is compatible
10086 -- only with modular types. If the context itself is universal, the
10087 -- operation is illegal.
10088
10089 if not Valid_Boolean_Arg (Typ) then
10090 Error_Msg_N ("invalid context for logical operation", N);
10091 Set_Etype (N, Any_Type);
10092 return;
10093
10094 elsif Typ = Any_Modular then
10095 Error_Msg_N
10096 ("no modular type available in this context", N);
10097 Set_Etype (N, Any_Type);
10098 return;
10099
10100 elsif Is_Modular_Integer_Type (Typ)
10101 and then Etype (Left_Opnd (N)) = Universal_Integer
10102 and then Etype (Right_Opnd (N)) = Universal_Integer
10103 then
10104 Check_For_Visible_Operator (N, B_Typ);
10105 end if;
10106
10107 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
10108 -- is active and the result type is standard Boolean (do not mess with
10109 -- ops that return a nonstandard Boolean type, because something strange
10110 -- is going on).
10111
10112 -- Note: you might expect this replacement to be done during expansion,
10113 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
10114 -- is used, no part of the right operand of an "and" or "or" operator
10115 -- should be executed if the left operand would short-circuit the
10116 -- evaluation of the corresponding "and then" or "or else". If we left
10117 -- the replacement to expansion time, then run-time checks associated
10118 -- with such operands would be evaluated unconditionally, due to being
10119 -- before the condition prior to the rewriting as short-circuit forms
10120 -- during expansion.
10121
10122 if Short_Circuit_And_Or
10123 and then B_Typ = Standard_Boolean
10124 and then Nkind (N) in N_Op_And | N_Op_Or
10125 then
10126 -- Mark the corresponding putative SCO operator as truly a logical
10127 -- (and short-circuit) operator.
10128
10129 if Generate_SCO and then Comes_From_Source (N) then
10130 Set_SCO_Logical_Operator (N);
10131 end if;
10132
10133 if Nkind (N) = N_Op_And then
10134 Rewrite (N,
10135 Make_And_Then (Sloc (N),
10136 Left_Opnd => Relocate_Node (Left_Opnd (N)),
10137 Right_Opnd => Relocate_Node (Right_Opnd (N))));
10138 Analyze_And_Resolve (N, B_Typ);
10139
10140 -- Case of OR changed to OR ELSE
10141
10142 else
10143 Rewrite (N,
10144 Make_Or_Else (Sloc (N),
10145 Left_Opnd => Relocate_Node (Left_Opnd (N)),
10146 Right_Opnd => Relocate_Node (Right_Opnd (N))));
10147 Analyze_And_Resolve (N, B_Typ);
10148 end if;
10149
10150 -- Return now, since analysis of the rewritten ops will take care of
10151 -- other reference bookkeeping and expression folding.
10152
10153 return;
10154 end if;
10155
10156 Resolve (Left_Opnd (N), B_Typ);
10157 Resolve (Right_Opnd (N), B_Typ);
10158
10159 Check_Unset_Reference (Left_Opnd (N));
10160 Check_Unset_Reference (Right_Opnd (N));
10161
10162 Set_Etype (N, B_Typ);
10163 Generate_Operator_Reference (N, B_Typ);
10164 Eval_Logical_Op (N);
10165 end Resolve_Logical_Op;
10166
10167 ---------------------------------
10168 -- Resolve_Membership_Equality --
10169 ---------------------------------
10170
10171 procedure Resolve_Membership_Equality (N : Node_Id; Typ : Entity_Id) is
10172 Utyp : constant Entity_Id := Underlying_Type (Typ);
10173
10174 begin
10175 -- RM 4.5.2(4.1/3): if the type is limited, then it shall have a visible
10176 -- primitive equality operator. This means that we can use the regular
10177 -- visibility-based resolution and reset Entity in order to trigger it.
10178
10179 if Is_Limited_Type (Typ) then
10180 Set_Entity (N, Empty);
10181
10182 -- RM 4.5.2(28.1/3): if the type is a record, then the membership test
10183 -- uses the primitive equality for the type [even if it is not visible].
10184 -- We only deal with the untagged case here, because the tagged case is
10185 -- handled uniformly in the expander.
10186
10187 elsif Is_Record_Type (Utyp) and then not Is_Tagged_Type (Utyp) then
10188 declare
10189 Eq_Id : constant Entity_Id := Get_User_Defined_Equality (Typ);
10190
10191 begin
10192 if Present (Eq_Id) then
10193 Rewrite_Operator_As_Call (N, Eq_Id);
10194 end if;
10195 end;
10196 end if;
10197 end Resolve_Membership_Equality;
10198
10199 ---------------------------
10200 -- Resolve_Membership_Op --
10201 ---------------------------
10202
10203 -- The context can only be a boolean type, and does not determine the
10204 -- arguments. Arguments should be unambiguous, but the preference rule for
10205 -- universal types applies.
10206
10207 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
10208 pragma Assert (Is_Boolean_Type (Typ));
10209
10210 L : constant Node_Id := Left_Opnd (N);
10211 R : constant Node_Id := Right_Opnd (N);
10212 T : Entity_Id;
10213
10214 procedure Resolve_Set_Membership;
10215 -- Analysis has determined a unique type for the left operand. Use it as
10216 -- the basis to resolve the disjuncts.
10217
10218 ----------------------------
10219 -- Resolve_Set_Membership --
10220 ----------------------------
10221
10222 procedure Resolve_Set_Membership is
10223 Alt : Node_Id;
10224
10225 begin
10226 -- If the left operand is overloaded, find type compatible with not
10227 -- overloaded alternative of the right operand.
10228
10229 Alt := First (Alternatives (N));
10230 if Is_Overloaded (L) then
10231 T := Empty;
10232 while Present (Alt) loop
10233 if not Is_Overloaded (Alt) then
10234 T := Intersect_Types (L, Alt);
10235 exit;
10236 else
10237 Next (Alt);
10238 end if;
10239 end loop;
10240
10241 -- Unclear how to resolve expression if all alternatives are also
10242 -- overloaded.
10243
10244 if No (T) then
10245 Error_Msg_N ("ambiguous expression", N);
10246 end if;
10247
10248 else
10249 T := Intersect_Types (L, Alt);
10250 end if;
10251
10252 Resolve (L, T);
10253
10254 Alt := First (Alternatives (N));
10255 while Present (Alt) loop
10256
10257 -- Alternative is an expression, a range
10258 -- or a subtype mark.
10259
10260 if not Is_Entity_Name (Alt)
10261 or else not Is_Type (Entity (Alt))
10262 then
10263 Resolve (Alt, T);
10264 end if;
10265
10266 Next (Alt);
10267 end loop;
10268
10269 -- Check for duplicates for discrete case
10270
10271 if Is_Discrete_Type (T) then
10272 declare
10273 type Ent is record
10274 Alt : Node_Id;
10275 Val : Uint;
10276 end record;
10277
10278 Alts : array (0 .. List_Length (Alternatives (N))) of Ent;
10279 Nalts : Nat;
10280
10281 begin
10282 -- Loop checking duplicates. This is quadratic, but giant sets
10283 -- are unlikely in this context so it's a reasonable choice.
10284
10285 Nalts := 0;
10286 Alt := First (Alternatives (N));
10287 while Present (Alt) loop
10288 if Is_OK_Static_Expression (Alt)
10289 and then Nkind (Alt) in N_Integer_Literal
10290 | N_Character_Literal
10291 | N_Has_Entity
10292 then
10293 Nalts := Nalts + 1;
10294 Alts (Nalts) := (Alt, Expr_Value (Alt));
10295
10296 for J in 1 .. Nalts - 1 loop
10297 if Alts (J).Val = Alts (Nalts).Val then
10298 Error_Msg_Sloc := Sloc (Alts (J).Alt);
10299 Error_Msg_N ("duplicate of value given#??", Alt);
10300 end if;
10301 end loop;
10302 end if;
10303
10304 Next (Alt);
10305 end loop;
10306 end;
10307 end if;
10308
10309 -- RM 4.5.2 (28.1/3) specifies that for types other than records or
10310 -- limited types, evaluation of a membership test uses the predefined
10311 -- equality for the type. This may be confusing to users, and the
10312 -- following warning appears useful for the most common case.
10313
10314 if Is_Scalar_Type (Etype (L))
10315 and then Present (Get_User_Defined_Equality (Etype (L)))
10316 then
10317 Error_Msg_NE
10318 ("membership test on& uses predefined equality?", N, Etype (L));
10319 Error_Msg_N
10320 ("\even if user-defined equality exists (RM 4.5.2 (28.1/3)?", N);
10321 end if;
10322 end Resolve_Set_Membership;
10323
10324 -- Start of processing for Resolve_Membership_Op
10325
10326 begin
10327 if L = Error or else R = Error then
10328 return;
10329 end if;
10330
10331 if Present (Alternatives (N)) then
10332 Resolve_Set_Membership;
10333 goto SM_Exit;
10334
10335 elsif not Is_Overloaded (R)
10336 and then Is_Universal_Numeric_Type (Etype (R))
10337 and then Is_Overloaded (L)
10338 then
10339 T := Etype (R);
10340
10341 -- If the left operand is of a universal numeric type and the right
10342 -- operand is not, we do not resolve the operands to the tested type
10343 -- but to the universal type instead. If not conforming to the letter,
10344 -- it's conforming to the spirit of the specification of membership
10345 -- tests, which are typically used to guard a specific operation and
10346 -- ought not to fail a check in doing so. Without this, in the case of
10347
10348 -- type Small_Length is range 1 .. 16;
10349
10350 -- function Is_Small_String (S : String) return Boolean is
10351 -- begin
10352 -- return S'Length in Small_Length;
10353 -- end;
10354
10355 -- the function Is_Small_String would fail a range check for strings
10356 -- larger than 127 characters.
10357
10358 -- The test on the size is required in GNAT because universal_integer
10359 -- does not cover all the values of all the supported integer types,
10360 -- for example the large values of Long_Long_Long_Unsigned.
10361
10362 elsif not Is_Overloaded (L)
10363 and then Is_Universal_Numeric_Type (Etype (L))
10364 and then (Is_Overloaded (R)
10365 or else
10366 (not Is_Universal_Numeric_Type (Etype (R))
10367 and then
10368 (not Is_Integer_Type (Etype (R))
10369 or else
10370 RM_Size (Etype (R)) < RM_Size (Universal_Integer))))
10371 then
10372 T := Etype (L);
10373
10374 -- If the right operand is 'Range, we first need to resolve it (to
10375 -- the tested type) so that it is rewritten as an N_Range, before
10376 -- converting its bounds and resolving it again below.
10377
10378 if Nkind (R) = N_Attribute_Reference
10379 and then Attribute_Name (R) = Name_Range
10380 then
10381 Resolve (R);
10382 end if;
10383
10384 -- If the right operand is an N_Range, we convert its bounds to the
10385 -- universal type before resolving it.
10386
10387 if Nkind (R) = N_Range then
10388 Rewrite (R,
10389 Make_Range (Sloc (R),
10390 Low_Bound => Convert_To (T, Low_Bound (R)),
10391 High_Bound => Convert_To (T, High_Bound (R))));
10392 Analyze (R);
10393 end if;
10394
10395 -- Ada 2005 (AI-251): Support the following case:
10396
10397 -- type I is interface;
10398 -- type T is tagged ...
10399
10400 -- function Test (O : I'Class) is
10401 -- begin
10402 -- return O in T'Class.
10403 -- end Test;
10404
10405 -- In this case we have nothing else to do. The membership test will be
10406 -- done at run time.
10407
10408 elsif Ada_Version >= Ada_2005
10409 and then Is_Class_Wide_Type (Etype (L))
10410 and then Is_Interface (Etype (L))
10411 and then not Is_Interface (Etype (R))
10412 then
10413 return;
10414
10415 else
10416 T := Intersect_Types (L, R);
10417 end if;
10418
10419 -- If mixed-mode operations are present and operands are all literal,
10420 -- the only interpretation involves Duration, which is probably not
10421 -- the intention of the programmer.
10422
10423 if T = Any_Fixed then
10424 T := Unique_Fixed_Point_Type (N);
10425
10426 if T = Any_Type then
10427 return;
10428 end if;
10429 end if;
10430
10431 Resolve (L, T);
10432 Check_Unset_Reference (L);
10433
10434 if Nkind (R) = N_Range
10435 and then not Is_Scalar_Type (T)
10436 then
10437 Error_Msg_N ("scalar type required for range", R);
10438 end if;
10439
10440 if Is_Entity_Name (R) then
10441 Freeze_Expression (R);
10442 else
10443 Resolve (R, T);
10444 Check_Unset_Reference (R);
10445 end if;
10446
10447 -- Here after resolving membership operation
10448
10449 <<SM_Exit>>
10450
10451 Eval_Membership_Op (N);
10452 end Resolve_Membership_Op;
10453
10454 ------------------
10455 -- Resolve_Null --
10456 ------------------
10457
10458 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
10459 Loc : constant Source_Ptr := Sloc (N);
10460
10461 begin
10462 -- Handle restriction against anonymous null access values This
10463 -- restriction can be turned off using -gnatdj.
10464
10465 -- Ada 2005 (AI-231): Remove restriction
10466
10467 if Ada_Version < Ada_2005
10468 and then not Debug_Flag_J
10469 and then Ekind (Typ) = E_Anonymous_Access_Type
10470 and then Comes_From_Source (N)
10471 then
10472 -- In the common case of a call which uses an explicitly null value
10473 -- for an access parameter, give specialized error message.
10474
10475 if Nkind (Parent (N)) in N_Subprogram_Call then
10476 Error_Msg_N
10477 ("NULL is not allowed as argument for an access parameter", N);
10478
10479 -- Standard message for all other cases (are there any?)
10480
10481 else
10482 Error_Msg_N
10483 ("NULL cannot be of an anonymous access type", N);
10484 end if;
10485 end if;
10486
10487 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
10488 -- assignment to a null-excluding object.
10489
10490 if Ada_Version >= Ada_2005
10491 and then Can_Never_Be_Null (Typ)
10492 and then Nkind (Parent (N)) = N_Assignment_Statement
10493 then
10494 if Inside_Init_Proc then
10495
10496 -- Decide whether to generate an if_statement around our
10497 -- null-excluding check to avoid them on certain internal object
10498 -- declarations by looking at the type the current Init_Proc
10499 -- belongs to.
10500
10501 -- Generate:
10502 -- if T1b_skip_null_excluding_check then
10503 -- [constraint_error "access check failed"]
10504 -- end if;
10505
10506 if Needs_Conditional_Null_Excluding_Check
10507 (Etype (First_Formal (Enclosing_Init_Proc)))
10508 then
10509 Insert_Action (N,
10510 Make_If_Statement (Loc,
10511 Condition =>
10512 Make_Identifier (Loc,
10513 New_External_Name
10514 (Chars (Typ), "_skip_null_excluding_check")),
10515 Then_Statements =>
10516 New_List (
10517 Make_Raise_Constraint_Error (Loc,
10518 Reason => CE_Access_Check_Failed))));
10519
10520 -- Otherwise, simply create the check
10521
10522 else
10523 Insert_Action (N,
10524 Make_Raise_Constraint_Error (Loc,
10525 Reason => CE_Access_Check_Failed));
10526 end if;
10527 else
10528 Insert_Action
10529 (Compile_Time_Constraint_Error (N,
10530 "(Ada 2005) NULL not allowed in null-excluding objects??"),
10531 Make_Raise_Constraint_Error (Loc,
10532 Reason => CE_Access_Check_Failed));
10533 end if;
10534 end if;
10535
10536 -- In a distributed context, null for a remote access to subprogram may
10537 -- need to be replaced with a special record aggregate. In this case,
10538 -- return after having done the transformation.
10539
10540 if (Ekind (Typ) = E_Record_Type
10541 or else Is_Remote_Access_To_Subprogram_Type (Typ))
10542 and then Remote_AST_Null_Value (N, Typ)
10543 then
10544 return;
10545 end if;
10546
10547 -- The null literal takes its type from the context
10548
10549 Set_Etype (N, Typ);
10550 end Resolve_Null;
10551
10552 -----------------------
10553 -- Resolve_Op_Concat --
10554 -----------------------
10555
10556 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
10557
10558 -- We wish to avoid deep recursion, because concatenations are often
10559 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
10560 -- operands nonrecursively until we find something that is not a simple
10561 -- concatenation (A in this case). We resolve that, and then walk back
10562 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
10563 -- to do the rest of the work at each level. The Parent pointers allow
10564 -- us to avoid recursion, and thus avoid running out of memory. See also
10565 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
10566
10567 NN : Node_Id := N;
10568 Op1 : Node_Id;
10569
10570 begin
10571 -- The following code is equivalent to:
10572
10573 -- Resolve_Op_Concat_First (NN, Typ);
10574 -- Resolve_Op_Concat_Arg (N, ...);
10575 -- Resolve_Op_Concat_Rest (N, Typ);
10576
10577 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
10578 -- operand is a concatenation.
10579
10580 -- Walk down left operands
10581
10582 loop
10583 Resolve_Op_Concat_First (NN, Typ);
10584 Op1 := Left_Opnd (NN);
10585 exit when not (Nkind (Op1) = N_Op_Concat
10586 and then not Is_Array_Type (Component_Type (Typ))
10587 and then Entity (Op1) = Entity (NN));
10588 NN := Op1;
10589 end loop;
10590
10591 -- Now (given the above example) NN is A&B and Op1 is A
10592
10593 -- First resolve Op1 ...
10594
10595 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
10596
10597 -- ... then walk NN back up until we reach N (where we started), calling
10598 -- Resolve_Op_Concat_Rest along the way.
10599
10600 loop
10601 Resolve_Op_Concat_Rest (NN, Typ);
10602 exit when NN = N;
10603 NN := Parent (NN);
10604 end loop;
10605 end Resolve_Op_Concat;
10606
10607 ---------------------------
10608 -- Resolve_Op_Concat_Arg --
10609 ---------------------------
10610
10611 procedure Resolve_Op_Concat_Arg
10612 (N : Node_Id;
10613 Arg : Node_Id;
10614 Typ : Entity_Id;
10615 Is_Comp : Boolean)
10616 is
10617 Btyp : constant Entity_Id := Base_Type (Typ);
10618 Ctyp : constant Entity_Id := Component_Type (Typ);
10619
10620 begin
10621 if In_Instance then
10622 if Is_Comp
10623 or else (not Is_Overloaded (Arg)
10624 and then Etype (Arg) /= Any_Composite
10625 and then Covers (Ctyp, Etype (Arg)))
10626 then
10627 Resolve (Arg, Ctyp);
10628 else
10629 Resolve (Arg, Btyp);
10630 end if;
10631
10632 -- If both Array & Array and Array & Component are visible, there is a
10633 -- potential ambiguity that must be reported.
10634
10635 elsif Has_Compatible_Type (Arg, Ctyp) then
10636 if Nkind (Arg) = N_Aggregate
10637 and then Is_Composite_Type (Ctyp)
10638 then
10639 if Is_Private_Type (Ctyp) then
10640 Resolve (Arg, Btyp);
10641
10642 -- If the operation is user-defined and not overloaded use its
10643 -- profile. The operation may be a renaming, in which case it has
10644 -- been rewritten, and we want the original profile.
10645
10646 elsif not Is_Overloaded (N)
10647 and then Comes_From_Source (Entity (Original_Node (N)))
10648 and then Ekind (Entity (Original_Node (N))) = E_Function
10649 then
10650 Resolve (Arg,
10651 Etype
10652 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
10653 return;
10654
10655 -- Otherwise an aggregate may match both the array type and the
10656 -- component type.
10657
10658 else
10659 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
10660 Set_Etype (Arg, Any_Type);
10661 end if;
10662
10663 else
10664 if Is_Overloaded (Arg)
10665 and then Has_Compatible_Type (Arg, Typ)
10666 and then Etype (Arg) /= Any_Type
10667 then
10668 declare
10669 I : Interp_Index;
10670 It : Interp;
10671 Func : Entity_Id;
10672
10673 begin
10674 Get_First_Interp (Arg, I, It);
10675 Func := It.Nam;
10676 Get_Next_Interp (I, It);
10677
10678 -- Special-case the error message when the overloading is
10679 -- caused by a function that yields an array and can be
10680 -- called without parameters.
10681
10682 if It.Nam = Func then
10683 Error_Msg_Sloc := Sloc (Func);
10684 Error_Msg_N ("ambiguous call to function#", Arg);
10685 Error_Msg_NE
10686 ("\\interpretation as call yields&", Arg, Typ);
10687 Error_Msg_NE
10688 ("\\interpretation as indexing of call yields&",
10689 Arg, Ctyp);
10690
10691 else
10692 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
10693
10694 Get_First_Interp (Arg, I, It);
10695 while Present (It.Nam) loop
10696 Error_Msg_Sloc := Sloc (It.Nam);
10697
10698 if Base_Type (It.Typ) = Btyp
10699 or else
10700 Base_Type (It.Typ) = Base_Type (Ctyp)
10701 then
10702 Error_Msg_N -- CODEFIX
10703 ("\\possible interpretation#", Arg);
10704 end if;
10705
10706 Get_Next_Interp (I, It);
10707 end loop;
10708 end if;
10709 end;
10710 end if;
10711
10712 Resolve (Arg, Ctyp);
10713
10714 if Nkind (Arg) = N_String_Literal then
10715 Set_Etype (Arg, Ctyp);
10716
10717 elsif Is_Scalar_Type (Etype (Arg))
10718 and then Compile_Time_Known_Value (Arg)
10719 then
10720 -- Determine if the out-of-range violation constitutes a
10721 -- warning or an error according to the expression base type,
10722 -- according to Ada 2022 RM 4.9 (35/2).
10723
10724 if Is_Out_Of_Range (Arg, Base_Type (Ctyp)) then
10725 Apply_Compile_Time_Constraint_Error
10726 (Arg, "value not in range of}", CE_Range_Check_Failed,
10727 Ent => Base_Type (Ctyp),
10728 Typ => Base_Type (Ctyp));
10729
10730 elsif Is_Out_Of_Range (Arg, Ctyp) then
10731 Apply_Compile_Time_Constraint_Error
10732 (Arg, "value not in range of}??", CE_Range_Check_Failed,
10733 Ent => Ctyp,
10734 Typ => Ctyp);
10735 end if;
10736 end if;
10737
10738 if Arg = Left_Opnd (N) then
10739 Set_Is_Component_Left_Opnd (N);
10740 else
10741 Set_Is_Component_Right_Opnd (N);
10742 end if;
10743 end if;
10744
10745 else
10746 Resolve (Arg, Btyp);
10747 end if;
10748
10749 Check_Unset_Reference (Arg);
10750 end Resolve_Op_Concat_Arg;
10751
10752 -----------------------------
10753 -- Resolve_Op_Concat_First --
10754 -----------------------------
10755
10756 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
10757 Btyp : constant Entity_Id := Base_Type (Typ);
10758 Op1 : constant Node_Id := Left_Opnd (N);
10759 Op2 : constant Node_Id := Right_Opnd (N);
10760
10761 begin
10762 -- The parser folds an enormous sequence of concatenations of string
10763 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
10764 -- in the right operand. If the expression resolves to a predefined "&"
10765 -- operator, all is well. Otherwise, the parser's folding is wrong, so
10766 -- we give an error. See P_Simple_Expression in Par.Ch4.
10767
10768 if Nkind (Op2) = N_String_Literal
10769 and then Is_Folded_In_Parser (Op2)
10770 and then Ekind (Entity (N)) = E_Function
10771 then
10772 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
10773 and then String_Length (Strval (Op1)) = 0);
10774 Error_Msg_N ("too many user-defined concatenations", N);
10775 return;
10776 end if;
10777
10778 Set_Etype (N, Btyp);
10779
10780 if Is_Limited_Composite (Btyp) then
10781 Error_Msg_N ("concatenation not available for limited array", N);
10782 Explain_Limited_Type (Btyp, N);
10783 end if;
10784 end Resolve_Op_Concat_First;
10785
10786 ----------------------------
10787 -- Resolve_Op_Concat_Rest --
10788 ----------------------------
10789
10790 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
10791 Op1 : constant Node_Id := Left_Opnd (N);
10792 Op2 : constant Node_Id := Right_Opnd (N);
10793
10794 begin
10795 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
10796
10797 Generate_Operator_Reference (N, Typ);
10798
10799 if Is_String_Type (Typ) then
10800 Eval_Concatenation (N);
10801 end if;
10802
10803 -- If this is not a static concatenation, but the result is a string
10804 -- type (and not an array of strings) ensure that static string operands
10805 -- have their subtypes properly constructed.
10806
10807 if Nkind (N) /= N_String_Literal
10808 and then Is_Character_Type (Component_Type (Typ))
10809 then
10810 Set_String_Literal_Subtype (Op1, Typ);
10811 Set_String_Literal_Subtype (Op2, Typ);
10812 end if;
10813 end Resolve_Op_Concat_Rest;
10814
10815 ----------------------
10816 -- Resolve_Op_Expon --
10817 ----------------------
10818
10819 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
10820 B_Typ : constant Entity_Id := Base_Type (Typ);
10821
10822 begin
10823 -- Catch attempts to do fixed-point exponentiation with universal
10824 -- operands, which is a case where the illegality is not caught during
10825 -- normal operator analysis. This is not done in preanalysis mode
10826 -- since the tree is not fully decorated during preanalysis.
10827
10828 if Full_Analysis then
10829 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
10830 Error_Msg_N ("exponentiation not available for fixed point", N);
10831 return;
10832
10833 elsif Nkind (Parent (N)) in N_Op
10834 and then Present (Etype (Parent (N)))
10835 and then Is_Fixed_Point_Type (Etype (Parent (N)))
10836 and then Etype (N) = Universal_Real
10837 and then Comes_From_Source (N)
10838 then
10839 Error_Msg_N ("exponentiation not available for fixed point", N);
10840 return;
10841 end if;
10842 end if;
10843
10844 if Ekind (Entity (N)) = E_Function
10845 and then Is_Imported (Entity (N))
10846 and then Is_Intrinsic_Subprogram (Entity (N))
10847 then
10848 Generate_Reference (Entity (N), N);
10849 Resolve_Intrinsic_Operator (N, Typ);
10850 return;
10851 end if;
10852
10853 if Is_Universal_Numeric_Type (Etype (Left_Opnd (N))) then
10854 Check_For_Visible_Operator (N, B_Typ);
10855 end if;
10856
10857 -- We do the resolution using the base type, because intermediate values
10858 -- in expressions are always of the base type, not a subtype of it.
10859
10860 Resolve (Left_Opnd (N), B_Typ);
10861 Resolve (Right_Opnd (N), Standard_Integer);
10862
10863 -- For integer types, right argument must be in Natural range
10864
10865 if Is_Integer_Type (Typ) then
10866 Apply_Scalar_Range_Check (Right_Opnd (N), Standard_Natural);
10867 end if;
10868
10869 Check_Unset_Reference (Left_Opnd (N));
10870 Check_Unset_Reference (Right_Opnd (N));
10871
10872 Set_Etype (N, B_Typ);
10873 Generate_Operator_Reference (N, B_Typ);
10874
10875 Analyze_Dimension (N);
10876
10877 if Ada_Version >= Ada_2012 and then Has_Dimension_System (B_Typ) then
10878 -- Evaluate the exponentiation operator for dimensioned type
10879
10880 Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ);
10881 else
10882 Eval_Op_Expon (N);
10883 end if;
10884
10885 -- Set overflow checking bit. Much cleverer code needed here eventually
10886 -- and perhaps the Resolve routines should be separated for the various
10887 -- arithmetic operations, since they will need different processing. ???
10888
10889 if Nkind (N) in N_Op then
10890 if not Overflow_Checks_Suppressed (Etype (N)) then
10891 Enable_Overflow_Check (N);
10892 end if;
10893 end if;
10894 end Resolve_Op_Expon;
10895
10896 --------------------
10897 -- Resolve_Op_Not --
10898 --------------------
10899
10900 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
10901 function Parent_Is_Boolean return Boolean;
10902 -- This function determines if the parent node is a boolean operator or
10903 -- operation (comparison op, membership test, or short circuit form) and
10904 -- the not in question is the left operand of this operation. Note that
10905 -- if the not is in parens, then false is returned.
10906
10907 -----------------------
10908 -- Parent_Is_Boolean --
10909 -----------------------
10910
10911 function Parent_Is_Boolean return Boolean is
10912 begin
10913 return Paren_Count (N) = 0
10914 and then Nkind (Parent (N)) in N_Membership_Test
10915 | N_Op_Boolean
10916 | N_Short_Circuit
10917 and then Left_Opnd (Parent (N)) = N;
10918 end Parent_Is_Boolean;
10919
10920 -- Local variables
10921
10922 B_Typ : Entity_Id;
10923
10924 -- Start of processing for Resolve_Op_Not
10925
10926 begin
10927 -- Predefined operations on scalar types yield the base type. On the
10928 -- other hand, logical operations on arrays yield the type of the
10929 -- arguments (and the context).
10930
10931 if Is_Array_Type (Typ) then
10932 B_Typ := Typ;
10933 else
10934 B_Typ := Base_Type (Typ);
10935 end if;
10936
10937 -- Straightforward case of incorrect arguments
10938
10939 if not Valid_Boolean_Arg (Typ) then
10940 Error_Msg_N ("invalid operand type for operator&", N);
10941 Set_Etype (N, Any_Type);
10942 return;
10943
10944 -- Special case of probable missing parens
10945
10946 elsif Typ = Universal_Integer or else Typ = Any_Modular then
10947 if Parent_Is_Boolean then
10948 Error_Msg_N
10949 ("operand of NOT must be enclosed in parentheses",
10950 Right_Opnd (N));
10951 else
10952 Error_Msg_N
10953 ("no modular type available in this context", N);
10954 end if;
10955
10956 Set_Etype (N, Any_Type);
10957 return;
10958
10959 -- OK resolution of NOT
10960
10961 else
10962 -- Warn if non-boolean types involved. This is a case like not a < b
10963 -- where a and b are modular, where we will get (not a) < b and most
10964 -- likely not (a < b) was intended.
10965
10966 if Warn_On_Questionable_Missing_Parens
10967 and then not Is_Boolean_Type (Typ)
10968 and then Parent_Is_Boolean
10969 then
10970 Error_Msg_N ("?q?not expression should be parenthesized here!", N);
10971 end if;
10972
10973 -- Warn on double negation if checking redundant constructs
10974
10975 if Warn_On_Redundant_Constructs
10976 and then Comes_From_Source (N)
10977 and then Comes_From_Source (Right_Opnd (N))
10978 and then Root_Type (Typ) = Standard_Boolean
10979 and then Nkind (Right_Opnd (N)) = N_Op_Not
10980 then
10981 Error_Msg_N ("redundant double negation?r?", N);
10982 end if;
10983
10984 -- Complete resolution and evaluation of NOT
10985
10986 Resolve (Right_Opnd (N), B_Typ);
10987 Check_Unset_Reference (Right_Opnd (N));
10988 Set_Etype (N, B_Typ);
10989 Generate_Operator_Reference (N, B_Typ);
10990 Eval_Op_Not (N);
10991 end if;
10992 end Resolve_Op_Not;
10993
10994 -----------------------------
10995 -- Resolve_Operator_Symbol --
10996 -----------------------------
10997
10998 -- Nothing to be done, all resolved already
10999
11000 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
11001 pragma Warnings (Off, N);
11002 pragma Warnings (Off, Typ);
11003
11004 begin
11005 null;
11006 end Resolve_Operator_Symbol;
11007
11008 ----------------------------------
11009 -- Resolve_Qualified_Expression --
11010 ----------------------------------
11011
11012 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
11013 pragma Warnings (Off, Typ);
11014
11015 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
11016 Expr : constant Node_Id := Expression (N);
11017
11018 begin
11019 Resolve (Expr, Target_Typ);
11020 Check_Unset_Reference (Expr);
11021
11022 -- A qualified expression requires an exact match of the type, class-
11023 -- wide matching is not allowed. However, if the qualifying type is
11024 -- specific and the expression has a class-wide type, it may still be
11025 -- okay, since it can be the result of the expansion of a call to a
11026 -- dispatching function, so we also have to check class-wideness of the
11027 -- type of the expression's original node.
11028
11029 if (Is_Class_Wide_Type (Target_Typ)
11030 or else
11031 (Is_Class_Wide_Type (Etype (Expr))
11032 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
11033 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
11034 then
11035 Wrong_Type (Expr, Target_Typ);
11036 end if;
11037
11038 -- If the target type is unconstrained, then we reset the type of the
11039 -- result from the type of the expression. For other cases, the actual
11040 -- subtype of the expression is the target type. But we avoid doing it
11041 -- for an allocator since this is not needed and might be problematic.
11042
11043 if Is_Composite_Type (Target_Typ)
11044 and then not Is_Constrained (Target_Typ)
11045 and then Nkind (Parent (N)) /= N_Allocator
11046 then
11047 Set_Etype (N, Etype (Expr));
11048 end if;
11049
11050 Analyze_Dimension (N);
11051 Eval_Qualified_Expression (N);
11052
11053 -- If we still have a qualified expression after the static evaluation,
11054 -- then apply a scalar range check if needed. The reason that we do this
11055 -- after the Eval call is that otherwise, the application of the range
11056 -- check may convert an illegal static expression and result in warning
11057 -- rather than giving an error (e.g Integer'(Integer'Last + 1)).
11058
11059 if Nkind (N) = N_Qualified_Expression
11060 and then Is_Scalar_Type (Target_Typ)
11061 then
11062 Apply_Scalar_Range_Check (Expr, Target_Typ);
11063 end if;
11064
11065 -- AI12-0100: Once the qualified expression is resolved, check whether
11066 -- operand satisfies a static predicate of the target subtype, if any.
11067 -- In the static expression case, a predicate check failure is an error.
11068
11069 if Has_Predicates (Target_Typ) then
11070 Check_Expression_Against_Static_Predicate
11071 (Expr, Target_Typ, Static_Failure_Is_Error => True);
11072 end if;
11073 end Resolve_Qualified_Expression;
11074
11075 ------------------------------
11076 -- Resolve_Raise_Expression --
11077 ------------------------------
11078
11079 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id) is
11080 begin
11081 if Typ = Raise_Type then
11082 Error_Msg_N ("cannot find unique type for raise expression", N);
11083 Set_Etype (N, Any_Type);
11084
11085 else
11086 Set_Etype (N, Typ);
11087
11088 -- Apply check for required parentheses in the enclosing
11089 -- context of raise_expressions (RM 11.3 (2)), including default
11090 -- expressions in contexts that can include aspect specifications,
11091 -- and ancestor parts of extension aggregates.
11092
11093 declare
11094 Par : Node_Id := Parent (N);
11095 Parentheses_Found : Boolean := Paren_Count (N) > 0;
11096
11097 begin
11098 while Present (Par)
11099 and then Nkind (Par) in N_Has_Etype
11100 loop
11101 if Paren_Count (Par) > 0 then
11102 Parentheses_Found := True;
11103 end if;
11104
11105 if Nkind (Par) = N_Extension_Aggregate
11106 and then N = Ancestor_Part (Par)
11107 then
11108 exit;
11109 end if;
11110
11111 Par := Parent (Par);
11112 end loop;
11113
11114 if not Parentheses_Found
11115 and then Comes_From_Source (Par)
11116 and then
11117 (Nkind (Par) in N_Modular_Type_Definition
11118 | N_Floating_Point_Definition
11119 | N_Ordinary_Fixed_Point_Definition
11120 | N_Decimal_Fixed_Point_Definition
11121 | N_Extension_Aggregate
11122 | N_Discriminant_Specification
11123 | N_Parameter_Specification
11124 | N_Formal_Object_Declaration
11125
11126 or else (Nkind (Par) = N_Object_Declaration
11127 and then
11128 Nkind (Parent (Par)) /= N_Extended_Return_Statement))
11129 then
11130 Error_Msg_N
11131 ("raise_expression must be parenthesized in this context",
11132 N);
11133 end if;
11134 end;
11135 end if;
11136 end Resolve_Raise_Expression;
11137
11138 -------------------
11139 -- Resolve_Range --
11140 -------------------
11141
11142 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
11143 L : constant Node_Id := Low_Bound (N);
11144 H : constant Node_Id := High_Bound (N);
11145
11146 function First_Last_Ref return Boolean;
11147 -- Returns True if N is of the form X'First .. X'Last where X is the
11148 -- same entity for both attributes.
11149
11150 --------------------
11151 -- First_Last_Ref --
11152 --------------------
11153
11154 function First_Last_Ref return Boolean is
11155 Lorig : constant Node_Id := Original_Node (L);
11156 Horig : constant Node_Id := Original_Node (H);
11157
11158 begin
11159 if Nkind (Lorig) = N_Attribute_Reference
11160 and then Nkind (Horig) = N_Attribute_Reference
11161 and then Attribute_Name (Lorig) = Name_First
11162 and then Attribute_Name (Horig) = Name_Last
11163 then
11164 declare
11165 PL : constant Node_Id := Prefix (Lorig);
11166 PH : constant Node_Id := Prefix (Horig);
11167 begin
11168 return Is_Entity_Name (PL)
11169 and then Is_Entity_Name (PH)
11170 and then Entity (PL) = Entity (PH);
11171 end;
11172 end if;
11173
11174 return False;
11175 end First_Last_Ref;
11176
11177 -- Start of processing for Resolve_Range
11178
11179 begin
11180 Set_Etype (N, Typ);
11181
11182 Resolve (L, Typ);
11183 Resolve (H, Typ);
11184
11185 -- Reanalyze the lower bound after both bounds have been analyzed, so
11186 -- that the range is known to be static or not by now. This may trigger
11187 -- more compile-time evaluation, which is useful for static analysis
11188 -- with GNATprove. This is not needed for compilation or static analysis
11189 -- with CodePeer, as full expansion does that evaluation then.
11190
11191 if GNATprove_Mode then
11192 Set_Analyzed (L, False);
11193 Resolve (L, Typ);
11194 end if;
11195
11196 -- Check for inappropriate range on unordered enumeration type
11197
11198 if Bad_Unordered_Enumeration_Reference (N, Typ)
11199
11200 -- Exclude X'First .. X'Last if X is the same entity for both
11201
11202 and then not First_Last_Ref
11203 then
11204 Error_Msg_Sloc := Sloc (Typ);
11205 Error_Msg_NE
11206 ("subrange of unordered enumeration type& declared#?.u?", N, Typ);
11207 end if;
11208
11209 Check_Unset_Reference (L);
11210 Check_Unset_Reference (H);
11211
11212 -- We have to check the bounds for being within the base range as
11213 -- required for a non-static context. Normally this is automatic and
11214 -- done as part of evaluating expressions, but the N_Range node is an
11215 -- exception, since in GNAT we consider this node to be a subexpression,
11216 -- even though in Ada it is not. The circuit in Sem_Eval could check for
11217 -- this, but that would put the test on the main evaluation path for
11218 -- expressions.
11219
11220 Check_Non_Static_Context (L);
11221 Check_Non_Static_Context (H);
11222
11223 -- Check for an ambiguous range over character literals. This will
11224 -- happen with a membership test involving only literals.
11225
11226 if Typ = Any_Character then
11227 Ambiguous_Character (L);
11228 Set_Etype (N, Any_Type);
11229 return;
11230 end if;
11231
11232 -- If bounds are static, constant-fold them, so size computations are
11233 -- identical between front-end and back-end. Do not perform this
11234 -- transformation while analyzing generic units, as type information
11235 -- would be lost when reanalyzing the constant node in the instance.
11236
11237 if Is_Discrete_Type (Typ) and then Expander_Active then
11238 if Is_OK_Static_Expression (L) then
11239 Fold_Uint (L, Expr_Value (L), Static => True);
11240 end if;
11241
11242 if Is_OK_Static_Expression (H) then
11243 Fold_Uint (H, Expr_Value (H), Static => True);
11244 end if;
11245 end if;
11246
11247 -- If we have a compile-time-known null range, we warn, because that is
11248 -- likely to be a mistake. (Dynamic null ranges make sense, but often
11249 -- compile-time-known ones do not.) Warn only if this is in a subtype
11250 -- declaration. We do this here, rather than while analyzing a subtype
11251 -- declaration, in case we decide to expand the cases. We do not want to
11252 -- warn in all cases, because some are idiomatic, such as an empty
11253 -- aggregate (1 .. 0 => <>).
11254
11255 -- We don't warn in generics or their instances, because there might be
11256 -- some instances where the range is null, and some where it is not,
11257 -- which would lead to false alarms.
11258
11259 if not (Inside_A_Generic or In_Instance)
11260 and then Comes_From_Source (N)
11261 and then Compile_Time_Compare
11262 (Low_Bound (N), High_Bound (N), Assume_Valid => True) = GT
11263 and then Nkind (Parent (N)) = N_Range_Constraint
11264 and then Nkind (Parent (Parent (N))) = N_Subtype_Indication
11265 and then Nkind (Parent (Parent (Parent (N)))) = N_Subtype_Declaration
11266 and then Is_OK_Static_Range (N)
11267 then
11268 Error_Msg_N ("null range??", N);
11269 end if;
11270 end Resolve_Range;
11271
11272 --------------------------
11273 -- Resolve_Real_Literal --
11274 --------------------------
11275
11276 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
11277 Actual_Typ : constant Entity_Id := Etype (N);
11278
11279 begin
11280 -- Special processing for fixed-point literals to make sure that the
11281 -- value is an exact multiple of the small where this is required. We
11282 -- skip this for the universal real case, and also for generic types.
11283
11284 if Is_Fixed_Point_Type (Typ)
11285 and then Typ /= Universal_Fixed
11286 and then Typ /= Any_Fixed
11287 and then not Is_Generic_Type (Typ)
11288 then
11289 -- We must freeze the base type to get the proper value of the small
11290
11291 if not Is_Frozen (Base_Type (Typ)) then
11292 Freeze_Fixed_Point_Type (Base_Type (Typ));
11293 end if;
11294
11295 declare
11296 Val : constant Ureal := Realval (N);
11297 Cintr : constant Ureal := Val / Small_Value (Base_Type (Typ));
11298 Cint : constant Uint := UR_Trunc (Cintr);
11299 Den : constant Uint := Norm_Den (Cintr);
11300 Stat : Boolean;
11301
11302 begin
11303 -- Case of literal is not an exact multiple of the Small
11304
11305 if Den /= 1 then
11306
11307 -- For a source program literal for a decimal fixed-point type,
11308 -- this is statically illegal (RM 4.9(36)).
11309
11310 if Is_Decimal_Fixed_Point_Type (Typ)
11311 and then Actual_Typ = Universal_Real
11312 and then Comes_From_Source (N)
11313 then
11314 Error_Msg_N ("value has extraneous low order digits", N);
11315 end if;
11316
11317 -- Generate a warning if literal from source
11318
11319 if Is_OK_Static_Expression (N)
11320 and then Warn_On_Bad_Fixed_Value
11321 then
11322 Error_Msg_N
11323 ("?b?static fixed-point value is not a multiple of Small!",
11324 N);
11325 end if;
11326
11327 -- Replace literal by a value that is the exact representation
11328 -- of a value of the type, i.e. a multiple of the small value,
11329 -- by truncation, since Machine_Rounds is false for all GNAT
11330 -- fixed-point types (RM 4.9(38)).
11331
11332 Stat := Is_OK_Static_Expression (N);
11333 Rewrite (N,
11334 Make_Real_Literal (Sloc (N),
11335 Realval => Small_Value (Typ) * Cint));
11336
11337 Set_Is_Static_Expression (N, Stat);
11338 end if;
11339
11340 -- In all cases, set the corresponding integer field
11341
11342 Set_Corresponding_Integer_Value (N, Cint);
11343 end;
11344 end if;
11345
11346 -- Now replace the actual type by the expected type as usual
11347
11348 Set_Etype (N, Typ);
11349 Eval_Real_Literal (N);
11350 end Resolve_Real_Literal;
11351
11352 -----------------------
11353 -- Resolve_Reference --
11354 -----------------------
11355
11356 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
11357 P : constant Node_Id := Prefix (N);
11358
11359 begin
11360 -- Replace general access with specific type
11361
11362 if Ekind (Etype (N)) = E_Allocator_Type then
11363 Set_Etype (N, Base_Type (Typ));
11364 end if;
11365
11366 Resolve (P, Designated_Type (Etype (N)));
11367
11368 -- If we are taking the reference of a volatile entity, then treat it as
11369 -- a potential modification of this entity. This is too conservative,
11370 -- but necessary because remove side effects can cause transformations
11371 -- of normal assignments into reference sequences that otherwise fail to
11372 -- notice the modification.
11373
11374 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
11375 Note_Possible_Modification (P, Sure => False);
11376 end if;
11377 end Resolve_Reference;
11378
11379 --------------------------------
11380 -- Resolve_Selected_Component --
11381 --------------------------------
11382
11383 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
11384 Comp : Entity_Id;
11385 Comp1 : Entity_Id := Empty; -- prevent junk warning
11386 P : constant Node_Id := Prefix (N);
11387 S : constant Node_Id := Selector_Name (N);
11388 T : Entity_Id := Etype (P);
11389 I : Interp_Index;
11390 I1 : Interp_Index := 0; -- prevent junk warning
11391 It : Interp;
11392 It1 : Interp;
11393 Found : Boolean;
11394
11395 function Init_Component return Boolean;
11396 -- Check whether this is the initialization of a component within an
11397 -- init proc (by assignment or call to another init proc). If true,
11398 -- there is no need for a discriminant check.
11399
11400 --------------------
11401 -- Init_Component --
11402 --------------------
11403
11404 function Init_Component return Boolean is
11405 begin
11406 return Inside_Init_Proc
11407 and then Nkind (Prefix (N)) = N_Identifier
11408 and then Chars (Prefix (N)) = Name_uInit
11409 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
11410 end Init_Component;
11411
11412 -- Start of processing for Resolve_Selected_Component
11413
11414 begin
11415 if Is_Overloaded (P) then
11416
11417 -- Use the context type to select the prefix that has a selector
11418 -- of the correct name and type.
11419
11420 Found := False;
11421 Get_First_Interp (P, I, It);
11422
11423 Search : while Present (It.Typ) loop
11424 if Is_Access_Type (It.Typ) then
11425 T := Designated_Type (It.Typ);
11426 else
11427 T := It.Typ;
11428 end if;
11429
11430 -- Find the selected component. For a private prefix, the selector
11431 -- can denote a discriminant.
11432
11433 if Is_Record_Type (T) or else Is_Private_Type (T) then
11434
11435 -- If the prefix has a class-wide type, the visible components
11436 -- are those of the root type.
11437
11438 if Is_Class_Wide_Type (T) then
11439 T := Root_Type (T);
11440 end if;
11441
11442 Comp := First_Entity (T);
11443 while Present (Comp) loop
11444 if Chars (Comp) = Chars (S)
11445 and then Covers (Typ, Etype (Comp))
11446 then
11447 if not Found then
11448 Found := True;
11449 I1 := I;
11450 It1 := It;
11451 Comp1 := Comp;
11452
11453 else
11454 It := Disambiguate (P, I1, I, Any_Type);
11455
11456 if It = No_Interp then
11457 Error_Msg_N
11458 ("ambiguous prefix for selected component", N);
11459 Set_Etype (N, Typ);
11460 return;
11461
11462 else
11463 It1 := It;
11464
11465 -- There may be an implicit dereference. Retrieve
11466 -- designated record type.
11467
11468 if Is_Access_Type (It1.Typ) then
11469 T := Designated_Type (It1.Typ);
11470 else
11471 T := It1.Typ;
11472 end if;
11473
11474 if Scope (Comp1) /= T then
11475
11476 -- Resolution chooses the new interpretation.
11477 -- Find the component with the right name.
11478
11479 Comp1 := First_Entity (T);
11480 while Present (Comp1)
11481 and then Chars (Comp1) /= Chars (S)
11482 loop
11483 Next_Entity (Comp1);
11484 end loop;
11485 end if;
11486
11487 exit Search;
11488 end if;
11489 end if;
11490 end if;
11491
11492 Next_Entity (Comp);
11493 end loop;
11494 end if;
11495
11496 Get_Next_Interp (I, It);
11497 end loop Search;
11498
11499 -- There must be a legal interpretation at this point
11500
11501 pragma Assert (Found);
11502 Resolve (P, It1.Typ);
11503
11504 -- In general the expected type is the type of the context, not the
11505 -- type of the candidate selected component.
11506
11507 Set_Etype (N, Typ);
11508 Set_Entity_With_Checks (S, Comp1);
11509
11510 -- The type of the context and that of the component are
11511 -- compatible and in general identical, but if they are anonymous
11512 -- access-to-subprogram types, the relevant type is that of the
11513 -- component. This matters in Unnest_Subprograms mode, where the
11514 -- relevant context is the one in which the type is declared, not
11515 -- the point of use. This determines what activation record to use.
11516
11517 if Ekind (Typ) = E_Anonymous_Access_Subprogram_Type then
11518 Set_Etype (N, Etype (Comp1));
11519
11520 -- When the type of the component is an access to a class-wide type
11521 -- the relevant type is that of the component (since in such case we
11522 -- may need to generate implicit type conversions or dispatching
11523 -- calls).
11524
11525 elsif Is_Access_Type (Typ)
11526 and then not Is_Class_Wide_Type (Designated_Type (Typ))
11527 and then Is_Class_Wide_Type (Designated_Type (Etype (Comp1)))
11528 then
11529 Set_Etype (N, Etype (Comp1));
11530 end if;
11531
11532 else
11533 -- Resolve prefix with its type
11534
11535 Resolve (P, T);
11536 end if;
11537
11538 -- Generate cross-reference. We needed to wait until full overloading
11539 -- resolution was complete to do this, since otherwise we can't tell if
11540 -- we are an lvalue or not.
11541
11542 if Known_To_Be_Assigned (N) then
11543 Generate_Reference (Entity (S), S, 'm');
11544 else
11545 Generate_Reference (Entity (S), S, 'r');
11546 end if;
11547
11548 -- If the prefix's type is an access type, get to the real record type.
11549 -- Note: we do not apply an access check because an explicit dereference
11550 -- will be introduced later, and the check will happen there.
11551
11552 if Is_Access_Type (Etype (P)) then
11553 T := Implicitly_Designated_Type (Etype (P));
11554 Check_Fully_Declared_Prefix (T, P);
11555
11556 else
11557 T := Etype (P);
11558 end if;
11559
11560 -- Set flag for expander if discriminant check required on a component
11561 -- appearing within a variant.
11562
11563 if Has_Discriminants (T)
11564 and then Ekind (Entity (S)) = E_Component
11565 and then Present (Original_Record_Component (Entity (S)))
11566 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
11567 and then
11568 Is_Declared_Within_Variant (Original_Record_Component (Entity (S)))
11569 and then not Discriminant_Checks_Suppressed (T)
11570 and then not Init_Component
11571 then
11572 Set_Do_Discriminant_Check (N);
11573 end if;
11574
11575 if Ekind (Entity (S)) = E_Void then
11576 Error_Msg_N ("premature use of component", S);
11577 end if;
11578
11579 -- If the prefix is a record conversion, this may be a renamed
11580 -- discriminant whose bounds differ from those of the original
11581 -- one, so we must ensure that a range check is performed.
11582
11583 if Nkind (P) = N_Type_Conversion
11584 and then Ekind (Entity (S)) = E_Discriminant
11585 and then Is_Discrete_Type (Typ)
11586 then
11587 Set_Etype (N, Base_Type (Typ));
11588 end if;
11589
11590 -- Eval_Selected_Component may e.g. fold statically known discriminants.
11591
11592 Eval_Selected_Component (N);
11593
11594 if Nkind (N) = N_Selected_Component then
11595
11596 -- If the record type is atomic and the component is not, then this
11597 -- is worth a warning before Ada 2022, since we have a situation
11598 -- where the access to the component may cause extra read/writes of
11599 -- the atomic object, or partial word accesses, both of which may be
11600 -- unexpected.
11601
11602 if Is_Atomic_Non_VFA_Ref_With_Address (N)
11603 and then not Is_Atomic (Entity (S))
11604 and then not Is_Atomic (Etype (Entity (S)))
11605 and then Ada_Version < Ada_2022
11606 then
11607 Error_Msg_N
11608 ("??access to non-atomic component of atomic record",
11609 Prefix (N));
11610 Error_Msg_N
11611 ("\??may cause unexpected accesses to atomic object",
11612 Prefix (N));
11613 end if;
11614
11615 Resolve_Implicit_Dereference (Prefix (N));
11616 Analyze_Dimension (N);
11617 end if;
11618 end Resolve_Selected_Component;
11619
11620 -------------------
11621 -- Resolve_Shift --
11622 -------------------
11623
11624 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
11625 B_Typ : constant Entity_Id := Base_Type (Typ);
11626 L : constant Node_Id := Left_Opnd (N);
11627 R : constant Node_Id := Right_Opnd (N);
11628
11629 begin
11630 -- We do the resolution using the base type, because intermediate values
11631 -- in expressions always are of the base type, not a subtype of it.
11632
11633 Resolve (L, B_Typ);
11634 Resolve (R, Standard_Natural);
11635
11636 Check_Unset_Reference (L);
11637 Check_Unset_Reference (R);
11638
11639 Set_Etype (N, B_Typ);
11640 Generate_Operator_Reference (N, B_Typ);
11641 Eval_Shift (N);
11642 end Resolve_Shift;
11643
11644 ---------------------------
11645 -- Resolve_Short_Circuit --
11646 ---------------------------
11647
11648 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
11649 B_Typ : constant Entity_Id := Base_Type (Typ);
11650 L : constant Node_Id := Left_Opnd (N);
11651 R : constant Node_Id := Right_Opnd (N);
11652
11653 begin
11654 -- Ensure all actions associated with the left operand (e.g.
11655 -- finalization of transient objects) are fully evaluated locally within
11656 -- an expression with actions. This is particularly helpful for coverage
11657 -- analysis at the object level. However this should not happen in
11658 -- generics.
11659
11660 if Expander_Active then
11661 declare
11662 Reloc_L : constant Node_Id := Relocate_Node (L);
11663 begin
11664 Save_Interps (Old_N => L, New_N => Reloc_L);
11665
11666 Rewrite (L,
11667 Make_Expression_With_Actions (Sloc (L),
11668 Actions => New_List,
11669 Expression => Reloc_L));
11670
11671 -- Set Comes_From_Source on L to preserve warnings for unset
11672 -- reference.
11673
11674 Preserve_Comes_From_Source (L, Reloc_L);
11675 end;
11676 end if;
11677
11678 Resolve (L, B_Typ);
11679 Resolve (R, B_Typ);
11680
11681 -- Check for issuing warning for always False assert/check, this happens
11682 -- when assertions are turned off, in which case the pragma Assert/Check
11683 -- was transformed into:
11684
11685 -- if False and then <condition> then ...
11686
11687 -- and we detect this pattern
11688
11689 if Warn_On_Assertion_Failure
11690 and then Is_Entity_Name (R)
11691 and then Entity (R) = Standard_False
11692 and then Nkind (Parent (N)) = N_If_Statement
11693 and then Nkind (N) = N_And_Then
11694 and then Is_Entity_Name (L)
11695 and then Entity (L) = Standard_False
11696 then
11697 declare
11698 Orig : constant Node_Id := Original_Node (Parent (N));
11699
11700 begin
11701 -- Special handling of Asssert pragma
11702
11703 if Nkind (Orig) = N_Pragma
11704 and then Pragma_Name (Orig) = Name_Assert
11705 then
11706 declare
11707 Expr : constant Node_Id :=
11708 Original_Node
11709 (Expression
11710 (First (Pragma_Argument_Associations (Orig))));
11711
11712 begin
11713 -- Don't warn if original condition is explicit False,
11714 -- since obviously the failure is expected in this case.
11715
11716 if Is_Entity_Name (Expr)
11717 and then Entity (Expr) = Standard_False
11718 then
11719 null;
11720
11721 -- Issue warning. We do not want the deletion of the
11722 -- IF/AND-THEN to take this message with it. We achieve this
11723 -- by making sure that the expanded code points to the Sloc
11724 -- of the expression, not the original pragma.
11725
11726 else
11727 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
11728 -- The source location of the expression is not usually
11729 -- the best choice here. For example, it gets located on
11730 -- the last AND keyword in a chain of boolean expressiond
11731 -- AND'ed together. It is best to put the message on the
11732 -- first character of the assertion, which is the effect
11733 -- of the First_Node call here.
11734
11735 Error_Msg_F
11736 ("?.a?assertion would fail at run time!",
11737 Expression
11738 (First (Pragma_Argument_Associations (Orig))));
11739 end if;
11740 end;
11741
11742 -- Similar processing for Check pragma
11743
11744 elsif Nkind (Orig) = N_Pragma
11745 and then Pragma_Name (Orig) = Name_Check
11746 then
11747 -- Don't want to warn if original condition is explicit False
11748
11749 declare
11750 Expr : constant Node_Id :=
11751 Original_Node
11752 (Expression
11753 (Next (First (Pragma_Argument_Associations (Orig)))));
11754 begin
11755 if Is_Entity_Name (Expr)
11756 and then Entity (Expr) = Standard_False
11757 then
11758 null;
11759
11760 -- Post warning
11761
11762 else
11763 -- Again use Error_Msg_F rather than Error_Msg_N, see
11764 -- comment above for an explanation of why we do this.
11765
11766 Error_Msg_F
11767 ("?.a?check would fail at run time!",
11768 Expression
11769 (Last (Pragma_Argument_Associations (Orig))));
11770 end if;
11771 end;
11772 end if;
11773 end;
11774 end if;
11775
11776 -- Continue with processing of short circuit
11777
11778 Check_Unset_Reference (L);
11779 Check_Unset_Reference (R);
11780
11781 Set_Etype (N, B_Typ);
11782 Eval_Short_Circuit (N);
11783 end Resolve_Short_Circuit;
11784
11785 -------------------
11786 -- Resolve_Slice --
11787 -------------------
11788
11789 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
11790 Drange : constant Node_Id := Discrete_Range (N);
11791 Pref : constant Node_Id := Prefix (N);
11792 Array_Type : Entity_Id := Empty;
11793 Dexpr : Node_Id := Empty;
11794 Index_Type : Entity_Id;
11795
11796 begin
11797 if Is_Overloaded (Pref) then
11798
11799 -- Use the context type to select the prefix that yields the correct
11800 -- array type.
11801
11802 declare
11803 I : Interp_Index;
11804 I1 : Interp_Index := 0;
11805 It : Interp;
11806 Found : Boolean := False;
11807
11808 begin
11809 Get_First_Interp (Pref, I, It);
11810 while Present (It.Typ) loop
11811 if (Is_Array_Type (It.Typ)
11812 and then Covers (Typ, It.Typ))
11813 or else (Is_Access_Type (It.Typ)
11814 and then Is_Array_Type (Designated_Type (It.Typ))
11815 and then Covers (Typ, Designated_Type (It.Typ)))
11816 then
11817 if Found then
11818 It := Disambiguate (Pref, I1, I, Any_Type);
11819
11820 if It = No_Interp then
11821 Error_Msg_N ("ambiguous prefix for slicing", N);
11822 Set_Etype (N, Typ);
11823 return;
11824 else
11825 Found := True;
11826 Array_Type := It.Typ;
11827 I1 := I;
11828 end if;
11829 else
11830 Found := True;
11831 Array_Type := It.Typ;
11832 I1 := I;
11833 end if;
11834 end if;
11835
11836 Get_Next_Interp (I, It);
11837 end loop;
11838 end;
11839
11840 else
11841 Array_Type := Etype (Pref);
11842 end if;
11843
11844 Resolve (Pref, Array_Type);
11845
11846 -- If the prefix's type is an access type, get to the real array type.
11847 -- Note: we do not apply an access check because an explicit dereference
11848 -- will be introduced later, and the check will happen there.
11849
11850 if Is_Access_Type (Array_Type) then
11851 Array_Type := Implicitly_Designated_Type (Array_Type);
11852
11853 -- If the prefix is an access to an unconstrained array, we must use
11854 -- the actual subtype of the object to perform the index checks. The
11855 -- object denoted by the prefix is implicit in the node, so we build
11856 -- an explicit representation for it in order to compute the actual
11857 -- subtype.
11858
11859 if not Is_Constrained (Array_Type) then
11860 Remove_Side_Effects (Pref);
11861
11862 declare
11863 Obj : constant Node_Id :=
11864 Make_Explicit_Dereference (Sloc (N),
11865 Prefix => New_Copy_Tree (Pref));
11866 begin
11867 Set_Etype (Obj, Array_Type);
11868 Set_Parent (Obj, Parent (N));
11869 Array_Type := Get_Actual_Subtype (Obj);
11870 end;
11871 end if;
11872
11873 -- In CodePeer mode the attribute Image is not expanded, so when it
11874 -- acts as a prefix of a slice, we handle it like a call to function
11875 -- returning an unconstrained string. Same for the Wide variants of
11876 -- attribute Image.
11877
11878 elsif Is_Entity_Name (Pref)
11879 or else Nkind (Pref) = N_Explicit_Dereference
11880 or else (Nkind (Pref) = N_Function_Call
11881 and then not Is_Constrained (Etype (Pref)))
11882 or else (CodePeer_Mode
11883 and then Nkind (Pref) = N_Attribute_Reference
11884 and then Attribute_Name (Pref) in Name_Image
11885 | Name_Wide_Image
11886 | Name_Wide_Wide_Image)
11887 then
11888 Array_Type := Get_Actual_Subtype (Pref);
11889
11890 -- If the name is a selected component that depends on discriminants,
11891 -- build an actual subtype for it. This can happen only when the name
11892 -- itself is overloaded; otherwise the actual subtype is created when
11893 -- the selected component is analyzed.
11894
11895 elsif Nkind (Pref) = N_Selected_Component
11896 and then Full_Analysis
11897 and then Depends_On_Discriminant (First_Index (Array_Type))
11898 then
11899 declare
11900 Act_Decl : constant Node_Id :=
11901 Build_Actual_Subtype_Of_Component (Array_Type, Pref);
11902 begin
11903 Insert_Action (N, Act_Decl);
11904 Array_Type := Defining_Identifier (Act_Decl);
11905 end;
11906
11907 -- Maybe this should just be "else", instead of checking for the
11908 -- specific case of slice??? This is needed for the case where the
11909 -- prefix is an Image attribute, which gets expanded to a slice, and so
11910 -- has a constrained subtype which we want to use for the slice range
11911 -- check applied below (the range check won't get done if the
11912 -- unconstrained subtype of the 'Image is used).
11913
11914 elsif Nkind (Pref) = N_Slice then
11915 Array_Type := Etype (Pref);
11916 end if;
11917
11918 -- Obtain the type of the array index
11919
11920 if Ekind (Array_Type) = E_String_Literal_Subtype then
11921 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
11922 else
11923 Index_Type := Etype (First_Index (Array_Type));
11924 end if;
11925
11926 -- If name was overloaded, set slice type correctly now
11927
11928 Set_Etype (N, Array_Type);
11929
11930 -- Handle the generation of a range check that compares the array index
11931 -- against the discrete_range. The check is not applied to internally
11932 -- built nodes associated with the expansion of dispatch tables. Check
11933 -- that Ada.Tags has already been loaded to avoid extra dependencies on
11934 -- the unit.
11935
11936 if Tagged_Type_Expansion
11937 and then RTU_Loaded (Ada_Tags)
11938 and then Nkind (Pref) = N_Selected_Component
11939 and then Present (Entity (Selector_Name (Pref)))
11940 and then Entity (Selector_Name (Pref)) =
11941 RTE_Record_Component (RE_Prims_Ptr)
11942 then
11943 null;
11944
11945 -- The discrete_range is specified by a subtype name. Create an
11946 -- equivalent range attribute, apply checks to this attribute, but
11947 -- insert them into the range expression of the slice itself.
11948
11949 elsif Is_Entity_Name (Drange) then
11950 Dexpr :=
11951 Make_Attribute_Reference
11952 (Sloc (Drange),
11953 Prefix =>
11954 New_Occurrence_Of (Entity (Drange), Sloc (Drange)),
11955 Attribute_Name => Name_Range);
11956
11957 Analyze_And_Resolve (Dexpr, Etype (Drange));
11958
11959 elsif Nkind (Drange) = N_Subtype_Indication then
11960 Dexpr := Range_Expression (Constraint (Drange));
11961
11962 -- The discrete_range is a regular range (or a range attribute, which
11963 -- will be resolved into a regular range). Resolve the bounds and remove
11964 -- their side effects.
11965
11966 else
11967 Resolve (Drange, Base_Type (Index_Type));
11968
11969 if Nkind (Drange) = N_Range then
11970 Force_Evaluation (Low_Bound (Drange));
11971 Force_Evaluation (High_Bound (Drange));
11972
11973 Dexpr := Drange;
11974 end if;
11975 end if;
11976
11977 if Present (Dexpr) then
11978 Apply_Range_Check (Dexpr, Index_Type, Insert_Node => Drange);
11979 end if;
11980
11981 Set_Slice_Subtype (N);
11982
11983 -- Check bad use of type with predicates
11984
11985 declare
11986 Subt : Entity_Id;
11987
11988 begin
11989 if Nkind (Drange) = N_Subtype_Indication
11990 and then Has_Predicates (Entity (Subtype_Mark (Drange)))
11991 then
11992 Subt := Entity (Subtype_Mark (Drange));
11993 else
11994 Subt := Etype (Drange);
11995 end if;
11996
11997 if Has_Predicates (Subt) then
11998 Bad_Predicated_Subtype_Use
11999 ("subtype& has predicate, not allowed in slice", Drange, Subt);
12000 end if;
12001 end;
12002
12003 -- Otherwise here is where we check suspicious indexes
12004
12005 if Nkind (Drange) = N_Range then
12006 Warn_On_Suspicious_Index (Pref, Low_Bound (Drange));
12007 Warn_On_Suspicious_Index (Pref, High_Bound (Drange));
12008 end if;
12009
12010 Resolve_Implicit_Dereference (Pref);
12011 Analyze_Dimension (N);
12012 Eval_Slice (N);
12013 end Resolve_Slice;
12014
12015 ----------------------------
12016 -- Resolve_String_Literal --
12017 ----------------------------
12018
12019 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
12020 C_Typ : constant Entity_Id := Component_Type (Typ);
12021 R_Typ : constant Entity_Id := Root_Type (C_Typ);
12022 Loc : constant Source_Ptr := Sloc (N);
12023 Str : constant String_Id := Strval (N);
12024 Strlen : constant Nat := String_Length (Str);
12025 Subtype_Id : Entity_Id;
12026 Need_Check : Boolean;
12027
12028 begin
12029 -- For a string appearing in a concatenation, defer creation of the
12030 -- string_literal_subtype until the end of the resolution of the
12031 -- concatenation, because the literal may be constant-folded away. This
12032 -- is a useful optimization for long concatenation expressions.
12033
12034 -- If the string is an aggregate built for a single character (which
12035 -- happens in a non-static context) or a is null string to which special
12036 -- checks may apply, we build the subtype. Wide strings must also get a
12037 -- string subtype if they come from a one character aggregate. Strings
12038 -- generated by attributes might be static, but it is often hard to
12039 -- determine whether the enclosing context is static, so we generate
12040 -- subtypes for them as well, thus losing some rarer optimizations ???
12041 -- Same for strings that come from a static conversion.
12042
12043 Need_Check :=
12044 (Strlen = 0 and then Typ /= Standard_String)
12045 or else Nkind (Parent (N)) /= N_Op_Concat
12046 or else (N /= Left_Opnd (Parent (N))
12047 and then N /= Right_Opnd (Parent (N)))
12048 or else ((Typ = Standard_Wide_String
12049 or else Typ = Standard_Wide_Wide_String)
12050 and then Nkind (Original_Node (N)) /= N_String_Literal);
12051
12052 -- If the resolving type is itself a string literal subtype, we can just
12053 -- reuse it, since there is no point in creating another.
12054
12055 if Ekind (Typ) = E_String_Literal_Subtype then
12056 Subtype_Id := Typ;
12057
12058 elsif Nkind (Parent (N)) = N_Op_Concat
12059 and then not Need_Check
12060 and then Nkind (Original_Node (N)) not in N_Character_Literal
12061 | N_Attribute_Reference
12062 | N_Qualified_Expression
12063 | N_Type_Conversion
12064 then
12065 Subtype_Id := Typ;
12066
12067 -- Do not generate a string literal subtype for the default expression
12068 -- of a formal parameter in GNATprove mode. This is because the string
12069 -- subtype is associated with the freezing actions of the subprogram,
12070 -- however freezing is disabled in GNATprove mode and as a result the
12071 -- subtype is unavailable.
12072
12073 elsif GNATprove_Mode
12074 and then Nkind (Parent (N)) = N_Parameter_Specification
12075 then
12076 Subtype_Id := Typ;
12077
12078 -- Otherwise we must create a string literal subtype. Note that the
12079 -- whole idea of string literal subtypes is simply to avoid the need
12080 -- for building a full fledged array subtype for each literal.
12081
12082 else
12083 Set_String_Literal_Subtype (N, Typ);
12084 Subtype_Id := Etype (N);
12085 end if;
12086
12087 if Nkind (Parent (N)) /= N_Op_Concat
12088 or else Need_Check
12089 then
12090 Set_Etype (N, Subtype_Id);
12091 Eval_String_Literal (N);
12092 end if;
12093
12094 if Is_Limited_Composite (Typ)
12095 or else Is_Private_Composite (Typ)
12096 then
12097 Error_Msg_N ("string literal not available for private array", N);
12098 Set_Etype (N, Any_Type);
12099 return;
12100 end if;
12101
12102 -- The validity of a null string has been checked in the call to
12103 -- Eval_String_Literal.
12104
12105 if Strlen = 0 then
12106 return;
12107
12108 -- Always accept string literal with component type Any_Character, which
12109 -- occurs in error situations and in comparisons of literals, both of
12110 -- which should accept all literals.
12111
12112 elsif R_Typ = Any_Character then
12113 return;
12114
12115 -- If the type is bit-packed, then we always transform the string
12116 -- literal into a full fledged aggregate.
12117
12118 elsif Is_Bit_Packed_Array (Typ) then
12119 null;
12120
12121 -- Deal with cases of Wide_Wide_String, Wide_String, and String
12122
12123 else
12124 -- For Standard.Wide_Wide_String, or any other type whose component
12125 -- type is Standard.Wide_Wide_Character, we know that all the
12126 -- characters in the string must be acceptable, since the parser
12127 -- accepted the characters as valid character literals.
12128
12129 if R_Typ = Standard_Wide_Wide_Character then
12130 null;
12131
12132 -- For the case of Standard.String, or any other type whose component
12133 -- type is Standard.Character, we must make sure that there are no
12134 -- wide characters in the string, i.e. that it is entirely composed
12135 -- of characters in range of type Character.
12136
12137 -- If the string literal is the result of a static concatenation, the
12138 -- test has already been performed on the components, and need not be
12139 -- repeated.
12140
12141 elsif R_Typ = Standard_Character
12142 and then Nkind (Original_Node (N)) /= N_Op_Concat
12143 then
12144 for J in 1 .. Strlen loop
12145 if not In_Character_Range (Get_String_Char (Str, J)) then
12146
12147 -- If we are out of range, post error. This is one of the
12148 -- very few places that we place the flag in the middle of
12149 -- a token, right under the offending wide character. Not
12150 -- quite clear if this is right wrt wide character encoding
12151 -- sequences, but it's only an error message.
12152
12153 Error_Msg
12154 ("literal out of range of type Standard.Character",
12155 Loc + Source_Ptr (J));
12156 return;
12157 end if;
12158 end loop;
12159
12160 -- For the case of Standard.Wide_String, or any other type whose
12161 -- component type is Standard.Wide_Character, we must make sure that
12162 -- there are no wide characters in the string, i.e. that it is
12163 -- entirely composed of characters in range of type Wide_Character.
12164
12165 -- If the string literal is the result of a static concatenation,
12166 -- the test has already been performed on the components, and need
12167 -- not be repeated.
12168
12169 elsif R_Typ = Standard_Wide_Character
12170 and then Nkind (Original_Node (N)) /= N_Op_Concat
12171 then
12172 for J in 1 .. Strlen loop
12173 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
12174
12175 -- If we are out of range, post error. This is one of the
12176 -- very few places that we place the flag in the middle of
12177 -- a token, right under the offending wide character.
12178
12179 -- This is not quite right, because characters in general
12180 -- will take more than one character position ???
12181
12182 Error_Msg
12183 ("literal out of range of type Standard.Wide_Character",
12184 Loc + Source_Ptr (J));
12185 return;
12186 end if;
12187 end loop;
12188
12189 -- If the root type is not a standard character, then we will convert
12190 -- the string into an aggregate and will let the aggregate code do
12191 -- the checking. Standard Wide_Wide_Character is also OK here.
12192
12193 else
12194 null;
12195 end if;
12196
12197 -- See if the component type of the array corresponding to the string
12198 -- has compile time known bounds. If yes we can directly check
12199 -- whether the evaluation of the string will raise constraint error.
12200 -- Otherwise we need to transform the string literal into the
12201 -- corresponding character aggregate and let the aggregate code do
12202 -- the checking. We use the same transformation if the component
12203 -- type has a static predicate, which will be applied to each
12204 -- character when the aggregate is resolved.
12205
12206 if Is_Standard_Character_Type (R_Typ) then
12207
12208 -- Check for the case of full range, where we are definitely OK
12209
12210 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
12211 return;
12212 end if;
12213
12214 -- Here the range is not the complete base type range, so check
12215
12216 declare
12217 Comp_Typ_Lo : constant Node_Id :=
12218 Type_Low_Bound (Component_Type (Typ));
12219 Comp_Typ_Hi : constant Node_Id :=
12220 Type_High_Bound (Component_Type (Typ));
12221
12222 Char_Val : Uint;
12223
12224 begin
12225 if Compile_Time_Known_Value (Comp_Typ_Lo)
12226 and then Compile_Time_Known_Value (Comp_Typ_Hi)
12227 then
12228 for J in 1 .. Strlen loop
12229 Char_Val := UI_From_CC (Get_String_Char (Str, J));
12230
12231 if Char_Val < Expr_Value (Comp_Typ_Lo)
12232 or else Char_Val > Expr_Value (Comp_Typ_Hi)
12233 then
12234 Apply_Compile_Time_Constraint_Error
12235 (N, "character out of range??",
12236 CE_Range_Check_Failed,
12237 Loc => Loc + Source_Ptr (J));
12238 end if;
12239 end loop;
12240
12241 if not Has_Static_Predicate (C_Typ) then
12242 return;
12243 end if;
12244 end if;
12245 end;
12246 end if;
12247 end if;
12248
12249 -- If we got here we meed to transform the string literal into the
12250 -- equivalent qualified positional array aggregate. This is rather
12251 -- heavy artillery for this situation, but it is hard work to avoid.
12252
12253 declare
12254 Lits : constant List_Id := New_List;
12255 P : Source_Ptr := Loc + 1;
12256 C : Char_Code;
12257
12258 begin
12259 -- Build the character literals, we give them source locations that
12260 -- correspond to the string positions, which is a bit tricky given
12261 -- the possible presence of wide character escape sequences.
12262
12263 for J in 1 .. Strlen loop
12264 C := Get_String_Char (Str, J);
12265 Set_Character_Literal_Name (C);
12266
12267 Append_To (Lits,
12268 Make_Character_Literal (P,
12269 Chars => Name_Find,
12270 Char_Literal_Value => UI_From_CC (C)));
12271
12272 if In_Character_Range (C) then
12273 P := P + 1;
12274
12275 -- Should we have a call to Skip_Wide here ???
12276
12277 -- ??? else
12278 -- Skip_Wide (P);
12279
12280 end if;
12281 end loop;
12282
12283 Rewrite (N,
12284 Make_Qualified_Expression (Loc,
12285 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
12286 Expression =>
12287 Make_Aggregate (Loc, Expressions => Lits)));
12288
12289 Analyze_And_Resolve (N, Typ);
12290 end;
12291 end Resolve_String_Literal;
12292
12293 -------------------------
12294 -- Resolve_Target_Name --
12295 -------------------------
12296
12297 procedure Resolve_Target_Name (N : Node_Id; Typ : Entity_Id) is
12298 begin
12299 Set_Etype (N, Typ);
12300 end Resolve_Target_Name;
12301
12302 -----------------------------
12303 -- Resolve_Type_Conversion --
12304 -----------------------------
12305
12306 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
12307 Conv_OK : constant Boolean := Conversion_OK (N);
12308 Operand : constant Node_Id := Expression (N);
12309 Operand_Typ : constant Entity_Id := Etype (Operand);
12310 Target_Typ : constant Entity_Id := Etype (N);
12311 Rop : Node_Id;
12312 Orig_N : Node_Id;
12313 Orig_T : Node_Id;
12314
12315 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
12316 -- Set to False to suppress cases where we want to suppress the test
12317 -- for redundancy to avoid possible false positives on this warning.
12318
12319 begin
12320 if not Conv_OK
12321 and then not Valid_Conversion (N, Target_Typ, Operand)
12322 then
12323 return;
12324 end if;
12325
12326 -- If the Operand Etype is Universal_Fixed, then the conversion is
12327 -- never redundant. We need this check because by the time we have
12328 -- finished the rather complex transformation, the conversion looks
12329 -- redundant when it is not.
12330
12331 if Operand_Typ = Universal_Fixed then
12332 Test_Redundant := False;
12333
12334 -- If the operand is marked as Any_Fixed, then special processing is
12335 -- required. This is also a case where we suppress the test for a
12336 -- redundant conversion, since most certainly it is not redundant.
12337
12338 elsif Operand_Typ = Any_Fixed then
12339 Test_Redundant := False;
12340
12341 -- Mixed-mode operation involving a literal. Context must be a fixed
12342 -- type which is applied to the literal subsequently.
12343
12344 -- Multiplication and division involving two fixed type operands must
12345 -- yield a universal real because the result is computed in arbitrary
12346 -- precision.
12347
12348 if Is_Fixed_Point_Type (Typ)
12349 and then Nkind (Operand) in N_Op_Divide | N_Op_Multiply
12350 and then Etype (Left_Opnd (Operand)) = Any_Fixed
12351 and then Etype (Right_Opnd (Operand)) = Any_Fixed
12352 then
12353 Set_Etype (Operand, Universal_Real);
12354
12355 elsif Is_Numeric_Type (Typ)
12356 and then Nkind (Operand) in N_Op_Multiply | N_Op_Divide
12357 and then (Etype (Right_Opnd (Operand)) = Universal_Real
12358 or else
12359 Etype (Left_Opnd (Operand)) = Universal_Real)
12360 then
12361 -- Return if expression is ambiguous
12362
12363 if Unique_Fixed_Point_Type (N) = Any_Type then
12364 return;
12365
12366 -- If nothing else, the available fixed type is Duration
12367
12368 else
12369 Set_Etype (Operand, Standard_Duration);
12370 end if;
12371
12372 -- Resolve the real operand with largest available precision
12373
12374 if Etype (Right_Opnd (Operand)) = Universal_Real then
12375 Rop := New_Copy_Tree (Right_Opnd (Operand));
12376 else
12377 Rop := New_Copy_Tree (Left_Opnd (Operand));
12378 end if;
12379
12380 Resolve (Rop, Universal_Real);
12381
12382 -- If the operand is a literal (it could be a non-static and
12383 -- illegal exponentiation) check whether the use of Duration
12384 -- is potentially inaccurate.
12385
12386 if Nkind (Rop) = N_Real_Literal
12387 and then Realval (Rop) /= Ureal_0
12388 and then abs Realval (Rop) < Delta_Value (Standard_Duration)
12389 then
12390 Error_Msg_N
12391 ("??universal real operand can only "
12392 & "be interpreted as Duration!", Rop);
12393 Error_Msg_N
12394 ("\??precision will be lost in the conversion!", Rop);
12395 end if;
12396
12397 elsif Is_Numeric_Type (Typ)
12398 and then Nkind (Operand) in N_Op
12399 and then Unique_Fixed_Point_Type (N) /= Any_Type
12400 then
12401 Set_Etype (Operand, Standard_Duration);
12402
12403 else
12404 Error_Msg_N ("invalid context for mixed mode operation", N);
12405 Set_Etype (Operand, Any_Type);
12406 return;
12407 end if;
12408 end if;
12409
12410 Resolve (Operand);
12411
12412 Analyze_Dimension (N);
12413
12414 -- Note: we do the Eval_Type_Conversion call before applying the
12415 -- required checks for a subtype conversion. This is important, since
12416 -- both are prepared under certain circumstances to change the type
12417 -- conversion to a constraint error node, but in the case of
12418 -- Eval_Type_Conversion this may reflect an illegality in the static
12419 -- case, and we would miss the illegality (getting only a warning
12420 -- message), if we applied the type conversion checks first.
12421
12422 Eval_Type_Conversion (N);
12423
12424 -- Even when evaluation is not possible, we may be able to simplify the
12425 -- conversion or its expression. This needs to be done before applying
12426 -- checks, since otherwise the checks may use the original expression
12427 -- and defeat the simplifications. This is specifically the case for
12428 -- elimination of the floating-point Truncation attribute in
12429 -- float-to-int conversions.
12430
12431 Simplify_Type_Conversion (N);
12432
12433 -- If after evaluation we still have a type conversion, then we may need
12434 -- to apply checks required for a subtype conversion. But skip them if
12435 -- universal fixed operands are involved, since range checks are handled
12436 -- separately for these cases, after the expansion done by Exp_Fixd.
12437
12438 if Nkind (N) = N_Type_Conversion
12439 and then not Is_Generic_Type (Root_Type (Target_Typ))
12440 and then Target_Typ /= Universal_Fixed
12441 and then Etype (Operand) /= Universal_Fixed
12442 then
12443 Apply_Type_Conversion_Checks (N);
12444 end if;
12445
12446 -- Issue warning for conversion of simple object to its own type. We
12447 -- have to test the original nodes, since they may have been rewritten
12448 -- by various optimizations.
12449
12450 Orig_N := Original_Node (N);
12451
12452 -- Here we test for a redundant conversion if the warning mode is
12453 -- active (and was not locally reset), and we have a type conversion
12454 -- from source not appearing in a generic instance.
12455
12456 if Test_Redundant
12457 and then Nkind (Orig_N) = N_Type_Conversion
12458 and then Comes_From_Source (Orig_N)
12459 and then not In_Instance
12460 then
12461 Orig_N := Original_Node (Expression (Orig_N));
12462 Orig_T := Target_Typ;
12463
12464 -- If the node is part of a larger expression, the Target_Type
12465 -- may not be the original type of the node if the context is a
12466 -- condition. Recover original type to see if conversion is needed.
12467
12468 if Is_Boolean_Type (Orig_T)
12469 and then Nkind (Parent (N)) in N_Op
12470 then
12471 Orig_T := Etype (Parent (N));
12472 end if;
12473
12474 -- If we have an entity name, then give the warning if the entity
12475 -- is the right type, or if it is a loop parameter covered by the
12476 -- original type (that's needed because loop parameters have an
12477 -- odd subtype coming from the bounds).
12478
12479 if (Is_Entity_Name (Orig_N)
12480 and then Present (Entity (Orig_N))
12481 and then
12482 (Etype (Entity (Orig_N)) = Orig_T
12483 or else
12484 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
12485 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
12486
12487 -- If not an entity, then type of expression must match
12488
12489 or else Etype (Orig_N) = Orig_T
12490 then
12491 -- One more check, do not give warning if the analyzed conversion
12492 -- has an expression with non-static bounds, and the bounds of the
12493 -- target are static. This avoids junk warnings in cases where the
12494 -- conversion is necessary to establish staticness, for example in
12495 -- a case statement.
12496
12497 if not Is_OK_Static_Subtype (Operand_Typ)
12498 and then Is_OK_Static_Subtype (Target_Typ)
12499 then
12500 null;
12501
12502 -- Never give a warning if the operand is a conditional expression
12503 -- because RM 4.5.7(10/3) forces its type to be the target type.
12504
12505 elsif Nkind (Orig_N) in N_Case_Expression | N_If_Expression then
12506 null;
12507
12508 -- Finally, if this type conversion occurs in a context requiring
12509 -- a prefix, and the expression is a qualified expression then the
12510 -- type conversion is not redundant, since a qualified expression
12511 -- is not a prefix, whereas a type conversion is. For example, "X
12512 -- := T'(Funx(...)).Y;" is illegal because a selected component
12513 -- requires a prefix, but a type conversion makes it legal: "X :=
12514 -- T(T'(Funx(...))).Y;"
12515
12516 -- In Ada 2012, a qualified expression is a name, so this idiom is
12517 -- no longer needed, but we still suppress the warning because it
12518 -- seems unfriendly for warnings to pop up when you switch to the
12519 -- newer language version.
12520
12521 elsif Nkind (Orig_N) = N_Qualified_Expression
12522 and then Nkind (Parent (N)) in N_Attribute_Reference
12523 | N_Indexed_Component
12524 | N_Selected_Component
12525 | N_Slice
12526 | N_Explicit_Dereference
12527 then
12528 null;
12529
12530 -- Never warn on conversion to Long_Long_Integer'Base since
12531 -- that is most likely an artifact of the extended overflow
12532 -- checking and comes from complex expanded code.
12533
12534 elsif Orig_T = Base_Type (Standard_Long_Long_Integer) then
12535 null;
12536
12537 -- Do not warn on conversion to class-wide type on helpers of
12538 -- class-wide preconditions because in this context the warning
12539 -- would be spurious (since the class-wide precondition has been
12540 -- installed in the return statement of the helper, which has a
12541 -- class-wide formal type instead of a regular tagged type).
12542
12543 elsif Is_Class_Wide_Type (Orig_T)
12544 and then Is_Subprogram_Or_Generic_Subprogram (Current_Scope)
12545 and then Present (Class_Preconditions_Subprogram (Current_Scope))
12546 then
12547 null;
12548
12549 -- Here we give the redundant conversion warning. If it is an
12550 -- entity, give the name of the entity in the message. If not,
12551 -- just mention the expression.
12552
12553 else
12554 if Is_Entity_Name (Orig_N) then
12555 Error_Msg_Node_2 := Orig_T;
12556 Error_Msg_NE -- CODEFIX
12557 ("?r?redundant conversion, & is of type &!",
12558 N, Entity (Orig_N));
12559 else
12560 Error_Msg_NE
12561 ("?r?redundant conversion, expression is of type&!",
12562 N, Orig_T);
12563 end if;
12564 end if;
12565 end if;
12566 end if;
12567
12568 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
12569 -- No need to perform any interface conversion if the type of the
12570 -- expression coincides with the target type.
12571
12572 if Ada_Version >= Ada_2005
12573 and then Expander_Active
12574 and then Operand_Typ /= Target_Typ
12575 then
12576 declare
12577 Opnd : Entity_Id := Operand_Typ;
12578 Target : Entity_Id := Target_Typ;
12579
12580 begin
12581 -- If the type of the operand is a limited view, use nonlimited
12582 -- view when available. If it is a class-wide type, recover the
12583 -- class-wide type of the nonlimited view.
12584
12585 if From_Limited_With (Opnd)
12586 and then Has_Non_Limited_View (Opnd)
12587 then
12588 Opnd := Non_Limited_View (Opnd);
12589 Set_Etype (Expression (N), Opnd);
12590 end if;
12591
12592 -- It seems that Non_Limited_View should also be applied for
12593 -- Target when it has a limited view, but that leads to missing
12594 -- error checks on interface conversions further below. ???
12595
12596 if Is_Access_Type (Opnd) then
12597 Opnd := Designated_Type (Opnd);
12598
12599 -- If the type of the operand is a limited view, use nonlimited
12600 -- view when available. If it is a class-wide type, recover the
12601 -- class-wide type of the nonlimited view.
12602
12603 if From_Limited_With (Opnd)
12604 and then Has_Non_Limited_View (Opnd)
12605 then
12606 Opnd := Non_Limited_View (Opnd);
12607 end if;
12608 end if;
12609
12610 if Is_Access_Type (Target_Typ) then
12611 Target := Designated_Type (Target);
12612
12613 -- If the target type is a limited view, use nonlimited view
12614 -- when available.
12615
12616 if From_Limited_With (Target)
12617 and then Has_Non_Limited_View (Target)
12618 then
12619 Target := Non_Limited_View (Target);
12620 end if;
12621 end if;
12622
12623 if Opnd = Target then
12624 null;
12625
12626 -- Conversion from interface type
12627
12628 -- It seems that it would be better for the error checks below
12629 -- to be performed as part of Validate_Conversion (and maybe some
12630 -- of the error checks above could be moved as well?). ???
12631
12632 elsif Is_Interface (Opnd) then
12633
12634 -- Ada 2005 (AI-217): Handle entities from limited views
12635
12636 if From_Limited_With (Opnd) then
12637 Error_Msg_Qual_Level := 99;
12638 Error_Msg_NE -- CODEFIX
12639 ("missing WITH clause on package &", N,
12640 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
12641 Error_Msg_N
12642 ("type conversions require visibility of the full view",
12643 N);
12644
12645 elsif From_Limited_With (Target)
12646 and then not
12647 (Is_Access_Type (Target_Typ)
12648 and then Present (Non_Limited_View (Etype (Target))))
12649 then
12650 Error_Msg_Qual_Level := 99;
12651 Error_Msg_NE -- CODEFIX
12652 ("missing WITH clause on package &", N,
12653 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
12654 Error_Msg_N
12655 ("type conversions require visibility of the full view",
12656 N);
12657
12658 else
12659 Expand_Interface_Conversion (N);
12660 end if;
12661
12662 -- Conversion to interface type
12663
12664 elsif Is_Interface (Target) then
12665 Expand_Interface_Conversion (N);
12666 end if;
12667 end;
12668 end if;
12669
12670 -- Ada 2012: Once the type conversion is resolved, check whether the
12671 -- operand satisfies a static predicate of the target subtype, if any.
12672 -- In the static expression case, a predicate check failure is an error.
12673
12674 if Has_Predicates (Target_Typ) then
12675 Check_Expression_Against_Static_Predicate
12676 (N, Target_Typ, Static_Failure_Is_Error => True);
12677 end if;
12678
12679 -- If at this stage we have a fixed to integer conversion, make sure the
12680 -- Do_Range_Check flag is set, because such conversions in general need
12681 -- a range check. We only need this if expansion is off, see above why.
12682
12683 if Nkind (N) = N_Type_Conversion
12684 and then not Expander_Active
12685 and then Is_Integer_Type (Target_Typ)
12686 and then Is_Fixed_Point_Type (Operand_Typ)
12687 and then not Range_Checks_Suppressed (Target_Typ)
12688 and then not Range_Checks_Suppressed (Operand_Typ)
12689 then
12690 Set_Do_Range_Check (Operand);
12691 end if;
12692 end Resolve_Type_Conversion;
12693
12694 ----------------------
12695 -- Resolve_Unary_Op --
12696 ----------------------
12697
12698 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
12699 B_Typ : constant Entity_Id := Base_Type (Typ);
12700 R : constant Node_Id := Right_Opnd (N);
12701 OK : Boolean;
12702 Lo : Uint;
12703 Hi : Uint;
12704
12705 begin
12706 -- Deal with intrinsic unary operators
12707
12708 if Comes_From_Source (N)
12709 and then Ekind (Entity (N)) = E_Function
12710 and then Is_Imported (Entity (N))
12711 and then Is_Intrinsic_Subprogram (Entity (N))
12712 then
12713 Resolve_Intrinsic_Unary_Operator (N, Typ);
12714 return;
12715 end if;
12716
12717 -- Deal with universal cases
12718
12719 if Is_Universal_Numeric_Type (Etype (R)) then
12720 Check_For_Visible_Operator (N, B_Typ);
12721 end if;
12722
12723 Set_Etype (N, B_Typ);
12724 Resolve (R, B_Typ);
12725
12726 -- Generate warning for negative literal of a modular type, unless it is
12727 -- enclosed directly in a type qualification or a type conversion, as it
12728 -- is likely not what the user intended. We don't issue the warning for
12729 -- the common use of -1 to denote OxFFFF_FFFF...
12730
12731 if Warn_On_Suspicious_Modulus_Value
12732 and then Nkind (N) = N_Op_Minus
12733 and then Nkind (R) = N_Integer_Literal
12734 and then Comes_From_Source (R)
12735 and then Is_Modular_Integer_Type (B_Typ)
12736 and then Nkind (Parent (N)) not in N_Qualified_Expression
12737 | N_Type_Conversion
12738 and then Expr_Value (R) > Uint_1
12739 then
12740 Error_Msg_N
12741 ("?.m?negative literal of modular type is in fact positive", N);
12742 Error_Msg_Uint_1 := (-Expr_Value (R)) mod Modulus (B_Typ);
12743 Error_Msg_Uint_2 := Expr_Value (R);
12744 Error_Msg_N ("\do you really mean^ when writing -^ '?", N);
12745 Error_Msg_N
12746 ("\if you do, use qualification to avoid this warning", N);
12747 end if;
12748
12749 -- Generate warning for expressions like abs (x mod 2)
12750
12751 if Warn_On_Redundant_Constructs
12752 and then Nkind (N) = N_Op_Abs
12753 then
12754 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
12755
12756 if OK and then Hi >= Lo and then Lo >= 0 then
12757 Error_Msg_N -- CODEFIX
12758 ("?r?abs applied to known non-negative value has no effect", N);
12759 end if;
12760 end if;
12761
12762 -- Deal with reference generation
12763
12764 Check_Unset_Reference (R);
12765 Generate_Operator_Reference (N, B_Typ);
12766 Analyze_Dimension (N);
12767 Eval_Unary_Op (N);
12768
12769 -- Set overflow checking bit. Much cleverer code needed here eventually
12770 -- and perhaps the Resolve routines should be separated for the various
12771 -- arithmetic operations, since they will need different processing ???
12772
12773 if Nkind (N) in N_Op then
12774 if not Overflow_Checks_Suppressed (Etype (N)) then
12775 Enable_Overflow_Check (N);
12776 end if;
12777 end if;
12778
12779 -- Generate warning for expressions like -5 mod 3 for integers. No need
12780 -- to worry in the floating-point case, since parens do not affect the
12781 -- result so there is no point in giving in a warning.
12782
12783 declare
12784 Norig : constant Node_Id := Original_Node (N);
12785 Rorig : Node_Id;
12786 Val : Uint;
12787 HB : Uint;
12788 LB : Uint;
12789 Lval : Uint;
12790 Opnd : Node_Id;
12791
12792 begin
12793 if Warn_On_Questionable_Missing_Parens
12794 and then Comes_From_Source (Norig)
12795 and then Is_Integer_Type (Typ)
12796 and then Nkind (Norig) = N_Op_Minus
12797 then
12798 Rorig := Original_Node (Right_Opnd (Norig));
12799
12800 -- We are looking for cases where the right operand is not
12801 -- parenthesized, and is a binary operator, multiply, divide, or
12802 -- mod. These are the cases where the grouping can affect results.
12803
12804 if Paren_Count (Rorig) = 0
12805 and then Nkind (Rorig) in N_Op_Mod | N_Op_Multiply | N_Op_Divide
12806 then
12807 -- For mod, we always give the warning, since the value is
12808 -- affected by the parenthesization (e.g. (-5) mod 315 /=
12809 -- -(5 mod 315)). But for the other cases, the only concern is
12810 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
12811 -- overflows, but (-2) * 64 does not). So we try to give the
12812 -- message only when overflow is possible.
12813
12814 if Nkind (Rorig) /= N_Op_Mod
12815 and then Compile_Time_Known_Value (R)
12816 then
12817 Val := Expr_Value (R);
12818
12819 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
12820 HB := Expr_Value (Type_High_Bound (Typ));
12821 else
12822 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
12823 end if;
12824
12825 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
12826 LB := Expr_Value (Type_Low_Bound (Typ));
12827 else
12828 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
12829 end if;
12830
12831 -- Note that the test below is deliberately excluding the
12832 -- largest negative number, since that is a potentially
12833 -- troublesome case (e.g. -2 * x, where the result is the
12834 -- largest negative integer has an overflow with 2 * x).
12835
12836 if Val > LB and then Val <= HB then
12837 return;
12838 end if;
12839 end if;
12840
12841 -- For the multiplication case, the only case we have to worry
12842 -- about is when (-a)*b is exactly the largest negative number
12843 -- so that -(a*b) can cause overflow. This can only happen if
12844 -- a is a power of 2, and more generally if any operand is a
12845 -- constant that is not a power of 2, then the parentheses
12846 -- cannot affect whether overflow occurs. We only bother to
12847 -- test the left most operand
12848
12849 -- Loop looking at left operands for one that has known value
12850
12851 Opnd := Rorig;
12852 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
12853 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
12854 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
12855
12856 -- Operand value of 0 or 1 skips warning
12857
12858 if Lval <= 1 then
12859 return;
12860
12861 -- Otherwise check power of 2, if power of 2, warn, if
12862 -- anything else, skip warning.
12863
12864 else
12865 while Lval /= 2 loop
12866 if Lval mod 2 = 1 then
12867 return;
12868 else
12869 Lval := Lval / 2;
12870 end if;
12871 end loop;
12872
12873 exit Opnd_Loop;
12874 end if;
12875 end if;
12876
12877 -- Keep looking at left operands
12878
12879 Opnd := Left_Opnd (Opnd);
12880 end loop Opnd_Loop;
12881
12882 -- For rem or "/" we can only have a problematic situation
12883 -- if the divisor has a value of minus one or one. Otherwise
12884 -- overflow is impossible (divisor > 1) or we have a case of
12885 -- division by zero in any case.
12886
12887 if Nkind (Rorig) in N_Op_Divide | N_Op_Rem
12888 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
12889 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
12890 then
12891 return;
12892 end if;
12893
12894 -- If we fall through warning should be issued
12895
12896 -- Shouldn't we test Warn_On_Questionable_Missing_Parens ???
12897
12898 Error_Msg_N
12899 ("??unary minus expression should be parenthesized here!", N);
12900 end if;
12901 end if;
12902 end;
12903 end Resolve_Unary_Op;
12904
12905 ---------------------------------------
12906 -- Resolve_Unchecked_Type_Conversion --
12907 ---------------------------------------
12908
12909 procedure Resolve_Unchecked_Type_Conversion
12910 (N : Node_Id;
12911 Typ : Entity_Id)
12912 is
12913 pragma Warnings (Off, Typ);
12914
12915 Operand : constant Node_Id := Expression (N);
12916 Opnd_Type : constant Entity_Id := Etype (Operand);
12917
12918 begin
12919 -- Resolve operand using its own type
12920
12921 Resolve (Operand, Opnd_Type);
12922
12923 -- If the expression is a conversion to universal integer of an
12924 -- an expression with an integer type, then we can eliminate the
12925 -- intermediate conversion to universal integer.
12926
12927 if Nkind (Operand) = N_Type_Conversion
12928 and then Entity (Subtype_Mark (Operand)) = Universal_Integer
12929 and then Is_Integer_Type (Etype (Expression (Operand)))
12930 then
12931 Rewrite (Operand, Relocate_Node (Expression (Operand)));
12932 Analyze_And_Resolve (Operand);
12933 end if;
12934
12935 -- In an inlined context, the unchecked conversion may be applied
12936 -- to a literal, in which case its type is the type of the context.
12937 -- (In other contexts conversions cannot apply to literals).
12938
12939 if In_Inlined_Body
12940 and then (Opnd_Type = Any_Character or else
12941 Opnd_Type = Any_Integer or else
12942 Opnd_Type = Any_Real)
12943 then
12944 Set_Etype (Operand, Typ);
12945 end if;
12946
12947 Analyze_Dimension (N);
12948 Eval_Unchecked_Conversion (N);
12949 end Resolve_Unchecked_Type_Conversion;
12950
12951 ------------------------------
12952 -- Rewrite_Operator_As_Call --
12953 ------------------------------
12954
12955 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
12956 Loc : constant Source_Ptr := Sloc (N);
12957 Actuals : constant List_Id := New_List;
12958 New_N : Node_Id;
12959
12960 begin
12961 if Nkind (N) in N_Binary_Op then
12962 Append (Left_Opnd (N), Actuals);
12963 end if;
12964
12965 Append (Right_Opnd (N), Actuals);
12966
12967 New_N :=
12968 Make_Function_Call (Sloc => Loc,
12969 Name => New_Occurrence_Of (Nam, Loc),
12970 Parameter_Associations => Actuals);
12971
12972 Preserve_Comes_From_Source (New_N, N);
12973 Preserve_Comes_From_Source (Name (New_N), N);
12974 Rewrite (N, New_N);
12975 Set_Etype (N, Etype (Nam));
12976 end Rewrite_Operator_As_Call;
12977
12978 ------------------------------
12979 -- Rewrite_Renamed_Operator --
12980 ------------------------------
12981
12982 procedure Rewrite_Renamed_Operator
12983 (N : Node_Id;
12984 Op : Entity_Id;
12985 Typ : Entity_Id)
12986 is
12987 Nam : constant Name_Id := Chars (Op);
12988 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
12989 Op_Node : Node_Id;
12990
12991 begin
12992 -- Do not perform this transformation within a pre/postcondition,
12993 -- because the expression will be reanalyzed, and the transformation
12994 -- might affect the visibility of the operator, e.g. in an instance.
12995 -- Note that fully analyzed and expanded pre/postconditions appear as
12996 -- pragma Check equivalents.
12997
12998 if In_Pre_Post_Condition (N) then
12999 return;
13000 end if;
13001
13002 -- Likewise when an expression function is being preanalyzed, since the
13003 -- expression will be reanalyzed as part of the generated body.
13004
13005 if In_Spec_Expression then
13006 declare
13007 S : constant Entity_Id := Current_Scope_No_Loops;
13008 begin
13009 if Ekind (S) = E_Function
13010 and then Nkind (Original_Node (Unit_Declaration_Node (S))) =
13011 N_Expression_Function
13012 then
13013 return;
13014 end if;
13015 end;
13016 end if;
13017
13018 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
13019 Set_Chars (Op_Node, Nam);
13020 Set_Etype (Op_Node, Etype (N));
13021 Set_Entity (Op_Node, Op);
13022 Set_Right_Opnd (Op_Node, Right_Opnd (N));
13023
13024 if Is_Binary then
13025 Set_Left_Opnd (Op_Node, Left_Opnd (N));
13026 end if;
13027
13028 -- Indicate that both the original entity and its renaming are
13029 -- referenced at this point.
13030
13031 Generate_Reference (Entity (N), N);
13032 Generate_Reference (Op, N);
13033
13034 Rewrite (N, Op_Node);
13035
13036 -- If the context type is private, add the appropriate conversions so
13037 -- that the operator is applied to the full view. This is done in the
13038 -- routines that resolve intrinsic operators.
13039
13040 if Is_Intrinsic_Subprogram (Op) and then Is_Private_Type (Typ) then
13041 case Nkind (N) is
13042 when N_Op_Add
13043 | N_Op_Divide
13044 | N_Op_Expon
13045 | N_Op_Mod
13046 | N_Op_Multiply
13047 | N_Op_Rem
13048 | N_Op_Subtract
13049 =>
13050 Resolve_Intrinsic_Operator (N, Typ);
13051
13052 when N_Op_Abs
13053 | N_Op_Minus
13054 | N_Op_Plus
13055 =>
13056 Resolve_Intrinsic_Unary_Operator (N, Typ);
13057
13058 when others =>
13059 Resolve (N, Typ);
13060 end case;
13061 end if;
13062 end Rewrite_Renamed_Operator;
13063
13064 -----------------------
13065 -- Set_Slice_Subtype --
13066 -----------------------
13067
13068 -- Build an implicit subtype declaration to represent the type delivered by
13069 -- the slice. This is an abbreviated version of an array subtype. We define
13070 -- an index subtype for the slice, using either the subtype name or the
13071 -- discrete range of the slice. To be consistent with index usage elsewhere
13072 -- we create a list header to hold the single index. This list is not
13073 -- otherwise attached to the syntax tree.
13074
13075 procedure Set_Slice_Subtype (N : Node_Id) is
13076 Loc : constant Source_Ptr := Sloc (N);
13077 Index_List : constant List_Id := New_List;
13078 Index : Node_Id;
13079 Index_Subtype : Entity_Id;
13080 Index_Type : Entity_Id;
13081 Slice_Subtype : Entity_Id;
13082 Drange : constant Node_Id := Discrete_Range (N);
13083
13084 begin
13085 Index_Type := Base_Type (Etype (Drange));
13086
13087 if Is_Entity_Name (Drange) then
13088 Index_Subtype := Entity (Drange);
13089
13090 else
13091 -- We force the evaluation of a range. This is definitely needed in
13092 -- the renamed case, and seems safer to do unconditionally. Note in
13093 -- any case that since we will create and insert an Itype referring
13094 -- to this range, we must make sure any side effect removal actions
13095 -- are inserted before the Itype definition.
13096
13097 if Nkind (Drange) = N_Range then
13098 Force_Evaluation (Low_Bound (Drange));
13099 Force_Evaluation (High_Bound (Drange));
13100
13101 -- If the discrete range is given by a subtype indication, the
13102 -- type of the slice is the base of the subtype mark.
13103
13104 elsif Nkind (Drange) = N_Subtype_Indication then
13105 declare
13106 R : constant Node_Id := Range_Expression (Constraint (Drange));
13107 begin
13108 Index_Type := Base_Type (Entity (Subtype_Mark (Drange)));
13109 Force_Evaluation (Low_Bound (R));
13110 Force_Evaluation (High_Bound (R));
13111 end;
13112 end if;
13113
13114 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
13115
13116 -- Take a new copy of Drange (where bounds have been rewritten to
13117 -- reference side-effect-free names). Using a separate tree ensures
13118 -- that further expansion (e.g. while rewriting a slice assignment
13119 -- into a FOR loop) does not attempt to remove side effects on the
13120 -- bounds again (which would cause the bounds in the index subtype
13121 -- definition to refer to temporaries before they are defined) (the
13122 -- reason is that some names are considered side effect free here
13123 -- for the subtype, but not in the context of a loop iteration
13124 -- scheme).
13125
13126 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
13127 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
13128 Set_Etype (Index_Subtype, Index_Type);
13129 Set_Size_Info (Index_Subtype, Index_Type);
13130 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
13131 Set_Is_Constrained (Index_Subtype);
13132 end if;
13133
13134 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
13135
13136 Index := New_Occurrence_Of (Index_Subtype, Loc);
13137 Set_Etype (Index, Index_Subtype);
13138 Append (Index, Index_List);
13139
13140 Set_First_Index (Slice_Subtype, Index);
13141 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
13142 Set_Is_Constrained (Slice_Subtype, True);
13143
13144 Check_Compile_Time_Size (Slice_Subtype);
13145
13146 -- The Etype of the existing Slice node is reset to this slice subtype.
13147 -- Its bounds are obtained from its first index.
13148
13149 Set_Etype (N, Slice_Subtype);
13150
13151 -- For bit-packed slice subtypes, freeze immediately (except in the case
13152 -- of being in a "spec expression" where we never freeze when we first
13153 -- see the expression).
13154
13155 if Is_Bit_Packed_Array (Slice_Subtype) and not In_Spec_Expression then
13156 Freeze_Itype (Slice_Subtype, N);
13157
13158 -- For all other cases insert an itype reference in the slice's actions
13159 -- so that the itype is frozen at the proper place in the tree (i.e. at
13160 -- the point where actions for the slice are analyzed). Note that this
13161 -- is different from freezing the itype immediately, which might be
13162 -- premature (e.g. if the slice is within a transient scope). This needs
13163 -- to be done only if expansion is enabled.
13164
13165 elsif Expander_Active then
13166 Ensure_Defined (Typ => Slice_Subtype, N => N);
13167 end if;
13168 end Set_Slice_Subtype;
13169
13170 --------------------------------
13171 -- Set_String_Literal_Subtype --
13172 --------------------------------
13173
13174 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
13175 Loc : constant Source_Ptr := Sloc (N);
13176 Low_Bound : constant Node_Id :=
13177 Type_Low_Bound (Etype (First_Index (Typ)));
13178 Length : constant Nat :=
13179 (case Nkind (N) is
13180 when N_String_Literal => String_Length (Strval (N)),
13181 -- Sinput.Source_Last points to an EOF character that's not in the
13182 -- original file and we do not include that character.
13183 when N_External_Initializer => Nat (
13184 Source_Last (File_Index (N)) - Source_First (File_Index (N))),
13185 when others => 0);
13186 Subtype_Id : Entity_Id;
13187
13188 begin
13189 if Nkind (N) not in N_String_Literal | N_External_Initializer then
13190 return;
13191 end if;
13192
13193 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
13194 Set_String_Literal_Length (Subtype_Id, UI_From_Int (Length));
13195 Set_Etype (Subtype_Id, Base_Type (Typ));
13196 Set_Is_Constrained (Subtype_Id);
13197 Set_Etype (N, Subtype_Id);
13198
13199 -- The low bound is set from the low bound of the corresponding index
13200 -- type. Note that we do not store the high bound in the string literal
13201 -- subtype, but it can be deduced if necessary from the length and the
13202 -- low bound.
13203
13204 if Is_OK_Static_Expression (Low_Bound) then
13205 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
13206
13207 -- If the lower bound is not static we create a range for the string
13208 -- literal, using the index type and the known length of the literal.
13209 -- If the length is 1, then the upper bound is set to a mere copy of
13210 -- the lower bound; or else, if the index type is a signed integer,
13211 -- then the upper bound is computed as Low_Bound + L - 1; otherwise,
13212 -- the upper bound is computed as T'Val (T'Pos (Low_Bound) + L - 1).
13213
13214 else
13215 declare
13216 Index_List : constant List_Id := New_List;
13217 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
13218 Array_Subtype : Entity_Id;
13219 Drange : Node_Id;
13220 High_Bound : Node_Id;
13221 Index : Node_Id;
13222 Index_Subtype : Entity_Id;
13223
13224 begin
13225 if Length = 1 then
13226 High_Bound := New_Copy_Tree (Low_Bound);
13227
13228 elsif Is_Signed_Integer_Type (Index_Type) then
13229 High_Bound :=
13230 Make_Op_Add (Loc,
13231 Left_Opnd => New_Copy_Tree (Low_Bound),
13232 Right_Opnd => Make_Integer_Literal (Loc, Length - 1));
13233
13234 else
13235 High_Bound :=
13236 Make_Attribute_Reference (Loc,
13237 Attribute_Name => Name_Val,
13238 Prefix =>
13239 New_Occurrence_Of (Index_Type, Loc),
13240 Expressions => New_List (
13241 Make_Op_Add (Loc,
13242 Left_Opnd =>
13243 Make_Attribute_Reference (Loc,
13244 Attribute_Name => Name_Pos,
13245 Prefix =>
13246 New_Occurrence_Of (Index_Type, Loc),
13247 Expressions =>
13248 New_List (New_Copy_Tree (Low_Bound))),
13249 Right_Opnd =>
13250 Make_Integer_Literal (Loc, Length - 1))));
13251 end if;
13252
13253 if Is_Integer_Type (Index_Type) then
13254 Set_String_Literal_Low_Bound
13255 (Subtype_Id, Make_Integer_Literal (Loc, 1));
13256
13257 else
13258 -- If the index type is an enumeration type, build bounds
13259 -- expression with attributes.
13260
13261 Set_String_Literal_Low_Bound
13262 (Subtype_Id,
13263 Make_Attribute_Reference (Loc,
13264 Attribute_Name => Name_First,
13265 Prefix =>
13266 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
13267 end if;
13268
13269 Analyze_And_Resolve
13270 (String_Literal_Low_Bound (Subtype_Id), Base_Type (Index_Type));
13271
13272 -- Build bona fide subtype for the string, and wrap it in an
13273 -- unchecked conversion, because the back end expects the
13274 -- String_Literal_Subtype to have a static lower bound.
13275
13276 Index_Subtype :=
13277 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
13278 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
13279 Set_Scalar_Range (Index_Subtype, Drange);
13280 Set_Parent (Drange, N);
13281 Analyze_And_Resolve (Drange, Index_Type);
13282
13283 -- In this context, the Index_Type may already have a constraint,
13284 -- so use common base type on string subtype. The base type may
13285 -- be used when generating attributes of the string, for example
13286 -- in the context of a slice assignment.
13287
13288 Set_Etype (Index_Subtype, Base_Type (Index_Type));
13289 Set_Size_Info (Index_Subtype, Index_Type);
13290 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
13291
13292 Array_Subtype := Create_Itype (E_Array_Subtype, N);
13293
13294 Index := New_Occurrence_Of (Index_Subtype, Loc);
13295 Set_Etype (Index, Index_Subtype);
13296 Append (Index, Index_List);
13297
13298 Set_First_Index (Array_Subtype, Index);
13299 Set_Etype (Array_Subtype, Base_Type (Typ));
13300 Set_Is_Constrained (Array_Subtype, True);
13301
13302 Rewrite (N, Unchecked_Convert_To (Array_Subtype, N));
13303 Set_Etype (N, Array_Subtype);
13304 end;
13305 end if;
13306 end Set_String_Literal_Subtype;
13307
13308 ------------------------------
13309 -- Simplify_Type_Conversion --
13310 ------------------------------
13311
13312 procedure Simplify_Type_Conversion (N : Node_Id) is
13313 begin
13314 if Nkind (N) = N_Type_Conversion then
13315 declare
13316 Operand : constant Node_Id := Expression (N);
13317 Target_Typ : constant Entity_Id := Etype (N);
13318 Opnd_Typ : constant Entity_Id := Etype (Operand);
13319
13320 begin
13321 -- Special processing if the conversion is the expression of a
13322 -- Rounding or Truncation attribute reference. In this case we
13323 -- replace:
13324
13325 -- ityp (ftyp'Rounding (x)) or ityp (ftyp'Truncation (x))
13326
13327 -- by
13328
13329 -- ityp (x)
13330
13331 -- with the Float_Truncate flag set to False or True respectively,
13332 -- which is more efficient. We reuse Rounding for Machine_Rounding
13333 -- as System.Fat_Gen, which is a permissible behavior.
13334
13335 if Is_Floating_Point_Type (Opnd_Typ)
13336 and then
13337 (Is_Integer_Type (Target_Typ)
13338 or else (Is_Fixed_Point_Type (Target_Typ)
13339 and then Conversion_OK (N)))
13340 and then Nkind (Operand) = N_Attribute_Reference
13341 and then Attribute_Name (Operand) in Name_Rounding
13342 | Name_Machine_Rounding
13343 | Name_Truncation
13344 then
13345 declare
13346 Truncate : constant Boolean :=
13347 Attribute_Name (Operand) = Name_Truncation;
13348 begin
13349 Rewrite (Operand,
13350 Relocate_Node (First (Expressions (Operand))));
13351 Set_Float_Truncate (N, Truncate);
13352 end;
13353
13354 -- Special processing for the conversion of an integer literal to
13355 -- a dynamic type: we first convert the literal to the root type
13356 -- and then convert the result to the target type, the goal being
13357 -- to avoid doing range checks in universal integer.
13358
13359 elsif Is_Integer_Type (Target_Typ)
13360 and then not Is_Generic_Type (Root_Type (Target_Typ))
13361 and then Nkind (Operand) = N_Integer_Literal
13362 and then Opnd_Typ = Universal_Integer
13363 then
13364 Convert_To_And_Rewrite (Root_Type (Target_Typ), Operand);
13365 Analyze_And_Resolve (Operand);
13366
13367 -- If the expression is a conversion to universal integer of an
13368 -- an expression with an integer type, then we can eliminate the
13369 -- intermediate conversion to universal integer.
13370
13371 elsif Nkind (Operand) = N_Type_Conversion
13372 and then Entity (Subtype_Mark (Operand)) = Universal_Integer
13373 and then Is_Integer_Type (Etype (Expression (Operand)))
13374 then
13375 Rewrite (Operand, Relocate_Node (Expression (Operand)));
13376 Analyze_And_Resolve (Operand);
13377 end if;
13378 end;
13379 end if;
13380 end Simplify_Type_Conversion;
13381
13382 ------------------------------
13383 -- Try_User_Defined_Literal --
13384 ------------------------------
13385
13386 function Try_User_Defined_Literal
13387 (N : Node_Id;
13388 Typ : Entity_Id) return Boolean
13389 is
13390 begin
13391 if Has_Applicable_User_Defined_Literal (N, Typ) then
13392 return True;
13393
13394 elsif Nkind (N) = N_If_Expression then
13395 -- Both dependent expressions must have the same type as the context
13396
13397 declare
13398 Condition : constant Node_Id := First (Expressions (N));
13399 Then_Expr : constant Node_Id := Next (Condition);
13400 Else_Expr : constant Node_Id := Next (Then_Expr);
13401
13402 begin
13403 if Has_Applicable_User_Defined_Literal (Then_Expr, Typ) then
13404 Resolve (Else_Expr, Typ);
13405 Analyze_And_Resolve (N, Typ);
13406 return True;
13407
13408 elsif Has_Applicable_User_Defined_Literal (Else_Expr, Typ) then
13409 Resolve (Then_Expr, Typ);
13410 Analyze_And_Resolve (N, Typ);
13411 return True;
13412 end if;
13413 end;
13414
13415 elsif Nkind (N) = N_Case_Expression then
13416 -- All dependent expressions must have the same type as the context
13417
13418 declare
13419 Alt : Node_Id;
13420
13421 begin
13422 Alt := First (Alternatives (N));
13423
13424 while Present (Alt) loop
13425 if Has_Applicable_User_Defined_Literal (Expression (Alt), Typ)
13426 then
13427 declare
13428 Other_Alt : Node_Id;
13429
13430 begin
13431 Other_Alt := First (Alternatives (N));
13432
13433 while Present (Other_Alt) loop
13434 if Other_Alt /= Alt then
13435 Resolve (Expression (Other_Alt), Typ);
13436 end if;
13437
13438 Next (Other_Alt);
13439 end loop;
13440
13441 Analyze_And_Resolve (N, Typ);
13442 return True;
13443 end;
13444 end if;
13445
13446 Next (Alt);
13447 end loop;
13448 end;
13449 end if;
13450
13451 return False;
13452 end Try_User_Defined_Literal;
13453
13454 -------------------------------------------
13455 -- Try_User_Defined_Literal_For_Operator --
13456 -------------------------------------------
13457
13458 function Try_User_Defined_Literal_For_Operator
13459 (N : Node_Id;
13460 Typ : Entity_Id) return Boolean
13461 is
13462 begin
13463 if Nkind (N) in N_Op_Add
13464 | N_Op_Divide
13465 | N_Op_Mod
13466 | N_Op_Multiply
13467 | N_Op_Rem
13468 | N_Op_Subtract
13469 then
13470 -- Both operands must have the same type as the context
13471 -- (ignoring for now fixed-point and exponentiation ops).
13472
13473 if Has_Applicable_User_Defined_Literal (Right_Opnd (N), Typ)
13474 or else (Nkind (Left_Opnd (N)) in N_Op
13475 and then Covers (Typ, Etype (Right_Opnd (N))))
13476 then
13477 Resolve (Left_Opnd (N), Typ);
13478 Analyze_And_Resolve (N, Typ);
13479 return True;
13480
13481 elsif Has_Applicable_User_Defined_Literal (Left_Opnd (N), Typ)
13482 or else (Nkind (Right_Opnd (N)) in N_Op
13483 and then Covers (Typ, Etype (Left_Opnd (N))))
13484 then
13485 Resolve (Right_Opnd (N), Typ);
13486 Analyze_And_Resolve (N, Typ);
13487 return True;
13488 end if;
13489
13490 elsif Nkind (N) in N_Binary_Op then
13491 -- For other binary operators the context does not impose a type on
13492 -- the operands, but their types must match.
13493
13494 if Nkind (Left_Opnd (N))
13495 not in N_Integer_Literal | N_String_Literal | N_Real_Literal
13496 and then
13497 Has_Applicable_User_Defined_Literal
13498 (Right_Opnd (N), Etype (Left_Opnd (N)))
13499 then
13500 Analyze_And_Resolve (N, Typ);
13501 return True;
13502
13503 elsif Nkind (Right_Opnd (N))
13504 not in N_Integer_Literal | N_String_Literal | N_Real_Literal
13505 and then
13506 Has_Applicable_User_Defined_Literal
13507 (Left_Opnd (N), Etype (Right_Opnd (N)))
13508 then
13509 Analyze_And_Resolve (N, Typ);
13510 return True;
13511 end if;
13512
13513 elsif Nkind (N) in N_Unary_Op
13514 and then Has_Applicable_User_Defined_Literal (Right_Opnd (N), Typ)
13515 then
13516 Analyze_And_Resolve (N, Typ);
13517 return True;
13518 end if;
13519
13520 return False;
13521 end Try_User_Defined_Literal_For_Operator;
13522
13523 -----------------------------
13524 -- Unique_Fixed_Point_Type --
13525 -----------------------------
13526
13527 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
13528 procedure Fixed_Point_Error (T1 : Entity_Id; T2 : Entity_Id);
13529 -- Give error messages for true ambiguity. Messages are posted on node
13530 -- N, and entities T1, T2 are the possible interpretations.
13531
13532 -----------------------
13533 -- Fixed_Point_Error --
13534 -----------------------
13535
13536 procedure Fixed_Point_Error (T1 : Entity_Id; T2 : Entity_Id) is
13537 begin
13538 Error_Msg_N ("ambiguous universal_fixed_expression", N);
13539 Error_Msg_NE ("\\possible interpretation as}", N, T1);
13540 Error_Msg_NE ("\\possible interpretation as}", N, T2);
13541 end Fixed_Point_Error;
13542
13543 -- Local variables
13544
13545 ErrN : Node_Id;
13546 Item : Node_Id;
13547 Scop : Entity_Id;
13548 T1 : Entity_Id;
13549 T2 : Entity_Id;
13550
13551 -- Start of processing for Unique_Fixed_Point_Type
13552
13553 begin
13554 -- The operations on Duration are visible, so Duration is always a
13555 -- possible interpretation.
13556
13557 T1 := Standard_Duration;
13558
13559 -- Look for fixed-point types in enclosing scopes
13560
13561 Scop := Current_Scope;
13562 while Scop /= Standard_Standard loop
13563 T2 := First_Entity (Scop);
13564 while Present (T2) loop
13565 if Is_Fixed_Point_Type (T2)
13566 and then Current_Entity (T2) = T2
13567 and then Scope (Base_Type (T2)) = Scop
13568 then
13569 if Present (T1) then
13570 Fixed_Point_Error (T1, T2);
13571 return Any_Type;
13572 else
13573 T1 := T2;
13574 end if;
13575 end if;
13576
13577 Next_Entity (T2);
13578 end loop;
13579
13580 Scop := Scope (Scop);
13581 end loop;
13582
13583 -- Look for visible fixed type declarations in the context
13584
13585 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
13586 while Present (Item) loop
13587 if Nkind (Item) = N_With_Clause then
13588 Scop := Entity (Name (Item));
13589 T2 := First_Entity (Scop);
13590 while Present (T2) loop
13591 if Is_Fixed_Point_Type (T2)
13592 and then Scope (Base_Type (T2)) = Scop
13593 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
13594 then
13595 if Present (T1) then
13596 Fixed_Point_Error (T1, T2);
13597 return Any_Type;
13598 else
13599 T1 := T2;
13600 end if;
13601 end if;
13602
13603 Next_Entity (T2);
13604 end loop;
13605 end if;
13606
13607 Next (Item);
13608 end loop;
13609
13610 if Nkind (N) = N_Real_Literal then
13611 Error_Msg_NE ("??real literal interpreted as }!", N, T1);
13612
13613 else
13614 -- When the context is a type conversion, issue the warning on the
13615 -- expression of the conversion because it is the actual operation.
13616
13617 if Nkind (N) in N_Type_Conversion | N_Unchecked_Type_Conversion then
13618 ErrN := Expression (N);
13619 else
13620 ErrN := N;
13621 end if;
13622
13623 Error_Msg_NE
13624 ("??universal_fixed expression interpreted as }!", ErrN, T1);
13625 end if;
13626
13627 return T1;
13628 end Unique_Fixed_Point_Type;
13629
13630 --------------------------
13631 -- Is_Ambiguous_Operand --
13632 --------------------------
13633
13634 function Is_Ambiguous_Operand
13635 (Operand : Node_Id;
13636 In_Interp_Expr : Boolean := False;
13637 Report_Errors : Boolean := True) return Boolean
13638 is
13639 I : Interp_Index;
13640 I1 : Interp_Index;
13641 It : Interp;
13642 It1 : Interp;
13643 N1 : Entity_Id;
13644 Opnd_Type : Entity_Id;
13645 T1 : Entity_Id;
13646
13647 begin
13648 pragma Assert (Is_Overloaded (Operand));
13649
13650 -- Procedure calls are not valid in this context, but were not removed
13651 -- by prior type-checking because the context does not impose a specific
13652 -- type. Remove them now.
13653
13654 -- The node may be labelled overloaded, but still contain only one
13655 -- interpretation because others were discarded earlier. If this is
13656 -- the case, retain the single interpretation if legal.
13657
13658 Get_First_Interp (Operand, I, It);
13659 Opnd_Type := It.Typ;
13660 Get_Next_Interp (I, It);
13661
13662 if Present (It.Typ)
13663 and then Opnd_Type /= Standard_Void_Type
13664 then
13665 -- More than one candidate interpretation is available
13666
13667 Get_First_Interp (Operand, I, It);
13668 while Present (It.Nam) loop
13669 if It.Typ = Standard_Void_Type then
13670 Remove_Interp (I);
13671 end if;
13672
13673 -- When compiling for a system where Address is of a visible
13674 -- integer type, spurious ambiguities can be produced when
13675 -- arithmetic operations have a literal operand and return
13676 -- System.Address or a descendant of it. These ambiguities
13677 -- are usually resolved by the context, but for conversions
13678 -- there is no context type and the removal of the spurious
13679 -- operations must be done explicitly here.
13680
13681 if not Address_Is_Private
13682 and then Is_Descendant_Of_Address (It.Typ)
13683 then
13684 Remove_Interp (I);
13685 end if;
13686
13687 Get_Next_Interp (I, It);
13688 end loop;
13689 end if;
13690
13691 Get_First_Interp (Operand, I, It);
13692
13693 if No (It.Nam) then
13694 if In_Interp_Expr then
13695 Report_Error_N
13696 ("illegal expression", Operand, Report_Errors);
13697 else
13698 Report_Error_N
13699 ("illegal operand in conversion", Operand, Report_Errors);
13700 end if;
13701
13702 return True;
13703 end if;
13704
13705 I1 := I;
13706 It1 := It;
13707
13708 -- The node may be labeled overloaded, but still contain only
13709 -- one interpretation because others were discarded earlier.
13710
13711 Get_Next_Interp (I, It);
13712
13713 if Present (It.Typ) then
13714 N1 := It1.Nam;
13715 T1 := It1.Typ;
13716 It1 := Disambiguate (Operand, I1, I, Any_Type);
13717
13718 if It1 = No_Interp then
13719 if In_Interp_Expr then
13720 Report_Error_NE
13721 ("ambiguous call to&", Operand, Entity (Name (Operand)),
13722 Report_Errors);
13723 else
13724 Report_Error_N
13725 ("ambiguous operand in conversion", Operand, Report_Errors);
13726 end if;
13727
13728 -- Report the first two interpretations
13729
13730 Report_Interpretation (Operand, It.Nam, It.Typ);
13731 Report_Interpretation (Operand, N1, T1);
13732
13733 return True;
13734 end if;
13735 end if;
13736
13737 Set_Etype (Operand, It1.Typ);
13738 return False;
13739 end Is_Ambiguous_Operand;
13740
13741 ----------------------
13742 -- Valid_Conversion --
13743 ----------------------
13744
13745 function Valid_Conversion
13746 (N : Node_Id;
13747 Target : Entity_Id;
13748 Operand : Node_Id;
13749 Report_Errs : Boolean := True) return Boolean
13750 is
13751 Target_Type : constant Entity_Id := Base_Type (Target);
13752 Opnd_Type : Entity_Id;
13753 Inc_Ancestor : Entity_Id;
13754
13755 function Conversion_Check
13756 (Valid : Boolean;
13757 Msg : String) return Boolean;
13758 -- Little routine to post Msg if Valid is False, returns Valid value
13759
13760 function In_Instance_Code return Boolean;
13761 -- Return True if expression is within an instance but is not in one of
13762 -- the actuals of the instantiation. Type conversions within an instance
13763 -- are not rechecked because type visibility may lead to spurious errors
13764 -- but conversions in an actual for a formal object must be checked.
13765
13766 function Is_Discrim_Of_Bad_Access_Conversion_Argument
13767 (Expr : Node_Id) return Boolean;
13768 -- Implicit anonymous-to-named access type conversions are not allowed
13769 -- if the "statically deeper than" relationship does not apply to the
13770 -- type of the conversion operand. See RM 8.6(28.1) and AARM 8.6(28.d).
13771 -- We deal with most such cases elsewhere so that we can emit more
13772 -- specific error messages (e.g., if the operand is an access parameter
13773 -- or a saooaaat (stand-alone object of an anonymous access type)), but
13774 -- here is where we catch the case where the operand is an access
13775 -- discriminant selected from a dereference of another such "bad"
13776 -- conversion argument.
13777
13778 function Valid_Tagged_Conversion
13779 (Target_Type : Entity_Id;
13780 Opnd_Type : Entity_Id) return Boolean;
13781 -- Specifically test for validity of tagged conversions
13782
13783 function Valid_Array_Conversion return Boolean;
13784 -- Check index and component conformance, and accessibility levels if
13785 -- the component types are anonymous access types (Ada 2005).
13786
13787 ----------------------
13788 -- Conversion_Check --
13789 ----------------------
13790
13791 function Conversion_Check
13792 (Valid : Boolean;
13793 Msg : String) return Boolean
13794 is
13795 begin
13796 if not Valid
13797
13798 -- A generic unit has already been analyzed and we have verified
13799 -- that a particular conversion is OK in that context. Since the
13800 -- instance is reanalyzed without relying on the relationships
13801 -- established during the analysis of the generic, it is possible
13802 -- to end up with inconsistent views of private types. Do not emit
13803 -- the error message in such cases. The rest of the machinery in
13804 -- Valid_Conversion still ensures the proper compatibility of
13805 -- target and operand types.
13806
13807 and then not In_Instance_Code
13808 then
13809 Report_Error_N (Msg, Operand, Report_Errs);
13810 end if;
13811
13812 return Valid;
13813 end Conversion_Check;
13814
13815 ----------------------
13816 -- In_Instance_Code --
13817 ----------------------
13818
13819 function In_Instance_Code return Boolean is
13820 Par : Node_Id;
13821
13822 begin
13823 if not In_Instance then
13824 return False;
13825
13826 else
13827 Par := Parent (N);
13828 while Present (Par) loop
13829
13830 -- The expression is part of an actual object if it appears in
13831 -- the generated object declaration in the instance.
13832
13833 if Nkind (Par) = N_Object_Declaration
13834 and then Present (Corresponding_Generic_Association (Par))
13835 then
13836 return False;
13837
13838 else
13839 exit when
13840 Nkind (Par) in N_Statement_Other_Than_Procedure_Call
13841 or else Nkind (Par) in N_Subprogram_Call
13842 or else Nkind (Par) in N_Declaration;
13843 end if;
13844
13845 Par := Parent (Par);
13846 end loop;
13847
13848 -- Otherwise the expression appears within the instantiated unit
13849
13850 return True;
13851 end if;
13852 end In_Instance_Code;
13853
13854 --------------------------------------------------
13855 -- Is_Discrim_Of_Bad_Access_Conversion_Argument --
13856 --------------------------------------------------
13857
13858 function Is_Discrim_Of_Bad_Access_Conversion_Argument
13859 (Expr : Node_Id) return Boolean
13860 is
13861 Exp_Type : Entity_Id := Base_Type (Etype (Expr));
13862 pragma Assert (Is_Access_Type (Exp_Type));
13863
13864 Associated_Node : Node_Id;
13865 Deref_Prefix : Node_Id;
13866 begin
13867 if not Is_Anonymous_Access_Type (Exp_Type) then
13868 return False;
13869 end if;
13870
13871 pragma Assert (Is_Itype (Exp_Type));
13872 Associated_Node := Associated_Node_For_Itype (Exp_Type);
13873
13874 if Nkind (Associated_Node) /= N_Discriminant_Specification then
13875 return False; -- not the type of an access discriminant
13876 end if;
13877
13878 -- return False if Expr not of form <prefix>.all.Some_Component
13879
13880 if Nkind (Expr) /= N_Selected_Component
13881 or else Nkind (Prefix (Expr)) /= N_Explicit_Dereference
13882 then
13883 -- conditional expressions, declare expressions ???
13884 return False;
13885 end if;
13886
13887 Deref_Prefix := Prefix (Prefix (Expr));
13888 Exp_Type := Base_Type (Etype (Deref_Prefix));
13889
13890 -- The "statically deeper relationship" does not apply
13891 -- to generic formal access types, so a prefix of such
13892 -- a type is a "bad" prefix.
13893
13894 if Is_Generic_Formal (Exp_Type) then
13895 return True;
13896
13897 -- The "statically deeper relationship" does apply to
13898 -- any other named access type.
13899
13900 elsif not Is_Anonymous_Access_Type (Exp_Type) then
13901 return False;
13902 end if;
13903
13904 pragma Assert (Is_Itype (Exp_Type));
13905 Associated_Node := Associated_Node_For_Itype (Exp_Type);
13906
13907 -- The "statically deeper relationship" applies to some
13908 -- anonymous access types and not to others. Return
13909 -- True for the cases where it does not apply. Also check
13910 -- recursively for the
13911 -- <prefix>.all.Access_Discrim.all.Access_Discrim case,
13912 -- where the correct result depends on <prefix>.
13913
13914 return Nkind (Associated_Node) in
13915 N_Procedure_Specification | -- access parameter
13916 N_Function_Specification | -- access parameter
13917 N_Object_Declaration -- saooaaat
13918 or else Is_Discrim_Of_Bad_Access_Conversion_Argument (Deref_Prefix);
13919 end Is_Discrim_Of_Bad_Access_Conversion_Argument;
13920
13921 ----------------------------
13922 -- Valid_Array_Conversion --
13923 ----------------------------
13924
13925 function Valid_Array_Conversion return Boolean is
13926 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
13927 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
13928
13929 Opnd_Index : Node_Id;
13930 Opnd_Index_Type : Entity_Id;
13931
13932 Target_Comp_Type : constant Entity_Id :=
13933 Component_Type (Target_Type);
13934 Target_Comp_Base : constant Entity_Id :=
13935 Base_Type (Target_Comp_Type);
13936
13937 Target_Index : Node_Id;
13938 Target_Index_Type : Entity_Id;
13939
13940 begin
13941 -- Error if wrong number of dimensions
13942
13943 if
13944 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
13945 then
13946 Report_Error_N
13947 ("incompatible number of dimensions for conversion", Operand,
13948 Report_Errs);
13949 return False;
13950
13951 -- Number of dimensions matches
13952
13953 else
13954 -- Loop through indexes of the two arrays
13955
13956 Target_Index := First_Index (Target_Type);
13957 Opnd_Index := First_Index (Opnd_Type);
13958 while Present (Target_Index) and then Present (Opnd_Index) loop
13959 Target_Index_Type := Etype (Target_Index);
13960 Opnd_Index_Type := Etype (Opnd_Index);
13961
13962 -- Error if index types are incompatible
13963
13964 if not (Is_Integer_Type (Target_Index_Type)
13965 and then Is_Integer_Type (Opnd_Index_Type))
13966 and then Root_Type (Target_Index_Type)
13967 /= Root_Type (Opnd_Index_Type)
13968 then
13969 Report_Error_N
13970 ("incompatible index types for array conversion",
13971 Operand, Report_Errs);
13972 return False;
13973 end if;
13974
13975 Next_Index (Target_Index);
13976 Next_Index (Opnd_Index);
13977 end loop;
13978
13979 -- If component types have same base type, all set
13980
13981 if Target_Comp_Base = Opnd_Comp_Base then
13982 null;
13983
13984 -- Here if base types of components are not the same. The only
13985 -- time this is allowed is if we have anonymous access types.
13986
13987 -- The conversion of arrays of anonymous access types can lead
13988 -- to dangling pointers. AI-392 formalizes the accessibility
13989 -- checks that must be applied to such conversions to prevent
13990 -- out-of-scope references.
13991
13992 elsif Ekind (Target_Comp_Base) in
13993 E_Anonymous_Access_Type
13994 | E_Anonymous_Access_Subprogram_Type
13995 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
13996 and then
13997 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
13998 then
13999 if Type_Access_Level (Target_Type) <
14000 Deepest_Type_Access_Level (Opnd_Type)
14001 then
14002 if In_Instance_Body then
14003 Error_Msg_Warn := SPARK_Mode /= On;
14004 Report_Error_N
14005 ("source array type has deeper accessibility "
14006 & "level than target<<", Operand, Report_Errs);
14007 Report_Error_N
14008 ("\Program_Error [<<", Operand, Report_Errs);
14009 Rewrite (N,
14010 Make_Raise_Program_Error (Sloc (N),
14011 Reason => PE_Accessibility_Check_Failed));
14012 Set_Etype (N, Target_Type);
14013 return False;
14014
14015 -- Conversion not allowed because of accessibility levels
14016
14017 else
14018 Report_Error_N
14019 ("source array type has deeper accessibility "
14020 & "level than target", Operand, Report_Errs);
14021 return False;
14022 end if;
14023
14024 else
14025 null;
14026 end if;
14027
14028 -- All other cases where component base types do not match
14029
14030 else
14031 Report_Error_N
14032 ("incompatible component types for array conversion",
14033 Operand, Report_Errs);
14034 return False;
14035 end if;
14036
14037 -- Check that component subtypes statically match. For numeric
14038 -- types this means that both must be either constrained or
14039 -- unconstrained. For enumeration types the bounds must match.
14040 -- All of this is checked in Subtypes_Statically_Match.
14041
14042 if not Subtypes_Statically_Match
14043 (Target_Comp_Type, Opnd_Comp_Type)
14044 then
14045 Report_Error_N
14046 ("component subtypes must statically match", Operand,
14047 Report_Errs);
14048 return False;
14049 end if;
14050 end if;
14051
14052 return True;
14053 end Valid_Array_Conversion;
14054
14055 -----------------------------
14056 -- Valid_Tagged_Conversion --
14057 -----------------------------
14058
14059 function Valid_Tagged_Conversion
14060 (Target_Type : Entity_Id;
14061 Opnd_Type : Entity_Id) return Boolean
14062 is
14063 begin
14064 -- Upward conversions are allowed (RM 4.6(22))
14065
14066 if Covers (Target_Type, Opnd_Type)
14067 or else Is_Ancestor (Target_Type, Opnd_Type)
14068 then
14069 return True;
14070
14071 -- Downward conversion are allowed if the operand is class-wide
14072 -- (RM 4.6(23)).
14073
14074 elsif Is_Class_Wide_Type (Opnd_Type)
14075 and then Covers (Opnd_Type, Target_Type)
14076 then
14077 return True;
14078
14079 elsif Covers (Opnd_Type, Target_Type)
14080 or else Is_Ancestor (Opnd_Type, Target_Type)
14081 then
14082 -- Deal with non-extension derivation involving an
14083 -- untagged view of a tagged type.
14084
14085 if not Is_Tagged_Type (Target_Type) then
14086 return True;
14087 end if;
14088
14089 return
14090 Conversion_Check (False,
14091 "downward conversion of tagged objects not allowed");
14092
14093 -- Ada 2005 (AI-251): A conversion is valid if the operand and target
14094 -- types are both class-wide types and the specific type associated
14095 -- with at least one of them is an interface type (RM 4.6 (23.1/2));
14096 -- at run-time a check will verify the validity of this interface
14097 -- type conversion.
14098
14099 elsif Is_Class_Wide_Type (Target_Type)
14100 and then Is_Class_Wide_Type (Opnd_Type)
14101 and then (Is_Interface (Target_Type)
14102 or else Is_Interface (Opnd_Type))
14103 then
14104 return True;
14105
14106 -- Report errors
14107
14108 elsif Is_Class_Wide_Type (Target_Type)
14109 and then Is_Interface (Target_Type)
14110 and then not Is_Interface (Opnd_Type)
14111 and then not Interface_Present_In_Ancestor
14112 (Typ => Opnd_Type,
14113 Iface => Target_Type)
14114 then
14115 Error_Msg_Name_1 := Chars (Etype (Target_Type));
14116 Error_Msg_Name_2 := Chars (Opnd_Type);
14117 Report_Error_N
14118 ("wrong interface conversion (% is not a progenitor "
14119 & "of %)", N, Report_Errs);
14120 return False;
14121
14122 elsif Is_Class_Wide_Type (Opnd_Type)
14123 and then Is_Interface (Opnd_Type)
14124 and then not Is_Interface (Target_Type)
14125 and then not Interface_Present_In_Ancestor
14126 (Typ => Target_Type,
14127 Iface => Opnd_Type)
14128 then
14129 Error_Msg_Name_1 := Chars (Etype (Opnd_Type));
14130 Error_Msg_Name_2 := Chars (Target_Type);
14131 Report_Error_N
14132 ("wrong interface conversion (% is not a progenitor "
14133 & "of %)", N, Report_Errs);
14134
14135 -- Search for interface types shared between the target type and
14136 -- the operand interface type to complete the text of the error
14137 -- since the source of this error is a missing type conversion
14138 -- to such interface type.
14139
14140 if Has_Interfaces (Target_Type) then
14141 declare
14142 Operand_Ifaces_List : Elist_Id;
14143 Operand_Iface_Elmt : Elmt_Id;
14144 Target_Ifaces_List : Elist_Id;
14145 Target_Iface_Elmt : Elmt_Id;
14146 First_Candidate : Boolean := True;
14147
14148 begin
14149 Collect_Interfaces (Base_Type (Target_Type),
14150 Target_Ifaces_List);
14151 Collect_Interfaces (Root_Type (Base_Type (Opnd_Type)),
14152 Operand_Ifaces_List);
14153
14154 Operand_Iface_Elmt := First_Elmt (Operand_Ifaces_List);
14155 while Present (Operand_Iface_Elmt) loop
14156 Target_Iface_Elmt := First_Elmt (Target_Ifaces_List);
14157 while Present (Target_Iface_Elmt) loop
14158 if Node (Operand_Iface_Elmt)
14159 = Node (Target_Iface_Elmt)
14160 then
14161 Error_Msg_Name_1 :=
14162 Chars (Node (Target_Iface_Elmt));
14163
14164 if First_Candidate then
14165 First_Candidate := False;
14166 Report_Error_N
14167 ("\must convert to `%''Class` before downward "
14168 & "conversion", Operand, Report_Errs);
14169 else
14170 Report_Error_N
14171 ("\or must convert to `%''Class` before "
14172 & "downward conversion",
14173 Operand, Report_Errs);
14174 end if;
14175 end if;
14176
14177 Next_Elmt (Target_Iface_Elmt);
14178 end loop;
14179
14180 Next_Elmt (Operand_Iface_Elmt);
14181 end loop;
14182 end;
14183 end if;
14184
14185 return False;
14186
14187 elsif not Is_Class_Wide_Type (Target_Type)
14188 and then Is_Interface (Target_Type)
14189 then
14190 Report_Error_N
14191 ("wrong use of interface type in tagged conversion",
14192 N, Report_Errs);
14193 Report_Error_N
14194 ("\add ''Class to the target interface type",
14195 N, Report_Errs);
14196 return False;
14197
14198 elsif not Is_Class_Wide_Type (Opnd_Type)
14199 and then Is_Interface (Opnd_Type)
14200 then
14201 Report_Error_N
14202 ("must convert to class-wide interface type before downward "
14203 & "conversion", Operand, Report_Errs);
14204 return False;
14205
14206 else
14207 Report_Error_NE
14208 ("invalid tagged conversion, not compatible with}",
14209 N, First_Subtype (Opnd_Type), Report_Errs);
14210 return False;
14211 end if;
14212 end Valid_Tagged_Conversion;
14213
14214 -- Start of processing for Valid_Conversion
14215
14216 begin
14217 Check_Parameterless_Call (Operand);
14218
14219 if Is_Overloaded (Operand)
14220 and then Is_Ambiguous_Operand (Operand)
14221 then
14222 return False;
14223 end if;
14224
14225 -- When we encounter a class-wide equivalent type used to represent
14226 -- a fully sized mutably tagged type, pretend we are actually looking
14227 -- at the class-wide mutably tagged type instead.
14228
14229 Opnd_Type :=
14230 Get_Corresponding_Mutably_Tagged_Type_If_Present (Etype (Operand));
14231
14232 -- Deal with conversion of integer type to address if the pragma
14233 -- Allow_Integer_Address is in effect. We convert the conversion to
14234 -- an unchecked conversion in this case and we are all done.
14235
14236 if Address_Integer_Convert_OK (Opnd_Type, Target_Type) then
14237 Rewrite (N, Unchecked_Convert_To (Target_Type, Expression (N)));
14238 Analyze_And_Resolve (N, Target_Type);
14239 return True;
14240 end if;
14241
14242 -- If we are within a child unit, check whether the type of the
14243 -- expression has an ancestor in a parent unit, in which case it
14244 -- belongs to its derivation class even if the ancestor is private.
14245 -- See RM 7.3.1 (5.2/3).
14246
14247 Inc_Ancestor := Get_Incomplete_View_Of_Ancestor (Opnd_Type);
14248
14249 -- Numeric types
14250
14251 if Is_Numeric_Type (Target_Type) then
14252
14253 -- A universal fixed expression can be converted to any numeric type
14254
14255 if Opnd_Type = Universal_Fixed then
14256 return True;
14257
14258 -- Also no need to check when in an instance or inlined body, because
14259 -- the legality has been established when the template was analyzed.
14260 -- Furthermore, numeric conversions may occur where only a private
14261 -- view of the operand type is visible at the instantiation point.
14262 -- This results in a spurious error if we check that the operand type
14263 -- is a numeric type.
14264
14265 -- Note: in a previous version of this unit, the following tests were
14266 -- applied only for generated code (Comes_From_Source set to False),
14267 -- but in fact the test is required for source code as well, since
14268 -- this situation can arise in source code.
14269
14270 elsif In_Instance_Code or else In_Inlined_Body then
14271 return True;
14272
14273 -- Otherwise we need the conversion check
14274
14275 else
14276 return Conversion_Check
14277 (Is_Numeric_Type (Opnd_Type)
14278 or else
14279 (Present (Inc_Ancestor)
14280 and then Is_Numeric_Type (Inc_Ancestor)),
14281 "illegal operand for numeric conversion");
14282 end if;
14283
14284 -- Array types
14285
14286 elsif Is_Array_Type (Target_Type) then
14287 if not Is_Array_Type (Opnd_Type)
14288 or else Opnd_Type = Any_Composite
14289 or else Opnd_Type = Any_String
14290 then
14291 if not Comes_From_Source (N)
14292 and then Implementation_Base_Type (Target_Type) =
14293 Implementation_Base_Type (Opnd_Type)
14294 then
14295 return True;
14296 end if;
14297
14298 Report_Error_N
14299 ("illegal operand for array conversion", Operand, Report_Errs);
14300 return False;
14301
14302 else
14303 return Valid_Array_Conversion;
14304 end if;
14305
14306 -- Ada 2005 (AI-251): Internally generated conversions of access to
14307 -- interface types added to force the displacement of the pointer to
14308 -- reference the corresponding dispatch table.
14309
14310 elsif not Comes_From_Source (N)
14311 and then Is_Access_Type (Target_Type)
14312 and then Is_Interface (Designated_Type (Target_Type))
14313 then
14314 return True;
14315
14316 -- Ada 2005 (AI-251): Anonymous access types where target references an
14317 -- interface type.
14318
14319 elsif Is_Access_Type (Opnd_Type)
14320 and then Ekind (Target_Type) in
14321 E_General_Access_Type | E_Anonymous_Access_Type
14322 and then Is_Interface (Directly_Designated_Type (Target_Type))
14323 then
14324 -- Check the static accessibility rule of 4.6(17). Note that the
14325 -- check is not enforced when within an instance body, since the
14326 -- RM requires such cases to be caught at run time.
14327
14328 -- If the operand is a rewriting of an allocator no check is needed
14329 -- because there are no accessibility issues.
14330
14331 if Nkind (Original_Node (N)) = N_Allocator then
14332 null;
14333
14334 elsif Ekind (Target_Type) /= E_Anonymous_Access_Type then
14335 if Type_Access_Level (Opnd_Type) >
14336 Deepest_Type_Access_Level (Target_Type)
14337 then
14338 -- In an instance, this is a run-time check, but one we know
14339 -- will fail, so generate an appropriate warning. The raise
14340 -- will be generated by Expand_N_Type_Conversion.
14341
14342 if In_Instance_Body then
14343 Error_Msg_Warn := SPARK_Mode /= On;
14344 Report_Error_N
14345 ("cannot convert local pointer to non-local access type<<",
14346 Operand, Report_Errs);
14347 Report_Error_N ("\Program_Error [<<", Operand, Report_Errs);
14348
14349 else
14350 Report_Error_N
14351 ("cannot convert local pointer to non-local access type",
14352 Operand, Report_Errs);
14353 return False;
14354 end if;
14355
14356 -- Special accessibility checks are needed in the case of access
14357 -- discriminants declared for a limited type.
14358
14359 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
14360 and then not Is_Local_Anonymous_Access (Opnd_Type)
14361 then
14362 -- When the operand is a selected access discriminant the check
14363 -- needs to be made against the level of the object denoted by
14364 -- the prefix of the selected name (Accessibility_Level handles
14365 -- checking the prefix of the operand for this case).
14366
14367 if Nkind (Operand) = N_Selected_Component
14368 and then Static_Accessibility_Level
14369 (Operand, Zero_On_Dynamic_Level)
14370 > Deepest_Type_Access_Level (Target_Type)
14371 then
14372 -- In an instance, this is a run-time check, but one we know
14373 -- will fail, so generate an appropriate warning. The raise
14374 -- will be generated by Expand_N_Type_Conversion.
14375
14376 if In_Instance_Body then
14377 Error_Msg_Warn := SPARK_Mode /= On;
14378 Report_Error_N
14379 ("cannot convert access discriminant to non-local "
14380 & "access type<<", Operand, Report_Errs);
14381 Report_Error_N
14382 ("\Program_Error [<<", Operand, Report_Errs);
14383
14384 -- Real error if not in instance body
14385
14386 else
14387 Report_Error_N
14388 ("cannot convert access discriminant to non-local "
14389 & "access type", Operand, Report_Errs);
14390 return False;
14391 end if;
14392 end if;
14393
14394 -- The case of a reference to an access discriminant from
14395 -- within a limited type declaration (which will appear as
14396 -- a discriminal) is always illegal because the level of the
14397 -- discriminant is considered to be deeper than any (nameable)
14398 -- access type.
14399
14400 if Is_Entity_Name (Operand)
14401 and then not Is_Local_Anonymous_Access (Opnd_Type)
14402 and then
14403 Ekind (Entity (Operand)) in E_In_Parameter | E_Constant
14404 and then Present (Discriminal_Link (Entity (Operand)))
14405 then
14406 Report_Error_N
14407 ("discriminant has deeper accessibility level than target",
14408 Operand, Report_Errs);
14409 return False;
14410 end if;
14411 end if;
14412 end if;
14413
14414 return True;
14415
14416 -- General and anonymous access types
14417
14418 elsif Ekind (Target_Type) in
14419 E_General_Access_Type | E_Anonymous_Access_Type
14420 and then
14421 Conversion_Check
14422 (Is_Access_Type (Opnd_Type)
14423 and then
14424 Ekind (Opnd_Type) not in
14425 E_Access_Subprogram_Type |
14426 E_Access_Protected_Subprogram_Type,
14427 "must be an access-to-object type")
14428 then
14429 if Is_Access_Constant (Opnd_Type)
14430 and then not Is_Access_Constant (Target_Type)
14431 then
14432 Report_Error_N
14433 ("access-to-constant operand type not allowed",
14434 Operand, Report_Errs);
14435 return False;
14436 end if;
14437
14438 declare
14439 Extended_Opnd : constant Boolean :=
14440 Is_Extended_Access_Type (Opnd_Type);
14441 Extended_Target : constant Boolean :=
14442 Is_Extended_Access_Type (Target_Type);
14443 begin
14444 -- An extended access value may designate objects that are
14445 -- impossible to reference using a non-extended type, so
14446 -- prohibit conversions that would require being able to
14447 -- do the impossible.
14448
14449 if Extended_Opnd then
14450 if not Extended_Target then
14451 Report_Error_N
14452 ("cannot convert extended access value"
14453 & " to non-extended access type",
14454 Operand, Report_Errs);
14455 return False;
14456 end if;
14457
14458 -- Detect bad conversion on copy back for a view conversion
14459
14460 elsif Extended_Target and then Is_View_Conversion (N) then
14461 Report_Error_N
14462 ("cannot convert non-extended value"
14463 & " to extended access type in view conversion",
14464 Operand);
14465 return False;
14466 end if;
14467 end;
14468
14469 -- Check the static accessibility rule of 4.6(17). Note that the
14470 -- check is not enforced when within an instance body, since the RM
14471 -- requires such cases to be caught at run time.
14472
14473 if Ekind (Target_Type) /= E_Anonymous_Access_Type
14474 or else Is_Local_Anonymous_Access (Target_Type)
14475 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
14476 N_Object_Declaration
14477 then
14478 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
14479 -- conversions from an anonymous access type to a named general
14480 -- access type. Such conversions are not allowed in the case of
14481 -- access parameters and stand-alone objects of an anonymous
14482 -- access type. The implicit conversion case is recognized by
14483 -- testing that Comes_From_Source is False and that it's been
14484 -- rewritten. The Comes_From_Source test isn't sufficient because
14485 -- nodes in inlined calls to predefined library routines can have
14486 -- Comes_From_Source set to False. (Is there a better way to test
14487 -- for implicit conversions???).
14488 --
14489 -- Do not treat a rewritten 'Old attribute reference like other
14490 -- rewrite substitutions. This makes a difference, for example,
14491 -- in the case where we are generating the expansion of a
14492 -- membership test of the form
14493 -- Saooaaat'Old in Named_Access_Type
14494 -- because in this case Valid_Conversion needs to return True
14495 -- (otherwise the expansion will be False - see the call site
14496 -- in exp_ch4.adb).
14497
14498 if Ada_Version >= Ada_2012
14499 and then not Comes_From_Source (N)
14500 and then Is_Rewrite_Substitution (N)
14501 and then not Is_Attribute_Old (Original_Node (N))
14502 and then Ekind (Base_Type (Target_Type)) = E_General_Access_Type
14503 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
14504 then
14505 if Is_Itype (Opnd_Type) then
14506
14507 -- When applying restriction No_Dynamic_Accessibility_Check,
14508 -- implicit conversions are allowed when the operand type is
14509 -- not deeper than the target type.
14510
14511 if No_Dynamic_Accessibility_Checks_Enabled (N) then
14512 if Type_Access_Level (Opnd_Type)
14513 > Deepest_Type_Access_Level (Target_Type)
14514 then
14515 Report_Error_N
14516 ("operand has deeper level than target", Operand,
14517 Report_Errs);
14518 return False;
14519 end if;
14520
14521 -- Implicit conversions aren't allowed for objects of an
14522 -- anonymous access type, since such objects have nonstatic
14523 -- levels in Ada 2012.
14524
14525 elsif Nkind (Associated_Node_For_Itype (Opnd_Type))
14526 = N_Object_Declaration
14527 then
14528 Report_Error_N
14529 ("implicit conversion of stand-alone anonymous "
14530 & "access object not allowed", Operand, Report_Errs);
14531 return False;
14532
14533 -- Implicit conversions aren't allowed for anonymous access
14534 -- parameters. We exclude anonymous access results as well
14535 -- as universal_access "=".
14536
14537 elsif not Is_Local_Anonymous_Access (Opnd_Type)
14538 and then Nkind (Associated_Node_For_Itype (Opnd_Type)) in
14539 N_Function_Specification |
14540 N_Procedure_Specification
14541 and then Nkind (Parent (N)) not in N_Op_Eq | N_Op_Ne
14542 then
14543 Report_Error_N
14544 ("implicit conversion of anonymous access parameter "
14545 & "not allowed", Operand, Report_Errs);
14546 return False;
14547
14548 -- Detect access discriminant values that are illegal
14549 -- implicit anonymous-to-named access conversion operands.
14550
14551 elsif Is_Discrim_Of_Bad_Access_Conversion_Argument (Operand)
14552 then
14553 Report_Error_N
14554 ("implicit conversion of anonymous access value "
14555 & "not allowed", Operand, Report_Errs);
14556 return False;
14557
14558 -- In other cases, the level of the operand's type must be
14559 -- statically less deep than that of the target type, else
14560 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
14561
14562 elsif Type_Access_Level (Opnd_Type) >
14563 Deepest_Type_Access_Level (Target_Type)
14564 then
14565 Report_Error_N
14566 ("implicit conversion of anonymous access value "
14567 & "violates accessibility", Operand, Report_Errs);
14568 return False;
14569 end if;
14570 end if;
14571
14572 -- Check if the operand is deeper than the target type, taking
14573 -- care to avoid the case where we are converting a result of a
14574 -- function returning an anonymous access type since the "master
14575 -- of the call" would be target type of the conversion unless
14576 -- the target type is anonymous access as well - see RM 3.10.2
14577 -- (10.3/3).
14578
14579 -- Note that when the restriction No_Dynamic_Accessibility_Checks
14580 -- is in effect wei also want to proceed with the conversion check
14581 -- described above.
14582
14583 elsif Type_Access_Level (Opnd_Type, Assoc_Ent => Operand)
14584 > Deepest_Type_Access_Level (Target_Type)
14585 and then (Nkind (Associated_Node_For_Itype (Opnd_Type))
14586 /= N_Function_Specification
14587 or else Ekind (Target_Type) in Anonymous_Access_Kind
14588 or else No_Dynamic_Accessibility_Checks_Enabled (N))
14589
14590 -- Check we are not in a return value ???
14591
14592 and then (not In_Return_Value (N)
14593 or else
14594 Nkind (Associated_Node_For_Itype (Target_Type))
14595 = N_Component_Declaration)
14596 then
14597 -- In an instance, this is a run-time check, but one we know
14598 -- will fail, so generate an appropriate warning. The raise
14599 -- will be generated by Expand_N_Type_Conversion.
14600
14601 if In_Instance_Body then
14602 Error_Msg_Warn := SPARK_Mode /= On;
14603 Report_Error_N
14604 ("cannot convert local pointer to non-local access type<<",
14605 Operand, Report_Errs);
14606 Report_Error_N ("\Program_Error [<<", Operand, Report_Errs);
14607
14608 -- If not in an instance body, this is a real error
14609
14610 else
14611 -- Avoid generation of spurious error message
14612
14613 if not Error_Posted (N) then
14614 Report_Error_N
14615 ("cannot convert local pointer to non-local access type",
14616 Operand, Report_Errs);
14617 end if;
14618
14619 return False;
14620 end if;
14621
14622 -- Special accessibility checks are needed in the case of access
14623 -- discriminants declared for a limited type.
14624
14625 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
14626 and then not Is_Local_Anonymous_Access (Opnd_Type)
14627 then
14628 -- When the operand is a selected access discriminant the check
14629 -- needs to be made against the level of the object denoted by
14630 -- the prefix of the selected name (Accessibility_Level handles
14631 -- checking the prefix of the operand for this case).
14632
14633 if Nkind (Operand) = N_Selected_Component
14634 and then Static_Accessibility_Level
14635 (Operand, Zero_On_Dynamic_Level)
14636 > Deepest_Type_Access_Level (Target_Type)
14637 then
14638 -- In an instance, this is a run-time check, but one we know
14639 -- will fail, so generate an appropriate warning. The raise
14640 -- will be generated by Expand_N_Type_Conversion.
14641
14642 if In_Instance_Body then
14643 Error_Msg_Warn := SPARK_Mode /= On;
14644 Report_Error_N
14645 ("cannot convert access discriminant to non-local "
14646 & "access type<<", Operand, Report_Errs);
14647 Report_Error_N
14648 ("\Program_Error [<<", Operand, Report_Errs);
14649
14650 -- If not in an instance body, this is a real error
14651
14652 else
14653 Report_Error_N
14654 ("cannot convert access discriminant to non-local "
14655 & "access type", Operand, Report_Errs);
14656 return False;
14657 end if;
14658 end if;
14659
14660 -- The case of a reference to an access discriminant from
14661 -- within a limited type declaration (which will appear as
14662 -- a discriminal) is always illegal because the level of the
14663 -- discriminant is considered to be deeper than any (nameable)
14664 -- access type.
14665
14666 if Is_Entity_Name (Operand)
14667 and then
14668 Ekind (Entity (Operand)) in E_In_Parameter | E_Constant
14669 and then Present (Discriminal_Link (Entity (Operand)))
14670 then
14671 Report_Error_N
14672 ("discriminant has deeper accessibility level than target",
14673 Operand, Report_Errs);
14674 return False;
14675 end if;
14676 end if;
14677 end if;
14678
14679 -- In the presence of limited_with clauses we have to use nonlimited
14680 -- views, if available.
14681
14682 Check_Limited : declare
14683 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
14684 -- Helper function to handle limited views
14685
14686 --------------------------
14687 -- Full_Designated_Type --
14688 --------------------------
14689
14690 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
14691 Desig : constant Entity_Id := Designated_Type (T);
14692
14693 begin
14694 -- Handle the limited view of a type
14695
14696 if From_Limited_With (Desig)
14697 and then Has_Non_Limited_View (Desig)
14698 then
14699 return Available_View (Desig);
14700 else
14701 return Desig;
14702 end if;
14703 end Full_Designated_Type;
14704
14705 -- Local Declarations
14706
14707 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
14708 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
14709
14710 Same_Base : constant Boolean :=
14711 Base_Type (Target) = Base_Type (Opnd);
14712
14713 -- Start of processing for Check_Limited
14714
14715 begin
14716 if Is_Tagged_Type (Target) then
14717 return Valid_Tagged_Conversion (Target, Opnd);
14718
14719 else
14720 if not Same_Base then
14721 Report_Error_NE
14722 ("target designated type not compatible with }",
14723 N, Base_Type (Opnd), Report_Errs);
14724 return False;
14725
14726 -- Ada 2005 AI-384: legality rule is symmetric in both
14727 -- designated types. The conversion is legal (with possible
14728 -- constraint check) if either designated type is
14729 -- unconstrained.
14730
14731 elsif Subtypes_Statically_Match (Target, Opnd)
14732 or else
14733 (Has_Discriminants (Target)
14734 and then
14735 (not Is_Constrained (Opnd)
14736 or else not Is_Constrained (Target)))
14737 then
14738 -- Special case, if Value_Size has been used to make the
14739 -- sizes different, the conversion is not allowed even
14740 -- though the subtypes statically match.
14741
14742 if Known_Static_RM_Size (Target)
14743 and then Known_Static_RM_Size (Opnd)
14744 and then RM_Size (Target) /= RM_Size (Opnd)
14745 then
14746 Report_Error_NE
14747 ("target designated subtype not compatible with }",
14748 N, Opnd, Report_Errs);
14749 Report_Error_NE
14750 ("\because sizes of the two designated subtypes differ",
14751 N, Opnd, Report_Errs);
14752 return False;
14753
14754 -- Normal case where conversion is allowed
14755
14756 else
14757 return True;
14758 end if;
14759
14760 else
14761 Error_Msg_NE
14762 ("target designated subtype not compatible with }",
14763 N, Opnd);
14764 return False;
14765 end if;
14766 end if;
14767 end Check_Limited;
14768
14769 -- Access to subprogram types. If the operand is an access parameter,
14770 -- the type has a deeper accessibility that any master, and cannot be
14771 -- assigned. We must make an exception if the conversion is part of an
14772 -- assignment and the target is the return object of an extended return
14773 -- statement, because in that case the accessibility check takes place
14774 -- after the return.
14775
14776 elsif Is_Access_Subprogram_Type (Target_Type)
14777
14778 -- Note: this test of Opnd_Type is there to prevent entering this
14779 -- branch in the case of a remote access to subprogram type, which
14780 -- is internally represented as an E_Record_Type.
14781
14782 and then Is_Access_Type (Opnd_Type)
14783 then
14784 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
14785 and then Is_Entity_Name (Operand)
14786 and then Ekind (Entity (Operand)) = E_In_Parameter
14787 and then
14788 (Nkind (Parent (N)) /= N_Assignment_Statement
14789 or else not Is_Entity_Name (Name (Parent (N)))
14790 or else not Is_Return_Object (Entity (Name (Parent (N)))))
14791 then
14792 Report_Error_N
14793 ("illegal attempt to store anonymous access to subprogram",
14794 Operand, Report_Errs);
14795 Report_Error_N
14796 ("\value has deeper accessibility than any master "
14797 & "(RM 3.10.2 (13))",
14798 Operand, Report_Errs);
14799
14800 Error_Msg_NE
14801 ("\use named access type for& instead of access parameter",
14802 Operand, Entity (Operand));
14803 end if;
14804
14805 -- Check that the designated types are subtype conformant
14806
14807 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
14808 Old_Id => Designated_Type (Opnd_Type),
14809 Err_Loc => N);
14810
14811 -- Check the static accessibility rule of 4.6(20)
14812
14813 if Type_Access_Level (Opnd_Type) >
14814 Deepest_Type_Access_Level (Target_Type)
14815 then
14816 Report_Error_N
14817 ("operand type has deeper accessibility level than target",
14818 Operand, Report_Errs);
14819
14820 -- Check that if the operand type is declared in a generic body,
14821 -- then the target type must be declared within that same body
14822 -- (enforces last sentence of 4.6(20)).
14823
14824 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
14825 declare
14826 O_Gen : constant Node_Id :=
14827 Enclosing_Generic_Body (Opnd_Type);
14828
14829 T_Gen : Node_Id;
14830
14831 begin
14832 T_Gen := Enclosing_Generic_Body (Target_Type);
14833 while Present (T_Gen) and then T_Gen /= O_Gen loop
14834 T_Gen := Enclosing_Generic_Body (T_Gen);
14835 end loop;
14836
14837 if T_Gen /= O_Gen then
14838 Report_Error_N
14839 ("target type must be declared in same generic body "
14840 & "as operand type", N, Report_Errs);
14841 end if;
14842 end;
14843 end if;
14844
14845 -- Check that the strub modes are compatible.
14846 -- We wish to reject explicit conversions only for
14847 -- incompatible modes.
14848
14849 return Conversion_Check
14850 (Compatible_Strub_Modes
14851 (Designated_Type (Target_Type),
14852 Designated_Type (Opnd_Type)),
14853 "incompatible `strub` modes");
14854
14855 -- Remote access to subprogram types
14856
14857 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
14858 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
14859 then
14860 -- It is valid to convert from one RAS type to another provided
14861 -- that their specification statically match.
14862
14863 -- Note: at this point, remote access to subprogram types have been
14864 -- expanded to their E_Record_Type representation, and we need to
14865 -- go back to the original access type definition using the
14866 -- Corresponding_Remote_Type attribute in order to check that the
14867 -- designated profiles match.
14868
14869 pragma Assert (Ekind (Target_Type) = E_Record_Type);
14870 pragma Assert (Ekind (Opnd_Type) = E_Record_Type);
14871
14872 Check_Subtype_Conformant
14873 (New_Id =>
14874 Designated_Type (Corresponding_Remote_Type (Target_Type)),
14875 Old_Id =>
14876 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
14877 Err_Loc =>
14878 N);
14879
14880 -- Check that the strub modes are compatible.
14881 -- We wish to reject explicit conversions only for
14882 -- incompatible modes.
14883
14884 return Conversion_Check
14885 (Compatible_Strub_Modes
14886 (Designated_Type (Target_Type),
14887 Designated_Type (Opnd_Type)),
14888 "incompatible `strub` modes");
14889
14890 -- If it was legal in the generic, it's legal in the instance
14891
14892 elsif In_Instance then
14893 return True;
14894
14895 -- Ignore privacy for streaming or Put_Image routines
14896
14897 elsif not Comes_From_Source (N)
14898 and then Original_Implementation_Base_Type (Target_Type) =
14899 Original_Implementation_Base_Type (Opnd_Type)
14900 then
14901 return True;
14902
14903 -- If both are tagged types, check legality of view conversions
14904
14905 elsif (Is_Tagged_Type (Target_Type) and then Is_Tagged_Type (Opnd_Type))
14906 or else (not Comes_From_Source (N)
14907 and then
14908 Is_Tagged_Type (Implementation_Base_Type (Target_Type))
14909 and then
14910 Is_Tagged_Type (Implementation_Base_Type (Opnd_Type)))
14911 then
14912 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
14913
14914 -- Types derived from the same root type are convertible
14915
14916 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
14917 return True;
14918
14919 -- In an instance or an inlined body, there may be inconsistent views of
14920 -- the same type, or of types derived from a common root. Similarly
14921 -- for compiler-generated streaming or Put_Image subprograms.
14922
14923 elsif (In_Instance or In_Inlined_Body or not Comes_From_Source (N))
14924 and then
14925 Root_Type (Underlying_Type (Target_Type)) =
14926 Root_Type (Underlying_Type (Opnd_Type))
14927 then
14928 return True;
14929
14930 -- Special check for common access type error case
14931
14932 elsif Ekind (Target_Type) = E_Access_Type
14933 and then Is_Access_Type (Opnd_Type)
14934 then
14935 Report_Error_N
14936 ("target type must be general access type!", N, Report_Errs);
14937 Report_Error_NE -- CODEFIX
14938 ("\add ALL to }!", N, Target_Type, Report_Errs);
14939 return False;
14940
14941 -- Here we have a real conversion error
14942
14943 else
14944 -- Check for missing regular with_clause when only a limited view of
14945 -- target is available.
14946
14947 if From_Limited_With (Opnd_Type) and then In_Package_Body then
14948 Report_Error_NE
14949 ("invalid conversion, not compatible with limited view of }",
14950 N, Opnd_Type, Report_Errs);
14951 Report_Error_NE
14952 ("\add with_clause for& to current unit!",
14953 N, Scope (Opnd_Type), Report_Errs);
14954
14955 elsif Is_Access_Type (Opnd_Type)
14956 and then From_Limited_With (Designated_Type (Opnd_Type))
14957 and then In_Package_Body
14958 then
14959 Report_Error_NE
14960 ("invalid conversion, not compatible with }",
14961 N, Opnd_Type, Report_Errs);
14962 Report_Error_NE
14963 ("\add with_clause for& to current unit!",
14964 N, Scope (Designated_Type (Opnd_Type)), Report_Errs);
14965
14966 else
14967 Report_Error_NE
14968 ("invalid conversion, not compatible with }",
14969 N, Opnd_Type, Report_Errs);
14970 end if;
14971
14972 return False;
14973 end if;
14974 end Valid_Conversion;
14975
14976 end Sem_Res;