Coverage Report

Created: 2019-03-22 08:08

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/Sema/Overload.h
Line
Count
Source (jump to first uncovered line)
1
//===- Overload.h - C++ Overloading -----------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file defines the data structures and types used in C++
10
// overload resolution.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_SEMA_OVERLOAD_H
15
#define LLVM_CLANG_SEMA_OVERLOAD_H
16
17
#include "clang/AST/Decl.h"
18
#include "clang/AST/DeclAccessPair.h"
19
#include "clang/AST/DeclBase.h"
20
#include "clang/AST/DeclCXX.h"
21
#include "clang/AST/DeclTemplate.h"
22
#include "clang/AST/Expr.h"
23
#include "clang/AST/Type.h"
24
#include "clang/Basic/LLVM.h"
25
#include "clang/Basic/SourceLocation.h"
26
#include "clang/Sema/SemaFixItUtils.h"
27
#include "clang/Sema/TemplateDeduction.h"
28
#include "llvm/ADT/ArrayRef.h"
29
#include "llvm/ADT/None.h"
30
#include "llvm/ADT/STLExtras.h"
31
#include "llvm/ADT/SmallPtrSet.h"
32
#include "llvm/ADT/SmallVector.h"
33
#include "llvm/ADT/StringRef.h"
34
#include "llvm/Support/AlignOf.h"
35
#include "llvm/Support/Allocator.h"
36
#include "llvm/Support/Casting.h"
37
#include "llvm/Support/ErrorHandling.h"
38
#include <cassert>
39
#include <cstddef>
40
#include <cstdint>
41
#include <utility>
42
43
namespace clang {
44
45
class APValue;
46
class ASTContext;
47
class Sema;
48
49
  /// OverloadingResult - Capture the result of performing overload
50
  /// resolution.
51
  enum OverloadingResult {
52
    /// Overload resolution succeeded.
53
    OR_Success,
54
55
    /// No viable function found.
56
    OR_No_Viable_Function,
57
58
    /// Ambiguous candidates found.
59
    OR_Ambiguous,
60
61
    /// Succeeded, but refers to a deleted function.
62
    OR_Deleted
63
  };
64
65
  enum OverloadCandidateDisplayKind {
66
    /// Requests that all candidates be shown.  Viable candidates will
67
    /// be printed first.
68
    OCD_AllCandidates,
69
70
    /// Requests that only viable candidates be shown.
71
    OCD_ViableCandidates
72
  };
73
74
  /// ImplicitConversionKind - The kind of implicit conversion used to
75
  /// convert an argument to a parameter's type. The enumerator values
76
  /// match with the table titled 'Conversions' in [over.ics.scs] and are listed
77
  /// such that better conversion kinds have smaller values.
78
  enum ImplicitConversionKind {
79
    /// Identity conversion (no conversion)
80
    ICK_Identity = 0,
81
82
    /// Lvalue-to-rvalue conversion (C++ [conv.lval])
83
    ICK_Lvalue_To_Rvalue,
84
85
    /// Array-to-pointer conversion (C++ [conv.array])
86
    ICK_Array_To_Pointer,
87
88
    /// Function-to-pointer (C++ [conv.array])
89
    ICK_Function_To_Pointer,
90
91
    /// Function pointer conversion (C++17 [conv.fctptr])
92
    ICK_Function_Conversion,
93
94
    /// Qualification conversions (C++ [conv.qual])
95
    ICK_Qualification,
96
97
    /// Integral promotions (C++ [conv.prom])
98
    ICK_Integral_Promotion,
99
100
    /// Floating point promotions (C++ [conv.fpprom])
101
    ICK_Floating_Promotion,
102
103
    /// Complex promotions (Clang extension)
104
    ICK_Complex_Promotion,
105
106
    /// Integral conversions (C++ [conv.integral])
107
    ICK_Integral_Conversion,
108
109
    /// Floating point conversions (C++ [conv.double]
110
    ICK_Floating_Conversion,
111
112
    /// Complex conversions (C99 6.3.1.6)
113
    ICK_Complex_Conversion,
114
115
    /// Floating-integral conversions (C++ [conv.fpint])
116
    ICK_Floating_Integral,
117
118
    /// Pointer conversions (C++ [conv.ptr])
119
    ICK_Pointer_Conversion,
120
121
    /// Pointer-to-member conversions (C++ [conv.mem])
122
    ICK_Pointer_Member,
123
124
    /// Boolean conversions (C++ [conv.bool])
125
    ICK_Boolean_Conversion,
126
127
    /// Conversions between compatible types in C99
128
    ICK_Compatible_Conversion,
129
130
    /// Derived-to-base (C++ [over.best.ics])
131
    ICK_Derived_To_Base,
132
133
    /// Vector conversions
134
    ICK_Vector_Conversion,
135
136
    /// A vector splat from an arithmetic type
137
    ICK_Vector_Splat,
138
139
    /// Complex-real conversions (C99 6.3.1.7)
140
    ICK_Complex_Real,
141
142
    /// Block Pointer conversions
143
    ICK_Block_Pointer_Conversion,
144
145
    /// Transparent Union Conversions
146
    ICK_TransparentUnionConversion,
147
148
    /// Objective-C ARC writeback conversion
149
    ICK_Writeback_Conversion,
150
151
    /// Zero constant to event (OpenCL1.2 6.12.10)
152
    ICK_Zero_Event_Conversion,
153
154
    /// Zero constant to queue
155
    ICK_Zero_Queue_Conversion,
156
157
    /// Conversions allowed in C, but not C++
158
    ICK_C_Only_Conversion,
159
160
    /// C-only conversion between pointers with incompatible types
161
    ICK_Incompatible_Pointer_Conversion,
162
163
    /// The number of conversion kinds
164
    ICK_Num_Conversion_Kinds,
165
  };
166
167
  /// ImplicitConversionRank - The rank of an implicit conversion
168
  /// kind. The enumerator values match with Table 9 of (C++
169
  /// 13.3.3.1.1) and are listed such that better conversion ranks
170
  /// have smaller values.
171
  enum ImplicitConversionRank {
172
    /// Exact Match
173
    ICR_Exact_Match = 0,
174
175
    /// Promotion
176
    ICR_Promotion,
177
178
    /// Conversion
179
    ICR_Conversion,
180
181
    /// OpenCL Scalar Widening
182
    ICR_OCL_Scalar_Widening,
183
184
    /// Complex <-> Real conversion
185
    ICR_Complex_Real_Conversion,
186
187
    /// ObjC ARC writeback conversion
188
    ICR_Writeback_Conversion,
189
190
    /// Conversion only allowed in the C standard (e.g. void* to char*).
191
    ICR_C_Conversion,
192
193
    /// Conversion not allowed by the C standard, but that we accept as an
194
    /// extension anyway.
195
    ICR_C_Conversion_Extension
196
  };
197
198
  ImplicitConversionRank GetConversionRank(ImplicitConversionKind Kind);
199
200
  /// NarrowingKind - The kind of narrowing conversion being performed by a
201
  /// standard conversion sequence according to C++11 [dcl.init.list]p7.
202
  enum NarrowingKind {
203
    /// Not a narrowing conversion.
204
    NK_Not_Narrowing,
205
206
    /// A narrowing conversion by virtue of the source and destination types.
207
    NK_Type_Narrowing,
208
209
    /// A narrowing conversion, because a constant expression got narrowed.
210
    NK_Constant_Narrowing,
211
212
    /// A narrowing conversion, because a non-constant-expression variable might
213
    /// have got narrowed.
214
    NK_Variable_Narrowing,
215
216
    /// Cannot tell whether this is a narrowing conversion because the
217
    /// expression is value-dependent.
218
    NK_Dependent_Narrowing,
219
  };
220
221
  /// StandardConversionSequence - represents a standard conversion
222
  /// sequence (C++ 13.3.3.1.1). A standard conversion sequence
223
  /// contains between zero and three conversions. If a particular
224
  /// conversion is not needed, it will be set to the identity conversion
225
  /// (ICK_Identity). Note that the three conversions are
226
  /// specified as separate members (rather than in an array) so that
227
  /// we can keep the size of a standard conversion sequence to a
228
  /// single word.
229
0
  class StandardConversionSequence {
230
  public:
231
    /// First -- The first conversion can be an lvalue-to-rvalue
232
    /// conversion, array-to-pointer conversion, or
233
    /// function-to-pointer conversion.
234
    ImplicitConversionKind First : 8;
235
236
    /// Second - The second conversion can be an integral promotion,
237
    /// floating point promotion, integral conversion, floating point
238
    /// conversion, floating-integral conversion, pointer conversion,
239
    /// pointer-to-member conversion, or boolean conversion.
240
    ImplicitConversionKind Second : 8;
241
242
    /// Third - The third conversion can be a qualification conversion
243
    /// or a function conversion.
244
    ImplicitConversionKind Third : 8;
245
246
    /// Whether this is the deprecated conversion of a
247
    /// string literal to a pointer to non-const character data
248
    /// (C++ 4.2p2).
249
    unsigned DeprecatedStringLiteralToCharPtr : 1;
250
251
    /// Whether the qualification conversion involves a change in the
252
    /// Objective-C lifetime (for automatic reference counting).
253
    unsigned QualificationIncludesObjCLifetime : 1;
254
255
    /// IncompatibleObjC - Whether this is an Objective-C conversion
256
    /// that we should warn about (if we actually use it).
257
    unsigned IncompatibleObjC : 1;
258
259
    /// ReferenceBinding - True when this is a reference binding
260
    /// (C++ [over.ics.ref]).
261
    unsigned ReferenceBinding : 1;
262
263
    /// DirectBinding - True when this is a reference binding that is a
264
    /// direct binding (C++ [dcl.init.ref]).
265
    unsigned DirectBinding : 1;
266
267
    /// Whether this is an lvalue reference binding (otherwise, it's
268
    /// an rvalue reference binding).
269
    unsigned IsLvalueReference : 1;
270
271
    /// Whether we're binding to a function lvalue.
272
    unsigned BindsToFunctionLvalue : 1;
273
274
    /// Whether we're binding to an rvalue.
275
    unsigned BindsToRvalue : 1;
276
277
    /// Whether this binds an implicit object argument to a
278
    /// non-static member function without a ref-qualifier.
279
    unsigned BindsImplicitObjectArgumentWithoutRefQualifier : 1;
280
281
    /// Whether this binds a reference to an object with a different
282
    /// Objective-C lifetime qualifier.
283
    unsigned ObjCLifetimeConversionBinding : 1;
284
285
    /// FromType - The type that this conversion is converting
286
    /// from. This is an opaque pointer that can be translated into a
287
    /// QualType.
288
    void *FromTypePtr;
289
290
    /// ToType - The types that this conversion is converting to in
291
    /// each step. This is an opaque pointer that can be translated
292
    /// into a QualType.
293
    void *ToTypePtrs[3];
294
295
    /// CopyConstructor - The copy constructor that is used to perform
296
    /// this conversion, when the conversion is actually just the
297
    /// initialization of an object via copy constructor. Such
298
    /// conversions are either identity conversions or derived-to-base
299
    /// conversions.
300
    CXXConstructorDecl *CopyConstructor;
301
    DeclAccessPair FoundCopyConstructor;
302
303
38.6M
    void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); }
