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