OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [gcc-4.5.1/] [gcc-4.5.1-or32-1.0rc2/] [gcc/] [ada/] [sem_res.adb] - Blame information for rev 435

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 281 jeremybenn
------------------------------------------------------------------------------
2
--                                                                          --
3
--                         GNAT COMPILER COMPONENTS                         --
4
--                                                                          --
5
--                              S E M _ R E S                               --
6
--                                                                          --
7
--                                 B o d y                                  --
8
--                                                                          --
9
--          Copyright (C) 1992-2009, Free Software Foundation, Inc.         --
10
--                                                                          --
11
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
12
-- terms of the  GNU General Public License as published  by the Free Soft- --
13
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16
-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17
-- for  more details.  You should have  received  a copy of the GNU General --
18
-- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19
-- http://www.gnu.org/licenses for a complete copy of the license.          --
20
--                                                                          --
21
-- GNAT was originally developed  by the GNAT team at  New York University. --
22
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
23
--                                                                          --
24
------------------------------------------------------------------------------
25
 
26
with Atree;    use Atree;
27
with Checks;   use Checks;
28
with Debug;    use Debug;
29
with Debug_A;  use Debug_A;
30
with Einfo;    use Einfo;
31
with Elists;   use Elists;
32
with Errout;   use Errout;
33
with Expander; use Expander;
34
with Exp_Disp; use Exp_Disp;
35
with Exp_Ch6;  use Exp_Ch6;
36
with Exp_Ch7;  use Exp_Ch7;
37
with Exp_Tss;  use Exp_Tss;
38
with Exp_Util; use Exp_Util;
39
with Fname;    use Fname;
40
with Freeze;   use Freeze;
41
with Itypes;   use Itypes;
42
with Lib;      use Lib;
43
with Lib.Xref; use Lib.Xref;
44
with Namet;    use Namet;
45
with Nmake;    use Nmake;
46
with Nlists;   use Nlists;
47
with Opt;      use Opt;
48
with Output;   use Output;
49
with Restrict; use Restrict;
50
with Rident;   use Rident;
51
with Rtsfind;  use Rtsfind;
52
with Sem;      use Sem;
53
with Sem_Aux;  use Sem_Aux;
54
with Sem_Aggr; use Sem_Aggr;
55
with Sem_Attr; use Sem_Attr;
56
with Sem_Cat;  use Sem_Cat;
57
with Sem_Ch4;  use Sem_Ch4;
58
with Sem_Ch6;  use Sem_Ch6;
59
with Sem_Ch8;  use Sem_Ch8;
60
with Sem_Ch13; use Sem_Ch13;
61
with Sem_Disp; use Sem_Disp;
62
with Sem_Dist; use Sem_Dist;
63
with Sem_Elim; use Sem_Elim;
64
with Sem_Elab; use Sem_Elab;
65
with Sem_Eval; use Sem_Eval;
66
with Sem_Intr; use Sem_Intr;
67
with Sem_Util; use Sem_Util;
68
with Sem_Type; use Sem_Type;
69
with Sem_Warn; use Sem_Warn;
70
with Sinfo;    use Sinfo;
71
with Snames;   use Snames;
72
with Stand;    use Stand;
73
with Stringt;  use Stringt;
74
with Style;    use Style;
75
with Tbuild;   use Tbuild;
76
with Uintp;    use Uintp;
77
with Urealp;   use Urealp;
78
 
79
package body Sem_Res is
80
 
81
   -----------------------
82
   -- Local Subprograms --
83
   -----------------------
84
 
85
   --  Second pass (top-down) type checking and overload resolution procedures
86
   --  Typ is the type required by context. These procedures propagate the
87
   --  type information recursively to the descendants of N. If the node
88
   --  is not overloaded, its Etype is established in the first pass. If
89
   --  overloaded,  the Resolve routines set the correct type. For arith.
90
   --  operators, the Etype is the base type of the context.
91
 
92
   --  Note that Resolve_Attribute is separated off in Sem_Attr
93
 
94
   procedure Check_Discriminant_Use (N : Node_Id);
95
   --  Enforce the restrictions on the use of discriminants when constraining
96
   --  a component of a discriminated type (record or concurrent type).
97
 
98
   procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id);
99
   --  Given a node for an operator associated with type T, check that
100
   --  the operator is visible. Operators all of whose operands are
101
   --  universal must be checked for visibility during resolution
102
   --  because their type is not determinable based on their operands.
103
 
104
   procedure Check_Fully_Declared_Prefix
105
     (Typ  : Entity_Id;
106
      Pref : Node_Id);
107
   --  Check that the type of the prefix of a dereference is not incomplete
108
 
109
   function Check_Infinite_Recursion (N : Node_Id) return Boolean;
110
   --  Given a call node, N, which is known to occur immediately within the
111
   --  subprogram being called, determines whether it is a detectable case of
112
   --  an infinite recursion, and if so, outputs appropriate messages. Returns
113
   --  True if an infinite recursion is detected, and False otherwise.
114
 
115
   procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id);
116
   --  If the type of the object being initialized uses the secondary stack
117
   --  directly or indirectly, create a transient scope for the call to the
118
   --  init proc. This is because we do not create transient scopes for the
119
   --  initialization of individual components within the init proc itself.
120
   --  Could be optimized away perhaps?
121
 
122
   procedure Check_No_Direct_Boolean_Operators (N : Node_Id);
123
   --  N is the node for a logical operator. If the operator is predefined, and
124
   --  the root type of the operands is Standard.Boolean, then a check is made
125
   --  for restriction No_Direct_Boolean_Operators. This procedure also handles
126
   --  the style check for Style_Check_Boolean_And_Or.
127
 
128
   function Is_Definite_Access_Type (E : Entity_Id) return Boolean;
129
   --  Determine whether E is an access type declared by an access
130
   --  declaration, and not an (anonymous) allocator type.
131
 
132
   function Is_Predefined_Op (Nam : Entity_Id) return Boolean;
133
   --  Utility to check whether the name in the call is a predefined
134
   --  operator, in which case the call is made into an operator node.
135
   --  An instance of an intrinsic conversion operation may be given
136
   --  an operator name, but is not treated like an operator.
137
 
138
   procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id);
139
   --  If a default expression in entry call N depends on the discriminants
140
   --  of the task, it must be replaced with a reference to the discriminant
141
   --  of the task being called.
142
 
143
   procedure Resolve_Op_Concat_Arg
144
     (N       : Node_Id;
145
      Arg     : Node_Id;
146
      Typ     : Entity_Id;
147
      Is_Comp : Boolean);
148
   --  Internal procedure for Resolve_Op_Concat to resolve one operand of
149
   --  concatenation operator.  The operand is either of the array type or of
150
   --  the component type. If the operand is an aggregate, and the component
151
   --  type is composite, this is ambiguous if component type has aggregates.
152
 
153
   procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id);
154
   --  Does the first part of the work of Resolve_Op_Concat
155
 
156
   procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id);
157
   --  Does the "rest" of the work of Resolve_Op_Concat, after the left operand
158
   --  has been resolved. See Resolve_Op_Concat for details.
159
 
160
   procedure Resolve_Allocator                 (N : Node_Id; Typ : Entity_Id);
161
   procedure Resolve_Arithmetic_Op             (N : Node_Id; Typ : Entity_Id);
162
   procedure Resolve_Call                      (N : Node_Id; Typ : Entity_Id);
163
   procedure Resolve_Character_Literal         (N : Node_Id; Typ : Entity_Id);
164
   procedure Resolve_Comparison_Op             (N : Node_Id; Typ : Entity_Id);
165
   procedure Resolve_Conditional_Expression    (N : Node_Id; Typ : Entity_Id);
166
   procedure Resolve_Equality_Op               (N : Node_Id; Typ : Entity_Id);
167
   procedure Resolve_Explicit_Dereference      (N : Node_Id; Typ : Entity_Id);
168
   procedure Resolve_Entity_Name               (N : Node_Id; Typ : Entity_Id);
169
   procedure Resolve_Indexed_Component         (N : Node_Id; Typ : Entity_Id);
170
   procedure Resolve_Integer_Literal           (N : Node_Id; Typ : Entity_Id);
171
   procedure Resolve_Logical_Op                (N : Node_Id; Typ : Entity_Id);
172
   procedure Resolve_Membership_Op             (N : Node_Id; Typ : Entity_Id);
173
   procedure Resolve_Null                      (N : Node_Id; Typ : Entity_Id);
174
   procedure Resolve_Operator_Symbol           (N : Node_Id; Typ : Entity_Id);
175
   procedure Resolve_Op_Concat                 (N : Node_Id; Typ : Entity_Id);
176
   procedure Resolve_Op_Expon                  (N : Node_Id; Typ : Entity_Id);
177
   procedure Resolve_Op_Not                    (N : Node_Id; Typ : Entity_Id);
178
   procedure Resolve_Qualified_Expression      (N : Node_Id; Typ : Entity_Id);
179
   procedure Resolve_Range                     (N : Node_Id; Typ : Entity_Id);
180
   procedure Resolve_Real_Literal              (N : Node_Id; Typ : Entity_Id);
181
   procedure Resolve_Reference                 (N : Node_Id; Typ : Entity_Id);
182
   procedure Resolve_Selected_Component        (N : Node_Id; Typ : Entity_Id);
183
   procedure Resolve_Shift                     (N : Node_Id; Typ : Entity_Id);
184
   procedure Resolve_Short_Circuit             (N : Node_Id; Typ : Entity_Id);
185
   procedure Resolve_Slice                     (N : Node_Id; Typ : Entity_Id);
186
   procedure Resolve_String_Literal            (N : Node_Id; Typ : Entity_Id);
187
   procedure Resolve_Subprogram_Info           (N : Node_Id; Typ : Entity_Id);
188
   procedure Resolve_Type_Conversion           (N : Node_Id; Typ : Entity_Id);
189
   procedure Resolve_Unary_Op                  (N : Node_Id; Typ : Entity_Id);
190
   procedure Resolve_Unchecked_Expression      (N : Node_Id; Typ : Entity_Id);
191
   procedure Resolve_Unchecked_Type_Conversion (N : Node_Id; Typ : Entity_Id);
192
 
193
   function Operator_Kind
194
     (Op_Name   : Name_Id;
195
      Is_Binary : Boolean) return Node_Kind;
196
   --  Utility to map the name of an operator into the corresponding Node. Used
197
   --  by other node rewriting procedures.
198
 
199
   procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id);
200
   --  Resolve actuals of call, and add default expressions for missing ones.
201
   --  N is the Node_Id for the subprogram call, and Nam is the entity of the
202
   --  called subprogram.
203
 
204
   procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id);
205
   --  Called from Resolve_Call, when the prefix denotes an entry or element
206
   --  of entry family. Actuals are resolved as for subprograms, and the node
207
   --  is rebuilt as an entry call. Also called for protected operations. Typ
208
   --  is the context type, which is used when the operation is a protected
209
   --  function with no arguments, and the return value is indexed.
210
 
211
   procedure Resolve_Intrinsic_Operator (N : Node_Id; Typ : Entity_Id);
212
   --  A call to a user-defined intrinsic operator is rewritten as a call
213
   --  to the corresponding predefined operator, with suitable conversions.
214
 
215
   procedure Resolve_Intrinsic_Unary_Operator (N : Node_Id; Typ : Entity_Id);
216
   --  Ditto, for unary operators (only arithmetic ones)
217
 
218
   procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id);
219
   --  If an operator node resolves to a call to a user-defined operator,
220
   --  rewrite the node as a function call.
221
 
222
   procedure Make_Call_Into_Operator
223
     (N     : Node_Id;
224
      Typ   : Entity_Id;
225
      Op_Id : Entity_Id);
226
   --  Inverse transformation: if an operator is given in functional notation,
227
   --  then after resolving the node, transform into an operator node, so
228
   --  that operands are resolved properly. Recall that predefined operators
229
   --  do not have a full signature and special resolution rules apply.
230
 
231
   procedure Rewrite_Renamed_Operator
232
     (N   : Node_Id;
233
      Op  : Entity_Id;
234
      Typ : Entity_Id);
235
   --  An operator can rename another, e.g. in  an instantiation. In that
236
   --  case, the proper operator node must be constructed and resolved.
237
 
238
   procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id);
239
   --  The String_Literal_Subtype is built for all strings that are not
240
   --  operands of a static concatenation operation. If the argument is
241
   --  not a N_String_Literal node, then the call has no effect.
242
 
243
   procedure Set_Slice_Subtype (N : Node_Id);
244
   --  Build subtype of array type, with the range specified by the slice
245
 
246
   procedure Simplify_Type_Conversion (N : Node_Id);
247
   --  Called after N has been resolved and evaluated, but before range checks
248
   --  have been applied. Currently simplifies a combination of floating-point
249
   --  to integer conversion and Truncation attribute.
250
 
251
   function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id;
252
   --  A universal_fixed expression in an universal context is unambiguous
253
   --  if there is only one applicable fixed point type. Determining whether
254
   --  there is only one requires a search over all visible entities, and
255
   --  happens only in very pathological cases (see 6115-006).
256
 
257
   function Valid_Conversion
258
     (N       : Node_Id;
259
      Target  : Entity_Id;
260
      Operand : Node_Id) return Boolean;
261
   --  Verify legality rules given in 4.6 (8-23). Target is the target
262
   --  type of the conversion, which may be an implicit conversion of
263
   --  an actual parameter to an anonymous access type (in which case
264
   --  N denotes the actual parameter and N = Operand).
265
 
266
   -------------------------
267
   -- Ambiguous_Character --
268
   -------------------------
269
 
270
   procedure Ambiguous_Character (C : Node_Id) is
271
      E : Entity_Id;
272
 
273
   begin
274
      if Nkind (C) = N_Character_Literal then
275
         Error_Msg_N ("ambiguous character literal", C);
276
 
277
         --  First the ones in Standard
278
 
279
         Error_Msg_N
280
           ("\\possible interpretation: Character!", C);
281
         Error_Msg_N
282
           ("\\possible interpretation: Wide_Character!", C);
283
 
284
         --  Include Wide_Wide_Character in Ada 2005 mode
285
 
286
         if Ada_Version >= Ada_05 then
287
            Error_Msg_N
288
              ("\\possible interpretation: Wide_Wide_Character!", C);
289
         end if;
290
 
291
         --  Now any other types that match
292
 
293
         E := Current_Entity (C);
294
         while Present (E) loop
295
            Error_Msg_NE ("\\possible interpretation:}!", C, Etype (E));
296
            E := Homonym (E);
297
         end loop;
298
      end if;
299
   end Ambiguous_Character;
300
 
301
   -------------------------
302
   -- Analyze_And_Resolve --
303
   -------------------------
304
 
305
   procedure Analyze_And_Resolve (N : Node_Id) is
306
   begin
307
      Analyze (N);
308
      Resolve (N);
309
   end Analyze_And_Resolve;
310
 
311
   procedure Analyze_And_Resolve (N : Node_Id; Typ : Entity_Id) is
312
   begin
313
      Analyze (N);
314
      Resolve (N, Typ);
315
   end Analyze_And_Resolve;
316
 
317
   --  Version withs check(s) suppressed
318
 
319
   procedure Analyze_And_Resolve
320
     (N        : Node_Id;
321
      Typ      : Entity_Id;
322
      Suppress : Check_Id)
323
   is
324
      Scop : constant Entity_Id := Current_Scope;
325
 
326
   begin
327
      if Suppress = All_Checks then
328
         declare
329
            Svg : constant Suppress_Array := Scope_Suppress;
330
         begin
331
            Scope_Suppress := (others => True);
332
            Analyze_And_Resolve (N, Typ);
333
            Scope_Suppress := Svg;
334
         end;
335
 
336
      else
337
         declare
338
            Svg : constant Boolean := Scope_Suppress (Suppress);
339
 
340
         begin
341
            Scope_Suppress (Suppress) := True;
342
            Analyze_And_Resolve (N, Typ);
343
            Scope_Suppress (Suppress) := Svg;
344
         end;
345
      end if;
346
 
347
      if Current_Scope /= Scop
348
        and then Scope_Is_Transient
349
      then
350
         --  This can only happen if a transient scope was created
351
         --  for an inner expression, which will be removed upon
352
         --  completion of the analysis of an enclosing construct.
353
         --  The transient scope must have the suppress status of
354
         --  the enclosing environment, not of this Analyze call.
355
 
356
         Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
357
           Scope_Suppress;
358
      end if;
359
   end Analyze_And_Resolve;
360
 
361
   procedure Analyze_And_Resolve
362
     (N        : Node_Id;
363
      Suppress : Check_Id)
364
   is
365
      Scop : constant Entity_Id := Current_Scope;
366
 
367
   begin
368
      if Suppress = All_Checks then
369
         declare
370
            Svg : constant Suppress_Array := Scope_Suppress;
371
         begin
372
            Scope_Suppress := (others => True);
373
            Analyze_And_Resolve (N);
374
            Scope_Suppress := Svg;
375
         end;
376
 
377
      else
378
         declare
379
            Svg : constant Boolean := Scope_Suppress (Suppress);
380
 
381
         begin
382
            Scope_Suppress (Suppress) := True;
383
            Analyze_And_Resolve (N);
384
            Scope_Suppress (Suppress) := Svg;
385
         end;
386
      end if;
387
 
388
      if Current_Scope /= Scop
389
        and then Scope_Is_Transient
390
      then
391
         Scope_Stack.Table (Scope_Stack.Last).Save_Scope_Suppress :=
392
           Scope_Suppress;
393
      end if;
394
   end Analyze_And_Resolve;
395
 
396
   ----------------------------
397
   -- Check_Discriminant_Use --
398
   ----------------------------
399
 
400
   procedure Check_Discriminant_Use (N : Node_Id) is
401
      PN   : constant Node_Id   := Parent (N);
402
      Disc : constant Entity_Id := Entity (N);
403
      P    : Node_Id;
404
      D    : Node_Id;
405
 
406
   begin
407
      --  Any use in a spec-expression is legal
408
 
409
      if In_Spec_Expression then
410
         null;
411
 
412
      elsif Nkind (PN) = N_Range then
413
 
414
         --  Discriminant cannot be used to constrain a scalar type
415
 
416
         P := Parent (PN);
417
 
418
         if Nkind (P) = N_Range_Constraint
419
           and then Nkind (Parent (P)) = N_Subtype_Indication
420
           and then Nkind (Parent (Parent (P))) = N_Component_Definition
421
         then
422
            Error_Msg_N ("discriminant cannot constrain scalar type", N);
423
 
424
         elsif Nkind (P) = N_Index_Or_Discriminant_Constraint then
425
 
426
            --  The following check catches the unusual case where
427
            --  a discriminant appears within an index constraint
428
            --  that is part of a larger expression within a constraint
429
            --  on a component, e.g. "C : Int range 1 .. F (new A(1 .. D))".
430
            --  For now we only check case of record components, and
431
            --  note that a similar check should also apply in the
432
            --  case of discriminant constraints below. ???
433
 
434
            --  Note that the check for N_Subtype_Declaration below is to
435
            --  detect the valid use of discriminants in the constraints of a
436
            --  subtype declaration when this subtype declaration appears
437
            --  inside the scope of a record type (which is syntactically
438
            --  illegal, but which may be created as part of derived type
439
            --  processing for records). See Sem_Ch3.Build_Derived_Record_Type
440
            --  for more info.
441
 
442
            if Ekind (Current_Scope) = E_Record_Type
443
              and then Scope (Disc) = Current_Scope
444
              and then not
445
                (Nkind (Parent (P)) = N_Subtype_Indication
446
                  and then
447
                    Nkind_In (Parent (Parent (P)), N_Component_Definition,
448
                                                   N_Subtype_Declaration)
449
                  and then Paren_Count (N) = 0)
450
            then
451
               Error_Msg_N
452
                 ("discriminant must appear alone in component constraint", N);
453
               return;
454
            end if;
455
 
456
            --   Detect a common error:
457
 
458
            --   type R (D : Positive := 100) is record
459
            --     Name : String (1 .. D);
460
            --   end record;
461
 
462
            --  The default value causes an object of type R to be allocated
463
            --  with room for Positive'Last characters. The RM does not mandate
464
            --  the allocation of the maximum size, but that is what GNAT does
465
            --  so we should warn the programmer that there is a problem.
466
 
467
            Check_Large : declare
468
               SI : Node_Id;
469
               T  : Entity_Id;
470
               TB : Node_Id;
471
               CB : Entity_Id;
472
 
473
               function Large_Storage_Type (T : Entity_Id) return Boolean;
474
               --  Return True if type T has a large enough range that
475
               --  any array whose index type covered the whole range of
476
               --  the type would likely raise Storage_Error.
477
 
478
               ------------------------
479
               -- Large_Storage_Type --
480
               ------------------------
481
 
482
               function Large_Storage_Type (T : Entity_Id) return Boolean is
483
               begin
484
                  --  The type is considered large if its bounds are known at
485
                  --  compile time and if it requires at least as many bits as
486
                  --  a Positive to store the possible values.
487
 
488
                  return Compile_Time_Known_Value (Type_Low_Bound (T))
489
                    and then Compile_Time_Known_Value (Type_High_Bound (T))
490
                    and then
491
                      Minimum_Size (T, Biased => True) >=
492
                        RM_Size (Standard_Positive);
493
               end Large_Storage_Type;
494
 
495
            --  Start of processing for Check_Large
496
 
497
            begin
498
               --  Check that the Disc has a large range
499
 
500
               if not Large_Storage_Type (Etype (Disc)) then
501
                  goto No_Danger;
502
               end if;
503
 
504
               --  If the enclosing type is limited, we allocate only the
505
               --  default value, not the maximum, and there is no need for
506
               --  a warning.
507
 
508
               if Is_Limited_Type (Scope (Disc)) then
509
                  goto No_Danger;
510
               end if;
511
 
512
               --  Check that it is the high bound
513
 
514
               if N /= High_Bound (PN)
515
                 or else No (Discriminant_Default_Value (Disc))
516
               then
517
                  goto No_Danger;
518
               end if;
519
 
520
               --  Check the array allows a large range at this bound.
521
               --  First find the array
522
 
523
               SI := Parent (P);
524
 
525
               if Nkind (SI) /= N_Subtype_Indication then
526
                  goto No_Danger;
527
               end if;
528
 
529
               T := Entity (Subtype_Mark (SI));
530
 
531
               if not Is_Array_Type (T) then
532
                  goto No_Danger;
533
               end if;
534
 
535
               --  Next, find the dimension
536
 
537
               TB := First_Index (T);
538
               CB := First (Constraints (P));
539
               while True
540
                 and then Present (TB)
541
                 and then Present (CB)
542
                 and then CB /= PN
543
               loop
544
                  Next_Index (TB);
545
                  Next (CB);
546
               end loop;
547
 
548
               if CB /= PN then
549
                  goto No_Danger;
550
               end if;
551
 
552
               --  Now, check the dimension has a large range
553
 
554
               if not Large_Storage_Type (Etype (TB)) then
555
                  goto No_Danger;
556
               end if;
557
 
558
               --  Warn about the danger
559
 
560
               Error_Msg_N
561
                 ("?creation of & object may raise Storage_Error!",
562
                  Scope (Disc));
563
 
564
               <<No_Danger>>
565
                  null;
566
 
567
            end Check_Large;
568
         end if;
569
 
570
      --  Legal case is in index or discriminant constraint
571
 
572
      elsif Nkind_In (PN, N_Index_Or_Discriminant_Constraint,
573
                          N_Discriminant_Association)
574
      then
575
         if Paren_Count (N) > 0 then
576
            Error_Msg_N
577
              ("discriminant in constraint must appear alone",  N);
578
 
579
         elsif Nkind (N) = N_Expanded_Name
580
           and then Comes_From_Source (N)
581
         then
582
            Error_Msg_N
583
              ("discriminant must appear alone as a direct name", N);
584
         end if;
585
 
586
         return;
587
 
588
      --  Otherwise, context is an expression. It should not be within
589
      --  (i.e. a subexpression of) a constraint for a component.
590
 
591
      else
592
         D := PN;
593
         P := Parent (PN);
594
         while not Nkind_In (P, N_Component_Declaration,
595
                                N_Subtype_Indication,
596
                                N_Entry_Declaration)
597
         loop
598
            D := P;
599
            P := Parent (P);
600
            exit when No (P);
601
         end loop;
602
 
603
         --  If the discriminant is used in an expression that is a bound
604
         --  of a scalar type, an Itype is created and the bounds are attached
605
         --  to its range,  not to the original subtype indication. Such use
606
         --  is of course a double fault.
607
 
608
         if (Nkind (P) = N_Subtype_Indication
609
              and then Nkind_In (Parent (P), N_Component_Definition,
610
                                             N_Derived_Type_Definition)
611
              and then D = Constraint (P))
612
 
613
         --  The constraint itself may be given by a subtype indication,
614
         --  rather than by a more common discrete range.
615
 
616
           or else (Nkind (P) = N_Subtype_Indication
617
                      and then
618
                    Nkind (Parent (P)) = N_Index_Or_Discriminant_Constraint)
619
           or else Nkind (P) = N_Entry_Declaration
620
           or else Nkind (D) = N_Defining_Identifier
621
         then
622
            Error_Msg_N
623
              ("discriminant in constraint must appear alone",  N);
624
         end if;
625
      end if;
626
   end Check_Discriminant_Use;
627
 
628
   --------------------------------
629
   -- Check_For_Visible_Operator --
630
   --------------------------------
631
 
632
   procedure Check_For_Visible_Operator (N : Node_Id; T : Entity_Id) is
633
   begin
634
      if Is_Invisible_Operator (N, T) then
635
         Error_Msg_NE
636
           ("operator for} is not directly visible!", N, First_Subtype (T));
637
         Error_Msg_N ("use clause would make operation legal!", N);
638
      end if;
639
   end Check_For_Visible_Operator;
640
 
641
   ----------------------------------
642
   --  Check_Fully_Declared_Prefix --
643
   ----------------------------------
644
 
645
   procedure Check_Fully_Declared_Prefix
646
     (Typ  : Entity_Id;
647
      Pref : Node_Id)
648
   is
649
   begin
650
      --  Check that the designated type of the prefix of a dereference is
651
      --  not an incomplete type. This cannot be done unconditionally, because
652
      --  dereferences of private types are legal in default expressions. This
653
      --  case is taken care of in Check_Fully_Declared, called below. There
654
      --  are also 2005 cases where it is legal for the prefix to be unfrozen.
655
 
656
      --  This consideration also applies to similar checks for allocators,
657
      --  qualified expressions, and type conversions.
658
 
659
      --  An additional exception concerns other per-object expressions that
660
      --  are not directly related to component declarations, in particular
661
      --  representation pragmas for tasks. These will be per-object
662
      --  expressions if they depend on discriminants or some global entity.
663
      --  If the task has access discriminants, the designated type may be
664
      --  incomplete at the point the expression is resolved. This resolution
665
      --  takes place within the body of the initialization procedure, where
666
      --  the discriminant is replaced by its discriminal.
667
 
668
      if Is_Entity_Name (Pref)
669
        and then Ekind (Entity (Pref)) = E_In_Parameter
670
      then
671
         null;
672
 
673
      --  Ada 2005 (AI-326): Tagged incomplete types allowed. The wrong usages
674
      --  are handled by Analyze_Access_Attribute, Analyze_Assignment,
675
      --  Analyze_Object_Renaming, and Freeze_Entity.
676
 
677
      elsif Ada_Version >= Ada_05
678
        and then Is_Entity_Name (Pref)
679
        and then Is_Access_Type (Etype (Pref))
680
        and then Ekind (Directly_Designated_Type (Etype (Pref))) =
681
                                                       E_Incomplete_Type
682
        and then Is_Tagged_Type (Directly_Designated_Type (Etype (Pref)))
683
      then
684
         null;
685
      else
686
         Check_Fully_Declared (Typ, Parent (Pref));
687
      end if;
688
   end Check_Fully_Declared_Prefix;
689
 
690
   ------------------------------
691
   -- Check_Infinite_Recursion --
692
   ------------------------------
693
 
694
   function Check_Infinite_Recursion (N : Node_Id) return Boolean is
695
      P : Node_Id;
696
      C : Node_Id;
697
 
698
      function Same_Argument_List return Boolean;
699
      --  Check whether list of actuals is identical to list of formals
700
      --  of called function (which is also the enclosing scope).
701
 
702
      ------------------------
703
      -- Same_Argument_List --
704
      ------------------------
705
 
706
      function Same_Argument_List return Boolean is
707
         A    : Node_Id;
708
         F    : Entity_Id;
709
         Subp : Entity_Id;
710
 
711
      begin
712
         if not Is_Entity_Name (Name (N)) then
713
            return False;
714
         else
715
            Subp := Entity (Name (N));
716
         end if;
717
 
718
         F := First_Formal (Subp);
719
         A := First_Actual (N);
720
         while Present (F) and then Present (A) loop
721
            if not Is_Entity_Name (A)
722
              or else Entity (A) /= F
723
            then
724
               return False;
725
            end if;
726
 
727
            Next_Actual (A);
728
            Next_Formal (F);
729
         end loop;
730
 
731
         return True;
732
      end Same_Argument_List;
733
 
734
   --  Start of processing for Check_Infinite_Recursion
735
 
736
   begin
737
      --  Special case, if this is a procedure call and is a call to the
738
      --  current procedure with the same argument list, then this is for
739
      --  sure an infinite recursion and we insert a call to raise SE.
740
 
741
      if Is_List_Member (N)
742
        and then List_Length (List_Containing (N)) = 1
743
        and then Same_Argument_List
744
      then
745
         declare
746
            P : constant Node_Id := Parent (N);
747
         begin
748
            if Nkind (P) = N_Handled_Sequence_Of_Statements
749
              and then Nkind (Parent (P)) = N_Subprogram_Body
750
              and then Is_Empty_List (Declarations (Parent (P)))
751
            then
752
               Error_Msg_N ("!?infinite recursion", N);
753
               Error_Msg_N ("\!?Storage_Error will be raised at run time", N);
754
               Insert_Action (N,
755
                 Make_Raise_Storage_Error (Sloc (N),
756
                   Reason => SE_Infinite_Recursion));
757
               return True;
758
            end if;
759
         end;
760
      end if;
761
 
762
      --  If not that special case, search up tree, quitting if we reach a
763
      --  construct (e.g. a conditional) that tells us that this is not a
764
      --  case for an infinite recursion warning.
765
 
766
      C := N;
767
      loop
768
         P := Parent (C);
769
 
770
         --  If no parent, then we were not inside a subprogram, this can for
771
         --  example happen when processing certain pragmas in a spec. Just
772
         --  return False in this case.
773
 
774
         if No (P) then
775
            return False;
776
         end if;
777
 
778
         --  Done if we get to subprogram body, this is definitely an infinite
779
         --  recursion case if we did not find anything to stop us.
780
 
781
         exit when Nkind (P) = N_Subprogram_Body;
782
 
783
         --  If appearing in conditional, result is false
784
 
785
         if Nkind_In (P, N_Or_Else,
786
                         N_And_Then,
787
                         N_If_Statement,
788
                         N_Case_Statement)
789
         then
790
            return False;
791
 
792
         elsif Nkind (P) = N_Handled_Sequence_Of_Statements
793
           and then C /= First (Statements (P))
794
         then
795
            --  If the call is the expression of a return statement and the
796
            --  actuals are identical to the formals, it's worth a warning.
797
            --  However, we skip this if there is an immediately preceding
798
            --  raise statement, since the call is never executed.
799
 
800
            --  Furthermore, this corresponds to a common idiom:
801
 
802
            --    function F (L : Thing) return Boolean is
803
            --    begin
804
            --       raise Program_Error;
805
            --       return F (L);
806
            --    end F;
807
 
808
            --  for generating a stub function
809
 
810
            if Nkind (Parent (N)) = N_Simple_Return_Statement
811
              and then Same_Argument_List
812
            then
813
               exit when not Is_List_Member (Parent (N));
814
 
815
               --  OK, return statement is in a statement list, look for raise
816
 
817
               declare
818
                  Nod : Node_Id;
819
 
820
               begin
821
                  --  Skip past N_Freeze_Entity nodes generated by expansion
822
 
823
                  Nod := Prev (Parent (N));
824
                  while Present (Nod)
825
                    and then Nkind (Nod) = N_Freeze_Entity
826
                  loop
827
                     Prev (Nod);
828
                  end loop;
829
 
830
                  --  If no raise statement, give warning
831
 
832
                  exit when Nkind (Nod) /= N_Raise_Statement
833
                    and then
834
                      (Nkind (Nod) not in N_Raise_xxx_Error
835
                         or else Present (Condition (Nod)));
836
               end;
837
            end if;
838
 
839
            return False;
840
 
841
         else
842
            C := P;
843
         end if;
844
      end loop;
845
 
846
      Error_Msg_N ("!?possible infinite recursion", N);
847
      Error_Msg_N ("\!?Storage_Error may be raised at run time", N);
848
 
849
      return True;
850
   end Check_Infinite_Recursion;
851
 
852
   -------------------------------
853
   -- Check_Initialization_Call --
854
   -------------------------------
855
 
856
   procedure Check_Initialization_Call (N : Entity_Id; Nam : Entity_Id) is
857
      Typ : constant Entity_Id := Etype (First_Formal (Nam));
858
 
859
      function Uses_SS (T : Entity_Id) return Boolean;
860
      --  Check whether the creation of an object of the type will involve
861
      --  use of the secondary stack. If T is a record type, this is true
862
      --  if the expression for some component uses the secondary stack, e.g.
863
      --  through a call to a function that returns an unconstrained value.
864
      --  False if T is controlled, because cleanups occur elsewhere.
865
 
866
      -------------
867
      -- Uses_SS --
868
      -------------
869
 
870
      function Uses_SS (T : Entity_Id) return Boolean is
871
         Comp      : Entity_Id;
872
         Expr      : Node_Id;
873
         Full_Type : Entity_Id := Underlying_Type (T);
874
 
875
      begin
876
         --  Normally we want to use the underlying type, but if it's not set
877
         --  then continue with T.
878
 
879
         if not Present (Full_Type) then
880
            Full_Type := T;
881
         end if;
882
 
883
         if Is_Controlled (Full_Type) then
884
            return False;
885
 
886
         elsif Is_Array_Type (Full_Type) then
887
            return Uses_SS (Component_Type (Full_Type));
888
 
889
         elsif Is_Record_Type (Full_Type) then
890
            Comp := First_Component (Full_Type);
891
            while Present (Comp) loop
892
               if Ekind (Comp) = E_Component
893
                 and then Nkind (Parent (Comp)) = N_Component_Declaration
894
               then
895
                  --  The expression for a dynamic component may be rewritten
896
                  --  as a dereference, so retrieve original node.
897
 
898
                  Expr := Original_Node (Expression (Parent (Comp)));
899
 
900
                  --  Return True if the expression is a call to a function
901
                  --  (including an attribute function such as Image) with
902
                  --  a result that requires a transient scope.
903
 
904
                  if (Nkind (Expr) = N_Function_Call
905
                       or else (Nkind (Expr) = N_Attribute_Reference
906
                                 and then Present (Expressions (Expr))))
907
                    and then Requires_Transient_Scope (Etype (Expr))
908
                  then
909
                     return True;
910
 
911
                  elsif Uses_SS (Etype (Comp)) then
912
                     return True;
913
                  end if;
914
               end if;
915
 
916
               Next_Component (Comp);
917
            end loop;
918
 
919
            return False;
920
 
921
         else
922
            return False;
923
         end if;
924
      end Uses_SS;
925
 
926
   --  Start of processing for Check_Initialization_Call
927
 
928
   begin
929
      --  Establish a transient scope if the type needs it
930
 
931
      if Uses_SS (Typ) then
932
         Establish_Transient_Scope (First_Actual (N), Sec_Stack => True);
933
      end if;
934
   end Check_Initialization_Call;
935
 
936
   ---------------------------------------
937
   -- Check_No_Direct_Boolean_Operators --
938
   ---------------------------------------
939
 
940
   procedure Check_No_Direct_Boolean_Operators (N : Node_Id) is
941
   begin
942
      if Scope (Entity (N)) = Standard_Standard
943
        and then Root_Type (Etype (Left_Opnd (N))) = Standard_Boolean
944
      then
945
         --  Restriction only applies to original source code
946
 
947
         if Comes_From_Source (N) then
948
            Check_Restriction (No_Direct_Boolean_Operators, N);
949
         end if;
950
      end if;
951
 
952
      if Style_Check then
953
         Check_Boolean_Operator (N);
954
      end if;
955
   end Check_No_Direct_Boolean_Operators;
956
 
957
   ------------------------------
958
   -- Check_Parameterless_Call --
959
   ------------------------------
960
 
961
   procedure Check_Parameterless_Call (N : Node_Id) is
962
      Nam : Node_Id;
963
 
964
      function Prefix_Is_Access_Subp return Boolean;
965
      --  If the prefix is of an access_to_subprogram type, the node must be
966
      --  rewritten as a call. Ditto if the prefix is overloaded and all its
967
      --  interpretations are access to subprograms.
968
 
969
      ---------------------------
970
      -- Prefix_Is_Access_Subp --
971
      ---------------------------
972
 
973
      function Prefix_Is_Access_Subp return Boolean is
974
         I   : Interp_Index;
975
         It  : Interp;
976
 
977
      begin
978
         if not Is_Overloaded (N) then
979
            return
980
              Ekind (Etype (N)) = E_Subprogram_Type
981
                and then Base_Type (Etype (Etype (N))) /= Standard_Void_Type;
982
         else
983
            Get_First_Interp (N, I, It);
984
            while Present (It.Typ) loop
985
               if Ekind (It.Typ) /= E_Subprogram_Type
986
                 or else Base_Type (Etype (It.Typ)) = Standard_Void_Type
987
               then
988
                  return False;
989
               end if;
990
 
991
               Get_Next_Interp (I, It);
992
            end loop;
993
 
994
            return True;
995
         end if;
996
      end Prefix_Is_Access_Subp;
997
 
998
   --  Start of processing for Check_Parameterless_Call
999
 
1000
   begin
1001
      --  Defend against junk stuff if errors already detected
1002
 
1003
      if Total_Errors_Detected /= 0 then
1004
         if Nkind (N) in N_Has_Etype and then Etype (N) = Any_Type then
1005
            return;
1006
         elsif Nkind (N) in N_Has_Chars
1007
           and then Chars (N) in Error_Name_Or_No_Name
1008
         then
1009
            return;
1010
         end if;
1011
 
1012
         Require_Entity (N);
1013
      end if;
1014
 
1015
      --  If the context expects a value, and the name is a procedure, this is
1016
      --  most likely a missing 'Access. Don't try to resolve the parameterless
1017
      --  call, error will be caught when the outer call is analyzed.
1018
 
1019
      if Is_Entity_Name (N)
1020
        and then Ekind (Entity (N)) = E_Procedure
1021
        and then not Is_Overloaded (N)
1022
        and then
1023
         Nkind_In (Parent (N), N_Parameter_Association,
1024
                               N_Function_Call,
1025
                               N_Procedure_Call_Statement)
1026
      then
1027
         return;
1028
      end if;
1029
 
1030
      --  Rewrite as call if overloadable entity that is (or could be, in the
1031
      --  overloaded case) a function call. If we know for sure that the entity
1032
      --  is an enumeration literal, we do not rewrite it.
1033
 
1034
      if (Is_Entity_Name (N)
1035
            and then Is_Overloadable (Entity (N))
1036
            and then (Ekind (Entity (N)) /= E_Enumeration_Literal
1037
                        or else Is_Overloaded (N)))
1038
 
1039
      --  Rewrite as call if it is an explicit dereference of an expression of
1040
      --  a subprogram access type, and the subprogram type is not that of a
1041
      --  procedure or entry.
1042
 
1043
      or else
1044
        (Nkind (N) = N_Explicit_Dereference and then Prefix_Is_Access_Subp)
1045
 
1046
      --  Rewrite as call if it is a selected component which is a function,
1047
      --  this is the case of a call to a protected function (which may be
1048
      --  overloaded with other protected operations).
1049
 
1050
      or else
1051
        (Nkind (N) = N_Selected_Component
1052
          and then (Ekind (Entity (Selector_Name (N))) = E_Function
1053
                      or else
1054
                        ((Ekind (Entity (Selector_Name (N))) = E_Entry
1055
                            or else
1056
                          Ekind (Entity (Selector_Name (N))) = E_Procedure)
1057
                            and then Is_Overloaded (Selector_Name (N)))))
1058
 
1059
      --  If one of the above three conditions is met, rewrite as call.
1060
      --  Apply the rewriting only once.
1061
 
1062
      then
1063
         if Nkind (Parent (N)) /= N_Function_Call
1064
           or else N /= Name (Parent (N))
1065
         then
1066
            Nam := New_Copy (N);
1067
 
1068
            --  If overloaded, overload set belongs to new copy
1069
 
1070
            Save_Interps (N, Nam);
1071
 
1072
            --  Change node to parameterless function call (note that the
1073
            --  Parameter_Associations associations field is left set to Empty,
1074
            --  its normal default value since there are no parameters)
1075
 
1076
            Change_Node (N, N_Function_Call);
1077
            Set_Name (N, Nam);
1078
            Set_Sloc (N, Sloc (Nam));
1079
            Analyze_Call (N);
1080
         end if;
1081
 
1082
      elsif Nkind (N) = N_Parameter_Association then
1083
         Check_Parameterless_Call (Explicit_Actual_Parameter (N));
1084
      end if;
1085
   end Check_Parameterless_Call;
1086
 
1087
   -----------------------------
1088
   -- Is_Definite_Access_Type --
1089
   -----------------------------
1090
 
1091
   function Is_Definite_Access_Type (E : Entity_Id) return Boolean is
1092
      Btyp : constant Entity_Id := Base_Type (E);
1093
   begin
1094
      return Ekind (Btyp) = E_Access_Type
1095
        or else (Ekind (Btyp) = E_Access_Subprogram_Type
1096
                  and then Comes_From_Source (Btyp));
1097
   end Is_Definite_Access_Type;
1098
 
1099
   ----------------------
1100
   -- Is_Predefined_Op --
1101
   ----------------------
1102
 
1103
   function Is_Predefined_Op (Nam : Entity_Id) return Boolean is
1104
   begin
1105
      return Is_Intrinsic_Subprogram (Nam)
1106
        and then not Is_Generic_Instance (Nam)
1107
        and then Chars (Nam) in Any_Operator_Name
1108
        and then (No (Alias (Nam))
1109
                   or else Is_Predefined_Op (Alias (Nam)));
1110
   end Is_Predefined_Op;
1111
 
1112
   -----------------------------
1113
   -- Make_Call_Into_Operator --
1114
   -----------------------------
1115
 
1116
   procedure Make_Call_Into_Operator
1117
     (N     : Node_Id;
1118
      Typ   : Entity_Id;
1119
      Op_Id : Entity_Id)
1120
   is
1121
      Op_Name   : constant Name_Id := Chars (Op_Id);
1122
      Act1      : Node_Id := First_Actual (N);
1123
      Act2      : Node_Id := Next_Actual (Act1);
1124
      Error     : Boolean := False;
1125
      Func      : constant Entity_Id := Entity (Name (N));
1126
      Is_Binary : constant Boolean   := Present (Act2);
1127
      Op_Node   : Node_Id;
1128
      Opnd_Type : Entity_Id;
1129
      Orig_Type : Entity_Id := Empty;
1130
      Pack      : Entity_Id;
1131
 
1132
      type Kind_Test is access function (E : Entity_Id) return Boolean;
1133
 
1134
      function Operand_Type_In_Scope (S : Entity_Id) return Boolean;
1135
      --  If the operand is not universal, and the operator is given by a
1136
      --  expanded name,  verify that the operand has an interpretation with
1137
      --  a type defined in the given scope of the operator.
1138
 
1139
      function Type_In_P (Test : Kind_Test) return Entity_Id;
1140
      --  Find a type of the given class in the package Pack that contains
1141
      --  the operator.
1142
 
1143
      ---------------------------
1144
      -- Operand_Type_In_Scope --
1145
      ---------------------------
1146
 
1147
      function Operand_Type_In_Scope (S : Entity_Id) return Boolean is
1148
         Nod : constant Node_Id := Right_Opnd (Op_Node);
1149
         I   : Interp_Index;
1150
         It  : Interp;
1151
 
1152
      begin
1153
         if not Is_Overloaded (Nod) then
1154
            return Scope (Base_Type (Etype (Nod))) = S;
1155
 
1156
         else
1157
            Get_First_Interp (Nod, I, It);
1158
            while Present (It.Typ) loop
1159
               if Scope (Base_Type (It.Typ)) = S then
1160
                  return True;
1161
               end if;
1162
 
1163
               Get_Next_Interp (I, It);
1164
            end loop;
1165
 
1166
            return False;
1167
         end if;
1168
      end Operand_Type_In_Scope;
1169
 
1170
      ---------------
1171
      -- Type_In_P --
1172
      ---------------
1173
 
1174
      function Type_In_P (Test : Kind_Test) return Entity_Id is
1175
         E : Entity_Id;
1176
 
1177
         function In_Decl return Boolean;
1178
         --  Verify that node is not part of the type declaration for the
1179
         --  candidate type, which would otherwise be invisible.
1180
 
1181
         -------------
1182
         -- In_Decl --
1183
         -------------
1184
 
1185
         function In_Decl return Boolean is
1186
            Decl_Node : constant Node_Id := Parent (E);
1187
            N2        : Node_Id;
1188
 
1189
         begin
1190
            N2 := N;
1191
 
1192
            if Etype (E) = Any_Type then
1193
               return True;
1194
 
1195
            elsif No (Decl_Node) then
1196
               return False;
1197
 
1198
            else
1199
               while Present (N2)
1200
                 and then Nkind (N2) /= N_Compilation_Unit
1201
               loop
1202
                  if N2 = Decl_Node then
1203
                     return True;
1204
                  else
1205
                     N2 := Parent (N2);
1206
                  end if;
1207
               end loop;
1208
 
1209
               return False;
1210
            end if;
1211
         end In_Decl;
1212
 
1213
      --  Start of processing for Type_In_P
1214
 
1215
      begin
1216
         --  If the context type is declared in the prefix package, this
1217
         --  is the desired base type.
1218
 
1219
         if Scope (Base_Type (Typ)) = Pack
1220
           and then Test (Typ)
1221
         then
1222
            return Base_Type (Typ);
1223
 
1224
         else
1225
            E := First_Entity (Pack);
1226
            while Present (E) loop
1227
               if Test (E)
1228
                 and then not In_Decl
1229
               then
1230
                  return E;
1231
               end if;
1232
 
1233
               Next_Entity (E);
1234
            end loop;
1235
 
1236
            return Empty;
1237
         end if;
1238
      end Type_In_P;
1239
 
1240
   --  Start of processing for Make_Call_Into_Operator
1241
 
1242
   begin
1243
      Op_Node := New_Node (Operator_Kind (Op_Name, Is_Binary), Sloc (N));
1244
 
1245
      --  Binary operator
1246
 
1247
      if Is_Binary then
1248
         Set_Left_Opnd  (Op_Node, Relocate_Node (Act1));
1249
         Set_Right_Opnd (Op_Node, Relocate_Node (Act2));
1250
         Save_Interps (Act1, Left_Opnd  (Op_Node));
1251
         Save_Interps (Act2, Right_Opnd (Op_Node));
1252
         Act1 := Left_Opnd (Op_Node);
1253
         Act2 := Right_Opnd (Op_Node);
1254
 
1255
      --  Unary operator
1256
 
1257
      else
1258
         Set_Right_Opnd (Op_Node, Relocate_Node (Act1));
1259
         Save_Interps (Act1, Right_Opnd (Op_Node));
1260
         Act1 := Right_Opnd (Op_Node);
1261
      end if;
1262
 
1263
      --  If the operator is denoted by an expanded name, and the prefix is
1264
      --  not Standard, but the operator is a predefined one whose scope is
1265
      --  Standard, then this is an implicit_operator, inserted as an
1266
      --  interpretation by the procedure of the same name. This procedure
1267
      --  overestimates the presence of implicit operators, because it does
1268
      --  not examine the type of the operands. Verify now that the operand
1269
      --  type appears in the given scope. If right operand is universal,
1270
      --  check the other operand. In the case of concatenation, either
1271
      --  argument can be the component type, so check the type of the result.
1272
      --  If both arguments are literals, look for a type of the right kind
1273
      --  defined in the given scope. This elaborate nonsense is brought to
1274
      --  you courtesy of b33302a. The type itself must be frozen, so we must
1275
      --  find the type of the proper class in the given scope.
1276
 
1277
      --  A final wrinkle is the multiplication operator for fixed point
1278
      --  types, which is defined in Standard only, and not in the scope of
1279
      --  the fixed_point type itself.
1280
 
1281
      if Nkind (Name (N)) = N_Expanded_Name then
1282
         Pack := Entity (Prefix (Name (N)));
1283
 
1284
         --  If the entity being called is defined in the given package,
1285
         --  it is a renaming of a predefined operator, and known to be
1286
         --  legal.
1287
 
1288
         if Scope (Entity (Name (N))) = Pack
1289
            and then Pack /= Standard_Standard
1290
         then
1291
            null;
1292
 
1293
         --  Visibility does not need to be checked in an instance: if the
1294
         --  operator was not visible in the generic it has been diagnosed
1295
         --  already, else there is an implicit copy of it in the instance.
1296
 
1297
         elsif In_Instance then
1298
            null;
1299
 
1300
         elsif (Op_Name =  Name_Op_Multiply
1301
              or else Op_Name = Name_Op_Divide)
1302
           and then Is_Fixed_Point_Type (Etype (Left_Opnd  (Op_Node)))
1303
           and then Is_Fixed_Point_Type (Etype (Right_Opnd (Op_Node)))
1304
         then
1305
            if Pack /= Standard_Standard then
1306
               Error := True;
1307
            end if;
1308
 
1309
         --  Ada 2005, AI-420:  Predefined equality on Universal_Access
1310
         --  is available.
1311
 
1312
         elsif Ada_Version >= Ada_05
1313
           and then (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1314
           and then Ekind (Etype (Act1)) = E_Anonymous_Access_Type
1315
         then
1316
            null;
1317
 
1318
         else
1319
            Opnd_Type := Base_Type (Etype (Right_Opnd (Op_Node)));
1320
 
1321
            if Op_Name = Name_Op_Concat then
1322
               Opnd_Type := Base_Type (Typ);
1323
 
1324
            elsif (Scope (Opnd_Type) = Standard_Standard
1325
                     and then Is_Binary)
1326
              or else (Nkind (Right_Opnd (Op_Node)) = N_Attribute_Reference
1327
                        and then Is_Binary
1328
                        and then not Comes_From_Source (Opnd_Type))
1329
            then
1330
               Opnd_Type := Base_Type (Etype (Left_Opnd (Op_Node)));
1331
            end if;
1332
 
1333
            if Scope (Opnd_Type) = Standard_Standard then
1334
 
1335
               --  Verify that the scope contains a type that corresponds to
1336
               --  the given literal. Optimize the case where Pack is Standard.
1337
 
1338
               if Pack /= Standard_Standard then
1339
 
1340
                  if Opnd_Type = Universal_Integer then
1341
                     Orig_Type :=  Type_In_P (Is_Integer_Type'Access);
1342
 
1343
                  elsif Opnd_Type = Universal_Real then
1344
                     Orig_Type := Type_In_P (Is_Real_Type'Access);
1345
 
1346
                  elsif Opnd_Type = Any_String then
1347
                     Orig_Type := Type_In_P (Is_String_Type'Access);
1348
 
1349
                  elsif Opnd_Type = Any_Access then
1350
                     Orig_Type :=  Type_In_P (Is_Definite_Access_Type'Access);
1351
 
1352
                  elsif Opnd_Type = Any_Composite then
1353
                     Orig_Type := Type_In_P (Is_Composite_Type'Access);
1354
 
1355
                     if Present (Orig_Type) then
1356
                        if Has_Private_Component (Orig_Type) then
1357
                           Orig_Type := Empty;
1358
                        else
1359
                           Set_Etype (Act1, Orig_Type);
1360
 
1361
                           if Is_Binary then
1362
                              Set_Etype (Act2, Orig_Type);
1363
                           end if;
1364
                        end if;
1365
                     end if;
1366
 
1367
                  else
1368
                     Orig_Type := Empty;
1369
                  end if;
1370
 
1371
                  Error := No (Orig_Type);
1372
               end if;
1373
 
1374
            elsif Ekind (Opnd_Type) = E_Allocator_Type
1375
               and then No (Type_In_P (Is_Definite_Access_Type'Access))
1376
            then
1377
               Error := True;
1378
 
1379
            --  If the type is defined elsewhere, and the operator is not
1380
            --  defined in the given scope (by a renaming declaration, e.g.)
1381
            --  then this is an error as well. If an extension of System is
1382
            --  present, and the type may be defined there, Pack must be
1383
            --  System itself.
1384
 
1385
            elsif Scope (Opnd_Type) /= Pack
1386
              and then Scope (Op_Id) /= Pack
1387
              and then (No (System_Aux_Id)
1388
                         or else Scope (Opnd_Type) /= System_Aux_Id
1389
                         or else Pack /= Scope (System_Aux_Id))
1390
            then
1391
               if not Is_Overloaded (Right_Opnd (Op_Node)) then
1392
                  Error := True;
1393
               else
1394
                  Error := not Operand_Type_In_Scope (Pack);
1395
               end if;
1396
 
1397
            elsif Pack = Standard_Standard
1398
              and then not Operand_Type_In_Scope (Standard_Standard)
1399
            then
1400
               Error := True;
1401
            end if;
1402
         end if;
1403
 
1404
         if Error then
1405
            Error_Msg_Node_2 := Pack;
1406
            Error_Msg_NE
1407
              ("& not declared in&", N, Selector_Name (Name (N)));
1408
            Set_Etype (N, Any_Type);
1409
            return;
1410
         end if;
1411
      end if;
1412
 
1413
      Set_Chars  (Op_Node, Op_Name);
1414
 
1415
      if not Is_Private_Type (Etype (N)) then
1416
         Set_Etype (Op_Node, Base_Type (Etype (N)));
1417
      else
1418
         Set_Etype (Op_Node, Etype (N));
1419
      end if;
1420
 
1421
      --  If this is a call to a function that renames a predefined equality,
1422
      --  the renaming declaration provides a type that must be used to
1423
      --  resolve the operands. This must be done now because resolution of
1424
      --  the equality node will not resolve any remaining ambiguity, and it
1425
      --  assumes that the first operand is not overloaded.
1426
 
1427
      if (Op_Name = Name_Op_Eq or else Op_Name = Name_Op_Ne)
1428
        and then Ekind (Func) = E_Function
1429
        and then Is_Overloaded (Act1)
1430
      then
1431
         Resolve (Act1, Base_Type (Etype (First_Formal (Func))));
1432
         Resolve (Act2, Base_Type (Etype (First_Formal (Func))));
1433
      end if;
1434
 
1435
      Set_Entity (Op_Node, Op_Id);
1436
      Generate_Reference (Op_Id, N, ' ');
1437
 
1438
      --  Do rewrite setting Comes_From_Source on the result if the original
1439
      --  call came from source. Although it is not strictly the case that the
1440
      --  operator as such comes from the source, logically it corresponds
1441
      --  exactly to the function call in the source, so it should be marked
1442
      --  this way (e.g. to make sure that validity checks work fine).
1443
 
1444
      declare
1445
         CS : constant Boolean := Comes_From_Source (N);
1446
      begin
1447
         Rewrite (N, Op_Node);
1448
         Set_Comes_From_Source (N, CS);
1449
      end;
1450
 
1451
      --  If this is an arithmetic operator and the result type is private,
1452
      --  the operands and the result must be wrapped in conversion to
1453
      --  expose the underlying numeric type and expand the proper checks,
1454
      --  e.g. on division.
1455
 
1456
      if Is_Private_Type (Typ) then
1457
         case Nkind (N) is
1458
            when N_Op_Add  | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
1459
            N_Op_Expon     | N_Op_Mod      | N_Op_Rem      =>
1460
               Resolve_Intrinsic_Operator (N, Typ);
1461
 
1462
            when N_Op_Plus | N_Op_Minus    | N_Op_Abs      =>
1463
               Resolve_Intrinsic_Unary_Operator (N, Typ);
1464
 
1465
            when others =>
1466
               Resolve (N, Typ);
1467
         end case;
1468
      else
1469
         Resolve (N, Typ);
1470
      end if;
1471
 
1472
      --  For predefined operators on literals, the operation freezes
1473
      --  their type.
1474
 
1475
      if Present (Orig_Type) then
1476
         Set_Etype (Act1, Orig_Type);
1477
         Freeze_Expression (Act1);
1478
      end if;
1479
   end Make_Call_Into_Operator;
1480
 
1481
   -------------------
1482
   -- Operator_Kind --
1483
   -------------------
1484
 
1485
   function Operator_Kind
1486
     (Op_Name   : Name_Id;
1487
      Is_Binary : Boolean) return Node_Kind
1488
   is
1489
      Kind : Node_Kind;
1490
 
1491
   begin
1492
      if Is_Binary then
1493
         if    Op_Name =  Name_Op_And      then
1494
            Kind := N_Op_And;
1495
         elsif Op_Name =  Name_Op_Or       then
1496
            Kind := N_Op_Or;
1497
         elsif Op_Name =  Name_Op_Xor      then
1498
            Kind := N_Op_Xor;
1499
         elsif Op_Name =  Name_Op_Eq       then
1500
            Kind := N_Op_Eq;
1501
         elsif Op_Name =  Name_Op_Ne       then
1502
            Kind := N_Op_Ne;
1503
         elsif Op_Name =  Name_Op_Lt       then
1504
            Kind := N_Op_Lt;
1505
         elsif Op_Name =  Name_Op_Le       then
1506
            Kind := N_Op_Le;
1507
         elsif Op_Name =  Name_Op_Gt       then
1508
            Kind := N_Op_Gt;
1509
         elsif Op_Name =  Name_Op_Ge       then
1510
            Kind := N_Op_Ge;
1511
         elsif Op_Name =  Name_Op_Add      then
1512
            Kind := N_Op_Add;
1513
         elsif Op_Name =  Name_Op_Subtract then
1514
            Kind := N_Op_Subtract;
1515
         elsif Op_Name =  Name_Op_Concat   then
1516
            Kind := N_Op_Concat;
1517
         elsif Op_Name =  Name_Op_Multiply then
1518
            Kind := N_Op_Multiply;
1519
         elsif Op_Name =  Name_Op_Divide   then
1520
            Kind := N_Op_Divide;
1521
         elsif Op_Name =  Name_Op_Mod      then
1522
            Kind := N_Op_Mod;
1523
         elsif Op_Name =  Name_Op_Rem      then
1524
            Kind := N_Op_Rem;
1525
         elsif Op_Name =  Name_Op_Expon    then
1526
            Kind := N_Op_Expon;
1527
         else
1528
            raise Program_Error;
1529
         end if;
1530
 
1531
      --  Unary operators
1532
 
1533
      else
1534
         if    Op_Name =  Name_Op_Add      then
1535
            Kind := N_Op_Plus;
1536
         elsif Op_Name =  Name_Op_Subtract then
1537
            Kind := N_Op_Minus;
1538
         elsif Op_Name =  Name_Op_Abs      then
1539
            Kind := N_Op_Abs;
1540
         elsif Op_Name =  Name_Op_Not      then
1541
            Kind := N_Op_Not;
1542
         else
1543
            raise Program_Error;
1544
         end if;
1545
      end if;
1546
 
1547
      return Kind;
1548
   end Operator_Kind;
1549
 
1550
   ----------------------------
1551
   -- Preanalyze_And_Resolve --
1552
   ----------------------------
1553
 
1554
   procedure Preanalyze_And_Resolve (N : Node_Id; T : Entity_Id) is
1555
      Save_Full_Analysis : constant Boolean := Full_Analysis;
1556
 
1557
   begin
1558
      Full_Analysis := False;
1559
      Expander_Mode_Save_And_Set (False);
1560
 
1561
      --  We suppress all checks for this analysis, since the checks will
1562
      --  be applied properly, and in the right location, when the default
1563
      --  expression is reanalyzed and reexpanded later on.
1564
 
1565
      Analyze_And_Resolve (N, T, Suppress => All_Checks);
1566
 
1567
      Expander_Mode_Restore;
1568
      Full_Analysis := Save_Full_Analysis;
1569
   end Preanalyze_And_Resolve;
1570
 
1571
   --  Version without context type
1572
 
1573
   procedure Preanalyze_And_Resolve (N : Node_Id) is
1574
      Save_Full_Analysis : constant Boolean := Full_Analysis;
1575
 
1576
   begin
1577
      Full_Analysis := False;
1578
      Expander_Mode_Save_And_Set (False);
1579
 
1580
      Analyze (N);
1581
      Resolve (N, Etype (N), Suppress => All_Checks);
1582
 
1583
      Expander_Mode_Restore;
1584
      Full_Analysis := Save_Full_Analysis;
1585
   end Preanalyze_And_Resolve;
1586
 
1587
   ----------------------------------
1588
   -- Replace_Actual_Discriminants --
1589
   ----------------------------------
1590
 
1591
   procedure Replace_Actual_Discriminants (N : Node_Id; Default : Node_Id) is
1592
      Loc : constant Source_Ptr := Sloc (N);
1593
      Tsk : Node_Id := Empty;
1594
 
1595
      function Process_Discr (Nod : Node_Id) return Traverse_Result;
1596
 
1597
      -------------------
1598
      -- Process_Discr --
1599
      -------------------
1600
 
1601
      function Process_Discr (Nod : Node_Id) return Traverse_Result is
1602
         Ent : Entity_Id;
1603
 
1604
      begin
1605
         if Nkind (Nod) = N_Identifier then
1606
            Ent := Entity (Nod);
1607
 
1608
            if Present (Ent)
1609
              and then Ekind (Ent) = E_Discriminant
1610
            then
1611
               Rewrite (Nod,
1612
                 Make_Selected_Component (Loc,
1613
                   Prefix        => New_Copy_Tree (Tsk, New_Sloc => Loc),
1614
                   Selector_Name => Make_Identifier (Loc, Chars (Ent))));
1615
 
1616
               Set_Etype (Nod, Etype (Ent));
1617
            end if;
1618
 
1619
         end if;
1620
 
1621
         return OK;
1622
      end Process_Discr;
1623
 
1624
      procedure Replace_Discrs is new Traverse_Proc (Process_Discr);
1625
 
1626
   --  Start of processing for Replace_Actual_Discriminants
1627
 
1628
   begin
1629
      if not Expander_Active then
1630
         return;
1631
      end if;
1632
 
1633
      if Nkind (Name (N)) = N_Selected_Component then
1634
         Tsk := Prefix (Name (N));
1635
 
1636
      elsif Nkind (Name (N)) = N_Indexed_Component then
1637
         Tsk := Prefix (Prefix (Name (N)));
1638
      end if;
1639
 
1640
      if No (Tsk) then
1641
         return;
1642
      else
1643
         Replace_Discrs (Default);
1644
      end if;
1645
   end Replace_Actual_Discriminants;
1646
 
1647
   -------------
1648
   -- Resolve --
1649
   -------------
1650
 
1651
   procedure Resolve (N : Node_Id; Typ : Entity_Id) is
1652
      Ambiguous : Boolean   := False;
1653
      Ctx_Type  : Entity_Id := Typ;
1654
      Expr_Type : Entity_Id := Empty; -- prevent junk warning
1655
      Err_Type  : Entity_Id := Empty;
1656
      Found     : Boolean   := False;
1657
      From_Lib  : Boolean;
1658
      I         : Interp_Index;
1659
      I1        : Interp_Index := 0;  -- prevent junk warning
1660
      It        : Interp;
1661
      It1       : Interp;
1662
      Seen      : Entity_Id := Empty; -- prevent junk warning
1663
 
1664
      function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean;
1665
      --  Determine whether a node comes from a predefined library unit or
1666
      --  Standard.
1667
 
1668
      procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id);
1669
      --  Try and fix up a literal so that it matches its expected type. New
1670
      --  literals are manufactured if necessary to avoid cascaded errors.
1671
 
1672
      procedure Resolution_Failed;
1673
      --  Called when attempt at resolving current expression fails
1674
 
1675
      ------------------------------------
1676
      -- Comes_From_Predefined_Lib_Unit --
1677
      -------------------------------------
1678
 
1679
      function Comes_From_Predefined_Lib_Unit (Nod : Node_Id) return Boolean is
1680
      begin
1681
         return
1682
           Sloc (Nod) = Standard_Location
1683
             or else Is_Predefined_File_Name (Unit_File_Name (
1684
                       Get_Source_Unit (Sloc (Nod))));
1685
      end Comes_From_Predefined_Lib_Unit;
1686
 
1687
      --------------------
1688
      -- Patch_Up_Value --
1689
      --------------------
1690
 
1691
      procedure Patch_Up_Value (N : Node_Id; Typ : Entity_Id) is
1692
      begin
1693
         if Nkind (N) = N_Integer_Literal
1694
           and then Is_Real_Type (Typ)
1695
         then
1696
            Rewrite (N,
1697
              Make_Real_Literal (Sloc (N),
1698
                Realval => UR_From_Uint (Intval (N))));
1699
            Set_Etype (N, Universal_Real);
1700
            Set_Is_Static_Expression (N);
1701
 
1702
         elsif Nkind (N) = N_Real_Literal
1703
           and then Is_Integer_Type (Typ)
1704
         then
1705
            Rewrite (N,
1706
              Make_Integer_Literal (Sloc (N),
1707
                Intval => UR_To_Uint (Realval (N))));
1708
            Set_Etype (N, Universal_Integer);
1709
            Set_Is_Static_Expression (N);
1710
 
1711
         elsif Nkind (N) = N_String_Literal
1712
           and then Is_Character_Type (Typ)
1713
         then
1714
            Set_Character_Literal_Name (Char_Code (Character'Pos ('A')));
1715
            Rewrite (N,
1716
              Make_Character_Literal (Sloc (N),
1717
                Chars => Name_Find,
1718
                Char_Literal_Value =>
1719
                  UI_From_Int (Character'Pos ('A'))));
1720
            Set_Etype (N, Any_Character);
1721
            Set_Is_Static_Expression (N);
1722
 
1723
         elsif Nkind (N) /= N_String_Literal
1724
           and then Is_String_Type (Typ)
1725
         then
1726
            Rewrite (N,
1727
              Make_String_Literal (Sloc (N),
1728
                Strval => End_String));
1729
 
1730
         elsif Nkind (N) = N_Range then
1731
            Patch_Up_Value (Low_Bound (N), Typ);
1732
            Patch_Up_Value (High_Bound (N), Typ);
1733
         end if;
1734
      end Patch_Up_Value;
1735
 
1736
      -----------------------
1737
      -- Resolution_Failed --
1738
      -----------------------
1739
 
1740
      procedure Resolution_Failed is
1741
      begin
1742
         Patch_Up_Value (N, Typ);
1743
         Set_Etype (N, Typ);
1744
         Debug_A_Exit ("resolving  ", N, " (done, resolution failed)");
1745
         Set_Is_Overloaded (N, False);
1746
 
1747
         --  The caller will return without calling the expander, so we need
1748
         --  to set the analyzed flag. Note that it is fine to set Analyzed
1749
         --  to True even if we are in the middle of a shallow analysis,
1750
         --  (see the spec of sem for more details) since this is an error
1751
         --  situation anyway, and there is no point in repeating the
1752
         --  analysis later (indeed it won't work to repeat it later, since
1753
         --  we haven't got a clear resolution of which entity is being
1754
         --  referenced.)
1755
 
1756
         Set_Analyzed (N, True);
1757
         return;
1758
      end Resolution_Failed;
1759
 
1760
   --  Start of processing for Resolve
1761
 
1762
   begin
1763
      if N = Error then
1764
         return;
1765
      end if;
1766
 
1767
      --  Access attribute on remote subprogram cannot be used for
1768
      --  a non-remote access-to-subprogram type.
1769
 
1770
      if Nkind (N) = N_Attribute_Reference
1771
        and then (Attribute_Name (N) = Name_Access
1772
                    or else Attribute_Name (N) = Name_Unrestricted_Access
1773
                    or else Attribute_Name (N) = Name_Unchecked_Access)
1774
        and then Comes_From_Source (N)
1775
        and then Is_Entity_Name (Prefix (N))
1776
        and then Is_Subprogram (Entity (Prefix (N)))
1777
        and then Is_Remote_Call_Interface (Entity (Prefix (N)))
1778
        and then not Is_Remote_Access_To_Subprogram_Type (Typ)
1779
      then
1780
         Error_Msg_N
1781
           ("prefix must statically denote a non-remote subprogram", N);
1782
      end if;
1783
 
1784
      From_Lib := Comes_From_Predefined_Lib_Unit (N);
1785
 
1786
      --  If the context is a Remote_Access_To_Subprogram, access attributes
1787
      --  must be resolved with the corresponding fat pointer. There is no need
1788
      --  to check for the attribute name since the return type of an
1789
      --  attribute is never a remote type.
1790
 
1791
      if Nkind (N) = N_Attribute_Reference
1792
        and then Comes_From_Source (N)
1793
        and then (Is_Remote_Call_Interface (Typ)
1794
                    or else Is_Remote_Types (Typ))
1795
      then
1796
         declare
1797
            Attr      : constant Attribute_Id :=
1798
                          Get_Attribute_Id (Attribute_Name (N));
1799
            Pref      : constant Node_Id      := Prefix (N);
1800
            Decl      : Node_Id;
1801
            Spec      : Node_Id;
1802
            Is_Remote : Boolean := True;
1803
 
1804
         begin
1805
            --  Check that Typ is a remote access-to-subprogram type
1806
 
1807
            if Is_Remote_Access_To_Subprogram_Type (Typ) then
1808
               --  Prefix (N) must statically denote a remote subprogram
1809
               --  declared in a package specification.
1810
 
1811
               if Attr = Attribute_Access then
1812
                  Decl := Unit_Declaration_Node (Entity (Pref));
1813
 
1814
                  if Nkind (Decl) = N_Subprogram_Body then
1815
                     Spec := Corresponding_Spec (Decl);
1816
 
1817
                     if not No (Spec) then
1818
                        Decl := Unit_Declaration_Node (Spec);
1819
                     end if;
1820
                  end if;
1821
 
1822
                  Spec := Parent (Decl);
1823
 
1824
                  if not Is_Entity_Name (Prefix (N))
1825
                    or else Nkind (Spec) /= N_Package_Specification
1826
                    or else
1827
                      not Is_Remote_Call_Interface (Defining_Entity (Spec))
1828
                  then
1829
                     Is_Remote := False;
1830
                     Error_Msg_N
1831
                       ("prefix must statically denote a remote subprogram ",
1832
                        N);
1833
                  end if;
1834
               end if;
1835
 
1836
               --   If we are generating code for a distributed program.
1837
               --   perform semantic checks against the corresponding
1838
               --   remote entities.
1839
 
1840
               if (Attr = Attribute_Access
1841
                    or else Attr = Attribute_Unchecked_Access
1842
                    or else Attr = Attribute_Unrestricted_Access)
1843
                 and then Expander_Active
1844
                 and then Get_PCS_Name /= Name_No_DSA
1845
               then
1846
                  Check_Subtype_Conformant
1847
                    (New_Id  => Entity (Prefix (N)),
1848
                     Old_Id  => Designated_Type
1849
                       (Corresponding_Remote_Type (Typ)),
1850
                     Err_Loc => N);
1851
 
1852
                  if Is_Remote then
1853
                     Process_Remote_AST_Attribute (N, Typ);
1854
                  end if;
1855
               end if;
1856
            end if;
1857
         end;
1858
      end if;
1859
 
1860
      Debug_A_Entry ("resolving  ", N);
1861
 
1862
      if Comes_From_Source (N) then
1863
         if Is_Fixed_Point_Type (Typ) then
1864
            Check_Restriction (No_Fixed_Point, N);
1865
 
1866
         elsif Is_Floating_Point_Type (Typ)
1867
           and then Typ /= Universal_Real
1868
           and then Typ /= Any_Real
1869
         then
1870
            Check_Restriction (No_Floating_Point, N);
1871
         end if;
1872
      end if;
1873
 
1874
      --  Return if already analyzed
1875
 
1876
      if Analyzed (N) then
1877
         Debug_A_Exit ("resolving  ", N, "  (done, already analyzed)");
1878
         return;
1879
 
1880
      --  Return if type = Any_Type (previous error encountered)
1881
 
1882
      elsif Etype (N) = Any_Type then
1883
         Debug_A_Exit ("resolving  ", N, "  (done, Etype = Any_Type)");
1884
         return;
1885
      end if;
1886
 
1887
      Check_Parameterless_Call (N);
1888
 
1889
      --  If not overloaded, then we know the type, and all that needs doing
1890
      --  is to check that this type is compatible with the context.
1891
 
1892
      if not Is_Overloaded (N) then
1893
         Found := Covers (Typ, Etype (N));
1894
         Expr_Type := Etype (N);
1895
 
1896
      --  In the overloaded case, we must select the interpretation that
1897
      --  is compatible with the context (i.e. the type passed to Resolve)
1898
 
1899
      else
1900
         --  Loop through possible interpretations
1901
 
1902
         Get_First_Interp (N, I, It);
1903
         Interp_Loop : while Present (It.Typ) loop
1904
 
1905
            --  We are only interested in interpretations that are compatible
1906
            --  with the expected type, any other interpretations are ignored.
1907
 
1908
            if not Covers (Typ, It.Typ) then
1909
               if Debug_Flag_V then
1910
                  Write_Str ("    interpretation incompatible with context");
1911
                  Write_Eol;
1912
               end if;
1913
 
1914
            else
1915
               --  Skip the current interpretation if it is disabled by an
1916
               --  abstract operator. This action is performed only when the
1917
               --  type against which we are resolving is the same as the
1918
               --  type of the interpretation.
1919
 
1920
               if Ada_Version >= Ada_05
1921
                 and then It.Typ = Typ
1922
                 and then Typ /= Universal_Integer
1923
                 and then Typ /= Universal_Real
1924
                 and then Present (It.Abstract_Op)
1925
               then
1926
                  goto Continue;
1927
               end if;
1928
 
1929
               --  First matching interpretation
1930
 
1931
               if not Found then
1932
                  Found := True;
1933
                  I1    := I;
1934
                  Seen  := It.Nam;
1935
                  Expr_Type := It.Typ;
1936
 
1937
               --  Matching interpretation that is not the first, maybe an
1938
               --  error, but there are some cases where preference rules are
1939
               --  used to choose between the two possibilities. These and
1940
               --  some more obscure cases are handled in Disambiguate.
1941
 
1942
               else
1943
                  --  If the current statement is part of a predefined library
1944
                  --  unit, then all interpretations which come from user level
1945
                  --  packages should not be considered.
1946
 
1947
                  if From_Lib
1948
                    and then not Comes_From_Predefined_Lib_Unit (It.Nam)
1949
                  then
1950
                     goto Continue;
1951
                  end if;
1952
 
1953
                  Error_Msg_Sloc := Sloc (Seen);
1954
                  It1 := Disambiguate (N, I1, I, Typ);
1955
 
1956
                  --  Disambiguation has succeeded. Skip the remaining
1957
                  --  interpretations.
1958
 
1959
                  if It1 /= No_Interp then
1960
                     Seen := It1.Nam;
1961
                     Expr_Type := It1.Typ;
1962
 
1963
                     while Present (It.Typ) loop
1964
                        Get_Next_Interp (I, It);
1965
                     end loop;
1966
 
1967
                  else
1968
                     --  Before we issue an ambiguity complaint, check for
1969
                     --  the case of a subprogram call where at least one
1970
                     --  of the arguments is Any_Type, and if so, suppress
1971
                     --  the message, since it is a cascaded error.
1972
 
1973
                     if Nkind_In (N, N_Function_Call,
1974
                                     N_Procedure_Call_Statement)
1975
                     then
1976
                        declare
1977
                           A : Node_Id;
1978
                           E : Node_Id;
1979
 
1980
                        begin
1981
                           A := First_Actual (N);
1982
                           while Present (A) loop
1983
                              E := A;
1984
 
1985
                              if Nkind (E) = N_Parameter_Association then
1986
                                 E := Explicit_Actual_Parameter (E);
1987
                              end if;
1988
 
1989
                              if Etype (E) = Any_Type then
1990
                                 if Debug_Flag_V then
1991
                                    Write_Str ("Any_Type in call");
1992
                                    Write_Eol;
1993
                                 end if;
1994
 
1995
                                 exit Interp_Loop;
1996
                              end if;
1997
 
1998
                              Next_Actual (A);
1999
                           end loop;
2000
                        end;
2001
 
2002
                     elsif Nkind (N) in N_Binary_Op
2003
                       and then (Etype (Left_Opnd (N)) = Any_Type
2004
                                  or else Etype (Right_Opnd (N)) = Any_Type)
2005
                     then
2006
                        exit Interp_Loop;
2007
 
2008
                     elsif Nkind (N) in  N_Unary_Op
2009
                       and then Etype (Right_Opnd (N)) = Any_Type
2010
                     then
2011
                        exit Interp_Loop;
2012
                     end if;
2013
 
2014
                     --  Not that special case, so issue message using the
2015
                     --  flag Ambiguous to control printing of the header
2016
                     --  message only at the start of an ambiguous set.
2017
 
2018
                     if not Ambiguous then
2019
                        if Nkind (N) = N_Function_Call
2020
                          and then Nkind (Name (N)) = N_Explicit_Dereference
2021
                        then
2022
                           Error_Msg_N
2023
                             ("ambiguous expression "
2024
                               & "(cannot resolve indirect call)!", N);
2025
                        else
2026
                           Error_Msg_NE -- CODEFIX
2027
                             ("ambiguous expression (cannot resolve&)!",
2028
                              N, It.Nam);
2029
                        end if;
2030
 
2031
                        Ambiguous := True;
2032
 
2033
                        if Nkind (Parent (Seen)) = N_Full_Type_Declaration then
2034
                           Error_Msg_N
2035
                             ("\\possible interpretation (inherited)#!", N);
2036
                        else
2037
                           Error_Msg_N -- CODEFIX
2038
                             ("\\possible interpretation#!", N);
2039
                        end if;
2040
                     end if;
2041
 
2042
                     Error_Msg_Sloc := Sloc (It.Nam);
2043
 
2044
                     --  By default, the error message refers to the candidate
2045
                     --  interpretation. But if it is a predefined operator, it
2046
                     --  is implicitly declared at the declaration of the type
2047
                     --  of the operand. Recover the sloc of that declaration
2048
                     --  for the error message.
2049
 
2050
                     if Nkind (N) in N_Op
2051
                       and then Scope (It.Nam) = Standard_Standard
2052
                       and then not Is_Overloaded (Right_Opnd (N))
2053
                       and then Scope (Base_Type (Etype (Right_Opnd (N)))) /=
2054
                                                             Standard_Standard
2055
                     then
2056
                        Err_Type := First_Subtype (Etype (Right_Opnd (N)));
2057
 
2058
                        if Comes_From_Source (Err_Type)
2059
                          and then Present (Parent (Err_Type))
2060
                        then
2061
                           Error_Msg_Sloc := Sloc (Parent (Err_Type));
2062
                        end if;
2063
 
2064
                     elsif Nkind (N) in N_Binary_Op
2065
                       and then Scope (It.Nam) = Standard_Standard
2066
                       and then not Is_Overloaded (Left_Opnd (N))
2067
                       and then Scope (Base_Type (Etype (Left_Opnd (N)))) /=
2068
                                                             Standard_Standard
2069
                     then
2070
                        Err_Type := First_Subtype (Etype (Left_Opnd (N)));
2071
 
2072
                        if Comes_From_Source (Err_Type)
2073
                          and then Present (Parent (Err_Type))
2074
                        then
2075
                           Error_Msg_Sloc := Sloc (Parent (Err_Type));
2076
                        end if;
2077
 
2078
                     --  If this is an indirect call, use the subprogram_type
2079
                     --  in the message, to have a meaningful location.
2080
                     --  Indicate as well if this is an inherited operation,
2081
                     --  created by a type declaration.
2082
 
2083
                     elsif Nkind (N) = N_Function_Call
2084
                       and then Nkind (Name (N)) = N_Explicit_Dereference
2085
                       and then Is_Type (It.Nam)
2086
                     then
2087
                        Err_Type := It.Nam;
2088
                        Error_Msg_Sloc :=
2089
                          Sloc (Associated_Node_For_Itype (Err_Type));
2090
                     else
2091
                        Err_Type := Empty;
2092
                     end if;
2093
 
2094
                     if Nkind (N) in N_Op
2095
                       and then Scope (It.Nam) = Standard_Standard
2096
                       and then Present (Err_Type)
2097
                     then
2098
                        --  Special-case the message for universal_fixed
2099
                        --  operators, which are not declared with the type
2100
                        --  of the operand, but appear forever in Standard.
2101
 
2102
                        if  It.Typ = Universal_Fixed
2103
                          and then Scope (It.Nam) = Standard_Standard
2104
                        then
2105
                           Error_Msg_N
2106
                             ("\\possible interpretation as " &
2107
                                "universal_fixed operation " &
2108
                                  "(RM 4.5.5 (19))", N);
2109
                        else
2110
                           Error_Msg_N
2111
                             ("\\possible interpretation (predefined)#!", N);
2112
                        end if;
2113
 
2114
                     elsif
2115
                       Nkind (Parent (It.Nam)) = N_Full_Type_Declaration
2116
                     then
2117
                        Error_Msg_N
2118
                          ("\\possible interpretation (inherited)#!", N);
2119
                     else
2120
                        Error_Msg_N -- CODEFIX
2121
                          ("\\possible interpretation#!", N);
2122
                     end if;
2123
 
2124
                  end if;
2125
               end if;
2126
 
2127
               --  We have a matching interpretation, Expr_Type is the type
2128
               --  from this interpretation, and Seen is the entity.
2129
 
2130
               --  For an operator, just set the entity name. The type will be
2131
               --  set by the specific operator resolution routine.
2132
 
2133
               if Nkind (N) in N_Op then
2134
                  Set_Entity (N, Seen);
2135
                  Generate_Reference (Seen, N);
2136
 
2137
               elsif Nkind (N) = N_Character_Literal then
2138
                  Set_Etype (N, Expr_Type);
2139
 
2140
               elsif Nkind (N) = N_Conditional_Expression then
2141
                  Set_Etype (N, Expr_Type);
2142
 
2143
               --  For an explicit dereference, attribute reference, range,
2144
               --  short-circuit form (which is not an operator node), or call
2145
               --  with a name that is an explicit dereference, there is
2146
               --  nothing to be done at this point.
2147
 
2148
               elsif Nkind_In (N, N_Explicit_Dereference,
2149
                                  N_Attribute_Reference,
2150
                                  N_And_Then,
2151
                                  N_Indexed_Component,
2152
                                  N_Or_Else,
2153
                                  N_Range,
2154
                                  N_Selected_Component,
2155
                                  N_Slice)
2156
                 or else Nkind (Name (N)) = N_Explicit_Dereference
2157
               then
2158
                  null;
2159
 
2160
               --  For procedure or function calls, set the type of the name,
2161
               --  and also the entity pointer for the prefix
2162
 
2163
               elsif Nkind_In (N, N_Procedure_Call_Statement, N_Function_Call)
2164
                 and then (Is_Entity_Name (Name (N))
2165
                            or else Nkind (Name (N)) = N_Operator_Symbol)
2166
               then
2167
                  Set_Etype  (Name (N), Expr_Type);
2168
                  Set_Entity (Name (N), Seen);
2169
                  Generate_Reference (Seen, Name (N));
2170
 
2171
               elsif Nkind (N) = N_Function_Call
2172
                 and then Nkind (Name (N)) = N_Selected_Component
2173
               then
2174
                  Set_Etype (Name (N), Expr_Type);
2175
                  Set_Entity (Selector_Name (Name (N)), Seen);
2176
                  Generate_Reference (Seen, Selector_Name (Name (N)));
2177
 
2178
               --  For all other cases, just set the type of the Name
2179
 
2180
               else
2181
                  Set_Etype (Name (N), Expr_Type);
2182
               end if;
2183
 
2184
            end if;
2185
 
2186
            <<Continue>>
2187
 
2188
            --  Move to next interpretation
2189
 
2190
            exit Interp_Loop when No (It.Typ);
2191
 
2192
            Get_Next_Interp (I, It);
2193
         end loop Interp_Loop;
2194
      end if;
2195
 
2196
      --  At this stage Found indicates whether or not an acceptable
2197
      --  interpretation exists. If not, then we have an error, except
2198
      --  that if the context is Any_Type as a result of some other error,
2199
      --  then we suppress the error report.
2200
 
2201
      if not Found then
2202
         if Typ /= Any_Type then
2203
 
2204
            --  If type we are looking for is Void, then this is the procedure
2205
            --  call case, and the error is simply that what we gave is not a
2206
            --  procedure name (we think of procedure calls as expressions with
2207
            --  types internally, but the user doesn't think of them this way!)
2208
 
2209
            if Typ = Standard_Void_Type then
2210
 
2211
               --  Special case message if function used as a procedure
2212
 
2213
               if Nkind (N) = N_Procedure_Call_Statement
2214
                 and then Is_Entity_Name (Name (N))
2215
                 and then Ekind (Entity (Name (N))) = E_Function
2216
               then
2217
                  Error_Msg_NE
2218
                    ("cannot use function & in a procedure call",
2219
                     Name (N), Entity (Name (N)));
2220
 
2221
               --  Otherwise give general message (not clear what cases this
2222
               --  covers, but no harm in providing for them!)
2223
 
2224
               else
2225
                  Error_Msg_N ("expect procedure name in procedure call", N);
2226
               end if;
2227
 
2228
               Found := True;
2229
 
2230
            --  Otherwise we do have a subexpression with the wrong type
2231
 
2232
            --  Check for the case of an allocator which uses an access type
2233
            --  instead of the designated type. This is a common error and we
2234
            --  specialize the message, posting an error on the operand of the
2235
            --  allocator, complaining that we expected the designated type of
2236
            --  the allocator.
2237
 
2238
            elsif Nkind (N) = N_Allocator
2239
              and then Ekind (Typ) in Access_Kind
2240
              and then Ekind (Etype (N)) in Access_Kind
2241
              and then Designated_Type (Etype (N)) = Typ
2242
            then
2243
               Wrong_Type (Expression (N), Designated_Type (Typ));
2244
               Found := True;
2245
 
2246
            --  Check for view mismatch on Null in instances, for which the
2247
            --  view-swapping mechanism has no identifier.
2248
 
2249
            elsif (In_Instance or else In_Inlined_Body)
2250
              and then (Nkind (N) = N_Null)
2251
              and then Is_Private_Type (Typ)
2252
              and then Is_Access_Type (Full_View (Typ))
2253
            then
2254
               Resolve (N, Full_View (Typ));
2255
               Set_Etype (N, Typ);
2256
               return;
2257
 
2258
            --  Check for an aggregate. Sometimes we can get bogus aggregates
2259
            --  from misuse of parentheses, and we are about to complain about
2260
            --  the aggregate without even looking inside it.
2261
 
2262
            --  Instead, if we have an aggregate of type Any_Composite, then
2263
            --  analyze and resolve the component fields, and then only issue
2264
            --  another message if we get no errors doing this (otherwise
2265
            --  assume that the errors in the aggregate caused the problem).
2266
 
2267
            elsif Nkind (N) = N_Aggregate
2268
              and then Etype (N) = Any_Composite
2269
            then
2270
               --  Disable expansion in any case. If there is a type mismatch
2271
               --  it may be fatal to try to expand the aggregate. The flag
2272
               --  would otherwise be set to false when the error is posted.
2273
 
2274
               Expander_Active := False;
2275
 
2276
               declare
2277
                  procedure Check_Aggr (Aggr : Node_Id);
2278
                  --  Check one aggregate, and set Found to True if we have a
2279
                  --  definite error in any of its elements
2280
 
2281
                  procedure Check_Elmt (Aelmt : Node_Id);
2282
                  --  Check one element of aggregate and set Found to True if
2283
                  --  we definitely have an error in the element.
2284
 
2285
                  ----------------
2286
                  -- Check_Aggr --
2287
                  ----------------
2288
 
2289
                  procedure Check_Aggr (Aggr : Node_Id) is
2290
                     Elmt : Node_Id;
2291
 
2292
                  begin
2293
                     if Present (Expressions (Aggr)) then
2294
                        Elmt := First (Expressions (Aggr));
2295
                        while Present (Elmt) loop
2296
                           Check_Elmt (Elmt);
2297
                           Next (Elmt);
2298
                        end loop;
2299
                     end if;
2300
 
2301
                     if Present (Component_Associations (Aggr)) then
2302
                        Elmt := First (Component_Associations (Aggr));
2303
                        while Present (Elmt) loop
2304
 
2305
                           --  If this is a default-initialized component, then
2306
                           --  there is nothing to check. The box will be
2307
                           --  replaced by the appropriate call during late
2308
                           --  expansion.
2309
 
2310
                           if not Box_Present (Elmt) then
2311
                              Check_Elmt (Expression (Elmt));
2312
                           end if;
2313
 
2314
                           Next (Elmt);
2315
                        end loop;
2316
                     end if;
2317
                  end Check_Aggr;
2318
 
2319
                  ----------------
2320
                  -- Check_Elmt --
2321
                  ----------------
2322
 
2323
                  procedure Check_Elmt (Aelmt : Node_Id) is
2324
                  begin
2325
                     --  If we have a nested aggregate, go inside it (to
2326
                     --  attempt a naked analyze-resolve of the aggregate
2327
                     --  can cause undesirable cascaded errors). Do not
2328
                     --  resolve expression if it needs a type from context,
2329
                     --  as for integer * fixed expression.
2330
 
2331
                     if Nkind (Aelmt) = N_Aggregate then
2332
                        Check_Aggr (Aelmt);
2333
 
2334
                     else
2335
                        Analyze (Aelmt);
2336
 
2337
                        if not Is_Overloaded (Aelmt)
2338
                          and then Etype (Aelmt) /= Any_Fixed
2339
                        then
2340
                           Resolve (Aelmt);
2341
                        end if;
2342
 
2343
                        if Etype (Aelmt) = Any_Type then
2344
                           Found := True;
2345
                        end if;
2346
                     end if;
2347
                  end Check_Elmt;
2348
 
2349
               begin
2350
                  Check_Aggr (N);
2351
               end;
2352
            end if;
2353
 
2354
            --  If an error message was issued already, Found got reset
2355
            --  to True, so if it is still False, issue the standard
2356
            --  Wrong_Type message.
2357
 
2358
            if not Found then
2359
               if Is_Overloaded (N)
2360
                 and then Nkind (N) = N_Function_Call
2361
               then
2362
                  declare
2363
                     Subp_Name : Node_Id;
2364
                  begin
2365
                     if Is_Entity_Name (Name (N)) then
2366
                        Subp_Name := Name (N);
2367
 
2368
                     elsif Nkind (Name (N)) = N_Selected_Component then
2369
 
2370
                        --  Protected operation: retrieve operation name
2371
 
2372
                        Subp_Name := Selector_Name (Name (N));
2373
                     else
2374
                        raise Program_Error;
2375
                     end if;
2376
 
2377
                     Error_Msg_Node_2 := Typ;
2378
                     Error_Msg_NE ("no visible interpretation of&" &
2379
                       " matches expected type&", N, Subp_Name);
2380
                  end;
2381
 
2382
                  if All_Errors_Mode then
2383
                     declare
2384
                        Index : Interp_Index;
2385
                        It    : Interp;
2386
 
2387
                     begin
2388
                        Error_Msg_N ("\\possible interpretations:", N);
2389
 
2390
                        Get_First_Interp (Name (N), Index, It);
2391
                        while Present (It.Nam) loop
2392
                           Error_Msg_Sloc := Sloc (It.Nam);
2393
                           Error_Msg_Node_2 := It.Nam;
2394
                           Error_Msg_NE
2395
                             ("\\  type& for & declared#", N, It.Typ);
2396
                           Get_Next_Interp (Index, It);
2397
                        end loop;
2398
                     end;
2399
 
2400
                  else
2401
                     Error_Msg_N ("\use -gnatf for details", N);
2402
                  end if;
2403
               else
2404
                  Wrong_Type (N, Typ);
2405
               end if;
2406
            end if;
2407
         end if;
2408
 
2409
         Resolution_Failed;
2410
         return;
2411
 
2412
      --  Test if we have more than one interpretation for the context
2413
 
2414
      elsif Ambiguous then
2415
         Resolution_Failed;
2416
         return;
2417
 
2418
      --  Here we have an acceptable interpretation for the context
2419
 
2420
      else
2421
         --  Propagate type information and normalize tree for various
2422
         --  predefined operations. If the context only imposes a class of
2423
         --  types, rather than a specific type, propagate the actual type
2424
         --  downward.
2425
 
2426
         if Typ = Any_Integer
2427
           or else Typ = Any_Boolean
2428
           or else Typ = Any_Modular
2429
           or else Typ = Any_Real
2430
           or else Typ = Any_Discrete
2431
         then
2432
            Ctx_Type := Expr_Type;
2433
 
2434
            --  Any_Fixed is legal in a real context only if a specific
2435
            --  fixed point type is imposed. If Norman Cohen can be
2436
            --  confused by this, it deserves a separate message.
2437
 
2438
            if Typ = Any_Real
2439
              and then Expr_Type = Any_Fixed
2440
            then
2441
               Error_Msg_N ("illegal context for mixed mode operation", N);
2442
               Set_Etype (N, Universal_Real);
2443
               Ctx_Type := Universal_Real;
2444
            end if;
2445
         end if;
2446
 
2447
         --  A user-defined operator is transformed into a function call at
2448
         --  this point, so that further processing knows that operators are
2449
         --  really operators (i.e. are predefined operators). User-defined
2450
         --  operators that are intrinsic are just renamings of the predefined
2451
         --  ones, and need not be turned into calls either, but if they rename
2452
         --  a different operator, we must transform the node accordingly.
2453
         --  Instantiations of Unchecked_Conversion are intrinsic but are
2454
         --  treated as functions, even if given an operator designator.
2455
 
2456
         if Nkind (N) in N_Op
2457
           and then Present (Entity (N))
2458
           and then Ekind (Entity (N)) /= E_Operator
2459
         then
2460
 
2461
            if not Is_Predefined_Op (Entity (N)) then
2462
               Rewrite_Operator_As_Call (N, Entity (N));
2463
 
2464
            elsif Present (Alias (Entity (N)))
2465
              and then
2466
                Nkind (Parent (Parent (Entity (N)))) =
2467
                                    N_Subprogram_Renaming_Declaration
2468
            then
2469
               Rewrite_Renamed_Operator (N, Alias (Entity (N)), Typ);
2470
 
2471
               --  If the node is rewritten, it will be fully resolved in
2472
               --  Rewrite_Renamed_Operator.
2473
 
2474
               if Analyzed (N) then
2475
                  return;
2476
               end if;
2477
            end if;
2478
         end if;
2479
 
2480
         case N_Subexpr'(Nkind (N)) is
2481
 
2482
            when N_Aggregate => Resolve_Aggregate                (N, Ctx_Type);
2483
 
2484
            when N_Allocator => Resolve_Allocator                (N, Ctx_Type);
2485
 
2486
            when N_Short_Circuit
2487
                             => Resolve_Short_Circuit            (N, Ctx_Type);
2488
 
2489
            when N_Attribute_Reference
2490
                             => Resolve_Attribute                (N, Ctx_Type);
2491
 
2492
            when N_Character_Literal
2493
                             => Resolve_Character_Literal        (N, Ctx_Type);
2494
 
2495
            when N_Conditional_Expression
2496
                             => Resolve_Conditional_Expression   (N, Ctx_Type);
2497
 
2498
            when N_Expanded_Name
2499
                             => Resolve_Entity_Name              (N, Ctx_Type);
2500
 
2501
            when N_Extension_Aggregate
2502
                             => Resolve_Extension_Aggregate      (N, Ctx_Type);
2503
 
2504
            when N_Explicit_Dereference
2505
                             => Resolve_Explicit_Dereference     (N, Ctx_Type);
2506
 
2507
            when N_Function_Call
2508
                             => Resolve_Call                     (N, Ctx_Type);
2509
 
2510
            when N_Identifier
2511
                             => Resolve_Entity_Name              (N, Ctx_Type);
2512
 
2513
            when N_Indexed_Component
2514
                             => Resolve_Indexed_Component        (N, Ctx_Type);
2515
 
2516
            when N_Integer_Literal
2517
                             => Resolve_Integer_Literal          (N, Ctx_Type);
2518
 
2519
            when N_Membership_Test
2520
                             => Resolve_Membership_Op            (N, Ctx_Type);
2521
 
2522
            when N_Null      => Resolve_Null                     (N, Ctx_Type);
2523
 
2524
            when N_Op_And | N_Op_Or | N_Op_Xor
2525
                             => Resolve_Logical_Op               (N, Ctx_Type);
2526
 
2527
            when N_Op_Eq | N_Op_Ne
2528
                             => Resolve_Equality_Op              (N, Ctx_Type);
2529
 
2530
            when N_Op_Lt | N_Op_Le | N_Op_Gt | N_Op_Ge
2531
                             => Resolve_Comparison_Op            (N, Ctx_Type);
2532
 
2533
            when N_Op_Not    => Resolve_Op_Not                   (N, Ctx_Type);
2534
 
2535
            when N_Op_Add    | N_Op_Subtract | N_Op_Multiply |
2536
                 N_Op_Divide | N_Op_Mod      | N_Op_Rem
2537
 
2538
                             => Resolve_Arithmetic_Op            (N, Ctx_Type);
2539
 
2540
            when N_Op_Concat => Resolve_Op_Concat                (N, Ctx_Type);
2541
 
2542
            when N_Op_Expon  => Resolve_Op_Expon                 (N, Ctx_Type);
2543
 
2544
            when N_Op_Plus | N_Op_Minus  | N_Op_Abs
2545
                             => Resolve_Unary_Op                 (N, Ctx_Type);
2546
 
2547
            when N_Op_Shift  => Resolve_Shift                    (N, Ctx_Type);
2548
 
2549
            when N_Procedure_Call_Statement
2550
                             => Resolve_Call                     (N, Ctx_Type);
2551
 
2552
            when N_Operator_Symbol
2553
                             => Resolve_Operator_Symbol          (N, Ctx_Type);
2554
 
2555
            when N_Qualified_Expression
2556
                             => Resolve_Qualified_Expression     (N, Ctx_Type);
2557
 
2558
            when N_Raise_xxx_Error
2559
                             => Set_Etype (N, Ctx_Type);
2560
 
2561
            when N_Range     => Resolve_Range                    (N, Ctx_Type);
2562
 
2563
            when N_Real_Literal
2564
                             => Resolve_Real_Literal             (N, Ctx_Type);
2565
 
2566
            when N_Reference => Resolve_Reference                (N, Ctx_Type);
2567
 
2568
            when N_Selected_Component
2569
                             => Resolve_Selected_Component       (N, Ctx_Type);
2570
 
2571
            when N_Slice     => Resolve_Slice                    (N, Ctx_Type);
2572
 
2573
            when N_String_Literal
2574
                             => Resolve_String_Literal           (N, Ctx_Type);
2575
 
2576
            when N_Subprogram_Info
2577
                             => Resolve_Subprogram_Info          (N, Ctx_Type);
2578
 
2579
            when N_Type_Conversion
2580
                             => Resolve_Type_Conversion          (N, Ctx_Type);
2581
 
2582
            when N_Unchecked_Expression =>
2583
               Resolve_Unchecked_Expression                      (N, Ctx_Type);
2584
 
2585
            when N_Unchecked_Type_Conversion =>
2586
               Resolve_Unchecked_Type_Conversion                 (N, Ctx_Type);
2587
 
2588
         end case;
2589
 
2590
         --  If the subexpression was replaced by a non-subexpression, then
2591
         --  all we do is to expand it. The only legitimate case we know of
2592
         --  is converting procedure call statement to entry call statements,
2593
         --  but there may be others, so we are making this test general.
2594
 
2595
         if Nkind (N) not in N_Subexpr then
2596
            Debug_A_Exit ("resolving  ", N, "  (done)");
2597
            Expand (N);
2598
            return;
2599
         end if;
2600
 
2601
         --  The expression is definitely NOT overloaded at this point, so
2602
         --  we reset the Is_Overloaded flag to avoid any confusion when
2603
         --  reanalyzing the node.
2604
 
2605
         Set_Is_Overloaded (N, False);
2606
 
2607
         --  Freeze expression type, entity if it is a name, and designated
2608
         --  type if it is an allocator (RM 13.14(10,11,13)).
2609
 
2610
         --  Now that the resolution of the type of the node is complete,
2611
         --  and we did not detect an error, we can expand this node. We
2612
         --  skip the expand call if we are in a default expression, see
2613
         --  section "Handling of Default Expressions" in Sem spec.
2614
 
2615
         Debug_A_Exit ("resolving  ", N, "  (done)");
2616
 
2617
         --  We unconditionally freeze the expression, even if we are in
2618
         --  default expression mode (the Freeze_Expression routine tests
2619
         --  this flag and only freezes static types if it is set).
2620
 
2621
         Freeze_Expression (N);
2622
 
2623
         --  Now we can do the expansion
2624
 
2625
         Expand (N);
2626
      end if;
2627
   end Resolve;
2628
 
2629
   -------------
2630
   -- Resolve --
2631
   -------------
2632
 
2633
   --  Version with check(s) suppressed
2634
 
2635
   procedure Resolve (N : Node_Id; Typ : Entity_Id; Suppress : Check_Id) is
2636
   begin
2637
      if Suppress = All_Checks then
2638
         declare
2639
            Svg : constant Suppress_Array := Scope_Suppress;
2640
         begin
2641
            Scope_Suppress := (others => True);
2642
            Resolve (N, Typ);
2643
            Scope_Suppress := Svg;
2644
         end;
2645
 
2646
      else
2647
         declare
2648
            Svg : constant Boolean := Scope_Suppress (Suppress);
2649
         begin
2650
            Scope_Suppress (Suppress) := True;
2651
            Resolve (N, Typ);
2652
            Scope_Suppress (Suppress) := Svg;
2653
         end;
2654
      end if;
2655
   end Resolve;
2656
 
2657
   -------------
2658
   -- Resolve --
2659
   -------------
2660
 
2661
   --  Version with implicit type
2662
 
2663
   procedure Resolve (N : Node_Id) is
2664
   begin
2665
      Resolve (N, Etype (N));
2666
   end Resolve;
2667
 
2668
   ---------------------
2669
   -- Resolve_Actuals --
2670
   ---------------------
2671
 
2672
   procedure Resolve_Actuals (N : Node_Id; Nam : Entity_Id) is
2673
      Loc    : constant Source_Ptr := Sloc (N);
2674
      A      : Node_Id;
2675
      F      : Entity_Id;
2676
      A_Typ  : Entity_Id;
2677
      F_Typ  : Entity_Id;
2678
      Prev   : Node_Id := Empty;
2679
      Orig_A : Node_Id;
2680
 
2681
      procedure Check_Argument_Order;
2682
      --  Performs a check for the case where the actuals are all simple
2683
      --  identifiers that correspond to the formal names, but in the wrong
2684
      --  order, which is considered suspicious and cause for a warning.
2685
 
2686
      procedure Check_Prefixed_Call;
2687
      --  If the original node is an overloaded call in prefix notation,
2688
      --  insert an 'Access or a dereference as needed over the first actual.
2689
      --  Try_Object_Operation has already verified that there is a valid
2690
      --  interpretation, but the form of the actual can only be determined
2691
      --  once the primitive operation is identified.
2692
 
2693
      procedure Insert_Default;
2694
      --  If the actual is missing in a call, insert in the actuals list
2695
      --  an instance of the default expression. The insertion is always
2696
      --  a named association.
2697
 
2698
      function Same_Ancestor (T1, T2 : Entity_Id) return Boolean;
2699
      --  Check whether T1 and T2, or their full views, are derived from a
2700
      --  common type. Used to enforce the restrictions on array conversions
2701
      --  of AI95-00246.
2702
 
2703
      function Static_Concatenation (N : Node_Id) return Boolean;
2704
      --  Predicate to determine whether an actual that is a concatenation
2705
      --  will be evaluated statically and does not need a transient scope.
2706
      --  This must be determined before the actual is resolved and expanded
2707
      --  because if needed the transient scope must be introduced earlier.
2708
 
2709
      --------------------------
2710
      -- Check_Argument_Order --
2711
      --------------------------
2712
 
2713
      procedure Check_Argument_Order is
2714
      begin
2715
         --  Nothing to do if no parameters, or original node is neither a
2716
         --  function call nor a procedure call statement (happens in the
2717
         --  operator-transformed-to-function call case), or the call does
2718
         --  not come from source, or this warning is off.
2719
 
2720
         if not Warn_On_Parameter_Order
2721
           or else
2722
             No (Parameter_Associations (N))
2723
           or else
2724
             not Nkind_In (Original_Node (N), N_Procedure_Call_Statement,
2725
                                              N_Function_Call)
2726
           or else
2727
             not Comes_From_Source (N)
2728
         then
2729
            return;
2730
         end if;
2731
 
2732
         declare
2733
            Nargs : constant Nat := List_Length (Parameter_Associations (N));
2734
 
2735
         begin
2736
            --  Nothing to do if only one parameter
2737
 
2738
            if Nargs < 2 then
2739
               return;
2740
            end if;
2741
 
2742
            --  Here if at least two arguments
2743
 
2744
            declare
2745
               Actuals : array (1 .. Nargs) of Node_Id;
2746
               Actual  : Node_Id;
2747
               Formal  : Node_Id;
2748
 
2749
               Wrong_Order : Boolean := False;
2750
               --  Set True if an out of order case is found
2751
 
2752
            begin
2753
               --  Collect identifier names of actuals, fail if any actual is
2754
               --  not a simple identifier, and record max length of name.
2755
 
2756
               Actual := First (Parameter_Associations (N));
2757
               for J in Actuals'Range loop
2758
                  if Nkind (Actual) /= N_Identifier then
2759
                     return;
2760
                  else
2761
                     Actuals (J) := Actual;
2762
                     Next (Actual);
2763
                  end if;
2764
               end loop;
2765
 
2766
               --  If we got this far, all actuals are identifiers and the list
2767
               --  of their names is stored in the Actuals array.
2768
 
2769
               Formal := First_Formal (Nam);
2770
               for J in Actuals'Range loop
2771
 
2772
                  --  If we ran out of formals, that's odd, probably an error
2773
                  --  which will be detected elsewhere, but abandon the search.
2774
 
2775
                  if No (Formal) then
2776
                     return;
2777
                  end if;
2778
 
2779
                  --  If name matches and is in order OK
2780
 
2781
                  if Chars (Formal) = Chars (Actuals (J)) then
2782
                     null;
2783
 
2784
                  else
2785
                     --  If no match, see if it is elsewhere in list and if so
2786
                     --  flag potential wrong order if type is compatible.
2787
 
2788
                     for K in Actuals'Range loop
2789
                        if Chars (Formal) = Chars (Actuals (K))
2790
                          and then
2791
                            Has_Compatible_Type (Actuals (K), Etype (Formal))
2792
                        then
2793
                           Wrong_Order := True;
2794
                           goto Continue;
2795
                        end if;
2796
                     end loop;
2797
 
2798
                     --  No match
2799
 
2800
                     return;
2801
                  end if;
2802
 
2803
                  <<Continue>> Next_Formal (Formal);
2804
               end loop;
2805
 
2806
               --  If Formals left over, also probably an error, skip warning
2807
 
2808
               if Present (Formal) then
2809
                  return;
2810
               end if;
2811
 
2812
               --  Here we give the warning if something was out of order
2813
 
2814
               if Wrong_Order then
2815
                  Error_Msg_N
2816
                    ("actuals for this call may be in wrong order?", N);
2817
               end if;
2818
            end;
2819
         end;
2820
      end Check_Argument_Order;
2821
 
2822
      -------------------------
2823
      -- Check_Prefixed_Call --
2824
      -------------------------
2825
 
2826
      procedure Check_Prefixed_Call is
2827
         Act    : constant Node_Id   := First_Actual (N);
2828
         A_Type : constant Entity_Id := Etype (Act);
2829
         F_Type : constant Entity_Id := Etype (First_Formal (Nam));
2830
         Orig   : constant Node_Id := Original_Node (N);
2831
         New_A  : Node_Id;
2832
 
2833
      begin
2834
         --  Check whether the call is a prefixed call, with or without
2835
         --  additional actuals.
2836
 
2837
         if Nkind (Orig) = N_Selected_Component
2838
           or else
2839
             (Nkind (Orig) = N_Indexed_Component
2840
               and then Nkind (Prefix (Orig)) = N_Selected_Component
2841
               and then Is_Entity_Name (Prefix (Prefix (Orig)))
2842
               and then Is_Entity_Name (Act)
2843
               and then Chars (Act) = Chars (Prefix (Prefix (Orig))))
2844
         then
2845
            if Is_Access_Type (A_Type)
2846
              and then not Is_Access_Type (F_Type)
2847
            then
2848
               --  Introduce dereference on object in prefix
2849
 
2850
               New_A :=
2851
                 Make_Explicit_Dereference (Sloc (Act),
2852
                   Prefix => Relocate_Node (Act));
2853
               Rewrite (Act, New_A);
2854
               Analyze (Act);
2855
 
2856
            elsif Is_Access_Type (F_Type)
2857
              and then not Is_Access_Type (A_Type)
2858
            then
2859
               --  Introduce an implicit 'Access in prefix
2860
 
2861
               if not Is_Aliased_View (Act) then
2862
                  Error_Msg_NE
2863
                    ("object in prefixed call to& must be aliased"
2864
                         & " (RM-2005 4.3.1 (13))",
2865
                    Prefix (Act), Nam);
2866
               end if;
2867
 
2868
               Rewrite (Act,
2869
                 Make_Attribute_Reference (Loc,
2870
                   Attribute_Name => Name_Access,
2871
                   Prefix         => Relocate_Node (Act)));
2872
            end if;
2873
 
2874
            Analyze (Act);
2875
         end if;
2876
      end Check_Prefixed_Call;
2877
 
2878
      --------------------
2879
      -- Insert_Default --
2880
      --------------------
2881
 
2882
      procedure Insert_Default is
2883
         Actval : Node_Id;
2884
         Assoc  : Node_Id;
2885
 
2886
      begin
2887
         --  Missing argument in call, nothing to insert
2888
 
2889
         if No (Default_Value (F)) then
2890
            return;
2891
 
2892
         else
2893
            --  Note that we do a full New_Copy_Tree, so that any associated
2894
            --  Itypes are properly copied. This may not be needed any more,
2895
            --  but it does no harm as a safety measure! Defaults of a generic
2896
            --  formal may be out of bounds of the corresponding actual (see
2897
            --  cc1311b) and an additional check may be required.
2898
 
2899
            Actval :=
2900
              New_Copy_Tree
2901
                (Default_Value (F),
2902
                 New_Scope => Current_Scope,
2903
                 New_Sloc  => Loc);
2904
 
2905
            if Is_Concurrent_Type (Scope (Nam))
2906
              and then Has_Discriminants (Scope (Nam))
2907
            then
2908
               Replace_Actual_Discriminants (N, Actval);
2909
            end if;
2910
 
2911
            if Is_Overloadable (Nam)
2912
              and then Present (Alias (Nam))
2913
            then
2914
               if Base_Type (Etype (F)) /= Base_Type (Etype (Actval))
2915
                 and then not Is_Tagged_Type (Etype (F))
2916
               then
2917
                  --  If default is a real literal, do not introduce a
2918
                  --  conversion whose effect may depend on the run-time
2919
                  --  size of universal real.
2920
 
2921
                  if Nkind (Actval) = N_Real_Literal then
2922
                     Set_Etype (Actval, Base_Type (Etype (F)));
2923
                  else
2924
                     Actval := Unchecked_Convert_To (Etype (F), Actval);
2925
                  end if;
2926
               end if;
2927
 
2928
               if Is_Scalar_Type (Etype (F)) then
2929
                  Enable_Range_Check (Actval);
2930
               end if;
2931
 
2932
               Set_Parent (Actval, N);
2933
 
2934
               --  Resolve aggregates with their base type, to avoid scope
2935
               --  anomalies: the subtype was first built in the subprogram
2936
               --  declaration, and the current call may be nested.
2937
 
2938
               if Nkind (Actval) = N_Aggregate then
2939
                  Analyze_And_Resolve (Actval, Etype (F));
2940
               else
2941
                  Analyze_And_Resolve (Actval, Etype (Actval));
2942
               end if;
2943
 
2944
            else
2945
               Set_Parent (Actval, N);
2946
 
2947
               --  See note above concerning aggregates
2948
 
2949
               if Nkind (Actval) = N_Aggregate
2950
                 and then Has_Discriminants (Etype (Actval))
2951
               then
2952
                  Analyze_And_Resolve (Actval, Base_Type (Etype (Actval)));
2953
 
2954
               --  Resolve entities with their own type, which may differ
2955
               --  from the type of a reference in a generic context (the
2956
               --  view swapping mechanism did not anticipate the re-analysis
2957
               --  of default values in calls).
2958
 
2959
               elsif Is_Entity_Name (Actval) then
2960
                  Analyze_And_Resolve (Actval, Etype (Entity (Actval)));
2961
 
2962
               else
2963
                  Analyze_And_Resolve (Actval, Etype (Actval));
2964
               end if;
2965
            end if;
2966
 
2967
            --  If default is a tag indeterminate function call, propagate
2968
            --  tag to obtain proper dispatching.
2969
 
2970
            if Is_Controlling_Formal (F)
2971
              and then Nkind (Default_Value (F)) = N_Function_Call
2972
            then
2973
               Set_Is_Controlling_Actual (Actval);
2974
            end if;
2975
 
2976
         end if;
2977
 
2978
         --  If the default expression raises constraint error, then just
2979
         --  silently replace it with an N_Raise_Constraint_Error node,
2980
         --  since we already gave the warning on the subprogram spec.
2981
 
2982
         if Raises_Constraint_Error (Actval) then
2983
            Rewrite (Actval,
2984
              Make_Raise_Constraint_Error (Loc,
2985
                Reason => CE_Range_Check_Failed));
2986
            Set_Raises_Constraint_Error (Actval);
2987
            Set_Etype (Actval, Etype (F));
2988
         end if;
2989
 
2990
         Assoc :=
2991
           Make_Parameter_Association (Loc,
2992
             Explicit_Actual_Parameter => Actval,
2993
             Selector_Name => Make_Identifier (Loc, Chars (F)));
2994
 
2995
         --  Case of insertion is first named actual
2996
 
2997
         if No (Prev) or else
2998
            Nkind (Parent (Prev)) /= N_Parameter_Association
2999
         then
3000
            Set_Next_Named_Actual (Assoc, First_Named_Actual (N));
3001
            Set_First_Named_Actual (N, Actval);
3002
 
3003
            if No (Prev) then
3004
               if No (Parameter_Associations (N)) then
3005
                  Set_Parameter_Associations (N, New_List (Assoc));
3006
               else
3007
                  Append (Assoc, Parameter_Associations (N));
3008
               end if;
3009
 
3010
            else
3011
               Insert_After (Prev, Assoc);
3012
            end if;
3013
 
3014
         --  Case of insertion is not first named actual
3015
 
3016
         else
3017
            Set_Next_Named_Actual
3018
              (Assoc, Next_Named_Actual (Parent (Prev)));
3019
            Set_Next_Named_Actual (Parent (Prev), Actval);
3020
            Append (Assoc, Parameter_Associations (N));
3021
         end if;
3022
 
3023
         Mark_Rewrite_Insertion (Assoc);
3024
         Mark_Rewrite_Insertion (Actval);
3025
 
3026
         Prev := Actval;
3027
      end Insert_Default;
3028
 
3029
      -------------------
3030
      -- Same_Ancestor --
3031
      -------------------
3032
 
3033
      function Same_Ancestor (T1, T2 : Entity_Id) return Boolean is
3034
         FT1 : Entity_Id := T1;
3035
         FT2 : Entity_Id := T2;
3036
 
3037
      begin
3038
         if Is_Private_Type (T1)
3039
           and then Present (Full_View (T1))
3040
         then
3041
            FT1 := Full_View (T1);
3042
         end if;
3043
 
3044
         if Is_Private_Type (T2)
3045
           and then Present (Full_View (T2))
3046
         then
3047
            FT2 := Full_View (T2);
3048
         end if;
3049
 
3050
         return Root_Type (Base_Type (FT1)) = Root_Type (Base_Type (FT2));
3051
      end Same_Ancestor;
3052
 
3053
      --------------------------
3054
      -- Static_Concatenation --
3055
      --------------------------
3056
 
3057
      function Static_Concatenation (N : Node_Id) return Boolean is
3058
      begin
3059
         case Nkind (N) is
3060
            when N_String_Literal =>
3061
               return True;
3062
 
3063
            when N_Op_Concat =>
3064
 
3065
               --  Concatenation is static when both operands are static
3066
               --  and the concatenation operator is a predefined one.
3067
 
3068
               return Scope (Entity (N)) = Standard_Standard
3069
                        and then
3070
                      Static_Concatenation (Left_Opnd (N))
3071
                        and then
3072
                      Static_Concatenation (Right_Opnd (N));
3073
 
3074
            when others =>
3075
               if Is_Entity_Name (N) then
3076
                  declare
3077
                     Ent : constant Entity_Id := Entity (N);
3078
                  begin
3079
                     return Ekind (Ent) = E_Constant
3080
                              and then Present (Constant_Value (Ent))
3081
                              and then
3082
                                Is_Static_Expression (Constant_Value (Ent));
3083
                  end;
3084
 
3085
               else
3086
                  return False;
3087
               end if;
3088
         end case;
3089
      end Static_Concatenation;
3090
 
3091
   --  Start of processing for Resolve_Actuals
3092
 
3093
   begin
3094
      Check_Argument_Order;
3095
 
3096
      if Present (First_Actual (N)) then
3097
         Check_Prefixed_Call;
3098
      end if;
3099
 
3100
      A := First_Actual (N);
3101
      F := First_Formal (Nam);
3102
      while Present (F) loop
3103
         if No (A) and then Needs_No_Actuals (Nam) then
3104
            null;
3105
 
3106
         --  If we have an error in any actual or formal, indicated by a type
3107
         --  of Any_Type, then abandon resolution attempt, and set result type
3108
         --  to Any_Type.
3109
 
3110
         elsif (Present (A) and then Etype (A) = Any_Type)
3111
           or else Etype (F) = Any_Type
3112
         then
3113
            Set_Etype (N, Any_Type);
3114
            return;
3115
         end if;
3116
 
3117
         --  Case where actual is present
3118
 
3119
         --  If the actual is an entity, generate a reference to it now. We
3120
         --  do this before the actual is resolved, because a formal of some
3121
         --  protected subprogram, or a task discriminant, will be rewritten
3122
         --  during expansion, and the reference to the source entity may
3123
         --  be lost.
3124
 
3125
         if Present (A)
3126
           and then Is_Entity_Name (A)
3127
           and then Comes_From_Source (N)
3128
         then
3129
            Orig_A := Entity (A);
3130
 
3131
            if Present (Orig_A) then
3132
               if Is_Formal (Orig_A)
3133
                 and then Ekind (F) /= E_In_Parameter
3134
               then
3135
                  Generate_Reference (Orig_A, A, 'm');
3136
               elsif not Is_Overloaded (A) then
3137
                  Generate_Reference (Orig_A, A);
3138
               end if;
3139
            end if;
3140
         end if;
3141
 
3142
         if Present (A)
3143
           and then (Nkind (Parent (A)) /= N_Parameter_Association
3144
                       or else
3145
                     Chars (Selector_Name (Parent (A))) = Chars (F))
3146
         then
3147
            --  If style checking mode on, check match of formal name
3148
 
3149
            if Style_Check then
3150
               if Nkind (Parent (A)) = N_Parameter_Association then
3151
                  Check_Identifier (Selector_Name (Parent (A)), F);
3152
               end if;
3153
            end if;
3154
 
3155
            --  If the formal is Out or In_Out, do not resolve and expand the
3156
            --  conversion, because it is subsequently expanded into explicit
3157
            --  temporaries and assignments. However, the object of the
3158
            --  conversion can be resolved. An exception is the case of tagged
3159
            --  type conversion with a class-wide actual. In that case we want
3160
            --  the tag check to occur and no temporary will be needed (no
3161
            --  representation change can occur) and the parameter is passed by
3162
            --  reference, so we go ahead and resolve the type conversion.
3163
            --  Another exception is the case of reference to component or
3164
            --  subcomponent of a bit-packed array, in which case we want to
3165
            --  defer expansion to the point the in and out assignments are
3166
            --  performed.
3167
 
3168
            if Ekind (F) /= E_In_Parameter
3169
              and then Nkind (A) = N_Type_Conversion
3170
              and then not Is_Class_Wide_Type (Etype (Expression (A)))
3171
            then
3172
               if Ekind (F) = E_In_Out_Parameter
3173
                 and then Is_Array_Type (Etype (F))
3174
               then
3175
                  if Has_Aliased_Components (Etype (Expression (A)))
3176
                    /= Has_Aliased_Components (Etype (F))
3177
                  then
3178
 
3179
                     --  In a view conversion, the conversion must be legal in
3180
                     --  both directions, and thus both component types must be
3181
                     --  aliased, or neither (4.6 (8)).
3182
 
3183
                     --  The additional rule 4.6 (24.9.2) seems unduly
3184
                     --  restrictive: the privacy requirement should not apply
3185
                     --  to generic types, and should be checked in an
3186
                     --  instance. ARG query is in order ???
3187
 
3188
                     Error_Msg_N
3189
                       ("both component types in a view conversion must be"
3190
                         & " aliased, or neither", A);
3191
 
3192
                  elsif
3193
                     not Same_Ancestor (Etype (F), Etype (Expression (A)))
3194
                  then
3195
                     if Is_By_Reference_Type (Etype (F))
3196
                        or else Is_By_Reference_Type (Etype (Expression (A)))
3197
                     then
3198
                        Error_Msg_N
3199
                          ("view conversion between unrelated by reference " &
3200
                           "array types not allowed (\'A'I-00246)", A);
3201
                     else
3202
                        declare
3203
                           Comp_Type : constant Entity_Id :=
3204
                                         Component_Type
3205
                                           (Etype (Expression (A)));
3206
                        begin
3207
                           if Comes_From_Source (A)
3208
                             and then Ada_Version >= Ada_05
3209
                             and then
3210
                               ((Is_Private_Type (Comp_Type)
3211
                                   and then not Is_Generic_Type (Comp_Type))
3212
                                 or else Is_Tagged_Type (Comp_Type)
3213
                                 or else Is_Volatile (Comp_Type))
3214
                           then
3215
                              Error_Msg_N
3216
                                ("component type of a view conversion cannot"
3217
                                   & " be private, tagged, or volatile"
3218
                                   & " (RM 4.6 (24))",
3219
                                   Expression (A));
3220
                           end if;
3221
                        end;
3222
                     end if;
3223
                  end if;
3224
               end if;
3225
 
3226
               if (Conversion_OK (A)
3227
                     or else Valid_Conversion (A, Etype (A), Expression (A)))
3228
                 and then not Is_Ref_To_Bit_Packed_Array (Expression (A))
3229
               then
3230
                  Resolve (Expression (A));
3231
               end if;
3232
 
3233
            --  If the actual is a function call that returns a limited
3234
            --  unconstrained object that needs finalization, create a
3235
            --  transient scope for it, so that it can receive the proper
3236
            --  finalization list.
3237
 
3238
            elsif Nkind (A) = N_Function_Call
3239
              and then Is_Limited_Record (Etype (F))
3240
              and then not Is_Constrained (Etype (F))
3241
              and then Expander_Active
3242
              and then
3243
                (Is_Controlled (Etype (F)) or else Has_Task (Etype (F)))
3244
            then
3245
               Establish_Transient_Scope (A, False);
3246
 
3247
            --  A small optimization: if one of the actuals is a concatenation
3248
            --  create a block around a procedure call to recover stack space.
3249
            --  This alleviates stack usage when several procedure calls in
3250
            --  the same statement list use concatenation. We do not perform
3251
            --  this wrapping for code statements, where the argument is a
3252
            --  static string, and we want to preserve warnings involving
3253
            --  sequences of such statements.
3254
 
3255
            elsif Nkind (A) = N_Op_Concat
3256
              and then Nkind (N) = N_Procedure_Call_Statement
3257
              and then Expander_Active
3258
              and then
3259
                not (Is_Intrinsic_Subprogram (Nam)
3260
                      and then Chars (Nam) = Name_Asm)
3261
              and then not Static_Concatenation (A)
3262
            then
3263
               Establish_Transient_Scope (A, False);
3264
               Resolve (A, Etype (F));
3265
 
3266
            else
3267
               if Nkind (A) = N_Type_Conversion
3268
                 and then Is_Array_Type (Etype (F))
3269
                 and then not Same_Ancestor (Etype (F), Etype (Expression (A)))
3270
                 and then
3271
                  (Is_Limited_Type (Etype (F))
3272
                     or else Is_Limited_Type (Etype (Expression (A))))
3273
               then
3274
                  Error_Msg_N
3275
                    ("conversion between unrelated limited array types " &
3276
                     "not allowed (\A\I-00246)", A);
3277
 
3278
                  if Is_Limited_Type (Etype (F)) then
3279
                     Explain_Limited_Type (Etype (F), A);
3280
                  end if;
3281
 
3282
                  if Is_Limited_Type (Etype (Expression (A))) then
3283
                     Explain_Limited_Type (Etype (Expression (A)), A);
3284
                  end if;
3285
               end if;
3286
 
3287
               --  (Ada 2005: AI-251): If the actual is an allocator whose
3288
               --  directly designated type is a class-wide interface, we build
3289
               --  an anonymous access type to use it as the type of the
3290
               --  allocator. Later, when the subprogram call is expanded, if
3291
               --  the interface has a secondary dispatch table the expander
3292
               --  will add a type conversion to force the correct displacement
3293
               --  of the pointer.
3294
 
3295
               if Nkind (A) = N_Allocator then
3296
                  declare
3297
                     DDT : constant Entity_Id :=
3298
                             Directly_Designated_Type (Base_Type (Etype (F)));
3299
 
3300
                     New_Itype : Entity_Id;
3301
 
3302
                  begin
3303
                     if Is_Class_Wide_Type (DDT)
3304
                       and then Is_Interface (DDT)
3305
                     then
3306
                        New_Itype := Create_Itype (E_Anonymous_Access_Type, A);
3307
                        Set_Etype (New_Itype, Etype (A));
3308
                        Set_Directly_Designated_Type (New_Itype,
3309
                          Directly_Designated_Type (Etype (A)));
3310
                        Set_Etype (A, New_Itype);
3311
                     end if;
3312
 
3313
                     --  Ada 2005, AI-162:If the actual is an allocator, the
3314
                     --  innermost enclosing statement is the master of the
3315
                     --  created object. This needs to be done with expansion
3316
                     --  enabled only, otherwise the transient scope will not
3317
                     --  be removed in the expansion of the wrapped construct.
3318
 
3319
                     if (Is_Controlled (DDT) or else Has_Task (DDT))
3320
                       and then Expander_Active
3321
                     then
3322
                        Establish_Transient_Scope (A, False);
3323
                     end if;
3324
                  end;
3325
               end if;
3326
 
3327
               --  (Ada 2005): The call may be to a primitive operation of
3328
               --   a tagged synchronized type, declared outside of the type.
3329
               --   In this case the controlling actual must be converted to
3330
               --   its corresponding record type, which is the formal type.
3331
               --   The actual may be a subtype, either because of a constraint
3332
               --   or because it is a generic actual, so use base type to
3333
               --   locate concurrent type.
3334
 
3335
               A_Typ := Base_Type (Etype (A));
3336
               F_Typ := Base_Type (Etype (F));
3337
 
3338
               declare
3339
                  Full_A_Typ : Entity_Id;
3340
 
3341
               begin
3342
                  if Present (Full_View (A_Typ)) then
3343
                     Full_A_Typ := Base_Type (Full_View (A_Typ));
3344
                  else
3345
                     Full_A_Typ := A_Typ;
3346
                  end if;
3347
 
3348
                  --  Tagged synchronized type (case 1): the actual is a
3349
                  --  concurrent type
3350
 
3351
                  if Is_Concurrent_Type (A_Typ)
3352
                    and then Corresponding_Record_Type (A_Typ) = F_Typ
3353
                  then
3354
                     Rewrite (A,
3355
                       Unchecked_Convert_To
3356
                         (Corresponding_Record_Type (A_Typ), A));
3357
                     Resolve (A, Etype (F));
3358
 
3359
                  --  Tagged synchronized type (case 2): the formal is a
3360
                  --  concurrent type
3361
 
3362
                  elsif Ekind (Full_A_Typ) = E_Record_Type
3363
                    and then Present
3364
                               (Corresponding_Concurrent_Type (Full_A_Typ))
3365
                    and then Is_Concurrent_Type (F_Typ)
3366
                    and then Present (Corresponding_Record_Type (F_Typ))
3367
                    and then Full_A_Typ = Corresponding_Record_Type (F_Typ)
3368
                  then
3369
                     Resolve (A, Corresponding_Record_Type (F_Typ));
3370
 
3371
                  --  Common case
3372
 
3373
                  else
3374
                     Resolve (A, Etype (F));
3375
                  end if;
3376
               end;
3377
            end if;
3378
 
3379
            A_Typ := Etype (A);
3380
            F_Typ := Etype (F);
3381
 
3382
            --  For mode IN, if actual is an entity, and the type of the formal
3383
            --  has warnings suppressed, then we reset Never_Set_In_Source for
3384
            --  the calling entity. The reason for this is to catch cases like
3385
            --  GNAT.Spitbol.Patterns.Vstring_Var where the called subprogram
3386
            --  uses trickery to modify an IN parameter.
3387
 
3388
            if Ekind (F) = E_In_Parameter
3389
              and then Is_Entity_Name (A)
3390
              and then Present (Entity (A))
3391
              and then Ekind (Entity (A)) = E_Variable
3392
              and then Has_Warnings_Off (F_Typ)
3393
            then
3394
               Set_Never_Set_In_Source (Entity (A), False);
3395
            end if;
3396
 
3397
            --  Perform error checks for IN and IN OUT parameters
3398
 
3399
            if Ekind (F) /= E_Out_Parameter then
3400
 
3401
               --  Check unset reference. For scalar parameters, it is clearly
3402
               --  wrong to pass an uninitialized value as either an IN or
3403
               --  IN-OUT parameter. For composites, it is also clearly an
3404
               --  error to pass a completely uninitialized value as an IN
3405
               --  parameter, but the case of IN OUT is trickier. We prefer
3406
               --  not to give a warning here. For example, suppose there is
3407
               --  a routine that sets some component of a record to False.
3408
               --  It is perfectly reasonable to make this IN-OUT and allow
3409
               --  either initialized or uninitialized records to be passed
3410
               --  in this case.
3411
 
3412
               --  For partially initialized composite values, we also avoid
3413
               --  warnings, since it is quite likely that we are passing a
3414
               --  partially initialized value and only the initialized fields
3415
               --  will in fact be read in the subprogram.
3416
 
3417
               if Is_Scalar_Type (A_Typ)
3418
                 or else (Ekind (F) = E_In_Parameter
3419
                            and then not Is_Partially_Initialized_Type (A_Typ))
3420
               then
3421
                  Check_Unset_Reference (A);
3422
               end if;
3423
 
3424
               --  In Ada 83 we cannot pass an OUT parameter as an IN or IN OUT
3425
               --  actual to a nested call, since this is case of reading an
3426
               --  out parameter, which is not allowed.
3427
 
3428
               if Ada_Version = Ada_83
3429
                 and then Is_Entity_Name (A)
3430
                 and then Ekind (Entity (A)) = E_Out_Parameter
3431
               then
3432
                  Error_Msg_N ("(Ada 83) illegal reading of out parameter", A);
3433
               end if;
3434
            end if;
3435
 
3436
            --  Case of OUT or IN OUT parameter
3437
 
3438
            if Ekind (F) /= E_In_Parameter then
3439
 
3440
               --  For an Out parameter, check for useless assignment. Note
3441
               --  that we can't set Last_Assignment this early, because we may
3442
               --  kill current values in Resolve_Call, and that call would
3443
               --  clobber the Last_Assignment field.
3444
 
3445
               --  Note: call Warn_On_Useless_Assignment before doing the check
3446
               --  below for Is_OK_Variable_For_Out_Formal so that the setting
3447
               --  of Referenced_As_LHS/Referenced_As_Out_Formal properly
3448
               --  reflects the last assignment, not this one!
3449
 
3450
               if Ekind (F) = E_Out_Parameter then
3451
                  if Warn_On_Modified_As_Out_Parameter (F)
3452
                    and then Is_Entity_Name (A)
3453
                    and then Present (Entity (A))
3454
                    and then Comes_From_Source (N)
3455
                  then
3456
                     Warn_On_Useless_Assignment (Entity (A), A);
3457
                  end if;
3458
               end if;
3459
 
3460
               --  Validate the form of the actual. Note that the call to
3461
               --  Is_OK_Variable_For_Out_Formal generates the required
3462
               --  reference in this case.
3463
 
3464
               if not Is_OK_Variable_For_Out_Formal (A) then
3465
                  Error_Msg_NE ("actual for& must be a variable", A, F);
3466
               end if;
3467
 
3468
               --  What's the following about???
3469
 
3470
               if Is_Entity_Name (A) then
3471
                  Kill_Checks (Entity (A));
3472
               else
3473
                  Kill_All_Checks;
3474
               end if;
3475
            end if;
3476
 
3477
            if Etype (A) = Any_Type then
3478
               Set_Etype (N, Any_Type);
3479
               return;
3480
            end if;
3481
 
3482
            --  Apply appropriate range checks for in, out, and in-out
3483
            --  parameters. Out and in-out parameters also need a separate
3484
            --  check, if there is a type conversion, to make sure the return
3485
            --  value meets the constraints of the variable before the
3486
            --  conversion.
3487
 
3488
            --  Gigi looks at the check flag and uses the appropriate types.
3489
            --  For now since one flag is used there is an optimization which
3490
            --  might not be done in the In Out case since Gigi does not do
3491
            --  any analysis. More thought required about this ???
3492
 
3493
            if Ekind (F) = E_In_Parameter
3494
              or else Ekind (F) = E_In_Out_Parameter
3495
            then
3496
               if Is_Scalar_Type (Etype (A)) then
3497
                  Apply_Scalar_Range_Check (A, F_Typ);
3498
 
3499
               elsif Is_Array_Type (Etype (A)) then
3500
                  Apply_Length_Check (A, F_Typ);
3501
 
3502
               elsif Is_Record_Type (F_Typ)
3503
                 and then Has_Discriminants (F_Typ)
3504
                 and then Is_Constrained (F_Typ)
3505
                 and then (not Is_Derived_Type (F_Typ)
3506
                             or else Comes_From_Source (Nam))
3507
               then
3508
                  Apply_Discriminant_Check (A, F_Typ);
3509
 
3510
               elsif Is_Access_Type (F_Typ)
3511
                 and then Is_Array_Type (Designated_Type (F_Typ))
3512
                 and then Is_Constrained (Designated_Type (F_Typ))
3513
               then
3514
                  Apply_Length_Check (A, F_Typ);
3515
 
3516
               elsif Is_Access_Type (F_Typ)
3517
                 and then Has_Discriminants (Designated_Type (F_Typ))
3518
                 and then Is_Constrained (Designated_Type (F_Typ))
3519
               then
3520
                  Apply_Discriminant_Check (A, F_Typ);
3521
 
3522
               else
3523
                  Apply_Range_Check (A, F_Typ);
3524
               end if;
3525
 
3526
               --  Ada 2005 (AI-231)
3527
 
3528
               if Ada_Version >= Ada_05
3529
                 and then Is_Access_Type (F_Typ)
3530
                 and then Can_Never_Be_Null (F_Typ)
3531
                 and then Known_Null (A)
3532
               then
3533
                  Apply_Compile_Time_Constraint_Error
3534
                    (N      => A,
3535
                     Msg    => "(Ada 2005) null not allowed in "
3536
                               & "null-excluding formal?",
3537
                     Reason => CE_Null_Not_Allowed);
3538
               end if;
3539
            end if;
3540
 
3541
            if Ekind (F) = E_Out_Parameter
3542
              or else Ekind (F) = E_In_Out_Parameter
3543
            then
3544
               if Nkind (A) = N_Type_Conversion then
3545
                  if Is_Scalar_Type (A_Typ) then
3546
                     Apply_Scalar_Range_Check
3547
                       (Expression (A), Etype (Expression (A)), A_Typ);
3548
                  else
3549
                     Apply_Range_Check
3550
                       (Expression (A), Etype (Expression (A)), A_Typ);
3551
                  end if;
3552
 
3553
               else
3554
                  if Is_Scalar_Type (F_Typ) then
3555
                     Apply_Scalar_Range_Check (A, A_Typ, F_Typ);
3556
 
3557
                  elsif Is_Array_Type (F_Typ)
3558
                    and then Ekind (F) = E_Out_Parameter
3559
                  then
3560
                     Apply_Length_Check (A, F_Typ);
3561
 
3562
                  else
3563
                     Apply_Range_Check (A, A_Typ, F_Typ);
3564
                  end if;
3565
               end if;
3566
            end if;
3567
 
3568
            --  An actual associated with an access parameter is implicitly
3569
            --  converted to the anonymous access type of the formal and must
3570
            --  satisfy the legality checks for access conversions.
3571
 
3572
            if Ekind (F_Typ) = E_Anonymous_Access_Type then
3573
               if not Valid_Conversion (A, F_Typ, A) then
3574
                  Error_Msg_N
3575
                    ("invalid implicit conversion for access parameter", A);
3576
               end if;
3577
            end if;
3578
 
3579
            --  Check bad case of atomic/volatile argument (RM C.6(12))
3580
 
3581
            if Is_By_Reference_Type (Etype (F))
3582
              and then Comes_From_Source (N)
3583
            then
3584
               if Is_Atomic_Object (A)
3585
                 and then not Is_Atomic (Etype (F))
3586
               then
3587
                  Error_Msg_N
3588
                    ("cannot pass atomic argument to non-atomic formal",
3589
                     N);
3590
 
3591
               elsif Is_Volatile_Object (A)
3592
                 and then not Is_Volatile (Etype (F))
3593
               then
3594
                  Error_Msg_N
3595
                    ("cannot pass volatile argument to non-volatile formal",
3596
                     N);
3597
               end if;
3598
            end if;
3599
 
3600
            --  Check that subprograms don't have improper controlling
3601
            --  arguments (RM 3.9.2 (9)).
3602
 
3603
            --  A primitive operation may have an access parameter of an
3604
            --  incomplete tagged type, but a dispatching call is illegal
3605
            --  if the type is still incomplete.
3606
 
3607
            if Is_Controlling_Formal (F) then
3608
               Set_Is_Controlling_Actual (A);
3609
 
3610
               if Ekind (Etype (F)) = E_Anonymous_Access_Type then
3611
                  declare
3612
                     Desig : constant Entity_Id := Designated_Type (Etype (F));
3613
                  begin
3614
                     if Ekind (Desig) = E_Incomplete_Type
3615
                       and then No (Full_View (Desig))
3616
                       and then No (Non_Limited_View (Desig))
3617
                     then
3618
                        Error_Msg_NE
3619
                          ("premature use of incomplete type& " &
3620
                           "in dispatching call", A, Desig);
3621
                     end if;
3622
                  end;
3623
               end if;
3624
 
3625
            elsif Nkind (A) = N_Explicit_Dereference then
3626
               Validate_Remote_Access_To_Class_Wide_Type (A);
3627
            end if;
3628
 
3629
            if (Is_Class_Wide_Type (A_Typ) or else Is_Dynamically_Tagged (A))
3630
              and then not Is_Class_Wide_Type (F_Typ)
3631
              and then not Is_Controlling_Formal (F)
3632
            then
3633
               Error_Msg_N ("class-wide argument not allowed here!", A);
3634
 
3635
               if Is_Subprogram (Nam)
3636
                 and then Comes_From_Source (Nam)
3637
               then
3638
                  Error_Msg_Node_2 := F_Typ;
3639
                  Error_Msg_NE
3640
                    ("& is not a dispatching operation of &!", A, Nam);
3641
               end if;
3642
 
3643
            elsif Is_Access_Type (A_Typ)
3644
              and then Is_Access_Type (F_Typ)
3645
              and then Ekind (F_Typ) /= E_Access_Subprogram_Type
3646
              and then Ekind (F_Typ) /= E_Anonymous_Access_Subprogram_Type
3647
              and then (Is_Class_Wide_Type (Designated_Type (A_Typ))
3648
                         or else (Nkind (A) = N_Attribute_Reference
3649
                                   and then
3650
                                     Is_Class_Wide_Type (Etype (Prefix (A)))))
3651
              and then not Is_Class_Wide_Type (Designated_Type (F_Typ))
3652
              and then not Is_Controlling_Formal (F)
3653
 
3654
              --  Disable these checks for call to imported C++ subprograms
3655
 
3656
              and then not
3657
                (Is_Entity_Name (Name (N))
3658
                  and then Is_Imported (Entity (Name (N)))
3659
                  and then Convention (Entity (Name (N))) = Convention_CPP)
3660
            then
3661
               Error_Msg_N
3662
                 ("access to class-wide argument not allowed here!", A);
3663
 
3664
               if Is_Subprogram (Nam)
3665
                 and then Comes_From_Source (Nam)
3666
               then
3667
                  Error_Msg_Node_2 := Designated_Type (F_Typ);
3668
                  Error_Msg_NE
3669
                    ("& is not a dispatching operation of &!", A, Nam);
3670
               end if;
3671
            end if;
3672
 
3673
            Eval_Actual (A);
3674
 
3675
            --  If it is a named association, treat the selector_name as
3676
            --  a proper identifier, and mark the corresponding entity.
3677
 
3678
            if Nkind (Parent (A)) = N_Parameter_Association then
3679
               Set_Entity (Selector_Name (Parent (A)), F);
3680
               Generate_Reference (F, Selector_Name (Parent (A)));
3681
               Set_Etype (Selector_Name (Parent (A)), F_Typ);
3682
               Generate_Reference (F_Typ, N, ' ');
3683
            end if;
3684
 
3685
            Prev := A;
3686
 
3687
            if Ekind (F) /= E_Out_Parameter then
3688
               Check_Unset_Reference (A);
3689
            end if;
3690
 
3691
            Next_Actual (A);
3692
 
3693
         --  Case where actual is not present
3694
 
3695
         else
3696
            Insert_Default;
3697
         end if;
3698
 
3699
         Next_Formal (F);
3700
      end loop;
3701
   end Resolve_Actuals;
3702
 
3703
   -----------------------
3704
   -- Resolve_Allocator --
3705
   -----------------------
3706
 
3707
   procedure Resolve_Allocator (N : Node_Id; Typ : Entity_Id) is
3708
      E        : constant Node_Id := Expression (N);
3709
      Subtyp   : Entity_Id;
3710
      Discrim  : Entity_Id;
3711
      Constr   : Node_Id;
3712
      Aggr     : Node_Id;
3713
      Assoc    : Node_Id := Empty;
3714
      Disc_Exp : Node_Id;
3715
 
3716
      procedure Check_Allocator_Discrim_Accessibility
3717
        (Disc_Exp  : Node_Id;
3718
         Alloc_Typ : Entity_Id);
3719
      --  Check that accessibility level associated with an access discriminant
3720
      --  initialized in an allocator by the expression Disc_Exp is not deeper
3721
      --  than the level of the allocator type Alloc_Typ. An error message is
3722
      --  issued if this condition is violated. Specialized checks are done for
3723
      --  the cases of a constraint expression which is an access attribute or
3724
      --  an access discriminant.
3725
 
3726
      function In_Dispatching_Context return Boolean;
3727
      --  If the allocator is an actual in a call, it is allowed to be class-
3728
      --  wide when the context is not because it is a controlling actual.
3729
 
3730
      procedure Propagate_Coextensions (Root : Node_Id);
3731
      --  Propagate all nested coextensions which are located one nesting
3732
      --  level down the tree to the node Root. Example:
3733
      --
3734
      --    Top_Record
3735
      --       Level_1_Coextension
3736
      --          Level_2_Coextension
3737
      --
3738
      --  The algorithm is paired with delay actions done by the Expander. In
3739
      --  the above example, assume all coextensions are controlled types.
3740
      --  The cycle of analysis, resolution and expansion will yield:
3741
      --
3742
      --  1) Analyze Top_Record
3743
      --  2) Analyze Level_1_Coextension
3744
      --  3) Analyze Level_2_Coextension
3745
      --  4) Resolve Level_2_Coextension. The allocator is marked as a
3746
      --       coextension.
3747
      --  5) Expand Level_2_Coextension. A temporary variable Temp_1 is
3748
      --       generated to capture the allocated object. Temp_1 is attached
3749
      --       to the coextension chain of Level_2_Coextension.
3750
      --  6) Resolve Level_1_Coextension. The allocator is marked as a
3751
      --       coextension. A forward tree traversal is performed which finds
3752
      --       Level_2_Coextension's list and copies its contents into its
3753
      --       own list.
3754
      --  7) Expand Level_1_Coextension. A temporary variable Temp_2 is
3755
      --       generated to capture the allocated object. Temp_2 is attached
3756
      --       to the coextension chain of Level_1_Coextension. Currently, the
3757
      --       contents of the list are [Temp_2, Temp_1].
3758
      --  8) Resolve Top_Record. A forward tree traversal is performed which
3759
      --       finds Level_1_Coextension's list and copies its contents into
3760
      --       its own list.
3761
      --  9) Expand Top_Record. Generate finalization calls for Temp_1 and
3762
      --       Temp_2 and attach them to Top_Record's finalization list.
3763
 
3764
      -------------------------------------------
3765
      -- Check_Allocator_Discrim_Accessibility --
3766
      -------------------------------------------
3767
 
3768
      procedure Check_Allocator_Discrim_Accessibility
3769
        (Disc_Exp  : Node_Id;
3770
         Alloc_Typ : Entity_Id)
3771
      is
3772
      begin
3773
         if Type_Access_Level (Etype (Disc_Exp)) >
3774
            Type_Access_Level (Alloc_Typ)
3775
         then
3776
            Error_Msg_N
3777
              ("operand type has deeper level than allocator type", Disc_Exp);
3778
 
3779
         --  When the expression is an Access attribute the level of the prefix
3780
         --  object must not be deeper than that of the allocator's type.
3781
 
3782
         elsif Nkind (Disc_Exp) = N_Attribute_Reference
3783
           and then Get_Attribute_Id (Attribute_Name (Disc_Exp))
3784
                      = Attribute_Access
3785
           and then Object_Access_Level (Prefix (Disc_Exp))
3786
                      > Type_Access_Level (Alloc_Typ)
3787
         then
3788
            Error_Msg_N
3789
              ("prefix of attribute has deeper level than allocator type",
3790
               Disc_Exp);
3791
 
3792
         --  When the expression is an access discriminant the check is against
3793
         --  the level of the prefix object.
3794
 
3795
         elsif Ekind (Etype (Disc_Exp)) = E_Anonymous_Access_Type
3796
           and then Nkind (Disc_Exp) = N_Selected_Component
3797
           and then Object_Access_Level (Prefix (Disc_Exp))
3798
                      > Type_Access_Level (Alloc_Typ)
3799
         then
3800
            Error_Msg_N
3801
              ("access discriminant has deeper level than allocator type",
3802
               Disc_Exp);
3803
 
3804
         --  All other cases are legal
3805
 
3806
         else
3807
            null;
3808
         end if;
3809
      end Check_Allocator_Discrim_Accessibility;
3810
 
3811
      ----------------------------
3812
      -- In_Dispatching_Context --
3813
      ----------------------------
3814
 
3815
      function In_Dispatching_Context return Boolean is
3816
         Par : constant Node_Id := Parent (N);
3817
      begin
3818
         return Nkind_In (Par, N_Function_Call, N_Procedure_Call_Statement)
3819
           and then Is_Entity_Name (Name (Par))
3820
           and then Is_Dispatching_Operation (Entity (Name (Par)));
3821
      end In_Dispatching_Context;
3822
 
3823
      ----------------------------
3824
      -- Propagate_Coextensions --
3825
      ----------------------------
3826
 
3827
      procedure Propagate_Coextensions (Root : Node_Id) is
3828
 
3829
         procedure Copy_List (From : Elist_Id; To : Elist_Id);
3830
         --  Copy the contents of list From into list To, preserving the
3831
         --  order of elements.
3832
 
3833
         function Process_Allocator (Nod : Node_Id) return Traverse_Result;
3834
         --  Recognize an allocator or a rewritten allocator node and add it
3835
         --  along with its nested coextensions to the list of Root.
3836
 
3837
         ---------------
3838
         -- Copy_List --
3839
         ---------------
3840
 
3841
         procedure Copy_List (From : Elist_Id; To : Elist_Id) is
3842
            From_Elmt : Elmt_Id;
3843
         begin
3844
            From_Elmt := First_Elmt (From);
3845
            while Present (From_Elmt) loop
3846
               Append_Elmt (Node (From_Elmt), To);
3847
               Next_Elmt (From_Elmt);
3848
            end loop;
3849
         end Copy_List;
3850
 
3851
         -----------------------
3852
         -- Process_Allocator --
3853
         -----------------------
3854
 
3855
         function Process_Allocator (Nod : Node_Id) return Traverse_Result is
3856
            Orig_Nod : Node_Id := Nod;
3857
 
3858
         begin
3859
            --  This is a possible rewritten subtype indication allocator. Any
3860
            --  nested coextensions will appear as discriminant constraints.
3861
 
3862
            if Nkind (Nod) = N_Identifier
3863
              and then Present (Original_Node (Nod))
3864
              and then Nkind (Original_Node (Nod)) = N_Subtype_Indication
3865
            then
3866
               declare
3867
                  Discr      : Node_Id;
3868
                  Discr_Elmt : Elmt_Id;
3869
 
3870
               begin
3871
                  if Is_Record_Type (Entity (Nod)) then
3872
                     Discr_Elmt :=
3873
                       First_Elmt (Discriminant_Constraint (Entity (Nod)));
3874
                     while Present (Discr_Elmt) loop
3875
                        Discr := Node (Discr_Elmt);
3876
 
3877
                        if Nkind (Discr) = N_Identifier
3878
                          and then Present (Original_Node (Discr))
3879
                          and then Nkind (Original_Node (Discr)) = N_Allocator
3880
                          and then Present (Coextensions (
3881
                                     Original_Node (Discr)))
3882
                        then
3883
                           if No (Coextensions (Root)) then
3884
                              Set_Coextensions (Root, New_Elmt_List);
3885
                           end if;
3886
 
3887
                           Copy_List
3888
                             (From => Coextensions (Original_Node (Discr)),
3889
                              To   => Coextensions (Root));
3890
                        end if;
3891
 
3892
                        Next_Elmt (Discr_Elmt);
3893
                     end loop;
3894
 
3895
                     --  There is no need to continue the traversal of this
3896
                     --  subtree since all the information has already been
3897
                     --  propagated.
3898
 
3899
                     return Skip;
3900
                  end if;
3901
               end;
3902
 
3903
            --  Case of either a stand alone allocator or a rewritten allocator
3904
            --  with an aggregate.
3905
 
3906
            else
3907
               if Present (Original_Node (Nod)) then
3908
                  Orig_Nod := Original_Node (Nod);
3909
               end if;
3910
 
3911
               if Nkind (Orig_Nod) = N_Allocator then
3912
 
3913
                  --  Propagate the list of nested coextensions to the Root
3914
                  --  allocator. This is done through list copy since a single
3915
                  --  allocator may have multiple coextensions. Do not touch
3916
                  --  coextensions roots.
3917
 
3918
                  if not Is_Coextension_Root (Orig_Nod)
3919
                    and then Present (Coextensions (Orig_Nod))
3920
                  then
3921
                     if No (Coextensions (Root)) then
3922
                        Set_Coextensions (Root, New_Elmt_List);
3923
                     end if;
3924
 
3925
                     Copy_List
3926
                       (From => Coextensions (Orig_Nod),
3927
                        To   => Coextensions (Root));
3928
                  end if;
3929
 
3930
                  --  There is no need to continue the traversal of this
3931
                  --  subtree since all the information has already been
3932
                  --  propagated.
3933
 
3934
                  return Skip;
3935
               end if;
3936
            end if;
3937
 
3938
            --  Keep on traversing, looking for the next allocator
3939
 
3940
            return OK;
3941
         end Process_Allocator;
3942
 
3943
         procedure Process_Allocators is
3944
           new Traverse_Proc (Process_Allocator);
3945
 
3946
      --  Start of processing for Propagate_Coextensions
3947
 
3948
      begin
3949
         Process_Allocators (Expression (Root));
3950
      end Propagate_Coextensions;
3951
 
3952
   --  Start of processing for Resolve_Allocator
3953
 
3954
   begin
3955
      --  Replace general access with specific type
3956
 
3957
      if Ekind (Etype (N)) = E_Allocator_Type then
3958
         Set_Etype (N, Base_Type (Typ));
3959
      end if;
3960
 
3961
      if Is_Abstract_Type (Typ) then
3962
         Error_Msg_N ("type of allocator cannot be abstract",  N);
3963
      end if;
3964
 
3965
      --  For qualified expression, resolve the expression using the
3966
      --  given subtype (nothing to do for type mark, subtype indication)
3967
 
3968
      if Nkind (E) = N_Qualified_Expression then
3969
         if Is_Class_Wide_Type (Etype (E))
3970
           and then not Is_Class_Wide_Type (Designated_Type (Typ))
3971
           and then not In_Dispatching_Context
3972
         then
3973
            Error_Msg_N
3974
              ("class-wide allocator not allowed for this access type", N);
3975
         end if;
3976
 
3977
         Resolve (Expression (E), Etype (E));
3978
         Check_Unset_Reference (Expression (E));
3979
 
3980
         --  A qualified expression requires an exact match of the type,
3981
         --  class-wide matching is not allowed.
3982
 
3983
         if (Is_Class_Wide_Type (Etype (Expression (E)))
3984
                 or else Is_Class_Wide_Type (Etype (E)))
3985
           and then Base_Type (Etype (Expression (E))) /= Base_Type (Etype (E))
3986
         then
3987
            Wrong_Type (Expression (E), Etype (E));
3988
         end if;
3989
 
3990
         --  A special accessibility check is needed for allocators that
3991
         --  constrain access discriminants. The level of the type of the
3992
         --  expression used to constrain an access discriminant cannot be
3993
         --  deeper than the type of the allocator (in contrast to access
3994
         --  parameters, where the level of the actual can be arbitrary).
3995
 
3996
         --  We can't use Valid_Conversion to perform this check because
3997
         --  in general the type of the allocator is unrelated to the type
3998
         --  of the access discriminant.
3999
 
4000
         if Ekind (Typ) /= E_Anonymous_Access_Type
4001
           or else Is_Local_Anonymous_Access (Typ)
4002
         then
4003
            Subtyp := Entity (Subtype_Mark (E));
4004
 
4005
            Aggr := Original_Node (Expression (E));
4006
 
4007
            if Has_Discriminants (Subtyp)
4008
              and then Nkind_In (Aggr, N_Aggregate, N_Extension_Aggregate)
4009
            then
4010
               Discrim := First_Discriminant (Base_Type (Subtyp));
4011
 
4012
               --  Get the first component expression of the aggregate
4013
 
4014
               if Present (Expressions (Aggr)) then
4015
                  Disc_Exp := First (Expressions (Aggr));
4016
 
4017
               elsif Present (Component_Associations (Aggr)) then
4018
                  Assoc := First (Component_Associations (Aggr));
4019
 
4020
                  if Present (Assoc) then
4021
                     Disc_Exp := Expression (Assoc);
4022
                  else
4023
                     Disc_Exp := Empty;
4024
                  end if;
4025
 
4026
               else
4027
                  Disc_Exp := Empty;
4028
               end if;
4029
 
4030
               while Present (Discrim) and then Present (Disc_Exp) loop
4031
                  if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4032
                     Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4033
                  end if;
4034
 
4035
                  Next_Discriminant (Discrim);
4036
 
4037
                  if Present (Discrim) then
4038
                     if Present (Assoc) then
4039
                        Next (Assoc);
4040
                        Disc_Exp := Expression (Assoc);
4041
 
4042
                     elsif Present (Next (Disc_Exp)) then
4043
                        Next (Disc_Exp);
4044
 
4045
                     else
4046
                        Assoc := First (Component_Associations (Aggr));
4047
 
4048
                        if Present (Assoc) then
4049
                           Disc_Exp := Expression (Assoc);
4050
                        else
4051
                           Disc_Exp := Empty;
4052
                        end if;
4053
                     end if;
4054
                  end if;
4055
               end loop;
4056
            end if;
4057
         end if;
4058
 
4059
      --  For a subtype mark or subtype indication, freeze the subtype
4060
 
4061
      else
4062
         Freeze_Expression (E);
4063
 
4064
         if Is_Access_Constant (Typ) and then not No_Initialization (N) then
4065
            Error_Msg_N
4066
              ("initialization required for access-to-constant allocator", N);
4067
         end if;
4068
 
4069
         --  A special accessibility check is needed for allocators that
4070
         --  constrain access discriminants. The level of the type of the
4071
         --  expression used to constrain an access discriminant cannot be
4072
         --  deeper than the type of the allocator (in contrast to access
4073
         --  parameters, where the level of the actual can be arbitrary).
4074
         --  We can't use Valid_Conversion to perform this check because
4075
         --  in general the type of the allocator is unrelated to the type
4076
         --  of the access discriminant.
4077
 
4078
         if Nkind (Original_Node (E)) = N_Subtype_Indication
4079
           and then (Ekind (Typ) /= E_Anonymous_Access_Type
4080
                      or else Is_Local_Anonymous_Access (Typ))
4081
         then
4082
            Subtyp := Entity (Subtype_Mark (Original_Node (E)));
4083
 
4084
            if Has_Discriminants (Subtyp) then
4085
               Discrim := First_Discriminant (Base_Type (Subtyp));
4086
               Constr := First (Constraints (Constraint (Original_Node (E))));
4087
               while Present (Discrim) and then Present (Constr) loop
4088
                  if Ekind (Etype (Discrim)) = E_Anonymous_Access_Type then
4089
                     if Nkind (Constr) = N_Discriminant_Association then
4090
                        Disc_Exp := Original_Node (Expression (Constr));
4091
                     else
4092
                        Disc_Exp := Original_Node (Constr);
4093
                     end if;
4094
 
4095
                     Check_Allocator_Discrim_Accessibility (Disc_Exp, Typ);
4096
                  end if;
4097
 
4098
                  Next_Discriminant (Discrim);
4099
                  Next (Constr);
4100
               end loop;
4101
            end if;
4102
         end if;
4103
      end if;
4104
 
4105
      --  Ada 2005 (AI-344): A class-wide allocator requires an accessibility
4106
      --  check that the level of the type of the created object is not deeper
4107
      --  than the level of the allocator's access type, since extensions can
4108
      --  now occur at deeper levels than their ancestor types. This is a
4109
      --  static accessibility level check; a run-time check is also needed in
4110
      --  the case of an initialized allocator with a class-wide argument (see
4111
      --  Expand_Allocator_Expression).
4112
 
4113
      if Ada_Version >= Ada_05
4114
        and then Is_Class_Wide_Type (Designated_Type (Typ))
4115
      then
4116
         declare
4117
            Exp_Typ : Entity_Id;
4118
 
4119
         begin
4120
            if Nkind (E) = N_Qualified_Expression then
4121
               Exp_Typ := Etype (E);
4122
            elsif Nkind (E) = N_Subtype_Indication then
4123
               Exp_Typ := Entity (Subtype_Mark (Original_Node (E)));
4124
            else
4125
               Exp_Typ := Entity (E);
4126
            end if;
4127
 
4128
            if Type_Access_Level (Exp_Typ) > Type_Access_Level (Typ) then
4129
               if In_Instance_Body then
4130
                  Error_Msg_N ("?type in allocator has deeper level than" &
4131
                               " designated class-wide type", E);
4132
                  Error_Msg_N ("\?Program_Error will be raised at run time",
4133
                               E);
4134
                  Rewrite (N,
4135
                    Make_Raise_Program_Error (Sloc (N),
4136
                      Reason => PE_Accessibility_Check_Failed));
4137
                  Set_Etype (N, Typ);
4138
 
4139
               --  Do not apply Ada 2005 accessibility checks on a class-wide
4140
               --  allocator if the type given in the allocator is a formal
4141
               --  type. A run-time check will be performed in the instance.
4142
 
4143
               elsif not Is_Generic_Type (Exp_Typ) then
4144
                  Error_Msg_N ("type in allocator has deeper level than" &
4145
                               " designated class-wide type", E);
4146
               end if;
4147
            end if;
4148
         end;
4149
      end if;
4150
 
4151
      --  Check for allocation from an empty storage pool
4152
 
4153
      if No_Pool_Assigned (Typ) then
4154
         declare
4155
            Loc : constant Source_Ptr := Sloc (N);
4156
         begin
4157
            Error_Msg_N ("?allocation from empty storage pool!", N);
4158
            Error_Msg_N ("\?Storage_Error will be raised at run time!", N);
4159
            Insert_Action (N,
4160
              Make_Raise_Storage_Error (Loc,
4161
                Reason => SE_Empty_Storage_Pool));
4162
         end;
4163
 
4164
      --  If the context is an unchecked conversion, as may happen within
4165
      --  an inlined subprogram, the allocator is being resolved with its
4166
      --  own anonymous type. In that case, if the target type has a specific
4167
      --  storage pool, it must be inherited explicitly by the allocator type.
4168
 
4169
      elsif Nkind (Parent (N)) = N_Unchecked_Type_Conversion
4170
        and then No (Associated_Storage_Pool (Typ))
4171
      then
4172
         Set_Associated_Storage_Pool
4173
           (Typ, Associated_Storage_Pool (Etype (Parent (N))));
4174
      end if;
4175
 
4176
      --  An erroneous allocator may be rewritten as a raise Program_Error
4177
      --  statement.
4178
 
4179
      if Nkind (N) = N_Allocator then
4180
 
4181
         --  An anonymous access discriminant is the definition of a
4182
         --  coextension.
4183
 
4184
         if Ekind (Typ) = E_Anonymous_Access_Type
4185
           and then Nkind (Associated_Node_For_Itype (Typ)) =
4186
                      N_Discriminant_Specification
4187
         then
4188
            --  Avoid marking an allocator as a dynamic coextension if it is
4189
            --  within a static construct.
4190
 
4191
            if not Is_Static_Coextension (N) then
4192
               Set_Is_Dynamic_Coextension (N);
4193
            end if;
4194
 
4195
         --  Cleanup for potential static coextensions
4196
 
4197
         else
4198
            Set_Is_Dynamic_Coextension (N, False);
4199
            Set_Is_Static_Coextension  (N, False);
4200
         end if;
4201
 
4202
         --  There is no need to propagate any nested coextensions if they
4203
         --  are marked as static since they will be rewritten on the spot.
4204
 
4205
         if not Is_Static_Coextension (N) then
4206
            Propagate_Coextensions (N);
4207
         end if;
4208
      end if;
4209
   end Resolve_Allocator;
4210
 
4211
   ---------------------------
4212
   -- Resolve_Arithmetic_Op --
4213
   ---------------------------
4214
 
4215
   --  Used for resolving all arithmetic operators except exponentiation
4216
 
4217
   procedure Resolve_Arithmetic_Op (N : Node_Id; Typ : Entity_Id) is
4218
      L   : constant Node_Id := Left_Opnd (N);
4219
      R   : constant Node_Id := Right_Opnd (N);
4220
      TL  : constant Entity_Id := Base_Type (Etype (L));
4221
      TR  : constant Entity_Id := Base_Type (Etype (R));
4222
      T   : Entity_Id;
4223
      Rop : Node_Id;
4224
 
4225
      B_Typ : constant Entity_Id := Base_Type (Typ);
4226
      --  We do the resolution using the base type, because intermediate values
4227
      --  in expressions always are of the base type, not a subtype of it.
4228
 
4229
      function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean;
4230
      --  Returns True if N is in a context that expects "any real type"
4231
 
4232
      function Is_Integer_Or_Universal (N : Node_Id) return Boolean;
4233
      --  Return True iff given type is Integer or universal real/integer
4234
 
4235
      procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id);
4236
      --  Choose type of integer literal in fixed-point operation to conform
4237
      --  to available fixed-point type. T is the type of the other operand,
4238
      --  which is needed to determine the expected type of N.
4239
 
4240
      procedure Set_Operand_Type (N : Node_Id);
4241
      --  Set operand type to T if universal
4242
 
4243
      -------------------------------
4244
      -- Expected_Type_Is_Any_Real --
4245
      -------------------------------
4246
 
4247
      function Expected_Type_Is_Any_Real (N : Node_Id) return Boolean is
4248
      begin
4249
         --  N is the expression after "delta" in a fixed_point_definition;
4250
         --  see RM-3.5.9(6):
4251
 
4252
         return Nkind_In (Parent (N), N_Ordinary_Fixed_Point_Definition,
4253
                                      N_Decimal_Fixed_Point_Definition,
4254
 
4255
         --  N is one of the bounds in a real_range_specification;
4256
         --  see RM-3.5.7(5):
4257
 
4258
                                      N_Real_Range_Specification,
4259
 
4260
         --  N is the expression of a delta_constraint;
4261
         --  see RM-J.3(3):
4262
 
4263
                                      N_Delta_Constraint);
4264
      end Expected_Type_Is_Any_Real;
4265
 
4266
      -----------------------------
4267
      -- Is_Integer_Or_Universal --
4268
      -----------------------------
4269
 
4270
      function Is_Integer_Or_Universal (N : Node_Id) return Boolean is
4271
         T     : Entity_Id;
4272
         Index : Interp_Index;
4273
         It    : Interp;
4274
 
4275
      begin
4276
         if not Is_Overloaded (N) then
4277
            T := Etype (N);
4278
            return Base_Type (T) = Base_Type (Standard_Integer)
4279
              or else T = Universal_Integer
4280
              or else T = Universal_Real;
4281
         else
4282
            Get_First_Interp (N, Index, It);
4283
            while Present (It.Typ) loop
4284
               if Base_Type (It.Typ) = Base_Type (Standard_Integer)
4285
                 or else It.Typ = Universal_Integer
4286
                 or else It.Typ = Universal_Real
4287
               then
4288
                  return True;
4289
               end if;
4290
 
4291
               Get_Next_Interp (Index, It);
4292
            end loop;
4293
         end if;
4294
 
4295
         return False;
4296
      end Is_Integer_Or_Universal;
4297
 
4298
      ----------------------------
4299
      -- Set_Mixed_Mode_Operand --
4300
      ----------------------------
4301
 
4302
      procedure Set_Mixed_Mode_Operand (N : Node_Id; T : Entity_Id) is
4303
         Index : Interp_Index;
4304
         It    : Interp;
4305
 
4306
      begin
4307
         if Universal_Interpretation (N) = Universal_Integer then
4308
 
4309
            --  A universal integer literal is resolved as standard integer
4310
            --  except in the case of a fixed-point result, where we leave it
4311
            --  as universal (to be handled by Exp_Fixd later on)
4312
 
4313
            if Is_Fixed_Point_Type (T) then
4314
               Resolve (N, Universal_Integer);
4315
            else
4316
               Resolve (N, Standard_Integer);
4317
            end if;
4318
 
4319
         elsif Universal_Interpretation (N) = Universal_Real
4320
           and then (T = Base_Type (Standard_Integer)
4321
                      or else T = Universal_Integer
4322
                      or else T = Universal_Real)
4323
         then
4324
            --  A universal real can appear in a fixed-type context. We resolve
4325
            --  the literal with that context, even though this might raise an
4326
            --  exception prematurely (the other operand may be zero).
4327
 
4328
            Resolve (N, B_Typ);
4329
 
4330
         elsif Etype (N) = Base_Type (Standard_Integer)
4331
           and then T = Universal_Real
4332
           and then Is_Overloaded (N)
4333
         then
4334
            --  Integer arg in mixed-mode operation. Resolve with universal
4335
            --  type, in case preference rule must be applied.
4336
 
4337
            Resolve (N, Universal_Integer);
4338
 
4339
         elsif Etype (N) = T
4340
           and then B_Typ /= Universal_Fixed
4341
         then
4342
            --  Not a mixed-mode operation, resolve with context
4343
 
4344
            Resolve (N, B_Typ);
4345
 
4346
         elsif Etype (N) = Any_Fixed then
4347
 
4348
            --  N may itself be a mixed-mode operation, so use context type
4349
 
4350
            Resolve (N, B_Typ);
4351
 
4352
         elsif Is_Fixed_Point_Type (T)
4353
           and then B_Typ = Universal_Fixed
4354
           and then Is_Overloaded (N)
4355
         then
4356
            --  Must be (fixed * fixed) operation, operand must have one
4357
            --  compatible interpretation.
4358
 
4359
            Resolve (N, Any_Fixed);
4360
 
4361
         elsif Is_Fixed_Point_Type (B_Typ)
4362
           and then (T = Universal_Real
4363
                      or else Is_Fixed_Point_Type (T))
4364
           and then Is_Overloaded (N)
4365
         then
4366
            --  C * F(X) in a fixed context, where C is a real literal or a
4367
            --  fixed-point expression. F must have either a fixed type
4368
            --  interpretation or an integer interpretation, but not both.
4369
 
4370
            Get_First_Interp (N, Index, It);
4371
            while Present (It.Typ) loop
4372
               if Base_Type (It.Typ) = Base_Type (Standard_Integer) then
4373
 
4374
                  if Analyzed (N) then
4375
                     Error_Msg_N ("ambiguous operand in fixed operation", N);
4376
                  else
4377
                     Resolve (N, Standard_Integer);
4378
                  end if;
4379
 
4380
               elsif Is_Fixed_Point_Type (It.Typ) then
4381
 
4382
                  if Analyzed (N) then
4383
                     Error_Msg_N ("ambiguous operand in fixed operation", N);
4384
                  else
4385
                     Resolve (N, It.Typ);
4386
                  end if;
4387
               end if;
4388
 
4389
               Get_Next_Interp (Index, It);
4390
            end loop;
4391
 
4392
            --  Reanalyze the literal with the fixed type of the context. If
4393
            --  context is Universal_Fixed, we are within a conversion, leave
4394
            --  the literal as a universal real because there is no usable
4395
            --  fixed type, and the target of the conversion plays no role in
4396
            --  the resolution.
4397
 
4398
            declare
4399
               Op2 : Node_Id;
4400
               T2  : Entity_Id;
4401
 
4402
            begin
4403
               if N = L then
4404
                  Op2 := R;
4405
               else
4406
                  Op2 := L;
4407
               end if;
4408
 
4409
               if B_Typ = Universal_Fixed
4410
                  and then Nkind (Op2) = N_Real_Literal
4411
               then
4412
                  T2 := Universal_Real;
4413
               else
4414
                  T2 := B_Typ;
4415
               end if;
4416
 
4417
               Set_Analyzed (Op2, False);
4418
               Resolve (Op2, T2);
4419
            end;
4420
 
4421
         else
4422
            Resolve (N);
4423
         end if;
4424
      end Set_Mixed_Mode_Operand;
4425
 
4426
      ----------------------
4427
      -- Set_Operand_Type --
4428
      ----------------------
4429
 
4430
      procedure Set_Operand_Type (N : Node_Id) is
4431
      begin
4432
         if Etype (N) = Universal_Integer
4433
           or else Etype (N) = Universal_Real
4434
         then
4435
            Set_Etype (N, T);
4436
         end if;
4437
      end Set_Operand_Type;
4438
 
4439
   --  Start of processing for Resolve_Arithmetic_Op
4440
 
4441
   begin
4442
      if Comes_From_Source (N)
4443
        and then Ekind (Entity (N)) = E_Function
4444
        and then Is_Imported (Entity (N))
4445
        and then Is_Intrinsic_Subprogram (Entity (N))
4446
      then
4447
         Resolve_Intrinsic_Operator (N, Typ);
4448
         return;
4449
 
4450
      --  Special-case for mixed-mode universal expressions or fixed point
4451
      --  type operation: each argument is resolved separately. The same
4452
      --  treatment is required if one of the operands of a fixed point
4453
      --  operation is universal real, since in this case we don't do a
4454
      --  conversion to a specific fixed-point type (instead the expander
4455
      --  takes care of the case).
4456
 
4457
      elsif (B_Typ = Universal_Integer or else B_Typ = Universal_Real)
4458
        and then Present (Universal_Interpretation (L))
4459
        and then Present (Universal_Interpretation (R))
4460
      then
4461
         Resolve (L, Universal_Interpretation (L));
4462
         Resolve (R, Universal_Interpretation (R));
4463
         Set_Etype (N, B_Typ);
4464
 
4465
      elsif (B_Typ = Universal_Real
4466
              or else Etype (N) = Universal_Fixed
4467
              or else (Etype (N) = Any_Fixed
4468
                        and then Is_Fixed_Point_Type (B_Typ))
4469
              or else (Is_Fixed_Point_Type (B_Typ)
4470
                        and then (Is_Integer_Or_Universal (L)
4471
                                   or else
4472
                                  Is_Integer_Or_Universal (R))))
4473
        and then Nkind_In (N, N_Op_Multiply, N_Op_Divide)
4474
      then
4475
         if TL = Universal_Integer or else TR = Universal_Integer then
4476
            Check_For_Visible_Operator (N, B_Typ);
4477
         end if;
4478
 
4479
         --  If context is a fixed type and one operand is integer, the
4480
         --  other is resolved with the type of the context.
4481
 
4482
         if Is_Fixed_Point_Type (B_Typ)
4483
           and then (Base_Type (TL) = Base_Type (Standard_Integer)
4484
                      or else TL = Universal_Integer)
4485
         then
4486
            Resolve (R, B_Typ);
4487
            Resolve (L, TL);
4488
 
4489
         elsif Is_Fixed_Point_Type (B_Typ)
4490
           and then (Base_Type (TR) = Base_Type (Standard_Integer)
4491
                      or else TR = Universal_Integer)
4492
         then
4493
            Resolve (L, B_Typ);
4494
            Resolve (R, TR);
4495
 
4496
         else
4497
            Set_Mixed_Mode_Operand (L, TR);
4498
            Set_Mixed_Mode_Operand (R, TL);
4499
         end if;
4500
 
4501
         --  Check the rule in RM05-4.5.5(19.1/2) disallowing universal_fixed
4502
         --  multiplying operators from being used when the expected type is
4503
         --  also universal_fixed. Note that B_Typ will be Universal_Fixed in
4504
         --  some cases where the expected type is actually Any_Real;
4505
         --  Expected_Type_Is_Any_Real takes care of that case.
4506
 
4507
         if Etype (N) = Universal_Fixed
4508
           or else Etype (N) = Any_Fixed
4509
         then
4510
            if B_Typ = Universal_Fixed
4511
              and then not Expected_Type_Is_Any_Real (N)
4512
              and then not Nkind_In (Parent (N), N_Type_Conversion,
4513
                                                 N_Unchecked_Type_Conversion)
4514
            then
4515
               Error_Msg_N ("type cannot be determined from context!", N);
4516
               Error_Msg_N ("\explicit conversion to result type required", N);
4517
 
4518
               Set_Etype (L, Any_Type);
4519
               Set_Etype (R, Any_Type);
4520
 
4521
            else
4522
               if Ada_Version = Ada_83
4523
                 and then Etype (N) = Universal_Fixed
4524
                 and then not
4525
                   Nkind_In (Parent (N), N_Type_Conversion,
4526
                                         N_Unchecked_Type_Conversion)
4527
               then
4528
                  Error_Msg_N
4529
                    ("(Ada 83) fixed-point operation "
4530
                     & "needs explicit conversion", N);
4531
               end if;
4532
 
4533
               --  The expected type is "any real type" in contexts like
4534
               --    type T is delta <universal_fixed-expression> ...
4535
               --  in which case we need to set the type to Universal_Real
4536
               --  so that static expression evaluation will work properly.
4537
 
4538
               if Expected_Type_Is_Any_Real (N) then
4539
                  Set_Etype (N, Universal_Real);
4540
               else
4541
                  Set_Etype (N, B_Typ);
4542
               end if;
4543
            end if;
4544
 
4545
         elsif Is_Fixed_Point_Type (B_Typ)
4546
           and then (Is_Integer_Or_Universal (L)
4547
                       or else Nkind (L) = N_Real_Literal
4548
                       or else Nkind (R) = N_Real_Literal
4549
                       or else Is_Integer_Or_Universal (R))
4550
         then
4551
            Set_Etype (N, B_Typ);
4552
 
4553
         elsif Etype (N) = Any_Fixed then
4554
 
4555
            --  If no previous errors, this is only possible if one operand
4556
            --  is overloaded and the context is universal. Resolve as such.
4557
 
4558
            Set_Etype (N, B_Typ);
4559
         end if;
4560
 
4561
      else
4562
         if (TL = Universal_Integer or else TL = Universal_Real)
4563
              and then
4564
            (TR = Universal_Integer or else TR = Universal_Real)
4565
         then
4566
            Check_For_Visible_Operator (N, B_Typ);
4567
         end if;
4568
 
4569
         --  If the context is Universal_Fixed and the operands are also
4570
         --  universal fixed, this is an error, unless there is only one
4571
         --  applicable fixed_point type (usually duration).
4572
 
4573
         if B_Typ = Universal_Fixed and then Etype (L) = Universal_Fixed then
4574
            T := Unique_Fixed_Point_Type (N);
4575
 
4576
            if T  = Any_Type then
4577
               Set_Etype (N, T);
4578
               return;
4579
            else
4580
               Resolve (L, T);
4581
               Resolve (R, T);
4582
            end if;
4583
 
4584
         else
4585
            Resolve (L, B_Typ);
4586
            Resolve (R, B_Typ);
4587
         end if;
4588
 
4589
         --  If one of the arguments was resolved to a non-universal type.
4590
         --  label the result of the operation itself with the same type.
4591
         --  Do the same for the universal argument, if any.
4592
 
4593
         T := Intersect_Types (L, R);
4594
         Set_Etype (N, Base_Type (T));
4595
         Set_Operand_Type (L);
4596
         Set_Operand_Type (R);
4597
      end if;
4598
 
4599
      Generate_Operator_Reference (N, Typ);
4600
      Eval_Arithmetic_Op (N);
4601
 
4602
      --  Set overflow and division checking bit. Much cleverer code needed
4603
      --  here eventually and perhaps the Resolve routines should be separated
4604
      --  for the various arithmetic operations, since they will need
4605
      --  different processing. ???
4606
 
4607
      if Nkind (N) in N_Op then
4608
         if not Overflow_Checks_Suppressed (Etype (N)) then
4609
            Enable_Overflow_Check (N);
4610
         end if;
4611
 
4612
         --  Give warning if explicit division by zero
4613
 
4614
         if Nkind_In (N, N_Op_Divide, N_Op_Rem, N_Op_Mod)
4615
           and then not Division_Checks_Suppressed (Etype (N))
4616
         then
4617
            Rop := Right_Opnd (N);
4618
 
4619
            if Compile_Time_Known_Value (Rop)
4620
              and then ((Is_Integer_Type (Etype (Rop))
4621
                           and then Expr_Value (Rop) = Uint_0)
4622
                          or else
4623
                        (Is_Real_Type (Etype (Rop))
4624
                           and then Expr_Value_R (Rop) = Ureal_0))
4625
            then
4626
               --  Specialize the warning message according to the operation
4627
 
4628
               case Nkind (N) is
4629
                  when N_Op_Divide =>
4630
                     Apply_Compile_Time_Constraint_Error
4631
                       (N, "division by zero?", CE_Divide_By_Zero,
4632
                        Loc => Sloc (Right_Opnd (N)));
4633
 
4634
                  when N_Op_Rem =>
4635
                     Apply_Compile_Time_Constraint_Error
4636
                       (N, "rem with zero divisor?", CE_Divide_By_Zero,
4637
                        Loc => Sloc (Right_Opnd (N)));
4638
 
4639
                  when N_Op_Mod =>
4640
                     Apply_Compile_Time_Constraint_Error
4641
                       (N, "mod with zero divisor?", CE_Divide_By_Zero,
4642
                        Loc => Sloc (Right_Opnd (N)));
4643
 
4644
                  --  Division by zero can only happen with division, rem,
4645
                  --  and mod operations.
4646
 
4647
                  when others =>
4648
                     raise Program_Error;
4649
               end case;
4650
 
4651
            --  Otherwise just set the flag to check at run time
4652
 
4653
            else
4654
               Activate_Division_Check (N);
4655
            end if;
4656
         end if;
4657
 
4658
         --  If Restriction No_Implicit_Conditionals is active, then it is
4659
         --  violated if either operand can be negative for mod, or for rem
4660
         --  if both operands can be negative.
4661
 
4662
         if Restrictions.Set (No_Implicit_Conditionals)
4663
           and then Nkind_In (N, N_Op_Rem, N_Op_Mod)
4664
         then
4665
            declare
4666
               Lo : Uint;
4667
               Hi : Uint;
4668
               OK : Boolean;
4669
 
4670
               LNeg : Boolean;
4671
               RNeg : Boolean;
4672
               --  Set if corresponding operand might be negative
4673
 
4674
            begin
4675
               Determine_Range
4676
                 (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
4677
               LNeg := (not OK) or else Lo < 0;
4678
 
4679
               Determine_Range
4680
                 (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True);
4681
               RNeg := (not OK) or else Lo < 0;
4682
 
4683
               --  Check if we will be generating conditionals. There are two
4684
               --  cases where that can happen, first for REM, the only case
4685
               --  is largest negative integer mod -1, where the division can
4686
               --  overflow, but we still have to give the right result. The
4687
               --  front end generates a test for this annoying case. Here we
4688
               --  just test if both operands can be negative (that's what the
4689
               --  expander does, so we match its logic here).
4690
 
4691
               --  The second case is mod where either operand can be negative.
4692
               --  In this case, the back end has to generate additonal tests.
4693
 
4694
               if (Nkind (N) = N_Op_Rem and then (LNeg and RNeg))
4695
                    or else
4696
                  (Nkind (N) = N_Op_Mod and then (LNeg or RNeg))
4697
               then
4698
                  Check_Restriction (No_Implicit_Conditionals, N);
4699
               end if;
4700
            end;
4701
         end if;
4702
      end if;
4703
 
4704
      Check_Unset_Reference (L);
4705
      Check_Unset_Reference (R);
4706
   end Resolve_Arithmetic_Op;
4707
 
4708
   ------------------
4709
   -- Resolve_Call --
4710
   ------------------
4711
 
4712
   procedure Resolve_Call (N : Node_Id; Typ : Entity_Id) is
4713
      Loc     : constant Source_Ptr := Sloc (N);
4714
      Subp    : constant Node_Id    := Name (N);
4715
      Nam     : Entity_Id;
4716
      I       : Interp_Index;
4717
      It      : Interp;
4718
      Norm_OK : Boolean;
4719
      Scop    : Entity_Id;
4720
      Rtype   : Entity_Id;
4721
 
4722
   begin
4723
      --  The context imposes a unique interpretation with type Typ on a
4724
      --  procedure or function call. Find the entity of the subprogram that
4725
      --  yields the expected type, and propagate the corresponding formal
4726
      --  constraints on the actuals. The caller has established that an
4727
      --  interpretation exists, and emitted an error if not unique.
4728
 
4729
      --  First deal with the case of a call to an access-to-subprogram,
4730
      --  dereference made explicit in Analyze_Call.
4731
 
4732
      if Ekind (Etype (Subp)) = E_Subprogram_Type then
4733
         if not Is_Overloaded (Subp) then
4734
            Nam := Etype (Subp);
4735
 
4736
         else
4737
            --  Find the interpretation whose type (a subprogram type) has a
4738
            --  return type that is compatible with the context. Analysis of
4739
            --  the node has established that one exists.
4740
 
4741
            Nam := Empty;
4742
 
4743
            Get_First_Interp (Subp,  I, It);
4744
            while Present (It.Typ) loop
4745
               if Covers (Typ, Etype (It.Typ)) then
4746
                  Nam := It.Typ;
4747
                  exit;
4748
               end if;
4749
 
4750
               Get_Next_Interp (I, It);
4751
            end loop;
4752
 
4753
            if No (Nam) then
4754
               raise Program_Error;
4755
            end if;
4756
         end if;
4757
 
4758
         --  If the prefix is not an entity, then resolve it
4759
 
4760
         if not Is_Entity_Name (Subp) then
4761
            Resolve (Subp, Nam);
4762
         end if;
4763
 
4764
         --  For an indirect call, we always invalidate checks, since we do not
4765
         --  know whether the subprogram is local or global. Yes we could do
4766
         --  better here, e.g. by knowing that there are no local subprograms,
4767
         --  but it does not seem worth the effort. Similarly, we kill all
4768
         --  knowledge of current constant values.
4769
 
4770
         Kill_Current_Values;
4771
 
4772
      --  If this is a procedure call which is really an entry call, do
4773
      --  the conversion of the procedure call to an entry call. Protected
4774
      --  operations use the same circuitry because the name in the call
4775
      --  can be an arbitrary expression with special resolution rules.
4776
 
4777
      elsif Nkind_In (Subp, N_Selected_Component, N_Indexed_Component)
4778
        or else (Is_Entity_Name (Subp)
4779
                  and then Ekind (Entity (Subp)) = E_Entry)
4780
      then
4781
         Resolve_Entry_Call (N, Typ);
4782
         Check_Elab_Call (N);
4783
 
4784
         --  Kill checks and constant values, as above for indirect case
4785
         --  Who knows what happens when another task is activated?
4786
 
4787
         Kill_Current_Values;
4788
         return;
4789
 
4790
      --  Normal subprogram call with name established in Resolve
4791
 
4792
      elsif not (Is_Type (Entity (Subp))) then
4793
         Nam := Entity (Subp);
4794
         Set_Entity_With_Style_Check (Subp, Nam);
4795
 
4796
      --  Otherwise we must have the case of an overloaded call
4797
 
4798
      else
4799
         pragma Assert (Is_Overloaded (Subp));
4800
 
4801
         --  Initialize Nam to prevent warning (we know it will be assigned
4802
         --  in the loop below, but the compiler does not know that).
4803
 
4804
         Nam := Empty;
4805
 
4806
         Get_First_Interp (Subp,  I, It);
4807
         while Present (It.Typ) loop
4808
            if Covers (Typ, It.Typ) then
4809
               Nam := It.Nam;
4810
               Set_Entity_With_Style_Check (Subp, Nam);
4811
               exit;
4812
            end if;
4813
 
4814
            Get_Next_Interp (I, It);
4815
         end loop;
4816
      end if;
4817
 
4818
      if Is_Access_Subprogram_Type (Base_Type (Etype (Nam)))
4819
         and then not Is_Access_Subprogram_Type (Base_Type (Typ))
4820
         and then Nkind (Subp) /= N_Explicit_Dereference
4821
         and then Present (Parameter_Associations (N))
4822
      then
4823
         --  The prefix is a parameterless function call that returns an access
4824
         --  to subprogram. If parameters are present in the current call, add
4825
         --  add an explicit dereference. We use the base type here because
4826
         --  within an instance these may be subtypes.
4827
 
4828
         --  The dereference is added either in Analyze_Call or here. Should
4829
         --  be consolidated ???
4830
 
4831
         Set_Is_Overloaded (Subp, False);
4832
         Set_Etype (Subp, Etype (Nam));
4833
         Insert_Explicit_Dereference (Subp);
4834
         Nam := Designated_Type (Etype (Nam));
4835
         Resolve (Subp, Nam);
4836
      end if;
4837
 
4838
      --  Check that a call to Current_Task does not occur in an entry body
4839
 
4840
      if Is_RTE (Nam, RE_Current_Task) then
4841
         declare
4842
            P : Node_Id;
4843
 
4844
         begin
4845
            P := N;
4846
            loop
4847
               P := Parent (P);
4848
 
4849
               --  Exclude calls that occur within the default of a formal
4850
               --  parameter of the entry, since those are evaluated outside
4851
               --  of the body.
4852
 
4853
               exit when No (P) or else Nkind (P) = N_Parameter_Specification;
4854
 
4855
               if Nkind (P) = N_Entry_Body
4856
                 or else (Nkind (P) = N_Subprogram_Body
4857
                           and then Is_Entry_Barrier_Function (P))
4858
               then
4859
                  Rtype := Etype (N);
4860
                  Error_Msg_NE
4861
                    ("?& should not be used in entry body (RM C.7(17))",
4862
                     N, Nam);
4863
                  Error_Msg_NE
4864
                    ("\Program_Error will be raised at run time?", N, Nam);
4865
                  Rewrite (N,
4866
                    Make_Raise_Program_Error (Loc,
4867
                      Reason => PE_Current_Task_In_Entry_Body));
4868
                  Set_Etype (N, Rtype);
4869
                  return;
4870
               end if;
4871
            end loop;
4872
         end;
4873
      end if;
4874
 
4875
      --  Check that a procedure call does not occur in the context of the
4876
      --  entry call statement of a conditional or timed entry call. Note that
4877
      --  the case of a call to a subprogram renaming of an entry will also be
4878
      --  rejected. The test for N not being an N_Entry_Call_Statement is
4879
      --  defensive, covering the possibility that the processing of entry
4880
      --  calls might reach this point due to later modifications of the code
4881
      --  above.
4882
 
4883
      if Nkind (Parent (N)) = N_Entry_Call_Alternative
4884
        and then Nkind (N) /= N_Entry_Call_Statement
4885
        and then Entry_Call_Statement (Parent (N)) = N
4886
      then
4887
         if Ada_Version < Ada_05 then
4888
            Error_Msg_N ("entry call required in select statement", N);
4889
 
4890
         --  Ada 2005 (AI-345): If a procedure_call_statement is used
4891
         --  for a procedure_or_entry_call, the procedure_name or
4892
         --  procedure_prefix of the procedure_call_statement shall denote
4893
         --  an entry renamed by a procedure, or (a view of) a primitive
4894
         --  subprogram of a limited interface whose first parameter is
4895
         --  a controlling parameter.
4896
 
4897
         elsif Nkind (N) = N_Procedure_Call_Statement
4898
           and then not Is_Renamed_Entry (Nam)
4899
           and then not Is_Controlling_Limited_Procedure (Nam)
4900
         then
4901
            Error_Msg_N
4902
             ("entry call or dispatching primitive of interface required", N);
4903
         end if;
4904
      end if;
4905
 
4906
      --  Check that this is not a call to a protected procedure or entry from
4907
      --  within a protected function.
4908
 
4909
      if Ekind (Current_Scope) = E_Function
4910
        and then Ekind (Scope (Current_Scope)) = E_Protected_Type
4911
        and then Ekind (Nam) /= E_Function
4912
        and then Scope (Nam) = Scope (Current_Scope)
4913
      then
4914
         Error_Msg_N ("within protected function, protected " &
4915
           "object is constant", N);
4916
         Error_Msg_N ("\cannot call operation that may modify it", N);
4917
      end if;
4918
 
4919
      --  Freeze the subprogram name if not in a spec-expression. Note that we
4920
      --  freeze procedure calls as well as function calls. Procedure calls are
4921
      --  not frozen according to the rules (RM 13.14(14)) because it is
4922
      --  impossible to have a procedure call to a non-frozen procedure in pure
4923
      --  Ada, but in the code that we generate in the expander, this rule
4924
      --  needs extending because we can generate procedure calls that need
4925
      --  freezing.
4926
 
4927
      if Is_Entity_Name (Subp) and then not In_Spec_Expression then
4928
         Freeze_Expression (Subp);
4929
      end if;
4930
 
4931
      --  For a predefined operator, the type of the result is the type imposed
4932
      --  by context, except for a predefined operation on universal fixed.
4933
      --  Otherwise The type of the call is the type returned by the subprogram
4934
      --  being called.
4935
 
4936
      if Is_Predefined_Op (Nam) then
4937
         if Etype (N) /= Universal_Fixed then
4938
            Set_Etype (N, Typ);
4939
         end if;
4940
 
4941
      --  If the subprogram returns an array type, and the context requires the
4942
      --  component type of that array type, the node is really an indexing of
4943
      --  the parameterless call. Resolve as such. A pathological case occurs
4944
      --  when the type of the component is an access to the array type. In
4945
      --  this case the call is truly ambiguous.
4946
 
4947
      elsif (Needs_No_Actuals (Nam) or else Needs_One_Actual (Nam))
4948
        and then
4949
          ((Is_Array_Type (Etype (Nam))
4950
                   and then Covers (Typ, Component_Type (Etype (Nam))))
4951
             or else (Is_Access_Type (Etype (Nam))
4952
                        and then Is_Array_Type (Designated_Type (Etype (Nam)))
4953
                        and then
4954
                          Covers (Typ,
4955
                            Component_Type (Designated_Type (Etype (Nam))))))
4956
      then
4957
         declare
4958
            Index_Node : Node_Id;
4959
            New_Subp   : Node_Id;
4960
            Ret_Type   : constant Entity_Id := Etype (Nam);
4961
 
4962
         begin
4963
            if Is_Access_Type (Ret_Type)
4964
              and then Ret_Type = Component_Type (Designated_Type (Ret_Type))
4965
            then
4966
               Error_Msg_N
4967
                 ("cannot disambiguate function call and indexing", N);
4968
            else
4969
               New_Subp := Relocate_Node (Subp);
4970
               Set_Entity (Subp, Nam);
4971
 
4972
               if (Is_Array_Type (Ret_Type)
4973
                    and then Component_Type (Ret_Type) /= Any_Type)
4974
                 or else
4975
                  (Is_Access_Type (Ret_Type)
4976
                    and then
4977
                      Component_Type (Designated_Type (Ret_Type)) /= Any_Type)
4978
               then
4979
                  if Needs_No_Actuals (Nam) then
4980
 
4981
                     --  Indexed call to a parameterless function
4982
 
4983
                     Index_Node :=
4984
                       Make_Indexed_Component (Loc,
4985
                         Prefix =>
4986
                           Make_Function_Call (Loc,
4987
                             Name => New_Subp),
4988
                         Expressions => Parameter_Associations (N));
4989
                  else
4990
                     --  An Ada 2005 prefixed call to a primitive operation
4991
                     --  whose first parameter is the prefix. This prefix was
4992
                     --  prepended to the parameter list, which is actually a
4993
                     --  list of indices. Remove the prefix in order to build
4994
                     --  the proper indexed component.
4995
 
4996
                     Index_Node :=
4997
                        Make_Indexed_Component (Loc,
4998
                          Prefix =>
4999
                            Make_Function_Call (Loc,
5000
                               Name => New_Subp,
5001
                               Parameter_Associations =>
5002
                                 New_List
5003
                                   (Remove_Head (Parameter_Associations (N)))),
5004
                           Expressions => Parameter_Associations (N));
5005
                  end if;
5006
 
5007
                  --  Since we are correcting a node classification error made
5008
                  --  by the parser, we call Replace rather than Rewrite.
5009
 
5010
                  Replace (N, Index_Node);
5011
                  Set_Etype (Prefix (N), Ret_Type);
5012
                  Set_Etype (N, Typ);
5013
                  Resolve_Indexed_Component (N, Typ);
5014
                  Check_Elab_Call (Prefix (N));
5015
               end if;
5016
            end if;
5017
 
5018
            return;
5019
         end;
5020
 
5021
      else
5022
         Set_Etype (N, Etype (Nam));
5023
      end if;
5024
 
5025
      --  In the case where the call is to an overloaded subprogram, Analyze
5026
      --  calls Normalize_Actuals once per overloaded subprogram. Therefore in
5027
      --  such a case Normalize_Actuals needs to be called once more to order
5028
      --  the actuals correctly. Otherwise the call will have the ordering
5029
      --  given by the last overloaded subprogram whether this is the correct
5030
      --  one being called or not.
5031
 
5032
      if Is_Overloaded (Subp) then
5033
         Normalize_Actuals (N, Nam, False, Norm_OK);
5034
         pragma Assert (Norm_OK);
5035
      end if;
5036
 
5037
      --  In any case, call is fully resolved now. Reset Overload flag, to
5038
      --  prevent subsequent overload resolution if node is analyzed again
5039
 
5040
      Set_Is_Overloaded (Subp, False);
5041
      Set_Is_Overloaded (N, False);
5042
 
5043
      --  If we are calling the current subprogram from immediately within its
5044
      --  body, then that is the case where we can sometimes detect cases of
5045
      --  infinite recursion statically. Do not try this in case restriction
5046
      --  No_Recursion is in effect anyway, and do it only for source calls.
5047
 
5048
      if Comes_From_Source (N) then
5049
         Scop := Current_Scope;
5050
 
5051
         --  Issue warning for possible infinite recursion in the absence
5052
         --  of the No_Recursion restriction.
5053
 
5054
         if Nam = Scop
5055
           and then not Restriction_Active (No_Recursion)
5056
           and then Check_Infinite_Recursion (N)
5057
         then
5058
            --  Here we detected and flagged an infinite recursion, so we do
5059
            --  not need to test the case below for further warnings. Also if
5060
            --  we now have a raise SE node, we are all done.
5061
 
5062
            if Nkind (N) = N_Raise_Storage_Error then
5063
               return;
5064
            end if;
5065
 
5066
         --  If call is to immediately containing subprogram, then check for
5067
         --  the case of a possible run-time detectable infinite recursion.
5068
 
5069
         else
5070
            Scope_Loop : while Scop /= Standard_Standard loop
5071
               if Nam = Scop then
5072
 
5073
                  --  Although in general case, recursion is not statically
5074
                  --  checkable, the case of calling an immediately containing
5075
                  --  subprogram is easy to catch.
5076
 
5077
                  Check_Restriction (No_Recursion, N);
5078
 
5079
                  --  If the recursive call is to a parameterless subprogram,
5080
                  --  then even if we can't statically detect infinite
5081
                  --  recursion, this is pretty suspicious, and we output a
5082
                  --  warning. Furthermore, we will try later to detect some
5083
                  --  cases here at run time by expanding checking code (see
5084
                  --  Detect_Infinite_Recursion in package Exp_Ch6).
5085
 
5086
                  --  If the recursive call is within a handler, do not emit a
5087
                  --  warning, because this is a common idiom: loop until input
5088
                  --  is correct, catch illegal input in handler and restart.
5089
 
5090
                  if No (First_Formal (Nam))
5091
                    and then Etype (Nam) = Standard_Void_Type
5092
                    and then not Error_Posted (N)
5093
                    and then Nkind (Parent (N)) /= N_Exception_Handler
5094
                  then
5095
                     --  For the case of a procedure call. We give the message
5096
                     --  only if the call is the first statement in a sequence
5097
                     --  of statements, or if all previous statements are
5098
                     --  simple assignments. This is simply a heuristic to
5099
                     --  decrease false positives, without losing too many good
5100
                     --  warnings. The idea is that these previous statements
5101
                     --  may affect global variables the procedure depends on.
5102
 
5103
                     if Nkind (N) = N_Procedure_Call_Statement
5104
                       and then Is_List_Member (N)
5105
                     then
5106
                        declare
5107
                           P : Node_Id;
5108
                        begin
5109
                           P := Prev (N);
5110
                           while Present (P) loop
5111
                              if Nkind (P) /= N_Assignment_Statement then
5112
                                 exit Scope_Loop;
5113
                              end if;
5114
 
5115
                              Prev (P);
5116
                           end loop;
5117
                        end;
5118
                     end if;
5119
 
5120
                     --  Do not give warning if we are in a conditional context
5121
 
5122
                     declare
5123
                        K : constant Node_Kind := Nkind (Parent (N));
5124
                     begin
5125
                        if (K = N_Loop_Statement
5126
                            and then Present (Iteration_Scheme (Parent (N))))
5127
                          or else K = N_If_Statement
5128
                          or else K = N_Elsif_Part
5129
                          or else K = N_Case_Statement_Alternative
5130
                        then
5131
                           exit Scope_Loop;
5132
                        end if;
5133
                     end;
5134
 
5135
                     --  Here warning is to be issued
5136
 
5137
                     Set_Has_Recursive_Call (Nam);
5138
                     Error_Msg_N
5139
                       ("?possible infinite recursion!", N);
5140
                     Error_Msg_N
5141
                       ("\?Storage_Error may be raised at run time!", N);
5142
                  end if;
5143
 
5144
                  exit Scope_Loop;
5145
               end if;
5146
 
5147
               Scop := Scope (Scop);
5148
            end loop Scope_Loop;
5149
         end if;
5150
      end if;
5151
 
5152
      --  If subprogram name is a predefined operator, it was given in
5153
      --  functional notation. Replace call node with operator node, so
5154
      --  that actuals can be resolved appropriately.
5155
 
5156
      if Is_Predefined_Op (Nam) or else Ekind (Nam) = E_Operator then
5157
         Make_Call_Into_Operator (N, Typ, Entity (Name (N)));
5158
         return;
5159
 
5160
      elsif Present (Alias (Nam))
5161
        and then Is_Predefined_Op (Alias (Nam))
5162
      then
5163
         Resolve_Actuals (N, Nam);
5164
         Make_Call_Into_Operator (N, Typ, Alias (Nam));
5165
         return;
5166
      end if;
5167
 
5168
      --  Create a transient scope if the resulting type requires it
5169
 
5170
      --  There are several notable exceptions:
5171
 
5172
      --  a) In init procs, the transient scope overhead is not needed, and is
5173
      --  even incorrect when the call is a nested initialization call for a
5174
      --  component whose expansion may generate adjust calls. However, if the
5175
      --  call is some other procedure call within an initialization procedure
5176
      --  (for example a call to Create_Task in the init_proc of the task
5177
      --  run-time record) a transient scope must be created around this call.
5178
 
5179
      --  b) Enumeration literal pseudo-calls need no transient scope
5180
 
5181
      --  c) Intrinsic subprograms (Unchecked_Conversion and source info
5182
      --  functions) do not use the secondary stack even though the return
5183
      --  type may be unconstrained.
5184
 
5185
      --  d) Calls to a build-in-place function, since such functions may
5186
      --  allocate their result directly in a target object, and cases where
5187
      --  the result does get allocated in the secondary stack are checked for
5188
      --  within the specialized Exp_Ch6 procedures for expanding those
5189
      --  build-in-place calls.
5190
 
5191
      --  e) If the subprogram is marked Inline_Always, then even if it returns
5192
      --  an unconstrained type the call does not require use of the secondary
5193
      --  stack. However, inlining will only take place if the body to inline
5194
      --  is already present. It may not be available if e.g. the subprogram is
5195
      --  declared in a child instance.
5196
 
5197
      --  If this is an initialization call for a type whose construction
5198
      --  uses the secondary stack, and it is not a nested call to initialize
5199
      --  a component, we do need to create a transient scope for it. We
5200
      --  check for this by traversing the type in Check_Initialization_Call.
5201
 
5202
      if Is_Inlined (Nam)
5203
        and then Has_Pragma_Inline_Always (Nam)
5204
        and then Nkind (Unit_Declaration_Node (Nam)) = N_Subprogram_Declaration
5205
        and then Present (Body_To_Inline (Unit_Declaration_Node (Nam)))
5206
      then
5207
         null;
5208
 
5209
      elsif Ekind (Nam) = E_Enumeration_Literal
5210
        or else Is_Build_In_Place_Function (Nam)
5211
        or else Is_Intrinsic_Subprogram (Nam)
5212
      then
5213
         null;
5214
 
5215
      elsif Expander_Active
5216
        and then Is_Type (Etype (Nam))
5217
        and then Requires_Transient_Scope (Etype (Nam))
5218
        and then
5219
          (not Within_Init_Proc
5220
            or else
5221
              (not Is_Init_Proc (Nam) and then Ekind (Nam) /= E_Function))
5222
      then
5223
         Establish_Transient_Scope (N, Sec_Stack => True);
5224
 
5225
         --  If the call appears within the bounds of a loop, it will
5226
         --  be rewritten and reanalyzed, nothing left to do here.
5227
 
5228
         if Nkind (N) /= N_Function_Call then
5229
            return;
5230
         end if;
5231
 
5232
      elsif Is_Init_Proc (Nam)
5233
        and then not Within_Init_Proc
5234
      then
5235
         Check_Initialization_Call (N, Nam);
5236
      end if;
5237
 
5238
      --  A protected function cannot be called within the definition of the
5239
      --  enclosing protected type.
5240
 
5241
      if Is_Protected_Type (Scope (Nam))
5242
        and then In_Open_Scopes (Scope (Nam))
5243
        and then not Has_Completion (Scope (Nam))
5244
      then
5245
         Error_Msg_NE
5246
           ("& cannot be called before end of protected definition", N, Nam);
5247
      end if;
5248
 
5249
      --  Propagate interpretation to actuals, and add default expressions
5250
      --  where needed.
5251
 
5252
      if Present (First_Formal (Nam)) then
5253
         Resolve_Actuals (N, Nam);
5254
 
5255
      --  Overloaded literals are rewritten as function calls, for purpose of
5256
      --  resolution. After resolution, we can replace the call with the
5257
      --  literal itself.
5258
 
5259
      elsif Ekind (Nam) = E_Enumeration_Literal then
5260
         Copy_Node (Subp, N);
5261
         Resolve_Entity_Name (N, Typ);
5262
 
5263
         --  Avoid validation, since it is a static function call
5264
 
5265
         Generate_Reference (Nam, Subp);
5266
         return;
5267
      end if;
5268
 
5269
      --  If the subprogram is not global, then kill all saved values and
5270
      --  checks. This is a bit conservative, since in many cases we could do
5271
      --  better, but it is not worth the effort. Similarly, we kill constant
5272
      --  values. However we do not need to do this for internal entities
5273
      --  (unless they are inherited user-defined subprograms), since they
5274
      --  are not in the business of molesting local values.
5275
 
5276
      --  If the flag Suppress_Value_Tracking_On_Calls is set, then we also
5277
      --  kill all checks and values for calls to global subprograms. This
5278
      --  takes care of the case where an access to a local subprogram is
5279
      --  taken, and could be passed directly or indirectly and then called
5280
      --  from almost any context.
5281
 
5282
      --  Note: we do not do this step till after resolving the actuals. That
5283
      --  way we still take advantage of the current value information while
5284
      --  scanning the actuals.
5285
 
5286
      --  We suppress killing values if we are processing the nodes associated
5287
      --  with N_Freeze_Entity nodes. Otherwise the declaration of a tagged
5288
      --  type kills all the values as part of analyzing the code that
5289
      --  initializes the dispatch tables.
5290
 
5291
      if Inside_Freezing_Actions = 0
5292
        and then (not Is_Library_Level_Entity (Nam)
5293
                   or else Suppress_Value_Tracking_On_Call
5294
                             (Nearest_Dynamic_Scope (Current_Scope)))
5295
        and then (Comes_From_Source (Nam)
5296
                   or else (Present (Alias (Nam))
5297
                             and then Comes_From_Source (Alias (Nam))))
5298
      then
5299
         Kill_Current_Values;
5300
      end if;
5301
 
5302
      --  If we are warning about unread OUT parameters, this is the place to
5303
      --  set Last_Assignment for OUT and IN OUT parameters. We have to do this
5304
      --  after the above call to Kill_Current_Values (since that call clears
5305
      --  the Last_Assignment field of all local variables).
5306
 
5307
      if (Warn_On_Modified_Unread or Warn_On_All_Unread_Out_Parameters)
5308
        and then Comes_From_Source (N)
5309
        and then In_Extended_Main_Source_Unit (N)
5310
      then
5311
         declare
5312
            F : Entity_Id;
5313
            A : Node_Id;
5314
 
5315
         begin
5316
            F := First_Formal (Nam);
5317
            A := First_Actual (N);
5318
            while Present (F) and then Present (A) loop
5319
               if (Ekind (F) = E_Out_Parameter
5320
                     or else
5321
                   Ekind (F) = E_In_Out_Parameter)
5322
                 and then Warn_On_Modified_As_Out_Parameter (F)
5323
                 and then Is_Entity_Name (A)
5324
                 and then Present (Entity (A))
5325
                 and then Comes_From_Source (N)
5326
                 and then Safe_To_Capture_Value (N, Entity (A))
5327
               then
5328
                  Set_Last_Assignment (Entity (A), A);
5329
               end if;
5330
 
5331
               Next_Formal (F);
5332
               Next_Actual (A);
5333
            end loop;
5334
         end;
5335
      end if;
5336
 
5337
      --  If the subprogram is a primitive operation, check whether or not
5338
      --  it is a correct dispatching call.
5339
 
5340
      if Is_Overloadable (Nam)
5341
        and then Is_Dispatching_Operation (Nam)
5342
      then
5343
         Check_Dispatching_Call (N);
5344
 
5345
      elsif Ekind (Nam) /= E_Subprogram_Type
5346
        and then Is_Abstract_Subprogram (Nam)
5347
        and then not In_Instance
5348
      then
5349
         Error_Msg_NE ("cannot call abstract subprogram &!", N, Nam);
5350
      end if;
5351
 
5352
      --  If this is a dispatching call, generate the appropriate reference,
5353
      --  for better source navigation in GPS.
5354
 
5355
      if Is_Overloadable (Nam)
5356
        and then Present (Controlling_Argument (N))
5357
      then
5358
         Generate_Reference (Nam, Subp, 'R');
5359
 
5360
      --  Normal case, not a dispatching call
5361
 
5362
      else
5363
         Generate_Reference (Nam, Subp);
5364
      end if;
5365
 
5366
      if Is_Intrinsic_Subprogram (Nam) then
5367
         Check_Intrinsic_Call (N);
5368
      end if;
5369
 
5370
      --  Check for violation of restriction No_Specific_Termination_Handlers
5371
      --  and warn on a potentially blocking call to Abort_Task.
5372
 
5373
      if Is_RTE (Nam, RE_Set_Specific_Handler)
5374
           or else
5375
         Is_RTE (Nam, RE_Specific_Handler)
5376
      then
5377
         Check_Restriction (No_Specific_Termination_Handlers, N);
5378
 
5379
      elsif Is_RTE (Nam, RE_Abort_Task) then
5380
         Check_Potentially_Blocking_Operation (N);
5381
      end if;
5382
 
5383
      --  Issue an error for a call to an eliminated subprogram
5384
 
5385
      Check_For_Eliminated_Subprogram (Subp, Nam);
5386
 
5387
      --  All done, evaluate call and deal with elaboration issues
5388
 
5389
      Eval_Call (N);
5390
      Check_Elab_Call (N);
5391
      Warn_On_Overlapping_Actuals (Nam, N);
5392
   end Resolve_Call;
5393
 
5394
   -------------------------------
5395
   -- Resolve_Character_Literal --
5396
   -------------------------------
5397
 
5398
   procedure Resolve_Character_Literal (N : Node_Id; Typ : Entity_Id) is
5399
      B_Typ : constant Entity_Id := Base_Type (Typ);
5400
      C     : Entity_Id;
5401
 
5402
   begin
5403
      --  Verify that the character does belong to the type of the context
5404
 
5405
      Set_Etype (N, B_Typ);
5406
      Eval_Character_Literal (N);
5407
 
5408
      --  Wide_Wide_Character literals must always be defined, since the set
5409
      --  of wide wide character literals is complete, i.e. if a character
5410
      --  literal is accepted by the parser, then it is OK for wide wide
5411
      --  character (out of range character literals are rejected).
5412
 
5413
      if Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5414
         return;
5415
 
5416
      --  Always accept character literal for type Any_Character, which
5417
      --  occurs in error situations and in comparisons of literals, both
5418
      --  of which should accept all literals.
5419
 
5420
      elsif B_Typ = Any_Character then
5421
         return;
5422
 
5423
      --  For Standard.Character or a type derived from it, check that
5424
      --  the literal is in range
5425
 
5426
      elsif Root_Type (B_Typ) = Standard_Character then
5427
         if In_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5428
            return;
5429
         end if;
5430
 
5431
      --  For Standard.Wide_Character or a type derived from it, check
5432
      --  that the literal is in range
5433
 
5434
      elsif Root_Type (B_Typ) = Standard_Wide_Character then
5435
         if In_Wide_Character_Range (UI_To_CC (Char_Literal_Value (N))) then
5436
            return;
5437
         end if;
5438
 
5439
      --  For Standard.Wide_Wide_Character or a type derived from it, we
5440
      --  know the literal is in range, since the parser checked!
5441
 
5442
      elsif Root_Type (B_Typ) = Standard_Wide_Wide_Character then
5443
         return;
5444
 
5445
      --  If the entity is already set, this has already been resolved in a
5446
      --  generic context, or comes from expansion. Nothing else to do.
5447
 
5448
      elsif Present (Entity (N)) then
5449
         return;
5450
 
5451
      --  Otherwise we have a user defined character type, and we can use the
5452
      --  standard visibility mechanisms to locate the referenced entity.
5453
 
5454
      else
5455
         C := Current_Entity (N);
5456
         while Present (C) loop
5457
            if Etype (C) = B_Typ then
5458
               Set_Entity_With_Style_Check (N, C);
5459
               Generate_Reference (C, N);
5460
               return;
5461
            end if;
5462
 
5463
            C := Homonym (C);
5464
         end loop;
5465
      end if;
5466
 
5467
      --  If we fall through, then the literal does not match any of the
5468
      --  entries of the enumeration type. This isn't just a constraint
5469
      --  error situation, it is an illegality (see RM 4.2).
5470
 
5471
      Error_Msg_NE
5472
        ("character not defined for }", N, First_Subtype (B_Typ));
5473
   end Resolve_Character_Literal;
5474
 
5475
   ---------------------------
5476
   -- Resolve_Comparison_Op --
5477
   ---------------------------
5478
 
5479
   --  Context requires a boolean type, and plays no role in resolution.
5480
   --  Processing identical to that for equality operators. The result
5481
   --  type is the base type, which matters when pathological subtypes of
5482
   --  booleans with limited ranges are used.
5483
 
5484
   procedure Resolve_Comparison_Op (N : Node_Id; Typ : Entity_Id) is
5485
      L : constant Node_Id := Left_Opnd (N);
5486
      R : constant Node_Id := Right_Opnd (N);
5487
      T : Entity_Id;
5488
 
5489
   begin
5490
      --  If this is an intrinsic operation which is not predefined, use the
5491
      --  types of its declared arguments to resolve the possibly overloaded
5492
      --  operands. Otherwise the operands are unambiguous and specify the
5493
      --  expected type.
5494
 
5495
      if Scope (Entity (N)) /= Standard_Standard then
5496
         T := Etype (First_Entity (Entity (N)));
5497
 
5498
      else
5499
         T := Find_Unique_Type (L, R);
5500
 
5501
         if T = Any_Fixed then
5502
            T := Unique_Fixed_Point_Type (L);
5503
         end if;
5504
      end if;
5505
 
5506
      Set_Etype (N, Base_Type (Typ));
5507
      Generate_Reference (T, N, ' ');
5508
 
5509
      if T /= Any_Type then
5510
         if T = Any_String    or else
5511
            T = Any_Composite or else
5512
            T = Any_Character
5513
         then
5514
            if T = Any_Character then
5515
               Ambiguous_Character (L);
5516
            else
5517
               Error_Msg_N ("ambiguous operands for comparison", N);
5518
            end if;
5519
 
5520
            Set_Etype (N, Any_Type);
5521
            return;
5522
 
5523
         else
5524
            Resolve (L, T);
5525
            Resolve (R, T);
5526
            Check_Unset_Reference (L);
5527
            Check_Unset_Reference (R);
5528
            Generate_Operator_Reference (N, T);
5529
            Check_Low_Bound_Tested (N);
5530
            Eval_Relational_Op (N);
5531
         end if;
5532
      end if;
5533
   end Resolve_Comparison_Op;
5534
 
5535
   ------------------------------------
5536
   -- Resolve_Conditional_Expression --
5537
   ------------------------------------
5538
 
5539
   procedure Resolve_Conditional_Expression (N : Node_Id; Typ : Entity_Id) is
5540
      Condition : constant Node_Id := First (Expressions (N));
5541
      Then_Expr : constant Node_Id := Next (Condition);
5542
      Else_Expr : Node_Id := Next (Then_Expr);
5543
 
5544
   begin
5545
      Resolve (Condition, Any_Boolean);
5546
      Resolve (Then_Expr, Typ);
5547
 
5548
      --  If ELSE expression present, just resolve using the determined type
5549
 
5550
      if Present (Else_Expr) then
5551
         Resolve (Else_Expr, Typ);
5552
 
5553
      --  If no ELSE expression is present, root type must be Standard.Boolean
5554
      --  and we provide a Standard.True result converted to the appropriate
5555
      --  Boolean type (in case it is a derived boolean type).
5556
 
5557
      elsif Root_Type (Typ) = Standard_Boolean then
5558
         Else_Expr :=
5559
           Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N)));
5560
         Analyze_And_Resolve (Else_Expr, Typ);
5561
         Append_To (Expressions (N), Else_Expr);
5562
 
5563
      else
5564
         Error_Msg_N ("can only omit ELSE expression in Boolean case", N);
5565
         Append_To (Expressions (N), Error);
5566
      end if;
5567
 
5568
      Set_Etype (N, Typ);
5569
      Eval_Conditional_Expression (N);
5570
   end Resolve_Conditional_Expression;
5571
 
5572
   -----------------------------------------
5573
   -- Resolve_Discrete_Subtype_Indication --
5574
   -----------------------------------------
5575
 
5576
   procedure Resolve_Discrete_Subtype_Indication
5577
     (N   : Node_Id;
5578
      Typ : Entity_Id)
5579
   is
5580
      R : Node_Id;
5581
      S : Entity_Id;
5582
 
5583
   begin
5584
      Analyze (Subtype_Mark (N));
5585
      S := Entity (Subtype_Mark (N));
5586
 
5587
      if Nkind (Constraint (N)) /= N_Range_Constraint then
5588
         Error_Msg_N ("expect range constraint for discrete type", N);
5589
         Set_Etype (N, Any_Type);
5590
 
5591
      else
5592
         R := Range_Expression (Constraint (N));
5593
 
5594
         if R = Error then
5595
            return;
5596
         end if;
5597
 
5598
         Analyze (R);
5599
 
5600
         if Base_Type (S) /= Base_Type (Typ) then
5601
            Error_Msg_NE
5602
              ("expect subtype of }", N, First_Subtype (Typ));
5603
 
5604
            --  Rewrite the constraint as a range of Typ
5605
            --  to allow compilation to proceed further.
5606
 
5607
            Set_Etype (N, Typ);
5608
            Rewrite (Low_Bound (R),
5609
              Make_Attribute_Reference (Sloc (Low_Bound (R)),
5610
                Prefix =>         New_Occurrence_Of (Typ, Sloc (R)),
5611
                Attribute_Name => Name_First));
5612
            Rewrite (High_Bound (R),
5613
              Make_Attribute_Reference (Sloc (High_Bound (R)),
5614
                Prefix =>         New_Occurrence_Of (Typ, Sloc (R)),
5615
                Attribute_Name => Name_First));
5616
 
5617
         else
5618
            Resolve (R, Typ);
5619
            Set_Etype (N, Etype (R));
5620
 
5621
            --  Additionally, we must check that the bounds are compatible
5622
            --  with the given subtype, which might be different from the
5623
            --  type of the context.
5624
 
5625
            Apply_Range_Check (R, S);
5626
 
5627
            --  ??? If the above check statically detects a Constraint_Error
5628
            --  it replaces the offending bound(s) of the range R with a
5629
            --  Constraint_Error node. When the itype which uses these bounds
5630
            --  is frozen the resulting call to Duplicate_Subexpr generates
5631
            --  a new temporary for the bounds.
5632
 
5633
            --  Unfortunately there are other itypes that are also made depend
5634
            --  on these bounds, so when Duplicate_Subexpr is called they get
5635
            --  a forward reference to the newly created temporaries and Gigi
5636
            --  aborts on such forward references. This is probably sign of a
5637
            --  more fundamental problem somewhere else in either the order of
5638
            --  itype freezing or the way certain itypes are constructed.
5639
 
5640
            --  To get around this problem we call Remove_Side_Effects right
5641
            --  away if either bounds of R are a Constraint_Error.
5642
 
5643
            declare
5644
               L : constant Node_Id := Low_Bound (R);
5645
               H : constant Node_Id := High_Bound (R);
5646
 
5647
            begin
5648
               if Nkind (L) = N_Raise_Constraint_Error then
5649
                  Remove_Side_Effects (L);
5650
               end if;
5651
 
5652
               if Nkind (H) = N_Raise_Constraint_Error then
5653
                  Remove_Side_Effects (H);
5654
               end if;
5655
            end;
5656
 
5657
            Check_Unset_Reference (Low_Bound  (R));
5658
            Check_Unset_Reference (High_Bound (R));
5659
         end if;
5660
      end if;
5661
   end Resolve_Discrete_Subtype_Indication;
5662
 
5663
   -------------------------
5664
   -- Resolve_Entity_Name --
5665
   -------------------------
5666
 
5667
   --  Used to resolve identifiers and expanded names
5668
 
5669
   procedure Resolve_Entity_Name (N : Node_Id; Typ : Entity_Id) is
5670
      E : constant Entity_Id := Entity (N);
5671
 
5672
   begin
5673
      --  If garbage from errors, set to Any_Type and return
5674
 
5675
      if No (E) and then Total_Errors_Detected /= 0 then
5676
         Set_Etype (N, Any_Type);
5677
         return;
5678
      end if;
5679
 
5680
      --  Replace named numbers by corresponding literals. Note that this is
5681
      --  the one case where Resolve_Entity_Name must reset the Etype, since
5682
      --  it is currently marked as universal.
5683
 
5684
      if Ekind (E) = E_Named_Integer then
5685
         Set_Etype (N, Typ);
5686
         Eval_Named_Integer (N);
5687
 
5688
      elsif Ekind (E) = E_Named_Real then
5689
         Set_Etype (N, Typ);
5690
         Eval_Named_Real (N);
5691
 
5692
      --  Allow use of subtype only if it is a concurrent type where we are
5693
      --  currently inside the body. This will eventually be expanded into a
5694
      --  call to Self (for tasks) or _object (for protected objects). Any
5695
      --  other use of a subtype is invalid.
5696
 
5697
      elsif Is_Type (E) then
5698
         if Is_Concurrent_Type (E)
5699
           and then In_Open_Scopes (E)
5700
         then
5701
            null;
5702
         else
5703
            Error_Msg_N
5704
               ("invalid use of subtype mark in expression or call", N);
5705
         end if;
5706
 
5707
      --  Check discriminant use if entity is discriminant in current scope,
5708
      --  i.e. discriminant of record or concurrent type currently being
5709
      --  analyzed. Uses in corresponding body are unrestricted.
5710
 
5711
      elsif Ekind (E) = E_Discriminant
5712
        and then Scope (E) = Current_Scope
5713
        and then not Has_Completion (Current_Scope)
5714
      then
5715
         Check_Discriminant_Use (N);
5716
 
5717
      --  A parameterless generic function cannot appear in a context that
5718
      --  requires resolution.
5719
 
5720
      elsif Ekind (E) = E_Generic_Function then
5721
         Error_Msg_N ("illegal use of generic function", N);
5722
 
5723
      elsif Ekind (E) = E_Out_Parameter
5724
        and then Ada_Version = Ada_83
5725
        and then (Nkind (Parent (N)) in N_Op
5726
                    or else (Nkind (Parent (N)) = N_Assignment_Statement
5727
                              and then N = Expression (Parent (N)))
5728
                    or else Nkind (Parent (N)) = N_Explicit_Dereference)
5729
      then
5730
         Error_Msg_N ("(Ada 83) illegal reading of out parameter", N);
5731
 
5732
      --  In all other cases, just do the possible static evaluation
5733
 
5734
      else
5735
         --  A deferred constant that appears in an expression must have a
5736
         --  completion, unless it has been removed by in-place expansion of
5737
         --  an aggregate.
5738
 
5739
         if Ekind (E) = E_Constant
5740
           and then Comes_From_Source (E)
5741
           and then No (Constant_Value (E))
5742
           and then Is_Frozen (Etype (E))
5743
           and then not In_Spec_Expression
5744
           and then not Is_Imported (E)
5745
         then
5746
 
5747
            if No_Initialization (Parent (E))
5748
              or else (Present (Full_View (E))
5749
                        and then No_Initialization (Parent (Full_View (E))))
5750
            then
5751
               null;
5752
            else
5753
               Error_Msg_N (
5754
                 "deferred constant is frozen before completion", N);
5755
            end if;
5756
         end if;
5757
 
5758
         Eval_Entity_Name (N);
5759
      end if;
5760
   end Resolve_Entity_Name;
5761
 
5762
   -------------------
5763
   -- Resolve_Entry --
5764
   -------------------
5765
 
5766
   procedure Resolve_Entry (Entry_Name : Node_Id) is
5767
      Loc    : constant Source_Ptr := Sloc (Entry_Name);
5768
      Nam    : Entity_Id;
5769
      New_N  : Node_Id;
5770
      S      : Entity_Id;
5771
      Tsk    : Entity_Id;
5772
      E_Name : Node_Id;
5773
      Index  : Node_Id;
5774
 
5775
      function Actual_Index_Type (E : Entity_Id) return Entity_Id;
5776
      --  If the bounds of the entry family being called depend on task
5777
      --  discriminants, build a new index subtype where a discriminant is
5778
      --  replaced with the value of the discriminant of the target task.
5779
      --  The target task is the prefix of the entry name in the call.
5780
 
5781
      -----------------------
5782
      -- Actual_Index_Type --
5783
      -----------------------
5784
 
5785
      function Actual_Index_Type (E : Entity_Id) return Entity_Id is
5786
         Typ   : constant Entity_Id := Entry_Index_Type (E);
5787
         Tsk   : constant Entity_Id := Scope (E);
5788
         Lo    : constant Node_Id   := Type_Low_Bound  (Typ);
5789
         Hi    : constant Node_Id   := Type_High_Bound (Typ);
5790
         New_T : Entity_Id;
5791
 
5792
         function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id;
5793
         --  If the bound is given by a discriminant, replace with a reference
5794
         --  to the discriminant of the same name in the target task. If the
5795
         --  entry name is the target of a requeue statement and the entry is
5796
         --  in the current protected object, the bound to be used is the
5797
         --  discriminal of the object (see apply_range_checks for details of
5798
         --  the transformation).
5799
 
5800
         -----------------------------
5801
         -- Actual_Discriminant_Ref --
5802
         -----------------------------
5803
 
5804
         function Actual_Discriminant_Ref (Bound : Node_Id) return Node_Id is
5805
            Typ : constant Entity_Id := Etype (Bound);
5806
            Ref : Node_Id;
5807
 
5808
         begin
5809
            Remove_Side_Effects (Bound);
5810
 
5811
            if not Is_Entity_Name (Bound)
5812
              or else Ekind (Entity (Bound)) /= E_Discriminant
5813
            then
5814
               return Bound;
5815
 
5816
            elsif Is_Protected_Type (Tsk)
5817
              and then In_Open_Scopes (Tsk)
5818
              and then Nkind (Parent (Entry_Name)) = N_Requeue_Statement
5819
            then
5820
               return New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
5821
 
5822
            else
5823
               Ref :=
5824
                 Make_Selected_Component (Loc,
5825
                   Prefix => New_Copy_Tree (Prefix (Prefix (Entry_Name))),
5826
                   Selector_Name => New_Occurrence_Of (Entity (Bound), Loc));
5827
               Analyze (Ref);
5828
               Resolve (Ref, Typ);
5829
               return Ref;
5830
            end if;
5831
         end Actual_Discriminant_Ref;
5832
 
5833
      --  Start of processing for Actual_Index_Type
5834
 
5835
      begin
5836
         if not Has_Discriminants (Tsk)
5837
           or else (not Is_Entity_Name (Lo)
5838
                     and then
5839
                    not Is_Entity_Name (Hi))
5840
         then
5841
            return Entry_Index_Type (E);
5842
 
5843
         else
5844
            New_T := Create_Itype (Ekind (Typ), Parent (Entry_Name));
5845
            Set_Etype        (New_T, Base_Type (Typ));
5846
            Set_Size_Info    (New_T, Typ);
5847
            Set_RM_Size      (New_T, RM_Size (Typ));
5848
            Set_Scalar_Range (New_T,
5849
              Make_Range (Sloc (Entry_Name),
5850
                Low_Bound  => Actual_Discriminant_Ref (Lo),
5851
                High_Bound => Actual_Discriminant_Ref (Hi)));
5852
 
5853
            return New_T;
5854
         end if;
5855
      end Actual_Index_Type;
5856
 
5857
   --  Start of processing of Resolve_Entry
5858
 
5859
   begin
5860
      --  Find name of entry being called, and resolve prefix of name
5861
      --  with its own type. The prefix can be overloaded, and the name
5862
      --  and signature of the entry must be taken into account.
5863
 
5864
      if Nkind (Entry_Name) = N_Indexed_Component then
5865
 
5866
         --  Case of dealing with entry family within the current tasks
5867
 
5868
         E_Name := Prefix (Entry_Name);
5869
 
5870
      else
5871
         E_Name := Entry_Name;
5872
      end if;
5873
 
5874
      if Is_Entity_Name (E_Name) then
5875
 
5876
         --  Entry call to an entry (or entry family) in the current task. This
5877
         --  is legal even though the task will deadlock. Rewrite as call to
5878
         --  current task.
5879
 
5880
         --  This can also be a call to an entry in an enclosing task. If this
5881
         --  is a single task, we have to retrieve its name, because the scope
5882
         --  of the entry is the task type, not the object. If the enclosing
5883
         --  task is a task type, the identity of the task is given by its own
5884
         --  self variable.
5885
 
5886
         --  Finally this can be a requeue on an entry of the same task or
5887
         --  protected object.
5888
 
5889
         S := Scope (Entity (E_Name));
5890
 
5891
         for J in reverse 0 .. Scope_Stack.Last loop
5892
            if Is_Task_Type (Scope_Stack.Table (J).Entity)
5893
              and then not Comes_From_Source (S)
5894
            then
5895
               --  S is an enclosing task or protected object. The concurrent
5896
               --  declaration has been converted into a type declaration, and
5897
               --  the object itself has an object declaration that follows
5898
               --  the type in the same declarative part.
5899
 
5900
               Tsk := Next_Entity (S);
5901
               while Etype (Tsk) /= S loop
5902
                  Next_Entity (Tsk);
5903
               end loop;
5904
 
5905
               S := Tsk;
5906
               exit;
5907
 
5908
            elsif S = Scope_Stack.Table (J).Entity then
5909
 
5910
               --  Call to current task. Will be transformed into call to Self
5911
 
5912
               exit;
5913
 
5914
            end if;
5915
         end loop;
5916
 
5917
         New_N :=
5918
           Make_Selected_Component (Loc,
5919
             Prefix => New_Occurrence_Of (S, Loc),
5920
             Selector_Name =>
5921
               New_Occurrence_Of (Entity (E_Name), Loc));
5922
         Rewrite (E_Name, New_N);
5923
         Analyze (E_Name);
5924
 
5925
      elsif Nkind (Entry_Name) = N_Selected_Component
5926
        and then Is_Overloaded (Prefix (Entry_Name))
5927
      then
5928
         --  Use the entry name (which must be unique at this point) to find
5929
         --  the prefix that returns the corresponding task type or protected
5930
         --  type.
5931
 
5932
         declare
5933
            Pref : constant Node_Id := Prefix (Entry_Name);
5934
            Ent  : constant Entity_Id :=  Entity (Selector_Name (Entry_Name));
5935
            I    : Interp_Index;
5936
            It   : Interp;
5937
 
5938
         begin
5939
            Get_First_Interp (Pref, I, It);
5940
            while Present (It.Typ) loop
5941
               if Scope (Ent) = It.Typ then
5942
                  Set_Etype (Pref, It.Typ);
5943
                  exit;
5944
               end if;
5945
 
5946
               Get_Next_Interp (I, It);
5947
            end loop;
5948
         end;
5949
      end if;
5950
 
5951
      if Nkind (Entry_Name) = N_Selected_Component then
5952
         Resolve (Prefix (Entry_Name));
5953
 
5954
      else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
5955
         Nam := Entity (Selector_Name (Prefix (Entry_Name)));
5956
         Resolve (Prefix (Prefix (Entry_Name)));
5957
         Index :=  First (Expressions (Entry_Name));
5958
         Resolve (Index, Entry_Index_Type (Nam));
5959
 
5960
         --  Up to this point the expression could have been the actual in a
5961
         --  simple entry call, and be given by a named association.
5962
 
5963
         if Nkind (Index) = N_Parameter_Association then
5964
            Error_Msg_N ("expect expression for entry index", Index);
5965
         else
5966
            Apply_Range_Check (Index, Actual_Index_Type (Nam));
5967
         end if;
5968
      end if;
5969
   end Resolve_Entry;
5970
 
5971
   ------------------------
5972
   -- Resolve_Entry_Call --
5973
   ------------------------
5974
 
5975
   procedure Resolve_Entry_Call (N : Node_Id; Typ : Entity_Id) is
5976
      Entry_Name  : constant Node_Id    := Name (N);
5977
      Loc         : constant Source_Ptr := Sloc (Entry_Name);
5978
      Actuals     : List_Id;
5979
      First_Named : Node_Id;
5980
      Nam         : Entity_Id;
5981
      Norm_OK     : Boolean;
5982
      Obj         : Node_Id;
5983
      Was_Over    : Boolean;
5984
 
5985
   begin
5986
      --  We kill all checks here, because it does not seem worth the effort to
5987
      --  do anything better, an entry call is a big operation.
5988
 
5989
      Kill_All_Checks;
5990
 
5991
      --  Processing of the name is similar for entry calls and protected
5992
      --  operation calls. Once the entity is determined, we can complete
5993
      --  the resolution of the actuals.
5994
 
5995
      --  The selector may be overloaded, in the case of a protected object
5996
      --  with overloaded functions. The type of the context is used for
5997
      --  resolution.
5998
 
5999
      if Nkind (Entry_Name) = N_Selected_Component
6000
        and then Is_Overloaded (Selector_Name (Entry_Name))
6001
        and then Typ /= Standard_Void_Type
6002
      then
6003
         declare
6004
            I  : Interp_Index;
6005
            It : Interp;
6006
 
6007
         begin
6008
            Get_First_Interp (Selector_Name (Entry_Name), I, It);
6009
            while Present (It.Typ) loop
6010
               if Covers (Typ, It.Typ) then
6011
                  Set_Entity (Selector_Name (Entry_Name), It.Nam);
6012
                  Set_Etype  (Entry_Name, It.Typ);
6013
 
6014
                  Generate_Reference (It.Typ, N, ' ');
6015
               end if;
6016
 
6017
               Get_Next_Interp (I, It);
6018
            end loop;
6019
         end;
6020
      end if;
6021
 
6022
      Resolve_Entry (Entry_Name);
6023
 
6024
      if Nkind (Entry_Name) = N_Selected_Component then
6025
 
6026
         --  Simple entry call
6027
 
6028
         Nam := Entity (Selector_Name (Entry_Name));
6029
         Obj := Prefix (Entry_Name);
6030
         Was_Over := Is_Overloaded (Selector_Name (Entry_Name));
6031
 
6032
      else pragma Assert (Nkind (Entry_Name) = N_Indexed_Component);
6033
 
6034
         --  Call to member of entry family
6035
 
6036
         Nam := Entity (Selector_Name (Prefix (Entry_Name)));
6037
         Obj := Prefix (Prefix (Entry_Name));
6038
         Was_Over := Is_Overloaded (Selector_Name (Prefix (Entry_Name)));
6039
      end if;
6040
 
6041
      --  We cannot in general check the maximum depth of protected entry
6042
      --  calls at compile time. But we can tell that any protected entry
6043
      --  call at all violates a specified nesting depth of zero.
6044
 
6045
      if Is_Protected_Type (Scope (Nam)) then
6046
         Check_Restriction (Max_Entry_Queue_Length, N);
6047
      end if;
6048
 
6049
      --  Use context type to disambiguate a protected function that can be
6050
      --  called without actuals and that returns an array type, and where
6051
      --  the argument list may be an indexing of the returned value.
6052
 
6053
      if Ekind (Nam) = E_Function
6054
        and then Needs_No_Actuals (Nam)
6055
        and then Present (Parameter_Associations (N))
6056
        and then
6057
          ((Is_Array_Type (Etype (Nam))
6058
             and then Covers (Typ, Component_Type (Etype (Nam))))
6059
 
6060
            or else (Is_Access_Type (Etype (Nam))
6061
                      and then Is_Array_Type (Designated_Type (Etype (Nam)))
6062
                      and then Covers (Typ,
6063
                        Component_Type (Designated_Type (Etype (Nam))))))
6064
      then
6065
         declare
6066
            Index_Node : Node_Id;
6067
 
6068
         begin
6069
            Index_Node :=
6070
              Make_Indexed_Component (Loc,
6071
                Prefix =>
6072
                  Make_Function_Call (Loc,
6073
                    Name => Relocate_Node (Entry_Name)),
6074
                Expressions => Parameter_Associations (N));
6075
 
6076
            --  Since we are correcting a node classification error made by
6077
            --  the parser, we call Replace rather than Rewrite.
6078
 
6079
            Replace (N, Index_Node);
6080
            Set_Etype (Prefix (N), Etype (Nam));
6081
            Set_Etype (N, Typ);
6082
            Resolve_Indexed_Component (N, Typ);
6083
            return;
6084
         end;
6085
      end if;
6086
 
6087
      --  The operation name may have been overloaded. Order the actuals
6088
      --  according to the formals of the resolved entity, and set the
6089
      --  return type to that of the operation.
6090
 
6091
      if Was_Over then
6092
         Normalize_Actuals (N, Nam, False, Norm_OK);
6093
         pragma Assert (Norm_OK);
6094
         Set_Etype (N, Etype (Nam));
6095
      end if;
6096
 
6097
      Resolve_Actuals (N, Nam);
6098
      Generate_Reference (Nam, Entry_Name);
6099
 
6100
      if Ekind (Nam) = E_Entry
6101
        or else Ekind (Nam) = E_Entry_Family
6102
      then
6103
         Check_Potentially_Blocking_Operation (N);
6104
      end if;
6105
 
6106
      --  Verify that a procedure call cannot masquerade as an entry
6107
      --  call where an entry call is expected.
6108
 
6109
      if Ekind (Nam) = E_Procedure then
6110
         if Nkind (Parent (N)) = N_Entry_Call_Alternative
6111
           and then N = Entry_Call_Statement (Parent (N))
6112
         then
6113
            Error_Msg_N ("entry call required in select statement", N);
6114
 
6115
         elsif Nkind (Parent (N)) = N_Triggering_Alternative
6116
           and then N = Triggering_Statement (Parent (N))
6117
         then
6118
            Error_Msg_N ("triggering statement cannot be procedure call", N);
6119
 
6120
         elsif Ekind (Scope (Nam)) = E_Task_Type
6121
           and then not In_Open_Scopes (Scope (Nam))
6122
         then
6123
            Error_Msg_N ("task has no entry with this name", Entry_Name);
6124
         end if;
6125
      end if;
6126
 
6127
      --  After resolution, entry calls and protected procedure calls are
6128
      --  changed into entry calls, for expansion. The structure of the node
6129
      --  does not change, so it can safely be done in place. Protected
6130
      --  function calls must keep their structure because they are
6131
      --  subexpressions.
6132
 
6133
      if Ekind (Nam) /= E_Function then
6134
 
6135
         --  A protected operation that is not a function may modify the
6136
         --  corresponding object, and cannot apply to a constant. If this
6137
         --  is an internal call, the prefix is the type itself.
6138
 
6139
         if Is_Protected_Type (Scope (Nam))
6140
           and then not Is_Variable (Obj)
6141
           and then (not Is_Entity_Name (Obj)
6142
                       or else not Is_Type (Entity (Obj)))
6143
         then
6144
            Error_Msg_N
6145
              ("prefix of protected procedure or entry call must be variable",
6146
               Entry_Name);
6147
         end if;
6148
 
6149
         Actuals := Parameter_Associations (N);
6150
         First_Named := First_Named_Actual (N);
6151
 
6152
         Rewrite (N,
6153
           Make_Entry_Call_Statement (Loc,
6154
             Name                   => Entry_Name,
6155
             Parameter_Associations => Actuals));
6156
 
6157
         Set_First_Named_Actual (N, First_Named);
6158
         Set_Analyzed (N, True);
6159
 
6160
      --  Protected functions can return on the secondary stack, in which
6161
      --  case we must trigger the transient scope mechanism.
6162
 
6163
      elsif Expander_Active
6164
        and then Requires_Transient_Scope (Etype (Nam))
6165
      then
6166
         Establish_Transient_Scope (N, Sec_Stack => True);
6167
      end if;
6168
   end Resolve_Entry_Call;
6169
 
6170
   -------------------------
6171
   -- Resolve_Equality_Op --
6172
   -------------------------
6173
 
6174
   --  Both arguments must have the same type, and the boolean context does
6175
   --  not participate in the resolution. The first pass verifies that the
6176
   --  interpretation is not ambiguous, and the type of the left argument is
6177
   --  correctly set, or is Any_Type in case of ambiguity. If both arguments
6178
   --  are strings or aggregates, allocators, or Null, they are ambiguous even
6179
   --  though they carry a single (universal) type. Diagnose this case here.
6180
 
6181
   procedure Resolve_Equality_Op (N : Node_Id; Typ : Entity_Id) is
6182
      L : constant Node_Id   := Left_Opnd (N);
6183
      R : constant Node_Id   := Right_Opnd (N);
6184
      T : Entity_Id := Find_Unique_Type (L, R);
6185
 
6186
      function Find_Unique_Access_Type return Entity_Id;
6187
      --  In the case of allocators, make a last-ditch attempt to find a single
6188
      --  access type with the right designated type. This is semantically
6189
      --  dubious, and of no interest to any real code, but c48008a makes it
6190
      --  all worthwhile.
6191
 
6192
      -----------------------------
6193
      -- Find_Unique_Access_Type --
6194
      -----------------------------
6195
 
6196
      function Find_Unique_Access_Type return Entity_Id is
6197
         Acc : Entity_Id;
6198
         E   : Entity_Id;
6199
         S   : Entity_Id;
6200
 
6201
      begin
6202
         if Ekind (Etype (R)) =  E_Allocator_Type then
6203
            Acc := Designated_Type (Etype (R));
6204
         elsif Ekind (Etype (L)) =  E_Allocator_Type then
6205
            Acc := Designated_Type (Etype (L));
6206
         else
6207
            return Empty;
6208
         end if;
6209
 
6210
         S := Current_Scope;
6211
         while S /= Standard_Standard loop
6212
            E := First_Entity (S);
6213
            while Present (E) loop
6214
               if Is_Type (E)
6215
                 and then Is_Access_Type (E)
6216
                 and then Ekind (E) /= E_Allocator_Type
6217
                 and then Designated_Type (E) = Base_Type (Acc)
6218
               then
6219
                  return E;
6220
               end if;
6221
 
6222
               Next_Entity (E);
6223
            end loop;
6224
 
6225
            S := Scope (S);
6226
         end loop;
6227
 
6228
         return Empty;
6229
      end Find_Unique_Access_Type;
6230
 
6231
   --  Start of processing for Resolve_Equality_Op
6232
 
6233
   begin
6234
      Set_Etype (N, Base_Type (Typ));
6235
      Generate_Reference (T, N, ' ');
6236
 
6237
      if T = Any_Fixed then
6238
         T := Unique_Fixed_Point_Type (L);
6239
      end if;
6240
 
6241
      if T /= Any_Type then
6242
         if T = Any_String
6243
           or else T = Any_Composite
6244
           or else T = Any_Character
6245
         then
6246
            if T = Any_Character then
6247
               Ambiguous_Character (L);
6248
            else
6249
               Error_Msg_N ("ambiguous operands for equality", N);
6250
            end if;
6251
 
6252
            Set_Etype (N, Any_Type);
6253
            return;
6254
 
6255
         elsif T = Any_Access
6256
           or else Ekind (T) = E_Allocator_Type
6257
           or else Ekind (T) = E_Access_Attribute_Type
6258
         then
6259
            T := Find_Unique_Access_Type;
6260
 
6261
            if No (T) then
6262
               Error_Msg_N ("ambiguous operands for equality", N);
6263
               Set_Etype (N, Any_Type);
6264
               return;
6265
            end if;
6266
         end if;
6267
 
6268
         Resolve (L, T);
6269
         Resolve (R, T);
6270
 
6271
         --  If the unique type is a class-wide type then it will be expanded
6272
         --  into a dispatching call to the predefined primitive. Therefore we
6273
         --  check here for potential violation of such restriction.
6274
 
6275
         if Is_Class_Wide_Type (T) then
6276
            Check_Restriction (No_Dispatching_Calls, N);
6277
         end if;
6278
 
6279
         if Warn_On_Redundant_Constructs
6280
           and then Comes_From_Source (N)
6281
           and then Is_Entity_Name (R)
6282
           and then Entity (R) = Standard_True
6283
           and then Comes_From_Source (R)
6284
         then
6285
            Error_Msg_N ("?comparison with True is redundant!", R);
6286
         end if;
6287
 
6288
         Check_Unset_Reference (L);
6289
         Check_Unset_Reference (R);
6290
         Generate_Operator_Reference (N, T);
6291
         Check_Low_Bound_Tested (N);
6292
 
6293
         --  If this is an inequality, it may be the implicit inequality
6294
         --  created for a user-defined operation, in which case the corres-
6295
         --  ponding equality operation is not intrinsic, and the operation
6296
         --  cannot be constant-folded. Else fold.
6297
 
6298
         if Nkind (N) = N_Op_Eq
6299
           or else Comes_From_Source (Entity (N))
6300
           or else Ekind (Entity (N)) = E_Operator
6301
           or else Is_Intrinsic_Subprogram
6302
             (Corresponding_Equality (Entity (N)))
6303
         then
6304
            Eval_Relational_Op (N);
6305
 
6306
         elsif Nkind (N) = N_Op_Ne
6307
           and then Is_Abstract_Subprogram (Entity (N))
6308
         then
6309
            Error_Msg_NE ("cannot call abstract subprogram &!", N, Entity (N));
6310
         end if;
6311
 
6312
         --  Ada 2005: If one operand is an anonymous access type, convert the
6313
         --  other operand to it, to ensure that the underlying types match in
6314
         --  the back-end. Same for access_to_subprogram, and the conversion
6315
         --  verifies that the types are subtype conformant.
6316
 
6317
         --  We apply the same conversion in the case one of the operands is a
6318
         --  private subtype of the type of the other.
6319
 
6320
         --  Why the Expander_Active test here ???
6321
 
6322
         if Expander_Active
6323
           and then
6324
             (Ekind (T) =  E_Anonymous_Access_Type
6325
               or else Ekind (T) = E_Anonymous_Access_Subprogram_Type
6326
               or else Is_Private_Type (T))
6327
         then
6328
            if Etype (L) /= T then
6329
               Rewrite (L,
6330
                 Make_Unchecked_Type_Conversion (Sloc (L),
6331
                   Subtype_Mark => New_Occurrence_Of (T, Sloc (L)),
6332
                   Expression   => Relocate_Node (L)));
6333
               Analyze_And_Resolve (L, T);
6334
            end if;
6335
 
6336
            if (Etype (R)) /= T then
6337
               Rewrite (R,
6338
                  Make_Unchecked_Type_Conversion (Sloc (R),
6339
                    Subtype_Mark => New_Occurrence_Of (Etype (L), Sloc (R)),
6340
                    Expression   => Relocate_Node (R)));
6341
               Analyze_And_Resolve (R, T);
6342
            end if;
6343
         end if;
6344
      end if;
6345
   end Resolve_Equality_Op;
6346
 
6347
   ----------------------------------
6348
   -- Resolve_Explicit_Dereference --
6349
   ----------------------------------
6350
 
6351
   procedure Resolve_Explicit_Dereference (N : Node_Id; Typ : Entity_Id) is
6352
      Loc   : constant Source_Ptr := Sloc (N);
6353
      New_N : Node_Id;
6354
      P     : constant Node_Id := Prefix (N);
6355
      I     : Interp_Index;
6356
      It    : Interp;
6357
 
6358
   begin
6359
      Check_Fully_Declared_Prefix (Typ, P);
6360
 
6361
      if Is_Overloaded (P) then
6362
 
6363
         --  Use the context type to select the prefix that has the correct
6364
         --  designated type.
6365
 
6366
         Get_First_Interp (P, I, It);
6367
         while Present (It.Typ) loop
6368
            exit when Is_Access_Type (It.Typ)
6369
              and then Covers (Typ, Designated_Type (It.Typ));
6370
            Get_Next_Interp (I, It);
6371
         end loop;
6372
 
6373
         if Present (It.Typ) then
6374
            Resolve (P, It.Typ);
6375
         else
6376
            --  If no interpretation covers the designated type of the prefix,
6377
            --  this is the pathological case where not all implementations of
6378
            --  the prefix allow the interpretation of the node as a call. Now
6379
            --  that the expected type is known, Remove other interpretations
6380
            --  from prefix, rewrite it as a call, and resolve again, so that
6381
            --  the proper call node is generated.
6382
 
6383
            Get_First_Interp (P, I, It);
6384
            while Present (It.Typ) loop
6385
               if Ekind (It.Typ) /= E_Access_Subprogram_Type then
6386
                  Remove_Interp (I);
6387
               end if;
6388
 
6389
               Get_Next_Interp (I, It);
6390
            end loop;
6391
 
6392
            New_N :=
6393
              Make_Function_Call (Loc,
6394
                Name =>
6395
                  Make_Explicit_Dereference (Loc,
6396
                    Prefix => P),
6397
                Parameter_Associations => New_List);
6398
 
6399
            Save_Interps (N, New_N);
6400
            Rewrite (N, New_N);
6401
            Analyze_And_Resolve (N, Typ);
6402
            return;
6403
         end if;
6404
 
6405
         Set_Etype (N, Designated_Type (It.Typ));
6406
 
6407
      else
6408
         Resolve (P);
6409
      end if;
6410
 
6411
      if Is_Access_Type (Etype (P)) then
6412
         Apply_Access_Check (N);
6413
      end if;
6414
 
6415
      --  If the designated type is a packed unconstrained array type, and the
6416
      --  explicit dereference is not in the context of an attribute reference,
6417
      --  then we must compute and set the actual subtype, since it is needed
6418
      --  by Gigi. The reason we exclude the attribute case is that this is
6419
      --  handled fine by Gigi, and in fact we use such attributes to build the
6420
      --  actual subtype. We also exclude generated code (which builds actual
6421
      --  subtypes directly if they are needed).
6422
 
6423
      if Is_Array_Type (Etype (N))
6424
        and then Is_Packed (Etype (N))
6425
        and then not Is_Constrained (Etype (N))
6426
        and then Nkind (Parent (N)) /= N_Attribute_Reference
6427
        and then Comes_From_Source (N)
6428
      then
6429
         Set_Etype (N, Get_Actual_Subtype (N));
6430
      end if;
6431
 
6432
      --  Note: No Eval processing is required for an explicit dereference,
6433
      --  because such a name can never be static.
6434
 
6435
   end Resolve_Explicit_Dereference;
6436
 
6437
   -------------------------------
6438
   -- Resolve_Indexed_Component --
6439
   -------------------------------
6440
 
6441
   procedure Resolve_Indexed_Component (N : Node_Id; Typ : Entity_Id) is
6442
      Name       : constant Node_Id := Prefix  (N);
6443
      Expr       : Node_Id;
6444
      Array_Type : Entity_Id := Empty; -- to prevent junk warning
6445
      Index      : Node_Id;
6446
 
6447
   begin
6448
      if Is_Overloaded (Name) then
6449
 
6450
         --  Use the context type to select the prefix that yields the correct
6451
         --  component type.
6452
 
6453
         declare
6454
            I     : Interp_Index;
6455
            It    : Interp;
6456
            I1    : Interp_Index := 0;
6457
            P     : constant Node_Id := Prefix (N);
6458
            Found : Boolean := False;
6459
 
6460
         begin
6461
            Get_First_Interp (P, I, It);
6462
            while Present (It.Typ) loop
6463
               if (Is_Array_Type (It.Typ)
6464
                     and then Covers (Typ, Component_Type (It.Typ)))
6465
                 or else (Is_Access_Type (It.Typ)
6466
                            and then Is_Array_Type (Designated_Type (It.Typ))
6467
                            and then Covers
6468
                              (Typ, Component_Type (Designated_Type (It.Typ))))
6469
               then
6470
                  if Found then
6471
                     It := Disambiguate (P, I1, I, Any_Type);
6472
 
6473
                     if It = No_Interp then
6474
                        Error_Msg_N ("ambiguous prefix for indexing",  N);
6475
                        Set_Etype (N, Typ);
6476
                        return;
6477
 
6478
                     else
6479
                        Found := True;
6480
                        Array_Type := It.Typ;
6481
                        I1 := I;
6482
                     end if;
6483
 
6484
                  else
6485
                     Found := True;
6486
                     Array_Type := It.Typ;
6487
                     I1 := I;
6488
                  end if;
6489
               end if;
6490
 
6491
               Get_Next_Interp (I, It);
6492
            end loop;
6493
         end;
6494
 
6495
      else
6496
         Array_Type := Etype (Name);
6497
      end if;
6498
 
6499
      Resolve (Name, Array_Type);
6500
      Array_Type := Get_Actual_Subtype_If_Available (Name);
6501
 
6502
      --  If prefix is access type, dereference to get real array type.
6503
      --  Note: we do not apply an access check because the expander always
6504
      --  introduces an explicit dereference, and the check will happen there.
6505
 
6506
      if Is_Access_Type (Array_Type) then
6507
         Array_Type := Designated_Type (Array_Type);
6508
      end if;
6509
 
6510
      --  If name was overloaded, set component type correctly now
6511
      --  If a misplaced call to an entry family (which has no index types)
6512
      --  return. Error will be diagnosed from calling context.
6513
 
6514
      if Is_Array_Type (Array_Type) then
6515
         Set_Etype (N, Component_Type (Array_Type));
6516
      else
6517
         return;
6518
      end if;
6519
 
6520
      Index := First_Index (Array_Type);
6521
      Expr  := First (Expressions (N));
6522
 
6523
      --  The prefix may have resolved to a string literal, in which case its
6524
      --  etype has a special representation. This is only possible currently
6525
      --  if the prefix is a static concatenation, written in functional
6526
      --  notation.
6527
 
6528
      if Ekind (Array_Type) = E_String_Literal_Subtype then
6529
         Resolve (Expr, Standard_Positive);
6530
 
6531
      else
6532
         while Present (Index) and Present (Expr) loop
6533
            Resolve (Expr, Etype (Index));
6534
            Check_Unset_Reference (Expr);
6535
 
6536
            if Is_Scalar_Type (Etype (Expr)) then
6537
               Apply_Scalar_Range_Check (Expr, Etype (Index));
6538
            else
6539
               Apply_Range_Check (Expr, Get_Actual_Subtype (Index));
6540
            end if;
6541
 
6542
            Next_Index (Index);
6543
            Next (Expr);
6544
         end loop;
6545
      end if;
6546
 
6547
      --  Do not generate the warning on suspicious index if we are analyzing
6548
      --  package Ada.Tags; otherwise we will report the warning with the
6549
      --  Prims_Ptr field of the dispatch table.
6550
 
6551
      if Scope (Etype (Prefix (N))) = Standard_Standard
6552
        or else not
6553
          Is_RTU (Cunit_Entity (Get_Source_Unit (Etype (Prefix (N)))),
6554
                  Ada_Tags)
6555
      then
6556
         Warn_On_Suspicious_Index (Name, First (Expressions (N)));
6557
         Eval_Indexed_Component (N);
6558
      end if;
6559
   end Resolve_Indexed_Component;
6560
 
6561
   -----------------------------
6562
   -- Resolve_Integer_Literal --
6563
   -----------------------------
6564
 
6565
   procedure Resolve_Integer_Literal (N : Node_Id; Typ : Entity_Id) is
6566
   begin
6567
      Set_Etype (N, Typ);
6568
      Eval_Integer_Literal (N);
6569
   end Resolve_Integer_Literal;
6570
 
6571
   --------------------------------
6572
   -- Resolve_Intrinsic_Operator --
6573
   --------------------------------
6574
 
6575
   procedure Resolve_Intrinsic_Operator  (N : Node_Id; Typ : Entity_Id) is
6576
      Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
6577
      Op   : Entity_Id;
6578
      Arg1 : Node_Id;
6579
      Arg2 : Node_Id;
6580
 
6581
   begin
6582
      Op := Entity (N);
6583
      while Scope (Op) /= Standard_Standard loop
6584
         Op := Homonym (Op);
6585
         pragma Assert (Present (Op));
6586
      end loop;
6587
 
6588
      Set_Entity (N, Op);
6589
      Set_Is_Overloaded (N, False);
6590
 
6591
      --  If the operand type is private, rewrite with suitable conversions on
6592
      --  the operands and the result, to expose the proper underlying numeric
6593
      --  type.
6594
 
6595
      if Is_Private_Type (Typ) then
6596
         Arg1 := Unchecked_Convert_To (Btyp, Left_Opnd  (N));
6597
 
6598
         if Nkind (N) = N_Op_Expon then
6599
            Arg2 := Unchecked_Convert_To (Standard_Integer, Right_Opnd (N));
6600
         else
6601
            Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
6602
         end if;
6603
 
6604
         Save_Interps (Left_Opnd (N),  Expression (Arg1));
6605
         Save_Interps (Right_Opnd (N), Expression (Arg2));
6606
 
6607
         Set_Left_Opnd  (N, Arg1);
6608
         Set_Right_Opnd (N, Arg2);
6609
 
6610
         Set_Etype (N, Btyp);
6611
         Rewrite (N, Unchecked_Convert_To (Typ, N));
6612
         Resolve (N, Typ);
6613
 
6614
      elsif Typ /= Etype (Left_Opnd (N))
6615
        or else Typ /= Etype (Right_Opnd (N))
6616
      then
6617
         --  Add explicit conversion where needed, and save interpretations in
6618
         --  case operands are overloaded.
6619
 
6620
         Arg1 := Convert_To (Typ, Left_Opnd  (N));
6621
         Arg2 := Convert_To (Typ, Right_Opnd (N));
6622
 
6623
         if Nkind (Arg1) = N_Type_Conversion then
6624
            Save_Interps (Left_Opnd (N), Expression (Arg1));
6625
         else
6626
            Save_Interps (Left_Opnd (N), Arg1);
6627
         end if;
6628
 
6629
         if Nkind (Arg2) = N_Type_Conversion then
6630
            Save_Interps (Right_Opnd (N), Expression (Arg2));
6631
         else
6632
            Save_Interps (Right_Opnd (N), Arg2);
6633
         end if;
6634
 
6635
         Rewrite (Left_Opnd  (N), Arg1);
6636
         Rewrite (Right_Opnd (N), Arg2);
6637
         Analyze (Arg1);
6638
         Analyze (Arg2);
6639
         Resolve_Arithmetic_Op (N, Typ);
6640
 
6641
      else
6642
         Resolve_Arithmetic_Op (N, Typ);
6643
      end if;
6644
   end Resolve_Intrinsic_Operator;
6645
 
6646
   --------------------------------------
6647
   -- Resolve_Intrinsic_Unary_Operator --
6648
   --------------------------------------
6649
 
6650
   procedure Resolve_Intrinsic_Unary_Operator
6651
     (N   : Node_Id;
6652
      Typ : Entity_Id)
6653
   is
6654
      Btyp : constant Entity_Id := Base_Type (Underlying_Type (Typ));
6655
      Op   : Entity_Id;
6656
      Arg2 : Node_Id;
6657
 
6658
   begin
6659
      Op := Entity (N);
6660
      while Scope (Op) /= Standard_Standard loop
6661
         Op := Homonym (Op);
6662
         pragma Assert (Present (Op));
6663
      end loop;
6664
 
6665
      Set_Entity (N, Op);
6666
 
6667
      if Is_Private_Type (Typ) then
6668
         Arg2 := Unchecked_Convert_To (Btyp, Right_Opnd (N));
6669
         Save_Interps (Right_Opnd (N), Expression (Arg2));
6670
 
6671
         Set_Right_Opnd (N, Arg2);
6672
 
6673
         Set_Etype (N, Btyp);
6674
         Rewrite (N, Unchecked_Convert_To (Typ, N));
6675
         Resolve (N, Typ);
6676
 
6677
      else
6678
         Resolve_Unary_Op (N, Typ);
6679
      end if;
6680
   end Resolve_Intrinsic_Unary_Operator;
6681
 
6682
   ------------------------
6683
   -- Resolve_Logical_Op --
6684
   ------------------------
6685
 
6686
   procedure Resolve_Logical_Op (N : Node_Id; Typ : Entity_Id) is
6687
      B_Typ : Entity_Id;
6688
 
6689
   begin
6690
      Check_No_Direct_Boolean_Operators (N);
6691
 
6692
      --  Predefined operations on scalar types yield the base type. On the
6693
      --  other hand, logical operations on arrays yield the type of the
6694
      --  arguments (and the context).
6695
 
6696
      if Is_Array_Type (Typ) then
6697
         B_Typ := Typ;
6698
      else
6699
         B_Typ := Base_Type (Typ);
6700
      end if;
6701
 
6702
      --  The following test is required because the operands of the operation
6703
      --  may be literals, in which case the resulting type appears to be
6704
      --  compatible with a signed integer type, when in fact it is compatible
6705
      --  only with modular types. If the context itself is universal, the
6706
      --  operation is illegal.
6707
 
6708
      if not Valid_Boolean_Arg (Typ) then
6709
         Error_Msg_N ("invalid context for logical operation", N);
6710
         Set_Etype (N, Any_Type);
6711
         return;
6712
 
6713
      elsif Typ = Any_Modular then
6714
         Error_Msg_N
6715
           ("no modular type available in this context", N);
6716
         Set_Etype (N, Any_Type);
6717
         return;
6718
      elsif Is_Modular_Integer_Type (Typ)
6719
        and then Etype (Left_Opnd (N)) = Universal_Integer
6720
        and then Etype (Right_Opnd (N)) = Universal_Integer
6721
      then
6722
         Check_For_Visible_Operator (N, B_Typ);
6723
      end if;
6724
 
6725
      Resolve (Left_Opnd (N), B_Typ);
6726
      Resolve (Right_Opnd (N), B_Typ);
6727
 
6728
      Check_Unset_Reference (Left_Opnd  (N));
6729
      Check_Unset_Reference (Right_Opnd (N));
6730
 
6731
      Set_Etype (N, B_Typ);
6732
      Generate_Operator_Reference (N, B_Typ);
6733
      Eval_Logical_Op (N);
6734
   end Resolve_Logical_Op;
6735
 
6736
   ---------------------------
6737
   -- Resolve_Membership_Op --
6738
   ---------------------------
6739
 
6740
   --  The context can only be a boolean type, and does not determine
6741
   --  the arguments. Arguments should be unambiguous, but the preference
6742
   --  rule for universal types applies.
6743
 
6744
   procedure Resolve_Membership_Op (N : Node_Id; Typ : Entity_Id) is
6745
      pragma Warnings (Off, Typ);
6746
 
6747
      L : constant Node_Id := Left_Opnd  (N);
6748
      R : constant Node_Id := Right_Opnd (N);
6749
      T : Entity_Id;
6750
 
6751
      procedure Resolve_Set_Membership;
6752
      --  Analysis has determined a unique type for the left operand.
6753
      --  Use it to resolve the disjuncts.
6754
 
6755
      ----------------------------
6756
      -- Resolve_Set_Membership --
6757
      ----------------------------
6758
 
6759
      procedure Resolve_Set_Membership is
6760
         Alt : Node_Id;
6761
 
6762
      begin
6763
         Resolve (L, Etype (L));
6764
 
6765
         Alt := First (Alternatives (N));
6766
         while Present (Alt) loop
6767
 
6768
            --  Alternative is an expression, a range
6769
            --  or a subtype mark.
6770
 
6771
            if not Is_Entity_Name (Alt)
6772
              or else not Is_Type (Entity (Alt))
6773
            then
6774
               Resolve (Alt, Etype (L));
6775
            end if;
6776
 
6777
            Next (Alt);
6778
         end loop;
6779
      end Resolve_Set_Membership;
6780
 
6781
   --  Start of processing for Resolve_Membership_Op
6782
 
6783
   begin
6784
      if L = Error or else R = Error then
6785
         return;
6786
      end if;
6787
 
6788
      if Present (Alternatives (N)) then
6789
         Resolve_Set_Membership;
6790
         return;
6791
 
6792
      elsif not Is_Overloaded (R)
6793
        and then
6794
          (Etype (R) = Universal_Integer or else
6795
           Etype (R) = Universal_Real)
6796
        and then Is_Overloaded (L)
6797
      then
6798
         T := Etype (R);
6799
 
6800
      --  Ada 2005 (AI-251): Support the following case:
6801
 
6802
      --      type I is interface;
6803
      --      type T is tagged ...
6804
 
6805
      --      function Test (O : I'Class) is
6806
      --      begin
6807
      --         return O in T'Class.
6808
      --      end Test;
6809
 
6810
      --  In this case we have nothing else to do. The membership test will be
6811
      --  done at run-time.
6812
 
6813
      elsif Ada_Version >= Ada_05
6814
        and then Is_Class_Wide_Type (Etype (L))
6815
        and then Is_Interface (Etype (L))
6816
        and then Is_Class_Wide_Type (Etype (R))
6817
        and then not Is_Interface (Etype (R))
6818
      then
6819
         return;
6820
 
6821
      else
6822
         T := Intersect_Types (L, R);
6823
      end if;
6824
 
6825
      Resolve (L, T);
6826
      Check_Unset_Reference (L);
6827
 
6828
      if Nkind (R) = N_Range
6829
        and then not Is_Scalar_Type (T)
6830
      then
6831
         Error_Msg_N ("scalar type required for range", R);
6832
      end if;
6833
 
6834
      if Is_Entity_Name (R) then
6835
         Freeze_Expression (R);
6836
      else
6837
         Resolve (R, T);
6838
         Check_Unset_Reference (R);
6839
      end if;
6840
 
6841
      Eval_Membership_Op (N);
6842
   end Resolve_Membership_Op;
6843
 
6844
   ------------------
6845
   -- Resolve_Null --
6846
   ------------------
6847
 
6848
   procedure Resolve_Null (N : Node_Id; Typ : Entity_Id) is
6849
      Loc : constant Source_Ptr := Sloc (N);
6850
 
6851
   begin
6852
      --  Handle restriction against anonymous null access values This
6853
      --  restriction can be turned off using -gnatdj.
6854
 
6855
      --  Ada 2005 (AI-231): Remove restriction
6856
 
6857
      if Ada_Version < Ada_05
6858
        and then not Debug_Flag_J
6859
        and then Ekind (Typ) = E_Anonymous_Access_Type
6860
        and then Comes_From_Source (N)
6861
      then
6862
         --  In the common case of a call which uses an explicitly null value
6863
         --  for an access parameter, give specialized error message.
6864
 
6865
         if Nkind_In (Parent (N), N_Procedure_Call_Statement,
6866
                                  N_Function_Call)
6867
         then
6868
            Error_Msg_N
6869
              ("null is not allowed as argument for an access parameter", N);
6870
 
6871
         --  Standard message for all other cases (are there any?)
6872
 
6873
         else
6874
            Error_Msg_N
6875
              ("null cannot be of an anonymous access type", N);
6876
         end if;
6877
      end if;
6878
 
6879
      --  Ada 2005 (AI-231): Generate the null-excluding check in case of
6880
      --  assignment to a null-excluding object
6881
 
6882
      if Ada_Version >= Ada_05
6883
        and then Can_Never_Be_Null (Typ)
6884
        and then Nkind (Parent (N)) = N_Assignment_Statement
6885
      then
6886
         if not Inside_Init_Proc then
6887
            Insert_Action
6888
              (Compile_Time_Constraint_Error (N,
6889
                 "(Ada 2005) null not allowed in null-excluding objects?"),
6890
               Make_Raise_Constraint_Error (Loc,
6891
                 Reason => CE_Access_Check_Failed));
6892
         else
6893
            Insert_Action (N,
6894
              Make_Raise_Constraint_Error (Loc,
6895
                Reason => CE_Access_Check_Failed));
6896
         end if;
6897
      end if;
6898
 
6899
      --  In a distributed context, null for a remote access to subprogram may
6900
      --  need to be replaced with a special record aggregate. In this case,
6901
      --  return after having done the transformation.
6902
 
6903
      if (Ekind (Typ) = E_Record_Type
6904
           or else Is_Remote_Access_To_Subprogram_Type (Typ))
6905
        and then Remote_AST_Null_Value (N, Typ)
6906
      then
6907
         return;
6908
      end if;
6909
 
6910
      --  The null literal takes its type from the context
6911
 
6912
      Set_Etype (N, Typ);
6913
   end Resolve_Null;
6914
 
6915
   -----------------------
6916
   -- Resolve_Op_Concat --
6917
   -----------------------
6918
 
6919
   procedure Resolve_Op_Concat (N : Node_Id; Typ : Entity_Id) is
6920
 
6921
      --  We wish to avoid deep recursion, because concatenations are often
6922
      --  deeply nested, as in A&B&...&Z. Therefore, we walk down the left
6923
      --  operands nonrecursively until we find something that is not a simple
6924
      --  concatenation (A in this case). We resolve that, and then walk back
6925
      --  up the tree following Parent pointers, calling Resolve_Op_Concat_Rest
6926
      --  to do the rest of the work at each level. The Parent pointers allow
6927
      --  us to avoid recursion, and thus avoid running out of memory. See also
6928
      --  Sem_Ch4.Analyze_Concatenation, where a similar approach is used.
6929
 
6930
      NN  : Node_Id := N;
6931
      Op1 : Node_Id;
6932
 
6933
   begin
6934
      --  The following code is equivalent to:
6935
 
6936
      --    Resolve_Op_Concat_First (NN, Typ);
6937
      --    Resolve_Op_Concat_Arg (N, ...);
6938
      --    Resolve_Op_Concat_Rest (N, Typ);
6939
 
6940
      --  where the Resolve_Op_Concat_Arg call recurses back here if the left
6941
      --  operand is a concatenation.
6942
 
6943
      --  Walk down left operands
6944
 
6945
      loop
6946
         Resolve_Op_Concat_First (NN, Typ);
6947
         Op1 := Left_Opnd (NN);
6948
         exit when not (Nkind (Op1) = N_Op_Concat
6949
                         and then not Is_Array_Type (Component_Type (Typ))
6950
                         and then Entity (Op1) = Entity (NN));
6951
         NN := Op1;
6952
      end loop;
6953
 
6954
      --  Now (given the above example) NN is A&B and Op1 is A
6955
 
6956
      --  First resolve Op1 ...
6957
 
6958
      Resolve_Op_Concat_Arg (NN, Op1, Typ, Is_Component_Left_Opnd  (NN));
6959
 
6960
      --  ... then walk NN back up until we reach N (where we started), calling
6961
      --  Resolve_Op_Concat_Rest along the way.
6962
 
6963
      loop
6964
         Resolve_Op_Concat_Rest (NN, Typ);
6965
         exit when NN = N;
6966
         NN := Parent (NN);
6967
      end loop;
6968
   end Resolve_Op_Concat;
6969
 
6970
   ---------------------------
6971
   -- Resolve_Op_Concat_Arg --
6972
   ---------------------------
6973
 
6974
   procedure Resolve_Op_Concat_Arg
6975
     (N       : Node_Id;
6976
      Arg     : Node_Id;
6977
      Typ     : Entity_Id;
6978
      Is_Comp : Boolean)
6979
   is
6980
      Btyp : constant Entity_Id := Base_Type (Typ);
6981
 
6982
   begin
6983
      if In_Instance then
6984
         if Is_Comp
6985
           or else (not Is_Overloaded (Arg)
6986
                     and then Etype (Arg) /= Any_Composite
6987
                     and then Covers (Component_Type (Typ), Etype (Arg)))
6988
         then
6989
            Resolve (Arg, Component_Type (Typ));
6990
         else
6991
            Resolve (Arg, Btyp);
6992
         end if;
6993
 
6994
      elsif Has_Compatible_Type (Arg, Component_Type (Typ)) then
6995
         if Nkind (Arg) = N_Aggregate
6996
           and then Is_Composite_Type (Component_Type (Typ))
6997
         then
6998
            if Is_Private_Type (Component_Type (Typ)) then
6999
               Resolve (Arg, Btyp);
7000
            else
7001
               Error_Msg_N ("ambiguous aggregate must be qualified", Arg);
7002
               Set_Etype (Arg, Any_Type);
7003
            end if;
7004
 
7005
         else
7006
            if Is_Overloaded (Arg)
7007
              and then Has_Compatible_Type (Arg, Typ)
7008
              and then Etype (Arg) /= Any_Type
7009
            then
7010
               declare
7011
                  I    : Interp_Index;
7012
                  It   : Interp;
7013
                  Func : Entity_Id;
7014
 
7015
               begin
7016
                  Get_First_Interp (Arg, I, It);
7017
                  Func := It.Nam;
7018
                  Get_Next_Interp (I, It);
7019
 
7020
                  --  Special-case the error message when the overloading is
7021
                  --  caused by a function that yields an array and can be
7022
                  --  called without parameters.
7023
 
7024
                  if It.Nam = Func then
7025
                     Error_Msg_Sloc := Sloc (Func);
7026
                     Error_Msg_N ("ambiguous call to function#", Arg);
7027
                     Error_Msg_NE
7028
                       ("\\interpretation as call yields&", Arg, Typ);
7029
                     Error_Msg_NE
7030
                       ("\\interpretation as indexing of call yields&",
7031
                         Arg, Component_Type (Typ));
7032
 
7033
                  else
7034
                     Error_Msg_N
7035
                       ("ambiguous operand for concatenation!", Arg);
7036
                     Get_First_Interp (Arg, I, It);
7037
                     while Present (It.Nam) loop
7038
                        Error_Msg_Sloc := Sloc (It.Nam);
7039
 
7040
                        if Base_Type (It.Typ) = Base_Type (Typ)
7041
                          or else Base_Type (It.Typ) =
7042
                            Base_Type (Component_Type (Typ))
7043
                        then
7044
                           Error_Msg_N -- CODEFIX
7045
                             ("\\possible interpretation#", Arg);
7046
                        end if;
7047
 
7048
                        Get_Next_Interp (I, It);
7049
                     end loop;
7050
                  end if;
7051
               end;
7052
            end if;
7053
 
7054
            Resolve (Arg, Component_Type (Typ));
7055
 
7056
            if Nkind (Arg) = N_String_Literal then
7057
               Set_Etype (Arg, Component_Type (Typ));
7058
            end if;
7059
 
7060
            if Arg = Left_Opnd (N) then
7061
               Set_Is_Component_Left_Opnd (N);
7062
            else
7063
               Set_Is_Component_Right_Opnd (N);
7064
            end if;
7065
         end if;
7066
 
7067
      else
7068
         Resolve (Arg, Btyp);
7069
      end if;
7070
 
7071
      Check_Unset_Reference (Arg);
7072
   end Resolve_Op_Concat_Arg;
7073
 
7074
   -----------------------------
7075
   -- Resolve_Op_Concat_First --
7076
   -----------------------------
7077
 
7078
   procedure Resolve_Op_Concat_First (N : Node_Id; Typ : Entity_Id) is
7079
      Btyp : constant Entity_Id := Base_Type (Typ);
7080
      Op1  : constant Node_Id := Left_Opnd (N);
7081
      Op2  : constant Node_Id := Right_Opnd (N);
7082
 
7083
   begin
7084
      --  The parser folds an enormous sequence of concatenations of string
7085
      --  literals into "" & "...", where the Is_Folded_In_Parser flag is set
7086
      --  in the right operand. If the expression resolves to a predefined "&"
7087
      --  operator, all is well. Otherwise, the parser's folding is wrong, so
7088
      --  we give an error. See P_Simple_Expression in Par.Ch4.
7089
 
7090
      if Nkind (Op2) = N_String_Literal
7091
        and then Is_Folded_In_Parser (Op2)
7092
        and then Ekind (Entity (N)) = E_Function
7093
      then
7094
         pragma Assert (Nkind (Op1) = N_String_Literal  --  should be ""
7095
               and then String_Length (Strval (Op1)) = 0);
7096
         Error_Msg_N ("too many user-defined concatenations", N);
7097
         return;
7098
      end if;
7099
 
7100
      Set_Etype (N, Btyp);
7101
 
7102
      if Is_Limited_Composite (Btyp) then
7103
         Error_Msg_N ("concatenation not available for limited array", N);
7104
         Explain_Limited_Type (Btyp, N);
7105
      end if;
7106
   end Resolve_Op_Concat_First;
7107
 
7108
   ----------------------------
7109
   -- Resolve_Op_Concat_Rest --
7110
   ----------------------------
7111
 
7112
   procedure Resolve_Op_Concat_Rest (N : Node_Id; Typ : Entity_Id) is
7113
      Op1  : constant Node_Id := Left_Opnd (N);
7114
      Op2  : constant Node_Id := Right_Opnd (N);
7115
 
7116
   begin
7117
      Resolve_Op_Concat_Arg (N, Op2, Typ, Is_Component_Right_Opnd  (N));
7118
 
7119
      Generate_Operator_Reference (N, Typ);
7120
 
7121
      if Is_String_Type (Typ) then
7122
         Eval_Concatenation (N);
7123
      end if;
7124
 
7125
      --  If this is not a static concatenation, but the result is a string
7126
      --  type (and not an array of strings) ensure that static string operands
7127
      --  have their subtypes properly constructed.
7128
 
7129
      if Nkind (N) /= N_String_Literal
7130
        and then Is_Character_Type (Component_Type (Typ))
7131
      then
7132
         Set_String_Literal_Subtype (Op1, Typ);
7133
         Set_String_Literal_Subtype (Op2, Typ);
7134
      end if;
7135
   end Resolve_Op_Concat_Rest;
7136
 
7137
   ----------------------
7138
   -- Resolve_Op_Expon --
7139
   ----------------------
7140
 
7141
   procedure Resolve_Op_Expon (N : Node_Id; Typ : Entity_Id) is
7142
      B_Typ : constant Entity_Id := Base_Type (Typ);
7143
 
7144
   begin
7145
      --  Catch attempts to do fixed-point exponentiation with universal
7146
      --  operands, which is a case where the illegality is not caught during
7147
      --  normal operator analysis.
7148
 
7149
      if Is_Fixed_Point_Type (Typ) and then Comes_From_Source (N) then
7150
         Error_Msg_N ("exponentiation not available for fixed point", N);
7151
         return;
7152
      end if;
7153
 
7154
      if Comes_From_Source (N)
7155
        and then Ekind (Entity (N)) = E_Function
7156
        and then Is_Imported (Entity (N))
7157
        and then Is_Intrinsic_Subprogram (Entity (N))
7158
      then
7159
         Resolve_Intrinsic_Operator (N, Typ);
7160
         return;
7161
      end if;
7162
 
7163
      if Etype (Left_Opnd (N)) = Universal_Integer
7164
        or else Etype (Left_Opnd (N)) = Universal_Real
7165
      then
7166
         Check_For_Visible_Operator (N, B_Typ);
7167
      end if;
7168
 
7169
      --  We do the resolution using the base type, because intermediate values
7170
      --  in expressions always are of the base type, not a subtype of it.
7171
 
7172
      Resolve (Left_Opnd (N), B_Typ);
7173
      Resolve (Right_Opnd (N), Standard_Integer);
7174
 
7175
      Check_Unset_Reference (Left_Opnd  (N));
7176
      Check_Unset_Reference (Right_Opnd (N));
7177
 
7178
      Set_Etype (N, B_Typ);
7179
      Generate_Operator_Reference (N, B_Typ);
7180
      Eval_Op_Expon (N);
7181
 
7182
      --  Set overflow checking bit. Much cleverer code needed here eventually
7183
      --  and perhaps the Resolve routines should be separated for the various
7184
      --  arithmetic operations, since they will need different processing. ???
7185
 
7186
      if Nkind (N) in N_Op then
7187
         if not Overflow_Checks_Suppressed (Etype (N)) then
7188
            Enable_Overflow_Check (N);
7189
         end if;
7190
      end if;
7191
   end Resolve_Op_Expon;
7192
 
7193
   --------------------
7194
   -- Resolve_Op_Not --
7195
   --------------------
7196
 
7197
   procedure Resolve_Op_Not (N : Node_Id; Typ : Entity_Id) is
7198
      B_Typ : Entity_Id;
7199
 
7200
      function Parent_Is_Boolean return Boolean;
7201
      --  This function determines if the parent node is a boolean operator
7202
      --  or operation (comparison op, membership test, or short circuit form)
7203
      --  and the not in question is the left operand of this operation.
7204
      --  Note that if the not is in parens, then false is returned.
7205
 
7206
      -----------------------
7207
      -- Parent_Is_Boolean --
7208
      -----------------------
7209
 
7210
      function Parent_Is_Boolean return Boolean is
7211
      begin
7212
         if Paren_Count (N) /= 0 then
7213
            return False;
7214
 
7215
         else
7216
            case Nkind (Parent (N)) is
7217
               when N_Op_And   |
7218
                    N_Op_Eq    |
7219
                    N_Op_Ge    |
7220
                    N_Op_Gt    |
7221
                    N_Op_Le    |
7222
                    N_Op_Lt    |
7223
                    N_Op_Ne    |
7224
                    N_Op_Or    |
7225
                    N_Op_Xor   |
7226
                    N_In       |
7227
                    N_Not_In   |
7228
                    N_And_Then |
7229
                    N_Or_Else  =>
7230
 
7231
                  return Left_Opnd (Parent (N)) = N;
7232
 
7233
               when others =>
7234
                  return False;
7235
            end case;
7236
         end if;
7237
      end Parent_Is_Boolean;
7238
 
7239
   --  Start of processing for Resolve_Op_Not
7240
 
7241
   begin
7242
      --  Predefined operations on scalar types yield the base type. On the
7243
      --  other hand, logical operations on arrays yield the type of the
7244
      --  arguments (and the context).
7245
 
7246
      if Is_Array_Type (Typ) then
7247
         B_Typ := Typ;
7248
      else
7249
         B_Typ := Base_Type (Typ);
7250
      end if;
7251
 
7252
      --  Straightforward case of incorrect arguments
7253
 
7254
      if not Valid_Boolean_Arg (Typ) then
7255
         Error_Msg_N ("invalid operand type for operator&", N);
7256
         Set_Etype (N, Any_Type);
7257
         return;
7258
 
7259
      --  Special case of probable missing parens
7260
 
7261
      elsif Typ = Universal_Integer or else Typ = Any_Modular then
7262
         if Parent_Is_Boolean then
7263
            Error_Msg_N
7264
              ("operand of not must be enclosed in parentheses",
7265
               Right_Opnd (N));
7266
         else
7267
            Error_Msg_N
7268
              ("no modular type available in this context", N);
7269
         end if;
7270
 
7271
         Set_Etype (N, Any_Type);
7272
         return;
7273
 
7274
      --  OK resolution of not
7275
 
7276
      else
7277
         --  Warn if non-boolean types involved. This is a case like not a < b
7278
         --  where a and b are modular, where we will get (not a) < b and most
7279
         --  likely not (a < b) was intended.
7280
 
7281
         if Warn_On_Questionable_Missing_Parens
7282
           and then not Is_Boolean_Type (Typ)
7283
           and then Parent_Is_Boolean
7284
         then
7285
            Error_Msg_N ("?not expression should be parenthesized here!", N);
7286
         end if;
7287
 
7288
         --  Warn on double negation if checking redundant constructs
7289
 
7290
         if Warn_On_Redundant_Constructs
7291
           and then Comes_From_Source (N)
7292
           and then Comes_From_Source (Right_Opnd (N))
7293
           and then Root_Type (Typ) = Standard_Boolean
7294
           and then Nkind (Right_Opnd (N)) = N_Op_Not
7295
         then
7296
            Error_Msg_N ("redundant double negation?", N);
7297
         end if;
7298
 
7299
         --  Complete resolution and evaluation of NOT
7300
 
7301
         Resolve (Right_Opnd (N), B_Typ);
7302
         Check_Unset_Reference (Right_Opnd (N));
7303
         Set_Etype (N, B_Typ);
7304
         Generate_Operator_Reference (N, B_Typ);
7305
         Eval_Op_Not (N);
7306
      end if;
7307
   end Resolve_Op_Not;
7308
 
7309
   -----------------------------
7310
   -- Resolve_Operator_Symbol --
7311
   -----------------------------
7312
 
7313
   --  Nothing to be done, all resolved already
7314
 
7315
   procedure Resolve_Operator_Symbol (N : Node_Id; Typ : Entity_Id) is
7316
      pragma Warnings (Off, N);
7317
      pragma Warnings (Off, Typ);
7318
 
7319
   begin
7320
      null;
7321
   end Resolve_Operator_Symbol;
7322
 
7323
   ----------------------------------
7324
   -- Resolve_Qualified_Expression --
7325
   ----------------------------------
7326
 
7327
   procedure Resolve_Qualified_Expression (N : Node_Id; Typ : Entity_Id) is
7328
      pragma Warnings (Off, Typ);
7329
 
7330
      Target_Typ : constant Entity_Id := Entity (Subtype_Mark (N));
7331
      Expr       : constant Node_Id   := Expression (N);
7332
 
7333
   begin
7334
      Resolve (Expr, Target_Typ);
7335
 
7336
      --  A qualified expression requires an exact match of the type,
7337
      --  class-wide matching is not allowed. However, if the qualifying
7338
      --  type is specific and the expression has a class-wide type, it
7339
      --  may still be okay, since it can be the result of the expansion
7340
      --  of a call to a dispatching function, so we also have to check
7341
      --  class-wideness of the type of the expression's original node.
7342
 
7343
      if (Is_Class_Wide_Type (Target_Typ)
7344
           or else
7345
             (Is_Class_Wide_Type (Etype (Expr))
7346
               and then Is_Class_Wide_Type (Etype (Original_Node (Expr)))))
7347
        and then Base_Type (Etype (Expr)) /= Base_Type (Target_Typ)
7348
      then
7349
         Wrong_Type (Expr, Target_Typ);
7350
      end if;
7351
 
7352
      --  If the target type is unconstrained, then we reset the type of
7353
      --  the result from the type of the expression. For other cases, the
7354
      --  actual subtype of the expression is the target type.
7355
 
7356
      if Is_Composite_Type (Target_Typ)
7357
        and then not Is_Constrained (Target_Typ)
7358
      then
7359
         Set_Etype (N, Etype (Expr));
7360
      end if;
7361
 
7362
      Eval_Qualified_Expression (N);
7363
   end Resolve_Qualified_Expression;
7364
 
7365
   -------------------
7366
   -- Resolve_Range --
7367
   -------------------
7368
 
7369
   procedure Resolve_Range (N : Node_Id; Typ : Entity_Id) is
7370
      L : constant Node_Id := Low_Bound (N);
7371
      H : constant Node_Id := High_Bound (N);
7372
 
7373
   begin
7374
      Set_Etype (N, Typ);
7375
      Resolve (L, Typ);
7376
      Resolve (H, Typ);
7377
 
7378
      Check_Unset_Reference (L);
7379
      Check_Unset_Reference (H);
7380
 
7381
      --  We have to check the bounds for being within the base range as
7382
      --  required for a non-static context. Normally this is automatic and
7383
      --  done as part of evaluating expressions, but the N_Range node is an
7384
      --  exception, since in GNAT we consider this node to be a subexpression,
7385
      --  even though in Ada it is not. The circuit in Sem_Eval could check for
7386
      --  this, but that would put the test on the main evaluation path for
7387
      --  expressions.
7388
 
7389
      Check_Non_Static_Context (L);
7390
      Check_Non_Static_Context (H);
7391
 
7392
      --  Check for an ambiguous range over character literals. This will
7393
      --  happen with a membership test involving only literals.
7394
 
7395
      if Typ = Any_Character then
7396
         Ambiguous_Character (L);
7397
         Set_Etype (N, Any_Type);
7398
         return;
7399
      end if;
7400
 
7401
      --  If bounds are static, constant-fold them, so size computations
7402
      --  are identical between front-end and back-end. Do not perform this
7403
      --  transformation while analyzing generic units, as type information
7404
      --  would then be lost when reanalyzing the constant node in the
7405
      --  instance.
7406
 
7407
      if Is_Discrete_Type (Typ) and then Expander_Active then
7408
         if Is_OK_Static_Expression (L) then
7409
            Fold_Uint  (L, Expr_Value (L), Is_Static_Expression (L));
7410
         end if;
7411
 
7412
         if Is_OK_Static_Expression (H) then
7413
            Fold_Uint  (H, Expr_Value (H), Is_Static_Expression (H));
7414
         end if;
7415
      end if;
7416
   end Resolve_Range;
7417
 
7418
   --------------------------
7419
   -- Resolve_Real_Literal --
7420
   --------------------------
7421
 
7422
   procedure Resolve_Real_Literal (N : Node_Id; Typ : Entity_Id) is
7423
      Actual_Typ : constant Entity_Id := Etype (N);
7424
 
7425
   begin
7426
      --  Special processing for fixed-point literals to make sure that the
7427
      --  value is an exact multiple of small where this is required. We
7428
      --  skip this for the universal real case, and also for generic types.
7429
 
7430
      if Is_Fixed_Point_Type (Typ)
7431
        and then Typ /= Universal_Fixed
7432
        and then Typ /= Any_Fixed
7433
        and then not Is_Generic_Type (Typ)
7434
      then
7435
         declare
7436
            Val   : constant Ureal := Realval (N);
7437
            Cintr : constant Ureal := Val / Small_Value (Typ);
7438
            Cint  : constant Uint  := UR_Trunc (Cintr);
7439
            Den   : constant Uint  := Norm_Den (Cintr);
7440
            Stat  : Boolean;
7441
 
7442
         begin
7443
            --  Case of literal is not an exact multiple of the Small
7444
 
7445
            if Den /= 1 then
7446
 
7447
               --  For a source program literal for a decimal fixed-point
7448
               --  type, this is statically illegal (RM 4.9(36)).
7449
 
7450
               if Is_Decimal_Fixed_Point_Type (Typ)
7451
                 and then Actual_Typ = Universal_Real
7452
                 and then Comes_From_Source (N)
7453
               then
7454
                  Error_Msg_N ("value has extraneous low order digits", N);
7455
               end if;
7456
 
7457
               --  Generate a warning if literal from source
7458
 
7459
               if Is_Static_Expression (N)
7460
                 and then Warn_On_Bad_Fixed_Value
7461
               then
7462
                  Error_Msg_N
7463
                    ("?static fixed-point value is not a multiple of Small!",
7464
                     N);
7465
               end if;
7466
 
7467
               --  Replace literal by a value that is the exact representation
7468
               --  of a value of the type, i.e. a multiple of the small value,
7469
               --  by truncation, since Machine_Rounds is false for all GNAT
7470
               --  fixed-point types (RM 4.9(38)).
7471
 
7472
               Stat := Is_Static_Expression (N);
7473
               Rewrite (N,
7474
                 Make_Real_Literal (Sloc (N),
7475
                   Realval => Small_Value (Typ) * Cint));
7476
 
7477
               Set_Is_Static_Expression (N, Stat);
7478
            end if;
7479
 
7480
            --  In all cases, set the corresponding integer field
7481
 
7482
            Set_Corresponding_Integer_Value (N, Cint);
7483
         end;
7484
      end if;
7485
 
7486
      --  Now replace the actual type by the expected type as usual
7487
 
7488
      Set_Etype (N, Typ);
7489
      Eval_Real_Literal (N);
7490
   end Resolve_Real_Literal;
7491
 
7492
   -----------------------
7493
   -- Resolve_Reference --
7494
   -----------------------
7495
 
7496
   procedure Resolve_Reference (N : Node_Id; Typ : Entity_Id) is
7497
      P : constant Node_Id := Prefix (N);
7498
 
7499
   begin
7500
      --  Replace general access with specific type
7501
 
7502
      if Ekind (Etype (N)) = E_Allocator_Type then
7503
         Set_Etype (N, Base_Type (Typ));
7504
      end if;
7505
 
7506
      Resolve (P, Designated_Type (Etype (N)));
7507
 
7508
      --  If we are taking the reference of a volatile entity, then treat
7509
      --  it as a potential modification of this entity. This is much too
7510
      --  conservative, but is necessary because remove side effects can
7511
      --  result in transformations of normal assignments into reference
7512
      --  sequences that otherwise fail to notice the modification.
7513
 
7514
      if Is_Entity_Name (P) and then Treat_As_Volatile (Entity (P)) then
7515
         Note_Possible_Modification (P, Sure => False);
7516
      end if;
7517
   end Resolve_Reference;
7518
 
7519
   --------------------------------
7520
   -- Resolve_Selected_Component --
7521
   --------------------------------
7522
 
7523
   procedure Resolve_Selected_Component (N : Node_Id; Typ : Entity_Id) is
7524
      Comp  : Entity_Id;
7525
      Comp1 : Entity_Id        := Empty; -- prevent junk warning
7526
      P     : constant Node_Id := Prefix  (N);
7527
      S     : constant Node_Id := Selector_Name (N);
7528
      T     : Entity_Id        := Etype (P);
7529
      I     : Interp_Index;
7530
      I1    : Interp_Index := 0; -- prevent junk warning
7531
      It    : Interp;
7532
      It1   : Interp;
7533
      Found : Boolean;
7534
 
7535
      function Init_Component return Boolean;
7536
      --  Check whether this is the initialization of a component within an
7537
      --  init proc (by assignment or call to another init proc). If true,
7538
      --  there is no need for a discriminant check.
7539
 
7540
      --------------------
7541
      -- Init_Component --
7542
      --------------------
7543
 
7544
      function Init_Component return Boolean is
7545
      begin
7546
         return Inside_Init_Proc
7547
           and then Nkind (Prefix (N)) = N_Identifier
7548
           and then Chars (Prefix (N)) = Name_uInit
7549
           and then Nkind (Parent (Parent (N))) = N_Case_Statement_Alternative;
7550
      end Init_Component;
7551
 
7552
   --  Start of processing for Resolve_Selected_Component
7553
 
7554
   begin
7555
      if Is_Overloaded (P) then
7556
 
7557
         --  Use the context type to select the prefix that has a selector
7558
         --  of the correct name and type.
7559
 
7560
         Found := False;
7561
         Get_First_Interp (P, I, It);
7562
 
7563
         Search : while Present (It.Typ) loop
7564
            if Is_Access_Type (It.Typ) then
7565
               T := Designated_Type (It.Typ);
7566
            else
7567
               T := It.Typ;
7568
            end if;
7569
 
7570
            if Is_Record_Type (T) then
7571
 
7572
               --  The visible components of a class-wide type are those of
7573
               --  the root type.
7574
 
7575
               if Is_Class_Wide_Type (T) then
7576
                  T := Etype (T);
7577
               end if;
7578
 
7579
               Comp := First_Entity (T);
7580
               while Present (Comp) loop
7581
                  if Chars (Comp) = Chars (S)
7582
                    and then Covers (Etype (Comp), Typ)
7583
                  then
7584
                     if not Found then
7585
                        Found := True;
7586
                        I1  := I;
7587
                        It1 := It;
7588
                        Comp1 := Comp;
7589
 
7590
                     else
7591
                        It := Disambiguate (P, I1, I, Any_Type);
7592
 
7593
                        if It = No_Interp then
7594
                           Error_Msg_N
7595
                             ("ambiguous prefix for selected component",  N);
7596
                           Set_Etype (N, Typ);
7597
                           return;
7598
 
7599
                        else
7600
                           It1 := It;
7601
 
7602
                           --  There may be an implicit dereference. Retrieve
7603
                           --  designated record type.
7604
 
7605
                           if Is_Access_Type (It1.Typ) then
7606
                              T := Designated_Type (It1.Typ);
7607
                           else
7608
                              T := It1.Typ;
7609
                           end if;
7610
 
7611
                           if Scope (Comp1) /= T then
7612
 
7613
                              --  Resolution chooses the new interpretation.
7614
                              --  Find the component with the right name.
7615
 
7616
                              Comp1 := First_Entity (T);
7617
                              while Present (Comp1)
7618
                                and then Chars (Comp1) /= Chars (S)
7619
                              loop
7620
                                 Comp1 := Next_Entity (Comp1);
7621
                              end loop;
7622
                           end if;
7623
 
7624
                           exit Search;
7625
                        end if;
7626
                     end if;
7627
                  end if;
7628
 
7629
                  Comp := Next_Entity (Comp);
7630
               end loop;
7631
 
7632
            end if;
7633
 
7634
            Get_Next_Interp (I, It);
7635
         end loop Search;
7636
 
7637
         Resolve (P, It1.Typ);
7638
         Set_Etype (N, Typ);
7639
         Set_Entity_With_Style_Check (S, Comp1);
7640
 
7641
      else
7642
         --  Resolve prefix with its type
7643
 
7644
         Resolve (P, T);
7645
      end if;
7646
 
7647
      --  Generate cross-reference. We needed to wait until full overloading
7648
      --  resolution was complete to do this, since otherwise we can't tell if
7649
      --  we are an lvalue or not.
7650
 
7651
      if May_Be_Lvalue (N) then
7652
         Generate_Reference (Entity (S), S, 'm');
7653
      else
7654
         Generate_Reference (Entity (S), S, 'r');
7655
      end if;
7656
 
7657
      --  If prefix is an access type, the node will be transformed into an
7658
      --  explicit dereference during expansion. The type of the node is the
7659
      --  designated type of that of the prefix.
7660
 
7661
      if Is_Access_Type (Etype (P)) then
7662
         T := Designated_Type (Etype (P));
7663
         Check_Fully_Declared_Prefix (T, P);
7664
      else
7665
         T := Etype (P);
7666
      end if;
7667
 
7668
      if Has_Discriminants (T)
7669
        and then (Ekind (Entity (S)) = E_Component
7670
                   or else
7671
                  Ekind (Entity (S)) = E_Discriminant)
7672
        and then Present (Original_Record_Component (Entity (S)))
7673
        and then Ekind (Original_Record_Component (Entity (S))) = E_Component
7674
        and then Present (Discriminant_Checking_Func
7675
                           (Original_Record_Component (Entity (S))))
7676
        and then not Discriminant_Checks_Suppressed (T)
7677
        and then not Init_Component
7678
      then
7679
         Set_Do_Discriminant_Check (N);
7680
      end if;
7681
 
7682
      if Ekind (Entity (S)) = E_Void then
7683
         Error_Msg_N ("premature use of component", S);
7684
      end if;
7685
 
7686
      --  If the prefix is a record conversion, this may be a renamed
7687
      --  discriminant whose bounds differ from those of the original
7688
      --  one, so we must ensure that a range check is performed.
7689
 
7690
      if Nkind (P) = N_Type_Conversion
7691
        and then Ekind (Entity (S)) = E_Discriminant
7692
        and then Is_Discrete_Type (Typ)
7693
      then
7694
         Set_Etype (N, Base_Type (Typ));
7695
      end if;
7696
 
7697
      --  Note: No Eval processing is required, because the prefix is of a
7698
      --  record type, or protected type, and neither can possibly be static.
7699
 
7700
   end Resolve_Selected_Component;
7701
 
7702
   -------------------
7703
   -- Resolve_Shift --
7704
   -------------------
7705
 
7706
   procedure Resolve_Shift (N : Node_Id; Typ : Entity_Id) is
7707
      B_Typ : constant Entity_Id := Base_Type (Typ);
7708
      L     : constant Node_Id   := Left_Opnd  (N);
7709
      R     : constant Node_Id   := Right_Opnd (N);
7710
 
7711
   begin
7712
      --  We do the resolution using the base type, because intermediate values
7713
      --  in expressions always are of the base type, not a subtype of it.
7714
 
7715
      Resolve (L, B_Typ);
7716
      Resolve (R, Standard_Natural);
7717
 
7718
      Check_Unset_Reference (L);
7719
      Check_Unset_Reference (R);
7720
 
7721
      Set_Etype (N, B_Typ);
7722
      Generate_Operator_Reference (N, B_Typ);
7723
      Eval_Shift (N);
7724
   end Resolve_Shift;
7725
 
7726
   ---------------------------
7727
   -- Resolve_Short_Circuit --
7728
   ---------------------------
7729
 
7730
   procedure Resolve_Short_Circuit (N : Node_Id; Typ : Entity_Id) is
7731
      B_Typ : constant Entity_Id := Base_Type (Typ);
7732
      L     : constant Node_Id   := Left_Opnd  (N);
7733
      R     : constant Node_Id   := Right_Opnd (N);
7734
 
7735
   begin
7736
      Resolve (L, B_Typ);
7737
      Resolve (R, B_Typ);
7738
 
7739
      --  Check for issuing warning for always False assert/check, this happens
7740
      --  when assertions are turned off, in which case the pragma Assert/Check
7741
      --  was transformed into:
7742
 
7743
      --     if False and then <condition> then ...
7744
 
7745
      --  and we detect this pattern
7746
 
7747
      if Warn_On_Assertion_Failure
7748
        and then Is_Entity_Name (R)
7749
        and then Entity (R) = Standard_False
7750
        and then Nkind (Parent (N)) = N_If_Statement
7751
        and then Nkind (N) = N_And_Then
7752
        and then Is_Entity_Name (L)
7753
        and then Entity (L) = Standard_False
7754
      then
7755
         declare
7756
            Orig : constant Node_Id := Original_Node (Parent (N));
7757
 
7758
         begin
7759
            if Nkind (Orig) = N_Pragma
7760
              and then Pragma_Name (Orig) = Name_Assert
7761
            then
7762
               --  Don't want to warn if original condition is explicit False
7763
 
7764
               declare
7765
                  Expr : constant Node_Id :=
7766
                           Original_Node
7767
                             (Expression
7768
                               (First (Pragma_Argument_Associations (Orig))));
7769
               begin
7770
                  if Is_Entity_Name (Expr)
7771
                    and then Entity (Expr) = Standard_False
7772
                  then
7773
                     null;
7774
                  else
7775
                     --  Issue warning. Note that we don't want to make this
7776
                     --  an unconditional warning, because if the assert is
7777
                     --  within deleted code we do not want the warning. But
7778
                     --  we do not want the deletion of the IF/AND-THEN to
7779
                     --  take this message with it. We achieve this by making
7780
                     --  sure that the expanded code points to the Sloc of
7781
                     --  the expression, not the original pragma.
7782
 
7783
                     Error_Msg_N ("?assertion would fail at run-time", Orig);
7784
                  end if;
7785
               end;
7786
 
7787
            --  Similar processing for Check pragma
7788
 
7789
            elsif Nkind (Orig) = N_Pragma
7790
              and then Pragma_Name (Orig) = Name_Check
7791
            then
7792
               --  Don't want to warn if original condition is explicit False
7793
 
7794
               declare
7795
                  Expr : constant Node_Id :=
7796
                           Original_Node
7797
                             (Expression
7798
                                (Next (First
7799
                                  (Pragma_Argument_Associations (Orig)))));
7800
               begin
7801
                  if Is_Entity_Name (Expr)
7802
                    and then Entity (Expr) = Standard_False
7803
                  then
7804
                     null;
7805
                  else
7806
                     Error_Msg_N ("?check would fail at run-time", Orig);
7807
                  end if;
7808
               end;
7809
            end if;
7810
         end;
7811
      end if;
7812
 
7813
      --  Continue with processing of short circuit
7814
 
7815
      Check_Unset_Reference (L);
7816
      Check_Unset_Reference (R);
7817
 
7818
      Set_Etype (N, B_Typ);
7819
      Eval_Short_Circuit (N);
7820
   end Resolve_Short_Circuit;
7821
 
7822
   -------------------
7823
   -- Resolve_Slice --
7824
   -------------------
7825
 
7826
   procedure Resolve_Slice (N : Node_Id; Typ : Entity_Id) is
7827
      Name       : constant Node_Id := Prefix (N);
7828
      Drange     : constant Node_Id := Discrete_Range (N);
7829
      Array_Type : Entity_Id        := Empty;
7830
      Index      : Node_Id;
7831
 
7832
   begin
7833
      if Is_Overloaded (Name) then
7834
 
7835
         --  Use the context type to select the prefix that yields the correct
7836
         --  array type.
7837
 
7838
         declare
7839
            I      : Interp_Index;
7840
            I1     : Interp_Index := 0;
7841
            It     : Interp;
7842
            P      : constant Node_Id := Prefix (N);
7843
            Found  : Boolean := False;
7844
 
7845
         begin
7846
            Get_First_Interp (P, I,  It);
7847
            while Present (It.Typ) loop
7848
               if (Is_Array_Type (It.Typ)
7849
                    and then Covers (Typ,  It.Typ))
7850
                 or else (Is_Access_Type (It.Typ)
7851
                           and then Is_Array_Type (Designated_Type (It.Typ))
7852
                           and then Covers (Typ, Designated_Type (It.Typ)))
7853
               then
7854
                  if Found then
7855
                     It := Disambiguate (P, I1, I, Any_Type);
7856
 
7857
                     if It = No_Interp then
7858
                        Error_Msg_N ("ambiguous prefix for slicing",  N);
7859
                        Set_Etype (N, Typ);
7860
                        return;
7861
                     else
7862
                        Found := True;
7863
                        Array_Type := It.Typ;
7864
                        I1 := I;
7865
                     end if;
7866
                  else
7867
                     Found := True;
7868
                     Array_Type := It.Typ;
7869
                     I1 := I;
7870
                  end if;
7871
               end if;
7872
 
7873
               Get_Next_Interp (I, It);
7874
            end loop;
7875
         end;
7876
 
7877
      else
7878
         Array_Type := Etype (Name);
7879
      end if;
7880
 
7881
      Resolve (Name, Array_Type);
7882
 
7883
      if Is_Access_Type (Array_Type) then
7884
         Apply_Access_Check (N);
7885
         Array_Type := Designated_Type (Array_Type);
7886
 
7887
         --  If the prefix is an access to an unconstrained array, we must use
7888
         --  the actual subtype of the object to perform the index checks. The
7889
         --  object denoted by the prefix is implicit in the node, so we build
7890
         --  an explicit representation for it in order to compute the actual
7891
         --  subtype.
7892
 
7893
         if not Is_Constrained (Array_Type) then
7894
            Remove_Side_Effects (Prefix (N));
7895
 
7896
            declare
7897
               Obj : constant Node_Id :=
7898
                       Make_Explicit_Dereference (Sloc (N),
7899
                         Prefix => New_Copy_Tree (Prefix (N)));
7900
            begin
7901
               Set_Etype (Obj, Array_Type);
7902
               Set_Parent (Obj, Parent (N));
7903
               Array_Type := Get_Actual_Subtype (Obj);
7904
            end;
7905
         end if;
7906
 
7907
      elsif Is_Entity_Name (Name)
7908
        or else (Nkind (Name) = N_Function_Call
7909
                  and then not Is_Constrained (Etype (Name)))
7910
      then
7911
         Array_Type := Get_Actual_Subtype (Name);
7912
 
7913
      --  If the name is a selected component that depends on discriminants,
7914
      --  build an actual subtype for it. This can happen only when the name
7915
      --  itself is overloaded; otherwise the actual subtype is created when
7916
      --  the selected component is analyzed.
7917
 
7918
      elsif Nkind (Name) = N_Selected_Component
7919
        and then Full_Analysis
7920
        and then Depends_On_Discriminant (First_Index (Array_Type))
7921
      then
7922
         declare
7923
            Act_Decl : constant Node_Id :=
7924
                         Build_Actual_Subtype_Of_Component (Array_Type, Name);
7925
         begin
7926
            Insert_Action (N, Act_Decl);
7927
            Array_Type := Defining_Identifier (Act_Decl);
7928
         end;
7929
 
7930
      --  Maybe this should just be "else", instead of checking for the
7931
      --  specific case of slice??? This is needed for the case where
7932
      --  the prefix is an Image attribute, which gets expanded to a
7933
      --  slice, and so has a constrained subtype which we want to use
7934
      --  for the slice range check applied below (the range check won't
7935
      --  get done if the unconstrained subtype of the 'Image is used).
7936
 
7937
      elsif Nkind (Name) = N_Slice then
7938
         Array_Type := Etype (Name);
7939
      end if;
7940
 
7941
      --  If name was overloaded, set slice type correctly now
7942
 
7943
      Set_Etype (N, Array_Type);
7944
 
7945
      --  If the range is specified by a subtype mark, no resolution is
7946
      --  necessary. Else resolve the bounds, and apply needed checks.
7947
 
7948
      if not Is_Entity_Name (Drange) then
7949
         Index := First_Index (Array_Type);
7950
         Resolve (Drange, Base_Type (Etype (Index)));
7951
 
7952
         if Nkind (Drange) = N_Range
7953
 
7954
            --  Do not apply the range check to nodes associated with the
7955
            --  frontend expansion of the dispatch table. We first check
7956
            --  if Ada.Tags is already loaded to void the addition of an
7957
            --  undesired dependence on such run-time unit.
7958
 
7959
           and then
7960
             (not Tagged_Type_Expansion
7961
               or else not
7962
                 (RTU_Loaded (Ada_Tags)
7963
                   and then Nkind (Prefix (N)) = N_Selected_Component
7964
                   and then Present (Entity (Selector_Name (Prefix (N))))
7965
                   and then Entity (Selector_Name (Prefix (N))) =
7966
                                         RTE_Record_Component (RE_Prims_Ptr)))
7967
         then
7968
            Apply_Range_Check (Drange, Etype (Index));
7969
         end if;
7970
      end if;
7971
 
7972
      Set_Slice_Subtype (N);
7973
 
7974
      if Nkind (Drange) = N_Range then
7975
         Warn_On_Suspicious_Index (Name, Low_Bound  (Drange));
7976
         Warn_On_Suspicious_Index (Name, High_Bound (Drange));
7977
      end if;
7978
 
7979
      Eval_Slice (N);
7980
   end Resolve_Slice;
7981
 
7982
   ----------------------------
7983
   -- Resolve_String_Literal --
7984
   ----------------------------
7985
 
7986
   procedure Resolve_String_Literal (N : Node_Id; Typ : Entity_Id) is
7987
      C_Typ      : constant Entity_Id  := Component_Type (Typ);
7988
      R_Typ      : constant Entity_Id  := Root_Type (C_Typ);
7989
      Loc        : constant Source_Ptr := Sloc (N);
7990
      Str        : constant String_Id  := Strval (N);
7991
      Strlen     : constant Nat        := String_Length (Str);
7992
      Subtype_Id : Entity_Id;
7993
      Need_Check : Boolean;
7994
 
7995
   begin
7996
      --  For a string appearing in a concatenation, defer creation of the
7997
      --  string_literal_subtype until the end of the resolution of the
7998
      --  concatenation, because the literal may be constant-folded away. This
7999
      --  is a useful optimization for long concatenation expressions.
8000
 
8001
      --  If the string is an aggregate built for a single character (which
8002
      --  happens in a non-static context) or a is null string to which special
8003
      --  checks may apply, we build the subtype. Wide strings must also get a
8004
      --  string subtype if they come from a one character aggregate. Strings
8005
      --  generated by attributes might be static, but it is often hard to
8006
      --  determine whether the enclosing context is static, so we generate
8007
      --  subtypes for them as well, thus losing some rarer optimizations ???
8008
      --  Same for strings that come from a static conversion.
8009
 
8010
      Need_Check :=
8011
        (Strlen = 0 and then Typ /= Standard_String)
8012
          or else Nkind (Parent (N)) /= N_Op_Concat
8013
          or else (N /= Left_Opnd (Parent (N))
8014
                    and then N /= Right_Opnd (Parent (N)))
8015
          or else ((Typ = Standard_Wide_String
8016
                      or else Typ = Standard_Wide_Wide_String)
8017
                    and then Nkind (Original_Node (N)) /= N_String_Literal);
8018
 
8019
      --  If the resolving type is itself a string literal subtype, we can just
8020
      --  reuse it, since there is no point in creating another.
8021
 
8022
      if Ekind (Typ) = E_String_Literal_Subtype then
8023
         Subtype_Id := Typ;
8024
 
8025
      elsif Nkind (Parent (N)) = N_Op_Concat
8026
        and then not Need_Check
8027
        and then not Nkind_In (Original_Node (N), N_Character_Literal,
8028
                                                  N_Attribute_Reference,
8029
                                                  N_Qualified_Expression,
8030
                                                  N_Type_Conversion)
8031
      then
8032
         Subtype_Id := Typ;
8033
 
8034
      --  Otherwise we must create a string literal subtype. Note that the
8035
      --  whole idea of string literal subtypes is simply to avoid the need
8036
      --  for building a full fledged array subtype for each literal.
8037
 
8038
      else
8039
         Set_String_Literal_Subtype (N, Typ);
8040
         Subtype_Id := Etype (N);
8041
      end if;
8042
 
8043
      if Nkind (Parent (N)) /= N_Op_Concat
8044
        or else Need_Check
8045
      then
8046
         Set_Etype (N, Subtype_Id);
8047
         Eval_String_Literal (N);
8048
      end if;
8049
 
8050
      if Is_Limited_Composite (Typ)
8051
        or else Is_Private_Composite (Typ)
8052
      then
8053
         Error_Msg_N ("string literal not available for private array", N);
8054
         Set_Etype (N, Any_Type);
8055
         return;
8056
      end if;
8057
 
8058
      --  The validity of a null string has been checked in the call to
8059
      --  Eval_String_Literal.
8060
 
8061
      if Strlen = 0 then
8062
         return;
8063
 
8064
      --  Always accept string literal with component type Any_Character, which
8065
      --  occurs in error situations and in comparisons of literals, both of
8066
      --  which should accept all literals.
8067
 
8068
      elsif R_Typ = Any_Character then
8069
         return;
8070
 
8071
      --  If the type is bit-packed, then we always transform the string
8072
      --  literal into a full fledged aggregate.
8073
 
8074
      elsif Is_Bit_Packed_Array (Typ) then
8075
         null;
8076
 
8077
      --  Deal with cases of Wide_Wide_String, Wide_String, and String
8078
 
8079
      else
8080
         --  For Standard.Wide_Wide_String, or any other type whose component
8081
         --  type is Standard.Wide_Wide_Character, we know that all the
8082
         --  characters in the string must be acceptable, since the parser
8083
         --  accepted the characters as valid character literals.
8084
 
8085
         if R_Typ = Standard_Wide_Wide_Character then
8086
            null;
8087
 
8088
         --  For the case of Standard.String, or any other type whose component
8089
         --  type is Standard.Character, we must make sure that there are no
8090
         --  wide characters in the string, i.e. that it is entirely composed
8091
         --  of characters in range of type Character.
8092
 
8093
         --  If the string literal is the result of a static concatenation, the
8094
         --  test has already been performed on the components, and need not be
8095
         --  repeated.
8096
 
8097
         elsif R_Typ = Standard_Character
8098
           and then Nkind (Original_Node (N)) /= N_Op_Concat
8099
         then
8100
            for J in 1 .. Strlen loop
8101
               if not In_Character_Range (Get_String_Char (Str, J)) then
8102
 
8103
                  --  If we are out of range, post error. This is one of the
8104
                  --  very few places that we place the flag in the middle of
8105
                  --  a token, right under the offending wide character. Not
8106
                  --  quite clear if this is right wrt wide character encoding
8107
                  --  sequences, but it's only an error message!
8108
 
8109
                  Error_Msg
8110
                    ("literal out of range of type Standard.Character",
8111
                     Source_Ptr (Int (Loc) + J));
8112
                  return;
8113
               end if;
8114
            end loop;
8115
 
8116
         --  For the case of Standard.Wide_String, or any other type whose
8117
         --  component type is Standard.Wide_Character, we must make sure that
8118
         --  there are no wide characters in the string, i.e. that it is
8119
         --  entirely composed of characters in range of type Wide_Character.
8120
 
8121
         --  If the string literal is the result of a static concatenation,
8122
         --  the test has already been performed on the components, and need
8123
         --  not be repeated.
8124
 
8125
         elsif R_Typ = Standard_Wide_Character
8126
           and then Nkind (Original_Node (N)) /= N_Op_Concat
8127
         then
8128
            for J in 1 .. Strlen loop
8129
               if not In_Wide_Character_Range (Get_String_Char (Str, J)) then
8130
 
8131
                  --  If we are out of range, post error. This is one of the
8132
                  --  very few places that we place the flag in the middle of
8133
                  --  a token, right under the offending wide character.
8134
 
8135
                  --  This is not quite right, because characters in general
8136
                  --  will take more than one character position ???
8137
 
8138
                  Error_Msg
8139
                    ("literal out of range of type Standard.Wide_Character",
8140
                     Source_Ptr (Int (Loc) + J));
8141
                  return;
8142
               end if;
8143
            end loop;
8144
 
8145
         --  If the root type is not a standard character, then we will convert
8146
         --  the string into an aggregate and will let the aggregate code do
8147
         --  the checking. Standard Wide_Wide_Character is also OK here.
8148
 
8149
         else
8150
            null;
8151
         end if;
8152
 
8153
         --  See if the component type of the array corresponding to the string
8154
         --  has compile time known bounds. If yes we can directly check
8155
         --  whether the evaluation of the string will raise constraint error.
8156
         --  Otherwise we need to transform the string literal into the
8157
         --  corresponding character aggregate and let the aggregate
8158
         --  code do the checking.
8159
 
8160
         if Is_Standard_Character_Type (R_Typ) then
8161
 
8162
            --  Check for the case of full range, where we are definitely OK
8163
 
8164
            if Component_Type (Typ) = Base_Type (Component_Type (Typ)) then
8165
               return;
8166
            end if;
8167
 
8168
            --  Here the range is not the complete base type range, so check
8169
 
8170
            declare
8171
               Comp_Typ_Lo : constant Node_Id :=
8172
                               Type_Low_Bound (Component_Type (Typ));
8173
               Comp_Typ_Hi : constant Node_Id :=
8174
                               Type_High_Bound (Component_Type (Typ));
8175
 
8176
               Char_Val : Uint;
8177
 
8178
            begin
8179
               if Compile_Time_Known_Value (Comp_Typ_Lo)
8180
                 and then Compile_Time_Known_Value (Comp_Typ_Hi)
8181
               then
8182
                  for J in 1 .. Strlen loop
8183
                     Char_Val := UI_From_Int (Int (Get_String_Char (Str, J)));
8184
 
8185
                     if Char_Val < Expr_Value (Comp_Typ_Lo)
8186
                       or else Char_Val > Expr_Value (Comp_Typ_Hi)
8187
                     then
8188
                        Apply_Compile_Time_Constraint_Error
8189
                          (N, "character out of range?", CE_Range_Check_Failed,
8190
                           Loc => Source_Ptr (Int (Loc) + J));
8191
                     end if;
8192
                  end loop;
8193
 
8194
                  return;
8195
               end if;
8196
            end;
8197
         end if;
8198
      end if;
8199
 
8200
      --  If we got here we meed to transform the string literal into the
8201
      --  equivalent qualified positional array aggregate. This is rather
8202
      --  heavy artillery for this situation, but it is hard work to avoid.
8203
 
8204
      declare
8205
         Lits : constant List_Id    := New_List;
8206
         P    : Source_Ptr := Loc + 1;
8207
         C    : Char_Code;
8208
 
8209
      begin
8210
         --  Build the character literals, we give them source locations that
8211
         --  correspond to the string positions, which is a bit tricky given
8212
         --  the possible presence of wide character escape sequences.
8213
 
8214
         for J in 1 .. Strlen loop
8215
            C := Get_String_Char (Str, J);
8216
            Set_Character_Literal_Name (C);
8217
 
8218
            Append_To (Lits,
8219
              Make_Character_Literal (P,
8220
                Chars              => Name_Find,
8221
                Char_Literal_Value => UI_From_CC (C)));
8222
 
8223
            if In_Character_Range (C) then
8224
               P := P + 1;
8225
 
8226
            --  Should we have a call to Skip_Wide here ???
8227
            --  ???     else
8228
            --             Skip_Wide (P);
8229
 
8230
            end if;
8231
         end loop;
8232
 
8233
         Rewrite (N,
8234
           Make_Qualified_Expression (Loc,
8235
             Subtype_Mark => New_Reference_To (Typ, Loc),
8236
             Expression   =>
8237
               Make_Aggregate (Loc, Expressions => Lits)));
8238
 
8239
         Analyze_And_Resolve (N, Typ);
8240
      end;
8241
   end Resolve_String_Literal;
8242
 
8243
   -----------------------------
8244
   -- Resolve_Subprogram_Info --
8245
   -----------------------------
8246
 
8247
   procedure Resolve_Subprogram_Info (N : Node_Id; Typ : Entity_Id) is
8248
   begin
8249
      Set_Etype (N, Typ);
8250
   end Resolve_Subprogram_Info;
8251
 
8252
   -----------------------------
8253
   -- Resolve_Type_Conversion --
8254
   -----------------------------
8255
 
8256
   procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
8257
      Conv_OK     : constant Boolean   := Conversion_OK (N);
8258
      Operand     : constant Node_Id   := Expression (N);
8259
      Operand_Typ : constant Entity_Id := Etype (Operand);
8260
      Target_Typ  : constant Entity_Id := Etype (N);
8261
      Rop         : Node_Id;
8262
      Orig_N      : Node_Id;
8263
      Orig_T      : Node_Id;
8264
 
8265
   begin
8266
      if not Conv_OK
8267
        and then not Valid_Conversion (N, Target_Typ, Operand)
8268
      then
8269
         return;
8270
      end if;
8271
 
8272
      if Etype (Operand) = Any_Fixed then
8273
 
8274
         --  Mixed-mode operation involving a literal. Context must be a fixed
8275
         --  type which is applied to the literal subsequently.
8276
 
8277
         if Is_Fixed_Point_Type (Typ) then
8278
            Set_Etype (Operand, Universal_Real);
8279
 
8280
         elsif Is_Numeric_Type (Typ)
8281
           and then Nkind_In (Operand, N_Op_Multiply, N_Op_Divide)
8282
           and then (Etype (Right_Opnd (Operand)) = Universal_Real
8283
                       or else
8284
                     Etype (Left_Opnd  (Operand)) = Universal_Real)
8285
         then
8286
            --  Return if expression is ambiguous
8287
 
8288
            if Unique_Fixed_Point_Type (N) = Any_Type then
8289
               return;
8290
 
8291
            --  If nothing else, the available fixed type is Duration
8292
 
8293
            else
8294
               Set_Etype (Operand, Standard_Duration);
8295
            end if;
8296
 
8297
            --  Resolve the real operand with largest available precision
8298
 
8299
            if Etype (Right_Opnd (Operand)) = Universal_Real then
8300
               Rop := New_Copy_Tree (Right_Opnd (Operand));
8301
            else
8302
               Rop := New_Copy_Tree (Left_Opnd (Operand));
8303
            end if;
8304
 
8305
            Resolve (Rop, Universal_Real);
8306
 
8307
            --  If the operand is a literal (it could be a non-static and
8308
            --  illegal exponentiation) check whether the use of Duration
8309
            --  is potentially inaccurate.
8310
 
8311
            if Nkind (Rop) = N_Real_Literal
8312
              and then Realval (Rop) /= Ureal_0
8313
              and then abs (Realval (Rop)) < Delta_Value (Standard_Duration)
8314
            then
8315
               Error_Msg_N
8316
                 ("?universal real operand can only " &
8317
                  "be interpreted as Duration!",
8318
                  Rop);
8319
               Error_Msg_N
8320
                 ("\?precision will be lost in the conversion!", Rop);
8321
            end if;
8322
 
8323
         elsif Is_Numeric_Type (Typ)
8324
           and then Nkind (Operand) in N_Op
8325
           and then Unique_Fixed_Point_Type (N) /= Any_Type
8326
         then
8327
            Set_Etype (Operand, Standard_Duration);
8328
 
8329
         else
8330
            Error_Msg_N ("invalid context for mixed mode operation", N);
8331
            Set_Etype (Operand, Any_Type);
8332
            return;
8333
         end if;
8334
      end if;
8335
 
8336
      Resolve (Operand);
8337
 
8338
      --  Note: we do the Eval_Type_Conversion call before applying the
8339
      --  required checks for a subtype conversion. This is important, since
8340
      --  both are prepared under certain circumstances to change the type
8341
      --  conversion to a constraint error node, but in the case of
8342
      --  Eval_Type_Conversion this may reflect an illegality in the static
8343
      --  case, and we would miss the illegality (getting only a warning
8344
      --  message), if we applied the type conversion checks first.
8345
 
8346
      Eval_Type_Conversion (N);
8347
 
8348
      --  Even when evaluation is not possible, we may be able to simplify the
8349
      --  conversion or its expression. This needs to be done before applying
8350
      --  checks, since otherwise the checks may use the original expression
8351
      --  and defeat the simplifications. This is specifically the case for
8352
      --  elimination of the floating-point Truncation attribute in
8353
      --  float-to-int conversions.
8354
 
8355
      Simplify_Type_Conversion (N);
8356
 
8357
      --  If after evaluation we still have a type conversion, then we may need
8358
      --  to apply checks required for a subtype conversion.
8359
 
8360
      --  Skip these type conversion checks if universal fixed operands
8361
      --  operands involved, since range checks are handled separately for
8362
      --  these cases (in the appropriate Expand routines in unit Exp_Fixd).
8363
 
8364
      if Nkind (N) = N_Type_Conversion
8365
        and then not Is_Generic_Type (Root_Type (Target_Typ))
8366
        and then Target_Typ  /= Universal_Fixed
8367
        and then Operand_Typ /= Universal_Fixed
8368
      then
8369
         Apply_Type_Conversion_Checks (N);
8370
      end if;
8371
 
8372
      --  Issue warning for conversion of simple object to its own type. We
8373
      --  have to test the original nodes, since they may have been rewritten
8374
      --  by various optimizations.
8375
 
8376
      Orig_N := Original_Node (N);
8377
 
8378
      if Warn_On_Redundant_Constructs
8379
        and then Comes_From_Source (Orig_N)
8380
        and then Nkind (Orig_N) = N_Type_Conversion
8381
        and then not In_Instance
8382
      then
8383
         Orig_N := Original_Node (Expression (Orig_N));
8384
         Orig_T := Target_Typ;
8385
 
8386
         --  If the node is part of a larger expression, the Target_Type
8387
         --  may not be the original type of the node if the context is a
8388
         --  condition. Recover original type to see if conversion is needed.
8389
 
8390
         if Is_Boolean_Type (Orig_T)
8391
          and then Nkind (Parent (N)) in N_Op
8392
         then
8393
            Orig_T := Etype (Parent (N));
8394
         end if;
8395
 
8396
         if Is_Entity_Name (Orig_N)
8397
           and then
8398
             (Etype (Entity (Orig_N)) = Orig_T
8399
                or else
8400
                  (Ekind (Entity (Orig_N)) = E_Loop_Parameter
8401
                     and then Covers (Orig_T, Etype (Entity (Orig_N)))))
8402
         then
8403
            --  One more check, do not give warning if the analyzed conversion
8404
            --  has an expression with non-static bounds, and the bounds of the
8405
            --  target are static. This avoids junk warnings in cases where the
8406
            --  conversion is necessary to establish staticness, for example in
8407
            --  a case statement.
8408
 
8409
            if not Is_OK_Static_Subtype (Operand_Typ)
8410
              and then Is_OK_Static_Subtype (Target_Typ)
8411
            then
8412
               null;
8413
 
8414
            --  Here we give the redundant conversion warning
8415
 
8416
            else
8417
               Error_Msg_Node_2 := Orig_T;
8418
               Error_Msg_NE -- CODEFIX
8419
                 ("?redundant conversion, & is of type &!",
8420
                  N, Entity (Orig_N));
8421
            end if;
8422
         end if;
8423
      end if;
8424
 
8425
      --  Ada 2005 (AI-251): Handle class-wide interface type conversions.
8426
      --  No need to perform any interface conversion if the type of the
8427
      --  expression coincides with the target type.
8428
 
8429
      if Ada_Version >= Ada_05
8430
        and then Expander_Active
8431
        and then Operand_Typ /= Target_Typ
8432
      then
8433
         declare
8434
            Opnd   : Entity_Id := Operand_Typ;
8435
            Target : Entity_Id := Target_Typ;
8436
 
8437
         begin
8438
            if Is_Access_Type (Opnd) then
8439
               Opnd := Directly_Designated_Type (Opnd);
8440
            end if;
8441
 
8442
            if Is_Access_Type (Target_Typ) then
8443
               Target := Directly_Designated_Type (Target);
8444
            end if;
8445
 
8446
            if Opnd = Target then
8447
               null;
8448
 
8449
            --  Conversion from interface type
8450
 
8451
            elsif Is_Interface (Opnd) then
8452
 
8453
               --  Ada 2005 (AI-217): Handle entities from limited views
8454
 
8455
               if From_With_Type (Opnd) then
8456
                  Error_Msg_Qual_Level := 99;
8457
                  Error_Msg_NE ("missing WITH clause on package &", N,
8458
                    Cunit_Entity (Get_Source_Unit (Base_Type (Opnd))));
8459
                  Error_Msg_N
8460
                    ("type conversions require visibility of the full view",
8461
                     N);
8462
 
8463
               elsif From_With_Type (Target)
8464
                 and then not
8465
                   (Is_Access_Type (Target_Typ)
8466
                      and then Present (Non_Limited_View (Etype (Target))))
8467
               then
8468
                  Error_Msg_Qual_Level := 99;
8469
                  Error_Msg_NE ("missing WITH clause on package &", N,
8470
                    Cunit_Entity (Get_Source_Unit (Base_Type (Target))));
8471
                  Error_Msg_N
8472
                    ("type conversions require visibility of the full view",
8473
                     N);
8474
 
8475
               else
8476
                  Expand_Interface_Conversion (N, Is_Static => False);
8477
               end if;
8478
 
8479
            --  Conversion to interface type
8480
 
8481
            elsif Is_Interface (Target) then
8482
 
8483
               --  Handle subtypes
8484
 
8485
               if Ekind (Opnd) = E_Protected_Subtype
8486
                 or else Ekind (Opnd) = E_Task_Subtype
8487
               then
8488
                  Opnd := Etype (Opnd);
8489
               end if;
8490
 
8491
               if not Interface_Present_In_Ancestor
8492
                        (Typ   => Opnd,
8493
                         Iface => Target)
8494
               then
8495
                  if Is_Class_Wide_Type (Opnd) then
8496
 
8497
                     --  The static analysis is not enough to know if the
8498
                     --  interface is implemented or not. Hence we must pass
8499
                     --  the work to the expander to generate code to evaluate
8500
                     --  the conversion at run-time.
8501
 
8502
                     Expand_Interface_Conversion (N, Is_Static => False);
8503
 
8504
                  else
8505
                     Error_Msg_Name_1 := Chars (Etype (Target));
8506
                     Error_Msg_Name_2 := Chars (Opnd);
8507
                     Error_Msg_N
8508
                       ("wrong interface conversion (% is not a progenitor " &
8509
                        "of %)", N);
8510
                  end if;
8511
 
8512
               else
8513
                  Expand_Interface_Conversion (N);
8514
               end if;
8515
            end if;
8516
         end;
8517
      end if;
8518
   end Resolve_Type_Conversion;
8519
 
8520
   ----------------------
8521
   -- Resolve_Unary_Op --
8522
   ----------------------
8523
 
8524
   procedure Resolve_Unary_Op (N : Node_Id; Typ : Entity_Id) is
8525
      B_Typ : constant Entity_Id := Base_Type (Typ);
8526
      R     : constant Node_Id   := Right_Opnd (N);
8527
      OK    : Boolean;
8528
      Lo    : Uint;
8529
      Hi    : Uint;
8530
 
8531
   begin
8532
      --  Deal with intrinsic unary operators
8533
 
8534
      if Comes_From_Source (N)
8535
        and then Ekind (Entity (N)) = E_Function
8536
        and then Is_Imported (Entity (N))
8537
        and then Is_Intrinsic_Subprogram (Entity (N))
8538
      then
8539
         Resolve_Intrinsic_Unary_Operator (N, Typ);
8540
         return;
8541
      end if;
8542
 
8543
      --  Deal with universal cases
8544
 
8545
      if Etype (R) = Universal_Integer
8546
           or else
8547
         Etype (R) = Universal_Real
8548
      then
8549
         Check_For_Visible_Operator (N, B_Typ);
8550
      end if;
8551
 
8552
      Set_Etype (N, B_Typ);
8553
      Resolve (R, B_Typ);
8554
 
8555
      --  Generate warning for expressions like abs (x mod 2)
8556
 
8557
      if Warn_On_Redundant_Constructs
8558
        and then Nkind (N) = N_Op_Abs
8559
      then
8560
         Determine_Range (Right_Opnd (N), OK, Lo, Hi);
8561
 
8562
         if OK and then Hi >= Lo and then Lo >= 0 then
8563
            Error_Msg_N
8564
             ("?abs applied to known non-negative value has no effect", N);
8565
         end if;
8566
      end if;
8567
 
8568
      --  Deal with reference generation
8569
 
8570
      Check_Unset_Reference (R);
8571
      Generate_Operator_Reference (N, B_Typ);
8572
      Eval_Unary_Op (N);
8573
 
8574
      --  Set overflow checking bit. Much cleverer code needed here eventually
8575
      --  and perhaps the Resolve routines should be separated for the various
8576
      --  arithmetic operations, since they will need different processing ???
8577
 
8578
      if Nkind (N) in N_Op then
8579
         if not Overflow_Checks_Suppressed (Etype (N)) then
8580
            Enable_Overflow_Check (N);
8581
         end if;
8582
      end if;
8583
 
8584
      --  Generate warning for expressions like -5 mod 3 for integers. No need
8585
      --  to worry in the floating-point case, since parens do not affect the
8586
      --  result so there is no point in giving in a warning.
8587
 
8588
      declare
8589
         Norig : constant Node_Id := Original_Node (N);
8590
         Rorig : Node_Id;
8591
         Val   : Uint;
8592
         HB    : Uint;
8593
         LB    : Uint;
8594
         Lval  : Uint;
8595
         Opnd  : Node_Id;
8596
 
8597
      begin
8598
         if Warn_On_Questionable_Missing_Parens
8599
           and then Comes_From_Source (Norig)
8600
           and then Is_Integer_Type (Typ)
8601
           and then Nkind (Norig) = N_Op_Minus
8602
         then
8603
            Rorig := Original_Node (Right_Opnd (Norig));
8604
 
8605
            --  We are looking for cases where the right operand is not
8606
            --  parenthesized, and is a binary operator, multiply, divide, or
8607
            --  mod. These are the cases where the grouping can affect results.
8608
 
8609
            if Paren_Count (Rorig) = 0
8610
              and then Nkind_In (Rorig, N_Op_Mod, N_Op_Multiply, N_Op_Divide)
8611
            then
8612
               --  For mod, we always give the warning, since the value is
8613
               --  affected by the parenthesization (e.g. (-5) mod 315 /=
8614
               --  -(5 mod 315)). But for the other cases, the only concern is
8615
               --  overflow, e.g. for the case of 8 big signed (-(2 * 64)
8616
               --  overflows, but (-2) * 64 does not). So we try to give the
8617
               --  message only when overflow is possible.
8618
 
8619
               if Nkind (Rorig) /= N_Op_Mod
8620
                 and then Compile_Time_Known_Value (R)
8621
               then
8622
                  Val := Expr_Value (R);
8623
 
8624
                  if Compile_Time_Known_Value (Type_High_Bound (Typ)) then
8625
                     HB := Expr_Value (Type_High_Bound (Typ));
8626
                  else
8627
                     HB := Expr_Value (Type_High_Bound (Base_Type (Typ)));
8628
                  end if;
8629
 
8630
                  if Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
8631
                     LB := Expr_Value (Type_Low_Bound (Typ));
8632
                  else
8633
                     LB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
8634
                  end if;
8635
 
8636
                  --  Note that the test below is deliberately excluding the
8637
                  --  largest negative number, since that is a potentially
8638
                  --  troublesome case (e.g. -2 * x, where the result is the
8639
                  --  largest negative integer has an overflow with 2 * x).
8640
 
8641
                  if Val > LB and then Val <= HB then
8642
                     return;
8643
                  end if;
8644
               end if;
8645
 
8646
               --  For the multiplication case, the only case we have to worry
8647
               --  about is when (-a)*b is exactly the largest negative number
8648
               --  so that -(a*b) can cause overflow. This can only happen if
8649
               --  a is a power of 2, and more generally if any operand is a
8650
               --  constant that is not a power of 2, then the parentheses
8651
               --  cannot affect whether overflow occurs. We only bother to
8652
               --  test the left most operand
8653
 
8654
               --  Loop looking at left operands for one that has known value
8655
 
8656
               Opnd := Rorig;
8657
               Opnd_Loop : while Nkind (Opnd) = N_Op_Multiply loop
8658
                  if Compile_Time_Known_Value (Left_Opnd (Opnd)) then
8659
                     Lval := UI_Abs (Expr_Value (Left_Opnd (Opnd)));
8660
 
8661
                     --  Operand value of 0 or 1 skips warning
8662
 
8663
                     if Lval <= 1 then
8664
                        return;
8665
 
8666
                     --  Otherwise check power of 2, if power of 2, warn, if
8667
                     --  anything else, skip warning.
8668
 
8669
                     else
8670
                        while Lval /= 2 loop
8671
                           if Lval mod 2 = 1 then
8672
                              return;
8673
                           else
8674
                              Lval := Lval / 2;
8675
                           end if;
8676
                        end loop;
8677
 
8678
                        exit Opnd_Loop;
8679
                     end if;
8680
                  end if;
8681
 
8682
                  --  Keep looking at left operands
8683
 
8684
                  Opnd := Left_Opnd (Opnd);
8685
               end loop Opnd_Loop;
8686
 
8687
               --  For rem or "/" we can only have a problematic situation
8688
               --  if the divisor has a value of minus one or one. Otherwise
8689
               --  overflow is impossible (divisor > 1) or we have a case of
8690
               --  division by zero in any case.
8691
 
8692
               if Nkind_In (Rorig, N_Op_Divide, N_Op_Rem)
8693
                 and then Compile_Time_Known_Value (Right_Opnd (Rorig))
8694
                 and then UI_Abs (Expr_Value (Right_Opnd (Rorig))) /= 1
8695
               then
8696
                  return;
8697
               end if;
8698
 
8699
               --  If we fall through warning should be issued
8700
 
8701
               Error_Msg_N
8702
                 ("?unary minus expression should be parenthesized here!", N);
8703
            end if;
8704
         end if;
8705
      end;
8706
   end Resolve_Unary_Op;
8707
 
8708
   ----------------------------------
8709
   -- Resolve_Unchecked_Expression --
8710
   ----------------------------------
8711
 
8712
   procedure Resolve_Unchecked_Expression
8713
     (N   : Node_Id;
8714
      Typ : Entity_Id)
8715
   is
8716
   begin
8717
      Resolve (Expression (N), Typ, Suppress => All_Checks);
8718
      Set_Etype (N, Typ);
8719
   end Resolve_Unchecked_Expression;
8720
 
8721
   ---------------------------------------
8722
   -- Resolve_Unchecked_Type_Conversion --
8723
   ---------------------------------------
8724
 
8725
   procedure Resolve_Unchecked_Type_Conversion
8726
     (N   : Node_Id;
8727
      Typ : Entity_Id)
8728
   is
8729
      pragma Warnings (Off, Typ);
8730
 
8731
      Operand   : constant Node_Id   := Expression (N);
8732
      Opnd_Type : constant Entity_Id := Etype (Operand);
8733
 
8734
   begin
8735
      --  Resolve operand using its own type
8736
 
8737
      Resolve (Operand, Opnd_Type);
8738
      Eval_Unchecked_Conversion (N);
8739
 
8740
   end Resolve_Unchecked_Type_Conversion;
8741
 
8742
   ------------------------------
8743
   -- Rewrite_Operator_As_Call --
8744
   ------------------------------
8745
 
8746
   procedure Rewrite_Operator_As_Call (N : Node_Id; Nam : Entity_Id) is
8747
      Loc     : constant Source_Ptr := Sloc (N);
8748
      Actuals : constant List_Id    := New_List;
8749
      New_N   : Node_Id;
8750
 
8751
   begin
8752
      if Nkind (N) in  N_Binary_Op then
8753
         Append (Left_Opnd (N), Actuals);
8754
      end if;
8755
 
8756
      Append (Right_Opnd (N), Actuals);
8757
 
8758
      New_N :=
8759
        Make_Function_Call (Sloc => Loc,
8760
          Name => New_Occurrence_Of (Nam, Loc),
8761
          Parameter_Associations => Actuals);
8762
 
8763
      Preserve_Comes_From_Source (New_N, N);
8764
      Preserve_Comes_From_Source (Name (New_N), N);
8765
      Rewrite (N, New_N);
8766
      Set_Etype (N, Etype (Nam));
8767
   end Rewrite_Operator_As_Call;
8768
 
8769
   ------------------------------
8770
   -- Rewrite_Renamed_Operator --
8771
   ------------------------------
8772
 
8773
   procedure Rewrite_Renamed_Operator
8774
     (N   : Node_Id;
8775
      Op  : Entity_Id;
8776
      Typ : Entity_Id)
8777
   is
8778
      Nam       : constant Name_Id := Chars (Op);
8779
      Is_Binary : constant Boolean := Nkind (N) in N_Binary_Op;
8780
      Op_Node   : Node_Id;
8781
 
8782
   begin
8783
      --  Rewrite the operator node using the real operator, not its renaming.
8784
      --  Exclude user-defined intrinsic operations of the same name, which are
8785
      --  treated separately and rewritten as calls.
8786
 
8787
      if Ekind (Op) /= E_Function
8788
        or else Chars (N) /= Nam
8789
      then
8790
         Op_Node := New_Node (Operator_Kind (Nam, Is_Binary), Sloc (N));
8791
         Set_Chars      (Op_Node, Nam);
8792
         Set_Etype      (Op_Node, Etype (N));
8793
         Set_Entity     (Op_Node, Op);
8794
         Set_Right_Opnd (Op_Node, Right_Opnd (N));
8795
 
8796
         --  Indicate that both the original entity and its renaming are
8797
         --  referenced at this point.
8798
 
8799
         Generate_Reference (Entity (N), N);
8800
         Generate_Reference (Op, N);
8801
 
8802
         if Is_Binary then
8803
            Set_Left_Opnd  (Op_Node, Left_Opnd  (N));
8804
         end if;
8805
 
8806
         Rewrite (N, Op_Node);
8807
 
8808
         --  If the context type is private, add the appropriate conversions
8809
         --  so that the operator is applied to the full view. This is done
8810
         --  in the routines that resolve intrinsic operators,
8811
 
8812
         if Is_Intrinsic_Subprogram (Op)
8813
           and then Is_Private_Type (Typ)
8814
         then
8815
            case Nkind (N) is
8816
               when N_Op_Add   | N_Op_Subtract | N_Op_Multiply | N_Op_Divide |
8817
                    N_Op_Expon | N_Op_Mod      | N_Op_Rem      =>
8818
                  Resolve_Intrinsic_Operator (N, Typ);
8819
 
8820
               when N_Op_Plus  | N_Op_Minus    | N_Op_Abs      =>
8821
                  Resolve_Intrinsic_Unary_Operator (N, Typ);
8822
 
8823
               when others =>
8824
                  Resolve (N, Typ);
8825
            end case;
8826
         end if;
8827
 
8828
      elsif Ekind (Op) = E_Function
8829
        and then Is_Intrinsic_Subprogram (Op)
8830
      then
8831
         --  Operator renames a user-defined operator of the same name. Use
8832
         --  the original operator in the node, which is the one that Gigi
8833
         --  knows about.
8834
 
8835
         Set_Entity (N, Op);
8836
         Set_Is_Overloaded (N, False);
8837
      end if;
8838
   end Rewrite_Renamed_Operator;
8839
 
8840
   -----------------------
8841
   -- Set_Slice_Subtype --
8842
   -----------------------
8843
 
8844
   --  Build an implicit subtype declaration to represent the type delivered
8845
   --  by the slice. This is an abbreviated version of an array subtype. We
8846
   --  define an index subtype for the slice, using either the subtype name
8847
   --  or the discrete range of the slice. To be consistent with index usage
8848
   --  elsewhere, we create a list header to hold the single index. This list
8849
   --  is not otherwise attached to the syntax tree.
8850
 
8851
   procedure Set_Slice_Subtype (N : Node_Id) is
8852
      Loc           : constant Source_Ptr := Sloc (N);
8853
      Index_List    : constant List_Id    := New_List;
8854
      Index         : Node_Id;
8855
      Index_Subtype : Entity_Id;
8856
      Index_Type    : Entity_Id;
8857
      Slice_Subtype : Entity_Id;
8858
      Drange        : constant Node_Id := Discrete_Range (N);
8859
 
8860
   begin
8861
      if Is_Entity_Name (Drange) then
8862
         Index_Subtype := Entity (Drange);
8863
 
8864
      else
8865
         --  We force the evaluation of a range. This is definitely needed in
8866
         --  the renamed case, and seems safer to do unconditionally. Note in
8867
         --  any case that since we will create and insert an Itype referring
8868
         --  to this range, we must make sure any side effect removal actions
8869
         --  are inserted before the Itype definition.
8870
 
8871
         if Nkind (Drange) = N_Range then
8872
            Force_Evaluation (Low_Bound (Drange));
8873
            Force_Evaluation (High_Bound (Drange));
8874
         end if;
8875
 
8876
         Index_Type := Base_Type (Etype (Drange));
8877
 
8878
         Index_Subtype := Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
8879
 
8880
         Set_Scalar_Range (Index_Subtype, Drange);
8881
         Set_Etype        (Index_Subtype, Index_Type);
8882
         Set_Size_Info    (Index_Subtype, Index_Type);
8883
         Set_RM_Size      (Index_Subtype, RM_Size (Index_Type));
8884
      end if;
8885
 
8886
      Slice_Subtype := Create_Itype (E_Array_Subtype, N);
8887
 
8888
      Index := New_Occurrence_Of (Index_Subtype, Loc);
8889
      Set_Etype (Index, Index_Subtype);
8890
      Append (Index, Index_List);
8891
 
8892
      Set_First_Index    (Slice_Subtype, Index);
8893
      Set_Etype          (Slice_Subtype, Base_Type (Etype (N)));
8894
      Set_Is_Constrained (Slice_Subtype, True);
8895
 
8896
      Check_Compile_Time_Size (Slice_Subtype);
8897
 
8898
      --  The Etype of the existing Slice node is reset to this slice subtype.
8899
      --  Its bounds are obtained from its first index.
8900
 
8901
      Set_Etype (N, Slice_Subtype);
8902
 
8903
      --  In the packed case, this must be immediately frozen
8904
 
8905
      --  Couldn't we always freeze here??? and if we did, then the above
8906
      --  call to Check_Compile_Time_Size could be eliminated, which would
8907
      --  be nice, because then that routine could be made private to Freeze.
8908
 
8909
      --  Why the test for In_Spec_Expression here ???
8910
 
8911
      if Is_Packed (Slice_Subtype) and not In_Spec_Expression then
8912
         Freeze_Itype (Slice_Subtype, N);
8913
      end if;
8914
 
8915
   end Set_Slice_Subtype;
8916
 
8917
   --------------------------------
8918
   -- Set_String_Literal_Subtype --
8919
   --------------------------------
8920
 
8921
   procedure Set_String_Literal_Subtype (N : Node_Id; Typ : Entity_Id) is
8922
      Loc        : constant Source_Ptr := Sloc (N);
8923
      Low_Bound  : constant Node_Id :=
8924
                     Type_Low_Bound (Etype (First_Index (Typ)));
8925
      Subtype_Id : Entity_Id;
8926
 
8927
   begin
8928
      if Nkind (N) /= N_String_Literal then
8929
         return;
8930
      end if;
8931
 
8932
      Subtype_Id := Create_Itype (E_String_Literal_Subtype, N);
8933
      Set_String_Literal_Length (Subtype_Id, UI_From_Int
8934
                                               (String_Length (Strval (N))));
8935
      Set_Etype          (Subtype_Id, Base_Type (Typ));
8936
      Set_Is_Constrained (Subtype_Id);
8937
      Set_Etype          (N, Subtype_Id);
8938
 
8939
      if Is_OK_Static_Expression (Low_Bound) then
8940
 
8941
      --  The low bound is set from the low bound of the corresponding
8942
      --  index type. Note that we do not store the high bound in the
8943
      --  string literal subtype, but it can be deduced if necessary
8944
      --  from the length and the low bound.
8945
 
8946
         Set_String_Literal_Low_Bound (Subtype_Id, Low_Bound);
8947
 
8948
      else
8949
         Set_String_Literal_Low_Bound
8950
           (Subtype_Id, Make_Integer_Literal (Loc, 1));
8951
         Set_Etype (String_Literal_Low_Bound (Subtype_Id), Standard_Positive);
8952
 
8953
         --  Build bona fide subtype for the string, and wrap it in an
8954
         --  unchecked conversion, because the backend expects the
8955
         --  String_Literal_Subtype to have a static lower bound.
8956
 
8957
         declare
8958
            Index_List    : constant List_Id    := New_List;
8959
            Index_Type    : constant Entity_Id := Etype (First_Index (Typ));
8960
            High_Bound    : constant Node_Id :=
8961
                               Make_Op_Add (Loc,
8962
                                  Left_Opnd => New_Copy_Tree (Low_Bound),
8963
                                  Right_Opnd =>
8964
                                    Make_Integer_Literal (Loc,
8965
                                      String_Length (Strval (N)) - 1));
8966
            Array_Subtype : Entity_Id;
8967
            Index_Subtype : Entity_Id;
8968
            Drange        : Node_Id;
8969
            Index         : Node_Id;
8970
 
8971
         begin
8972
            Index_Subtype :=
8973
              Create_Itype (Subtype_Kind (Ekind (Index_Type)), N);
8974
            Drange := Make_Range (Loc, New_Copy_Tree (Low_Bound), High_Bound);
8975
            Set_Scalar_Range (Index_Subtype, Drange);
8976
            Set_Parent (Drange, N);
8977
            Analyze_And_Resolve (Drange, Index_Type);
8978
 
8979
            --  In the context, the Index_Type may already have a constraint,
8980
            --  so use common base type on string subtype. The base type may
8981
            --  be used when generating attributes of the string, for example
8982
            --  in the context of a slice assignment.
8983
 
8984
            Set_Etype        (Index_Subtype, Base_Type (Index_Type));
8985
            Set_Size_Info    (Index_Subtype, Index_Type);
8986
            Set_RM_Size      (Index_Subtype, RM_Size (Index_Type));
8987
 
8988
            Array_Subtype := Create_Itype (E_Array_Subtype, N);
8989
 
8990
            Index := New_Occurrence_Of (Index_Subtype, Loc);
8991
            Set_Etype (Index, Index_Subtype);
8992
            Append (Index, Index_List);
8993
 
8994
            Set_First_Index    (Array_Subtype, Index);
8995
            Set_Etype          (Array_Subtype, Base_Type (Typ));
8996
            Set_Is_Constrained (Array_Subtype, True);
8997
 
8998
            Rewrite (N,
8999
              Make_Unchecked_Type_Conversion (Loc,
9000
                Subtype_Mark => New_Occurrence_Of (Array_Subtype, Loc),
9001
                Expression => Relocate_Node (N)));
9002
            Set_Etype (N, Array_Subtype);
9003
         end;
9004
      end if;
9005
   end Set_String_Literal_Subtype;
9006
 
9007
   ------------------------------
9008
   -- Simplify_Type_Conversion --
9009
   ------------------------------
9010
 
9011
   procedure Simplify_Type_Conversion (N : Node_Id) is
9012
   begin
9013
      if Nkind (N) = N_Type_Conversion then
9014
         declare
9015
            Operand    : constant Node_Id   := Expression (N);
9016
            Target_Typ : constant Entity_Id := Etype (N);
9017
            Opnd_Typ   : constant Entity_Id := Etype (Operand);
9018
 
9019
         begin
9020
            if Is_Floating_Point_Type (Opnd_Typ)
9021
              and then
9022
                (Is_Integer_Type (Target_Typ)
9023
                   or else (Is_Fixed_Point_Type (Target_Typ)
9024
                              and then Conversion_OK (N)))
9025
              and then Nkind (Operand) = N_Attribute_Reference
9026
              and then Attribute_Name (Operand) = Name_Truncation
9027
 
9028
            --  Special processing required if the conversion is the expression
9029
            --  of a Truncation attribute reference. In this case we replace:
9030
 
9031
            --     ityp (ftyp'Truncation (x))
9032
 
9033
            --  by
9034
 
9035
            --     ityp (x)
9036
 
9037
            --  with the Float_Truncate flag set, which is more efficient
9038
 
9039
            then
9040
               Rewrite (Operand,
9041
                 Relocate_Node (First (Expressions (Operand))));
9042
               Set_Float_Truncate (N, True);
9043
            end if;
9044
         end;
9045
      end if;
9046
   end Simplify_Type_Conversion;
9047
 
9048
   -----------------------------
9049
   -- Unique_Fixed_Point_Type --
9050
   -----------------------------
9051
 
9052
   function Unique_Fixed_Point_Type (N : Node_Id) return Entity_Id is
9053
      T1   : Entity_Id := Empty;
9054
      T2   : Entity_Id;
9055
      Item : Node_Id;
9056
      Scop : Entity_Id;
9057
 
9058
      procedure Fixed_Point_Error;
9059
      --  Give error messages for true ambiguity. Messages are posted on node
9060
      --  N, and entities T1, T2 are the possible interpretations.
9061
 
9062
      -----------------------
9063
      -- Fixed_Point_Error --
9064
      -----------------------
9065
 
9066
      procedure Fixed_Point_Error is
9067
      begin
9068
         Error_Msg_N ("ambiguous universal_fixed_expression", N);
9069
         Error_Msg_NE ("\\possible interpretation as}", N, T1);
9070
         Error_Msg_NE ("\\possible interpretation as}", N, T2);
9071
      end Fixed_Point_Error;
9072
 
9073
   --  Start of processing for Unique_Fixed_Point_Type
9074
 
9075
   begin
9076
      --  The operations on Duration are visible, so Duration is always a
9077
      --  possible interpretation.
9078
 
9079
      T1 := Standard_Duration;
9080
 
9081
      --  Look for fixed-point types in enclosing scopes
9082
 
9083
      Scop := Current_Scope;
9084
      while Scop /= Standard_Standard loop
9085
         T2 := First_Entity (Scop);
9086
         while Present (T2) loop
9087
            if Is_Fixed_Point_Type (T2)
9088
              and then Current_Entity (T2) = T2
9089
              and then Scope (Base_Type (T2)) = Scop
9090
            then
9091
               if Present (T1) then
9092
                  Fixed_Point_Error;
9093
                  return Any_Type;
9094
               else
9095
                  T1 := T2;
9096
               end if;
9097
            end if;
9098
 
9099
            Next_Entity (T2);
9100
         end loop;
9101
 
9102
         Scop := Scope (Scop);
9103
      end loop;
9104
 
9105
      --  Look for visible fixed type declarations in the context
9106
 
9107
      Item := First (Context_Items (Cunit (Current_Sem_Unit)));
9108
      while Present (Item) loop
9109
         if Nkind (Item) = N_With_Clause then
9110
            Scop := Entity (Name (Item));
9111
            T2 := First_Entity (Scop);
9112
            while Present (T2) loop
9113
               if Is_Fixed_Point_Type (T2)
9114
                 and then Scope (Base_Type (T2)) = Scop
9115
                 and then (Is_Potentially_Use_Visible (T2)
9116
                             or else In_Use (T2))
9117
               then
9118
                  if Present (T1) then
9119
                     Fixed_Point_Error;
9120
                     return Any_Type;
9121
                  else
9122
                     T1 := T2;
9123
                  end if;
9124
               end if;
9125
 
9126
               Next_Entity (T2);
9127
            end loop;
9128
         end if;
9129
 
9130
         Next (Item);
9131
      end loop;
9132
 
9133
      if Nkind (N) = N_Real_Literal then
9134
         Error_Msg_NE ("?real literal interpreted as }!", N, T1);
9135
      else
9136
         Error_Msg_NE ("?universal_fixed expression interpreted as }!", N, T1);
9137
      end if;
9138
 
9139
      return T1;
9140
   end Unique_Fixed_Point_Type;
9141
 
9142
   ----------------------
9143
   -- Valid_Conversion --
9144
   ----------------------
9145
 
9146
   function Valid_Conversion
9147
     (N       : Node_Id;
9148
      Target  : Entity_Id;
9149
      Operand : Node_Id) return Boolean
9150
   is
9151
      Target_Type : constant Entity_Id := Base_Type (Target);
9152
      Opnd_Type   : Entity_Id := Etype (Operand);
9153
 
9154
      function Conversion_Check
9155
        (Valid : Boolean;
9156
         Msg   : String) return Boolean;
9157
      --  Little routine to post Msg if Valid is False, returns Valid value
9158
 
9159
      function Valid_Tagged_Conversion
9160
        (Target_Type : Entity_Id;
9161
         Opnd_Type   : Entity_Id) return Boolean;
9162
      --  Specifically test for validity of tagged conversions
9163
 
9164
      function Valid_Array_Conversion return Boolean;
9165
      --  Check index and component conformance, and accessibility levels
9166
      --  if the component types are anonymous access types (Ada 2005)
9167
 
9168
      ----------------------
9169
      -- Conversion_Check --
9170
      ----------------------
9171
 
9172
      function Conversion_Check
9173
        (Valid : Boolean;
9174
         Msg   : String) return Boolean
9175
      is
9176
      begin
9177
         if not Valid then
9178
            Error_Msg_N (Msg, Operand);
9179
         end if;
9180
 
9181
         return Valid;
9182
      end Conversion_Check;
9183
 
9184
      ----------------------------
9185
      -- Valid_Array_Conversion --
9186
      ----------------------------
9187
 
9188
      function Valid_Array_Conversion return Boolean
9189
      is
9190
         Opnd_Comp_Type : constant Entity_Id := Component_Type (Opnd_Type);
9191
         Opnd_Comp_Base : constant Entity_Id := Base_Type (Opnd_Comp_Type);
9192
 
9193
         Opnd_Index      : Node_Id;
9194
         Opnd_Index_Type : Entity_Id;
9195
 
9196
         Target_Comp_Type : constant Entity_Id :=
9197
                              Component_Type (Target_Type);
9198
         Target_Comp_Base : constant Entity_Id :=
9199
                              Base_Type (Target_Comp_Type);
9200
 
9201
         Target_Index      : Node_Id;
9202
         Target_Index_Type : Entity_Id;
9203
 
9204
      begin
9205
         --  Error if wrong number of dimensions
9206
 
9207
         if
9208
           Number_Dimensions (Target_Type) /= Number_Dimensions (Opnd_Type)
9209
         then
9210
            Error_Msg_N
9211
              ("incompatible number of dimensions for conversion", Operand);
9212
            return False;
9213
 
9214
         --  Number of dimensions matches
9215
 
9216
         else
9217
            --  Loop through indexes of the two arrays
9218
 
9219
            Target_Index := First_Index (Target_Type);
9220
            Opnd_Index   := First_Index (Opnd_Type);
9221
            while Present (Target_Index) and then Present (Opnd_Index) loop
9222
               Target_Index_Type := Etype (Target_Index);
9223
               Opnd_Index_Type   := Etype (Opnd_Index);
9224
 
9225
               --  Error if index types are incompatible
9226
 
9227
               if not (Is_Integer_Type (Target_Index_Type)
9228
                       and then Is_Integer_Type (Opnd_Index_Type))
9229
                 and then (Root_Type (Target_Index_Type)
9230
                           /= Root_Type (Opnd_Index_Type))
9231
               then
9232
                  Error_Msg_N
9233
                    ("incompatible index types for array conversion",
9234
                     Operand);
9235
                  return False;
9236
               end if;
9237
 
9238
               Next_Index (Target_Index);
9239
               Next_Index (Opnd_Index);
9240
            end loop;
9241
 
9242
            --  If component types have same base type, all set
9243
 
9244
            if Target_Comp_Base  = Opnd_Comp_Base then
9245
               null;
9246
 
9247
               --  Here if base types of components are not the same. The only
9248
               --  time this is allowed is if we have anonymous access types.
9249
 
9250
               --  The conversion of arrays of anonymous access types can lead
9251
               --  to dangling pointers. AI-392 formalizes the accessibility
9252
               --  checks that must be applied to such conversions to prevent
9253
               --  out-of-scope references.
9254
 
9255
            elsif
9256
              (Ekind (Target_Comp_Base) = E_Anonymous_Access_Type
9257
                 or else
9258
               Ekind (Target_Comp_Base) = E_Anonymous_Access_Subprogram_Type)
9259
              and then Ekind (Opnd_Comp_Base) = Ekind (Target_Comp_Base)
9260
              and then
9261
                Subtypes_Statically_Match (Target_Comp_Type, Opnd_Comp_Type)
9262
            then
9263
               if Type_Access_Level (Target_Type) <
9264
                   Type_Access_Level (Opnd_Type)
9265
               then
9266
                  if In_Instance_Body then
9267
                     Error_Msg_N ("?source array type " &
9268
                       "has deeper accessibility level than target", Operand);
9269
                     Error_Msg_N ("\?Program_Error will be raised at run time",
9270
                         Operand);
9271
                     Rewrite (N,
9272
                       Make_Raise_Program_Error (Sloc (N),
9273
                         Reason => PE_Accessibility_Check_Failed));
9274
                     Set_Etype (N, Target_Type);
9275
                     return False;
9276
 
9277
                  --  Conversion not allowed because of accessibility levels
9278
 
9279
                  else
9280
                     Error_Msg_N ("source array type " &
9281
                       "has deeper accessibility level than target", Operand);
9282
                     return False;
9283
                  end if;
9284
               else
9285
                  null;
9286
               end if;
9287
 
9288
            --  All other cases where component base types do not match
9289
 
9290
            else
9291
               Error_Msg_N
9292
                 ("incompatible component types for array conversion",
9293
                  Operand);
9294
               return False;
9295
            end if;
9296
 
9297
            --  Check that component subtypes statically match. For numeric
9298
            --  types this means that both must be either constrained or
9299
            --  unconstrained. For enumeration types the bounds must match.
9300
            --  All of this is checked in Subtypes_Statically_Match.
9301
 
9302
            if not Subtypes_Statically_Match
9303
                            (Target_Comp_Type, Opnd_Comp_Type)
9304
            then
9305
               Error_Msg_N
9306
                 ("component subtypes must statically match", Operand);
9307
               return False;
9308
            end if;
9309
         end if;
9310
 
9311
         return True;
9312
      end Valid_Array_Conversion;
9313
 
9314
      -----------------------------
9315
      -- Valid_Tagged_Conversion --
9316
      -----------------------------
9317
 
9318
      function Valid_Tagged_Conversion
9319
        (Target_Type : Entity_Id;
9320
         Opnd_Type   : Entity_Id) return Boolean
9321
      is
9322
      begin
9323
         --  Upward conversions are allowed (RM 4.6(22))
9324
 
9325
         if Covers (Target_Type, Opnd_Type)
9326
           or else Is_Ancestor (Target_Type, Opnd_Type)
9327
         then
9328
            return True;
9329
 
9330
         --  Downward conversion are allowed if the operand is class-wide
9331
         --  (RM 4.6(23)).
9332
 
9333
         elsif Is_Class_Wide_Type (Opnd_Type)
9334
           and then Covers (Opnd_Type, Target_Type)
9335
         then
9336
            return True;
9337
 
9338
         elsif Covers (Opnd_Type, Target_Type)
9339
           or else Is_Ancestor (Opnd_Type, Target_Type)
9340
         then
9341
            return
9342
              Conversion_Check (False,
9343
                "downward conversion of tagged objects not allowed");
9344
 
9345
         --  Ada 2005 (AI-251): The conversion to/from interface types is
9346
         --  always valid
9347
 
9348
         elsif Is_Interface (Target_Type) or else Is_Interface (Opnd_Type) then
9349
            return True;
9350
 
9351
         --  If the operand is a class-wide type obtained through a limited_
9352
         --  with clause, and the context includes the non-limited view, use
9353
         --  it to determine whether the conversion is legal.
9354
 
9355
         elsif Is_Class_Wide_Type (Opnd_Type)
9356
           and then From_With_Type (Opnd_Type)
9357
           and then Present (Non_Limited_View (Etype (Opnd_Type)))
9358
           and then Is_Interface (Non_Limited_View (Etype (Opnd_Type)))
9359
         then
9360
            return True;
9361
 
9362
         elsif Is_Access_Type (Opnd_Type)
9363
           and then Is_Interface (Directly_Designated_Type (Opnd_Type))
9364
         then
9365
            return True;
9366
 
9367
         else
9368
            Error_Msg_NE
9369
              ("invalid tagged conversion, not compatible with}",
9370
               N, First_Subtype (Opnd_Type));
9371
            return False;
9372
         end if;
9373
      end Valid_Tagged_Conversion;
9374
 
9375
   --  Start of processing for Valid_Conversion
9376
 
9377
   begin
9378
      Check_Parameterless_Call (Operand);
9379
 
9380
      if Is_Overloaded (Operand) then
9381
         declare
9382
            I   : Interp_Index;
9383
            I1  : Interp_Index;
9384
            It  : Interp;
9385
            It1 : Interp;
9386
            N1  : Entity_Id;
9387
 
9388
         begin
9389
            --  Remove procedure calls, which syntactically cannot appear in
9390
            --  this context, but which cannot be removed by type checking,
9391
            --  because the context does not impose a type.
9392
 
9393
            --  When compiling for VMS, spurious ambiguities can be produced
9394
            --  when arithmetic operations have a literal operand and return
9395
            --  System.Address or a descendant of it. These ambiguities are
9396
            --  otherwise resolved by the context, but for conversions there
9397
            --  is no context type and the removal of the spurious operations
9398
            --  must be done explicitly here.
9399
 
9400
            --  The node may be labelled overloaded, but still contain only
9401
            --  one interpretation because others were discarded in previous
9402
            --  filters. If this is the case, retain the single interpretation
9403
            --  if legal.
9404
 
9405
            Get_First_Interp (Operand, I, It);
9406
            Opnd_Type := It.Typ;
9407
            Get_Next_Interp (I, It);
9408
 
9409
            if Present (It.Typ)
9410
              and then Opnd_Type /= Standard_Void_Type
9411
            then
9412
               --  More than one candidate interpretation is available
9413
 
9414
               Get_First_Interp (Operand, I, It);
9415
               while Present (It.Typ) loop
9416
                  if It.Typ = Standard_Void_Type then
9417
                     Remove_Interp (I);
9418
                  end if;
9419
 
9420
                  if Present (System_Aux_Id)
9421
                    and then Is_Descendent_Of_Address (It.Typ)
9422
                  then
9423
                     Remove_Interp (I);
9424
                  end if;
9425
 
9426
                  Get_Next_Interp (I, It);
9427
               end loop;
9428
            end if;
9429
 
9430
            Get_First_Interp (Operand, I, It);
9431
            I1  := I;
9432
            It1 := It;
9433
 
9434
            if No (It.Typ) then
9435
               Error_Msg_N ("illegal operand in conversion", Operand);
9436
               return False;
9437
            end if;
9438
 
9439
            Get_Next_Interp (I, It);
9440
 
9441
            if Present (It.Typ) then
9442
               N1  := It1.Nam;
9443
               It1 :=  Disambiguate (Operand, I1, I, Any_Type);
9444
 
9445
               if It1 = No_Interp then
9446
                  Error_Msg_N ("ambiguous operand in conversion", Operand);
9447
 
9448
                  Error_Msg_Sloc := Sloc (It.Nam);
9449
                  Error_Msg_N -- CODEFIX
9450
                    ("\\possible interpretation#!", Operand);
9451
 
9452
                  Error_Msg_Sloc := Sloc (N1);
9453
                  Error_Msg_N -- CODEFIX
9454
                    ("\\possible interpretation#!", Operand);
9455
 
9456
                  return False;
9457
               end if;
9458
            end if;
9459
 
9460
            Set_Etype (Operand, It1.Typ);
9461
            Opnd_Type := It1.Typ;
9462
         end;
9463
      end if;
9464
 
9465
      --  Numeric types
9466
 
9467
      if Is_Numeric_Type (Target_Type)  then
9468
 
9469
         --  A universal fixed expression can be converted to any numeric type
9470
 
9471
         if Opnd_Type = Universal_Fixed then
9472
            return True;
9473
 
9474
         --  Also no need to check when in an instance or inlined body, because
9475
         --  the legality has been established when the template was analyzed.
9476
         --  Furthermore, numeric conversions may occur where only a private
9477
         --  view of the operand type is visible at the instantiation point.
9478
         --  This results in a spurious error if we check that the operand type
9479
         --  is a numeric type.
9480
 
9481
         --  Note: in a previous version of this unit, the following tests were
9482
         --  applied only for generated code (Comes_From_Source set to False),
9483
         --  but in fact the test is required for source code as well, since
9484
         --  this situation can arise in source code.
9485
 
9486
         elsif In_Instance or else In_Inlined_Body then
9487
               return True;
9488
 
9489
         --  Otherwise we need the conversion check
9490
 
9491
         else
9492
            return Conversion_Check
9493
                    (Is_Numeric_Type (Opnd_Type),
9494
                     "illegal operand for numeric conversion");
9495
         end if;
9496
 
9497
      --  Array types
9498
 
9499
      elsif Is_Array_Type (Target_Type) then
9500
         if not Is_Array_Type (Opnd_Type)
9501
           or else Opnd_Type = Any_Composite
9502
           or else Opnd_Type = Any_String
9503
         then
9504
            Error_Msg_N
9505
              ("illegal operand for array conversion", Operand);
9506
            return False;
9507
         else
9508
            return Valid_Array_Conversion;
9509
         end if;
9510
 
9511
      --  Ada 2005 (AI-251): Anonymous access types where target references an
9512
      --  interface type.
9513
 
9514
      elsif (Ekind (Target_Type) = E_General_Access_Type
9515
              or else
9516
             Ekind (Target_Type) = E_Anonymous_Access_Type)
9517
        and then Is_Interface (Directly_Designated_Type (Target_Type))
9518
      then
9519
         --  Check the static accessibility rule of 4.6(17). Note that the
9520
         --  check is not enforced when within an instance body, since the
9521
         --  RM requires such cases to be caught at run time.
9522
 
9523
         if Ekind (Target_Type) /= E_Anonymous_Access_Type then
9524
            if Type_Access_Level (Opnd_Type) >
9525
               Type_Access_Level (Target_Type)
9526
            then
9527
               --  In an instance, this is a run-time check, but one we know
9528
               --  will fail, so generate an appropriate warning. The raise
9529
               --  will be generated by Expand_N_Type_Conversion.
9530
 
9531
               if In_Instance_Body then
9532
                  Error_Msg_N
9533
                    ("?cannot convert local pointer to non-local access type",
9534
                     Operand);
9535
                  Error_Msg_N
9536
                    ("\?Program_Error will be raised at run time", Operand);
9537
               else
9538
                  Error_Msg_N
9539
                    ("cannot convert local pointer to non-local access type",
9540
                     Operand);
9541
                  return False;
9542
               end if;
9543
 
9544
            --  Special accessibility checks are needed in the case of access
9545
            --  discriminants declared for a limited type.
9546
 
9547
            elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
9548
              and then not Is_Local_Anonymous_Access (Opnd_Type)
9549
            then
9550
               --  When the operand is a selected access discriminant the check
9551
               --  needs to be made against the level of the object denoted by
9552
               --  the prefix of the selected name (Object_Access_Level handles
9553
               --  checking the prefix of the operand for this case).
9554
 
9555
               if Nkind (Operand) = N_Selected_Component
9556
                 and then Object_Access_Level (Operand) >
9557
                          Type_Access_Level (Target_Type)
9558
               then
9559
                  --  In an instance, this is a run-time check, but one we know
9560
                  --  will fail, so generate an appropriate warning. The raise
9561
                  --  will be generated by Expand_N_Type_Conversion.
9562
 
9563
                  if In_Instance_Body then
9564
                     Error_Msg_N
9565
                       ("?cannot convert access discriminant to non-local" &
9566
                        " access type", Operand);
9567
                     Error_Msg_N
9568
                       ("\?Program_Error will be raised at run time", Operand);
9569
                  else
9570
                     Error_Msg_N
9571
                       ("cannot convert access discriminant to non-local" &
9572
                        " access type", Operand);
9573
                     return False;
9574
                  end if;
9575
               end if;
9576
 
9577
               --  The case of a reference to an access discriminant from
9578
               --  within a limited type declaration (which will appear as
9579
               --  a discriminal) is always illegal because the level of the
9580
               --  discriminant is considered to be deeper than any (nameable)
9581
               --  access type.
9582
 
9583
               if Is_Entity_Name (Operand)
9584
                 and then not Is_Local_Anonymous_Access (Opnd_Type)
9585
                 and then (Ekind (Entity (Operand)) = E_In_Parameter
9586
                            or else Ekind (Entity (Operand)) = E_Constant)
9587
                 and then Present (Discriminal_Link (Entity (Operand)))
9588
               then
9589
                  Error_Msg_N
9590
                    ("discriminant has deeper accessibility level than target",
9591
                     Operand);
9592
                  return False;
9593
               end if;
9594
            end if;
9595
         end if;
9596
 
9597
         return True;
9598
 
9599
      --  General and anonymous access types
9600
 
9601
      elsif (Ekind (Target_Type) = E_General_Access_Type
9602
        or else Ekind (Target_Type) = E_Anonymous_Access_Type)
9603
          and then
9604
            Conversion_Check
9605
              (Is_Access_Type (Opnd_Type)
9606
                 and then Ekind (Opnd_Type) /=
9607
                   E_Access_Subprogram_Type
9608
                 and then Ekind (Opnd_Type) /=
9609
                   E_Access_Protected_Subprogram_Type,
9610
               "must be an access-to-object type")
9611
      then
9612
         if Is_Access_Constant (Opnd_Type)
9613
           and then not Is_Access_Constant (Target_Type)
9614
         then
9615
            Error_Msg_N
9616
              ("access-to-constant operand type not allowed", Operand);
9617
            return False;
9618
         end if;
9619
 
9620
         --  Check the static accessibility rule of 4.6(17). Note that the
9621
         --  check is not enforced when within an instance body, since the RM
9622
         --  requires such cases to be caught at run time.
9623
 
9624
         if Ekind (Target_Type) /= E_Anonymous_Access_Type
9625
           or else Is_Local_Anonymous_Access (Target_Type)
9626
         then
9627
            if Type_Access_Level (Opnd_Type)
9628
              > Type_Access_Level (Target_Type)
9629
            then
9630
               --  In an instance, this is a run-time check, but one we know
9631
               --  will fail, so generate an appropriate warning. The raise
9632
               --  will be generated by Expand_N_Type_Conversion.
9633
 
9634
               if In_Instance_Body then
9635
                  Error_Msg_N
9636
                    ("?cannot convert local pointer to non-local access type",
9637
                     Operand);
9638
                  Error_Msg_N
9639
                    ("\?Program_Error will be raised at run time", Operand);
9640
 
9641
               else
9642
                  --  Avoid generation of spurious error message
9643
 
9644
                  if not Error_Posted (N) then
9645
                     Error_Msg_N
9646
                      ("cannot convert local pointer to non-local access type",
9647
                       Operand);
9648
                  end if;
9649
 
9650
                  return False;
9651
               end if;
9652
 
9653
            --  Special accessibility checks are needed in the case of access
9654
            --  discriminants declared for a limited type.
9655
 
9656
            elsif Ekind (Opnd_Type) = E_Anonymous_Access_Type
9657
              and then not Is_Local_Anonymous_Access (Opnd_Type)
9658
            then
9659
 
9660
               --  When the operand is a selected access discriminant the check
9661
               --  needs to be made against the level of the object denoted by
9662
               --  the prefix of the selected name (Object_Access_Level handles
9663
               --  checking the prefix of the operand for this case).
9664
 
9665
               if Nkind (Operand) = N_Selected_Component
9666
                 and then Object_Access_Level (Operand) >
9667
                          Type_Access_Level (Target_Type)
9668
               then
9669
                  --  In an instance, this is a run-time check, but one we know
9670
                  --  will fail, so generate an appropriate warning. The raise
9671
                  --  will be generated by Expand_N_Type_Conversion.
9672
 
9673
                  if In_Instance_Body then
9674
                     Error_Msg_N
9675
                       ("?cannot convert access discriminant to non-local" &
9676
                        " access type", Operand);
9677
                     Error_Msg_N
9678
                       ("\?Program_Error will be raised at run time",
9679
                        Operand);
9680
 
9681
                  else
9682
                     Error_Msg_N
9683
                       ("cannot convert access discriminant to non-local" &
9684
                        " access type", Operand);
9685
                     return False;
9686
                  end if;
9687
               end if;
9688
 
9689
               --  The case of a reference to an access discriminant from
9690
               --  within a limited type declaration (which will appear as
9691
               --  a discriminal) is always illegal because the level of the
9692
               --  discriminant is considered to be deeper than any (nameable)
9693
               --  access type.
9694
 
9695
               if Is_Entity_Name (Operand)
9696
                 and then (Ekind (Entity (Operand)) = E_In_Parameter
9697
                            or else Ekind (Entity (Operand)) = E_Constant)
9698
                 and then Present (Discriminal_Link (Entity (Operand)))
9699
               then
9700
                  Error_Msg_N
9701
                    ("discriminant has deeper accessibility level than target",
9702
                     Operand);
9703
                  return False;
9704
               end if;
9705
            end if;
9706
         end if;
9707
 
9708
         --  In the presence of limited_with clauses we have to use non-limited
9709
         --  views, if available.
9710
 
9711
         Check_Limited : declare
9712
            function Full_Designated_Type (T : Entity_Id) return Entity_Id;
9713
            --  Helper function to handle limited views
9714
 
9715
            --------------------------
9716
            -- Full_Designated_Type --
9717
            --------------------------
9718
 
9719
            function Full_Designated_Type (T : Entity_Id) return Entity_Id is
9720
               Desig : constant Entity_Id := Designated_Type (T);
9721
 
9722
            begin
9723
               --  Handle the limited view of a type
9724
 
9725
               if Is_Incomplete_Type (Desig)
9726
                 and then From_With_Type (Desig)
9727
                 and then Present (Non_Limited_View (Desig))
9728
               then
9729
                  return Available_View (Desig);
9730
               else
9731
                  return Desig;
9732
               end if;
9733
            end Full_Designated_Type;
9734
 
9735
            --  Local Declarations
9736
 
9737
            Target : constant Entity_Id := Full_Designated_Type (Target_Type);
9738
            Opnd   : constant Entity_Id := Full_Designated_Type (Opnd_Type);
9739
 
9740
            Same_Base : constant Boolean :=
9741
                          Base_Type (Target) = Base_Type (Opnd);
9742
 
9743
         --  Start of processing for Check_Limited
9744
 
9745
         begin
9746
            if Is_Tagged_Type (Target) then
9747
               return Valid_Tagged_Conversion (Target, Opnd);
9748
 
9749
            else
9750
               if not Same_Base then
9751
                  Error_Msg_NE
9752
                    ("target designated type not compatible with }",
9753
                     N, Base_Type (Opnd));
9754
                  return False;
9755
 
9756
               --  Ada 2005 AI-384: legality rule is symmetric in both
9757
               --  designated types. The conversion is legal (with possible
9758
               --  constraint check) if either designated type is
9759
               --  unconstrained.
9760
 
9761
               elsif Subtypes_Statically_Match (Target, Opnd)
9762
                 or else
9763
                   (Has_Discriminants (Target)
9764
                     and then
9765
                      (not Is_Constrained (Opnd)
9766
                        or else not Is_Constrained (Target)))
9767
               then
9768
                  --  Special case, if Value_Size has been used to make the
9769
                  --  sizes different, the conversion is not allowed even
9770
                  --  though the subtypes statically match.
9771
 
9772
                  if Known_Static_RM_Size (Target)
9773
                    and then Known_Static_RM_Size (Opnd)
9774
                    and then RM_Size (Target) /= RM_Size (Opnd)
9775
                  then
9776
                     Error_Msg_NE
9777
                       ("target designated subtype not compatible with }",
9778
                        N, Opnd);
9779
                     Error_Msg_NE
9780
                       ("\because sizes of the two designated subtypes differ",
9781
                        N, Opnd);
9782
                     return False;
9783
 
9784
                  --  Normal case where conversion is allowed
9785
 
9786
                  else
9787
                     return True;
9788
                  end if;
9789
 
9790
               else
9791
                  Error_Msg_NE
9792
                    ("target designated subtype not compatible with }",
9793
                     N, Opnd);
9794
                  return False;
9795
               end if;
9796
            end if;
9797
         end Check_Limited;
9798
 
9799
      --  Access to subprogram types. If the operand is an access parameter,
9800
      --  the type has a deeper accessibility that any master, and cannot
9801
      --  be assigned. We must make an exception if the conversion is part
9802
      --  of an assignment and the target is the return object of an extended
9803
      --  return statement, because in that case the accessibility check
9804
      --  takes place after the return.
9805
 
9806
      elsif Is_Access_Subprogram_Type (Target_Type)
9807
        and then No (Corresponding_Remote_Type (Opnd_Type))
9808
      then
9809
         if Ekind (Base_Type (Opnd_Type)) = E_Anonymous_Access_Subprogram_Type
9810
           and then Is_Entity_Name (Operand)
9811
           and then Ekind (Entity (Operand)) = E_In_Parameter
9812
           and then
9813
             (Nkind (Parent (N)) /= N_Assignment_Statement
9814
               or else not Is_Entity_Name (Name (Parent (N)))
9815
               or else not Is_Return_Object (Entity (Name (Parent (N)))))
9816
         then
9817
            Error_Msg_N
9818
              ("illegal attempt to store anonymous access to subprogram",
9819
               Operand);
9820
            Error_Msg_N
9821
              ("\value has deeper accessibility than any master " &
9822
               "(RM 3.10.2 (13))",
9823
               Operand);
9824
 
9825
            Error_Msg_NE
9826
             ("\use named access type for& instead of access parameter",
9827
               Operand, Entity (Operand));
9828
         end if;
9829
 
9830
         --  Check that the designated types are subtype conformant
9831
 
9832
         Check_Subtype_Conformant (New_Id  => Designated_Type (Target_Type),
9833
                                   Old_Id  => Designated_Type (Opnd_Type),
9834
                                   Err_Loc => N);
9835
 
9836
         --  Check the static accessibility rule of 4.6(20)
9837
 
9838
         if Type_Access_Level (Opnd_Type) >
9839
            Type_Access_Level (Target_Type)
9840
         then
9841
            Error_Msg_N
9842
              ("operand type has deeper accessibility level than target",
9843
               Operand);
9844
 
9845
         --  Check that if the operand type is declared in a generic body,
9846
         --  then the target type must be declared within that same body
9847
         --  (enforces last sentence of 4.6(20)).
9848
 
9849
         elsif Present (Enclosing_Generic_Body (Opnd_Type)) then
9850
            declare
9851
               O_Gen : constant Node_Id :=
9852
                         Enclosing_Generic_Body (Opnd_Type);
9853
 
9854
               T_Gen : Node_Id;
9855
 
9856
            begin
9857
               T_Gen := Enclosing_Generic_Body (Target_Type);
9858
               while Present (T_Gen) and then T_Gen /= O_Gen loop
9859
                  T_Gen := Enclosing_Generic_Body (T_Gen);
9860
               end loop;
9861
 
9862
               if T_Gen /= O_Gen then
9863
                  Error_Msg_N
9864
                    ("target type must be declared in same generic body"
9865
                     & " as operand type", N);
9866
               end if;
9867
            end;
9868
         end if;
9869
 
9870
         return True;
9871
 
9872
      --  Remote subprogram access types
9873
 
9874
      elsif Is_Remote_Access_To_Subprogram_Type (Target_Type)
9875
        and then Is_Remote_Access_To_Subprogram_Type (Opnd_Type)
9876
      then
9877
         --  It is valid to convert from one RAS type to another provided
9878
         --  that their specification statically match.
9879
 
9880
         Check_Subtype_Conformant
9881
           (New_Id  =>
9882
              Designated_Type (Corresponding_Remote_Type (Target_Type)),
9883
            Old_Id  =>
9884
              Designated_Type (Corresponding_Remote_Type (Opnd_Type)),
9885
            Err_Loc =>
9886
              N);
9887
         return True;
9888
 
9889
      --  If both are tagged types, check legality of view conversions
9890
 
9891
      elsif Is_Tagged_Type (Target_Type)
9892
        and then Is_Tagged_Type (Opnd_Type)
9893
      then
9894
         return Valid_Tagged_Conversion (Target_Type, Opnd_Type);
9895
 
9896
      --  Types derived from the same root type are convertible
9897
 
9898
      elsif Root_Type (Target_Type) = Root_Type (Opnd_Type) then
9899
         return True;
9900
 
9901
      --  In an instance or an inlined body, there may be inconsistent
9902
      --  views of the same type, or of types derived from a common root.
9903
 
9904
      elsif (In_Instance or In_Inlined_Body)
9905
        and then
9906
          Root_Type (Underlying_Type (Target_Type)) =
9907
          Root_Type (Underlying_Type (Opnd_Type))
9908
      then
9909
         return True;
9910
 
9911
      --  Special check for common access type error case
9912
 
9913
      elsif Ekind (Target_Type) = E_Access_Type
9914
         and then Is_Access_Type (Opnd_Type)
9915
      then
9916
         Error_Msg_N ("target type must be general access type!", N);
9917
         Error_Msg_NE ("add ALL to }!", N, Target_Type);
9918
         return False;
9919
 
9920
      else
9921
         Error_Msg_NE ("invalid conversion, not compatible with }",
9922
           N, Opnd_Type);
9923
         return False;
9924
      end if;
9925
   end Valid_Conversion;
9926
 
9927
end Sem_Res;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.