Index: cfe/trunk/include/clang/AST/ASTTypeTraits.h =================================================================== --- cfe/trunk/include/clang/AST/ASTTypeTraits.h +++ cfe/trunk/include/clang/AST/ASTTypeTraits.h @@ -64,6 +64,11 @@ /// \brief String representation of the kind. StringRef asStringRef() const; + /// \brief Strict weak ordering for ASTNodeKind. + bool operator<(const ASTNodeKind &Other) const { + return KindId < Other.KindId; + } + private: /// \brief Kind ids. /// @@ -137,6 +142,11 @@ #include "clang/AST/TypeNodes.def" #undef KIND_TO_KIND_ID +inline raw_ostream &operator<<(raw_ostream &OS, ASTNodeKind K) { + OS << K.asStringRef(); + return OS; +} + /// \brief A dynamically typed AST node container. /// /// Stores an AST node in a type safe way. This allows writing code that Index: cfe/trunk/include/clang/ASTMatchers/Dynamic/Registry.h =================================================================== --- cfe/trunk/include/clang/ASTMatchers/Dynamic/Registry.h +++ cfe/trunk/include/clang/ASTMatchers/Dynamic/Registry.h @@ -34,6 +34,22 @@ typedef const internal::MatcherDescriptor *MatcherCtor; +struct MatcherCompletion { + MatcherCompletion() {} + MatcherCompletion(StringRef TypedText, StringRef MatcherDecl) + : TypedText(TypedText), MatcherDecl(MatcherDecl) {} + + /// \brief The text to type to select this matcher. + std::string TypedText; + + /// \brief The "declaration" of the matcher, with type information. + std::string MatcherDecl; + + bool operator==(const MatcherCompletion &Other) const { + return TypedText == Other.TypedText && MatcherDecl == Other.MatcherDecl; + } +}; + class Registry { public: /// \brief Look up a matcher in the registry by name, @@ -45,6 +61,29 @@ lookupMatcherCtor(StringRef MatcherName, const SourceRange &NameRange, Diagnostics *Error); + /// \brief Compute the list of completions for \p Context. + /// + /// Each element of \p Context represents a matcher invocation, going from + /// outermost to innermost. Elements are pairs consisting of a reference to the + /// matcher constructor and the index of the next element in the argument list + /// of that matcher (or for the last element, the index of the completion + /// point in the argument list). An empty list requests completion for the + /// root matcher. + /// + /// The completions are ordered first by decreasing relevance, then + /// alphabetically. Relevance is determined by how closely the matcher's + /// type matches that of the context. For example, if the innermost matcher + /// takes a FunctionDecl matcher, the FunctionDecl matchers are returned + /// first, followed by the ValueDecl matchers, then NamedDecl, then Decl, then + /// polymorphic matchers. + /// + /// Matchers which are technically convertible to the innermost context but + /// which would match either all or no nodes are excluded. For example, + /// namedDecl and varDecl are excluded in a FunctionDecl context, because + /// those matchers would match respectively all or no nodes in such a context. + static std::vector + getCompletions(llvm::ArrayRef > Context); + /// \brief Construct a matcher from the registry. /// /// \param Ctor The matcher constructor to instantiate. Index: cfe/trunk/lib/ASTMatchers/Dynamic/Marshallers.h =================================================================== --- cfe/trunk/lib/ASTMatchers/Dynamic/Marshallers.h +++ cfe/trunk/lib/ASTMatchers/Dynamic/Marshallers.h @@ -34,6 +34,45 @@ namespace internal { +struct ArgKind { + enum Kind { + AK_Matcher, + AK_Unsigned, + AK_String + }; + ArgKind(Kind K) + : K(K) {} + ArgKind(ast_type_traits::ASTNodeKind MatcherKind) + : K(AK_Matcher), MatcherKind(MatcherKind) {} + + std::string asString() const { + switch (getArgKind()) { + case AK_Matcher: + return (Twine("Matcher<") + MatcherKind.asStringRef() + ">").str(); + case AK_Unsigned: + return "unsigned"; + case AK_String: + return "string"; + } + } + + Kind getArgKind() const { return K; } + ast_type_traits::ASTNodeKind getMatcherKind() const { + assert(K == AK_Matcher); + return MatcherKind; + } + + bool operator<(const ArgKind &Other) const { + if (K == AK_Matcher && Other.K == AK_Matcher) + return MatcherKind < Other.MatcherKind; + return K < Other.K; + } + +private: + Kind K; + ast_type_traits::ASTNodeKind MatcherKind; +}; + /// \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. @@ -42,11 +81,13 @@ }; template <> struct ArgTypeTraits { - static StringRef asString() { return "String"; } static bool is(const VariantValue &Value) { return Value.isString(); } static const std::string &get(const VariantValue &Value) { return Value.getString(); } + static ArgKind getKind() { + return ArgKind(ArgKind::AK_String); + } }; template <> @@ -54,38 +95,87 @@ }; template struct ArgTypeTraits > { - static std::string asString() { - return (Twine("Matcher<") + - ast_type_traits::ASTNodeKind::getFromNodeKind().asStringRef() + - ">").str(); - } static bool is(const VariantValue &Value) { return Value.isMatcher() && Value.getMatcher().hasTypedMatcher(); } static ast_matchers::internal::Matcher get(const VariantValue &Value) { return Value.getMatcher().getTypedMatcher(); } + static ArgKind getKind() { + return ArgKind(ast_type_traits::ASTNodeKind::getFromNodeKind()); + } }; template <> struct ArgTypeTraits { - static std::string asString() { return "Unsigned"; } static bool is(const VariantValue &Value) { return Value.isUnsigned(); } static unsigned get(const VariantValue &Value) { return Value.getUnsigned(); } + static ArgKind getKind() { + return ArgKind(ArgKind::AK_Unsigned); + } }; /// \brief Matcher descriptor interface. /// /// Provides a \c create() method that constructs the matcher from the provided -/// arguments. +/// arguments, and various other methods for type introspection. class MatcherDescriptor { public: virtual ~MatcherDescriptor() {} virtual VariantMatcher create(const SourceRange &NameRange, ArrayRef Args, Diagnostics *Error) const = 0; -}; + + /// Returns whether the matcher is variadic. Variadic matchers can take any + /// number of arguments, but they must be of the same type. + virtual bool isVariadic() const = 0; + + /// Returns the number of arguments accepted by the matcher if not variadic. + virtual unsigned getNumArgs() const = 0; + + /// Given that the matcher is being converted to type \p ThisKind, append the + /// set of argument types accepted for argument \p ArgNo to \p ArgKinds. + // FIXME: We should provide the ability to constrain the output of this + // function based on the types of other matcher arguments. + virtual void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, + std::vector &ArgKinds) const = 0; + + /// Returns whether this matcher is convertible to the given type. If it is + /// so convertible, store in *Specificity a value corresponding to the + /// "specificity" of the converted matcher to the given context, and in + /// *LeastDerivedKind the least derived matcher kind which would result in the + /// same matcher overload. Zero specificity indicates that this conversion + /// would produce a trivial matcher that will either always or never match. + /// Such matchers are excluded from code completion results. + virtual bool isConvertibleTo( + ast_type_traits::ASTNodeKind Kind, unsigned *Specificity = 0, + ast_type_traits::ASTNodeKind *LeastDerivedKind = 0) const = 0; + + /// Returns whether the matcher will, given a matcher of any type T, yield a + /// matcher of type T. + virtual bool isPolymorphic() const { return false; } +}; + +inline bool isRetKindConvertibleTo( + llvm::ArrayRef RetKinds, + ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, + ast_type_traits::ASTNodeKind *LeastDerivedKind) { + for (llvm::ArrayRef::const_iterator + i = RetKinds.begin(), + e = RetKinds.end(); + i != e; ++i) { + unsigned Distance; + if (i->isBaseOf(Kind, &Distance)) { + if (Specificity) + *Specificity = 100 - Distance; + if (LeastDerivedKind) + *LeastDerivedKind = *i; + return true; + } + } + return false; +} /// \brief Simple callback implementation. Marshaller and function are provided. /// @@ -104,46 +194,225 @@ /// \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. - FixedArgCountMatcherDescriptor(MarshallerType Marshaller, void (*Func)(), - StringRef MatcherName) - : Marshaller(Marshaller), Func(Func), MatcherName(MatcherName) {} + /// \param RetKinds The list of matcher types to which the matcher is + /// convertible. + /// \param ArgKinds The types of the arguments this matcher takes. + FixedArgCountMatcherDescriptor( + MarshallerType Marshaller, void (*Func)(), StringRef MatcherName, + llvm::ArrayRef RetKinds, + llvm::ArrayRef ArgKinds) + : Marshaller(Marshaller), Func(Func), MatcherName(MatcherName), + RetKinds(RetKinds.begin(), RetKinds.end()), + ArgKinds(ArgKinds.begin(), ArgKinds.end()) {} VariantMatcher create(const SourceRange &NameRange, ArrayRef Args, Diagnostics *Error) const { return Marshaller(Func, MatcherName, NameRange, Args, Error); } + bool isVariadic() const { return false; } + unsigned getNumArgs() const { return ArgKinds.size(); } + void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, + std::vector &Kinds) const { + Kinds.push_back(ArgKinds[ArgNo]); + } + bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, + ast_type_traits::ASTNodeKind *LeastDerivedKind) const { + return isRetKindConvertibleTo(RetKinds, Kind, Specificity, + LeastDerivedKind); + } + private: const MarshallerType Marshaller; void (* const Func)(); const std::string MatcherName; + const std::vector RetKinds; + const std::vector ArgKinds; }; -/// \brief Simple callback implementation. Free function is wrapped. +/// \brief Helper methods to extract and merge all possible typed matchers +/// out of the polymorphic object. +template +static void mergePolyMatchers(const PolyMatcher &Poly, + std::vector &Out, + ast_matchers::internal::EmptyTypeList) {} + +template +static void mergePolyMatchers(const PolyMatcher &Poly, + std::vector &Out, TypeList) { + Out.push_back(ast_matchers::internal::Matcher(Poly)); + mergePolyMatchers(Poly, Out, typename TypeList::tail()); +} + +/// \brief Convert the return values of the functions into a VariantMatcher. /// -/// This class simply wraps a free function with the right signature to export +/// There are 2 cases right now: The return value is a Matcher or is a +/// polymorphic matcher. For the former, we just construct the VariantMatcher. +/// For the latter, we instantiate all the possible Matcher of the poly +/// matcher. +static VariantMatcher outvalueToVariantMatcher(const DynTypedMatcher &Matcher) { + return VariantMatcher::SingleMatcher(Matcher); +} + +template +static VariantMatcher outvalueToVariantMatcher(const T &PolyMatcher, + typename T::ReturnTypes * = + NULL) { + std::vector Matchers; + mergePolyMatchers(PolyMatcher, Matchers, typename T::ReturnTypes()); + VariantMatcher Out = VariantMatcher::PolymorphicMatcher(Matchers); + return Out; +} + +template +inline void buildReturnTypeVectorFromTypeList( + std::vector &RetTypes) { + RetTypes.push_back( + ast_type_traits::ASTNodeKind::getFromNodeKind()); + buildReturnTypeVectorFromTypeList(RetTypes); +} + +template <> +inline void +buildReturnTypeVectorFromTypeList( + std::vector &RetTypes) {} + +template +struct BuildReturnTypeVector { + static void build(std::vector &RetTypes) { + buildReturnTypeVectorFromTypeList(RetTypes); + } +}; + +template +struct BuildReturnTypeVector > { + static void build(std::vector &RetTypes) { + RetTypes.push_back(ast_type_traits::ASTNodeKind::getFromNodeKind()); + } +}; + +template +struct BuildReturnTypeVector > { + static void build(std::vector &RetTypes) { + RetTypes.push_back(ast_type_traits::ASTNodeKind::getFromNodeKind()); + } +}; + +/// \brief Variadic marshaller function. +template )> +VariantMatcher +variadicMatcherDescriptor(StringRef MatcherName, const SourceRange &NameRange, + ArrayRef Args, Diagnostics *Error) { + ArgT **InnerArgs = new ArgT *[Args.size()](); + + bool HasError = false; + for (size_t i = 0, e = Args.size(); i != e; ++i) { + typedef ArgTypeTraits ArgTraits; + const ParserValue &Arg = Args[i]; + const VariantValue &Value = Arg.Value; + if (!ArgTraits::is(Value)) { + Error->addError(Arg.Range, Error->ET_RegistryWrongArgType) + << (i + 1) << ArgTraits::getKind().asString() << Value.getTypeAsString(); + HasError = true; + break; + } + InnerArgs[i] = new ArgT(ArgTraits::get(Value)); + } + + VariantMatcher Out; + if (!HasError) { + Out = outvalueToVariantMatcher( + Func(ArrayRef(InnerArgs, Args.size()))); + } + + for (size_t i = 0, e = Args.size(); i != e; ++i) { + delete InnerArgs[i]; + } + delete[] InnerArgs; + return Out; +} + +/// \brief Matcher descriptor for variadic functions. +/// +/// This class simply wraps a VariadicFunction with the right signature to export /// it as a MatcherDescriptor. /// 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 FreeFuncMatcherDescriptor : public MatcherDescriptor { +class VariadicFuncMatcherDescriptor : public MatcherDescriptor { public: typedef VariantMatcher (*RunFunc)(StringRef MatcherName, const SourceRange &NameRange, ArrayRef Args, Diagnostics *Error); - FreeFuncMatcherDescriptor(RunFunc Func, StringRef MatcherName) - : Func(Func), MatcherName(MatcherName.str()) {} + template )> + VariadicFuncMatcherDescriptor(llvm::VariadicFunction Func, + StringRef MatcherName) + : Func(&variadicMatcherDescriptor), + MatcherName(MatcherName.str()), + ArgsKind(ArgTypeTraits::getKind()) { + BuildReturnTypeVector::build(RetKinds); + } VariantMatcher create(const SourceRange &NameRange, ArrayRef Args, Diagnostics *Error) const { return Func(MatcherName, NameRange, Args, Error); } + bool isVariadic() const { return true; } + unsigned getNumArgs() const { return 0; } + void getArgKinds(ast_type_traits::ASTNodeKind ThisKind, unsigned ArgNo, + std::vector &Kinds) const { + Kinds.push_back(ArgsKind); + } + bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, + ast_type_traits::ASTNodeKind *LeastDerivedKind) const { + return isRetKindConvertibleTo(RetKinds, Kind, Specificity, + LeastDerivedKind); + } + private: const RunFunc Func; const std::string MatcherName; + std::vector RetKinds; + const ArgKind ArgsKind; +}; + +/// \brief Return CK_Trivial when appropriate for VariadicDynCastAllOfMatchers. +class DynCastAllOfMatcherDescriptor : public VariadicFuncMatcherDescriptor { +public: + template + DynCastAllOfMatcherDescriptor( + ast_matchers::internal::VariadicDynCastAllOfMatcher Func, + StringRef MatcherName) + : VariadicFuncMatcherDescriptor(Func, MatcherName), + DerivedKind(ast_type_traits::ASTNodeKind::getFromNodeKind()) { + } + + bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind, unsigned *Specificity, + ast_type_traits::ASTNodeKind *LeastDerivedKind) const + LLVM_OVERRIDE { + // If Kind is not a base of DerivedKind, either DerivedKind is a base of + // Kind (in which case the match will always succeed) or Kind and + // DerivedKind are unrelated (in which case it will always fail), so set + // Specificity to 0. + if (VariadicFuncMatcherDescriptor::isConvertibleTo(Kind, Specificity, + LeastDerivedKind)) { + if (Kind.isSame(DerivedKind) || !Kind.isBaseOf(DerivedKind)) { + if (Specificity) + *Specificity = 0; + } + return true; + } else { + return false; + } + } + +private: + const ast_type_traits::ASTNodeKind DerivedKind; }; /// \brief Helper macros to check the arguments on all marshaller functions. @@ -157,44 +426,11 @@ #define CHECK_ARG_TYPE(index, type) \ if (!ArgTypeTraits::is(Args[index].Value)) { \ Error->addError(Args[index].Range, Error->ET_RegistryWrongArgType) \ - << (index + 1) << ArgTypeTraits::asString() \ + << (index + 1) << ArgTypeTraits::getKind().asString() \ << Args[index].Value.getTypeAsString(); \ return VariantMatcher(); \ } -/// \brief Helper methods to extract and merge all possible typed matchers -/// out of the polymorphic object. -template -static void mergePolyMatchers(const PolyMatcher &Poly, - std::vector &Out, - ast_matchers::internal::EmptyTypeList) {} - -template -static void mergePolyMatchers(const PolyMatcher &Poly, - std::vector &Out, TypeList) { - Out.push_back(ast_matchers::internal::Matcher(Poly)); - mergePolyMatchers(Poly, Out, typename TypeList::tail()); -} - -/// \brief Convert the return values of the functions into a VariantMatcher. -/// -/// There are 2 cases right now: The return value is a Matcher or is a -/// polymorphic matcher. For the former, we just construct the VariantMatcher. -/// For the latter, we instantiate all the possible Matcher of the poly -/// matcher. -static VariantMatcher outvalueToVariantMatcher(const DynTypedMatcher &Matcher) { - return VariantMatcher::SingleMatcher(Matcher); -} - -template -static VariantMatcher outvalueToVariantMatcher(const T &PolyMatcher, - typename T::ReturnTypes * = - NULL) { - std::vector Matchers; - mergePolyMatchers(PolyMatcher, Matchers, typename T::ReturnTypes()); - VariantMatcher Out = VariantMatcher::PolymorphicMatcher(Matchers); - return Out; -} /// \brief 0-arg marshaller function. template @@ -238,41 +474,6 @@ #undef CHECK_ARG_COUNT #undef CHECK_ARG_TYPE -/// \brief Variadic marshaller function. -template )> -VariantMatcher -variadicMatcherDescriptor(StringRef MatcherName, const SourceRange &NameRange, - ArrayRef Args, Diagnostics *Error) { - ArgT **InnerArgs = new ArgT *[Args.size()](); - - bool HasError = false; - for (size_t i = 0, e = Args.size(); i != e; ++i) { - typedef ArgTypeTraits ArgTraits; - const ParserValue &Arg = Args[i]; - const VariantValue &Value = Arg.Value; - if (!ArgTraits::is(Value)) { - Error->addError(Arg.Range, Error->ET_RegistryWrongArgType) - << (i + 1) << ArgTraits::asString() << Value.getTypeAsString(); - HasError = true; - break; - } - InnerArgs[i] = new ArgT(ArgTraits::get(Value)); - } - - VariantMatcher Out; - if (!HasError) { - Out = outvalueToVariantMatcher( - Func(ArrayRef(InnerArgs, Args.size()))); - } - - for (size_t i = 0, e = Args.size(); i != e; ++i) { - delete InnerArgs[i]; - } - delete[] InnerArgs; - return Out; -} - /// \brief Helper class used to collect all the possible overloads of an /// argument adaptative matcher function. template