304
305
103M
    void setToType(unsigned Idx, QualType T) {
306
103M
      assert(Idx < 3 && "To type index is out of range");
307
103M
      ToTypePtrs[Idx] = T.getAsOpaquePtr();
308
103M
    }
309
310
3.75M
    void setAllToTypes(QualType T) {
311
3.75M
      ToTypePtrs[0] = T.getAsOpaquePtr();
312
3.75M
      ToTypePtrs[1] = ToTypePtrs[0];
313
3.75M
      ToTypePtrs[2] = ToTypePtrs[0];
314
3.75M
    }
315
316
13.7M
    QualType getFromType() const {
317
13.7M
      return QualType::getFromOpaquePtr(FromTypePtr);
318
13.7M
    }
319
320
35.7M
    QualType getToType(unsigned Idx) const {
321
35.7M
      assert(Idx < 3 && "To type index is out of range");
322
35.7M
      return QualType::getFromOpaquePtr(ToTypePtrs[Idx]);
323
35.7M
    }
324
325
    void setAsIdentityConversion();
326
327
72.7M
    bool isIdentityConversion() const {
328
72.7M
      return Second == ICK_Identity && 
Third == ICK_Identity26.5M
;
329
72.7M
    }
330
331
    ImplicitConversionRank getRank() const;
