Coverage Report

Created: 2018-12-11 17:59

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