2013-05-14 17:13:00 +08:00
|
|
|
//===--- Marshallers.h - Generic matcher function marshallers -*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief Functions templates and classes to wrap matcher construct functions.
|
|
|
|
///
|
|
|
|
/// A collection of template function and classes that provide a generic
|
|
|
|
/// marshalling layer on top of matcher construct functions.
|
|
|
|
/// These are used by the registry to export all marshaller constructors with
|
|
|
|
/// the same generic interface.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_AST_MATCHERS_DYNAMIC_MARSHALLERS_H
|
|
|
|
#define LLVM_CLANG_AST_MATCHERS_DYNAMIC_MARSHALLERS_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
|
|
#include "clang/ASTMatchers/Dynamic/Diagnostics.h"
|
|
|
|
#include "clang/ASTMatchers/Dynamic/VariantValue.h"
|
|
|
|
#include "clang/Basic/LLVM.h"
|
|
|
|
#include "llvm/Support/type_traits.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace ast_matchers {
|
|
|
|
namespace dynamic {
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
/// \brief Helper template class to just from argument type to the right is/get
|
|
|
|
/// functions in VariantValue.
|
|
|
|
/// Used to verify and extract the matcher arguments below.
|
|
|
|
template <class T> struct ArgTypeTraits;
|
|
|
|
template <class T> struct ArgTypeTraits<const T &> : public ArgTypeTraits<T> {
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct ArgTypeTraits<std::string> {
|
2013-06-20 22:28:32 +08:00
|
|
|
static StringRef asString() { return "String"; }
|
2013-05-14 17:13:00 +08:00
|
|
|
static bool is(const VariantValue &Value) { return Value.isString(); }
|
|
|
|
static const std::string &get(const VariantValue &Value) {
|
|
|
|
return Value.getString();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-21 23:51:31 +08:00
|
|
|
template <>
|
|
|
|
struct ArgTypeTraits<StringRef> : public ArgTypeTraits<std::string> {
|
|
|
|
};
|
|
|
|
|
2013-05-14 17:13:00 +08:00
|
|
|
template <class T> struct ArgTypeTraits<ast_matchers::internal::Matcher<T> > {
|
2013-06-20 22:28:32 +08:00
|
|
|
static std::string asString() {
|
|
|
|
return (Twine("Matcher<") +
|
|
|
|
ast_type_traits::ASTNodeKind::getFromNodeKind<T>().asStringRef() +
|
|
|
|
">").str();
|
|
|
|
}
|
|
|
|
static bool is(const VariantValue &Value) {
|
|
|
|
return Value.hasTypedMatcher<T>();
|
|
|
|
}
|
2013-05-14 17:13:00 +08:00
|
|
|
static ast_matchers::internal::Matcher<T> get(const VariantValue &Value) {
|
|
|
|
return Value.getTypedMatcher<T>();
|
|
|
|
}
|
2013-06-04 23:46:22 +08:00
|
|
|
};
|
2013-05-14 17:13:00 +08:00
|
|
|
|
2013-06-04 23:46:22 +08:00
|
|
|
template <> struct ArgTypeTraits<unsigned> {
|
2013-06-20 22:28:32 +08:00
|
|
|
static std::string asString() { return "Unsigned"; }
|
2013-06-04 23:46:22 +08:00
|
|
|
static bool is(const VariantValue &Value) { return Value.isUnsigned(); }
|
|
|
|
static unsigned get(const VariantValue &Value) {
|
|
|
|
return Value.getUnsigned();
|
|
|
|
}
|
2013-05-14 17:13:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Generic MatcherCreate interface.
|
|
|
|
///
|
|
|
|
/// Provides a \c run() method that constructs the matcher from the provided
|
|
|
|
/// arguments.
|
|
|
|
class MatcherCreateCallback {
|
|
|
|
public:
|
|
|
|
virtual ~MatcherCreateCallback() {}
|
2013-06-21 23:51:31 +08:00
|
|
|
virtual MatcherList run(const SourceRange &NameRange,
|
|
|
|
ArrayRef<ParserValue> Args,
|
|
|
|
Diagnostics *Error) const = 0;
|
2013-05-14 17:13:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief Simple callback implementation. Marshaller and function are provided.
|
2013-06-12 02:51:07 +08:00
|
|
|
///
|
|
|
|
/// This class wraps a function of arbitrary signature and a marshaller
|
|
|
|
/// function into a MatcherCreateCallback.
|
|
|
|
/// The marshaller is in charge of taking the VariantValue arguments, checking
|
|
|
|
/// their types, unpacking them and calling the underlying function.
|
|
|
|
template <typename FuncType>
|
2013-05-14 17:13:00 +08:00
|
|
|
class FixedArgCountMatcherCreateCallback : public MatcherCreateCallback {
|
|
|
|
public:
|
2013-06-12 02:51:07 +08:00
|
|
|
/// FIXME: Use void(*)() as FuncType on this interface to remove the template
|
|
|
|
/// argument of this class. The marshaller can cast the function pointer back
|
|
|
|
/// to the original type.
|
2013-06-21 23:51:31 +08:00
|
|
|
typedef MatcherList (*MarshallerType)(FuncType, StringRef,
|
|
|
|
const SourceRange &,
|
|
|
|
ArrayRef<ParserValue>,
|
|
|
|
Diagnostics *);
|
2013-06-12 02:51:07 +08:00
|
|
|
|
2013-05-18 01:50:16 +08:00
|
|
|
/// \param Marshaller Function to unpack the arguments and call \c Func
|
|
|
|
/// \param Func Matcher construct function. This is the function that
|
|
|
|
/// compile-time matcher expressions would use to create the matcher.
|
2013-05-14 17:13:00 +08:00
|
|
|
FixedArgCountMatcherCreateCallback(MarshallerType Marshaller, FuncType Func,
|
|
|
|
StringRef MatcherName)
|
|
|
|
: Marshaller(Marshaller), Func(Func), MatcherName(MatcherName.str()) {}
|
|
|
|
|
2013-06-21 23:51:31 +08:00
|
|
|
MatcherList run(const SourceRange &NameRange, ArrayRef<ParserValue> Args,
|
|
|
|
Diagnostics *Error) const {
|
2013-05-14 17:13:00 +08:00
|
|
|
return Marshaller(Func, MatcherName, NameRange, Args, Error);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const MarshallerType Marshaller;
|
|
|
|
const FuncType Func;
|
|
|
|
const std::string MatcherName;
|
|
|
|
};
|
|
|
|
|
2013-06-12 02:51:07 +08:00
|
|
|
/// \brief Simple callback implementation. Free function is wrapped.
|
|
|
|
///
|
|
|
|
/// This class simply wraps a free function with the right signature to export
|
|
|
|
/// it as a MatcherCreateCallback.
|
|
|
|
/// This allows us to have one implementation of the interface for as many free
|
|
|
|
/// functions as we want, reducing the number of symbols and size of the
|
|
|
|
/// object file.
|
|
|
|
class FreeFuncMatcherCreateCallback : public MatcherCreateCallback {
|
|
|
|
public:
|
2013-06-21 23:51:31 +08:00
|
|
|
typedef MatcherList (*RunFunc)(StringRef MatcherName,
|
|
|
|
const SourceRange &NameRange,
|
|
|
|
ArrayRef<ParserValue> Args,
|
|
|
|
Diagnostics *Error);
|
2013-06-12 02:51:07 +08:00
|
|
|
|
|
|
|
FreeFuncMatcherCreateCallback(RunFunc Func, StringRef MatcherName)
|
|
|
|
: Func(Func), MatcherName(MatcherName.str()) {}
|
|
|
|
|
2013-06-21 23:51:31 +08:00
|
|
|
MatcherList run(const SourceRange &NameRange, ArrayRef<ParserValue> Args,
|
|
|
|
Diagnostics *Error) const {
|
2013-06-12 02:51:07 +08:00
|
|
|
return Func(MatcherName, NameRange, Args, Error);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const RunFunc Func;
|
|
|
|
const std::string MatcherName;
|
|
|
|
};
|
2013-05-14 17:13:00 +08:00
|
|
|
|
|
|
|
/// \brief Helper macros to check the arguments on all marshaller functions.
|
|
|
|
#define CHECK_ARG_COUNT(count) \
|
|
|
|
if (Args.size() != count) { \
|
|
|
|
Error->pushErrorFrame(NameRange, Error->ET_RegistryWrongArgCount) \
|
|
|
|
<< count << Args.size(); \
|
2013-06-21 23:51:31 +08:00
|
|
|
return MatcherList(); \
|
2013-05-14 17:13:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define CHECK_ARG_TYPE(index, type) \
|
|
|
|
if (!ArgTypeTraits<type>::is(Args[index].Value)) { \
|
|
|
|
Error->pushErrorFrame(Args[index].Range, Error->ET_RegistryWrongArgType) \
|
2013-06-20 22:28:32 +08:00
|
|
|
<< (index + 1) << ArgTypeTraits<type>::asString() \
|
|
|
|
<< Args[index].Value.getTypeAsString(); \
|
2013-06-21 23:51:31 +08:00
|
|
|
return MatcherList(); \
|
2013-05-14 17:13:00 +08:00
|
|
|
}
|
|
|
|
|
2013-06-21 23:51:31 +08:00
|
|
|
/// \brief Helper methods to extract and merge all possible typed matchers
|
|
|
|
/// out of the polymorphic object.
|
|
|
|
template <class PolyMatcher>
|
|
|
|
static void mergePolyMatchers(const PolyMatcher &Poly, MatcherList *Out,
|
|
|
|
ast_matchers::internal::EmptyTypeList) {}
|
|
|
|
|
|
|
|
template <class PolyMatcher, class TypeList>
|
|
|
|
static void mergePolyMatchers(const PolyMatcher &Poly, MatcherList *Out,
|
|
|
|
TypeList) {
|
|
|
|
Out->add(ast_matchers::internal::Matcher<typename TypeList::head>(Poly));
|
|
|
|
mergePolyMatchers(Poly, Out, typename TypeList::tail());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Convert the return values of the functions into a MatcherList.
|
|
|
|
///
|
|
|
|
/// There are 2 cases right now: The return value is a Matcher<T> or is a
|
|
|
|
/// polymorphic matcher. For the former, we just construct the MatcherList. For
|
|
|
|
/// the latter, we instantiate all the possible Matcher<T> of the poly matcher.
|
|
|
|
template <typename T>
|
|
|
|
static MatcherList
|
|
|
|
outvalueToMatcherList(const ast_matchers::internal::Matcher<T> &Matcher) {
|
|
|
|
return MatcherList(Matcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static MatcherList
|
|
|
|
outvalueToMatcherList(const T& PolyMatcher, typename T::ReturnTypes* = NULL) {
|
|
|
|
MatcherList Matchers;
|
|
|
|
mergePolyMatchers(PolyMatcher, &Matchers, typename T::ReturnTypes());
|
|
|
|
return Matchers;
|
|
|
|
}
|
|
|
|
|
2013-05-14 17:13:00 +08:00
|
|
|
/// \brief 0-arg marshaller function.
|
|
|
|
template <typename ReturnType>
|
2013-06-21 23:51:31 +08:00
|
|
|
static MatcherList matcherMarshall0(ReturnType (*Func)(),
|
|
|
|
StringRef MatcherName,
|
|
|
|
const SourceRange &NameRange,
|
|
|
|
ArrayRef<ParserValue> Args,
|
|
|
|
Diagnostics *Error) {
|
2013-05-14 17:13:00 +08:00
|
|
|
CHECK_ARG_COUNT(0);
|
2013-06-21 23:51:31 +08:00
|
|
|
return outvalueToMatcherList(Func());
|
2013-05-14 17:13:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief 1-arg marshaller function.
|
2013-06-04 23:46:22 +08:00
|
|
|
template <typename ReturnType, typename ArgType1>
|
2013-06-21 23:51:31 +08:00
|
|
|
static MatcherList matcherMarshall1(ReturnType (*Func)(ArgType1),
|
|
|
|
StringRef MatcherName,
|
|
|
|
const SourceRange &NameRange,
|
|
|
|
ArrayRef<ParserValue> Args,
|
|
|
|
Diagnostics *Error) {
|
2013-05-14 17:13:00 +08:00
|
|
|
CHECK_ARG_COUNT(1);
|
|
|
|
CHECK_ARG_TYPE(0, ArgType1);
|
2013-06-21 23:51:31 +08:00
|
|
|
return outvalueToMatcherList(
|
|
|
|
Func(ArgTypeTraits<ArgType1>::get(Args[0].Value)));
|
2013-05-14 17:13:00 +08:00
|
|
|
}
|
|
|
|
|
2013-06-04 23:46:22 +08:00
|
|
|
/// \brief 2-arg marshaller function.
|
|
|
|
template <typename ReturnType, typename ArgType1, typename ArgType2>
|
2013-06-21 23:51:31 +08:00
|
|
|
static MatcherList matcherMarshall2(ReturnType (*Func)(ArgType1, ArgType2),
|
|
|
|
StringRef MatcherName,
|
|
|
|
const SourceRange &NameRange,
|
|
|
|
ArrayRef<ParserValue> Args,
|
|
|
|
Diagnostics *Error) {
|
2013-06-04 23:46:22 +08:00
|
|
|
CHECK_ARG_COUNT(2);
|
|
|
|
CHECK_ARG_TYPE(0, ArgType1);
|
|
|
|
CHECK_ARG_TYPE(1, ArgType2);
|
2013-06-21 23:51:31 +08:00
|
|
|
return outvalueToMatcherList(
|
|
|
|
Func(ArgTypeTraits<ArgType1>::get(Args[0].Value),
|
|
|
|
ArgTypeTraits<ArgType2>::get(Args[1].Value)));
|
2013-06-04 23:46:22 +08:00
|
|
|
}
|
|
|
|
|
2013-06-12 02:51:07 +08:00
|
|
|
#undef CHECK_ARG_COUNT
|
|
|
|
#undef CHECK_ARG_TYPE
|
|
|
|
|
2013-05-14 17:13:00 +08:00
|
|
|
/// \brief Variadic marshaller function.
|
|
|
|
template <typename BaseType, typename DerivedType>
|
2013-06-21 23:51:31 +08:00
|
|
|
MatcherList VariadicMatcherCreateCallback(StringRef MatcherName,
|
|
|
|
const SourceRange &NameRange,
|
|
|
|
ArrayRef<ParserValue> Args,
|
|
|
|
Diagnostics *Error) {
|
2013-05-14 17:13:00 +08:00
|
|
|
typedef ast_matchers::internal::Matcher<DerivedType> DerivedMatcherType;
|
2013-06-12 02:51:07 +08:00
|
|
|
DerivedMatcherType **InnerArgs = new DerivedMatcherType *[Args.size()]();
|
|
|
|
|
|
|
|
bool HasError = false;
|
|
|
|
for (size_t i = 0, e = Args.size(); i != e; ++i) {
|
2013-06-20 22:28:32 +08:00
|
|
|
typedef ArgTypeTraits<DerivedMatcherType> DerivedTraits;
|
|
|
|
const ParserValue &Arg = Args[i];
|
|
|
|
const VariantValue &Value = Arg.Value;
|
|
|
|
if (!DerivedTraits::is(Value)) {
|
|
|
|
Error->pushErrorFrame(Arg.Range, Error->ET_RegistryWrongArgType)
|
|
|
|
<< (i + 1) << DerivedTraits::asString() << Value.getTypeAsString();
|
2013-06-12 02:51:07 +08:00
|
|
|
HasError = true;
|
|
|
|
break;
|
2013-05-14 17:13:00 +08:00
|
|
|
}
|
2013-06-20 22:28:32 +08:00
|
|
|
InnerArgs[i] = new DerivedMatcherType(DerivedTraits::get(Value));
|
2013-05-14 17:13:00 +08:00
|
|
|
}
|
|
|
|
|
2013-06-21 23:51:31 +08:00
|
|
|
MatcherList Out;
|
2013-06-12 02:51:07 +08:00
|
|
|
if (!HasError) {
|
|
|
|
Out = ast_matchers::internal::makeDynCastAllOfComposite<BaseType>(
|
2013-06-21 23:51:31 +08:00
|
|
|
ArrayRef<const DerivedMatcherType *>(InnerArgs, Args.size()));
|
2013-06-12 02:51:07 +08:00
|
|
|
}
|
2013-05-14 17:13:00 +08:00
|
|
|
|
2013-06-12 02:51:07 +08:00
|
|
|
for (size_t i = 0, e = Args.size(); i != e; ++i) {
|
|
|
|
delete InnerArgs[i];
|
|
|
|
}
|
|
|
|
delete[] InnerArgs;
|
|
|
|
return Out;
|
|
|
|
}
|
2013-05-14 17:13:00 +08:00
|
|
|
|
|
|
|
/// Helper functions to select the appropriate marshaller functions.
|
2013-06-12 02:51:07 +08:00
|
|
|
/// They detect the number of arguments, arguments types and return type.
|
2013-05-14 17:13:00 +08:00
|
|
|
|
|
|
|
/// \brief 0-arg overload
|
|
|
|
template <typename ReturnType>
|
|
|
|
MatcherCreateCallback *makeMatcherAutoMarshall(ReturnType (*Func)(),
|
|
|
|
StringRef MatcherName) {
|
2013-06-12 02:51:07 +08:00
|
|
|
return new FixedArgCountMatcherCreateCallback<ReturnType (*)()>(
|
|
|
|
matcherMarshall0, Func, MatcherName);
|
2013-05-14 17:13:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief 1-arg overload
|
|
|
|
template <typename ReturnType, typename ArgType1>
|
|
|
|
MatcherCreateCallback *makeMatcherAutoMarshall(ReturnType (*Func)(ArgType1),
|
|
|
|
StringRef MatcherName) {
|
2013-06-12 02:51:07 +08:00
|
|
|
return new FixedArgCountMatcherCreateCallback<ReturnType (*)(ArgType1)>(
|
|
|
|
matcherMarshall1, Func, MatcherName);
|
2013-05-14 17:13:00 +08:00
|
|
|
}
|
|
|
|
|
2013-06-04 23:46:22 +08:00
|
|
|
/// \brief 2-arg overload
|
|
|
|
template <typename ReturnType, typename ArgType1, typename ArgType2>
|
|
|
|
MatcherCreateCallback *makeMatcherAutoMarshall(ReturnType (*Func)(ArgType1,
|
|
|
|
ArgType2),
|
|
|
|
StringRef MatcherName) {
|
2013-06-12 02:51:07 +08:00
|
|
|
return new FixedArgCountMatcherCreateCallback<
|
|
|
|
ReturnType (*)(ArgType1, ArgType2)>(matcherMarshall2, Func, MatcherName);
|
2013-06-04 23:46:22 +08:00
|
|
|
}
|
|
|
|
|
2013-06-21 23:51:31 +08:00
|
|
|
/// \brief Variadic overloads.
|
2013-05-14 17:13:00 +08:00
|
|
|
template <typename MatcherType>
|
|
|
|
MatcherCreateCallback *makeMatcherAutoMarshall(
|
|
|
|
ast_matchers::internal::VariadicAllOfMatcher<MatcherType> Func,
|
|
|
|
StringRef MatcherName) {
|
2013-06-12 02:51:07 +08:00
|
|
|
return new FreeFuncMatcherCreateCallback(
|
|
|
|
&VariadicMatcherCreateCallback<MatcherType, MatcherType>, MatcherName);
|
2013-05-14 17:13:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename BaseType, typename MatcherType>
|
|
|
|
MatcherCreateCallback *
|
|
|
|
makeMatcherAutoMarshall(ast_matchers::internal::VariadicDynCastAllOfMatcher<
|
|
|
|
BaseType, MatcherType> Func,
|
|
|
|
StringRef MatcherName) {
|
2013-06-12 02:51:07 +08:00
|
|
|
return new FreeFuncMatcherCreateCallback(
|
|
|
|
&VariadicMatcherCreateCallback<BaseType, MatcherType>, MatcherName);
|
2013-05-14 17:13:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace dynamic
|
|
|
|
} // namespace ast_matchers
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif // LLVM_CLANG_AST_MATCHERS_DYNAMIC_MARSHALLERS_H
|