332
    NarrowingKind
333
    getNarrowingKind(ASTContext &Context, const Expr *Converted,
334
                     APValue &ConstantValue, QualType &ConstantType,
335
                     bool IgnoreFloatToIntegralConversion = false) const;
336
    bool isPointerConversionToBool() const;
337
    bool isPointerConversionToVoidPointer(ASTContext& Context) const;
338
    void dump() const;
339
  };
340
341
  /// UserDefinedConversionSequence - Represents a user-defined
342
  /// conversion sequence (C++ 13.3.3.1.2).
343
0
  struct UserDefinedConversionSequence {
344
    /// Represents the standard conversion that occurs before
345
    /// the actual user-defined conversion.
346
    ///
347
    /// C++11 13.3.3.1.2p1:
348
    ///   If the user-defined conversion is specified by a constructor
349
    ///   (12.3.1), the initial standard conversion sequence converts
350
    ///   the source type to the type required by the argument of the
351
    ///   constructor. If the user-defined conversion is specified by
352
    ///   a conversion function (12.3.2), the initial standard
353
    ///   conversion sequence converts the source type to the implicit
354
    ///   object parameter of the conversion function.
355
    StandardConversionSequence Before;
356
357
    /// EllipsisConversion - When this is true, it means user-defined
358
    /// conversion sequence starts with a ... (ellipsis) conversion, instead of
359
    /// a standard conversion. In this case, 'Before' field must be ignored.
360
    // FIXME. I much rather put this as the first field. But there seems to be
361
    // a gcc code gen. bug which causes a crash in a test. Putting it here seems
362
    // to work around the crash.
363
    bool EllipsisConversion : 1;
364
365
    /// HadMultipleCandidates - When this is true, it means that the
366
    /// conversion function was resolved from an overloaded set having
367
    /// size greater than 1.
368
    bool HadMultipleCandidates : 1;
369
370
    /// After - Represents the standard conversion that occurs after
371
    /// the actual user-defined conversion.
372
    StandardConversionSequence After;
373
374
    /// ConversionFunction - The function that will perform the
375
    /// user-defined conversion. Null if the conversion is an
376
    /// aggregate initialization from an initializer list.
377
    FunctionDecl* ConversionFunction;
378
379
    /// The declaration that we found via name lookup, which might be
380
    /// the same as \c ConversionFunction or it might be a using declaration
381
    /// that refers to \c ConversionFunction.
382
    DeclAccessPair FoundConversionFunction;
383
384
    void dump() const;
385
  };
386
387
  /// Represents an ambiguous user-defined conversion sequence.
