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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [ada/] [sem_ch4.adb] - Blame information for rev 706

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 706 jeremybenn
------------------------------------------------------------------------------
2
--                                                                          --
3
--                         GNAT COMPILER COMPONENTS                         --
4
--                                                                          --
5
--                              S E M _ C H 4                               --
6
--                                                                          --
7
--                                 B o d y                                  --
8
--                                                                          --
9
--          Copyright (C) 1992-2012, Free Software Foundation, Inc.         --
10
--                                                                          --
11
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
12
-- terms of the  GNU General Public License as published  by the Free Soft- --
13
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16
-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17
-- for  more details.  You should have  received  a copy of the GNU General --
18
-- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19
-- http://www.gnu.org/licenses for a complete copy of the license.          --
20
--                                                                          --
21
-- GNAT was originally developed  by the GNAT team at  New York University. --
22
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
23
--                                                                          --
24
------------------------------------------------------------------------------
25
 
26
with Aspects;  use Aspects;
27
with Atree;    use Atree;
28
with Debug;    use Debug;
29
with Einfo;    use Einfo;
30
with Elists;   use Elists;
31
with Errout;   use Errout;
32
with Exp_Util; use Exp_Util;
33
with Fname;    use Fname;
34
with Itypes;   use Itypes;
35
with Lib;      use Lib;
36
with Lib.Xref; use Lib.Xref;
37
with Namet;    use Namet;
38
with Namet.Sp; use Namet.Sp;
39
with Nlists;   use Nlists;
40
with Nmake;    use Nmake;
41
with Opt;      use Opt;
42
with Output;   use Output;
43
with Restrict; use Restrict;
44
with Rident;   use Rident;
45
with Sem;      use Sem;
46
with Sem_Aux;  use Sem_Aux;
47
with Sem_Case; use Sem_Case;
48
with Sem_Cat;  use Sem_Cat;
49
with Sem_Ch3;  use Sem_Ch3;
50
with Sem_Ch5;  use Sem_Ch5;
51
with Sem_Ch6;  use Sem_Ch6;
52
with Sem_Ch8;  use Sem_Ch8;
53
with Sem_Dim;  use Sem_Dim;
54
with Sem_Disp; use Sem_Disp;
55
with Sem_Dist; use Sem_Dist;
56
with Sem_Eval; use Sem_Eval;
57
with Sem_Res;  use Sem_Res;
58
with Sem_Type; use Sem_Type;
59
with Sem_Util; use Sem_Util;
60
with Sem_Warn; use Sem_Warn;
61
with Stand;    use Stand;
62
with Sinfo;    use Sinfo;
63
with Snames;   use Snames;
64
with Tbuild;   use Tbuild;
65
with Uintp;    use Uintp;
66
 
67
package body Sem_Ch4 is
68
 
69
   -----------------------
70
   -- Local Subprograms --
71
   -----------------------
72
 
73
   procedure Analyze_Concatenation_Rest (N : Node_Id);
74
   --  Does the "rest" of the work of Analyze_Concatenation, after the left
75
   --  operand has been analyzed. See Analyze_Concatenation for details.
76
 
77
   procedure Analyze_Expression (N : Node_Id);
78
   --  For expressions that are not names, this is just a call to analyze.
79
   --  If the expression is a name, it may be a call to a parameterless
80
   --  function, and if so must be converted into an explicit call node
81
   --  and analyzed as such. This deproceduring must be done during the first
82
   --  pass of overload resolution, because otherwise a procedure call with
83
   --  overloaded actuals may fail to resolve.
84
 
85
   procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id);
86
   --  Analyze a call of the form "+"(x, y), etc. The prefix of the call
87
   --  is an operator name or an expanded name whose selector is an operator
88
   --  name, and one possible interpretation is as a predefined operator.
89
 
90
   procedure Analyze_Overloaded_Selected_Component (N : Node_Id);
91
   --  If the prefix of a selected_component is overloaded, the proper
92
   --  interpretation that yields a record type with the proper selector
93
   --  name must be selected.
94
 
95
   procedure Analyze_User_Defined_Binary_Op (N : Node_Id; Op_Id : Entity_Id);
96
   --  Procedure to analyze a user defined binary operator, which is resolved
97
   --  like a function, but instead of a list of actuals it is presented
98
   --  with the left and right operands of an operator node.
99
 
100
   procedure Analyze_User_Defined_Unary_Op (N : Node_Id; Op_Id : Entity_Id);
101
   --  Procedure to analyze a user defined unary operator, which is resolved
102
   --  like a function, but instead of a list of actuals, it is presented with
103
   --  the operand of the operator node.
104
 
105
   procedure Ambiguous_Operands (N : Node_Id);
106
   --  For equality, membership, and comparison operators with overloaded
107
   --  arguments, list possible interpretations.
108
 
109
   procedure Analyze_One_Call
110
      (N          : Node_Id;
111
       Nam        : Entity_Id;
112
       Report     : Boolean;
113
       Success    : out Boolean;
114
       Skip_First : Boolean := False);
115
   --  Check one interpretation of an overloaded subprogram name for
116
   --  compatibility with the types of the actuals in a call. If there is a
117
   --  single interpretation which does not match, post error if Report is
118
   --  set to True.
119
   --
120
   --  Nam is the entity that provides the formals against which the actuals
121
   --  are checked. Nam is either the name of a subprogram, or the internal
122
   --  subprogram type constructed for an access_to_subprogram. If the actuals
123
   --  are compatible with Nam, then Nam is added to the list of candidate
124
   --  interpretations for N, and Success is set to True.
125
   --
126
   --  The flag Skip_First is used when analyzing a call that was rewritten
127
   --  from object notation. In this case the first actual may have to receive
128
   --  an explicit dereference, depending on the first formal of the operation
129
   --  being called. The caller will have verified that the object is legal
130
   --  for the call. If the remaining parameters match, the first parameter
131
   --  will rewritten as a dereference if needed, prior to completing analysis.
132
 
133
   procedure Check_Misspelled_Selector
134
     (Prefix : Entity_Id;
135
      Sel    : Node_Id);
136
   --  Give possible misspelling diagnostic if Sel is likely to be a mis-
137
   --  spelling of one of the selectors of the Prefix. This is called by
138
   --  Analyze_Selected_Component after producing an invalid selector error
139
   --  message.
140
 
141
   function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean;
142
   --  Verify that type T is declared in scope S. Used to find interpretations
143
   --  for operators given by expanded names. This is abstracted as a separate
144
   --  function to handle extensions to System, where S is System, but T is
145
   --  declared in the extension.
146
 
147
   procedure Find_Arithmetic_Types
148
     (L, R  : Node_Id;
149
      Op_Id : Entity_Id;
150
      N     : Node_Id);
151
   --  L and R are the operands of an arithmetic operator. Find
152
   --  consistent pairs of interpretations for L and R that have a
153
   --  numeric type consistent with the semantics of the operator.
154
 
155
   procedure Find_Comparison_Types
156
     (L, R  : Node_Id;
157
      Op_Id : Entity_Id;
158
      N     : Node_Id);
159
   --  L and R are operands of a comparison operator. Find consistent
160
   --  pairs of interpretations for L and R.
161
 
162
   procedure Find_Concatenation_Types
163
     (L, R  : Node_Id;
164
      Op_Id : Entity_Id;
165
      N     : Node_Id);
166
   --  For the four varieties of concatenation
167
 
168
   procedure Find_Equality_Types
169
     (L, R  : Node_Id;
170
      Op_Id : Entity_Id;
171
      N     : Node_Id);
172
   --  Ditto for equality operators
173
 
174
   procedure Find_Boolean_Types
175
     (L, R  : Node_Id;
176
      Op_Id : Entity_Id;
177
      N     : Node_Id);
178
   --  Ditto for binary logical operations
179
 
180
   procedure Find_Negation_Types
181
     (R     : Node_Id;
182
      Op_Id : Entity_Id;
183
      N     : Node_Id);
184
   --  Find consistent interpretation for operand of negation operator
185
 
186
   procedure Find_Non_Universal_Interpretations
187
     (N     : Node_Id;
188
      R     : Node_Id;
189
      Op_Id : Entity_Id;
190
      T1    : Entity_Id);
191
   --  For equality and comparison operators, the result is always boolean,
192
   --  and the legality of the operation is determined from the visibility
193
   --  of the operand types. If one of the operands has a universal interpre-
194
   --  tation,  the legality check uses some compatible non-universal
195
   --  interpretation of the other operand. N can be an operator node, or
196
   --  a function call whose name is an operator designator.
197
 
198
   function Find_Primitive_Operation (N : Node_Id) return Boolean;
199
   --  Find candidate interpretations for the name Obj.Proc when it appears
200
   --  in a subprogram renaming declaration.
201
 
202
   procedure Find_Unary_Types
203
     (R     : Node_Id;
204
      Op_Id : Entity_Id;
205
      N     : Node_Id);
206
   --  Unary arithmetic types: plus, minus, abs
207
 
208
   procedure Check_Arithmetic_Pair
209
     (T1, T2 : Entity_Id;
210
      Op_Id  : Entity_Id;
211
      N      : Node_Id);
212
   --  Subsidiary procedure to Find_Arithmetic_Types. T1 and T2 are valid
213
   --  types for left and right operand. Determine whether they constitute
214
   --  a valid pair for the given operator, and record the corresponding
215
   --  interpretation of the operator node. The node N may be an operator
216
   --  node (the usual case) or a function call whose prefix is an operator
217
   --  designator. In both cases Op_Id is the operator name itself.
218
 
219
   procedure Diagnose_Call (N : Node_Id; Nam : Node_Id);
220
   --  Give detailed information on overloaded call where none of the
221
   --  interpretations match. N is the call node, Nam the designator for
222
   --  the overloaded entity being called.
223
 
224
   function Junk_Operand (N : Node_Id) return Boolean;
225
   --  Test for an operand that is an inappropriate entity (e.g. a package
226
   --  name or a label). If so, issue an error message and return True. If
227
   --  the operand is not an inappropriate entity kind, return False.
228
 
229
   procedure Operator_Check (N : Node_Id);
230
   --  Verify that an operator has received some valid interpretation. If none
231
   --  was found, determine whether a use clause would make the operation
232
   --  legal. The variable Candidate_Type (defined in Sem_Type) is set for
233
   --  every type compatible with the operator, even if the operator for the
234
   --  type is not directly visible. The routine uses this type to emit a more
235
   --  informative message.
236
 
237
   function Process_Implicit_Dereference_Prefix
238
     (E : Entity_Id;
239
      P : Node_Id) return Entity_Id;
240
   --  Called when P is the prefix of an implicit dereference, denoting an
241
   --  object E. The function returns the designated type of the prefix, taking
242
   --  into account that the designated type of an anonymous access type may be
243
   --  a limited view, when the non-limited view is visible.
244
   --  If in semantics only mode (-gnatc or generic), the function also records
245
   --  that the prefix is a reference to E, if any. Normally, such a reference
246
   --  is generated only when the implicit dereference is expanded into an
247
   --  explicit one, but for consistency we must generate the reference when
248
   --  expansion is disabled as well.
249
 
250
   procedure Remove_Abstract_Operations (N : Node_Id);
251
   --  Ada 2005: implementation of AI-310. An abstract non-dispatching
252
   --  operation is not a candidate interpretation.
253
 
254
   function Try_Container_Indexing
255
     (N      : Node_Id;
256
      Prefix : Node_Id;
257
      Expr   : Node_Id) return Boolean;
258
   --  AI05-0139: Generalized indexing to support iterators over containers
259
 
260
   function Try_Indexed_Call
261
     (N          : Node_Id;
262
      Nam        : Entity_Id;
263
      Typ        : Entity_Id;
264
      Skip_First : Boolean) return Boolean;
265
   --  If a function has defaults for all its actuals, a call to it may in fact
266
   --  be an indexing on the result of the call. Try_Indexed_Call attempts the
267
   --  interpretation as an indexing, prior to analysis as a call. If both are
268
   --  possible, the node is overloaded with both interpretations (same symbol
269
   --  but two different types). If the call is written in prefix form, the
270
   --  prefix becomes the first parameter in the call, and only the remaining
271
   --  actuals must be checked for the presence of defaults.
272
 
273
   function Try_Indirect_Call
274
     (N   : Node_Id;
275
      Nam : Entity_Id;
276
      Typ : Entity_Id) return Boolean;
277
   --  Similarly, a function F that needs no actuals can return an access to a
278
   --  subprogram, and the call F (X) interpreted as F.all (X). In this case
279
   --  the call may be overloaded with both interpretations.
280
 
281
   function Try_Object_Operation
282
     (N            : Node_Id;
283
      CW_Test_Only : Boolean := False) return Boolean;
284
   --  Ada 2005 (AI-252): Support the object.operation notation. If node N
285
   --  is a call in this notation, it is transformed into a normal subprogram
286
   --  call where the prefix is a parameter, and True is returned. If node
287
   --  N is not of this form, it is unchanged, and False is returned. if
288
   --  CW_Test_Only is true then N is an N_Selected_Component node which
289
   --  is part of a call to an entry or procedure of a tagged concurrent
290
   --  type and this routine is invoked to search for class-wide subprograms
291
   --  conflicting with the target entity.
292
 
293
   procedure wpo (T : Entity_Id);
294
   pragma Warnings (Off, wpo);
295
   --  Used for debugging: obtain list of primitive operations even if
296
   --  type is not frozen and dispatch table is not built yet.
297
 
298
   ------------------------
299
   -- Ambiguous_Operands --
300
   ------------------------
301
 
302
   procedure Ambiguous_Operands (N : Node_Id) is
303
      procedure List_Operand_Interps (Opnd : Node_Id);
304
 
305
      --------------------------
306
      -- List_Operand_Interps --
307
      --------------------------
308
 
309
      procedure List_Operand_Interps (Opnd : Node_Id) is
310
         Nam   : Node_Id;
311
         Err   : Node_Id := N;
312
 
313
      begin
314
         if Is_Overloaded (Opnd) then
315
            if Nkind (Opnd) in N_Op then
316
               Nam := Opnd;
317
            elsif Nkind (Opnd) = N_Function_Call then
318
               Nam := Name (Opnd);
319
            elsif Ada_Version >= Ada_2012 then
320
               declare
321
                  It : Interp;
322
                  I  : Interp_Index;
323
 
324
               begin
325
                  Get_First_Interp (Opnd, I, It);
326
                  while Present (It.Nam) loop
327
                     if Has_Implicit_Dereference (It.Typ) then
328
                        Error_Msg_N
329
                          ("can be interpreted as implicit dereference", Opnd);
330
                        return;
331
                     end if;
332
 
333
                     Get_Next_Interp (I, It);
334
                  end loop;
335
               end;
336
 
337
               return;
338
            end if;
339
 
340
         else
341
            return;
342
         end if;
343
 
344
         if Opnd = Left_Opnd (N) then
345
            Error_Msg_N ("\left operand has the following interpretations", N);
346
         else
347
            Error_Msg_N
348
              ("\right operand has the following interpretations", N);
349
            Err := Opnd;
350
         end if;
351
 
352
         List_Interps (Nam, Err);
353
      end List_Operand_Interps;
354
 
355
   --  Start of processing for Ambiguous_Operands
356
 
357
   begin
358
      if Nkind (N) in N_Membership_Test then
359
         Error_Msg_N ("ambiguous operands for membership",  N);
360
 
361
      elsif Nkind_In (N, N_Op_Eq, N_Op_Ne) then
362
         Error_Msg_N ("ambiguous operands for equality",  N);
363
 
364
      else
365
         Error_Msg_N ("ambiguous operands for comparison",  N);
366
      end if;
367
 
368
      if All_Errors_Mode then
369
         List_Operand_Interps (Left_Opnd  (N));
370
         List_Operand_Interps (Right_Opnd (N));
371
      else
372
         Error_Msg_N ("\use -gnatf switch for details", N);
373
      end if;
374
   end Ambiguous_Operands;
375
 
376
   -----------------------
377
   -- Analyze_Aggregate --
378
   -----------------------
379
 
380
   --  Most of the analysis of Aggregates requires that the type be known,
381
   --  and is therefore put off until resolution.
382
 
383
   procedure Analyze_Aggregate (N : Node_Id) is
384
   begin
385
      if No (Etype (N)) then
386
         Set_Etype (N, Any_Composite);
387
      end if;
388
   end Analyze_Aggregate;
389
 
390
   -----------------------
391
   -- Analyze_Allocator --
392
   -----------------------
393
 
394
   procedure Analyze_Allocator (N : Node_Id) is
395
      Loc      : constant Source_Ptr := Sloc (N);
396
      Sav_Errs : constant Nat        := Serious_Errors_Detected;
397
      E        : Node_Id             := Expression (N);
398
      Acc_Type : Entity_Id;
399
      Type_Id  : Entity_Id;
400
      P        : Node_Id;
401
      C        : Node_Id;
402
 
403
   begin
404
      Check_SPARK_Restriction ("allocator is not allowed", N);
405
 
406
      --  Deal with allocator restrictions
407
 
408
      --  In accordance with H.4(7), the No_Allocators restriction only applies
409
      --  to user-written allocators. The same consideration applies to the
410
      --  No_Allocators_Before_Elaboration restriction.
411
 
412
      if Comes_From_Source (N) then
413
         Check_Restriction (No_Allocators, N);
414
 
415
         --  Processing for No_Allocators_After_Elaboration, loop to look at
416
         --  enclosing context, checking task case and main subprogram case.
417
 
418
         C := N;
419
         P := Parent (C);
420
         while Present (P) loop
421
 
422
            --  In both cases we need a handled sequence of statements, where
423
            --  the occurrence of the allocator is within the statements.
424
 
425
            if Nkind (P) = N_Handled_Sequence_Of_Statements
426
              and then Is_List_Member (C)
427
              and then List_Containing (C) = Statements (P)
428
            then
429
               --  Check for allocator within task body, this is a definite
430
               --  violation of No_Allocators_After_Elaboration we can detect.
431
 
432
               if Nkind (Original_Node (Parent (P))) = N_Task_Body then
433
                  Check_Restriction (No_Allocators_After_Elaboration, N);
434
                  exit;
435
               end if;
436
 
437
               --  The other case is appearance in a subprogram body. This may
438
               --  be a violation if this is a library level subprogram, and it
439
               --  turns out to be used as the main program, but only the
440
               --  binder knows that, so just record the occurrence.
441
 
442
               if Nkind (Original_Node (Parent (P))) = N_Subprogram_Body
443
                 and then Nkind (Parent (Parent (P))) = N_Compilation_Unit
444
               then
445
                  Set_Has_Allocator (Current_Sem_Unit);
446
               end if;
447
            end if;
448
 
449
            C := P;
450
            P := Parent (C);
451
         end loop;
452
      end if;
453
 
454
      --  Ada 2012 (AI05-0111-3): Analyze the subpool_specification, if
455
      --  any. The expected type for the name is any type. A non-overloading
456
      --  rule then requires it to be of a type descended from
457
      --  System.Storage_Pools.Subpools.Subpool_Handle.
458
 
459
      --  This isn't exactly what the AI says, but it seems to be the right
460
      --  rule. The AI should be fixed.???
461
 
462
      declare
463
         Subpool : constant Node_Id := Subpool_Handle_Name (N);
464
 
465
      begin
466
         if Present (Subpool) then
467
            Analyze (Subpool);
468
 
469
            if Is_Overloaded (Subpool) then
470
               Error_Msg_N ("ambiguous subpool handle", Subpool);
471
            end if;
472
 
473
            --  Check that Etype (Subpool) is descended from Subpool_Handle
474
 
475
            Resolve (Subpool);
476
         end if;
477
      end;
478
 
479
      --  Analyze the qualified expression or subtype indication
480
 
481
      if Nkind (E) = N_Qualified_Expression then
482
         Acc_Type := Create_Itype (E_Allocator_Type, N);
483
         Set_Etype (Acc_Type, Acc_Type);
484
         Find_Type (Subtype_Mark (E));
485
 
486
         --  Analyze the qualified expression, and apply the name resolution
487
         --  rule given in  4.7(3).
488
 
489
         Analyze (E);
490
         Type_Id := Etype (E);
491
         Set_Directly_Designated_Type (Acc_Type, Type_Id);
492
 
493
         Resolve (Expression (E), Type_Id);
494
 
495
         --  Allocators generated by the build-in-place expansion mechanism
496
         --  are explicitly marked as coming from source but do not need to be
497
         --  checked for limited initialization. To exclude this case, ensure
498
         --  that the parent of the allocator is a source node.
499
 
500
         if Is_Limited_Type (Type_Id)
501
           and then Comes_From_Source (N)
502
           and then Comes_From_Source (Parent (N))
503
           and then not In_Instance_Body
504
         then
505
            if not OK_For_Limited_Init (Type_Id, Expression (E)) then
506
               Error_Msg_N ("initialization not allowed for limited types", N);
507
               Explain_Limited_Type (Type_Id, N);
508
            end if;
509
         end if;
510
 
511
         --  A qualified expression requires an exact match of the type,
512
         --  class-wide matching is not allowed.
513
 
514
         --  if Is_Class_Wide_Type (Type_Id)
515
         --    and then Base_Type
516
         --       (Etype (Expression (E))) /= Base_Type (Type_Id)
517
         --  then
518
         --     Wrong_Type (Expression (E), Type_Id);
519
         --  end if;
520
 
521
         Check_Non_Static_Context (Expression (E));
522
 
523
         --  We don't analyze the qualified expression itself because it's
524
         --  part of the allocator
525
 
526
         Set_Etype  (E, Type_Id);
527
 
528
      --  Case where allocator has a subtype indication
529
 
530
      else
531
         declare
532
            Def_Id   : Entity_Id;
533
            Base_Typ : Entity_Id;
534
 
535
         begin
536
            --  If the allocator includes a N_Subtype_Indication then a
537
            --  constraint is present, otherwise the node is a subtype mark.
538
            --  Introduce an explicit subtype declaration into the tree
539
            --  defining some anonymous subtype and rewrite the allocator to
540
            --  use this subtype rather than the subtype indication.
541
 
542
            --  It is important to introduce the explicit subtype declaration
543
            --  so that the bounds of the subtype indication are attached to
544
            --  the tree in case the allocator is inside a generic unit.
545
 
546
            if Nkind (E) = N_Subtype_Indication then
547
 
548
               --  A constraint is only allowed for a composite type in Ada
549
               --  95. In Ada 83, a constraint is also allowed for an
550
               --  access-to-composite type, but the constraint is ignored.
551
 
552
               Find_Type (Subtype_Mark (E));
553
               Base_Typ := Entity (Subtype_Mark (E));
554
 
555
               if Is_Elementary_Type (Base_Typ) then
556
                  if not (Ada_Version = Ada_83
557
                           and then Is_Access_Type (Base_Typ))
558
                  then
559
                     Error_Msg_N ("constraint not allowed here", E);
560
 
561
                     if Nkind (Constraint (E)) =
562
                       N_Index_Or_Discriminant_Constraint
563
                     then
564
                        Error_Msg_N -- CODEFIX
565
                          ("\if qualified expression was meant, " &
566
                              "use apostrophe", Constraint (E));
567
                     end if;
568
                  end if;
569
 
570
                  --  Get rid of the bogus constraint:
571
 
572
                  Rewrite (E, New_Copy_Tree (Subtype_Mark (E)));
573
                  Analyze_Allocator (N);
574
                  return;
575
 
576
               --  Ada 2005, AI-363: if the designated type has a constrained
577
               --  partial view, it cannot receive a discriminant constraint,
578
               --  and the allocated object is unconstrained.
579
 
580
               elsif Ada_Version >= Ada_2005
581
                 and then Effectively_Has_Constrained_Partial_View
582
                            (Typ  => Base_Typ,
583
                             Scop => Current_Scope)
584
               then
585
                  Error_Msg_N
586
                    ("constraint not allowed when type " &
587
                      "has a constrained partial view", Constraint (E));
588
               end if;
589
 
590
               if Expander_Active then
591
                  Def_Id := Make_Temporary (Loc, 'S');
592
 
593
                  Insert_Action (E,
594
                    Make_Subtype_Declaration (Loc,
595
                      Defining_Identifier => Def_Id,
596
                      Subtype_Indication  => Relocate_Node (E)));
597
 
598
                  if Sav_Errs /= Serious_Errors_Detected
599
                    and then Nkind (Constraint (E)) =
600
                               N_Index_Or_Discriminant_Constraint
601
                  then
602
                     Error_Msg_N -- CODEFIX
603
                       ("if qualified expression was meant, " &
604
                           "use apostrophe!", Constraint (E));
605
                  end if;
606
 
607
                  E := New_Occurrence_Of (Def_Id, Loc);
608
                  Rewrite (Expression (N), E);
609
               end if;
610
            end if;
611
 
612
            Type_Id := Process_Subtype (E, N);
613
            Acc_Type := Create_Itype (E_Allocator_Type, N);
614
            Set_Etype                    (Acc_Type, Acc_Type);
615
            Set_Directly_Designated_Type (Acc_Type, Type_Id);
616
            Check_Fully_Declared (Type_Id, N);
617
 
618
            --  Ada 2005 (AI-231): If the designated type is itself an access
619
            --  type that excludes null, its default initialization will
620
            --  be a null object, and we can insert an unconditional raise
621
            --  before the allocator.
622
 
623
            --  Ada 2012 (AI-104): A not null indication here is altogether
624
            --  illegal.
625
 
626
            if Can_Never_Be_Null (Type_Id) then
627
               declare
628
                  Not_Null_Check : constant Node_Id :=
629
                                     Make_Raise_Constraint_Error (Sloc (E),
630
                                       Reason => CE_Null_Not_Allowed);
631
 
632
               begin
633
                  if Ada_Version >= Ada_2012 then
634
                     Error_Msg_N
635
                       ("an uninitialized allocator cannot have"
636
                         & " a null exclusion", N);
637
 
638
                  elsif Expander_Active then
639
                     Insert_Action (N, Not_Null_Check);
640
                     Analyze (Not_Null_Check);
641
 
642
                  else
643
                     Error_Msg_N ("null value not allowed here?", E);
644
                  end if;
645
               end;
646
            end if;
647
 
648
            --  Check restriction against dynamically allocated protected
649
            --  objects. Note that when limited aggregates are supported,
650
            --  a similar test should be applied to an allocator with a
651
            --  qualified expression ???
652
 
653
            if Is_Protected_Type (Type_Id) then
654
               Check_Restriction (No_Protected_Type_Allocators, N);
655
            end if;
656
 
657
            --  Check for missing initialization. Skip this check if we already
658
            --  had errors on analyzing the allocator, since in that case these
659
            --  are probably cascaded errors.
660
 
661
            if Is_Indefinite_Subtype (Type_Id)
662
              and then Serious_Errors_Detected = Sav_Errs
663
            then
664
               if Is_Class_Wide_Type (Type_Id) then
665
                  Error_Msg_N
666
                    ("initialization required in class-wide allocation", N);
667
               else
668
                  if Ada_Version < Ada_2005
669
                    and then Is_Limited_Type (Type_Id)
670
                  then
671
                     Error_Msg_N ("unconstrained allocation not allowed", N);
672
 
673
                     if Is_Array_Type (Type_Id) then
674
                        Error_Msg_N
675
                          ("\constraint with array bounds required", N);
676
 
677
                     elsif Has_Unknown_Discriminants (Type_Id) then
678
                        null;
679
 
680
                     else pragma Assert (Has_Discriminants (Type_Id));
681
                        Error_Msg_N
682
                          ("\constraint with discriminant values required", N);
683
                     end if;
684
 
685
                  --  Limited Ada 2005 and general non-limited case
686
 
687
                  else
688
                     Error_Msg_N
689
                       ("uninitialized unconstrained allocation not allowed",
690
                        N);
691
 
692
                     if Is_Array_Type (Type_Id) then
693
                        Error_Msg_N
694
                          ("\qualified expression or constraint with " &
695
                           "array bounds required", N);
696
 
697
                     elsif Has_Unknown_Discriminants (Type_Id) then
698
                        Error_Msg_N ("\qualified expression required", N);
699
 
700
                     else pragma Assert (Has_Discriminants (Type_Id));
701
                        Error_Msg_N
702
                          ("\qualified expression or constraint with " &
703
                           "discriminant values required", N);
704
                     end if;
705
                  end if;
706
               end if;
707
            end if;
708
         end;
709
      end if;
710
 
711
      if Is_Abstract_Type (Type_Id) then
712
         Error_Msg_N ("cannot allocate abstract object", E);
713
      end if;
714
 
715
      if Has_Task (Designated_Type (Acc_Type)) then
716
         Check_Restriction (No_Tasking, N);
717
         Check_Restriction (Max_Tasks, N);
718
         Check_Restriction (No_Task_Allocators, N);
719
      end if;
720
 
721
      --  AI05-0013-1: No_Nested_Finalization forbids allocators if the access
722
      --  type is nested, and the designated type needs finalization. The rule
723
      --  is conservative in that class-wide types need finalization.
724
 
725
      if Needs_Finalization (Designated_Type (Acc_Type))
726
        and then not Is_Library_Level_Entity (Acc_Type)
727
      then
728
         Check_Restriction (No_Nested_Finalization, N);
729
      end if;
730
 
731
      --  Check that an allocator of a nested access type doesn't create a
732
      --  protected object when restriction No_Local_Protected_Objects applies.
733
      --  We don't have an equivalent to Has_Task for protected types, so only
734
      --  cases where the designated type itself is a protected type are
735
      --  currently checked. ???
736
 
737
      if Is_Protected_Type (Designated_Type (Acc_Type))
738
        and then not Is_Library_Level_Entity (Acc_Type)
739
      then
740
         Check_Restriction (No_Local_Protected_Objects, N);
741
      end if;
742
 
743
      --  If the No_Streams restriction is set, check that the type of the
744
      --  object is not, and does not contain, any subtype derived from
745
      --  Ada.Streams.Root_Stream_Type. Note that we guard the call to
746
      --  Has_Stream just for efficiency reasons. There is no point in
747
      --  spending time on a Has_Stream check if the restriction is not set.
748
 
749
      if Restriction_Check_Required (No_Streams) then
750
         if Has_Stream (Designated_Type (Acc_Type)) then
751
            Check_Restriction (No_Streams, N);
752
         end if;
753
      end if;
754
 
755
      Set_Etype (N, Acc_Type);
756
 
757
      if not Is_Library_Level_Entity (Acc_Type) then
758
         Check_Restriction (No_Local_Allocators, N);
759
      end if;
760
 
761
      if Serious_Errors_Detected > Sav_Errs then
762
         Set_Error_Posted (N);
763
         Set_Etype (N, Any_Type);
764
      end if;
765
   end Analyze_Allocator;
766
 
767
   ---------------------------
768
   -- Analyze_Arithmetic_Op --
769
   ---------------------------
770
 
771
   procedure Analyze_Arithmetic_Op (N : Node_Id) is
772
      L     : constant Node_Id := Left_Opnd (N);
773
      R     : constant Node_Id := Right_Opnd (N);
774
      Op_Id : Entity_Id;
775
 
776
   begin
777
      Candidate_Type := Empty;
778
      Analyze_Expression (L);
779
      Analyze_Expression (R);
780
 
781
      --  If the entity is already set, the node is the instantiation of a
782
      --  generic node with a non-local reference, or was manufactured by a
783
      --  call to Make_Op_xxx. In either case the entity is known to be valid,
784
      --  and we do not need to collect interpretations, instead we just get
785
      --  the single possible interpretation.
786
 
787
      Op_Id := Entity (N);
788
 
789
      if Present (Op_Id) then
790
         if Ekind (Op_Id) = E_Operator then
791
 
792
            if Nkind_In (N, N_Op_Divide, N_Op_Mod, N_Op_Multiply, N_Op_Rem)
793
              and then Treat_Fixed_As_Integer (N)
794
            then
795
               null;
796
            else
797
               Set_Etype (N, Any_Type);
798
               Find_Arithmetic_Types (L, R, Op_Id, N);
799
            end if;
800
 
801
         else
802
            Set_Etype (N, Any_Type);
803
            Add_One_Interp (N, Op_Id, Etype (Op_Id));
804
         end if;
805
 
806
      --  Entity is not already set, so we do need to collect interpretations
807
 
808
      else
809
         Op_Id := Get_Name_Entity_Id (Chars (N));
810
         Set_Etype (N, Any_Type);
811
 
812
         while Present (Op_Id) loop
813
            if Ekind (Op_Id) = E_Operator
814
              and then Present (Next_Entity (First_Entity (Op_Id)))
815
            then
816
               Find_Arithmetic_Types (L, R, Op_Id, N);
817
 
818
            --  The following may seem superfluous, because an operator cannot
819
            --  be generic, but this ignores the cleverness of the author of
820
            --  ACVC bc1013a.
821
 
822
            elsif Is_Overloadable (Op_Id) then
823
               Analyze_User_Defined_Binary_Op (N, Op_Id);
824
            end if;
825
 
826
            Op_Id := Homonym (Op_Id);
827
         end loop;
828
      end if;
829
 
830
      Operator_Check (N);
831
   end Analyze_Arithmetic_Op;
832
 
833
   ------------------
834
   -- Analyze_Call --
835
   ------------------
836
 
837
   --  Function, procedure, and entry calls are checked here. The Name in
838
   --  the call may be overloaded. The actuals have been analyzed and may
839
   --  themselves be overloaded. On exit from this procedure, the node N
840
   --  may have zero, one or more interpretations. In the first case an
841
   --  error message is produced. In the last case, the node is flagged
842
   --  as overloaded and the interpretations are collected in All_Interp.
843
 
844
   --  If the name is an Access_To_Subprogram, it cannot be overloaded, but
845
   --  the type-checking is similar to that of other calls.
846
 
847
   procedure Analyze_Call (N : Node_Id) is
848
      Actuals : constant List_Id := Parameter_Associations (N);
849
      Nam     : Node_Id;
850
      X       : Interp_Index;
851
      It      : Interp;
852
      Nam_Ent : Entity_Id;
853
      Success : Boolean := False;
854
 
855
      Deref : Boolean := False;
856
      --  Flag indicates whether an interpretation of the prefix is a
857
      --  parameterless call that returns an access_to_subprogram.
858
 
859
      procedure Check_Mixed_Parameter_And_Named_Associations;
860
      --  Check that parameter and named associations are not mixed. This is
861
      --  a restriction in SPARK mode.
862
 
863
      function Name_Denotes_Function return Boolean;
864
      --  If the type of the name is an access to subprogram, this may be the
865
      --  type of a name, or the return type of the function being called. If
866
      --  the name is not an entity then it can denote a protected function.
867
      --  Until we distinguish Etype from Return_Type, we must use this routine
868
      --  to resolve the meaning of the name in the call.
869
 
870
      procedure No_Interpretation;
871
      --  Output error message when no valid interpretation exists
872
 
873
      --------------------------------------------------
874
      -- Check_Mixed_Parameter_And_Named_Associations --
875
      --------------------------------------------------
876
 
877
      procedure Check_Mixed_Parameter_And_Named_Associations is
878
         Actual     : Node_Id;
879
         Named_Seen : Boolean;
880
 
881
      begin
882
         Named_Seen := False;
883
 
884
         Actual := First (Actuals);
885
         while Present (Actual) loop
886
            case Nkind (Actual) is
887
               when N_Parameter_Association =>
888
                  if Named_Seen then
889
                     Check_SPARK_Restriction
890
                       ("named association cannot follow positional one",
891
                        Actual);
892
                     exit;
893
                  end if;
894
               when others =>
895
                  Named_Seen := True;
896
            end case;
897
 
898
            Next (Actual);
899
         end loop;
900
      end Check_Mixed_Parameter_And_Named_Associations;
901
 
902
      ---------------------------
903
      -- Name_Denotes_Function --
904
      ---------------------------
905
 
906
      function Name_Denotes_Function return Boolean is
907
      begin
908
         if Is_Entity_Name (Nam) then
909
            return Ekind (Entity (Nam)) = E_Function;
910
 
911
         elsif Nkind (Nam) = N_Selected_Component then
912
            return Ekind (Entity (Selector_Name (Nam))) = E_Function;
913
 
914
         else
915
            return False;
916
         end if;
917
      end Name_Denotes_Function;
918
 
919
      -----------------------
920
      -- No_Interpretation --
921
      -----------------------
922
 
923
      procedure No_Interpretation is
924
         L : constant Boolean   := Is_List_Member (N);
925
         K : constant Node_Kind := Nkind (Parent (N));
926
 
927
      begin
928
         --  If the node is in a list whose parent is not an expression then it
929
         --  must be an attempted procedure call.
930
 
931
         if L and then K not in N_Subexpr then
932
            if Ekind (Entity (Nam)) = E_Generic_Procedure then
933
               Error_Msg_NE
934
                 ("must instantiate generic procedure& before call",
935
                  Nam, Entity (Nam));
936
            else
937
               Error_Msg_N
938
                 ("procedure or entry name expected", Nam);
939
            end if;
940
 
941
         --  Check for tasking cases where only an entry call will do
942
 
943
         elsif not L
944
           and then Nkind_In (K, N_Entry_Call_Alternative,
945
                                 N_Triggering_Alternative)
946
         then
947
            Error_Msg_N ("entry name expected", Nam);
948
 
949
         --  Otherwise give general error message
950
 
951
         else
952
            Error_Msg_N ("invalid prefix in call", Nam);
953
         end if;
954
      end No_Interpretation;
955
 
956
   --  Start of processing for Analyze_Call
957
 
958
   begin
959
      if Restriction_Check_Required (SPARK) then
960
         Check_Mixed_Parameter_And_Named_Associations;
961
      end if;
962
 
