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>
|
2011-02-19 08:21:00 +08:00
|
|
|
#include <cctype>
|
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.
|
|
|
|
static void printIntegral(const TemplateArgument &TemplArg,
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream &Out) {
|
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
|
|
|
|
|
|
|
if (T->isBooleanType()) {
|
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,
|
|
|
|
QualType Type)
|
|
|
|
: Kind(Integral) {
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2011-01-12 07:09:57 +08:00
|
|
|
TemplateArgument TemplateArgument::CreatePackCopy(ASTContext &Context,
|
|
|
|
const TemplateArgument *Args,
|
|
|
|
unsigned NumArgs) {
|
|
|
|
if (NumArgs == 0)
|
2012-09-26 10:36:12 +08:00
|
|
|
return getEmptyPack();
|
2011-01-12 07:09:57 +08:00
|
|
|
|
|
|
|
TemplateArgument *Storage = new (Context) TemplateArgument [NumArgs];
|
|
|
|
std::copy(Args, Args + NumArgs, Storage);
|
|
|
|
return TemplateArgument(Storage, NumArgs);
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
return getAsType()->isDependentType();
|
|
|
|
|
|
|
|
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:
|
|
|
|
return (getAsExpr()->isTypeDependent() || getAsExpr()->isValueDependent());
|
|
|
|
|
|
|
|
case Pack:
|
|
|
|
for (pack_iterator P = pack_begin(), PEnd = pack_end(); P != PEnd; ++P) {
|
|
|
|
if (P->isDependent())
|
|
|
|
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:
|
|
|
|
for (pack_iterator P = pack_begin(), PEnd = pack_end(); P != PEnd; ++P) {
|
|
|
|
if (P->isInstantiationDependent())
|
|
|
|
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:
|
|
|
|
for (pack_iterator P = pack_begin(), PEnd = pack_end(); P != PEnd; ++P)
|
|
|
|
if (P->containsUnexpandedParameterPack())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-15 07:41:42 +08:00
|
|
|
llvm::Optional<unsigned> TemplateArgument::getNumTemplateExpansions() const {
|
|
|
|
assert(Kind == TemplateExpansion);
|
|
|
|
if (TemplateArg.NumExpansions)
|
|
|
|
return TemplateArg.NumExpansions - 1;
|
|
|
|
|
|
|
|
return llvm::Optional<unsigned>();
|
|
|
|
}
|
|
|
|
|
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 {
|
2009-10-29 15:48:15 +08:00
|
|
|
ID.AddInteger(Kind);
|
|
|
|
switch (Kind) {
|
|
|
|
case Null:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Type:
|
|
|
|
getAsType().Profile(ID);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Declaration:
|
|
|
|
ID.AddPointer(getAsDecl()? getAsDecl()->getCanonicalDecl() : 0);
|
|
|
|
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:
|
2010-06-11 08:33:02 +08:00
|
|
|
return TypeOrValue == Other.TypeOrValue;
|
|
|
|
|
2012-09-26 10:36:12 +08:00
|
|
|
case Declaration:
|
|
|
|
return getAsDecl() == Other.getAsDecl() &&
|
|
|
|
isDeclForReferenceParam() && Other.isDeclForReferenceParam();
|
|
|
|
|
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:
|
|
|
|
Out << "<no value>";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Type: {
|
2011-06-18 06:11:49 +08:00
|
|
|
PrintingPolicy SubPolicy(Policy);
|
|
|
|
SubPolicy.SuppressStrongLifetime = true;
|
2010-12-21 00:52:59 +08:00
|
|
|
std::string TypeStr;
|
2011-06-18 06:11:49 +08:00
|
|
|
getAsType().getAsStringInternal(TypeStr, SubPolicy);
|
2010-12-21 00:52:59 +08:00
|
|
|
Out << TypeStr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Declaration: {
|
2012-09-26 10:36:12 +08:00
|
|
|
NamedDecl *ND = cast<NamedDecl>(getAsDecl());
|
|
|
|
if (ND->getDeclName()) {
|
|
|
|
// FIXME: distinguish between pointer and reference args?
|
|
|
|
Out << *ND;
|
2012-04-07 06:40:38 +08:00
|
|
|
} else {
|
2012-09-26 10:36:12 +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: {
|
2011-02-19 08:21:00 +08:00
|
|
|
printIntegral(*this, Out);
|
2010-12-21 00:52:59 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-01-06 01:40:24 +08:00
|
|
|
case Expression:
|
2010-12-21 00:52:59 +08:00
|
|
|
getAsExpr()->printPretty(Out, 0, Policy);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Pack:
|
|
|
|
Out << "<";
|
|
|
|
bool First = true;
|
|
|
|
for (TemplateArgument::pack_iterator P = pack_begin(), PEnd = pack_end();
|
|
|
|
P != PEnd; ++P) {
|
|
|
|
if (First)
|
|
|
|
First = false;
|
|
|
|
else
|
|
|
|
Out << ", ";
|
|
|
|
|
|
|
|
P->print(Policy, Out);
|
|
|
|
}
|
|
|
|
Out << ">";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2010-12-21 06:05:00 +08:00
|
|
|
TemplateArgumentLoc
|
|
|
|
TemplateArgumentLoc::getPackExpansionPattern(SourceLocation &Ellipsis,
|
2011-01-15 01:04:44 +08:00
|
|
|
llvm::Optional<unsigned> &NumExpansions,
|
2010-12-21 06:05:00 +08:00
|
|
|
ASTContext &Context) const {
|
|
|
|
assert(Argument.isPackExpansion());
|
|
|
|
|
|
|
|
switch (Argument.getKind()) {
|
|
|
|
case TemplateArgument::Type: {
|
2010-12-22 06:10:26 +08:00
|
|
|
// FIXME: We shouldn't ever have to worry about missing
|
|
|
|
// type-source info!
|
|
|
|
TypeSourceInfo *ExpansionTSInfo = getTypeSourceInfo();
|
|
|
|
if (!ExpansionTSInfo)
|
|
|
|
ExpansionTSInfo = Context.getTrivialTypeSourceInfo(
|
|
|
|
getArgument().getAsType(),
|
|
|
|
Ellipsis);
|
2010-12-21 06:05:00 +08:00
|
|
|
PackExpansionTypeLoc Expansion
|
2010-12-22 06:10:26 +08:00
|
|
|
= cast<PackExpansionTypeLoc>(ExpansionTSInfo->getTypeLoc());
|
2010-12-21 06:05:00 +08:00
|
|
|
Ellipsis = Expansion.getEllipsisLoc();
|
|
|
|
|
|
|
|
TypeLoc Pattern = Expansion.getPatternLoc();
|
2011-01-15 01:04:44 +08:00
|
|
|
NumExpansions = Expansion.getTypePtr()->getNumExpansions();
|
2010-12-21 06:05:00 +08:00
|
|
|
|
|
|
|
// FIXME: This is horrible. We know where the source location data is for
|
|
|
|
// the pattern, and we have the pattern's type, but we are forced to copy
|
|
|
|
// them into an ASTContext because TypeSourceInfo bundles them together
|
|
|
|
// and TemplateArgumentLoc traffics in TypeSourceInfo pointers.
|
|
|
|
TypeSourceInfo *PatternTSInfo
|
|
|
|
= Context.CreateTypeSourceInfo(Pattern.getType(),
|
|
|
|
Pattern.getFullDataSize());
|
|
|
|
memcpy(PatternTSInfo->getTypeLoc().getOpaqueData(),
|
|
|
|
Pattern.getOpaqueData(), Pattern.getFullDataSize());
|
|
|
|
return TemplateArgumentLoc(TemplateArgument(Pattern.getType()),
|
|
|
|
PatternTSInfo);
|
|
|
|
}
|
|
|
|
|
2011-01-04 01:17:50 +08:00
|
|
|
case TemplateArgument::Expression: {
|
2011-01-06 08:33:28 +08:00
|
|
|
PackExpansionExpr *Expansion
|
|
|
|
= cast<PackExpansionExpr>(Argument.getAsExpr());
|
|
|
|
Expr *Pattern = Expansion->getPattern();
|
|
|
|
Ellipsis = Expansion->getEllipsisLoc();
|
2011-01-15 05:20:45 +08:00
|
|
|
NumExpansions = Expansion->getNumExpansions();
|
2011-01-04 01:17:50 +08:00
|
|
|
return TemplateArgumentLoc(Pattern, Pattern);
|
|
|
|
}
|
2011-01-06 02:58:31 +08:00
|
|
|
|
|
|
|
case TemplateArgument::TemplateExpansion:
|
2011-01-06 08:33:28 +08:00
|
|
|
Ellipsis = getTemplateEllipsisLoc();
|
2011-01-15 07:41:42 +08:00
|
|
|
NumExpansions = Argument.getNumTemplateExpansions();
|
2011-01-06 01:40:24 +08:00
|
|
|
return TemplateArgumentLoc(Argument.getPackExpansionPattern(),
|
2011-03-03 01:09:35 +08:00
|
|
|
getTemplateQualifierLoc(),
|
2011-01-06 01:40:24 +08:00
|
|
|
getTemplateNameLoc());
|
2010-12-21 06:05:00 +08:00
|
|
|
|
|
|
|
case TemplateArgument::Declaration:
|
2012-09-26 10:36:12 +08:00
|
|
|
case TemplateArgument::NullPtr:
|
2011-01-06 02:58:31 +08:00
|
|
|
case TemplateArgument::Template:
|
2010-12-21 06:05:00 +08:00
|
|
|
case TemplateArgument::Integral:
|
|
|
|
case TemplateArgument::Pack:
|
|
|
|
case TemplateArgument::Null:
|
|
|
|
return TemplateArgumentLoc();
|
|
|
|
}
|
2012-01-21 05:50:17 +08:00
|
|
|
|
|
|
|
llvm_unreachable("Invalid TemplateArgument Kind!");
|
2010-12-21 06:05:00 +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);
|
|
|
|
Arg.getAsExpr()->printPretty(OS, 0, Policy);
|
|
|
|
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) {
|
2012-08-15 09:22:58 +08:00
|
|
|
std::size_t size = ASTTemplateArgumentListInfo::sizeFor(List.size());
|
2011-09-23 04:07:09 +08:00
|
|
|
void *Mem = C.Allocate(size, llvm::alignOf<ASTTemplateArgumentListInfo>());
|
|
|
|
ASTTemplateArgumentListInfo *TAI = new (Mem) ASTTemplateArgumentListInfo();
|
|
|
|
TAI->initializeFrom(List);
|
|
|
|
return TAI;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTTemplateArgumentListInfo::initializeFrom(
|
|
|
|
const TemplateArgumentListInfo &Info) {
|
|
|
|
LAngleLoc = Info.getLAngleLoc();
|
|
|
|
RAngleLoc = Info.getRAngleLoc();
|
|
|
|
NumTemplateArgs = Info.size();
|
|
|
|
|
|
|
|
TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
|
|
|
|
for (unsigned i = 0; i != NumTemplateArgs; ++i)
|
|
|
|
new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTTemplateArgumentListInfo::initializeFrom(
|
|
|
|
const TemplateArgumentListInfo &Info,
|
|
|
|
bool &Dependent,
|
|
|
|
bool &InstantiationDependent,
|
|
|
|
bool &ContainsUnexpandedParameterPack) {
|
|
|
|
LAngleLoc = Info.getLAngleLoc();
|
|
|
|
RAngleLoc = Info.getRAngleLoc();
|
|
|
|
NumTemplateArgs = Info.size();
|
|
|
|
|
|
|
|
TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
|
|
|
|
for (unsigned i = 0; i != NumTemplateArgs; ++i) {
|
|
|
|
Dependent = Dependent || Info[i].getArgument().isDependent();
|
|
|
|
InstantiationDependent = InstantiationDependent ||
|
|
|
|
Info[i].getArgument().isInstantiationDependent();
|
|
|
|
ContainsUnexpandedParameterPack
|
|
|
|
= ContainsUnexpandedParameterPack ||
|
|
|
|
Info[i].getArgument().containsUnexpandedParameterPack();
|
|
|
|
|
|
|
|
new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTTemplateArgumentListInfo::copyInto(
|
|
|
|
TemplateArgumentListInfo &Info) const {
|
|
|
|
Info.setLAngleLoc(LAngleLoc);
|
|
|
|
Info.setRAngleLoc(RAngleLoc);
|
|
|
|
for (unsigned I = 0; I != NumTemplateArgs; ++I)
|
|
|
|
Info.addArgument(getTemplateArgs()[I]);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t ASTTemplateArgumentListInfo::sizeFor(unsigned NumTemplateArgs) {
|
|
|
|
return sizeof(ASTTemplateArgumentListInfo) +
|
|
|
|
sizeof(TemplateArgumentLoc) * NumTemplateArgs;
|
|
|
|
}
|
|
|
|
|
2012-01-27 17:46:47 +08:00
|
|
|
void
|
|
|
|
ASTTemplateKWAndArgsInfo::initializeFrom(SourceLocation TemplateKWLoc,
|
|
|
|
const TemplateArgumentListInfo &Info) {
|
|
|
|
Base::initializeFrom(Info);
|
|
|
|
setTemplateKeywordLoc(TemplateKWLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ASTTemplateKWAndArgsInfo
|
|
|
|
::initializeFrom(SourceLocation TemplateKWLoc,
|
|
|
|
const TemplateArgumentListInfo &Info,
|
|
|
|
bool &Dependent,
|
|
|
|
bool &InstantiationDependent,
|
|
|
|
bool &ContainsUnexpandedParameterPack) {
|
|
|
|
Base::initializeFrom(Info, Dependent, InstantiationDependent,
|
|
|
|
ContainsUnexpandedParameterPack);
|
|
|
|
setTemplateKeywordLoc(TemplateKWLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ASTTemplateKWAndArgsInfo::initializeFrom(SourceLocation TemplateKWLoc) {
|
|
|
|
// No explicit template arguments, but template keyword loc is valid.
|
|
|
|
assert(TemplateKWLoc.isValid());
|
|
|
|
LAngleLoc = SourceLocation();
|
|
|
|
RAngleLoc = SourceLocation();
|
|
|
|
NumTemplateArgs = 0;
|
|
|
|
setTemplateKeywordLoc(TemplateKWLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t
|
|
|
|
ASTTemplateKWAndArgsInfo::sizeFor(unsigned NumTemplateArgs) {
|
|
|
|
// Add space for the template keyword location.
|
2012-08-15 09:22:58 +08:00
|
|
|
// FIXME: There's room for this in the padding before the template args in
|
|
|
|
// 64-bit builds.
|
2012-01-27 17:46:47 +08:00
|
|
|
return Base::sizeFor(NumTemplateArgs) + sizeof(SourceLocation);
|
2011-09-23 04:07:09 +08:00
|
|
|
}
|