2017-11-22 07:26:08 +08:00
|
|
|
//===- TemplateBase.cpp - Common template AST class implementation --------===//
|
2009-10-29 15:48:15 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-10-29 15:48:15 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements common classes used throughout C++ template
|
|
|
|
// representations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/TemplateBase.h"
|
2010-12-21 00:52:59 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2017-11-22 07:26:08 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2009-10-29 15:48:15 +08:00
|
|
|
#include "clang/AST/DeclBase.h"
|
2009-11-23 20:52:47 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2020-03-16 20:43:40 +08:00
|
|
|
#include "clang/AST/DependenceFlags.h"
|
2009-10-29 15:48:15 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2011-01-04 01:17:50 +08:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
2017-11-22 07:26:08 +08:00
|
|
|
#include "clang/AST/PrettyPrinter.h"
|
|
|
|
#include "clang/AST/TemplateName.h"
|
2011-02-19 08:21:00 +08:00
|
|
|
#include "clang/AST/Type.h"
|
2009-10-29 16:12:44 +08:00
|
|
|
#include "clang/AST/TypeLoc.h"
|
2010-05-09 01:41:32 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2017-11-22 07:26:08 +08:00
|
|
|
#include "clang/Basic/LLVM.h"
|
|
|
|
#include "clang/Basic/LangOptions.h"
|
|
|
|
#include "clang/Basic/SourceLocation.h"
|
|
|
|
#include "llvm/ADT/APSInt.h"
|
2010-12-21 00:52:59 +08:00
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
2017-11-22 07:26:08 +08:00
|
|
|
#include "llvm/ADT/None.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2017-11-22 07:26:08 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-02 01:12:56 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-11-22 07:26:08 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring>
|
2009-10-29 15:48:15 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Print a template integral argument value.
|
2011-02-19 08:21:00 +08:00
|
|
|
///
|
|
|
|
/// \param TemplArg the TemplateArgument instance to print.
|
|
|
|
///
|
|
|
|
/// \param Out the raw_ostream instance to use for printing.
|
2014-12-13 12:31:07 +08:00
|
|
|
///
|
|
|
|
/// \param Policy the printing policy for EnumConstantDecl printing.
|
2011-02-19 08:21:00 +08:00
|
|
|
static void printIntegral(const TemplateArgument &TemplArg,
|
2014-12-13 12:31:07 +08:00
|
|
|
raw_ostream &Out, const PrintingPolicy& Policy) {
|
2017-11-22 07:26:08 +08:00
|
|
|
const Type *T = TemplArg.getIntegralType().getTypePtr();
|
2012-06-07 23:09:51 +08:00
|
|
|
const llvm::APSInt &Val = TemplArg.getAsIntegral();
|
2011-02-19 08:21:00 +08:00
|
|
|
|
2014-12-13 12:38:19 +08:00
|
|
|
if (const EnumType *ET = T->getAs<EnumType>()) {
|
2014-12-13 12:31:07 +08:00
|
|
|
for (const EnumConstantDecl* ECD : ET->getDecl()->enumerators()) {
|
2015-01-09 08:58:16 +08:00
|
|
|
// In Sema::CheckTemplateArugment, enum template arguments value are
|
|
|
|
// extended to the size of the integer underlying the enum type. This
|
|
|
|
// may create a size difference between the enum value and template
|
|
|
|
// argument value, requiring isSameValue here instead of operator==.
|
|
|
|
if (llvm::APSInt::isSameValue(ECD->getInitVal(), Val)) {
|
2014-12-13 12:31:07 +08:00
|
|
|
ECD->printQualifiedName(Out, Policy);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 10:04:40 +08:00
|
|
|
if (T->isBooleanType() && !Policy.MSVCFormatting) {
|
2012-06-07 23:09:51 +08:00
|
|
|
Out << (Val.getBoolValue() ? "true" : "false");
|
2011-02-19 08:21:00 +08:00
|
|
|
} else if (T->isCharType()) {
|
2012-06-07 23:09:51 +08:00
|
|
|
const char Ch = Val.getZExtValue();
|
2011-02-26 04:09:13 +08:00
|
|
|
Out << ((Ch == '\'') ? "'\\" : "'");
|
2012-02-04 21:45:25 +08:00
|
|
|
Out.write_escaped(StringRef(&Ch, 1), /*UseHexEscapes=*/ true);
|
2011-02-26 04:09:13 +08:00
|
|
|
Out << "'";
|
2011-02-19 08:21:00 +08:00
|
|
|
} else {
|
2012-06-07 23:09:51 +08:00
|
|
|
Out << Val;
|
2011-02-19 08:21:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-29 15:48:15 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TemplateArgument Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-12-23 02:12:37 +08:00
|
|
|
TemplateArgument::TemplateArgument(ASTContext &Ctx, const llvm::APSInt &Value,
|
|
|
|
QualType Type) {
|
2013-08-22 07:05:56 +08:00
|
|
|
Integer.Kind = Integral;
|
2012-06-07 23:09:51 +08:00
|
|
|
// Copy the APSInt value into our decomposed form.
|
|
|
|
Integer.BitWidth = Value.getBitWidth();
|
|
|
|
Integer.IsUnsigned = Value.isUnsigned();
|
|
|
|
// If the value is large, we have to get additional memory from the ASTContext
|
2012-06-07 23:54:03 +08:00
|
|
|
unsigned NumWords = Value.getNumWords();
|
|
|
|
if (NumWords > 1) {
|
|
|
|
void *Mem = Ctx.Allocate(NumWords * sizeof(uint64_t));
|
|
|
|
std::memcpy(Mem, Value.getRawData(), NumWords * sizeof(uint64_t));
|
2012-06-07 23:09:51 +08:00
|
|
|
Integer.pVal = static_cast<uint64_t *>(Mem);
|
|
|
|
} else {
|
|
|
|
Integer.VAL = Value.getZExtValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
Integer.Type = Type.getAsOpaquePtr();
|
|
|
|
}
|
|
|
|
|
2015-08-05 17:40:22 +08:00
|
|
|
TemplateArgument
|
|
|
|
TemplateArgument::CreatePackCopy(ASTContext &Context,
|
|
|
|
ArrayRef<TemplateArgument> Args) {
|
|
|
|
if (Args.empty())
|
2012-09-26 10:36:12 +08:00
|
|
|
return getEmptyPack();
|
2015-08-05 17:40:22 +08:00
|
|
|
|
|
|
|
return TemplateArgument(Args.copy(Context));
|
2011-01-12 07:09:57 +08:00
|
|
|
}
|
|
|
|
|
[AST] Refactor propagation of dependency bits. NFC
Summary:
This changes introduces an enum to represent dependencies as a bitmask
and extract common patterns from code that computes dependency bits into
helper functions.
Reviewers: rsmith, martong, shafik, ilya-biryukov, hokein
Subscribers: hokein, sammccall, Mordante, riccibruno, merge_guards_bot, rnkovacs, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D71920
2020-03-02 23:07:09 +08:00
|
|
|
TemplateArgumentDependence TemplateArgument::getDependence() const {
|
|
|
|
auto Deps = TemplateArgumentDependence::None;
|
2010-12-15 09:34:56 +08:00
|
|
|
switch (getKind()) {
|
|
|
|
case Null:
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Should not have a NULL template argument");
|
2010-12-15 09:34:56 +08:00
|
|
|
|
|
|
|
case Type:
|
[AST] Refactor propagation of dependency bits. NFC
Summary:
This changes introduces an enum to represent dependencies as a bitmask
and extract common patterns from code that computes dependency bits into
helper functions.
Reviewers: rsmith, martong, shafik, ilya-biryukov, hokein
Subscribers: hokein, sammccall, Mordante, riccibruno, merge_guards_bot, rnkovacs, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D71920
2020-03-02 23:07:09 +08:00
|
|
|
Deps = toTemplateArgumentDependence(getAsType()->getDependence());
|
|
|
|
if (isa<PackExpansionType>(getAsType()))
|
|
|
|
Deps |= TemplateArgumentDependence::Dependent;
|
|
|
|
return Deps;
|
2010-12-15 09:34:56 +08:00
|
|
|
|
|
|
|
case Template:
|
[AST] Refactor propagation of dependency bits. NFC
Summary:
This changes introduces an enum to represent dependencies as a bitmask
and extract common patterns from code that computes dependency bits into
helper functions.
Reviewers: rsmith, martong, shafik, ilya-biryukov, hokein
Subscribers: hokein, sammccall, Mordante, riccibruno, merge_guards_bot, rnkovacs, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D71920
2020-03-02 23:07:09 +08:00
|
|
|
return toTemplateArgumentDependence(getAsTemplate().getDependence());
|
2011-01-06 02:58:31 +08:00
|
|
|
|
|
|
|
case TemplateExpansion:
|
[AST] Refactor propagation of dependency bits. NFC
Summary:
This changes introduces an enum to represent dependencies as a bitmask
and extract common patterns from code that computes dependency bits into
helper functions.
Reviewers: rsmith, martong, shafik, ilya-biryukov, hokein
Subscribers: hokein, sammccall, Mordante, riccibruno, merge_guards_bot, rnkovacs, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D71920
2020-03-02 23:07:09 +08:00
|
|
|
return TemplateArgumentDependence::Dependent |
|
|
|
|
TemplateArgumentDependence::Instantiation;
|
2011-01-06 02:58:31 +08:00
|
|
|
|
2020-12-23 02:12:40 +08:00
|
|
|
case Declaration: {
|
|
|
|
auto *DC = dyn_cast<DeclContext>(getAsDecl());
|
|
|
|
if (!DC)
|
|
|
|
DC = getAsDecl()->getDeclContext();
|
|
|
|
if (DC->isDependentContext())
|
|
|
|
Deps = TemplateArgumentDependence::Dependent |
|
|
|
|
TemplateArgumentDependence::Instantiation;
|
|
|
|
return Deps;
|
|
|
|
}
|
|
|
|
|
2012-09-26 10:36:12 +08:00
|
|
|
case NullPtr:
|
2010-12-15 09:34:56 +08:00
|
|
|
case Integral:
|
[AST] Refactor propagation of dependency bits. NFC
Summary:
This changes introduces an enum to represent dependencies as a bitmask
and extract common patterns from code that computes dependency bits into
helper functions.
Reviewers: rsmith, martong, shafik, ilya-biryukov, hokein
Subscribers: hokein, sammccall, Mordante, riccibruno, merge_guards_bot, rnkovacs, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D71920
2020-03-02 23:07:09 +08:00
|
|
|
return TemplateArgumentDependence::None;
|
2010-12-15 09:34:56 +08:00
|
|
|
|
|
|
|
case Expression:
|
[AST] Refactor propagation of dependency bits. NFC
Summary:
This changes introduces an enum to represent dependencies as a bitmask
and extract common patterns from code that computes dependency bits into
helper functions.
Reviewers: rsmith, martong, shafik, ilya-biryukov, hokein
Subscribers: hokein, sammccall, Mordante, riccibruno, merge_guards_bot, rnkovacs, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D71920
2020-03-02 23:07:09 +08:00
|
|
|
Deps = toTemplateArgumentDependence(getAsExpr()->getDependence());
|
2020-12-23 02:12:40 +08:00
|
|
|
if (isa<PackExpansionExpr>(getAsExpr()))
|
|
|
|
Deps |= TemplateArgumentDependence::Dependent |
|
|
|
|
TemplateArgumentDependence::Instantiation;
|
[AST] Refactor propagation of dependency bits. NFC
Summary:
This changes introduces an enum to represent dependencies as a bitmask
and extract common patterns from code that computes dependency bits into
helper functions.
Reviewers: rsmith, martong, shafik, ilya-biryukov, hokein
Subscribers: hokein, sammccall, Mordante, riccibruno, merge_guards_bot, rnkovacs, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D71920
2020-03-02 23:07:09 +08:00
|
|
|
return Deps;
|
2010-12-15 09:34:56 +08:00
|
|
|
|
|
|
|
case Pack:
|
2014-07-16 05:32:31 +08:00
|
|
|
for (const auto &P : pack_elements())
|
[AST] Refactor propagation of dependency bits. NFC
Summary:
This changes introduces an enum to represent dependencies as a bitmask
and extract common patterns from code that computes dependency bits into
helper functions.
Reviewers: rsmith, martong, shafik, ilya-biryukov, hokein
Subscribers: hokein, sammccall, Mordante, riccibruno, merge_guards_bot, rnkovacs, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D71920
2020-03-02 23:07:09 +08:00
|
|
|
Deps |= P.getDependence();
|
|
|
|
return Deps;
|
2010-12-15 09:34:56 +08:00
|
|
|
}
|
[AST] Refactor propagation of dependency bits. NFC
Summary:
This changes introduces an enum to represent dependencies as a bitmask
and extract common patterns from code that computes dependency bits into
helper functions.
Reviewers: rsmith, martong, shafik, ilya-biryukov, hokein
Subscribers: hokein, sammccall, Mordante, riccibruno, merge_guards_bot, rnkovacs, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D71920
2020-03-02 23:07:09 +08:00
|
|
|
llvm_unreachable("unhandled ArgKind");
|
|
|
|
}
|
2010-12-15 09:34:56 +08:00
|
|
|
|
[AST] Refactor propagation of dependency bits. NFC
Summary:
This changes introduces an enum to represent dependencies as a bitmask
and extract common patterns from code that computes dependency bits into
helper functions.
Reviewers: rsmith, martong, shafik, ilya-biryukov, hokein
Subscribers: hokein, sammccall, Mordante, riccibruno, merge_guards_bot, rnkovacs, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D71920
2020-03-02 23:07:09 +08:00
|
|
|
bool TemplateArgument::isDependent() const {
|
|
|
|
return getDependence() & TemplateArgumentDependence::Dependent;
|
2010-12-15 09:34:56 +08:00
|
|
|
}
|
|
|
|
|
2011-07-01 09:22:09 +08:00
|
|
|
bool TemplateArgument::isInstantiationDependent() const {
|
[AST] Refactor propagation of dependency bits. NFC
Summary:
This changes introduces an enum to represent dependencies as a bitmask
and extract common patterns from code that computes dependency bits into
helper functions.
Reviewers: rsmith, martong, shafik, ilya-biryukov, hokein
Subscribers: hokein, sammccall, Mordante, riccibruno, merge_guards_bot, rnkovacs, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D71920
2020-03-02 23:07:09 +08:00
|
|
|
return getDependence() & TemplateArgumentDependence::Instantiation;
|
2011-07-01 09:22:09 +08:00
|
|
|
}
|
|
|
|
|
2010-12-21 06:05:00 +08:00
|
|
|
bool TemplateArgument::isPackExpansion() const {
|
|
|
|
switch (getKind()) {
|
|
|
|
case Null:
|
|
|
|
case Declaration:
|
|
|
|
case Integral:
|
2018-07-31 03:24:48 +08:00
|
|
|
case Pack:
|
2011-01-06 02:58:31 +08:00
|
|
|
case Template:
|
2012-09-26 10:36:12 +08:00
|
|
|
case NullPtr:
|
2010-12-21 06:05:00 +08:00
|
|
|
return false;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-01-06 02:58:31 +08:00
|
|
|
case TemplateExpansion:
|
|
|
|
return true;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-12-21 06:05:00 +08:00
|
|
|
case Type:
|
2011-01-04 01:17:50 +08:00
|
|
|
return isa<PackExpansionType>(getAsType());
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-12-21 06:05:00 +08:00
|
|
|
case Expression:
|
2011-01-04 01:17:50 +08:00
|
|
|
return isa<PackExpansionExpr>(getAsExpr());
|
2010-12-21 06:05:00 +08:00
|
|
|
}
|
2012-01-21 05:50:17 +08:00
|
|
|
|
|
|
|
llvm_unreachable("Invalid TemplateArgument Kind!");
|
2010-12-21 06:05:00 +08:00
|
|
|
}
|
|
|
|
|
Variadic templates: extend Type, NestedNameSpecifier, TemplateName,
and TemplateArgument with an operation that determines whether there
are any unexpanded parameter packs within that construct. Use this
information to diagnose the appearance of the names of parameter packs
that have not been expanded (C++ [temp.variadic]p5). Since this
property is checked often (every declaration, ever expression
statement, etc.), we extend Type and Expr with a bit storing the
result of this computation, rather than walking the AST each time to
determine whether any unexpanded parameter packs occur.
This commit is deficient in several ways, which will be remedied with
future commits:
- Expr has a bit to store the presence of an unexpanded parameter
pack, but it is never set.
- The error messages don't point out where the unexpanded parameter
packs were named in the type/expression, but they should.
- We don't check for unexpanded parameter packs in all of the places
where we should.
- Testing is sparse, pending the resolution of the above three
issues.
llvm-svn: 121724
2010-12-14 06:49:22 +08:00
|
|
|
bool TemplateArgument::containsUnexpandedParameterPack() const {
|
[AST] Refactor propagation of dependency bits. NFC
Summary:
This changes introduces an enum to represent dependencies as a bitmask
and extract common patterns from code that computes dependency bits into
helper functions.
Reviewers: rsmith, martong, shafik, ilya-biryukov, hokein
Subscribers: hokein, sammccall, Mordante, riccibruno, merge_guards_bot, rnkovacs, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D71920
2020-03-02 23:07:09 +08:00
|
|
|
return getDependence() & TemplateArgumentDependence::UnexpandedPack;
|
Variadic templates: extend Type, NestedNameSpecifier, TemplateName,
and TemplateArgument with an operation that determines whether there
are any unexpanded parameter packs within that construct. Use this
information to diagnose the appearance of the names of parameter packs
that have not been expanded (C++ [temp.variadic]p5). Since this
property is checked often (every declaration, ever expression
statement, etc.), we extend Type and Expr with a bit storing the
result of this computation, rather than walking the AST each time to
determine whether any unexpanded parameter packs occur.
This commit is deficient in several ways, which will be remedied with
future commits:
- Expr has a bit to store the presence of an unexpanded parameter
pack, but it is never set.
- The error messages don't point out where the unexpanded parameter
packs were named in the type/expression, but they should.
- We don't check for unexpanded parameter packs in all of the places
where we should.
- Testing is sparse, pending the resolution of the above three
issues.
llvm-svn: 121724
2010-12-14 06:49:22 +08:00
|
|
|
}
|
|
|
|
|
2013-02-21 06:23:23 +08:00
|
|
|
Optional<unsigned> TemplateArgument::getNumTemplateExpansions() const {
|
2013-08-22 07:05:56 +08:00
|
|
|
assert(getKind() == TemplateExpansion);
|
2011-01-15 07:41:42 +08:00
|
|
|
if (TemplateArg.NumExpansions)
|
|
|
|
return TemplateArg.NumExpansions - 1;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
return None;
|
2011-01-15 07:41:42 +08:00
|
|
|
}
|
|
|
|
|
2016-12-23 09:30:39 +08:00
|
|
|
QualType TemplateArgument::getNonTypeTemplateArgumentType() const {
|
|
|
|
switch (getKind()) {
|
|
|
|
case TemplateArgument::Null:
|
|
|
|
case TemplateArgument::Type:
|
|
|
|
case TemplateArgument::Template:
|
|
|
|
case TemplateArgument::TemplateExpansion:
|
|
|
|
case TemplateArgument::Pack:
|
|
|
|
return QualType();
|
|
|
|
|
|
|
|
case TemplateArgument::Integral:
|
|
|
|
return getIntegralType();
|
|
|
|
|
|
|
|
case TemplateArgument::Expression:
|
|
|
|
return getAsExpr()->getType();
|
|
|
|
|
|
|
|
case TemplateArgument::Declaration:
|
|
|
|
return getParamTypeForDecl();
|
|
|
|
|
|
|
|
case TemplateArgument::NullPtr:
|
|
|
|
return getNullPtrType();
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Invalid TemplateArgument Kind!");
|
|
|
|
}
|
|
|
|
|
2009-10-29 15:48:15 +08:00
|
|
|
void TemplateArgument::Profile(llvm::FoldingSetNodeID &ID,
|
2011-01-12 17:06:06 +08:00
|
|
|
const ASTContext &Context) const {
|
2013-08-22 07:05:56 +08:00
|
|
|
ID.AddInteger(getKind());
|
|
|
|
switch (getKind()) {
|
2009-10-29 15:48:15 +08:00
|
|
|
case Null:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Type:
|
|
|
|
getAsType().Profile(ID);
|
|
|
|
break;
|
|
|
|
|
2013-08-22 07:05:56 +08:00
|
|
|
case NullPtr:
|
|
|
|
getNullPtrType().Profile(ID);
|
|
|
|
break;
|
|
|
|
|
2009-10-29 15:48:15 +08:00
|
|
|
case Declaration:
|
2020-11-15 09:37:25 +08:00
|
|
|
getParamTypeForDecl().Profile(ID);
|
2020-10-12 11:14:00 +08:00
|
|
|
ID.AddPointer(getAsDecl()? getAsDecl()->getCanonicalDecl() : nullptr);
|
2009-10-29 15:48:15 +08:00
|
|
|
break;
|
|
|
|
|
2009-11-11 09:00:40 +08:00
|
|
|
case Template:
|
2011-01-06 02:58:31 +08:00
|
|
|
case TemplateExpansion: {
|
|
|
|
TemplateName Template = getAsTemplateOrTemplatePattern();
|
2009-11-23 20:52:47 +08:00
|
|
|
if (TemplateTemplateParmDecl *TTP
|
|
|
|
= dyn_cast_or_null<TemplateTemplateParmDecl>(
|
2011-01-06 02:58:31 +08:00
|
|
|
Template.getAsTemplateDecl())) {
|
2009-11-23 20:52:47 +08:00
|
|
|
ID.AddBoolean(true);
|
|
|
|
ID.AddInteger(TTP->getDepth());
|
|
|
|
ID.AddInteger(TTP->getPosition());
|
2011-01-06 01:40:24 +08:00
|
|
|
ID.AddBoolean(TTP->isParameterPack());
|
2009-11-23 20:52:47 +08:00
|
|
|
} else {
|
|
|
|
ID.AddBoolean(false);
|
2011-01-06 02:58:31 +08:00
|
|
|
ID.AddPointer(Context.getCanonicalTemplateName(Template)
|
|
|
|
.getAsVoidPointer());
|
2009-11-23 20:52:47 +08:00
|
|
|
}
|
2009-11-11 09:00:40 +08:00
|
|
|
break;
|
2011-01-06 02:58:31 +08:00
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2009-10-29 15:48:15 +08:00
|
|
|
case Integral:
|
2020-11-07 10:14:41 +08:00
|
|
|
getAsIntegral().Profile(ID);
|
2020-12-23 02:12:37 +08:00
|
|
|
getIntegralType().Profile(ID);
|
2009-10-29 15:48:15 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Expression:
|
|
|
|
getAsExpr()->Profile(ID, Context, true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Pack:
|
|
|
|
ID.AddInteger(Args.NumArgs);
|
|
|
|
for (unsigned I = 0; I != Args.NumArgs; ++I)
|
|
|
|
Args.Args[I].Profile(ID, Context);
|
|
|
|
}
|
|
|
|
}
|
2009-10-29 16:12:44 +08:00
|
|
|
|
2010-06-11 08:33:02 +08:00
|
|
|
bool TemplateArgument::structurallyEquals(const TemplateArgument &Other) const {
|
|
|
|
if (getKind() != Other.getKind()) return false;
|
|
|
|
|
|
|
|
switch (getKind()) {
|
|
|
|
case Null:
|
|
|
|
case Type:
|
2018-07-31 03:24:48 +08:00
|
|
|
case Expression:
|
2012-09-26 10:36:12 +08:00
|
|
|
case NullPtr:
|
2013-08-22 07:05:56 +08:00
|
|
|
return TypeOrValue.V == Other.TypeOrValue.V;
|
2010-06-11 08:33:02 +08:00
|
|
|
|
2020-11-12 11:05:32 +08:00
|
|
|
case Template:
|
|
|
|
case TemplateExpansion:
|
|
|
|
return TemplateArg.Name == Other.TemplateArg.Name &&
|
|
|
|
TemplateArg.NumExpansions == Other.TemplateArg.NumExpansions;
|
|
|
|
|
2012-09-26 10:36:12 +08:00
|
|
|
case Declaration:
|
2020-10-12 11:14:00 +08:00
|
|
|
return getAsDecl() == Other.getAsDecl();
|
2012-09-26 10:36:12 +08:00
|
|
|
|
2010-06-11 08:33:02 +08:00
|
|
|
case Integral:
|
|
|
|
return getIntegralType() == Other.getIntegralType() &&
|
2012-06-07 23:09:51 +08:00
|
|
|
getAsIntegral() == Other.getAsIntegral();
|
2010-06-11 08:33:02 +08:00
|
|
|
|
|
|
|
case Pack:
|
|
|
|
if (Args.NumArgs != Other.Args.NumArgs) return false;
|
|
|
|
for (unsigned I = 0, E = Args.NumArgs; I != E; ++I)
|
|
|
|
if (!Args.Args[I].structurallyEquals(Other.Args.Args[I]))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-21 05:50:17 +08:00
|
|
|
llvm_unreachable("Invalid TemplateArgument Kind!");
|
2010-06-11 08:33:02 +08:00
|
|
|
}
|
|
|
|
|
2010-12-23 05:19:48 +08:00
|
|
|
TemplateArgument TemplateArgument::getPackExpansionPattern() const {
|
|
|
|
assert(isPackExpansion());
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-12-23 05:19:48 +08:00
|
|
|
switch (getKind()) {
|
2011-01-06 01:40:24 +08:00
|
|
|
case Type:
|
2019-10-07 21:58:05 +08:00
|
|
|
return getAsType()->castAs<PackExpansionType>()->getPattern();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-01-06 01:40:24 +08:00
|
|
|
case Expression:
|
|
|
|
return cast<PackExpansionExpr>(getAsExpr())->getPattern();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-01-06 02:58:31 +08:00
|
|
|
case TemplateExpansion:
|
2011-01-15 07:41:42 +08:00
|
|
|
return TemplateArgument(getAsTemplateOrTemplatePattern());
|
2012-09-26 10:36:12 +08:00
|
|
|
|
2011-01-06 01:40:24 +08:00
|
|
|
case Declaration:
|
|
|
|
case Integral:
|
|
|
|
case Pack:
|
|
|
|
case Null:
|
2011-01-06 02:58:31 +08:00
|
|
|
case Template:
|
2012-09-26 10:36:12 +08:00
|
|
|
case NullPtr:
|
2011-01-06 01:40:24 +08:00
|
|
|
return TemplateArgument();
|
2010-12-23 05:19:48 +08:00
|
|
|
}
|
2012-01-21 05:50:17 +08:00
|
|
|
|
|
|
|
llvm_unreachable("Invalid TemplateArgument Kind!");
|
2010-12-23 05:19:48 +08:00
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
void TemplateArgument::print(const PrintingPolicy &Policy,
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream &Out) const {
|
2010-12-21 00:52:59 +08:00
|
|
|
switch (getKind()) {
|
|
|
|
case Null:
|
2014-04-02 13:58:29 +08:00
|
|
|
Out << "(no value)";
|
2010-12-21 00:52:59 +08:00
|
|
|
break;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-12-21 00:52:59 +08:00
|
|
|
case Type: {
|
2011-06-18 06:11:49 +08:00
|
|
|
PrintingPolicy SubPolicy(Policy);
|
|
|
|
SubPolicy.SuppressStrongLifetime = true;
|
2013-02-22 23:46:01 +08:00
|
|
|
getAsType().print(Out, SubPolicy);
|
2010-12-21 00:52:59 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-12-21 00:52:59 +08:00
|
|
|
case Declaration: {
|
2018-03-01 13:43:23 +08:00
|
|
|
NamedDecl *ND = getAsDecl();
|
2020-09-21 14:16:08 +08:00
|
|
|
if (getParamTypeForDecl()->isRecordType()) {
|
|
|
|
if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(ND)) {
|
|
|
|
// FIXME: Include the type if it's not obvious from the context.
|
|
|
|
TPO->printAsInit(Out);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-04-15 14:35:35 +08:00
|
|
|
if (!getParamTypeForDecl()->isReferenceType())
|
|
|
|
Out << '&';
|
|
|
|
ND->printQualifiedName(Out);
|
2010-12-21 00:52:59 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-09-26 10:36:12 +08:00
|
|
|
|
2012-10-05 12:43:29 +08:00
|
|
|
case NullPtr:
|
2012-09-26 10:36:12 +08:00
|
|
|
Out << "nullptr";
|
2012-10-05 12:43:29 +08:00
|
|
|
break;
|
2012-09-26 10:36:12 +08:00
|
|
|
|
2011-01-06 02:58:31 +08:00
|
|
|
case Template:
|
2010-12-21 00:52:59 +08:00
|
|
|
getAsTemplate().print(Out, Policy);
|
|
|
|
break;
|
2011-01-06 02:58:31 +08:00
|
|
|
|
|
|
|
case TemplateExpansion:
|
|
|
|
getAsTemplateOrTemplatePattern().print(Out, Policy);
|
|
|
|
Out << "...";
|
|
|
|
break;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2017-11-22 07:26:08 +08:00
|
|
|
case Integral:
|
2014-12-13 12:31:07 +08:00
|
|
|
printIntegral(*this, Out, Policy);
|
2010-12-21 00:52:59 +08:00
|
|
|
break;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-01-06 01:40:24 +08:00
|
|
|
case Expression:
|
2014-05-12 13:36:57 +08:00
|
|
|
getAsExpr()->printPretty(Out, nullptr, Policy);
|
2010-12-21 00:52:59 +08:00
|
|
|
break;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-12-21 00:52:59 +08:00
|
|
|
case Pack:
|
|
|
|
Out << "<";
|
|
|
|
bool First = true;
|
2014-07-16 05:32:31 +08:00
|
|
|
for (const auto &P : pack_elements()) {
|
2010-12-21 00:52:59 +08:00
|
|
|
if (First)
|
|
|
|
First = false;
|
|
|
|
else
|
|
|
|
Out << ", ";
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2014-07-16 05:32:31 +08:00
|
|
|
P.print(Policy, Out);
|
2010-12-21 00:52:59 +08:00
|
|
|
}
|
|
|
|
Out << ">";
|
2018-07-31 03:24:48 +08:00
|
|
|
break;
|
2010-12-21 00:52:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-29 21:46:15 +08:00
|
|
|
void TemplateArgument::dump(raw_ostream &Out) const {
|
|
|
|
LangOptions LO; // FIXME! see also TemplateName::dump().
|
|
|
|
LO.CPlusPlus = true;
|
|
|
|
LO.Bool = true;
|
|
|
|
print(PrintingPolicy(LO), Out);
|
|
|
|
}
|
|
|
|
|
2016-01-30 03:38:18 +08:00
|
|
|
LLVM_DUMP_METHOD void TemplateArgument::dump() const { dump(llvm::errs()); }
|
2016-01-29 21:46:15 +08:00
|
|
|
|
2009-10-29 16:12:44 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TemplateArgumentLoc Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-10-30 02:45:58 +08:00
|
|
|
SourceRange TemplateArgumentLoc::getSourceRange() const {
|
2009-10-29 16:12:44 +08:00
|
|
|
switch (Argument.getKind()) {
|
|
|
|
case TemplateArgument::Expression:
|
2009-10-30 02:45:58 +08:00
|
|
|
return getSourceExpression()->getSourceRange();
|
2010-09-04 07:50:56 +08:00
|
|
|
|
2009-10-29 16:12:44 +08:00
|
|
|
case TemplateArgument::Declaration:
|
2009-10-30 02:45:58 +08:00
|
|
|
return getSourceDeclExpression()->getSourceRange();
|
2010-09-04 07:50:56 +08:00
|
|
|
|
2012-09-26 10:36:12 +08:00
|
|
|
case TemplateArgument::NullPtr:
|
|
|
|
return getSourceNullPtrExpression()->getSourceRange();
|
|
|
|
|
2009-10-30 02:45:58 +08:00
|
|
|
case TemplateArgument::Type:
|
2010-09-04 07:50:56 +08:00
|
|
|
if (TypeSourceInfo *TSI = getTypeSourceInfo())
|
|
|
|
return TSI->getTypeLoc().getSourceRange();
|
|
|
|
else
|
|
|
|
return SourceRange();
|
|
|
|
|
2011-01-06 02:58:31 +08:00
|
|
|
case TemplateArgument::Template:
|
2011-03-03 01:09:35 +08:00
|
|
|
if (getTemplateQualifierLoc())
|
2018-07-31 03:24:48 +08:00
|
|
|
return SourceRange(getTemplateQualifierLoc().getBeginLoc(),
|
2011-01-06 02:58:31 +08:00
|
|
|
getTemplateNameLoc());
|
|
|
|
return SourceRange(getTemplateNameLoc());
|
|
|
|
|
|
|
|
case TemplateArgument::TemplateExpansion:
|
2011-03-03 01:09:35 +08:00
|
|
|
if (getTemplateQualifierLoc())
|
2018-07-31 03:24:48 +08:00
|
|
|
return SourceRange(getTemplateQualifierLoc().getBeginLoc(),
|
2011-01-06 02:58:31 +08:00
|
|
|
getTemplateEllipsisLoc());
|
|
|
|
return SourceRange(getTemplateNameLoc(), getTemplateEllipsisLoc());
|
|
|
|
|
2009-10-29 16:12:44 +08:00
|
|
|
case TemplateArgument::Integral:
|
2012-09-26 10:36:12 +08:00
|
|
|
return getSourceIntegralExpression()->getSourceRange();
|
|
|
|
|
2009-10-29 16:12:44 +08:00
|
|
|
case TemplateArgument::Pack:
|
|
|
|
case TemplateArgument::Null:
|
2009-10-30 02:45:58 +08:00
|
|
|
return SourceRange();
|
2009-10-29 16:12:44 +08:00
|
|
|
}
|
|
|
|
|
2012-01-21 05:50:17 +08:00
|
|
|
llvm_unreachable("Invalid TemplateArgument Kind!");
|
2009-10-29 16:12:44 +08:00
|
|
|
}
|
2010-05-09 01:41:32 +08:00
|
|
|
|
2020-09-24 04:16:00 +08:00
|
|
|
template <typename T>
|
|
|
|
static const T &DiagTemplateArg(const T &DB, const TemplateArgument &Arg) {
|
2010-05-09 01:41:32 +08:00
|
|
|
switch (Arg.getKind()) {
|
|
|
|
case TemplateArgument::Null:
|
2010-08-05 12:58:04 +08:00
|
|
|
// This is bad, but not as bad as crashing because of argument
|
|
|
|
// count mismatches.
|
|
|
|
return DB << "(null template argument)";
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-05-09 01:41:32 +08:00
|
|
|
case TemplateArgument::Type:
|
|
|
|
return DB << Arg.getAsType();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-05-09 01:41:32 +08:00
|
|
|
case TemplateArgument::Declaration:
|
2012-09-26 10:36:12 +08:00
|
|
|
return DB << Arg.getAsDecl();
|
|
|
|
|
|
|
|
case TemplateArgument::NullPtr:
|
2012-04-07 06:40:38 +08:00
|
|
|
return DB << "nullptr";
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-05-09 01:41:32 +08:00
|
|
|
case TemplateArgument::Integral:
|
2012-06-07 23:09:51 +08:00
|
|
|
return DB << Arg.getAsIntegral().toString(10);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-05-09 01:41:32 +08:00
|
|
|
case TemplateArgument::Template:
|
2011-01-06 02:58:31 +08:00
|
|
|
return DB << Arg.getAsTemplate();
|
|
|
|
|
|
|
|
case TemplateArgument::TemplateExpansion:
|
|
|
|
return DB << Arg.getAsTemplateOrTemplatePattern() << "...";
|
|
|
|
|
2010-05-09 01:41:32 +08:00
|
|
|
case TemplateArgument::Expression: {
|
|
|
|
// This shouldn't actually ever happen, so it's okay that we're
|
|
|
|
// regurgitating an expression here.
|
|
|
|
// FIXME: We're guessing at LangOptions!
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<32> Str;
|
2010-05-09 01:41:32 +08:00
|
|
|
llvm::raw_svector_ostream OS(Str);
|
|
|
|
LangOptions LangOpts;
|
|
|
|
LangOpts.CPlusPlus = true;
|
|
|
|
PrintingPolicy Policy(LangOpts);
|
2014-05-12 13:36:57 +08:00
|
|
|
Arg.getAsExpr()->printPretty(OS, nullptr, Policy);
|
2010-05-09 01:41:32 +08:00
|
|
|
return DB << OS.str();
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-12-21 00:52:59 +08:00
|
|
|
case TemplateArgument::Pack: {
|
|
|
|
// FIXME: We're guessing at LangOptions!
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<32> Str;
|
2010-12-21 00:52:59 +08:00
|
|
|
llvm::raw_svector_ostream OS(Str);
|
|
|
|
LangOptions LangOpts;
|
|
|
|
LangOpts.CPlusPlus = true;
|
|
|
|
PrintingPolicy Policy(LangOpts);
|
|
|
|
Arg.print(Policy, OS);
|
|
|
|
return DB << OS.str();
|
|
|
|
}
|
2010-05-09 01:41:32 +08:00
|
|
|
}
|
2012-01-21 05:50:17 +08:00
|
|
|
|
|
|
|
llvm_unreachable("Invalid TemplateArgument Kind!");
|
2010-05-09 01:41:32 +08:00
|
|
|
}
|
2011-09-23 04:07:09 +08:00
|
|
|
|
2020-09-24 04:16:00 +08:00
|
|
|
const StreamingDiagnostic &clang::operator<<(const StreamingDiagnostic &DB,
|
|
|
|
const TemplateArgument &Arg) {
|
|
|
|
return DiagTemplateArg(DB, Arg);
|
|
|
|
}
|
|
|
|
|
2020-09-21 19:08:17 +08:00
|
|
|
clang::TemplateArgumentLocInfo::TemplateArgumentLocInfo(
|
|
|
|
ASTContext &Ctx, NestedNameSpecifierLoc QualifierLoc,
|
|
|
|
SourceLocation TemplateNameLoc, SourceLocation EllipsisLoc) {
|
|
|
|
TemplateTemplateArgLocInfo *Template = new (Ctx) TemplateTemplateArgLocInfo;
|
|
|
|
Template->Qualifier = QualifierLoc.getNestedNameSpecifier();
|
|
|
|
Template->QualifierLocData = QualifierLoc.getOpaqueData();
|
|
|
|
Template->TemplateNameLoc = TemplateNameLoc.getRawEncoding();
|
|
|
|
Template->EllipsisLoc = EllipsisLoc.getRawEncoding();
|
|
|
|
Pointer = Template;
|
|
|
|
}
|
|
|
|
|
2011-09-23 04:07:09 +08:00
|
|
|
const ASTTemplateArgumentListInfo *
|
2020-01-22 08:03:05 +08:00
|
|
|
ASTTemplateArgumentListInfo::Create(const ASTContext &C,
|
2011-09-23 04:07:09 +08:00
|
|
|
const TemplateArgumentListInfo &List) {
|
2015-12-24 10:59:37 +08:00
|
|
|
std::size_t size = totalSizeToAlloc<TemplateArgumentLoc>(List.size());
|
2016-10-20 22:27:22 +08:00
|
|
|
void *Mem = C.Allocate(size, alignof(ASTTemplateArgumentListInfo));
|
2015-12-24 10:59:37 +08:00
|
|
|
return new (Mem) ASTTemplateArgumentListInfo(List);
|
2011-09-23 04:07:09 +08:00
|
|
|
}
|
|
|
|
|
2015-12-24 10:59:37 +08:00
|
|
|
ASTTemplateArgumentListInfo::ASTTemplateArgumentListInfo(
|
|
|
|
const TemplateArgumentListInfo &Info) {
|
|
|
|
LAngleLoc = Info.getLAngleLoc();
|
|
|
|
RAngleLoc = Info.getRAngleLoc();
|
|
|
|
NumTemplateArgs = Info.size();
|
|
|
|
|
|
|
|
TemplateArgumentLoc *ArgBuffer = getTrailingObjects<TemplateArgumentLoc>();
|
2020-12-23 02:12:40 +08:00
|
|
|
for (unsigned i = 0; i != NumTemplateArgs; ++i)
|
|
|
|
new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
|
2015-12-24 10:59:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ASTTemplateKWAndArgsInfo::initializeFrom(
|
2015-12-30 02:15:14 +08:00
|
|
|
SourceLocation TemplateKWLoc, const TemplateArgumentListInfo &Info,
|
|
|
|
TemplateArgumentLoc *OutArgArray) {
|
2015-12-24 10:59:37 +08:00
|
|
|
this->TemplateKWLoc = TemplateKWLoc;
|
2011-09-23 04:07:09 +08:00
|
|
|
LAngleLoc = Info.getLAngleLoc();
|
|
|
|
RAngleLoc = Info.getRAngleLoc();
|
|
|
|
NumTemplateArgs = Info.size();
|
2020-12-23 02:12:40 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0; i != NumTemplateArgs; ++i)
|
|
|
|
new (&OutArgArray[i]) TemplateArgumentLoc(Info[i]);
|
2011-09-23 04:07:09 +08:00
|
|
|
}
|
|
|
|
|
2015-12-24 10:59:37 +08:00
|
|
|
void ASTTemplateKWAndArgsInfo::initializeFrom(SourceLocation TemplateKWLoc) {
|
|
|
|
assert(TemplateKWLoc.isValid());
|
|
|
|
LAngleLoc = SourceLocation();
|
|
|
|
RAngleLoc = SourceLocation();
|
|
|
|
this->TemplateKWLoc = TemplateKWLoc;
|
|
|
|
NumTemplateArgs = 0;
|
|
|
|
}
|
|
|
|
|
2020-12-23 02:12:40 +08:00
|
|
|
void ASTTemplateKWAndArgsInfo::initializeFrom(
|
|
|
|
SourceLocation TemplateKWLoc, const TemplateArgumentListInfo &Info,
|
|
|
|
TemplateArgumentLoc *OutArgArray, TemplateArgumentDependence &Deps) {
|
|
|
|
this->TemplateKWLoc = TemplateKWLoc;
|
|
|
|
LAngleLoc = Info.getLAngleLoc();
|
|
|
|
RAngleLoc = Info.getRAngleLoc();
|
|
|
|
NumTemplateArgs = Info.size();
|
|
|
|
|
|
|
|
for (unsigned i = 0; i != NumTemplateArgs; ++i) {
|
|
|
|
Deps |= Info[i].getArgument().getDependence();
|
|
|
|
|
|
|
|
new (&OutArgArray[i]) TemplateArgumentLoc(Info[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 02:15:14 +08:00
|
|
|
void ASTTemplateKWAndArgsInfo::copyInto(const TemplateArgumentLoc *ArgArray,
|
|
|
|
TemplateArgumentListInfo &Info) const {
|
2011-09-23 04:07:09 +08:00
|
|
|
Info.setLAngleLoc(LAngleLoc);
|
|
|
|
Info.setRAngleLoc(RAngleLoc);
|
|
|
|
for (unsigned I = 0; I != NumTemplateArgs; ++I)
|
2015-12-30 02:15:14 +08:00
|
|
|
Info.addArgument(ArgArray[I]);
|
2011-09-23 04:07:09 +08:00
|
|
|
}
|