1 //===--- Registry.h - Matcher registry -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Registry of all known matchers.
13 /// The registry provides a generic interface to construct any matcher by name.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_CLANG_AST_MATCHERS_DYNAMIC_REGISTRY_H
18 #define LLVM_CLANG_AST_MATCHERS_DYNAMIC_REGISTRY_H
20 #include "clang/ASTMatchers/Dynamic/Diagnostics.h"
21 #include "clang/ASTMatchers/Dynamic/VariantValue.h"
22 #include "clang/Basic/LLVM.h"
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/ADT/Optional.h"
25 #include "llvm/ADT/StringRef.h"
28 namespace ast_matchers {
32 class MatcherDescriptor;
35 typedef const internal::MatcherDescriptor *MatcherCtor;
37 struct MatcherCompletion {
38 MatcherCompletion() {}
39 MatcherCompletion(StringRef TypedText, StringRef MatcherDecl)
40 : TypedText(TypedText), MatcherDecl(MatcherDecl) {}
42 /// \brief The text to type to select this matcher.
43 std::string TypedText;
45 /// \brief The "declaration" of the matcher, with type information.
46 std::string MatcherDecl;
48 bool operator==(const MatcherCompletion &Other) const {
49 return TypedText == Other.TypedText && MatcherDecl == Other.MatcherDecl;
55 /// \brief Look up a matcher in the registry by name,
57 /// \return An opaque value which may be used to refer to the matcher
58 /// constructor, or Optional<MatcherCtor>() if not found.
59 static llvm::Optional<MatcherCtor> lookupMatcherCtor(StringRef MatcherName);
61 /// \brief Compute the list of completions for \p Context.
63 /// Each element of \p Context represents a matcher invocation, going from
64 /// outermost to innermost. Elements are pairs consisting of a reference to the
65 /// matcher constructor and the index of the next element in the argument list
66 /// of that matcher (or for the last element, the index of the completion
67 /// point in the argument list). An empty list requests completion for the
70 /// The completions are ordered first by decreasing relevance, then
71 /// alphabetically. Relevance is determined by how closely the matcher's
72 /// type matches that of the context. For example, if the innermost matcher
73 /// takes a FunctionDecl matcher, the FunctionDecl matchers are returned
74 /// first, followed by the ValueDecl matchers, then NamedDecl, then Decl, then
75 /// polymorphic matchers.
77 /// Matchers which are technically convertible to the innermost context but
78 /// which would match either all or no nodes are excluded. For example,
79 /// namedDecl and varDecl are excluded in a FunctionDecl context, because
80 /// those matchers would match respectively all or no nodes in such a context.
81 static std::vector<MatcherCompletion>
82 getCompletions(ArrayRef<std::pair<MatcherCtor, unsigned> > Context);
84 /// \brief Construct a matcher from the registry.
86 /// \param Ctor The matcher constructor to instantiate.
88 /// \param NameRange The location of the name in the matcher source.
89 /// Useful for error reporting.
91 /// \param Args The argument list for the matcher. The number and types of the
92 /// values must be valid for the matcher requested. Otherwise, the function
93 /// will return an error.
95 /// \return The matcher object constructed if no error was found.
96 /// A null matcher if the number of arguments or argument types do not match
97 /// the signature. In that case \c Error will contain the description of
99 static VariantMatcher constructMatcher(MatcherCtor Ctor,
100 const SourceRange &NameRange,
101 ArrayRef<ParserValue> Args,
104 /// \brief Construct a matcher from the registry and bind it.
106 /// Similar the \c constructMatcher() above, but it then tries to bind the
107 /// matcher to the specified \c BindID.
108 /// If the matcher is not bindable, it sets an error in \c Error and returns
110 static VariantMatcher constructBoundMatcher(MatcherCtor Ctor,
111 const SourceRange &NameRange,
113 ArrayRef<ParserValue> Args,
117 Registry() LLVM_DELETED_FUNCTION;
120 } // namespace dynamic
121 } // namespace ast_matchers
124 #endif // LLVM_CLANG_AST_MATCHERS_DYNAMIC_REGISTRY_H