1 //===--- Registry.h - Matcher registry -----*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// 10 /// \file 11 /// \brief Registry of all known matchers. 12 /// 13 /// The registry provides a generic interface to construct any matcher by name. 14 /// 15 //===----------------------------------------------------------------------===// 16 17 #ifndef LLVM_CLANG_ASTMATCHERS_DYNAMIC_REGISTRY_H 18 #define LLVM_CLANG_ASTMATCHERS_DYNAMIC_REGISTRY_H 19 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" 26 27 namespace clang { 28 namespace ast_matchers { 29 namespace dynamic { 30 31 namespace internal { 32 class MatcherDescriptor; 33 } 34 35 typedef const internal::MatcherDescriptor *MatcherCtor; 36 37 struct MatcherCompletion { MatcherCompletionMatcherCompletion38 MatcherCompletion() {} MatcherCompletionMatcherCompletion39 MatcherCompletion(StringRef TypedText, StringRef MatcherDecl, 40 unsigned Specificity) 41 : TypedText(TypedText), MatcherDecl(MatcherDecl), 42 Specificity(Specificity) {} 43 44 /// \brief The text to type to select this matcher. 45 std::string TypedText; 46 47 /// \brief The "declaration" of the matcher, with type information. 48 std::string MatcherDecl; 49 50 /// \brief Value corresponding to the "specificity" of the converted matcher. 51 /// 52 /// Zero specificity indicates that this conversion would produce a trivial 53 /// matcher that will either always or never match. 54 /// Such matchers are excluded from code completion results. 55 unsigned Specificity; 56 57 bool operator==(const MatcherCompletion &Other) const { 58 return TypedText == Other.TypedText && MatcherDecl == Other.MatcherDecl; 59 } 60 }; 61 62 class Registry { 63 public: 64 /// \brief Look up a matcher in the registry by name, 65 /// 66 /// \return An opaque value which may be used to refer to the matcher 67 /// constructor, or Optional<MatcherCtor>() if not found. 68 static llvm::Optional<MatcherCtor> lookupMatcherCtor(StringRef MatcherName); 69 70 /// \brief Compute the list of completion types for \p Context. 71 /// 72 /// Each element of \p Context represents a matcher invocation, going from 73 /// outermost to innermost. Elements are pairs consisting of a reference to 74 /// the matcher constructor and the index of the next element in the 75 /// argument list of that matcher (or for the last element, the index of 76 /// the completion point in the argument list). An empty list requests 77 /// completion for the root matcher. 78 static std::vector<ArgKind> getAcceptedCompletionTypes( 79 llvm::ArrayRef<std::pair<MatcherCtor, unsigned>> Context); 80 81 /// \brief Compute the list of completions that match any of 82 /// \p AcceptedTypes. 83 /// 84 /// \param AcceptedTypes All types accepted for this completion. 85 /// 86 /// \return All completions for the specified types. 87 /// Completions should be valid when used in \c lookupMatcherCtor(). 88 /// The matcher constructed from the return of \c lookupMatcherCtor() 89 /// should be convertible to some type in \p AcceptedTypes. 90 static std::vector<MatcherCompletion> 91 getMatcherCompletions(ArrayRef<ArgKind> AcceptedTypes); 92 93 /// \brief Construct a matcher from the registry. 94 /// 95 /// \param Ctor The matcher constructor to instantiate. 96 /// 97 /// \param NameRange The location of the name in the matcher source. 98 /// Useful for error reporting. 99 /// 100 /// \param Args The argument list for the matcher. The number and types of the 101 /// values must be valid for the matcher requested. Otherwise, the function 102 /// will return an error. 103 /// 104 /// \return The matcher object constructed if no error was found. 105 /// A null matcher if the number of arguments or argument types do not match 106 /// the signature. In that case \c Error will contain the description of 107 /// the error. 108 static VariantMatcher constructMatcher(MatcherCtor Ctor, 109 SourceRange NameRange, 110 ArrayRef<ParserValue> Args, 111 Diagnostics *Error); 112 113 /// \brief Construct a matcher from the registry and bind it. 114 /// 115 /// Similar the \c constructMatcher() above, but it then tries to bind the 116 /// matcher to the specified \c BindID. 117 /// If the matcher is not bindable, it sets an error in \c Error and returns 118 /// a null matcher. 119 static VariantMatcher constructBoundMatcher(MatcherCtor Ctor, 120 SourceRange NameRange, 121 StringRef BindID, 122 ArrayRef<ParserValue> Args, 123 Diagnostics *Error); 124 125 private: 126 Registry() = delete; 127 }; 128 129 } // namespace dynamic 130 } // namespace ast_matchers 131 } // namespace clang 132 133 #endif // LLVM_CLANG_AST_MATCHERS_DYNAMIC_REGISTRY_H 134