963
      --  Initialize the type of the result of the call to the error type,
964
      --  which will be reset if the type is successfully resolved.
965
 
966
      Set_Etype (N, Any_Type);
967
 
968
      Nam := Name (N);
969
 
970
      if not Is_Overloaded (Nam) then
971
 
972
         --  Only one interpretation to check
973
 
974
         if Ekind (Etype (Nam)) = E_Subprogram_Type then
975
            Nam_Ent := Etype (Nam);
976
 
977
         --  If the prefix is an access_to_subprogram, this may be an indirect
978
         --  call. This is the case if the name in the call is not an entity
979
         --  name, or if it is a function name in the context of a procedure
980
         --  call. In this latter case, we have a call to a parameterless
981
         --  function that returns a pointer_to_procedure which is the entity
982
         --  being called. Finally, F (X) may be a call to a parameterless
983
         --  function that returns a pointer to a function with parameters.
984
 
985
         elsif Is_Access_Type (Etype (Nam))
986
           and then Ekind (Designated_Type (Etype (Nam))) = E_Subprogram_Type
987
           and then
988
             (not Name_Denotes_Function
989
                or else Nkind (N) = N_Procedure_Call_Statement
990
                or else
991
                  (Nkind (Parent (N)) /= N_Explicit_Dereference
992
                     and then Is_Entity_Name (Nam)
993
                     and then No (First_Formal (Entity (Nam)))
994
                     and then Present (Actuals)))
995
         then
996
            Nam_Ent := Designated_Type (Etype (Nam));
997
            Insert_Explicit_Dereference (Nam);
998
 
999
         --  Selected component case. Simple entry or protected operation,
1000
         --  where the entry name is given by the selector name.
1001
 
1002
         elsif Nkind (Nam) = N_Selected_Component then
1003
            Nam_Ent := Entity (Selector_Name (Nam));
1004
 
1005
            if not Ekind_In (Nam_Ent, E_Entry,
1006
                                      E_Entry_Family,
1007
                                      E_Function,
1008
                                      E_Procedure)
1009
            then
1010
               Error_Msg_N ("name in call is not a callable entity", Nam);
1011
               Set_Etype (N, Any_Type);
1012
               return;
1013
            end if;
1014
 
1015
         --  If the name is an Indexed component, it can be a call to a member
1016
         --  of an entry family. The prefix must be a selected component whose
1017
         --  selector is the entry. Analyze_Procedure_Call normalizes several
1018
         --  kinds of call into this form.
1019
 
1020
         elsif Nkind (Nam) = N_Indexed_Component then
1021
            if Nkind (Prefix (Nam)) = N_Selected_Component then
1022
               Nam_Ent := Entity (Selector_Name (Prefix (Nam)));
1023
            else
1024
               Error_Msg_N ("name in call is not a callable entity", Nam);
1025
               Set_Etype (N, Any_Type);
1026
               return;
1027
            end if;
1028
 
1029
         elsif not Is_Entity_Name (Nam) then
1030
            Error_Msg_N ("name in call is not a callable entity", Nam);
1031
            Set_Etype (N, Any_Type);
1032
            return;
1033
 
1034
         else
1035
            Nam_Ent := Entity (Nam);
1036
 
1037
            --  If no interpretations, give error message
1038
 
1039
            if not Is_Overloadable (Nam_Ent) then
1040
               No_Interpretation;
1041
               return;
1042
            end if;
1043
         end if;
1044
 
1045
         --  Operations generated for RACW stub types are called only through
1046
         --  dispatching, and can never be the static interpretation of a call.
1047
 
1048
         if Is_RACW_Stub_Type_Operation (Nam_Ent) then
1049
            No_Interpretation;
1050
            return;
1051
         end if;
1052
 
1053
         Analyze_One_Call (N, Nam_Ent, True, Success);
1054
 
1055
         --  If this is an indirect call, the return type of the access_to
1056
         --  subprogram may be an incomplete type. At the point of the call,
1057
         --  use the full type if available, and at the same time update the
1058
         --  return type of the access_to_subprogram.
1059
 
1060
         if Success
1061
           and then Nkind (Nam) = N_Explicit_Dereference
1062
           and then Ekind (Etype (N)) = E_Incomplete_Type
1063
           and then Present (Full_View (Etype (N)))
1064
         then
1065
            Set_Etype (N, Full_View (Etype (N)));
1066
            Set_Etype (Nam_Ent, Etype (N));
1067
         end if;
1068
 
1069
      else
1070
         --  An overloaded selected component must denote overloaded operations
1071
         --  of a concurrent type. The interpretations are attached to the
1072
         --  simple name of those operations.
1073
 
1074
         if Nkind (Nam) = N_Selected_Component then
1075
            Nam := Selector_Name (Nam);
1076
         end if;
1077
 
1078
         Get_First_Interp (Nam, X, It);
1079
 
1080
         while Present (It.Nam) loop
1081
            Nam_Ent := It.Nam;
1082
            Deref   := False;
1083
 
1084
            --  Name may be call that returns an access to subprogram, or more
1085
            --  generally an overloaded expression one of whose interpretations
1086
            --  yields an access to subprogram. If the name is an entity, we do
1087
            --  not dereference, because the node is a call that returns the
1088
            --  access type: note difference between f(x), where the call may
1089
            --  return an access subprogram type, and f(x)(y), where the type
1090
            --  returned by the call to f is implicitly dereferenced to analyze
1091
            --  the outer call.
1092
 
1093
            if Is_Access_Type (Nam_Ent) then
1094
               Nam_Ent := Designated_Type (Nam_Ent);
1095
 
1096
            elsif Is_Access_Type (Etype (Nam_Ent))
1097
              and then
1098
                (not Is_Entity_Name (Nam)
1099
                   or else Nkind (N) = N_Procedure_Call_Statement)
1100
              and then Ekind (Designated_Type (Etype (Nam_Ent)))
1101
                                                          = E_Subprogram_Type
1102
            then
1103
               Nam_Ent := Designated_Type (Etype (Nam_Ent));
1104
 
1105
               if Is_Entity_Name (Nam) then
1106
                  Deref := True;
1107
               end if;
1108
            end if;
1109
 
1110
            --  If the call has been rewritten from a prefixed call, the first
1111
            --  parameter has been analyzed, but may need a subsequent
1112
            --  dereference, so skip its analysis now.
1113
 
1114
            if N /= Original_Node (N)
1115
              and then Nkind (Original_Node (N)) = Nkind (N)
1116
              and then Nkind (Name (N)) /= Nkind (Name (Original_Node (N)))
1117
              and then Present (Parameter_Associations (N))
1118
              and then Present (Etype (First (Parameter_Associations (N))))
1119
            then
1120
               Analyze_One_Call
1121
                 (N, Nam_Ent, False, Success, Skip_First => True);
1122
            else
1123
               Analyze_One_Call (N, Nam_Ent, False, Success);
1124
            end if;
1125
 
1126
            --  If the interpretation succeeds, mark the proper type of the
1127
            --  prefix (any valid candidate will do). If not, remove the
1128
            --  candidate interpretation. This only needs to be done for
1129
            --  overloaded protected operations, for other entities disambi-
1130
            --  guation is done directly in Resolve.
1131
 
1132
            if Success then
1133
               if Deref
1134
                 and then Nkind (Parent (N)) /= N_Explicit_Dereference
1135
               then
1136
                  Set_Entity (Nam, It.Nam);
1137
                  Insert_Explicit_Dereference (Nam);
1138
                  Set_Etype (Nam, Nam_Ent);
1139
 
1140
               else
1141
                  Set_Etype (Nam, It.Typ);
1142
               end if;
1143
 
1144
            elsif Nkind_In (Name (N), N_Selected_Component,
1145
                                      N_Function_Call)
1146
            then
1147
               Remove_Interp (X);
1148
            end if;
1149
 
1150
            Get_Next_Interp (X, It);
1151
         end loop;
1152
 
1153
         --  If the name is the result of a function call, it can only
1154
         --  be a call to a function returning an access to subprogram.
1155
         --  Insert explicit dereference.
1156
 
1157
         if Nkind (Nam) = N_Function_Call then
1158
            Insert_Explicit_Dereference (Nam);
1159
         end if;
1160
 
1161
         if Etype (N) = Any_Type then
1162
 
1163
            --  None of the interpretations is compatible with the actuals
1164
 
1165
            Diagnose_Call (N, Nam);
1166
 
1167
            --  Special checks for uninstantiated put routines
1168
 
1169
            if Nkind (N) = N_Procedure_Call_Statement
1170
              and then Is_Entity_Name (Nam)
1171
              and then Chars (Nam) = Name_Put
1172
              and then List_Length (Actuals) = 1
1173
            then
1174
               declare
1175
                  Arg : constant Node_Id := First (Actuals);
1176
                  Typ : Entity_Id;
1177
 
1178
               begin
1179
                  if Nkind (Arg) = N_Parameter_Association then
1180
                     Typ := Etype (Explicit_Actual_Parameter (Arg));
1181
                  else
1182
                     Typ := Etype (Arg);
1183
                  end if;
1184
 
1185
                  if Is_Signed_Integer_Type (Typ) then
1186
                     Error_Msg_N
1187
                       ("possible missing instantiation of " &
1188
                          "'Text_'I'O.'Integer_'I'O!", Nam);
1189
 
1190
                  elsif Is_Modular_Integer_Type (Typ) then
1191
                     Error_Msg_N
1192
                       ("possible missing instantiation of " &
1193
                          "'Text_'I'O.'Modular_'I'O!", Nam);
1194
 
1195
                  elsif Is_Floating_Point_Type (Typ) then
1196
                     Error_Msg_N
1197
                       ("possible missing instantiation of " &
1198
                          "'Text_'I'O.'Float_'I'O!", Nam);
1199
 
1200
                  elsif Is_Ordinary_Fixed_Point_Type (Typ) then
1201
                     Error_Msg_N
1202
                       ("possible missing instantiation of " &
1203
                          "'Text_'I'O.'Fixed_'I'O!", Nam);
1204
 
1205
                  elsif Is_Decimal_Fixed_Point_Type (Typ) then
1206
                     Error_Msg_N
1207
                       ("possible missing instantiation of " &
1208
                          "'Text_'I'O.'Decimal_'I'O!", Nam);
1209
 
1210
                  elsif Is_Enumeration_Type (Typ) then
1211
                     Error_Msg_N
1212
                       ("possible missing instantiation of " &
1213
                          "'Text_'I'O.'Enumeration_'I'O!", Nam);
1214
                  end if;
1215
               end;
1216
            end if;
1217
 
1218
         elsif not Is_Overloaded (N)
1219
           and then Is_Entity_Name (Nam)
1220
         then
1221
            --  Resolution yields a single interpretation. Verify that the
1222
            --  reference has capitalization consistent with the declaration.
1223
 
1224
            Set_Entity_With_Style_Check (Nam, Entity (Nam));
1225
            Generate_Reference (Entity (Nam), Nam);
1226
 
1227
            Set_Etype (Nam, Etype (Entity (Nam)));
1228
         else
1229
            Remove_Abstract_Operations (N);
1230
         end if;
1231
 
1232
         End_Interp_List;
1233
      end if;
1234
   end Analyze_Call;
1235
 
1236
   -----------------------------
1237
   -- Analyze_Case_Expression --
1238
   -----------------------------
1239
 
1240
   procedure Analyze_Case_Expression (N : Node_Id) is
1241
      Expr      : constant Node_Id := Expression (N);
1242
      FirstX    : constant Node_Id := Expression (First (Alternatives (N)));
1243
      Alt       : Node_Id;
1244
      Exp_Type  : Entity_Id;
1245
      Exp_Btype : Entity_Id;
1246
 
1247
      Dont_Care      : Boolean;
1248
      Others_Present : Boolean;
1249
 
1250
      procedure Non_Static_Choice_Error (Choice : Node_Id);
1251
      --  Error routine invoked by the generic instantiation below when
1252
      --  the case expression has a non static choice.
1253
 
1254
      package Case_Choices_Processing is new
1255
        Generic_Choices_Processing
1256
          (Get_Alternatives          => Alternatives,
1257
           Get_Choices               => Discrete_Choices,
1258
           Process_Empty_Choice      => No_OP,
1259
           Process_Non_Static_Choice => Non_Static_Choice_Error,
1260
           Process_Associated_Node   => No_OP);
1261
      use Case_Choices_Processing;
1262
 
1263
      -----------------------------
1264
      -- Non_Static_Choice_Error --
1265
      -----------------------------
1266
 
1267
      procedure Non_Static_Choice_Error (Choice : Node_Id) is
1268
      begin
1269
         Flag_Non_Static_Expr
1270
           ("choice given in case expression is not static!", Choice);
1271
      end Non_Static_Choice_Error;
1272
 
1273
   --  Start of processing for Analyze_Case_Expression
1274
 
1275
   begin
1276
      if Comes_From_Source (N) then
1277
         Check_Compiler_Unit (N);
1278
      end if;
1279
 
1280
      Analyze_And_Resolve (Expr, Any_Discrete);
1281
      Check_Unset_Reference (Expr);
1282
      Exp_Type := Etype (Expr);
1283
      Exp_Btype := Base_Type (Exp_Type);
1284
 
1285
      Alt := First (Alternatives (N));
1286
      while Present (Alt) loop
1287
         Analyze (Expression (Alt));
1288
         Next (Alt);
1289
      end loop;
1290
 
1291
      if not Is_Overloaded (FirstX) then
1292
         Set_Etype (N, Etype (FirstX));
1293
 
1294
      else
1295
         declare
1296
            I  : Interp_Index;
1297
            It : Interp;
1298
 
1299
         begin
1300
            Set_Etype (N, Any_Type);
1301
 
1302
            Get_First_Interp (FirstX, I, It);
1303
            while Present (It.Nam) loop
1304
 
1305
               --  For each interpretation of the first expression, we only
1306
               --  add the interpretation if every other expression in the
1307
               --  case expression alternatives has a compatible type.
1308
 
1309
               Alt := Next (First (Alternatives (N)));
1310
               while Present (Alt) loop
1311
                  exit when not Has_Compatible_Type (Expression (Alt), It.Typ);
1312
                  Next (Alt);
1313
               end loop;
1314
 
1315
               if No (Alt) then
1316
                  Add_One_Interp (N, It.Typ, It.Typ);
1317
               end if;
1318
 
1319
               Get_Next_Interp (I, It);
1320
            end loop;
1321
         end;
1322
      end if;
1323
 
1324
      Exp_Btype := Base_Type (Exp_Type);
1325
 
1326
      --  The expression must be of a discrete type which must be determinable
1327
      --  independently of the context in which the expression occurs, but
1328
      --  using the fact that the expression must be of a discrete type.
1329
      --  Moreover, the type this expression must not be a character literal
1330
      --  (which is always ambiguous).
1331
 
1332
      --  If error already reported by Resolve, nothing more to do
1333
 
1334
      if Exp_Btype = Any_Discrete
1335
        or else Exp_Btype = Any_Type
1336
      then
1337
         return;
1338
 
1339
      elsif Exp_Btype = Any_Character then
1340
         Error_Msg_N
1341
           ("character literal as case expression is ambiguous", Expr);
1342
         return;
1343
      end if;
1344
 
1345
      --  If the case expression is a formal object of mode in out, then
1346
      --  treat it as having a nonstatic subtype by forcing use of the base
1347
      --  type (which has to get passed to Check_Case_Choices below).  Also
1348
      --  use base type when the case expression is parenthesized.
1349
 
1350
      if Paren_Count (Expr) > 0
1351
        or else (Is_Entity_Name (Expr)
1352
                  and then Ekind (Entity (Expr)) = E_Generic_In_Out_Parameter)
1353
      then
1354
         Exp_Type := Exp_Btype;
1355
      end if;
1356
 
1357
      --  Call instantiated Analyze_Choices which does the rest of the work
1358
 
1359
      Analyze_Choices (N, Exp_Type, Dont_Care, Others_Present);
1360
 
1361
      if Exp_Type = Universal_Integer and then not Others_Present then
1362
         Error_Msg_N
1363
           ("case on universal integer requires OTHERS choice", Expr);
1364
      end if;
1365
   end Analyze_Case_Expression;
1366
 
1367
   ---------------------------
1368
   -- Analyze_Comparison_Op --
1369
   ---------------------------
1370
 
1371
   procedure Analyze_Comparison_Op (N : Node_Id) is
1372
      L     : constant Node_Id := Left_Opnd (N);
1373
      R     : constant Node_Id := Right_Opnd (N);
1374
      Op_Id : Entity_Id        := Entity (N);
1375
 
1376
   begin
1377
      Set_Etype (N, Any_Type);
1378
      Candidate_Type := Empty;
1379
 
1380
      Analyze_Expression (L);
1381
      Analyze_Expression (R);
1382
 
1383
      if Present (Op_Id) then
1384
         if Ekind (Op_Id) = E_Operator then
1385
            Find_Comparison_Types (L, R, Op_Id, N);
1386
         else
1387
            Add_One_Interp (N, Op_Id, Etype (Op_Id));
1388
         end if;
1389
 
1390
         if Is_Overloaded (L) then
1391
            Set_Etype (L, Intersect_Types (L, R));
1392
         end if;
1393
 
1394
      else
1395
         Op_Id := Get_Name_Entity_Id (Chars (N));
1396
         while Present (Op_Id) loop
1397
            if Ekind (Op_Id) = E_Operator then
1398
               Find_Comparison_Types (L, R, Op_Id, N);
1399
            else
1400
               Analyze_User_Defined_Binary_Op (N, Op_Id);
1401
            end if;
1402
 
1403
            Op_Id := Homonym (Op_Id);
1404
         end loop;
1405
      end if;
1406
 
1407
      Operator_Check (N);
1408
   end Analyze_Comparison_Op;
1409
 
1410
   ---------------------------
1411
   -- Analyze_Concatenation --
1412
   ---------------------------
1413
 
1414
   procedure Analyze_Concatenation (N : Node_Id) is
1415
 
1416
      --  We wish to avoid deep recursion, because concatenations are often
1417
      --  deeply nested, as in A&B&...&Z. Therefore, we walk down the left
1418
      --  operands nonrecursively until we find something that is not a
1419
      --  concatenation (A in this case), or has already been analyzed. We
1420
      --  analyze that, and then walk back up the tree following Parent
1421
      --  pointers, calling Analyze_Concatenation_Rest to do the rest of the
1422
      --  work at each level. The Parent pointers allow us to avoid recursion,
1423
      --  and thus avoid running out of memory.
1424
 
1425
      NN : Node_Id := N;
1426
      L  : Node_Id;
1427
 
1428
   begin
1429
      Candidate_Type := Empty;
1430
 
1431
      --  The following code is equivalent to:
1432
 
1433
      --    Set_Etype (N, Any_Type);
1434
      --    Analyze_Expression (Left_Opnd (N));
1435
      --    Analyze_Concatenation_Rest (N);
1436
 
1437
      --  where the Analyze_Expression call recurses back here if the left
1438
      --  operand is a concatenation.
1439
 
1440
      --  Walk down left operands
1441
 
1442
      loop
1443
         Set_Etype (NN, Any_Type);
1444
         L := Left_Opnd (NN);
1445
         exit when Nkind (L) /= N_Op_Concat or else Analyzed (L);
1446
         NN := L;
1447
      end loop;
1448
 
1449
      --  Now (given the above example) NN is A&B and L is A
1450
 
1451
      --  First analyze L ...
1452
 
1453
      Analyze_Expression (L);
1454
 
1455
      --  ... then walk NN back up until we reach N (where we started), calling
1456
      --  Analyze_Concatenation_Rest along the way.
1457
 
1458
      loop
1459
         Analyze_Concatenation_Rest (NN);
1460
         exit when NN = N;
1461
         NN := Parent (NN);
1462
      end loop;
1463
   end Analyze_Concatenation;
1464
 
1465
   --------------------------------
1466
   -- Analyze_Concatenation_Rest --
1467
   --------------------------------
1468
 
1469
   --  If the only one-dimensional array type in scope is String,
1470
   --  this is the resulting type of the operation. Otherwise there
1471
   --  will be a concatenation operation defined for each user-defined
1472
   --  one-dimensional array.
1473
 
1474
   procedure Analyze_Concatenation_Rest (N : Node_Id) is
1475
      L     : constant Node_Id := Left_Opnd (N);
1476
      R     : constant Node_Id := Right_Opnd (N);
1477
      Op_Id : Entity_Id        := Entity (N);
1478
      LT    : Entity_Id;
1479
      RT    : Entity_Id;
1480
 
1481
   begin
1482
      Analyze_Expression (R);
1483
 
1484
      --  If the entity is present, the node appears in an instance, and
1485
      --  denotes a predefined concatenation operation. The resulting type is
1486
      --  obtained from the arguments when possible. If the arguments are
1487
      --  aggregates, the array type and the concatenation type must be
1488
      --  visible.
1489
 
1490
      if Present (Op_Id) then
1491
         if Ekind (Op_Id) = E_Operator then
1492
            LT := Base_Type (Etype (L));
1493
            RT := Base_Type (Etype (R));
1494
 
1495
            if Is_Array_Type (LT)
1496
              and then (RT = LT or else RT = Base_Type (Component_Type (LT)))
1497
            then
1498
               Add_One_Interp (N, Op_Id, LT);
1499
 
1500
            elsif Is_Array_Type (RT)
1501
              and then LT = Base_Type (Component_Type (RT))
1502
            then
1503
               Add_One_Interp (N, Op_Id, RT);
1504
 
1505
            --  If one operand is a string type or a user-defined array type,
1506
            --  and the other is a literal, result is of the specific type.
1507
 
1508
            elsif
1509
              (Root_Type (LT) = Standard_String
1510
                 or else Scope (LT) /= Standard_Standard)
1511
              and then Etype (R) = Any_String
1512
            then
1513
               Add_One_Interp (N, Op_Id, LT);
1514
 
1515
            elsif
1516
              (Root_Type (RT) = Standard_String
1517
                 or else Scope (RT) /= Standard_Standard)
1518
              and then Etype (L) = Any_String
1519
            then
1520
               Add_One_Interp (N, Op_Id, RT);
1521
 
1522
            elsif not Is_Generic_Type (Etype (Op_Id)) then
1523
               Add_One_Interp (N, Op_Id, Etype (Op_Id));
1524
 
1525
            else
1526
               --  Type and its operations must be visible
1527
 
1528
               Set_Entity (N, Empty);
1529
               Analyze_Concatenation (N);
1530
            end if;
1531
 
1532
         else
1533
            Add_One_Interp (N, Op_Id, Etype (Op_Id));
1534
         end if;
1535
 
1536
      else
1537
         Op_Id := Get_Name_Entity_Id (Name_Op_Concat);
1538
         while Present (Op_Id) loop
1539
            if Ekind (Op_Id) = E_Operator then
1540
 
1541
               --  Do not consider operators declared in dead code, they can
1542
               --  not be part of the resolution.
1543
 
1544
               if Is_Eliminated (Op_Id) then
1545
                  null;
1546
               else
1547
                  Find_Concatenation_Types (L, R, Op_Id, N);
1548
               end if;
1549
 
1550
            else
1551
               Analyze_User_Defined_Binary_Op (N, Op_Id);
1552
            end if;
1553
 
1554
            Op_Id := Homonym (Op_Id);
1555
         end loop;
1556
      end if;
1557
 
1558
      Operator_Check (N);
1559
   end Analyze_Concatenation_Rest;
1560
 
1561
   ------------------------------------
1562
   -- Analyze_Conditional_Expression --
1563
   ------------------------------------
1564
 
1565
   procedure Analyze_Conditional_Expression (N : Node_Id) is
1566
      Condition : constant Node_Id := First (Expressions (N));
1567
      Then_Expr : constant Node_Id := Next (Condition);
1568
      Else_Expr : Node_Id;
1569
 
1570
   begin
1571
      --  Defend against error of missing expressions from previous error
1572
 
1573
      if No (Then_Expr) then
1574
         return;
1575
      end if;
1576
 
1577
      Check_SPARK_Restriction ("conditional expression is not allowed", N);
1578
 
1579
      Else_Expr := Next (Then_Expr);
1580
 
1581
      if Comes_From_Source (N) then
1582
         Check_Compiler_Unit (N);
1583
      end if;
1584
 
1585
      Analyze_Expression (Condition);
1586
      Analyze_Expression (Then_Expr);
1587
 
1588
      if Present (Else_Expr) then
1589
         Analyze_Expression (Else_Expr);
1590
      end if;
1591
 
1592
      --  If then expression not overloaded, then that decides the type
1593
 
1594
      if not Is_Overloaded (Then_Expr) then
1595
         Set_Etype (N, Etype (Then_Expr));
1596
 
1597
      --  Case where then expression is overloaded
1598
 
1599
      else
1600
         declare
1601
            I  : Interp_Index;
1602
            It : Interp;
1603
 
1604
         begin
1605
            Set_Etype (N, Any_Type);
1606
 
1607
            --  Shouldn't the following statement be down in the ELSE of the
1608
            --  following loop? ???
1609
 
1610
            Get_First_Interp (Then_Expr, I, It);
1611
 
1612
            --  if no Else_Expression the conditional must be boolean
1613
 
1614
            if No (Else_Expr) then
1615
               Set_Etype (N, Standard_Boolean);
1616
 
1617
            --  Else_Expression Present. For each possible intepretation of
1618
            --  the Then_Expression, add it only if the Else_Expression has
1619
            --  a compatible type.
1620
 
1621
            else
1622
               while Present (It.Nam) loop
1623
                  if Has_Compatible_Type (Else_Expr, It.Typ) then
1624
                     Add_One_Interp (N, It.Typ, It.Typ);
1625
                  end if;
1626
 
1627
                  Get_Next_Interp (I, It);
1628
               end loop;
1629
            end if;
1630
         end;
1631
      end if;
1632
   end Analyze_Conditional_Expression;
1633
 
1634
   -------------------------
1635
   -- Analyze_Equality_Op --
1636
   -------------------------
1637
 
1638
   procedure Analyze_Equality_Op (N : Node_Id) is
1639
      Loc   : constant Source_Ptr := Sloc (N);
1640
      L     : constant Node_Id := Left_Opnd (N);
1641
      R     : constant Node_Id := Right_Opnd (N);
1642
      Op_Id : Entity_Id;
1643
 
1644
   begin
1645
      Set_Etype (N, Any_Type);
1646
      Candidate_Type := Empty;
1647
 
1648
      Analyze_Expression (L);
1649
      Analyze_Expression (R);
1650
 
1651
      --  If the entity is set, the node is a generic instance with a non-local
1652
      --  reference to the predefined operator or to a user-defined function.
1653
      --  It can also be an inequality that is expanded into the negation of a
1654
      --  call to a user-defined equality operator.
1655
 
1656
      --  For the predefined case, the result is Boolean, regardless of the
1657
      --  type of the  operands. The operands may even be limited, if they are
1658
      --  generic actuals. If they are overloaded, label the left argument with
1659
      --  the common type that must be present, or with the type of the formal
1660
      --  of the user-defined function.
1661
 
1662
      if Present (Entity (N)) then
1663
         Op_Id := Entity (N);
1664
 
1665
         if Ekind (Op_Id) = E_Operator then
1666
            Add_One_Interp (N, Op_Id, Standard_Boolean);
1667
         else
1668
            Add_One_Interp (N, Op_Id, Etype (Op_Id));
1669
         end if;
1670
 
1671
         if Is_Overloaded (L) then
1672
            if Ekind (Op_Id) = E_Operator then
1673
               Set_Etype (L, Intersect_Types (L, R));
1674
            else
1675
               Set_Etype (L, Etype (First_Formal (Op_Id)));
1676
            end if;
1677
         end if;
1678
 
1679
      else
1680
         Op_Id := Get_Name_Entity_Id (Chars (N));
1681
         while Present (Op_Id) loop
1682
            if Ekind (Op_Id) = E_Operator then
1683
               Find_Equality_Types (L, R, Op_Id, N);
1684
            else
1685
               Analyze_User_Defined_Binary_Op (N, Op_Id);
1686
            end if;
1687
 
1688
            Op_Id := Homonym (Op_Id);
1689
         end loop;
1690
      end if;
1691
 
1692
      --  If there was no match, and the operator is inequality, this may
1693
      --  be a case where inequality has not been made explicit, as for
1694
      --  tagged types. Analyze the node as the negation of an equality
1695
      --  operation. This cannot be done earlier, because before analysis
1696
      --  we cannot rule out the presence of an explicit inequality.
1697
 
1698
      if Etype (N) = Any_Type
1699
        and then Nkind (N) = N_Op_Ne
1700
      then
1701
         Op_Id := Get_Name_Entity_Id (Name_Op_Eq);
1702
         while Present (Op_Id) loop
1703
            if Ekind (Op_Id) = E_Operator then
1704
               Find_Equality_Types (L, R, Op_Id, N);
1705
            else
1706
               Analyze_User_Defined_Binary_Op (N, Op_Id);
1707
            end if;
1708
 
1709
            Op_Id := Homonym (Op_Id);
1710
         end loop;
1711
 
1712
         if Etype (N) /= Any_Type then
1713
            Op_Id := Entity (N);
1714
 
1715
            Rewrite (N,
1716
              Make_Op_Not (Loc,
1717
                Right_Opnd =>
1718
                  Make_Op_Eq (Loc,
1719
                    Left_Opnd  => Left_Opnd (N),
1720
                    Right_Opnd => Right_Opnd (N))));
1721
 
1722
            Set_Entity (Right_Opnd (N), Op_Id);
1723
            Analyze (N);
1724
         end if;
1725
      end if;
1726
 
1727
      Operator_Check (N);
1728
   end Analyze_Equality_Op;
1729
 
1730
   ----------------------------------
1731
   -- Analyze_Explicit_Dereference --
1732
   ----------------------------------
1733
 
1734
   procedure Analyze_Explicit_Dereference (N : Node_Id) is
1735
      Loc   : constant Source_Ptr := Sloc (N);
1736
      P     : constant Node_Id := Prefix (N);
1737
      T     : Entity_Id;
1738
      I     : Interp_Index;
1739
      It    : Interp;
1740
      New_N : Node_Id;
1741
 
1742
      function Is_Function_Type return Boolean;
1743
      --  Check whether node may be interpreted as an implicit function call
1744
 
1745
      ----------------------
1746
      -- Is_Function_Type --
1747
      ----------------------
1748
 
1749
      function Is_Function_Type return Boolean is
1750
         I  : Interp_Index;
1751
         It : Interp;
1752
 
1753
      begin
1754
         if not Is_Overloaded (N) then
1755
            return Ekind (Base_Type (Etype (N))) = E_Subprogram_Type
1756
              and then Etype (Base_Type (Etype (N))) /= Standard_Void_Type;
1757
 
1758
         else
1759
            Get_First_Interp (N, I, It);
1760
            while Present (It.Nam) loop
1761
               if Ekind (Base_Type (It.Typ)) /= E_Subprogram_Type
1762
                 or else Etype (Base_Type (It.Typ)) = Standard_Void_Type
1763
               then
1764
                  return False;
1765
               end if;
1766
 
1767
               Get_Next_Interp (I, It);
1768
            end loop;
1769
 
1770
            return True;
1771
         end if;
1772
      end Is_Function_Type;
1773
 
1774
   --  Start of processing for Analyze_Explicit_Dereference
1775
 
1776
   begin
1777
      --  If source node, check SPARK restriction. We guard this with the
1778
      --  source node check, because ???
1779
 
1780
      if Comes_From_Source (N) then
1781
         Check_SPARK_Restriction ("explicit dereference is not allowed", N);
1782
      end if;
1783
 
1784
      --  In formal verification mode, keep track of all reads and writes
1785
      --  through explicit dereferences.
1786
 
1787
      if Alfa_Mode then
1788
         Alfa.Generate_Dereference (N);
1789
      end if;
1790
 
1791
      Analyze (P);
1792
      Set_Etype (N, Any_Type);
1793
 
1794
      --  Test for remote access to subprogram type, and if so return
1795
      --  after rewriting the original tree.
1796
 
1797
      if Remote_AST_E_Dereference (P) then
1798
         return;
1799
      end if;
1800
 
1801
      --  Normal processing for other than remote access to subprogram type
1802
 
1803
      if not Is_Overloaded (P) then
1804
         if Is_Access_Type (Etype (P)) then
1805
 
1806
            --  Set the Etype. We need to go through Is_For_Access_Subtypes to
1807
            --  avoid other problems caused by the Private_Subtype and it is
1808
            --  safe to go to the Base_Type because this is the same as
1809
            --  converting the access value to its Base_Type.
1810
 
1811
            declare
1812
               DT : Entity_Id := Designated_Type (Etype (P));
1813
 
1814
            begin
1815
               if Ekind (DT) = E_Private_Subtype
1816
                 and then Is_For_Access_Subtype (DT)
1817
               then
1818
                  DT := Base_Type (DT);
1819
               end if;
1820
 
1821
               --  An explicit dereference is a legal occurrence of an
1822
               --  incomplete type imported through a limited_with clause,
1823
               --  if the full view is visible.
1824
 
1825
               if From_With_Type (DT)
1826
                 and then not From_With_Type (Scope (DT))
1827
                 and then
1828
                   (Is_Immediately_Visible (Scope (DT))
1829
                     or else
1830
                       (Is_Child_Unit (Scope (DT))
1831
                          and then Is_Visible_Child_Unit (Scope (DT))))
1832
               then
1833
                  Set_Etype (N, Available_View (DT));
1834
 
1835
               else
1836
                  Set_Etype (N, DT);
1837
               end if;
1838
            end;
1839
 
1840
         elsif Etype (P) /= Any_Type then
1841
            Error_Msg_N ("prefix of dereference must be an access type", N);
1842
            return;
1843
         end if;
1844
 
1845
      else
1846
         Get_First_Interp (P, I, It);
1847
         while Present (It.Nam) loop
1848
            T := It.Typ;
1849
 
1850
            if Is_Access_Type (T) then
1851
               Add_One_Interp (N, Designated_Type (T), Designated_Type (T));
1852
            end if;
1853
 
1854
            Get_Next_Interp (I, It);
1855
         end loop;
1856
 
1857
         --  Error if no interpretation of the prefix has an access type
1858
 
1859
         if Etype (N) = Any_Type then
1860
            Error_Msg_N
1861
              ("access type required in prefix of explicit dereference", P);
1862
            Set_Etype (N, Any_Type);
1863
            return;
1864
         end if;
1865
      end if;
1866
 
1867
      if Is_Function_Type
1868
        and then Nkind (Parent (N)) /= N_Indexed_Component
1869
 
1870
        and then (Nkind (Parent (N)) /= N_Function_Call
1871
                   or else N /= Name (Parent (N)))
1872
 
1873
        and then (Nkind (Parent (N)) /= N_Procedure_Call_Statement
1874
                   or else N /= Name (Parent (N)))
1875
 
1876
        and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
1877
        and then (Nkind (Parent (N)) /= N_Attribute_Reference
1878
                    or else
1879
                      (Attribute_Name (Parent (N)) /= Name_Address
1880
                        and then
1881
                       Attribute_Name (Parent (N)) /= Name_Access))
1882
      then
1883
         --  Name is a function call with no actuals, in a context that
1884
         --  requires deproceduring (including as an actual in an enclosing
1885
         --  function or procedure call). There are some pathological cases
1886
         --  where the prefix might include functions that return access to
1887
         --  subprograms and others that return a regular type. Disambiguation
1888
         --  of those has to take place in Resolve.
1889
 
1890
         New_N :=
1891
           Make_Function_Call (Loc,
1892
           Name => Make_Explicit_Dereference (Loc, P),
1893
           Parameter_Associations => New_List);
1894
 
1895
         --  If the prefix is overloaded, remove operations that have formals,
1896
         --  we know that this is a parameterless call.
1897
 
1898
         if Is_Overloaded (P) then
1899
            Get_First_Interp (P, I, It);
1900
            while Present (It.Nam) loop
1901
               T := It.Typ;
1902
 
1903
               if No (First_Formal (Base_Type (Designated_Type (T)))) then
1904
                  Set_Etype (P, T);
1905
               else
1906
                  Remove_Interp (I);
1907
               end if;
1908
 
1909
               Get_Next_Interp (I, It);
1910
            end loop;
1911
         end if;
1912
 
1913
         Rewrite (N, New_N);
1914
         Analyze (N);
1915
 
1916
      elsif not Is_Function_Type
1917
        and then Is_Overloaded (N)
1918
      then
1919
         --  The prefix may include access to subprograms and other access
1920
         --  types. If the context selects the interpretation that is a
1921
         --  function call (not a procedure call) we cannot rewrite the node
1922
         --  yet, but we include the result of the call interpretation.
1923
 
1924
         Get_First_Interp (N, I, It);
1925
         while Present (It.Nam) loop
1926
            if Ekind (Base_Type (It.Typ)) = E_Subprogram_Type
1927
               and then Etype (Base_Type (It.Typ)) /= Standard_Void_Type
1928
               and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
1929
            then
1930
               Add_One_Interp (N, Etype (It.Typ), Etype (It.Typ));
1931
            end if;
1932
 
1933
            Get_Next_Interp (I, It);
1934
         end loop;
1935
      end if;
1936
 
1937
      --  A value of remote access-to-class-wide must not be dereferenced
1938
      --  (RM E.2.2(16)).
1939
 
1940
      Validate_Remote_Access_To_Class_Wide_Type (N);
1941
   end Analyze_Explicit_Dereference;
1942
 
1943
   ------------------------
1944
   -- Analyze_Expression --
1945
   ------------------------
1946
 
1947
   procedure Analyze_Expression (N : Node_Id) is
1948
   begin
1949
      Analyze (N);
