clang  3.9.0
Registry.h
Go to the documentation of this file.
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 
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 
36 
39  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>
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.
109  SourceRange NameRange,
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.
120  SourceRange NameRange,
121  StringRef BindID,
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
static VariantMatcher constructBoundMatcher(MatcherCtor Ctor, SourceRange NameRange, StringRef BindID, ArrayRef< ParserValue > Args, Diagnostics *Error)
Construct a matcher from the registry and bind it.
Definition: Registry.cpp:579
static std::vector< MatcherCompletion > getMatcherCompletions(ArrayRef< ArgKind > AcceptedTypes)
Compute the list of completions that match any of AcceptedTypes.
Definition: Registry.cpp:492
bool operator==(const MatcherCompletion &Other) const
Definition: Registry.h:57
static std::vector< ArgKind > getAcceptedCompletionTypes(llvm::ArrayRef< std::pair< MatcherCtor, unsigned >> Context)
Compute the list of completion types for Context.
Definition: Registry.cpp:461
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
std::string MatcherDecl
The "declaration" of the matcher, with type information.
Definition: Registry.h:48
static llvm::Optional< MatcherCtor > lookupMatcherCtor(StringRef MatcherName)
Look up a matcher in the registry by name,.
Definition: Registry.cpp:433
ASTContext * Context
const internal::MatcherDescriptor * MatcherCtor
Definition: Registry.h:35
Diagnostics class to manage error messages.
Helper class to manage error messages.
Definition: Diagnostics.h:51
unsigned Specificity
Value corresponding to the "specificity" of the converted matcher.
Definition: Registry.h:55
std::string TypedText
The text to type to select this matcher.
Definition: Registry.h:45
MatcherCompletion(StringRef TypedText, StringRef MatcherDecl, unsigned Specificity)
Definition: Registry.h:39
Polymorphic value type.
static VariantMatcher constructMatcher(MatcherCtor Ctor, SourceRange NameRange, ArrayRef< ParserValue > Args, Diagnostics *Error)
Construct a matcher from the registry.
Definition: Registry.cpp:571