388
  struct AmbiguousConversionSequence {
389
    using ConversionSet =
390
        SmallVector<std::pair<NamedDecl *, FunctionDecl *>, 4>;
391
392
    void *FromTypePtr;
393
    void *ToTypePtr;
394
    char Buffer[sizeof(ConversionSet)];
395
396
9
    QualType getFromType() const {
397
9
      return QualType::getFromOpaquePtr(FromTypePtr);
398
9
    }
399
400
9
    QualType getToType() const {
401
9
      return QualType::getFromOpaquePtr(ToTypePtr);
402
9
    }
403
404
248
    void setFromType(QualType T) { FromTypePtr = T.getAsOpaquePtr(); }
405
248
    void setToType(QualType T) { ToTypePtr = T.getAsOpaquePtr(); }
406
407
1.70k
    ConversionSet &conversions() {
408
1.70k
      return *reinterpret_cast<ConversionSet*>(Buffer);
409
1.70k
    }
410
411
318
    const ConversionSet &conversions() const {
412
318
      return *reinterpret_cast<const ConversionSet*>(Buffer);
413
318
    }
414
415
568
    void addConversion(NamedDecl *Found, FunctionDecl *D) {
416
568
      conversions().push_back(std::make_pair(Found, D));
417
568
    }
418
419
    using iterator = ConversionSet::iterator;
420
421
0
    iterator begin() { return conversions().begin(); }
422
0
    iterator end() { return conversions().end(); }
423
424
    using const_iterator = ConversionSet::const_iterator;
425
426
9
    const_iterator begin() const { return conversions().begin(); }
427
9
    const_iterator end() const { return conversions().end(); }
428
429
    void construct();
430
    void destruct();
431
    void copyFrom(const AmbiguousConversionSequence &);
432
  };
433
434
  /// BadConversionSequence - Records information about an invalid
435
  /// conversion sequence.
436
0
  struct BadConversionSequence {
437
    enum FailureKind {
438
      no_conversion,
439
      unrelated_class,
440
      bad_qualifiers,
441
      lvalue_ref_to_rvalue,
442
      rvalue_ref_to_lvalue
443
    };
444
445
    // This can be null, e.g. for implicit object arguments.
446
    Expr *FromExpr;
447
448
    FailureKind Kind;
449
450
  private:
451
    // The type we're converting from (an opaque QualType).
452
    void *FromTy;
453
454
    // The type we're converting to (an opaque QualType).
455
    void *ToTy;
456
457
  public:
458
9.41M
    void init(FailureKind K, Expr *From, QualType To) {
459
9.41M
      init(K, From->getType(), To);
460
9.41M
      FromExpr = From;
461
9.41M
    }
462
463
9.68M
    void init(FailureKind K, QualType From, QualType To) {
464
9.68M
      Kind = K;
465
9.68M
      FromExpr = nullptr;
466
9.68M
      setFromType(From);
467
9.68M
      setToType(To);
468
9.68M
    }
469
470
401k
    QualType getFromType() const { return QualType::getFromOpaquePtr(FromTy); }
471
401k
    QualType getToType() const { return QualType::getFromOpaquePtr(ToTy); }
472
473
    void setFromExpr(Expr *E) {
474
      FromExpr = E;
475
      setFromType(E->getType());
476
    }
477
478
9.68M
    void setFromType(QualType T) { FromTy = T.getAsOpaquePtr(); }
479
9.68M
    void setToType(QualType T) { ToTy = T.getAsOpaquePtr(); }
480
  };
481
482
  /// ImplicitConversionSequence - Represents an implicit conversion
483
  /// sequence, which may be a standard conversion sequence
484
  /// (C++ 13.3.3.1.1), user-defined conversion sequence (C++ 13.3.3.1.2),
485
  /// or an ellipsis conversion sequence (C++ 13.3.3.1.3).