1950
      Check_Parameterless_Call (N);
1951
   end Analyze_Expression;
1952
 
1953
   -------------------------------------
1954
   -- Analyze_Expression_With_Actions --
1955
   -------------------------------------
1956
 
1957
   procedure Analyze_Expression_With_Actions (N : Node_Id) is
1958
      A : Node_Id;
1959
 
1960
   begin
1961
      A := First (Actions (N));
1962
      loop
1963
         Analyze (A);
1964
         Next (A);
1965
         exit when No (A);
1966
      end loop;
1967
 
1968
      Analyze_Expression (Expression (N));
1969
      Set_Etype (N, Etype (Expression (N)));
1970
   end Analyze_Expression_With_Actions;
1971
 
1972
   ------------------------------------
1973
   -- Analyze_Indexed_Component_Form --
1974
   ------------------------------------
1975
 
1976
   procedure Analyze_Indexed_Component_Form (N : Node_Id) is
1977
      P     : constant Node_Id := Prefix (N);
1978
      Exprs : constant List_Id := Expressions (N);
1979
      Exp   : Node_Id;
1980
      P_T   : Entity_Id;
1981
      E     : Node_Id;
1982
      U_N   : Entity_Id;
1983
 
1984
      procedure Process_Function_Call;
1985
      --  Prefix in indexed component form is an overloadable entity,
1986
      --  so the node is a function call. Reformat it as such.
1987
 
1988
      procedure Process_Indexed_Component;
1989
      --  Prefix in indexed component form is actually an indexed component.
1990
      --  This routine processes it, knowing that the prefix is already
1991
      --  resolved.
1992
 
1993
      procedure Process_Indexed_Component_Or_Slice;
1994
      --  An indexed component with a single index may designate a slice if
1995
      --  the index is a subtype mark. This routine disambiguates these two
1996
      --  cases by resolving the prefix to see if it is a subtype mark.
1997
 
1998
      procedure Process_Overloaded_Indexed_Component;
1999
      --  If the prefix of an indexed component is overloaded, the proper
2000
      --  interpretation is selected by the index types and the context.
2001
 
2002
      ---------------------------
2003
      -- Process_Function_Call --
2004
      ---------------------------
2005
 
2006
      procedure Process_Function_Call is
2007
         Actual : Node_Id;
2008
 
2009
      begin
2010
         Change_Node (N, N_Function_Call);
2011
         Set_Name (N, P);
2012
         Set_Parameter_Associations (N, Exprs);
2013
 
2014
         --  Analyze actuals prior to analyzing the call itself
2015
 
2016
         Actual := First (Parameter_Associations (N));
2017
         while Present (Actual) loop
2018
            Analyze (Actual);
2019
            Check_Parameterless_Call (Actual);
2020
 
2021
            --  Move to next actual. Note that we use Next, not Next_Actual
2022
            --  here. The reason for this is a bit subtle. If a function call
2023
            --  includes named associations, the parser recognizes the node as
2024
            --  a call, and it is analyzed as such. If all associations are
2025
            --  positional, the parser builds an indexed_component node, and
2026
            --  it is only after analysis of the prefix that the construct
2027
            --  is recognized as a call, in which case Process_Function_Call
2028
            --  rewrites the node and analyzes the actuals. If the list of
2029
            --  actuals is malformed, the parser may leave the node as an
2030
            --  indexed component (despite the presence of named associations).
2031
            --  The iterator Next_Actual is equivalent to Next if the list is
2032
            --  positional, but follows the normalized chain of actuals when
2033
            --  named associations are present. In this case normalization has
2034
            --  not taken place, and actuals remain unanalyzed, which leads to
2035
            --  subsequent crashes or loops if there is an attempt to continue
2036
            --  analysis of the program.
2037
 
2038
            Next (Actual);
2039
         end loop;
2040
 
2041
         Analyze_Call (N);
2042
      end Process_Function_Call;
2043
 
2044
      -------------------------------
2045
      -- Process_Indexed_Component --
2046
      -------------------------------
2047
 
2048
      procedure Process_Indexed_Component is
2049
         Exp        : Node_Id;
2050
         Array_Type : Entity_Id;
2051
         Index      : Node_Id;
2052
         Pent       : Entity_Id := Empty;
2053
 
2054
      begin
2055
         Exp := First (Exprs);
2056
 
2057
         if Is_Overloaded (P) then
2058
            Process_Overloaded_Indexed_Component;
2059
 
2060
         else
2061
            Array_Type := Etype (P);
2062
 
2063
            if Is_Entity_Name (P) then
2064
               Pent := Entity (P);
2065
            elsif Nkind (P) = N_Selected_Component
2066
              and then Is_Entity_Name (Selector_Name (P))
2067
            then
2068
               Pent := Entity (Selector_Name (P));
2069
            end if;
2070
 
2071
            --  Prefix must be appropriate for an array type, taking into
2072
            --  account a possible implicit dereference.
2073
 
2074
            if Is_Access_Type (Array_Type) then
2075
               Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
2076
               Array_Type := Process_Implicit_Dereference_Prefix (Pent, P);
2077
            end if;
2078
 
2079
            if Is_Array_Type (Array_Type) then
2080
               null;
2081
 
2082
            elsif Present (Pent) and then Ekind (Pent) = E_Entry_Family then
2083
               Analyze (Exp);
2084
               Set_Etype (N, Any_Type);
2085
 
2086
               if not Has_Compatible_Type
2087
                 (Exp, Entry_Index_Type (Pent))
2088
               then
2089
                  Error_Msg_N ("invalid index type in entry name", N);
2090
 
2091
               elsif Present (Next (Exp)) then
2092
                  Error_Msg_N ("too many subscripts in entry reference", N);
2093
 
2094
               else
2095
                  Set_Etype (N,  Etype (P));
2096
               end if;
2097
 
2098
               return;
2099
 
2100
            elsif Is_Record_Type (Array_Type)
2101
              and then Remote_AST_I_Dereference (P)
2102
            then
2103
               return;
2104
 
2105
            elsif Try_Container_Indexing (N, P, Exp) then
2106
               return;
2107
 
2108
            elsif Array_Type = Any_Type then
2109
               Set_Etype (N, Any_Type);
2110
 
2111
               --  In most cases the analysis of the prefix will have emitted
2112
               --  an error already, but if the prefix may be interpreted as a
2113
               --  call in prefixed notation, the report is left to the caller.
2114
               --  To prevent cascaded errors, report only if no previous ones.
2115
 
2116
               if Serious_Errors_Detected = 0 then
2117
                  Error_Msg_N ("invalid prefix in indexed component", P);
2118
 
2119
                  if Nkind (P) = N_Expanded_Name then
2120
                     Error_Msg_NE ("\& is not visible", P, Selector_Name (P));
2121
                  end if;
2122
               end if;
2123
 
2124
               return;
2125
 
2126
            --  Here we definitely have a bad indexing
2127
 
2128
            else
2129
               if Nkind (Parent (N)) = N_Requeue_Statement
2130
                 and then Present (Pent) and then Ekind (Pent) = E_Entry
2131
               then
2132
                  Error_Msg_N
2133
                    ("REQUEUE does not permit parameters", First (Exprs));
2134
 
2135
               elsif Is_Entity_Name (P)
2136
                 and then Etype (P) = Standard_Void_Type
2137
               then
2138
                  Error_Msg_NE ("incorrect use of&", P, Entity (P));
2139
 
2140
               else
2141
                  Error_Msg_N ("array type required in indexed component", P);
2142
               end if;
2143
 
2144
               Set_Etype (N, Any_Type);
2145
               return;
2146
            end if;
2147
 
2148
            Index := First_Index (Array_Type);
2149
            while Present (Index) and then Present (Exp) loop
2150
               if not Has_Compatible_Type (Exp, Etype (Index)) then
2151
                  Wrong_Type (Exp, Etype (Index));
2152
                  Set_Etype (N, Any_Type);
2153
                  return;
2154
               end if;
2155
 
2156
               Next_Index (Index);
2157
               Next (Exp);
2158
            end loop;
2159
 
2160
            Set_Etype (N, Component_Type (Array_Type));
2161
            Check_Implicit_Dereference (N, Etype (N));
2162
 
2163
            if Present (Index) then
2164
               Error_Msg_N
2165
                 ("too few subscripts in array reference", First (Exprs));
2166
 
2167
            elsif Present (Exp) then
2168
               Error_Msg_N ("too many subscripts in array reference", Exp);
2169
            end if;
2170
         end if;
2171
      end Process_Indexed_Component;
2172
 
2173
      ----------------------------------------
2174
      -- Process_Indexed_Component_Or_Slice --
2175
      ----------------------------------------
2176
 
2177
      procedure Process_Indexed_Component_Or_Slice is
2178
      begin
2179
         Exp := First (Exprs);
2180
         while Present (Exp) loop
2181
            Analyze_Expression (Exp);
2182
            Next (Exp);
2183
         end loop;
2184
 
2185
         Exp := First (Exprs);
2186
 
2187
         --  If one index is present, and it is a subtype name, then the
2188
         --  node denotes a slice (note that the case of an explicit range
2189
         --  for a slice was already built as an N_Slice node in the first
2190
         --  place, so that case is not handled here).
2191
 
2192
         --  We use a replace rather than a rewrite here because this is one
2193
         --  of the cases in which the tree built by the parser is plain wrong.
2194
 
2195
         if No (Next (Exp))
2196
           and then Is_Entity_Name (Exp)
2197
           and then Is_Type (Entity (Exp))
2198
         then
2199
            Replace (N,
2200
               Make_Slice (Sloc (N),
2201
                 Prefix => P,
2202
                 Discrete_Range => New_Copy (Exp)));
2203
            Analyze (N);
2204
 
2205
         --  Otherwise (more than one index present, or single index is not
2206
         --  a subtype name), then we have the indexed component case.
2207
 
2208
         else
2209
            Process_Indexed_Component;
2210
         end if;
2211
      end Process_Indexed_Component_Or_Slice;
2212
 
2213
      ------------------------------------------
2214
      -- Process_Overloaded_Indexed_Component --
2215
      ------------------------------------------
2216
 
2217
      procedure Process_Overloaded_Indexed_Component is
2218
         Exp   : Node_Id;
2219
         I     : Interp_Index;
2220
         It    : Interp;
2221
         Typ   : Entity_Id;
2222
         Index : Node_Id;
2223
         Found : Boolean;
2224
 
2225
      begin
2226
         Set_Etype (N, Any_Type);
2227
 
2228
         Get_First_Interp (P, I, It);
2229
         while Present (It.Nam) loop
2230
            Typ := It.Typ;
2231
 
2232
            if Is_Access_Type (Typ) then
2233
               Typ := Designated_Type (Typ);
2234
               Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
2235
            end if;
2236
 
2237
            if Is_Array_Type (Typ) then
2238
 
2239
               --  Got a candidate: verify that index types are compatible
2240
 
2241
               Index := First_Index (Typ);
2242
               Found := True;
2243
               Exp := First (Exprs);
2244
               while Present (Index) and then Present (Exp) loop
2245
                  if Has_Compatible_Type (Exp, Etype (Index)) then
2246
                     null;
2247
                  else
2248
                     Found := False;
2249
                     Remove_Interp (I);
2250
                     exit;
2251
                  end if;
2252
 
2253
                  Next_Index (Index);
2254
                  Next (Exp);
2255
               end loop;
2256
 
2257
               if Found and then No (Index) and then No (Exp) then
2258
                  declare
2259
                     CT : constant Entity_Id :=
2260
                            Base_Type (Component_Type (Typ));
2261
                  begin
2262
                     Add_One_Interp (N, CT, CT);
2263
                     Check_Implicit_Dereference (N, CT);
2264
                  end;
2265
               end if;
2266
 
2267
            elsif Try_Container_Indexing (N, P, First (Exprs)) then
2268
               return;
2269
 
2270
            end if;
2271
 
2272
            Get_Next_Interp (I, It);
2273
         end loop;
2274
 
2275
         if Etype (N) = Any_Type then
2276
            Error_Msg_N ("no legal interpretation for indexed component", N);
2277
            Set_Is_Overloaded (N, False);
2278
         end if;
2279
 
2280
         End_Interp_List;
2281
      end Process_Overloaded_Indexed_Component;
2282
 
2283
   --  Start of processing for Analyze_Indexed_Component_Form
2284
 
2285
   begin
2286
      --  Get name of array, function or type
2287
 
2288
      Analyze (P);
2289
 
2290
      if Nkind_In (N, N_Function_Call, N_Procedure_Call_Statement) then
2291
 
2292
         --  If P is an explicit dereference whose prefix is of a
2293
         --  remote access-to-subprogram type, then N has already
2294
         --  been rewritten as a subprogram call and analyzed.
2295
 
2296
         return;
2297
      end if;
2298
 
2299
      pragma Assert (Nkind (N) = N_Indexed_Component);
2300
 
2301
      P_T := Base_Type (Etype (P));
2302
 
2303
      if Is_Entity_Name (P) and then Present (Entity (P)) then
2304
         U_N := Entity (P);
2305
 
2306
         if Is_Type (U_N) then
2307
 
2308
            --  Reformat node as a type conversion
2309
 
2310
            E := Remove_Head (Exprs);
2311
 
2312
            if Present (First (Exprs)) then
2313
               Error_Msg_N
2314
                ("argument of type conversion must be single expression", N);
2315
            end if;
2316
 
2317
            Change_Node (N, N_Type_Conversion);
2318
            Set_Subtype_Mark (N, P);
2319
            Set_Etype (N, U_N);
2320
            Set_Expression (N, E);
2321
 
2322
            --  After changing the node, call for the specific Analysis
2323
            --  routine directly, to avoid a double call to the expander.
2324
 
2325
            Analyze_Type_Conversion (N);
2326
            return;
2327
         end if;
2328
 
2329
         if Is_Overloadable (U_N) then
2330
            Process_Function_Call;
2331
 
2332
         elsif Ekind (Etype (P)) = E_Subprogram_Type
2333
           or else (Is_Access_Type (Etype (P))
2334
                      and then
2335
                        Ekind (Designated_Type (Etype (P))) =
2336
                                                   E_Subprogram_Type)
2337
         then
2338
            --  Call to access_to-subprogram with possible implicit dereference
2339
 
2340
            Process_Function_Call;
2341
 
2342
         elsif Is_Generic_Subprogram (U_N) then
2343
 
2344
            --  A common beginner's (or C++ templates fan) error
2345
 
2346
            Error_Msg_N ("generic subprogram cannot be called", N);
2347
            Set_Etype (N, Any_Type);
2348
            return;
2349
 
2350
         else
2351
            Process_Indexed_Component_Or_Slice;
2352
         end if;
2353
 
2354
      --  If not an entity name, prefix is an expression that may denote
2355
      --  an array or an access-to-subprogram.
2356
 
2357
      else
2358
         if Ekind (P_T) = E_Subprogram_Type
2359
           or else (Is_Access_Type (P_T)
2360
                     and then
2361
                       Ekind (Designated_Type (P_T)) = E_Subprogram_Type)
2362
         then
2363
            Process_Function_Call;
2364
 
2365
         elsif Nkind (P) = N_Selected_Component
2366
           and then Is_Overloadable (Entity (Selector_Name (P)))
2367
         then
2368
            Process_Function_Call;
2369
 
2370
         else
2371
            --  Indexed component, slice, or a call to a member of a family
2372
            --  entry, which will be converted to an entry call later.
2373
 
2374
            Process_Indexed_Component_Or_Slice;
2375
         end if;
2376
      end if;
2377
   end Analyze_Indexed_Component_Form;
2378
 
2379
   ------------------------
2380
   -- Analyze_Logical_Op --
2381
   ------------------------
2382
 
2383
   procedure Analyze_Logical_Op (N : Node_Id) is
2384
      L     : constant Node_Id := Left_Opnd (N);
2385
      R     : constant Node_Id := Right_Opnd (N);
2386
      Op_Id : Entity_Id := Entity (N);
2387
 
2388
   begin
2389
      Set_Etype (N, Any_Type);
2390
      Candidate_Type := Empty;
2391
 
2392
      Analyze_Expression (L);
2393
      Analyze_Expression (R);
2394
 
2395
      if Present (Op_Id) then
2396
 
2397
         if Ekind (Op_Id) = E_Operator then
2398
            Find_Boolean_Types (L, R, Op_Id, N);
2399
         else
2400
            Add_One_Interp (N, Op_Id, Etype (Op_Id));
2401
         end if;
2402
 
2403
      else
2404
         Op_Id := Get_Name_Entity_Id (Chars (N));
2405
         while Present (Op_Id) loop
2406
            if Ekind (Op_Id) = E_Operator then
2407
               Find_Boolean_Types (L, R, Op_Id, N);
2408
            else
2409
               Analyze_User_Defined_Binary_Op (N, Op_Id);
2410
            end if;
2411
 
2412
            Op_Id := Homonym (Op_Id);
2413
         end loop;
2414
      end if;
2415
 
2416
      Operator_Check (N);
2417
   end Analyze_Logical_Op;
2418
 
2419
   ---------------------------
2420
   -- Analyze_Membership_Op --
2421
   ---------------------------
2422
 
2423
   procedure Analyze_Membership_Op (N : Node_Id) is
2424
      Loc   : constant Source_Ptr := Sloc (N);
2425
      L     : constant Node_Id    := Left_Opnd (N);
2426
      R     : constant Node_Id    := Right_Opnd (N);
2427
 
2428
      Index : Interp_Index;
2429
      It    : Interp;
2430
      Found : Boolean := False;
2431
      I_F   : Interp_Index;
2432
      T_F   : Entity_Id;
2433
 
2434
      procedure Try_One_Interp (T1 : Entity_Id);
2435
      --  Routine to try one proposed interpretation. Note that the context
2436
      --  of the operation plays no role in resolving the arguments, so that
2437
      --  if there is more than one interpretation of the operands that is
2438
      --  compatible with a membership test, the operation is ambiguous.
2439
 
2440
      --------------------
2441
      -- Try_One_Interp --
2442
      --------------------
2443
 
2444
      procedure Try_One_Interp (T1 : Entity_Id) is
2445
      begin
2446
         if Has_Compatible_Type (R, T1) then
2447
            if Found
2448
              and then Base_Type (T1) /= Base_Type (T_F)
2449
            then
2450
               It := Disambiguate (L, I_F, Index, Any_Type);
2451
 
2452
               if It = No_Interp then
2453
                  Ambiguous_Operands (N);
2454
                  Set_Etype (L, Any_Type);
2455
                  return;
2456
 
2457
               else
2458
                  T_F := It.Typ;
2459
               end if;
2460
 
2461
            else
2462
               Found := True;
2463
               T_F   := T1;
2464
               I_F   := Index;
2465
            end if;
2466
 
2467
            Set_Etype (L, T_F);
2468
         end if;
2469
      end Try_One_Interp;
2470
 
2471
      procedure Analyze_Set_Membership;
2472
      --  If a set of alternatives is present, analyze each and find the
2473
      --  common type to which they must all resolve.
2474
 
2475
      ----------------------------
2476
      -- Analyze_Set_Membership --
2477
      ----------------------------
2478
 
2479
      procedure Analyze_Set_Membership is
2480
         Alt               : Node_Id;
2481
         Index             : Interp_Index;
2482
         It                : Interp;
2483
         Candidate_Interps : Node_Id;
2484
         Common_Type       : Entity_Id := Empty;
2485
 
2486
      begin
2487
         Analyze (L);
2488
         Candidate_Interps := L;
2489
 
2490
         if not Is_Overloaded (L) then
2491
            Common_Type := Etype (L);
2492
 
2493
            Alt := First (Alternatives (N));
2494
            while Present (Alt) loop
2495
               Analyze (Alt);
2496
 
2497
               if not Has_Compatible_Type (Alt, Common_Type) then
2498
                  Wrong_Type (Alt, Common_Type);
2499
               end if;
2500
 
2501
               Next (Alt);
2502
            end loop;
2503
 
2504
         else
2505
            Alt := First (Alternatives (N));
2506
            while Present (Alt) loop
2507
               Analyze (Alt);
2508
               if not Is_Overloaded (Alt) then
2509
                  Common_Type := Etype (Alt);
2510
 
2511
               else
2512
                  Get_First_Interp (Alt, Index, It);
2513
                  while Present (It.Typ) loop
2514
                     if not
2515
                       Has_Compatible_Type (Candidate_Interps, It.Typ)
2516
                     then
2517
                        Remove_Interp (Index);
2518
                     end if;
2519
 
2520
                     Get_Next_Interp (Index, It);
2521
                  end loop;
2522
 
2523
                  Get_First_Interp (Alt, Index, It);
2524
 
2525
                  if No (It.Typ) then
2526
                     Error_Msg_N ("alternative has no legal type", Alt);
2527
                     return;
2528
                  end if;
2529
 
2530
                  --  If alternative is not overloaded, we have a unique type
2531
                  --  for all of them.
2532
 
2533
                  Set_Etype (Alt, It.Typ);
2534
                  Get_Next_Interp (Index, It);
2535
 
2536
                  if No (It.Typ) then
2537
                     Set_Is_Overloaded (Alt, False);
2538
                     Common_Type := Etype (Alt);
2539
                  end if;
2540
 
2541
                  Candidate_Interps := Alt;
2542
               end if;
2543
 
2544
               Next (Alt);
2545
            end loop;
2546
         end if;
2547
 
2548
         Set_Etype (N, Standard_Boolean);
2549
 
2550
         if Present (Common_Type) then
2551
            Set_Etype (L, Common_Type);
2552
            Set_Is_Overloaded (L, False);
2553
 
2554
         else
2555
            Error_Msg_N ("cannot resolve membership operation", N);
2556
         end if;
2557
      end Analyze_Set_Membership;
2558
 
2559
   --  Start of processing for Analyze_Membership_Op
2560
 
2561
   begin
2562
      Analyze_Expression (L);
2563
 
2564
      if No (R)
2565
        and then Ada_Version >= Ada_2012
2566
      then
2567
         Analyze_Set_Membership;
2568
         return;
2569
      end if;
2570
 
2571
      if Nkind (R) = N_Range
2572
        or else (Nkind (R) = N_Attribute_Reference
2573
                  and then Attribute_Name (R) = Name_Range)
2574
      then
2575
         Analyze (R);
2576
 
2577
         if not Is_Overloaded (L) then
2578
            Try_One_Interp (Etype (L));
2579
 
2580
         else
2581
            Get_First_Interp (L, Index, It);
2582
            while Present (It.Typ) loop
2583
               Try_One_Interp (It.Typ);
2584
               Get_Next_Interp (Index, It);
2585
            end loop;
2586
         end if;
2587
 
2588
      --  If not a range, it can be a subtype mark, or else it is a degenerate
2589
      --  membership test with a singleton value, i.e. a test for equality,
2590
      --  if the types are compatible.
2591
 
2592
      else
2593
         Analyze (R);
2594
 
2595
         if Is_Entity_Name (R)
2596
           and then Is_Type (Entity (R))
2597
         then
2598
            Find_Type (R);
2599
            Check_Fully_Declared (Entity (R), R);
2600
 
2601
         elsif Ada_Version >= Ada_2012
2602
           and then Has_Compatible_Type (R, Etype (L))
2603
         then
2604
            if Nkind (N) = N_In then
2605
               Rewrite (N,
2606
                 Make_Op_Eq (Loc,
2607
                   Left_Opnd  => L,
2608
                   Right_Opnd => R));
2609
            else
2610
               Rewrite (N,
2611
                 Make_Op_Ne (Loc,
2612
                   Left_Opnd  => L,
2613
                   Right_Opnd => R));
2614
            end if;
2615
 
2616
            Analyze (N);
2617
            return;
2618
 
2619
         else
2620
            --  In all versions of the language, if we reach this point there
2621
            --  is a previous error that will be diagnosed below.
2622
 
2623
            Find_Type (R);
2624
         end if;
2625
      end if;
2626
 
2627
      --  Compatibility between expression and subtype mark or range is
2628
      --  checked during resolution. The result of the operation is Boolean
2629
      --  in any case.
2630
 
2631
      Set_Etype (N, Standard_Boolean);
2632
 
2633
      if Comes_From_Source (N)
2634
        and then Present (Right_Opnd (N))
2635
        and then Is_CPP_Class (Etype (Etype (Right_Opnd (N))))
2636
      then
2637
         Error_Msg_N ("membership test not applicable to cpp-class types", N);
2638
      end if;
2639
   end Analyze_Membership_Op;
2640
 
2641
   -----------------
2642
   -- Analyze_Mod --
2643
   -----------------
2644
 
2645
   procedure Analyze_Mod (N : Node_Id) is
2646
   begin
2647
      --  A special warning check, if we have an expression of the form:
2648
      --    expr mod 2 * literal
2649
      --  where literal is 64 or less, then probably what was meant was
2650
      --    expr mod 2 ** literal
2651
      --  so issue an appropriate warning.
2652
 
2653
      if Warn_On_Suspicious_Modulus_Value
2654
        and then Nkind (Right_Opnd (N)) = N_Integer_Literal
2655
        and then Intval (Right_Opnd (N)) = Uint_2
2656
        and then Nkind (Parent (N)) = N_Op_Multiply
2657
        and then Nkind (Right_Opnd (Parent (N))) = N_Integer_Literal
2658
        and then Intval (Right_Opnd (Parent (N))) <= Uint_64
2659
      then
2660
         Error_Msg_N
2661
           ("suspicious MOD value, was '*'* intended'??", Parent (N));
2662
      end if;
2663
 
2664
      --  Remaining processing is same as for other arithmetic operators
2665
 
2666
      Analyze_Arithmetic_Op (N);
2667
   end Analyze_Mod;
2668
 
2669
   ----------------------
2670
   -- Analyze_Negation --
2671
   ----------------------
2672
 
2673
   procedure Analyze_Negation (N : Node_Id) is
2674
      R     : constant Node_Id := Right_Opnd (N);
2675
      Op_Id : Entity_Id := Entity (N);
2676
 
2677
   begin
2678
      Set_Etype (N, Any_Type);
2679
      Candidate_Type := Empty;
2680
 
2681
      Analyze_Expression (R);
2682
 
2683
      if Present (Op_Id) then
2684
         if Ekind (Op_Id) = E_Operator then
2685
            Find_Negation_Types (R, Op_Id, N);
2686
         else
2687
            Add_One_Interp (N, Op_Id, Etype (Op_Id));
2688
         end if;
2689
 
2690
      else
2691
         Op_Id := Get_Name_Entity_Id (Chars (N));
2692
         while Present (Op_Id) loop
2693
            if Ekind (Op_Id) = E_Operator then
2694
               Find_Negation_Types (R, Op_Id, N);
2695
            else
2696
               Analyze_User_Defined_Unary_Op (N, Op_Id);
2697
            end if;
2698
 
2699
            Op_Id := Homonym (Op_Id);
2700
         end loop;
2701
      end if;
2702
 
2703
      Operator_Check (N);
2704
   end Analyze_Negation;
2705
 
2706
   ------------------
2707
   -- Analyze_Null --
2708
   ------------------
2709
 
2710
   procedure Analyze_Null (N : Node_Id) is
2711
   begin
2712
      Check_SPARK_Restriction ("null is not allowed", N);
2713
 
2714
      Set_Etype (N, Any_Access);
2715
   end Analyze_Null;
2716
 
2717
   ----------------------
2718
   -- Analyze_One_Call --
2719
   ----------------------
2720
 
2721
   procedure Analyze_One_Call
2722
      (N          : Node_Id;
2723
       Nam        : Entity_Id;
2724
       Report     : Boolean;
2725
       Success    : out Boolean;
2726
       Skip_First : Boolean := False)
2727
   is
2728
      Actuals : constant List_Id   := Parameter_Associations (N);
2729
      Prev_T  : constant Entity_Id := Etype (N);
2730
 
2731
      Must_Skip  : constant Boolean := Skip_First
2732
                     or else Nkind (Original_Node (N)) = N_Selected_Component
2733
                     or else
2734
                       (Nkind (Original_Node (N)) = N_Indexed_Component
2735
                          and then Nkind (Prefix (Original_Node (N)))
2736
                            = N_Selected_Component);
2737
      --  The first formal must be omitted from the match when trying to find
2738
      --  a primitive operation that is a possible interpretation, and also
2739
      --  after the call has been rewritten, because the corresponding actual
2740
      --  is already known to be compatible, and because this may be an
2741
      --  indexing of a call with default parameters.
2742
 
2743
      Formal      : Entity_Id;
2744
      Actual      : Node_Id;
2745
      Is_Indexed  : Boolean := False;
2746
      Is_Indirect : Boolean := False;
2747
      Subp_Type   : constant Entity_Id := Etype (Nam);
2748
      Norm_OK     : Boolean;
2749
 
2750
      function Operator_Hidden_By (Fun : Entity_Id) return Boolean;
2751
      --  There may be a user-defined operator that hides the current
2752
      --  interpretation. We must check for this independently of the
2753
      --  analysis of the call with the user-defined operation, because
2754
      --  the parameter names may be wrong and yet the hiding takes place.
2755
      --  This fixes a problem with ACATS test B34014O.
2756
      --
2757
      --  When the type Address is a visible integer type, and the DEC
2758
      --  system extension is visible, the predefined operator may be
2759
      --  hidden as well, by one of the address operations in auxdec.
2760
      --  Finally, The abstract operations on address do not hide the
2761
      --  predefined operator (this is the purpose of making them abstract).
2762
 
2763
      procedure Indicate_Name_And_Type;
2764
      --  If candidate interpretation matches, indicate name and type of
2765
      --  result on call node.
2766
 
2767
      ----------------------------
2768
      -- Indicate_Name_And_Type --
2769
      ----------------------------
2770
 
2771
      procedure Indicate_Name_And_Type is
2772
      begin
2773
         Add_One_Interp (N, Nam, Etype (Nam));
2774
         Check_Implicit_Dereference (N, Etype (Nam));
2775
         Success := True;
2776
 
2777
         --  If the prefix of the call is a name, indicate the entity
2778
         --  being called. If it is not a name,  it is an expression that
2779
         --  denotes an access to subprogram or else an entry or family. In
2780
         --  the latter case, the name is a selected component, and the entity
2781
         --  being called is noted on the selector.
2782
 
2783
         if not Is_Type (Nam) then
2784
            if Is_Entity_Name (Name (N)) then
2785
               Set_Entity (Name (N), Nam);
2786
 
2787
            elsif Nkind (Name (N)) = N_Selected_Component then
2788
               Set_Entity (Selector_Name (Name (N)),  Nam);
2789
            end if;
2790
         end if;
2791
 
2792
         if Debug_Flag_E and not Report then
2793
            Write_Str (" Overloaded call ");
2794
            Write_Int (Int (N));
2795
            Write_Str (" compatible with ");
2796
            Write_Int (Int (Nam));
2797
            Write_Eol;
2798
         end if;
2799
      end Indicate_Name_And_Type;
2800
 
2801
      ------------------------
2802
      -- Operator_Hidden_By --
2803
      ------------------------
2804
 
2805
      function Operator_Hidden_By (Fun : Entity_Id) return Boolean is
2806
         Act1  : constant Node_Id   := First_Actual (N);
2807
         Act2  : constant Node_Id   := Next_Actual (Act1);
2808
         Form1 : constant Entity_Id := First_Formal (Fun);
2809
         Form2 : constant Entity_Id := Next_Formal (Form1);
2810
 
2811
      begin
2812
         if Ekind (Fun) /= E_Function
2813
           or else Is_Abstract_Subprogram (Fun)
2814
         then
2815
            return False;
2816
 
2817
         elsif not Has_Compatible_Type (Act1, Etype (Form1)) then
2818
            return False;
2819
 
2820
         elsif Present (Form2) then
2821
            if
2822
              No (Act2) or else not Has_Compatible_Type (Act2, Etype (Form2))
2823
            then
2824
               return False;
2825
            end if;
2826
 
2827
         elsif Present (Act2) then
2828
            return False;
2829
         end if;
2830
 
2831
         --  Now we know that the arity of the operator matches the function,
2832
         --  and the function call is a valid interpretation. The function
2833
         --  hides the operator if it has the right signature, or if one of
2834
         --  its operands is a non-abstract operation on Address when this is
2835
         --  a visible integer type.
2836
 
2837
         return Hides_Op (Fun, Nam)
2838
           or else Is_Descendent_Of_Address (Etype (Form1))
2839
           or else
2840
             (Present (Form2)
2841
               and then Is_Descendent_Of_Address (Etype (Form2)));
2842
      end Operator_Hidden_By;
2843
 
2844
   --  Start of processing for Analyze_One_Call
2845
 
2846
   begin
2847
      Success := False;
2848
 
2849
      --  If the subprogram has no formals or if all the formals have defaults,
2850
      --  and the return type is an array type, the node may denote an indexing
2851
      --  of the result of a parameterless call. In Ada 2005, the subprogram
2852
      --  may have one non-defaulted formal, and the call may have been written
2853
      --  in prefix notation, so that the rebuilt parameter list has more than
2854
      --  one actual.
2855
 
2856
      if not Is_Overloadable (Nam)
2857
        and then Ekind (Nam) /= E_Subprogram_Type
2858
        and then Ekind (Nam) /= E_Entry_Family
2859
      then
2860
         return;
2861
      end if;
2862
 
2863
      --  An indexing requires at least one actual
2864
 
2865
      if not Is_Empty_List (Actuals)
2866
        and then
2867
          (Needs_No_Actuals (Nam)
2868
            or else
2869
              (Needs_One_Actual (Nam)
2870
                 and then Present (Next_Actual (First (Actuals)))))
2871
      then
2872
         if Is_Array_Type (Subp_Type) then
2873
            Is_Indexed := Try_Indexed_Call (N, Nam, Subp_Type, Must_Skip);
2874
 
2875
         elsif Is_Access_Type (Subp_Type)
2876
           and then Is_Array_Type (Designated_Type (Subp_Type))
2877
         then
2878
            Is_Indexed :=
2879
              Try_Indexed_Call
2880
                (N, Nam, Designated_Type (Subp_Type), Must_Skip);
2881
 
2882
         --  The prefix can also be a parameterless function that returns an
2883
         --  access to subprogram, in which case this is an indirect call.
2884
         --  If this succeeds, an explicit dereference is added later on,
2885
         --  in Analyze_Call or Resolve_Call.
2886
 
2887
         elsif Is_Access_Type (Subp_Type)
2888
           and then Ekind (Designated_Type (Subp_Type)) = E_Subprogram_Type
2889
         then
2890
            Is_Indirect := Try_Indirect_Call (N, Nam, Subp_Type);
2891
         end if;
2892
 
2893
      end if;
2894
 
2895
      --  If the call has been transformed into a slice, it is of the form
2896
      --  F (Subtype) where F is parameterless. The node has been rewritten in
2897
      --  Try_Indexed_Call and there is nothing else to do.
2898
 
2899
      if Is_Indexed
2900
        and then  Nkind (N) = N_Slice
2901
      then
2902
         return;
2903
      end if;
2904
 
2905
      Normalize_Actuals
2906
        (N, Nam, (Report and not Is_Indexed and not Is_Indirect), Norm_OK);
2907
 
2908
      if not Norm_OK then
2909
 
2910
         --  If an indirect call is a possible interpretation, indicate
2911
         --  success to the caller.
2912
 
2913
         if Is_Indirect then
2914
            Success := True;
2915
            return;
2916
 
2917
         --  Mismatch in number or names of parameters
2918
 
2919
         elsif Debug_Flag_E then
2920
            Write_Str (" normalization fails in call ");
2921
            Write_Int (Int (N));
2922
            Write_Str (" with subprogram ");
2923
            Write_Int (Int (Nam));
2924
            Write_Eol;
2925
         end if;
2926
 
2927
      --  If the context expects a function call, discard any interpretation
2928
      --  that is a procedure. If the node is not overloaded, leave as is for
2929
      --  better error reporting when type mismatch is found.
2930
 
2931
      elsif Nkind (N) = N_Function_Call
2932
        and then Is_Overloaded (Name (N))
2933
        and then Ekind (Nam) = E_Procedure
2934
      then
2935
         return;
2936
 
2937
      --  Ditto for function calls in a procedure context
2938
 
2939
      elsif Nkind (N) = N_Procedure_Call_Statement
2940
         and then Is_Overloaded (Name (N))
2941
         and then Etype (Nam) /= Standard_Void_Type
2942
      then
2943
         return;
2944
 
2945
      elsif No (Actuals) then
2946
 
2947
         --  If Normalize succeeds, then there are default parameters for
2948
         --  all formals.
2949
 
2950
         Indicate_Name_And_Type;
2951
 
2952
      elsif Ekind (Nam) = E_Operator then
2953
         if Nkind (N) = N_Procedure_Call_Statement then
2954
            return;
2955
         end if;
2956
 
2957
         --  This can occur when the prefix of the call is an operator
2958
         --  name or an expanded name whose selector is an operator name.
2959
 
2960
         Analyze_Operator_Call (N, Nam);
2961
 
2962
         if Etype (N) /= Prev_T then
2963
 
2964
            --  Check that operator is not hidden by a function interpretation
2965
 
2966
            if Is_Overloaded (Name (N)) then
2967
               declare
2968
                  I  : Interp_Index;
2969
                  It : Interp;
2970
 
2971
               begin
2972
                  Get_First_Interp (Name (N), I, It);
2973
                  while Present (It.Nam) loop
2974
                     if Operator_Hidden_By (It.Nam) then
2975
                        Set_Etype (N, Prev_T);
2976
                        return;
2977
                     end if;
2978
 
2979
                     Get_Next_Interp (I, It);
2980
                  end loop;
2981
               end;
2982
            end if;
2983
 
