]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/sem_res.adb
[multiple changes]
[thirdparty/gcc.git] / gcc / ada / sem_res.adb
CommitLineData
996ae0b0
RK
1------------------------------------------------------------------------------
2-- --
3-- GNAT COMPILER COMPONENTS --
4-- --
5-- S E M _ R E S --
6-- --
7-- B o d y --
8-- --
445e5888 9-- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
996ae0b0
RK
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- --
b5c84c3c 13-- ware Foundation; either version 3, or (at your option) any later ver- --
996ae0b0
RK
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 --
b5c84c3c
RD
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. --
996ae0b0
RK
20-- --
21-- GNAT was originally developed by the GNAT team at New York University. --
71ff80dc 22-- Extensive contributions were provided by Ada Core Technologies Inc. --
996ae0b0
RK
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;
758c442c 33with Exp_Disp; use Exp_Disp;
0669bebe 34with Exp_Ch6; use Exp_Ch6;
996ae0b0 35with Exp_Ch7; use Exp_Ch7;
fbf5a39b 36with Exp_Tss; use Exp_Tss;
996ae0b0 37with Exp_Util; use Exp_Util;
dae2b8ea 38with Fname; use Fname;
996ae0b0 39with Freeze; use Freeze;
8636f52f 40with Ghost; use Ghost;
ecad37f3 41with Inline; use Inline;
996ae0b0
RK
42with Itypes; use Itypes;
43with Lib; use Lib;
44with Lib.Xref; use Lib.Xref;
45with Namet; use Namet;
46with Nmake; use Nmake;
47with Nlists; use Nlists;
48with Opt; use Opt;
49with Output; use Output;
0566484a 50with Par_SCO; use Par_SCO;
996ae0b0 51with Restrict; use Restrict;
6e937c1c 52with Rident; use Rident;
996ae0b0
RK
53with Rtsfind; use Rtsfind;
54with Sem; use Sem;
a4100e55 55with Sem_Aux; use Sem_Aux;
996ae0b0
RK
56with Sem_Aggr; use Sem_Aggr;
57with Sem_Attr; use Sem_Attr;
58with Sem_Cat; use Sem_Cat;
59with Sem_Ch4; use Sem_Ch4;
60with Sem_Ch6; use Sem_Ch6;
61with Sem_Ch8; use Sem_Ch8;
4b92fd3c 62with Sem_Ch13; use Sem_Ch13;
dec6faf1 63with Sem_Dim; use Sem_Dim;
996ae0b0
RK
64with Sem_Disp; use Sem_Disp;
65with Sem_Dist; use Sem_Dist;
16212e89 66with Sem_Elim; use Sem_Elim;
996ae0b0
RK
67with Sem_Elab; use Sem_Elab;
68with Sem_Eval; use Sem_Eval;
69with Sem_Intr; use Sem_Intr;
70with Sem_Util; use Sem_Util;
ce72a9a3 71with Targparm; use Targparm;
996ae0b0
RK
72with Sem_Type; use Sem_Type;
73with Sem_Warn; use Sem_Warn;
74with Sinfo; use Sinfo;
f4b049db 75with Sinfo.CN; use Sinfo.CN;
fbf5a39b 76with Snames; use Snames;
996ae0b0
RK
77with Stand; use Stand;
78with Stringt; use Stringt;
45fc7ddb 79with Style; use Style;
996ae0b0
RK
80with Tbuild; use Tbuild;
81with Uintp; use Uintp;
82with Urealp; use Urealp;
83
84package body Sem_Res is
85
86 -----------------------
87 -- Local Subprograms --
88 -----------------------
89
90 -- Second pass (top-down) type checking and overload resolution procedures
ac16e74c
RD
91 -- Typ is the type required by context. These procedures propagate the
92 -- type information recursively to the descendants of N. If the node is not
5cc9353d 93 -- overloaded, its Etype is established in the first pass. If overloaded,
ac16e74c 94 -- the Resolve routines set the correct type. For arithmetic operators, the
5cc9353d 95 -- Etype is the base type of the context.
996ae0b0
RK
96
97 -- Note that Resolve_Attribute is separated off in Sem_Attr
98
996ae0b0
RK
99 procedure Check_Discriminant_Use (N : Node_Id);
100 -- Enforce the restrictions on the use of discriminants when constraining
101 -- a component of a discriminated type (record or concurrent type).
102
103 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
966fc9c5
AC
104 -- Given a node for an operator associated with type T, check that the
105 -- operator is visible. Operators all of whose operands are universal must
106 -- be checked for visibility during resolution because their type is not
107 -- determinable based on their operands.
996ae0b0 108
c8ef728f
ES
109 procedure Check_Fully_Declared_Prefix
110 (Typ : Entity_Id;
111 Pref : Node_Id);
112 -- Check that the type of the prefix of a dereference is not incomplete
113
996ae0b0
RK
114 function Check_Infinite_Recursion (N : Node_Id) return Boolean;
115 -- Given a call node, N, which is known to occur immediately within the
116 -- subprogram being called, determines whether it is a detectable case of
117 -- an infinite recursion, and if so, outputs appropriate messages. Returns
118 -- True if an infinite recursion is detected, and False otherwise.
119
120 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id);
121 -- If the type of the object being initialized uses the secondary stack
122 -- directly or indirectly, create a transient scope for the call to the
fbf5a39b
AC
123 -- init proc. This is because we do not create transient scopes for the
124 -- initialization of individual components within the init proc itself.
996ae0b0
RK
125 -- Could be optimized away perhaps?
126
f61580d4 127 procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
6fb4cdde
AC
128 -- N is the node for a logical operator. If the operator is predefined, and
129 -- the root type of the operands is Standard.Boolean, then a check is made
a36c1c3e
RD
130 -- for restriction No_Direct_Boolean_Operators. This procedure also handles
131 -- the style check for Style_Check_Boolean_And_Or.
f61580d4 132
c2a2dbcc
RD
133 function Is_Atomic_Ref_With_Address (N : Node_Id) return Boolean;
134 -- N is either an indexed component or a selected component. This function
135 -- returns true if the prefix refers to an object that has an address
136 -- clause (the case in which we may want to issue a warning).
137
67ce0d7e 138 function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
5cc9353d
RD
139 -- Determine whether E is an access type declared by an access declaration,
140 -- and not an (anonymous) allocator type.
67ce0d7e 141
996ae0b0 142 function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
6a497607
AC
143 -- Utility to check whether the entity for an operator is a predefined
144 -- operator, in which case the expression is left as an operator in the
145 -- tree (else it is rewritten into a call). An instance of an intrinsic
146 -- conversion operation may be given an operator name, but is not treated
147 -- like an operator. Note that an operator that is an imported back-end
148 -- builtin has convention Intrinsic, but is expected to be rewritten into
149 -- a call, so such an operator is not treated as predefined by this
150 -- predicate.
996ae0b0
RK
151
152 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
153 -- If a default expression in entry call N depends on the discriminants
154 -- of the task, it must be replaced with a reference to the discriminant
155 -- of the task being called.
156
10303118
BD
157 procedure Resolve_Op_Concat_Arg
158 (N : Node_Id;
159 Arg : Node_Id;
160 Typ : Entity_Id;
161 Is_Comp : Boolean);
162 -- Internal procedure for Resolve_Op_Concat to resolve one operand of
163 -- concatenation operator. The operand is either of the array type or of
164 -- the component type. If the operand is an aggregate, and the component
165 -- type is composite, this is ambiguous if component type has aggregates.
166
167 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
168 -- Does the first part of the work of Resolve_Op_Concat
169
170 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
171 -- Does the "rest" of the work of Resolve_Op_Concat, after the left operand
172 -- has been resolved. See Resolve_Op_Concat for details.
173
996ae0b0
RK
174 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id);
175 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id);
176 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id);
19d846a0 177 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id);
996ae0b0
RK
178 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id);
179 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id);
955871d3 180 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id);
996ae0b0
RK
181 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id);
182 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id);
955871d3 183 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id);
9b16cb57 184 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id);
5f50020a 185 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id);
996ae0b0
RK
186 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id);
187 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id);
188 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id);
189 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id);
190 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id);
191 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id);
192 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id);
193 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id);
194 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id);
195 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id);
7610fee8 196 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id);
996ae0b0
RK
197 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id);
198 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id);
199 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id);
200 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id);
201 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id);
202 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id);
203 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id);
204 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id);
996ae0b0
RK
205 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id);
206 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id);
207 procedure Resolve_Unchecked_Expression (N : Node_Id; Typ : Entity_Id);
208 procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
209
210 function Operator_Kind
211 (Op_Name : Name_Id;
0ab80019 212 Is_Binary : Boolean) return Node_Kind;
996ae0b0
RK
213 -- Utility to map the name of an operator into the corresponding Node. Used
214 -- by other node rewriting procedures.
215
216 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
bc5f3720
RD
217 -- Resolve actuals of call, and add default expressions for missing ones.
218 -- N is the Node_Id for the subprogram call, and Nam is the entity of the
219 -- called subprogram.
996ae0b0
RK
220
221 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
222 -- Called from Resolve_Call, when the prefix denotes an entry or element
223 -- of entry family. Actuals are resolved as for subprograms, and the node
224 -- is rebuilt as an entry call. Also called for protected operations. Typ
225 -- is the context type, which is used when the operation is a protected
226 -- function with no arguments, and the return value is indexed.
227
228 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
5cc9353d
RD
229 -- A call to a user-defined intrinsic operator is rewritten as a call to
230 -- the corresponding predefined operator, with suitable conversions. Note
231 -- that this applies only for intrinsic operators that denote predefined
232 -- operators, not ones that are intrinsic imports of back-end builtins.
996ae0b0 233
fbf5a39b 234 procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
7a5b62b0 235 -- Ditto, for arithmetic unary operators
fbf5a39b 236
996ae0b0
RK
237 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
238 -- If an operator node resolves to a call to a user-defined operator,
239 -- rewrite the node as a function call.
240
241 procedure Make_Call_Into_Operator
242 (N : Node_Id;
243 Typ : Entity_Id;
244 Op_Id : Entity_Id);
245 -- Inverse transformation: if an operator is given in functional notation,
ac16e74c
RD
246 -- then after resolving the node, transform into an operator node, so that
247 -- operands are resolved properly. Recall that predefined operators do not
248 -- have a full signature and special resolution rules apply.
996ae0b0 249
0ab80019
AC
250 procedure Rewrite_Renamed_Operator
251 (N : Node_Id;
252 Op : Entity_Id;
253 Typ : Entity_Id);
21d7ef70 254 -- An operator can rename another, e.g. in an instantiation. In that
0ab80019 255 -- case, the proper operator node must be constructed and resolved.
996ae0b0
RK
256
257 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
258 -- The String_Literal_Subtype is built for all strings that are not
966fc9c5
AC
259 -- operands of a static concatenation operation. If the argument is not
260 -- a N_String_Literal node, then the call has no effect.
996ae0b0
RK
261
262 procedure Set_Slice_Subtype (N : Node_Id);
fbf5a39b 263 -- Build subtype of array type, with the range specified by the slice
996ae0b0 264
0669bebe
GB
265 procedure Simplify_Type_Conversion (N : Node_Id);
266 -- Called after N has been resolved and evaluated, but before range checks
267 -- have been applied. Currently simplifies a combination of floating-point
24228312 268 -- to integer conversion and Rounding or Truncation attribute.
0669bebe 269
996ae0b0 270 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
5cc9353d
RD
271 -- A universal_fixed expression in an universal context is unambiguous if
272 -- there is only one applicable fixed point type. Determining whether there
273 -- is only one requires a search over all visible entities, and happens
274 -- only in very pathological cases (see 6115-006).
996ae0b0 275
996ae0b0
RK
276 -------------------------
277 -- Ambiguous_Character --
278 -------------------------
279
280 procedure Ambiguous_Character (C : Node_Id) is
281 E : Entity_Id;
282
283 begin
284 if Nkind (C) = N_Character_Literal then
ed2233dc 285 Error_Msg_N ("ambiguous character literal", C);
b7d1f17f
HK
286
287 -- First the ones in Standard
288
ed2233dc
AC
289 Error_Msg_N ("\\possible interpretation: Character!", C);
290 Error_Msg_N ("\\possible interpretation: Wide_Character!", C);
b7d1f17f
HK
291
292 -- Include Wide_Wide_Character in Ada 2005 mode
293
0791fbe9 294 if Ada_Version >= Ada_2005 then
ed2233dc 295 Error_Msg_N ("\\possible interpretation: Wide_Wide_Character!", C);
b7d1f17f
HK
296 end if;
297
298 -- Now any other types that match
996ae0b0
RK
299
300 E := Current_Entity (C);
1420b484 301 while Present (E) loop
ed2233dc 302 Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
1420b484
JM
303 E := Homonym (E);
304 end loop;
996ae0b0
RK
305 end if;
306 end Ambiguous_Character;
307
308 -------------------------
309 -- Analyze_And_Resolve --
310 -------------------------
311
312 procedure Analyze_And_Resolve (N : Node_Id) is
313 begin
314 Analyze (N);
fbf5a39b 315 Resolve (N);
996ae0b0
RK
316 end Analyze_And_Resolve;
317
318 procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
319 begin
320 Analyze (N);
321 Resolve (N, Typ);
322 end Analyze_And_Resolve;
323
a91e9ac7 324 -- Versions with check(s) suppressed
996ae0b0
RK
325
326 procedure Analyze_And_Resolve
327 (N : Node_Id;
328 Typ : Entity_Id;
329 Suppress : Check_Id)
330 is
fbf5a39b 331 Scop : constant Entity_Id := Current_Scope;
996ae0b0
RK
332
333 begin
334 if Suppress = All_Checks then
335 declare
a7f1b24f 336 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
996ae0b0 337 begin
a7f1b24f 338 Scope_Suppress.Suppress := (others => True);
996ae0b0 339 Analyze_And_Resolve (N, Typ);
a7f1b24f 340 Scope_Suppress.Suppress := Sva;
a91e9ac7
AC
341 end;
342
996ae0b0
RK
343 else
344 declare
3217f71e 345 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
996ae0b0 346 begin
3217f71e 347 Scope_Suppress.Suppress (Suppress) := True;
996ae0b0 348 Analyze_And_Resolve (N, Typ);
3217f71e 349 Scope_Suppress.Suppress (Suppress) := Svg;
996ae0b0
RK
350 end;
351 end if;
352
353 if Current_Scope /= Scop
354 and then Scope_Is_Transient
355 then
5cc9353d
RD
356 -- This can only happen if a transient scope was created for an inner
357 -- expression, which will be removed upon completion of the analysis
358 -- of an enclosing construct. The transient scope must have the
359 -- suppress status of the enclosing environment, not of this Analyze
360 -- call.
996ae0b0
RK
361
362 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
363 Scope_Suppress;
364 end if;
365 end Analyze_And_Resolve;
366
367 procedure Analyze_And_Resolve
368 (N : Node_Id;
369 Suppress : Check_Id)
370 is
fbf5a39b 371 Scop : constant Entity_Id := Current_Scope;
996ae0b0
RK
372
373 begin
374 if Suppress = All_Checks then
375 declare
a7f1b24f 376 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
a91e9ac7 377 begin
a7f1b24f 378 Scope_Suppress.Suppress := (others => True);
a91e9ac7 379 Analyze_And_Resolve (N);
a7f1b24f 380 Scope_Suppress.Suppress := Sva;
a91e9ac7
AC
381 end;
382
996ae0b0
RK
383 else
384 declare
3217f71e 385 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
996ae0b0 386 begin
3217f71e 387 Scope_Suppress.Suppress (Suppress) := True;
996ae0b0 388 Analyze_And_Resolve (N);
3217f71e 389 Scope_Suppress.Suppress (Suppress) := Svg;
996ae0b0
RK
390 end;
391 end if;
392
3217f71e 393 if Current_Scope /= Scop and then Scope_Is_Transient then
996ae0b0
RK
394 Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
395 Scope_Suppress;
396 end if;
397 end Analyze_And_Resolve;
398
399 ----------------------------
400 -- Check_Discriminant_Use --
401 ----------------------------
402
403 procedure Check_Discriminant_Use (N : Node_Id) is
404 PN : constant Node_Id := Parent (N);
405 Disc : constant Entity_Id := Entity (N);
406 P : Node_Id;
407 D : Node_Id;
408
409 begin
f3d0f304 410 -- Any use in a spec-expression is legal
996ae0b0 411
45fc7ddb 412 if In_Spec_Expression then
996ae0b0
RK
413 null;
414
415 elsif Nkind (PN) = N_Range then
416
a77842bd 417 -- Discriminant cannot be used to constrain a scalar type
996ae0b0
RK
418
419 P := Parent (PN);
420
421 if Nkind (P) = N_Range_Constraint
422 and then Nkind (Parent (P)) = N_Subtype_Indication
a397db96 423 and then Nkind (Parent (Parent (P))) = N_Component_Definition
996ae0b0
RK
424 then
425 Error_Msg_N ("discriminant cannot constrain scalar type", N);
426
427 elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
428
5cc9353d 429 -- The following check catches the unusual case where a
966fc9c5
AC
430 -- discriminant appears within an index constraint that is part
431 -- of a larger expression within a constraint on a component,
432 -- e.g. "C : Int range 1 .. F (new A(1 .. D))". For now we only
433 -- check case of record components, and note that a similar check
434 -- should also apply in the case of discriminant constraints
435 -- below. ???
996ae0b0
RK
436
437 -- Note that the check for N_Subtype_Declaration below is to
438 -- detect the valid use of discriminants in the constraints of a
439 -- subtype declaration when this subtype declaration appears
440 -- inside the scope of a record type (which is syntactically
441 -- illegal, but which may be created as part of derived type
442 -- processing for records). See Sem_Ch3.Build_Derived_Record_Type
443 -- for more info.
444
445 if Ekind (Current_Scope) = E_Record_Type
446 and then Scope (Disc) = Current_Scope
447 and then not
448 (Nkind (Parent (P)) = N_Subtype_Indication
45fc7ddb
HK
449 and then
450 Nkind_In (Parent (Parent (P)), N_Component_Definition,
451 N_Subtype_Declaration)
996ae0b0
RK
452 and then Paren_Count (N) = 0)
453 then
454 Error_Msg_N
455 ("discriminant must appear alone in component constraint", N);
456 return;
457 end if;
458
a0ac3932 459 -- Detect a common error:
9bc43c53 460
996ae0b0 461 -- type R (D : Positive := 100) is record
9bc43c53 462 -- Name : String (1 .. D);
996ae0b0
RK
463 -- end record;
464
a0ac3932
RD
465 -- The default value causes an object of type R to be allocated
466 -- with room for Positive'Last characters. The RM does not mandate
467 -- the allocation of the maximum size, but that is what GNAT does
468 -- so we should warn the programmer that there is a problem.
996ae0b0 469
a0ac3932 470 Check_Large : declare
996ae0b0
RK
471 SI : Node_Id;
472 T : Entity_Id;
473 TB : Node_Id;
474 CB : Entity_Id;
475
476 function Large_Storage_Type (T : Entity_Id) return Boolean;
5cc9353d
RD
477 -- Return True if type T has a large enough range that any
478 -- array whose index type covered the whole range of the type
479 -- would likely raise Storage_Error.
996ae0b0 480
fbf5a39b
AC
481 ------------------------
482 -- Large_Storage_Type --
483 ------------------------
484
996ae0b0
RK
485 function Large_Storage_Type (T : Entity_Id) return Boolean is
486 begin
4b92fd3c
ST
487 -- The type is considered large if its bounds are known at
488 -- compile time and if it requires at least as many bits as
489 -- a Positive to store the possible values.
490
491 return Compile_Time_Known_Value (Type_Low_Bound (T))
492 and then Compile_Time_Known_Value (Type_High_Bound (T))
493 and then
494 Minimum_Size (T, Biased => True) >=
a0ac3932 495 RM_Size (Standard_Positive);
996ae0b0
RK
496 end Large_Storage_Type;
497
a0ac3932
RD
498 -- Start of processing for Check_Large
499
996ae0b0
RK
500 begin
501 -- Check that the Disc has a large range
502
503 if not Large_Storage_Type (Etype (Disc)) then
504 goto No_Danger;
505 end if;
506
507 -- If the enclosing type is limited, we allocate only the
508 -- default value, not the maximum, and there is no need for
509 -- a warning.
510
511 if Is_Limited_Type (Scope (Disc)) then
512 goto No_Danger;
513 end if;
514
515 -- Check that it is the high bound
516
517 if N /= High_Bound (PN)
c8ef728f 518 or else No (Discriminant_Default_Value (Disc))
996ae0b0
RK
519 then
520 goto No_Danger;
521 end if;
522
5cc9353d
RD
523 -- Check the array allows a large range at this bound. First
524 -- find the array
996ae0b0
RK
525
526 SI := Parent (P);
527
528 if Nkind (SI) /= N_Subtype_Indication then
529 goto No_Danger;
530 end if;
531
532 T := Entity (Subtype_Mark (SI));
533
534 if not Is_Array_Type (T) then
535 goto No_Danger;
536 end if;
537
538 -- Next, find the dimension
539
540 TB := First_Index (T);
541 CB := First (Constraints (P));
542 while True
543 and then Present (TB)
544 and then Present (CB)
545 and then CB /= PN
546 loop
547 Next_Index (TB);
548 Next (CB);
549 end loop;
550
551 if CB /= PN then
552 goto No_Danger;
553 end if;
554
555 -- Now, check the dimension has a large range
556
557 if not Large_Storage_Type (Etype (TB)) then
558 goto No_Danger;
559 end if;
560
561 -- Warn about the danger
562
563 Error_Msg_N
324ac540 564 ("??creation of & object may raise Storage_Error!",
fbf5a39b 565 Scope (Disc));
996ae0b0
RK
566
567 <<No_Danger>>
568 null;
569
a0ac3932 570 end Check_Large;
996ae0b0
RK
571 end if;
572
573 -- Legal case is in index or discriminant constraint
574
45fc7ddb
HK
575 elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint,
576 N_Discriminant_Association)
996ae0b0
RK
577 then
578 if Paren_Count (N) > 0 then
579 Error_Msg_N
580 ("discriminant in constraint must appear alone", N);
758c442c
GD
581
582 elsif Nkind (N) = N_Expanded_Name
583 and then Comes_From_Source (N)
584 then
585 Error_Msg_N
586 ("discriminant must appear alone as a direct name", N);
996ae0b0
RK
587 end if;
588
589 return;
590
5cc9353d
RD
591 -- Otherwise, context is an expression. It should not be within (i.e. a
592 -- subexpression of) a constraint for a component.
996ae0b0
RK
593
594 else
595 D := PN;
596 P := Parent (PN);
45fc7ddb
HK
597 while not Nkind_In (P, N_Component_Declaration,
598 N_Subtype_Indication,
599 N_Entry_Declaration)
996ae0b0
RK
600 loop
601 D := P;
602 P := Parent (P);
603 exit when No (P);
604 end loop;
605
5cc9353d
RD
606 -- If the discriminant is used in an expression that is a bound of a
607 -- scalar type, an Itype is created and the bounds are attached to
608 -- its range, not to the original subtype indication. Such use is of
609 -- course a double fault.
996ae0b0
RK
610
611 if (Nkind (P) = N_Subtype_Indication
45fc7ddb
HK
612 and then Nkind_In (Parent (P), N_Component_Definition,
613 N_Derived_Type_Definition)
996ae0b0
RK
614 and then D = Constraint (P))
615
19fb051c
AC
616 -- The constraint itself may be given by a subtype indication,
617 -- rather than by a more common discrete range.
996ae0b0
RK
618
619 or else (Nkind (P) = N_Subtype_Indication
fbf5a39b
AC
620 and then
621 Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
996ae0b0
RK
622 or else Nkind (P) = N_Entry_Declaration
623 or else Nkind (D) = N_Defining_Identifier
624 then
625 Error_Msg_N
626 ("discriminant in constraint must appear alone", N);
627 end if;
628 end if;
629 end Check_Discriminant_Use;
630
631 --------------------------------
632 -- Check_For_Visible_Operator --
633 --------------------------------
634
635 procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
996ae0b0 636 begin
fbf5a39b 637 if Is_Invisible_Operator (N, T) then
305caf42 638 Error_Msg_NE -- CODEFIX
996ae0b0 639 ("operator for} is not directly visible!", N, First_Subtype (T));
305caf42
AC
640 Error_Msg_N -- CODEFIX
641 ("use clause would make operation legal!", N);
996ae0b0
RK
642 end if;
643 end Check_For_Visible_Operator;
644
c8ef728f
ES
645 ----------------------------------
646 -- Check_Fully_Declared_Prefix --
647 ----------------------------------
648
649 procedure Check_Fully_Declared_Prefix
650 (Typ : Entity_Id;
651 Pref : Node_Id)
652 is
653 begin
654 -- Check that the designated type of the prefix of a dereference is
655 -- not an incomplete type. This cannot be done unconditionally, because
656 -- dereferences of private types are legal in default expressions. This
657 -- case is taken care of in Check_Fully_Declared, called below. There
658 -- are also 2005 cases where it is legal for the prefix to be unfrozen.
659
660 -- This consideration also applies to similar checks for allocators,
661 -- qualified expressions, and type conversions.
662
663 -- An additional exception concerns other per-object expressions that
664 -- are not directly related to component declarations, in particular
665 -- representation pragmas for tasks. These will be per-object
666 -- expressions if they depend on discriminants or some global entity.
667 -- If the task has access discriminants, the designated type may be
668 -- incomplete at the point the expression is resolved. This resolution
669 -- takes place within the body of the initialization procedure, where
670 -- the discriminant is replaced by its discriminal.
671
672 if Is_Entity_Name (Pref)
673 and then Ekind (Entity (Pref)) = E_In_Parameter
674 then
675 null;
676
677 -- Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
678 -- are handled by Analyze_Access_Attribute, Analyze_Assignment,
679 -- Analyze_Object_Renaming, and Freeze_Entity.
680
0791fbe9 681 elsif Ada_Version >= Ada_2005
c8ef728f 682 and then Is_Entity_Name (Pref)
811c6a85 683 and then Is_Access_Type (Etype (Pref))
c8ef728f
ES
684 and then Ekind (Directly_Designated_Type (Etype (Pref))) =
685 E_Incomplete_Type
686 and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
687 then
688 null;
689 else
690 Check_Fully_Declared (Typ, Parent (Pref));
691 end if;
692 end Check_Fully_Declared_Prefix;
693
996ae0b0
RK
694 ------------------------------
695 -- Check_Infinite_Recursion --
696 ------------------------------
697
698 function Check_Infinite_Recursion (N : Node_Id) return Boolean is
699 P : Node_Id;
700 C : Node_Id;
701
07fc65c4 702 function Same_Argument_List return Boolean;
5cc9353d
RD
703 -- Check whether list of actuals is identical to list of formals of
704 -- called function (which is also the enclosing scope).
07fc65c4
GB
705
706 ------------------------
707 -- Same_Argument_List --
708 ------------------------
709
710 function Same_Argument_List return Boolean is
711 A : Node_Id;
712 F : Entity_Id;
713 Subp : Entity_Id;
714
715 begin
716 if not Is_Entity_Name (Name (N)) then
717 return False;
718 else
719 Subp := Entity (Name (N));
720 end if;
721
722 F := First_Formal (Subp);
723 A := First_Actual (N);
07fc65c4 724 while Present (F) and then Present (A) loop
445e5888 725 if not Is_Entity_Name (A) or else Entity (A) /= F then
07fc65c4
GB
726 return False;
727 end if;
728
729 Next_Actual (A);
730 Next_Formal (F);
731 end loop;
732
733 return True;
734 end Same_Argument_List;
735
736 -- Start of processing for Check_Infinite_Recursion
737
996ae0b0 738 begin
26570b21
RD
739 -- Special case, if this is a procedure call and is a call to the
740 -- current procedure with the same argument list, then this is for
741 -- sure an infinite recursion and we insert a call to raise SE.
742
743 if Is_List_Member (N)
744 and then List_Length (List_Containing (N)) = 1
745 and then Same_Argument_List
746 then
747 declare
748 P : constant Node_Id := Parent (N);
749 begin
750 if Nkind (P) = N_Handled_Sequence_Of_Statements
751 and then Nkind (Parent (P)) = N_Subprogram_Body
752 and then Is_Empty_List (Declarations (Parent (P)))
753 then
43417b90 754 Error_Msg_Warn := SPARK_Mode /= On;
4a28b181
AC
755 Error_Msg_N ("!infinite recursion<<", N);
756 Error_Msg_N ("\!Storage_Error [<<", N);
26570b21
RD
757 Insert_Action (N,
758 Make_Raise_Storage_Error (Sloc (N),
759 Reason => SE_Infinite_Recursion));
760 return True;
761 end if;
762 end;
763 end if;
764
765 -- If not that special case, search up tree, quitting if we reach a
766 -- construct (e.g. a conditional) that tells us that this is not a
767 -- case for an infinite recursion warning.
996ae0b0
RK
768
769 C := N;
770 loop
771 P := Parent (C);
9a7da240
RD
772
773 -- If no parent, then we were not inside a subprogram, this can for
774 -- example happen when processing certain pragmas in a spec. Just
775 -- return False in this case.
776
777 if No (P) then
778 return False;
779 end if;
780
781 -- Done if we get to subprogram body, this is definitely an infinite
782 -- recursion case if we did not find anything to stop us.
783
996ae0b0 784 exit when Nkind (P) = N_Subprogram_Body;
9a7da240
RD
785
786 -- If appearing in conditional, result is false
787
45fc7ddb
HK
788 if Nkind_In (P, N_Or_Else,
789 N_And_Then,
d347f572
AC
790 N_Case_Expression,
791 N_Case_Statement,
9b16cb57 792 N_If_Expression,
d347f572 793 N_If_Statement)
996ae0b0
RK
794 then
795 return False;
796
797 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
798 and then C /= First (Statements (P))
799 then
26570b21
RD
800 -- If the call is the expression of a return statement and the
801 -- actuals are identical to the formals, it's worth a warning.
802 -- However, we skip this if there is an immediately preceding
803 -- raise statement, since the call is never executed.
07fc65c4
GB
804
805 -- Furthermore, this corresponds to a common idiom:
806
807 -- function F (L : Thing) return Boolean is
808 -- begin
809 -- raise Program_Error;
810 -- return F (L);
811 -- end F;
812
813 -- for generating a stub function
814
aa5147f0 815 if Nkind (Parent (N)) = N_Simple_Return_Statement
07fc65c4
GB
816 and then Same_Argument_List
817 then
9ebe3743
HK
818 exit when not Is_List_Member (Parent (N));
819
820 -- OK, return statement is in a statement list, look for raise
821
822 declare
823 Nod : Node_Id;
824
825 begin
826 -- Skip past N_Freeze_Entity nodes generated by expansion
827
828 Nod := Prev (Parent (N));
829 while Present (Nod)
830 and then Nkind (Nod) = N_Freeze_Entity
831 loop
832 Prev (Nod);
833 end loop;
834
3235dc87
AC
835 -- If no raise statement, give warning. We look at the
836 -- original node, because in the case of "raise ... with
837 -- ...", the node has been transformed into a call.
9ebe3743 838
3235dc87 839 exit when Nkind (Original_Node (Nod)) /= N_Raise_Statement
9ebe3743
HK
840 and then
841 (Nkind (Nod) not in N_Raise_xxx_Error
19fb051c 842 or else Present (Condition (Nod)));
9ebe3743 843 end;
07fc65c4
GB
844 end if;
845
996ae0b0
RK
846 return False;
847
848 else
849 C := P;
850 end if;
851 end loop;
852
43417b90 853 Error_Msg_Warn := SPARK_Mode /= On;
4a28b181
AC
854 Error_Msg_N ("!possible infinite recursion<<", N);
855 Error_Msg_N ("\!??Storage_Error ]<<", N);
996ae0b0
RK
856
857 return True;
858 end Check_Infinite_Recursion;
859
860 -------------------------------
861 -- Check_Initialization_Call --
862 -------------------------------
863
864 procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
fbf5a39b 865 Typ : constant Entity_Id := Etype (First_Formal (Nam));
996ae0b0
RK
866
867 function Uses_SS (T : Entity_Id) return Boolean;
07fc65c4
GB
868 -- Check whether the creation of an object of the type will involve
869 -- use of the secondary stack. If T is a record type, this is true
f3d57416 870 -- if the expression for some component uses the secondary stack, e.g.
07fc65c4
GB
871 -- through a call to a function that returns an unconstrained value.
872 -- False if T is controlled, because cleanups occur elsewhere.
873
874 -------------
875 -- Uses_SS --
876 -------------
996ae0b0
RK
877
878 function Uses_SS (T : Entity_Id) return Boolean is
aa5147f0
ES
879 Comp : Entity_Id;
880 Expr : Node_Id;
881 Full_Type : Entity_Id := Underlying_Type (T);
996ae0b0
RK
882
883 begin
aa5147f0
ES
884 -- Normally we want to use the underlying type, but if it's not set
885 -- then continue with T.
886
887 if not Present (Full_Type) then
888 Full_Type := T;
889 end if;
890
891 if Is_Controlled (Full_Type) then
996ae0b0
RK
892 return False;
893
aa5147f0
ES
894 elsif Is_Array_Type (Full_Type) then
895 return Uses_SS (Component_Type (Full_Type));
996ae0b0 896
aa5147f0
ES
897 elsif Is_Record_Type (Full_Type) then
898 Comp := First_Component (Full_Type);
996ae0b0 899 while Present (Comp) loop
996ae0b0
RK
900 if Ekind (Comp) = E_Component
901 and then Nkind (Parent (Comp)) = N_Component_Declaration
902 then
aa5147f0
ES
903 -- The expression for a dynamic component may be rewritten
904 -- as a dereference, so retrieve original node.
905
906 Expr := Original_Node (Expression (Parent (Comp)));
996ae0b0 907
aa5147f0 908 -- Return True if the expression is a call to a function
1d57c04f
AC
909 -- (including an attribute function such as Image, or a
910 -- user-defined operator) with a result that requires a
911 -- transient scope.
fbf5a39b 912
aa5147f0 913 if (Nkind (Expr) = N_Function_Call
1d57c04f 914 or else Nkind (Expr) in N_Op
aa5147f0
ES
915 or else (Nkind (Expr) = N_Attribute_Reference
916 and then Present (Expressions (Expr))))
996ae0b0
RK
917 and then Requires_Transient_Scope (Etype (Expr))
918 then
919 return True;
920
921 elsif Uses_SS (Etype (Comp)) then
922 return True;
923 end if;
924 end if;
925
926 Next_Component (Comp);
927 end loop;
928
929 return False;
930
931 else
932 return False;
933 end if;
934 end Uses_SS;
935
07fc65c4
GB
936 -- Start of processing for Check_Initialization_Call
937
996ae0b0 938 begin
0669bebe 939 -- Establish a transient scope if the type needs it
07fc65c4 940
0669bebe 941 if Uses_SS (Typ) then
996ae0b0
RK
942 Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
943 end if;
944 end Check_Initialization_Call;
945
f61580d4
AC
946 ---------------------------------------
947 -- Check_No_Direct_Boolean_Operators --
948 ---------------------------------------
949
950 procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
951 begin
952 if Scope (Entity (N)) = Standard_Standard
953 and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
954 then
6fb4cdde 955 -- Restriction only applies to original source code
f61580d4 956
6fb4cdde 957 if Comes_From_Source (N) then
f61580d4
AC
958 Check_Restriction (No_Direct_Boolean_Operators, N);
959 end if;
960 end if;
a36c1c3e 961
545d3e65
RD
962 -- Do style check (but skip if in instance, error is on template)
963
a36c1c3e 964 if Style_Check then
545d3e65
RD
965 if not In_Instance then
966 Check_Boolean_Operator (N);
967 end if;
a36c1c3e 968 end if;
f61580d4
AC
969 end Check_No_Direct_Boolean_Operators;
970
996ae0b0
RK
971 ------------------------------
972 -- Check_Parameterless_Call --
973 ------------------------------
974
975 procedure Check_Parameterless_Call (N : Node_Id) is
976 Nam : Node_Id;
977
bc5f3720
RD
978 function Prefix_Is_Access_Subp return Boolean;
979 -- If the prefix is of an access_to_subprogram type, the node must be
980 -- rewritten as a call. Ditto if the prefix is overloaded and all its
981 -- interpretations are access to subprograms.
982
983 ---------------------------
984 -- Prefix_Is_Access_Subp --
985 ---------------------------
986
987 function Prefix_Is_Access_Subp return Boolean is
988 I : Interp_Index;
989 It : Interp;
990
991 begin
22b77f68 992 -- If the context is an attribute reference that can apply to
b4a4936b 993 -- functions, this is never a parameterless call (RM 4.1.4(6)).
96d2756f
AC
994
995 if Nkind (Parent (N)) = N_Attribute_Reference
b69cd36a
AC
996 and then Nam_In (Attribute_Name (Parent (N)), Name_Address,
997 Name_Code_Address,
998 Name_Access)
96d2756f
AC
999 then
1000 return False;
1001 end if;
1002
bc5f3720
RD
1003 if not Is_Overloaded (N) then
1004 return
1005 Ekind (Etype (N)) = E_Subprogram_Type
1006 and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
1007 else
1008 Get_First_Interp (N, I, It);
1009 while Present (It.Typ) loop
1010 if Ekind (It.Typ) /= E_Subprogram_Type
1011 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
1012 then
1013 return False;
1014 end if;
1015
1016 Get_Next_Interp (I, It);
1017 end loop;
1018
1019 return True;
1020 end if;
1021 end Prefix_Is_Access_Subp;
1022
1023 -- Start of processing for Check_Parameterless_Call
1024
996ae0b0 1025 begin
07fc65c4
GB
1026 -- Defend against junk stuff if errors already detected
1027
1028 if Total_Errors_Detected /= 0 then
1029 if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1030 return;
1031 elsif Nkind (N) in N_Has_Chars
1032 and then Chars (N) in Error_Name_Or_No_Name
1033 then
1034 return;
1035 end if;
fbf5a39b
AC
1036
1037 Require_Entity (N);
996ae0b0
RK
1038 end if;
1039
45fc7ddb
HK
1040 -- If the context expects a value, and the name is a procedure, this is
1041 -- most likely a missing 'Access. Don't try to resolve the parameterless
1042 -- call, error will be caught when the outer call is analyzed.
18c0ecbe
AC
1043
1044 if Is_Entity_Name (N)
1045 and then Ekind (Entity (N)) = E_Procedure
1046 and then not Is_Overloaded (N)
1047 and then
45fc7ddb
HK
1048 Nkind_In (Parent (N), N_Parameter_Association,
1049 N_Function_Call,
1050 N_Procedure_Call_Statement)
18c0ecbe
AC
1051 then
1052 return;
1053 end if;
1054
45fc7ddb
HK
1055 -- Rewrite as call if overloadable entity that is (or could be, in the
1056 -- overloaded case) a function call. If we know for sure that the entity
1057 -- is an enumeration literal, we do not rewrite it.
f4b049db 1058
e1d9659d
AC
1059 -- If the entity is the name of an operator, it cannot be a call because
1060 -- operators cannot have default parameters. In this case, this must be
1061 -- a string whose contents coincide with an operator name. Set the kind
96d2756f 1062 -- of the node appropriately.
996ae0b0
RK
1063
1064 if (Is_Entity_Name (N)
e1d9659d 1065 and then Nkind (N) /= N_Operator_Symbol
996ae0b0
RK
1066 and then Is_Overloadable (Entity (N))
1067 and then (Ekind (Entity (N)) /= E_Enumeration_Literal
964f13da 1068 or else Is_Overloaded (N)))
996ae0b0 1069
09494c32 1070 -- Rewrite as call if it is an explicit dereference of an expression of
f3d57416 1071 -- a subprogram access type, and the subprogram type is not that of a
996ae0b0
RK
1072 -- procedure or entry.
1073
1074 or else
bc5f3720 1075 (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
996ae0b0
RK
1076
1077 -- Rewrite as call if it is a selected component which is a function,
1078 -- this is the case of a call to a protected function (which may be
1079 -- overloaded with other protected operations).
1080
1081 or else
1082 (Nkind (N) = N_Selected_Component
1083 and then (Ekind (Entity (Selector_Name (N))) = E_Function
964f13da
RD
1084 or else
1085 (Ekind_In (Entity (Selector_Name (N)), E_Entry,
1086 E_Procedure)
1087 and then Is_Overloaded (Selector_Name (N)))))
996ae0b0 1088
5cc9353d
RD
1089 -- If one of the above three conditions is met, rewrite as call. Apply
1090 -- the rewriting only once.
996ae0b0
RK
1091
1092 then
1093 if Nkind (Parent (N)) /= N_Function_Call
1094 or else N /= Name (Parent (N))
1095 then
747de90b
AC
1096
1097 -- This may be a prefixed call that was not fully analyzed, e.g.
1098 -- an actual in an instance.
1099
1100 if Ada_Version >= Ada_2005
1101 and then Nkind (N) = N_Selected_Component
1102 and then Is_Dispatching_Operation (Entity (Selector_Name (N)))
1103 then
1104 Analyze_Selected_Component (N);
996c8821 1105
747de90b
AC
1106 if Nkind (N) /= N_Selected_Component then
1107 return;
1108 end if;
1109 end if;
1110
b80a2b4b
AC
1111 -- The node is the name of the parameterless call. Preserve its
1112 -- descendants, which may be complex expressions.
1113
1114 Nam := Relocate_Node (N);
996ae0b0 1115
bc5f3720 1116 -- If overloaded, overload set belongs to new copy
996ae0b0
RK
1117
1118 Save_Interps (N, Nam);
1119
1120 -- Change node to parameterless function call (note that the
1121 -- Parameter_Associations associations field is left set to Empty,
1122 -- its normal default value since there are no parameters)
1123
1124 Change_Node (N, N_Function_Call);
1125 Set_Name (N, Nam);
1126 Set_Sloc (N, Sloc (Nam));
1127 Analyze_Call (N);
1128 end if;
1129
1130 elsif Nkind (N) = N_Parameter_Association then
1131 Check_Parameterless_Call (Explicit_Actual_Parameter (N));
e1d9659d
AC
1132
1133 elsif Nkind (N) = N_Operator_Symbol then
1134 Change_Operator_Symbol_To_String_Literal (N);
1135 Set_Is_Overloaded (N, False);
1136 Set_Etype (N, Any_String);
996ae0b0
RK
1137 end if;
1138 end Check_Parameterless_Call;
1139
c2a2dbcc
RD
1140 --------------------------------
1141 -- Is_Atomic_Ref_With_Address --
1142 --------------------------------
1143
1144 function Is_Atomic_Ref_With_Address (N : Node_Id) return Boolean is
1145 Pref : constant Node_Id := Prefix (N);
1146
1147 begin
1148 if not Is_Entity_Name (Pref) then
1149 return False;
1150
1151 else
1152 declare
1153 Pent : constant Entity_Id := Entity (Pref);
1154 Ptyp : constant Entity_Id := Etype (Pent);
1155 begin
1156 return not Is_Access_Type (Ptyp)
1157 and then (Is_Atomic (Ptyp) or else Is_Atomic (Pent))
1158 and then Present (Address_Clause (Pent));
1159 end;
1160 end if;
1161 end Is_Atomic_Ref_With_Address;
1162
67ce0d7e
RD
1163 -----------------------------
1164 -- Is_Definite_Access_Type --
1165 -----------------------------
1166
1167 function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1168 Btyp : constant Entity_Id := Base_Type (E);
1169 begin
1170 return Ekind (Btyp) = E_Access_Type
1171 or else (Ekind (Btyp) = E_Access_Subprogram_Type
72e9f2b9 1172 and then Comes_From_Source (Btyp));
67ce0d7e
RD
1173 end Is_Definite_Access_Type;
1174
996ae0b0
RK
1175 ----------------------
1176 -- Is_Predefined_Op --
1177 ----------------------
1178
1179 function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1180 begin
6a497607
AC
1181 -- Predefined operators are intrinsic subprograms
1182
1183 if not Is_Intrinsic_Subprogram (Nam) then
1184 return False;
1185 end if;
1186
1187 -- A call to a back-end builtin is never a predefined operator
1188
1189 if Is_Imported (Nam) and then Present (Interface_Name (Nam)) then
1190 return False;
1191 end if;
1192
1193 return not Is_Generic_Instance (Nam)
996ae0b0 1194 and then Chars (Nam) in Any_Operator_Name
6a497607 1195 and then (No (Alias (Nam)) or else Is_Predefined_Op (Alias (Nam)));
996ae0b0
RK
1196 end Is_Predefined_Op;
1197
1198 -----------------------------
1199 -- Make_Call_Into_Operator --
1200 -----------------------------
1201
1202 procedure Make_Call_Into_Operator
1203 (N : Node_Id;
1204 Typ : Entity_Id;
1205 Op_Id : Entity_Id)
1206 is
1207 Op_Name : constant Name_Id := Chars (Op_Id);
1208 Act1 : Node_Id := First_Actual (N);
1209 Act2 : Node_Id := Next_Actual (Act1);
1210 Error : Boolean := False;
2820d220
AC
1211 Func : constant Entity_Id := Entity (Name (N));
1212 Is_Binary : constant Boolean := Present (Act2);
996ae0b0
RK
1213 Op_Node : Node_Id;
1214 Opnd_Type : Entity_Id;
1215 Orig_Type : Entity_Id := Empty;
1216 Pack : Entity_Id;
1217
1218 type Kind_Test is access function (E : Entity_Id) return Boolean;
1219
996ae0b0 1220 function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
b4a4936b
AC
1221 -- If the operand is not universal, and the operator is given by an
1222 -- expanded name, verify that the operand has an interpretation with a
1223 -- type defined in the given scope of the operator.
996ae0b0
RK
1224
1225 function Type_In_P (Test : Kind_Test) return Entity_Id;
b4a4936b
AC
1226 -- Find a type of the given class in package Pack that contains the
1227 -- operator.
996ae0b0 1228
996ae0b0
RK
1229 ---------------------------
1230 -- Operand_Type_In_Scope --
1231 ---------------------------
1232
1233 function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1234 Nod : constant Node_Id := Right_Opnd (Op_Node);
1235 I : Interp_Index;
1236 It : Interp;
1237
1238 begin
1239 if not Is_Overloaded (Nod) then
1240 return Scope (Base_Type (Etype (Nod))) = S;
1241
1242 else
1243 Get_First_Interp (Nod, I, It);
996ae0b0 1244 while Present (It.Typ) loop
996ae0b0
RK
1245 if Scope (Base_Type (It.Typ)) = S then
1246 return True;
1247 end if;
1248
1249 Get_Next_Interp (I, It);
1250 end loop;
1251
1252 return False;
1253 end if;
1254 end Operand_Type_In_Scope;
1255
1256 ---------------
1257 -- Type_In_P --
1258 ---------------
1259
1260 function Type_In_P (Test : Kind_Test) return Entity_Id is
1261 E : Entity_Id;
1262
1263 function In_Decl return Boolean;
1264 -- Verify that node is not part of the type declaration for the
1265 -- candidate type, which would otherwise be invisible.
1266
1267 -------------
1268 -- In_Decl --
1269 -------------
1270
1271 function In_Decl return Boolean is
1272 Decl_Node : constant Node_Id := Parent (E);
1273 N2 : Node_Id;
1274
1275 begin
1276 N2 := N;
1277
1278 if Etype (E) = Any_Type then
1279 return True;
1280
1281 elsif No (Decl_Node) then
1282 return False;
1283
1284 else
1285 while Present (N2)
1286 and then Nkind (N2) /= N_Compilation_Unit
1287 loop
1288 if N2 = Decl_Node then
1289 return True;
1290 else
1291 N2 := Parent (N2);
1292 end if;
1293 end loop;
1294
1295 return False;
1296 end if;
1297 end In_Decl;
1298
1299 -- Start of processing for Type_In_P
1300
1301 begin
b4a4936b
AC
1302 -- If the context type is declared in the prefix package, this is the
1303 -- desired base type.
996ae0b0 1304
b4a4936b 1305 if Scope (Base_Type (Typ)) = Pack and then Test (Typ) then
996ae0b0
RK
1306 return Base_Type (Typ);
1307
1308 else
1309 E := First_Entity (Pack);
996ae0b0 1310 while Present (E) loop
445e5888 1311 if Test (E) and then not In_Decl then
996ae0b0
RK
1312 return E;
1313 end if;
1314
1315 Next_Entity (E);
1316 end loop;
1317
1318 return Empty;
1319 end if;
1320 end Type_In_P;
1321
996ae0b0
RK
1322 -- Start of processing for Make_Call_Into_Operator
1323
1324 begin
1325 Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1326
1327 -- Binary operator
1328
1329 if Is_Binary then
1330 Set_Left_Opnd (Op_Node, Relocate_Node (Act1));
1331 Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1332 Save_Interps (Act1, Left_Opnd (Op_Node));
1333 Save_Interps (Act2, Right_Opnd (Op_Node));
1334 Act1 := Left_Opnd (Op_Node);
1335 Act2 := Right_Opnd (Op_Node);
1336
1337 -- Unary operator
1338
1339 else
1340 Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1341 Save_Interps (Act1, Right_Opnd (Op_Node));
1342 Act1 := Right_Opnd (Op_Node);
1343 end if;
1344
1345 -- If the operator is denoted by an expanded name, and the prefix is
1346 -- not Standard, but the operator is a predefined one whose scope is
1347 -- Standard, then this is an implicit_operator, inserted as an
1348 -- interpretation by the procedure of the same name. This procedure
1349 -- overestimates the presence of implicit operators, because it does
1350 -- not examine the type of the operands. Verify now that the operand
1351 -- type appears in the given scope. If right operand is universal,
1352 -- check the other operand. In the case of concatenation, either
1353 -- argument can be the component type, so check the type of the result.
1354 -- If both arguments are literals, look for a type of the right kind
1355 -- defined in the given scope. This elaborate nonsense is brought to
1356 -- you courtesy of b33302a. The type itself must be frozen, so we must
1357 -- find the type of the proper class in the given scope.
1358
06f2efd7
TQ
1359 -- A final wrinkle is the multiplication operator for fixed point types,
1360 -- which is defined in Standard only, and not in the scope of the
b4a4936b 1361 -- fixed point type itself.
996ae0b0
RK
1362
1363 if Nkind (Name (N)) = N_Expanded_Name then
1364 Pack := Entity (Prefix (Name (N)));
1365
1115dd7e
ES
1366 -- If this is a package renaming, get renamed entity, which will be
1367 -- the scope of the operands if operaton is type-correct.
1368
1369 if Present (Renamed_Entity (Pack)) then
1370 Pack := Renamed_Entity (Pack);
1371 end if;
1372
06f2efd7
TQ
1373 -- If the entity being called is defined in the given package, it is
1374 -- a renaming of a predefined operator, and known to be legal.
996ae0b0
RK
1375
1376 if Scope (Entity (Name (N))) = Pack
1377 and then Pack /= Standard_Standard
1378 then
1379 null;
1380
9ebe3743
HK
1381 -- Visibility does not need to be checked in an instance: if the
1382 -- operator was not visible in the generic it has been diagnosed
1383 -- already, else there is an implicit copy of it in the instance.
1384
1385 elsif In_Instance then
1386 null;
1387
b69cd36a 1388 elsif Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide)
996ae0b0
RK
1389 and then Is_Fixed_Point_Type (Etype (Left_Opnd (Op_Node)))
1390 and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1391 then
1392 if Pack /= Standard_Standard then
1393 Error := True;
1394 end if;
1395
b4a4936b 1396 -- Ada 2005 AI-420: Predefined equality on Universal_Access is
06f2efd7 1397 -- available.
c8ef728f 1398
0791fbe9 1399 elsif Ada_Version >= Ada_2005
b69cd36a 1400 and then Nam_In (Op_Name, Name_Op_Eq, Name_Op_Ne)
c8ef728f
ES
1401 and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1402 then
1403 null;
1404
996ae0b0
RK
1405 else
1406 Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1407
1408 if Op_Name = Name_Op_Concat then
1409 Opnd_Type := Base_Type (Typ);
1410
1411 elsif (Scope (Opnd_Type) = Standard_Standard
1412 and then Is_Binary)
1413 or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1414 and then Is_Binary
1415 and then not Comes_From_Source (Opnd_Type))
1416 then
1417 Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1418 end if;
1419
1420 if Scope (Opnd_Type) = Standard_Standard then
1421
1422 -- Verify that the scope contains a type that corresponds to
1423 -- the given literal. Optimize the case where Pack is Standard.
1424
1425 if Pack /= Standard_Standard then
1426
1427 if Opnd_Type = Universal_Integer then
06f2efd7 1428 Orig_Type := Type_In_P (Is_Integer_Type'Access);
996ae0b0
RK
1429
1430 elsif Opnd_Type = Universal_Real then
1431 Orig_Type := Type_In_P (Is_Real_Type'Access);
1432
1433 elsif Opnd_Type = Any_String then
1434 Orig_Type := Type_In_P (Is_String_Type'Access);
1435
1436 elsif Opnd_Type = Any_Access then
06f2efd7 1437 Orig_Type := Type_In_P (Is_Definite_Access_Type'Access);
996ae0b0
RK
1438
1439 elsif Opnd_Type = Any_Composite then
1440 Orig_Type := Type_In_P (Is_Composite_Type'Access);
1441
1442 if Present (Orig_Type) then
1443 if Has_Private_Component (Orig_Type) then
1444 Orig_Type := Empty;
1445 else
1446 Set_Etype (Act1, Orig_Type);
1447
1448 if Is_Binary then
1449 Set_Etype (Act2, Orig_Type);
1450 end if;
1451 end if;
1452 end if;
1453
1454 else
1455 Orig_Type := Empty;
1456 end if;
1457
1458 Error := No (Orig_Type);
1459 end if;
1460
1461 elsif Ekind (Opnd_Type) = E_Allocator_Type
1462 and then No (Type_In_P (Is_Definite_Access_Type'Access))
1463 then
1464 Error := True;
1465
1466 -- If the type is defined elsewhere, and the operator is not
1467 -- defined in the given scope (by a renaming declaration, e.g.)
1468 -- then this is an error as well. If an extension of System is
1469 -- present, and the type may be defined there, Pack must be
1470 -- System itself.
1471
1472 elsif Scope (Opnd_Type) /= Pack
1473 and then Scope (Op_Id) /= Pack
1474 and then (No (System_Aux_Id)
1475 or else Scope (Opnd_Type) /= System_Aux_Id
1476 or else Pack /= Scope (System_Aux_Id))
1477 then
244e5a2c
AC
1478 if not Is_Overloaded (Right_Opnd (Op_Node)) then
1479 Error := True;
1480 else
1481 Error := not Operand_Type_In_Scope (Pack);
1482 end if;
996ae0b0
RK
1483
1484 elsif Pack = Standard_Standard
1485 and then not Operand_Type_In_Scope (Standard_Standard)
1486 then
1487 Error := True;
1488 end if;
1489 end if;
1490
1491 if Error then
1492 Error_Msg_Node_2 := Pack;
1493 Error_Msg_NE
1494 ("& not declared in&", N, Selector_Name (Name (N)));
1495 Set_Etype (N, Any_Type);
1496 return;
88b17d45
AC
1497
1498 -- Detect a mismatch between the context type and the result type
1499 -- in the named package, which is otherwise not detected if the
1500 -- operands are universal. Check is only needed if source entity is
1501 -- an operator, not a function that renames an operator.
1502
1503 elsif Nkind (Parent (N)) /= N_Type_Conversion
1504 and then Ekind (Entity (Name (N))) = E_Operator
1505 and then Is_Numeric_Type (Typ)
1506 and then not Is_Universal_Numeric_Type (Typ)
1507 and then Scope (Base_Type (Typ)) /= Pack
1508 and then not In_Instance
1509 then
1510 if Is_Fixed_Point_Type (Typ)
b69cd36a 1511 and then Nam_In (Op_Name, Name_Op_Multiply, Name_Op_Divide)
88b17d45
AC
1512 then
1513 -- Already checked above
1514
1515 null;
1516
e86a3a7e 1517 -- Operator may be defined in an extension of System
80c3be7a
AC
1518
1519 elsif Present (System_Aux_Id)
1520 and then Scope (Opnd_Type) = System_Aux_Id
1521 then
1522 null;
1523
88b17d45 1524 else
be5a1b93
TQ
1525 -- Could we use Wrong_Type here??? (this would require setting
1526 -- Etype (N) to the actual type found where Typ was expected).
1527
e86a3a7e 1528 Error_Msg_NE ("expect }", N, Typ);
88b17d45 1529 end if;
996ae0b0
RK
1530 end if;
1531 end if;
1532
1533 Set_Chars (Op_Node, Op_Name);
fbf5a39b
AC
1534
1535 if not Is_Private_Type (Etype (N)) then
1536 Set_Etype (Op_Node, Base_Type (Etype (N)));
1537 else
1538 Set_Etype (Op_Node, Etype (N));
1539 end if;
1540
2820d220
AC
1541 -- If this is a call to a function that renames a predefined equality,
1542 -- the renaming declaration provides a type that must be used to
1543 -- resolve the operands. This must be done now because resolution of
1544 -- the equality node will not resolve any remaining ambiguity, and it
1545 -- assumes that the first operand is not overloaded.
1546
b69cd36a 1547 if Nam_In (Op_Name, Name_Op_Eq, Name_Op_Ne)
2820d220
AC
1548 and then Ekind (Func) = E_Function
1549 and then Is_Overloaded (Act1)
1550 then
1551 Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1552 Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1553 end if;
1554
996ae0b0
RK
1555 Set_Entity (Op_Node, Op_Id);
1556 Generate_Reference (Op_Id, N, ' ');
45fc7ddb
HK
1557
1558 -- Do rewrite setting Comes_From_Source on the result if the original
1559 -- call came from source. Although it is not strictly the case that the
1560 -- operator as such comes from the source, logically it corresponds
1561 -- exactly to the function call in the source, so it should be marked
1562 -- this way (e.g. to make sure that validity checks work fine).
1563
1564 declare
1565 CS : constant Boolean := Comes_From_Source (N);
1566 begin
1567 Rewrite (N, Op_Node);
1568 Set_Comes_From_Source (N, CS);
1569 end;
fbf5a39b
AC
1570
1571 -- If this is an arithmetic operator and the result type is private,
1572 -- the operands and the result must be wrapped in conversion to
1573 -- expose the underlying numeric type and expand the proper checks,
1574 -- e.g. on division.
1575
1576 if Is_Private_Type (Typ) then
1577 case Nkind (N) is
5cc9353d
RD
1578 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1579 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
fbf5a39b
AC
1580 Resolve_Intrinsic_Operator (N, Typ);
1581
5cc9353d 1582 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
fbf5a39b
AC
1583 Resolve_Intrinsic_Unary_Operator (N, Typ);
1584
1585 when others =>
1586 Resolve (N, Typ);
1587 end case;
1588 else
1589 Resolve (N, Typ);
1590 end if;
466c2127
AC
1591
1592 -- If in ASIS_Mode, propagate operand types to original actuals of
d7a3e18c 1593 -- function call, which would otherwise not be fully resolved. If
00ba7be8
AC
1594 -- the call has already been constant-folded, nothing to do. We
1595 -- relocate the operand nodes rather than copy them, to preserve
1596 -- original_node pointers, given that the operands themselves may
c61ef416
AC
1597 -- have been rewritten. If the call was itself a rewriting of an
1598 -- operator node, nothing to do.
466c2127 1599
c61ef416
AC
1600 if ASIS_Mode
1601 and then Nkind (N) in N_Op
1602 and then Nkind (Original_Node (N)) = N_Function_Call
1603 then
c05ba1f1 1604 declare
5fde9688 1605 L : Node_Id;
c05ba1f1
AC
1606 R : constant Node_Id := Right_Opnd (N);
1607
1608 Old_First : constant Node_Id :=
1609 First (Parameter_Associations (Original_Node (N)));
1610 Old_Sec : Node_Id;
1611
1612 begin
1613 if Is_Binary then
5fde9688
AC
1614 L := Left_Opnd (N);
1615 Old_Sec := Next (Old_First);
c05ba1f1
AC
1616
1617 -- If the original call has named associations, replace the
1618 -- explicit actual parameter in the association with the proper
1619 -- resolved operand.
1620
1621 if Nkind (Old_First) = N_Parameter_Association then
1622 if Chars (Selector_Name (Old_First)) =
1623 Chars (First_Entity (Op_Id))
1624 then
1625 Rewrite (Explicit_Actual_Parameter (Old_First),
1626 Relocate_Node (L));
1627 else
1628 Rewrite (Explicit_Actual_Parameter (Old_First),
1629 Relocate_Node (R));
1630 end if;
1631
1632 else
1633 Rewrite (Old_First, Relocate_Node (L));
1634 end if;
1635
1636 if Nkind (Old_Sec) = N_Parameter_Association then
1637 if Chars (Selector_Name (Old_Sec)) =
1638 Chars (First_Entity (Op_Id))
1639 then
1640 Rewrite (Explicit_Actual_Parameter (Old_Sec),
1641 Relocate_Node (L));
1642 else
1643 Rewrite (Explicit_Actual_Parameter (Old_Sec),
1644 Relocate_Node (R));
1645 end if;
1646
1647 else
1648 Rewrite (Old_Sec, Relocate_Node (R));
1649 end if;
1650
1651 else
1652 if Nkind (Old_First) = N_Parameter_Association then
1653 Rewrite (Explicit_Actual_Parameter (Old_First),
1654 Relocate_Node (R));
1655 else
1656 Rewrite (Old_First, Relocate_Node (R));
1657 end if;
1658 end if;
1659 end;
3699edc4
AC
1660
1661 Set_Parent (Original_Node (N), Parent (N));
466c2127 1662 end if;
996ae0b0
RK
1663 end Make_Call_Into_Operator;
1664
1665 -------------------
1666 -- Operator_Kind --
1667 -------------------
1668
1669 function Operator_Kind
1670 (Op_Name : Name_Id;
0ab80019 1671 Is_Binary : Boolean) return Node_Kind
996ae0b0
RK
1672 is
1673 Kind : Node_Kind;
1674
1675 begin
b0186f71
AC
1676 -- Use CASE statement or array???
1677
996ae0b0 1678 if Is_Binary then
1b1d88b1 1679 if Op_Name = Name_Op_And then
aa5147f0 1680 Kind := N_Op_And;
1b1d88b1 1681 elsif Op_Name = Name_Op_Or then
aa5147f0 1682 Kind := N_Op_Or;
1b1d88b1 1683 elsif Op_Name = Name_Op_Xor then
aa5147f0 1684 Kind := N_Op_Xor;
1b1d88b1 1685 elsif Op_Name = Name_Op_Eq then
aa5147f0 1686 Kind := N_Op_Eq;
1b1d88b1 1687 elsif Op_Name = Name_Op_Ne then
aa5147f0 1688 Kind := N_Op_Ne;
1b1d88b1 1689 elsif Op_Name = Name_Op_Lt then
aa5147f0 1690 Kind := N_Op_Lt;
1b1d88b1 1691 elsif Op_Name = Name_Op_Le then
aa5147f0 1692 Kind := N_Op_Le;
1b1d88b1 1693 elsif Op_Name = Name_Op_Gt then
aa5147f0 1694 Kind := N_Op_Gt;
1b1d88b1 1695 elsif Op_Name = Name_Op_Ge then
aa5147f0 1696 Kind := N_Op_Ge;
1b1d88b1 1697 elsif Op_Name = Name_Op_Add then
aa5147f0 1698 Kind := N_Op_Add;
1b1d88b1 1699 elsif Op_Name = Name_Op_Subtract then
aa5147f0 1700 Kind := N_Op_Subtract;
1b1d88b1 1701 elsif Op_Name = Name_Op_Concat then
aa5147f0 1702 Kind := N_Op_Concat;
1b1d88b1 1703 elsif Op_Name = Name_Op_Multiply then
aa5147f0 1704 Kind := N_Op_Multiply;
1b1d88b1 1705 elsif Op_Name = Name_Op_Divide then
aa5147f0 1706 Kind := N_Op_Divide;
1b1d88b1 1707 elsif Op_Name = Name_Op_Mod then
aa5147f0 1708 Kind := N_Op_Mod;
1b1d88b1 1709 elsif Op_Name = Name_Op_Rem then
aa5147f0 1710 Kind := N_Op_Rem;
1b1d88b1 1711 elsif Op_Name = Name_Op_Expon then
aa5147f0 1712 Kind := N_Op_Expon;
996ae0b0
RK
1713 else
1714 raise Program_Error;
1715 end if;
1716
1717 -- Unary operators
1718
1719 else
1b1d88b1 1720 if Op_Name = Name_Op_Add then
aa5147f0 1721 Kind := N_Op_Plus;
1b1d88b1 1722 elsif Op_Name = Name_Op_Subtract then
aa5147f0 1723 Kind := N_Op_Minus;
1b1d88b1 1724 elsif Op_Name = Name_Op_Abs then
aa5147f0 1725 Kind := N_Op_Abs;
1b1d88b1 1726 elsif Op_Name = Name_Op_Not then
aa5147f0 1727 Kind := N_Op_Not;
996ae0b0
RK
1728 else
1729 raise Program_Error;
1730 end if;
1731 end if;
1732
1733 return Kind;
1734 end Operator_Kind;
1735
45fc7ddb
HK
1736 ----------------------------
1737 -- Preanalyze_And_Resolve --
1738 ----------------------------
996ae0b0 1739
45fc7ddb 1740 procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
996ae0b0
RK
1741 Save_Full_Analysis : constant Boolean := Full_Analysis;
1742
1743 begin
1744 Full_Analysis := False;
1745 Expander_Mode_Save_And_Set (False);
1746
a7f1b24f
RD
1747 -- Normally, we suppress all checks for this preanalysis. There is no
1748 -- point in processing them now, since they will be applied properly
1749 -- and in the proper location when the default expressions reanalyzed
1750 -- and reexpanded later on. We will also have more information at that
1751 -- point for possible suppression of individual checks.
1115dd7e 1752
06b599fd
YM
1753 -- However, in SPARK mode, most expansion is suppressed, and this
1754 -- later reanalysis and reexpansion may not occur. SPARK mode does
a7f1b24f 1755 -- require the setting of checking flags for proof purposes, so we
06b599fd 1756 -- do the SPARK preanalysis without suppressing checks.
a7f1b24f 1757
06b599fd 1758 -- This special handling for SPARK mode is required for example in the
a7f1b24f
RD
1759 -- case of Ada 2012 constructs such as quantified expressions, which are
1760 -- expanded in two separate steps.
996ae0b0 1761
f5da7a97 1762 if GNATprove_Mode then
1115dd7e 1763 Analyze_And_Resolve (N, T);
1115dd7e
ES
1764 else
1765 Analyze_And_Resolve (N, T, Suppress => All_Checks);
1766 end if;
996ae0b0
RK
1767
1768 Expander_Mode_Restore;
1769 Full_Analysis := Save_Full_Analysis;
45fc7ddb 1770 end Preanalyze_And_Resolve;
996ae0b0 1771
a77842bd 1772 -- Version without context type
996ae0b0 1773
45fc7ddb 1774 procedure Preanalyze_And_Resolve (N : Node_Id) is
996ae0b0
RK
1775 Save_Full_Analysis : constant Boolean := Full_Analysis;
1776
1777 begin
1778 Full_Analysis := False;
1779 Expander_Mode_Save_And_Set (False);
1780
1781 Analyze (N);
1782 Resolve (N, Etype (N), Suppress => All_Checks);
1783
1784 Expander_Mode_Restore;
1785 Full_Analysis := Save_Full_Analysis;
45fc7ddb 1786 end Preanalyze_And_Resolve;
996ae0b0
RK
1787
1788 ----------------------------------
1789 -- Replace_Actual_Discriminants --
1790 ----------------------------------
1791
1792 procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1793 Loc : constant Source_Ptr := Sloc (N);
1794 Tsk : Node_Id := Empty;
1795
1796 function Process_Discr (Nod : Node_Id) return Traverse_Result;
e0296583 1797 -- Comment needed???
996ae0b0
RK
1798
1799 -------------------
1800 -- Process_Discr --
1801 -------------------
1802
1803 function Process_Discr (Nod : Node_Id) return Traverse_Result is
1804 Ent : Entity_Id;
1805
1806 begin
1807 if Nkind (Nod) = N_Identifier then
1808 Ent := Entity (Nod);
1809
1810 if Present (Ent)
1811 and then Ekind (Ent) = E_Discriminant
1812 then
1813 Rewrite (Nod,
1814 Make_Selected_Component (Loc,
1815 Prefix => New_Copy_Tree (Tsk, New_Sloc => Loc),
1816 Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1817
1818 Set_Etype (Nod, Etype (Ent));
1819 end if;
1820
1821 end if;
1822
1823 return OK;
1824 end Process_Discr;
1825
1826 procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1827
1828 -- Start of processing for Replace_Actual_Discriminants
1829
1830 begin
4460a9bc 1831 if not Expander_Active then
996ae0b0
RK
1832 return;
1833 end if;
1834
1835 if Nkind (Name (N)) = N_Selected_Component then
1836 Tsk := Prefix (Name (N));
1837
1838 elsif Nkind (Name (N)) = N_Indexed_Component then
1839 Tsk := Prefix (Prefix (Name (N)));
1840 end if;
1841
1842 if No (Tsk) then
1843 return;
1844 else
1845 Replace_Discrs (Default);
1846 end if;
1847 end Replace_Actual_Discriminants;
1848
1849 -------------
1850 -- Resolve --
1851 -------------
1852
1853 procedure Resolve (N : Node_Id; Typ : Entity_Id) is
dae2b8ea
HK
1854 Ambiguous : Boolean := False;
1855 Ctx_Type : Entity_Id := Typ;
1856 Expr_Type : Entity_Id := Empty; -- prevent junk warning
1857 Err_Type : Entity_Id := Empty;
1858 Found : Boolean := False;
1859 From_Lib : Boolean;
996ae0b0 1860 I : Interp_Index;
dae2b8ea 1861 I1 : Interp_Index := 0; -- prevent junk warning
996ae0b0
RK
1862 It : Interp;
1863 It1 : Interp;
996ae0b0 1864 Seen : Entity_Id := Empty; -- prevent junk warning
dae2b8ea
HK
1865
1866 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1867 -- Determine whether a node comes from a predefined library unit or
1868 -- Standard.
996ae0b0
RK
1869
1870 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1871 -- Try and fix up a literal so that it matches its expected type. New
1872 -- literals are manufactured if necessary to avoid cascaded errors.
1873
7415029d
AC
1874 procedure Report_Ambiguous_Argument;
1875 -- Additional diagnostics when an ambiguous call has an ambiguous
1876 -- argument (typically a controlling actual).
1877
996ae0b0
RK
1878 procedure Resolution_Failed;
1879 -- Called when attempt at resolving current expression fails
1880
dae2b8ea
HK
1881 ------------------------------------
1882 -- Comes_From_Predefined_Lib_Unit --
1883 -------------------------------------
1884
1885 function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1886 begin
1887 return
1888 Sloc (Nod) = Standard_Location
5cc9353d
RD
1889 or else Is_Predefined_File_Name
1890 (Unit_File_Name (Get_Source_Unit (Sloc (Nod))));
dae2b8ea
HK
1891 end Comes_From_Predefined_Lib_Unit;
1892
996ae0b0
RK
1893 --------------------
1894 -- Patch_Up_Value --
1895 --------------------
1896
1897 procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1898 begin
e0296583 1899 if Nkind (N) = N_Integer_Literal and then Is_Real_Type (Typ) then
996ae0b0
RK
1900 Rewrite (N,
1901 Make_Real_Literal (Sloc (N),
1902 Realval => UR_From_Uint (Intval (N))));
1903 Set_Etype (N, Universal_Real);
1904 Set_Is_Static_Expression (N);
1905
e0296583 1906 elsif Nkind (N) = N_Real_Literal and then Is_Integer_Type (Typ) then
996ae0b0
RK
1907 Rewrite (N,
1908 Make_Integer_Literal (Sloc (N),
1909 Intval => UR_To_Uint (Realval (N))));
1910 Set_Etype (N, Universal_Integer);
1911 Set_Is_Static_Expression (N);
45fc7ddb 1912
996ae0b0 1913 elsif Nkind (N) = N_String_Literal
e0296583 1914 and then Is_Character_Type (Typ)
996ae0b0
RK
1915 then
1916 Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1917 Rewrite (N,
1918 Make_Character_Literal (Sloc (N),
1919 Chars => Name_Find,
82c80734
RD
1920 Char_Literal_Value =>
1921 UI_From_Int (Character'Pos ('A'))));
996ae0b0
RK
1922 Set_Etype (N, Any_Character);
1923 Set_Is_Static_Expression (N);
1924
e0296583 1925 elsif Nkind (N) /= N_String_Literal and then Is_String_Type (Typ) then
996ae0b0
RK
1926 Rewrite (N,
1927 Make_String_Literal (Sloc (N),
1928 Strval => End_String));
1929
1930 elsif Nkind (N) = N_Range then
e0296583 1931 Patch_Up_Value (Low_Bound (N), Typ);
996ae0b0
RK
1932 Patch_Up_Value (High_Bound (N), Typ);
1933 end if;
1934 end Patch_Up_Value;
1935
7415029d
AC
1936 -------------------------------
1937 -- Report_Ambiguous_Argument --
1938 -------------------------------
1939
1940 procedure Report_Ambiguous_Argument is
1941 Arg : constant Node_Id := First (Parameter_Associations (N));
1942 I : Interp_Index;
1943 It : Interp;
1944
1945 begin
1946 if Nkind (Arg) = N_Function_Call
1947 and then Is_Entity_Name (Name (Arg))
1948 and then Is_Overloaded (Name (Arg))
1949 then
ed2233dc 1950 Error_Msg_NE ("ambiguous call to&", Arg, Name (Arg));
7415029d 1951
e0296583 1952 -- Could use comments on what is going on here???
bfc07071 1953
7415029d
AC
1954 Get_First_Interp (Name (Arg), I, It);
1955 while Present (It.Nam) loop
1956 Error_Msg_Sloc := Sloc (It.Nam);
1957
1958 if Nkind (Parent (It.Nam)) = N_Full_Type_Declaration then
ed2233dc 1959 Error_Msg_N ("interpretation (inherited) #!", Arg);
7415029d 1960 else
ed2233dc 1961 Error_Msg_N ("interpretation #!", Arg);
7415029d
AC
1962 end if;
1963
1964 Get_Next_Interp (I, It);
1965 end loop;
1966 end if;
1967 end Report_Ambiguous_Argument;
1968
996ae0b0
RK
1969 -----------------------
1970 -- Resolution_Failed --
1971 -----------------------
1972
1973 procedure Resolution_Failed is
1974 begin
1975 Patch_Up_Value (N, Typ);
1976 Set_Etype (N, Typ);
1977 Debug_A_Exit ("resolving ", N, " (done, resolution failed)");
1978 Set_Is_Overloaded (N, False);
1979
1980 -- The caller will return without calling the expander, so we need
1981 -- to set the analyzed flag. Note that it is fine to set Analyzed
1982 -- to True even if we are in the middle of a shallow analysis,
1983 -- (see the spec of sem for more details) since this is an error
1984 -- situation anyway, and there is no point in repeating the
1985 -- analysis later (indeed it won't work to repeat it later, since
1986 -- we haven't got a clear resolution of which entity is being
1987 -- referenced.)
1988
1989 Set_Analyzed (N, True);
1990 return;
1991 end Resolution_Failed;
1992
1af4455a
HK
1993 -- Local variables
1994
1995 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
1996
996ae0b0
RK
1997 -- Start of processing for Resolve
1998
1999 begin
5c736541
RD
2000 if N = Error then
2001 return;
2002 end if;
2003
1af4455a
HK
2004 -- A declaration may be subject to pragma Ghost. Set the mode now to
2005 -- ensure that any nodes generated during analysis and expansion are
2006 -- marked as Ghost.
2007
2008 if Is_Declaration (N) then
2009 Set_Ghost_Mode (N);
2010 end if;
2011
e0296583
AC
2012 -- Access attribute on remote subprogram cannot be used for a non-remote
2013 -- access-to-subprogram type.
996ae0b0
RK
2014
2015 if Nkind (N) = N_Attribute_Reference
b69cd36a
AC
2016 and then Nam_In (Attribute_Name (N), Name_Access,
2017 Name_Unrestricted_Access,
2018 Name_Unchecked_Access)
996ae0b0
RK
2019 and then Comes_From_Source (N)
2020 and then Is_Entity_Name (Prefix (N))
2021 and then Is_Subprogram (Entity (Prefix (N)))
2022 and then Is_Remote_Call_Interface (Entity (Prefix (N)))
2023 and then not Is_Remote_Access_To_Subprogram_Type (Typ)
2024 then
2025 Error_Msg_N
2026 ("prefix must statically denote a non-remote subprogram", N);
2027 end if;
2028
dae2b8ea
HK
2029 From_Lib := Comes_From_Predefined_Lib_Unit (N);
2030
996ae0b0
RK
2031 -- If the context is a Remote_Access_To_Subprogram, access attributes
2032 -- must be resolved with the corresponding fat pointer. There is no need
2033 -- to check for the attribute name since the return type of an
2034 -- attribute is never a remote type.
2035
2036 if Nkind (N) = N_Attribute_Reference
2037 and then Comes_From_Source (N)
19fb051c 2038 and then (Is_Remote_Call_Interface (Typ) or else Is_Remote_Types (Typ))
996ae0b0
RK
2039 then
2040 declare
2041 Attr : constant Attribute_Id :=
2042 Get_Attribute_Id (Attribute_Name (N));
2043 Pref : constant Node_Id := Prefix (N);
2044 Decl : Node_Id;
2045 Spec : Node_Id;
2046 Is_Remote : Boolean := True;
2047
2048 begin
a77842bd 2049 -- Check that Typ is a remote access-to-subprogram type
996ae0b0 2050
a77842bd 2051 if Is_Remote_Access_To_Subprogram_Type (Typ) then
955871d3 2052
996ae0b0
RK
2053 -- Prefix (N) must statically denote a remote subprogram
2054 -- declared in a package specification.
2055
799d0e05
AC
2056 if Attr = Attribute_Access or else
2057 Attr = Attribute_Unchecked_Access or else
2058 Attr = Attribute_Unrestricted_Access
2059 then
996ae0b0
RK
2060 Decl := Unit_Declaration_Node (Entity (Pref));
2061
2062 if Nkind (Decl) = N_Subprogram_Body then
2063 Spec := Corresponding_Spec (Decl);
2064
b8e6830b 2065 if Present (Spec) then
996ae0b0
RK
2066 Decl := Unit_Declaration_Node (Spec);
2067 end if;
2068 end if;
2069
2070 Spec := Parent (Decl);
2071
2072 if not Is_Entity_Name (Prefix (N))
2073 or else Nkind (Spec) /= N_Package_Specification
2074 or else
2075 not Is_Remote_Call_Interface (Defining_Entity (Spec))
2076 then
2077 Is_Remote := False;
2078 Error_Msg_N
2079 ("prefix must statically denote a remote subprogram ",
2080 N);
2081 end if;
996ae0b0 2082
799d0e05
AC
2083 -- If we are generating code in distributed mode, perform
2084 -- semantic checks against corresponding remote entities.
fbf5a39b 2085
4460a9bc 2086 if Expander_Active
799d0e05
AC
2087 and then Get_PCS_Name /= Name_No_DSA
2088 then
2089 Check_Subtype_Conformant
2090 (New_Id => Entity (Prefix (N)),
2091 Old_Id => Designated_Type
2092 (Corresponding_Remote_Type (Typ)),
2093 Err_Loc => N);
2094
2095 if Is_Remote then
2096 Process_Remote_AST_Attribute (N, Typ);
2097 end if;
996ae0b0
RK
2098 end if;
2099 end if;
2100 end if;
2101 end;
2102 end if;
2103
2104 Debug_A_Entry ("resolving ", N);
fe58fea7 2105
ee1a7572
AC
2106 if Debug_Flag_V then
2107 Write_Overloads (N);
2108 end if;
996ae0b0 2109
07fc65c4
GB
2110 if Comes_From_Source (N) then
2111 if Is_Fixed_Point_Type (Typ) then
2112 Check_Restriction (No_Fixed_Point, N);
996ae0b0 2113
07fc65c4
GB
2114 elsif Is_Floating_Point_Type (Typ)
2115 and then Typ /= Universal_Real
2116 and then Typ /= Any_Real
2117 then
2118 Check_Restriction (No_Floating_Point, N);
2119 end if;
996ae0b0
RK
2120 end if;
2121
2122 -- Return if already analyzed
2123
2124 if Analyzed (N) then
2125 Debug_A_Exit ("resolving ", N, " (done, already analyzed)");
dec6faf1 2126 Analyze_Dimension (N);
1af4455a 2127 Ghost_Mode := Save_Ghost_Mode;
996ae0b0
RK
2128 return;
2129
3e586e10
AC
2130 -- Any case of Any_Type as the Etype value means that we had a
2131 -- previous error.
1486a00e
AC
2132
2133 elsif Etype (N) = Any_Type then
996ae0b0 2134 Debug_A_Exit ("resolving ", N, " (done, Etype = Any_Type)");
1af4455a 2135 Ghost_Mode := Save_Ghost_Mode;
996ae0b0
RK
2136 return;
2137 end if;
2138
2139 Check_Parameterless_Call (N);
2140
064f4527
TQ
2141 -- The resolution of an Expression_With_Actions is determined by
2142 -- its Expression.
2143
2144 if Nkind (N) = N_Expression_With_Actions then
2145 Resolve (Expression (N), Typ);
2146
2147 Found := True;
2148 Expr_Type := Etype (Expression (N));
2149
996ae0b0
RK
2150 -- If not overloaded, then we know the type, and all that needs doing
2151 -- is to check that this type is compatible with the context.
2152
064f4527 2153 elsif not Is_Overloaded (N) then
996ae0b0
RK
2154 Found := Covers (Typ, Etype (N));
2155 Expr_Type := Etype (N);
2156
2157 -- In the overloaded case, we must select the interpretation that
2158 -- is compatible with the context (i.e. the type passed to Resolve)
2159
2160 else
996ae0b0
RK
2161 -- Loop through possible interpretations
2162
1420b484 2163 Get_First_Interp (N, I, It);
996ae0b0 2164 Interp_Loop : while Present (It.Typ) loop
ee1a7572
AC
2165 if Debug_Flag_V then
2166 Write_Str ("Interp: ");
2167 Write_Interp (It);
2168 end if;
2169
996ae0b0 2170 -- We are only interested in interpretations that are compatible
aa5147f0 2171 -- with the expected type, any other interpretations are ignored.
996ae0b0 2172
fbf5a39b
AC
2173 if not Covers (Typ, It.Typ) then
2174 if Debug_Flag_V then
2175 Write_Str (" interpretation incompatible with context");
2176 Write_Eol;
2177 end if;
996ae0b0 2178
fbf5a39b 2179 else
aa5147f0
ES
2180 -- Skip the current interpretation if it is disabled by an
2181 -- abstract operator. This action is performed only when the
2182 -- type against which we are resolving is the same as the
2183 -- type of the interpretation.
2184
0791fbe9 2185 if Ada_Version >= Ada_2005
aa5147f0
ES
2186 and then It.Typ = Typ
2187 and then Typ /= Universal_Integer
2188 and then Typ /= Universal_Real
2189 and then Present (It.Abstract_Op)
2190 then
ee1a7572
AC
2191 if Debug_Flag_V then
2192 Write_Line ("Skip.");
2193 end if;
2194
aa5147f0
ES
2195 goto Continue;
2196 end if;
2197
996ae0b0
RK
2198 -- First matching interpretation
2199
2200 if not Found then
2201 Found := True;
2202 I1 := I;
2203 Seen := It.Nam;
2204 Expr_Type := It.Typ;
2205
fbf5a39b 2206 -- Matching interpretation that is not the first, maybe an
996ae0b0
RK
2207 -- error, but there are some cases where preference rules are
2208 -- used to choose between the two possibilities. These and
2209 -- some more obscure cases are handled in Disambiguate.
2210
2211 else
90b51aaf
AC
2212 -- If the current statement is part of a predefined library
2213 -- unit, then all interpretations which come from user level
2214 -- packages should not be considered. Check previous and
2215 -- current one.
2216
2217 if From_Lib then
2218 if not Comes_From_Predefined_Lib_Unit (It.Nam) then
2219 goto Continue;
2220
2221 elsif not Comes_From_Predefined_Lib_Unit (Seen) then
2222
c2e54001 2223 -- Previous interpretation must be discarded
90b51aaf 2224
c2e54001
AC
2225 I1 := I;
2226 Seen := It.Nam;
90b51aaf
AC
2227 Expr_Type := It.Typ;
2228 Set_Entity (N, Seen);
2229 goto Continue;
2230 end if;
dae2b8ea
HK
2231 end if;
2232
c2e54001 2233 -- Otherwise apply further disambiguation steps
90b51aaf 2234
996ae0b0
RK
2235 Error_Msg_Sloc := Sloc (Seen);
2236 It1 := Disambiguate (N, I1, I, Typ);
2237
fbf5a39b
AC
2238 -- Disambiguation has succeeded. Skip the remaining
2239 -- interpretations.
996ae0b0 2240
fbf5a39b
AC
2241 if It1 /= No_Interp then
2242 Seen := It1.Nam;
2243 Expr_Type := It1.Typ;
2244
2245 while Present (It.Typ) loop
2246 Get_Next_Interp (I, It);
2247 end loop;
2248
2249 else
996ae0b0
RK
2250 -- Before we issue an ambiguity complaint, check for
2251 -- the case of a subprogram call where at least one
2252 -- of the arguments is Any_Type, and if so, suppress
2253 -- the message, since it is a cascaded error.
2254
d3b00ce3 2255 if Nkind (N) in N_Subprogram_Call then
996ae0b0 2256 declare
1420b484 2257 A : Node_Id;
996ae0b0
RK
2258 E : Node_Id;
2259
2260 begin
1420b484 2261 A := First_Actual (N);
996ae0b0
RK
2262 while Present (A) loop
2263 E := A;
2264
2265 if Nkind (E) = N_Parameter_Association then
2266 E := Explicit_Actual_Parameter (E);
2267 end if;
2268
2269 if Etype (E) = Any_Type then
2270 if Debug_Flag_V then
2271 Write_Str ("Any_Type in call");
2272 Write_Eol;
2273 end if;
2274
2275 exit Interp_Loop;
2276 end if;
2277
2278 Next_Actual (A);
2279 end loop;
2280 end;
2281
aa5147f0 2282 elsif Nkind (N) in N_Binary_Op
996ae0b0
RK
2283 and then (Etype (Left_Opnd (N)) = Any_Type
2284 or else Etype (Right_Opnd (N)) = Any_Type)
2285 then
2286 exit Interp_Loop;
2287
21d7ef70 2288 elsif Nkind (N) in N_Unary_Op
996ae0b0
RK
2289 and then Etype (Right_Opnd (N)) = Any_Type
2290 then
2291 exit Interp_Loop;
2292 end if;
2293
2294 -- Not that special case, so issue message using the
2295 -- flag Ambiguous to control printing of the header
2296 -- message only at the start of an ambiguous set.
2297
2298 if not Ambiguous then
aa180613
RD
2299 if Nkind (N) = N_Function_Call
2300 and then Nkind (Name (N)) = N_Explicit_Dereference
2301 then
ed2233dc 2302 Error_Msg_N
aa180613
RD
2303 ("ambiguous expression "
2304 & "(cannot resolve indirect call)!", N);
2305 else
483c78cb 2306 Error_Msg_NE -- CODEFIX
aa180613
RD
2307 ("ambiguous expression (cannot resolve&)!",
2308 N, It.Nam);
2309 end if;
fbf5a39b 2310
996ae0b0 2311 Ambiguous := True;
0669bebe
GB
2312
2313 if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
ed2233dc 2314 Error_Msg_N
0669bebe
GB
2315 ("\\possible interpretation (inherited)#!", N);
2316 else
4e7a4f6e
AC
2317 Error_Msg_N -- CODEFIX
2318 ("\\possible interpretation#!", N);
0669bebe 2319 end if;
7415029d 2320
d3b00ce3 2321 if Nkind (N) in N_Subprogram_Call
7415029d
AC
2322 and then Present (Parameter_Associations (N))
2323 then
2324 Report_Ambiguous_Argument;
2325 end if;
996ae0b0
RK
2326 end if;
2327
2328 Error_Msg_Sloc := Sloc (It.Nam);
996ae0b0 2329
fbf5a39b 2330 -- By default, the error message refers to the candidate
0669bebe
GB
2331 -- interpretation. But if it is a predefined operator, it
2332 -- is implicitly declared at the declaration of the type
2333 -- of the operand. Recover the sloc of that declaration
2334 -- for the error message.
fbf5a39b
AC
2335
2336 if Nkind (N) in N_Op
2337 and then Scope (It.Nam) = Standard_Standard
2338 and then not Is_Overloaded (Right_Opnd (N))
0669bebe
GB
2339 and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2340 Standard_Standard
fbf5a39b
AC
2341 then
2342 Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2343
2344 if Comes_From_Source (Err_Type)
2345 and then Present (Parent (Err_Type))
2346 then
2347 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2348 end if;
2349
2350 elsif Nkind (N) in N_Binary_Op
2351 and then Scope (It.Nam) = Standard_Standard
2352 and then not Is_Overloaded (Left_Opnd (N))
0669bebe
GB
2353 and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2354 Standard_Standard
fbf5a39b
AC
2355 then
2356 Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2357
2358 if Comes_From_Source (Err_Type)
2359 and then Present (Parent (Err_Type))
2360 then
2361 Error_Msg_Sloc := Sloc (Parent (Err_Type));
2362 end if;
aa180613
RD
2363
2364 -- If this is an indirect call, use the subprogram_type
5cc9353d
RD
2365 -- in the message, to have a meaningful location. Also
2366 -- indicate if this is an inherited operation, created
2367 -- by a type declaration.
aa180613
RD
2368
2369 elsif Nkind (N) = N_Function_Call
2370 and then Nkind (Name (N)) = N_Explicit_Dereference
2371 and then Is_Type (It.Nam)
2372 then
2373 Err_Type := It.Nam;
2374 Error_Msg_Sloc :=
2375 Sloc (Associated_Node_For_Itype (Err_Type));
fbf5a39b
AC
2376 else
2377 Err_Type := Empty;
2378 end if;
2379
2380 if Nkind (N) in N_Op
2381 and then Scope (It.Nam) = Standard_Standard
2382 and then Present (Err_Type)
2383 then
aa5147f0
ES
2384 -- Special-case the message for universal_fixed
2385 -- operators, which are not declared with the type
2386 -- of the operand, but appear forever in Standard.
2387
9fe696a3 2388 if It.Typ = Universal_Fixed
aa5147f0
ES
2389 and then Scope (It.Nam) = Standard_Standard
2390 then
ed2233dc 2391 Error_Msg_N
1486a00e
AC
2392 ("\\possible interpretation as universal_fixed "
2393 & "operation (RM 4.5.5 (19))", N);
aa5147f0 2394 else
ed2233dc 2395 Error_Msg_N
aa5147f0
ES
2396 ("\\possible interpretation (predefined)#!", N);
2397 end if;
aa180613
RD
2398
2399 elsif
2400 Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2401 then
ed2233dc 2402 Error_Msg_N
aa180613 2403 ("\\possible interpretation (inherited)#!", N);
fbf5a39b 2404 else
4e7a4f6e
AC
2405 Error_Msg_N -- CODEFIX
2406 ("\\possible interpretation#!", N);
fbf5a39b 2407 end if;
996ae0b0 2408
996ae0b0
RK
2409 end if;
2410 end if;
2411
0669bebe
GB
2412 -- We have a matching interpretation, Expr_Type is the type
2413 -- from this interpretation, and Seen is the entity.
996ae0b0 2414
0669bebe
GB
2415 -- For an operator, just set the entity name. The type will be
2416 -- set by the specific operator resolution routine.
996ae0b0
RK
2417
2418 if Nkind (N) in N_Op then
2419 Set_Entity (N, Seen);
2420 Generate_Reference (Seen, N);
2421
19d846a0
RD
2422 elsif Nkind (N) = N_Case_Expression then
2423 Set_Etype (N, Expr_Type);
2424
996ae0b0
RK
2425 elsif Nkind (N) = N_Character_Literal then
2426 Set_Etype (N, Expr_Type);
2427
9b16cb57 2428 elsif Nkind (N) = N_If_Expression then
e0ba1bfd
ES
2429 Set_Etype (N, Expr_Type);
2430
dedac3eb
RD
2431 -- AI05-0139-2: Expression is overloaded because type has
2432 -- implicit dereference. If type matches context, no implicit
2433 -- dereference is involved.
44a10091
AC
2434
2435 elsif Has_Implicit_Dereference (Expr_Type) then
2436 Set_Etype (N, Expr_Type);
2437 Set_Is_Overloaded (N, False);
2438 exit Interp_Loop;
2439
2440 elsif Is_Overloaded (N)
2441 and then Present (It.Nam)
2442 and then Ekind (It.Nam) = E_Discriminant
2443 and then Has_Implicit_Dereference (It.Nam)
2444 then
5f50020a
ES
2445 -- If the node is a general indexing, the dereference is
2446 -- is inserted when resolving the rewritten form, else
2447 -- insert it now.
2448
2449 if Nkind (N) /= N_Indexed_Component
2450 or else No (Generalized_Indexing (N))
2451 then
2452 Build_Explicit_Dereference (N, It.Nam);
2453 end if;
44a10091 2454
996ae0b0 2455 -- For an explicit dereference, attribute reference, range,
0669bebe
GB
2456 -- short-circuit form (which is not an operator node), or call
2457 -- with a name that is an explicit dereference, there is
2458 -- nothing to be done at this point.
996ae0b0 2459
45fc7ddb
HK
2460 elsif Nkind_In (N, N_Explicit_Dereference,
2461 N_Attribute_Reference,
2462 N_And_Then,
2463 N_Indexed_Component,
2464 N_Or_Else,
2465 N_Range,
2466 N_Selected_Component,
2467 N_Slice)
996ae0b0
RK
2468 or else Nkind (Name (N)) = N_Explicit_Dereference
2469 then
2470 null;
2471
0669bebe 2472 -- For procedure or function calls, set the type of the name,
4519314c 2473 -- and also the entity pointer for the prefix.
996ae0b0 2474
d3b00ce3 2475 elsif Nkind (N) in N_Subprogram_Call
a3f2babd 2476 and then Is_Entity_Name (Name (N))
996ae0b0
RK
2477 then
2478 Set_Etype (Name (N), Expr_Type);
2479 Set_Entity (Name (N), Seen);
2480 Generate_Reference (Seen, Name (N));
2481
2482 elsif Nkind (N) = N_Function_Call
2483 and then Nkind (Name (N)) = N_Selected_Component
2484 then
2485 Set_Etype (Name (N), Expr_Type);
2486 Set_Entity (Selector_Name (Name (N)), Seen);
2487 Generate_Reference (Seen, Selector_Name (Name (N)));
2488
2489 -- For all other cases, just set the type of the Name
2490
2491 else
2492 Set_Etype (Name (N), Expr_Type);
2493 end if;
2494
996ae0b0
RK
2495 end if;
2496
aa5147f0
ES
2497 <<Continue>>
2498
996ae0b0
RK
2499 -- Move to next interpretation
2500
c8ef728f 2501 exit Interp_Loop when No (It.Typ);
996ae0b0
RK
2502
2503 Get_Next_Interp (I, It);
2504 end loop Interp_Loop;
2505 end if;
2506
2507 -- At this stage Found indicates whether or not an acceptable
4519314c
AC
2508 -- interpretation exists. If not, then we have an error, except that if
2509 -- the context is Any_Type as a result of some other error, then we
2510 -- suppress the error report.
996ae0b0
RK
2511
2512 if not Found then
2513 if Typ /= Any_Type then
2514
0669bebe
GB
2515 -- If type we are looking for is Void, then this is the procedure
2516 -- call case, and the error is simply that what we gave is not a
2517 -- procedure name (we think of procedure calls as expressions with
159a5104 2518 -- types internally, but the user doesn't think of them this way).
996ae0b0
RK
2519
2520 if Typ = Standard_Void_Type then
91b1417d
AC
2521
2522 -- Special case message if function used as a procedure
2523
2524 if Nkind (N) = N_Procedure_Call_Statement
2525 and then Is_Entity_Name (Name (N))
2526 and then Ekind (Entity (Name (N))) = E_Function
2527 then
2528 Error_Msg_NE
2529 ("cannot use function & in a procedure call",
2530 Name (N), Entity (Name (N)));
2531
0669bebe 2532 -- Otherwise give general message (not clear what cases this
a90bd866 2533 -- covers, but no harm in providing for them).
91b1417d
AC
2534
2535 else
2536 Error_Msg_N ("expect procedure name in procedure call", N);
2537 end if;
2538
996ae0b0
RK
2539 Found := True;
2540
2541 -- Otherwise we do have a subexpression with the wrong type
2542
0669bebe
GB
2543 -- Check for the case of an allocator which uses an access type
2544 -- instead of the designated type. This is a common error and we
2545 -- specialize the message, posting an error on the operand of the
2546 -- allocator, complaining that we expected the designated type of
2547 -- the allocator.
996ae0b0
RK
2548
2549 elsif Nkind (N) = N_Allocator
3f1bc2cf
AC
2550 and then Is_Access_Type (Typ)
2551 and then Is_Access_Type (Etype (N))
996ae0b0
RK
2552 and then Designated_Type (Etype (N)) = Typ
2553 then
2554 Wrong_Type (Expression (N), Designated_Type (Typ));
2555 Found := True;
2556
0669bebe
GB
2557 -- Check for view mismatch on Null in instances, for which the
2558 -- view-swapping mechanism has no identifier.
17be0cdf
ES
2559
2560 elsif (In_Instance or else In_Inlined_Body)
2561 and then (Nkind (N) = N_Null)
2562 and then Is_Private_Type (Typ)
2563 and then Is_Access_Type (Full_View (Typ))
2564 then
2565 Resolve (N, Full_View (Typ));
2566 Set_Etype (N, Typ);
1af4455a 2567 Ghost_Mode := Save_Ghost_Mode;
17be0cdf
ES
2568 return;
2569
aa180613
RD
2570 -- Check for an aggregate. Sometimes we can get bogus aggregates
2571 -- from misuse of parentheses, and we are about to complain about
2572 -- the aggregate without even looking inside it.
996ae0b0 2573
aa180613
RD
2574 -- Instead, if we have an aggregate of type Any_Composite, then
2575 -- analyze and resolve the component fields, and then only issue
2576 -- another message if we get no errors doing this (otherwise
2577 -- assume that the errors in the aggregate caused the problem).
996ae0b0
RK
2578
2579 elsif Nkind (N) = N_Aggregate
2580 and then Etype (N) = Any_Composite
2581 then
996ae0b0
RK
2582 -- Disable expansion in any case. If there is a type mismatch
2583 -- it may be fatal to try to expand the aggregate. The flag
2584 -- would otherwise be set to false when the error is posted.
2585
2586 Expander_Active := False;
2587
2588 declare
2589 procedure Check_Aggr (Aggr : Node_Id);
aa180613
RD
2590 -- Check one aggregate, and set Found to True if we have a
2591 -- definite error in any of its elements
996ae0b0
RK
2592
2593 procedure Check_Elmt (Aelmt : Node_Id);
aa180613
RD
2594 -- Check one element of aggregate and set Found to True if
2595 -- we definitely have an error in the element.
2596
2597 ----------------
2598 -- Check_Aggr --
2599 ----------------
996ae0b0
RK
2600
2601 procedure Check_Aggr (Aggr : Node_Id) is
2602 Elmt : Node_Id;
2603
2604 begin
2605 if Present (Expressions (Aggr)) then
2606 Elmt := First (Expressions (Aggr));
2607 while Present (Elmt) loop
2608 Check_Elmt (Elmt);
2609 Next (Elmt);
2610 end loop;
2611 end if;
2612
2613 if Present (Component_Associations (Aggr)) then
2614 Elmt := First (Component_Associations (Aggr));
2615 while Present (Elmt) loop
aa180613 2616
0669bebe
GB
2617 -- If this is a default-initialized component, then
2618 -- there is nothing to check. The box will be
2619 -- replaced by the appropriate call during late
2620 -- expansion.
aa180613
RD
2621
2622 if not Box_Present (Elmt) then
2623 Check_Elmt (Expression (Elmt));
2624 end if;
2625
996ae0b0
RK
2626 Next (Elmt);
2627 end loop;
2628 end if;
2629 end Check_Aggr;
2630
fbf5a39b
AC
2631 ----------------
2632 -- Check_Elmt --
2633 ----------------
2634
996ae0b0
RK
2635 procedure Check_Elmt (Aelmt : Node_Id) is
2636 begin
2637 -- If we have a nested aggregate, go inside it (to
5cc9353d
RD
2638 -- attempt a naked analyze-resolve of the aggregate can
2639 -- cause undesirable cascaded errors). Do not resolve
2640 -- expression if it needs a type from context, as for
2641 -- integer * fixed expression.
996ae0b0
RK
2642
2643 if Nkind (Aelmt) = N_Aggregate then
2644 Check_Aggr (Aelmt);
2645
2646 else
2647 Analyze (Aelmt);
2648
2649 if not Is_Overloaded (Aelmt)
2650 and then Etype (Aelmt) /= Any_Fixed
2651 then
fbf5a39b 2652 Resolve (Aelmt);
996ae0b0
RK
2653 end if;
2654
2655 if Etype (Aelmt) = Any_Type then
2656 Found := True;
2657 end if;
2658 end if;
2659 end Check_Elmt;
2660
2661 begin
2662 Check_Aggr (N);
2663 end;
2664 end if;
2665
6fd0a72a
AC
2666 -- Looks like we have a type error, but check for special case
2667 -- of Address wanted, integer found, with the configuration pragma
2668 -- Allow_Integer_Address active. If we have this case, introduce
2669 -- an unchecked conversion to allow the integer expression to be
2670 -- treated as an Address. The reverse case of integer wanted,
2671 -- Address found, is treated in an analogous manner.
2672
061828e3
AC
2673 if Address_Integer_Convert_OK (Typ, Etype (N)) then
2674 Rewrite (N, Unchecked_Convert_To (Typ, Relocate_Node (N)));
2675 Analyze_And_Resolve (N, Typ);
1af4455a 2676 Ghost_Mode := Save_Ghost_Mode;
061828e3 2677 return;
6fd0a72a 2678 end if;
818b578d 2679
6fd0a72a
AC
2680 -- That special Allow_Integer_Address check did not appply, so we
2681 -- have a real type error. If an error message was issued already,
2682 -- Found got reset to True, so if it's still False, issue standard
2683 -- Wrong_Type message.
818b578d 2684
6fd0a72a
AC
2685 if not Found then
2686 if Is_Overloaded (N) and then Nkind (N) = N_Function_Call then
65356e64
AC
2687 declare
2688 Subp_Name : Node_Id;
6fd0a72a 2689
65356e64
AC
2690 begin
2691 if Is_Entity_Name (Name (N)) then
2692 Subp_Name := Name (N);
2693
2694 elsif Nkind (Name (N)) = N_Selected_Component then
2695
a77842bd 2696 -- Protected operation: retrieve operation name
65356e64
AC
2697
2698 Subp_Name := Selector_Name (Name (N));
19fb051c 2699
65356e64
AC
2700 else
2701 raise Program_Error;
2702 end if;
2703
2704 Error_Msg_Node_2 := Typ;
1486a00e
AC
2705 Error_Msg_NE
2706 ("no visible interpretation of& "
2707 & "matches expected type&", N, Subp_Name);
65356e64 2708 end;
996ae0b0
RK
2709
2710 if All_Errors_Mode then
2711 declare
2712 Index : Interp_Index;
2713 It : Interp;
2714
2715 begin
aa180613 2716 Error_Msg_N ("\\possible interpretations:", N);
996ae0b0 2717
1420b484 2718 Get_First_Interp (Name (N), Index, It);
996ae0b0 2719 while Present (It.Nam) loop
ea985d95 2720 Error_Msg_Sloc := Sloc (It.Nam);
aa5147f0
ES
2721 Error_Msg_Node_2 := It.Nam;
2722 Error_Msg_NE
2723 ("\\ type& for & declared#", N, It.Typ);
996ae0b0
RK
2724 Get_Next_Interp (Index, It);
2725 end loop;
2726 end;
aa5147f0 2727
996ae0b0
RK
2728 else
2729 Error_Msg_N ("\use -gnatf for details", N);
2730 end if;
19fb051c 2731
996ae0b0
RK
2732 else
2733 Wrong_Type (N, Typ);
2734 end if;
2735 end if;
2736 end if;
2737
2738 Resolution_Failed;
1af4455a 2739 Ghost_Mode := Save_Ghost_Mode;
996ae0b0
RK
2740 return;
2741
2742 -- Test if we have more than one interpretation for the context
2743
2744 elsif Ambiguous then
2745 Resolution_Failed;
1af4455a 2746 Ghost_Mode := Save_Ghost_Mode;
996ae0b0
RK
2747 return;
2748
fe58fea7
AC
2749 -- Only one intepretation
2750
996ae0b0 2751 else
ee1a7572
AC
2752 -- In Ada 2005, if we have something like "X : T := 2 + 2;", where
2753 -- the "+" on T is abstract, and the operands are of universal type,
2754 -- the above code will have (incorrectly) resolved the "+" to the
fe58fea7
AC
2755 -- universal one in Standard. Therefore check for this case and give
2756 -- an error. We can't do this earlier, because it would cause legal
2757 -- cases to get errors (when some other type has an abstract "+").
ee1a7572 2758
36504e5f
AC
2759 if Ada_Version >= Ada_2005
2760 and then Nkind (N) in N_Op
2761 and then Is_Overloaded (N)
2762 and then Is_Universal_Numeric_Type (Etype (Entity (N)))
ee1a7572
AC
2763 then
2764 Get_First_Interp (N, I, It);
2765 while Present (It.Typ) loop
2766 if Present (It.Abstract_Op) and then
2767 Etype (It.Abstract_Op) = Typ
2768 then
2769 Error_Msg_NE
2770 ("cannot call abstract subprogram &!", N, It.Abstract_Op);
2771 return;
2772 end if;
2773
2774 Get_Next_Interp (I, It);
2775 end loop;
2776 end if;
2777
2778 -- Here we have an acceptable interpretation for the context
2779
996ae0b0
RK
2780 -- Propagate type information and normalize tree for various
2781 -- predefined operations. If the context only imposes a class of
2782 -- types, rather than a specific type, propagate the actual type
2783 -- downward.
2784
19fb051c
AC
2785 if Typ = Any_Integer or else
2786 Typ = Any_Boolean or else
2787 Typ = Any_Modular or else
2788 Typ = Any_Real or else
2789 Typ = Any_Discrete
996ae0b0
RK
2790 then
2791 Ctx_Type := Expr_Type;
2792
5cc9353d
RD
2793 -- Any_Fixed is legal in a real context only if a specific fixed-
2794 -- point type is imposed. If Norman Cohen can be confused by this,
2795 -- it deserves a separate message.
996ae0b0
RK
2796
2797 if Typ = Any_Real
2798 and then Expr_Type = Any_Fixed
2799 then
758c442c 2800 Error_Msg_N ("illegal context for mixed mode operation", N);
996ae0b0
RK
2801 Set_Etype (N, Universal_Real);
2802 Ctx_Type := Universal_Real;
2803 end if;
2804 end if;
2805
f3d57416 2806 -- A user-defined operator is transformed into a function call at
0ab80019
AC
2807 -- this point, so that further processing knows that operators are
2808 -- really operators (i.e. are predefined operators). User-defined
2809 -- operators that are intrinsic are just renamings of the predefined
2810 -- ones, and need not be turned into calls either, but if they rename
2811 -- a different operator, we must transform the node accordingly.
2812 -- Instantiations of Unchecked_Conversion are intrinsic but are
2813 -- treated as functions, even if given an operator designator.
2814
2815 if Nkind (N) in N_Op
2816 and then Present (Entity (N))
2817 and then Ekind (Entity (N)) /= E_Operator
2818 then
2819
2820 if not Is_Predefined_Op (Entity (N)) then
2821 Rewrite_Operator_As_Call (N, Entity (N));
2822
615cbd95
AC
2823 elsif Present (Alias (Entity (N)))
2824 and then
45fc7ddb
HK
2825 Nkind (Parent (Parent (Entity (N)))) =
2826 N_Subprogram_Renaming_Declaration
615cbd95 2827 then
0ab80019
AC
2828 Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2829
2830 -- If the node is rewritten, it will be fully resolved in
2831 -- Rewrite_Renamed_Operator.
2832
2833 if Analyzed (N) then
1af4455a 2834 Ghost_Mode := Save_Ghost_Mode;
0ab80019
AC
2835 return;
2836 end if;
2837 end if;
2838 end if;
2839
996ae0b0
RK
2840 case N_Subexpr'(Nkind (N)) is
2841
2842 when N_Aggregate => Resolve_Aggregate (N, Ctx_Type);
2843
2844 when N_Allocator => Resolve_Allocator (N, Ctx_Type);
2845
514d0fc5 2846 when N_Short_Circuit
996ae0b0
RK
2847 => Resolve_Short_Circuit (N, Ctx_Type);
2848
2849 when N_Attribute_Reference
2850 => Resolve_Attribute (N, Ctx_Type);
2851
19d846a0
RD
2852 when N_Case_Expression
2853 => Resolve_Case_Expression (N, Ctx_Type);
2854
996ae0b0
RK
2855 when N_Character_Literal
2856 => Resolve_Character_Literal (N, Ctx_Type);
2857
996ae0b0
RK
2858 when N_Expanded_Name
2859 => Resolve_Entity_Name (N, Ctx_Type);
2860
996ae0b0
RK
2861 when N_Explicit_Dereference
2862 => Resolve_Explicit_Dereference (N, Ctx_Type);
2863
955871d3
AC
2864 when N_Expression_With_Actions
2865 => Resolve_Expression_With_Actions (N, Ctx_Type);
2866
2867 when N_Extension_Aggregate
2868 => Resolve_Extension_Aggregate (N, Ctx_Type);
2869
996ae0b0
RK
2870 when N_Function_Call
2871 => Resolve_Call (N, Ctx_Type);
2872
2873 when N_Identifier
2874 => Resolve_Entity_Name (N, Ctx_Type);
2875
9b16cb57
RD
2876 when N_If_Expression
2877 => Resolve_If_Expression (N, Ctx_Type);
2878
996ae0b0
RK
2879 when N_Indexed_Component
2880 => Resolve_Indexed_Component (N, Ctx_Type);
2881
2882 when N_Integer_Literal
2883 => Resolve_Integer_Literal (N, Ctx_Type);
2884
0669bebe
GB
2885 when N_Membership_Test
2886 => Resolve_Membership_Op (N, Ctx_Type);
2887
996ae0b0
RK
2888 when N_Null => Resolve_Null (N, Ctx_Type);
2889
2890 when N_Op_And | N_Op_Or | N_Op_Xor
2891 => Resolve_Logical_Op (N, Ctx_Type);
2892
2893 when N_Op_Eq | N_Op_Ne
2894 => Resolve_Equality_Op (N, Ctx_Type);
2895
2896 when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2897 => Resolve_Comparison_Op (N, Ctx_Type);
2898
2899 when N_Op_Not => Resolve_Op_Not (N, Ctx_Type);
2900
2901 when N_Op_Add | N_Op_Subtract | N_Op_Multiply |
2902 N_Op_Divide | N_Op_Mod | N_Op_Rem
2903
2904 => Resolve_Arithmetic_Op (N, Ctx_Type);
2905
2906 when N_Op_Concat => Resolve_Op_Concat (N, Ctx_Type);
2907
2908 when N_Op_Expon => Resolve_Op_Expon (N, Ctx_Type);
2909
2910 when N_Op_Plus | N_Op_Minus | N_Op_Abs
2911 => Resolve_Unary_Op (N, Ctx_Type);
2912
2913 when N_Op_Shift => Resolve_Shift (N, Ctx_Type);
2914
2915 when N_Procedure_Call_Statement
2916 => Resolve_Call (N, Ctx_Type);
2917
2918 when N_Operator_Symbol
2919 => Resolve_Operator_Symbol (N, Ctx_Type);
2920
2921 when N_Qualified_Expression
2922 => Resolve_Qualified_Expression (N, Ctx_Type);
2923
c8d63650
RD
2924 -- Why is the following null, needs a comment ???
2925
983a3d80
RD
2926 when N_Quantified_Expression
2927 => null;
2928
c8d63650 2929 when N_Raise_Expression
7610fee8 2930 => Resolve_Raise_Expression (N, Ctx_Type);
c8d63650 2931
996ae0b0
RK
2932 when N_Raise_xxx_Error
2933 => Set_Etype (N, Ctx_Type);
2934
2935 when N_Range => Resolve_Range (N, Ctx_Type);
2936
2937 when N_Real_Literal
2938 => Resolve_Real_Literal (N, Ctx_Type);
2939
2940 when N_Reference => Resolve_Reference (N, Ctx_Type);
2941
2942 when N_Selected_Component
2943 => Resolve_Selected_Component (N, Ctx_Type);
2944
2945 when N_Slice => Resolve_Slice (N, Ctx_Type);
2946
2947 when N_String_Literal
2948 => Resolve_String_Literal (N, Ctx_Type);
2949
996ae0b0
RK
2950 when N_Type_Conversion
2951 => Resolve_Type_Conversion (N, Ctx_Type);
2952
2953 when N_Unchecked_Expression =>
2954 Resolve_Unchecked_Expression (N, Ctx_Type);
2955
2956 when N_Unchecked_Type_Conversion =>
2957 Resolve_Unchecked_Type_Conversion (N, Ctx_Type);
996ae0b0
RK
2958 end case;
2959
6cce2156
GD
2960 -- Ada 2012 (AI05-0149): Apply an (implicit) conversion to an
2961 -- expression of an anonymous access type that occurs in the context
2962 -- of a named general access type, except when the expression is that
2963 -- of a membership test. This ensures proper legality checking in
2964 -- terms of allowed conversions (expressions that would be illegal to
2965 -- convert implicitly are allowed in membership tests).
2966
2967 if Ada_Version >= Ada_2012
2968 and then Ekind (Ctx_Type) = E_General_Access_Type
2969 and then Ekind (Etype (N)) = E_Anonymous_Access_Type
2970 and then Nkind (Parent (N)) not in N_Membership_Test
2971 then
2972 Rewrite (N, Convert_To (Ctx_Type, Relocate_Node (N)));
2973 Analyze_And_Resolve (N, Ctx_Type);
2974 end if;
2975
996ae0b0
RK
2976 -- If the subexpression was replaced by a non-subexpression, then
2977 -- all we do is to expand it. The only legitimate case we know of
2978 -- is converting procedure call statement to entry call statements,
2979 -- but there may be others, so we are making this test general.
2980
2981 if Nkind (N) not in N_Subexpr then
2982 Debug_A_Exit ("resolving ", N, " (done)");
2983 Expand (N);
1af4455a 2984 Ghost_Mode := Save_Ghost_Mode;
996ae0b0
RK
2985 return;
2986 end if;
2987
2988 -- The expression is definitely NOT overloaded at this point, so
2989 -- we reset the Is_Overloaded flag to avoid any confusion when
2990 -- reanalyzing the node.
2991
2992 Set_Is_Overloaded (N, False);
2993
2994 -- Freeze expression type, entity if it is a name, and designated
fbf5a39b 2995 -- type if it is an allocator (RM 13.14(10,11,13)).
996ae0b0 2996
5cc9353d
RD
2997 -- Now that the resolution of the type of the node is complete, and
2998 -- we did not detect an error, we can expand this node. We skip the
2999 -- expand call if we are in a default expression, see section
3000 -- "Handling of Default Expressions" in Sem spec.
996ae0b0
RK
3001
3002 Debug_A_Exit ("resolving ", N, " (done)");
3003
3004 -- We unconditionally freeze the expression, even if we are in
5cc9353d
RD
3005 -- default expression mode (the Freeze_Expression routine tests this
3006 -- flag and only freezes static types if it is set).
996ae0b0 3007
3e65bfab
AC
3008 -- Ada 2012 (AI05-177): The declaration of an expression function
3009 -- does not cause freezing, but we never reach here in that case.
3010 -- Here we are resolving the corresponding expanded body, so we do
3011 -- need to perform normal freezing.
08f8a983 3012
3e65bfab 3013 Freeze_Expression (N);
996ae0b0
RK
3014
3015 -- Now we can do the expansion
3016
3017 Expand (N);
3018 end if;
1af4455a
HK
3019
3020 Ghost_Mode := Save_Ghost_Mode;
996ae0b0
RK
3021 end Resolve;
3022
fbf5a39b
AC
3023 -------------
3024 -- Resolve --
3025 -------------
3026
996ae0b0
RK
3027 -- Version with check(s) suppressed
3028
3029 procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
3030 begin
3031 if Suppress = All_Checks then
3032 declare
a7f1b24f 3033 Sva : constant Suppress_Array := Scope_Suppress.Suppress;
996ae0b0 3034 begin
a7f1b24f 3035 Scope_Suppress.Suppress := (others => True);
996ae0b0 3036 Resolve (N, Typ);
a7f1b24f 3037 Scope_Suppress.Suppress := Sva;
996ae0b0
RK
3038 end;
3039
3040 else
3041 declare
3217f71e 3042 Svg : constant Boolean := Scope_Suppress.Suppress (Suppress);
996ae0b0 3043 begin
3217f71e 3044 Scope_Suppress.Suppress (Suppress) := True;
996ae0b0 3045 Resolve (N, Typ);
3217f71e 3046 Scope_Suppress.Suppress (Suppress) := Svg;
996ae0b0
RK
3047 end;
3048 end if;
3049 end Resolve;
3050
fbf5a39b
AC
3051 -------------
3052 -- Resolve --
3053 -------------
3054
3055 -- Version with implicit type
3056
3057 procedure Resolve (N : Node_Id) is
3058 begin
3059 Resolve (N, Etype (N));
3060 end Resolve;
3061
996ae0b0
RK
3062 ---------------------
3063 -- Resolve_Actuals --
3064 ---------------------
3065
3066 procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
3067 Loc : constant Source_Ptr := Sloc (N);
3068 A : Node_Id;
97779c34 3069 A_Id : Entity_Id;
996ae0b0 3070 A_Typ : Entity_Id;
97779c34 3071 F : Entity_Id;
996ae0b0
RK
3072 F_Typ : Entity_Id;
3073 Prev : Node_Id := Empty;
67ce0d7e 3074 Orig_A : Node_Id;
e6b3f5ba
ES
3075 Real_F : Entity_Id;
3076
3077 Real_Subp : Entity_Id;
4d6a38a5
ES
3078 -- If the subprogram being called is an inherited operation for
3079 -- a formal derived type in an instance, Real_Subp is the subprogram
3080 -- that will be called. It may have different formal names than the
3081 -- operation of the formal in the generic, so after actual is resolved
3082 -- the name of the actual in a named association must carry the name
3083 -- of the actual of the subprogram being called.
996ae0b0 3084
f3691f46
ES
3085 procedure Check_Aliased_Parameter;
3086 -- Check rules on aliased parameters and related accessibility rules
fc27e20e 3087 -- in (RM 3.10.2 (10.2-10.4)).
f3691f46 3088
45fc7ddb
HK
3089 procedure Check_Argument_Order;
3090 -- Performs a check for the case where the actuals are all simple
3091 -- identifiers that correspond to the formal names, but in the wrong
3092 -- order, which is considered suspicious and cause for a warning.
3093
b7d1f17f
HK
3094 procedure Check_Prefixed_Call;
3095 -- If the original node is an overloaded call in prefix notation,
3096 -- insert an 'Access or a dereference as needed over the first actual.
3097 -- Try_Object_Operation has already verified that there is a valid
3098 -- interpretation, but the form of the actual can only be determined
3099 -- once the primitive operation is identified.
3100
996ae0b0
RK
3101 procedure Insert_Default;
3102 -- If the actual is missing in a call, insert in the actuals list
3103 -- an instance of the default expression. The insertion is always
3104 -- a named association.
3105
97779c34
AC
3106 procedure Property_Error
3107 (Var : Node_Id;
3108 Var_Id : Entity_Id;
3109 Prop_Nam : Name_Id);
3110 -- Emit an error concerning variable Var with entity Var_Id that has
3111 -- enabled property Prop_Nam when it acts as an actual parameter in a
3112 -- call and the corresponding formal parameter is of mode IN.
3113
fbf5a39b
AC
3114 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
3115 -- Check whether T1 and T2, or their full views, are derived from a
3116 -- common type. Used to enforce the restrictions on array conversions
3117 -- of AI95-00246.
3118
a7a3cf5c
AC
3119 function Static_Concatenation (N : Node_Id) return Boolean;
3120 -- Predicate to determine whether an actual that is a concatenation
3121 -- will be evaluated statically and does not need a transient scope.
3122 -- This must be determined before the actual is resolved and expanded
3123 -- because if needed the transient scope must be introduced earlier.
3124
07a64c02
AC
3125 -----------------------------
3126 -- Check_Aliased_Parameter --
3127 -----------------------------
f3691f46
ES
3128
3129 procedure Check_Aliased_Parameter is
3130 Nominal_Subt : Entity_Id;
3131
3132 begin
3133 if Is_Aliased (F) then
3134 if Is_Tagged_Type (A_Typ) then
3135 null;
3136
3137 elsif Is_Aliased_View (A) then
3138 if Is_Constr_Subt_For_U_Nominal (A_Typ) then
3139 Nominal_Subt := Base_Type (A_Typ);
3140 else
3141 Nominal_Subt := A_Typ;
3142 end if;
3143
3144 if Subtypes_Statically_Match (F_Typ, Nominal_Subt) then
3145 null;
3146
3147 -- In a generic body assume the worst for generic formals:
3148 -- they can have a constrained partial view (AI05-041).
3149
3150 elsif Has_Discriminants (F_Typ)
3151 and then not Is_Constrained (F_Typ)
3152 and then not Has_Constrained_Partial_View (F_Typ)
3153 and then not Is_Generic_Type (F_Typ)
3154 then
3155 null;
3156
3157 else
3158 Error_Msg_NE ("untagged actual does not match "
fc27e20e 3159 & "aliased formal&", A, F);
f3691f46
ES
3160 end if;
3161
3162 else
3163 Error_Msg_NE ("actual for aliased formal& must be "
fc27e20e 3164 & "aliased object", A, F);
f3691f46
ES
3165 end if;
3166
3167 if Ekind (Nam) = E_Procedure then
3168 null;
3169
3170 elsif Ekind (Etype (Nam)) = E_Anonymous_Access_Type then
3171 if Nkind (Parent (N)) = N_Type_Conversion
fc27e20e
RD
3172 and then Type_Access_Level (Etype (Parent (N))) <
3173 Object_Access_Level (A)
f3691f46
ES
3174 then
3175 Error_Msg_N ("aliased actual has wrong accessibility", A);
3176 end if;
3177
3178 elsif Nkind (Parent (N)) = N_Qualified_Expression
3179 and then Nkind (Parent (Parent (N))) = N_Allocator
fc27e20e
RD
3180 and then Type_Access_Level (Etype (Parent (Parent (N)))) <
3181 Object_Access_Level (A)
f3691f46
ES
3182 then
3183 Error_Msg_N
fc27e20e 3184 ("aliased actual in allocator has wrong accessibility", A);
f3691f46
ES
3185 end if;
3186 end if;
3187 end Check_Aliased_Parameter;
3188
45fc7ddb
HK
3189 --------------------------
3190 -- Check_Argument_Order --
3191 --------------------------
3192
3193 procedure Check_Argument_Order is
3194 begin
3195 -- Nothing to do if no parameters, or original node is neither a
3196 -- function call nor a procedure call statement (happens in the
3197 -- operator-transformed-to-function call case), or the call does
3198 -- not come from source, or this warning is off.
3199
3200 if not Warn_On_Parameter_Order
19fb051c 3201 or else No (Parameter_Associations (N))
d3b00ce3 3202 or else Nkind (Original_Node (N)) not in N_Subprogram_Call
19fb051c 3203 or else not Comes_From_Source (N)
45fc7ddb
HK
3204 then
3205 return;
3206 end if;
3207
3208 declare
3209 Nargs : constant Nat := List_Length (Parameter_Associations (N));
3210
3211 begin
3212 -- Nothing to do if only one parameter
3213
3214 if Nargs < 2 then
3215 return;
3216 end if;
3217
3218 -- Here if at least two arguments
3219
3220 declare
3221 Actuals : array (1 .. Nargs) of Node_Id;
3222 Actual : Node_Id;
3223 Formal : Node_Id;
3224
3225 Wrong_Order : Boolean := False;
3226 -- Set True if an out of order case is found
3227
3228 begin
3229 -- Collect identifier names of actuals, fail if any actual is
3230 -- not a simple identifier, and record max length of name.
3231
3232 Actual := First (Parameter_Associations (N));
3233 for J in Actuals'Range loop
3234 if Nkind (Actual) /= N_Identifier then
3235 return;
3236 else
3237 Actuals (J) := Actual;
3238 Next (Actual);
3239 end if;
3240 end loop;
3241
3242 -- If we got this far, all actuals are identifiers and the list
3243 -- of their names is stored in the Actuals array.
3244
3245 Formal := First_Formal (Nam);
3246 for J in Actuals'Range loop
3247
3248 -- If we ran out of formals, that's odd, probably an error
3249 -- which will be detected elsewhere, but abandon the search.
3250
3251 if No (Formal) then
3252 return;
3253 end if;
3254
3255 -- If name matches and is in order OK
3256
3257 if Chars (Formal) = Chars (Actuals (J)) then
3258 null;
3259
3260 else
3261 -- If no match, see if it is elsewhere in list and if so
3262 -- flag potential wrong order if type is compatible.
3263
3264 for K in Actuals'Range loop
3265 if Chars (Formal) = Chars (Actuals (K))
3266 and then
3267 Has_Compatible_Type (Actuals (K), Etype (Formal))
3268 then
3269 Wrong_Order := True;
3270 goto Continue;
3271 end if;
3272 end loop;
3273
3274 -- No match
3275
3276 return;
3277 end if;
3278
3279 <<Continue>> Next_Formal (Formal);
3280 end loop;
3281
3282 -- If Formals left over, also probably an error, skip warning
3283
3284 if Present (Formal) then
3285 return;
3286 end if;
3287
3288 -- Here we give the warning if something was out of order
3289
3290 if Wrong_Order then
3291 Error_Msg_N
a3633438 3292 ("?P?actuals for this call may be in wrong order", N);
45fc7ddb
HK
3293 end if;
3294 end;
3295 end;
3296 end Check_Argument_Order;
3297
b7d1f17f
HK
3298 -------------------------
3299 -- Check_Prefixed_Call --
3300 -------------------------
3301
3302 procedure Check_Prefixed_Call is
3303 Act : constant Node_Id := First_Actual (N);
3304 A_Type : constant Entity_Id := Etype (Act);
3305 F_Type : constant Entity_Id := Etype (First_Formal (Nam));
3306 Orig : constant Node_Id := Original_Node (N);
3307 New_A : Node_Id;
3308
3309 begin
3310 -- Check whether the call is a prefixed call, with or without
3311 -- additional actuals.
3312
3313 if Nkind (Orig) = N_Selected_Component
3314 or else
3315 (Nkind (Orig) = N_Indexed_Component
3316 and then Nkind (Prefix (Orig)) = N_Selected_Component
3317 and then Is_Entity_Name (Prefix (Prefix (Orig)))
3318 and then Is_Entity_Name (Act)
3319 and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
3320 then
3321 if Is_Access_Type (A_Type)
3322 and then not Is_Access_Type (F_Type)
3323 then
3324 -- Introduce dereference on object in prefix
3325
3326 New_A :=
3327 Make_Explicit_Dereference (Sloc (Act),
3328 Prefix => Relocate_Node (Act));
3329 Rewrite (Act, New_A);
3330 Analyze (Act);
3331
3332 elsif Is_Access_Type (F_Type)
3333 and then not Is_Access_Type (A_Type)
3334 then
3335 -- Introduce an implicit 'Access in prefix
3336
3337 if not Is_Aliased_View (Act) then
ed2233dc 3338 Error_Msg_NE
039538bc 3339 ("object in prefixed call to& must be aliased "
715e529d 3340 & "(RM 4.1.3 (13 1/2))",
b7d1f17f
HK
3341 Prefix (Act), Nam);
3342 end if;
3343
3344 Rewrite (Act,
3345 Make_Attribute_Reference (Loc,
3346 Attribute_Name => Name_Access,
3347 Prefix => Relocate_Node (Act)));
3348 end if;
3349
3350 Analyze (Act);
3351 end if;
3352 end Check_Prefixed_Call;
3353
996ae0b0
RK
3354 --------------------
3355 -- Insert_Default --
3356 --------------------
3357
3358 procedure Insert_Default is
3359 Actval : Node_Id;
3360 Assoc : Node_Id;
3361
3362 begin
fbf5a39b 3363 -- Missing argument in call, nothing to insert
996ae0b0 3364
fbf5a39b
AC
3365 if No (Default_Value (F)) then
3366 return;
3367
3368 else
3369 -- Note that we do a full New_Copy_Tree, so that any associated
3370 -- Itypes are properly copied. This may not be needed any more,
a90bd866 3371 -- but it does no harm as a safety measure. Defaults of a generic
fbf5a39b
AC
3372 -- formal may be out of bounds of the corresponding actual (see
3373 -- cc1311b) and an additional check may be required.
996ae0b0 3374
b7d1f17f
HK
3375 Actval :=
3376 New_Copy_Tree
3377 (Default_Value (F),
3378 New_Scope => Current_Scope,
3379 New_Sloc => Loc);
996ae0b0
RK
3380
3381 if Is_Concurrent_Type (Scope (Nam))
3382 and then Has_Discriminants (Scope (Nam))
3383 then
3384 Replace_Actual_Discriminants (N, Actval);
3385 end if;
3386
3387 if Is_Overloadable (Nam)
3388 and then Present (Alias (Nam))
3389 then
3390 if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
3391 and then not Is_Tagged_Type (Etype (F))
3392 then
3393 -- If default is a real literal, do not introduce a
3394 -- conversion whose effect may depend on the run-time
3395 -- size of universal real.
3396
3397 if Nkind (Actval) = N_Real_Literal then
3398 Set_Etype (Actval, Base_Type (Etype (F)));
3399 else
3400 Actval := Unchecked_Convert_To (Etype (F), Actval);
3401 end if;
3402 end if;
3403
3404 if Is_Scalar_Type (Etype (F)) then
3405 Enable_Range_Check (Actval);
3406 end if;
3407
996ae0b0
RK
3408 Set_Parent (Actval, N);
3409
3410 -- Resolve aggregates with their base type, to avoid scope
f3d57416 3411 -- anomalies: the subtype was first built in the subprogram
996ae0b0
RK
3412 -- declaration, and the current call may be nested.
3413
76b84bf0
AC
3414 if Nkind (Actval) = N_Aggregate then
3415 Analyze_And_Resolve (Actval, Etype (F));
996ae0b0
RK
3416 else
3417 Analyze_And_Resolve (Actval, Etype (Actval));
3418 end if;
fbf5a39b
AC
3419
3420 else
3421 Set_Parent (Actval, N);
3422
a77842bd 3423 -- See note above concerning aggregates
fbf5a39b
AC
3424
3425 if Nkind (Actval) = N_Aggregate
3426 and then Has_Discriminants (Etype (Actval))
3427 then
3428 Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
3429
5cc9353d
RD
3430 -- Resolve entities with their own type, which may differ from
3431 -- the type of a reference in a generic context (the view
3432 -- swapping mechanism did not anticipate the re-analysis of
3433 -- default values in calls).
fbf5a39b
AC
3434
3435 elsif Is_Entity_Name (Actval) then
3436 Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
3437
3438 else
3439 Analyze_And_Resolve (Actval, Etype (Actval));
3440 end if;
996ae0b0
RK
3441 end if;
3442
5cc9353d
RD
3443 -- If default is a tag indeterminate function call, propagate tag
3444 -- to obtain proper dispatching.
996ae0b0
RK
3445
3446 if Is_Controlling_Formal (F)
3447 and then Nkind (Default_Value (F)) = N_Function_Call
3448 then
3449 Set_Is_Controlling_Actual (Actval);
3450 end if;
3451
996ae0b0
RK
3452 end if;
3453
3454 -- If the default expression raises constraint error, then just
5cc9353d
RD
3455 -- silently replace it with an N_Raise_Constraint_Error node, since
3456 -- we already gave the warning on the subprogram spec. If node is
3457 -- already a Raise_Constraint_Error leave as is, to prevent loops in
3458 -- the warnings removal machinery.
996ae0b0 3459
2604ec03
AC
3460 if Raises_Constraint_Error (Actval)
3461 and then Nkind (Actval) /= N_Raise_Constraint_Error
3462 then
996ae0b0 3463 Rewrite (Actval,
07fc65c4
GB
3464 Make_Raise_Constraint_Error (Loc,
3465 Reason => CE_Range_Check_Failed));
996ae0b0
RK
3466 Set_Raises_Constraint_Error (Actval);
3467 Set_Etype (Actval, Etype (F));
3468 end if;
3469
3470 Assoc :=
3471 Make_Parameter_Association (Loc,
3472 Explicit_Actual_Parameter => Actval,
3473 Selector_Name => Make_Identifier (Loc, Chars (F)));
3474
3475 -- Case of insertion is first named actual
3476
3477 if No (Prev) or else
3478 Nkind (Parent (Prev)) /= N_Parameter_Association
3479 then
3480 Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3481 Set_First_Named_Actual (N, Actval);
3482
3483 if No (Prev) then
c8ef728f 3484 if No (Parameter_Associations (N)) then
996ae0b0
RK
3485 Set_Parameter_Associations (N, New_List (Assoc));
3486 else
3487 Append (Assoc, Parameter_Associations (N));
3488 end if;
3489
3490 else
3491 Insert_After (Prev, Assoc);
3492 end if;
3493
3494 -- Case of insertion is not first named actual
3495
3496 else
3497 Set_Next_Named_Actual
3498 (Assoc, Next_Named_Actual (Parent (Prev)));
3499 Set_Next_Named_Actual (Parent (Prev), Actval);
3500 Append (Assoc, Parameter_Associations (N));
3501 end if;
3502
3503 Mark_Rewrite_Insertion (Assoc);
3504 Mark_Rewrite_Insertion (Actval);
3505
3506 Prev := Actval;
3507 end Insert_Default;
3508
97779c34
AC
3509 --------------------
3510 -- Property_Error --
3511 --------------------
3512
3513 procedure Property_Error
3514 (Var : Node_Id;
3515 Var_Id : Entity_Id;
3516 Prop_Nam : Name_Id)
3517 is
3518 begin
3519 Error_Msg_Name_1 := Prop_Nam;
3520 Error_Msg_NE
3521 ("external variable & with enabled property % cannot appear as "
3522 & "actual in procedure call (SPARK RM 7.1.3(11))", Var, Var_Id);
3523 Error_Msg_N ("\\corresponding formal parameter has mode In", Var);
3524 end Property_Error;
3525
fbf5a39b
AC
3526 -------------------
3527 -- Same_Ancestor --
3528 -------------------
3529
3530 function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3531 FT1 : Entity_Id := T1;
3532 FT2 : Entity_Id := T2;
3533
3534 begin
3535 if Is_Private_Type (T1)
3536 and then Present (Full_View (T1))
3537 then
3538 FT1 := Full_View (T1);
3539 end if;
3540
3541 if Is_Private_Type (T2)
3542 and then Present (Full_View (T2))
3543 then
3544 FT2 := Full_View (T2);
3545 end if;
3546
3547 return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3548 end Same_Ancestor;
3549
a7a3cf5c
AC
3550 --------------------------
3551 -- Static_Concatenation --
3552 --------------------------
3553
3554 function Static_Concatenation (N : Node_Id) return Boolean is
3555 begin
c72a85f2
TQ
3556 case Nkind (N) is
3557 when N_String_Literal =>
3558 return True;
a7a3cf5c 3559
d81b4bfe
TQ
3560 when N_Op_Concat =>
3561
5cc9353d
RD
3562 -- Concatenation is static when both operands are static and
3563 -- the concatenation operator is a predefined one.
4342eda9
TQ
3564
3565 return Scope (Entity (N)) = Standard_Standard
3566 and then
3567 Static_Concatenation (Left_Opnd (N))
c72a85f2
TQ
3568 and then
3569 Static_Concatenation (Right_Opnd (N));
3570
3571 when others =>
3572 if Is_Entity_Name (N) then
3573 declare
3574 Ent : constant Entity_Id := Entity (N);
3575 begin
3576 return Ekind (Ent) = E_Constant
3577 and then Present (Constant_Value (Ent))
d81b4bfe 3578 and then
edab6088 3579 Is_OK_Static_Expression (Constant_Value (Ent));
c72a85f2 3580 end;
a7a3cf5c 3581
a7a3cf5c
AC
3582 else
3583 return False;
3584 end if;
c72a85f2 3585 end case;
a7a3cf5c
AC
3586 end Static_Concatenation;
3587
996ae0b0
RK
3588 -- Start of processing for Resolve_Actuals
3589
3590 begin
45fc7ddb
HK
3591 Check_Argument_Order;
3592
e6b3f5ba
ES
3593 if Is_Overloadable (Nam)
3594 and then Is_Inherited_Operation (Nam)
4d6a38a5 3595 and then In_Instance
e6b3f5ba
ES
3596 and then Present (Alias (Nam))
3597 and then Present (Overridden_Operation (Alias (Nam)))
3598 then
3599 Real_Subp := Alias (Nam);
3600 else
3601 Real_Subp := Empty;
3602 end if;
3603
b7d1f17f
HK
3604 if Present (First_Actual (N)) then
3605 Check_Prefixed_Call;
3606 end if;
3607
996ae0b0
RK
3608 A := First_Actual (N);
3609 F := First_Formal (Nam);
e6b3f5ba
ES
3610
3611 if Present (Real_Subp) then
3612 Real_F := First_Formal (Real_Subp);
3613 end if;
3614
996ae0b0 3615 while Present (F) loop
fbf5a39b
AC
3616 if No (A) and then Needs_No_Actuals (Nam) then
3617 null;
996ae0b0 3618
d81b4bfe
TQ
3619 -- If we have an error in any actual or formal, indicated by a type
3620 -- of Any_Type, then abandon resolution attempt, and set result type
7610fee8
AC
3621 -- to Any_Type. Skip this if the actual is a Raise_Expression, whose
3622 -- type is imposed from context.
07fc65c4 3623
fbf5a39b
AC
3624 elsif (Present (A) and then Etype (A) = Any_Type)
3625 or else Etype (F) = Any_Type
07fc65c4 3626 then
7610fee8
AC
3627 if Nkind (A) /= N_Raise_Expression then
3628 Set_Etype (N, Any_Type);
3629 return;
3630 end if;
07fc65c4
GB
3631 end if;
3632
e65f50ec
ES
3633 -- Case where actual is present
3634
45fc7ddb 3635 -- If the actual is an entity, generate a reference to it now. We
36fcf362
RD
3636 -- do this before the actual is resolved, because a formal of some
3637 -- protected subprogram, or a task discriminant, will be rewritten
5cc9353d 3638 -- during expansion, and the source entity reference may be lost.
36fcf362
RD
3639
3640 if Present (A)
3641 and then Is_Entity_Name (A)
3642 and then Comes_From_Source (N)
3643 then
3644 Orig_A := Entity (A);
3645
3646 if Present (Orig_A) then
3647 if Is_Formal (Orig_A)
3648 and then Ekind (F) /= E_In_Parameter
3649 then
3650 Generate_Reference (Orig_A, A, 'm');
19fb051c 3651
36fcf362 3652 elsif not Is_Overloaded (A) then
ba08ba84
AC
3653 if Ekind (F) /= E_Out_Parameter then
3654 Generate_Reference (Orig_A, A);
3655
3656 -- RM 6.4.1(12): For an out parameter that is passed by
3657 -- copy, the formal parameter object is created, and:
3658
3659 -- * For an access type, the formal parameter is initialized
3660 -- from the value of the actual, without checking that the
3661 -- value satisfies any constraint, any predicate, or any
3662 -- exclusion of the null value.
3663
3664 -- * For a scalar type that has the Default_Value aspect
3665 -- specified, the formal parameter is initialized from the
3666 -- value of the actual, without checking that the value
c91dbd18
AC
3667 -- satisfies any constraint or any predicate.
3668 -- I do not understand why this case is included??? this is
3669 -- not a case where an OUT parameter is treated as IN OUT.
ba08ba84
AC
3670
3671 -- * For a composite type with discriminants or that has
3672 -- implicit initial values for any subcomponents, the
3673 -- behavior is as for an in out parameter passed by copy.
3674
3675 -- Hence for these cases we generate the read reference now
3676 -- (the write reference will be generated later by
3677 -- Note_Possible_Modification).
3678
3679 elsif Is_By_Copy_Type (Etype (F))
3680 and then
3681 (Is_Access_Type (Etype (F))
3682 or else
3683 (Is_Scalar_Type (Etype (F))
3684 and then
3685 Present (Default_Aspect_Value (Etype (F))))
3686 or else
3687 (Is_Composite_Type (Etype (F))
c91dbd18
AC
3688 and then (Has_Discriminants (Etype (F))
3689 or else Is_Partially_Initialized_Type
3690 (Etype (F)))))
ba08ba84
AC
3691 then
3692 Generate_Reference (Orig_A, A);
3693 end if;
36fcf362
RD
3694 end if;
3695 end if;
3696 end if;
3697
996ae0b0
RK
3698 if Present (A)
3699 and then (Nkind (Parent (A)) /= N_Parameter_Association
19fb051c 3700 or else Chars (Selector_Name (Parent (A))) = Chars (F))
996ae0b0 3701 then
45fc7ddb
HK
3702 -- If style checking mode on, check match of formal name
3703
3704 if Style_Check then
3705 if Nkind (Parent (A)) = N_Parameter_Association then
3706 Check_Identifier (Selector_Name (Parent (A)), F);
3707 end if;
3708 end if;
3709
996ae0b0
RK
3710 -- If the formal is Out or In_Out, do not resolve and expand the
3711 -- conversion, because it is subsequently expanded into explicit
3712 -- temporaries and assignments. However, the object of the
ea985d95
RD
3713 -- conversion can be resolved. An exception is the case of tagged
3714 -- type conversion with a class-wide actual. In that case we want
3715 -- the tag check to occur and no temporary will be needed (no
3716 -- representation change can occur) and the parameter is passed by
3717 -- reference, so we go ahead and resolve the type conversion.
c8ef728f 3718 -- Another exception is the case of reference to component or
ea985d95
RD
3719 -- subcomponent of a bit-packed array, in which case we want to
3720 -- defer expansion to the point the in and out assignments are
3721 -- performed.
996ae0b0
RK
3722
3723 if Ekind (F) /= E_In_Parameter
3724 and then Nkind (A) = N_Type_Conversion
3725 and then not Is_Class_Wide_Type (Etype (Expression (A)))
3726 then
07fc65c4
GB
3727 if Ekind (F) = E_In_Out_Parameter
3728 and then Is_Array_Type (Etype (F))
07fc65c4 3729 then
038140ed
AC
3730 -- In a view conversion, the conversion must be legal in
3731 -- both directions, and thus both component types must be
3732 -- aliased, or neither (4.6 (8)).
758c442c 3733
038140ed
AC
3734 -- The extra rule in 4.6 (24.9.2) seems unduly restrictive:
3735 -- the privacy requirement should not apply to generic
3736 -- types, and should be checked in an instance. ARG query
3737 -- is in order ???
45fc7ddb 3738
038140ed
AC
3739 if Has_Aliased_Components (Etype (Expression (A))) /=
3740 Has_Aliased_Components (Etype (F))
3741 then
45fc7ddb
HK
3742 Error_Msg_N
3743 ("both component types in a view conversion must be"
3744 & " aliased, or neither", A);
3745
038140ed
AC
3746 -- Comment here??? what set of cases???
3747
45fc7ddb
HK
3748 elsif
3749 not Same_Ancestor (Etype (F), Etype (Expression (A)))
3750 then
038140ed
AC
3751 -- Check view conv between unrelated by ref array types
3752
45fc7ddb
HK
3753 if Is_By_Reference_Type (Etype (F))
3754 or else Is_By_Reference_Type (Etype (Expression (A)))
758c442c
GD
3755 then
3756 Error_Msg_N
1486a00e
AC
3757 ("view conversion between unrelated by reference "
3758 & "array types not allowed (\'A'I-00246)", A);
038140ed
AC
3759
3760 -- In Ada 2005 mode, check view conversion component
3761 -- type cannot be private, tagged, or volatile. Note
3762 -- that we only apply this to source conversions. The
3763 -- generated code can contain conversions which are
3764 -- not subject to this test, and we cannot extract the
3765 -- component type in such cases since it is not present.
3766
3767 elsif Comes_From_Source (A)
3768 and then Ada_Version >= Ada_2005
3769 then
45fc7ddb
HK
3770 declare
3771 Comp_Type : constant Entity_Id :=
3772 Component_Type
3773 (Etype (Expression (A)));
3774 begin
038140ed
AC
3775 if (Is_Private_Type (Comp_Type)
3776 and then not Is_Generic_Type (Comp_Type))
3777 or else Is_Tagged_Type (Comp_Type)
3778 or else Is_Volatile (Comp_Type)
45fc7ddb
HK
3779 then
3780 Error_Msg_N
3781 ("component type of a view conversion cannot"
3782 & " be private, tagged, or volatile"
3783 & " (RM 4.6 (24))",
3784 Expression (A));
3785 end if;
3786 end;
758c442c 3787 end if;
fbf5a39b 3788 end if;
07fc65c4
GB
3789 end if;
3790
038140ed
AC
3791 -- Resolve expression if conversion is all OK
3792
16397eff 3793 if (Conversion_OK (A)
038140ed 3794 or else Valid_Conversion (A, Etype (A), Expression (A)))
16397eff 3795 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
996ae0b0 3796 then
fbf5a39b 3797 Resolve (Expression (A));
996ae0b0
RK
3798 end if;
3799
b7d1f17f
HK
3800 -- If the actual is a function call that returns a limited
3801 -- unconstrained object that needs finalization, create a
3802 -- transient scope for it, so that it can receive the proper
3803 -- finalization list.
3804
3805 elsif Nkind (A) = N_Function_Call
3806 and then Is_Limited_Record (Etype (F))
3807 and then not Is_Constrained (Etype (F))
4460a9bc 3808 and then Expander_Active
19fb051c 3809 and then (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
b7d1f17f 3810 then
13b2f7fd 3811 Establish_Transient_Scope (A, Sec_Stack => False);
24a120ac 3812 Resolve (A, Etype (F));
b7d1f17f 3813
a52fefe6
AC
3814 -- A small optimization: if one of the actuals is a concatenation
3815 -- create a block around a procedure call to recover stack space.
3816 -- This alleviates stack usage when several procedure calls in
76e776e5
AC
3817 -- the same statement list use concatenation. We do not perform
3818 -- this wrapping for code statements, where the argument is a
3819 -- static string, and we want to preserve warnings involving
3820 -- sequences of such statements.
a52fefe6
AC
3821
3822 elsif Nkind (A) = N_Op_Concat
3823 and then Nkind (N) = N_Procedure_Call_Statement
4460a9bc 3824 and then Expander_Active
76e776e5
AC
3825 and then
3826 not (Is_Intrinsic_Subprogram (Nam)
3827 and then Chars (Nam) = Name_Asm)
a7a3cf5c 3828 and then not Static_Concatenation (A)
a52fefe6 3829 then
13b2f7fd 3830 Establish_Transient_Scope (A, Sec_Stack => False);
a52fefe6
AC
3831 Resolve (A, Etype (F));
3832
996ae0b0 3833 else
fbf5a39b
AC
3834 if Nkind (A) = N_Type_Conversion
3835 and then Is_Array_Type (Etype (F))
3836 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3837 and then
3838 (Is_Limited_Type (Etype (F))
2e86f679 3839 or else Is_Limited_Type (Etype (Expression (A))))
fbf5a39b
AC
3840 then
3841 Error_Msg_N
1486a00e 3842 ("conversion between unrelated limited array types "
2590ef12 3843 & "not allowed ('A'I-00246)", A);
fbf5a39b 3844
758c442c
GD
3845 if Is_Limited_Type (Etype (F)) then
3846 Explain_Limited_Type (Etype (F), A);
3847 end if;
fbf5a39b 3848
758c442c
GD
3849 if Is_Limited_Type (Etype (Expression (A))) then
3850 Explain_Limited_Type (Etype (Expression (A)), A);
3851 end if;
fbf5a39b
AC
3852 end if;
3853
c8ef728f
ES
3854 -- (Ada 2005: AI-251): If the actual is an allocator whose
3855 -- directly designated type is a class-wide interface, we build
3856 -- an anonymous access type to use it as the type of the
3857 -- allocator. Later, when the subprogram call is expanded, if
3858 -- the interface has a secondary dispatch table the expander
3859 -- will add a type conversion to force the correct displacement
3860 -- of the pointer.
3861
3862 if Nkind (A) = N_Allocator then
3863 declare
3864 DDT : constant Entity_Id :=
3865 Directly_Designated_Type (Base_Type (Etype (F)));
45fc7ddb 3866
c8ef728f 3867 New_Itype : Entity_Id;
45fc7ddb 3868
c8ef728f
ES
3869 begin
3870 if Is_Class_Wide_Type (DDT)
3871 and then Is_Interface (DDT)
3872 then
3873 New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
45fc7ddb 3874 Set_Etype (New_Itype, Etype (A));
2590ef12
RD
3875 Set_Directly_Designated_Type
3876 (New_Itype, Directly_Designated_Type (Etype (A)));
c8ef728f
ES
3877 Set_Etype (A, New_Itype);
3878 end if;
0669bebe
GB
3879
3880 -- Ada 2005, AI-162:If the actual is an allocator, the
3881 -- innermost enclosing statement is the master of the
b7d1f17f
HK
3882 -- created object. This needs to be done with expansion
3883 -- enabled only, otherwise the transient scope will not
3884 -- be removed in the expansion of the wrapped construct.
0669bebe 3885
45fc7ddb 3886 if (Is_Controlled (DDT) or else Has_Task (DDT))
4460a9bc 3887 and then Expander_Active
0669bebe 3888 then
13b2f7fd 3889 Establish_Transient_Scope (A, Sec_Stack => False);
0669bebe 3890 end if;
c8ef728f 3891 end;
57f4c288
ES
3892
3893 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3894 Check_Restriction (No_Access_Parameter_Allocators, A);
3895 end if;
c8ef728f
ES
3896 end if;
3897
2e86f679
RD
3898 -- (Ada 2005): The call may be to a primitive operation of a
3899 -- tagged synchronized type, declared outside of the type. In
3900 -- this case the controlling actual must be converted to its
3901 -- corresponding record type, which is the formal type. The
3902 -- actual may be a subtype, either because of a constraint or
3903 -- because it is a generic actual, so use base type to locate
3904 -- concurrent type.
b7d1f17f 3905
15e4986c
JM
3906 F_Typ := Base_Type (Etype (F));
3907
cb7fa356
AC
3908 if Is_Tagged_Type (F_Typ)
3909 and then (Is_Concurrent_Type (F_Typ)
2590ef12 3910 or else Is_Concurrent_Record_Type (F_Typ))
cb7fa356
AC
3911 then
3912 -- If the actual is overloaded, look for an interpretation
3913 -- that has a synchronized type.
3914
3915 if not Is_Overloaded (A) then
3916 A_Typ := Base_Type (Etype (A));
15e4986c 3917
15e4986c 3918 else
cb7fa356
AC
3919 declare
3920 Index : Interp_Index;
3921 It : Interp;
218e6dee 3922
cb7fa356
AC
3923 begin
3924 Get_First_Interp (A, Index, It);
3925 while Present (It.Typ) loop
3926 if Is_Concurrent_Type (It.Typ)
3927 or else Is_Concurrent_Record_Type (It.Typ)
3928 then
3929 A_Typ := Base_Type (It.Typ);
3930 exit;
3931 end if;
3932
3933 Get_Next_Interp (Index, It);
3934 end loop;
3935 end;
15e4986c 3936 end if;
b7d1f17f 3937
cb7fa356
AC
3938 declare
3939 Full_A_Typ : Entity_Id;
15e4986c 3940
cb7fa356
AC
3941 begin
3942 if Present (Full_View (A_Typ)) then
3943 Full_A_Typ := Base_Type (Full_View (A_Typ));
3944 else
3945 Full_A_Typ := A_Typ;
3946 end if;
3947
3948 -- Tagged synchronized type (case 1): the actual is a
3949 -- concurrent type.
3950
3951 if Is_Concurrent_Type (A_Typ)
3952 and then Corresponding_Record_Type (A_Typ) = F_Typ
3953 then
3954 Rewrite (A,
3955 Unchecked_Convert_To
3956 (Corresponding_Record_Type (A_Typ), A));
3957 Resolve (A, Etype (F));
15e4986c 3958
cb7fa356
AC
3959 -- Tagged synchronized type (case 2): the formal is a
3960 -- concurrent type.
15e4986c 3961
cb7fa356
AC
3962 elsif Ekind (Full_A_Typ) = E_Record_Type
3963 and then Present
15e4986c 3964 (Corresponding_Concurrent_Type (Full_A_Typ))
cb7fa356
AC
3965 and then Is_Concurrent_Type (F_Typ)
3966 and then Present (Corresponding_Record_Type (F_Typ))
3967 and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
3968 then
3969 Resolve (A, Corresponding_Record_Type (F_Typ));
15e4986c 3970
cb7fa356 3971 -- Common case
15e4986c 3972
cb7fa356
AC
3973 else
3974 Resolve (A, Etype (F));
3975 end if;
3976 end;
cb7fa356 3977
2590ef12 3978 -- Not a synchronized operation
cb7fa356 3979
2590ef12 3980 else
cb7fa356
AC
3981 Resolve (A, Etype (F));
3982 end if;
996ae0b0
RK
3983 end if;
3984
3985 A_Typ := Etype (A);
3986 F_Typ := Etype (F);
3987
1ebc2612
AC
3988 -- An actual cannot be an untagged formal incomplete type
3989
3990 if Ekind (A_Typ) = E_Incomplete_Type
3991 and then not Is_Tagged_Type (A_Typ)
3992 and then Is_Generic_Type (A_Typ)
3993 then
3994 Error_Msg_N
3995 ("invalid use of untagged formal incomplete type", A);
3996 end if;
3997
e24329cd 3998 if Comes_From_Source (Original_Node (N))
6320f5e1
AC
3999 and then Nkind_In (Original_Node (N), N_Function_Call,
4000 N_Procedure_Call_Statement)
b0186f71 4001 then
e24329cd
YM
4002 -- In formal mode, check that actual parameters matching
4003 -- formals of tagged types are objects (or ancestor type
4004 -- conversions of objects), not general expressions.
780d052e 4005
e24329cd 4006 if Is_Actual_Tagged_Parameter (A) then
ce5ba43a 4007 if Is_SPARK_05_Object_Reference (A) then
e24329cd
YM
4008 null;
4009
4010 elsif Nkind (A) = N_Type_Conversion then
4011 declare
4012 Operand : constant Node_Id := Expression (A);
4013 Operand_Typ : constant Entity_Id := Etype (Operand);
4014 Target_Typ : constant Entity_Id := A_Typ;
4015
4016 begin
ce5ba43a
AC
4017 if not Is_SPARK_05_Object_Reference (Operand) then
4018 Check_SPARK_05_Restriction
e24329cd
YM
4019 ("object required", Operand);
4020
4021 -- In formal mode, the only view conversions are those
4022 -- involving ancestor conversion of an extended type.
4023
4024 elsif not
4025 (Is_Tagged_Type (Target_Typ)
780d052e
RD
4026 and then not Is_Class_Wide_Type (Target_Typ)
4027 and then Is_Tagged_Type (Operand_Typ)
4028 and then not Is_Class_Wide_Type (Operand_Typ)
4029 and then Is_Ancestor (Target_Typ, Operand_Typ))
e24329cd
YM
4030 then
4031 if Ekind_In
4032 (F, E_Out_Parameter, E_In_Out_Parameter)
4033 then
ce5ba43a 4034 Check_SPARK_05_Restriction
e24329cd
YM
4035 ("ancestor conversion is the only permitted "
4036 & "view conversion", A);
4037 else
ce5ba43a 4038 Check_SPARK_05_Restriction
e24329cd
YM
4039 ("ancestor conversion required", A);
4040 end if;
4041
4042 else
4043 null;
4044 end if;
4045 end;
4046
4047 else
ce5ba43a 4048 Check_SPARK_05_Restriction ("object required", A);
b0186f71 4049 end if;
e24329cd
YM
4050
4051 -- In formal mode, the only view conversions are those
4052 -- involving ancestor conversion of an extended type.
4053
4054 elsif Nkind (A) = N_Type_Conversion
4055 and then Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
4056 then
ce5ba43a 4057 Check_SPARK_05_Restriction
e24329cd
YM
4058 ("ancestor conversion is the only permitted view "
4059 & "conversion", A);
4060 end if;
b0186f71
AC
4061 end if;
4062
26570b21
RD
4063 -- has warnings suppressed, then we reset Never_Set_In_Source for
4064 -- the calling entity. The reason for this is to catch cases like
4065 -- GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
4066 -- uses trickery to modify an IN parameter.
4067
4068 if Ekind (F) = E_In_Parameter
4069 and then Is_Entity_Name (A)
4070 and then Present (Entity (A))
4071 and then Ekind (Entity (A)) = E_Variable
4072 and then Has_Warnings_Off (F_Typ)
4073 then
4074 Set_Never_Set_In_Source (Entity (A), False);
4075 end if;
4076
fbf5a39b
AC
4077 -- Perform error checks for IN and IN OUT parameters
4078
4079 if Ekind (F) /= E_Out_Parameter then
4080
4081 -- Check unset reference. For scalar parameters, it is clearly
4082 -- wrong to pass an uninitialized value as either an IN or
4083 -- IN-OUT parameter. For composites, it is also clearly an
4084 -- error to pass a completely uninitialized value as an IN
4085 -- parameter, but the case of IN OUT is trickier. We prefer
4086 -- not to give a warning here. For example, suppose there is
4087 -- a routine that sets some component of a record to False.
4088 -- It is perfectly reasonable to make this IN-OUT and allow
4089 -- either initialized or uninitialized records to be passed
4090 -- in this case.
4091
4092 -- For partially initialized composite values, we also avoid
4093 -- warnings, since it is quite likely that we are passing a
4094 -- partially initialized value and only the initialized fields
4095 -- will in fact be read in the subprogram.
4096
4097 if Is_Scalar_Type (A_Typ)
4098 or else (Ekind (F) = E_In_Parameter
19fb051c 4099 and then not Is_Partially_Initialized_Type (A_Typ))
996ae0b0 4100 then
fbf5a39b 4101 Check_Unset_Reference (A);
996ae0b0 4102 end if;
996ae0b0 4103
758c442c 4104 -- In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
a921e83c
AC
4105 -- actual to a nested call, since this constitutes a reading of
4106 -- the parameter, which is not allowed.
996ae0b0 4107
847d950d
HK
4108 if Ada_Version = Ada_83
4109 and then Is_Entity_Name (A)
996ae0b0
RK
4110 and then Ekind (Entity (A)) = E_Out_Parameter
4111 then
847d950d 4112 Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
996ae0b0
RK
4113 end if;
4114 end if;
4115
67ce0d7e
RD
4116 -- Case of OUT or IN OUT parameter
4117
36fcf362 4118 if Ekind (F) /= E_In_Parameter then
67ce0d7e
RD
4119
4120 -- For an Out parameter, check for useless assignment. Note
45fc7ddb
HK
4121 -- that we can't set Last_Assignment this early, because we may
4122 -- kill current values in Resolve_Call, and that call would
4123 -- clobber the Last_Assignment field.
67ce0d7e 4124
45fc7ddb
HK
4125 -- Note: call Warn_On_Useless_Assignment before doing the check
4126 -- below for Is_OK_Variable_For_Out_Formal so that the setting
4127 -- of Referenced_As_LHS/Referenced_As_Out_Formal properly
a90bd866 4128 -- reflects the last assignment, not this one.
36fcf362 4129
67ce0d7e 4130 if Ekind (F) = E_Out_Parameter then
36fcf362 4131 if Warn_On_Modified_As_Out_Parameter (F)
67ce0d7e
RD
4132 and then Is_Entity_Name (A)
4133 and then Present (Entity (A))
36fcf362 4134 and then Comes_From_Source (N)
67ce0d7e 4135 then
36fcf362 4136 Warn_On_Useless_Assignment (Entity (A), A);
67ce0d7e
RD
4137 end if;
4138 end if;
4139
36fcf362
RD
4140 -- Validate the form of the actual. Note that the call to
4141 -- Is_OK_Variable_For_Out_Formal generates the required
4142 -- reference in this case.
4143
0180fd26
AC
4144 -- A call to an initialization procedure for an aggregate
4145 -- component may initialize a nested component of a constant
4146 -- designated object. In this context the object is variable.
4147
4148 if not Is_OK_Variable_For_Out_Formal (A)
4149 and then not Is_Init_Proc (Nam)
4150 then
36fcf362 4151 Error_Msg_NE ("actual for& must be a variable", A, F);
43dbd3e3
AC
4152
4153 if Is_Subprogram (Current_Scope)
4154 and then
4155 (Is_Invariant_Procedure (Current_Scope)
2590ef12 4156 or else Is_Predicate_Function (Current_Scope))
43dbd3e3 4157 then
2590ef12
RD
4158 Error_Msg_N
4159 ("function used in predicate cannot "
4160 & "modify its argument", F);
43dbd3e3 4161 end if;
36fcf362
RD
4162 end if;
4163
67ce0d7e 4164 -- What's the following about???
fbf5a39b
AC
4165
4166 if Is_Entity_Name (A) then
4167 Kill_Checks (Entity (A));
4168 else
4169 Kill_All_Checks;
4170 end if;
4171 end if;
4172
4173 if Etype (A) = Any_Type then
4174 Set_Etype (N, Any_Type);
4175 return;
4176 end if;
4177
5f6fb720 4178 -- Apply appropriate constraint/predicate checks for IN [OUT] case
996ae0b0 4179
8a95f4e8 4180 if Ekind_In (F, E_In_Parameter, E_In_Out_Parameter) then
48f91b44 4181
5f6fb720
AC
4182 -- Apply predicate tests except in certain special cases. Note
4183 -- that it might be more consistent to apply these only when
4184 -- expansion is active (in Exp_Ch6.Expand_Actuals), as we do
b8e6830b 4185 -- for the outbound predicate tests ???
48f91b44 4186
b8e6830b 4187 if Predicate_Tests_On_Arguments (Nam) then
48f91b44
RD
4188 Apply_Predicate_Check (A, F_Typ);
4189 end if;
4190
4191 -- Apply required constraint checks
4192
5f6fb720
AC
4193 -- Gigi looks at the check flag and uses the appropriate types.
4194 -- For now since one flag is used there is an optimization
4195 -- which might not be done in the IN OUT case since Gigi does
4196 -- not do any analysis. More thought required about this ???
4197
4198 -- In fact is this comment obsolete??? doesn't the expander now
4199 -- generate all these tests anyway???
4200
996ae0b0
RK
4201 if Is_Scalar_Type (Etype (A)) then
4202 Apply_Scalar_Range_Check (A, F_Typ);
4203
4204 elsif Is_Array_Type (Etype (A)) then
4205 Apply_Length_Check (A, F_Typ);
4206
4207 elsif Is_Record_Type (F_Typ)
4208 and then Has_Discriminants (F_Typ)
4209 and then Is_Constrained (F_Typ)
4210 and then (not Is_Derived_Type (F_Typ)
19fb051c 4211 or else Comes_From_Source (Nam))
996ae0b0
RK
4212 then
4213 Apply_Discriminant_Check (A, F_Typ);
4214
f1bd0415
AC
4215 -- For view conversions of a discriminated object, apply
4216 -- check to object itself, the conversion alreay has the
4217 -- proper type.
4218
4219 if Nkind (A) = N_Type_Conversion
4220 and then Is_Constrained (Etype (Expression (A)))
4221 then
4222 Apply_Discriminant_Check (Expression (A), F_Typ);
4223 end if;
4224
996ae0b0
RK
4225 elsif Is_Access_Type (F_Typ)
4226 and then Is_Array_Type (Designated_Type (F_Typ))
4227 and then Is_Constrained (Designated_Type (F_Typ))
4228 then
4229 Apply_Length_Check (A, F_Typ);
4230
4231 elsif Is_Access_Type (F_Typ)
4232 and then Has_Discriminants (Designated_Type (F_Typ))
4233 and then Is_Constrained (Designated_Type (F_Typ))
4234 then
4235 Apply_Discriminant_Check (A, F_Typ);
4236
4237 else
4238 Apply_Range_Check (A, F_Typ);
4239 end if;
2820d220 4240
0f1a6a0b
AC
4241 -- Ada 2005 (AI-231): Note that the controlling parameter case
4242 -- already existed in Ada 95, which is partially checked
4243 -- elsewhere (see Checks), and we don't want the warning
4244 -- message to differ.
2820d220 4245
0f1a6a0b 4246 if Is_Access_Type (F_Typ)
1420b484 4247 and then Can_Never_Be_Null (F_Typ)
aa5147f0 4248 and then Known_Null (A)
2820d220 4249 then
0f1a6a0b
AC
4250 if Is_Controlling_Formal (F) then
4251 Apply_Compile_Time_Constraint_Error
4252 (N => A,
324ac540 4253 Msg => "null value not allowed here??",
0f1a6a0b
AC
4254 Reason => CE_Access_Check_Failed);
4255
4256 elsif Ada_Version >= Ada_2005 then
4257 Apply_Compile_Time_Constraint_Error
4258 (N => A,
4259 Msg => "(Ada 2005) null not allowed in "
324ac540 4260 & "null-excluding formal??",
0f1a6a0b
AC
4261 Reason => CE_Null_Not_Allowed);
4262 end if;
2820d220 4263 end if;
996ae0b0
RK
4264 end if;
4265
5f6fb720
AC
4266 -- Checks for OUT parameters and IN OUT parameters
4267
8a95f4e8 4268 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter) then
5f6fb720
AC
4269
4270 -- If there is a type conversion, to make sure the return value
4271 -- meets the constraints of the variable before the conversion.
4272
996ae0b0
RK
4273 if Nkind (A) = N_Type_Conversion then
4274 if Is_Scalar_Type (A_Typ) then
4275 Apply_Scalar_Range_Check
4276 (Expression (A), Etype (Expression (A)), A_Typ);
4277 else
4278 Apply_Range_Check
4279 (Expression (A), Etype (Expression (A)), A_Typ);
4280 end if;
4281
5f6fb720
AC
4282 -- If no conversion apply scalar range checks and length checks
4283 -- base on the subtype of the actual (NOT that of the formal).
4284
996ae0b0
RK
4285 else
4286 if Is_Scalar_Type (F_Typ) then
4287 Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
996ae0b0
RK
4288 elsif Is_Array_Type (F_Typ)
4289 and then Ekind (F) = E_Out_Parameter
4290 then
4291 Apply_Length_Check (A, F_Typ);
996ae0b0
RK
4292 else
4293 Apply_Range_Check (A, A_Typ, F_Typ);
4294 end if;
4295 end if;
5f6fb720
AC
4296
4297 -- Note: we do not apply the predicate checks for the case of
4298 -- OUT and IN OUT parameters. They are instead applied in the
4299 -- Expand_Actuals routine in Exp_Ch6.
996ae0b0
RK
4300 end if;
4301
4302 -- An actual associated with an access parameter is implicitly
45fc7ddb
HK
4303 -- converted to the anonymous access type of the formal and must
4304 -- satisfy the legality checks for access conversions.
996ae0b0
RK
4305
4306 if Ekind (F_Typ) = E_Anonymous_Access_Type then
4307 if not Valid_Conversion (A, F_Typ, A) then
4308 Error_Msg_N
4309 ("invalid implicit conversion for access parameter", A);
4310 end if;
de94a7e7
AC
4311
4312 -- If the actual is an access selected component of a variable,
4313 -- the call may modify its designated object. It is reasonable
4314 -- to treat this as a potential modification of the enclosing
4315 -- record, to prevent spurious warnings that it should be
4316 -- declared as a constant, because intuitively programmers
4317 -- regard the designated subcomponent as part of the record.
4318
4319 if Nkind (A) = N_Selected_Component
4320 and then Is_Entity_Name (Prefix (A))
4321 and then not Is_Constant_Object (Entity (Prefix (A)))
4322 then
4323 Note_Possible_Modification (A, Sure => False);
4324 end if;
996ae0b0
RK
4325 end if;
4326
4327 -- Check bad case of atomic/volatile argument (RM C.6(12))
4328
4329 if Is_By_Reference_Type (Etype (F))
4330 and then Comes_From_Source (N)
4331 then
4332 if Is_Atomic_Object (A)
4333 and then not Is_Atomic (Etype (F))
4334 then
b5bf3335
AC
4335 Error_Msg_NE
4336 ("cannot pass atomic argument to non-atomic formal&",
4337 A, F);
996ae0b0
RK
4338
4339 elsif Is_Volatile_Object (A)
4340 and then not Is_Volatile (Etype (F))
4341 then
b5bf3335
AC
4342 Error_Msg_NE
4343 ("cannot pass volatile argument to non-volatile formal&",
4344 A, F);
996ae0b0
RK
4345 end if;
4346 end if;
4347
4348 -- Check that subprograms don't have improper controlling
d81b4bfe 4349 -- arguments (RM 3.9.2 (9)).
996ae0b0 4350
0669bebe
GB
4351 -- A primitive operation may have an access parameter of an
4352 -- incomplete tagged type, but a dispatching call is illegal
4353 -- if the type is still incomplete.
4354
996ae0b0
RK
4355 if Is_Controlling_Formal (F) then
4356 Set_Is_Controlling_Actual (A);
0669bebe
GB
4357
4358 if Ekind (Etype (F)) = E_Anonymous_Access_Type then
4359 declare
4360 Desig : constant Entity_Id := Designated_Type (Etype (F));
4361 begin
4362 if Ekind (Desig) = E_Incomplete_Type
4363 and then No (Full_View (Desig))
4364 and then No (Non_Limited_View (Desig))
4365 then
4366 Error_Msg_NE
1486a00e
AC
4367 ("premature use of incomplete type& "
4368 & "in dispatching call", A, Desig);
0669bebe
GB
4369 end if;
4370 end;
4371 end if;
4372
996ae0b0
RK
4373 elsif Nkind (A) = N_Explicit_Dereference then
4374 Validate_Remote_Access_To_Class_Wide_Type (A);
4375 end if;
4376
6c802906
AC
4377 -- Apply legality rule 3.9.2 (9/1)
4378
996ae0b0
RK
4379 if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
4380 and then not Is_Class_Wide_Type (F_Typ)
4381 and then not Is_Controlling_Formal (F)
6c802906 4382 and then not In_Instance
996ae0b0
RK
4383 then
4384 Error_Msg_N ("class-wide argument not allowed here!", A);
07fc65c4 4385
b9696ffb 4386 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
996ae0b0
RK
4387 Error_Msg_Node_2 := F_Typ;
4388 Error_Msg_NE
82c80734 4389 ("& is not a dispatching operation of &!", A, Nam);
996ae0b0
RK
4390 end if;
4391
97216ca8
ES
4392 -- Apply the checks described in 3.10.2(27): if the context is a
4393 -- specific access-to-object, the actual cannot be class-wide.
4394 -- Use base type to exclude access_to_subprogram cases.
4395
996ae0b0
RK
4396 elsif Is_Access_Type (A_Typ)
4397 and then Is_Access_Type (F_Typ)
97216ca8 4398 and then not Is_Access_Subprogram_Type (Base_Type (F_Typ))
996ae0b0 4399 and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
07fc65c4
GB
4400 or else (Nkind (A) = N_Attribute_Reference
4401 and then
2590ef12 4402 Is_Class_Wide_Type (Etype (Prefix (A)))))
996ae0b0
RK
4403 and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
4404 and then not Is_Controlling_Formal (F)
ae65d635 4405
46fe0142 4406 -- Disable these checks for call to imported C++ subprograms
ae65d635 4407
46fe0142
AC
4408 and then not
4409 (Is_Entity_Name (Name (N))
4410 and then Is_Imported (Entity (Name (N)))
4411 and then Convention (Entity (Name (N))) = Convention_CPP)
996ae0b0
RK
4412 then
4413 Error_Msg_N
4414 ("access to class-wide argument not allowed here!", A);
07fc65c4 4415
97216ca8 4416 if Is_Subprogram (Nam) and then Comes_From_Source (Nam) then
996ae0b0
RK
4417 Error_Msg_Node_2 := Designated_Type (F_Typ);
4418 Error_Msg_NE
82c80734 4419 ("& is not a dispatching operation of &!", A, Nam);
996ae0b0
RK
4420 end if;
4421 end if;
4422
f3691f46
ES
4423 Check_Aliased_Parameter;
4424
996ae0b0
RK
4425 Eval_Actual (A);
4426
8e4dac80 4427 -- If it is a named association, treat the selector_name as a
2590ef12 4428 -- proper identifier, and mark the corresponding entity.
996ae0b0 4429
1f9939b5 4430 if Nkind (Parent (A)) = N_Parameter_Association
2590ef12
RD
4431
4432 -- Ignore reference in SPARK mode, as it refers to an entity not
4433 -- in scope at the point of reference, so the reference should
4434 -- be ignored for computing effects of subprograms.
4435
f5da7a97 4436 and then not GNATprove_Mode
1f9939b5 4437 then
e6b3f5ba
ES
4438 -- If subprogram is overridden, use name of formal that
4439 -- is being called.
4440
4441 if Present (Real_Subp) then
4442 Set_Entity (Selector_Name (Parent (A)), Real_F);
4443 Set_Etype (Selector_Name (Parent (A)), Etype (Real_F));
4444
4445 else
4446 Set_Entity (Selector_Name (Parent (A)), F);
4447 Generate_Reference (F, Selector_Name (Parent (A)));
4448 Set_Etype (Selector_Name (Parent (A)), F_Typ);
4449 Generate_Reference (F_Typ, N, ' ');
4450 end if;
996ae0b0
RK
4451 end if;
4452
4453 Prev := A;
fbf5a39b
AC
4454
4455 if Ekind (F) /= E_Out_Parameter then
4456 Check_Unset_Reference (A);
4457 end if;
4458
fb1fdf7d 4459 -- The following checks are only relevant when SPARK_Mode is on as
7b4ebba5
AC
4460 -- they are not standard Ada legality rule. Internally generated
4461 -- temporaries are ignored.
6c3c671e 4462
fb1fdf7d 4463 if SPARK_Mode = On
7b4ebba5 4464 and then Comes_From_Source (A)
847d950d 4465 and then Is_Effectively_Volatile_Object (A)
6c3c671e 4466 then
ed962eda
AC
4467 -- An effectively volatile object may act as an actual when the
4468 -- corresponding formal is of a non-scalar volatile type
4469 -- (SPARK RM 7.1.3(12)).
6c3c671e
AC
4470
4471 if Is_Volatile (Etype (F))
4472 and then not Is_Scalar_Type (Etype (F))
4473 then
4474 null;
4475
ed962eda
AC
4476 -- An effectively volatile object may act as an actual in a
4477 -- call to an instance of Unchecked_Conversion.
4478 -- (SPARK RM 7.1.3(12)).
6c3c671e
AC
4479
4480 elsif Is_Unchecked_Conversion_Instance (Nam) then
4481 null;
4482
4483 else
4484 Error_Msg_N
fb1fdf7d 4485 ("volatile object cannot act as actual in a call (SPARK "
f1bd0415 4486 & "RM 7.1.3(12))", A);
6c3c671e 4487 end if;
97779c34
AC
4488
4489 -- Detect an external variable with an enabled property that
4490 -- does not match the mode of the corresponding formal in a
7b4ebba5
AC
4491 -- procedure call. Functions are not considered because they
4492 -- cannot have effectively volatile formal parameters in the
4493 -- first place.
97779c34
AC
4494
4495 if Ekind (Nam) = E_Procedure
de4ac038 4496 and then Ekind (F) = E_In_Parameter
97779c34
AC
4497 and then Is_Entity_Name (A)
4498 and then Present (Entity (A))
4499 and then Ekind (Entity (A)) = E_Variable
4500 then
4501 A_Id := Entity (A);
4502
de4ac038
AC
4503 if Async_Readers_Enabled (A_Id) then
4504 Property_Error (A, A_Id, Name_Async_Readers);
4505 elsif Effective_Reads_Enabled (A_Id) then
4506 Property_Error (A, A_Id, Name_Effective_Reads);
4507 elsif Effective_Writes_Enabled (A_Id) then
4508 Property_Error (A, A_Id, Name_Effective_Writes);
97779c34
AC
4509 end if;
4510 end if;
6c3c671e
AC
4511 end if;
4512
039538bc
AC
4513 -- A formal parameter of a specific tagged type whose related
4514 -- subprogram is subject to pragma Extensions_Visible with value
4515 -- "False" cannot act as an actual in a subprogram with value
b3407ce0 4516 -- "True" (SPARK RM 6.1.7(3)).
039538bc
AC
4517
4518 if Is_EVF_Expression (A)
4519 and then Extensions_Visible_Status (Nam) =
4520 Extensions_Visible_True
4521 then
4522 Error_Msg_N
4523 ("formal parameter with Extensions_Visible False cannot act "
4524 & "as actual parameter", A);
4525 Error_Msg_NE
4526 ("\subprogram & has Extensions_Visible True", A, Nam);
4527 end if;
4528
3c756b76
AC
4529 -- The actual parameter of a Ghost subprogram whose formal is of
4530 -- mode IN OUT or OUT must be a Ghost variable (SPARK RM 6.9(13)).
4531
95fef24f
AC
4532 if Comes_From_Source (Nam)
4533 and then Is_Ghost_Entity (Nam)
3c756b76
AC
4534 and then Ekind_In (F, E_In_Out_Parameter, E_Out_Parameter)
4535 and then Is_Entity_Name (A)
4536 and then Present (Entity (A))
4537 and then not Is_Ghost_Entity (Entity (A))
4538 then
4539 Error_Msg_NE
4540 ("non-ghost variable & cannot appear as actual in call to "
4541 & "ghost procedure", A, Entity (A));
4542
4543 if Ekind (F) = E_In_Out_Parameter then
4544 Error_Msg_N ("\corresponding formal has mode `IN OUT`", A);
4545 else
4546 Error_Msg_N ("\corresponding formal has mode OUT", A);
4547 end if;
4548 end if;
4549
996ae0b0
RK
4550 Next_Actual (A);
4551
fbf5a39b
AC
4552 -- Case where actual is not present
4553
996ae0b0
RK
4554 else
4555 Insert_Default;
4556 end if;
4557
4558 Next_Formal (F);
4d6a38a5
ES
4559
4560 if Present (Real_Subp) then
4561 Next_Formal (Real_F);
4562 end if;
996ae0b0 4563 end loop;
996ae0b0
RK
4564 end Resolve_Actuals;
4565
4566 -----------------------
4567 -- Resolve_Allocator --
4568 -----------------------
4569
4570 procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
949a18cc 4571 Desig_T : constant Entity_Id := Designated_Type (Typ);
ee2e3f6b 4572 E : constant Node_Id := Expression (N);
996ae0b0
RK
4573 Subtyp : Entity_Id;
4574 Discrim : Entity_Id;
4575 Constr : Node_Id;
b7d1f17f
HK
4576 Aggr : Node_Id;
4577 Assoc : Node_Id := Empty;
996ae0b0
RK
4578 Disc_Exp : Node_Id;
4579
b7d1f17f
HK
4580 procedure Check_Allocator_Discrim_Accessibility
4581 (Disc_Exp : Node_Id;
4582 Alloc_Typ : Entity_Id);
4583 -- Check that accessibility level associated with an access discriminant
4584 -- initialized in an allocator by the expression Disc_Exp is not deeper
4585 -- than the level of the allocator type Alloc_Typ. An error message is
4586 -- issued if this condition is violated. Specialized checks are done for
4587 -- the cases of a constraint expression which is an access attribute or
4588 -- an access discriminant.
4589
07fc65c4 4590 function In_Dispatching_Context return Boolean;
b7d1f17f
HK
4591 -- If the allocator is an actual in a call, it is allowed to be class-
4592 -- wide when the context is not because it is a controlling actual.
4593
b7d1f17f
HK
4594 -------------------------------------------
4595 -- Check_Allocator_Discrim_Accessibility --
4596 -------------------------------------------
4597
4598 procedure Check_Allocator_Discrim_Accessibility
4599 (Disc_Exp : Node_Id;
4600 Alloc_Typ : Entity_Id)
4601 is
4602 begin
4603 if Type_Access_Level (Etype (Disc_Exp)) >
f460d8f3 4604 Deepest_Type_Access_Level (Alloc_Typ)
b7d1f17f
HK
4605 then
4606 Error_Msg_N
4607 ("operand type has deeper level than allocator type", Disc_Exp);
4608
4609 -- When the expression is an Access attribute the level of the prefix
4610 -- object must not be deeper than that of the allocator's type.
4611
4612 elsif Nkind (Disc_Exp) = N_Attribute_Reference
83e5da69
AC
4613 and then Get_Attribute_Id (Attribute_Name (Disc_Exp)) =
4614 Attribute_Access
4615 and then Object_Access_Level (Prefix (Disc_Exp)) >
4616 Deepest_Type_Access_Level (Alloc_Typ)
b7d1f17f
HK
4617 then
4618 Error_Msg_N
4619 ("prefix of attribute has deeper level than allocator type",
4620 Disc_Exp);
4621
4622 -- When the expression is an access discriminant the check is against
4623 -- the level of the prefix object.
4624
4625 elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
4626 and then Nkind (Disc_Exp) = N_Selected_Component
83e5da69
AC
4627 and then Object_Access_Level (Prefix (Disc_Exp)) >
4628 Deepest_Type_Access_Level (Alloc_Typ)
b7d1f17f
HK
4629 then
4630 Error_Msg_N
4631 ("access discriminant has deeper level than allocator type",
4632 Disc_Exp);
4633
4634 -- All other cases are legal
4635
4636 else
4637 null;
4638 end if;
4639 end Check_Allocator_Discrim_Accessibility;
07fc65c4
GB
4640
4641 ----------------------------
4642 -- In_Dispatching_Context --
4643 ----------------------------
4644
4645 function In_Dispatching_Context return Boolean is
4646 Par : constant Node_Id := Parent (N);
b7d1f17f
HK
4647
4648 begin
d3b00ce3
AC
4649 return Nkind (Par) in N_Subprogram_Call
4650 and then Is_Entity_Name (Name (Par))
4651 and then Is_Dispatching_Operation (Entity (Name (Par)));
df3e68b1 4652 end In_Dispatching_Context;
b7d1f17f 4653
07fc65c4
GB
4654 -- Start of processing for Resolve_Allocator
4655
996ae0b0
RK
4656 begin
4657 -- Replace general access with specific type
4658
4659 if Ekind (Etype (N)) = E_Allocator_Type then
4660 Set_Etype (N, Base_Type (Typ));
4661 end if;
4662
0669bebe 4663 if Is_Abstract_Type (Typ) then
996ae0b0
RK
4664 Error_Msg_N ("type of allocator cannot be abstract", N);
4665 end if;
4666
2e86f679
RD
4667 -- For qualified expression, resolve the expression using the given
4668 -- subtype (nothing to do for type mark, subtype indication)
996ae0b0
RK
4669
4670 if Nkind (E) = N_Qualified_Expression then
4671 if Is_Class_Wide_Type (Etype (E))
949a18cc 4672 and then not Is_Class_Wide_Type (Desig_T)
07fc65c4 4673 and then not In_Dispatching_Context
996ae0b0
RK
4674 then
4675 Error_Msg_N
4676 ("class-wide allocator not allowed for this access type", N);
4677 end if;
4678
4679 Resolve (Expression (E), Etype (E));
f3691f46 4680 Check_Non_Static_Context (Expression (E));
996ae0b0
RK
4681 Check_Unset_Reference (Expression (E));
4682
2e86f679
RD
4683 -- A qualified expression requires an exact match of the type.
4684 -- Class-wide matching is not allowed.
fbf5a39b 4685
7b4db06c 4686 if (Is_Class_Wide_Type (Etype (Expression (E)))
19fb051c 4687 or else Is_Class_Wide_Type (Etype (E)))
fbf5a39b
AC
4688 and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
4689 then
4690 Wrong_Type (Expression (E), Etype (E));
4691 end if;
4692
a8551b5f
AC
4693 -- Calls to build-in-place functions are not currently supported in
4694 -- allocators for access types associated with a simple storage pool.
4695 -- Supporting such allocators may require passing additional implicit
4696 -- parameters to build-in-place functions (or a significant revision
4697 -- of the current b-i-p implementation to unify the handling for
4698 -- multiple kinds of storage pools). ???
4699
51245e2d 4700 if Is_Limited_View (Desig_T)
a8551b5f
AC
4701 and then Nkind (Expression (E)) = N_Function_Call
4702 then
4703 declare
260359e3
AC
4704 Pool : constant Entity_Id :=
4705 Associated_Storage_Pool (Root_Type (Typ));
a8551b5f
AC
4706 begin
4707 if Present (Pool)
f6205414
AC
4708 and then
4709 Present (Get_Rep_Pragma
4710 (Etype (Pool), Name_Simple_Storage_Pool_Type))
a8551b5f
AC
4711 then
4712 Error_Msg_N
1486a00e
AC
4713 ("limited function calls not yet supported in simple "
4714 & "storage pool allocators", Expression (E));
a8551b5f
AC
4715 end if;
4716 end;
4717 end if;
4718
b7d1f17f
HK
4719 -- A special accessibility check is needed for allocators that
4720 -- constrain access discriminants. The level of the type of the
4721 -- expression used to constrain an access discriminant cannot be
f3d57416 4722 -- deeper than the type of the allocator (in contrast to access
b7d1f17f
HK
4723 -- parameters, where the level of the actual can be arbitrary).
4724
2e86f679
RD
4725 -- We can't use Valid_Conversion to perform this check because in
4726 -- general the type of the allocator is unrelated to the type of
4727 -- the access discriminant.
b7d1f17f
HK
4728
4729 if Ekind (Typ) /= E_Anonymous_Access_Type
4730 or else Is_Local_Anonymous_Access (Typ)
4731 then
4732 Subtyp := Entity (Subtype_Mark (E));
4733
4734 Aggr := Original_Node (Expression (E));
4735
4736 if Has_Discriminants (Subtyp)
45fc7ddb 4737 and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
b7d1f17f
HK
4738 then
4739 Discrim := First_Discriminant (Base_Type (Subtyp));
4740
4741 -- Get the first component expression of the aggregate
4742
4743 if Present (Expressions (Aggr)) then
4744 Disc_Exp := First (Expressions (Aggr));
4745
4746 elsif Present (Component_Associations (Aggr)) then
4747 Assoc := First (Component_Associations (Aggr));
4748
4749 if Present (Assoc) then
4750 Disc_Exp := Expression (Assoc);
4751 else
4752 Disc_Exp := Empty;
4753 end if;
4754
4755 else
4756 Disc_Exp := Empty;
4757 end if;
4758
4759 while Present (Discrim) and then Present (Disc_Exp) loop
4760 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4761 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4762 end if;
4763
4764 Next_Discriminant (Discrim);
4765
4766 if Present (Discrim) then
4767 if Present (Assoc) then
4768 Next (Assoc);
4769 Disc_Exp := Expression (Assoc);
4770
4771 elsif Present (Next (Disc_Exp)) then
4772 Next (Disc_Exp);
4773
4774 else
4775 Assoc := First (Component_Associations (Aggr));
4776
4777 if Present (Assoc) then
4778 Disc_Exp := Expression (Assoc);
4779 else
4780 Disc_Exp := Empty;
4781 end if;
4782 end if;
4783 end if;
4784 end loop;
4785 end if;
4786 end if;
4787
996ae0b0
RK
4788 -- For a subtype mark or subtype indication, freeze the subtype
4789
4790 else
4791 Freeze_Expression (E);
4792
4793 if Is_Access_Constant (Typ) and then not No_Initialization (N) then
4794 Error_Msg_N
4795 ("initialization required for access-to-constant allocator", N);
4796 end if;
4797
4798 -- A special accessibility check is needed for allocators that
4799 -- constrain access discriminants. The level of the type of the
b7d1f17f 4800 -- expression used to constrain an access discriminant cannot be
f3d57416 4801 -- deeper than the type of the allocator (in contrast to access
996ae0b0
RK
4802 -- parameters, where the level of the actual can be arbitrary).
4803 -- We can't use Valid_Conversion to perform this check because
4804 -- in general the type of the allocator is unrelated to the type
b7d1f17f 4805 -- of the access discriminant.
996ae0b0
RK
4806
4807 if Nkind (Original_Node (E)) = N_Subtype_Indication
b7d1f17f
HK
4808 and then (Ekind (Typ) /= E_Anonymous_Access_Type
4809 or else Is_Local_Anonymous_Access (Typ))
996ae0b0
RK
4810 then
4811 Subtyp := Entity (Subtype_Mark (Original_Node (E)));
4812
4813 if Has_Discriminants (Subtyp) then
4814 Discrim := First_Discriminant (Base_Type (Subtyp));
4815 Constr := First (Constraints (Constraint (Original_Node (E))));
996ae0b0
RK
4816 while Present (Discrim) and then Present (Constr) loop
4817 if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4818 if Nkind (Constr) = N_Discriminant_Association then
4819 Disc_Exp := Original_Node (Expression (Constr));
4820 else
4821 Disc_Exp := Original_Node (Constr);
4822 end if;
4823
b7d1f17f 4824 Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
996ae0b0 4825 end if;
b7d1f17f 4826
996ae0b0
RK
4827 Next_Discriminant (Discrim);
4828 Next (Constr);
4829 end loop;
4830 end if;
4831 end if;
4832 end if;
4833
758c442c
GD
4834 -- Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4835 -- check that the level of the type of the created object is not deeper
4836 -- than the level of the allocator's access type, since extensions can
4837 -- now occur at deeper levels than their ancestor types. This is a
4838 -- static accessibility level check; a run-time check is also needed in
4839 -- the case of an initialized allocator with a class-wide argument (see
4840 -- Expand_Allocator_Expression).
4841
0791fbe9 4842 if Ada_Version >= Ada_2005
949a18cc 4843 and then Is_Class_Wide_Type (Desig_T)
758c442c
GD
4844 then
4845 declare
b7d1f17f 4846 Exp_Typ : Entity_Id;
758c442c
GD
4847
4848 begin
4849 if Nkind (E) = N_Qualified_Expression then
4850 Exp_Typ := Etype (E);
4851 elsif Nkind (E) = N_Subtype_Indication then
4852 Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4853 else
4854 Exp_Typ := Entity (E);
4855 end if;
4856
f460d8f3 4857 if Type_Access_Level (Exp_Typ) >
83e5da69
AC
4858 Deepest_Type_Access_Level (Typ)
4859 then
758c442c 4860 if In_Instance_Body then
43417b90 4861 Error_Msg_Warn := SPARK_Mode /= On;
1486a00e 4862 Error_Msg_N
4a28b181
AC
4863 ("type in allocator has deeper level than "
4864 & "designated class-wide type<<", E);
4865 Error_Msg_N ("\Program_Error [<<", E);
758c442c
GD
4866 Rewrite (N,
4867 Make_Raise_Program_Error (Sloc (N),
4868 Reason => PE_Accessibility_Check_Failed));
4869 Set_Etype (N, Typ);
aa180613
RD
4870
4871 -- Do not apply Ada 2005 accessibility checks on a class-wide
4872 -- allocator if the type given in the allocator is a formal
4873 -- type. A run-time check will be performed in the instance.
4874
4875 elsif not Is_Generic_Type (Exp_Typ) then
1486a00e
AC
4876 Error_Msg_N ("type in allocator has deeper level than "
4877 & "designated class-wide type", E);
758c442c
GD
4878 end if;
4879 end if;
4880 end;
4881 end if;
4882
996ae0b0
RK
4883 -- Check for allocation from an empty storage pool
4884
4885 if No_Pool_Assigned (Typ) then
8da337c5 4886 Error_Msg_N ("allocation from empty storage pool!", N);
1420b484 4887
5cc9353d
RD
4888 -- If the context is an unchecked conversion, as may happen within an
4889 -- inlined subprogram, the allocator is being resolved with its own
4890 -- anonymous type. In that case, if the target type has a specific
1420b484
JM
4891 -- storage pool, it must be inherited explicitly by the allocator type.
4892
4893 elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4894 and then No (Associated_Storage_Pool (Typ))
4895 then
4896 Set_Associated_Storage_Pool
4897 (Typ, Associated_Storage_Pool (Etype (Parent (N))));
996ae0b0 4898 end if;
b7d1f17f 4899
e57ab550
AC
4900 if Ekind (Etype (N)) = E_Anonymous_Access_Type then
4901 Check_Restriction (No_Anonymous_Allocators, N);
4902 end if;
4903
6aaa0587
ES
4904 -- Check that an allocator with task parts isn't for a nested access
4905 -- type when restriction No_Task_Hierarchy applies.
4906
4907 if not Is_Library_Level_Entity (Base_Type (Typ))
949a18cc 4908 and then Has_Task (Base_Type (Desig_T))
6aaa0587
ES
4909 then
4910 Check_Restriction (No_Task_Hierarchy, N);
4911 end if;
4912
77a40ec1 4913 -- An illegal allocator may be rewritten as a raise Program_Error
b7d1f17f
HK
4914 -- statement.
4915
4916 if Nkind (N) = N_Allocator then
4917
4918 -- An anonymous access discriminant is the definition of a
aa5147f0 4919 -- coextension.
b7d1f17f
HK
4920
4921 if Ekind (Typ) = E_Anonymous_Access_Type
4922 and then Nkind (Associated_Node_For_Itype (Typ)) =
4923 N_Discriminant_Specification
4924 then
949a18cc
AC
4925 declare
4926 Discr : constant Entity_Id :=
4927 Defining_Identifier (Associated_Node_For_Itype (Typ));
ee2e3f6b 4928
949a18cc 4929 begin
57f4c288
ES
4930 Check_Restriction (No_Coextensions, N);
4931
5d59eef2
AC
4932 -- Ada 2012 AI05-0052: If the designated type of the allocator
4933 -- is limited, then the allocator shall not be used to define
4934 -- the value of an access discriminant unless the discriminated
949a18cc
AC
4935 -- type is immutably limited.
4936
4937 if Ada_Version >= Ada_2012
4938 and then Is_Limited_Type (Desig_T)
51245e2d 4939 and then not Is_Limited_View (Scope (Discr))
949a18cc
AC
4940 then
4941 Error_Msg_N
5d59eef2
AC
4942 ("only immutably limited types can have anonymous "
4943 & "access discriminants designating a limited type", N);
949a18cc
AC
4944 end if;
4945 end;
4946
b7d1f17f 4947 -- Avoid marking an allocator as a dynamic coextension if it is
aa5147f0 4948 -- within a static construct.
b7d1f17f
HK
4949
4950 if not Is_Static_Coextension (N) then
aa5147f0 4951 Set_Is_Dynamic_Coextension (N);
b7d1f17f
HK
4952 end if;
4953
4954 -- Cleanup for potential static coextensions
4955
4956 else
aa5147f0
ES
4957 Set_Is_Dynamic_Coextension (N, False);
4958 Set_Is_Static_Coextension (N, False);
b7d1f17f 4959 end if;
b7d1f17f 4960 end if;
d9b056ea 4961
833eaa8a 4962 -- Report a simple error: if the designated object is a local task,
14848f57
AC
4963 -- its body has not been seen yet, and its activation will fail an
4964 -- elaboration check.
d9b056ea 4965
949a18cc
AC
4966 if Is_Task_Type (Desig_T)
4967 and then Scope (Base_Type (Desig_T)) = Current_Scope
d9b056ea
AC
4968 and then Is_Compilation_Unit (Current_Scope)
4969 and then Ekind (Current_Scope) = E_Package
4970 and then not In_Package_Body (Current_Scope)
4971 then
43417b90 4972 Error_Msg_Warn := SPARK_Mode /= On;
4a28b181
AC
4973 Error_Msg_N ("cannot activate task before body seen<<", N);
4974 Error_Msg_N ("\Program_Error [<<", N);
d9b056ea 4975 end if;
14848f57 4976
7b2aafc9
HK
4977 -- Ada 2012 (AI05-0111-3): Detect an attempt to allocate a task or a
4978 -- type with a task component on a subpool. This action must raise
4979 -- Program_Error at runtime.
14848f57
AC
4980
4981 if Ada_Version >= Ada_2012
dfbcb149 4982 and then Nkind (N) = N_Allocator
14848f57
AC
4983 and then Present (Subpool_Handle_Name (N))
4984 and then Has_Task (Desig_T)
4985 then
43417b90 4986 Error_Msg_Warn := SPARK_Mode /= On;
4a28b181
AC
4987 Error_Msg_N ("cannot allocate task on subpool<<", N);
4988 Error_Msg_N ("\Program_Error [<<", N);
7b2aafc9
HK
4989
4990 Rewrite (N,
4991 Make_Raise_Program_Error (Sloc (N),
4992 Reason => PE_Explicit_Raise));
4993 Set_Etype (N, Typ);
14848f57 4994 end if;
996ae0b0
RK
4995 end Resolve_Allocator;
4996
4997 ---------------------------
4998 -- Resolve_Arithmetic_Op --
4999 ---------------------------
5000
5001 -- Used for resolving all arithmetic operators except exponentiation
5002
5003 procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
fbf5a39b
AC
5004 L : constant Node_Id := Left_Opnd (N);
5005 R : constant Node_Id := Right_Opnd (N);
5006 TL : constant Entity_Id := Base_Type (Etype (L));
5007 TR : constant Entity_Id := Base_Type (Etype (R));
5008 T : Entity_Id;
5009 Rop : Node_Id;
996ae0b0
RK
5010
5011 B_Typ : constant Entity_Id := Base_Type (Typ);
5012 -- We do the resolution using the base type, because intermediate values
5013 -- in expressions always are of the base type, not a subtype of it.
5014
aa180613
RD
5015 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
5016 -- Returns True if N is in a context that expects "any real type"
5017
996ae0b0
RK
5018 function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
5019 -- Return True iff given type is Integer or universal real/integer
5020
5021 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
5022 -- Choose type of integer literal in fixed-point operation to conform
5023 -- to available fixed-point type. T is the type of the other operand,
5024 -- which is needed to determine the expected type of N.
5025
5026 procedure Set_Operand_Type (N : Node_Id);
5027 -- Set operand type to T if universal
5028
aa180613
RD
5029 -------------------------------
5030 -- Expected_Type_Is_Any_Real --
5031 -------------------------------
5032
5033 function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
5034 begin
5035 -- N is the expression after "delta" in a fixed_point_definition;
5036 -- see RM-3.5.9(6):
5037
45fc7ddb
HK
5038 return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
5039 N_Decimal_Fixed_Point_Definition,
aa180613
RD
5040
5041 -- N is one of the bounds in a real_range_specification;
5042 -- see RM-3.5.7(5):
5043
45fc7ddb 5044 N_Real_Range_Specification,
aa180613
RD
5045
5046 -- N is the expression of a delta_constraint;
5047 -- see RM-J.3(3):
5048
45fc7ddb 5049 N_Delta_Constraint);
aa180613
RD
5050 end Expected_Type_Is_Any_Real;
5051
996ae0b0
RK
5052 -----------------------------
5053 -- Is_Integer_Or_Universal --
5054 -----------------------------
5055
5056 function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
5057 T : Entity_Id;
5058 Index : Interp_Index;
5059 It : Interp;
5060
5061 begin
5062 if not Is_Overloaded (N) then
5063 T := Etype (N);
5064 return Base_Type (T) = Base_Type (Standard_Integer)
5065 or else T = Universal_Integer
5066 or else T = Universal_Real;
5067 else
5068 Get_First_Interp (N, Index, It);
996ae0b0 5069 while Present (It.Typ) loop
996ae0b0
RK
5070 if Base_Type (It.Typ) = Base_Type (Standard_Integer)
5071 or else It.Typ = Universal_Integer
5072 or else It.Typ = Universal_Real
5073 then
5074 return True;
5075 end if;
5076
5077 Get_Next_Interp (Index, It);
5078 end loop;
5079 end if;
5080
5081 return False;
5082 end Is_Integer_Or_Universal;
5083
5084 ----------------------------
5085 -- Set_Mixed_Mode_Operand --
5086 ----------------------------
5087
5088 procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
5089 Index : Interp_Index;
5090 It : Interp;
5091
5092 begin
5093 if Universal_Interpretation (N) = Universal_Integer then
5094
5095 -- A universal integer literal is resolved as standard integer
758c442c
GD
5096 -- except in the case of a fixed-point result, where we leave it
5097 -- as universal (to be handled by Exp_Fixd later on)
996ae0b0
RK
5098
5099 if Is_Fixed_Point_Type (T) then
5100 Resolve (N, Universal_Integer);
5101 else
5102 Resolve (N, Standard_Integer);
5103 end if;
5104
5105 elsif Universal_Interpretation (N) = Universal_Real
5106 and then (T = Base_Type (Standard_Integer)
5107 or else T = Universal_Integer
5108 or else T = Universal_Real)
5109 then
5110 -- A universal real can appear in a fixed-type context. We resolve
5111 -- the literal with that context, even though this might raise an
5112 -- exception prematurely (the other operand may be zero).
5113
5114 Resolve (N, B_Typ);
5115
5116 elsif Etype (N) = Base_Type (Standard_Integer)
5117 and then T = Universal_Real
5118 and then Is_Overloaded (N)
5119 then
5120 -- Integer arg in mixed-mode operation. Resolve with universal
5121 -- type, in case preference rule must be applied.
5122
5123 Resolve (N, Universal_Integer);
5124
5125 elsif Etype (N) = T
5126 and then B_Typ /= Universal_Fixed
5127 then
a77842bd 5128 -- Not a mixed-mode operation, resolve with context
996ae0b0
RK
5129
5130 Resolve (N, B_Typ);
5131
5132 elsif Etype (N) = Any_Fixed then
5133
a77842bd 5134 -- N may itself be a mixed-mode operation, so use context type
996ae0b0
RK
5135
5136 Resolve (N, B_Typ);
5137
5138 elsif Is_Fixed_Point_Type (T)
5139 and then B_Typ = Universal_Fixed
5140 and then Is_Overloaded (N)
5141 then
5142 -- Must be (fixed * fixed) operation, operand must have one
5143 -- compatible interpretation.
5144
5145 Resolve (N, Any_Fixed);
5146
5147 elsif Is_Fixed_Point_Type (B_Typ)
2e86f679 5148 and then (T = Universal_Real or else Is_Fixed_Point_Type (T))
996ae0b0
RK
5149 and then Is_Overloaded (N)
5150 then
5151 -- C * F(X) in a fixed context, where C is a real literal or a
5152 -- fixed-point expression. F must have either a fixed type
5153 -- interpretation or an integer interpretation, but not both.
5154
5155 Get_First_Interp (N, Index, It);
996ae0b0 5156 while Present (It.Typ) loop
996ae0b0 5157 if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
996ae0b0
RK
5158 if Analyzed (N) then
5159 Error_Msg_N ("ambiguous operand in fixed operation", N);
5160 else
5161 Resolve (N, Standard_Integer);
5162 end if;
5163
5164 elsif Is_Fixed_Point_Type (It.Typ) then
996ae0b0
RK
5165 if Analyzed (N) then
5166 Error_Msg_N ("ambiguous operand in fixed operation", N);
5167 else
5168 Resolve (N, It.Typ);
5169 end if;
5170 end if;
5171
5172 Get_Next_Interp (Index, It);
5173 end loop;
5174
758c442c
GD
5175 -- Reanalyze the literal with the fixed type of the context. If
5176 -- context is Universal_Fixed, we are within a conversion, leave
5177 -- the literal as a universal real because there is no usable
5178 -- fixed type, and the target of the conversion plays no role in
5179 -- the resolution.
996ae0b0 5180
0ab80019
AC
5181 declare
5182 Op2 : Node_Id;
5183 T2 : Entity_Id;
5184
5185 begin
5186 if N = L then
5187 Op2 := R;
5188 else
5189 Op2 := L;
5190 end if;
5191
5192 if B_Typ = Universal_Fixed
5193 and then Nkind (Op2) = N_Real_Literal
5194 then
5195 T2 := Universal_Real;
5196 else
5197 T2 := B_Typ;
5198 end if;
5199
5200 Set_Analyzed (Op2, False);
5201 Resolve (Op2, T2);
5202 end;
996ae0b0
RK
5203
5204 else
fbf5a39b 5205 Resolve (N);
996ae0b0
RK
5206 end if;
5207 end Set_Mixed_Mode_Operand;
5208
5209 ----------------------
5210 -- Set_Operand_Type --
5211 ----------------------
5212
5213 procedure Set_Operand_Type (N : Node_Id) is
5214 begin
5215 if Etype (N) = Universal_Integer
5216 or else Etype (N) = Universal_Real
5217 then
5218 Set_Etype (N, T);
5219 end if;
5220 end Set_Operand_Type;
5221
996ae0b0
RK
5222 -- Start of processing for Resolve_Arithmetic_Op
5223
5224 begin
5225 if Comes_From_Source (N)
5226 and then Ekind (Entity (N)) = E_Function
5227 and then Is_Imported (Entity (N))
fbf5a39b 5228 and then Is_Intrinsic_Subprogram (Entity (N))
996ae0b0
RK
5229 then
5230 Resolve_Intrinsic_Operator (N, Typ);
5231 return;
5232
5cc9353d
RD
5233 -- Special-case for mixed-mode universal expressions or fixed point type
5234 -- operation: each argument is resolved separately. The same treatment
5235 -- is required if one of the operands of a fixed point operation is
5236 -- universal real, since in this case we don't do a conversion to a
5237 -- specific fixed-point type (instead the expander handles the case).
996ae0b0 5238
ddf67a1d
AC
5239 -- Set the type of the node to its universal interpretation because
5240 -- legality checks on an exponentiation operand need the context.
5241
45fc7ddb 5242 elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
996ae0b0
RK
5243 and then Present (Universal_Interpretation (L))
5244 and then Present (Universal_Interpretation (R))
5245 then
ddf67a1d 5246 Set_Etype (N, B_Typ);
996ae0b0
RK
5247 Resolve (L, Universal_Interpretation (L));
5248 Resolve (R, Universal_Interpretation (R));
996ae0b0
RK
5249
5250 elsif (B_Typ = Universal_Real
45fc7ddb
HK
5251 or else Etype (N) = Universal_Fixed
5252 or else (Etype (N) = Any_Fixed
5253 and then Is_Fixed_Point_Type (B_Typ))
5254 or else (Is_Fixed_Point_Type (B_Typ)
5255 and then (Is_Integer_Or_Universal (L)
2e86f679 5256 or else
45fc7ddb
HK
5257 Is_Integer_Or_Universal (R))))
5258 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
996ae0b0
RK
5259 then
5260 if TL = Universal_Integer or else TR = Universal_Integer then
5261 Check_For_Visible_Operator (N, B_Typ);
5262 end if;
5263
5cc9353d
RD
5264 -- If context is a fixed type and one operand is integer, the other
5265 -- is resolved with the type of the context.
996ae0b0
RK
5266
5267 if Is_Fixed_Point_Type (B_Typ)
5268 and then (Base_Type (TL) = Base_Type (Standard_Integer)
5269 or else TL = Universal_Integer)
5270 then
5271 Resolve (R, B_Typ);
5272 Resolve (L, TL);
5273
5274 elsif Is_Fixed_Point_Type (B_Typ)
5275 and then (Base_Type (TR) = Base_Type (Standard_Integer)
5276 or else TR = Universal_Integer)
5277 then
5278 Resolve (L, B_Typ);
5279 Resolve (R, TR);
5280
5281 else
5282 Set_Mixed_Mode_Operand (L, TR);
5283 Set_Mixed_Mode_Operand (R, TL);
5284 end if;
5285
45fc7ddb
HK
5286 -- Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
5287 -- multiplying operators from being used when the expected type is
5288 -- also universal_fixed. Note that B_Typ will be Universal_Fixed in
5289 -- some cases where the expected type is actually Any_Real;
5290 -- Expected_Type_Is_Any_Real takes care of that case.
aa180613 5291
996ae0b0
RK
5292 if Etype (N) = Universal_Fixed
5293 or else Etype (N) = Any_Fixed
5294 then
5295 if B_Typ = Universal_Fixed
aa180613 5296 and then not Expected_Type_Is_Any_Real (N)
45fc7ddb
HK
5297 and then not Nkind_In (Parent (N), N_Type_Conversion,
5298 N_Unchecked_Type_Conversion)
996ae0b0 5299 then
45fc7ddb
HK
5300 Error_Msg_N ("type cannot be determined from context!", N);
5301 Error_Msg_N ("\explicit conversion to result type required", N);
996ae0b0
RK
5302
5303 Set_Etype (L, Any_Type);
5304 Set_Etype (R, Any_Type);
5305
5306 else
0ab80019 5307 if Ada_Version = Ada_83
45fc7ddb
HK
5308 and then Etype (N) = Universal_Fixed
5309 and then not
5310 Nkind_In (Parent (N), N_Type_Conversion,
5311 N_Unchecked_Type_Conversion)
996ae0b0
RK
5312 then
5313 Error_Msg_N
a921e83c
AC
5314 ("(Ada 83) fixed-point operation needs explicit "
5315 & "conversion", N);
996ae0b0
RK
5316 end if;
5317
aa180613 5318 -- The expected type is "any real type" in contexts like
5cc9353d 5319
aa180613 5320 -- type T is delta <universal_fixed-expression> ...
5cc9353d 5321
aa180613
RD
5322 -- in which case we need to set the type to Universal_Real
5323 -- so that static expression evaluation will work properly.
5324
5325 if Expected_Type_Is_Any_Real (N) then
5326 Set_Etype (N, Universal_Real);
5327 else
5328 Set_Etype (N, B_Typ);
5329 end if;
996ae0b0
RK
5330 end if;
5331
5332 elsif Is_Fixed_Point_Type (B_Typ)
5333 and then (Is_Integer_Or_Universal (L)
5334 or else Nkind (L) = N_Real_Literal
5335 or else Nkind (R) = N_Real_Literal
45fc7ddb 5336 or else Is_Integer_Or_Universal (R))
996ae0b0
RK
5337 then
5338 Set_Etype (N, B_Typ);
5339
5340 elsif Etype (N) = Any_Fixed then
5341
5cc9353d
RD
5342 -- If no previous errors, this is only possible if one operand is
5343 -- overloaded and the context is universal. Resolve as such.
996ae0b0
RK
5344
5345 Set_Etype (N, B_Typ);
5346 end if;
5347
5348 else
5349 if (TL = Universal_Integer or else TL = Universal_Real)
2e86f679 5350 and then
45fc7ddb 5351 (TR = Universal_Integer or else TR = Universal_Real)
996ae0b0
RK
5352 then
5353 Check_For_Visible_Operator (N, B_Typ);
5354 end if;
5355
5356 -- If the context is Universal_Fixed and the operands are also
5357 -- universal fixed, this is an error, unless there is only one
841dd0f5 5358 -- applicable fixed_point type (usually Duration).
996ae0b0 5359
45fc7ddb 5360 if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
996ae0b0
RK
5361 T := Unique_Fixed_Point_Type (N);
5362
5363 if T = Any_Type then
5364 Set_Etype (N, T);
5365 return;
5366 else
5367 Resolve (L, T);
5368 Resolve (R, T);
5369 end if;
5370
5371 else
5372 Resolve (L, B_Typ);
5373 Resolve (R, B_Typ);
5374 end if;
5375
5376 -- If one of the arguments was resolved to a non-universal type.
5377 -- label the result of the operation itself with the same type.
5378 -- Do the same for the universal argument, if any.
5379
5380 T := Intersect_Types (L, R);
5381 Set_Etype (N, Base_Type (T));
5382 Set_Operand_Type (L);
5383 Set_Operand_Type (R);
5384 end if;
5385
fbf5a39b 5386 Generate_Operator_Reference (N, Typ);
dec6faf1 5387 Analyze_Dimension (N);
996ae0b0
RK
5388 Eval_Arithmetic_Op (N);
5389
2ba431e5 5390 -- In SPARK, a multiplication or division with operands of fixed point
d18bbd25 5391 -- types must be qualified or explicitly converted to identify the
2ba431e5 5392 -- result type.
b0186f71 5393
fe5d3068
YM
5394 if (Is_Fixed_Point_Type (Etype (L))
5395 or else Is_Fixed_Point_Type (Etype (R)))
b0186f71
AC
5396 and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
5397 and then
5398 not Nkind_In (Parent (N), N_Qualified_Expression, N_Type_Conversion)
5399 then
ce5ba43a 5400 Check_SPARK_05_Restriction
fe5d3068 5401 ("operation should be qualified or explicitly converted", N);
b0186f71
AC
5402 end if;
5403
acad3c0a 5404 -- Set overflow and division checking bit
996ae0b0
RK
5405
5406 if Nkind (N) in N_Op then
5407 if not Overflow_Checks_Suppressed (Etype (N)) then
fbf5a39b 5408 Enable_Overflow_Check (N);
996ae0b0
RK
5409 end if;
5410
fbf5a39b
AC
5411 -- Give warning if explicit division by zero
5412
45fc7ddb 5413 if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
996ae0b0
RK
5414 and then not Division_Checks_Suppressed (Etype (N))
5415 then
fbf5a39b
AC
5416 Rop := Right_Opnd (N);
5417
5418 if Compile_Time_Known_Value (Rop)
5419 and then ((Is_Integer_Type (Etype (Rop))
780d052e
RD
5420 and then Expr_Value (Rop) = Uint_0)
5421 or else
5422 (Is_Real_Type (Etype (Rop))
5423 and then Expr_Value_R (Rop) = Ureal_0))
fbf5a39b 5424 then
ce72a9a3
AC
5425 -- Specialize the warning message according to the operation.
5426 -- The following warnings are for the case
aa180613
RD
5427
5428 case Nkind (N) is
5429 when N_Op_Divide =>
ce72a9a3
AC
5430
5431 -- For division, we have two cases, for float division
5432 -- of an unconstrained float type, on a machine where
5433 -- Machine_Overflows is false, we don't get an exception
5434 -- at run-time, but rather an infinity or Nan. The Nan
5435 -- case is pretty obscure, so just warn about infinities.
5436
5437 if Is_Floating_Point_Type (Typ)
5438 and then not Is_Constrained (Typ)
5439 and then not Machine_Overflows_On_Target
5440 then
5441 Error_Msg_N
1486a00e
AC
5442 ("float division by zero, may generate "
5443 & "'+'/'- infinity??", Right_Opnd (N));
ce72a9a3
AC
5444
5445 -- For all other cases, we get a Constraint_Error
5446
5447 else
5448 Apply_Compile_Time_Constraint_Error
324ac540 5449 (N, "division by zero??", CE_Divide_By_Zero,
ce72a9a3
AC
5450 Loc => Sloc (Right_Opnd (N)));
5451 end if;
aa180613
RD
5452
5453 when N_Op_Rem =>
5454 Apply_Compile_Time_Constraint_Error
324ac540 5455 (N, "rem with zero divisor??", CE_Divide_By_Zero,
aa180613
RD
5456 Loc => Sloc (Right_Opnd (N)));
5457
5458 when N_Op_Mod =>
5459 Apply_Compile_Time_Constraint_Error
324ac540 5460 (N, "mod with zero divisor??", CE_Divide_By_Zero,
aa180613
RD
5461 Loc => Sloc (Right_Opnd (N)));
5462
5463 -- Division by zero can only happen with division, rem,
5464 -- and mod operations.
5465
5466 when others =>
5467 raise Program_Error;
5468 end case;
fbf5a39b
AC
5469
5470 -- Otherwise just set the flag to check at run time
5471
5472 else
b7d1f17f 5473 Activate_Division_Check (N);
fbf5a39b 5474 end if;
996ae0b0 5475 end if;
45fc7ddb
HK
5476
5477 -- If Restriction No_Implicit_Conditionals is active, then it is
5478 -- violated if either operand can be negative for mod, or for rem
5479 -- if both operands can be negative.
5480
7a963087 5481 if Restriction_Check_Required (No_Implicit_Conditionals)
45fc7ddb
HK
5482 and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
5483 then
5484 declare
5485 Lo : Uint;
5486 Hi : Uint;
5487 OK : Boolean;
5488
5489 LNeg : Boolean;
5490 RNeg : Boolean;
5491 -- Set if corresponding operand might be negative
5492
5493 begin
5d5e9775
AC
5494 Determine_Range
5495 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
45fc7ddb
HK
5496 LNeg := (not OK) or else Lo < 0;
5497
5d5e9775
AC
5498 Determine_Range
5499 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
45fc7ddb
HK
5500 RNeg := (not OK) or else Lo < 0;
5501
5d5e9775
AC
5502 -- Check if we will be generating conditionals. There are two
5503 -- cases where that can happen, first for REM, the only case
5504 -- is largest negative integer mod -1, where the division can
5505 -- overflow, but we still have to give the right result. The
5506 -- front end generates a test for this annoying case. Here we
5507 -- just test if both operands can be negative (that's what the
5508 -- expander does, so we match its logic here).
5509
5510 -- The second case is mod where either operand can be negative.
308e6f3a 5511 -- In this case, the back end has to generate additional tests.
5d5e9775 5512
45fc7ddb 5513 if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
2e86f679 5514 or else
45fc7ddb
HK
5515 (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
5516 then
5517 Check_Restriction (No_Implicit_Conditionals, N);
5518 end if;
5519 end;
5520 end if;
996ae0b0
RK
5521 end if;
5522
5523 Check_Unset_Reference (L);
5524 Check_Unset_Reference (R);
996ae0b0
RK
5525 end Resolve_Arithmetic_Op;
5526
5527 ------------------
5528 -- Resolve_Call --
5529 ------------------
5530
5531 procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
ee81cbe9
AC
5532 function Same_Or_Aliased_Subprograms
5533 (S : Entity_Id;
5534 E : Entity_Id) return Boolean;
5535 -- Returns True if the subprogram entity S is the same as E or else
5536 -- S is an alias of E.
5537
001c7783
AC
5538 ---------------------------------
5539 -- Same_Or_Aliased_Subprograms --
5540 ---------------------------------
5541
ee81cbe9
AC
5542 function Same_Or_Aliased_Subprograms
5543 (S : Entity_Id;
5544 E : Entity_Id) return Boolean
5545 is
5546 Subp_Alias : constant Entity_Id := Alias (S);
ee81cbe9 5547 begin
b2834fbd 5548 return S = E or else (Present (Subp_Alias) and then Subp_Alias = E);
ee81cbe9
AC
5549 end Same_Or_Aliased_Subprograms;
5550
bf0b0e5e
AC
5551 -- Local variables
5552
5553 Loc : constant Source_Ptr := Sloc (N);
5554 Subp : constant Node_Id := Name (N);
5555 Body_Id : Entity_Id;
5556 I : Interp_Index;
5557 It : Interp;
5558 Nam : Entity_Id;
5559 Nam_Decl : Node_Id;
5560 Nam_UA : Entity_Id;
5561 Norm_OK : Boolean;
5562 Rtype : Entity_Id;
5563 Scop : Entity_Id;
5564
ee81cbe9
AC
5565 -- Start of processing for Resolve_Call
5566
996ae0b0 5567 begin
758c442c
GD
5568 -- The context imposes a unique interpretation with type Typ on a
5569 -- procedure or function call. Find the entity of the subprogram that
5570 -- yields the expected type, and propagate the corresponding formal
5571 -- constraints on the actuals. The caller has established that an
5572 -- interpretation exists, and emitted an error if not unique.
996ae0b0
RK
5573
5574 -- First deal with the case of a call to an access-to-subprogram,
5575 -- dereference made explicit in Analyze_Call.
5576
5577 if Ekind (Etype (Subp)) = E_Subprogram_Type then
996ae0b0
RK
5578 if not Is_Overloaded (Subp) then
5579 Nam := Etype (Subp);
5580
5581 else
758c442c
GD
5582 -- Find the interpretation whose type (a subprogram type) has a
5583 -- return type that is compatible with the context. Analysis of
5584 -- the node has established that one exists.
996ae0b0 5585
996ae0b0
RK
5586 Nam := Empty;
5587
1420b484 5588 Get_First_Interp (Subp, I, It);
996ae0b0 5589 while Present (It.Typ) loop
996ae0b0
RK
5590 if Covers (Typ, Etype (It.Typ)) then
5591 Nam := It.Typ;
5592 exit;
5593 end if;
5594
5595 Get_Next_Interp (I, It);
5596 end loop;
5597
5598 if No (Nam) then
5599 raise Program_Error;
5600 end if;
5601 end if;
5602
5603 -- If the prefix is not an entity, then resolve it
5604
5605 if not Is_Entity_Name (Subp) then
5606 Resolve (Subp, Nam);
5607 end if;
5608
758c442c
GD
5609 -- For an indirect call, we always invalidate checks, since we do not
5610 -- know whether the subprogram is local or global. Yes we could do
5611 -- better here, e.g. by knowing that there are no local subprograms,
aa180613 5612 -- but it does not seem worth the effort. Similarly, we kill all
758c442c 5613 -- knowledge of current constant values.
fbf5a39b
AC
5614
5615 Kill_Current_Values;
5616
b7d1f17f
HK
5617 -- If this is a procedure call which is really an entry call, do
5618 -- the conversion of the procedure call to an entry call. Protected
5619 -- operations use the same circuitry because the name in the call
5620 -- can be an arbitrary expression with special resolution rules.
996ae0b0 5621
45fc7ddb 5622 elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
996ae0b0
RK
5623 or else (Is_Entity_Name (Subp)
5624 and then Ekind (Entity (Subp)) = E_Entry)
5625 then
5626 Resolve_Entry_Call (N, Typ);
5627 Check_Elab_Call (N);
fbf5a39b
AC
5628
5629 -- Kill checks and constant values, as above for indirect case
5630 -- Who knows what happens when another task is activated?
5631
5632 Kill_Current_Values;
996ae0b0
RK
5633 return;
5634
5635 -- Normal subprogram call with name established in Resolve
5636
5637 elsif not (Is_Type (Entity (Subp))) then
5638 Nam := Entity (Subp);
e7ba564f 5639 Set_Entity_With_Checks (Subp, Nam);
fb12497d 5640
996ae0b0
RK
5641 -- Otherwise we must have the case of an overloaded call
5642
5643 else
5644 pragma Assert (Is_Overloaded (Subp));
d81b4bfe
TQ
5645
5646 -- Initialize Nam to prevent warning (we know it will be assigned
5647 -- in the loop below, but the compiler does not know that).
5648
5649 Nam := Empty;
996ae0b0
RK
5650
5651 Get_First_Interp (Subp, I, It);
996ae0b0
RK
5652 while Present (It.Typ) loop
5653 if Covers (Typ, It.Typ) then
5654 Nam := It.Nam;
e7ba564f 5655 Set_Entity_With_Checks (Subp, Nam);
996ae0b0
RK
5656 exit;
5657 end if;
5658
5659 Get_Next_Interp (I, It);
5660 end loop;
5661 end if;
5662
c9b99571 5663 if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
c5cec2fe
AC
5664 and then not Is_Access_Subprogram_Type (Base_Type (Typ))
5665 and then Nkind (Subp) /= N_Explicit_Dereference
5666 and then Present (Parameter_Associations (N))
53cf4600 5667 then
66aa7643
TQ
5668 -- The prefix is a parameterless function call that returns an access
5669 -- to subprogram. If parameters are present in the current call, add
5670 -- add an explicit dereference. We use the base type here because
5671 -- within an instance these may be subtypes.
53cf4600
ES
5672
5673 -- The dereference is added either in Analyze_Call or here. Should
5674 -- be consolidated ???
5675
5676 Set_Is_Overloaded (Subp, False);
5677 Set_Etype (Subp, Etype (Nam));
5678 Insert_Explicit_Dereference (Subp);
5679 Nam := Designated_Type (Etype (Nam));
5680 Resolve (Subp, Nam);
5681 end if;
5682
996ae0b0
RK
5683 -- Check that a call to Current_Task does not occur in an entry body
5684
5685 if Is_RTE (Nam, RE_Current_Task) then
5686 declare
5687 P : Node_Id;
5688
5689 begin
5690 P := N;
5691 loop
5692 P := Parent (P);
45fc7ddb
HK
5693
5694 -- Exclude calls that occur within the default of a formal
5695 -- parameter of the entry, since those are evaluated outside
5696 -- of the body.
5697
5698 exit when No (P) or else Nkind (P) = N_Parameter_Specification;
996ae0b0 5699
aa180613
RD
5700 if Nkind (P) = N_Entry_Body
5701 or else (Nkind (P) = N_Subprogram_Body
45fc7ddb 5702 and then Is_Entry_Barrier_Function (P))
aa180613
RD
5703 then
5704 Rtype := Etype (N);
43417b90 5705 Error_Msg_Warn := SPARK_Mode /= On;
996ae0b0 5706 Error_Msg_NE
4a28b181 5707 ("& should not be used in entry body (RM C.7(17))<<",
996ae0b0 5708 N, Nam);
4a28b181 5709 Error_Msg_NE ("\Program_Error [<<", N, Nam);
aa180613
RD
5710 Rewrite (N,
5711 Make_Raise_Program_Error (Loc,
5712 Reason => PE_Current_Task_In_Entry_Body));
5713 Set_Etype (N, Rtype);
e65f50ec 5714 return;
996ae0b0
RK
5715 end if;
5716 end loop;
5717 end;
5718 end if;
5719
758c442c
GD
5720 -- Check that a procedure call does not occur in the context of the
5721 -- entry call statement of a conditional or timed entry call. Note that
5722 -- the case of a call to a subprogram renaming of an entry will also be
5723 -- rejected. The test for N not being an N_Entry_Call_Statement is
5724 -- defensive, covering the possibility that the processing of entry
5725 -- calls might reach this point due to later modifications of the code
5726 -- above.
996ae0b0
RK
5727
5728 if Nkind (Parent (N)) = N_Entry_Call_Alternative
5729 and then Nkind (N) /= N_Entry_Call_Statement
5730 and then Entry_Call_Statement (Parent (N)) = N
5731 then
0791fbe9 5732 if Ada_Version < Ada_2005 then
1420b484
JM
5733 Error_Msg_N ("entry call required in select statement", N);
5734
5735 -- Ada 2005 (AI-345): If a procedure_call_statement is used
66aa7643
TQ
5736 -- for a procedure_or_entry_call, the procedure_name or
5737 -- procedure_prefix of the procedure_call_statement shall denote
1420b484
JM
5738 -- an entry renamed by a procedure, or (a view of) a primitive
5739 -- subprogram of a limited interface whose first parameter is
5740 -- a controlling parameter.
5741
5742 elsif Nkind (N) = N_Procedure_Call_Statement
5743 and then not Is_Renamed_Entry (Nam)
5744 and then not Is_Controlling_Limited_Procedure (Nam)
5745 then
5746 Error_Msg_N
c8ef728f 5747 ("entry call or dispatching primitive of interface required", N);
1420b484 5748 end if;
996ae0b0
RK
5749 end if;
5750
3b8056a5
AC
5751 -- If the SPARK_05 restriction is active, we are not allowed
5752 -- to have a call to a subprogram before we see its completion.
5753
5754 if not Has_Completion (Nam)
5755 and then Restriction_Check_Required (SPARK_05)
5756
5757 -- Don't flag strange internal calls
5758
5759 and then Comes_From_Source (N)
5760 and then Comes_From_Source (Nam)
5761
5762 -- Only flag calls in extended main source
5763
5764 and then In_Extended_Main_Source_Unit (Nam)
5765 and then In_Extended_Main_Source_Unit (N)
5766
5767 -- Exclude enumeration literals from this processing
5768
5769 and then Ekind (Nam) /= E_Enumeration_Literal
5770 then
ce5ba43a 5771 Check_SPARK_05_Restriction
3b8056a5
AC
5772 ("call to subprogram cannot appear before its body", N);
5773 end if;
5774
66aa7643
TQ
5775 -- Check that this is not a call to a protected procedure or entry from
5776 -- within a protected function.
fbf5a39b 5777
c92e8586 5778 Check_Internal_Protected_Use (N, Nam);
fbf5a39b 5779
2fabf41e
AC
5780 -- Freeze the subprogram name if not in a spec-expression. Note that
5781 -- we freeze procedure calls as well as function calls. Procedure calls
5782 -- are not frozen according to the rules (RM 13.14(14)) because it is
5783 -- impossible to have a procedure call to a non-frozen procedure in
5784 -- pure Ada, but in the code that we generate in the expander, this
5785 -- rule needs extending because we can generate procedure calls that
5786 -- need freezing.
996ae0b0 5787
a429e6b3
AC
5788 -- In Ada 2012, expression functions may be called within pre/post
5789 -- conditions of subsequent functions or expression functions. Such
dd4e47ab
AC
5790 -- calls do not freeze when they appear within generated bodies,
5791 -- (including the body of another expression function) which would
2fabf41e 5792 -- place the freeze node in the wrong scope. An expression function
dd4e47ab
AC
5793 -- is frozen in the usual fashion, by the appearance of a real body,
5794 -- or at the end of a declarative part.
a429e6b3
AC
5795
5796 if Is_Entity_Name (Subp) and then not In_Spec_Expression
dd4e47ab 5797 and then not Is_Expression_Function (Current_Scope)
a429e6b3
AC
5798 and then
5799 (not Is_Expression_Function (Entity (Subp))
5800 or else Scope (Entity (Subp)) = Current_Scope)
5801 then
996ae0b0
RK
5802 Freeze_Expression (Subp);
5803 end if;
5804
758c442c
GD
5805 -- For a predefined operator, the type of the result is the type imposed
5806 -- by context, except for a predefined operation on universal fixed.
5807 -- Otherwise The type of the call is the type returned by the subprogram
5808 -- being called.
996ae0b0
RK
5809
5810 if Is_Predefined_Op (Nam) then
996ae0b0
RK
5811 if Etype (N) /= Universal_Fixed then
5812 Set_Etype (N, Typ);
5813 end if;
5814
758c442c
GD
5815 -- If the subprogram returns an array type, and the context requires the
5816 -- component type of that array type, the node is really an indexing of
5817 -- the parameterless call. Resolve as such. A pathological case occurs
5818 -- when the type of the component is an access to the array type. In
5819 -- this case the call is truly ambiguous.
996ae0b0 5820
0669bebe 5821 elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
996ae0b0
RK
5822 and then
5823 ((Is_Array_Type (Etype (Nam))
19fb051c 5824 and then Covers (Typ, Component_Type (Etype (Nam))))
84f80f5b
AC
5825 or else
5826 (Is_Access_Type (Etype (Nam))
5827 and then Is_Array_Type (Designated_Type (Etype (Nam)))
5828 and then
5829 Covers (Typ, Component_Type (Designated_Type (Etype (Nam))))))
996ae0b0
RK
5830 then
5831 declare
5832 Index_Node : Node_Id;
fbf5a39b
AC
5833 New_Subp : Node_Id;
5834 Ret_Type : constant Entity_Id := Etype (Nam);
996ae0b0
RK
5835
5836 begin
fbf5a39b
AC
5837 if Is_Access_Type (Ret_Type)
5838 and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
5839 then
5840 Error_Msg_N
5841 ("cannot disambiguate function call and indexing", N);
5842 else
5843 New_Subp := Relocate_Node (Subp);
4bb9c7b9
AC
5844
5845 -- The called entity may be an explicit dereference, in which
5846 -- case there is no entity to set.
5847
5848 if Nkind (New_Subp) /= N_Explicit_Dereference then
5849 Set_Entity (Subp, Nam);
5850 end if;
fbf5a39b 5851
7205254b 5852 if (Is_Array_Type (Ret_Type)
5d5e9775 5853 and then Component_Type (Ret_Type) /= Any_Type)
7205254b
JM
5854 or else
5855 (Is_Access_Type (Ret_Type)
5d5e9775
AC
5856 and then
5857 Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
7205254b 5858 then
0669bebe
GB
5859 if Needs_No_Actuals (Nam) then
5860
5861 -- Indexed call to a parameterless function
5862
5863 Index_Node :=
5864 Make_Indexed_Component (Loc,
fc999c5d
RD
5865 Prefix =>
5866 Make_Function_Call (Loc, Name => New_Subp),
0669bebe
GB
5867 Expressions => Parameter_Associations (N));
5868 else
5869 -- An Ada 2005 prefixed call to a primitive operation
5870 -- whose first parameter is the prefix. This prefix was
5871 -- prepended to the parameter list, which is actually a
3b42c566 5872 -- list of indexes. Remove the prefix in order to build
0669bebe
GB
5873 -- the proper indexed component.
5874
5875 Index_Node :=
5876 Make_Indexed_Component (Loc,
fc999c5d 5877 Prefix =>
0669bebe 5878 Make_Function_Call (Loc,
fc999c5d 5879 Name => New_Subp,
0669bebe
GB
5880 Parameter_Associations =>
5881 New_List
5882 (Remove_Head (Parameter_Associations (N)))),
5883 Expressions => Parameter_Associations (N));
5884 end if;
fbf5a39b 5885
74e7891f
RD
5886 -- Preserve the parenthesis count of the node
5887
5888 Set_Paren_Count (Index_Node, Paren_Count (N));
5889
fbf5a39b
AC
5890 -- Since we are correcting a node classification error made
5891 -- by the parser, we call Replace rather than Rewrite.
5892
5893 Replace (N, Index_Node);
74e7891f 5894
fbf5a39b
AC
5895 Set_Etype (Prefix (N), Ret_Type);
5896 Set_Etype (N, Typ);
5897 Resolve_Indexed_Component (N, Typ);
5898 Check_Elab_Call (Prefix (N));
5899 end if;
996ae0b0
RK
5900 end if;
5901
5902 return;
5903 end;
5904
5905 else
5906 Set_Etype (N, Etype (Nam));
5907 end if;
5908
5909 -- In the case where the call is to an overloaded subprogram, Analyze
5910 -- calls Normalize_Actuals once per overloaded subprogram. Therefore in
5911 -- such a case Normalize_Actuals needs to be called once more to order
5912 -- the actuals correctly. Otherwise the call will have the ordering
5913 -- given by the last overloaded subprogram whether this is the correct
5914 -- one being called or not.
5915
5916 if Is_Overloaded (Subp) then
5917 Normalize_Actuals (N, Nam, False, Norm_OK);
5918 pragma Assert (Norm_OK);
5919 end if;
5920
5921 -- In any case, call is fully resolved now. Reset Overload flag, to
5922 -- prevent subsequent overload resolution if node is analyzed again
5923
5924 Set_Is_Overloaded (Subp, False);
5925 Set_Is_Overloaded (N, False);
5926
c5cec2fe
AC
5927 -- A Ghost entity must appear in a specific context
5928
5929 if Is_Ghost_Entity (Nam) and then Comes_From_Source (N) then
5930 Check_Ghost_Context (Nam, N);
5931 end if;
5932
758c442c
GD
5933 -- If we are calling the current subprogram from immediately within its
5934 -- body, then that is the case where we can sometimes detect cases of
5935 -- infinite recursion statically. Do not try this in case restriction
b7d1f17f 5936 -- No_Recursion is in effect anyway, and do it only for source calls.
996ae0b0 5937
b7d1f17f
HK
5938 if Comes_From_Source (N) then
5939 Scop := Current_Scope;
996ae0b0 5940
b2834fbd
AC
5941 -- Check violation of SPARK_05 restriction which does not permit
5942 -- a subprogram body to contain a call to the subprogram directly.
5943
5944 if Restriction_Check_Required (SPARK_05)
5945 and then Same_Or_Aliased_Subprograms (Nam, Scop)
5946 then
ce5ba43a 5947 Check_SPARK_05_Restriction
b2834fbd
AC
5948 ("subprogram may not contain direct call to itself", N);
5949 end if;
5950
26570b21
RD
5951 -- Issue warning for possible infinite recursion in the absence
5952 -- of the No_Recursion restriction.
5953
ee81cbe9 5954 if Same_Or_Aliased_Subprograms (Nam, Scop)
b7d1f17f
HK
5955 and then not Restriction_Active (No_Recursion)
5956 and then Check_Infinite_Recursion (N)
5957 then
5958 -- Here we detected and flagged an infinite recursion, so we do
da20aa43
RD
5959 -- not need to test the case below for further warnings. Also we
5960 -- are all done if we now have a raise SE node.
996ae0b0 5961
26570b21
RD
5962 if Nkind (N) = N_Raise_Storage_Error then
5963 return;
5964 end if;
996ae0b0 5965
26570b21
RD
5966 -- If call is to immediately containing subprogram, then check for
5967 -- the case of a possible run-time detectable infinite recursion.
996ae0b0 5968
b7d1f17f
HK
5969 else
5970 Scope_Loop : while Scop /= Standard_Standard loop
ee81cbe9 5971 if Same_Or_Aliased_Subprograms (Nam, Scop) then
b7d1f17f
HK
5972
5973 -- Although in general case, recursion is not statically
5974 -- checkable, the case of calling an immediately containing
5975 -- subprogram is easy to catch.
5976
5977 Check_Restriction (No_Recursion, N);
5978
5979 -- If the recursive call is to a parameterless subprogram,
5980 -- then even if we can't statically detect infinite
5981 -- recursion, this is pretty suspicious, and we output a
5982 -- warning. Furthermore, we will try later to detect some
5983 -- cases here at run time by expanding checking code (see
5984 -- Detect_Infinite_Recursion in package Exp_Ch6).
5985
5986 -- If the recursive call is within a handler, do not emit a
5987 -- warning, because this is a common idiom: loop until input
5988 -- is correct, catch illegal input in handler and restart.
5989
5990 if No (First_Formal (Nam))
5991 and then Etype (Nam) = Standard_Void_Type
5992 and then not Error_Posted (N)
5993 and then Nkind (Parent (N)) /= N_Exception_Handler
aa180613 5994 then
b7d1f17f
HK
5995 -- For the case of a procedure call. We give the message
5996 -- only if the call is the first statement in a sequence
5997 -- of statements, or if all previous statements are
5998 -- simple assignments. This is simply a heuristic to
5999 -- decrease false positives, without losing too many good
6000 -- warnings. The idea is that these previous statements
6001 -- may affect global variables the procedure depends on.
78efd712
AC
6002 -- We also exclude raise statements, that may arise from
6003 -- constraint checks and are probably unrelated to the
6004 -- intended control flow.
b7d1f17f
HK
6005
6006 if Nkind (N) = N_Procedure_Call_Statement
6007 and then Is_List_Member (N)
6008 then
6009 declare
6010 P : Node_Id;
6011 begin
6012 P := Prev (N);
6013 while Present (P) loop
fc999c5d
RD
6014 if not Nkind_In (P, N_Assignment_Statement,
6015 N_Raise_Constraint_Error)
78efd712 6016 then
b7d1f17f
HK
6017 exit Scope_Loop;
6018 end if;
6019
6020 Prev (P);
6021 end loop;
6022 end;
6023 end if;
6024
6025 -- Do not give warning if we are in a conditional context
6026
aa180613 6027 declare
b7d1f17f 6028 K : constant Node_Kind := Nkind (Parent (N));
aa180613 6029 begin
b7d1f17f 6030 if (K = N_Loop_Statement
b5c739f9 6031 and then Present (Iteration_Scheme (Parent (N))))
b7d1f17f
HK
6032 or else K = N_If_Statement
6033 or else K = N_Elsif_Part
6034 or else K = N_Case_Statement_Alternative
6035 then
6036 exit Scope_Loop;
6037 end if;
aa180613 6038 end;
aa180613 6039
b7d1f17f 6040 -- Here warning is to be issued
aa180613 6041
b7d1f17f 6042 Set_Has_Recursive_Call (Nam);
43417b90 6043 Error_Msg_Warn := SPARK_Mode /= On;
4a28b181
AC
6044 Error_Msg_N ("possible infinite recursion<<!", N);
6045 Error_Msg_N ("\Storage_Error ]<<!", N);
b7d1f17f 6046 end if;
aa180613 6047
b7d1f17f 6048 exit Scope_Loop;
996ae0b0
RK
6049 end if;
6050
b7d1f17f
HK
6051 Scop := Scope (Scop);
6052 end loop Scope_Loop;
6053 end if;
996ae0b0
RK
6054 end if;
6055
b5c739f9
RD
6056 -- Check obsolescent reference to Ada.Characters.Handling subprogram
6057
6058 Check_Obsolescent_2005_Entity (Nam, Subp);
6059
996ae0b0
RK
6060 -- If subprogram name is a predefined operator, it was given in
6061 -- functional notation. Replace call node with operator node, so
6062 -- that actuals can be resolved appropriately.
6063
6064 if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
6065 Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
6066 return;
6067
6068 elsif Present (Alias (Nam))
6069 and then Is_Predefined_Op (Alias (Nam))
6070 then
6071 Resolve_Actuals (N, Nam);
6072 Make_Call_Into_Operator (N, Typ, Alias (Nam));
6073 return;
6074 end if;
6075
fbf5a39b
AC
6076 -- Create a transient scope if the resulting type requires it
6077
4017021b
AC
6078 -- There are several notable exceptions:
6079
4d2907fd 6080 -- a) In init procs, the transient scope overhead is not needed, and is
4017021b
AC
6081 -- even incorrect when the call is a nested initialization call for a
6082 -- component whose expansion may generate adjust calls. However, if the
6083 -- call is some other procedure call within an initialization procedure
6084 -- (for example a call to Create_Task in the init_proc of the task
6085 -- run-time record) a transient scope must be created around this call.
6086
4d2907fd 6087 -- b) Enumeration literal pseudo-calls need no transient scope
4017021b 6088
4d2907fd 6089 -- c) Intrinsic subprograms (Unchecked_Conversion and source info
4017021b 6090 -- functions) do not use the secondary stack even though the return
4d2907fd 6091 -- type may be unconstrained.
4017021b 6092
4d2907fd 6093 -- d) Calls to a build-in-place function, since such functions may
4017021b
AC
6094 -- allocate their result directly in a target object, and cases where
6095 -- the result does get allocated in the secondary stack are checked for
6096 -- within the specialized Exp_Ch6 procedures for expanding those
6097 -- build-in-place calls.
6098
6099 -- e) If the subprogram is marked Inline_Always, then even if it returns
c8ef728f 6100 -- an unconstrained type the call does not require use of the secondary
45fc7ddb
HK
6101 -- stack. However, inlining will only take place if the body to inline
6102 -- is already present. It may not be available if e.g. the subprogram is
6103 -- declared in a child instance.
c8ef728f 6104
4017021b
AC
6105 -- If this is an initialization call for a type whose construction
6106 -- uses the secondary stack, and it is not a nested call to initialize
6107 -- a component, we do need to create a transient scope for it. We
6108 -- check for this by traversing the type in Check_Initialization_Call.
6109
c8ef728f 6110 if Is_Inlined (Nam)
84f4072a
JM
6111 and then Has_Pragma_Inline (Nam)
6112 and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
6113 and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
c8ef728f
ES
6114 then
6115 null;
6116
4017021b
AC
6117 elsif Ekind (Nam) = E_Enumeration_Literal
6118 or else Is_Build_In_Place_Function (Nam)
6119 or else Is_Intrinsic_Subprogram (Nam)
6120 then
6121 null;
6122
4460a9bc 6123 elsif Expander_Active
996ae0b0
RK
6124 and then Is_Type (Etype (Nam))
6125 and then Requires_Transient_Scope (Etype (Nam))
4017021b
AC
6126 and then
6127 (not Within_Init_Proc
6128 or else
6129 (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
996ae0b0 6130 then
0669bebe 6131 Establish_Transient_Scope (N, Sec_Stack => True);
996ae0b0 6132
a9f4e3d2
AC
6133 -- If the call appears within the bounds of a loop, it will
6134 -- be rewritten and reanalyzed, nothing left to do here.
6135
6136 if Nkind (N) /= N_Function_Call then
6137 return;
6138 end if;
6139
fbf5a39b 6140 elsif Is_Init_Proc (Nam)
996ae0b0
RK
6141 and then not Within_Init_Proc
6142 then
6143 Check_Initialization_Call (N, Nam);
6144 end if;
6145
6146 -- A protected function cannot be called within the definition of the
88f7d2d1
AC
6147 -- enclosing protected type, unless it is part of a pre/postcondition
6148 -- on another protected operation.
996ae0b0
RK
6149
6150 if Is_Protected_Type (Scope (Nam))
6151 and then In_Open_Scopes (Scope (Nam))
6152 and then not Has_Completion (Scope (Nam))
88f7d2d1 6153 and then not In_Spec_Expression
996ae0b0
RK
6154 then
6155 Error_Msg_NE
6156 ("& cannot be called before end of protected definition", N, Nam);
6157 end if;
6158
6159 -- Propagate interpretation to actuals, and add default expressions
6160 -- where needed.
6161
6162 if Present (First_Formal (Nam)) then
6163 Resolve_Actuals (N, Nam);
6164
d81b4bfe
TQ
6165 -- Overloaded literals are rewritten as function calls, for purpose of
6166 -- resolution. After resolution, we can replace the call with the
6167 -- literal itself.
996ae0b0
RK
6168
6169 elsif Ekind (Nam) = E_Enumeration_Literal then
6170 Copy_Node (Subp, N);
6171 Resolve_Entity_Name (N, Typ);
6172
fbf5a39b 6173 -- Avoid validation, since it is a static function call
996ae0b0 6174
e65f50ec 6175 Generate_Reference (Nam, Subp);
996ae0b0
RK
6176 return;
6177 end if;
6178
b7d1f17f
HK
6179 -- If the subprogram is not global, then kill all saved values and
6180 -- checks. This is a bit conservative, since in many cases we could do
6181 -- better, but it is not worth the effort. Similarly, we kill constant
6182 -- values. However we do not need to do this for internal entities
6183 -- (unless they are inherited user-defined subprograms), since they
6184 -- are not in the business of molesting local values.
6185
6186 -- If the flag Suppress_Value_Tracking_On_Calls is set, then we also
6187 -- kill all checks and values for calls to global subprograms. This
6188 -- takes care of the case where an access to a local subprogram is
6189 -- taken, and could be passed directly or indirectly and then called
6190 -- from almost any context.
aa180613
RD
6191
6192 -- Note: we do not do this step till after resolving the actuals. That
6193 -- way we still take advantage of the current value information while
6194 -- scanning the actuals.
6195
45fc7ddb
HK
6196 -- We suppress killing values if we are processing the nodes associated
6197 -- with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
6198 -- type kills all the values as part of analyzing the code that
6199 -- initializes the dispatch tables.
6200
6201 if Inside_Freezing_Actions = 0
6202 and then (not Is_Library_Level_Entity (Nam)
24357840
RD
6203 or else Suppress_Value_Tracking_On_Call
6204 (Nearest_Dynamic_Scope (Current_Scope)))
aa180613
RD
6205 and then (Comes_From_Source (Nam)
6206 or else (Present (Alias (Nam))
6207 and then Comes_From_Source (Alias (Nam))))
6208 then
6209 Kill_Current_Values;
6210 end if;
6211
36fcf362
RD
6212 -- If we are warning about unread OUT parameters, this is the place to
6213 -- set Last_Assignment for OUT and IN OUT parameters. We have to do this
6214 -- after the above call to Kill_Current_Values (since that call clears
6215 -- the Last_Assignment field of all local variables).
67ce0d7e 6216
36fcf362 6217 if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
67ce0d7e
RD
6218 and then Comes_From_Source (N)
6219 and then In_Extended_Main_Source_Unit (N)
6220 then
6221 declare
6222 F : Entity_Id;
6223 A : Node_Id;
6224
6225 begin
6226 F := First_Formal (Nam);
6227 A := First_Actual (N);
6228 while Present (F) and then Present (A) loop
964f13da 6229 if Ekind_In (F, E_Out_Parameter, E_In_Out_Parameter)
36fcf362 6230 and then Warn_On_Modified_As_Out_Parameter (F)
67ce0d7e
RD
6231 and then Is_Entity_Name (A)
6232 and then Present (Entity (A))
36fcf362 6233 and then Comes_From_Source (N)
67ce0d7e
RD
6234 and then Safe_To_Capture_Value (N, Entity (A))
6235 then
6236 Set_Last_Assignment (Entity (A), A);
6237 end if;
6238
6239 Next_Formal (F);
6240 Next_Actual (A);
6241 end loop;
6242 end;
6243 end if;
6244
996ae0b0
RK
6245 -- If the subprogram is a primitive operation, check whether or not
6246 -- it is a correct dispatching call.
6247
6248 if Is_Overloadable (Nam)
6249 and then Is_Dispatching_Operation (Nam)
6250 then
6251 Check_Dispatching_Call (N);
6252
0669bebe
GB
6253 elsif Ekind (Nam) /= E_Subprogram_Type
6254 and then Is_Abstract_Subprogram (Nam)
996ae0b0
RK
6255 and then not In_Instance
6256 then
6257 Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
6258 end if;
6259
e65f50ec
ES
6260 -- If this is a dispatching call, generate the appropriate reference,
6261 -- for better source navigation in GPS.
6262
6263 if Is_Overloadable (Nam)
6264 and then Present (Controlling_Argument (N))
6265 then
6266 Generate_Reference (Nam, Subp, 'R');
c5d91669 6267
5cc9353d 6268 -- Normal case, not a dispatching call: generate a call reference
c5d91669 6269
e65f50ec 6270 else
9c870c90 6271 Generate_Reference (Nam, Subp, 's');
e65f50ec
ES
6272 end if;
6273
996ae0b0
RK
6274 if Is_Intrinsic_Subprogram (Nam) then
6275 Check_Intrinsic_Call (N);
6276 end if;
6277
5b2217f8 6278 -- Check for violation of restriction No_Specific_Termination_Handlers
dce86910 6279 -- and warn on a potentially blocking call to Abort_Task.
5b2217f8 6280
273adcdf
AC
6281 if Restriction_Check_Required (No_Specific_Termination_Handlers)
6282 and then (Is_RTE (Nam, RE_Set_Specific_Handler)
6283 or else
6284 Is_RTE (Nam, RE_Specific_Handler))
5b2217f8
RD
6285 then
6286 Check_Restriction (No_Specific_Termination_Handlers, N);
dce86910
AC
6287
6288 elsif Is_RTE (Nam, RE_Abort_Task) then
6289 Check_Potentially_Blocking_Operation (N);
5b2217f8
RD
6290 end if;
6291
806f6d37
AC
6292 -- A call to Ada.Real_Time.Timing_Events.Set_Handler to set a relative
6293 -- timing event violates restriction No_Relative_Delay (AI-0211). We
6294 -- need to check the second argument to determine whether it is an
6295 -- absolute or relative timing event.
afbcdf5e 6296
273adcdf
AC
6297 if Restriction_Check_Required (No_Relative_Delay)
6298 and then Is_RTE (Nam, RE_Set_Handler)
806f6d37
AC
6299 and then Is_RTE (Etype (Next_Actual (First_Actual (N))), RE_Time_Span)
6300 then
afbcdf5e
AC
6301 Check_Restriction (No_Relative_Delay, N);
6302 end if;
6303
21791d97
AC
6304 -- Issue an error for a call to an eliminated subprogram. This routine
6305 -- will not perform the check if the call appears within a default
6306 -- expression.
16212e89 6307
df378148 6308 Check_For_Eliminated_Subprogram (Subp, Nam);
16212e89 6309
12f0c50c
AC
6310 -- In formal mode, the primitive operations of a tagged type or type
6311 -- extension do not include functions that return the tagged type.
6312
f6820c2d
AC
6313 if Nkind (N) = N_Function_Call
6314 and then Is_Tagged_Type (Etype (N))
6315 and then Is_Entity_Name (Name (N))
1a83142e 6316 and then Is_Inherited_Operation_For_Type (Entity (Name (N)), Etype (N))
f6820c2d 6317 then
ce5ba43a 6318 Check_SPARK_05_Restriction ("function not inherited", N);
f6820c2d 6319 end if;
12f0c50c 6320
e8374e7a
AC
6321 -- Implement rule in 12.5.1 (23.3/2): In an instance, if the actual is
6322 -- class-wide and the call dispatches on result in a context that does
6323 -- not provide a tag, the call raises Program_Error.
1f6439e3
AC
6324
6325 if Nkind (N) = N_Function_Call
6326 and then In_Instance
6327 and then Is_Generic_Actual_Type (Typ)
6328 and then Is_Class_Wide_Type (Typ)
6329 and then Has_Controlling_Result (Nam)
6330 and then Nkind (Parent (N)) = N_Object_Declaration
6331 then
e8374e7a 6332 -- Verify that none of the formals are controlling
1f6439e3
AC
6333
6334 declare
e8374e7a 6335 Call_OK : Boolean := False;
1f6439e3
AC
6336 F : Entity_Id;
6337
6338 begin
6339 F := First_Formal (Nam);
6340 while Present (F) loop
6341 if Is_Controlling_Formal (F) then
6342 Call_OK := True;
6343 exit;
6344 end if;
e8374e7a 6345
1f6439e3
AC
6346 Next_Formal (F);
6347 end loop;
6348
6349 if not Call_OK then
43417b90 6350 Error_Msg_Warn := SPARK_Mode /= On;
4a28b181
AC
6351 Error_Msg_N ("!cannot determine tag of result<<", N);
6352 Error_Msg_N ("\Program_Error [<<!", N);
1f6439e3
AC
6353 Insert_Action (N,
6354 Make_Raise_Program_Error (Sloc (N),
6355 Reason => PE_Explicit_Raise));
6356 end if;
6357 end;
6358 end if;
6359
fc999c5d
RD
6360 -- Check for calling a function with OUT or IN OUT parameter when the
6361 -- calling context (us right now) is not Ada 2012, so does not allow
ef2c20e7
AC
6362 -- OUT or IN OUT parameters in function calls. Functions declared in
6363 -- a predefined unit are OK, as they may be called indirectly from a
6364 -- user-declared instantiation.
fc999c5d
RD
6365
6366 if Ada_Version < Ada_2012
6367 and then Ekind (Nam) = E_Function
6368 and then Has_Out_Or_In_Out_Parameter (Nam)
ef2c20e7 6369 and then not In_Predefined_Unit (Nam)
fc999c5d
RD
6370 then
6371 Error_Msg_NE ("& has at least one OUT or `IN OUT` parameter", N, Nam);
6372 Error_Msg_N ("\call to this function only allowed in Ada 2012", N);
6373 end if;
6374
0929eaeb
AC
6375 -- Check the dimensions of the actuals in the call. For function calls,
6376 -- propagate the dimensions from the returned type to N.
6377
6378 Analyze_Dimension_Call (N, Nam);
dec6faf1 6379
67ce0d7e
RD
6380 -- All done, evaluate call and deal with elaboration issues
6381
c01a9391 6382 Eval_Call (N);
996ae0b0 6383 Check_Elab_Call (N);
ecad37f3 6384
10671e7a
AC
6385 -- In GNATprove mode, expansion is disabled, but we want to inline some
6386 -- subprograms to facilitate formal verification. Indirect calls through
6387 -- a subprogram type or within a generic cannot be inlined. Inlining is
6388 -- performed only for calls subject to SPARK_Mode on.
ecad37f3
ES
6389
6390 if GNATprove_Mode
2d180af1 6391 and then SPARK_Mode = On
10671e7a
AC
6392 and then Is_Overloadable (Nam)
6393 and then not Inside_A_Generic
ecad37f3 6394 then
bf0b0e5e
AC
6395 Nam_UA := Ultimate_Alias (Nam);
6396 Nam_Decl := Unit_Declaration_Node (Nam_UA);
e5c4e2bc 6397
bf0b0e5e
AC
6398 if Nkind (Nam_Decl) = N_Subprogram_Declaration then
6399 Body_Id := Corresponding_Body (Nam_Decl);
eb1ee757 6400
bf0b0e5e
AC
6401 -- Nothing to do if the subprogram is not eligible for inlining in
6402 -- GNATprove mode.
2178830b 6403
bf0b0e5e 6404 if not Is_Inlined_Always (Nam_UA)
39521a94 6405 or else not Can_Be_Inlined_In_GNATprove_Mode (Nam_UA, Body_Id)
4bd4bb7f 6406 then
2178830b
AC
6407 null;
6408
6409 -- Calls cannot be inlined inside assertions, as GNATprove treats
6410 -- assertions as logic expressions.
6411
6412 elsif In_Assertion_Expr /= 0 then
e96b7045 6413 Error_Msg_NE ("info: no contextual analysis of &?", N, Nam);
2178830b 6414 Error_Msg_N ("\call appears in assertion expression", N);
eb1ee757 6415 Set_Is_Inlined_Always (Nam_UA, False);
4bd4bb7f 6416
3dd7e28d
YM
6417 -- Calls cannot be inlined inside default expressions
6418
6419 elsif In_Default_Expr then
e96b7045 6420 Error_Msg_NE ("info: no contextual analysis of &?", N, Nam);
3dd7e28d
YM
6421 Error_Msg_N ("\call appears in default expression", N);
6422 Set_Is_Inlined_Always (Nam_UA, False);
6423
2178830b
AC
6424 -- Inlining should not be performed during pre-analysis
6425
6426 elsif Full_Analysis then
6427
6428 -- With the one-pass inlining technique, a call cannot be
6429 -- inlined if the corresponding body has not been seen yet.
6430
39521a94 6431 if No (Body_Id) then
2178830b 6432 Error_Msg_NE
e96b7045 6433 ("info: no contextual analysis of & (body not seen yet)?",
1eb31e60 6434 N, Nam);
eb1ee757 6435 Set_Is_Inlined_Always (Nam_UA, False);
2178830b
AC
6436
6437 -- Nothing to do if there is no body to inline, indicating that
6438 -- the subprogram is not suitable for inlining in GNATprove
6439 -- mode.
6440
bf0b0e5e 6441 elsif No (Body_To_Inline (Nam_Decl)) then
2178830b
AC
6442 null;
6443
6444 -- Calls cannot be inlined inside potentially unevaluated
6445 -- expressions, as this would create complex actions inside
6446 -- expressions, that are not handled by GNATprove.
6447
6448 elsif Is_Potentially_Unevaluated (N) then
e96b7045 6449 Error_Msg_NE ("info: no contextual analysis of &?", N, Nam);
52c1498c
YM
6450 Error_Msg_N
6451 ("\call appears in potentially unevaluated context", N);
eb1ee757 6452 Set_Is_Inlined_Always (Nam_UA, False);
2178830b
AC
6453
6454 -- Otherwise, inline the call
6455
52c1498c 6456 else
eb1ee757 6457 Expand_Inlined_Call (N, Nam_UA, Nam);
52c1498c 6458 end if;
e5c4e2bc 6459 end if;
bf0b0e5e 6460 end if;
ecad37f3
ES
6461 end if;
6462
76b84bf0 6463 Warn_On_Overlapping_Actuals (Nam, N);
996ae0b0
RK
6464 end Resolve_Call;
6465
19d846a0
RD
6466 -----------------------------
6467 -- Resolve_Case_Expression --
6468 -----------------------------
6469
6470 procedure Resolve_Case_Expression (N : Node_Id; Typ : Entity_Id) is
b6dd03dd
ES
6471 Alt : Node_Id;
6472 Is_Dyn : Boolean;
19d846a0
RD
6473
6474 begin
6475 Alt := First (Alternatives (N));
6476 while Present (Alt) loop
6477 Resolve (Expression (Alt), Typ);
6478 Next (Alt);
6479 end loop;
6480
b6dd03dd
ES
6481 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
6482 -- dynamically tagged must be known statically.
6483
6484 if Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
6485 Alt := First (Alternatives (N));
6486 Is_Dyn := Is_Dynamically_Tagged (Expression (Alt));
6487
6488 while Present (Alt) loop
6489 if Is_Dynamically_Tagged (Expression (Alt)) /= Is_Dyn then
6490 Error_Msg_N ("all or none of the dependent expressions "
6491 & "can be dynamically tagged", N);
6492 end if;
6493
6494 Next (Alt);
6495 end loop;
6496 end if;
6497
19d846a0
RD
6498 Set_Etype (N, Typ);
6499 Eval_Case_Expression (N);
6500 end Resolve_Case_Expression;
6501
996ae0b0
RK
6502 -------------------------------
6503 -- Resolve_Character_Literal --
6504 -------------------------------
6505
6506 procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
6507 B_Typ : constant Entity_Id := Base_Type (Typ);
6508 C : Entity_Id;
6509
6510 begin
6511 -- Verify that the character does belong to the type of the context
6512
6513 Set_Etype (N, B_Typ);
6514 Eval_Character_Literal (N);
6515
82c80734
RD
6516 -- Wide_Wide_Character literals must always be defined, since the set
6517 -- of wide wide character literals is complete, i.e. if a character
6518 -- literal is accepted by the parser, then it is OK for wide wide
6519 -- character (out of range character literals are rejected).
996ae0b0 6520
82c80734 6521 if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
996ae0b0
RK
6522 return;
6523
6524 -- Always accept character literal for type Any_Character, which
6525 -- occurs in error situations and in comparisons of literals, both
6526 -- of which should accept all literals.
6527
6528 elsif B_Typ = Any_Character then
6529 return;
6530
5cc9353d
RD
6531 -- For Standard.Character or a type derived from it, check that the
6532 -- literal is in range.
996ae0b0
RK
6533
6534 elsif Root_Type (B_Typ) = Standard_Character then
82c80734
RD
6535 if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
6536 return;
6537 end if;
6538
5cc9353d
RD
6539 -- For Standard.Wide_Character or a type derived from it, check that the
6540 -- literal is in range.
82c80734
RD
6541
6542 elsif Root_Type (B_Typ) = Standard_Wide_Character then
6543 if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
996ae0b0
RK
6544 return;
6545 end if;
6546
82c80734 6547 -- For Standard.Wide_Wide_Character or a type derived from it, we
159a5104 6548 -- know the literal is in range, since the parser checked.
82c80734
RD
6549
6550 elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
6551 return;
6552
d81b4bfe
TQ
6553 -- If the entity is already set, this has already been resolved in a
6554 -- generic context, or comes from expansion. Nothing else to do.
996ae0b0
RK
6555
6556 elsif Present (Entity (N)) then
6557 return;
6558
d81b4bfe
TQ
6559 -- Otherwise we have a user defined character type, and we can use the
6560 -- standard visibility mechanisms to locate the referenced entity.
996ae0b0
RK
6561
6562 else
6563 C := Current_Entity (N);
996ae0b0
RK
6564 while Present (C) loop
6565 if Etype (C) = B_Typ then
e7ba564f 6566 Set_Entity_With_Checks (N, C);
996ae0b0
RK
6567 Generate_Reference (C, N);
6568 return;
6569 end if;
6570
6571 C := Homonym (C);
6572 end loop;
6573 end if;
6574
6575 -- If we fall through, then the literal does not match any of the
5cc9353d
RD
6576 -- entries of the enumeration type. This isn't just a constraint error
6577 -- situation, it is an illegality (see RM 4.2).
996ae0b0
RK
6578
6579 Error_Msg_NE
6580 ("character not defined for }", N, First_Subtype (B_Typ));
996ae0b0
RK
6581 end Resolve_Character_Literal;
6582
6583 ---------------------------
6584 -- Resolve_Comparison_Op --
6585 ---------------------------
6586
6587 -- Context requires a boolean type, and plays no role in resolution.
5cc9353d
RD
6588 -- Processing identical to that for equality operators. The result type is
6589 -- the base type, which matters when pathological subtypes of booleans with
6590 -- limited ranges are used.
996ae0b0
RK
6591
6592 procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
6593 L : constant Node_Id := Left_Opnd (N);
6594 R : constant Node_Id := Right_Opnd (N);
6595 T : Entity_Id;
6596
6597 begin
d81b4bfe
TQ
6598 -- If this is an intrinsic operation which is not predefined, use the
6599 -- types of its declared arguments to resolve the possibly overloaded
6600 -- operands. Otherwise the operands are unambiguous and specify the
6601 -- expected type.
996ae0b0
RK
6602
6603 if Scope (Entity (N)) /= Standard_Standard then
6604 T := Etype (First_Entity (Entity (N)));
1420b484 6605
996ae0b0
RK
6606 else
6607 T := Find_Unique_Type (L, R);
6608
6609 if T = Any_Fixed then
6610 T := Unique_Fixed_Point_Type (L);
6611 end if;
6612 end if;
6613
fbf5a39b 6614 Set_Etype (N, Base_Type (Typ));
996ae0b0
RK
6615 Generate_Reference (T, N, ' ');
6616
bd29d519 6617 -- Skip remaining processing if already set to Any_Type
996ae0b0 6618
bd29d519
AC
6619 if T = Any_Type then
6620 return;
6621 end if;
6622
6623 -- Deal with other error cases
996ae0b0 6624
bd29d519
AC
6625 if T = Any_String or else
6626 T = Any_Composite or else
6627 T = Any_Character
6628 then
6629 if T = Any_Character then
6630 Ambiguous_Character (L);
996ae0b0 6631 else
bd29d519 6632 Error_Msg_N ("ambiguous operands for comparison", N);
996ae0b0 6633 end if;
bd29d519
AC
6634
6635 Set_Etype (N, Any_Type);
6636 return;
996ae0b0 6637 end if;
bd29d519
AC
6638
6639 -- Resolve the operands if types OK
6640
6641 Resolve (L, T);
6642 Resolve (R, T);
6643 Check_Unset_Reference (L);
6644 Check_Unset_Reference (R);
6645 Generate_Operator_Reference (N, T);
6646 Check_Low_Bound_Tested (N);
6647
2ba431e5
YM
6648 -- In SPARK, ordering operators <, <=, >, >= are not defined for Boolean
6649 -- types or array types except String.
b0186f71 6650
fe5d3068 6651 if Is_Boolean_Type (T) then
ce5ba43a 6652 Check_SPARK_05_Restriction
fe5d3068 6653 ("comparison is not defined on Boolean type", N);
975c6896 6654
ad05f2e9
AC
6655 elsif Is_Array_Type (T)
6656 and then Base_Type (T) /= Standard_String
6657 then
ce5ba43a 6658 Check_SPARK_05_Restriction
ad05f2e9 6659 ("comparison is not defined on array types other than String", N);
b0186f71
AC
6660 end if;
6661
bd29d519
AC
6662 -- Check comparison on unordered enumeration
6663
f6636994 6664 if Bad_Unordered_Enumeration_Reference (N, Etype (L)) then
b1d12996
AC
6665 Error_Msg_Sloc := Sloc (Etype (L));
6666 Error_Msg_NE
6667 ("comparison on unordered enumeration type& declared#?U?",
6668 N, Etype (L));
bd29d519
AC
6669 end if;
6670
5cc9353d
RD
6671 -- Evaluate the relation (note we do this after the above check since
6672 -- this Eval call may change N to True/False.
bd29d519 6673
dec6faf1 6674 Analyze_Dimension (N);
bd29d519 6675 Eval_Relational_Op (N);
996ae0b0
RK
6676 end Resolve_Comparison_Op;
6677
996ae0b0
RK
6678 -----------------------------------------
6679 -- Resolve_Discrete_Subtype_Indication --
6680 -----------------------------------------
6681
6682 procedure Resolve_Discrete_Subtype_Indication
6683 (N : Node_Id;
6684 Typ : Entity_Id)
6685 is
6686 R : Node_Id;
6687 S : Entity_Id;
6688
6689 begin
6690 Analyze (Subtype_Mark (N));
6691 S := Entity (Subtype_Mark (N));
6692
6693 if Nkind (Constraint (N)) /= N_Range_Constraint then
6694 Error_Msg_N ("expect range constraint for discrete type", N);
6695 Set_Etype (N, Any_Type);
6696
6697 else
6698 R := Range_Expression (Constraint (N));
5c736541
RD
6699
6700 if R = Error then
6701 return;
6702 end if;
6703
996ae0b0
RK
6704 Analyze (R);
6705
6706 if Base_Type (S) /= Base_Type (Typ) then
6707 Error_Msg_NE
6708 ("expect subtype of }", N, First_Subtype (Typ));
6709
6710 -- Rewrite the constraint as a range of Typ
6711 -- to allow compilation to proceed further.
6712
6713 Set_Etype (N, Typ);
6714 Rewrite (Low_Bound (R),
6715 Make_Attribute_Reference (Sloc (Low_Bound (R)),
5cc9353d 6716 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
996ae0b0
RK
6717 Attribute_Name => Name_First));
6718 Rewrite (High_Bound (R),
6719 Make_Attribute_Reference (Sloc (High_Bound (R)),
5cc9353d 6720 Prefix => New_Occurrence_Of (Typ, Sloc (R)),
996ae0b0
RK
6721 Attribute_Name => Name_First));
6722
6723 else
6724 Resolve (R, Typ);
6725 Set_Etype (N, Etype (R));
6726
6727 -- Additionally, we must check that the bounds are compatible
6728 -- with the given subtype, which might be different from the
6729 -- type of the context.
6730
6731 Apply_Range_Check (R, S);
6732
6733 -- ??? If the above check statically detects a Constraint_Error
6734 -- it replaces the offending bound(s) of the range R with a
6735 -- Constraint_Error node. When the itype which uses these bounds
6736 -- is frozen the resulting call to Duplicate_Subexpr generates
6737 -- a new temporary for the bounds.
6738
6739 -- Unfortunately there are other itypes that are also made depend
6740 -- on these bounds, so when Duplicate_Subexpr is called they get
6741 -- a forward reference to the newly created temporaries and Gigi
6742 -- aborts on such forward references. This is probably sign of a
6743 -- more fundamental problem somewhere else in either the order of
6744 -- itype freezing or the way certain itypes are constructed.
6745
6746 -- To get around this problem we call Remove_Side_Effects right
6747 -- away if either bounds of R are a Constraint_Error.
6748
6749 declare
fbf5a39b
AC
6750 L : constant Node_Id := Low_Bound (R);
6751 H : constant Node_Id := High_Bound (R);
996ae0b0
RK
6752
6753 begin
6754 if Nkind (L) = N_Raise_Constraint_Error then
6755 Remove_Side_Effects (L);
6756 end if;
6757
6758 if Nkind (H) = N_Raise_Constraint_Error then
6759 Remove_Side_Effects (H);
6760 end if;
6761 end;
6762
6763 Check_Unset_Reference (Low_Bound (R));
6764 Check_Unset_Reference (High_Bound (R));
6765 end if;
6766 end if;
6767 end Resolve_Discrete_Subtype_Indication;
6768
6769 -------------------------
6770 -- Resolve_Entity_Name --
6771 -------------------------
6772
6773 -- Used to resolve identifiers and expanded names
6774
6775 procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
a921e83c
AC
6776 function Is_Assignment_Or_Object_Expression
6777 (Context : Node_Id;
6778 Expr : Node_Id) return Boolean;
6779 -- Determine whether node Context denotes an assignment statement or an
6780 -- object declaration whose expression is node Expr.
6781
3f80a182
AC
6782 function Is_OK_Volatile_Context
6783 (Context : Node_Id;
6784 Obj_Ref : Node_Id) return Boolean;
6785 -- Determine whether node Context denotes a "non-interfering context"
847d950d 6786 -- (as defined in SPARK RM 7.1.3(12)) where volatile reference Obj_Ref
3f80a182
AC
6787 -- can safely reside.
6788
a921e83c
AC
6789 ----------------------------------------
6790 -- Is_Assignment_Or_Object_Expression --
6791 ----------------------------------------
6792
6793 function Is_Assignment_Or_Object_Expression
6794 (Context : Node_Id;
6795 Expr : Node_Id) return Boolean
6796 is
6797 begin
6798 if Nkind_In (Context, N_Assignment_Statement,
6799 N_Object_Declaration)
6800 and then Expression (Context) = Expr
6801 then
6802 return True;
6803
6804 -- Check whether a construct that yields a name is the expression of
6805 -- an assignment statement or an object declaration.
6806
6807 elsif (Nkind_In (Context, N_Attribute_Reference,
6808 N_Explicit_Dereference,
6809 N_Indexed_Component,
6810 N_Selected_Component,
6811 N_Slice)
6812 and then Prefix (Context) = Expr)
6813 or else
6814 (Nkind_In (Context, N_Type_Conversion,
6815 N_Unchecked_Type_Conversion)
6816 and then Expression (Context) = Expr)
6817 then
6818 return
6819 Is_Assignment_Or_Object_Expression
6820 (Context => Parent (Context),
6821 Expr => Context);
6822
6823 -- Otherwise the context is not an assignment statement or an object
6824 -- declaration.
6825
6826 else
6827 return False;
6828 end if;
6829 end Is_Assignment_Or_Object_Expression;
6830
5073ad7a
AC
6831 ----------------------------
6832 -- Is_OK_Volatile_Context --
6833 ----------------------------
f9966234 6834
5073ad7a
AC
6835 function Is_OK_Volatile_Context
6836 (Context : Node_Id;
6837 Obj_Ref : Node_Id) return Boolean
6838 is
fc90cc62
AC
6839 function Is_Protected_Operation_Call (Nod : Node_Id) return Boolean;
6840 -- Determine whether an arbitrary node denotes a call to a protected
6841 -- entry, function or procedure in prefixed form where the prefix is
6842 -- Obj_Ref.
6843
5073ad7a
AC
6844 function Within_Check (Nod : Node_Id) return Boolean;
6845 -- Determine whether an arbitrary node appears in a check node
f9966234 6846
ed962eda 6847 function Within_Subprogram_Call (Nod : Node_Id) return Boolean;
5073ad7a 6848 -- Determine whether an arbitrary node appears in a procedure call
f9966234 6849
847d950d
HK
6850 function Within_Volatile_Function (Id : Entity_Id) return Boolean;
6851 -- Determine whether an arbitrary entity appears in a volatile
6852 -- function.
6853
fc90cc62
AC
6854 ---------------------------------
6855 -- Is_Protected_Operation_Call --
6856 ---------------------------------
6857
6858 function Is_Protected_Operation_Call (Nod : Node_Id) return Boolean is
6859 Pref : Node_Id;
6860 Subp : Node_Id;
6861
6862 begin
6863 -- A call to a protected operations retains its selected component
6864 -- form as opposed to other prefixed calls that are transformed in
6865 -- expanded names.
6866
6867 if Nkind (Nod) = N_Selected_Component then
6868 Pref := Prefix (Nod);
6869 Subp := Selector_Name (Nod);
6870
6871 return
6872 Pref = Obj_Ref
6873 and then Is_Protected_Type (Etype (Pref))
6874 and then Is_Entity_Name (Subp)
6875 and then Ekind_In (Entity (Subp), E_Entry,
6876 E_Entry_Family,
6877 E_Function,
6878 E_Procedure);
6879 else
6880 return False;
6881 end if;
6882 end Is_Protected_Operation_Call;
6883
5073ad7a
AC
6884 ------------------
6885 -- Within_Check --
6886 ------------------
f9966234 6887
5073ad7a
AC
6888 function Within_Check (Nod : Node_Id) return Boolean is
6889 Par : Node_Id;
f9966234 6890
5073ad7a
AC
6891 begin
6892 -- Climb the parent chain looking for a check node
f9966234 6893
5073ad7a
AC
6894 Par := Nod;
6895 while Present (Par) loop
6896 if Nkind (Par) in N_Raise_xxx_Error then
6897 return True;
f9966234 6898
5073ad7a 6899 -- Prevent the search from going too far
f9966234 6900
5073ad7a
AC
6901 elsif Is_Body_Or_Package_Declaration (Par) then
6902 exit;
6903 end if;
6904
6905 Par := Parent (Par);
6906 end loop;
6907
6908 return False;
6909 end Within_Check;
6910
ed962eda
AC
6911 ----------------------------
6912 -- Within_Subprogram_Call --
6913 ----------------------------
5073ad7a 6914
ed962eda 6915 function Within_Subprogram_Call (Nod : Node_Id) return Boolean is
5073ad7a
AC
6916 Par : Node_Id;
6917
6918 begin
ed962eda 6919 -- Climb the parent chain looking for a function or procedure call
5073ad7a
AC
6920
6921 Par := Nod;
6922 while Present (Par) loop
ed962eda
AC
6923 if Nkind_In (Par, N_Function_Call,
6924 N_Procedure_Call_Statement)
6925 then
5073ad7a
AC
6926 return True;
6927
6928 -- Prevent the search from going too far
6929
6930 elsif Is_Body_Or_Package_Declaration (Par) then
6931 exit;
6932 end if;
6933
6934 Par := Parent (Par);
6935 end loop;
6936
6937 return False;
ed962eda 6938 end Within_Subprogram_Call;
5073ad7a 6939
847d950d
HK
6940 ------------------------------
6941 -- Within_Volatile_Function --
6942 ------------------------------
6943
6944 function Within_Volatile_Function (Id : Entity_Id) return Boolean is
6945 Func_Id : Entity_Id;
6946
6947 begin
6948 -- Traverse the scope stack looking for a [generic] function
6949
6950 Func_Id := Id;
6951 while Present (Func_Id) and then Func_Id /= Standard_Standard loop
6952 if Ekind_In (Func_Id, E_Function, E_Generic_Function) then
6953 return Is_Volatile_Function (Func_Id);
6954 end if;
6955
6956 Func_Id := Scope (Func_Id);
6957 end loop;
6958
6959 return False;
6960 end Within_Volatile_Function;
6961
6962 -- Local variables
6963
6964 Obj_Id : Entity_Id;
6965
5073ad7a 6966 -- Start of processing for Is_OK_Volatile_Context
3f80a182 6967
3f80a182
AC
6968 begin
6969 -- The volatile object appears on either side of an assignment
6970
6971 if Nkind (Context) = N_Assignment_Statement then
6972 return True;
6973
6974 -- The volatile object is part of the initialization expression of
847d950d 6975 -- another object.
3f80a182
AC
6976
6977 elsif Nkind (Context) = N_Object_Declaration
6978 and then Present (Expression (Context))
6979 and then Expression (Context) = Obj_Ref
6980 then
847d950d
HK
6981 Obj_Id := Defining_Entity (Context);
6982
6983 -- The volatile object acts as the initialization expression of an
6984 -- extended return statement. This is valid context as long as the
6985 -- function is volatile.
6986
6987 if Is_Return_Object (Obj_Id) then
6988 return Within_Volatile_Function (Obj_Id);
6989
6990 -- Otherwise this is a normal object initialization
6991
6992 else
6993 return True;
6994 end if;
3f80a182
AC
6995
6996 -- The volatile object appears as an actual parameter in a call to an
6997 -- instance of Unchecked_Conversion whose result is renamed.
6998
6999 elsif Nkind (Context) = N_Function_Call
fc90cc62 7000 and then Is_Entity_Name (Name (Context))
3f80a182
AC
7001 and then Is_Unchecked_Conversion_Instance (Entity (Name (Context)))
7002 and then Nkind (Parent (Context)) = N_Object_Renaming_Declaration
7003 then
7004 return True;
7005
fc90cc62
AC
7006 -- The volatile object is actually the prefix in a protected entry,
7007 -- function, or procedure call.
7008
7009 elsif Is_Protected_Operation_Call (Context) then
7010 return True;
7011
847d950d
HK
7012 -- The volatile object appears as the expression of a simple return
7013 -- statement that applies to a volatile function.
7014
7015 elsif Nkind (Context) = N_Simple_Return_Statement
7016 and then Expression (Context) = Obj_Ref
7017 then
7018 return
7019 Within_Volatile_Function (Return_Statement_Entity (Context));
7020
3f80a182
AC
7021 -- The volatile object appears as the prefix of a name occurring
7022 -- in a non-interfering context.
7023
7024 elsif Nkind_In (Context, N_Attribute_Reference,
a921e83c 7025 N_Explicit_Dereference,
3f80a182
AC
7026 N_Indexed_Component,
7027 N_Selected_Component,
7028 N_Slice)
7029 and then Prefix (Context) = Obj_Ref
7030 and then Is_OK_Volatile_Context
7031 (Context => Parent (Context),
7032 Obj_Ref => Context)
7033 then
7034 return True;
7035
4d1429b2
AC
7036 -- The volatile object appears as the expression of a type conversion
7037 -- occurring in a non-interfering context.
7038
7039 elsif Nkind_In (Context, N_Type_Conversion,
7040 N_Unchecked_Type_Conversion)
7041 and then Expression (Context) = Obj_Ref
7042 and then Is_OK_Volatile_Context
7043 (Context => Parent (Context),
7044 Obj_Ref => Context)
7045 then
7046 return True;
7047
3f80a182
AC
7048 -- Allow references to volatile objects in various checks. This is
7049 -- not a direct SPARK 2014 requirement.
7050
5073ad7a
AC
7051 elsif Within_Check (Context) then
7052 return True;
7053
7054 -- Assume that references to effectively volatile objects that appear
ed962eda 7055 -- as actual parameters in a subprogram call are always legal. A full
5073ad7a
AC
7056 -- legality check is done when the actuals are resolved.
7057
ed962eda 7058 elsif Within_Subprogram_Call (Context) then
3f80a182
AC
7059 return True;
7060
5073ad7a
AC
7061 -- Otherwise the context is not suitable for an effectively volatile
7062 -- object.
7063
3f80a182
AC
7064 else
7065 return False;
7066 end if;
7067 end Is_OK_Volatile_Context;
7068
f9966234
AC
7069 -- Local variables
7070
7071 E : constant Entity_Id := Entity (N);
d99565f8 7072 Par : Node_Id;
f9966234
AC
7073
7074 -- Start of processing for Resolve_Entity_Name
996ae0b0
RK
7075
7076 begin
07fc65c4
GB
7077 -- If garbage from errors, set to Any_Type and return
7078
7079 if No (E) and then Total_Errors_Detected /= 0 then
7080 Set_Etype (N, Any_Type);
7081 return;
7082 end if;
7083
996ae0b0
RK
7084 -- Replace named numbers by corresponding literals. Note that this is
7085 -- the one case where Resolve_Entity_Name must reset the Etype, since
7086 -- it is currently marked as universal.
7087
7088 if Ekind (E) = E_Named_Integer then
7089 Set_Etype (N, Typ);
7090 Eval_Named_Integer (N);
7091
7092 elsif Ekind (E) = E_Named_Real then
7093 Set_Etype (N, Typ);
7094 Eval_Named_Real (N);
7095
6989bc1f
AC
7096 -- For enumeration literals, we need to make sure that a proper style
7097 -- check is done, since such literals are overloaded, and thus we did
7098 -- not do a style check during the first phase of analysis.
7099
7100 elsif Ekind (E) = E_Enumeration_Literal then
e7ba564f 7101 Set_Entity_With_Checks (N, E);
6989bc1f
AC
7102 Eval_Entity_Name (N);
7103
596b25f9
AC
7104 -- Case of (sub)type name appearing in a context where an expression
7105 -- is expected. This is legal if occurrence is a current instance.
7106 -- See RM 8.6 (17/3).
996ae0b0
RK
7107
7108 elsif Is_Type (E) then
596b25f9 7109 if Is_Current_Instance (N) then
996ae0b0 7110 null;
e606088a 7111
308e6f3a 7112 -- Any other use is an error
e606088a 7113
996ae0b0
RK
7114 else
7115 Error_Msg_N
758c442c 7116 ("invalid use of subtype mark in expression or call", N);
996ae0b0
RK
7117 end if;
7118
7119 -- Check discriminant use if entity is discriminant in current scope,
7120 -- i.e. discriminant of record or concurrent type currently being
7121 -- analyzed. Uses in corresponding body are unrestricted.
7122
7123 elsif Ekind (E) = E_Discriminant
7124 and then Scope (E) = Current_Scope
7125 and then not Has_Completion (Current_Scope)
7126 then
7127 Check_Discriminant_Use (N);
7128
7129 -- A parameterless generic function cannot appear in a context that
7130 -- requires resolution.
7131
7132 elsif Ekind (E) = E_Generic_Function then
7133 Error_Msg_N ("illegal use of generic function", N);
7134
a921e83c
AC
7135 -- In Ada 83 an OUT parameter cannot be read
7136
996ae0b0 7137 elsif Ekind (E) = E_Out_Parameter
996ae0b0 7138 and then (Nkind (Parent (N)) in N_Op
a921e83c
AC
7139 or else Nkind (Parent (N)) = N_Explicit_Dereference
7140 or else Is_Assignment_Or_Object_Expression
7141 (Context => Parent (N),
7142 Expr => N))
996ae0b0 7143 then
a921e83c
AC
7144 if Ada_Version = Ada_83 then
7145 Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
a921e83c 7146 end if;
996ae0b0
RK
7147
7148 -- In all other cases, just do the possible static evaluation
7149
7150 else
d81b4bfe
TQ
7151 -- A deferred constant that appears in an expression must have a
7152 -- completion, unless it has been removed by in-place expansion of
7153 -- an aggregate.
996ae0b0
RK
7154
7155 if Ekind (E) = E_Constant
7156 and then Comes_From_Source (E)
7157 and then No (Constant_Value (E))
7158 and then Is_Frozen (Etype (E))
45fc7ddb 7159 and then not In_Spec_Expression
996ae0b0
RK
7160 and then not Is_Imported (E)
7161 then
996ae0b0
RK
7162 if No_Initialization (Parent (E))
7163 or else (Present (Full_View (E))
7164 and then No_Initialization (Parent (Full_View (E))))
7165 then
7166 null;
7167 else
7168 Error_Msg_N (
7169 "deferred constant is frozen before completion", N);
7170 end if;
7171 end if;
7172
7173 Eval_Entity_Name (N);
7174 end if;
6c3c671e 7175
d99565f8
AC
7176 Par := Parent (N);
7177
7178 -- When the entity appears in a parameter association, retrieve the
7179 -- related subprogram call.
7180
7181 if Nkind (Par) = N_Parameter_Association then
7182 Par := Parent (Par);
7183 end if;
7184
ed37f25a 7185 if Comes_From_Source (N) then
d950f051 7186
ed37f25a
AC
7187 -- The following checks are only relevant when SPARK_Mode is on as
7188 -- they are not standard Ada legality rules.
6c3c671e 7189
ed37f25a 7190 if SPARK_Mode = On then
c5cec2fe 7191
ed37f25a
AC
7192 -- An effectively volatile object subject to enabled properties
7193 -- Async_Writers or Effective_Reads must appear in non-interfering
7194 -- context (SPARK RM 7.1.3(12)).
c5cec2fe 7195
ed37f25a
AC
7196 if Is_Object (E)
7197 and then Is_Effectively_Volatile (E)
7198 and then (Async_Writers_Enabled (E)
7199 or else Effective_Reads_Enabled (E))
7200 and then not Is_OK_Volatile_Context (Par, N)
7201 then
7202 SPARK_Msg_N
7203 ("volatile object cannot appear in this context "
7204 & "(SPARK RM 7.1.3(12))", N);
7205 end if;
c5cec2fe 7206
ed37f25a
AC
7207 -- Check possible elaboration issues with respect to variables
7208
7209 if Ekind (E) = E_Variable then
7210 Check_Elab_Call (N);
7211 end if;
7212 end if;
de4899bb 7213
ed37f25a 7214 -- A Ghost entity must appear in a specific context
de4899bb 7215
ed37f25a
AC
7216 if Is_Ghost_Entity (E) then
7217 Check_Ghost_Context (E, N);
7218 end if;
de4899bb 7219 end if;
996ae0b0
RK
7220 end Resolve_Entity_Name;
7221
7222 -------------------
7223 -- Resolve_Entry --
7224 -------------------
7225
7226 procedure Resolve_Entry (Entry_Name : Node_Id) is
7227 Loc : constant Source_Ptr := Sloc (Entry_Name);
7228 Nam : Entity_Id;
7229 New_N : Node_Id;
7230 S : Entity_Id;
7231 Tsk : Entity_Id;
7232 E_Name : Node_Id;
7233 Index : Node_Id;
7234
7235 function Actual_Index_Type (E : Entity_Id) return Entity_Id;
7236 -- If the bounds of the entry family being called depend on task
7237 -- discriminants, build a new index subtype where a discriminant is
7238 -- replaced with the value of the discriminant of the target task.
7239 -- The target task is the prefix of the entry name in the call.
7240
7241 -----------------------
7242 -- Actual_Index_Type --
7243 -----------------------
7244
7245 function Actual_Index_Type (E : Entity_Id) return Entity_Id is
fbf5a39b
AC
7246 Typ : constant Entity_Id := Entry_Index_Type (E);
7247 Tsk : constant Entity_Id := Scope (E);
7248 Lo : constant Node_Id := Type_Low_Bound (Typ);
7249 Hi : constant Node_Id := Type_High_Bound (Typ);
996ae0b0
RK
7250 New_T : Entity_Id;
7251
7252 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
7253 -- If the bound is given by a discriminant, replace with a reference
d81b4bfe
TQ
7254 -- to the discriminant of the same name in the target task. If the
7255 -- entry name is the target of a requeue statement and the entry is
7256 -- in the current protected object, the bound to be used is the
008f6fd3 7257 -- discriminal of the object (see Apply_Range_Checks for details of
d81b4bfe 7258 -- the transformation).
996ae0b0
RK
7259
7260 -----------------------------
7261 -- Actual_Discriminant_Ref --
7262 -----------------------------
7263
7264 function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
fbf5a39b 7265 Typ : constant Entity_Id := Etype (Bound);
996ae0b0
RK
7266 Ref : Node_Id;
7267
7268 begin
7269 Remove_Side_Effects (Bound);
7270
7271 if not Is_Entity_Name (Bound)
7272 or else Ekind (Entity (Bound)) /= E_Discriminant
7273 then
7274 return Bound;
7275
7276 elsif Is_Protected_Type (Tsk)
7277 and then In_Open_Scopes (Tsk)
7278 and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
7279 then
6ca9ec9c
AC
7280 -- Note: here Bound denotes a discriminant of the corresponding
7281 -- record type tskV, whose discriminal is a formal of the
7282 -- init-proc tskVIP. What we want is the body discriminal,
7283 -- which is associated to the discriminant of the original
7284 -- concurrent type tsk.
7285
5a153b27
AC
7286 return New_Occurrence_Of
7287 (Find_Body_Discriminal (Entity (Bound)), Loc);
996ae0b0
RK
7288
7289 else
7290 Ref :=
7291 Make_Selected_Component (Loc,
7292 Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
7293 Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
7294 Analyze (Ref);
7295 Resolve (Ref, Typ);
7296 return Ref;
7297 end if;
7298 end Actual_Discriminant_Ref;
7299
7300 -- Start of processing for Actual_Index_Type
7301
7302 begin
7303 if not Has_Discriminants (Tsk)
19fb051c 7304 or else (not Is_Entity_Name (Lo) and then not Is_Entity_Name (Hi))
996ae0b0
RK
7305 then
7306 return Entry_Index_Type (E);
7307
7308 else
7309 New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
7310 Set_Etype (New_T, Base_Type (Typ));
7311 Set_Size_Info (New_T, Typ);
7312 Set_RM_Size (New_T, RM_Size (Typ));
7313 Set_Scalar_Range (New_T,
7314 Make_Range (Sloc (Entry_Name),
7315 Low_Bound => Actual_Discriminant_Ref (Lo),
7316 High_Bound => Actual_Discriminant_Ref (Hi)));
7317
7318 return New_T;
7319 end if;
7320 end Actual_Index_Type;
7321
704228bd 7322 -- Start of processing for Resolve_Entry
996ae0b0
RK
7323
7324 begin
5cc9353d
RD
7325 -- Find name of entry being called, and resolve prefix of name with its
7326 -- own type. The prefix can be overloaded, and the name and signature of
7327 -- the entry must be taken into account.
996ae0b0
RK
7328
7329 if Nkind (Entry_Name) = N_Indexed_Component then
7330
7331 -- Case of dealing with entry family within the current tasks
7332
7333 E_Name := Prefix (Entry_Name);
7334
7335 else
7336 E_Name := Entry_Name;
7337 end if;
7338
7339 if Is_Entity_Name (E_Name) then
996ae0b0 7340
d81b4bfe
TQ
7341 -- Entry call to an entry (or entry family) in the current task. This
7342 -- is legal even though the task will deadlock. Rewrite as call to
7343 -- current task.
996ae0b0 7344
d81b4bfe
TQ
7345 -- This can also be a call to an entry in an enclosing task. If this
7346 -- is a single task, we have to retrieve its name, because the scope
7347 -- of the entry is the task type, not the object. If the enclosing
7348 -- task is a task type, the identity of the task is given by its own
7349 -- self variable.
7350
7351 -- Finally this can be a requeue on an entry of the same task or
7352 -- protected object.
996ae0b0
RK
7353
7354 S := Scope (Entity (E_Name));
7355
7356 for J in reverse 0 .. Scope_Stack.Last loop
996ae0b0
RK
7357 if Is_Task_Type (Scope_Stack.Table (J).Entity)
7358 and then not Comes_From_Source (S)
7359 then
7360 -- S is an enclosing task or protected object. The concurrent
7361 -- declaration has been converted into a type declaration, and
7362 -- the object itself has an object declaration that follows
7363 -- the type in the same declarative part.
7364
7365 Tsk := Next_Entity (S);
996ae0b0
RK
7366 while Etype (Tsk) /= S loop
7367 Next_Entity (Tsk);
7368 end loop;
7369
7370 S := Tsk;
7371 exit;
7372
7373 elsif S = Scope_Stack.Table (J).Entity then
7374
7375 -- Call to current task. Will be transformed into call to Self
7376
7377 exit;
7378
7379 end if;
7380 end loop;
7381
7382 New_N :=
7383 Make_Selected_Component (Loc,
7384 Prefix => New_Occurrence_Of (S, Loc),
7385 Selector_Name =>
7386 New_Occurrence_Of (Entity (E_Name), Loc));
7387 Rewrite (E_Name, New_N);
7388 Analyze (E_Name);
7389
7390 elsif Nkind (Entry_Name) = N_Selected_Component
7391 and then Is_Overloaded (Prefix (Entry_Name))
7392 then
d81b4bfe 7393 -- Use the entry name (which must be unique at this point) to find
5cc9353d 7394 -- the prefix that returns the corresponding task/protected type.
996ae0b0
RK
7395
7396 declare
fbf5a39b 7397 Pref : constant Node_Id := Prefix (Entry_Name);
c8307596 7398 Ent : constant Entity_Id := Entity (Selector_Name (Entry_Name));
996ae0b0
RK
7399 I : Interp_Index;
7400 It : Interp;
996ae0b0
RK
7401
7402 begin
7403 Get_First_Interp (Pref, I, It);
996ae0b0 7404 while Present (It.Typ) loop
996ae0b0
RK
7405 if Scope (Ent) = It.Typ then
7406 Set_Etype (Pref, It.Typ);
7407 exit;
7408 end if;
7409
7410 Get_Next_Interp (I, It);
7411 end loop;
7412 end;
7413 end if;
7414
7415 if Nkind (Entry_Name) = N_Selected_Component then
fbf5a39b 7416 Resolve (Prefix (Entry_Name));
996ae0b0
RK
7417
7418 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
7419 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
fbf5a39b 7420 Resolve (Prefix (Prefix (Entry_Name)));
c8307596 7421 Index := First (Expressions (Entry_Name));
996ae0b0
RK
7422 Resolve (Index, Entry_Index_Type (Nam));
7423
d81b4bfe
TQ
7424 -- Up to this point the expression could have been the actual in a
7425 -- simple entry call, and be given by a named association.
996ae0b0
RK
7426
7427 if Nkind (Index) = N_Parameter_Association then
7428 Error_Msg_N ("expect expression for entry index", Index);
7429 else
7430 Apply_Range_Check (Index, Actual_Index_Type (Nam));
7431 end if;
7432 end if;
996ae0b0
RK
7433 end Resolve_Entry;
7434
7435 ------------------------
7436 -- Resolve_Entry_Call --
7437 ------------------------
7438
7439 procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
7440 Entry_Name : constant Node_Id := Name (N);
7441 Loc : constant Source_Ptr := Sloc (Entry_Name);
7442 Actuals : List_Id;
7443 First_Named : Node_Id;
7444 Nam : Entity_Id;
7445 Norm_OK : Boolean;
7446 Obj : Node_Id;
7447 Was_Over : Boolean;
7448
7449 begin
d81b4bfe
TQ
7450 -- We kill all checks here, because it does not seem worth the effort to
7451 -- do anything better, an entry call is a big operation.
fbf5a39b
AC
7452
7453 Kill_All_Checks;
7454
996ae0b0
RK
7455 -- Processing of the name is similar for entry calls and protected
7456 -- operation calls. Once the entity is determined, we can complete
7457 -- the resolution of the actuals.
7458
7459 -- The selector may be overloaded, in the case of a protected object
7460 -- with overloaded functions. The type of the context is used for
7461 -- resolution.
7462
7463 if Nkind (Entry_Name) = N_Selected_Component
7464 and then Is_Overloaded (Selector_Name (Entry_Name))
7465 and then Typ /= Standard_Void_Type
7466 then
7467 declare
7468 I : Interp_Index;
7469 It : Interp;
7470
7471 begin
7472 Get_First_Interp (Selector_Name (Entry_Name), I, It);
996ae0b0 7473 while Present (It.Typ) loop
996ae0b0
RK
7474 if Covers (Typ, It.Typ) then
7475 Set_Entity (Selector_Name (Entry_Name), It.Nam);
7476 Set_Etype (Entry_Name, It.Typ);
7477
7478 Generate_Reference (It.Typ, N, ' ');
7479 end if;
7480
7481 Get_Next_Interp (I, It);
7482 end loop;
7483 end;
7484 end if;
7485
7486 Resolve_Entry (Entry_Name);
7487
7488 if Nkind (Entry_Name) = N_Selected_Component then
7489
a77842bd 7490 -- Simple entry call
996ae0b0
RK
7491
7492 Nam := Entity (Selector_Name (Entry_Name));
7493 Obj := Prefix (Entry_Name);
7494 Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
7495
7496 else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
7497
a77842bd 7498 -- Call to member of entry family
996ae0b0
RK
7499
7500 Nam := Entity (Selector_Name (Prefix (Entry_Name)));
7501 Obj := Prefix (Prefix (Entry_Name));
7502 Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
7503 end if;
7504
5cc9353d
RD
7505 -- We cannot in general check the maximum depth of protected entry calls
7506 -- at compile time. But we can tell that any protected entry call at all
7507 -- violates a specified nesting depth of zero.
fbf5a39b
AC
7508
7509 if Is_Protected_Type (Scope (Nam)) then
9f4fd324 7510 Check_Restriction (Max_Entry_Queue_Length, N);
fbf5a39b
AC
7511 end if;
7512
996ae0b0 7513 -- Use context type to disambiguate a protected function that can be
5cc9353d
RD
7514 -- called without actuals and that returns an array type, and where the
7515 -- argument list may be an indexing of the returned value.
996ae0b0
RK
7516
7517 if Ekind (Nam) = E_Function
7518 and then Needs_No_Actuals (Nam)
7519 and then Present (Parameter_Associations (N))
7520 and then
7521 ((Is_Array_Type (Etype (Nam))
7522 and then Covers (Typ, Component_Type (Etype (Nam))))
7523
7524 or else (Is_Access_Type (Etype (Nam))
7525 and then Is_Array_Type (Designated_Type (Etype (Nam)))
19fb051c
AC
7526 and then
7527 Covers
7528 (Typ,
7529 Component_Type (Designated_Type (Etype (Nam))))))
996ae0b0
RK
7530 then
7531 declare
7532 Index_Node : Node_Id;
7533
7534 begin
7535 Index_Node :=
7536 Make_Indexed_Component (Loc,
7537 Prefix =>
19fb051c 7538 Make_Function_Call (Loc, Name => Relocate_Node (Entry_Name)),
996ae0b0
RK
7539 Expressions => Parameter_Associations (N));
7540
5cc9353d
RD
7541 -- Since we are correcting a node classification error made by the
7542 -- parser, we call Replace rather than Rewrite.
996ae0b0
RK
7543
7544 Replace (N, Index_Node);
7545 Set_Etype (Prefix (N), Etype (Nam));
7546 Set_Etype (N, Typ);
7547 Resolve_Indexed_Component (N, Typ);
7548 return;
7549 end;
7550 end if;
7551
b7f17b20
ES
7552 if Ekind_In (Nam, E_Entry, E_Entry_Family)
7553 and then Present (PPC_Wrapper (Nam))
7554 and then Current_Scope /= PPC_Wrapper (Nam)
7555 then
468ee96a 7556 -- Rewrite as call to the precondition wrapper, adding the task
5cc9353d
RD
7557 -- object to the list of actuals. If the call is to a member of an
7558 -- entry family, include the index as well.
b7f17b20
ES
7559
7560 declare
468ee96a 7561 New_Call : Node_Id;
b7f17b20 7562 New_Actuals : List_Id;
19fb051c 7563
b7f17b20
ES
7564 begin
7565 New_Actuals := New_List (Obj);
3fd9f17c 7566
9fe696a3 7567 if Nkind (Entry_Name) = N_Indexed_Component then
3fd9f17c
AC
7568 Append_To (New_Actuals,
7569 New_Copy_Tree (First (Expressions (Entry_Name))));
7570 end if;
7571
b7f17b20 7572 Append_List (Parameter_Associations (N), New_Actuals);
468ee96a
AC
7573 New_Call :=
7574 Make_Procedure_Call_Statement (Loc,
7575 Name =>
7576 New_Occurrence_Of (PPC_Wrapper (Nam), Loc),
7577 Parameter_Associations => New_Actuals);
b7f17b20 7578 Rewrite (N, New_Call);
ecda544d
ES
7579
7580 -- Preanalyze and resolve new call. Current procedure is called
7581 -- from Resolve_Call, after which expansion will take place.
7582
7583 Preanalyze_And_Resolve (N);
b7f17b20
ES
7584 return;
7585 end;
7586 end if;
7587
996ae0b0 7588 -- The operation name may have been overloaded. Order the actuals
5cc9353d
RD
7589 -- according to the formals of the resolved entity, and set the return
7590 -- type to that of the operation.
996ae0b0
RK
7591
7592 if Was_Over then
7593 Normalize_Actuals (N, Nam, False, Norm_OK);
7594 pragma Assert (Norm_OK);
fbf5a39b 7595 Set_Etype (N, Etype (Nam));
996ae0b0
RK
7596 end if;
7597
7598 Resolve_Actuals (N, Nam);
c92e8586 7599 Check_Internal_Protected_Use (N, Nam);
ae6ede77
AC
7600
7601 -- Create a call reference to the entry
7602
7603 Generate_Reference (Nam, Entry_Name, 's');
996ae0b0 7604
8a95f4e8 7605 if Ekind_In (Nam, E_Entry, E_Entry_Family) then
996ae0b0
RK
7606 Check_Potentially_Blocking_Operation (N);
7607 end if;
7608
7609 -- Verify that a procedure call cannot masquerade as an entry
7610 -- call where an entry call is expected.
7611
7612 if Ekind (Nam) = E_Procedure then
996ae0b0
RK
7613 if Nkind (Parent (N)) = N_Entry_Call_Alternative
7614 and then N = Entry_Call_Statement (Parent (N))
7615 then
7616 Error_Msg_N ("entry call required in select statement", N);
7617
7618 elsif Nkind (Parent (N)) = N_Triggering_Alternative
7619 and then N = Triggering_Statement (Parent (N))
7620 then
7621 Error_Msg_N ("triggering statement cannot be procedure call", N);
7622
7623 elsif Ekind (Scope (Nam)) = E_Task_Type
7624 and then not In_Open_Scopes (Scope (Nam))
7625 then
758c442c 7626 Error_Msg_N ("task has no entry with this name", Entry_Name);
996ae0b0
RK
7627 end if;
7628 end if;
7629
d81b4bfe
TQ
7630 -- After resolution, entry calls and protected procedure calls are
7631 -- changed into entry calls, for expansion. The structure of the node
7632 -- does not change, so it can safely be done in place. Protected
7633 -- function calls must keep their structure because they are
7634 -- subexpressions.
996ae0b0
RK
7635
7636 if Ekind (Nam) /= E_Function then
7637
7638 -- A protected operation that is not a function may modify the
d81b4bfe
TQ
7639 -- corresponding object, and cannot apply to a constant. If this
7640 -- is an internal call, the prefix is the type itself.
996ae0b0
RK
7641
7642 if Is_Protected_Type (Scope (Nam))
7643 and then not Is_Variable (Obj)
7644 and then (not Is_Entity_Name (Obj)
7645 or else not Is_Type (Entity (Obj)))
7646 then
7647 Error_Msg_N
7648 ("prefix of protected procedure or entry call must be variable",
7649 Entry_Name);
7650 end if;
7651
7652 Actuals := Parameter_Associations (N);
7653 First_Named := First_Named_Actual (N);
7654
7655 Rewrite (N,
7656 Make_Entry_Call_Statement (Loc,
7657 Name => Entry_Name,
7658 Parameter_Associations => Actuals));
7659
7660 Set_First_Named_Actual (N, First_Named);
7661 Set_Analyzed (N, True);
7662
7663 -- Protected functions can return on the secondary stack, in which
1420b484 7664 -- case we must trigger the transient scope mechanism.
996ae0b0 7665
4460a9bc 7666 elsif Expander_Active
996ae0b0
RK
7667 and then Requires_Transient_Scope (Etype (Nam))
7668 then
0669bebe 7669 Establish_Transient_Scope (N, Sec_Stack => True);
996ae0b0 7670 end if;
996ae0b0
RK
7671 end Resolve_Entry_Call;
7672
7673 -------------------------
7674 -- Resolve_Equality_Op --
7675 -------------------------
7676
d81b4bfe
TQ
7677 -- Both arguments must have the same type, and the boolean context does
7678 -- not participate in the resolution. The first pass verifies that the
7679 -- interpretation is not ambiguous, and the type of the left argument is
7680 -- correctly set, or is Any_Type in case of ambiguity. If both arguments
7681 -- are strings or aggregates, allocators, or Null, they are ambiguous even
7682 -- though they carry a single (universal) type. Diagnose this case here.
996ae0b0
RK
7683
7684 procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
7685 L : constant Node_Id := Left_Opnd (N);
7686 R : constant Node_Id := Right_Opnd (N);
7687 T : Entity_Id := Find_Unique_Type (L, R);
7688
9b16cb57
RD
7689 procedure Check_If_Expression (Cond : Node_Id);
7690 -- The resolution rule for if expressions requires that each such must
7691 -- have a unique type. This means that if several dependent expressions
7692 -- are of a non-null anonymous access type, and the context does not
7693 -- impose an expected type (as can be the case in an equality operation)
7694 -- the expression must be rejected.
a8930b80 7695
327b1ba4
AC
7696 procedure Explain_Redundancy (N : Node_Id);
7697 -- Attempt to explain the nature of a redundant comparison with True. If
7698 -- the expression N is too complex, this routine issues a general error
7699 -- message.
7700
996ae0b0 7701 function Find_Unique_Access_Type return Entity_Id;
289a994b
AC
7702 -- In the case of allocators and access attributes, the context must
7703 -- provide an indication of the specific access type to be used. If
7704 -- one operand is of such a "generic" access type, check whether there
7705 -- is a specific visible access type that has the same designated type.
7706 -- This is semantically dubious, and of no interest to any real code,
7707 -- but c48008a makes it all worthwhile.
996ae0b0 7708
9b16cb57
RD
7709 -------------------------
7710 -- Check_If_Expression --
7711 -------------------------
a8930b80 7712
9b16cb57 7713 procedure Check_If_Expression (Cond : Node_Id) is
a8930b80
AC
7714 Then_Expr : Node_Id;
7715 Else_Expr : Node_Id;
7716
7717 begin
9b16cb57 7718 if Nkind (Cond) = N_If_Expression then
a8930b80
AC
7719 Then_Expr := Next (First (Expressions (Cond)));
7720 Else_Expr := Next (Then_Expr);
7721
7722 if Nkind (Then_Expr) /= N_Null
7723 and then Nkind (Else_Expr) /= N_Null
7724 then
9b16cb57 7725 Error_Msg_N ("cannot determine type of if expression", Cond);
a8930b80
AC
7726 end if;
7727 end if;
9b16cb57 7728 end Check_If_Expression;
a8930b80 7729
327b1ba4
AC
7730 ------------------------
7731 -- Explain_Redundancy --
7732 ------------------------
7733
7734 procedure Explain_Redundancy (N : Node_Id) is
7735 Error : Name_Id;
7736 Val : Node_Id;
7737 Val_Id : Entity_Id;
7738
7739 begin
7740 Val := N;
7741
7742 -- Strip the operand down to an entity
7743
7744 loop
7745 if Nkind (Val) = N_Selected_Component then
7746 Val := Selector_Name (Val);
7747 else
7748 exit;
7749 end if;
7750 end loop;
7751
7752 -- The construct denotes an entity
7753
7754 if Is_Entity_Name (Val) and then Present (Entity (Val)) then
7755 Val_Id := Entity (Val);
7756
7757 -- Do not generate an error message when the comparison is done
7758 -- against the enumeration literal Standard.True.
7759
7760 if Ekind (Val_Id) /= E_Enumeration_Literal then
7761
7762 -- Build a customized error message
7763
7764 Name_Len := 0;
7765 Add_Str_To_Name_Buffer ("?r?");
7766
7767 if Ekind (Val_Id) = E_Component then
7768 Add_Str_To_Name_Buffer ("component ");
7769
7770 elsif Ekind (Val_Id) = E_Constant then
7771 Add_Str_To_Name_Buffer ("constant ");
7772
7773 elsif Ekind (Val_Id) = E_Discriminant then
7774 Add_Str_To_Name_Buffer ("discriminant ");
7775
7776 elsif Is_Formal (Val_Id) then
7777 Add_Str_To_Name_Buffer ("parameter ");
7778
7779 elsif Ekind (Val_Id) = E_Variable then
7780 Add_Str_To_Name_Buffer ("variable ");
7781 end if;
7782
7783 Add_Str_To_Name_Buffer ("& is always True!");
7784 Error := Name_Find;
7785
7786 Error_Msg_NE (Get_Name_String (Error), Val, Val_Id);
7787 end if;
7788
7789 -- The construct is too complex to disect, issue a general message
7790
7791 else
7792 Error_Msg_N ("?r?expression is always True!", Val);
7793 end if;
7794 end Explain_Redundancy;
7795
996ae0b0
RK
7796 -----------------------------
7797 -- Find_Unique_Access_Type --
7798 -----------------------------
7799
7800 function Find_Unique_Access_Type return Entity_Id is
7801 Acc : Entity_Id;
7802 E : Entity_Id;
1420b484 7803 S : Entity_Id;
996ae0b0
RK
7804
7805 begin
59fad002
AC
7806 if Ekind_In (Etype (R), E_Allocator_Type,
7807 E_Access_Attribute_Type)
289a994b 7808 then
996ae0b0 7809 Acc := Designated_Type (Etype (R));
289a994b 7810
59fad002
AC
7811 elsif Ekind_In (Etype (L), E_Allocator_Type,
7812 E_Access_Attribute_Type)
289a994b 7813 then
996ae0b0 7814 Acc := Designated_Type (Etype (L));
996ae0b0
RK
7815 else
7816 return Empty;
7817 end if;
7818
1420b484 7819 S := Current_Scope;
996ae0b0
RK
7820 while S /= Standard_Standard loop
7821 E := First_Entity (S);
996ae0b0 7822 while Present (E) loop
996ae0b0
RK
7823 if Is_Type (E)
7824 and then Is_Access_Type (E)
7825 and then Ekind (E) /= E_Allocator_Type
7826 and then Designated_Type (E) = Base_Type (Acc)
7827 then
7828 return E;
7829 end if;
7830
7831 Next_Entity (E);
7832 end loop;
7833
7834 S := Scope (S);
7835 end loop;
7836
7837 return Empty;
7838 end Find_Unique_Access_Type;
7839
7840 -- Start of processing for Resolve_Equality_Op
7841
7842 begin
7843 Set_Etype (N, Base_Type (Typ));
7844 Generate_Reference (T, N, ' ');
7845
7846 if T = Any_Fixed then
7847 T := Unique_Fixed_Point_Type (L);
7848 end if;
7849
7850 if T /= Any_Type then
19fb051c
AC
7851 if T = Any_String or else
7852 T = Any_Composite or else
7853 T = Any_Character
996ae0b0 7854 then
996ae0b0
RK
7855 if T = Any_Character then
7856 Ambiguous_Character (L);
7857 else
7858 Error_Msg_N ("ambiguous operands for equality", N);
7859 end if;
7860
7861 Set_Etype (N, Any_Type);
7862 return;
7863
7864 elsif T = Any_Access
964f13da 7865 or else Ekind_In (T, E_Allocator_Type, E_Access_Attribute_Type)
996ae0b0
RK
7866 then
7867 T := Find_Unique_Access_Type;
7868
7869 if No (T) then
7870 Error_Msg_N ("ambiguous operands for equality", N);
7871 Set_Etype (N, Any_Type);
7872 return;
7873 end if;
a8930b80 7874
9b16cb57
RD
7875 -- If expressions must have a single type, and if the context does
7876 -- not impose one the dependent expressions cannot be anonymous
7877 -- access types.
7878
7879 -- Why no similar processing for case expressions???
a8930b80
AC
7880
7881 elsif Ada_Version >= Ada_2012
ae2aa109
AC
7882 and then Ekind_In (Etype (L), E_Anonymous_Access_Type,
7883 E_Anonymous_Access_Subprogram_Type)
7884 and then Ekind_In (Etype (R), E_Anonymous_Access_Type,
7885 E_Anonymous_Access_Subprogram_Type)
a8930b80 7886 then
9b16cb57
RD
7887 Check_If_Expression (L);
7888 Check_If_Expression (R);
996ae0b0
RK
7889 end if;
7890
996ae0b0
RK
7891 Resolve (L, T);
7892 Resolve (R, T);
fbf5a39b 7893
2ba431e5
YM
7894 -- In SPARK, equality operators = and /= for array types other than
7895 -- String are only defined when, for each index position, the
7896 -- operands have equal static bounds.
b0186f71 7897
975c6896 7898 if Is_Array_Type (T) then
9b16cb57 7899
7b98672f
YM
7900 -- Protect call to Matching_Static_Array_Bounds to avoid costly
7901 -- operation if not needed.
7902
6480338a 7903 if Restriction_Check_Required (SPARK_05)
7b98672f 7904 and then Base_Type (T) /= Standard_String
975c6896
YM
7905 and then Base_Type (Etype (L)) = Base_Type (Etype (R))
7906 and then Etype (L) /= Any_Composite -- or else L in error
7907 and then Etype (R) /= Any_Composite -- or else R in error
7908 and then not Matching_Static_Array_Bounds (Etype (L), Etype (R))
7909 then
ce5ba43a 7910 Check_SPARK_05_Restriction
975c6896
YM
7911 ("array types should have matching static bounds", N);
7912 end if;
b0186f71
AC
7913 end if;
7914
0669bebe
GB
7915 -- If the unique type is a class-wide type then it will be expanded
7916 -- into a dispatching call to the predefined primitive. Therefore we
7917 -- check here for potential violation of such restriction.
7918
7919 if Is_Class_Wide_Type (T) then
7920 Check_Restriction (No_Dispatching_Calls, N);
7921 end if;
7922
fbf5a39b
AC
7923 if Warn_On_Redundant_Constructs
7924 and then Comes_From_Source (N)
327b1ba4 7925 and then Comes_From_Source (R)
fbf5a39b
AC
7926 and then Is_Entity_Name (R)
7927 and then Entity (R) = Standard_True
fbf5a39b 7928 then
305caf42 7929 Error_Msg_N -- CODEFIX
327b1ba4
AC
7930 ("?r?comparison with True is redundant!", N);
7931 Explain_Redundancy (Original_Node (R));
fbf5a39b
AC
7932 end if;
7933
996ae0b0
RK
7934 Check_Unset_Reference (L);
7935 Check_Unset_Reference (R);
fbf5a39b 7936 Generate_Operator_Reference (N, T);
fad0600d 7937 Check_Low_Bound_Tested (N);
996ae0b0
RK
7938
7939 -- If this is an inequality, it may be the implicit inequality
7940 -- created for a user-defined operation, in which case the corres-
7941 -- ponding equality operation is not intrinsic, and the operation
7942 -- cannot be constant-folded. Else fold.
7943
7944 if Nkind (N) = N_Op_Eq
7945 or else Comes_From_Source (Entity (N))
7946 or else Ekind (Entity (N)) = E_Operator
7947 or else Is_Intrinsic_Subprogram
19fb051c 7948 (Corresponding_Equality (Entity (N)))
996ae0b0 7949 then
dec6faf1 7950 Analyze_Dimension (N);
996ae0b0 7951 Eval_Relational_Op (N);
45fc7ddb 7952
996ae0b0 7953 elsif Nkind (N) = N_Op_Ne
0669bebe 7954 and then Is_Abstract_Subprogram (Entity (N))
996ae0b0
RK
7955 then
7956 Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
7957 end if;
758c442c 7958
d81b4bfe
TQ
7959 -- Ada 2005: If one operand is an anonymous access type, convert the
7960 -- other operand to it, to ensure that the underlying types match in
7961 -- the back-end. Same for access_to_subprogram, and the conversion
7962 -- verifies that the types are subtype conformant.
b7d1f17f 7963
d81b4bfe
TQ
7964 -- We apply the same conversion in the case one of the operands is a
7965 -- private subtype of the type of the other.
c8ef728f 7966
b7d1f17f
HK
7967 -- Why the Expander_Active test here ???
7968
4460a9bc 7969 if Expander_Active
b7d1f17f 7970 and then
964f13da
RD
7971 (Ekind_In (T, E_Anonymous_Access_Type,
7972 E_Anonymous_Access_Subprogram_Type)
b7d1f17f 7973 or else Is_Private_Type (T))
c8ef728f
ES
7974 then
7975 if Etype (L) /= T then
7976 Rewrite (L,
7977 Make_Unchecked_Type_Conversion (Sloc (L),
7978 Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
7979 Expression => Relocate_Node (L)));
7980 Analyze_And_Resolve (L, T);
7981 end if;
7982
7983 if (Etype (R)) /= T then
7984 Rewrite (R,
7985 Make_Unchecked_Type_Conversion (Sloc (R),
7986 Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
7987 Expression => Relocate_Node (R)));
7988 Analyze_And_Resolve (R, T);
7989 end if;
7990 end if;
996ae0b0
RK
7991 end if;
7992 end Resolve_Equality_Op;
7993
7994 ----------------------------------
7995 -- Resolve_Explicit_Dereference --
7996 ----------------------------------
7997
7998 procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
bc5f3720
RD
7999 Loc : constant Source_Ptr := Sloc (N);
8000 New_N : Node_Id;
8001 P : constant Node_Id := Prefix (N);
50878404
AC
8002
8003 P_Typ : Entity_Id;
8004 -- The candidate prefix type, if overloaded
8005
bc5f3720
RD
8006 I : Interp_Index;
8007 It : Interp;
996ae0b0
RK
8008
8009 begin
c8ef728f 8010 Check_Fully_Declared_Prefix (Typ, P);
50878404 8011 P_Typ := Empty;
996ae0b0 8012
3e586e10
AC
8013 -- A useful optimization: check whether the dereference denotes an
8014 -- element of a container, and if so rewrite it as a call to the
8015 -- corresponding Element function.
ebb6b0bd 8016
3e586e10
AC
8017 -- Disabled for now, on advice of ARG. A more restricted form of the
8018 -- predicate might be acceptable ???
8019
8020 -- if Is_Container_Element (N) then
8021 -- return;
8022 -- end if;
8023
996ae0b0
RK
8024 if Is_Overloaded (P) then
8025
758c442c 8026 -- Use the context type to select the prefix that has the correct
d7a44b14
AC
8027 -- designated type. Keep the first match, which will be the inner-
8028 -- most.
996ae0b0
RK
8029
8030 Get_First_Interp (P, I, It);
50878404 8031
996ae0b0 8032 while Present (It.Typ) loop
50878404
AC
8033 if Is_Access_Type (It.Typ)
8034 and then Covers (Typ, Designated_Type (It.Typ))
8035 then
d7a44b14
AC
8036 if No (P_Typ) then
8037 P_Typ := It.Typ;
8038 end if;
50878404
AC
8039
8040 -- Remove access types that do not match, but preserve access
8041 -- to subprogram interpretations, in case a further dereference
8042 -- is needed (see below).
8043
8044 elsif Ekind (It.Typ) /= E_Access_Subprogram_Type then
8045 Remove_Interp (I);
8046 end if;
8047
996ae0b0
RK
8048 Get_Next_Interp (I, It);
8049 end loop;
8050
50878404
AC
8051 if Present (P_Typ) then
8052 Resolve (P, P_Typ);
8053 Set_Etype (N, Designated_Type (P_Typ));
8054
bc5f3720 8055 else
758c442c
GD
8056 -- If no interpretation covers the designated type of the prefix,
8057 -- this is the pathological case where not all implementations of
8058 -- the prefix allow the interpretation of the node as a call. Now
8059 -- that the expected type is known, Remove other interpretations
8060 -- from prefix, rewrite it as a call, and resolve again, so that
8061 -- the proper call node is generated.
bc5f3720
RD
8062
8063 Get_First_Interp (P, I, It);
8064 while Present (It.Typ) loop
8065 if Ekind (It.Typ) /= E_Access_Subprogram_Type then
8066 Remove_Interp (I);
8067 end if;
8068
8069 Get_Next_Interp (I, It);
8070 end loop;
8071
8072 New_N :=
8073 Make_Function_Call (Loc,
8074 Name =>
8075 Make_Explicit_Dereference (Loc,
8076 Prefix => P),
8077 Parameter_Associations => New_List);
8078
8079 Save_Interps (N, New_N);
8080 Rewrite (N, New_N);
8081 Analyze_And_Resolve (N, Typ);
8082 return;
8083 end if;
8084
29ba9f52 8085 -- If not overloaded, resolve P with its own type
50878404 8086
29ba9f52 8087 else
fbf5a39b 8088 Resolve (P);
996ae0b0
RK
8089 end if;
8090
8091 if Is_Access_Type (Etype (P)) then
8092 Apply_Access_Check (N);
8093 end if;
8094
758c442c
GD
8095 -- If the designated type is a packed unconstrained array type, and the
8096 -- explicit dereference is not in the context of an attribute reference,
8097 -- then we must compute and set the actual subtype, since it is needed
8098 -- by Gigi. The reason we exclude the attribute case is that this is
8099 -- handled fine by Gigi, and in fact we use such attributes to build the
8100 -- actual subtype. We also exclude generated code (which builds actual
8101 -- subtypes directly if they are needed).
996ae0b0
RK
8102
8103 if Is_Array_Type (Etype (N))
8104 and then Is_Packed (Etype (N))
8105 and then not Is_Constrained (Etype (N))
8106 and then Nkind (Parent (N)) /= N_Attribute_Reference
8107 and then Comes_From_Source (N)
8108 then
8109 Set_Etype (N, Get_Actual_Subtype (N));
8110 end if;
8111
d29f68cf 8112 Analyze_Dimension (N);
09494c32
AC
8113 -- Note: No Eval processing is required for an explicit dereference,
8114 -- because such a name can never be static.
996ae0b0
RK
8115
8116 end Resolve_Explicit_Dereference;
8117
955871d3
AC
8118 -------------------------------------
8119 -- Resolve_Expression_With_Actions --
8120 -------------------------------------
8121
8122 procedure Resolve_Expression_With_Actions (N : Node_Id; Typ : Entity_Id) is
8123 begin
8124 Set_Etype (N, Typ);
064f4527
TQ
8125
8126 -- If N has no actions, and its expression has been constant folded,
8127 -- then rewrite N as just its expression. Note, we can't do this in
8128 -- the general case of Is_Empty_List (Actions (N)) as this would cause
8129 -- Expression (N) to be expanded again.
8130
8131 if Is_Empty_List (Actions (N))
8132 and then Compile_Time_Known_Value (Expression (N))
8133 then
8134 Rewrite (N, Expression (N));
8135 end if;
955871d3
AC
8136 end Resolve_Expression_With_Actions;
8137
5f50020a
ES
8138 ----------------------------------
8139 -- Resolve_Generalized_Indexing --
8140 ----------------------------------
8141
8142 procedure Resolve_Generalized_Indexing (N : Node_Id; Typ : Entity_Id) is
8143 Indexing : constant Node_Id := Generalized_Indexing (N);
8144 Call : Node_Id;
0566484a 8145 Indexes : List_Id;
5f50020a
ES
8146 Pref : Node_Id;
8147
8148 begin
0566484a 8149 -- In ASIS mode, propagate the information about the indexes back to
5f50020a
ES
8150 -- to the original indexing node. The generalized indexing is either
8151 -- a function call, or a dereference of one. The actuals include the
8152 -- prefix of the original node, which is the container expression.
8153
8154 if ASIS_Mode then
8155 Resolve (Indexing, Typ);
8156 Set_Etype (N, Etype (Indexing));
8157 Set_Is_Overloaded (N, False);
32bba3c9 8158
5f50020a 8159 Call := Indexing;
32bba3c9 8160 while Nkind_In (Call, N_Explicit_Dereference, N_Selected_Component)
5f50020a
ES
8161 loop
8162 Call := Prefix (Call);
8163 end loop;
8164
8165 if Nkind (Call) = N_Function_Call then
0566484a
AC
8166 Indexes := Parameter_Associations (Call);
8167 Pref := Remove_Head (Indexes);
8168 Set_Expressions (N, Indexes);
5f50020a
ES
8169 Set_Prefix (N, Pref);
8170 end if;
8171
8172 else
8173 Rewrite (N, Indexing);
8174 Resolve (N, Typ);
8175 end if;
8176 end Resolve_Generalized_Indexing;
8177
9b16cb57
RD
8178 ---------------------------
8179 -- Resolve_If_Expression --
8180 ---------------------------
8181
8182 procedure Resolve_If_Expression (N : Node_Id; Typ : Entity_Id) is
8183 Condition : constant Node_Id := First (Expressions (N));
8184 Then_Expr : constant Node_Id := Next (Condition);
8185 Else_Expr : Node_Id := Next (Then_Expr);
8186 Else_Typ : Entity_Id;
8187 Then_Typ : Entity_Id;
8188
8189 begin
8190 Resolve (Condition, Any_Boolean);
8191 Resolve (Then_Expr, Typ);
8192 Then_Typ := Etype (Then_Expr);
8193
30ebb114
AC
8194 -- When the "then" expression is of a scalar subtype different from the
8195 -- result subtype, then insert a conversion to ensure the generation of
8196 -- a constraint check. The same is done for the else part below, again
8197 -- comparing subtypes rather than base types.
9b16cb57
RD
8198
8199 if Is_Scalar_Type (Then_Typ)
30ebb114 8200 and then Then_Typ /= Typ
9b16cb57
RD
8201 then
8202 Rewrite (Then_Expr, Convert_To (Typ, Then_Expr));
8203 Analyze_And_Resolve (Then_Expr, Typ);
8204 end if;
8205
8206 -- If ELSE expression present, just resolve using the determined type
8207
8208 if Present (Else_Expr) then
8209 Resolve (Else_Expr, Typ);
8210 Else_Typ := Etype (Else_Expr);
8211
b6dd03dd 8212 if Is_Scalar_Type (Else_Typ) and then Else_Typ /= Typ then
9b16cb57
RD
8213 Rewrite (Else_Expr, Convert_To (Typ, Else_Expr));
8214 Analyze_And_Resolve (Else_Expr, Typ);
b6dd03dd
ES
8215
8216 -- Apply RM 4.5.7 (17/3): whether the expression is statically or
8217 -- dynamically tagged must be known statically.
8218
8219 elsif Is_Tagged_Type (Typ) and then not Is_Class_Wide_Type (Typ) then
8220 if Is_Dynamically_Tagged (Then_Expr) /=
8221 Is_Dynamically_Tagged (Else_Expr)
8222 then
8223 Error_Msg_N ("all or none of the dependent expressions "
8224 & "can be dynamically tagged", N);
8225 end if;
9b16cb57
RD
8226 end if;
8227
8228 -- If no ELSE expression is present, root type must be Standard.Boolean
8229 -- and we provide a Standard.True result converted to the appropriate
8230 -- Boolean type (in case it is a derived boolean type).
8231
8232 elsif Root_Type (Typ) = Standard_Boolean then
8233 Else_Expr :=
8234 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
8235 Analyze_And_Resolve (Else_Expr, Typ);
8236 Append_To (Expressions (N), Else_Expr);
8237
8238 else
8239 Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
8240 Append_To (Expressions (N), Error);
8241 end if;
8242
8243 Set_Etype (N, Typ);
8244 Eval_If_Expression (N);
9b16cb57
RD
8245 end Resolve_If_Expression;
8246
996ae0b0
RK
8247 -------------------------------
8248 -- Resolve_Indexed_Component --
8249 -------------------------------
8250
8251 procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
8252 Name : constant Node_Id := Prefix (N);
8253 Expr : Node_Id;
8254 Array_Type : Entity_Id := Empty; -- to prevent junk warning
8255 Index : Node_Id;
8256
8257 begin
5f50020a
ES
8258 if Present (Generalized_Indexing (N)) then
8259 Resolve_Generalized_Indexing (N, Typ);
8260 return;
8261 end if;
8262
996ae0b0
RK
8263 if Is_Overloaded (Name) then
8264
758c442c
GD
8265 -- Use the context type to select the prefix that yields the correct
8266 -- component type.
996ae0b0
RK
8267
8268 declare
8269 I : Interp_Index;
8270 It : Interp;
8271 I1 : Interp_Index := 0;
8272 P : constant Node_Id := Prefix (N);
8273 Found : Boolean := False;
8274
8275 begin
8276 Get_First_Interp (P, I, It);
996ae0b0 8277 while Present (It.Typ) loop
996ae0b0
RK
8278 if (Is_Array_Type (It.Typ)
8279 and then Covers (Typ, Component_Type (It.Typ)))
8280 or else (Is_Access_Type (It.Typ)
8281 and then Is_Array_Type (Designated_Type (It.Typ))
19fb051c
AC
8282 and then
8283 Covers
8284 (Typ,
8285 Component_Type (Designated_Type (It.Typ))))
996ae0b0
RK
8286 then
8287 if Found then
8288 It := Disambiguate (P, I1, I, Any_Type);
8289
8290 if It = No_Interp then
8291 Error_Msg_N ("ambiguous prefix for indexing", N);
8292 Set_Etype (N, Typ);
8293 return;
8294
8295 else
8296 Found := True;
8297 Array_Type := It.Typ;
8298 I1 := I;
8299 end if;
8300
8301 else
8302 Found := True;
8303 Array_Type := It.Typ;
8304 I1 := I;
8305 end if;
8306 end if;
8307
8308 Get_Next_Interp (I, It);
8309 end loop;
8310 end;
8311
8312 else
8313 Array_Type := Etype (Name);
8314 end if;
8315
8316 Resolve (Name, Array_Type);
8317 Array_Type := Get_Actual_Subtype_If_Available (Name);
8318
8319 -- If prefix is access type, dereference to get real array type.
8320 -- Note: we do not apply an access check because the expander always
8321 -- introduces an explicit dereference, and the check will happen there.
8322
8323 if Is_Access_Type (Array_Type) then
8324 Array_Type := Designated_Type (Array_Type);
8325 end if;
8326
a77842bd 8327 -- If name was overloaded, set component type correctly now
f3d57416 8328 -- If a misplaced call to an entry family (which has no index types)
b7d1f17f 8329 -- return. Error will be diagnosed from calling context.
996ae0b0 8330
b7d1f17f
HK
8331 if Is_Array_Type (Array_Type) then
8332 Set_Etype (N, Component_Type (Array_Type));
8333 else
8334 return;
8335 end if;
996ae0b0
RK
8336
8337 Index := First_Index (Array_Type);
8338 Expr := First (Expressions (N));
8339
758c442c
GD
8340 -- The prefix may have resolved to a string literal, in which case its
8341 -- etype has a special representation. This is only possible currently
8342 -- if the prefix is a static concatenation, written in functional
8343 -- notation.
996ae0b0
RK
8344
8345 if Ekind (Array_Type) = E_String_Literal_Subtype then
8346 Resolve (Expr, Standard_Positive);
8347
8348 else
8349 while Present (Index) and Present (Expr) loop
8350 Resolve (Expr, Etype (Index));
8351 Check_Unset_Reference (Expr);
8352
8353 if Is_Scalar_Type (Etype (Expr)) then
8354 Apply_Scalar_Range_Check (Expr, Etype (Index));
8355 else
8356 Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
8357 end if;
8358
8359 Next_Index (Index);
8360 Next (Expr);
8361 end loop;
8362 end if;
8363
dec6faf1
AC
8364 Analyze_Dimension (N);
8365
0669bebe
GB
8366 -- Do not generate the warning on suspicious index if we are analyzing
8367 -- package Ada.Tags; otherwise we will report the warning with the
8368 -- Prims_Ptr field of the dispatch table.
8369
8370 if Scope (Etype (Prefix (N))) = Standard_Standard
8371 or else not
8372 Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
8373 Ada_Tags)
8374 then
8375 Warn_On_Suspicious_Index (Name, First (Expressions (N)));
8376 Eval_Indexed_Component (N);
8377 end if;
c28408b7 8378
c2a2dbcc
RD
8379 -- If the array type is atomic, and the component is not atomic, then
8380 -- this is worth a warning, since we have a situation where the access
8381 -- to the component may cause extra read/writes of the atomic array
8382 -- object, or partial word accesses, which could be unexpected.
c28408b7
RD
8383
8384 if Nkind (N) = N_Indexed_Component
c2a2dbcc
RD
8385 and then Is_Atomic_Ref_With_Address (N)
8386 and then not (Has_Atomic_Components (Array_Type)
8387 or else (Is_Entity_Name (Prefix (N))
8388 and then Has_Atomic_Components
8389 (Entity (Prefix (N)))))
8390 and then not Is_Atomic (Component_Type (Array_Type))
c28408b7 8391 then
b6dd03dd
ES
8392 Error_Msg_N
8393 ("??access to non-atomic component of atomic array", Prefix (N));
8394 Error_Msg_N
8395 ("??\may cause unexpected accesses to atomic object", Prefix (N));
c28408b7 8396 end if;
996ae0b0
RK
8397 end Resolve_Indexed_Component;
8398
8399 -----------------------------
8400 -- Resolve_Integer_Literal --
8401 -----------------------------
8402
8403 procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
8404 begin
8405 Set_Etype (N, Typ);
8406 Eval_Integer_Literal (N);
8407 end Resolve_Integer_Literal;
8408
15ce9ca2
AC
8409 --------------------------------
8410 -- Resolve_Intrinsic_Operator --
8411 --------------------------------
996ae0b0
RK
8412
8413 procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id) is
7a5b62b0
AC
8414 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
8415 Op : Entity_Id;
8416 Arg1 : Node_Id;
8417 Arg2 : Node_Id;
996ae0b0 8418
78efd712
AC
8419 function Convert_Operand (Opnd : Node_Id) return Node_Id;
8420 -- If the operand is a literal, it cannot be the expression in a
8421 -- conversion. Use a qualified expression instead.
8422
b6dd03dd
ES
8423 ---------------------
8424 -- Convert_Operand --
8425 ---------------------
8426
78efd712
AC
8427 function Convert_Operand (Opnd : Node_Id) return Node_Id is
8428 Loc : constant Source_Ptr := Sloc (Opnd);
8429 Res : Node_Id;
b6dd03dd 8430
78efd712
AC
8431 begin
8432 if Nkind_In (Opnd, N_Integer_Literal, N_Real_Literal) then
8433 Res :=
8434 Make_Qualified_Expression (Loc,
8435 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
8436 Expression => Relocate_Node (Opnd));
8437 Analyze (Res);
8438
8439 else
8440 Res := Unchecked_Convert_To (Btyp, Opnd);
8441 end if;
8442
8443 return Res;
8444 end Convert_Operand;
8445
d72e7628 8446 -- Start of processing for Resolve_Intrinsic_Operator
7109f4f5 8447
996ae0b0 8448 begin
305caf42
AC
8449 -- We must preserve the original entity in a generic setting, so that
8450 -- the legality of the operation can be verified in an instance.
8451
4460a9bc 8452 if not Expander_Active then
305caf42
AC
8453 return;
8454 end if;
8455
996ae0b0 8456 Op := Entity (N);
996ae0b0
RK
8457 while Scope (Op) /= Standard_Standard loop
8458 Op := Homonym (Op);
8459 pragma Assert (Present (Op));
8460 end loop;
8461
8462 Set_Entity (N, Op);
af152989 8463 Set_Is_Overloaded (N, False);
996ae0b0 8464
7109f4f5
AC
8465 -- If the result or operand types are private, rewrite with unchecked
8466 -- conversions on the operands and the result, to expose the proper
8467 -- underlying numeric type.
996ae0b0 8468
7109f4f5
AC
8469 if Is_Private_Type (Typ)
8470 or else Is_Private_Type (Etype (Left_Opnd (N)))
8471 or else Is_Private_Type (Etype (Right_Opnd (N)))
8472 then
78efd712 8473 Arg1 := Convert_Operand (Left_Opnd (N));
fbf5a39b
AC
8474
8475 if Nkind (N) = N_Op_Expon then
8476 Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
8477 else
78efd712 8478 Arg2 := Convert_Operand (Right_Opnd (N));
fbf5a39b
AC
8479 end if;
8480
bb481772
AC
8481 if Nkind (Arg1) = N_Type_Conversion then
8482 Save_Interps (Left_Opnd (N), Expression (Arg1));
8483 end if;
8484
8485 if Nkind (Arg2) = N_Type_Conversion then
8486 Save_Interps (Right_Opnd (N), Expression (Arg2));
8487 end if;
996ae0b0 8488
fbf5a39b
AC
8489 Set_Left_Opnd (N, Arg1);
8490 Set_Right_Opnd (N, Arg2);
8491
8492 Set_Etype (N, Btyp);
8493 Rewrite (N, Unchecked_Convert_To (Typ, N));
8494 Resolve (N, Typ);
8495
8496 elsif Typ /= Etype (Left_Opnd (N))
8497 or else Typ /= Etype (Right_Opnd (N))
8498 then
d81b4bfe 8499 -- Add explicit conversion where needed, and save interpretations in
7a5b62b0 8500 -- case operands are overloaded.
fbf5a39b 8501
af152989 8502 Arg1 := Convert_To (Typ, Left_Opnd (N));
fbf5a39b
AC
8503 Arg2 := Convert_To (Typ, Right_Opnd (N));
8504
8505 if Nkind (Arg1) = N_Type_Conversion then
8506 Save_Interps (Left_Opnd (N), Expression (Arg1));
af152989
AC
8507 else
8508 Save_Interps (Left_Opnd (N), Arg1);
fbf5a39b
AC
8509 end if;
8510
8511 if Nkind (Arg2) = N_Type_Conversion then
8512 Save_Interps (Right_Opnd (N), Expression (Arg2));
af152989 8513 else
0ab80019 8514 Save_Interps (Right_Opnd (N), Arg2);
fbf5a39b
AC
8515 end if;
8516
8517 Rewrite (Left_Opnd (N), Arg1);
8518 Rewrite (Right_Opnd (N), Arg2);
8519 Analyze (Arg1);
8520 Analyze (Arg2);
8521 Resolve_Arithmetic_Op (N, Typ);
8522
8523 else
8524 Resolve_Arithmetic_Op (N, Typ);
8525 end if;
996ae0b0
RK
8526 end Resolve_Intrinsic_Operator;
8527
fbf5a39b
AC
8528 --------------------------------------
8529 -- Resolve_Intrinsic_Unary_Operator --
8530 --------------------------------------
8531
8532 procedure Resolve_Intrinsic_Unary_Operator
8533 (N : Node_Id;
8534 Typ : Entity_Id)
8535 is
8536 Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
8537 Op : Entity_Id;
8538 Arg2 : Node_Id;
8539
8540 begin
8541 Op := Entity (N);
fbf5a39b
AC
8542 while Scope (Op) /= Standard_Standard loop
8543 Op := Homonym (Op);
8544 pragma Assert (Present (Op));
8545 end loop;
8546
8547 Set_Entity (N, Op);
8548
8549 if Is_Private_Type (Typ) then
8550 Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
8551 Save_Interps (Right_Opnd (N), Expression (Arg2));
8552
8553 Set_Right_Opnd (N, Arg2);
8554
8555 Set_Etype (N, Btyp);
8556 Rewrite (N, Unchecked_Convert_To (Typ, N));
8557 Resolve (N, Typ);
8558
8559 else
8560 Resolve_Unary_Op (N, Typ);
8561 end if;
8562 end Resolve_Intrinsic_Unary_Operator;
8563
996ae0b0
RK
8564 ------------------------
8565 -- Resolve_Logical_Op --
8566 ------------------------
8567
8568 procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
8569 B_Typ : Entity_Id;
8570
8571 begin
f61580d4
AC
8572 Check_No_Direct_Boolean_Operators (N);
8573
758c442c
GD
8574 -- Predefined operations on scalar types yield the base type. On the
8575 -- other hand, logical operations on arrays yield the type of the
8576 -- arguments (and the context).
996ae0b0
RK
8577
8578 if Is_Array_Type (Typ) then
8579 B_Typ := Typ;
8580 else
8581 B_Typ := Base_Type (Typ);
8582 end if;
8583
8584 -- The following test is required because the operands of the operation
8585 -- may be literals, in which case the resulting type appears to be
8586 -- compatible with a signed integer type, when in fact it is compatible
8587 -- only with modular types. If the context itself is universal, the
8588 -- operation is illegal.
8589
7a5b62b0 8590 if not Valid_Boolean_Arg (Typ) then
996ae0b0
RK
8591 Error_Msg_N ("invalid context for logical operation", N);
8592 Set_Etype (N, Any_Type);
8593 return;
8594
8595 elsif Typ = Any_Modular then
8596 Error_Msg_N
8597 ("no modular type available in this context", N);
8598 Set_Etype (N, Any_Type);
8599 return;
19fb051c 8600
07fc65c4
GB
8601 elsif Is_Modular_Integer_Type (Typ)
8602 and then Etype (Left_Opnd (N)) = Universal_Integer
8603 and then Etype (Right_Opnd (N)) = Universal_Integer
8604 then
8605 Check_For_Visible_Operator (N, B_Typ);
996ae0b0
RK
8606 end if;
8607
f2d10a02
AC
8608 -- Replace AND by AND THEN, or OR by OR ELSE, if Short_Circuit_And_Or
8609 -- is active and the result type is standard Boolean (do not mess with
8610 -- ops that return a nonstandard Boolean type, because something strange
8611 -- is going on).
8612
8613 -- Note: you might expect this replacement to be done during expansion,
8614 -- but that doesn't work, because when the pragma Short_Circuit_And_Or
8615 -- is used, no part of the right operand of an "and" or "or" operator
8616 -- should be executed if the left operand would short-circuit the
8617 -- evaluation of the corresponding "and then" or "or else". If we left
8618 -- the replacement to expansion time, then run-time checks associated
8619 -- with such operands would be evaluated unconditionally, due to being
af89615f 8620 -- before the condition prior to the rewriting as short-circuit forms
f2d10a02
AC
8621 -- during expansion.
8622
8623 if Short_Circuit_And_Or
8624 and then B_Typ = Standard_Boolean
8625 and then Nkind_In (N, N_Op_And, N_Op_Or)
8626 then
0566484a
AC
8627 -- Mark the corresponding putative SCO operator as truly a logical
8628 -- (and short-circuit) operator.
8629
8630 if Generate_SCO and then Comes_From_Source (N) then
8631 Set_SCO_Logical_Operator (N);
8632 end if;
8633
f2d10a02
AC
8634 if Nkind (N) = N_Op_And then
8635 Rewrite (N,
8636 Make_And_Then (Sloc (N),
8637 Left_Opnd => Relocate_Node (Left_Opnd (N)),
8638 Right_Opnd => Relocate_Node (Right_Opnd (N))));
8639 Analyze_And_Resolve (N, B_Typ);
8640
8641 -- Case of OR changed to OR ELSE
8642
8643 else
8644 Rewrite (N,
8645 Make_Or_Else (Sloc (N),
8646 Left_Opnd => Relocate_Node (Left_Opnd (N)),
8647 Right_Opnd => Relocate_Node (Right_Opnd (N))));
8648 Analyze_And_Resolve (N, B_Typ);
8649 end if;
8650
8651 -- Return now, since analysis of the rewritten ops will take care of
8652 -- other reference bookkeeping and expression folding.
8653
8654 return;
8655 end if;
8656
996ae0b0
RK
8657 Resolve (Left_Opnd (N), B_Typ);
8658 Resolve (Right_Opnd (N), B_Typ);
8659
8660 Check_Unset_Reference (Left_Opnd (N));
8661 Check_Unset_Reference (Right_Opnd (N));
8662
8663 Set_Etype (N, B_Typ);
fbf5a39b 8664 Generate_Operator_Reference (N, B_Typ);
996ae0b0 8665 Eval_Logical_Op (N);
9f90d123 8666
2ba431e5
YM
8667 -- In SPARK, logical operations AND, OR and XOR for arrays are defined
8668 -- only when both operands have same static lower and higher bounds. Of
8669 -- course the types have to match, so only check if operands are
8670 -- compatible and the node itself has no errors.
9f90d123 8671
f5afb270
AC
8672 if Is_Array_Type (B_Typ)
8673 and then Nkind (N) in N_Binary_Op
8674 then
8675 declare
8676 Left_Typ : constant Node_Id := Etype (Left_Opnd (N));
8677 Right_Typ : constant Node_Id := Etype (Right_Opnd (N));
2598ee6d 8678
f5afb270 8679 begin
7b98672f
YM
8680 -- Protect call to Matching_Static_Array_Bounds to avoid costly
8681 -- operation if not needed.
8682
6480338a 8683 if Restriction_Check_Required (SPARK_05)
7b98672f 8684 and then Base_Type (Left_Typ) = Base_Type (Right_Typ)
f5afb270
AC
8685 and then Left_Typ /= Any_Composite -- or Left_Opnd in error
8686 and then Right_Typ /= Any_Composite -- or Right_Opnd in error
8687 and then not Matching_Static_Array_Bounds (Left_Typ, Right_Typ)
8688 then
ce5ba43a 8689 Check_SPARK_05_Restriction
f5afb270
AC
8690 ("array types should have matching static bounds", N);
8691 end if;
8692 end;
8693 end if;
996ae0b0
RK
8694 end Resolve_Logical_Op;
8695
8696 ---------------------------
8697 -- Resolve_Membership_Op --
8698 ---------------------------
8699
5cc9353d
RD
8700 -- The context can only be a boolean type, and does not determine the
8701 -- arguments. Arguments should be unambiguous, but the preference rule for
8702 -- universal types applies.
996ae0b0
RK
8703
8704 procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
07fc65c4
GB
8705 pragma Warnings (Off, Typ);
8706
197e4514 8707 L : constant Node_Id := Left_Opnd (N);
b1c11e0e 8708 R : constant Node_Id := Right_Opnd (N);
996ae0b0
RK
8709 T : Entity_Id;
8710
197e4514 8711 procedure Resolve_Set_Membership;
5cc9353d
RD
8712 -- Analysis has determined a unique type for the left operand. Use it to
8713 -- resolve the disjuncts.
197e4514
AC
8714
8715 ----------------------------
8716 -- Resolve_Set_Membership --
8717 ----------------------------
8718
8719 procedure Resolve_Set_Membership is
9cb62ce3 8720 Alt : Node_Id;
cd1a470a 8721 Ltyp : Entity_Id;
197e4514
AC
8722
8723 begin
cd1a470a
AC
8724 -- If the left operand is overloaded, find type compatible with not
8725 -- overloaded alternative of the right operand.
8726
8727 if Is_Overloaded (L) then
8728 Ltyp := Empty;
8729 Alt := First (Alternatives (N));
8730 while Present (Alt) loop
8731 if not Is_Overloaded (Alt) then
8732 Ltyp := Intersect_Types (L, Alt);
8733 exit;
8734 else
8735 Next (Alt);
8736 end if;
8737 end loop;
8738
8739 -- Unclear how to resolve expression if all alternatives are also
8740 -- overloaded.
8741
8742 if No (Ltyp) then
8743 Error_Msg_N ("ambiguous expression", N);
8744 end if;
8745
8746 else
8747 Ltyp := Etype (L);
8748 end if;
8749
9cb62ce3 8750 Resolve (L, Ltyp);
197e4514
AC
8751
8752 Alt := First (Alternatives (N));
8753 while Present (Alt) loop
8754
8755 -- Alternative is an expression, a range
8756 -- or a subtype mark.
8757
8758 if not Is_Entity_Name (Alt)
8759 or else not Is_Type (Entity (Alt))
8760 then
9cb62ce3 8761 Resolve (Alt, Ltyp);
197e4514
AC
8762 end if;
8763
8764 Next (Alt);
8765 end loop;
9cb62ce3
AC
8766
8767 -- Check for duplicates for discrete case
8768
8769 if Is_Discrete_Type (Ltyp) then
8770 declare
8771 type Ent is record
8772 Alt : Node_Id;
8773 Val : Uint;
8774 end record;
8775
8776 Alts : array (0 .. List_Length (Alternatives (N))) of Ent;
8777 Nalts : Nat;
8778
8779 begin
8780 -- Loop checking duplicates. This is quadratic, but giant sets
8781 -- are unlikely in this context so it's a reasonable choice.
8782
8783 Nalts := 0;
8784 Alt := First (Alternatives (N));
8785 while Present (Alt) loop
edab6088 8786 if Is_OK_Static_Expression (Alt)
9cb62ce3 8787 and then (Nkind_In (Alt, N_Integer_Literal,
324ac540 8788 N_Character_Literal)
9cb62ce3
AC
8789 or else Nkind (Alt) in N_Has_Entity)
8790 then
8791 Nalts := Nalts + 1;
8792 Alts (Nalts) := (Alt, Expr_Value (Alt));
8793
8794 for J in 1 .. Nalts - 1 loop
8795 if Alts (J).Val = Alts (Nalts).Val then
8796 Error_Msg_Sloc := Sloc (Alts (J).Alt);
324ac540 8797 Error_Msg_N ("duplicate of value given#??", Alt);
9cb62ce3
AC
8798 end if;
8799 end loop;
8800 end if;
8801
8802 Alt := Next (Alt);
8803 end loop;
8804 end;
8805 end if;
197e4514
AC
8806 end Resolve_Set_Membership;
8807
442c0581 8808 -- Start of processing for Resolve_Membership_Op
197e4514 8809
996ae0b0
RK
8810 begin
8811 if L = Error or else R = Error then
8812 return;
8813 end if;
8814
197e4514
AC
8815 if Present (Alternatives (N)) then
8816 Resolve_Set_Membership;
edab6088 8817 goto SM_Exit;
197e4514
AC
8818
8819 elsif not Is_Overloaded (R)
996ae0b0 8820 and then
19fb051c
AC
8821 (Etype (R) = Universal_Integer
8822 or else
996ae0b0
RK
8823 Etype (R) = Universal_Real)
8824 and then Is_Overloaded (L)
8825 then
8826 T := Etype (R);
1420b484 8827
d81b4bfe 8828 -- Ada 2005 (AI-251): Support the following case:
1420b484
JM
8829
8830 -- type I is interface;
8831 -- type T is tagged ...
8832
c8ef728f 8833 -- function Test (O : I'Class) is
1420b484
JM
8834 -- begin
8835 -- return O in T'Class.
8836 -- end Test;
8837
d81b4bfe 8838 -- In this case we have nothing else to do. The membership test will be
e7c0dd39 8839 -- done at run time.
1420b484 8840
0791fbe9 8841 elsif Ada_Version >= Ada_2005
1420b484
JM
8842 and then Is_Class_Wide_Type (Etype (L))
8843 and then Is_Interface (Etype (L))
8844 and then Is_Class_Wide_Type (Etype (R))
8845 and then not Is_Interface (Etype (R))
8846 then
8847 return;
996ae0b0
RK
8848 else
8849 T := Intersect_Types (L, R);
8850 end if;
8851
9a0ddeee
AC
8852 -- If mixed-mode operations are present and operands are all literal,
8853 -- the only interpretation involves Duration, which is probably not
8854 -- the intention of the programmer.
8855
8856 if T = Any_Fixed then
8857 T := Unique_Fixed_Point_Type (N);
8858
8859 if T = Any_Type then
8860 return;
8861 end if;
8862 end if;
8863
996ae0b0
RK
8864 Resolve (L, T);
8865 Check_Unset_Reference (L);
8866
8867 if Nkind (R) = N_Range
8868 and then not Is_Scalar_Type (T)
8869 then
8870 Error_Msg_N ("scalar type required for range", R);
8871 end if;
8872
8873 if Is_Entity_Name (R) then
8874 Freeze_Expression (R);
8875 else
8876 Resolve (R, T);
8877 Check_Unset_Reference (R);
8878 end if;
8879
edab6088
RD
8880 -- Here after resolving membership operation
8881
8882 <<SM_Exit>>
8883
996ae0b0
RK
8884 Eval_Membership_Op (N);
8885 end Resolve_Membership_Op;
8886
8887 ------------------
8888 -- Resolve_Null --
8889 ------------------
8890
8891 procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
b1c11e0e
JM
8892 Loc : constant Source_Ptr := Sloc (N);
8893
996ae0b0 8894 begin
758c442c 8895 -- Handle restriction against anonymous null access values This
6ba6b1e3 8896 -- restriction can be turned off using -gnatdj.
996ae0b0 8897
0ab80019 8898 -- Ada 2005 (AI-231): Remove restriction
2820d220 8899
0791fbe9 8900 if Ada_Version < Ada_2005
2820d220 8901 and then not Debug_Flag_J
996ae0b0
RK
8902 and then Ekind (Typ) = E_Anonymous_Access_Type
8903 and then Comes_From_Source (N)
8904 then
d81b4bfe
TQ
8905 -- In the common case of a call which uses an explicitly null value
8906 -- for an access parameter, give specialized error message.
996ae0b0 8907
d3b00ce3 8908 if Nkind (Parent (N)) in N_Subprogram_Call then
996ae0b0
RK
8909 Error_Msg_N
8910 ("null is not allowed as argument for an access parameter", N);
8911
8912 -- Standard message for all other cases (are there any?)
8913
8914 else
8915 Error_Msg_N
8916 ("null cannot be of an anonymous access type", N);
8917 end if;
8918 end if;
8919
b1c11e0e
JM
8920 -- Ada 2005 (AI-231): Generate the null-excluding check in case of
8921 -- assignment to a null-excluding object
8922
0791fbe9 8923 if Ada_Version >= Ada_2005
b1c11e0e
JM
8924 and then Can_Never_Be_Null (Typ)
8925 and then Nkind (Parent (N)) = N_Assignment_Statement
8926 then
8927 if not Inside_Init_Proc then
8928 Insert_Action
8929 (Compile_Time_Constraint_Error (N,
324ac540 8930 "(Ada 2005) null not allowed in null-excluding objects??"),
b1c11e0e
JM
8931 Make_Raise_Constraint_Error (Loc,
8932 Reason => CE_Access_Check_Failed));
8933 else
8934 Insert_Action (N,
8935 Make_Raise_Constraint_Error (Loc,
8936 Reason => CE_Access_Check_Failed));
8937 end if;
8938 end if;
8939
d81b4bfe
TQ
8940 -- In a distributed context, null for a remote access to subprogram may
8941 -- need to be replaced with a special record aggregate. In this case,
8942 -- return after having done the transformation.
996ae0b0
RK
8943
8944 if (Ekind (Typ) = E_Record_Type
8945 or else Is_Remote_Access_To_Subprogram_Type (Typ))
8946 and then Remote_AST_Null_Value (N, Typ)
8947 then
8948 return;
8949 end if;
8950
a77842bd 8951 -- The null literal takes its type from the context
996ae0b0
RK
8952
8953 Set_Etype (N, Typ);
8954 end Resolve_Null;
8955
8956 -----------------------
8957 -- Resolve_Op_Concat --
8958 -----------------------
8959
8960 procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
996ae0b0 8961
10303118
BD
8962 -- We wish to avoid deep recursion, because concatenations are often
8963 -- deeply nested, as in A&B&...&Z. Therefore, we walk down the left
8964 -- operands nonrecursively until we find something that is not a simple
8965 -- concatenation (A in this case). We resolve that, and then walk back
8966 -- up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
8967 -- to do the rest of the work at each level. The Parent pointers allow
8968 -- us to avoid recursion, and thus avoid running out of memory. See also
d81b4bfe 8969 -- Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
996ae0b0 8970
10303118
BD
8971 NN : Node_Id := N;
8972 Op1 : Node_Id;
996ae0b0 8973
10303118
BD
8974 begin
8975 -- The following code is equivalent to:
996ae0b0 8976
10303118
BD
8977 -- Resolve_Op_Concat_First (NN, Typ);
8978 -- Resolve_Op_Concat_Arg (N, ...);
8979 -- Resolve_Op_Concat_Rest (N, Typ);
996ae0b0 8980
10303118
BD
8981 -- where the Resolve_Op_Concat_Arg call recurses back here if the left
8982 -- operand is a concatenation.
996ae0b0 8983
10303118 8984 -- Walk down left operands
996ae0b0 8985
10303118
BD
8986 loop
8987 Resolve_Op_Concat_First (NN, Typ);
8988 Op1 := Left_Opnd (NN);
8989 exit when not (Nkind (Op1) = N_Op_Concat
8990 and then not Is_Array_Type (Component_Type (Typ))
8991 and then Entity (Op1) = Entity (NN));
8992 NN := Op1;
8993 end loop;
996ae0b0 8994
10303118 8995 -- Now (given the above example) NN is A&B and Op1 is A
996ae0b0 8996
10303118 8997 -- First resolve Op1 ...
9ebe3743 8998
10303118 8999 Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd (NN));
9ebe3743 9000
10303118
BD
9001 -- ... then walk NN back up until we reach N (where we started), calling
9002 -- Resolve_Op_Concat_Rest along the way.
9ebe3743 9003
10303118
BD
9004 loop
9005 Resolve_Op_Concat_Rest (NN, Typ);
9006 exit when NN = N;
9007 NN := Parent (NN);
9008 end loop;
2933b16c 9009
fe5d3068 9010 if Base_Type (Etype (N)) /= Standard_String then
ce5ba43a 9011 Check_SPARK_05_Restriction
fe5d3068 9012 ("result of concatenation should have type String", N);
2933b16c 9013 end if;
10303118 9014 end Resolve_Op_Concat;
9ebe3743 9015
10303118
BD
9016 ---------------------------
9017 -- Resolve_Op_Concat_Arg --
9018 ---------------------------
996ae0b0 9019
10303118
BD
9020 procedure Resolve_Op_Concat_Arg
9021 (N : Node_Id;
9022 Arg : Node_Id;
9023 Typ : Entity_Id;
9024 Is_Comp : Boolean)
9025 is
9026 Btyp : constant Entity_Id := Base_Type (Typ);
668a19bc 9027 Ctyp : constant Entity_Id := Component_Type (Typ);
996ae0b0 9028
10303118
BD
9029 begin
9030 if In_Instance then
9031 if Is_Comp
9032 or else (not Is_Overloaded (Arg)
9033 and then Etype (Arg) /= Any_Composite
668a19bc 9034 and then Covers (Ctyp, Etype (Arg)))
10303118 9035 then
668a19bc 9036 Resolve (Arg, Ctyp);
10303118
BD
9037 else
9038 Resolve (Arg, Btyp);
9039 end if;
fbf5a39b 9040
668a19bc
ES
9041 -- If both Array & Array and Array & Component are visible, there is a
9042 -- potential ambiguity that must be reported.
9043
9044 elsif Has_Compatible_Type (Arg, Ctyp) then
10303118 9045 if Nkind (Arg) = N_Aggregate
668a19bc 9046 and then Is_Composite_Type (Ctyp)
10303118 9047 then
668a19bc 9048 if Is_Private_Type (Ctyp) then
10303118 9049 Resolve (Arg, Btyp);
668a19bc
ES
9050
9051 -- If the operation is user-defined and not overloaded use its
9052 -- profile. The operation may be a renaming, in which case it has
9053 -- been rewritten, and we want the original profile.
9054
9055 elsif not Is_Overloaded (N)
9056 and then Comes_From_Source (Entity (Original_Node (N)))
9057 and then Ekind (Entity (Original_Node (N))) = E_Function
9058 then
9059 Resolve (Arg,
9060 Etype
9061 (Next_Formal (First_Formal (Entity (Original_Node (N))))));
9062 return;
9063
9064 -- Otherwise an aggregate may match both the array type and the
9065 -- component type.
9066
10303118
BD
9067 else
9068 Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
9069 Set_Etype (Arg, Any_Type);
996ae0b0
RK
9070 end if;
9071
9072 else
10303118
BD
9073 if Is_Overloaded (Arg)
9074 and then Has_Compatible_Type (Arg, Typ)
9075 and then Etype (Arg) /= Any_Type
9076 then
9077 declare
9078 I : Interp_Index;
9079 It : Interp;
9080 Func : Entity_Id;
9081
9082 begin
9083 Get_First_Interp (Arg, I, It);
9084 Func := It.Nam;
9085 Get_Next_Interp (I, It);
9086
9087 -- Special-case the error message when the overloading is
9088 -- caused by a function that yields an array and can be
9089 -- called without parameters.
9090
9091 if It.Nam = Func then
9092 Error_Msg_Sloc := Sloc (Func);
9093 Error_Msg_N ("ambiguous call to function#", Arg);
9094 Error_Msg_NE
9095 ("\\interpretation as call yields&", Arg, Typ);
9096 Error_Msg_NE
9097 ("\\interpretation as indexing of call yields&",
9098 Arg, Component_Type (Typ));
9099
9100 else
668a19bc 9101 Error_Msg_N ("ambiguous operand for concatenation!", Arg);
19fb051c 9102
10303118
BD
9103 Get_First_Interp (Arg, I, It);
9104 while Present (It.Nam) loop
9105 Error_Msg_Sloc := Sloc (It.Nam);
9106
668a19bc
ES
9107 if Base_Type (It.Typ) = Btyp
9108 or else
9109 Base_Type (It.Typ) = Base_Type (Ctyp)
10303118 9110 then
4e7a4f6e
AC
9111 Error_Msg_N -- CODEFIX
9112 ("\\possible interpretation#", Arg);
10303118
BD
9113 end if;
9114
9115 Get_Next_Interp (I, It);
9116 end loop;
9117 end if;
9118 end;
9119 end if;
9120
9121 Resolve (Arg, Component_Type (Typ));
9122
9123 if Nkind (Arg) = N_String_Literal then
9124 Set_Etype (Arg, Component_Type (Typ));
9125 end if;
9126
9127 if Arg = Left_Opnd (N) then
9128 Set_Is_Component_Left_Opnd (N);
9129 else
9130 Set_Is_Component_Right_Opnd (N);
9131 end if;
996ae0b0
RK
9132 end if;
9133
10303118
BD
9134 else
9135 Resolve (Arg, Btyp);
9136 end if;
9137
2ba431e5 9138 -- Concatenation is restricted in SPARK: each operand must be either a
92e77027
AC
9139 -- string literal, the name of a string constant, a static character or
9140 -- string expression, or another concatenation. Arg cannot be a
9141 -- concatenation here as callers of Resolve_Op_Concat_Arg call it
9142 -- separately on each final operand, past concatenation operations.
2933b16c 9143
fe5d3068 9144 if Is_Character_Type (Etype (Arg)) then
edab6088 9145 if not Is_OK_Static_Expression (Arg) then
ce5ba43a 9146 Check_SPARK_05_Restriction
5b5588dd 9147 ("character operand for concatenation should be static", Arg);
fe5d3068 9148 end if;
2933b16c 9149
fe5d3068 9150 elsif Is_String_Type (Etype (Arg)) then
92e77027
AC
9151 if not (Nkind_In (Arg, N_Identifier, N_Expanded_Name)
9152 and then Is_Constant_Object (Entity (Arg)))
edab6088 9153 and then not Is_OK_Static_Expression (Arg)
92e77027 9154 then
ce5ba43a 9155 Check_SPARK_05_Restriction
5b5588dd 9156 ("string operand for concatenation should be static", Arg);
fe5d3068 9157 end if;
2933b16c 9158
b9e48541
AC
9159 -- Do not issue error on an operand that is neither a character nor a
9160 -- string, as the error is issued in Resolve_Op_Concat.
2933b16c 9161
fe5d3068
YM
9162 else
9163 null;
2933b16c
RD
9164 end if;
9165
10303118
BD
9166 Check_Unset_Reference (Arg);
9167 end Resolve_Op_Concat_Arg;
996ae0b0 9168
10303118
BD
9169 -----------------------------
9170 -- Resolve_Op_Concat_First --
9171 -----------------------------
9172
9173 procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
9174 Btyp : constant Entity_Id := Base_Type (Typ);
9175 Op1 : constant Node_Id := Left_Opnd (N);
9176 Op2 : constant Node_Id := Right_Opnd (N);
996ae0b0
RK
9177
9178 begin
dae2b8ea
HK
9179 -- The parser folds an enormous sequence of concatenations of string
9180 -- literals into "" & "...", where the Is_Folded_In_Parser flag is set
4fc26524 9181 -- in the right operand. If the expression resolves to a predefined "&"
dae2b8ea
HK
9182 -- operator, all is well. Otherwise, the parser's folding is wrong, so
9183 -- we give an error. See P_Simple_Expression in Par.Ch4.
9184
9185 if Nkind (Op2) = N_String_Literal
9186 and then Is_Folded_In_Parser (Op2)
9187 and then Ekind (Entity (N)) = E_Function
9188 then
9189 pragma Assert (Nkind (Op1) = N_String_Literal -- should be ""
9190 and then String_Length (Strval (Op1)) = 0);
9191 Error_Msg_N ("too many user-defined concatenations", N);
9192 return;
9193 end if;
9194
996ae0b0
RK
9195 Set_Etype (N, Btyp);
9196
9197 if Is_Limited_Composite (Btyp) then
9198 Error_Msg_N ("concatenation not available for limited array", N);
fbf5a39b 9199 Explain_Limited_Type (Btyp, N);
996ae0b0 9200 end if;
10303118 9201 end Resolve_Op_Concat_First;
996ae0b0 9202
10303118
BD
9203 ----------------------------
9204 -- Resolve_Op_Concat_Rest --
9205 ----------------------------
996ae0b0 9206
10303118
BD
9207 procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
9208 Op1 : constant Node_Id := Left_Opnd (N);
9209 Op2 : constant Node_Id := Right_Opnd (N);
996ae0b0 9210
10303118
BD
9211 begin
9212 Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd (N));
996ae0b0 9213
fbf5a39b 9214 Generate_Operator_Reference (N, Typ);
996ae0b0
RK
9215
9216 if Is_String_Type (Typ) then
9217 Eval_Concatenation (N);
9218 end if;
9219
d81b4bfe
TQ
9220 -- If this is not a static concatenation, but the result is a string
9221 -- type (and not an array of strings) ensure that static string operands
9222 -- have their subtypes properly constructed.
996ae0b0
RK
9223
9224 if Nkind (N) /= N_String_Literal
9225 and then Is_Character_Type (Component_Type (Typ))
9226 then
9227 Set_String_Literal_Subtype (Op1, Typ);
9228 Set_String_Literal_Subtype (Op2, Typ);
9229 end if;
10303118 9230 end Resolve_Op_Concat_Rest;
996ae0b0
RK
9231
9232 ----------------------
9233 -- Resolve_Op_Expon --
9234 ----------------------
9235
9236 procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
9237 B_Typ : constant Entity_Id := Base_Type (Typ);
9238
9239 begin
f3d57416 9240 -- Catch attempts to do fixed-point exponentiation with universal
758c442c 9241 -- operands, which is a case where the illegality is not caught during
4530b919
AC
9242 -- normal operator analysis. This is not done in preanalysis mode
9243 -- since the tree is not fully decorated during preanalysis.
996ae0b0 9244
4530b919
AC
9245 if Full_Analysis then
9246 if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
9247 Error_Msg_N ("exponentiation not available for fixed point", N);
9248 return;
4d792549 9249
4530b919
AC
9250 elsif Nkind (Parent (N)) in N_Op
9251 and then Is_Fixed_Point_Type (Etype (Parent (N)))
9252 and then Etype (N) = Universal_Real
9253 and then Comes_From_Source (N)
9254 then
9255 Error_Msg_N ("exponentiation not available for fixed point", N);
9256 return;
9257 end if;
996ae0b0
RK
9258 end if;
9259
fbf5a39b
AC
9260 if Comes_From_Source (N)
9261 and then Ekind (Entity (N)) = E_Function
9262 and then Is_Imported (Entity (N))
9263 and then Is_Intrinsic_Subprogram (Entity (N))
9264 then
9265 Resolve_Intrinsic_Operator (N, Typ);
9266 return;
9267 end if;
9268
996ae0b0
RK
9269 if Etype (Left_Opnd (N)) = Universal_Integer
9270 or else Etype (Left_Opnd (N)) = Universal_Real
9271 then
9272 Check_For_Visible_Operator (N, B_Typ);
9273 end if;
9274
9275 -- We do the resolution using the base type, because intermediate values
4530b919 9276 -- in expressions are always of the base type, not a subtype of it.
996ae0b0
RK
9277
9278 Resolve (Left_Opnd (N), B_Typ);
9279 Resolve (Right_Opnd (N), Standard_Integer);
9280
7dbd3de9
RD
9281 -- For integer types, right argument must be in Natural range
9282
9283 if Is_Integer_Type (Typ) then
9284 Apply_Scalar_Range_Check (Right_Opnd (N), Standard_Natural);
9285 end if;
9286
996ae0b0
RK
9287 Check_Unset_Reference (Left_Opnd (N));
9288 Check_Unset_Reference (Right_Opnd (N));
9289
9290 Set_Etype (N, B_Typ);
fbf5a39b 9291 Generate_Operator_Reference (N, B_Typ);
dec6faf1
AC
9292
9293 Analyze_Dimension (N);
9294
15954beb 9295 if Ada_Version >= Ada_2012 and then Has_Dimension_System (B_Typ) then
6c57023b 9296 -- Evaluate the exponentiation operator for dimensioned type
dec6faf1 9297
6c57023b
AC
9298 Eval_Op_Expon_For_Dimensioned_Type (N, B_Typ);
9299 else
9300 Eval_Op_Expon (N);
dec6faf1
AC
9301 end if;
9302
996ae0b0
RK
9303 -- Set overflow checking bit. Much cleverer code needed here eventually
9304 -- and perhaps the Resolve routines should be separated for the various
9305 -- arithmetic operations, since they will need different processing. ???
9306
9307 if Nkind (N) in N_Op then
9308 if not Overflow_Checks_Suppressed (Etype (N)) then
fbf5a39b 9309 Enable_Overflow_Check (N);
996ae0b0
RK
9310 end if;
9311 end if;
996ae0b0
RK
9312 end Resolve_Op_Expon;
9313
9314 --------------------
9315 -- Resolve_Op_Not --
9316 --------------------
9317
9318 procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
9319 B_Typ : Entity_Id;
9320
9321 function Parent_Is_Boolean return Boolean;
5cc9353d
RD
9322 -- This function determines if the parent node is a boolean operator or
9323 -- operation (comparison op, membership test, or short circuit form) and
9324 -- the not in question is the left operand of this operation. Note that
9325 -- if the not is in parens, then false is returned.
996ae0b0 9326
aa180613
RD
9327 -----------------------
9328 -- Parent_Is_Boolean --
9329 -----------------------
9330
996ae0b0
RK
9331 function Parent_Is_Boolean return Boolean is
9332 begin
9333 if Paren_Count (N) /= 0 then
9334 return False;
9335
9336 else
9337 case Nkind (Parent (N)) is
9338 when N_Op_And |
9339 N_Op_Eq |
9340 N_Op_Ge |
9341 N_Op_Gt |
9342 N_Op_Le |
9343 N_Op_Lt |
9344 N_Op_Ne |
9345 N_Op_Or |
9346 N_Op_Xor |
9347 N_In |
9348 N_Not_In |
9349 N_And_Then |
aa180613 9350 N_Or_Else =>
996ae0b0
RK
9351
9352 return Left_Opnd (Parent (N)) = N;
9353
9354 when others =>
9355 return False;
9356 end case;
9357 end if;
9358 end Parent_Is_Boolean;
9359
9360 -- Start of processing for Resolve_Op_Not
9361
9362 begin
758c442c
GD
9363 -- Predefined operations on scalar types yield the base type. On the
9364 -- other hand, logical operations on arrays yield the type of the
9365 -- arguments (and the context).
996ae0b0
RK
9366
9367 if Is_Array_Type (Typ) then
9368 B_Typ := Typ;
9369 else
9370 B_Typ := Base_Type (Typ);
9371 end if;
9372
f3d57416 9373 -- Straightforward case of incorrect arguments
aa180613 9374
7a5b62b0 9375 if not Valid_Boolean_Arg (Typ) then
996ae0b0
RK
9376 Error_Msg_N ("invalid operand type for operator&", N);
9377 Set_Etype (N, Any_Type);
9378 return;
9379
aa180613
RD
9380 -- Special case of probable missing parens
9381
fbf5a39b 9382 elsif Typ = Universal_Integer or else Typ = Any_Modular then
996ae0b0 9383 if Parent_Is_Boolean then
ed2233dc 9384 Error_Msg_N
996ae0b0
RK
9385 ("operand of not must be enclosed in parentheses",
9386 Right_Opnd (N));
9387 else
9388 Error_Msg_N
9389 ("no modular type available in this context", N);
9390 end if;
9391
9392 Set_Etype (N, Any_Type);
9393 return;
9394
5cc9353d 9395 -- OK resolution of NOT
aa180613 9396
996ae0b0 9397 else
aa180613
RD
9398 -- Warn if non-boolean types involved. This is a case like not a < b
9399 -- where a and b are modular, where we will get (not a) < b and most
9400 -- likely not (a < b) was intended.
9401
9402 if Warn_On_Questionable_Missing_Parens
9403 and then not Is_Boolean_Type (Typ)
996ae0b0
RK
9404 and then Parent_Is_Boolean
9405 then
324ac540 9406 Error_Msg_N ("?q?not expression should be parenthesized here!", N);
996ae0b0
RK
9407 end if;
9408
09bc9ab6
RD
9409 -- Warn on double negation if checking redundant constructs
9410
9411 if Warn_On_Redundant_Constructs
9412 and then Comes_From_Source (N)
9413 and then Comes_From_Source (Right_Opnd (N))
9414 and then Root_Type (Typ) = Standard_Boolean
9415 and then Nkind (Right_Opnd (N)) = N_Op_Not
9416 then
324ac540 9417 Error_Msg_N ("redundant double negation?r?", N);
09bc9ab6
RD
9418 end if;
9419
9420 -- Complete resolution and evaluation of NOT
9421
996ae0b0
RK
9422 Resolve (Right_Opnd (N), B_Typ);
9423 Check_Unset_Reference (Right_Opnd (N));
9424 Set_Etype (N, B_Typ);
fbf5a39b 9425 Generate_Operator_Reference (N, B_Typ);
996ae0b0
RK
9426 Eval_Op_Not (N);
9427 end if;
9428 end Resolve_Op_Not;
9429
9430 -----------------------------
9431 -- Resolve_Operator_Symbol --
9432 -----------------------------
9433
9434 -- Nothing to be done, all resolved already
9435
9436 procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
07fc65c4
GB
9437 pragma Warnings (Off, N);
9438 pragma Warnings (Off, Typ);
9439
996ae0b0
RK
9440 begin
9441 null;
9442 end Resolve_Operator_Symbol;
9443
9444 ----------------------------------
9445 -- Resolve_Qualified_Expression --
9446 ----------------------------------
9447
9448 procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
07fc65c4
GB
9449 pragma Warnings (Off, Typ);
9450
996ae0b0
RK
9451 Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
9452 Expr : constant Node_Id := Expression (N);
9453
9454 begin
9455 Resolve (Expr, Target_Typ);
9456
7b98672f
YM
9457 -- Protect call to Matching_Static_Array_Bounds to avoid costly
9458 -- operation if not needed.
9459
6480338a 9460 if Restriction_Check_Required (SPARK_05)
7b98672f 9461 and then Is_Array_Type (Target_Typ)
b0186f71 9462 and then Is_Array_Type (Etype (Expr))
db72f10a 9463 and then Etype (Expr) /= Any_Composite -- or else Expr in error
b0186f71
AC
9464 and then not Matching_Static_Array_Bounds (Target_Typ, Etype (Expr))
9465 then
ce5ba43a 9466 Check_SPARK_05_Restriction
fe5d3068 9467 ("array types should have matching static bounds", N);
b0186f71
AC
9468 end if;
9469
5cc9353d
RD
9470 -- A qualified expression requires an exact match of the type, class-
9471 -- wide matching is not allowed. However, if the qualifying type is
9472 -- specific and the expression has a class-wide type, it may still be
9473 -- okay, since it can be the result of the expansion of a call to a
9474 -- dispatching function, so we also have to check class-wideness of the
9475 -- type of the expression's original node.
1420b484
JM
9476
9477 if (Is_Class_Wide_Type (Target_Typ)
9478 or else
9479 (Is_Class_Wide_Type (Etype (Expr))
9480 and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
996ae0b0
RK
9481 and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
9482 then
9483 Wrong_Type (Expr, Target_Typ);
9484 end if;
9485
90c63b09
AC
9486 -- If the target type is unconstrained, then we reset the type of the
9487 -- result from the type of the expression. For other cases, the actual
9488 -- subtype of the expression is the target type.
996ae0b0
RK
9489
9490 if Is_Composite_Type (Target_Typ)
9491 and then not Is_Constrained (Target_Typ)
9492 then
9493 Set_Etype (N, Etype (Expr));
9494 end if;
9495
dec6faf1 9496 Analyze_Dimension (N);
996ae0b0 9497 Eval_Qualified_Expression (N);
6cf7eae6
AC
9498
9499 -- If we still have a qualified expression after the static evaluation,
9500 -- then apply a scalar range check if needed. The reason that we do this
9501 -- after the Eval call is that otherwise, the application of the range
9502 -- check may convert an illegal static expression and result in warning
9503 -- rather than giving an error (e.g Integer'(Integer'Last + 1)).
9504
9505 if Nkind (N) = N_Qualified_Expression and then Is_Scalar_Type (Typ) then
9506 Apply_Scalar_Range_Check (Expr, Typ);
9507 end if;
996ae0b0
RK
9508 end Resolve_Qualified_Expression;
9509
7610fee8
AC
9510 ------------------------------
9511 -- Resolve_Raise_Expression --
9512 ------------------------------
9513
9514 procedure Resolve_Raise_Expression (N : Node_Id; Typ : Entity_Id) is
9515 begin
3e586e10
AC
9516 if Typ = Raise_Type then
9517 Error_Msg_N ("cannot find unique type for raise expression", N);
9518 Set_Etype (N, Any_Type);
9519 else
9520 Set_Etype (N, Typ);
9521 end if;
7610fee8
AC
9522 end Resolve_Raise_Expression;
9523
996ae0b0
RK
9524 -------------------
9525 -- Resolve_Range --
9526 -------------------
9527
9528 procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
9529 L : constant Node_Id := Low_Bound (N);
9530 H : constant Node_Id := High_Bound (N);
9531
bd29d519
AC
9532 function First_Last_Ref return Boolean;
9533 -- Returns True if N is of the form X'First .. X'Last where X is the
9534 -- same entity for both attributes.
9535
9536 --------------------
9537 -- First_Last_Ref --
9538 --------------------
9539
9540 function First_Last_Ref return Boolean is
9541 Lorig : constant Node_Id := Original_Node (L);
9542 Horig : constant Node_Id := Original_Node (H);
9543
9544 begin
9545 if Nkind (Lorig) = N_Attribute_Reference
9546 and then Nkind (Horig) = N_Attribute_Reference
9547 and then Attribute_Name (Lorig) = Name_First
9548 and then Attribute_Name (Horig) = Name_Last
9549 then
9550 declare
9551 PL : constant Node_Id := Prefix (Lorig);
9552 PH : constant Node_Id := Prefix (Horig);
9553 begin
9554 if Is_Entity_Name (PL)
9555 and then Is_Entity_Name (PH)
9556 and then Entity (PL) = Entity (PH)
9557 then
9558 return True;
9559 end if;
9560 end;
9561 end if;
9562
9563 return False;
9564 end First_Last_Ref;
9565
9566 -- Start of processing for Resolve_Range
9567
996ae0b0
RK
9568 begin
9569 Set_Etype (N, Typ);
9570 Resolve (L, Typ);
9571 Resolve (H, Typ);
9572
bd29d519
AC
9573 -- Check for inappropriate range on unordered enumeration type
9574
9575 if Bad_Unordered_Enumeration_Reference (N, Typ)
9576
9577 -- Exclude X'First .. X'Last if X is the same entity for both
9578
9579 and then not First_Last_Ref
9580 then
b1d12996
AC
9581 Error_Msg_Sloc := Sloc (Typ);
9582 Error_Msg_NE
9583 ("subrange of unordered enumeration type& declared#?U?", N, Typ);
498d1b80
AC
9584 end if;
9585
996ae0b0
RK
9586 Check_Unset_Reference (L);
9587 Check_Unset_Reference (H);
9588
9589 -- We have to check the bounds for being within the base range as
758c442c
GD
9590 -- required for a non-static context. Normally this is automatic and
9591 -- done as part of evaluating expressions, but the N_Range node is an
9592 -- exception, since in GNAT we consider this node to be a subexpression,
9593 -- even though in Ada it is not. The circuit in Sem_Eval could check for
9594 -- this, but that would put the test on the main evaluation path for
9595 -- expressions.
996ae0b0
RK
9596
9597 Check_Non_Static_Context (L);
9598 Check_Non_Static_Context (H);
9599
b7d1f17f
HK
9600 -- Check for an ambiguous range over character literals. This will
9601 -- happen with a membership test involving only literals.
9602
9603 if Typ = Any_Character then
9604 Ambiguous_Character (L);
9605 Set_Etype (N, Any_Type);
9606 return;
9607 end if;
9608
5cc9353d
RD
9609 -- If bounds are static, constant-fold them, so size computations are
9610 -- identical between front-end and back-end. Do not perform this
fbf5a39b 9611 -- transformation while analyzing generic units, as type information
5cc9353d 9612 -- would be lost when reanalyzing the constant node in the instance.
fbf5a39b 9613
4460a9bc 9614 if Is_Discrete_Type (Typ) and then Expander_Active then
fbf5a39b 9615 if Is_OK_Static_Expression (L) then
edab6088 9616 Fold_Uint (L, Expr_Value (L), Is_OK_Static_Expression (L));
fbf5a39b
AC
9617 end if;
9618
9619 if Is_OK_Static_Expression (H) then
edab6088 9620 Fold_Uint (H, Expr_Value (H), Is_OK_Static_Expression (H));
fbf5a39b
AC
9621 end if;
9622 end if;
996ae0b0
RK
9623 end Resolve_Range;
9624
9625 --------------------------
9626 -- Resolve_Real_Literal --
9627 --------------------------
9628
9629 procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
9630 Actual_Typ : constant Entity_Id := Etype (N);
9631
9632 begin
9633 -- Special processing for fixed-point literals to make sure that the
5cc9353d
RD
9634 -- value is an exact multiple of small where this is required. We skip
9635 -- this for the universal real case, and also for generic types.
996ae0b0
RK
9636
9637 if Is_Fixed_Point_Type (Typ)
9638 and then Typ /= Universal_Fixed
9639 and then Typ /= Any_Fixed
9640 and then not Is_Generic_Type (Typ)
9641 then
9642 declare
9643 Val : constant Ureal := Realval (N);
9644 Cintr : constant Ureal := Val / Small_Value (Typ);
9645 Cint : constant Uint := UR_Trunc (Cintr);
9646 Den : constant Uint := Norm_Den (Cintr);
9647 Stat : Boolean;
9648
9649 begin
9650 -- Case of literal is not an exact multiple of the Small
9651
9652 if Den /= 1 then
9653
5cc9353d
RD
9654 -- For a source program literal for a decimal fixed-point type,
9655 -- this is statically illegal (RM 4.9(36)).
996ae0b0
RK
9656
9657 if Is_Decimal_Fixed_Point_Type (Typ)
9658 and then Actual_Typ = Universal_Real
9659 and then Comes_From_Source (N)
9660 then
9661 Error_Msg_N ("value has extraneous low order digits", N);
9662 end if;
9663
bc5f3720
RD
9664 -- Generate a warning if literal from source
9665
edab6088 9666 if Is_OK_Static_Expression (N)
bc5f3720
RD
9667 and then Warn_On_Bad_Fixed_Value
9668 then
9669 Error_Msg_N
324ac540 9670 ("?b?static fixed-point value is not a multiple of Small!",
bc5f3720
RD
9671 N);
9672 end if;
9673
996ae0b0
RK
9674 -- Replace literal by a value that is the exact representation
9675 -- of a value of the type, i.e. a multiple of the small value,
9676 -- by truncation, since Machine_Rounds is false for all GNAT
9677 -- fixed-point types (RM 4.9(38)).
9678
edab6088 9679 Stat := Is_OK_Static_Expression (N);
996ae0b0
RK
9680 Rewrite (N,
9681 Make_Real_Literal (Sloc (N),
9682 Realval => Small_Value (Typ) * Cint));
9683
9684 Set_Is_Static_Expression (N, Stat);
9685 end if;
9686
9687 -- In all cases, set the corresponding integer field
9688
9689 Set_Corresponding_Integer_Value (N, Cint);
9690 end;
9691 end if;
9692
9693 -- Now replace the actual type by the expected type as usual
9694
9695 Set_Etype (N, Typ);
9696 Eval_Real_Literal (N);
9697 end Resolve_Real_Literal;
9698
9699 -----------------------
9700 -- Resolve_Reference --
9701 -----------------------
9702
9703 procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
9704 P : constant Node_Id := Prefix (N);
9705
9706 begin
9707 -- Replace general access with specific type
9708
9709 if Ekind (Etype (N)) = E_Allocator_Type then
9710 Set_Etype (N, Base_Type (Typ));
9711 end if;
9712
9713 Resolve (P, Designated_Type (Etype (N)));
9714
5cc9353d
RD
9715 -- If we are taking the reference of a volatile entity, then treat it as
9716 -- a potential modification of this entity. This is too conservative,
9717 -- but necessary because remove side effects can cause transformations
9718 -- of normal assignments into reference sequences that otherwise fail to
9719 -- notice the modification.
996ae0b0 9720
fbf5a39b 9721 if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
45fc7ddb 9722 Note_Possible_Modification (P, Sure => False);
996ae0b0
RK
9723 end if;
9724 end Resolve_Reference;
9725
9726 --------------------------------
9727 -- Resolve_Selected_Component --
9728 --------------------------------
9729
9730 procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
9731 Comp : Entity_Id;
9732 Comp1 : Entity_Id := Empty; -- prevent junk warning
c2a2dbcc 9733 P : constant Node_Id := Prefix (N);
996ae0b0
RK
9734 S : constant Node_Id := Selector_Name (N);
9735 T : Entity_Id := Etype (P);
9736 I : Interp_Index;
9737 I1 : Interp_Index := 0; -- prevent junk warning
9738 It : Interp;
9739 It1 : Interp;
9740 Found : Boolean;
9741
6510f4c9
GB
9742 function Init_Component return Boolean;
9743 -- Check whether this is the initialization of a component within an
fbf5a39b 9744 -- init proc (by assignment or call to another init proc). If true,
6510f4c9
GB
9745 -- there is no need for a discriminant check.
9746
9747 --------------------
9748 -- Init_Component --
9749 --------------------
9750
9751 function Init_Component return Boolean is
9752 begin
9753 return Inside_Init_Proc
9754 and then Nkind (Prefix (N)) = N_Identifier
9755 and then Chars (Prefix (N)) = Name_uInit
9756 and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
9757 end Init_Component;
9758
9759 -- Start of processing for Resolve_Selected_Component
9760
996ae0b0
RK
9761 begin
9762 if Is_Overloaded (P) then
9763
9764 -- Use the context type to select the prefix that has a selector
9765 -- of the correct name and type.
9766
9767 Found := False;
9768 Get_First_Interp (P, I, It);
9769
9770 Search : while Present (It.Typ) loop
9771 if Is_Access_Type (It.Typ) then
9772 T := Designated_Type (It.Typ);
9773 else
9774 T := It.Typ;
9775 end if;
9776
95eb8b69
AC
9777 -- Locate selected component. For a private prefix the selector
9778 -- can denote a discriminant.
9779
9780 if Is_Record_Type (T) or else Is_Private_Type (T) then
36fcf362
RD
9781
9782 -- The visible components of a class-wide type are those of
9783 -- the root type.
9784
9785 if Is_Class_Wide_Type (T) then
9786 T := Etype (T);
9787 end if;
9788
996ae0b0 9789 Comp := First_Entity (T);
996ae0b0 9790 while Present (Comp) loop
996ae0b0 9791 if Chars (Comp) = Chars (S)
dda38714 9792 and then Covers (Typ, Etype (Comp))
996ae0b0
RK
9793 then
9794 if not Found then
9795 Found := True;
9796 I1 := I;
9797 It1 := It;
9798 Comp1 := Comp;
9799
9800 else
9801 It := Disambiguate (P, I1, I, Any_Type);
9802
9803 if It = No_Interp then
9804 Error_Msg_N
9805 ("ambiguous prefix for selected component", N);
9806 Set_Etype (N, Typ);
9807 return;
9808
9809 else
9810 It1 := It;
9811
c8ef728f
ES
9812 -- There may be an implicit dereference. Retrieve
9813 -- designated record type.
9814
9815 if Is_Access_Type (It1.Typ) then
9816 T := Designated_Type (It1.Typ);
9817 else
9818 T := It1.Typ;
9819 end if;
9820
9821 if Scope (Comp1) /= T then
996ae0b0
RK
9822
9823 -- Resolution chooses the new interpretation.
9824 -- Find the component with the right name.
9825
c8ef728f 9826 Comp1 := First_Entity (T);
996ae0b0
RK
9827 while Present (Comp1)
9828 and then Chars (Comp1) /= Chars (S)
9829 loop
9830 Comp1 := Next_Entity (Comp1);
9831 end loop;
9832 end if;
9833
9834 exit Search;
9835 end if;
9836 end if;
9837 end if;
9838
9839 Comp := Next_Entity (Comp);
9840 end loop;
996ae0b0
RK
9841 end if;
9842
9843 Get_Next_Interp (I, It);
996ae0b0
RK
9844 end loop Search;
9845
9926efec 9846 -- There must be a legal interpretation at this point
dda38714
AC
9847
9848 pragma Assert (Found);
996ae0b0
RK
9849 Resolve (P, It1.Typ);
9850 Set_Etype (N, Typ);
e7ba564f 9851 Set_Entity_With_Checks (S, Comp1);
996ae0b0
RK
9852
9853 else
fbf5a39b 9854 -- Resolve prefix with its type
996ae0b0
RK
9855
9856 Resolve (P, T);
9857 end if;
9858
aa180613
RD
9859 -- Generate cross-reference. We needed to wait until full overloading
9860 -- resolution was complete to do this, since otherwise we can't tell if
01e17342 9861 -- we are an lvalue or not.
aa180613
RD
9862
9863 if May_Be_Lvalue (N) then
9864 Generate_Reference (Entity (S), S, 'm');
9865 else
9866 Generate_Reference (Entity (S), S, 'r');
9867 end if;
9868
c8ef728f
ES
9869 -- If prefix is an access type, the node will be transformed into an
9870 -- explicit dereference during expansion. The type of the node is the
9871 -- designated type of that of the prefix.
996ae0b0
RK
9872
9873 if Is_Access_Type (Etype (P)) then
996ae0b0 9874 T := Designated_Type (Etype (P));
c8ef728f 9875 Check_Fully_Declared_Prefix (T, P);
996ae0b0
RK
9876 else
9877 T := Etype (P);
9878 end if;
9879
c386239f
AC
9880 -- Set flag for expander if discriminant check required on a component
9881 -- appearing within a variant.
ef1c0511 9882
996ae0b0 9883 if Has_Discriminants (T)
1b1d88b1 9884 and then Ekind (Entity (S)) = E_Component
996ae0b0
RK
9885 and then Present (Original_Record_Component (Entity (S)))
9886 and then Ekind (Original_Record_Component (Entity (S))) = E_Component
c96c518f
AC
9887 and then
9888 Is_Declared_Within_Variant (Original_Record_Component (Entity (S)))
996ae0b0 9889 and then not Discriminant_Checks_Suppressed (T)
6510f4c9 9890 and then not Init_Component
996ae0b0
RK
9891 then
9892 Set_Do_Discriminant_Check (N);
9893 end if;
9894
9895 if Ekind (Entity (S)) = E_Void then
9896 Error_Msg_N ("premature use of component", S);
9897 end if;
9898
9899 -- If the prefix is a record conversion, this may be a renamed
9900 -- discriminant whose bounds differ from those of the original
9901 -- one, so we must ensure that a range check is performed.
9902
9903 if Nkind (P) = N_Type_Conversion
9904 and then Ekind (Entity (S)) = E_Discriminant
fbf5a39b 9905 and then Is_Discrete_Type (Typ)
996ae0b0
RK
9906 then
9907 Set_Etype (N, Base_Type (Typ));
9908 end if;
9909
9910 -- Note: No Eval processing is required, because the prefix is of a
9911 -- record type, or protected type, and neither can possibly be static.
9912
c2a2dbcc
RD
9913 -- If the record type is atomic, and the component is non-atomic, then
9914 -- this is worth a warning, since we have a situation where the access
9915 -- to the component may cause extra read/writes of the atomic array
9916 -- object, or partial word accesses, both of which may be unexpected.
c28408b7
RD
9917
9918 if Nkind (N) = N_Selected_Component
c2a2dbcc
RD
9919 and then Is_Atomic_Ref_With_Address (N)
9920 and then not Is_Atomic (Entity (S))
9921 and then not Is_Atomic (Etype (Entity (S)))
c28408b7 9922 then
54c04d6c 9923 Error_Msg_N
c2a2dbcc
RD
9924 ("??access to non-atomic component of atomic record",
9925 Prefix (N));
54c04d6c 9926 Error_Msg_N
c2a2dbcc
RD
9927 ("\??may cause unexpected accesses to atomic object",
9928 Prefix (N));
c28408b7 9929 end if;
54c04d6c 9930
dec6faf1 9931 Analyze_Dimension (N);
996ae0b0
RK
9932 end Resolve_Selected_Component;
9933
9934 -------------------
9935 -- Resolve_Shift --
9936 -------------------
9937
9938 procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
9939 B_Typ : constant Entity_Id := Base_Type (Typ);
9940 L : constant Node_Id := Left_Opnd (N);
9941 R : constant Node_Id := Right_Opnd (N);
9942
9943 begin
9944 -- We do the resolution using the base type, because intermediate values
9945 -- in expressions always are of the base type, not a subtype of it.
9946
9947 Resolve (L, B_Typ);
9948 Resolve (R, Standard_Natural);
9949
9950 Check_Unset_Reference (L);
9951 Check_Unset_Reference (R);
9952
9953 Set_Etype (N, B_Typ);
fbf5a39b 9954 Generate_Operator_Reference (N, B_Typ);
996ae0b0
RK
9955 Eval_Shift (N);
9956 end Resolve_Shift;
9957
9958 ---------------------------
9959 -- Resolve_Short_Circuit --
9960 ---------------------------
9961
9962 procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
9963 B_Typ : constant Entity_Id := Base_Type (Typ);
9964 L : constant Node_Id := Left_Opnd (N);
9965 R : constant Node_Id := Right_Opnd (N);
9966
9967 begin
064f4527
TQ
9968 -- Ensure all actions associated with the left operand (e.g.
9969 -- finalization of transient controlled objects) are fully evaluated
9970 -- locally within an expression with actions. This is particularly
9971 -- helpful for coverage analysis. However this should not happen in
9972 -- generics.
9973
4460a9bc 9974 if Expander_Active then
064f4527
TQ
9975 declare
9976 Reloc_L : constant Node_Id := Relocate_Node (L);
9977 begin
9978 Save_Interps (Old_N => L, New_N => Reloc_L);
9979
9980 Rewrite (L,
9981 Make_Expression_With_Actions (Sloc (L),
9982 Actions => New_List,
9983 Expression => Reloc_L));
9984
9985 -- Set Comes_From_Source on L to preserve warnings for unset
9986 -- reference.
9987
9988 Set_Comes_From_Source (L, Comes_From_Source (Reloc_L));
9989 end;
9990 end if;
9991
996ae0b0
RK
9992 Resolve (L, B_Typ);
9993 Resolve (R, B_Typ);
9994
45fc7ddb
HK
9995 -- Check for issuing warning for always False assert/check, this happens
9996 -- when assertions are turned off, in which case the pragma Assert/Check
36fcf362
RD
9997 -- was transformed into:
9998
9999 -- if False and then <condition> then ...
10000
10001 -- and we detect this pattern
10002
10003 if Warn_On_Assertion_Failure
10004 and then Is_Entity_Name (R)
10005 and then Entity (R) = Standard_False
10006 and then Nkind (Parent (N)) = N_If_Statement
10007 and then Nkind (N) = N_And_Then
10008 and then Is_Entity_Name (L)
10009 and then Entity (L) = Standard_False
10010 then
10011 declare
10012 Orig : constant Node_Id := Original_Node (Parent (N));
45fc7ddb 10013
36fcf362 10014 begin
20a65dcb
RD
10015 -- Special handling of Asssert pragma
10016
36fcf362 10017 if Nkind (Orig) = N_Pragma
26570b21 10018 and then Pragma_Name (Orig) = Name_Assert
36fcf362 10019 then
36fcf362
RD
10020 declare
10021 Expr : constant Node_Id :=
10022 Original_Node
10023 (Expression
10024 (First (Pragma_Argument_Associations (Orig))));
20a65dcb 10025
36fcf362 10026 begin
20a65dcb
RD
10027 -- Don't warn if original condition is explicit False,
10028 -- since obviously the failure is expected in this case.
10029
36fcf362
RD
10030 if Is_Entity_Name (Expr)
10031 and then Entity (Expr) = Standard_False
10032 then
10033 null;
51bf9bdf 10034
20a65dcb
RD
10035 -- Issue warning. We do not want the deletion of the
10036 -- IF/AND-THEN to take this message with it. We achieve this
10037 -- by making sure that the expanded code points to the Sloc
10038 -- of the expression, not the original pragma.
10039
10040 else
8a06151a
RD
10041 -- Note: Use Error_Msg_F here rather than Error_Msg_N.
10042 -- The source location of the expression is not usually
10043 -- the best choice here. For example, it gets located on
10044 -- the last AND keyword in a chain of boolean expressiond
10045 -- AND'ed together. It is best to put the message on the
10046 -- first character of the assertion, which is the effect
10047 -- of the First_Node call here.
10048
ca20a08e 10049 Error_Msg_F
685bc70f 10050 ("?A?assertion would fail at run time!",
51bf9bdf
AC
10051 Expression
10052 (First (Pragma_Argument_Associations (Orig))));
36fcf362
RD
10053 end if;
10054 end;
45fc7ddb
HK
10055
10056 -- Similar processing for Check pragma
10057
10058 elsif Nkind (Orig) = N_Pragma
10059 and then Pragma_Name (Orig) = Name_Check
10060 then
10061 -- Don't want to warn if original condition is explicit False
10062
10063 declare
10064 Expr : constant Node_Id :=
324ac540
AC
10065 Original_Node
10066 (Expression
10067 (Next (First (Pragma_Argument_Associations (Orig)))));
45fc7ddb
HK
10068 begin
10069 if Is_Entity_Name (Expr)
10070 and then Entity (Expr) = Standard_False
10071 then
10072 null;
8a06151a
RD
10073
10074 -- Post warning
10075
45fc7ddb 10076 else
8a06151a
RD
10077 -- Again use Error_Msg_F rather than Error_Msg_N, see
10078 -- comment above for an explanation of why we do this.
10079
ca20a08e 10080 Error_Msg_F
685bc70f 10081 ("?A?check would fail at run time!",
51bf9bdf
AC
10082 Expression
10083 (Last (Pragma_Argument_Associations (Orig))));
45fc7ddb
HK
10084 end if;
10085 end;
36fcf362
RD
10086 end if;
10087 end;
10088 end if;
10089
10090 -- Continue with processing of short circuit
10091
996ae0b0
RK
10092 Check_Unset_Reference (L);
10093 Check_Unset_Reference (R);
10094
10095 Set_Etype (N, B_Typ);
10096 Eval_Short_Circuit (N);
10097 end Resolve_Short_Circuit;
10098
10099 -------------------
10100 -- Resolve_Slice --
10101 -------------------
10102
10103 procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
996ae0b0 10104 Drange : constant Node_Id := Discrete_Range (N);
5f44f0d4 10105 Name : constant Node_Id := Prefix (N);
996ae0b0 10106 Array_Type : Entity_Id := Empty;
800da977 10107 Dexpr : Node_Id := Empty;
5f44f0d4 10108 Index_Type : Entity_Id;
996ae0b0
RK
10109
10110 begin
10111 if Is_Overloaded (Name) then
10112
d81b4bfe
TQ
10113 -- Use the context type to select the prefix that yields the correct
10114 -- array type.
996ae0b0
RK
10115
10116 declare
10117 I : Interp_Index;
10118 I1 : Interp_Index := 0;
10119 It : Interp;
10120 P : constant Node_Id := Prefix (N);
10121 Found : Boolean := False;
10122
10123 begin
10124 Get_First_Interp (P, I, It);
996ae0b0 10125 while Present (It.Typ) loop
996ae0b0
RK
10126 if (Is_Array_Type (It.Typ)
10127 and then Covers (Typ, It.Typ))
10128 or else (Is_Access_Type (It.Typ)
10129 and then Is_Array_Type (Designated_Type (It.Typ))
10130 and then Covers (Typ, Designated_Type (It.Typ)))
10131 then
10132 if Found then
10133 It := Disambiguate (P, I1, I, Any_Type);
10134
10135 if It = No_Interp then
10136 Error_Msg_N ("ambiguous prefix for slicing", N);
10137 Set_Etype (N, Typ);
10138 return;
10139 else
10140 Found := True;
10141 Array_Type := It.Typ;
10142 I1 := I;
10143 end if;
10144 else
10145 Found := True;
10146 Array_Type := It.Typ;
10147 I1 := I;
10148 end if;
10149 end if;
10150
10151 Get_Next_Interp (I, It);
10152 end loop;
10153 end;
10154
10155 else
10156 Array_Type := Etype (Name);
10157 end if;
10158
10159 Resolve (Name, Array_Type);
10160
10161 if Is_Access_Type (Array_Type) then
10162 Apply_Access_Check (N);
10163 Array_Type := Designated_Type (Array_Type);
10164
c8ef728f
ES
10165 -- If the prefix is an access to an unconstrained array, we must use
10166 -- the actual subtype of the object to perform the index checks. The
10167 -- object denoted by the prefix is implicit in the node, so we build
10168 -- an explicit representation for it in order to compute the actual
10169 -- subtype.
82c80734
RD
10170
10171 if not Is_Constrained (Array_Type) then
10172 Remove_Side_Effects (Prefix (N));
10173
10174 declare
10175 Obj : constant Node_Id :=
10176 Make_Explicit_Dereference (Sloc (N),
10177 Prefix => New_Copy_Tree (Prefix (N)));
10178 begin
10179 Set_Etype (Obj, Array_Type);
10180 Set_Parent (Obj, Parent (N));
10181 Array_Type := Get_Actual_Subtype (Obj);
10182 end;
10183 end if;
10184
996ae0b0 10185 elsif Is_Entity_Name (Name)
6c994759 10186 or else Nkind (Name) = N_Explicit_Dereference
996ae0b0
RK
10187 or else (Nkind (Name) = N_Function_Call
10188 and then not Is_Constrained (Etype (Name)))
10189 then
10190 Array_Type := Get_Actual_Subtype (Name);
aa5147f0
ES
10191
10192 -- If the name is a selected component that depends on discriminants,
10193 -- build an actual subtype for it. This can happen only when the name
10194 -- itself is overloaded; otherwise the actual subtype is created when
10195 -- the selected component is analyzed.
10196
10197 elsif Nkind (Name) = N_Selected_Component
10198 and then Full_Analysis
10199 and then Depends_On_Discriminant (First_Index (Array_Type))
10200 then
10201 declare
10202 Act_Decl : constant Node_Id :=
10203 Build_Actual_Subtype_Of_Component (Array_Type, Name);
10204 begin
10205 Insert_Action (N, Act_Decl);
10206 Array_Type := Defining_Identifier (Act_Decl);
10207 end;
d79e621a
GD
10208
10209 -- Maybe this should just be "else", instead of checking for the
5cc9353d
RD
10210 -- specific case of slice??? This is needed for the case where the
10211 -- prefix is an Image attribute, which gets expanded to a slice, and so
10212 -- has a constrained subtype which we want to use for the slice range
10213 -- check applied below (the range check won't get done if the
10214 -- unconstrained subtype of the 'Image is used).
d79e621a
GD
10215
10216 elsif Nkind (Name) = N_Slice then
10217 Array_Type := Etype (Name);
996ae0b0
RK
10218 end if;
10219
800da977
AC
10220 -- Obtain the type of the array index
10221
10222 if Ekind (Array_Type) = E_String_Literal_Subtype then
10223 Index_Type := Etype (String_Literal_Low_Bound (Array_Type));
10224 else
10225 Index_Type := Etype (First_Index (Array_Type));
10226 end if;
10227
996ae0b0
RK
10228 -- If name was overloaded, set slice type correctly now
10229
10230 Set_Etype (N, Array_Type);
10231
800da977
AC
10232 -- Handle the generation of a range check that compares the array index
10233 -- against the discrete_range. The check is not applied to internally
10234 -- built nodes associated with the expansion of dispatch tables. Check
10235 -- that Ada.Tags has already been loaded to avoid extra dependencies on
10236 -- the unit.
10237
10238 if Tagged_Type_Expansion
10239 and then RTU_Loaded (Ada_Tags)
10240 and then Nkind (Prefix (N)) = N_Selected_Component
10241 and then Present (Entity (Selector_Name (Prefix (N))))
10242 and then Entity (Selector_Name (Prefix (N))) =
10243 RTE_Record_Component (RE_Prims_Ptr)
10244 then
10245 null;
996ae0b0 10246
800da977
AC
10247 -- The discrete_range is specified by a subtype indication. Create a
10248 -- shallow copy and inherit the type, parent and source location from
10249 -- the discrete_range. This ensures that the range check is inserted
10250 -- relative to the slice and that the runtime exception points to the
10251 -- proper construct.
5f44f0d4 10252
800da977
AC
10253 elsif Is_Entity_Name (Drange) then
10254 Dexpr := New_Copy (Scalar_Range (Entity (Drange)));
996ae0b0 10255
800da977
AC
10256 Set_Etype (Dexpr, Etype (Drange));
10257 Set_Parent (Dexpr, Parent (Drange));
10258 Set_Sloc (Dexpr, Sloc (Drange));
dbe945f1 10259
800da977
AC
10260 -- The discrete_range is a regular range. Resolve the bounds and remove
10261 -- their side effects.
dbe945f1 10262
800da977
AC
10263 else
10264 Resolve (Drange, Base_Type (Index_Type));
10265
10266 if Nkind (Drange) = N_Range then
10267 Force_Evaluation (Low_Bound (Drange));
cae81f17 10268 Force_Evaluation (High_Bound (Drange));
0669bebe 10269
800da977 10270 Dexpr := Drange;
996ae0b0
RK
10271 end if;
10272 end if;
10273
800da977
AC
10274 if Present (Dexpr) then
10275 Apply_Range_Check (Dexpr, Index_Type);
10276 end if;
10277
996ae0b0 10278 Set_Slice_Subtype (N);
aa180613 10279
ea034236
AC
10280 -- Check bad use of type with predicates
10281
24de083f
AC
10282 declare
10283 Subt : Entity_Id;
10284
10285 begin
10286 if Nkind (Drange) = N_Subtype_Indication
b330e3c8 10287 and then Has_Predicates (Entity (Subtype_Mark (Drange)))
24de083f
AC
10288 then
10289 Subt := Entity (Subtype_Mark (Drange));
24de083f
AC
10290 else
10291 Subt := Etype (Drange);
10292 end if;
10293
10294 if Has_Predicates (Subt) then
10295 Bad_Predicated_Subtype_Use
10296 ("subtype& has predicate, not allowed in slice", Drange, Subt);
10297 end if;
10298 end;
ea034236
AC
10299
10300 -- Otherwise here is where we check suspicious indexes
10301
24de083f 10302 if Nkind (Drange) = N_Range then
aa180613
RD
10303 Warn_On_Suspicious_Index (Name, Low_Bound (Drange));
10304 Warn_On_Suspicious_Index (Name, High_Bound (Drange));
10305 end if;
10306
dec6faf1 10307 Analyze_Dimension (N);
996ae0b0 10308 Eval_Slice (N);
996ae0b0
RK
10309 end Resolve_Slice;
10310
10311 ----------------------------
10312 -- Resolve_String_Literal --
10313 ----------------------------
10314
10315 procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
10316 C_Typ : constant Entity_Id := Component_Type (Typ);
10317 R_Typ : constant Entity_Id := Root_Type (C_Typ);
10318 Loc : constant Source_Ptr := Sloc (N);
10319 Str : constant String_Id := Strval (N);
10320 Strlen : constant Nat := String_Length (Str);
10321 Subtype_Id : Entity_Id;
10322 Need_Check : Boolean;
10323
10324 begin
10325 -- For a string appearing in a concatenation, defer creation of the
10326 -- string_literal_subtype until the end of the resolution of the
c8ef728f
ES
10327 -- concatenation, because the literal may be constant-folded away. This
10328 -- is a useful optimization for long concatenation expressions.
996ae0b0 10329
c8ef728f 10330 -- If the string is an aggregate built for a single character (which
996ae0b0 10331 -- happens in a non-static context) or a is null string to which special
c8ef728f
ES
10332 -- checks may apply, we build the subtype. Wide strings must also get a
10333 -- string subtype if they come from a one character aggregate. Strings
996ae0b0
RK
10334 -- generated by attributes might be static, but it is often hard to
10335 -- determine whether the enclosing context is static, so we generate
10336 -- subtypes for them as well, thus losing some rarer optimizations ???
10337 -- Same for strings that come from a static conversion.
10338
10339 Need_Check :=
10340 (Strlen = 0 and then Typ /= Standard_String)
10341 or else Nkind (Parent (N)) /= N_Op_Concat
10342 or else (N /= Left_Opnd (Parent (N))
10343 and then N /= Right_Opnd (Parent (N)))
82c80734
RD
10344 or else ((Typ = Standard_Wide_String
10345 or else Typ = Standard_Wide_Wide_String)
996ae0b0
RK
10346 and then Nkind (Original_Node (N)) /= N_String_Literal);
10347
d81b4bfe
TQ
10348 -- If the resolving type is itself a string literal subtype, we can just
10349 -- reuse it, since there is no point in creating another.
996ae0b0
RK
10350
10351 if Ekind (Typ) = E_String_Literal_Subtype then
10352 Subtype_Id := Typ;
10353
10354 elsif Nkind (Parent (N)) = N_Op_Concat
10355 and then not Need_Check
45fc7ddb
HK
10356 and then not Nkind_In (Original_Node (N), N_Character_Literal,
10357 N_Attribute_Reference,
10358 N_Qualified_Expression,
10359 N_Type_Conversion)
996ae0b0
RK
10360 then
10361 Subtype_Id := Typ;
10362
79904ebc
AC
10363 -- Do not generate a string literal subtype for the default expression
10364 -- of a formal parameter in GNATprove mode. This is because the string
10365 -- subtype is associated with the freezing actions of the subprogram,
10366 -- however freezing is disabled in GNATprove mode and as a result the
10367 -- subtype is unavailable.
10368
10369 elsif GNATprove_Mode
10370 and then Nkind (Parent (N)) = N_Parameter_Specification
10371 then
10372 Subtype_Id := Typ;
10373
996ae0b0
RK
10374 -- Otherwise we must create a string literal subtype. Note that the
10375 -- whole idea of string literal subtypes is simply to avoid the need
10376 -- for building a full fledged array subtype for each literal.
45fc7ddb 10377
996ae0b0
RK
10378 else
10379 Set_String_Literal_Subtype (N, Typ);
10380 Subtype_Id := Etype (N);
10381 end if;
10382
10383 if Nkind (Parent (N)) /= N_Op_Concat
10384 or else Need_Check
10385 then
10386 Set_Etype (N, Subtype_Id);
10387 Eval_String_Literal (N);
10388 end if;
10389
10390 if Is_Limited_Composite (Typ)
10391 or else Is_Private_Composite (Typ)
10392 then
10393 Error_Msg_N ("string literal not available for private array", N);
10394 Set_Etype (N, Any_Type);
10395 return;
10396 end if;
10397
d81b4bfe
TQ
10398 -- The validity of a null string has been checked in the call to
10399 -- Eval_String_Literal.
996ae0b0
RK
10400
10401 if Strlen = 0 then
10402 return;
10403
c8ef728f
ES
10404 -- Always accept string literal with component type Any_Character, which
10405 -- occurs in error situations and in comparisons of literals, both of
10406 -- which should accept all literals.
996ae0b0
RK
10407
10408 elsif R_Typ = Any_Character then
10409 return;
10410
f3d57416
RW
10411 -- If the type is bit-packed, then we always transform the string
10412 -- literal into a full fledged aggregate.
996ae0b0
RK
10413
10414 elsif Is_Bit_Packed_Array (Typ) then
10415 null;
10416
82c80734 10417 -- Deal with cases of Wide_Wide_String, Wide_String, and String
996ae0b0
RK
10418
10419 else
82c80734
RD
10420 -- For Standard.Wide_Wide_String, or any other type whose component
10421 -- type is Standard.Wide_Wide_Character, we know that all the
996ae0b0
RK
10422 -- characters in the string must be acceptable, since the parser
10423 -- accepted the characters as valid character literals.
10424
82c80734 10425 if R_Typ = Standard_Wide_Wide_Character then
996ae0b0
RK
10426 null;
10427
c8ef728f
ES
10428 -- For the case of Standard.String, or any other type whose component
10429 -- type is Standard.Character, we must make sure that there are no
10430 -- wide characters in the string, i.e. that it is entirely composed
10431 -- of characters in range of type Character.
996ae0b0 10432
c8ef728f
ES
10433 -- If the string literal is the result of a static concatenation, the
10434 -- test has already been performed on the components, and need not be
10435 -- repeated.
996ae0b0
RK
10436
10437 elsif R_Typ = Standard_Character
10438 and then Nkind (Original_Node (N)) /= N_Op_Concat
10439 then
10440 for J in 1 .. Strlen loop
10441 if not In_Character_Range (Get_String_Char (Str, J)) then
10442
10443 -- If we are out of range, post error. This is one of the
10444 -- very few places that we place the flag in the middle of
d81b4bfe
TQ
10445 -- a token, right under the offending wide character. Not
10446 -- quite clear if this is right wrt wide character encoding
a90bd866 10447 -- sequences, but it's only an error message.
996ae0b0
RK
10448
10449 Error_Msg
82c80734
RD
10450 ("literal out of range of type Standard.Character",
10451 Source_Ptr (Int (Loc) + J));
10452 return;
10453 end if;
10454 end loop;
10455
10456 -- For the case of Standard.Wide_String, or any other type whose
10457 -- component type is Standard.Wide_Character, we must make sure that
10458 -- there are no wide characters in the string, i.e. that it is
10459 -- entirely composed of characters in range of type Wide_Character.
10460
10461 -- If the string literal is the result of a static concatenation,
10462 -- the test has already been performed on the components, and need
10463 -- not be repeated.
10464
10465 elsif R_Typ = Standard_Wide_Character
10466 and then Nkind (Original_Node (N)) /= N_Op_Concat
10467 then
10468 for J in 1 .. Strlen loop
10469 if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
10470
10471 -- If we are out of range, post error. This is one of the
10472 -- very few places that we place the flag in the middle of
10473 -- a token, right under the offending wide character.
10474
10475 -- This is not quite right, because characters in general
10476 -- will take more than one character position ???
10477
10478 Error_Msg
10479 ("literal out of range of type Standard.Wide_Character",
996ae0b0
RK
10480 Source_Ptr (Int (Loc) + J));
10481 return;
10482 end if;
10483 end loop;
10484
10485 -- If the root type is not a standard character, then we will convert
10486 -- the string into an aggregate and will let the aggregate code do
82c80734 10487 -- the checking. Standard Wide_Wide_Character is also OK here.
996ae0b0
RK
10488
10489 else
10490 null;
996ae0b0
RK
10491 end if;
10492
c8ef728f
ES
10493 -- See if the component type of the array corresponding to the string
10494 -- has compile time known bounds. If yes we can directly check
10495 -- whether the evaluation of the string will raise constraint error.
10496 -- Otherwise we need to transform the string literal into the
5cc9353d
RD
10497 -- corresponding character aggregate and let the aggregate code do
10498 -- the checking.
996ae0b0 10499
45fc7ddb
HK
10500 if Is_Standard_Character_Type (R_Typ) then
10501
996ae0b0
RK
10502 -- Check for the case of full range, where we are definitely OK
10503
10504 if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
10505 return;
10506 end if;
10507
10508 -- Here the range is not the complete base type range, so check
10509
10510 declare
10511 Comp_Typ_Lo : constant Node_Id :=
10512 Type_Low_Bound (Component_Type (Typ));
10513 Comp_Typ_Hi : constant Node_Id :=
10514 Type_High_Bound (Component_Type (Typ));
10515
10516 Char_Val : Uint;
10517
10518 begin
10519 if Compile_Time_Known_Value (Comp_Typ_Lo)
10520 and then Compile_Time_Known_Value (Comp_Typ_Hi)
10521 then
10522 for J in 1 .. Strlen loop
10523 Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
10524
10525 if Char_Val < Expr_Value (Comp_Typ_Lo)
10526 or else Char_Val > Expr_Value (Comp_Typ_Hi)
10527 then
10528 Apply_Compile_Time_Constraint_Error
324ac540
AC
10529 (N, "character out of range??",
10530 CE_Range_Check_Failed,
996ae0b0
RK
10531 Loc => Source_Ptr (Int (Loc) + J));
10532 end if;
10533 end loop;
10534
10535 return;
10536 end if;
10537 end;
10538 end if;
10539 end if;
10540
10541 -- If we got here we meed to transform the string literal into the
10542 -- equivalent qualified positional array aggregate. This is rather
10543 -- heavy artillery for this situation, but it is hard work to avoid.
10544
10545 declare
fbf5a39b 10546 Lits : constant List_Id := New_List;
996ae0b0
RK
10547 P : Source_Ptr := Loc + 1;
10548 C : Char_Code;
10549
10550 begin
c8ef728f
ES
10551 -- Build the character literals, we give them source locations that
10552 -- correspond to the string positions, which is a bit tricky given
10553 -- the possible presence of wide character escape sequences.
996ae0b0
RK
10554
10555 for J in 1 .. Strlen loop
10556 C := Get_String_Char (Str, J);
10557 Set_Character_Literal_Name (C);
10558
10559 Append_To (Lits,
82c80734
RD
10560 Make_Character_Literal (P,
10561 Chars => Name_Find,
10562 Char_Literal_Value => UI_From_CC (C)));
996ae0b0
RK
10563
10564 if In_Character_Range (C) then
10565 P := P + 1;
10566
10567 -- Should we have a call to Skip_Wide here ???
5cc9353d 10568
996ae0b0
RK
10569 -- ??? else
10570 -- Skip_Wide (P);
10571
10572 end if;
10573 end loop;
10574
10575 Rewrite (N,
10576 Make_Qualified_Expression (Loc,
e4494292 10577 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
996ae0b0
RK
10578 Expression =>
10579 Make_Aggregate (Loc, Expressions => Lits)));
10580
10581 Analyze_And_Resolve (N, Typ);
10582 end;
10583 end Resolve_String_Literal;
10584
996ae0b0
RK
10585 -----------------------------
10586 -- Resolve_Type_Conversion --
10587 -----------------------------
10588
10589 procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
4b2d2c13
AC
10590 Conv_OK : constant Boolean := Conversion_OK (N);
10591 Operand : constant Node_Id := Expression (N);
b7d1f17f
HK
10592 Operand_Typ : constant Entity_Id := Etype (Operand);
10593 Target_Typ : constant Entity_Id := Etype (N);
996ae0b0 10594 Rop : Node_Id;
fbf5a39b
AC
10595 Orig_N : Node_Id;
10596 Orig_T : Node_Id;
996ae0b0 10597
ae2aa109
AC
10598 Test_Redundant : Boolean := Warn_On_Redundant_Constructs;
10599 -- Set to False to suppress cases where we want to suppress the test
10600 -- for redundancy to avoid possible false positives on this warning.
10601
996ae0b0 10602 begin
996ae0b0 10603 if not Conv_OK
b7d1f17f 10604 and then not Valid_Conversion (N, Target_Typ, Operand)
996ae0b0
RK
10605 then
10606 return;
10607 end if;
10608
ae2aa109
AC
10609 -- If the Operand Etype is Universal_Fixed, then the conversion is
10610 -- never redundant. We need this check because by the time we have
10611 -- finished the rather complex transformation, the conversion looks
10612 -- redundant when it is not.
10613
10614 if Operand_Typ = Universal_Fixed then
10615 Test_Redundant := False;
10616
10617 -- If the operand is marked as Any_Fixed, then special processing is
10618 -- required. This is also a case where we suppress the test for a
10619 -- redundant conversion, since most certainly it is not redundant.
10620
10621 elsif Operand_Typ = Any_Fixed then
10622 Test_Redundant := False;
996ae0b0
RK
10623
10624 -- Mixed-mode operation involving a literal. Context must be a fixed
10625 -- type which is applied to the literal subsequently.
10626
10627 if Is_Fixed_Point_Type (Typ) then
10628 Set_Etype (Operand, Universal_Real);
10629
10630 elsif Is_Numeric_Type (Typ)
45fc7ddb 10631 and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
996ae0b0 10632 and then (Etype (Right_Opnd (Operand)) = Universal_Real
45fc7ddb
HK
10633 or else
10634 Etype (Left_Opnd (Operand)) = Universal_Real)
996ae0b0 10635 then
a77842bd
TQ
10636 -- Return if expression is ambiguous
10637
996ae0b0 10638 if Unique_Fixed_Point_Type (N) = Any_Type then
a77842bd 10639 return;
82c80734 10640
a77842bd
TQ
10641 -- If nothing else, the available fixed type is Duration
10642
10643 else
996ae0b0
RK
10644 Set_Etype (Operand, Standard_Duration);
10645 end if;
10646
bc5f3720 10647 -- Resolve the real operand with largest available precision
9ebe3743 10648
996ae0b0
RK
10649 if Etype (Right_Opnd (Operand)) = Universal_Real then
10650 Rop := New_Copy_Tree (Right_Opnd (Operand));
10651 else
10652 Rop := New_Copy_Tree (Left_Opnd (Operand));
10653 end if;
10654
9ebe3743 10655 Resolve (Rop, Universal_Real);
996ae0b0 10656
82c80734
RD
10657 -- If the operand is a literal (it could be a non-static and
10658 -- illegal exponentiation) check whether the use of Duration
10659 -- is potentially inaccurate.
10660
10661 if Nkind (Rop) = N_Real_Literal
10662 and then Realval (Rop) /= Ureal_0
996ae0b0
RK
10663 and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
10664 then
aa180613 10665 Error_Msg_N
67b8ac46
AC
10666 ("??universal real operand can only "
10667 & "be interpreted as Duration!", Rop);
aa180613 10668 Error_Msg_N
324ac540 10669 ("\??precision will be lost in the conversion!", Rop);
996ae0b0
RK
10670 end if;
10671
891a6e79
AC
10672 elsif Is_Numeric_Type (Typ)
10673 and then Nkind (Operand) in N_Op
10674 and then Unique_Fixed_Point_Type (N) /= Any_Type
10675 then
10676 Set_Etype (Operand, Standard_Duration);
10677
996ae0b0
RK
10678 else
10679 Error_Msg_N ("invalid context for mixed mode operation", N);
10680 Set_Etype (Operand, Any_Type);
10681 return;
10682 end if;
10683 end if;
10684
fbf5a39b 10685 Resolve (Operand);
996ae0b0 10686
2ba431e5
YM
10687 -- In SPARK, a type conversion between array types should be restricted
10688 -- to types which have matching static bounds.
b0186f71 10689
7b98672f
YM
10690 -- Protect call to Matching_Static_Array_Bounds to avoid costly
10691 -- operation if not needed.
10692
6480338a 10693 if Restriction_Check_Required (SPARK_05)
7b98672f 10694 and then Is_Array_Type (Target_Typ)
b0186f71 10695 and then Is_Array_Type (Operand_Typ)
db72f10a 10696 and then Operand_Typ /= Any_Composite -- or else Operand in error
b0186f71
AC
10697 and then not Matching_Static_Array_Bounds (Target_Typ, Operand_Typ)
10698 then
ce5ba43a 10699 Check_SPARK_05_Restriction
fe5d3068 10700 ("array types should have matching static bounds", N);
b0186f71
AC
10701 end if;
10702
e24329cd
YM
10703 -- In formal mode, the operand of an ancestor type conversion must be an
10704 -- object (not an expression).
10705
10706 if Is_Tagged_Type (Target_Typ)
10707 and then not Is_Class_Wide_Type (Target_Typ)
10708 and then Is_Tagged_Type (Operand_Typ)
10709 and then not Is_Class_Wide_Type (Operand_Typ)
10710 and then Is_Ancestor (Target_Typ, Operand_Typ)
ce5ba43a 10711 and then not Is_SPARK_05_Object_Reference (Operand)
e24329cd 10712 then
ce5ba43a 10713 Check_SPARK_05_Restriction ("object required", Operand);
e24329cd
YM
10714 end if;
10715
dec6faf1
AC
10716 Analyze_Dimension (N);
10717
996ae0b0 10718 -- Note: we do the Eval_Type_Conversion call before applying the
d81b4bfe
TQ
10719 -- required checks for a subtype conversion. This is important, since
10720 -- both are prepared under certain circumstances to change the type
10721 -- conversion to a constraint error node, but in the case of
10722 -- Eval_Type_Conversion this may reflect an illegality in the static
10723 -- case, and we would miss the illegality (getting only a warning
10724 -- message), if we applied the type conversion checks first.
996ae0b0
RK
10725
10726 Eval_Type_Conversion (N);
10727
d81b4bfe
TQ
10728 -- Even when evaluation is not possible, we may be able to simplify the
10729 -- conversion or its expression. This needs to be done before applying
10730 -- checks, since otherwise the checks may use the original expression
10731 -- and defeat the simplifications. This is specifically the case for
10732 -- elimination of the floating-point Truncation attribute in
10733 -- float-to-int conversions.
0669bebe
GB
10734
10735 Simplify_Type_Conversion (N);
10736
d81b4bfe
TQ
10737 -- If after evaluation we still have a type conversion, then we may need
10738 -- to apply checks required for a subtype conversion.
996ae0b0
RK
10739
10740 -- Skip these type conversion checks if universal fixed operands
10741 -- operands involved, since range checks are handled separately for
10742 -- these cases (in the appropriate Expand routines in unit Exp_Fixd).
10743
10744 if Nkind (N) = N_Type_Conversion
b7d1f17f
HK
10745 and then not Is_Generic_Type (Root_Type (Target_Typ))
10746 and then Target_Typ /= Universal_Fixed
10747 and then Operand_Typ /= Universal_Fixed
996ae0b0
RK
10748 then
10749 Apply_Type_Conversion_Checks (N);
10750 end if;
10751
d81b4bfe
TQ
10752 -- Issue warning for conversion of simple object to its own type. We
10753 -- have to test the original nodes, since they may have been rewritten
10754 -- by various optimizations.
fbf5a39b
AC
10755
10756 Orig_N := Original_Node (N);
996ae0b0 10757
ae2aa109
AC
10758 -- Here we test for a redundant conversion if the warning mode is
10759 -- active (and was not locally reset), and we have a type conversion
10760 -- from source not appearing in a generic instance.
10761
10762 if Test_Redundant
fbf5a39b 10763 and then Nkind (Orig_N) = N_Type_Conversion
ae2aa109 10764 and then Comes_From_Source (Orig_N)
5453d5bd 10765 and then not In_Instance
996ae0b0 10766 then
fbf5a39b 10767 Orig_N := Original_Node (Expression (Orig_N));
b7d1f17f 10768 Orig_T := Target_Typ;
fbf5a39b
AC
10769
10770 -- If the node is part of a larger expression, the Target_Type
10771 -- may not be the original type of the node if the context is a
10772 -- condition. Recover original type to see if conversion is needed.
10773
10774 if Is_Boolean_Type (Orig_T)
10775 and then Nkind (Parent (N)) in N_Op
10776 then
10777 Orig_T := Etype (Parent (N));
10778 end if;
10779
4adf3c50 10780 -- If we have an entity name, then give the warning if the entity
ae2aa109
AC
10781 -- is the right type, or if it is a loop parameter covered by the
10782 -- original type (that's needed because loop parameters have an
10783 -- odd subtype coming from the bounds).
10784
10785 if (Is_Entity_Name (Orig_N)
98bf4cf4
AC
10786 and then
10787 (Etype (Entity (Orig_N)) = Orig_T
10788 or else
10789 (Ekind (Entity (Orig_N)) = E_Loop_Parameter
10790 and then Covers (Orig_T, Etype (Entity (Orig_N))))))
ae2aa109 10791
477bd732 10792 -- If not an entity, then type of expression must match
ae2aa109
AC
10793
10794 or else Etype (Orig_N) = Orig_T
fbf5a39b 10795 then
4b2d2c13
AC
10796 -- One more check, do not give warning if the analyzed conversion
10797 -- has an expression with non-static bounds, and the bounds of the
10798 -- target are static. This avoids junk warnings in cases where the
10799 -- conversion is necessary to establish staticness, for example in
10800 -- a case statement.
10801
10802 if not Is_OK_Static_Subtype (Operand_Typ)
10803 and then Is_OK_Static_Subtype (Target_Typ)
10804 then
10805 null;
10806
5cc9353d
RD
10807 -- Finally, if this type conversion occurs in a context requiring
10808 -- a prefix, and the expression is a qualified expression then the
10809 -- type conversion is not redundant, since a qualified expression
10810 -- is not a prefix, whereas a type conversion is. For example, "X
10811 -- := T'(Funx(...)).Y;" is illegal because a selected component
10812 -- requires a prefix, but a type conversion makes it legal: "X :=
10813 -- T(T'(Funx(...))).Y;"
4adf3c50 10814
9db0b232
AC
10815 -- In Ada 2012, a qualified expression is a name, so this idiom is
10816 -- no longer needed, but we still suppress the warning because it
10817 -- seems unfriendly for warnings to pop up when you switch to the
10818 -- newer language version.
be257e99
AC
10819
10820 elsif Nkind (Orig_N) = N_Qualified_Expression
f5d96d00
AC
10821 and then Nkind_In (Parent (N), N_Attribute_Reference,
10822 N_Indexed_Component,
10823 N_Selected_Component,
10824 N_Slice,
10825 N_Explicit_Dereference)
be257e99
AC
10826 then
10827 null;
10828
2352eadb
AC
10829 -- Never warn on conversion to Long_Long_Integer'Base since
10830 -- that is most likely an artifact of the extended overflow
10831 -- checking and comes from complex expanded code.
10832
10833 elsif Orig_T = Base_Type (Standard_Long_Long_Integer) then
10834 null;
10835
ae2aa109
AC
10836 -- Here we give the redundant conversion warning. If it is an
10837 -- entity, give the name of the entity in the message. If not,
10838 -- just mention the expression.
4b2d2c13 10839
324ac540
AC
10840 -- Shoudn't we test Warn_On_Redundant_Constructs here ???
10841
4b2d2c13 10842 else
ae2aa109
AC
10843 if Is_Entity_Name (Orig_N) then
10844 Error_Msg_Node_2 := Orig_T;
10845 Error_Msg_NE -- CODEFIX
324ac540 10846 ("??redundant conversion, & is of type &!",
ae2aa109
AC
10847 N, Entity (Orig_N));
10848 else
10849 Error_Msg_NE
324ac540 10850 ("??redundant conversion, expression is of type&!",
ae2aa109
AC
10851 N, Orig_T);
10852 end if;
4b2d2c13 10853 end if;
fbf5a39b 10854 end if;
996ae0b0 10855 end if;
758c442c 10856
b7d1f17f 10857 -- Ada 2005 (AI-251): Handle class-wide interface type conversions.
0669bebe
GB
10858 -- No need to perform any interface conversion if the type of the
10859 -- expression coincides with the target type.
758c442c 10860
0791fbe9 10861 if Ada_Version >= Ada_2005
4460a9bc 10862 and then Expander_Active
b7d1f17f 10863 and then Operand_Typ /= Target_Typ
0669bebe 10864 then
b7d1f17f
HK
10865 declare
10866 Opnd : Entity_Id := Operand_Typ;
10867 Target : Entity_Id := Target_Typ;
758c442c 10868
b7d1f17f 10869 begin
e4dc3327
AC
10870 -- If the type of the operand is a limited view, use nonlimited
10871 -- view when available. If it is a class-wide type, recover the
10872 -- class-wide type of the nonlimited view.
414c6563 10873
47346923
AC
10874 if From_Limited_With (Opnd)
10875 and then Has_Non_Limited_View (Opnd)
10876 then
10877 Opnd := Non_Limited_View (Opnd);
10878 Set_Etype (Expression (N), Opnd);
414c6563
AC
10879 end if;
10880
b7d1f17f 10881 if Is_Access_Type (Opnd) then
841dd0f5 10882 Opnd := Designated_Type (Opnd);
1420b484
JM
10883 end if;
10884
b7d1f17f 10885 if Is_Access_Type (Target_Typ) then
841dd0f5 10886 Target := Designated_Type (Target);
4197ae1e 10887 end if;
c8ef728f 10888
b7d1f17f
HK
10889 if Opnd = Target then
10890 null;
c8ef728f 10891
b7d1f17f 10892 -- Conversion from interface type
ea985d95 10893
b7d1f17f 10894 elsif Is_Interface (Opnd) then
ea985d95 10895
b7d1f17f 10896 -- Ada 2005 (AI-217): Handle entities from limited views
aa180613 10897
7b56a91b 10898 if From_Limited_With (Opnd) then
b7d1f17f 10899 Error_Msg_Qual_Level := 99;
305caf42
AC
10900 Error_Msg_NE -- CODEFIX
10901 ("missing WITH clause on package &", N,
b7d1f17f
HK
10902 Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
10903 Error_Msg_N
10904 ("type conversions require visibility of the full view",
10905 N);
aa180613 10906
7b56a91b 10907 elsif From_Limited_With (Target)
aa5147f0
ES
10908 and then not
10909 (Is_Access_Type (Target_Typ)
10910 and then Present (Non_Limited_View (Etype (Target))))
10911 then
b7d1f17f 10912 Error_Msg_Qual_Level := 99;
305caf42
AC
10913 Error_Msg_NE -- CODEFIX
10914 ("missing WITH clause on package &", N,
b7d1f17f
HK
10915 Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
10916 Error_Msg_N
10917 ("type conversions require visibility of the full view",
10918 N);
aa180613 10919
b7d1f17f 10920 else
f6f4d8d4 10921 Expand_Interface_Conversion (N);
b7d1f17f
HK
10922 end if;
10923
10924 -- Conversion to interface type
10925
10926 elsif Is_Interface (Target) then
10927
10928 -- Handle subtypes
10929
8a95f4e8 10930 if Ekind_In (Opnd, E_Protected_Subtype, E_Task_Subtype) then
b7d1f17f
HK
10931 Opnd := Etype (Opnd);
10932 end if;
10933
f6f4d8d4
JM
10934 if Is_Class_Wide_Type (Opnd)
10935 or else Interface_Present_In_Ancestor
10936 (Typ => Opnd,
10937 Iface => Target)
b7d1f17f 10938 then
b7d1f17f 10939 Expand_Interface_Conversion (N);
f6f4d8d4
JM
10940 else
10941 Error_Msg_Name_1 := Chars (Etype (Target));
10942 Error_Msg_Name_2 := Chars (Opnd);
10943 Error_Msg_N
10944 ("wrong interface conversion (% is not a progenitor "
10945 & "of %)", N);
b7d1f17f
HK
10946 end if;
10947 end if;
10948 end;
758c442c 10949 end if;
804fc056
AC
10950
10951 -- Ada 2012: if target type has predicates, the result requires a
10952 -- predicate check. If the context is a call to another predicate
10953 -- check we must prevent infinite recursion.
10954
10955 if Has_Predicates (Target_Typ) then
10956 if Nkind (Parent (N)) = N_Function_Call
10957 and then Present (Name (Parent (N)))
fc142f63
AC
10958 and then (Is_Predicate_Function (Entity (Name (Parent (N))))
10959 or else
10960 Is_Predicate_Function_M (Entity (Name (Parent (N)))))
804fc056
AC
10961 then
10962 null;
10963
10964 else
10965 Apply_Predicate_Check (N, Target_Typ);
10966 end if;
10967 end if;
98bf4cf4
AC
10968
10969 -- If at this stage we have a real to integer conversion, make sure
10970 -- that the Do_Range_Check flag is set, because such conversions in
d26d790d
AC
10971 -- general need a range check. We only need this if expansion is off
10972 -- or we are in GNATProve mode.
98bf4cf4
AC
10973
10974 if Nkind (N) = N_Type_Conversion
d26d790d 10975 and then (GNATprove_Mode or not Expander_Active)
98bf4cf4
AC
10976 and then Is_Integer_Type (Target_Typ)
10977 and then Is_Real_Type (Operand_Typ)
10978 then
10979 Set_Do_Range_Check (Operand);
10980 end if;
996ae0b0
RK
10981 end Resolve_Type_Conversion;
10982
10983 ----------------------
10984 -- Resolve_Unary_Op --
10985 ----------------------
10986
10987 procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
fbf5a39b
AC
10988 B_Typ : constant Entity_Id := Base_Type (Typ);
10989 R : constant Node_Id := Right_Opnd (N);
10990 OK : Boolean;
10991 Lo : Uint;
10992 Hi : Uint;
996ae0b0
RK
10993
10994 begin
7a489a2b
AC
10995 if Is_Modular_Integer_Type (Typ) and then Nkind (N) /= N_Op_Not then
10996 Error_Msg_Name_1 := Chars (Typ);
ce5ba43a 10997 Check_SPARK_05_Restriction
7a489a2b
AC
10998 ("unary operator not defined for modular type%", N);
10999 end if;
11000
b7d1f17f 11001 -- Deal with intrinsic unary operators
996ae0b0 11002
fbf5a39b
AC
11003 if Comes_From_Source (N)
11004 and then Ekind (Entity (N)) = E_Function
11005 and then Is_Imported (Entity (N))
11006 and then Is_Intrinsic_Subprogram (Entity (N))
11007 then
11008 Resolve_Intrinsic_Unary_Operator (N, Typ);
11009 return;
11010 end if;
11011
0669bebe
GB
11012 -- Deal with universal cases
11013
996ae0b0 11014 if Etype (R) = Universal_Integer
0669bebe
GB
11015 or else
11016 Etype (R) = Universal_Real
996ae0b0
RK
11017 then
11018 Check_For_Visible_Operator (N, B_Typ);
11019 end if;
11020
11021 Set_Etype (N, B_Typ);
11022 Resolve (R, B_Typ);
fbf5a39b 11023
9ebe3743
HK
11024 -- Generate warning for expressions like abs (x mod 2)
11025
11026 if Warn_On_Redundant_Constructs
11027 and then Nkind (N) = N_Op_Abs
11028 then
11029 Determine_Range (Right_Opnd (N), OK, Lo, Hi);
11030
11031 if OK and then Hi >= Lo and then Lo >= 0 then
305caf42 11032 Error_Msg_N -- CODEFIX
324ac540 11033 ("?r?abs applied to known non-negative value has no effect", N);
9ebe3743
HK
11034 end if;
11035 end if;
11036
0669bebe
GB
11037 -- Deal with reference generation
11038
996ae0b0 11039 Check_Unset_Reference (R);
fbf5a39b 11040 Generate_Operator_Reference (N, B_Typ);
dec6faf1 11041 Analyze_Dimension (N);
996ae0b0
RK
11042 Eval_Unary_Op (N);
11043
11044 -- Set overflow checking bit. Much cleverer code needed here eventually
11045 -- and perhaps the Resolve routines should be separated for the various
11046 -- arithmetic operations, since they will need different processing ???
11047
11048 if Nkind (N) in N_Op then
11049 if not Overflow_Checks_Suppressed (Etype (N)) then
fbf5a39b 11050 Enable_Overflow_Check (N);
996ae0b0
RK
11051 end if;
11052 end if;
0669bebe 11053
d81b4bfe
TQ
11054 -- Generate warning for expressions like -5 mod 3 for integers. No need
11055 -- to worry in the floating-point case, since parens do not affect the
11056 -- result so there is no point in giving in a warning.
0669bebe
GB
11057
11058 declare
11059 Norig : constant Node_Id := Original_Node (N);
11060 Rorig : Node_Id;
11061 Val : Uint;
11062 HB : Uint;
11063 LB : Uint;
11064 Lval : Uint;
11065 Opnd : Node_Id;
11066
11067 begin
11068 if Warn_On_Questionable_Missing_Parens
11069 and then Comes_From_Source (Norig)
11070 and then Is_Integer_Type (Typ)
11071 and then Nkind (Norig) = N_Op_Minus
11072 then
11073 Rorig := Original_Node (Right_Opnd (Norig));
11074
11075 -- We are looking for cases where the right operand is not
f3d57416 11076 -- parenthesized, and is a binary operator, multiply, divide, or
0669bebe
GB
11077 -- mod. These are the cases where the grouping can affect results.
11078
11079 if Paren_Count (Rorig) = 0
45fc7ddb 11080 and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
0669bebe
GB
11081 then
11082 -- For mod, we always give the warning, since the value is
11083 -- affected by the parenthesization (e.g. (-5) mod 315 /=
d81b4bfe 11084 -- -(5 mod 315)). But for the other cases, the only concern is
0669bebe
GB
11085 -- overflow, e.g. for the case of 8 big signed (-(2 * 64)
11086 -- overflows, but (-2) * 64 does not). So we try to give the
11087 -- message only when overflow is possible.
11088
11089 if Nkind (Rorig) /= N_Op_Mod
11090 and then Compile_Time_Known_Value (R)
11091 then
11092 Val := Expr_Value (R);
11093
11094 if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
11095 HB := Expr_Value (Type_High_Bound (Typ));
11096 else
11097 HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
11098 end if;
11099
11100 if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
11101 LB := Expr_Value (Type_Low_Bound (Typ));
11102 else
11103 LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
11104 end if;
11105
d81b4bfe
TQ
11106 -- Note that the test below is deliberately excluding the
11107 -- largest negative number, since that is a potentially
0669bebe
GB
11108 -- troublesome case (e.g. -2 * x, where the result is the
11109 -- largest negative integer has an overflow with 2 * x).
11110
11111 if Val > LB and then Val <= HB then
11112 return;
11113 end if;
11114 end if;
11115
11116 -- For the multiplication case, the only case we have to worry
11117 -- about is when (-a)*b is exactly the largest negative number
11118 -- so that -(a*b) can cause overflow. This can only happen if
11119 -- a is a power of 2, and more generally if any operand is a
11120 -- constant that is not a power of 2, then the parentheses
11121 -- cannot affect whether overflow occurs. We only bother to
11122 -- test the left most operand
11123
11124 -- Loop looking at left operands for one that has known value
11125
11126 Opnd := Rorig;
11127 Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
11128 if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
11129 Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
11130
11131 -- Operand value of 0 or 1 skips warning
11132
11133 if Lval <= 1 then
11134 return;
11135
11136 -- Otherwise check power of 2, if power of 2, warn, if
11137 -- anything else, skip warning.
11138
11139 else
11140 while Lval /= 2 loop
11141 if Lval mod 2 = 1 then
11142 return;
11143 else
11144 Lval := Lval / 2;
11145 end if;
11146 end loop;
11147
11148 exit Opnd_Loop;
11149 end if;
11150 end if;
11151
11152 -- Keep looking at left operands
11153
11154 Opnd := Left_Opnd (Opnd);
11155 end loop Opnd_Loop;
11156
11157 -- For rem or "/" we can only have a problematic situation
11158 -- if the divisor has a value of minus one or one. Otherwise
11159 -- overflow is impossible (divisor > 1) or we have a case of
11160 -- division by zero in any case.
11161
45fc7ddb 11162 if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
0669bebe
GB
11163 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
11164 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
11165 then
11166 return;
11167 end if;
11168
11169 -- If we fall through warning should be issued
11170
324ac540
AC
11171 -- Shouldn't we test Warn_On_Questionable_Missing_Parens ???
11172
ed2233dc 11173 Error_Msg_N
324ac540 11174 ("??unary minus expression should be parenthesized here!", N);
0669bebe
GB
11175 end if;
11176 end if;
11177 end;
996ae0b0
RK
11178 end Resolve_Unary_Op;
11179
11180 ----------------------------------
11181 -- Resolve_Unchecked_Expression --
11182 ----------------------------------
11183
11184 procedure Resolve_Unchecked_Expression
11185 (N : Node_Id;
11186 Typ : Entity_Id)
11187 is
11188 begin
11189 Resolve (Expression (N), Typ, Suppress => All_Checks);
11190 Set_Etype (N, Typ);
11191 end Resolve_Unchecked_Expression;
11192
11193 ---------------------------------------
11194 -- Resolve_Unchecked_Type_Conversion --
11195 ---------------------------------------
11196
11197 procedure Resolve_Unchecked_Type_Conversion
11198 (N : Node_Id;
11199 Typ : Entity_Id)
11200 is
07fc65c4
GB
11201 pragma Warnings (Off, Typ);
11202
996ae0b0
RK
11203 Operand : constant Node_Id := Expression (N);
11204 Opnd_Type : constant Entity_Id := Etype (Operand);
11205
11206 begin
a77842bd 11207 -- Resolve operand using its own type
996ae0b0
RK
11208
11209 Resolve (Operand, Opnd_Type);
36428cc4
AC
11210
11211 -- In an inlined context, the unchecked conversion may be applied
11212 -- to a literal, in which case its type is the type of the context.
11213 -- (In other contexts conversions cannot apply to literals).
11214
11215 if In_Inlined_Body
480156b2
AC
11216 and then (Opnd_Type = Any_Character or else
11217 Opnd_Type = Any_Integer or else
11218 Opnd_Type = Any_Real)
36428cc4
AC
11219 then
11220 Set_Etype (Operand, Typ);
11221 end if;
11222
dec6faf1 11223 Analyze_Dimension (N);
996ae0b0 11224 Eval_Unchecked_Conversion (N);
996ae0b0
RK
11225 end Resolve_Unchecked_Type_Conversion;
11226
11227 ------------------------------
11228 -- Rewrite_Operator_As_Call --
11229 ------------------------------
11230
11231 procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
fbf5a39b
AC
11232 Loc : constant Source_Ptr := Sloc (N);
11233 Actuals : constant List_Id := New_List;
996ae0b0
RK
11234 New_N : Node_Id;
11235
11236 begin
21d7ef70 11237 if Nkind (N) in N_Binary_Op then
996ae0b0
RK
11238 Append (Left_Opnd (N), Actuals);
11239 end if;
11240
11241 Append (Right_Opnd (N), Actuals);
11242
11243 New_N :=
11244 Make_Function_Call (Sloc => Loc,
11245 Name => New_Occurrence_Of (Nam, Loc),
11246 Parameter_Associations => Actuals);
11247
11248 Preserve_Comes_From_Source (New_N, N);
11249 Preserve_Comes_From_Source (Name (New_N), N);
11250 Rewrite (N, New_N);
11251 Set_Etype (N, Etype (Nam));
11252 end Rewrite_Operator_As_Call;
11253
11254 ------------------------------
11255 -- Rewrite_Renamed_Operator --
11256 ------------------------------
11257
0ab80019
AC
11258 procedure Rewrite_Renamed_Operator
11259 (N : Node_Id;
11260 Op : Entity_Id;
11261 Typ : Entity_Id)
11262 is
996ae0b0
RK
11263 Nam : constant Name_Id := Chars (Op);
11264 Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
11265 Op_Node : Node_Id;
11266
11267 begin
8d81fb4e
AC
11268 -- Do not perform this transformation within a pre/postcondition,
11269 -- because the expression will be re-analyzed, and the transformation
11270 -- might affect the visibility of the operator, e.g. in an instance.
11271
11272 if In_Assertion_Expr > 0 then
11273 return;
11274 end if;
11275
d81b4bfe
TQ
11276 -- Rewrite the operator node using the real operator, not its renaming.
11277 -- Exclude user-defined intrinsic operations of the same name, which are
11278 -- treated separately and rewritten as calls.
996ae0b0 11279
964f13da 11280 if Ekind (Op) /= E_Function or else Chars (N) /= Nam then
996ae0b0
RK
11281 Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
11282 Set_Chars (Op_Node, Nam);
11283 Set_Etype (Op_Node, Etype (N));
11284 Set_Entity (Op_Node, Op);
11285 Set_Right_Opnd (Op_Node, Right_Opnd (N));
11286
b7d1f17f
HK
11287 -- Indicate that both the original entity and its renaming are
11288 -- referenced at this point.
fbf5a39b
AC
11289
11290 Generate_Reference (Entity (N), N);
996ae0b0
RK
11291 Generate_Reference (Op, N);
11292
11293 if Is_Binary then
11294 Set_Left_Opnd (Op_Node, Left_Opnd (N));
11295 end if;
11296
11297 Rewrite (N, Op_Node);
0ab80019 11298
1366997b
AC
11299 -- If the context type is private, add the appropriate conversions so
11300 -- that the operator is applied to the full view. This is done in the
11301 -- routines that resolve intrinsic operators.
0ab80019
AC
11302
11303 if Is_Intrinsic_Subprogram (Op)
11304 and then Is_Private_Type (Typ)
11305 then
11306 case Nkind (N) is
11307 when N_Op_Add | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
11308 N_Op_Expon | N_Op_Mod | N_Op_Rem =>
11309 Resolve_Intrinsic_Operator (N, Typ);
11310
d81b4bfe 11311 when N_Op_Plus | N_Op_Minus | N_Op_Abs =>
0ab80019
AC
11312 Resolve_Intrinsic_Unary_Operator (N, Typ);
11313
11314 when others =>
11315 Resolve (N, Typ);
11316 end case;
11317 end if;
11318
964f13da
RD
11319 elsif Ekind (Op) = E_Function and then Is_Intrinsic_Subprogram (Op) then
11320
1366997b
AC
11321 -- Operator renames a user-defined operator of the same name. Use the
11322 -- original operator in the node, which is the one Gigi knows about.
0ab80019
AC
11323
11324 Set_Entity (N, Op);
11325 Set_Is_Overloaded (N, False);
996ae0b0
RK
11326 end if;
11327 end Rewrite_Renamed_Operator;
11328
11329 -----------------------
11330 -- Set_Slice_Subtype --
11331 -----------------------
11332
1366997b
AC
11333 -- Build an implicit subtype declaration to represent the type delivered by
11334 -- the slice. This is an abbreviated version of an array subtype. We define
11335 -- an index subtype for the slice, using either the subtype name or the
11336 -- discrete range of the slice. To be consistent with index usage elsewhere
11337 -- we create a list header to hold the single index. This list is not
11338 -- otherwise attached to the syntax tree.
996ae0b0
RK
11339
11340 procedure Set_Slice_Subtype (N : Node_Id) is
11341 Loc : constant Source_Ptr := Sloc (N);
fbf5a39b 11342 Index_List : constant List_Id := New_List;
996ae0b0 11343 Index : Node_Id;
996ae0b0
RK
11344 Index_Subtype : Entity_Id;
11345 Index_Type : Entity_Id;
11346 Slice_Subtype : Entity_Id;
11347 Drange : constant Node_Id := Discrete_Range (N);
11348
11349 begin
08cd7c2f
AC
11350 Index_Type := Base_Type (Etype (Drange));
11351
996ae0b0
RK
11352 if Is_Entity_Name (Drange) then
11353 Index_Subtype := Entity (Drange);
11354
11355 else
11356 -- We force the evaluation of a range. This is definitely needed in
11357 -- the renamed case, and seems safer to do unconditionally. Note in
11358 -- any case that since we will create and insert an Itype referring
11359 -- to this range, we must make sure any side effect removal actions
11360 -- are inserted before the Itype definition.
11361
11362 if Nkind (Drange) = N_Range then
11363 Force_Evaluation (Low_Bound (Drange));
11364 Force_Evaluation (High_Bound (Drange));
996ae0b0 11365
08cd7c2f
AC
11366 -- If the discrete range is given by a subtype indication, the
11367 -- type of the slice is the base of the subtype mark.
11368
11369 elsif Nkind (Drange) = N_Subtype_Indication then
11370 declare
11371 R : constant Node_Id := Range_Expression (Constraint (Drange));
11372 begin
11373 Index_Type := Base_Type (Entity (Subtype_Mark (Drange)));
11374 Force_Evaluation (Low_Bound (R));
11375 Force_Evaluation (High_Bound (R));
11376 end;
11377 end if;
996ae0b0
RK
11378
11379 Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
11380
8a95f4e8 11381 -- Take a new copy of Drange (where bounds have been rewritten to
3c1ecd7e
AC
11382 -- reference side-effect-free names). Using a separate tree ensures
11383 -- that further expansion (e.g. while rewriting a slice assignment
8a95f4e8
RD
11384 -- into a FOR loop) does not attempt to remove side effects on the
11385 -- bounds again (which would cause the bounds in the index subtype
11386 -- definition to refer to temporaries before they are defined) (the
11387 -- reason is that some names are considered side effect free here
11388 -- for the subtype, but not in the context of a loop iteration
11389 -- scheme).
11390
11391 Set_Scalar_Range (Index_Subtype, New_Copy_Tree (Drange));
4230bdb7 11392 Set_Parent (Scalar_Range (Index_Subtype), Index_Subtype);
996ae0b0
RK
11393 Set_Etype (Index_Subtype, Index_Type);
11394 Set_Size_Info (Index_Subtype, Index_Type);
11395 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
11396 end if;
11397
11398 Slice_Subtype := Create_Itype (E_Array_Subtype, N);
11399
11400 Index := New_Occurrence_Of (Index_Subtype, Loc);
11401 Set_Etype (Index, Index_Subtype);
11402 Append (Index, Index_List);
11403
996ae0b0
RK
11404 Set_First_Index (Slice_Subtype, Index);
11405 Set_Etype (Slice_Subtype, Base_Type (Etype (N)));
11406 Set_Is_Constrained (Slice_Subtype, True);
996ae0b0 11407
8a95f4e8
RD
11408 Check_Compile_Time_Size (Slice_Subtype);
11409
b7d1f17f
HK
11410 -- The Etype of the existing Slice node is reset to this slice subtype.
11411 -- Its bounds are obtained from its first index.
996ae0b0
RK
11412
11413 Set_Etype (N, Slice_Subtype);
11414
5cc9353d
RD
11415 -- For packed slice subtypes, freeze immediately (except in the case of
11416 -- being in a "spec expression" where we never freeze when we first see
11417 -- the expression).
8a95f4e8
RD
11418
11419 if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
11420 Freeze_Itype (Slice_Subtype, N);
996ae0b0 11421
cfab0c49
AC
11422 -- For all other cases insert an itype reference in the slice's actions
11423 -- so that the itype is frozen at the proper place in the tree (i.e. at
11424 -- the point where actions for the slice are analyzed). Note that this
11425 -- is different from freezing the itype immediately, which might be
6ff6152d
ES
11426 -- premature (e.g. if the slice is within a transient scope). This needs
11427 -- to be done only if expansion is enabled.
cfab0c49 11428
4460a9bc 11429 elsif Expander_Active then
8a95f4e8
RD
11430 Ensure_Defined (Typ => Slice_Subtype, N => N);
11431 end if;
996ae0b0
RK
11432 end Set_Slice_Subtype;
11433
11434 --------------------------------
11435 -- Set_String_Literal_Subtype --
11436 --------------------------------
11437
11438 procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
c8ef728f
ES
11439 Loc : constant Source_Ptr := Sloc (N);
11440 Low_Bound : constant Node_Id :=
d81b4bfe 11441 Type_Low_Bound (Etype (First_Index (Typ)));
996ae0b0
RK
11442 Subtype_Id : Entity_Id;
11443
11444 begin
11445 if Nkind (N) /= N_String_Literal then
11446 return;
996ae0b0
RK
11447 end if;
11448
c8ef728f 11449 Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
91b1417d
AC
11450 Set_String_Literal_Length (Subtype_Id, UI_From_Int
11451 (String_Length (Strval (N))));
c8ef728f
ES
11452 Set_Etype (Subtype_Id, Base_Type (Typ));
11453 Set_Is_Constrained (Subtype_Id);
11454 Set_Etype (N, Subtype_Id);
11455
1366997b
AC
11456 -- The low bound is set from the low bound of the corresponding index
11457 -- type. Note that we do not store the high bound in the string literal
11458 -- subtype, but it can be deduced if necessary from the length and the
11459 -- low bound.
996ae0b0 11460
5f44f0d4 11461 if Is_OK_Static_Expression (Low_Bound) then
c8ef728f 11462 Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
996ae0b0 11463
5f44f0d4
AC
11464 -- If the lower bound is not static we create a range for the string
11465 -- literal, using the index type and the known length of the literal.
11466 -- The index type is not necessarily Positive, so the upper bound is
11467 -- computed as T'Val (T'Pos (Low_Bound) + L - 1).
c8ef728f 11468
5f44f0d4 11469 else
c8ef728f 11470 declare
5f44f0d4
AC
11471 Index_List : constant List_Id := New_List;
11472 Index_Type : constant Entity_Id := Etype (First_Index (Typ));
11473 High_Bound : constant Node_Id :=
53f29d4f
AC
11474 Make_Attribute_Reference (Loc,
11475 Attribute_Name => Name_Val,
11476 Prefix =>
11477 New_Occurrence_Of (Index_Type, Loc),
11478 Expressions => New_List (
11479 Make_Op_Add (Loc,
11480 Left_Opnd =>
11481 Make_Attribute_Reference (Loc,
11482 Attribute_Name => Name_Pos,
11483 Prefix =>
11484 New_Occurrence_Of (Index_Type, Loc),
11485 Expressions =>
11486 New_List (New_Copy_Tree (Low_Bound))),
11487 Right_Opnd =>
11488 Make_Integer_Literal (Loc,
11489 String_Length (Strval (N)) - 1))));
c0b11850 11490
c8ef728f 11491 Array_Subtype : Entity_Id;
c8ef728f
ES
11492 Drange : Node_Id;
11493 Index : Node_Id;
5f44f0d4 11494 Index_Subtype : Entity_Id;
c8ef728f
ES
11495
11496 begin
56e94186
AC
11497 if Is_Integer_Type (Index_Type) then
11498 Set_String_Literal_Low_Bound
11499 (Subtype_Id, Make_Integer_Literal (Loc, 1));
11500
11501 else
11502 -- If the index type is an enumeration type, build bounds
11503 -- expression with attributes.
11504
11505 Set_String_Literal_Low_Bound
11506 (Subtype_Id,
11507 Make_Attribute_Reference (Loc,
11508 Attribute_Name => Name_First,
11509 Prefix =>
11510 New_Occurrence_Of (Base_Type (Index_Type), Loc)));
11511 Set_Etype (String_Literal_Low_Bound (Subtype_Id), Index_Type);
11512 end if;
11513
c0b11850
AC
11514 Analyze_And_Resolve (String_Literal_Low_Bound (Subtype_Id));
11515
11516 -- Build bona fide subtype for the string, and wrap it in an
11517 -- unchecked conversion, because the backend expects the
11518 -- String_Literal_Subtype to have a static lower bound.
11519
c8ef728f
ES
11520 Index_Subtype :=
11521 Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
0669bebe 11522 Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
c8ef728f
ES
11523 Set_Scalar_Range (Index_Subtype, Drange);
11524 Set_Parent (Drange, N);
11525 Analyze_And_Resolve (Drange, Index_Type);
11526
36fcf362
RD
11527 -- In the context, the Index_Type may already have a constraint,
11528 -- so use common base type on string subtype. The base type may
11529 -- be used when generating attributes of the string, for example
11530 -- in the context of a slice assignment.
11531
4adf3c50
AC
11532 Set_Etype (Index_Subtype, Base_Type (Index_Type));
11533 Set_Size_Info (Index_Subtype, Index_Type);
11534 Set_RM_Size (Index_Subtype, RM_Size (Index_Type));
c8ef728f
ES
11535
11536 Array_Subtype := Create_Itype (E_Array_Subtype, N);
11537
11538 Index := New_Occurrence_Of (Index_Subtype, Loc);
11539 Set_Etype (Index, Index_Subtype);
11540 Append (Index, Index_List);
11541
11542 Set_First_Index (Array_Subtype, Index);
11543 Set_Etype (Array_Subtype, Base_Type (Typ));
11544 Set_Is_Constrained (Array_Subtype, True);
c8ef728f
ES
11545
11546 Rewrite (N,
11547 Make_Unchecked_Type_Conversion (Loc,
11548 Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
5f44f0d4 11549 Expression => Relocate_Node (N)));
c8ef728f
ES
11550 Set_Etype (N, Array_Subtype);
11551 end;
11552 end if;
996ae0b0
RK
11553 end Set_String_Literal_Subtype;
11554
0669bebe
GB
11555 ------------------------------
11556 -- Simplify_Type_Conversion --
11557 ------------------------------
11558
11559 procedure Simplify_Type_Conversion (N : Node_Id) is
11560 begin
11561 if Nkind (N) = N_Type_Conversion then
11562 declare
11563 Operand : constant Node_Id := Expression (N);
11564 Target_Typ : constant Entity_Id := Etype (N);
11565 Opnd_Typ : constant Entity_Id := Etype (Operand);
11566
11567 begin
24228312
AC
11568 -- Special processing if the conversion is the expression of a
11569 -- Rounding or Truncation attribute reference. In this case we
11570 -- replace:
0669bebe 11571
24228312 11572 -- ityp (ftyp'Rounding (x)) or ityp (ftyp'Truncation (x))
0669bebe
GB
11573
11574 -- by
11575
11576 -- ityp (x)
11577
24228312
AC
11578 -- with the Float_Truncate flag set to False or True respectively,
11579 -- which is more efficient.
0669bebe 11580
24228312
AC
11581 if Is_Floating_Point_Type (Opnd_Typ)
11582 and then
11583 (Is_Integer_Type (Target_Typ)
7a5b62b0
AC
11584 or else (Is_Fixed_Point_Type (Target_Typ)
11585 and then Conversion_OK (N)))
24228312 11586 and then Nkind (Operand) = N_Attribute_Reference
7a5b62b0
AC
11587 and then Nam_In (Attribute_Name (Operand), Name_Rounding,
11588 Name_Truncation)
0669bebe 11589 then
24228312
AC
11590 declare
11591 Truncate : constant Boolean :=
7a5b62b0 11592 Attribute_Name (Operand) = Name_Truncation;
24228312
AC
11593 begin
11594 Rewrite (Operand,
11595 Relocate_Node (First (Expressions (Operand))));
11596 Set_Float_Truncate (N, Truncate);
11597 end;
0669bebe
GB
11598 end if;
11599 end;
11600 end if;
11601 end Simplify_Type_Conversion;
11602
996ae0b0
RK
11603 -----------------------------
11604 -- Unique_Fixed_Point_Type --
11605 -----------------------------
11606
11607 function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
11608 T1 : Entity_Id := Empty;
11609 T2 : Entity_Id;
11610 Item : Node_Id;
11611 Scop : Entity_Id;
11612
11613 procedure Fixed_Point_Error;
d81b4bfe
TQ
11614 -- Give error messages for true ambiguity. Messages are posted on node
11615 -- N, and entities T1, T2 are the possible interpretations.
a77842bd
TQ
11616
11617 -----------------------
11618 -- Fixed_Point_Error --
11619 -----------------------
996ae0b0
RK
11620
11621 procedure Fixed_Point_Error is
11622 begin
ed2233dc
AC
11623 Error_Msg_N ("ambiguous universal_fixed_expression", N);
11624 Error_Msg_NE ("\\possible interpretation as}", N, T1);
11625 Error_Msg_NE ("\\possible interpretation as}", N, T2);
996ae0b0
RK
11626 end Fixed_Point_Error;
11627
a77842bd
TQ
11628 -- Start of processing for Unique_Fixed_Point_Type
11629
996ae0b0
RK
11630 begin
11631 -- The operations on Duration are visible, so Duration is always a
11632 -- possible interpretation.
11633
11634 T1 := Standard_Duration;
11635
bc5f3720 11636 -- Look for fixed-point types in enclosing scopes
996ae0b0 11637
fbf5a39b 11638 Scop := Current_Scope;
996ae0b0
RK
11639 while Scop /= Standard_Standard loop
11640 T2 := First_Entity (Scop);
996ae0b0
RK
11641 while Present (T2) loop
11642 if Is_Fixed_Point_Type (T2)
11643 and then Current_Entity (T2) = T2
11644 and then Scope (Base_Type (T2)) = Scop
11645 then
11646 if Present (T1) then
11647 Fixed_Point_Error;
11648 return Any_Type;
11649 else
11650 T1 := T2;
11651 end if;
11652 end if;
11653
11654 Next_Entity (T2);
11655 end loop;
11656
11657 Scop := Scope (Scop);
11658 end loop;
11659
a77842bd 11660 -- Look for visible fixed type declarations in the context
996ae0b0
RK
11661
11662 Item := First (Context_Items (Cunit (Current_Sem_Unit)));
996ae0b0 11663 while Present (Item) loop
996ae0b0
RK
11664 if Nkind (Item) = N_With_Clause then
11665 Scop := Entity (Name (Item));
11666 T2 := First_Entity (Scop);
996ae0b0
RK
11667 while Present (T2) loop
11668 if Is_Fixed_Point_Type (T2)
11669 and then Scope (Base_Type (T2)) = Scop
19fb051c 11670 and then (Is_Potentially_Use_Visible (T2) or else In_Use (T2))
996ae0b0
RK
11671 then
11672 if Present (T1) then
11673 Fixed_Point_Error;
11674 return Any_Type;
11675 else
11676 T1 := T2;
11677 end if;
11678 end if;
11679
11680 Next_Entity (T2);
11681 end loop;
11682 end if;
11683
11684 Next (Item);
11685 end loop;
11686
11687 if Nkind (N) = N_Real_Literal then
324ac540
AC
11688 Error_Msg_NE
11689 ("??real literal interpreted as }!", N, T1);
996ae0b0 11690 else
324ac540
AC
11691 Error_Msg_NE
11692 ("??universal_fixed expression interpreted as }!", N, T1);
996ae0b0
RK
11693 end if;
11694
11695 return T1;
11696 end Unique_Fixed_Point_Type;
11697
11698 ----------------------
11699 -- Valid_Conversion --
11700 ----------------------
11701
11702 function Valid_Conversion
6cce2156
GD
11703 (N : Node_Id;
11704 Target : Entity_Id;
11705 Operand : Node_Id;
11706 Report_Errs : Boolean := True) return Boolean
996ae0b0 11707 is
e6425869
AC
11708 Target_Type : constant Entity_Id := Base_Type (Target);
11709 Opnd_Type : Entity_Id := Etype (Operand);
11710 Inc_Ancestor : Entity_Id;
996ae0b0
RK
11711
11712 function Conversion_Check
11713 (Valid : Boolean;
0ab80019 11714 Msg : String) return Boolean;
996ae0b0
RK
11715 -- Little routine to post Msg if Valid is False, returns Valid value
11716
1486a00e 11717 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id);
6cce2156
GD
11718 -- If Report_Errs, then calls Errout.Error_Msg_N with its arguments
11719
1486a00e 11720 procedure Conversion_Error_NE
6cce2156
GD
11721 (Msg : String;
11722 N : Node_Or_Entity_Id;
11723 E : Node_Or_Entity_Id);
11724 -- If Report_Errs, then calls Errout.Error_Msg_NE with its arguments
11725
996ae0b0
RK
11726 function Valid_Tagged_Conversion
11727 (Target_Type : Entity_Id;
0ab80019 11728 Opnd_Type : Entity_Id) return Boolean;
996ae0b0
RK
11729 -- Specifically test for validity of tagged conversions
11730
aa180613 11731 function Valid_Array_Conversion return Boolean;
4adf3c50
AC
11732 -- Check index and component conformance, and accessibility levels if
11733 -- the component types are anonymous access types (Ada 2005).
aa180613 11734
996ae0b0
RK
11735 ----------------------
11736 -- Conversion_Check --
11737 ----------------------
11738
11739 function Conversion_Check
11740 (Valid : Boolean;
0ab80019 11741 Msg : String) return Boolean
996ae0b0
RK
11742 is
11743 begin
0a190dfd
AC
11744 if not Valid
11745
11746 -- A generic unit has already been analyzed and we have verified
11747 -- that a particular conversion is OK in that context. Since the
11748 -- instance is reanalyzed without relying on the relationships
11749 -- established during the analysis of the generic, it is possible
11750 -- to end up with inconsistent views of private types. Do not emit
11751 -- the error message in such cases. The rest of the machinery in
11752 -- Valid_Conversion still ensures the proper compatibility of
11753 -- target and operand types.
11754
11755 and then not In_Instance
11756 then
1486a00e 11757 Conversion_Error_N (Msg, Operand);
996ae0b0
RK
11758 end if;
11759
11760 return Valid;
11761 end Conversion_Check;
11762
1486a00e
AC
11763 ------------------------
11764 -- Conversion_Error_N --
11765 ------------------------
6cce2156 11766
1486a00e 11767 procedure Conversion_Error_N (Msg : String; N : Node_Or_Entity_Id) is
6cce2156
GD
11768 begin
11769 if Report_Errs then
1486a00e 11770 Error_Msg_N (Msg, N);
6cce2156 11771 end if;
1486a00e 11772 end Conversion_Error_N;
6cce2156 11773
1486a00e
AC
11774 -------------------------
11775 -- Conversion_Error_NE --
11776 -------------------------
6cce2156 11777
1486a00e 11778 procedure Conversion_Error_NE
6cce2156
GD
11779 (Msg : String;
11780 N : Node_Or_Entity_Id;
11781 E : Node_Or_Entity_Id)
11782 is
11783 begin
11784 if Report_Errs then
1486a00e 11785 Error_Msg_NE (Msg, N, E);
6cce2156 11786 end if;
1486a00e 11787 end Conversion_Error_NE;
6cce2156 11788
aa180613
RD
11789 ----------------------------
11790 -- Valid_Array_Conversion --
11791 ----------------------------
11792
11793 function Valid_Array_Conversion return Boolean
11794 is
11795 Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
11796 Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
11797
11798 Opnd_Index : Node_Id;
11799 Opnd_Index_Type : Entity_Id;
11800
11801 Target_Comp_Type : constant Entity_Id :=
11802 Component_Type (Target_Type);
11803 Target_Comp_Base : constant Entity_Id :=
11804 Base_Type (Target_Comp_Type);
11805
11806 Target_Index : Node_Id;
11807 Target_Index_Type : Entity_Id;
11808
11809 begin
11810 -- Error if wrong number of dimensions
11811
11812 if
11813 Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
11814 then
1486a00e 11815 Conversion_Error_N
aa180613
RD
11816 ("incompatible number of dimensions for conversion", Operand);
11817 return False;
11818
11819 -- Number of dimensions matches
11820
11821 else
11822 -- Loop through indexes of the two arrays
11823
11824 Target_Index := First_Index (Target_Type);
11825 Opnd_Index := First_Index (Opnd_Type);
11826 while Present (Target_Index) and then Present (Opnd_Index) loop
11827 Target_Index_Type := Etype (Target_Index);
11828 Opnd_Index_Type := Etype (Opnd_Index);
11829
11830 -- Error if index types are incompatible
11831
11832 if not (Is_Integer_Type (Target_Index_Type)
11833 and then Is_Integer_Type (Opnd_Index_Type))
11834 and then (Root_Type (Target_Index_Type)
11835 /= Root_Type (Opnd_Index_Type))
11836 then
1486a00e 11837 Conversion_Error_N
aa180613
RD
11838 ("incompatible index types for array conversion",
11839 Operand);
11840 return False;
11841 end if;
11842
11843 Next_Index (Target_Index);
11844 Next_Index (Opnd_Index);
11845 end loop;
11846
11847 -- If component types have same base type, all set
11848
11849 if Target_Comp_Base = Opnd_Comp_Base then
11850 null;
11851
11852 -- Here if base types of components are not the same. The only
11853 -- time this is allowed is if we have anonymous access types.
11854
11855 -- The conversion of arrays of anonymous access types can lead
11856 -- to dangling pointers. AI-392 formalizes the accessibility
11857 -- checks that must be applied to such conversions to prevent
11858 -- out-of-scope references.
11859
19fb051c
AC
11860 elsif Ekind_In
11861 (Target_Comp_Base, E_Anonymous_Access_Type,
11862 E_Anonymous_Access_Subprogram_Type)
aa180613
RD
11863 and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
11864 and then
11865 Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
11866 then
11867 if Type_Access_Level (Target_Type) <
83e5da69 11868 Deepest_Type_Access_Level (Opnd_Type)
aa180613
RD
11869 then
11870 if In_Instance_Body then
43417b90 11871 Error_Msg_Warn := SPARK_Mode /= On;
1486a00e 11872 Conversion_Error_N
4a28b181
AC
11873 ("source array type has deeper accessibility "
11874 & "level than target<<", Operand);
11875 Conversion_Error_N ("\Program_Error [<<", Operand);
aa180613
RD
11876 Rewrite (N,
11877 Make_Raise_Program_Error (Sloc (N),
11878 Reason => PE_Accessibility_Check_Failed));
11879 Set_Etype (N, Target_Type);
11880 return False;
11881
11882 -- Conversion not allowed because of accessibility levels
11883
11884 else
1486a00e
AC
11885 Conversion_Error_N
11886 ("source array type has deeper accessibility "
11887 & "level than target", Operand);
aa180613
RD
11888 return False;
11889 end if;
19fb051c 11890
aa180613
RD
11891 else
11892 null;
11893 end if;
11894
11895 -- All other cases where component base types do not match
11896
11897 else
1486a00e 11898 Conversion_Error_N
aa180613
RD
11899 ("incompatible component types for array conversion",
11900 Operand);
11901 return False;
11902 end if;
11903
45fc7ddb
HK
11904 -- Check that component subtypes statically match. For numeric
11905 -- types this means that both must be either constrained or
11906 -- unconstrained. For enumeration types the bounds must match.
11907 -- All of this is checked in Subtypes_Statically_Match.
aa180613 11908
45fc7ddb 11909 if not Subtypes_Statically_Match
83e5da69 11910 (Target_Comp_Type, Opnd_Comp_Type)
aa180613 11911 then
1486a00e 11912 Conversion_Error_N
aa180613
RD
11913 ("component subtypes must statically match", Operand);
11914 return False;
11915 end if;
11916 end if;
11917
11918 return True;
11919 end Valid_Array_Conversion;
11920
996ae0b0
RK
11921 -----------------------------
11922 -- Valid_Tagged_Conversion --
11923 -----------------------------
11924
11925 function Valid_Tagged_Conversion
11926 (Target_Type : Entity_Id;
0ab80019 11927 Opnd_Type : Entity_Id) return Boolean
996ae0b0
RK
11928 is
11929 begin
a77842bd 11930 -- Upward conversions are allowed (RM 4.6(22))
996ae0b0
RK
11931
11932 if Covers (Target_Type, Opnd_Type)
11933 or else Is_Ancestor (Target_Type, Opnd_Type)
11934 then
11935 return True;
11936
a77842bd
TQ
11937 -- Downward conversion are allowed if the operand is class-wide
11938 -- (RM 4.6(23)).
996ae0b0
RK
11939
11940 elsif Is_Class_Wide_Type (Opnd_Type)
b7d1f17f 11941 and then Covers (Opnd_Type, Target_Type)
996ae0b0
RK
11942 then
11943 return True;
11944
11945 elsif Covers (Opnd_Type, Target_Type)
11946 or else Is_Ancestor (Opnd_Type, Target_Type)
11947 then
11948 return
11949 Conversion_Check (False,
11950 "downward conversion of tagged objects not allowed");
758c442c 11951
0669bebe
GB
11952 -- Ada 2005 (AI-251): The conversion to/from interface types is
11953 -- always valid
758c442c 11954
0669bebe 11955 elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
758c442c
GD
11956 return True;
11957
b7d1f17f 11958 -- If the operand is a class-wide type obtained through a limited_
e4dc3327 11959 -- with clause, and the context includes the nonlimited view, use
b7d1f17f
HK
11960 -- it to determine whether the conversion is legal.
11961
11962 elsif Is_Class_Wide_Type (Opnd_Type)
7b56a91b 11963 and then From_Limited_With (Opnd_Type)
b7d1f17f
HK
11964 and then Present (Non_Limited_View (Etype (Opnd_Type)))
11965 and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
11966 then
11967 return True;
11968
aa180613
RD
11969 elsif Is_Access_Type (Opnd_Type)
11970 and then Is_Interface (Directly_Designated_Type (Opnd_Type))
11971 then
11972 return True;
11973
996ae0b0 11974 else
1486a00e 11975 Conversion_Error_NE
996ae0b0
RK
11976 ("invalid tagged conversion, not compatible with}",
11977 N, First_Subtype (Opnd_Type));
11978 return False;
11979 end if;
11980 end Valid_Tagged_Conversion;
11981
11982 -- Start of processing for Valid_Conversion
11983
11984 begin
11985 Check_Parameterless_Call (Operand);
11986
11987 if Is_Overloaded (Operand) then
11988 declare
11989 I : Interp_Index;
11990 I1 : Interp_Index;
11991 It : Interp;
11992 It1 : Interp;
11993 N1 : Entity_Id;
f0d10385 11994 T1 : Entity_Id;
996ae0b0
RK
11995
11996 begin
d81b4bfe
TQ
11997 -- Remove procedure calls, which syntactically cannot appear in
11998 -- this context, but which cannot be removed by type checking,
996ae0b0
RK
11999 -- because the context does not impose a type.
12000
4adf3c50
AC
12001 -- The node may be labelled overloaded, but still contain only one
12002 -- interpretation because others were discarded earlier. If this
12003 -- is the case, retain the single interpretation if legal.
9ebe3743 12004
996ae0b0 12005 Get_First_Interp (Operand, I, It);
9ebe3743
HK
12006 Opnd_Type := It.Typ;
12007 Get_Next_Interp (I, It);
996ae0b0 12008
9ebe3743
HK
12009 if Present (It.Typ)
12010 and then Opnd_Type /= Standard_Void_Type
12011 then
12012 -- More than one candidate interpretation is available
996ae0b0 12013
9ebe3743
HK
12014 Get_First_Interp (Operand, I, It);
12015 while Present (It.Typ) loop
12016 if It.Typ = Standard_Void_Type then
12017 Remove_Interp (I);
12018 end if;
1420b484 12019
4d49c6e1
AC
12020 -- When compiling for a system where Address is of a visible
12021 -- integer type, spurious ambiguities can be produced when
12022 -- arithmetic operations have a literal operand and return
12023 -- System.Address or a descendant of it. These ambiguities
12024 -- are usually resolved by the context, but for conversions
12025 -- there is no context type and the removal of the spurious
12026 -- operations must be done explicitly here.
12027
12028 if not Address_Is_Private
9ebe3743
HK
12029 and then Is_Descendent_Of_Address (It.Typ)
12030 then
12031 Remove_Interp (I);
12032 end if;
12033
12034 Get_Next_Interp (I, It);
12035 end loop;
12036 end if;
996ae0b0
RK
12037
12038 Get_First_Interp (Operand, I, It);
12039 I1 := I;
12040 It1 := It;
12041
12042 if No (It.Typ) then
1486a00e 12043 Conversion_Error_N ("illegal operand in conversion", Operand);
996ae0b0
RK
12044 return False;
12045 end if;
12046
12047 Get_Next_Interp (I, It);
12048
12049 if Present (It.Typ) then
12050 N1 := It1.Nam;
f0d10385 12051 T1 := It1.Typ;
c8307596 12052 It1 := Disambiguate (Operand, I1, I, Any_Type);
996ae0b0
RK
12053
12054 if It1 = No_Interp then
1486a00e
AC
12055 Conversion_Error_N
12056 ("ambiguous operand in conversion", Operand);
996ae0b0 12057
f0d10385
AC
12058 -- If the interpretation involves a standard operator, use
12059 -- the location of the type, which may be user-defined.
12060
12061 if Sloc (It.Nam) = Standard_Location then
12062 Error_Msg_Sloc := Sloc (It.Typ);
12063 else
12064 Error_Msg_Sloc := Sloc (It.Nam);
12065 end if;
12066
1486a00e 12067 Conversion_Error_N -- CODEFIX
4e7a4f6e 12068 ("\\possible interpretation#!", Operand);
996ae0b0 12069
f0d10385
AC
12070 if Sloc (N1) = Standard_Location then
12071 Error_Msg_Sloc := Sloc (T1);
12072 else
12073 Error_Msg_Sloc := Sloc (N1);
12074 end if;
12075
1486a00e 12076 Conversion_Error_N -- CODEFIX
4e7a4f6e 12077 ("\\possible interpretation#!", Operand);
996ae0b0
RK
12078
12079 return False;
12080 end if;
12081 end if;
12082
12083 Set_Etype (Operand, It1.Typ);
12084 Opnd_Type := It1.Typ;
12085 end;
12086 end if;
12087
6fd0a72a
AC
12088 -- Deal with conversion of integer type to address if the pragma
12089 -- Allow_Integer_Address is in effect. We convert the conversion to
a90bd866 12090 -- an unchecked conversion in this case and we are all done.
6fd0a72a 12091
061828e3 12092 if Address_Integer_Convert_OK (Opnd_Type, Target_Type) then
6fd0a72a
AC
12093 Rewrite (N, Unchecked_Convert_To (Target_Type, Expression (N)));
12094 Analyze_And_Resolve (N, Target_Type);
12095 return True;
12096 end if;
12097
e6425869
AC
12098 -- If we are within a child unit, check whether the type of the
12099 -- expression has an ancestor in a parent unit, in which case it
12100 -- belongs to its derivation class even if the ancestor is private.
12101 -- See RM 7.3.1 (5.2/3).
12102
12103 Inc_Ancestor := Get_Incomplete_View_Of_Ancestor (Opnd_Type);
12104
aa180613 12105 -- Numeric types
996ae0b0 12106
6fd0a72a 12107 if Is_Numeric_Type (Target_Type) then
996ae0b0 12108
aa180613 12109 -- A universal fixed expression can be converted to any numeric type
996ae0b0 12110
996ae0b0
RK
12111 if Opnd_Type = Universal_Fixed then
12112 return True;
7324bf49 12113
aa180613
RD
12114 -- Also no need to check when in an instance or inlined body, because
12115 -- the legality has been established when the template was analyzed.
12116 -- Furthermore, numeric conversions may occur where only a private
f3d57416 12117 -- view of the operand type is visible at the instantiation point.
aa180613
RD
12118 -- This results in a spurious error if we check that the operand type
12119 -- is a numeric type.
12120
12121 -- Note: in a previous version of this unit, the following tests were
12122 -- applied only for generated code (Comes_From_Source set to False),
12123 -- but in fact the test is required for source code as well, since
12124 -- this situation can arise in source code.
12125
12126 elsif In_Instance or else In_Inlined_Body then
d347f572 12127 return True;
aa180613
RD
12128
12129 -- Otherwise we need the conversion check
7324bf49 12130
996ae0b0 12131 else
aa180613 12132 return Conversion_Check
6fd0a72a
AC
12133 (Is_Numeric_Type (Opnd_Type)
12134 or else
12135 (Present (Inc_Ancestor)
12136 and then Is_Numeric_Type (Inc_Ancestor)),
12137 "illegal operand for numeric conversion");
996ae0b0
RK
12138 end if;
12139
aa180613
RD
12140 -- Array types
12141
996ae0b0
RK
12142 elsif Is_Array_Type (Target_Type) then
12143 if not Is_Array_Type (Opnd_Type)
12144 or else Opnd_Type = Any_Composite
12145 or else Opnd_Type = Any_String
12146 then
1486a00e
AC
12147 Conversion_Error_N
12148 ("illegal operand for array conversion", Operand);
996ae0b0 12149 return False;
b2502161 12150
996ae0b0 12151 else
aa180613 12152 return Valid_Array_Conversion;
996ae0b0
RK
12153 end if;
12154
4b963531
AC
12155 -- Ada 2005 (AI-251): Internally generated conversions of access to
12156 -- interface types added to force the displacement of the pointer to
12157 -- reference the corresponding dispatch table.
12158
12159 elsif not Comes_From_Source (N)
12160 and then Is_Access_Type (Target_Type)
12161 and then Is_Interface (Designated_Type (Target_Type))
12162 then
12163 return True;
12164
e65f50ec
ES
12165 -- Ada 2005 (AI-251): Anonymous access types where target references an
12166 -- interface type.
758c442c 12167
966fc9c5
AC
12168 elsif Is_Access_Type (Opnd_Type)
12169 and then Ekind_In (Target_Type, E_General_Access_Type,
12170 E_Anonymous_Access_Type)
758c442c
GD
12171 and then Is_Interface (Directly_Designated_Type (Target_Type))
12172 then
12173 -- Check the static accessibility rule of 4.6(17). Note that the
d81b4bfe
TQ
12174 -- check is not enforced when within an instance body, since the
12175 -- RM requires such cases to be caught at run time.
758c442c 12176
4172a8e3
AC
12177 -- If the operand is a rewriting of an allocator no check is needed
12178 -- because there are no accessibility issues.
12179
12180 if Nkind (Original_Node (N)) = N_Allocator then
12181 null;
12182
12183 elsif Ekind (Target_Type) /= E_Anonymous_Access_Type then
758c442c 12184 if Type_Access_Level (Opnd_Type) >
996c8821 12185 Deepest_Type_Access_Level (Target_Type)
758c442c
GD
12186 then
12187 -- In an instance, this is a run-time check, but one we know
12188 -- will fail, so generate an appropriate warning. The raise
12189 -- will be generated by Expand_N_Type_Conversion.
12190
12191 if In_Instance_Body then
43417b90 12192 Error_Msg_Warn := SPARK_Mode /= On;
1486a00e 12193 Conversion_Error_N
4a28b181 12194 ("cannot convert local pointer to non-local access type<<",
758c442c 12195 Operand);
4a28b181 12196 Conversion_Error_N ("\Program_Error [<<", Operand);
996c8821 12197
758c442c 12198 else
1486a00e 12199 Conversion_Error_N
758c442c
GD
12200 ("cannot convert local pointer to non-local access type",
12201 Operand);
12202 return False;
12203 end if;
12204
12205 -- Special accessibility checks are needed in the case of access
12206 -- discriminants declared for a limited type.
12207
12208 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
12209 and then not Is_Local_Anonymous_Access (Opnd_Type)
12210 then
12211 -- When the operand is a selected access discriminant the check
12212 -- needs to be made against the level of the object denoted by
d81b4bfe
TQ
12213 -- the prefix of the selected name (Object_Access_Level handles
12214 -- checking the prefix of the operand for this case).
758c442c
GD
12215
12216 if Nkind (Operand) = N_Selected_Component
c8ef728f 12217 and then Object_Access_Level (Operand) >
d15f9422 12218 Deepest_Type_Access_Level (Target_Type)
758c442c 12219 then
d81b4bfe
TQ
12220 -- In an instance, this is a run-time check, but one we know
12221 -- will fail, so generate an appropriate warning. The raise
12222 -- will be generated by Expand_N_Type_Conversion.
758c442c
GD
12223
12224 if In_Instance_Body then
43417b90 12225 Error_Msg_Warn := SPARK_Mode /= On;
1486a00e 12226 Conversion_Error_N
4a28b181
AC
12227 ("cannot convert access discriminant to non-local "
12228 & "access type<<", Operand);
12229 Conversion_Error_N ("\Program_Error [<<", Operand);
12230
12231 -- Real error if not in instance body
12232
758c442c 12233 else
1486a00e
AC
12234 Conversion_Error_N
12235 ("cannot convert access discriminant to non-local "
12236 & "access type", Operand);
758c442c
GD
12237 return False;
12238 end if;
12239 end if;
12240
12241 -- The case of a reference to an access discriminant from
12242 -- within a limited type declaration (which will appear as
12243 -- a discriminal) is always illegal because the level of the
f3d57416 12244 -- discriminant is considered to be deeper than any (nameable)
758c442c
GD
12245 -- access type.
12246
12247 if Is_Entity_Name (Operand)
12248 and then not Is_Local_Anonymous_Access (Opnd_Type)
964f13da
RD
12249 and then
12250 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
758c442c
GD
12251 and then Present (Discriminal_Link (Entity (Operand)))
12252 then
1486a00e 12253 Conversion_Error_N
758c442c
GD
12254 ("discriminant has deeper accessibility level than target",
12255 Operand);
12256 return False;
12257 end if;
12258 end if;
12259 end if;
12260
12261 return True;
12262
aa180613
RD
12263 -- General and anonymous access types
12264
964f13da
RD
12265 elsif Ekind_In (Target_Type, E_General_Access_Type,
12266 E_Anonymous_Access_Type)
996ae0b0
RK
12267 and then
12268 Conversion_Check
12269 (Is_Access_Type (Opnd_Type)
964f13da
RD
12270 and then not
12271 Ekind_In (Opnd_Type, E_Access_Subprogram_Type,
12272 E_Access_Protected_Subprogram_Type),
996ae0b0
RK
12273 "must be an access-to-object type")
12274 then
12275 if Is_Access_Constant (Opnd_Type)
12276 and then not Is_Access_Constant (Target_Type)
12277 then
1486a00e 12278 Conversion_Error_N
996ae0b0
RK
12279 ("access-to-constant operand type not allowed", Operand);
12280 return False;
12281 end if;
12282
758c442c
GD
12283 -- Check the static accessibility rule of 4.6(17). Note that the
12284 -- check is not enforced when within an instance body, since the RM
12285 -- requires such cases to be caught at run time.
996ae0b0 12286
758c442c
GD
12287 if Ekind (Target_Type) /= E_Anonymous_Access_Type
12288 or else Is_Local_Anonymous_Access (Target_Type)
d15f9422 12289 or else Nkind (Associated_Node_For_Itype (Target_Type)) =
996c8821 12290 N_Object_Declaration
758c442c 12291 then
6cce2156
GD
12292 -- Ada 2012 (AI05-0149): Perform legality checking on implicit
12293 -- conversions from an anonymous access type to a named general
12294 -- access type. Such conversions are not allowed in the case of
12295 -- access parameters and stand-alone objects of an anonymous
c199ccf7
AC
12296 -- access type. The implicit conversion case is recognized by
12297 -- testing that Comes_From_Source is False and that it's been
12298 -- rewritten. The Comes_From_Source test isn't sufficient because
12299 -- nodes in inlined calls to predefined library routines can have
12300 -- Comes_From_Source set to False. (Is there a better way to test
12301 -- for implicit conversions???)
6cce2156
GD
12302
12303 if Ada_Version >= Ada_2012
12304 and then not Comes_From_Source (N)
c199ccf7 12305 and then N /= Original_Node (N)
6cce2156
GD
12306 and then Ekind (Target_Type) = E_General_Access_Type
12307 and then Ekind (Opnd_Type) = E_Anonymous_Access_Type
996ae0b0 12308 then
6cce2156
GD
12309 if Is_Itype (Opnd_Type) then
12310
12311 -- Implicit conversions aren't allowed for objects of an
12312 -- anonymous access type, since such objects have nonstatic
12313 -- levels in Ada 2012.
12314
12315 if Nkind (Associated_Node_For_Itype (Opnd_Type)) =
12316 N_Object_Declaration
12317 then
1486a00e
AC
12318 Conversion_Error_N
12319 ("implicit conversion of stand-alone anonymous "
12320 & "access object not allowed", Operand);
6cce2156
GD
12321 return False;
12322
12323 -- Implicit conversions aren't allowed for anonymous access
12324 -- parameters. The "not Is_Local_Anonymous_Access_Type" test
12325 -- is done to exclude anonymous access results.
12326
12327 elsif not Is_Local_Anonymous_Access (Opnd_Type)
12328 and then Nkind_In (Associated_Node_For_Itype (Opnd_Type),
12329 N_Function_Specification,
12330 N_Procedure_Specification)
12331 then
1486a00e
AC
12332 Conversion_Error_N
12333 ("implicit conversion of anonymous access formal "
12334 & "not allowed", Operand);
6cce2156
GD
12335 return False;
12336
12337 -- This is a case where there's an enclosing object whose
12338 -- to which the "statically deeper than" relationship does
12339 -- not apply (such as an access discriminant selected from
12340 -- a dereference of an access parameter).
12341
12342 elsif Object_Access_Level (Operand)
12343 = Scope_Depth (Standard_Standard)
12344 then
1486a00e
AC
12345 Conversion_Error_N
12346 ("implicit conversion of anonymous access value "
12347 & "not allowed", Operand);
6cce2156
GD
12348 return False;
12349
12350 -- In other cases, the level of the operand's type must be
12351 -- statically less deep than that of the target type, else
12352 -- implicit conversion is disallowed (by RM12-8.6(27.1/3)).
12353
d15f9422 12354 elsif Type_Access_Level (Opnd_Type) >
996c8821 12355 Deepest_Type_Access_Level (Target_Type)
6cce2156 12356 then
1486a00e
AC
12357 Conversion_Error_N
12358 ("implicit conversion of anonymous access value "
12359 & "violates accessibility", Operand);
6cce2156
GD
12360 return False;
12361 end if;
12362 end if;
12363
d15f9422 12364 elsif Type_Access_Level (Opnd_Type) >
996c8821 12365 Deepest_Type_Access_Level (Target_Type)
6cce2156 12366 then
d81b4bfe
TQ
12367 -- In an instance, this is a run-time check, but one we know
12368 -- will fail, so generate an appropriate warning. The raise
12369 -- will be generated by Expand_N_Type_Conversion.
996ae0b0
RK
12370
12371 if In_Instance_Body then
43417b90 12372 Error_Msg_Warn := SPARK_Mode /= On;
1486a00e 12373 Conversion_Error_N
4a28b181 12374 ("cannot convert local pointer to non-local access type<<",
996ae0b0 12375 Operand);
4a28b181
AC
12376 Conversion_Error_N ("\Program_Error [<<", Operand);
12377
12378 -- If not in an instance body, this is a real error
996ae0b0
RK
12379
12380 else
b90cfacd
HK
12381 -- Avoid generation of spurious error message
12382
12383 if not Error_Posted (N) then
1486a00e 12384 Conversion_Error_N
b90cfacd
HK
12385 ("cannot convert local pointer to non-local access type",
12386 Operand);
12387 end if;
12388
996ae0b0
RK
12389 return False;
12390 end if;
12391
758c442c
GD
12392 -- Special accessibility checks are needed in the case of access
12393 -- discriminants declared for a limited type.
12394
12395 elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
12396 and then not Is_Local_Anonymous_Access (Opnd_Type)
12397 then
758c442c
GD
12398 -- When the operand is a selected access discriminant the check
12399 -- needs to be made against the level of the object denoted by
d81b4bfe
TQ
12400 -- the prefix of the selected name (Object_Access_Level handles
12401 -- checking the prefix of the operand for this case).
996ae0b0
RK
12402
12403 if Nkind (Operand) = N_Selected_Component
45fc7ddb 12404 and then Object_Access_Level (Operand) >
996c8821 12405 Deepest_Type_Access_Level (Target_Type)
996ae0b0 12406 then
d81b4bfe
TQ
12407 -- In an instance, this is a run-time check, but one we know
12408 -- will fail, so generate an appropriate warning. The raise
12409 -- will be generated by Expand_N_Type_Conversion.
996ae0b0
RK
12410
12411 if In_Instance_Body then
43417b90 12412 Error_Msg_Warn := SPARK_Mode /= On;
1486a00e 12413 Conversion_Error_N
4a28b181
AC
12414 ("cannot convert access discriminant to non-local "
12415 & "access type<<", Operand);
12416 Conversion_Error_N ("\Program_Error [<<", Operand);
12417
12418 -- If not in an instance body, this is a real error
996ae0b0
RK
12419
12420 else
1486a00e
AC
12421 Conversion_Error_N
12422 ("cannot convert access discriminant to non-local "
12423 & "access type", Operand);
996ae0b0
RK
12424 return False;
12425 end if;
12426 end if;
12427
758c442c
GD
12428 -- The case of a reference to an access discriminant from
12429 -- within a limited type declaration (which will appear as
12430 -- a discriminal) is always illegal because the level of the
f3d57416 12431 -- discriminant is considered to be deeper than any (nameable)
758c442c 12432 -- access type.
996ae0b0
RK
12433
12434 if Is_Entity_Name (Operand)
964f13da
RD
12435 and then
12436 Ekind_In (Entity (Operand), E_In_Parameter, E_Constant)
996ae0b0
RK
12437 and then Present (Discriminal_Link (Entity (Operand)))
12438 then
1486a00e 12439 Conversion_Error_N
996ae0b0
RK
12440 ("discriminant has deeper accessibility level than target",
12441 Operand);
12442 return False;
12443 end if;
12444 end if;
12445 end if;
12446
e4dc3327 12447 -- In the presence of limited_with clauses we have to use nonlimited
14e33999 12448 -- views, if available.
d81b4bfe 12449
14e33999 12450 Check_Limited : declare
0669bebe
GB
12451 function Full_Designated_Type (T : Entity_Id) return Entity_Id;
12452 -- Helper function to handle limited views
12453
12454 --------------------------
12455 -- Full_Designated_Type --
12456 --------------------------
12457
12458 function Full_Designated_Type (T : Entity_Id) return Entity_Id is
950d217a 12459 Desig : constant Entity_Id := Designated_Type (T);
c0985d4e 12460
0669bebe 12461 begin
950d217a
AC
12462 -- Handle the limited view of a type
12463
47346923
AC
12464 if From_Limited_With (Desig)
12465 and then Has_Non_Limited_View (Desig)
0669bebe 12466 then
950d217a
AC
12467 return Available_View (Desig);
12468 else
12469 return Desig;
0669bebe
GB
12470 end if;
12471 end Full_Designated_Type;
12472
d81b4bfe
TQ
12473 -- Local Declarations
12474
0669bebe
GB
12475 Target : constant Entity_Id := Full_Designated_Type (Target_Type);
12476 Opnd : constant Entity_Id := Full_Designated_Type (Opnd_Type);
12477
12478 Same_Base : constant Boolean :=
12479 Base_Type (Target) = Base_Type (Opnd);
996ae0b0 12480
14e33999 12481 -- Start of processing for Check_Limited
d81b4bfe 12482
996ae0b0
RK
12483 begin
12484 if Is_Tagged_Type (Target) then
12485 return Valid_Tagged_Conversion (Target, Opnd);
12486
12487 else
0669bebe 12488 if not Same_Base then
1486a00e 12489 Conversion_Error_NE
996ae0b0
RK
12490 ("target designated type not compatible with }",
12491 N, Base_Type (Opnd));
12492 return False;
12493
da709d08
AC
12494 -- Ada 2005 AI-384: legality rule is symmetric in both
12495 -- designated types. The conversion is legal (with possible
12496 -- constraint check) if either designated type is
12497 -- unconstrained.
12498
12499 elsif Subtypes_Statically_Match (Target, Opnd)
12500 or else
12501 (Has_Discriminants (Target)
12502 and then
12503 (not Is_Constrained (Opnd)
12504 or else not Is_Constrained (Target)))
996ae0b0 12505 then
9fa33291
RD
12506 -- Special case, if Value_Size has been used to make the
12507 -- sizes different, the conversion is not allowed even
12508 -- though the subtypes statically match.
12509
12510 if Known_Static_RM_Size (Target)
12511 and then Known_Static_RM_Size (Opnd)
12512 and then RM_Size (Target) /= RM_Size (Opnd)
12513 then
1486a00e 12514 Conversion_Error_NE
9fa33291
RD
12515 ("target designated subtype not compatible with }",
12516 N, Opnd);
1486a00e 12517 Conversion_Error_NE
9fa33291
RD
12518 ("\because sizes of the two designated subtypes differ",
12519 N, Opnd);
12520 return False;
12521
12522 -- Normal case where conversion is allowed
12523
12524 else
12525 return True;
12526 end if;
da709d08
AC
12527
12528 else
996ae0b0
RK
12529 Error_Msg_NE
12530 ("target designated subtype not compatible with }",
12531 N, Opnd);
12532 return False;
996ae0b0
RK
12533 end if;
12534 end if;
14e33999 12535 end Check_Limited;
996ae0b0 12536
cdbf04c0 12537 -- Access to subprogram types. If the operand is an access parameter,
4adf3c50
AC
12538 -- the type has a deeper accessibility that any master, and cannot be
12539 -- assigned. We must make an exception if the conversion is part of an
12540 -- assignment and the target is the return object of an extended return
12541 -- statement, because in that case the accessibility check takes place
12542 -- after the return.
aa180613 12543
dce86910 12544 elsif Is_Access_Subprogram_Type (Target_Type)
b07b7ace 12545
3f1bc2cf
AC
12546 -- Note: this test of Opnd_Type is there to prevent entering this
12547 -- branch in the case of a remote access to subprogram type, which
12548 -- is internally represented as an E_Record_Type.
b07b7ace 12549
3f1bc2cf 12550 and then Is_Access_Type (Opnd_Type)
996ae0b0 12551 then
cdbf04c0
AC
12552 if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
12553 and then Is_Entity_Name (Operand)
12554 and then Ekind (Entity (Operand)) = E_In_Parameter
53cf4600
ES
12555 and then
12556 (Nkind (Parent (N)) /= N_Assignment_Statement
12557 or else not Is_Entity_Name (Name (Parent (N)))
12558 or else not Is_Return_Object (Entity (Name (Parent (N)))))
0669bebe 12559 then
1486a00e 12560 Conversion_Error_N
0669bebe
GB
12561 ("illegal attempt to store anonymous access to subprogram",
12562 Operand);
1486a00e
AC
12563 Conversion_Error_N
12564 ("\value has deeper accessibility than any master "
12565 & "(RM 3.10.2 (13))",
0669bebe
GB
12566 Operand);
12567
c147ac26
ES
12568 Error_Msg_NE
12569 ("\use named access type for& instead of access parameter",
12570 Operand, Entity (Operand));
0669bebe
GB
12571 end if;
12572
996ae0b0
RK
12573 -- Check that the designated types are subtype conformant
12574
bc5f3720
RD
12575 Check_Subtype_Conformant (New_Id => Designated_Type (Target_Type),
12576 Old_Id => Designated_Type (Opnd_Type),
12577 Err_Loc => N);
996ae0b0
RK
12578
12579 -- Check the static accessibility rule of 4.6(20)
12580
12581 if Type_Access_Level (Opnd_Type) >
996c8821 12582 Deepest_Type_Access_Level (Target_Type)
996ae0b0 12583 then
1486a00e 12584 Conversion_Error_N
996ae0b0
RK
12585 ("operand type has deeper accessibility level than target",
12586 Operand);
12587
12588 -- Check that if the operand type is declared in a generic body,
12589 -- then the target type must be declared within that same body
12590 -- (enforces last sentence of 4.6(20)).
12591
12592 elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
12593 declare
12594 O_Gen : constant Node_Id :=
12595 Enclosing_Generic_Body (Opnd_Type);
12596
1420b484 12597 T_Gen : Node_Id;
996ae0b0
RK
12598
12599 begin
1420b484 12600 T_Gen := Enclosing_Generic_Body (Target_Type);
996ae0b0
RK
12601 while Present (T_Gen) and then T_Gen /= O_Gen loop
12602 T_Gen := Enclosing_Generic_Body (T_Gen);
12603 end loop;
12604
12605 if T_Gen /= O_Gen then
1486a00e
AC
12606 Conversion_Error_N
12607 ("target type must be declared in same generic body "
12608 & "as operand type", N);
996ae0b0
RK
12609 end if;
12610 end;
12611 end if;
12612
12613 return True;
12614
b07b7ace 12615 -- Remote access to subprogram types
aa180613 12616
996ae0b0
RK
12617 elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
12618 and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
12619 then
12620 -- It is valid to convert from one RAS type to another provided
12621 -- that their specification statically match.
12622
b07b7ace
AC
12623 -- Note: at this point, remote access to subprogram types have been
12624 -- expanded to their E_Record_Type representation, and we need to
12625 -- go back to the original access type definition using the
12626 -- Corresponding_Remote_Type attribute in order to check that the
12627 -- designated profiles match.
12628
12629 pragma Assert (Ekind (Target_Type) = E_Record_Type);
12630 pragma Assert (Ekind (Opnd_Type) = E_Record_Type);
12631
996ae0b0
RK
12632 Check_Subtype_Conformant
12633 (New_Id =>
12634 Designated_Type (Corresponding_Remote_Type (Target_Type)),
12635 Old_Id =>
12636 Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
12637 Err_Loc =>
12638 N);
12639 return True;
aa180613 12640
be482a8c
AC
12641 -- If it was legal in the generic, it's legal in the instance
12642
12643 elsif In_Instance_Body then
12644 return True;
12645
e65f50ec 12646 -- If both are tagged types, check legality of view conversions
996ae0b0 12647
e65f50ec 12648 elsif Is_Tagged_Type (Target_Type)
4adf3c50
AC
12649 and then
12650 Is_Tagged_Type (Opnd_Type)
e65f50ec 12651 then
996ae0b0
RK
12652 return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
12653
a77842bd 12654 -- Types derived from the same root type are convertible
996ae0b0
RK
12655
12656 elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
12657 return True;
12658
4adf3c50
AC
12659 -- In an instance or an inlined body, there may be inconsistent views of
12660 -- the same type, or of types derived from a common root.
996ae0b0 12661
aa5147f0
ES
12662 elsif (In_Instance or In_Inlined_Body)
12663 and then
d81b4bfe
TQ
12664 Root_Type (Underlying_Type (Target_Type)) =
12665 Root_Type (Underlying_Type (Opnd_Type))
996ae0b0
RK
12666 then
12667 return True;
12668
12669 -- Special check for common access type error case
12670
12671 elsif Ekind (Target_Type) = E_Access_Type
12672 and then Is_Access_Type (Opnd_Type)
12673 then
1486a00e
AC
12674 Conversion_Error_N ("target type must be general access type!", N);
12675 Conversion_Error_NE -- CODEFIX
305caf42 12676 ("add ALL to }!", N, Target_Type);
996ae0b0
RK
12677 return False;
12678
818b578d
AC
12679 -- Here we have a real conversion error
12680
996ae0b0 12681 else
1486a00e
AC
12682 Conversion_Error_NE
12683 ("invalid conversion, not compatible with }", N, Opnd_Type);
996ae0b0
RK
12684 return False;
12685 end if;
12686 end Valid_Conversion;
12687
12688end Sem_Res;