486
  class ImplicitConversionSequence {
487
  public:
488
    /// Kind - The kind of implicit conversion sequence. BadConversion
489
    /// specifies that there is no conversion from the source type to
490
    /// the target type.  AmbiguousConversion represents the unique
491
    /// ambiguous conversion (C++0x [over.best.ics]p10).
492
    enum Kind {
493
      StandardConversion = 0,
494
      UserDefinedConversion,
495
      AmbiguousConversion,
496
      EllipsisConversion,
497
      BadConversion
498
    };
499
500
  private:
501
    enum {
502
      Uninitialized = BadConversion + 1
503
    };
504
505
    /// ConversionKind - The kind of implicit conversion sequence.
506
    unsigned ConversionKind : 30;
507
508
    /// Whether the target is really a std::initializer_list, and the
509
    /// sequence only represents the worst element conversion.
510
    unsigned StdInitializerListElement : 1;
511
512
45.5M
    void setKind(Kind K) {
513
45.5M
      destruct();
514
45.5M
      ConversionKind = K;
515
45.5M
    }
516
517
203M
    void destruct() {
518
203M
      if (ConversionKind == AmbiguousConversion) 
Ambiguous.destruct()570
;
519
203M
    }
520
521
  public:
522
    union {
523
      /// When ConversionKind == StandardConversion, provides the
524
      /// details of the standard conversion sequence.
525
      StandardConversionSequence Standard;
526
527
      /// When ConversionKind == UserDefinedConversion, provides the
528
      /// details of the user-defined conversion sequence.
529
      UserDefinedConversionSequence UserDefined;
530
531
      /// When ConversionKind == AmbiguousConversion, provides the
532
      /// details of the ambiguous conversion.
533
      AmbiguousConversionSequence Ambiguous;
534
535
      /// When ConversionKind == BadConversion, provides the details
536
      /// of the bad conversion.
537
      BadConversionSequence Bad;
538
    };
539
540
    ImplicitConversionSequence()
541
86.6M
        : ConversionKind(Uninitialized), StdInitializerListElement(false) {
542
86.6M
      Standard.setAsIdentityConversion();
543
86.6M
    }
544
545
    ImplicitConversionSequence(const ImplicitConversionSequence &Other)
546
        : ConversionKind(Other.ConversionKind),
547
71.2M
          StdInitializerListElement(Other.StdInitializerListElement) {
548
71.2M
      switch (ConversionKind) {
549
71.2M
      
case Uninitialized: break0
;
550
71.2M
      
case StandardConversion: Standard = Other.Standard; break63.0M
;
551
71.2M
      
case UserDefinedConversion: UserDefined = Other.UserDefined; break358k
;
552
71.2M
      
case AmbiguousConversion: Ambiguous.copyFrom(Other.Ambiguous); break300
;
553
71.2M
      
case EllipsisConversion: break0
;
554
71.2M
      
case BadConversion: Bad = Other.Bad; break7.86M
;
555
71.2M
      }
556
71.2M
    }
557
558
    ImplicitConversionSequence &
559
32.5M
    operator=(const ImplicitConversionSequence &Other) {
560
32.5M
      destruct();
561
32.5M
      new (this) ImplicitConversionSequence(Other);
562
32.5M
      return *this;
563
32.5M
    }
564
565
125M
    ~ImplicitConversionSequence() {
566
125M
      destruct();
567
125M
    }
568
569
549M
    Kind getKind() const {
570
549M
      assert(isInitialized() && "querying uninitialized conversion");
571
549M
      return Kind(ConversionKind);
572
549M
    }
573
574
    /// Return a ranking of the implicit conversion sequence
575
    /// kind, where smaller ranks represent better conversion
576
    /// sequences.
577
    ///
578
    /// In particular, this routine gives user-defined conversion
579
    /// sequences and ambiguous conversion sequences the same rank,
580
    /// per C++ [over.best.ics]p10.
581
117M
    unsigned getKindRank() const {
582
117M
      switch (getKind()) {
583
117M
      case StandardConversion:
584
115M
        return 0;
585
117M
586
117M
      case UserDefinedConversion:
587
1.65M
      case AmbiguousConversion:
588
1.65M
        return 1;
589
1.65M
590
1.65M
      case EllipsisConversion:
591
109k
        return 2;
592
1.65M
593
1.65M
      case BadConversion:
594
46.9k
        return 3;
595
0
      }
596
0
597
0
      llvm_unreachable("Invalid ImplicitConversionSequence::Kind!");
598
0
    }
599
600
68.9M
    bool isBad() const { return getKind() == BadConversion; }
601
164M
    bool isStandard() const { return getKind() == StandardConversion; }
602
35.1k
    bool isEllipsis() const { return getKind() == EllipsisConversion; }
603
575
    bool isAmbiguous() const { return getKind() == AmbiguousConversion; }
604
125M
    bool isUserDefined() const { return getKind() == UserDefinedConversion; }
605
644
    bool isFailure() const { return isBad() || 
isAmbiguous()494
; }
606
607
    /// Determines whether this conversion sequence has been
608
    /// initialized.  Most operations should never need to query
609
    /// uninitialized conversions and should assert as above.
610
5.74M
    bool isInitialized() const { return ConversionKind != Uninitialized; }
611
612
    /// Sets this sequence as a bad conversion for an explicit argument.
613
    void setBad(BadConversionSequence::FailureKind Failure,
614
9.41M
                Expr *FromExpr, QualType ToType) {
615
9.41M
      setKind(BadConversion);
616
9.41M
      Bad.init(Failure, FromExpr, ToType);
617
9.41M
    }
618
619
    /// Sets this sequence as a bad conversion for an implicit argument.
620
    void setBad(BadConversionSequence::FailureKind Failure,
621
265k
                QualType FromType, QualType ToType) {
622
265k
      setKind(BadConversion);
623
265k
      Bad.init(Failure, FromType, ToType);
624
265k
    }
625
626
35.4M
    void setStandard() { setKind(StandardConversion); }
627
89.8k
    void setEllipsis() { setKind(EllipsisConversion); }
628
329k
    void setUserDefined() { setKind(UserDefinedConversion); }
629
630
270
    void setAmbiguous() {
631
270
      if (ConversionKind == AmbiguousConversion) 
return0
;
632
270
      ConversionKind = AmbiguousConversion;
633
270
      Ambiguous.construct();
634
270
    }
635
636
349
    void setAsIdentityConversion(QualType T) {
637
349
      setStandard();
638
349
      Standard.setAsIdentityConversion();
639
349
      Standard.setFromType(T);
640
349
      Standard.setAllToTypes(T);
641
349
    }
642
643
    /// Whether the target is really a std::initializer_list, and the
644
    /// sequence only represents the worst element conversion.
645
87.6M
    bool isStdInitializerListElement() const {
646
87.6M
      return StdInitializerListElement;
647
87.6M
    }
648
649
847
    void setStdInitializerListElement(bool V = true) {
650
847
      StdInitializerListElement = V;
651
847
    }
652
653
    // The result of a comparison between implicit conversion
654
    // sequences. Use Sema::CompareImplicitConversionSequences to
655
    // actually perform the comparison.
656
    enum CompareKind {
657
      Better = -1,
658
      Indistinguishable = 0,
659
      Worse = 1
660
    };
661
662
    void DiagnoseAmbiguousConversion(Sema &S,
663
                                     SourceLocation CaretLoc,
664
                                     const PartialDiagnostic &PDiag) const;
665
666
    void dump() const;
667
  };