2984
            --  If operator matches formals, record its name on the call.
2985
            --  If the operator is overloaded, Resolve will select the
2986
            --  correct one from the list of interpretations. The call
2987
            --  node itself carries the first candidate.
2988
 
2989
            Set_Entity (Name (N), Nam);
2990
            Success := True;
2991
 
2992
         elsif Report and then Etype (N) = Any_Type then
2993
            Error_Msg_N ("incompatible arguments for operator", N);
2994
         end if;
2995
 
2996
      else
2997
         --  Normalize_Actuals has chained the named associations in the
2998
         --  correct order of the formals.
2999
 
3000
         Actual := First_Actual (N);
3001
         Formal := First_Formal (Nam);
3002
 
3003
         --  If we are analyzing a call rewritten from object notation, skip
3004
         --  first actual, which may be rewritten later as an explicit
3005
         --  dereference.
3006
 
3007
         if Must_Skip then
3008
            Next_Actual (Actual);
3009
            Next_Formal (Formal);
3010
         end if;
3011
 
3012
         while Present (Actual) and then Present (Formal) loop
3013
            if Nkind (Parent (Actual)) /= N_Parameter_Association
3014
              or else Chars (Selector_Name (Parent (Actual))) = Chars (Formal)
3015
            then
3016
               --  The actual can be compatible with the formal, but we must
3017
               --  also check that the context is not an address type that is
3018
               --  visibly an integer type, as is the case in VMS_64. In this
3019
               --  case the use of literals is illegal, except in the body of
3020
               --  descendents of system, where arithmetic operations on
3021
               --  address are of course used.
3022
 
3023
               if Has_Compatible_Type (Actual, Etype (Formal))
3024
                 and then
3025
                  (Etype (Actual) /= Universal_Integer
3026
                    or else not Is_Descendent_Of_Address (Etype (Formal))
3027
                    or else
3028
                      Is_Predefined_File_Name
3029
                        (Unit_File_Name (Get_Source_Unit (N))))
3030
               then
3031
                  Next_Actual (Actual);
3032
                  Next_Formal (Formal);
3033
 
3034
               else
3035
                  if Debug_Flag_E then
3036
                     Write_Str (" type checking fails in call ");
3037
                     Write_Int (Int (N));
3038
                     Write_Str (" with formal ");
3039
                     Write_Int (Int (Formal));
3040
                     Write_Str (" in subprogram ");
3041
                     Write_Int (Int (Nam));
3042
                     Write_Eol;
3043
                  end if;
3044
 
3045
                  if Report and not Is_Indexed and not Is_Indirect then
3046
 
3047
                     --  Ada 2005 (AI-251): Complete the error notification
3048
                     --  to help new Ada 2005 users.
3049
 
3050
                     if Is_Class_Wide_Type (Etype (Formal))
3051
                       and then Is_Interface (Etype (Etype (Formal)))
3052
                       and then not Interface_Present_In_Ancestor
3053
                                      (Typ   => Etype (Actual),
3054
                                       Iface => Etype (Etype (Formal)))
3055
                     then
3056
                        Error_Msg_NE
3057
                          ("(Ada 2005) does not implement interface }",
3058
                           Actual, Etype (Etype (Formal)));
3059
                     end if;
3060
 
3061
                     Wrong_Type (Actual, Etype (Formal));
3062
 
3063
                     if Nkind (Actual) = N_Op_Eq
3064
                       and then Nkind (Left_Opnd (Actual)) = N_Identifier
3065
                     then
3066
                        Formal := First_Formal (Nam);
3067
                        while Present (Formal) loop
3068
                           if Chars (Left_Opnd (Actual)) = Chars (Formal) then
3069
                              Error_Msg_N -- CODEFIX
3070
                                ("possible misspelling of `='>`!", Actual);
3071
                              exit;
3072
                           end if;
3073
 
3074
                           Next_Formal (Formal);
3075
                        end loop;
3076
                     end if;
3077
 
3078
                     if All_Errors_Mode then
3079
                        Error_Msg_Sloc := Sloc (Nam);
3080
 
3081
                        if Etype (Formal) = Any_Type then
3082
                           Error_Msg_N
3083
                             ("there is no legal actual parameter", Actual);
3084
                        end if;
3085
 
3086
                        if Is_Overloadable (Nam)
3087
                          and then Present (Alias (Nam))
3088
                          and then not Comes_From_Source (Nam)
3089
                        then
3090
                           Error_Msg_NE
3091
                             ("\\  =='> in call to inherited operation & #!",
3092
                              Actual, Nam);
3093
 
3094
                        elsif Ekind (Nam) = E_Subprogram_Type then
3095
                           declare
3096
                              Access_To_Subprogram_Typ :
3097
                                constant Entity_Id :=
3098
                                  Defining_Identifier
3099
                                    (Associated_Node_For_Itype (Nam));
3100
                           begin
3101
                              Error_Msg_NE (
3102
                                "\\  =='> in call to dereference of &#!",
3103
                                Actual, Access_To_Subprogram_Typ);
3104
                           end;
3105
 
3106
                        else
3107
                           Error_Msg_NE
3108
                             ("\\  =='> in call to &#!", Actual, Nam);
3109
 
3110
                        end if;
3111
                     end if;
3112
                  end if;
3113
 
3114
                  return;
3115
               end if;
3116
 
3117
            else
3118
               --  Normalize_Actuals has verified that a default value exists
3119
               --  for this formal. Current actual names a subsequent formal.
3120
 
3121
               Next_Formal (Formal);
3122
            end if;
3123
         end loop;
3124
 
3125
         --  On exit, all actuals match
3126
 
3127
         Indicate_Name_And_Type;
3128
      end if;
3129
   end Analyze_One_Call;
3130
 
3131
   ---------------------------
3132
   -- Analyze_Operator_Call --
3133
   ---------------------------
3134
 
3135
   procedure Analyze_Operator_Call (N : Node_Id; Op_Id : Entity_Id) is
3136
      Op_Name : constant Name_Id := Chars (Op_Id);
3137
      Act1    : constant Node_Id := First_Actual (N);
3138
      Act2    : constant Node_Id := Next_Actual (Act1);
3139
 
3140
   begin
3141
      --  Binary operator case
3142
 
3143
      if Present (Act2) then
3144
 
3145
         --  If more than two operands, then not binary operator after all
3146
 
3147
         if Present (Next_Actual (Act2)) then
3148
            return;
3149
         end if;
3150
 
3151
         --  Otherwise action depends on operator
3152
 
3153
         case Op_Name is
3154
            when Name_Op_Add      |
3155
                 Name_Op_Subtract |
3156
                 Name_Op_Multiply |
3157
                 Name_Op_Divide   |
3158
                 Name_Op_Mod      |
3159
                 Name_Op_Rem      |
3160
                 Name_Op_Expon    =>
3161
               Find_Arithmetic_Types (Act1, Act2, Op_Id, N);
3162
 
3163
            when Name_Op_And      |
3164
                 Name_Op_Or       |
3165
                 Name_Op_Xor      =>
3166
               Find_Boolean_Types (Act1, Act2, Op_Id, N);
3167
 
3168
            when Name_Op_Lt       |
3169
                 Name_Op_Le       |
3170
                 Name_Op_Gt       |
3171
                 Name_Op_Ge       =>
3172
               Find_Comparison_Types (Act1, Act2, Op_Id,  N);
3173
 
3174
            when Name_Op_Eq       |
3175
                 Name_Op_Ne       =>
3176
               Find_Equality_Types (Act1, Act2, Op_Id,  N);
3177
 
3178
            when Name_Op_Concat   =>
3179
               Find_Concatenation_Types (Act1, Act2, Op_Id, N);
3180
 
3181
            --  Is this when others, or should it be an abort???
3182
 
3183
            when others           =>
3184
               null;
3185
         end case;
3186
 
3187
      --  Unary operator case
3188
 
3189
      else
3190
         case Op_Name is
3191
            when Name_Op_Subtract |
3192
                 Name_Op_Add      |
3193
                 Name_Op_Abs      =>
3194
               Find_Unary_Types (Act1, Op_Id, N);
3195
 
3196
            when Name_Op_Not      =>
3197
               Find_Negation_Types (Act1, Op_Id, N);
3198
 
3199
            --  Is this when others correct, or should it be an abort???
3200
 
3201
            when others           =>
3202
               null;
3203
         end case;
3204
      end if;
3205
   end Analyze_Operator_Call;
3206
 
3207
   -------------------------------------------
3208
   -- Analyze_Overloaded_Selected_Component --
3209
   -------------------------------------------
3210
 
3211
   procedure Analyze_Overloaded_Selected_Component (N : Node_Id) is
3212
      Nam   : constant Node_Id := Prefix (N);
3213
      Sel   : constant Node_Id := Selector_Name (N);
3214
      Comp  : Entity_Id;
3215
      I     : Interp_Index;
3216
      It    : Interp;
3217
      T     : Entity_Id;
3218
 
3219
   begin
3220
      Set_Etype (Sel, Any_Type);
3221
 
3222
      Get_First_Interp (Nam, I, It);
3223
      while Present (It.Typ) loop
3224
         if Is_Access_Type (It.Typ) then
3225
            T := Designated_Type (It.Typ);
3226
            Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
3227
         else
3228
            T := It.Typ;
3229
         end if;
3230
 
3231
         --  Locate the component. For a private prefix the selector can denote
3232
         --  a discriminant.
3233
 
3234
         if Is_Record_Type (T) or else Is_Private_Type (T) then
3235
 
3236
            --  If the prefix is a class-wide type, the visible components are
3237
            --  those of the base type.
3238
 
3239
            if Is_Class_Wide_Type (T) then
3240
               T := Etype (T);
3241
            end if;
3242
 
3243
            Comp := First_Entity (T);
3244
            while Present (Comp) loop
3245
               if Chars (Comp) = Chars (Sel)
3246
                 and then Is_Visible_Component (Comp)
3247
               then
3248
 
3249
                  --  AI05-105:  if the context is an object renaming with
3250
                  --  an anonymous access type, the expected type of the
3251
                  --  object must be anonymous. This is a name resolution rule.
3252
 
3253
                  if Nkind (Parent (N)) /= N_Object_Renaming_Declaration
3254
                    or else No (Access_Definition (Parent (N)))
3255
                    or else Ekind (Etype (Comp)) = E_Anonymous_Access_Type
3256
                    or else
3257
                      Ekind (Etype (Comp)) = E_Anonymous_Access_Subprogram_Type
3258
                  then
3259
                     Set_Entity (Sel, Comp);
3260
                     Set_Etype (Sel, Etype (Comp));
3261
                     Add_One_Interp (N, Etype (Comp), Etype (Comp));
3262
                     Check_Implicit_Dereference (N, Etype (Comp));
3263
 
3264
                     --  This also specifies a candidate to resolve the name.
3265
                     --  Further overloading will be resolved from context.
3266
                     --  The selector name itself does not carry overloading
3267
                     --  information.
3268
 
3269
                     Set_Etype (Nam, It.Typ);
3270
 
3271
                  else
3272
                     --  Named access type in the context of a renaming
3273
                     --  declaration with an access definition. Remove
3274
                     --  inapplicable candidate.
3275
 
3276
                     Remove_Interp (I);
3277
                  end if;
3278
               end if;
3279
 
3280
               Next_Entity (Comp);
3281
            end loop;
3282
 
3283
         elsif Is_Concurrent_Type (T) then
3284
            Comp := First_Entity (T);
3285
            while Present (Comp)
3286
              and then Comp /= First_Private_Entity (T)
3287
            loop
3288
               if Chars (Comp) = Chars (Sel) then
3289
                  if Is_Overloadable (Comp) then
3290
                     Add_One_Interp (Sel, Comp, Etype (Comp));
3291
                  else
3292
                     Set_Entity_With_Style_Check (Sel, Comp);
3293
                     Generate_Reference (Comp, Sel);
3294
                  end if;
3295
 
3296
                  Set_Etype (Sel, Etype (Comp));
3297
                  Set_Etype (N,   Etype (Comp));
3298
                  Set_Etype (Nam, It.Typ);
3299
 
3300
                  --  For access type case, introduce explicit dereference for
3301
                  --  more uniform treatment of entry calls. Do this only once
3302
                  --  if several interpretations yield an access type.
3303
 
3304
                  if Is_Access_Type (Etype (Nam))
3305
                    and then Nkind (Nam) /= N_Explicit_Dereference
3306
                  then
3307
                     Insert_Explicit_Dereference (Nam);
3308
                     Error_Msg_NW
3309
                       (Warn_On_Dereference, "?implicit dereference", N);
3310
                  end if;
3311
               end if;
3312
 
3313
               Next_Entity (Comp);
3314
            end loop;
3315
 
3316
            Set_Is_Overloaded (N, Is_Overloaded (Sel));
3317
         end if;
3318
 
3319
         Get_Next_Interp (I, It);
3320
      end loop;
3321
 
3322
      if Etype (N) = Any_Type
3323
        and then not Try_Object_Operation (N)
3324
      then
3325
         Error_Msg_NE ("undefined selector& for overloaded prefix", N, Sel);
3326
         Set_Entity (Sel, Any_Id);
3327
         Set_Etype  (Sel, Any_Type);
3328
      end if;
3329
   end Analyze_Overloaded_Selected_Component;
3330
 
3331
   ----------------------------------
3332
   -- Analyze_Qualified_Expression --
3333
   ----------------------------------
3334
 
3335
   procedure Analyze_Qualified_Expression (N : Node_Id) is
3336
      Mark : constant Entity_Id := Subtype_Mark (N);
3337
      Expr : constant Node_Id   := Expression (N);
3338
      I    : Interp_Index;
3339
      It   : Interp;
3340
      T    : Entity_Id;
3341
 
3342
   begin
3343
      Analyze_Expression (Expr);
3344
 
3345
      Set_Etype (N, Any_Type);
3346
      Find_Type (Mark);
3347
      T := Entity (Mark);
3348
      Set_Etype (N, T);
3349
 
3350
      if T = Any_Type then
3351
         return;
3352
      end if;
3353
 
3354
      Check_Fully_Declared (T, N);
3355
 
3356
      --  If expected type is class-wide, check for exact match before
3357
      --  expansion, because if the expression is a dispatching call it
3358
      --  may be rewritten as explicit dereference with class-wide result.
3359
      --  If expression is overloaded, retain only interpretations that
3360
      --  will yield exact matches.
3361
 
3362
      if Is_Class_Wide_Type (T) then
3363
         if not Is_Overloaded (Expr) then
3364
            if  Base_Type (Etype (Expr)) /= Base_Type (T) then
3365
               if Nkind (Expr) = N_Aggregate then
3366
                  Error_Msg_N ("type of aggregate cannot be class-wide", Expr);
3367
               else
3368
                  Wrong_Type (Expr, T);
3369
               end if;
3370
            end if;
3371
 
3372
         else
3373
            Get_First_Interp (Expr, I, It);
3374
 
3375
            while Present (It.Nam) loop
3376
               if Base_Type (It.Typ) /= Base_Type (T) then
3377
                  Remove_Interp (I);
3378
               end if;
3379
 
3380
               Get_Next_Interp (I, It);
3381
            end loop;
3382
         end if;
3383
      end if;
3384
 
3385
      Set_Etype  (N, T);
3386
   end Analyze_Qualified_Expression;
3387
 
3388
   -----------------------------------
3389
   -- Analyze_Quantified_Expression --
3390
   -----------------------------------
3391
 
3392
   procedure Analyze_Quantified_Expression (N : Node_Id) is
3393
      Loc : constant Source_Ptr := Sloc (N);
3394
      Ent : constant Entity_Id :=
3395
              New_Internal_Entity
3396
                (E_Loop, Current_Scope, Sloc (N), 'L');
3397
 
3398
      Iterator : Node_Id;
3399
 
3400
   begin
3401
      Set_Etype  (Ent,  Standard_Void_Type);
3402
      Set_Scope  (Ent, Current_Scope);
3403
      Set_Parent (Ent, N);
3404
 
3405
      Check_SPARK_Restriction ("quantified expression is not allowed", N);
3406
 
3407
      --  If expansion is enabled (and not in Alfa mode), the condition is
3408
      --  analyzed after rewritten as a loop. So we only need to set the type.
3409
 
3410
      if Operating_Mode /= Check_Semantics
3411
        and then not Alfa_Mode
3412
      then
3413
         Set_Etype (N, Standard_Boolean);
3414
         return;
3415
      end if;
3416
 
3417
      if Present (Loop_Parameter_Specification (N)) then
3418
         Iterator :=
3419
           Make_Iteration_Scheme (Loc,
3420
             Loop_Parameter_Specification =>
3421
               Loop_Parameter_Specification (N));
3422
      else
3423
         Iterator :=
3424
           Make_Iteration_Scheme (Loc,
3425
              Iterator_Specification =>
3426
                Iterator_Specification (N));
3427
      end if;
3428
 
3429
      Push_Scope (Ent);
3430
      Set_Parent (Iterator, N);
3431
      Analyze_Iteration_Scheme (Iterator);
3432
 
3433
      --  The loop specification may have been converted into an iterator
3434
      --  specification during its analysis. Update the quantified node
3435
      --  accordingly.
3436
 
3437
      if Present (Iterator_Specification (Iterator)) then
3438
         Set_Iterator_Specification
3439
           (N, Iterator_Specification (Iterator));
3440
         Set_Loop_Parameter_Specification (N, Empty);
3441
      end if;
3442
 
3443
      Analyze (Condition (N));
3444
      End_Scope;
3445
      Set_Etype (N, Standard_Boolean);
3446
   end Analyze_Quantified_Expression;
3447
 
3448
   -------------------
3449
   -- Analyze_Range --
3450
   -------------------
3451
 
3452
   procedure Analyze_Range (N : Node_Id) is
3453
      L        : constant Node_Id := Low_Bound (N);
3454
      H        : constant Node_Id := High_Bound (N);
3455
      I1, I2   : Interp_Index;
3456
      It1, It2 : Interp;
3457
 
3458
      procedure Check_Common_Type (T1, T2 : Entity_Id);
3459
      --  Verify the compatibility of two types,  and choose the
3460
      --  non universal one if the other is universal.
3461
 
3462
      procedure Check_High_Bound (T : Entity_Id);
3463
      --  Test one interpretation of the low bound against all those
3464
      --  of the high bound.
3465
 
3466
      procedure Check_Universal_Expression (N : Node_Id);
3467
      --  In Ada 83, reject bounds of a universal range that are not literals
3468
      --  or entity names.
3469
 
3470
      -----------------------
3471
      -- Check_Common_Type --
3472
      -----------------------
3473
 
3474
      procedure Check_Common_Type (T1, T2 : Entity_Id) is
3475
      begin
3476
         if Covers (T1 => T1, T2 => T2)
3477
              or else
3478
            Covers (T1 => T2, T2 => T1)
3479
         then
3480
            if T1 = Universal_Integer
3481
              or else T1 = Universal_Real
3482
              or else T1 = Any_Character
3483
            then
3484
               Add_One_Interp (N, Base_Type (T2), Base_Type (T2));
3485
 
3486
            elsif T1 = T2 then
3487
               Add_One_Interp (N, T1, T1);
3488
 
3489
            else
3490
               Add_One_Interp (N, Base_Type (T1), Base_Type (T1));
3491
            end if;
3492
         end if;
3493
      end Check_Common_Type;
3494
 
3495
      ----------------------
3496
      -- Check_High_Bound --
3497
      ----------------------
3498
 
3499
      procedure Check_High_Bound (T : Entity_Id) is
3500
      begin
3501
         if not Is_Overloaded (H) then
3502
            Check_Common_Type (T, Etype (H));
3503
         else
3504
            Get_First_Interp (H, I2, It2);
3505
            while Present (It2.Typ) loop
3506
               Check_Common_Type (T, It2.Typ);
3507
               Get_Next_Interp (I2, It2);
3508
            end loop;
3509
         end if;
3510
      end Check_High_Bound;
3511
 
3512
      -----------------------------
3513
      -- Is_Universal_Expression --
3514
      -----------------------------
3515
 
3516
      procedure Check_Universal_Expression (N : Node_Id) is
3517
      begin
3518
         if Etype (N) = Universal_Integer
3519
           and then Nkind (N) /= N_Integer_Literal
3520
           and then not Is_Entity_Name (N)
3521
           and then Nkind (N) /= N_Attribute_Reference
3522
         then
3523
            Error_Msg_N ("illegal bound in discrete range", N);
3524
         end if;
3525
      end Check_Universal_Expression;
3526
 
3527
   --  Start of processing for Analyze_Range
3528
 
3529
   begin
3530
      Set_Etype (N, Any_Type);
3531
      Analyze_Expression (L);
3532
      Analyze_Expression (H);
3533
 
3534
      if Etype (L) = Any_Type or else Etype (H) = Any_Type then
3535
         return;
3536
 
3537
      else
3538
         if not Is_Overloaded (L) then
3539
            Check_High_Bound (Etype (L));
3540
         else
3541
            Get_First_Interp (L, I1, It1);
3542
            while Present (It1.Typ) loop
3543
               Check_High_Bound (It1.Typ);
3544
               Get_Next_Interp (I1, It1);
3545
            end loop;
3546
         end if;
3547
 
3548
         --  If result is Any_Type, then we did not find a compatible pair
3549
 
3550
         if Etype (N) = Any_Type then
3551
            Error_Msg_N ("incompatible types in range ", N);
3552
         end if;
3553
      end if;
3554
 
3555
      if Ada_Version = Ada_83
3556
        and then
3557
          (Nkind (Parent (N)) = N_Loop_Parameter_Specification
3558
             or else Nkind (Parent (N)) = N_Constrained_Array_Definition)
3559
      then
3560
         Check_Universal_Expression (L);
3561
         Check_Universal_Expression (H);
3562
      end if;
3563
   end Analyze_Range;
3564
 
3565
   -----------------------
3566
   -- Analyze_Reference --
3567
   -----------------------
3568
 
3569
   procedure Analyze_Reference (N : Node_Id) is
3570
      P        : constant Node_Id := Prefix (N);
3571
      E        : Entity_Id;
3572
      T        : Entity_Id;
3573
      Acc_Type : Entity_Id;
3574
 
3575
   begin
3576
      Analyze (P);
3577
 
3578
      --  An interesting error check, if we take the 'Reference of an object
3579
      --  for which a pragma Atomic or Volatile has been given, and the type
3580
      --  of the object is not Atomic or Volatile, then we are in trouble. The
3581
      --  problem is that no trace of the atomic/volatile status will remain
3582
      --  for the backend to respect when it deals with the resulting pointer,
3583
      --  since the pointer type will not be marked atomic (it is a pointer to
3584
      --  the base type of the object).
3585
 
3586
      --  It is not clear if that can ever occur, but in case it does, we will
3587
      --  generate an error message. Not clear if this message can ever be
3588
      --  generated, and pretty clear that it represents a bug if it is, still
3589
      --  seems worth checking, except in CodePeer mode where we do not really
3590
      --  care and don't want to bother the user.
3591
 
3592
      T := Etype (P);
3593
 
3594
      if Is_Entity_Name (P)
3595
        and then Is_Object_Reference (P)
3596
        and then not CodePeer_Mode
3597
      then
3598
         E := Entity (P);
3599
         T := Etype (P);
3600
 
3601
         if (Has_Atomic_Components   (E)
3602
               and then not Has_Atomic_Components   (T))
3603
           or else
3604
            (Has_Volatile_Components (E)
3605
               and then not Has_Volatile_Components (T))
3606
           or else (Is_Atomic   (E) and then not Is_Atomic   (T))
3607
           or else (Is_Volatile (E) and then not Is_Volatile (T))
3608
         then
3609
            Error_Msg_N ("cannot take reference to Atomic/Volatile object", N);
3610
         end if;
3611
      end if;
3612
 
3613
      --  Carry on with normal processing
3614
 
3615
      Acc_Type := Create_Itype (E_Allocator_Type, N);
3616
      Set_Etype (Acc_Type,  Acc_Type);
3617
      Set_Directly_Designated_Type (Acc_Type, Etype (P));
3618
      Set_Etype (N, Acc_Type);
3619
   end Analyze_Reference;
3620
 
3621
   --------------------------------
3622
   -- Analyze_Selected_Component --
3623
   --------------------------------
3624
 
3625
   --  Prefix is a record type or a task or protected type. In the latter case,
3626
   --  the selector must denote a visible entry.
3627
 
3628
   procedure Analyze_Selected_Component (N : Node_Id) is
3629
      Name          : constant Node_Id := Prefix (N);
3630
      Sel           : constant Node_Id := Selector_Name (N);
3631
      Act_Decl      : Node_Id;
3632
      Comp          : Entity_Id;
3633
      Has_Candidate : Boolean := False;
3634
      In_Scope      : Boolean;
3635
      Parent_N      : Node_Id;
3636
      Pent          : Entity_Id := Empty;
3637
      Prefix_Type   : Entity_Id;
3638
 
3639
      Type_To_Use : Entity_Id;
3640
      --  In most cases this is the Prefix_Type, but if the Prefix_Type is
3641
      --  a class-wide type, we use its root type, whose components are
3642
      --  present in the class-wide type.
3643
 
3644
      Is_Single_Concurrent_Object : Boolean;
3645
      --  Set True if the prefix is a single task or a single protected object
3646
 
3647
      procedure Find_Component_In_Instance (Rec : Entity_Id);
3648
      --  In an instance, a component of a private extension may not be visible
3649
      --  while it was visible in the generic. Search candidate scope for a
3650
      --  component with the proper identifier. This is only done if all other
3651
      --  searches have failed. When the match is found (it always will be),
3652
      --  the Etype of both N and Sel are set from this component, and the
3653
      --  entity of Sel is set to reference this component.
3654
 
3655
      function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean;
3656
      --  It is known that the parent of N denotes a subprogram call. Comp
3657
      --  is an overloadable component of the concurrent type of the prefix.
3658
      --  Determine whether all formals of the parent of N and Comp are mode
3659
      --  conformant. If the parent node is not analyzed yet it may be an
3660
      --  indexed component rather than a function call.
3661
 
3662
      --------------------------------
3663
      -- Find_Component_In_Instance --
3664
      --------------------------------
3665
 
3666
      procedure Find_Component_In_Instance (Rec : Entity_Id) is
3667
         Comp : Entity_Id;
3668
 
3669
      begin
3670
         Comp := First_Component (Rec);
3671
         while Present (Comp) loop
3672
            if Chars (Comp) = Chars (Sel) then
3673
               Set_Entity_With_Style_Check (Sel, Comp);
3674
               Set_Etype (Sel, Etype (Comp));
3675
               Set_Etype (N,   Etype (Comp));
3676
               return;
3677
            end if;
3678
 
3679
            Next_Component (Comp);
3680
         end loop;
3681
 
3682
         --  This must succeed because code was legal in the generic
3683
 
3684
         raise Program_Error;
3685
      end Find_Component_In_Instance;
3686
 
3687
      ------------------------------
3688
      -- Has_Mode_Conformant_Spec --
3689
      ------------------------------
3690
 
3691
      function Has_Mode_Conformant_Spec (Comp : Entity_Id) return Boolean is
3692
         Comp_Param : Entity_Id;
3693
         Param      : Node_Id;
3694
         Param_Typ  : Entity_Id;
3695
 
3696
      begin
3697
         Comp_Param := First_Formal (Comp);
3698
 
3699
         if Nkind (Parent (N)) = N_Indexed_Component then
3700
            Param := First (Expressions (Parent (N)));
3701
         else
3702
            Param := First (Parameter_Associations (Parent (N)));
3703
         end if;
3704
 
3705
         while Present (Comp_Param)
3706
           and then Present (Param)
3707
         loop
3708
            Param_Typ := Find_Parameter_Type (Param);
3709
 
3710
            if Present (Param_Typ)
3711
              and then
3712
                not Conforming_Types
3713
                     (Etype (Comp_Param), Param_Typ, Mode_Conformant)
3714
            then
3715
               return False;
3716
            end if;
3717
 
3718
            Next_Formal (Comp_Param);
3719
            Next (Param);
3720
         end loop;
3721
 
3722
         --  One of the specs has additional formals
3723
 
3724
         if Present (Comp_Param) or else Present (Param) then
3725
            return False;
3726
         end if;
3727
 
3728
         return True;
3729
      end Has_Mode_Conformant_Spec;
3730
 
3731
   --  Start of processing for Analyze_Selected_Component
3732
 
3733
   begin
3734
      Set_Etype (N, Any_Type);
3735
 
3736
      if Is_Overloaded (Name) then
3737
         Analyze_Overloaded_Selected_Component (N);
3738
         return;
3739
 
3740
      elsif Etype (Name) = Any_Type then
3741
         Set_Entity (Sel, Any_Id);
3742
         Set_Etype (Sel, Any_Type);
3743
         return;
3744
 
3745
      else
3746
         Prefix_Type := Etype (Name);
3747
      end if;
3748
 
3749
      if Is_Access_Type (Prefix_Type) then
3750
 
3751
         --  A RACW object can never be used as prefix of a selected component
3752
         --  since that means it is dereferenced without being a controlling
3753
         --  operand of a dispatching operation (RM E.2.2(16/1)). Before
3754
         --  reporting an error, we must check whether this is actually a
3755
         --  dispatching call in prefix form.
3756
 
3757
         if Is_Remote_Access_To_Class_Wide_Type (Prefix_Type)
3758
           and then Comes_From_Source (N)
3759
         then
3760
            if Try_Object_Operation (N) then
3761
               return;
3762
            else
3763
               Error_Msg_N
3764
                 ("invalid dereference of a remote access-to-class-wide value",
3765
                  N);
3766
            end if;
3767
 
3768
         --  Normal case of selected component applied to access type
3769
 
3770
         else
3771
            Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
3772
 
3773
            if Is_Entity_Name (Name) then
3774
               Pent := Entity (Name);
3775
            elsif Nkind (Name) = N_Selected_Component
3776
              and then Is_Entity_Name (Selector_Name (Name))
3777
            then
3778
               Pent := Entity (Selector_Name (Name));
3779
            end if;
3780
 
3781
            Prefix_Type := Process_Implicit_Dereference_Prefix (Pent, Name);
3782
         end if;
3783
 
3784
      --  If we have an explicit dereference of a remote access-to-class-wide
3785
      --  value, then issue an error (see RM-E.2.2(16/1)). However we first
3786
      --  have to check for the case of a prefix that is a controlling operand
3787
      --  of a prefixed dispatching call, as the dereference is legal in that
3788
      --  case. Normally this condition is checked in Validate_Remote_Access_
3789
      --  To_Class_Wide_Type, but we have to defer the checking for selected
3790
      --  component prefixes because of the prefixed dispatching call case.
3791
      --  Note that implicit dereferences are checked for this just above.
3792
 
3793
      elsif Nkind (Name) = N_Explicit_Dereference
3794
        and then Is_Remote_Access_To_Class_Wide_Type (Etype (Prefix (Name)))
3795
        and then Comes_From_Source (N)
3796
      then
3797
         if Try_Object_Operation (N) then
3798
            return;
3799
         else
3800
            Error_Msg_N
3801
              ("invalid dereference of a remote access-to-class-wide value",
3802
               N);
3803
         end if;
3804
      end if;
3805
 
3806
      --  (Ada 2005): if the prefix is the limited view of a type, and
3807
      --  the context already includes the full view, use the full view
3808
      --  in what follows, either to retrieve a component of to find
3809
      --  a primitive operation. If the prefix is an explicit dereference,
3810
      --  set the type of the prefix to reflect this transformation.
3811
      --  If the non-limited view is itself an incomplete type, get the
3812
      --  full view if available.
3813
 
3814
      if Is_Incomplete_Type (Prefix_Type)
3815
        and then From_With_Type (Prefix_Type)
3816
        and then Present (Non_Limited_View (Prefix_Type))
3817
      then
3818
         Prefix_Type := Get_Full_View (Non_Limited_View (Prefix_Type));
3819
 
3820
         if Nkind (N) = N_Explicit_Dereference then
3821
            Set_Etype (Prefix (N), Prefix_Type);
3822
         end if;
3823
 
3824
      elsif Ekind (Prefix_Type) = E_Class_Wide_Type
3825
        and then From_With_Type (Prefix_Type)
3826
        and then Present (Non_Limited_View (Etype (Prefix_Type)))
3827
      then
3828
         Prefix_Type :=
3829
           Class_Wide_Type (Non_Limited_View (Etype (Prefix_Type)));
3830
 
3831
         if Nkind (N) = N_Explicit_Dereference then
3832
            Set_Etype (Prefix (N), Prefix_Type);
3833
         end if;
3834
      end if;
3835
 
3836
      if Ekind (Prefix_Type) = E_Private_Subtype then
3837
         Prefix_Type := Base_Type (Prefix_Type);
3838
      end if;
3839
 
3840
      Type_To_Use := Prefix_Type;
3841
 
3842
      --  For class-wide types, use the entity list of the root type. This
3843
      --  indirection is specially important for private extensions because
3844
      --  only the root type get switched (not the class-wide type).
3845
 
3846
      if Is_Class_Wide_Type (Prefix_Type) then
3847
         Type_To_Use := Root_Type (Prefix_Type);
3848
      end if;
3849
 
3850
      --  If the prefix is a single concurrent object, use its name in error
3851
      --  messages, rather than that of its anonymous type.
3852
 
3853
      Is_Single_Concurrent_Object :=
3854
        Is_Concurrent_Type (Prefix_Type)
3855
          and then Is_Internal_Name (Chars (Prefix_Type))
3856
          and then not Is_Derived_Type (Prefix_Type)
3857
          and then Is_Entity_Name (Name);
3858
 
3859
      Comp := First_Entity (Type_To_Use);
3860
 
3861
      --  If the selector has an original discriminant, the node appears in
3862
      --  an instance. Replace the discriminant with the corresponding one
3863
      --  in the current discriminated type. For nested generics, this must
3864
      --  be done transitively, so note the new original discriminant.
3865
 
3866
      if Nkind (Sel) = N_Identifier
3867
        and then In_Instance
3868
        and then Present (Original_Discriminant (Sel))
3869
      then
3870
         Comp := Find_Corresponding_Discriminant (Sel, Prefix_Type);
3871
 
3872
         --  Mark entity before rewriting, for completeness and because
3873
         --  subsequent semantic checks might examine the original node.
3874
 
3875
         Set_Entity (Sel, Comp);
3876
         Rewrite (Selector_Name (N),
3877
           New_Occurrence_Of (Comp, Sloc (N)));
3878
         Set_Original_Discriminant (Selector_Name (N), Comp);
3879
         Set_Etype (N, Etype (Comp));
3880
         Check_Implicit_Dereference (N, Etype (Comp));
3881
 
3882
         if Is_Access_Type (Etype (Name)) then
3883
            Insert_Explicit_Dereference (Name);
3884
            Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
3885
         end if;
3886
 
3887
      elsif Is_Record_Type (Prefix_Type) then
3888
 
3889
         --  Find component with given name
3890
         --  In an instance, if the node is known as a prefixed call, do
3891
         --  not examine components whose visibility may be accidental.
3892
 
3893
         while Present (Comp) and then not Is_Prefixed_Call (N) loop
3894
            if Chars (Comp) = Chars (Sel)
3895
              and then Is_Visible_Component (Comp)
3896
            then
3897
               Set_Entity_With_Style_Check (Sel, Comp);
3898
               Set_Etype (Sel, Etype (Comp));
3899
 
3900
               if Ekind (Comp) = E_Discriminant then
3901
                  if Is_Unchecked_Union (Base_Type (Prefix_Type)) then
3902
                     Error_Msg_N
3903
                       ("cannot reference discriminant of Unchecked_Union",
3904
                        Sel);
3905
                  end if;
3906
 
3907
                  if Is_Generic_Type (Prefix_Type)
3908
                       or else
3909
                     Is_Generic_Type (Root_Type (Prefix_Type))
3910
                  then
3911
                     Set_Original_Discriminant (Sel, Comp);
3912
                  end if;
3913
               end if;
3914
 
3915
               --  Resolve the prefix early otherwise it is not possible to
3916
               --  build the actual subtype of the component: it may need
3917
               --  to duplicate this prefix and duplication is only allowed
3918
               --  on fully resolved expressions.
3919
 
3920
               Resolve (Name);
3921
 
3922
               --  Ada 2005 (AI-50217): Check wrong use of incomplete types or
3923
               --  subtypes in a package specification.
3924
               --  Example:
3925
 
3926
               --    limited with Pkg;
3927
               --    package Pkg is
3928
               --       type Acc_Inc is access Pkg.T;
3929
               --       X : Acc_Inc;
3930
               --       N : Natural := X.all.Comp;  --  ERROR, limited view
3931
               --    end Pkg;                       --  Comp is not visible
3932
 
3933
               if Nkind (Name) = N_Explicit_Dereference
3934
                 and then From_With_Type (Etype (Prefix (Name)))
3935
                 and then not Is_Potentially_Use_Visible (Etype (Name))
3936
                 and then Nkind (Parent (Cunit_Entity (Current_Sem_Unit))) =
3937
                            N_Package_Specification
3938
               then
3939
                  Error_Msg_NE
3940
                    ("premature usage of incomplete}", Prefix (Name),
3941
                     Etype (Prefix (Name)));
3942
               end if;
3943
 
3944
               --  We never need an actual subtype for the case of a selection
3945
               --  for a indexed component of a non-packed array, since in
3946
               --  this case gigi generates all the checks and can find the
3947
               --  necessary bounds information.
3948
 
3949
               --  We also do not need an actual subtype for the case of a
3950
               --  first, last, length, or range attribute applied to a
3951
               --  non-packed array, since gigi can again get the bounds in
