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