2009-10-29 15:48:15 +08:00
|
|
|
//===--- TemplateBase.cpp - Common template AST class implementation ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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"
|
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"
|
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"
|
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"
|
2010-12-21 00:52:59 +08:00
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
2012-02-04 21:45:25 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-12-02 01:12:56 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-01-12 07:09:57 +08:00
|
|
|
#include <algorithm>
|
2009-10-29 15:48:15 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2011-02-19 08:21:00 +08:00
|
|
|
/// \brief Print a template integral argument value.
|
|
|
|
///
|
|
|
|
/// \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) {
|
2011-02-19 08:21:00 +08:00
|
|
|
const ::clang::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
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-06-07 23:09:51 +08:00
|
|
|
TemplateArgument::TemplateArgument(ASTContext &Ctx, const llvm::APSInt &Value,
|
2013-08-22 07:05:56 +08:00
|
|
|
QualType Type) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-12-15 09:34:56 +08:00
|
|
|
bool TemplateArgument::isDependent() const {
|
|
|
|
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:
|
2014-10-18 04:56:14 +08:00
|
|
|
return getAsType()->isDependentType() ||
|
|
|
|
isa<PackExpansionType>(getAsType());
|
2010-12-15 09:34:56 +08:00
|
|
|
|
|
|
|
case Template:
|
|
|
|
return getAsTemplate().isDependent();
|
2011-01-06 02:58:31 +08:00
|
|
|
|
|
|
|
case TemplateExpansion:
|
|
|
|
return true;
|
|
|
|
|
2010-12-15 09:34:56 +08:00
|
|
|
case Declaration:
|
2012-09-26 10:36:12 +08:00
|
|
|
if (DeclContext *DC = dyn_cast<DeclContext>(getAsDecl()))
|
|
|
|
return DC->isDependentContext();
|
|
|
|
return getAsDecl()->getDeclContext()->isDependentContext();
|
|
|
|
|
|
|
|
case NullPtr:
|
2012-04-07 06:40:38 +08:00
|
|
|
return false;
|
2010-12-15 09:34:56 +08:00
|
|
|
|
|
|
|
case Integral:
|
|
|
|
// Never dependent
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case Expression:
|
2014-10-18 04:56:14 +08:00
|
|
|
return (getAsExpr()->isTypeDependent() || getAsExpr()->isValueDependent() ||
|
|
|
|
isa<PackExpansionExpr>(getAsExpr()));
|
2010-12-15 09:34:56 +08:00
|
|
|
|
|
|
|
case Pack:
|
2014-07-16 05:32:31 +08:00
|
|
|
for (const auto &P : pack_elements())
|
|
|
|
if (P.isDependent())
|
2010-12-15 09:34:56 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-21 05:50:17 +08:00
|
|
|
llvm_unreachable("Invalid TemplateArgument Kind!");
|
2010-12-15 09:34:56 +08:00
|
|
|
}
|
|
|
|
|
2011-07-01 09:22:09 +08:00
|
|
|
bool TemplateArgument::isInstantiationDependent() const {
|
|
|
|
switch (getKind()) {
|
|
|
|
case Null:
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Should not have a NULL template argument");
|
2011-07-01 09:22:09 +08:00
|
|
|
|
|
|
|
case Type:
|
|
|
|
return getAsType()->isInstantiationDependentType();
|
|
|
|
|
|
|
|
case Template:
|
|
|
|
return getAsTemplate().isInstantiationDependent();
|
|
|
|
|
|
|
|
case TemplateExpansion:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case Declaration:
|
2012-09-26 10:36:12 +08:00
|
|
|
if (DeclContext *DC = dyn_cast<DeclContext>(getAsDecl()))
|
|
|
|
return DC->isDependentContext();
|
|
|
|
return getAsDecl()->getDeclContext()->isDependentContext();
|
|
|
|
|
|
|
|
case NullPtr:
|
2012-04-07 06:40:38 +08:00
|
|
|
return false;
|
|
|
|
|
2011-07-01 09:22:09 +08:00
|
|
|
case Integral:
|
|
|
|
// Never dependent
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case Expression:
|
|
|
|
return getAsExpr()->isInstantiationDependent();
|
|
|
|
|
|
|
|
case Pack:
|
2014-07-16 05:32:31 +08:00
|
|
|
for (const auto &P : pack_elements())
|
|
|
|
if (P.isInstantiationDependent())
|
2011-07-01 09:22:09 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-21 05:50:17 +08:00
|
|
|
|
|
|
|
llvm_unreachable("Invalid TemplateArgument Kind!");
|
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:
|
|
|
|
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;
|
|
|
|
|
2011-01-06 02:58:31 +08:00
|
|
|
case TemplateExpansion:
|
|
|
|
return true;
|
|
|
|
|
2010-12-21 06:05:00 +08:00
|
|
|
case Type:
|
2011-01-04 01:17:50 +08:00
|
|
|
return isa<PackExpansionType>(getAsType());
|
2011-01-06 02:58:31 +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 {
|
|
|
|
switch (getKind()) {
|
|
|
|
case Null:
|
|
|
|
case Declaration:
|
|
|
|
case Integral:
|
2011-01-06 02:58:31 +08:00
|
|
|
case TemplateExpansion:
|
2012-09-26 10:36:12 +08:00
|
|
|
case NullPtr:
|
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
|
|
|
break;
|
|
|
|
|
|
|
|
case Type:
|
|
|
|
if (getAsType()->containsUnexpandedParameterPack())
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Template:
|
2011-01-06 02:58:31 +08:00
|
|
|
if (getAsTemplate().containsUnexpandedParameterPack())
|
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
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Expression:
|
|
|
|
if (getAsExpr()->containsUnexpandedParameterPack())
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Pack:
|
2014-07-16 05:32:31 +08:00
|
|
|
for (const auto &P : pack_elements())
|
|
|
|
if (P.containsUnexpandedParameterPack())
|
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
|
|
|
return true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2013-02-21 09:47:18 +08:00
|
|
|
return None;
|
2011-01-15 07:41:42 +08:00
|
|
|
}
|
|
|
|
|
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:
|
2014-05-12 13:36:57 +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
|
|
|
}
|
2009-11-11 09:00:40 +08:00
|
|
|
|
2009-10-29 15:48:15 +08:00
|
|
|
case Integral:
|
2012-06-07 23:09:51 +08:00
|
|
|
getAsIntegral().Profile(ID);
|
2009-10-29 15:48:15 +08:00
|
|
|
getIntegralType().Profile(ID);
|
|
|
|
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:
|
2011-01-06 02:58:31 +08:00
|
|
|
case Expression:
|
|
|
|
case Template:
|
|
|
|
case TemplateExpansion:
|
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
|
|
|
|
2012-09-26 10:36:12 +08:00
|
|
|
case Declaration:
|
2014-10-16 12:21:25 +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());
|
|
|
|
|
|
|
|
switch (getKind()) {
|
2011-01-06 01:40:24 +08:00
|
|
|
case Type:
|
|
|
|
return getAsType()->getAs<PackExpansionType>()->getPattern();
|
|
|
|
|
|
|
|
case Expression:
|
|
|
|
return cast<PackExpansionExpr>(getAsExpr())->getPattern();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-12-21 00:52:59 +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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Declaration: {
|
2012-09-26 10:36:12 +08:00
|
|
|
NamedDecl *ND = cast<NamedDecl>(getAsDecl());
|
2013-05-10 06:43:45 +08:00
|
|
|
Out << '&';
|
2012-09-26 10:36:12 +08:00
|
|
|
if (ND->getDeclName()) {
|
|
|
|
// FIXME: distinguish between pointer and reference args?
|
2013-05-10 06:43:45 +08:00
|
|
|
ND->printQualifiedName(Out);
|
2012-04-07 06:40:38 +08:00
|
|
|
} else {
|
2014-04-02 13:58:29 +08:00
|
|
|
Out << "(anonymous)";
|
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;
|
|
|
|
|
2010-12-21 00:52:59 +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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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 << ", ";
|
|
|
|
|
2014-07-16 05:32:31 +08:00
|
|
|
P.print(Policy, Out);
|
2010-12-21 00:52:59 +08:00
|
|
|
}
|
|
|
|
Out << ">";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-01-06 08:33:28 +08:00
|
|
|
TemplateArgumentLocInfo::TemplateArgumentLocInfo() {
|
2011-04-28 16:19:45 +08:00
|
|
|
memset((void*)this, 0, sizeof(TemplateArgumentLocInfo));
|
2011-01-06 08:33:28 +08:00
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
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())
|
|
|
|
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
|
|
|
|
|
|
|
const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB,
|
|
|
|
const TemplateArgument &Arg) {
|
|
|
|
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)";
|
2010-05-09 01:41:32 +08:00
|
|
|
|
|
|
|
case TemplateArgument::Type:
|
|
|
|
return DB << Arg.getAsType();
|
|
|
|
|
|
|
|
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";
|
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);
|
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();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
const ASTTemplateArgumentListInfo *
|
|
|
|
ASTTemplateArgumentListInfo::Create(ASTContext &C,
|
|
|
|
const TemplateArgumentListInfo &List) {
|
2015-12-24 10:59:37 +08:00
|
|
|
std::size_t size = totalSizeToAlloc<TemplateArgumentLoc>(List.size());
|
2011-09-23 04:07:09 +08:00
|
|
|
void *Mem = C.Allocate(size, llvm::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>();
|
|
|
|
for (unsigned i = 0; i != NumTemplateArgs; ++i)
|
|
|
|
new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
for (unsigned i = 0; i != NumTemplateArgs; ++i)
|
2015-12-30 02:15:14 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTTemplateKWAndArgsInfo::initializeFrom(
|
|
|
|
SourceLocation TemplateKWLoc, const TemplateArgumentListInfo &Info,
|
2015-12-30 02:15:14 +08:00
|
|
|
TemplateArgumentLoc *OutArgArray, bool &Dependent,
|
|
|
|
bool &InstantiationDependent, bool &ContainsUnexpandedParameterPack) {
|
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();
|
|
|
|
|
|
|
|
for (unsigned i = 0; i != NumTemplateArgs; ++i) {
|
|
|
|
Dependent = Dependent || Info[i].getArgument().isDependent();
|
2015-12-24 10:59:37 +08:00
|
|
|
InstantiationDependent = InstantiationDependent ||
|
2011-09-23 04:07:09 +08:00
|
|
|
Info[i].getArgument().isInstantiationDependent();
|
2015-12-24 10:59:37 +08:00
|
|
|
ContainsUnexpandedParameterPack =
|
|
|
|
ContainsUnexpandedParameterPack ||
|
2011-09-23 04:07:09 +08:00
|
|
|
Info[i].getArgument().containsUnexpandedParameterPack();
|
|
|
|
|
2015-12-30 02:15:14 +08:00
|
|
|
new (&OutArgArray[i]) TemplateArgumentLoc(Info[i]);
|
2011-09-23 04:07:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|