3952
               --  these cases (gigi cannot handle the packed case, since it
3953
               --  has the bounds of the packed array type, not the original
3954
               --  bounds of the type). However, if the prefix is itself a
3955
               --  selected component, as in a.b.c (i), gigi may regard a.b.c
3956
               --  as a dynamic-sized temporary, so we do generate an actual
3957
               --  subtype for this case.
3958
 
3959
               Parent_N := Parent (N);
3960
 
3961
               if not Is_Packed (Etype (Comp))
3962
                 and then
3963
                   ((Nkind (Parent_N) = N_Indexed_Component
3964
                       and then Nkind (Name) /= N_Selected_Component)
3965
                     or else
3966
                      (Nkind (Parent_N) = N_Attribute_Reference
3967
                         and then (Attribute_Name (Parent_N) = Name_First
3968
                                     or else
3969
                                   Attribute_Name (Parent_N) = Name_Last
3970
                                     or else
3971
                                   Attribute_Name (Parent_N) = Name_Length
3972
                                     or else
3973
                                   Attribute_Name (Parent_N) = Name_Range)))
3974
               then
3975
                  Set_Etype (N, Etype (Comp));
3976
 
3977
               --  If full analysis is not enabled, we do not generate an
3978
               --  actual subtype, because in the absence of expansion
3979
               --  reference to a formal of a protected type, for example,
3980
               --  will not be properly transformed, and will lead to
3981
               --  out-of-scope references in gigi.
3982
 
3983
               --  In all other cases, we currently build an actual subtype.
3984
               --  It seems likely that many of these cases can be avoided,
3985
               --  but right now, the front end makes direct references to the
3986
               --  bounds (e.g. in generating a length check), and if we do
3987
               --  not make an actual subtype, we end up getting a direct
3988
               --  reference to a discriminant, which will not do.
3989
 
3990
               elsif Full_Analysis then
3991
                  Act_Decl :=
3992
                    Build_Actual_Subtype_Of_Component (Etype (Comp), N);
3993
                  Insert_Action (N, Act_Decl);
3994
 
3995
                  if No (Act_Decl) then
3996
                     Set_Etype (N, Etype (Comp));
3997
 
3998
                  else
3999
                     --  Component type depends on discriminants. Enter the
4000
                     --  main attributes of the subtype.
4001
 
4002
                     declare
4003
                        Subt : constant Entity_Id :=
4004
                                 Defining_Identifier (Act_Decl);
4005
 
4006
                     begin
4007
                        Set_Etype (Subt, Base_Type (Etype (Comp)));
4008
                        Set_Ekind (Subt, Ekind (Etype (Comp)));
4009
                        Set_Etype (N, Subt);
4010
                     end;
4011
                  end if;
4012
 
4013
               --  If Full_Analysis not enabled, just set the Etype
4014
 
4015
               else
4016
                  Set_Etype (N, Etype (Comp));
4017
               end if;
4018
 
4019
               Check_Implicit_Dereference (N, Etype (N));
4020
               return;
4021
            end if;
4022
 
4023
            --  If the prefix is a private extension, check only the visible
4024
            --  components of the partial view. This must include the tag,
4025
            --  which can appear in expanded code in a tag check.
4026
 
4027
            if Ekind (Type_To_Use) = E_Record_Type_With_Private
4028
              and then Chars (Selector_Name (N)) /= Name_uTag
4029
            then
4030
               exit when Comp = Last_Entity (Type_To_Use);
4031
            end if;
4032
 
4033
            Next_Entity (Comp);
4034
         end loop;
4035
 
4036
         --  Ada 2005 (AI-252): The selected component can be interpreted as
4037
         --  a prefixed view of a subprogram. Depending on the context, this is
4038
         --  either a name that can appear in a renaming declaration, or part
4039
         --  of an enclosing call given in prefix form.
4040
 
4041
         --  Ada 2005 (AI05-0030): In the case of dispatching requeue, the
4042
         --  selected component should resolve to a name.
4043
 
4044
         if Ada_Version >= Ada_2005
4045
           and then Is_Tagged_Type (Prefix_Type)
4046
           and then not Is_Concurrent_Type (Prefix_Type)
4047
         then
4048
            if Nkind (Parent (N)) = N_Generic_Association
4049
              or else Nkind (Parent (N)) = N_Requeue_Statement
4050
              or else Nkind (Parent (N)) = N_Subprogram_Renaming_Declaration
4051
            then
4052
               if Find_Primitive_Operation (N) then
4053
                  return;
4054
               end if;
4055
 
4056
            elsif Try_Object_Operation (N) then
4057
               return;
4058
            end if;
4059
 
4060
            --  If the transformation fails, it will be necessary to redo the
4061
            --  analysis with all errors enabled, to indicate candidate
4062
            --  interpretations and reasons for each failure ???
4063
 
4064
         end if;
4065
 
4066
      elsif Is_Private_Type (Prefix_Type) then
4067
 
4068
         --  Allow access only to discriminants of the type. If the type has
4069
         --  no full view, gigi uses the parent type for the components, so we
4070
         --  do the same here.
4071
 
4072
         if No (Full_View (Prefix_Type)) then
4073
            Type_To_Use := Root_Type (Base_Type (Prefix_Type));
4074
            Comp := First_Entity (Type_To_Use);
4075
         end if;
4076
 
4077
         while Present (Comp) loop
4078
            if Chars (Comp) = Chars (Sel) then
4079
               if Ekind (Comp) = E_Discriminant then
4080
                  Set_Entity_With_Style_Check (Sel, Comp);
4081
                  Generate_Reference (Comp, Sel);
4082
 
4083
                  Set_Etype (Sel, Etype (Comp));
4084
                  Set_Etype (N,   Etype (Comp));
4085
                  Check_Implicit_Dereference (N, Etype (N));
4086
 
4087
                  if Is_Generic_Type (Prefix_Type)
4088
                    or else Is_Generic_Type (Root_Type (Prefix_Type))
4089
                  then
4090
                     Set_Original_Discriminant (Sel, Comp);
4091
                  end if;
4092
 
4093
               --  Before declaring an error, check whether this is tagged
4094
               --  private type and a call to a primitive operation.
4095
 
4096
               elsif Ada_Version >= Ada_2005
4097
                 and then Is_Tagged_Type (Prefix_Type)
4098
                 and then Try_Object_Operation (N)
4099
               then
4100
                  return;
4101
 
4102
               else
4103
                  Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4104
                  Error_Msg_NE ("invisible selector& for }", N, Sel);
4105
                  Set_Entity (Sel, Any_Id);
4106
                  Set_Etype (N, Any_Type);
4107
               end if;
4108
 
4109
               return;
4110
            end if;
4111
 
4112
            Next_Entity (Comp);
4113
         end loop;
4114
 
4115
      elsif Is_Concurrent_Type (Prefix_Type) then
4116
 
4117
         --  Find visible operation with given name. For a protected type,
4118
         --  the possible candidates are discriminants, entries or protected
4119
         --  procedures. For a task type, the set can only include entries or
4120
         --  discriminants if the task type is not an enclosing scope. If it
4121
         --  is an enclosing scope (e.g. in an inner task) then all entities
4122
         --  are visible, but the prefix must denote the enclosing scope, i.e.
4123
         --  can only be a direct name or an expanded name.
4124
 
4125
         Set_Etype (Sel, Any_Type);
4126
         In_Scope := In_Open_Scopes (Prefix_Type);
4127
 
4128
         while Present (Comp) loop
4129
            if Chars (Comp) = Chars (Sel) then
4130
               if Is_Overloadable (Comp) then
4131
                  Add_One_Interp (Sel, Comp, Etype (Comp));
4132
 
4133
                  --  If the prefix is tagged, the correct interpretation may
4134
                  --  lie in the primitive or class-wide operations of the
4135
                  --  type. Perform a simple conformance check to determine
4136
                  --  whether Try_Object_Operation should be invoked even if
4137
                  --  a visible entity is found.
4138
 
4139
                  if Is_Tagged_Type (Prefix_Type)
4140
                    and then
4141
                      Nkind_In (Parent (N), N_Procedure_Call_Statement,
4142
                                            N_Function_Call,
4143
                                            N_Indexed_Component)
4144
                    and then Has_Mode_Conformant_Spec (Comp)
4145
                  then
4146
                     Has_Candidate := True;
4147
                  end if;
4148
 
4149
               --  Note: a selected component may not denote a component of a
4150
               --  protected type (4.1.3(7)).
4151
 
4152
               elsif Ekind_In (Comp, E_Discriminant, E_Entry_Family)
4153
                 or else (In_Scope
4154
                            and then not Is_Protected_Type (Prefix_Type)
4155
                            and then Is_Entity_Name (Name))
4156
               then
4157
                  Set_Entity_With_Style_Check (Sel, Comp);
4158
                  Generate_Reference (Comp, Sel);
4159
 
4160
                  --  The selector is not overloadable, so we have a candidate
4161
                  --  interpretation.
4162
 
4163
                  Has_Candidate := True;
4164
 
4165
               else
4166
                  goto Next_Comp;
4167
               end if;
4168
 
4169
               Set_Etype (Sel, Etype (Comp));
4170
               Set_Etype (N,   Etype (Comp));
4171
 
4172
               if Ekind (Comp) = E_Discriminant then
4173
                  Set_Original_Discriminant (Sel, Comp);
4174
               end if;
4175
 
4176
               --  For access type case, introduce explicit dereference for
4177
               --  more uniform treatment of entry calls.
4178
 
4179
               if Is_Access_Type (Etype (Name)) then
4180
                  Insert_Explicit_Dereference (Name);
4181
                  Error_Msg_NW
4182
                    (Warn_On_Dereference, "?implicit dereference", N);
4183
               end if;
4184
            end if;
4185
 
4186
            <<Next_Comp>>
4187
               Next_Entity (Comp);
4188
               exit when not In_Scope
4189
                 and then
4190
                   Comp = First_Private_Entity (Base_Type (Prefix_Type));
4191
         end loop;
4192
 
4193
         --  If there is no visible entity with the given name or none of the
4194
         --  visible entities are plausible interpretations, check whether
4195
         --  there is some other primitive operation with that name.
4196
 
4197
         if Ada_Version >= Ada_2005
4198
           and then Is_Tagged_Type (Prefix_Type)
4199
         then
4200
            if (Etype (N) = Any_Type
4201
                  or else not Has_Candidate)
4202
              and then Try_Object_Operation (N)
4203
            then
4204
               return;
4205
 
4206
            --  If the context is not syntactically a procedure call, it
4207
            --  may be a call to a primitive function declared outside of
4208
            --  the synchronized type.
4209
 
4210
            --  If the context is a procedure call, there might still be
4211
            --  an overloading between an entry and a primitive procedure
4212
            --  declared outside of the synchronized type, called in prefix
4213
            --  notation. This is harder to disambiguate because in one case
4214
            --  the controlling formal is implicit ???
4215
 
4216
            elsif Nkind (Parent (N)) /= N_Procedure_Call_Statement
4217
              and then Nkind (Parent (N)) /= N_Indexed_Component
4218
              and then Try_Object_Operation (N)
4219
            then
4220
               return;
4221
            end if;
4222
 
4223
            --  Ada 2012 (AI05-0090-1): If we found a candidate of a call to an
4224
            --  entry or procedure of a tagged concurrent type we must check
4225
            --  if there are class-wide subprograms covering the primitive. If
4226
            --  true then Try_Object_Operation reports the error.
4227
 
4228
            if Has_Candidate
4229
              and then Is_Concurrent_Type (Prefix_Type)
4230
              and then Nkind (Parent (N)) = N_Procedure_Call_Statement
4231
 
4232
               --  Duplicate the call. This is required to avoid problems with
4233
               --  the tree transformations performed by Try_Object_Operation.
4234
 
4235
              and then
4236
                Try_Object_Operation
4237
                  (N            => Sinfo.Name (New_Copy_Tree (Parent (N))),
4238
                   CW_Test_Only => True)
4239
            then
4240
               return;
4241
            end if;
4242
         end if;
4243
 
4244
         if Etype (N) = Any_Type and then Is_Protected_Type (Prefix_Type) then
4245
 
4246
            --  Case of a prefix of a protected type: selector might denote
4247
            --  an invisible private component.
4248
 
4249
            Comp := First_Private_Entity (Base_Type (Prefix_Type));
4250
            while Present (Comp) and then Chars (Comp) /= Chars (Sel) loop
4251
               Next_Entity (Comp);
4252
            end loop;
4253
 
4254
            if Present (Comp) then
4255
               if Is_Single_Concurrent_Object then
4256
                  Error_Msg_Node_2 := Entity (Name);
4257
                  Error_Msg_NE ("invisible selector& for &", N, Sel);
4258
 
4259
               else
4260
                  Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4261
                  Error_Msg_NE ("invisible selector& for }", N, Sel);
4262
               end if;
4263
               return;
4264
            end if;
4265
         end if;
4266
 
4267
         Set_Is_Overloaded (N, Is_Overloaded (Sel));
4268
 
4269
      else
4270
         --  Invalid prefix
4271
 
4272
         Error_Msg_NE ("invalid prefix in selected component&", N, Sel);
4273
      end if;
4274
 
4275
      --  If N still has no type, the component is not defined in the prefix
4276
 
4277
      if Etype (N) = Any_Type then
4278
 
4279
         if Is_Single_Concurrent_Object then
4280
            Error_Msg_Node_2 := Entity (Name);
4281
            Error_Msg_NE ("no selector& for&", N, Sel);
4282
 
4283
            Check_Misspelled_Selector (Type_To_Use, Sel);
4284
 
4285
         elsif Is_Generic_Type (Prefix_Type)
4286
           and then Ekind (Prefix_Type) = E_Record_Type_With_Private
4287
           and then Prefix_Type /= Etype (Prefix_Type)
4288
           and then Is_Record_Type (Etype (Prefix_Type))
4289
         then
4290
            --  If this is a derived formal type, the parent may have
4291
            --  different visibility at this point. Try for an inherited
4292
            --  component before reporting an error.
4293
 
4294
            Set_Etype (Prefix (N), Etype (Prefix_Type));
4295
            Analyze_Selected_Component (N);
4296
            return;
4297
 
4298
         --  Similarly, if this is the actual for a formal derived type, the
4299
         --  component inherited from the generic parent may not be visible
4300
         --  in the actual, but the selected component is legal.
4301
 
4302
         elsif Ekind (Prefix_Type) = E_Record_Subtype_With_Private
4303
           and then Is_Generic_Actual_Type (Prefix_Type)
4304
           and then Present (Full_View (Prefix_Type))
4305
         then
4306
 
4307
            Find_Component_In_Instance
4308
              (Generic_Parent_Type (Parent (Prefix_Type)));
4309
            return;
4310
 
4311
         --  Finally, the formal and the actual may be private extensions,
4312
         --  but the generic is declared in a child unit of the parent, and
4313
         --  an additional step is needed to retrieve the proper scope.
4314
 
4315
         elsif In_Instance
4316
           and then Present (Parent_Subtype (Etype (Base_Type (Prefix_Type))))
4317
         then
4318
            Find_Component_In_Instance
4319
              (Parent_Subtype (Etype (Base_Type (Prefix_Type))));
4320
            return;
4321
 
4322
         --  Component not found, specialize error message when appropriate
4323
 
4324
         else
4325
            if Ekind (Prefix_Type) = E_Record_Subtype then
4326
 
4327
               --  Check whether this is a component of the base type which
4328
               --  is absent from a statically constrained subtype. This will
4329
               --  raise constraint error at run time, but is not a compile-
4330
               --  time error. When the selector is illegal for base type as
4331
               --  well fall through and generate a compilation error anyway.
4332
 
4333
               Comp := First_Component (Base_Type (Prefix_Type));
4334
               while Present (Comp) loop
4335
                  if Chars (Comp) = Chars (Sel)
4336
                    and then Is_Visible_Component (Comp)
4337
                  then
4338
                     Set_Entity_With_Style_Check (Sel, Comp);
4339
                     Generate_Reference (Comp, Sel);
4340
                     Set_Etype (Sel, Etype (Comp));
4341
                     Set_Etype (N,   Etype (Comp));
4342
 
4343
                     --  Emit appropriate message. Gigi will replace the
4344
                     --  node subsequently with the appropriate Raise.
4345
 
4346
                     Apply_Compile_Time_Constraint_Error
4347
                       (N, "component not present in }?",
4348
                        CE_Discriminant_Check_Failed,
4349
                        Ent => Prefix_Type, Rep => False);
4350
                     Set_Raises_Constraint_Error (N);
4351
                     return;
4352
                  end if;
4353
 
4354
                  Next_Component (Comp);
4355
               end loop;
4356
 
4357
            end if;
4358
 
4359
            Error_Msg_Node_2 := First_Subtype (Prefix_Type);
4360
            Error_Msg_NE ("no selector& for}", N, Sel);
4361
 
4362
            --  Add information in the case of an incomplete prefix
4363
 
4364
            if Is_Incomplete_Type (Type_To_Use) then
4365
               declare
4366
                  Inc : constant Entity_Id := First_Subtype (Type_To_Use);
4367
 
4368
               begin
4369
                  if From_With_Type (Scope (Type_To_Use)) then
4370
                     Error_Msg_NE
4371
                       ("\limited view of& has no components", N, Inc);
4372
 
4373
                  else
4374
                     Error_Msg_NE
4375
                       ("\premature usage of incomplete type&", N, Inc);
4376
 
4377
                     if Nkind (Parent (Inc)) =
4378
                                          N_Incomplete_Type_Declaration
4379
                     then
4380
                        --  Record location of premature use in entity so that
4381
                        --  a continuation message is generated when the
4382
                        --  completion is seen.
4383
 
4384
                        Set_Premature_Use (Parent (Inc), N);
4385
                     end if;
4386
                  end if;
4387
               end;
4388
            end if;
4389
 
4390
            Check_Misspelled_Selector (Type_To_Use, Sel);
4391
         end if;
4392
 
4393
         Set_Entity (Sel, Any_Id);
4394
         Set_Etype (Sel, Any_Type);
4395
      end if;
4396
   end Analyze_Selected_Component;
4397
 
4398
   ---------------------------
4399
   -- Analyze_Short_Circuit --
4400
   ---------------------------
4401
 
4402
   procedure Analyze_Short_Circuit (N : Node_Id) is
4403
      L   : constant Node_Id := Left_Opnd  (N);
4404
      R   : constant Node_Id := Right_Opnd (N);
4405
      Ind : Interp_Index;
4406
      It  : Interp;
4407
 
4408
   begin
4409
      Analyze_Expression (L);
4410
      Analyze_Expression (R);
4411
      Set_Etype (N, Any_Type);
4412
 
4413
      if not Is_Overloaded (L) then
4414
         if Root_Type (Etype (L)) = Standard_Boolean
4415
           and then Has_Compatible_Type (R, Etype (L))
4416
         then
4417
            Add_One_Interp (N, Etype (L), Etype (L));
4418
         end if;
4419
 
4420
      else
4421
         Get_First_Interp (L, Ind, It);
4422
         while Present (It.Typ) loop
4423
            if Root_Type (It.Typ) = Standard_Boolean
4424
              and then Has_Compatible_Type (R, It.Typ)
4425
            then
4426
               Add_One_Interp (N, It.Typ, It.Typ);
4427
            end if;
4428
 
4429
            Get_Next_Interp (Ind, It);
4430
         end loop;
4431
      end if;
4432
 
4433
      --  Here we have failed to find an interpretation. Clearly we know that
4434
      --  it is not the case that both operands can have an interpretation of
4435
      --  Boolean, but this is by far the most likely intended interpretation.
4436
      --  So we simply resolve both operands as Booleans, and at least one of
4437
      --  these resolutions will generate an error message, and we do not need
4438
      --  to give another error message on the short circuit operation itself.
4439
 
4440
      if Etype (N) = Any_Type then
4441
         Resolve (L, Standard_Boolean);
4442
         Resolve (R, Standard_Boolean);
4443
         Set_Etype (N, Standard_Boolean);
4444
      end if;
4445
   end Analyze_Short_Circuit;
4446
 
4447
   -------------------
4448
   -- Analyze_Slice --
4449
   -------------------
4450
 
4451
   procedure Analyze_Slice (N : Node_Id) is
4452
      P          : constant Node_Id := Prefix (N);
4453
      D          : constant Node_Id := Discrete_Range (N);
4454
      Array_Type : Entity_Id;
4455
 
4456
      procedure Analyze_Overloaded_Slice;
4457
      --  If the prefix is overloaded, select those interpretations that
4458
      --  yield a one-dimensional array type.
4459
 
4460
      ------------------------------
4461
      -- Analyze_Overloaded_Slice --
4462
      ------------------------------
4463
 
4464
      procedure Analyze_Overloaded_Slice is
4465
         I   : Interp_Index;
4466
         It  : Interp;
4467
         Typ : Entity_Id;
4468
 
4469
      begin
4470
         Set_Etype (N, Any_Type);
4471
 
4472
         Get_First_Interp (P, I, It);
4473
         while Present (It.Nam) loop
4474
            Typ := It.Typ;
4475
 
4476
            if Is_Access_Type (Typ) then
4477
               Typ := Designated_Type (Typ);
4478
               Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
4479
            end if;
4480
 
4481
            if Is_Array_Type (Typ)
4482
              and then Number_Dimensions (Typ) = 1
4483
              and then Has_Compatible_Type (D, Etype (First_Index (Typ)))
4484
            then
4485
               Add_One_Interp (N, Typ, Typ);
4486
            end if;
4487
 
4488
            Get_Next_Interp (I, It);
4489
         end loop;
4490
 
4491
         if Etype (N) = Any_Type then
4492
            Error_Msg_N ("expect array type in prefix of slice",  N);
4493
         end if;
4494
      end Analyze_Overloaded_Slice;
4495
 
4496
   --  Start of processing for Analyze_Slice
4497
 
4498
   begin
4499
      if Comes_From_Source (N) then
4500
         Check_SPARK_Restriction ("slice is not allowed", N);
4501
      end if;
4502
 
4503
      Analyze (P);
4504
      Analyze (D);
4505
 
4506
      if Is_Overloaded (P) then
4507
         Analyze_Overloaded_Slice;
4508
 
4509
      else
4510
         Array_Type := Etype (P);
4511
         Set_Etype (N, Any_Type);
4512
 
4513
         if Is_Access_Type (Array_Type) then
4514
            Array_Type := Designated_Type (Array_Type);
4515
            Error_Msg_NW (Warn_On_Dereference, "?implicit dereference", N);
4516
         end if;
4517
 
4518
         if not Is_Array_Type (Array_Type) then
4519
            Wrong_Type (P, Any_Array);
4520
 
4521
         elsif Number_Dimensions (Array_Type) > 1 then
4522
            Error_Msg_N
4523
              ("type is not one-dimensional array in slice prefix", N);
4524
 
4525
         elsif not
4526
           Has_Compatible_Type (D, Etype (First_Index (Array_Type)))
4527
         then
4528
            Wrong_Type (D, Etype (First_Index (Array_Type)));
4529
 
4530
         else
4531
            Set_Etype (N, Array_Type);
4532
         end if;
4533
      end if;
4534
   end Analyze_Slice;
4535
 
4536
   -----------------------------
4537
   -- Analyze_Type_Conversion --
4538
   -----------------------------
4539
 
4540
   procedure Analyze_Type_Conversion (N : Node_Id) is
4541
      Expr : constant Node_Id := Expression (N);
4542
      T    : Entity_Id;
4543
 
4544
   begin
4545
      --  If Conversion_OK is set, then the Etype is already set, and the
4546
      --  only processing required is to analyze the expression. This is
4547
      --  used to construct certain "illegal" conversions which are not
4548
      --  allowed by Ada semantics, but can be handled OK by Gigi, see
4549
      --  Sinfo for further details.
4550
 
4551
      if Conversion_OK (N) then
4552
         Analyze (Expr);
4553
         return;
4554
      end if;
4555
 
4556
      --  Otherwise full type analysis is required, as well as some semantic
4557
      --  checks to make sure the argument of the conversion is appropriate.
4558
 
4559
      Find_Type (Subtype_Mark (N));
4560
      T := Entity (Subtype_Mark (N));
4561
      Set_Etype (N, T);
4562
      Check_Fully_Declared (T, N);
4563
      Analyze_Expression (Expr);
4564
      Validate_Remote_Type_Type_Conversion (N);
4565
 
4566
      --  Only remaining step is validity checks on the argument. These
4567
      --  are skipped if the conversion does not come from the source.
4568
 
4569
      if not Comes_From_Source (N) then
4570
         return;
4571
 
4572
      --  If there was an error in a generic unit, no need to replicate the
4573
      --  error message. Conversely, constant-folding in the generic may
4574
      --  transform the argument of a conversion into a string literal, which
4575
      --  is legal. Therefore the following tests are not performed in an
4576
      --  instance.
4577
 
4578
      elsif In_Instance then
4579
         return;
4580
 
4581
      elsif Nkind (Expr) = N_Null then
4582
         Error_Msg_N ("argument of conversion cannot be null", N);
4583
         Error_Msg_N ("\use qualified expression instead", N);
4584
         Set_Etype (N, Any_Type);
4585
 
4586
      elsif Nkind (Expr) = N_Aggregate then
4587
         Error_Msg_N ("argument of conversion cannot be aggregate", N);
4588
         Error_Msg_N ("\use qualified expression instead", N);
4589
 
4590
      elsif Nkind (Expr) = N_Allocator then
4591
         Error_Msg_N ("argument of conversion cannot be an allocator", N);
4592
         Error_Msg_N ("\use qualified expression instead", N);
4593
 
4594
      elsif Nkind (Expr) = N_String_Literal then
4595
         Error_Msg_N ("argument of conversion cannot be string literal", N);
4596
         Error_Msg_N ("\use qualified expression instead", N);
4597
 
4598
      elsif Nkind (Expr) = N_Character_Literal then
4599
         if Ada_Version = Ada_83 then
4600
            Resolve (Expr, T);
4601
         else
4602
            Error_Msg_N ("argument of conversion cannot be character literal",
4603
              N);
4604
            Error_Msg_N ("\use qualified expression instead", N);
4605
         end if;
4606
 
4607
      elsif Nkind (Expr) = N_Attribute_Reference
4608
        and then
4609
          (Attribute_Name (Expr) = Name_Access            or else
4610
           Attribute_Name (Expr) = Name_Unchecked_Access  or else
4611
           Attribute_Name (Expr) = Name_Unrestricted_Access)
4612
      then
4613
         Error_Msg_N ("argument of conversion cannot be access", N);
4614
         Error_Msg_N ("\use qualified expression instead", N);
4615
      end if;
4616
   end Analyze_Type_Conversion;
4617
 
4618
   ----------------------
4619
   -- Analyze_Unary_Op --
4620
   ----------------------
4621
 
4622
   procedure Analyze_Unary_Op (N : Node_Id) is
4623
      R     : constant Node_Id := Right_Opnd (N);
4624
      Op_Id : Entity_Id := Entity (N);
4625
 
4626
   begin
4627
      Set_Etype (N, Any_Type);
4628
      Candidate_Type := Empty;
4629
 
4630
      Analyze_Expression (R);
4631
 
4632
      if Present (Op_Id) then
4633
         if Ekind (Op_Id) = E_Operator then
4634
            Find_Unary_Types (R, Op_Id,  N);
4635
         else
4636
            Add_One_Interp (N, Op_Id, Etype (Op_Id));
4637
         end if;
4638
 
4639
      else
4640
         Op_Id := Get_Name_Entity_Id (Chars (N));
4641
         while Present (Op_Id) loop
4642
            if Ekind (Op_Id) = E_Operator then
4643
               if No (Next_Entity (First_Entity (Op_Id))) then
4644
                  Find_Unary_Types (R, Op_Id,  N);
4645
               end if;
4646
 
4647
            elsif Is_Overloadable (Op_Id) then
4648
               Analyze_User_Defined_Unary_Op (N, Op_Id);
4649
            end if;
4650
 
4651
            Op_Id := Homonym (Op_Id);
4652
         end loop;
4653
      end if;
4654
 
4655
      Operator_Check (N);
4656
   end Analyze_Unary_Op;
4657
 
4658
   ----------------------------------
4659
   -- Analyze_Unchecked_Expression --
4660
   ----------------------------------
4661
 
4662
   procedure Analyze_Unchecked_Expression (N : Node_Id) is
4663
   begin
4664
      Analyze (Expression (N), Suppress => All_Checks);
4665
      Set_Etype (N, Etype (Expression (N)));
4666
      Save_Interps (Expression (N), N);
4667
   end Analyze_Unchecked_Expression;
4668
 
4669
   ---------------------------------------
4670
   -- Analyze_Unchecked_Type_Conversion --
4671
   ---------------------------------------
4672
 
4673
   procedure Analyze_Unchecked_Type_Conversion (N : Node_Id) is
4674
   begin
4675
      Find_Type (Subtype_Mark (N));
4676
      Analyze_Expression (Expression (N));
4677
      Set_Etype (N, Entity (Subtype_Mark (N)));
4678
   end Analyze_Unchecked_Type_Conversion;
4679
 
4680
   ------------------------------------
4681
   -- Analyze_User_Defined_Binary_Op --
4682
   ------------------------------------
4683
 
4684
   procedure Analyze_User_Defined_Binary_Op
4685
     (N     : Node_Id;
4686
      Op_Id : Entity_Id)
4687
   is
4688
   begin
4689
      --  Only do analysis if the operator Comes_From_Source, since otherwise
4690
      --  the operator was generated by the expander, and all such operators
4691
      --  always refer to the operators in package Standard.
4692
 
4693
      if Comes_From_Source (N) then
4694
         declare
4695
            F1 : constant Entity_Id := First_Formal (Op_Id);
4696
            F2 : constant Entity_Id := Next_Formal (F1);
4697
 
4698
         begin
4699
            --  Verify that Op_Id is a visible binary function. Note that since
4700
            --  we know Op_Id is overloaded, potentially use visible means use
4701
            --  visible for sure (RM 9.4(11)).
4702
 
4703
            if Ekind (Op_Id) = E_Function
4704
              and then Present (F2)
4705
              and then (Is_Immediately_Visible (Op_Id)
4706
                         or else Is_Potentially_Use_Visible (Op_Id))
4707
              and then Has_Compatible_Type (Left_Opnd (N), Etype (F1))
4708
              and then Has_Compatible_Type (Right_Opnd (N), Etype (F2))
4709
            then
4710
               Add_One_Interp (N, Op_Id, Etype (Op_Id));
4711
 
4712
               --  If the left operand is overloaded, indicate that the
4713
               --  current type is a viable candidate. This is redundant
4714
               --  in most cases, but for equality and comparison operators
4715
               --  where the context does not impose a type on the operands,
4716
               --  setting the proper type is necessary to avoid subsequent
4717
               --  ambiguities during resolution, when both user-defined and
4718
               --  predefined operators may be candidates.
4719
 
4720
               if Is_Overloaded (Left_Opnd (N)) then
4721
                  Set_Etype (Left_Opnd (N), Etype (F1));
4722
               end if;
4723
 
4724
               if Debug_Flag_E then
4725
                  Write_Str ("user defined operator ");
4726
                  Write_Name (Chars (Op_Id));
4727
                  Write_Str (" on node ");
4728
                  Write_Int (Int (N));
4729
                  Write_Eol;
4730
               end if;
4731
            end if;
4732
         end;
4733
      end if;
4734
   end Analyze_User_Defined_Binary_Op;
4735
 
4736
   -----------------------------------
4737
   -- Analyze_User_Defined_Unary_Op --
4738
   -----------------------------------
4739
 
4740
   procedure Analyze_User_Defined_Unary_Op
4741
     (N     : Node_Id;
4742
      Op_Id : Entity_Id)
4743
   is
4744
   begin
4745
      --  Only do analysis if the operator Comes_From_Source, since otherwise
4746
      --  the operator was generated by the expander, and all such operators
4747
      --  always refer to the operators in package Standard.
4748
 
4749
      if Comes_From_Source (N) then
4750
         declare
4751
            F : constant Entity_Id := First_Formal (Op_Id);
4752
 
4753
         begin
4754
            --  Verify that Op_Id is a visible unary function. Note that since
4755
            --  we know Op_Id is overloaded, potentially use visible means use
4756
            --  visible for sure (RM 9.4(11)).
4757
 
4758
            if Ekind (Op_Id) = E_Function
4759
              and then No (Next_Formal (F))
4760
              and then (Is_Immediately_Visible (Op_Id)
4761
                         or else Is_Potentially_Use_Visible (Op_Id))
4762
              and then Has_Compatible_Type (Right_Opnd (N), Etype (F))
4763
            then
4764
               Add_One_Interp (N, Op_Id, Etype (Op_Id));
4765
            end if;
4766
         end;
4767
      end if;
4768
   end Analyze_User_Defined_Unary_Op;
4769
 
4770
   ---------------------------
4771
   -- Check_Arithmetic_Pair --
4772
   ---------------------------
4773
 
4774
   procedure Check_Arithmetic_Pair
4775
     (T1, T2 : Entity_Id;
4776
      Op_Id  : Entity_Id;
4777
      N      : Node_Id)
4778
   is
4779
      Op_Name : constant Name_Id := Chars (Op_Id);
4780
 
4781
      function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean;
4782
      --  Check whether the fixed-point type Typ has a user-defined operator
4783
      --  (multiplication or division) that should hide the corresponding
4784
      --  predefined operator. Used to implement Ada 2005 AI-264, to make
4785
      --  such operators more visible and therefore useful.
4786
 
4787
      --  If the name of the operation is an expanded name with prefix
4788
      --  Standard, the predefined universal fixed operator is available,
4789
      --  as specified by AI-420 (RM 4.5.5 (19.1/2)).
4790
 
4791
      function Specific_Type (T1, T2 : Entity_Id) return Entity_Id;
4792
      --  Get specific type (i.e. non-universal type if there is one)
4793
 
4794
      ------------------
4795
      -- Has_Fixed_Op --
4796
      ------------------
4797
 
4798
      function Has_Fixed_Op (Typ : Entity_Id; Op : Entity_Id) return Boolean is
4799
         Bas : constant Entity_Id := Base_Type (Typ);
4800
         Ent : Entity_Id;
4801
         F1  : Entity_Id;
4802
         F2  : Entity_Id;
4803
 
4804
      begin
4805
         --  If the universal_fixed operation is given explicitly the rule
4806
         --  concerning primitive operations of the type do not apply.
4807
 
4808
         if Nkind (N) = N_Function_Call
4809
           and then Nkind (Name (N)) = N_Expanded_Name
4810
           and then Entity (Prefix (Name (N))) = Standard_Standard
4811
         then
4812
            return False;
4813
         end if;
4814
 
4815
         --  The operation is treated as primitive if it is declared in the
4816
         --  same scope as the type, and therefore on the same entity chain.
4817
 
4818
         Ent := Next_Entity (Typ);
4819
         while Present (Ent) loop
4820
            if Chars (Ent) = Chars (Op) then
4821
               F1 := First_Formal (Ent);
4822
               F2 := Next_Formal (F1);
4823
 
4824
               --  The operation counts as primitive if either operand or
4825
               --  result are of the given base type, and both operands are
4826
               --  fixed point types.
4827
 
4828
               if (Base_Type (Etype (F1)) = Bas
4829
                    and then Is_Fixed_Point_Type (Etype (F2)))
4830
 
4831
                 or else
4832
                   (Base_Type (Etype (F2)) = Bas
4833
                     and then Is_Fixed_Point_Type (Etype (F1)))
4834
 
4835
                 or else
4836
                   (Base_Type (Etype (Ent)) = Bas
4837
                     and then Is_Fixed_Point_Type (Etype (F1))
4838
                     and then Is_Fixed_Point_Type (Etype (F2)))
4839
               then
4840
                  return True;
4841
               end if;
4842
            end if;
4843
 
4844
            Next_Entity (Ent);
4845
         end loop;
4846
 
4847
         return False;
4848
      end Has_Fixed_Op;
4849
 
4850
      -------------------
4851
      -- Specific_Type --
4852
      -------------------
4853
 
4854
      function Specific_Type (T1, T2 : Entity_Id) return Entity_Id is
4855
      begin
4856
         if T1 = Universal_Integer or else T1 = Universal_Real then
4857
            return Base_Type (T2);
4858
         else
4859
            return Base_Type (T1);
4860
         end if;
4861
      end Specific_Type;
4862
 
4863
   --  Start of processing for Check_Arithmetic_Pair
4864
 
4865
   begin
4866
      if Op_Name = Name_Op_Add or else Op_Name = Name_Op_Subtract then
4867
 
4868
         if Is_Numeric_Type (T1)
4869
           and then Is_Numeric_Type (T2)
4870
           and then (Covers (T1 => T1, T2 => T2)
4871
                       or else
4872
                     Covers (T1 => T2, T2 => T1))
4873
         then
4874
            Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
4875
         end if;
4876
 
4877
      elsif Op_Name = Name_Op_Multiply or else Op_Name = Name_Op_Divide then
4878
 
4879
         if Is_Fixed_Point_Type (T1)
4880
           and then (Is_Fixed_Point_Type (T2)
4881
                       or else T2 = Universal_Real)
4882
         then
4883
            --  If Treat_Fixed_As_Integer is set then the Etype is already set
4884
            --  and no further processing is required (this is the case of an
4885
            --  operator constructed by Exp_Fixd for a fixed point operation)
4886
            --  Otherwise add one interpretation with universal fixed result
4887
            --  If the operator is given in  functional notation, it comes
4888
            --  from source and Fixed_As_Integer cannot apply.
4889
 
4890
            if (Nkind (N) not in N_Op
4891
                 or else not Treat_Fixed_As_Integer (N))