668
669
  enum OverloadFailureKind {
670
    ovl_fail_too_many_arguments,
671
    ovl_fail_too_few_arguments,
672
    ovl_fail_bad_conversion,
673
    ovl_fail_bad_deduction,
674
675
    /// This conversion candidate was not considered because it
676
    /// duplicates the work of a trivial or derived-to-base
677
    /// conversion.
678
    ovl_fail_trivial_conversion,
679
680
    /// This conversion candidate was not considered because it is
681
    /// an illegal instantiation of a constructor temploid: it is
682
    /// callable with one argument, we only have one argument, and
683
    /// its first parameter type is exactly the type of the class.
684
    ///
685
    /// Defining such a constructor directly is illegal, and
686
    /// template-argument deduction is supposed to ignore such
687
    /// instantiations, but we can still get one with the right
688
    /// kind of implicit instantiation.
689
    ovl_fail_illegal_constructor,
690
691
    /// This conversion candidate is not viable because its result
692
    /// type is not implicitly convertible to the desired type.
693
    ovl_fail_bad_final_conversion,
694
695
    /// This conversion function template specialization candidate is not
696
    /// viable because the final conversion was not an exact match.
697
    ovl_fail_final_conversion_not_exact,
698
699
    /// (CUDA) This candidate was not viable because the callee
700
    /// was not accessible from the caller's target (i.e. host->device,
701
    /// global->host, device->host).
702
    ovl_fail_bad_target,
703
704
    /// This candidate function was not viable because an enable_if
705
    /// attribute disabled it.
706
    ovl_fail_enable_if,
707
708
    /// This candidate was not viable because its address could not be taken.
709
    ovl_fail_addr_not_available,
710
711
    /// This candidate was not viable because its OpenCL extension is disabled.
712
    ovl_fail_ext_disabled,
713
714
    /// This inherited constructor is not viable because it would slice the
715
    /// argument.
716
    ovl_fail_inhctor_slice,
717
718
    /// This candidate was not viable because it is a non-default multiversioned
719
    /// function.
720
    ovl_non_default_multiversion_function,
721
  };
722
723
  /// A list of implicit conversion sequences for the arguments of an
724
  /// OverloadCandidate.
725
  using ConversionSequenceList =
726
      llvm::MutableArrayRef<ImplicitConversionSequence>;
727
728
  /// OverloadCandidate - A single candidate in an overload set (C++ 13.3).
729
  struct OverloadCandidate {
730
    /// Function - The actual function that this candidate
731
    /// represents. When NULL, this is a built-in candidate
732
    /// (C++ [over.oper]) or a surrogate for a conversion to a
733
    /// function pointer or reference (C++ [over.call.object]).
734
    FunctionDecl *Function;
735
736
    /// FoundDecl - The original declaration that was looked up /
737
    /// invented / otherwise found, together with its access.
738
    /// Might be a UsingShadowDecl or a FunctionTemplateDecl.
739
    DeclAccessPair FoundDecl;
740
741
    /// BuiltinParamTypes - Provides the parameter types of a built-in overload
742
    /// candidate. Only valid when Function is NULL.
743
    QualType BuiltinParamTypes[3];
744
745
    /// Surrogate - The conversion function for which this candidate
746
    /// is a surrogate, but only if IsSurrogate is true.
747
    CXXConversionDecl *Surrogate;
748
749
    /// The conversion sequences used to convert the function arguments
750
    /// to the function parameters.
751
    ConversionSequenceList Conversions;
752
753
    /// The FixIt hints which can be used to fix the Bad candidate.
754
    ConversionFixItGenerator Fix;
755
756
    /// Viable - True to indicate that this overload candidate is viable.
757
    bool Viable : 1;
758
759
    /// IsSurrogate - True to indicate that this candidate is a
760
    /// surrogate for a conversion to a function pointer or reference
761
    /// (C++ [over.call.object]).
762
    bool IsSurrogate : 1;
763
764
    /// IgnoreObjectArgument - True to indicate that the first
765
    /// argument's conversion, which for this function represents the
766
    /// implicit object argument, should be ignored. This will be true
767
    /// when the candidate is a static member function (where the
768
    /// implicit object argument is just a placeholder) or a
769
    /// non-static member function when the call doesn't have an
770
    /// object argument.
771
    bool IgnoreObjectArgument : 1;
772
773
    /// True if the candidate was found using ADL.
774
    CallExpr::ADLCallKind IsADLCandidate : 1;
775
776
    /// FailureKind - The reason why this candidate is not viable.
777
    /// Actually an OverloadFailureKind.
778
    unsigned char FailureKind;
779
780
    /// The number of call arguments that were explicitly provided,
781
    /// to be used while performing partial ordering of function templates.
782
    unsigned ExplicitCallArguments;
783
784
    union {
785
      DeductionFailureInfo DeductionFailure;
786
787
      /// FinalConversion - For a conversion function (where Function is
788
      /// a CXXConversionDecl), the standard conversion that occurs
789
      /// after the call to the overload candidate to convert the result
790
      /// of calling the conversion function to the required type.
791
      StandardConversionSequence FinalConversion;
792
    };
793
794
    /// hasAmbiguousConversion - Returns whether this overload
795
    /// candidate requires an ambiguous conversion or not.
796
    bool hasAmbiguousConversion() const {
797
      for (auto &C : Conversions) {
798
        if (!C.isInitialized()) return false;
799
        if (C.isAmbiguous()) return true;
800
      }
801
      return false;
802
    }
803
804
394k
    bool TryToFixBadConversion(unsigned Idx, Sema &S) {
805
394k
      bool CanFix = Fix.tryToFixConversion(
806
394k
                      Conversions[Idx].Bad.FromExpr,
807
394k
                      Conversions[Idx].Bad.getFromType(),
808
394k
                      Conversions[Idx].Bad.getToType(), S);
809
394k
810
394k
      // If at least one conversion fails, the candidate cannot be fixed.
811
394k
      if (!CanFix)
812
394k
        Fix.clear();
813
394k
814
394k
      return CanFix;
815
394k
    }
816
817
874
    unsigned getNumParams() const {
818
874
      if (IsSurrogate) {
819
9
        auto STy = Surrogate->getConversionType();
820
18
        while (STy->isPointerType() || 
STy->isReferenceType()9
)
821
9
          STy = STy->getPointeeType();
822
9
        return STy->getAs<FunctionProtoType>()->getNumParams();
823
9
      }
824
865
      if (Function)
825
865
        return Function->getNumParams();
826
0
      return ExplicitCallArguments;
827
0
    }
828
829
  private:
830
    friend class OverloadCandidateSet;
831
22.5M
    OverloadCandidate() : IsADLCandidate(CallExpr::NotADL) {}
832
  };
