clang API Documentation

Registry.h
Go to the documentation of this file.
00001 //===--- Registry.h - Matcher registry -----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 ///
00010 /// \file
00011 /// \brief Registry of all known matchers.
00012 ///
00013 /// The registry provides a generic interface to construct any matcher by name.
00014 ///
00015 //===----------------------------------------------------------------------===//
00016 
00017 #ifndef LLVM_CLANG_ASTMATCHERS_DYNAMIC_REGISTRY_H
00018 #define LLVM_CLANG_ASTMATCHERS_DYNAMIC_REGISTRY_H
00019 
00020 #include "clang/ASTMatchers/Dynamic/Diagnostics.h"
00021 #include "clang/ASTMatchers/Dynamic/VariantValue.h"
00022 #include "clang/Basic/LLVM.h"
00023 #include "llvm/ADT/ArrayRef.h"
00024 #include "llvm/ADT/Optional.h"
00025 #include "llvm/ADT/StringRef.h"
00026 
00027 namespace clang {
00028 namespace ast_matchers {
00029 namespace dynamic {
00030 
00031 namespace internal {
00032 class MatcherDescriptor;
00033 }
00034 
00035 typedef const internal::MatcherDescriptor *MatcherCtor;
00036 
00037 struct MatcherCompletion {
00038   MatcherCompletion() {}
00039   MatcherCompletion(StringRef TypedText, StringRef MatcherDecl,
00040                     unsigned Specificity)
00041       : TypedText(TypedText), MatcherDecl(MatcherDecl),
00042         Specificity(Specificity) {}
00043 
00044   /// \brief The text to type to select this matcher.
00045   std::string TypedText;
00046 
00047   /// \brief The "declaration" of the matcher, with type information.
00048   std::string MatcherDecl;
00049 
00050   /// \brief Value corresponding to the "specificity" of the converted matcher.
00051   ///
00052   /// Zero specificity indicates that this conversion would produce a trivial
00053   /// matcher that will either always or never match.
00054   /// Such matchers are excluded from code completion results.
00055   unsigned Specificity;
00056 
00057   bool operator==(const MatcherCompletion &Other) const {
00058     return TypedText == Other.TypedText && MatcherDecl == Other.MatcherDecl;
00059   }
00060 };
00061 
00062 class Registry {
00063 public:
00064   /// \brief Look up a matcher in the registry by name,
00065   ///
00066   /// \return An opaque value which may be used to refer to the matcher
00067   /// constructor, or Optional<MatcherCtor>() if not found.
00068   static llvm::Optional<MatcherCtor> lookupMatcherCtor(StringRef MatcherName);
00069 
00070   /// \brief Compute the list of completion types for \p Context.
00071   ///
00072   /// Each element of \p Context represents a matcher invocation, going from
00073   /// outermost to innermost. Elements are pairs consisting of a reference to
00074   /// the matcher constructor and the index of the next element in the
00075   /// argument list of that matcher (or for the last element, the index of
00076   /// the completion point in the argument list). An empty list requests
00077   /// completion for the root matcher.
00078   static std::vector<ArgKind> getAcceptedCompletionTypes(
00079       llvm::ArrayRef<std::pair<MatcherCtor, unsigned>> Context);
00080 
00081   /// \brief Compute the list of completions that match any of
00082   /// \p AcceptedTypes.
00083   ///
00084   /// \param AcceptedTypes All types accepted for this completion.
00085   ///
00086   /// \return All completions for the specified types.
00087   /// Completions should be valid when used in \c lookupMatcherCtor().
00088   /// The matcher constructed from the return of \c lookupMatcherCtor()
00089   /// should be convertible to some type in \p AcceptedTypes.
00090   static std::vector<MatcherCompletion>
00091   getMatcherCompletions(ArrayRef<ArgKind> AcceptedTypes);
00092 
00093   /// \brief Construct a matcher from the registry.
00094   ///
00095   /// \param Ctor The matcher constructor to instantiate.
00096   ///
00097   /// \param NameRange The location of the name in the matcher source.
00098   ///   Useful for error reporting.
00099   ///
00100   /// \param Args The argument list for the matcher. The number and types of the
00101   ///   values must be valid for the matcher requested. Otherwise, the function
00102   ///   will return an error.
00103   ///
00104   /// \return The matcher object constructed if no error was found.
00105   ///   A null matcher if the number of arguments or argument types do not match
00106   ///   the signature.  In that case \c Error will contain the description of
00107   ///   the error.
00108   static VariantMatcher constructMatcher(MatcherCtor Ctor,
00109                                          const SourceRange &NameRange,
00110                                          ArrayRef<ParserValue> Args,
00111                                          Diagnostics *Error);
00112 
00113   /// \brief Construct a matcher from the registry and bind it.
00114   ///
00115   /// Similar the \c constructMatcher() above, but it then tries to bind the
00116   /// matcher to the specified \c BindID.
00117   /// If the matcher is not bindable, it sets an error in \c Error and returns
00118   /// a null matcher.
00119   static VariantMatcher constructBoundMatcher(MatcherCtor Ctor,
00120                                               const SourceRange &NameRange,
00121                                               StringRef BindID,
00122                                               ArrayRef<ParserValue> Args,
00123                                               Diagnostics *Error);
00124 
00125 private:
00126   Registry() LLVM_DELETED_FUNCTION;
00127 };
00128 
00129 }  // namespace dynamic
00130 }  // namespace ast_matchers
00131 }  // namespace clang
00132 
00133 #endif  // LLVM_CLANG_AST_MATCHERS_DYNAMIC_REGISTRY_H