4892
              and then
4893
                (not Has_Fixed_Op (T1, Op_Id)
4894
                  or else Nkind (Parent (N)) = N_Type_Conversion)
4895
            then
4896
               Add_One_Interp (N, Op_Id, Universal_Fixed);
4897
            end if;
4898
 
4899
         elsif Is_Fixed_Point_Type (T2)
4900
           and then (Nkind (N) not in N_Op
4901
                      or else not Treat_Fixed_As_Integer (N))
4902
           and then T1 = Universal_Real
4903
           and then
4904
             (not Has_Fixed_Op (T1, Op_Id)
4905
               or else Nkind (Parent (N)) = N_Type_Conversion)
4906
         then
4907
            Add_One_Interp (N, Op_Id, Universal_Fixed);
4908
 
4909
         elsif Is_Numeric_Type (T1)
4910
           and then Is_Numeric_Type (T2)
4911
           and then (Covers (T1 => T1, T2 => T2)
4912
                       or else
4913
                     Covers (T1 => T2, T2 => T1))
4914
         then
4915
            Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
4916
 
4917
         elsif Is_Fixed_Point_Type (T1)
4918
           and then (Base_Type (T2) = Base_Type (Standard_Integer)
4919
                       or else T2 = Universal_Integer)
4920
         then
4921
            Add_One_Interp (N, Op_Id, T1);
4922
 
4923
         elsif T2 = Universal_Real
4924
           and then Base_Type (T1) = Base_Type (Standard_Integer)
4925
           and then Op_Name = Name_Op_Multiply
4926
         then
4927
            Add_One_Interp (N, Op_Id, Any_Fixed);
4928
 
4929
         elsif T1 = Universal_Real
4930
           and then Base_Type (T2) = Base_Type (Standard_Integer)
4931
         then
4932
            Add_One_Interp (N, Op_Id, Any_Fixed);
4933
 
4934
         elsif Is_Fixed_Point_Type (T2)
4935
           and then (Base_Type (T1) = Base_Type (Standard_Integer)
4936
                       or else T1 = Universal_Integer)
4937
           and then Op_Name = Name_Op_Multiply
4938
         then
4939
            Add_One_Interp (N, Op_Id, T2);
4940
 
4941
         elsif T1 = Universal_Real and then T2 = Universal_Integer then
4942
            Add_One_Interp (N, Op_Id, T1);
4943
 
4944
         elsif T2 = Universal_Real
4945
           and then T1 = Universal_Integer
4946
           and then Op_Name = Name_Op_Multiply
4947
         then
4948
            Add_One_Interp (N, Op_Id, T2);
4949
         end if;
4950
 
4951
      elsif Op_Name = Name_Op_Mod or else Op_Name = Name_Op_Rem then
4952
 
4953
         --  Note: The fixed-point operands case with Treat_Fixed_As_Integer
4954
         --  set does not require any special processing, since the Etype is
4955
         --  already set (case of operation constructed by Exp_Fixed).
4956
 
4957
         if Is_Integer_Type (T1)
4958
           and then (Covers (T1 => T1, T2 => T2)
4959
                       or else
4960
                     Covers (T1 => T2, T2 => T1))
4961
         then
4962
            Add_One_Interp (N, Op_Id, Specific_Type (T1, T2));
4963
         end if;
4964
 
4965
      elsif Op_Name = Name_Op_Expon then
4966
         if Is_Numeric_Type (T1)
4967
           and then not Is_Fixed_Point_Type (T1)
4968
           and then (Base_Type (T2) = Base_Type (Standard_Integer)
4969
                      or else T2 = Universal_Integer)
4970
         then
4971
            Add_One_Interp (N, Op_Id, Base_Type (T1));
4972
         end if;
4973
 
4974
      else pragma Assert (Nkind (N) in N_Op_Shift);
4975
 
4976
         --  If not one of the predefined operators, the node may be one
4977
         --  of the intrinsic functions. Its kind is always specific, and
4978
         --  we can use it directly, rather than the name of the operation.
4979
 
4980
         if Is_Integer_Type (T1)
4981
           and then (Base_Type (T2) = Base_Type (Standard_Integer)
4982
                      or else T2 = Universal_Integer)
4983
         then
4984
            Add_One_Interp (N, Op_Id, Base_Type (T1));
4985
         end if;
4986
      end if;
4987
   end Check_Arithmetic_Pair;
4988
 
4989
   -------------------------------
4990
   -- Check_Misspelled_Selector --
4991
   -------------------------------
4992
 
4993
   procedure Check_Misspelled_Selector
4994
     (Prefix : Entity_Id;
4995
      Sel    : Node_Id)
4996
   is
4997
      Max_Suggestions   : constant := 2;
4998
      Nr_Of_Suggestions : Natural := 0;
4999
 
5000
      Suggestion_1 : Entity_Id := Empty;
5001
      Suggestion_2 : Entity_Id := Empty;
5002
 
5003
      Comp : Entity_Id;
5004
 
5005
   begin
5006
      --  All the components of the prefix of selector Sel are matched
5007
      --  against  Sel and a count is maintained of possible misspellings.
5008
      --  When at the end of the analysis there are one or two (not more!)
5009
      --  possible misspellings, these misspellings will be suggested as
5010
      --  possible correction.
5011
 
5012
      if not (Is_Private_Type (Prefix) or else Is_Record_Type (Prefix)) then
5013
 
5014
         --  Concurrent types should be handled as well ???
5015
 
5016
         return;
5017
      end if;
5018
 
5019
      Comp  := First_Entity (Prefix);
5020
      while Nr_Of_Suggestions <= Max_Suggestions and then Present (Comp) loop
5021
         if Is_Visible_Component (Comp) then
5022
            if Is_Bad_Spelling_Of (Chars (Comp), Chars (Sel)) then
5023
               Nr_Of_Suggestions := Nr_Of_Suggestions + 1;
5024
 
5025
               case Nr_Of_Suggestions is
5026
                  when 1      => Suggestion_1 := Comp;
5027
                  when 2      => Suggestion_2 := Comp;
5028
                  when others => exit;
5029
               end case;
5030
            end if;
5031
         end if;
5032
 
5033
         Comp := Next_Entity (Comp);
5034
      end loop;
5035
 
5036
      --  Report at most two suggestions
5037
 
5038
      if Nr_Of_Suggestions = 1 then
5039
         Error_Msg_NE -- CODEFIX
5040
           ("\possible misspelling of&", Sel, Suggestion_1);
5041
 
5042
      elsif Nr_Of_Suggestions = 2 then
5043
         Error_Msg_Node_2 := Suggestion_2;
5044
         Error_Msg_NE -- CODEFIX
5045
           ("\possible misspelling of& or&", Sel, Suggestion_1);
5046
      end if;
5047
   end Check_Misspelled_Selector;
5048
 
5049
   ----------------------
5050
   -- Defined_In_Scope --
5051
   ----------------------
5052
 
5053
   function Defined_In_Scope (T : Entity_Id; S : Entity_Id) return Boolean
5054
   is
5055
      S1 : constant Entity_Id := Scope (Base_Type (T));
5056
   begin
5057
      return S1 = S
5058
        or else (S1 = System_Aux_Id and then S = Scope (S1));
5059
   end Defined_In_Scope;
5060
 
5061
   -------------------
5062
   -- Diagnose_Call --
5063
   -------------------
5064
 
5065
   procedure Diagnose_Call (N : Node_Id; Nam : Node_Id) is
5066
      Actual           : Node_Id;
5067
      X                : Interp_Index;
5068
      It               : Interp;
5069
      Err_Mode         : Boolean;
5070
      New_Nam          : Node_Id;
5071
      Void_Interp_Seen : Boolean := False;
5072
 
5073
      Success : Boolean;
5074
      pragma Warnings (Off, Boolean);
5075
 
5076
   begin
5077
      if Ada_Version >= Ada_2005 then
5078
         Actual := First_Actual (N);
5079
         while Present (Actual) loop
5080
 
5081
            --  Ada 2005 (AI-50217): Post an error in case of premature
5082
            --  usage of an entity from the limited view.
5083
 
5084
            if not Analyzed (Etype (Actual))
5085
             and then From_With_Type (Etype (Actual))
5086
            then
5087
               Error_Msg_Qual_Level := 1;
5088
               Error_Msg_NE
5089
                ("missing with_clause for scope of imported type&",
5090
                  Actual, Etype (Actual));
5091
               Error_Msg_Qual_Level := 0;
5092
            end if;
5093
 
5094
            Next_Actual (Actual);
5095
         end loop;
5096
      end if;
5097
 
5098
      --   Analyze each candidate call again, with full error reporting
5099
      --   for each.
5100
 
5101
      Error_Msg_N
5102
        ("no candidate interpretations match the actuals:!", Nam);
5103
      Err_Mode := All_Errors_Mode;
5104
      All_Errors_Mode := True;
5105
 
5106
      --  If this is a call to an operation of a concurrent type,
5107
      --  the failed interpretations have been removed from the
5108
      --  name. Recover them to provide full diagnostics.
5109
 
5110
      if Nkind (Parent (Nam)) = N_Selected_Component then
5111
         Set_Entity (Nam, Empty);
5112
         New_Nam := New_Copy_Tree (Parent (Nam));
5113
         Set_Is_Overloaded (New_Nam, False);
5114
         Set_Is_Overloaded (Selector_Name (New_Nam), False);
5115
         Set_Parent (New_Nam, Parent (Parent (Nam)));
5116
         Analyze_Selected_Component (New_Nam);
5117
         Get_First_Interp (Selector_Name (New_Nam), X, It);
5118
      else
5119
         Get_First_Interp (Nam, X, It);
5120
      end if;
5121
 
5122
      while Present (It.Nam) loop
5123
         if Etype (It.Nam) = Standard_Void_Type then
5124
            Void_Interp_Seen := True;
5125
         end if;
5126
 
5127
         Analyze_One_Call (N, It.Nam, True, Success);
5128
         Get_Next_Interp (X, It);
5129
      end loop;
5130
 
5131
      if Nkind (N) = N_Function_Call then
5132
         Get_First_Interp (Nam, X, It);
5133
         while Present (It.Nam) loop
5134
            if Ekind_In (It.Nam, E_Function, E_Operator) then
5135
               return;
5136
            else
5137
               Get_Next_Interp (X, It);
5138
            end if;
5139
         end loop;
5140
 
5141
         --  If all interpretations are procedures, this deserves a
5142
         --  more precise message. Ditto if this appears as the prefix
5143
         --  of a selected component, which may be a lexical error.
5144
 
5145
         Error_Msg_N
5146
           ("\context requires function call, found procedure name", Nam);
5147
 
5148
         if Nkind (Parent (N)) = N_Selected_Component
5149
           and then N = Prefix (Parent (N))
5150
         then
5151
            Error_Msg_N -- CODEFIX
5152
              ("\period should probably be semicolon", Parent (N));
5153
         end if;
5154
 
5155
      elsif Nkind (N) = N_Procedure_Call_Statement
5156
        and then not Void_Interp_Seen
5157
      then
5158
         Error_Msg_N (
5159
         "\function name found in procedure call", Nam);
5160
      end if;
5161
 
5162
      All_Errors_Mode := Err_Mode;
5163
   end Diagnose_Call;
5164
 
5165
   ---------------------------
5166
   -- Find_Arithmetic_Types --
5167
   ---------------------------
5168
 
5169
   procedure Find_Arithmetic_Types
5170
     (L, R  : Node_Id;
5171
      Op_Id : Entity_Id;
5172
      N     : Node_Id)
5173
   is
5174
      Index1 : Interp_Index;
5175
      Index2 : Interp_Index;
5176
      It1    : Interp;
5177
      It2    : Interp;
5178
 
5179
      procedure Check_Right_Argument (T : Entity_Id);
5180
      --  Check right operand of operator
5181
 
5182
      --------------------------
5183
      -- Check_Right_Argument --
5184
      --------------------------
5185
 
5186
      procedure Check_Right_Argument (T : Entity_Id) is
5187
      begin
5188
         if not Is_Overloaded (R) then
5189
            Check_Arithmetic_Pair (T, Etype (R), Op_Id,  N);
5190
         else
5191
            Get_First_Interp (R, Index2, It2);
5192
            while Present (It2.Typ) loop
5193
               Check_Arithmetic_Pair (T, It2.Typ, Op_Id, N);
5194
               Get_Next_Interp (Index2, It2);
5195
            end loop;
5196
         end if;
5197
      end Check_Right_Argument;
5198
 
5199
   --  Start of processing for Find_Arithmetic_Types
5200
 
5201
   begin
5202
      if not Is_Overloaded (L) then
5203
         Check_Right_Argument (Etype (L));
5204
 
5205
      else
5206
         Get_First_Interp (L, Index1, It1);
5207
         while Present (It1.Typ) loop
5208
            Check_Right_Argument (It1.Typ);
5209
            Get_Next_Interp (Index1, It1);
5210
         end loop;
5211
      end if;
5212
 
5213
   end Find_Arithmetic_Types;
5214
 
5215
   ------------------------
5216
   -- Find_Boolean_Types --
5217
   ------------------------
5218
 
5219
   procedure Find_Boolean_Types
5220
     (L, R  : Node_Id;
5221
      Op_Id : Entity_Id;
5222
      N     : Node_Id)
5223
   is
5224
      Index : Interp_Index;
5225
      It    : Interp;
5226
 
5227
      procedure Check_Numeric_Argument (T : Entity_Id);
5228
      --  Special case for logical operations one of whose operands is an
5229
      --  integer literal. If both are literal the result is any modular type.
5230
 
5231
      ----------------------------
5232
      -- Check_Numeric_Argument --
5233
      ----------------------------
5234
 
5235
      procedure Check_Numeric_Argument (T : Entity_Id) is
5236
      begin
5237
         if T = Universal_Integer then
5238
            Add_One_Interp (N, Op_Id, Any_Modular);
5239
 
5240
         elsif Is_Modular_Integer_Type (T) then
5241
            Add_One_Interp (N, Op_Id, T);
5242
         end if;
5243
      end Check_Numeric_Argument;
5244
 
5245
   --  Start of processing for Find_Boolean_Types
5246
 
5247
   begin
5248
      if not Is_Overloaded (L) then
5249
         if Etype (L) = Universal_Integer
5250
           or else Etype (L) = Any_Modular
5251
         then
5252
            if not Is_Overloaded (R) then
5253
               Check_Numeric_Argument (Etype (R));
5254
 
5255
            else
5256
               Get_First_Interp (R, Index, It);
5257
               while Present (It.Typ) loop
5258
                  Check_Numeric_Argument (It.Typ);
5259
                  Get_Next_Interp (Index, It);
5260
               end loop;
5261
            end if;
5262
 
5263
         --  If operands are aggregates, we must assume that they may be
5264
         --  boolean arrays, and leave disambiguation for the second pass.
5265
         --  If only one is an aggregate, verify that the other one has an
5266
         --  interpretation as a boolean array
5267
 
5268
         elsif Nkind (L) = N_Aggregate then
5269
            if Nkind (R) = N_Aggregate then
5270
               Add_One_Interp (N, Op_Id, Etype (L));
5271
 
5272
            elsif not Is_Overloaded (R) then
5273
               if Valid_Boolean_Arg (Etype (R)) then
5274
                  Add_One_Interp (N, Op_Id, Etype (R));
5275
               end if;
5276
 
5277
            else
5278
               Get_First_Interp (R, Index, It);
5279
               while Present (It.Typ) loop
5280
                  if Valid_Boolean_Arg (It.Typ) then
5281
                     Add_One_Interp (N, Op_Id, It.Typ);
5282
                  end if;
5283
 
5284
                  Get_Next_Interp (Index, It);
5285
               end loop;
5286
            end if;
5287
 
5288
         elsif Valid_Boolean_Arg (Etype (L))
5289
           and then Has_Compatible_Type (R, Etype (L))
5290
         then
5291
            Add_One_Interp (N, Op_Id, Etype (L));
5292
         end if;
5293
 
5294
      else
5295
         Get_First_Interp (L, Index, It);
5296
         while Present (It.Typ) loop
5297
            if Valid_Boolean_Arg (It.Typ)
5298
              and then Has_Compatible_Type (R, It.Typ)
5299
            then
5300
               Add_One_Interp (N, Op_Id, It.Typ);
5301
            end if;
5302
 
5303
            Get_Next_Interp (Index, It);
5304
         end loop;
5305
      end if;
5306
   end Find_Boolean_Types;
5307
 
5308
   ---------------------------
5309
   -- Find_Comparison_Types --
5310
   ---------------------------
5311
 
5312
   procedure Find_Comparison_Types
5313
     (L, R  : Node_Id;
5314
      Op_Id : Entity_Id;
5315
      N     : Node_Id)
5316
   is
5317
      Index : Interp_Index;
5318
      It    : Interp;
5319
      Found : Boolean := False;
5320
      I_F   : Interp_Index;
5321
      T_F   : Entity_Id;
5322
      Scop  : Entity_Id := Empty;
5323
 
5324
      procedure Try_One_Interp (T1 : Entity_Id);
5325
      --  Routine to try one proposed interpretation. Note that the context
5326
      --  of the operator plays no role in resolving the arguments, so that
5327
      --  if there is more than one interpretation of the operands that is
5328
      --  compatible with comparison, the operation is ambiguous.
5329
 
5330
      --------------------
5331
      -- Try_One_Interp --
5332
      --------------------
5333
 
5334
      procedure Try_One_Interp (T1 : Entity_Id) is
5335
      begin
5336
 
5337
         --  If the operator is an expanded name, then the type of the operand
5338
         --  must be defined in the corresponding scope. If the type is
5339
         --  universal, the context will impose the correct type.
5340
 
5341
         if Present (Scop)
5342
            and then not Defined_In_Scope (T1, Scop)
5343
            and then T1 /= Universal_Integer
5344
            and then T1 /= Universal_Real
5345
            and then T1 /= Any_String
5346
            and then T1 /= Any_Composite
5347
         then
5348
            return;
5349
         end if;
5350
 
5351
         if Valid_Comparison_Arg (T1)
5352
           and then Has_Compatible_Type (R, T1)
5353
         then
5354
            if Found
5355
              and then Base_Type (T1) /= Base_Type (T_F)
5356
            then
5357
               It := Disambiguate (L, I_F, Index, Any_Type);
5358
 
5359
               if It = No_Interp then
5360
                  Ambiguous_Operands (N);
5361
                  Set_Etype (L, Any_Type);
5362
                  return;
5363
 
5364
               else
5365
                  T_F := It.Typ;
5366
               end if;
5367
 
5368
            else
5369
               Found := True;
5370
               T_F   := T1;
5371
               I_F   := Index;
5372
            end if;
5373
 
5374
            Set_Etype (L, T_F);
5375
            Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
5376
 
5377
         end if;
5378
      end Try_One_Interp;
5379
 
5380
   --  Start of processing for Find_Comparison_Types
5381
 
5382
   begin
5383
      --  If left operand is aggregate, the right operand has to
5384
      --  provide a usable type for it.
5385
 
5386
      if Nkind (L) = N_Aggregate
5387
        and then Nkind (R) /= N_Aggregate
5388
      then
5389
         Find_Comparison_Types (L => R, R => L, Op_Id => Op_Id, N => N);
5390
         return;
5391
      end if;
5392
 
5393
      if Nkind (N) = N_Function_Call
5394
         and then Nkind (Name (N)) = N_Expanded_Name
5395
      then
5396
         Scop := Entity (Prefix (Name (N)));
5397
 
5398
         --  The prefix may be a package renaming, and the subsequent test
5399
         --  requires the original package.
5400
 
5401
         if Ekind (Scop) = E_Package
5402
           and then Present (Renamed_Entity (Scop))
5403
         then
5404
            Scop := Renamed_Entity (Scop);
5405
            Set_Entity (Prefix (Name (N)), Scop);
5406
         end if;
5407
      end if;
5408
 
5409
      if not Is_Overloaded (L) then
5410
         Try_One_Interp (Etype (L));
5411
 
5412
      else
5413
         Get_First_Interp (L, Index, It);
5414
         while Present (It.Typ) loop
5415
            Try_One_Interp (It.Typ);
5416
            Get_Next_Interp (Index, It);
5417
         end loop;
5418
      end if;
5419
   end Find_Comparison_Types;
5420
 
5421
   ----------------------------------------
5422
   -- Find_Non_Universal_Interpretations --
5423
   ----------------------------------------
5424
 
5425
   procedure Find_Non_Universal_Interpretations
5426
     (N     : Node_Id;
5427
      R     : Node_Id;
5428
      Op_Id : Entity_Id;
5429
      T1    : Entity_Id)
5430
   is
5431
      Index : Interp_Index;
5432
      It    : Interp;
5433
 
5434
   begin
5435
      if T1 = Universal_Integer
5436
        or else T1 = Universal_Real
5437
      then
5438
         if not Is_Overloaded (R) then
5439
            Add_One_Interp
5440
              (N, Op_Id, Standard_Boolean, Base_Type (Etype (R)));
5441
         else
5442
            Get_First_Interp (R, Index, It);
5443
            while Present (It.Typ) loop
5444
               if Covers (It.Typ, T1) then
5445
                  Add_One_Interp
5446
                    (N, Op_Id, Standard_Boolean, Base_Type (It.Typ));
5447
               end if;
5448
 
5449
               Get_Next_Interp (Index, It);
5450
            end loop;
5451
         end if;
5452
      else
5453
         Add_One_Interp (N, Op_Id, Standard_Boolean, Base_Type (T1));
5454
      end if;
5455
   end Find_Non_Universal_Interpretations;
5456
 
5457
   ------------------------------
5458
   -- Find_Concatenation_Types --
5459
   ------------------------------
5460
 
5461
   procedure Find_Concatenation_Types
5462
     (L, R  : Node_Id;
5463
      Op_Id : Entity_Id;
5464
      N     : Node_Id)
5465
   is
5466
      Op_Type : constant Entity_Id := Etype (Op_Id);
5467
 
5468
   begin
5469
      if Is_Array_Type (Op_Type)
5470
        and then not Is_Limited_Type (Op_Type)
5471
 
5472
        and then (Has_Compatible_Type (L, Op_Type)
5473
                    or else
5474
                  Has_Compatible_Type (L, Component_Type (Op_Type)))
5475
 
5476
        and then (Has_Compatible_Type (R, Op_Type)
5477
                    or else
5478
                  Has_Compatible_Type (R, Component_Type (Op_Type)))
5479
      then
5480
         Add_One_Interp (N, Op_Id, Op_Type);
5481
      end if;
5482
   end Find_Concatenation_Types;
5483
 
5484
   -------------------------
5485
   -- Find_Equality_Types --
5486
   -------------------------
5487
 
5488
   procedure Find_Equality_Types
5489
     (L, R  : Node_Id;
5490
      Op_Id : Entity_Id;
5491
      N     : Node_Id)
5492
   is
5493
      Index : Interp_Index;
5494
      It    : Interp;
5495
      Found : Boolean := False;
5496
      I_F   : Interp_Index;
5497
      T_F   : Entity_Id;
5498
      Scop  : Entity_Id := Empty;
5499
 
5500
      procedure Try_One_Interp (T1 : Entity_Id);
5501
      --  The context of the equality operator plays no role in resolving the
5502
      --  arguments, so that if there is more than one interpretation of the
5503
      --  operands that is compatible with equality, the construct is ambiguous
5504
      --  and an error can be emitted now, after trying to disambiguate, i.e.
5505
      --  applying preference rules.
5506
 
5507
      --------------------
5508
      -- Try_One_Interp --
5509
      --------------------
5510
 
5511
      procedure Try_One_Interp (T1 : Entity_Id) is
5512
         Bas : constant Entity_Id := Base_Type (T1);
5513
 
5514
      begin
5515
         --  If the operator is an expanded name, then the type of the operand
5516
         --  must be defined in the corresponding scope. If the type is
5517
         --  universal, the context will impose the correct type. An anonymous
5518
         --  type for a 'Access reference is also universal in this sense, as
5519
         --  the actual type is obtained from context.
5520
         --  In Ada 2005, the equality operator for anonymous access types
5521
         --  is declared in Standard, and preference rules apply to it.
5522
 
5523
         if Present (Scop) then
5524
            if Defined_In_Scope (T1, Scop)
5525
              or else T1 = Universal_Integer
5526
              or else T1 = Universal_Real
5527
              or else T1 = Any_Access
5528
              or else T1 = Any_String
5529
              or else T1 = Any_Composite
5530
              or else (Ekind (T1) = E_Access_Subprogram_Type
5531
                        and then not Comes_From_Source (T1))
5532
            then
5533
               null;
5534
 
5535
            elsif Ekind (T1) = E_Anonymous_Access_Type
5536
              and then Scop = Standard_Standard
5537
            then
5538
               null;
5539
 
5540
            else
5541
               --  The scope does not contain an operator for the type
5542
 
5543
               return;
5544
            end if;
5545
 
5546
         --  If we have infix notation, the operator must be usable. Within
5547
         --  an instance, if the type is already established we know it is
5548
         --  correct. If an operand is universal it is compatible with any
5549
         --  numeric type.
5550
 
5551
         --  In Ada 2005, the equality on anonymous access types is declared
5552
         --  in Standard, and is always visible.
5553
 
5554
         elsif In_Open_Scopes (Scope (Bas))
5555
           or else Is_Potentially_Use_Visible (Bas)
5556
           or else In_Use (Bas)
5557
           or else (In_Use (Scope (Bas)) and then not Is_Hidden (Bas))
5558
           or else (In_Instance
5559
                     and then
5560
                       (First_Subtype (T1) = First_Subtype (Etype (R))
5561
                         or else
5562
                           (Is_Numeric_Type (T1)
5563
                             and then Is_Universal_Numeric_Type (Etype (R)))))
5564
           or else Ekind (T1) = E_Anonymous_Access_Type
5565
         then
5566
            null;
5567
 
5568
         else
5569
            --  Save candidate type for subsequent error message, if any
5570
 
5571
            if not Is_Limited_Type (T1) then
5572
               Candidate_Type := T1;
5573
            end if;
5574
 
5575
            return;
5576
         end if;
5577
 
5578
         --  Ada 2005 (AI-230): Keep restriction imposed by Ada 83 and 95:
5579
         --  Do not allow anonymous access types in equality operators.
5580
 
5581
         if Ada_Version < Ada_2005
5582
           and then Ekind (T1) = E_Anonymous_Access_Type
5583
         then
5584
            return;
5585
         end if;
5586
 
5587
         if T1 /= Standard_Void_Type
5588
           and then Has_Compatible_Type (R, T1)
5589
           and then
5590
             ((not Is_Limited_Type (T1)
5591
                and then not Is_Limited_Composite (T1))
5592
 
5593
               or else
5594
                 (Is_Array_Type (T1)
5595
                   and then not Is_Limited_Type (Component_Type (T1))
5596
                   and then Available_Full_View_Of_Component (T1)))
5597
         then
5598
            if Found
5599
              and then Base_Type (T1) /= Base_Type (T_F)
5600
            then
5601
               It := Disambiguate (L, I_F, Index, Any_Type);
5602
 
5603
               if It = No_Interp then
5604
                  Ambiguous_Operands (N);
5605
                  Set_Etype (L, Any_Type);
5606
                  return;
5607
 
5608
               else
5609
                  T_F := It.Typ;
5610
               end if;
5611
 
5612
            else
5613
               Found := True;
5614
               T_F   := T1;
5615
               I_F   := Index;
5616
            end if;
5617
 
5618
            if not Analyzed (L) then
5619
               Set_Etype (L, T_F);
5620
            end if;
5621
 
5622
            Find_Non_Universal_Interpretations (N, R, Op_Id, T1);
5623
 
5624
            --  Case of operator was not visible, Etype still set to Any_Type
5625
 
5626
            if Etype (N) = Any_Type then
5627
               Found := False;
5628
            end if;
5629
 
5630
         elsif Scop = Standard_Standard
5631
           and then Ekind (T1) = E_Anonymous_Access_Type
5632
         then
5633
            Found := True;
5634
         end if;
5635
      end Try_One_Interp;
5636
 
5637
   --  Start of processing for Find_Equality_Types
5638
 
5639
   begin
5640
      --  If left operand is aggregate, the right operand has to
5641
      --  provide a usable type for it.
5642
 
5643
      if Nkind (L) = N_Aggregate
5644
        and then Nkind (R) /= N_Aggregate
5645
      then
5646
         Find_Equality_Types (L => R, R => L, Op_Id => Op_Id, N => N);
5647
         return;
5648
      end if;
5649
 
5650
      if Nkind (N) = N_Function_Call
5651
         and then Nkind (Name (N)) = N_Expanded_Name
5652
      then
5653
         Scop := Entity (Prefix (Name (N)));
5654
 
5655
         --  The prefix may be a package renaming, and the subsequent test
5656
         --  requires the original package.
5657
 
5658
         if Ekind (Scop) = E_Package
5659
           and then Present (Renamed_Entity (Scop))
5660
         then
5661
            Scop := Renamed_Entity (Scop);
5662
            Set_Entity (Prefix (Name (N)), Scop);
5663
         end if;
5664
      end if;
5665
 
5666
      if not Is_Overloaded (L) then
5667
         Try_One_Interp (Etype (L));
5668
 
5669
      else
5670
         Get_First_Interp (L, Index, It);
5671
         while Present (It.Typ) loop
5672
            Try_One_Interp (It.Typ);
5673
            Get_Next_Interp (Index, It);
5674
         end loop;
5675
      end if;
5676
   end Find_Equality_Types;
5677
 
5678
   -------------------------
5679
   -- Find_Negation_Types --
5680
   -------------------------
5681
 
5682
   procedure Find_Negation_Types
5683
     (R     : Node_Id;
5684
      Op_Id : Entity_Id;
5685
      N     : Node_Id)
5686
   is
5687
      Index : Interp_Index;
5688
      It    : Interp;
5689
 
5690
   begin
5691
      if not Is_Overloaded (R) then
5692
         if Etype (R) = Universal_Integer then
5693
            Add_One_Interp (N, Op_Id, Any_Modular);
5694
         elsif Valid_Boolean_Arg (Etype (R)) then
5695
            Add_One_Interp (N, Op_Id, Etype (R));
5696
         end if;
5697
 
5698
      else
5699
         Get_First_Interp (R, Index, It);
5700
         while Present (It.Typ) loop
5701
            if Valid_Boolean_Arg (It.Typ) then
5702
               Add_One_Interp (N, Op_Id, It.Typ);
5703
            end if;
5704
 
5705
            Get_Next_Interp (Index, It);
5706
         end loop;
5707
      end if;
5708
   end Find_Negation_Types;
5709
 
5710
   ------------------------------
5711
   -- Find_Primitive_Operation --
5712
   ------------------------------
5713
 
5714
   function Find_Primitive_Operation (N : Node_Id) return Boolean is
5715
      Obj : constant Node_Id := Prefix (N);
5716
      Op  : constant Node_Id := Selector_Name (N);
5717
 
5718
      Prim  : Elmt_Id;
5719
      Prims : Elist_Id;
5720
      Typ   : Entity_Id;
5721
 
5722
   begin
5723
      Set_Etype (Op, Any_Type);
5724
 
5725
      if Is_Access_Type (Etype (Obj)) then
5726
         Typ := Designated_Type (Etype (Obj));
5727
      else
5728
         Typ := Etype (Obj);
5729
      end if;
5730
 
5731
      if Is_Class_Wide_Type (Typ) then
5732
         Typ := Root_Type (Typ);
5733
      end if;
5734
 
5735
      Prims := Primitive_Operations (Typ);
5736
 
5737
      Prim := First_Elmt (Prims);
5738
      while Present (Prim) loop
5739
         if Chars (Node (Prim)) = Chars (Op) then
5740
            Add_One_Interp (Op, Node (Prim), Etype (Node (Prim)));
5741
            Set_Etype (N, Etype (Node (Prim)));
5742
         end if;
5743
 
5744
         Next_Elmt (Prim);
5745
      end loop;
5746
 
5747
      --  Now look for class-wide operations of the type or any of its
5748
      --  ancestors by iterating over the homonyms of the selector.
5749
 
5750
      declare
5751
         Cls_Type : constant Entity_Id := Class_Wide_Type (Typ);
5752
         Hom      : Entity_Id;
5753
 
5754
      begin
5755
         Hom := Current_Entity (Op);
5756
         while Present (Hom) loop
5757
            if (Ekind (Hom) = E_Procedure
5758
                  or else
5759
                Ekind (Hom) = E_Function)
5760
              and then Scope (Hom) = Scope (Typ)
5761
              and then Present (First_Formal (Hom))
5762
              and then
5763
                (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
5764
                  or else
5765
                    (Is_Access_Type (Etype (First_Formal (Hom)))
5766
                       and then
5767
                         Ekind (Etype (First_Formal (Hom))) =
5768
                           E_Anonymous_Access_Type
5769
                       and then
5770
                         Base_Type
5771
                           (Designated_Type (Etype (First_Formal (Hom)))) =
5772
                                                                Cls_Type))
5773
            then
5774
               Add_One_Interp (Op, Hom, Etype (Hom));
5775
               Set_Etype (N, Etype (Hom));
5776
            end if;
5777
 
5778
            Hom := Homonym (Hom);
5779
         end loop;
5780
      end;
5781
 
5782
      return Etype (Op) /= Any_Type;
5783
   end Find_Primitive_Operation;
5784
 
5785
   ----------------------
5786
   -- Find_Unary_Types --
5787
   ----------------------
5788
 
5789
   procedure Find_Unary_Types
5790
     (R     : Node_Id;
5791
      Op_Id : Entity_Id;
5792
      N     : Node_Id)
5793
   is
5794
      Index : Interp_Index;
5795
      It    : Interp;
5796
 
5797
   begin
5798
      if not Is_Overloaded (R) then
5799
         if Is_Numeric_Type (Etype (R)) then
5800
            Add_One_Interp (N, Op_Id, Base_Type (Etype (R)));
5801
         end if;
5802
 
5803
      else
5804
         Get_First_Interp (R, Index, It);
5805
         while Present (It.Typ) loop
5806
            if Is_Numeric_Type (It.Typ) then
5807
               Add_One_Interp (N, Op_Id, Base_Type (It.Typ));
5808
            end if;
5809
 
5810
            Get_Next_Interp (Index, It);
5811
         end loop;
5812
      end if;
5813
   end Find_Unary_Types;
5814
 
5815
   ------------------
5816
   -- Junk_Operand --
5817
   ------------------
5818
 
5819
   function Junk_Operand (N : Node_Id) return Boolean is
5820
      Enode : Node_Id;
5821
 
5822
   begin
5823
      if Error_Posted (N) then
5824
         return False;
5825
      end if;
5826
 
5827
      --  Get entity to be tested
5828
 
5829
      if Is_Entity_Name (N)
5830
        and then Present (Entity (N))
5831
      then
5832
         Enode := N;
5833
 
5834
      --  An odd case, a procedure name gets converted to a very peculiar
5835
      --  function call, and here is where we detect this happening.
5836
 
5837
      elsif Nkind (N) = N_Function_Call
5838
        and then Is_Entity_Name (Name (N))
5839
        and then Present (Entity (Name (N)))
5840
      then
5841
         Enode := Name (N);
5842
 
5843
      --  Another odd case, there are at least some cases of selected
5844
      --  components where the selected component is not marked as having
5845
      --  an entity, even though the selector does have an entity
5846
 
5847
      elsif Nkind (N) = N_Selected_Component
5848
        and then Present (Entity (Selector_Name (N)))
5849
      then
5850
         Enode := Selector_Name (N);
5851
 
5852
      else
5853
         return False;
5854
      end if;
5855
 
5856
      --  Now test the entity we got to see if it is a bad case
5857
 
5858
      case Ekind (Entity (Enode)) is
5859
 
5860
         when E_Package =>
5861
            Error_Msg_N
5862
              ("package name cannot be used as operand", Enode);
5863
 
5864
         when Generic_Unit_Kind =>
5865
            Error_Msg_N
5866
              ("generic unit name cannot be used as operand", Enode);
5867
 
5868
         when Type_Kind =>
5869
            Error_Msg_N
5870
              ("subtype name cannot be used as operand", Enode);
5871
 
5872
         when Entry_Kind =>
5873
            Error_Msg_N
5874
              ("entry name cannot be used as operand", Enode);
5875
 
5876
         when E_Procedure =>
5877
            Error_Msg_N
5878
              ("procedure name cannot be used as operand", Enode);
5879
 
5880
         when E_Exception =>
5881
            Error_Msg_N
5882
              ("exception name cannot be used as operand", Enode);
5883
 
5884
         when E_Block | E_Label | E_Loop =>
5885
            Error_Msg_N
5886
              ("label name cannot be used as operand", Enode);
5887
 
5888
         when others =>
5889
            return False;
5890
 
5891
      end case;
5892
 
5893
      return True;
5894
   end Junk_Operand;
5895
 
5896
   --------------------
5897
   -- Operator_Check --
5898
   --------------------
5899
 
5900
   procedure Operator_Check (N : Node_Id) is
5901
   begin
5902
      Remove_Abstract_Operations (N);
5903
 
5904
      --  Test for case of no interpretation found for operator
5905
 
5906
      if Etype (N) = Any_Type then
5907
         declare
5908
            L     : Node_Id;
5909
            R     : Node_Id;
5910
            Op_Id : Entity_Id := Empty;
5911
 
5912
         begin
5913
            R := Right_Opnd (N);
5914
 
5915
            if Nkind (N) in N_Binary_Op then
5916
               L := Left_Opnd (N);
5917
            else
5918
               L := Empty;
5919
            end if;
5920
 
5921
            --  If either operand has no type, then don't complain further,