833
834
  /// OverloadCandidateSet - A set of overload candidates, used in C++
835
  /// overload resolution (C++ 13.3).
836
  class OverloadCandidateSet {
837
  public:
838
    enum CandidateSetKind {
839
      /// Normal lookup.
840
      CSK_Normal,
841
842
      /// C++ [over.match.oper]:
843
      /// Lookup of operator function candidates in a call using operator
844
      /// syntax. Candidates that have no parameters of class type will be
845
      /// skipped unless there is a parameter of (reference to) enum type and
846
      /// the corresponding argument is of the same enum type.
847
      CSK_Operator,
848
849
      /// C++ [over.match.copy]:
850
      /// Copy-initialization of an object of class type by user-defined
851
      /// conversion.
852
      CSK_InitByUserDefinedConversion,
853
854
      /// C++ [over.match.ctor], [over.match.list]
855
      /// Initialization of an object of class type by constructor,
856
      /// using either a parenthesized or braced list of arguments.
857
      CSK_InitByConstructor,
858
    };
859
860
  private:
861
    SmallVector<OverloadCandidate, 16> Candidates;
862
    llvm::SmallPtrSet<Decl *, 16> Functions;
863
864
    // Allocator for ConversionSequenceLists. We store the first few of these
865
    // inline to avoid allocation for small sets.
866
    llvm::BumpPtrAllocator SlabAllocator;
867
868
    SourceLocation Loc;
869
    CandidateSetKind Kind;
870
871
    constexpr static unsigned NumInlineBytes =
872
        24 * sizeof(ImplicitConversionSequence);
873
    unsigned NumInlineBytesUsed = 0;
874
    llvm::AlignedCharArray<alignof(void *), NumInlineBytes> InlineSpace;
875
876
    /// If we have space, allocates from inline storage. Otherwise, allocates
877
    /// from the slab allocator.
878
    /// FIXME: It would probably be nice to have a SmallBumpPtrAllocator
879
    /// instead.
880
    /// FIXME: Now that this only allocates ImplicitConversionSequences, do we
881
    /// want to un-generalize this?
882
    template <typename T>
883
22.5M
    T *slabAllocate(unsigned N) {
884
22.5M
      // It's simpler if this doesn't need to consider alignment.
885
22.5M
      static_assert(alignof(T) == alignof(void *),
886
22.5M
                    "Only works for pointer-aligned types.");
887
22.5M
      static_assert(std::is_trivial<T>::value ||
888
22.5M
                        std::is_same<ImplicitConversionSequence, T>::value,
889
22.5M
                    "Add destruction logic to OverloadCandidateSet::clear().");
890
22.5M
891
22.5M
      unsigned NBytes = sizeof(T) * N;
892
22.5M
      if (NBytes > NumInlineBytes - NumInlineBytesUsed)
893
13.8M
        return SlabAllocator.Allocate<T>(N);
894
8.70M
      char *FreeSpaceStart = InlineSpace.buffer + NumInlineBytesUsed;
895
8.70M
      assert(uintptr_t(FreeSpaceStart) % alignof(void *) == 0 &&
896
8.70M
             "Misaligned storage!");
897
8.70M
898
8.70M
      NumInlineBytesUsed += NBytes;
899
8.70M
      return reinterpret_cast<T *>(FreeSpaceStart);
900
8.70M
    }
901
902
    void destroyCandidates();
903
904
  public:
905
    OverloadCandidateSet(SourceLocation Loc, CandidateSetKind CSK)
906
18.9M
        : Loc(Loc), Kind(CSK) {}
907
    OverloadCandidateSet(const OverloadCandidateSet &) = delete;
908
    OverloadCandidateSet &operator=(const OverloadCandidateSet &) = delete;
909
18.9M
    ~OverloadCandidateSet() { destroyCandidates(); }
910
911
5.06M
    SourceLocation getLocation() const { return Loc; }
912
4.10M
    CandidateSetKind getKind() const { return Kind; }
913
914
    /// Determine when this overload candidate will be new to the
915
    /// overload set.
916
8.88M
    bool isNewCandidate(Decl *F) {
917
8.88M
      return Functions.insert(F->getCanonicalDecl()).second;
918
8.88M
    }
919
920
    /// Clear out all of the candidates.
921
    void clear(CandidateSetKind CSK);
922
923
    using iterator = SmallVectorImpl<OverloadCandidate>::iterator;
924
925
27.2M
    iterator begin() { return Candidates.begin(); }
926
49.3M
    iterator end() { return Candidates.end(); }
927
928
3.94M
    size_t size() const { return Candidates.size(); }
929
1.69M
    bool empty() const { return Candidates.empty(); }
930
931
    /// Allocate storage for conversion sequences for NumConversions
932
    /// conversions.
933
    ConversionSequenceList
934
22.5M
    allocateConversionSequences(unsigned NumConversions) {
935
22.5M
      ImplicitConversionSequence *Conversions =
936
22.5M
          slabAllocate<ImplicitConversionSequence>(NumConversions);
937
22.5M
938
22.5M
      // Construct the new objects.
939
60.4M
      for (unsigned I = 0; I != NumConversions; 
++I37.8M
)
940
37.8M
        new (&Conversions[I]) ImplicitConversionSequence();
941
22.5M
942
22.5M
      return ConversionSequenceList(Conversions, NumConversions);
943
22.5M
    }
944
945
    /// Add a new candidate with NumConversions conversion sequence slots
946
    /// to the overload set.
947
    OverloadCandidate &addCandidate(unsigned NumConversions = 0,
948
22.5M
                                    ConversionSequenceList Conversions = None) {
949
22.5M
      assert((Conversions.empty() || Conversions.size() == NumConversions) &&
950
22.5M
             "preallocated conversion sequence has wrong length");
951
22.5M
952
22.5M
      Candidates.push_back(OverloadCandidate());
953
22.5M
      OverloadCandidate &C = Candidates.back();
954
22.5M
      C.Conversions = Conversions.empty()
955
22.5M
                          ? 
allocateConversionSequences(NumConversions)21.8M
956
22.5M
                          : 
Conversions650k
;
957
22.5M
      return C;
958
22.5M
    }
959
960
    /// Find the best viable function on this overload set, if it exists.
961
    OverloadingResult BestViableFunction(Sema &S, SourceLocation Loc,
962
                                         OverloadCandidateSet::iterator& Best);
963
964
    void NoteCandidates(Sema &S,
965
                        OverloadCandidateDisplayKind OCD,
966
                        ArrayRef<Expr *> Args,
967
                        StringRef Opc = "",
968
                        SourceLocation Loc = SourceLocation(),
969
                        llvm::function_ref<bool(OverloadCandidate&)> Filter =
970
416k
                          [](OverloadCandidate&) { return true; });
971
  };