5922
            --  since this simply means that we have a propagated error.
5923
 
5924
            if R = Error
5925
              or else Etype (R) = Any_Type
5926
              or else (Nkind (N) in N_Binary_Op and then Etype (L) = Any_Type)
5927
            then
5928
               return;
5929
 
5930
            --  We explicitly check for the case of concatenation of component
5931
            --  with component to avoid reporting spurious matching array types
5932
            --  that might happen to be lurking in distant packages (such as
5933
            --  run-time packages). This also prevents inconsistencies in the
5934
            --  messages for certain ACVC B tests, which can vary depending on
5935
            --  types declared in run-time interfaces. Another improvement when
5936
            --  aggregates are present is to look for a well-typed operand.
5937
 
5938
            elsif Present (Candidate_Type)
5939
              and then (Nkind (N) /= N_Op_Concat
5940
                         or else Is_Array_Type (Etype (L))
5941
                         or else Is_Array_Type (Etype (R)))
5942
            then
5943
               if Nkind (N) = N_Op_Concat then
5944
                  if Etype (L) /= Any_Composite
5945
                    and then Is_Array_Type (Etype (L))
5946
                  then
5947
                     Candidate_Type := Etype (L);
5948
 
5949
                  elsif Etype (R) /= Any_Composite
5950
                    and then Is_Array_Type (Etype (R))
5951
                  then
5952
                     Candidate_Type := Etype (R);
5953
                  end if;
5954
               end if;
5955
 
5956
               Error_Msg_NE -- CODEFIX
5957
                 ("operator for} is not directly visible!",
5958
                  N, First_Subtype (Candidate_Type));
5959
 
5960
               declare
5961
                  U : constant Node_Id :=
5962
                        Cunit (Get_Source_Unit (Candidate_Type));
5963
               begin
5964
                  if Unit_Is_Visible (U) then
5965
                     Error_Msg_N -- CODEFIX
5966
                       ("use clause would make operation legal!",  N);
5967
                  else
5968
                     Error_Msg_NE  --  CODEFIX
5969
                       ("add with_clause and use_clause for&!",
5970
                          N, Defining_Entity (Unit (U)));
5971
                  end if;
5972
               end;
5973
               return;
5974
 
5975
            --  If either operand is a junk operand (e.g. package name), then
5976
            --  post appropriate error messages, but do not complain further.
5977
 
5978
            --  Note that the use of OR in this test instead of OR ELSE is
5979
            --  quite deliberate, we may as well check both operands in the
5980
            --  binary operator case.
5981
 
5982
            elsif Junk_Operand (R)
5983
              or (Nkind (N) in N_Binary_Op and then Junk_Operand (L))
5984
            then
5985
               return;
5986
 
5987
            --  If we have a logical operator, one of whose operands is
5988
            --  Boolean, then we know that the other operand cannot resolve to
5989
            --  Boolean (since we got no interpretations), but in that case we
5990
            --  pretty much know that the other operand should be Boolean, so
5991
            --  resolve it that way (generating an error)
5992
 
5993
            elsif Nkind_In (N, N_Op_And, N_Op_Or, N_Op_Xor) then
5994
               if Etype (L) = Standard_Boolean then
5995
                  Resolve (R, Standard_Boolean);
5996
                  return;
5997
               elsif Etype (R) = Standard_Boolean then
5998
                  Resolve (L, Standard_Boolean);
5999
                  return;
6000
               end if;
6001
 
6002
            --  For an arithmetic operator or comparison operator, if one
6003
            --  of the operands is numeric, then we know the other operand
6004
            --  is not the same numeric type. If it is a non-numeric type,
6005
            --  then probably it is intended to match the other operand.
6006
 
6007
            elsif Nkind_In (N, N_Op_Add,
6008
                               N_Op_Divide,
6009
                               N_Op_Ge,
6010
                               N_Op_Gt,
6011
                               N_Op_Le)
6012
              or else
6013
                  Nkind_In (N, N_Op_Lt,
6014
                               N_Op_Mod,
6015
                               N_Op_Multiply,
6016
                               N_Op_Rem,
6017
                               N_Op_Subtract)
6018
            then
6019
               if Is_Numeric_Type (Etype (L))
6020
                 and then not Is_Numeric_Type (Etype (R))
6021
               then
6022
                  Resolve (R, Etype (L));
6023
                  return;
6024
 
6025
               elsif Is_Numeric_Type (Etype (R))
6026
                 and then not Is_Numeric_Type (Etype (L))
6027
               then
6028
                  Resolve (L, Etype (R));
6029
                  return;
6030
               end if;
6031
 
6032
            --  Comparisons on A'Access are common enough to deserve a
6033
            --  special message.
6034
 
6035
            elsif Nkind_In (N, N_Op_Eq, N_Op_Ne)
6036
               and then Ekind (Etype (L)) = E_Access_Attribute_Type
6037
               and then Ekind (Etype (R)) = E_Access_Attribute_Type
6038
            then
6039
               Error_Msg_N
6040
                 ("two access attributes cannot be compared directly", N);
6041
               Error_Msg_N
6042
                 ("\use qualified expression for one of the operands",
6043
                   N);
6044
               return;
6045
 
6046
            --  Another one for C programmers
6047
 
6048
            elsif Nkind (N) = N_Op_Concat
6049
              and then Valid_Boolean_Arg (Etype (L))
6050
              and then Valid_Boolean_Arg (Etype (R))
6051
            then
6052
               Error_Msg_N ("invalid operands for concatenation", N);
6053
               Error_Msg_N -- CODEFIX
6054
                 ("\maybe AND was meant", N);
6055
               return;
6056
 
6057
            --  A special case for comparison of access parameter with null
6058
 
6059
            elsif Nkind (N) = N_Op_Eq
6060
              and then Is_Entity_Name (L)
6061
              and then Nkind (Parent (Entity (L))) = N_Parameter_Specification
6062
              and then Nkind (Parameter_Type (Parent (Entity (L)))) =
6063
                                                  N_Access_Definition
6064
              and then Nkind (R) = N_Null
6065
            then
6066
               Error_Msg_N ("access parameter is not allowed to be null", L);
6067
               Error_Msg_N ("\(call would raise Constraint_Error)", L);
6068
               return;
6069
 
6070
            --  Another special case for exponentiation, where the right
6071
            --  operand must be Natural, independently of the base.
6072
 
6073
            elsif Nkind (N) = N_Op_Expon
6074
              and then Is_Numeric_Type (Etype (L))
6075
              and then not Is_Overloaded (R)
6076
              and then
6077
                First_Subtype (Base_Type (Etype (R))) /= Standard_Integer
6078
              and then Base_Type (Etype (R)) /= Universal_Integer
6079
            then
6080
               if Ada_Version >= Ada_2012
6081
                 and then Has_Dimension_System (Etype (L))
6082
               then
6083
                  Error_Msg_NE
6084
                    ("exponent for dimensioned type must be a rational" &
6085
                     ", found}", R, Etype (R));
6086
               else
6087
                  Error_Msg_NE
6088
                    ("exponent must be of type Natural, found}", R, Etype (R));
6089
               end if;
6090
 
6091
               return;
6092
            end if;
6093
 
6094
            --  If we fall through then just give general message. Note that in
6095
            --  the following messages, if the operand is overloaded we choose
6096
            --  an arbitrary type to complain about, but that is probably more
6097
            --  useful than not giving a type at all.
6098
 
6099
            if Nkind (N) in N_Unary_Op then
6100
               Error_Msg_Node_2 := Etype (R);
6101
               Error_Msg_N ("operator& not defined for}", N);
6102
               return;
6103
 
6104
            else
6105
               if Nkind (N) in N_Binary_Op then
6106
                  if not Is_Overloaded (L)
6107
                    and then not Is_Overloaded (R)
6108
                    and then Base_Type (Etype (L)) = Base_Type (Etype (R))
6109
                  then
6110
                     Error_Msg_Node_2 := First_Subtype (Etype (R));
6111
                     Error_Msg_N ("there is no applicable operator& for}", N);
6112
 
6113
                  else
6114
                     --  Another attempt to find a fix: one of the candidate
6115
                     --  interpretations may not be use-visible. This has
6116
                     --  already been checked for predefined operators, so
6117
                     --  we examine only user-defined functions.
6118
 
6119
                     Op_Id := Get_Name_Entity_Id (Chars (N));
6120
 
6121
                     while Present (Op_Id) loop
6122
                        if Ekind (Op_Id) /= E_Operator
6123
                          and then Is_Overloadable (Op_Id)
6124
                        then
6125
                           if not Is_Immediately_Visible (Op_Id)
6126
                             and then not In_Use (Scope (Op_Id))
6127
                             and then not Is_Abstract_Subprogram (Op_Id)
6128
                             and then not Is_Hidden (Op_Id)
6129
                             and then Ekind (Scope (Op_Id)) = E_Package
6130
                             and then
6131
                               Has_Compatible_Type
6132
                                 (L, Etype (First_Formal (Op_Id)))
6133
                             and then Present
6134
                              (Next_Formal (First_Formal (Op_Id)))
6135
                             and then
6136
                               Has_Compatible_Type
6137
                                 (R,
6138
                                  Etype (Next_Formal (First_Formal (Op_Id))))
6139
                           then
6140
                              Error_Msg_N
6141
                                ("No legal interpretation for operator&", N);
6142
                              Error_Msg_NE
6143
                                ("\use clause on& would make operation legal",
6144
                                   N, Scope (Op_Id));
6145
                              exit;
6146
                           end if;
6147
                        end if;
6148
 
6149
                        Op_Id := Homonym (Op_Id);
6150
                     end loop;
6151
 
6152
                     if No (Op_Id) then
6153
                        Error_Msg_N ("invalid operand types for operator&", N);
6154
 
6155
                        if Nkind (N) /= N_Op_Concat then
6156
                           Error_Msg_NE ("\left operand has}!",  N, Etype (L));
6157
                           Error_Msg_NE ("\right operand has}!", N, Etype (R));
6158
                        end if;
6159
                     end if;
6160
                  end if;
6161
               end if;
6162
            end if;
6163
         end;
6164
      end if;
6165
   end Operator_Check;
6166
 
6167
   -----------------------------------------
6168
   -- Process_Implicit_Dereference_Prefix --
6169
   -----------------------------------------
6170
 
6171
   function Process_Implicit_Dereference_Prefix
6172
     (E : Entity_Id;
6173
      P : Entity_Id) return Entity_Id
6174
   is
6175
      Ref : Node_Id;
6176
      Typ : constant Entity_Id := Designated_Type (Etype (P));
6177
 
6178
   begin
6179
      if Present (E)
6180
        and then (Operating_Mode = Check_Semantics or else not Expander_Active)
6181
      then
6182
         --  We create a dummy reference to E to ensure that the reference
6183
         --  is not considered as part of an assignment (an implicit
6184
         --  dereference can never assign to its prefix). The Comes_From_Source
6185
         --  attribute needs to be propagated for accurate warnings.
6186
 
6187
         Ref := New_Reference_To (E, Sloc (P));
6188
         Set_Comes_From_Source (Ref, Comes_From_Source (P));
6189
         Generate_Reference (E, Ref);
6190
      end if;
6191
 
6192
      --  An implicit dereference is a legal occurrence of an
6193
      --  incomplete type imported through a limited_with clause,
6194
      --  if the full view is visible.
6195
 
6196
      if From_With_Type (Typ)
6197
        and then not From_With_Type (Scope (Typ))
6198
        and then
6199
          (Is_Immediately_Visible (Scope (Typ))
6200
            or else
6201
              (Is_Child_Unit (Scope (Typ))
6202
                 and then Is_Visible_Child_Unit (Scope (Typ))))
6203
      then
6204
         return Available_View (Typ);
6205
      else
6206
         return Typ;
6207
      end if;
6208
 
6209
   end Process_Implicit_Dereference_Prefix;
6210
 
6211
   --------------------------------
6212
   -- Remove_Abstract_Operations --
6213
   --------------------------------
6214
 
6215
   procedure Remove_Abstract_Operations (N : Node_Id) is
6216
      Abstract_Op    : Entity_Id := Empty;
6217
      Address_Kludge : Boolean := False;
6218
      I              : Interp_Index;
6219
      It             : Interp;
6220
 
6221
      --  AI-310: If overloaded, remove abstract non-dispatching operations. We
6222
      --  activate this if either extensions are enabled, or if the abstract
6223
      --  operation in question comes from a predefined file. This latter test
6224
      --  allows us to use abstract to make operations invisible to users. In
6225
      --  particular, if type Address is non-private and abstract subprograms
6226
      --  are used to hide its operators, they will be truly hidden.
6227
 
6228
      type Operand_Position is (First_Op, Second_Op);
6229
      Univ_Type : constant Entity_Id := Universal_Interpretation (N);
6230
 
6231
      procedure Remove_Address_Interpretations (Op : Operand_Position);
6232
      --  Ambiguities may arise when the operands are literal and the address
6233
      --  operations in s-auxdec are visible. In that case, remove the
6234
      --  interpretation of a literal as Address, to retain the semantics of
6235
      --  Address as a private type.
6236
 
6237
      ------------------------------------
6238
      -- Remove_Address_Interpretations --
6239
      ------------------------------------
6240
 
6241
      procedure Remove_Address_Interpretations (Op : Operand_Position) is
6242
         Formal : Entity_Id;
6243
 
6244
      begin
6245
         if Is_Overloaded (N) then
6246
            Get_First_Interp (N, I, It);
6247
            while Present (It.Nam) loop
6248
               Formal := First_Entity (It.Nam);
6249
 
6250
               if Op = Second_Op then
6251
                  Formal := Next_Entity (Formal);
6252
               end if;
6253
 
6254
               if Is_Descendent_Of_Address (Etype (Formal)) then
6255
                  Address_Kludge := True;
6256
                  Remove_Interp (I);
6257
               end if;
6258
 
6259
               Get_Next_Interp (I, It);
6260
            end loop;
6261
         end if;
6262
      end Remove_Address_Interpretations;
6263
 
6264
   --  Start of processing for Remove_Abstract_Operations
6265
 
6266
   begin
6267
      if Is_Overloaded (N) then
6268
         if Debug_Flag_V then
6269
            Write_Str ("Remove_Abstract_Operations: ");
6270
            Write_Overloads (N);
6271
         end if;
6272
 
6273
         Get_First_Interp (N, I, It);
6274
 
6275
         while Present (It.Nam) loop
6276
            if Is_Overloadable (It.Nam)
6277
              and then Is_Abstract_Subprogram (It.Nam)
6278
              and then not Is_Dispatching_Operation (It.Nam)
6279
            then
6280
               Abstract_Op := It.Nam;
6281
 
6282
               if Is_Descendent_Of_Address (It.Typ) then
6283
                  Address_Kludge := True;
6284
                  Remove_Interp (I);
6285
                  exit;
6286
 
6287
               --  In Ada 2005, this operation does not participate in overload
6288
               --  resolution. If the operation is defined in a predefined
6289
               --  unit, it is one of the operations declared abstract in some
6290
               --  variants of System, and it must be removed as well.
6291
 
6292
               elsif Ada_Version >= Ada_2005
6293
                 or else Is_Predefined_File_Name
6294
                           (Unit_File_Name (Get_Source_Unit (It.Nam)))
6295
               then
6296
                  Remove_Interp (I);
6297
                  exit;
6298
               end if;
6299
            end if;
6300
 
6301
            Get_Next_Interp (I, It);
6302
         end loop;
6303
 
6304
         if No (Abstract_Op) then
6305
 
6306
            --  If some interpretation yields an integer type, it is still
6307
            --  possible that there are address interpretations. Remove them
6308
            --  if one operand is a literal, to avoid spurious ambiguities
6309
            --  on systems where Address is a visible integer type.
6310
 
6311
            if Is_Overloaded (N)
6312
              and then Nkind (N) in N_Op
6313
              and then Is_Integer_Type (Etype (N))
6314
            then
6315
               if Nkind (N) in N_Binary_Op then
6316
                  if Nkind (Right_Opnd (N)) = N_Integer_Literal then
6317
                     Remove_Address_Interpretations (Second_Op);
6318
 
6319
                  elsif Nkind (Right_Opnd (N)) = N_Integer_Literal then
6320
                     Remove_Address_Interpretations (First_Op);
6321
                  end if;
6322
               end if;
6323
            end if;
6324
 
6325
         elsif Nkind (N) in N_Op then
6326
 
6327
            --  Remove interpretations that treat literals as addresses. This
6328
            --  is never appropriate, even when Address is defined as a visible
6329
            --  Integer type. The reason is that we would really prefer Address
6330
            --  to behave as a private type, even in this case, which is there
6331
            --  only to accommodate oddities of VMS address sizes. If Address
6332
            --  is a visible integer type, we get lots of overload ambiguities.
6333
 
6334
            if Nkind (N) in N_Binary_Op then
6335
               declare
6336
                  U1 : constant Boolean :=
6337
                     Present (Universal_Interpretation (Right_Opnd (N)));
6338
                  U2 : constant Boolean :=
6339
                     Present (Universal_Interpretation (Left_Opnd (N)));
6340
 
6341
               begin
6342
                  if U1 then
6343
                     Remove_Address_Interpretations (Second_Op);
6344
                  end if;
6345
 
6346
                  if U2 then
6347
                     Remove_Address_Interpretations (First_Op);
6348
                  end if;
6349
 
6350
                  if not (U1 and U2) then
6351
 
6352
                     --  Remove corresponding predefined operator, which is
6353
                     --  always added to the overload set.
6354
 
6355
                     Get_First_Interp (N, I, It);
6356
                     while Present (It.Nam) loop
6357
                        if Scope (It.Nam) = Standard_Standard
6358
                          and then Base_Type (It.Typ) =
6359
                                   Base_Type (Etype (Abstract_Op))
6360
                        then
6361
                           Remove_Interp (I);
6362
                        end if;
6363
 
6364
                        Get_Next_Interp (I, It);
6365
                     end loop;
6366
 
6367
                  elsif Is_Overloaded (N)
6368
                    and then Present (Univ_Type)
6369
                  then
6370
                     --  If both operands have a universal interpretation,
6371
                     --  it is still necessary to remove interpretations that
6372
                     --  yield Address. Any remaining ambiguities will be
6373
                     --  removed in Disambiguate.
6374
 
6375
                     Get_First_Interp (N, I, It);
6376
                     while Present (It.Nam) loop
6377
                        if Is_Descendent_Of_Address (It.Typ) then
6378
                           Remove_Interp (I);
6379
 
6380
                        elsif not Is_Type (It.Nam) then
6381
                           Set_Entity (N, It.Nam);
6382
                        end if;
6383
 
6384
                        Get_Next_Interp (I, It);
6385
                     end loop;
6386
                  end if;
6387
               end;
6388
            end if;
6389
 
6390
         elsif Nkind (N) = N_Function_Call
6391
           and then
6392
             (Nkind (Name (N)) = N_Operator_Symbol
6393
                or else
6394
                  (Nkind (Name (N)) = N_Expanded_Name
6395
                     and then
6396
                       Nkind (Selector_Name (Name (N))) = N_Operator_Symbol))
6397
         then
6398
 
6399
            declare
6400
               Arg1 : constant Node_Id := First (Parameter_Associations (N));
6401
               U1   : constant Boolean :=
6402
                        Present (Universal_Interpretation (Arg1));
6403
               U2   : constant Boolean :=
6404
                        Present (Next (Arg1)) and then
6405
                        Present (Universal_Interpretation (Next (Arg1)));
6406
 
6407
            begin
6408
               if U1 then
6409
                  Remove_Address_Interpretations (First_Op);
6410
               end if;
6411
 
6412
               if U2 then
6413
                  Remove_Address_Interpretations (Second_Op);
6414
               end if;
6415
 
6416
               if not (U1 and U2) then
6417
                  Get_First_Interp (N, I, It);
6418
                  while Present (It.Nam) loop
6419
                     if Scope (It.Nam) = Standard_Standard
6420
                       and then It.Typ = Base_Type (Etype (Abstract_Op))
6421
                     then
6422
                        Remove_Interp (I);
6423
                     end if;
6424
 
6425
                     Get_Next_Interp (I, It);
6426
                  end loop;
6427
               end if;
6428
            end;
6429
         end if;
6430
 
6431
         --  If the removal has left no valid interpretations, emit an error
6432
         --  message now and label node as illegal.
6433
 
6434
         if Present (Abstract_Op) then
6435
            Get_First_Interp (N, I, It);
6436
 
6437
            if No (It.Nam) then
6438
 
6439
               --  Removal of abstract operation left no viable candidate
6440
 
6441
               Set_Etype (N, Any_Type);
6442
               Error_Msg_Sloc := Sloc (Abstract_Op);
6443
               Error_Msg_NE
6444
                 ("cannot call abstract operation& declared#", N, Abstract_Op);
6445
 
6446
            --  In Ada 2005, an abstract operation may disable predefined
6447
            --  operators. Since the context is not yet known, we mark the
6448
            --  predefined operators as potentially hidden. Do not include
6449
            --  predefined operators when addresses are involved since this
6450
            --  case is handled separately.
6451
 
6452
            elsif Ada_Version >= Ada_2005
6453
              and then not Address_Kludge
6454
            then
6455
               while Present (It.Nam) loop
6456
                  if Is_Numeric_Type (It.Typ)
6457
                    and then Scope (It.Typ) = Standard_Standard
6458
                  then
6459
                     Set_Abstract_Op (I, Abstract_Op);
6460
                  end if;
6461
 
6462
                  Get_Next_Interp (I, It);
6463
               end loop;
6464
            end if;
6465
         end if;
6466
 
6467
         if Debug_Flag_V then
6468
            Write_Str ("Remove_Abstract_Operations done: ");
6469
            Write_Overloads (N);
6470
         end if;
6471
      end if;
6472
   end Remove_Abstract_Operations;
6473
 
6474
   ----------------------------
6475
   -- Try_Container_Indexing --
6476
   ----------------------------
6477
 
6478
   function Try_Container_Indexing
6479
     (N      : Node_Id;
6480
      Prefix : Node_Id;
6481
      Expr   : Node_Id) return Boolean
6482
   is
6483
      Loc       : constant Source_Ptr := Sloc (N);
6484
      Disc      : Entity_Id;
6485
      Func      : Entity_Id;
6486
      Func_Name : Node_Id;
6487
      Indexing  : Node_Id;
6488
 
6489
   begin
6490
 
6491
      --  Check whether type has a specified indexing aspect
6492
 
6493
      Func_Name := Empty;
6494
 
6495
      if Is_Variable (Prefix) then
6496
         Func_Name := Find_Aspect (Etype (Prefix), Aspect_Variable_Indexing);
6497
      end if;
6498
 
6499
      if No (Func_Name) then
6500
         Func_Name := Find_Aspect (Etype (Prefix), Aspect_Constant_Indexing);
6501
      end if;
6502
 
6503
      --  If aspect does not exist the expression is illegal. Error is
6504
      --  diagnosed in caller.
6505
 
6506
      if No (Func_Name) then
6507
 
6508
         --  The prefix itself may be an indexing of a container
6509
         --  rewrite as such and re-analyze.
6510
 
6511
         if Has_Implicit_Dereference (Etype (Prefix)) then
6512
            Build_Explicit_Dereference
6513
              (Prefix, First_Discriminant (Etype (Prefix)));
6514
            return Try_Container_Indexing (N, Prefix, Expr);
6515
 
6516
         else
6517
            return False;
6518
         end if;
6519
      end if;
6520
 
6521
      if not Is_Overloaded (Func_Name) then
6522
         Func := Entity (Func_Name);
6523
         Indexing := Make_Function_Call (Loc,
6524
           Name => New_Occurrence_Of (Func, Loc),
6525
           Parameter_Associations =>
6526
             New_List (Relocate_Node (Prefix), Relocate_Node (Expr)));
6527
         Rewrite (N, Indexing);
6528
         Analyze (N);
6529
 
6530
         --  If the return type of the indexing function is a reference type,
6531
         --  add the dereference as a possible interpretation. Note that the
6532
         --  indexing aspect may be a function that returns the element type
6533
         --  with no intervening implicit dereference.
6534
 
6535
         if Has_Discriminants (Etype (Func)) then
6536
            Disc := First_Discriminant (Etype (Func));
6537
            while Present (Disc) loop
6538
               if Has_Implicit_Dereference (Disc) then
6539
                  Add_One_Interp (N, Disc, Designated_Type (Etype (Disc)));
6540
                  exit;
6541
               end if;
6542
 
6543
               Next_Discriminant (Disc);
6544
            end loop;
6545
         end if;
6546
 
6547
      else
6548
         Indexing := Make_Function_Call (Loc,
6549
           Name => Make_Identifier (Loc, Chars (Func_Name)),
6550
           Parameter_Associations =>
6551
             New_List (Relocate_Node (Prefix), Relocate_Node (Expr)));
6552
 
6553
         Rewrite (N, Indexing);
6554
 
6555
         declare
6556
            I  : Interp_Index;
6557
            It : Interp;
6558
            Success : Boolean;
6559
 
6560
         begin
6561
            Get_First_Interp (Func_Name, I, It);
6562
            Set_Etype (N, Any_Type);
6563
            while Present (It.Nam) loop
6564
               Analyze_One_Call (N, It.Nam, False, Success);
6565
               if Success then
6566
                  Set_Etype (Name (N), It.Typ);
6567
                  Set_Entity (Name (N), It.Nam);
6568
 
6569
                  --  Add implicit dereference interpretation
6570
 
6571
                  if Has_Discriminants (Etype (It.Nam)) then
6572
                     Disc := First_Discriminant (Etype (It.Nam));
6573
                     while Present (Disc) loop
6574
                        if Has_Implicit_Dereference (Disc) then
6575
                           Add_One_Interp
6576
                             (N, Disc, Designated_Type (Etype (Disc)));
6577
                           exit;
6578
                        end if;
6579
 
6580
                        Next_Discriminant (Disc);
6581
                     end loop;
6582
                  end if;
6583
 
6584
                  exit;
6585
               end if;
6586
               Get_Next_Interp (I, It);
6587
            end loop;
6588
         end;
6589
      end if;
6590
 
6591
      if Etype (N) = Any_Type then
6592
         Error_Msg_NE ("container cannot be indexed with&", N, Etype (Expr));
6593
         Rewrite (N, New_Occurrence_Of (Any_Id, Loc));
6594
      else
6595
         Analyze (N);
6596
      end if;
6597
 
6598
      return True;
6599
   end Try_Container_Indexing;
6600
 
6601
   -----------------------
6602
   -- Try_Indirect_Call --
6603
   -----------------------
6604
 
6605
   function Try_Indirect_Call
6606
     (N   : Node_Id;
6607
      Nam : Entity_Id;
6608
      Typ : Entity_Id) return Boolean
6609
   is
6610
      Actual : Node_Id;
6611
      Formal : Entity_Id;
6612
 
6613
      Call_OK : Boolean;
6614
      pragma Warnings (Off, Call_OK);
6615
 
6616
   begin
6617
      Normalize_Actuals (N, Designated_Type (Typ), False, Call_OK);
6618
 
6619
      Actual := First_Actual (N);
6620
      Formal := First_Formal (Designated_Type (Typ));
6621
      while Present (Actual) and then Present (Formal) loop
6622
         if not Has_Compatible_Type (Actual, Etype (Formal)) then
6623
            return False;
6624
         end if;
6625
 
6626
         Next (Actual);
6627
         Next_Formal (Formal);
6628
      end loop;
6629
 
6630
      if No (Actual) and then No (Formal) then
6631
         Add_One_Interp (N, Nam, Etype (Designated_Type (Typ)));
6632
 
6633
         --  Nam is a candidate interpretation for the name in the call,
6634
         --  if it is not an indirect call.
6635
 
6636
         if not Is_Type (Nam)
6637
            and then Is_Entity_Name (Name (N))
6638
         then
6639
            Set_Entity (Name (N), Nam);
6640
         end if;
6641
 
6642
         return True;
6643
      else
6644
         return False;
6645
      end if;
6646
   end Try_Indirect_Call;
6647
 
6648
   ----------------------
6649
   -- Try_Indexed_Call --
6650
   ----------------------
6651
 
6652
   function Try_Indexed_Call
6653
     (N          : Node_Id;
6654
      Nam        : Entity_Id;
6655
      Typ        : Entity_Id;
6656
      Skip_First : Boolean) return Boolean
6657
   is
6658
      Loc     : constant Source_Ptr := Sloc (N);
6659
      Actuals : constant List_Id    := Parameter_Associations (N);
6660
      Actual  : Node_Id;
6661
      Index   : Entity_Id;
6662
 
6663
   begin
6664
      Actual := First (Actuals);
6665
 
6666
      --  If the call was originally written in prefix form, skip the first
6667
      --  actual, which is obviously not defaulted.
6668
 
6669
      if Skip_First then
6670
         Next (Actual);
6671
      end if;
6672
 
6673
      Index := First_Index (Typ);
6674
      while Present (Actual) and then Present (Index) loop
6675
 
6676
         --  If the parameter list has a named association, the expression
6677
         --  is definitely a call and not an indexed component.
6678
 
6679
         if Nkind (Actual) = N_Parameter_Association then
6680
            return False;
6681
         end if;
6682
 
6683
         if Is_Entity_Name (Actual)
6684
           and then Is_Type (Entity (Actual))
6685
           and then No (Next (Actual))
6686
         then
6687
            --  A single actual that is a type name indicates a slice if the
6688
            --  type is discrete, and an error otherwise.
6689
 
6690
            if Is_Discrete_Type (Entity (Actual)) then
6691
               Rewrite (N,
6692
                 Make_Slice (Loc,
6693
                   Prefix =>
6694
                     Make_Function_Call (Loc,
6695
                       Name => Relocate_Node (Name (N))),
6696
                   Discrete_Range =>
6697
                     New_Occurrence_Of (Entity (Actual), Sloc (Actual))));
6698
 
6699
               Analyze (N);
6700
 
6701
            else
6702
               Error_Msg_N ("invalid use of type in expression", Actual);
6703
               Set_Etype (N, Any_Type);
6704
            end if;
6705
 
6706
            return True;
6707
 
6708
         elsif not Has_Compatible_Type (Actual, Etype (Index)) then
6709
            return False;
6710
         end if;
6711
 
6712
         Next (Actual);
6713
         Next_Index (Index);
6714
      end loop;
6715
 
6716
      if No (Actual) and then No (Index) then
6717
         Add_One_Interp (N, Nam, Component_Type (Typ));
6718
 
6719
         --  Nam is a candidate interpretation for the name in the call,
6720
         --  if it is not an indirect call.
6721
 
6722
         if not Is_Type (Nam)
6723
            and then Is_Entity_Name (Name (N))
6724
         then
6725
            Set_Entity (Name (N), Nam);
6726
         end if;
6727
 
6728
         return True;
6729
      else
6730
         return False;
6731
      end if;
6732
   end Try_Indexed_Call;
6733
 
6734
   --------------------------
6735
   -- Try_Object_Operation --
6736
   --------------------------
6737
 
6738
   function Try_Object_Operation
6739
     (N : Node_Id; CW_Test_Only : Boolean := False) return Boolean
6740
   is
6741
      K              : constant Node_Kind  := Nkind (Parent (N));
6742
      Is_Subprg_Call : constant Boolean    := Nkind_In
6743
                                               (K, N_Procedure_Call_Statement,
6744
                                                   N_Function_Call);
6745
      Loc            : constant Source_Ptr := Sloc (N);
6746
      Obj            : constant Node_Id    := Prefix (N);
6747
 
6748
      Subprog : constant Node_Id    :=
6749
                  Make_Identifier (Sloc (Selector_Name (N)),
6750
                    Chars => Chars (Selector_Name (N)));
6751
      --  Identifier on which possible interpretations will be collected
6752
 
6753
      Report_Error : Boolean := False;
6754
      --  If no candidate interpretation matches the context, redo the
6755
      --  analysis with error enabled to provide additional information.
6756
 
6757
      Actual          : Node_Id;
6758
      Candidate       : Entity_Id := Empty;
6759
      New_Call_Node   : Node_Id := Empty;
6760
      Node_To_Replace : Node_Id;
6761
      Obj_Type        : Entity_Id := Etype (Obj);
6762
      Success         : Boolean := False;
6763
 
6764
      function Valid_Candidate
6765
        (Success : Boolean;
6766
         Call    : Node_Id;
6767
         Subp    : Entity_Id) return Entity_Id;
6768
      --  If the subprogram is a valid interpretation, record it, and add
6769
      --  to the list of interpretations of Subprog. Otherwise return Empty.
6770
 
6771
      procedure Complete_Object_Operation
6772
        (Call_Node       : Node_Id;
6773
         Node_To_Replace : Node_Id);
6774
      --  Make Subprog the name of Call_Node, replace Node_To_Replace with
6775
      --  Call_Node, insert the object (or its dereference) as the first actual
6776
      --  in the call, and complete the analysis of the call.
6777
 
6778
      procedure Report_Ambiguity (Op : Entity_Id);
6779
      --  If a prefixed procedure call is ambiguous, indicate whether the
6780
      --  call includes an implicit dereference or an implicit 'Access.
6781
 
6782
      procedure Transform_Object_Operation
6783
        (Call_Node       : out Node_Id;
6784
         Node_To_Replace : out Node_Id);
6785
      --  Transform Obj.Operation (X, Y,,) into Operation (Obj, X, Y ..)
6786
      --  Call_Node is the resulting subprogram call, Node_To_Replace is
6787
      --  either N or the parent of N, and Subprog is a reference to the
6788
      --  subprogram we are trying to match.
6789
 
6790
      function Try_Class_Wide_Operation
6791
        (Call_Node       : Node_Id;
6792
         Node_To_Replace : Node_Id) return Boolean;
6793
      --  Traverse all ancestor types looking for a class-wide subprogram
6794
      --  for which the current operation is a valid non-dispatching call.
6795
 
6796
      procedure Try_One_Prefix_Interpretation (T : Entity_Id);
6797
      --  If prefix is overloaded, its interpretation may include different
6798
      --  tagged types, and we must examine the primitive operations and
6799
      --  the class-wide operations of each in order to find candidate
6800
      --  interpretations for the call as a whole.
6801
 
6802
      function Try_Primitive_Operation
6803
        (Call_Node       : Node_Id;
6804
         Node_To_Replace : Node_Id) return Boolean;
6805
      --  Traverse the list of primitive subprograms looking for a dispatching
6806
      --  operation for which the current node is a valid call .
6807
 
6808
      ---------------------
6809
      -- Valid_Candidate --
6810
      ---------------------
6811
 
6812
      function Valid_Candidate
6813
        (Success : Boolean;
6814
         Call    : Node_Id;
6815
         Subp    : Entity_Id) return Entity_Id
6816
      is
6817
         Arr_Type  : Entity_Id;
6818
         Comp_Type : Entity_Id;
6819
 
6820
      begin
6821
         --  If the subprogram is a valid interpretation, record it in global
6822
         --  variable Subprog, to collect all possible overloadings.
6823
 
6824
         if Success then
6825
            if Subp /= Entity (Subprog) then
6826
               Add_One_Interp (Subprog, Subp, Etype (Subp));
6827
            end if;
6828
         end if;
6829
 
6830
         --  If the call may be an indexed call, retrieve component type of
6831
         --  resulting expression, and add possible interpretation.
6832
 
6833
         Arr_Type  := Empty;
6834
         Comp_Type := Empty;
6835
 
6836
         if Nkind (Call) = N_Function_Call
6837
           and then Nkind (Parent (N)) = N_Indexed_Component
6838
           and then Needs_One_Actual (Subp)
6839
         then
6840
            if Is_Array_Type (Etype (Subp)) then
6841
               Arr_Type := Etype (Subp);
6842
 
6843
            elsif Is_Access_Type (Etype (Subp))
6844
              and then Is_Array_Type (Designated_Type (Etype (Subp)))
6845
            then
6846
               Arr_Type := Designated_Type (Etype (Subp));
6847
            end if;
6848
         end if;
6849
 
6850
         if Present (Arr_Type) then
6851
 
6852
            --  Verify that the actuals (excluding the object) match the types
6853
            --  of the indexes.
6854
 
6855
            declare
6856
               Actual : Node_Id;
6857
               Index  : Node_Id;
6858
 
6859
            begin
6860
               Actual := Next (First_Actual (Call));
6861
               Index  := First_Index (Arr_Type);
6862
               while Present (Actual) and then Present (Index) loop
6863
                  if not Has_Compatible_Type (Actual, Etype (Index)) then
6864
                     Arr_Type := Empty;
6865
                     exit;
6866
                  end if;
6867
 
6868
                  Next_Actual (Actual);
6869
                  Next_Index  (Index);
6870
               end loop;
6871
 
6872
               if No (Actual)
6873
                  and then No (Index)
6874
                  and then Present (Arr_Type)
6875
               then
6876
                  Comp_Type := Component_Type (Arr_Type);
6877
               end if;
6878
            end;
6879
 
6880
            if Present (Comp_Type)
6881
              and then Etype (Subprog) /= Comp_Type
6882
            then
6883
               Add_One_Interp (Subprog, Subp, Comp_Type);
6884
            end if;
6885
         end if;
6886
 
6887
         if Etype (Call) /= Any_Type then
6888
            return Subp;
6889
         else
6890
            return Empty;
6891
         end if;
6892
      end Valid_Candidate;
6893
 
6894
      -------------------------------
6895
      -- Complete_Object_Operation --
6896
      -------------------------------
6897
 
6898
      procedure Complete_Object_Operation
6899
        (Call_Node       : Node_Id;
6900
         Node_To_Replace : Node_Id)
6901
      is
6902
         Control      : constant Entity_Id := First_Formal (Entity (Subprog));
6903
         Formal_Type  : constant Entity_Id := Etype (Control);
6904
         First_Actual : Node_Id;
6905
 
6906
      begin
6907
         --  Place the name of the operation, with its interpretations,
6908
         --  on the rewritten call.
6909
 
6910
         Set_Name (Call_Node, Subprog);
6911
 
6912
         First_Actual := First (Parameter_Associations (Call_Node));
6913
 
6914
         --  For cross-reference purposes, treat the new node as being in
6915
         --  the source if the original one is. Set entity and type, even
6916
         --  though they may be overwritten during resolution if overloaded.
6917
 
6918
         Set_Comes_From_Source (Subprog, Comes_From_Source (N));
6919
         Set_Comes_From_Source (Call_Node, Comes_From_Source (N));
6920
 
6921
         if Nkind (N) = N_Selected_Component
6922
           and then not Inside_A_Generic
6923
         then
6924
            Set_Entity (Selector_Name (N), Entity (Subprog));
6925
            Set_Etype  (Selector_Name (N), Etype (Entity (Subprog)));
6926
         end if;
6927
 
6928
         --  If need be, rewrite first actual as an explicit dereference
6929
         --  If the call is overloaded, the rewriting can only be done
6930
         --  once the primitive operation is identified.
6931
 
6932
         if Is_Overloaded (Subprog) then
6933
 
6934
            --  The prefix itself may be overloaded, and its interpretations
6935
            --  must be propagated to the new actual in the call.
6936
 
6937
            if Is_Overloaded (Obj) then
6938
               Save_Interps (Obj, First_Actual);
6939
            end if;
6940
 
6941
            Rewrite (First_Actual, Obj);
6942
 
6943
         elsif not Is_Access_Type (Formal_Type)
6944
           and then Is_Access_Type (Etype (Obj))
6945
         then
6946
            Rewrite (First_Actual,
6947
              Make_Explicit_Dereference (Sloc (Obj), Obj));
6948
            Analyze (First_Actual);
6949
 
6950
            --  If we need to introduce an explicit dereference, verify that
6951
            --  the resulting actual is compatible with the mode of the formal.
6952
 
6953
            if Ekind (First_Formal (Entity (Subprog))) /= E_In_Parameter
6954
              and then Is_Access_Constant (Etype (Obj))
6955
            then
6956
               Error_Msg_NE
6957
                 ("expect variable in call to&", Prefix (N), Entity (Subprog));
6958
            end if;
6959
 
6960
         --  Conversely, if the formal is an access parameter and the object
6961
         --  is not, replace the actual with a 'Access reference. Its analysis
6962
         --  will check that the object is aliased.
6963
 
6964
         elsif Is_Access_Type (Formal_Type)
6965
           and then not Is_Access_Type (Etype (Obj))
6966
         then
6967
            --  A special case: A.all'access is illegal if A is an access to a
6968
            --  constant and the context requires an access to a variable.
6969
 
6970
            if not Is_Access_Constant (Formal_Type) then
6971
               if (Nkind (Obj) = N_Explicit_Dereference
6972
                    and then Is_Access_Constant (Etype (Prefix (Obj))))
6973
                 or else not Is_Variable (Obj)
6974
               then
6975
                  Error_Msg_NE
6976
                    ("actual for& must be a variable", Obj, Control);
6977
               end if;
6978
            end if;
6979
 
6980
            Rewrite (First_Actual,
6981
              Make_Attribute_Reference (Loc,
6982
                Attribute_Name => Name_Access,
6983
                Prefix => Relocate_Node (Obj)));
6984
 
6985
            if not Is_Aliased_View (Obj) then
6986
               Error_Msg_NE
6987
                 ("object in prefixed call to& must be aliased"
6988
                      & " (RM-2005 4.3.1 (13))",
6989
                 Prefix (First_Actual), Subprog);
6990
            end if;
6991
 
6992
            Analyze (First_Actual);
6993
 
6994
         else
6995
            if Is_Overloaded (Obj) then
6996
               Save_Interps (Obj, First_Actual);
6997
            end if;
6998
 
6999
            Rewrite (First_Actual, Obj);
7000
         end if;
7001
 
7002
         Rewrite (Node_To_Replace, Call_Node);
7003
 
7004
         --  Propagate the interpretations collected in subprog to the new
7005
         --  function call node, to be resolved from context.
7006
 
7007
         if Is_Overloaded (Subprog) then
7008
            Save_Interps (Subprog, Node_To_Replace);
7009
 
7010
         else
7011
            Analyze (Node_To_Replace);
7012
 
7013
            --  If the operation has been rewritten into a call, which may get
7014
            --  subsequently an explicit dereference, preserve the type on the
7015
            --  original node (selected component or indexed component) for
7016
            --  subsequent legality tests, e.g. Is_Variable. which examines
7017
            --  the original node.
7018
 
7019
            if Nkind (Node_To_Replace) = N_Function_Call then
7020
               Set_Etype
7021
                 (Original_Node (Node_To_Replace), Etype (Node_To_Replace));
7022
            end if;
7023
         end if;
7024
      end Complete_Object_Operation;
7025
 
7026
      ----------------------
7027
      -- Report_Ambiguity --
7028
      ----------------------
7029
 
7030
      procedure Report_Ambiguity (Op : Entity_Id) is
7031
         Access_Actual : constant Boolean :=
7032
                           Is_Access_Type (Etype (Prefix (N)));
7033
         Access_Formal : Boolean := False;
7034
 
7035
      begin
7036
         Error_Msg_Sloc := Sloc (Op);
7037
 
7038
         if Present (First_Formal (Op)) then
7039
            Access_Formal := Is_Access_Type (Etype (First_Formal (Op)));
7040
         end if;
7041
 
7042
         if Access_Formal and then not Access_Actual then
7043
            if Nkind (Parent (Op)) = N_Full_Type_Declaration then
7044
               Error_Msg_N
7045
                 ("\possible interpretation"
7046
                   & " (inherited, with implicit 'Access) #", N);
7047
            else
7048
               Error_Msg_N
7049
                 ("\possible interpretation (with implicit 'Access) #", N);
7050
            end if;
7051
 
7052
         elsif not Access_Formal and then Access_Actual then
7053
            if Nkind (Parent (Op)) = N_Full_Type_Declaration then
7054
               Error_Msg_N
7055
                 ("\possible interpretation"
7056
                   & " ( inherited, with implicit dereference) #", N);
7057
            else
7058
               Error_Msg_N
7059
                 ("\possible interpretation (with implicit dereference) #", N);
7060
            end if;
7061
 
7062
         else
7063
            if Nkind (Parent (Op)) = N_Full_Type_Declaration then
7064
               Error_Msg_N ("\possible interpretation (inherited)#", N);
7065
            else
7066
               Error_Msg_N -- CODEFIX
7067
                 ("\possible interpretation#", N);
7068
            end if;
7069
         end if;
7070
      end Report_Ambiguity;
7071
 
7072
      --------------------------------
7073
      -- Transform_Object_Operation --
7074
      --------------------------------
7075
 
7076
      procedure Transform_Object_Operation
7077
        (Call_Node       : out Node_Id;
7078
         Node_To_Replace : out Node_Id)
7079
      is
7080
         Dummy : constant Node_Id := New_Copy (Obj);
7081
         --  Placeholder used as a first parameter in the call, replaced
7082
         --  eventually by the proper object.
7083
 
7084
         Parent_Node : constant Node_Id := Parent (N);
7085
 
7086
         Actual  : Node_Id;
7087
         Actuals : List_Id;
7088
 
7089
      begin
7090
         --  Common case covering 1) Call to a procedure and 2) Call to a
7091
         --  function that has some additional actuals.
7092
 
7093
         if Nkind_In (Parent_Node, N_Function_Call,
7094
                                   N_Procedure_Call_Statement)
7095
 
7096
            --  N is a selected component node containing the name of the
7097
            --  subprogram. If N is not the name of the parent node we must
7098
            --  not replace the parent node by the new construct. This case
7099
            --  occurs when N is a parameterless call to a subprogram that
7100
            --  is an actual parameter of a call to another subprogram. For
7101
            --  example:
7102
            --            Some_Subprogram (..., Obj.Operation, ...)
7103
 
7104
            and then Name (Parent_Node) = N
7105
         then
7106
            Node_To_Replace := Parent_Node;
7107
 
7108
            Actuals := Parameter_Associations (Parent_Node);
7109
 
7110
            if Present (Actuals) then
7111
               Prepend (Dummy, Actuals);
7112
            else
7113
               Actuals := New_List (Dummy);
7114
            end if;
7115
 
7116
            if Nkind (Parent_Node) = N_Procedure_Call_Statement then
7117
               Call_Node :=
7118
                 Make_Procedure_Call_Statement (Loc,
7119
                   Name => New_Copy (Subprog),
7120
                   Parameter_Associations => Actuals);
7121
 
7122
            else
7123
               Call_Node :=
7124
                 Make_Function_Call (Loc,
7125
                   Name => New_Copy (Subprog),
7126
                   Parameter_Associations => Actuals);
7127
 
7128
            end if;
7129
 
7130
         --  Before analysis, a function call appears as an indexed component
7131
         --  if there are no named associations.
7132
 
7133
         elsif Nkind (Parent_Node) =  N_Indexed_Component
7134
           and then N = Prefix (Parent_Node)
7135
         then
7136
            Node_To_Replace := Parent_Node;
7137
            Actuals := Expressions (Parent_Node);
7138
 
7139
            Actual := First (Actuals);
7140
            while Present (Actual) loop
7141
               Analyze (Actual);
7142
               Next (Actual);
7143
            end loop;
7144
 
7145
            Prepend (Dummy, Actuals);
7146
 
7147
            Call_Node :=
7148
               Make_Function_Call (Loc,
7149
                 Name => New_Copy (Subprog),
7150
                 Parameter_Associations => Actuals);
7151
 
7152
         --  Parameterless call: Obj.F is rewritten as F (Obj)
7153
 
7154
         else
7155
            Node_To_Replace := N;
7156
 
7157
            Call_Node :=
7158
               Make_Function_Call (Loc,
7159
                 Name => New_Copy (Subprog),
7160
                 Parameter_Associations => New_List (Dummy));
7161
         end if;
7162
      end Transform_Object_Operation;
7163
 
7164
      ------------------------------
7165
      -- Try_Class_Wide_Operation --
7166
      ------------------------------
7167
 
7168
      function Try_Class_Wide_Operation
7169
        (Call_Node       : Node_Id;
7170
         Node_To_Replace : Node_Id) return Boolean
7171
      is
7172
         Anc_Type    : Entity_Id;
7173
         Matching_Op : Entity_Id := Empty;
7174
         Error       : Boolean;
7175
 
7176
         procedure Traverse_Homonyms
7177
           (Anc_Type : Entity_Id;
7178
            Error    : out Boolean);
7179
         --  Traverse the homonym chain of the subprogram searching for those
7180
         --  homonyms whose first formal has the Anc_Type's class-wide type,
7181
         --  or an anonymous access type designating the class-wide type. If
7182
         --  an ambiguity is detected, then Error is set to True.
7183
 
7184
         procedure Traverse_Interfaces
7185
           (Anc_Type : Entity_Id;
7186
            Error    : out Boolean);
7187
         --  Traverse the list of interfaces, if any, associated with Anc_Type
7188
         --  and search for acceptable class-wide homonyms associated with each
7189
         --  interface. If an ambiguity is detected, then Error is set to True.
7190
 
7191
         -----------------------
7192
         -- Traverse_Homonyms --
7193
         -----------------------
7194
 
7195
         procedure Traverse_Homonyms
7196
           (Anc_Type : Entity_Id;
7197
            Error    : out Boolean)
7198
         is
7199
            Cls_Type    : Entity_Id;
7200
            Hom         : Entity_Id;
7201
            Hom_Ref     : Node_Id;
7202
            Success     : Boolean;
7203
 
7204
         begin
7205
            Error := False;
7206
 
7207
            Cls_Type := Class_Wide_Type (Anc_Type);
7208
 
7209
            Hom := Current_Entity (Subprog);
7210
 
7211
            --  Find a non-hidden operation whose first parameter is of the
7212
            --  class-wide type, a subtype thereof, or an anonymous access
7213
            --  to same. If in an instance, the operation can be considered
7214
            --  even if hidden (it may be hidden because the instantiation is
7215
            --  expanded after the containing package has been analyzed).
7216
 
7217
            while Present (Hom) loop
7218
               if Ekind_In (Hom, E_Procedure, E_Function)
7219
                 and then (not Is_Hidden (Hom) or else In_Instance)
7220
                 and then Scope (Hom) = Scope (Anc_Type)
7221
                 and then Present (First_Formal (Hom))
7222
                 and then
7223
                   (Base_Type (Etype (First_Formal (Hom))) = Cls_Type
7224
                     or else
7225
                       (Is_Access_Type (Etype (First_Formal (Hom)))
7226
                          and then
7227
                            Ekind (Etype (First_Formal (Hom))) =
7228
                              E_Anonymous_Access_Type
7229
                          and then
7230
                            Base_Type
7231
                              (Designated_Type (Etype (First_Formal (Hom)))) =
7232
                                                                   Cls_Type))
7233
               then
7234
                  --  If the context is a procedure call, ignore functions
7235
                  --  in the name of the call.
7236
 
7237
                  if Ekind (Hom) = E_Function
7238
                    and then Nkind (Parent (N)) = N_Procedure_Call_Statement
7239
                    and then N = Name (Parent (N))
7240
                  then
7241
                     goto Next_Hom;
7242
 
7243
                  --  If the context is a function call, ignore procedures
7244
                  --  in the name of the call.
7245
 
7246
                  elsif Ekind (Hom) = E_Procedure
7247
                    and then Nkind (Parent (N)) /= N_Procedure_Call_Statement
7248
                  then
7249
                     goto Next_Hom;
7250
                  end if;
7251
 
7252
                  Set_Etype (Call_Node, Any_Type);
7253
                  Set_Is_Overloaded (Call_Node, False);
7254
                  Success := False;
7255
 
7256
                  if No (Matching_Op) then
7257
                     Hom_Ref := New_Reference_To (Hom, Sloc (Subprog));
7258
                     Set_Etype (Call_Node, Any_Type);
7259
                     Set_Parent (Call_Node, Parent (Node_To_Replace));
7260
 
7261
                     Set_Name (Call_Node, Hom_Ref);
7262
 
7263
                     Analyze_One_Call
7264
                       (N          => Call_Node,
7265
                        Nam        => Hom,
7266
                        Report     => Report_Error,
7267
                        Success    => Success,
7268
                        Skip_First => True);
7269
 
7270
                     Matching_Op :=
7271
                       Valid_Candidate (Success, Call_Node, Hom);
7272
 
7273
                  else
7274
                     Analyze_One_Call
7275
                       (N          => Call_Node,
7276
                        Nam        => Hom,
7277
                        Report     => Report_Error,
7278
                        Success    => Success,
7279
                        Skip_First => True);
7280
 
7281
                     if Present (Valid_Candidate (Success, Call_Node, Hom))
7282
                       and then Nkind (Call_Node) /= N_Function_Call
7283
                     then
7284
                        Error_Msg_NE ("ambiguous call to&", N, Hom);
7285
                        Report_Ambiguity (Matching_Op);
7286
                        Report_Ambiguity (Hom);
7287
                        Error := True;
7288
                        return;
7289
                     end if;
7290
                  end if;
7291
               end if;
7292
 
7293
               <<Next_Hom>>
7294
                  Hom := Homonym (Hom);
7295
            end loop;
7296
         end Traverse_Homonyms;
7297
 
7298
         -------------------------
7299
         -- Traverse_Interfaces --
7300
         -------------------------
7301
 
7302
         procedure Traverse_Interfaces
7303
           (Anc_Type : Entity_Id;
7304
            Error    : out Boolean)
7305
         is
7306
            Intface_List : constant List_Id :=
7307
                             Abstract_Interface_List (Anc_Type);
7308
            Intface      : Node_Id;
7309
 
7310
         begin
7311
            Error := False;
7312
 
7313
            if Is_Non_Empty_List (Intface_List) then
7314
               Intface := First (Intface_List);
7315
               while Present (Intface) loop
7316
 
7317
                  --  Look for acceptable class-wide homonyms associated with
7318
                  --  the interface.
7319
 
7320
                  Traverse_Homonyms (Etype (Intface), Error);
7321
 
7322
                  if Error then
7323
                     return;
7324
                  end if;
7325
 
7326
                  --  Continue the search by looking at each of the interface's
7327
                  --  associated interface ancestors.
7328
 
7329
                  Traverse_Interfaces (Etype (Intface), Error);
7330
 
7331
                  if Error then
7332
                     return;
7333
                  end if;
7334
 
7335
                  Next (Intface);
7336
               end loop;
7337
            end if;
7338
         end Traverse_Interfaces;
7339
 
7340
      --  Start of processing for Try_Class_Wide_Operation
7341
 
7342
      begin
7343
         --  If we are searching only for conflicting class-wide subprograms
7344
         --  then initialize directly Matching_Op with the target entity.
7345
 
7346
         if CW_Test_Only then
7347
            Matching_Op := Entity (Selector_Name (N));
7348
         end if;
7349
 
7350
         --  Loop through ancestor types (including interfaces), traversing
7351
         --  the homonym chain of the subprogram, trying out those homonyms
7352
         --  whose first formal has the class-wide type of the ancestor, or
7353
         --  an anonymous access type designating the class-wide type.
7354
 
7355
         Anc_Type := Obj_Type;
7356
         loop
7357
            --  Look for a match among homonyms associated with the ancestor
7358
 
7359
            Traverse_Homonyms (Anc_Type, Error);
7360
 
7361
            if Error then
7362
               return True;
7363
            end if;
7364
 
7365
            --  Continue the search for matches among homonyms associated with
7366
            --  any interfaces implemented by the ancestor.
7367
 
7368
            Traverse_Interfaces (Anc_Type, Error);
7369
 
7370
            if Error then
7371
               return True;
7372
            end if;
7373
 
7374
            exit when Etype (Anc_Type) = Anc_Type;
7375
            Anc_Type := Etype (Anc_Type);
7376
         end loop;
7377
 
7378
         if Present (Matching_Op) then
7379
            Set_Etype (Call_Node, Etype (Matching_Op));
7380
         end if;
7381
 
7382
         return Present (Matching_Op);
7383
      end Try_Class_Wide_Operation;
7384
 
7385
      -----------------------------------
7386
      -- Try_One_Prefix_Interpretation --
7387
      -----------------------------------
7388
 
7389
      procedure Try_One_Prefix_Interpretation (T : Entity_Id) is
7390
      begin
7391
         Obj_Type := T;
7392
 
7393
         if Is_Access_Type (Obj_Type) then
7394
            Obj_Type := Designated_Type (Obj_Type);
7395
         end if;
7396
 
7397
         if Ekind (Obj_Type) = E_Private_Subtype then
7398
            Obj_Type := Base_Type (Obj_Type);
7399
         end if;
7400
 
7401
         if Is_Class_Wide_Type (Obj_Type) then
7402
            Obj_Type := Etype (Class_Wide_Type (Obj_Type));
7403
         end if;
7404
 
7405
         --  The type may have be obtained through a limited_with clause,
7406
         --  in which case the primitive operations are available on its
7407
         --  non-limited view. If still incomplete, retrieve full view.
7408
 
7409
         if Ekind (Obj_Type) = E_Incomplete_Type
7410
           and then From_With_Type (Obj_Type)
7411
         then
7412
            Obj_Type := Get_Full_View (Non_Limited_View (Obj_Type));
7413
         end if;
7414
 
7415
         --  If the object is not tagged, or the type is still an incomplete
7416
         --  type, this is not a prefixed call.
7417
 
7418
         if not Is_Tagged_Type (Obj_Type)
7419
           or else Is_Incomplete_Type (Obj_Type)
7420
         then
7421
            return;
7422
         end if;
7423
 
7424
         declare
7425
            Dup_Call_Node : constant Node_Id := New_Copy (New_Call_Node);
7426
            CW_Result     : Boolean;
7427
            Prim_Result   : Boolean;
7428
            pragma Unreferenced (CW_Result);
7429
 
7430
         begin
7431
            if not CW_Test_Only then
7432
               Prim_Result :=
7433
                  Try_Primitive_Operation
7434
                   (Call_Node       => New_Call_Node,
7435
                    Node_To_Replace => Node_To_Replace);
7436
            end if;
7437
 
7438
            --  Check if there is a class-wide subprogram covering the
7439
            --  primitive. This check must be done even if a candidate
7440
            --  was found in order to report ambiguous calls.
7441
 
7442
            if not (Prim_Result) then
7443
               CW_Result :=
7444
                 Try_Class_Wide_Operation
7445
                   (Call_Node       => New_Call_Node,
7446
                    Node_To_Replace => Node_To_Replace);
7447
 
7448
            --  If we found a primitive we search for class-wide subprograms
7449
            --  using a duplicate of the call node (done to avoid missing its
7450
            --  decoration if there is no ambiguity).
7451
 
7452
            else
7453
               CW_Result :=
7454
                 Try_Class_Wide_Operation
7455
                   (Call_Node       => Dup_Call_Node,
7456
                    Node_To_Replace => Node_To_Replace);
7457
            end if;
7458
         end;
7459
      end Try_One_Prefix_Interpretation;
7460
 
7461
      -----------------------------
7462
      -- Try_Primitive_Operation --
7463
      -----------------------------
7464
 
7465
      function Try_Primitive_Operation
7466
        (Call_Node       : Node_Id;
7467
         Node_To_Replace : Node_Id) return Boolean
7468
      is
7469
         Elmt        : Elmt_Id;
7470
         Prim_Op     : Entity_Id;
7471
         Matching_Op : Entity_Id := Empty;
7472
         Prim_Op_Ref : Node_Id   := Empty;
7473
 
7474
         Corr_Type   : Entity_Id := Empty;
7475
         --  If the prefix is a synchronized type, the controlling type of
7476
         --  the primitive operation is the corresponding record type, else
7477
         --  this is the object type itself.
7478
 
7479
         Success     : Boolean   := False;
7480
 
7481
         function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id;
7482
         --  For tagged types the candidate interpretations are found in
7483
         --  the list of primitive operations of the type and its ancestors.
7484
         --  For formal tagged types we have to find the operations declared
7485
         --  in the same scope as the type (including in the generic formal
7486
         --  part) because the type itself carries no primitive operations,
7487
         --  except for formal derived types that inherit the operations of
7488
         --  the parent and progenitors.
7489
         --  If the context is a generic subprogram body, the generic formals
7490
         --  are visible by name, but are not in the entity list of the
7491
         --  subprogram because that list starts with the subprogram formals.
7492
         --  We retrieve the candidate operations from the generic declaration.
7493
 
7494
         function Is_Private_Overriding (Op : Entity_Id) return Boolean;
7495
         --  An operation that overrides an inherited operation in the private
7496
         --  part of its package may be hidden, but if the inherited operation
7497
         --  is visible a direct call to it will dispatch to the private one,
7498
         --  which is therefore a valid candidate.
7499
 
7500
         function Valid_First_Argument_Of (Op : Entity_Id) return Boolean;
7501
         --  Verify that the prefix, dereferenced if need be, is a valid
7502
         --  controlling argument in a call to Op. The remaining actuals
7503
         --  are checked in the subsequent call to Analyze_One_Call.
7504
 
7505
         ------------------------------
7506
         -- Collect_Generic_Type_Ops --
7507
         ------------------------------
7508
 
7509
         function Collect_Generic_Type_Ops (T : Entity_Id) return Elist_Id is
7510
            Bas        : constant Entity_Id := Base_Type (T);
7511
            Candidates : constant Elist_Id := New_Elmt_List;
7512
            Subp       : Entity_Id;
7513
            Formal     : Entity_Id;
7514
 
7515
            procedure Check_Candidate;
7516
            --  The operation is a candidate if its first parameter is a
7517
            --  controlling operand of the desired type.
7518
 
7519
            -----------------------
7520
            --  Check_Candidate; --
7521
            -----------------------
7522
 
7523
            procedure Check_Candidate is
7524
            begin
7525
               Formal := First_Formal (Subp);
7526
 
7527
               if Present (Formal)
7528
                 and then Is_Controlling_Formal (Formal)
7529
                 and then
7530
                   (Base_Type (Etype (Formal)) = Bas
7531
                     or else
7532
                       (Is_Access_Type (Etype (Formal))
7533
                         and then Designated_Type (Etype (Formal)) = Bas))
7534
               then
7535
                  Append_Elmt (Subp, Candidates);
7536
               end if;
7537
            end Check_Candidate;
7538
 
7539
         --  Start of processing for Collect_Generic_Type_Ops
7540
 
7541
         begin
7542
            if Is_Derived_Type (T) then
7543
               return Primitive_Operations (T);
7544
 
7545
            elsif Ekind_In (Scope (T), E_Procedure, E_Function) then
7546
 
7547
               --  Scan the list of generic formals to find subprograms
7548
               --  that may have a first controlling formal of the type.
7549
 
7550
               if Nkind (Unit_Declaration_Node (Scope (T)))
7551
                 = N_Generic_Subprogram_Declaration
7552
               then
7553
                  declare
7554
                     Decl : Node_Id;
7555
 
7556
                  begin
7557
                     Decl :=
7558
                       First (Generic_Formal_Declarations
7559
                               (Unit_Declaration_Node (Scope (T))));
7560
                     while Present (Decl) loop
7561
                        if Nkind (Decl) in N_Formal_Subprogram_Declaration then
7562
                           Subp := Defining_Entity (Decl);
7563
                           Check_Candidate;
7564
                        end if;
7565
 
7566
                        Next (Decl);
7567
                     end loop;
7568
                  end;
7569
               end if;
7570
               return Candidates;
7571
 
7572
            else
7573
               --  Scan the list of entities declared in the same scope as
7574
               --  the type. In general this will be an open scope, given that
7575
               --  the call we are analyzing can only appear within a generic
7576
               --  declaration or body (either the one that declares T, or a
7577
               --  child unit).
7578
 
7579
               --  For a subtype representing a generic actual type, go to the
7580
               --  base type.
7581
 
7582
               if Is_Generic_Actual_Type (T) then
7583
                  Subp := First_Entity (Scope (Base_Type (T)));
7584
               else
7585
                  Subp := First_Entity (Scope (T));
7586
               end if;
7587
 
7588
               while Present (Subp) loop
7589
                  if Is_Overloadable (Subp) then
7590
                     Check_Candidate;
7591
                  end if;
7592
 
7593
                  Next_Entity (Subp);
7594
               end loop;
7595
 
7596
               return Candidates;
7597
            end if;
7598
         end Collect_Generic_Type_Ops;
7599
 
7600
         ---------------------------
7601
         -- Is_Private_Overriding --
7602
         ---------------------------
7603
 
7604
         function Is_Private_Overriding (Op : Entity_Id) return Boolean is
7605
            Visible_Op : constant Entity_Id := Homonym (Op);
7606
 
7607
         begin
7608
            return Present (Visible_Op)
7609
              and then Scope (Op) = Scope (Visible_Op)
7610
              and then not Comes_From_Source (Visible_Op)
7611
              and then Alias (Visible_Op) = Op
7612
              and then not Is_Hidden (Visible_Op);
7613
         end Is_Private_Overriding;
7614
 
7615
         -----------------------------
7616
         -- Valid_First_Argument_Of --
7617
         -----------------------------
7618
 
7619
         function Valid_First_Argument_Of (Op : Entity_Id) return Boolean is
7620
            Typ : Entity_Id := Etype (First_Formal (Op));
7621
 
7622
         begin
7623
            if Is_Concurrent_Type (Typ)
7624
              and then Present (Corresponding_Record_Type (Typ))
7625
            then
7626
               Typ := Corresponding_Record_Type (Typ);
7627
            end if;
7628
 
7629
            --  Simple case. Object may be a subtype of the tagged type or
7630
            --  may be the corresponding record of a synchronized type.
7631
 
7632
            return Obj_Type = Typ
7633
              or else Base_Type (Obj_Type) = Typ
7634
              or else Corr_Type = Typ
7635
 
7636
               --  Prefix can be dereferenced
7637
 
7638
              or else
7639
                (Is_Access_Type (Corr_Type)
7640
                  and then Designated_Type (Corr_Type) = Typ)
7641
 
7642
               --  Formal is an access parameter, for which the object
7643
               --  can provide an access.
7644
 
7645
              or else
7646
                (Ekind (Typ) = E_Anonymous_Access_Type
7647
                  and then
7648
                    Base_Type (Designated_Type (Typ)) = Base_Type (Corr_Type));
7649
         end Valid_First_Argument_Of;
7650
 
7651
      --  Start of processing for Try_Primitive_Operation
7652
 
7653
      begin
7654
         --  Look for subprograms in the list of primitive operations. The name
7655
         --  must be identical, and the kind of call indicates the expected
7656
         --  kind of operation (function or procedure). If the type is a
7657
         --  (tagged) synchronized type, the primitive ops are attached to the
7658
         --  corresponding record (base) type.
7659
 
7660
         if Is_Concurrent_Type (Obj_Type) then
7661
            if Present (Corresponding_Record_Type (Obj_Type)) then
7662
               Corr_Type := Base_Type (Corresponding_Record_Type (Obj_Type));
7663
               Elmt := First_Elmt (Primitive_Operations (Corr_Type));
7664
            else
7665
               Corr_Type := Obj_Type;
7666
               Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
7667
            end if;
7668
 
7669
         elsif not Is_Generic_Type (Obj_Type) then
7670
            Corr_Type := Obj_Type;
7671
            Elmt := First_Elmt (Primitive_Operations (Obj_Type));
7672
 
7673
         else
7674
            Corr_Type := Obj_Type;
7675
            Elmt := First_Elmt (Collect_Generic_Type_Ops (Obj_Type));
7676
         end if;
7677
 
7678
         while Present (Elmt) loop
7679
            Prim_Op := Node (Elmt);
7680
 
7681
            if Chars (Prim_Op) = Chars (Subprog)
7682
              and then Present (First_Formal (Prim_Op))
7683
              and then Valid_First_Argument_Of (Prim_Op)
7684
              and then
7685
                (Nkind (Call_Node) = N_Function_Call)
7686
                   = (Ekind (Prim_Op) = E_Function)
7687
            then
7688
               --  Ada 2005 (AI-251): If this primitive operation corresponds
7689
               --  with an immediate ancestor interface there is no need to add
7690
               --  it to the list of interpretations; the corresponding aliased
7691
               --  primitive is also in this list of primitive operations and
7692
               --  will be used instead.
7693
 
7694
               if (Present (Interface_Alias (Prim_Op))
7695
                    and then Is_Ancestor (Find_Dispatching_Type
7696
                                            (Alias (Prim_Op)), Corr_Type))
7697
 
7698
                 --  Do not consider hidden primitives unless the type is in an
7699
                 --  open scope or we are within an instance, where visibility
7700
                 --  is known to be correct, or else if this is an overriding
7701
                 --  operation in the private part for an inherited operation.
7702
 
7703
                 or else (Is_Hidden (Prim_Op)
7704
                           and then not Is_Immediately_Visible (Obj_Type)
7705
                           and then not In_Instance
7706
                           and then not Is_Private_Overriding (Prim_Op))
7707
               then
7708
                  goto Continue;
7709
               end if;
7710
 
7711
               Set_Etype (Call_Node, Any_Type);
7712
               Set_Is_Overloaded (Call_Node, False);
7713
 
7714
               if No (Matching_Op) then
7715
                  Prim_Op_Ref := New_Reference_To (Prim_Op, Sloc (Subprog));
7716
                  Candidate := Prim_Op;
7717
 
7718
                  Set_Parent (Call_Node, Parent (Node_To_Replace));
7719
 
7720
                  Set_Name (Call_Node, Prim_Op_Ref);
7721
                  Success := False;
7722
 
7723
                  Analyze_One_Call
7724
                    (N          => Call_Node,
7725
                     Nam        => Prim_Op,
7726
                     Report     => Report_Error,
7727
                     Success    => Success,
7728
                     Skip_First => True);
7729
 
7730
                  Matching_Op := Valid_Candidate (Success, Call_Node, Prim_Op);
7731
 
7732
               --  More than one interpretation, collect for subsequent
7733
               --  disambiguation. If this is a procedure call and there
7734
               --  is another match, report ambiguity now.
7735
 
7736
               else
7737
                  Analyze_One_Call
7738
                    (N          => Call_Node,
7739
                     Nam        => Prim_Op,
7740
                     Report     => Report_Error,
7741
                     Success    => Success,
7742
                     Skip_First => True);
7743
 
7744
                  if Present (Valid_Candidate (Success, Call_Node, Prim_Op))
7745
                    and then Nkind (Call_Node) /= N_Function_Call
7746
                  then
7747
                     Error_Msg_NE ("ambiguous call to&", N, Prim_Op);
7748
                     Report_Ambiguity (Matching_Op);
7749
                     Report_Ambiguity (Prim_Op);
7750
                     return True;
7751
                  end if;
7752
               end if;
7753
            end if;
7754
 
7755
            <<Continue>>
7756
            Next_Elmt (Elmt);
7757
         end loop;
7758
 
7759
         if Present (Matching_Op) then
7760
            Set_Etype (Call_Node, Etype (Matching_Op));
7761
         end if;
7762
 
7763
         return Present (Matching_Op);
7764
      end Try_Primitive_Operation;
7765
 
7766
   --  Start of processing for Try_Object_Operation
7767
 
7768
   begin
7769
      Analyze_Expression (Obj);
7770
 
7771
      --  Analyze the actuals if node is known to be a subprogram call
7772
 
7773
      if Is_Subprg_Call and then N = Name (Parent (N)) then
7774
         Actual := First (Parameter_Associations (Parent (N)));
7775
         while Present (Actual) loop
7776
            Analyze_Expression (Actual);
7777
            Next (Actual);
7778
         end loop;
7779
      end if;
7780
 
7781
      --  Build a subprogram call node, using a copy of Obj as its first
7782
      --  actual. This is a placeholder, to be replaced by an explicit
7783
      --  dereference when needed.
7784
 
7785
      Transform_Object_Operation
7786
        (Call_Node       => New_Call_Node,
7787
         Node_To_Replace => Node_To_Replace);
7788
 
7789
      Set_Etype (New_Call_Node, Any_Type);
7790
      Set_Etype (Subprog, Any_Type);
7791
      Set_Parent (New_Call_Node, Parent (Node_To_Replace));
7792
 
7793
      if not Is_Overloaded (Obj) then
7794
         Try_One_Prefix_Interpretation (Obj_Type);
7795
 
7796
      else
7797
         declare
7798
            I  : Interp_Index;
7799
            It : Interp;
7800
         begin
7801
            Get_First_Interp (Obj, I, It);
7802
            while Present (It.Nam) loop
7803
               Try_One_Prefix_Interpretation (It.Typ);
7804
               Get_Next_Interp (I, It);
7805
            end loop;
7806
         end;
7807
      end if;
7808
 
7809
      if Etype (New_Call_Node) /= Any_Type then
7810
 
7811
         --  No need to complete the tree transformations if we are only
7812
         --  searching for conflicting class-wide subprograms
7813
 
7814
         if CW_Test_Only then
7815
            return False;
7816
         else
7817
            Complete_Object_Operation
7818
              (Call_Node       => New_Call_Node,
7819
               Node_To_Replace => Node_To_Replace);
7820
            return True;
7821
         end if;
7822
 
7823
      elsif Present (Candidate) then
7824
 
7825
         --  The argument list is not type correct. Re-analyze with error
7826
         --  reporting enabled, and use one of the possible candidates.
7827
         --  In All_Errors_Mode, re-analyze all failed interpretations.
7828
 
7829
         if All_Errors_Mode then
7830
            Report_Error := True;
7831
            if Try_Primitive_Operation
7832
                (Call_Node       => New_Call_Node,
7833
                 Node_To_Replace => Node_To_Replace)
7834
 
7835
              or else
7836
                Try_Class_Wide_Operation
7837
                  (Call_Node       => New_Call_Node,
7838
                   Node_To_Replace => Node_To_Replace)
7839
            then
7840
               null;
7841
            end if;
7842
 
7843
         else
7844
            Analyze_One_Call
7845
              (N          => New_Call_Node,
7846
               Nam        => Candidate,
7847
               Report     => True,
7848
               Success    => Success,
7849
               Skip_First => True);
7850
         end if;
7851
 
7852
         --  No need for further errors
7853
 
7854
         return True;
7855
 
7856
      else
7857
         --  There was no candidate operation, so report it as an error
7858
         --  in the caller: Analyze_Selected_Component.
7859
 
7860
         return False;
7861
      end if;
7862
   end Try_Object_Operation;
7863
 
7864
   ---------
7865
   -- wpo --
7866
   ---------
7867
 
7868
   procedure wpo (T : Entity_Id) is
7869
      Op : Entity_Id;
7870
      E  : Elmt_Id;
7871
 
7872
   begin
7873
      if not Is_Tagged_Type (T) then
7874
         return;
7875
      end if;
7876
 
7877
      E := First_Elmt (Primitive_Operations (Base_Type (T)));
7878
      while Present (E) loop
7879
         Op := Node (E);
7880
         Write_Int (Int (Op));
7881
         Write_Str (" === ");
7882
         Write_Name (Chars (Op));
7883
         Write_Str (" in ");
7884
         Write_Name (Chars (Scope (Op)));
7885
         Next_Elmt (E);
7886
         Write_Eol;
7887
      end loop;
7888
   end wpo;
7889
 
7890
end Sem_Ch4;

powered by: WebSVN 2.1.0

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