972
973
  bool isBetterOverloadCandidate(Sema &S,
974
                                 const OverloadCandidate &Cand1,
975
                                 const OverloadCandidate &Cand2,
976
                                 SourceLocation Loc,
977
                                 OverloadCandidateSet::CandidateSetKind Kind);
978
979
  struct ConstructorInfo {
980
    DeclAccessPair FoundDecl;
981
    CXXConstructorDecl *Constructor;
982
    FunctionTemplateDecl *ConstructorTmpl;
983
984
1.67M
    explicit operator bool() const { return Constructor; }
985
  };
986
987
  // FIXME: Add an AddOverloadCandidate / AddTemplateOverloadCandidate overload
988
  // that takes one of these.
989
3.84M
  inline ConstructorInfo getConstructorInfo(NamedDecl *ND) {
990
3.84M
    if (isa<UsingDecl>(ND))
991
1.05k
      return ConstructorInfo{};
992
3.83M
993
3.83M
    // For constructors, the access check is performed against the underlying
994
3.83M
    // declaration, not the found declaration.
995
3.83M
    auto *D = ND->getUnderlyingDecl();
996
3.83M
    ConstructorInfo Info = {DeclAccessPair::make(ND, D->getAccess()), nullptr,
997
3.83M
                            nullptr};
998
3.83M
    Info.ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D);
999
3.83M
    if (Info.ConstructorTmpl)
1000
621k
      D = Info.ConstructorTmpl->getTemplatedDecl();
1001
3.83M
    Info.Constructor = dyn_cast<CXXConstructorDecl>(D);
1002
3.83M
    return Info;
1003
3.83M
  }
1004
1005
} // namespace clang
1006
1007
#endif // LLVM_CLANG_SEMA_OVERLOAD_H