Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:46

0001 //===- Registry.h - Matcher registry ----------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 /// \file
0010 /// Registry of all known matchers.
0011 ///
0012 /// The registry provides a generic interface to construct any matcher by name.
0013 //
0014 //===----------------------------------------------------------------------===//
0015 
0016 #ifndef LLVM_CLANG_ASTMATCHERS_DYNAMIC_REGISTRY_H
0017 #define LLVM_CLANG_ASTMATCHERS_DYNAMIC_REGISTRY_H
0018 
0019 #include "clang/ASTMatchers/Dynamic/Diagnostics.h"
0020 #include "clang/ASTMatchers/Dynamic/VariantValue.h"
0021 #include "llvm/ADT/ArrayRef.h"
0022 #include "llvm/ADT/StringRef.h"
0023 #include <optional>
0024 #include <string>
0025 #include <utility>
0026 #include <vector>
0027 
0028 namespace clang {
0029 namespace ast_matchers {
0030 namespace dynamic {
0031 
0032 namespace internal {
0033 
0034 class MatcherDescriptor;
0035 
0036 /// A smart (owning) pointer for MatcherDescriptor. We can't use unique_ptr
0037 /// because MatcherDescriptor is forward declared
0038 class MatcherDescriptorPtr {
0039 public:
0040   explicit MatcherDescriptorPtr(MatcherDescriptor *);
0041   ~MatcherDescriptorPtr();
0042   MatcherDescriptorPtr(MatcherDescriptorPtr &&) = default;
0043   MatcherDescriptorPtr &operator=(MatcherDescriptorPtr &&) = default;
0044   MatcherDescriptorPtr(const MatcherDescriptorPtr &) = delete;
0045   MatcherDescriptorPtr &operator=(const MatcherDescriptorPtr &) = delete;
0046 
0047   MatcherDescriptor *get() { return Ptr; }
0048 
0049 private:
0050   MatcherDescriptor *Ptr;
0051 };
0052 
0053 } // namespace internal
0054 
0055 using MatcherCtor = const internal::MatcherDescriptor *;
0056 
0057 struct MatcherCompletion {
0058   MatcherCompletion() = default;
0059   MatcherCompletion(StringRef TypedText, StringRef MatcherDecl,
0060                     unsigned Specificity)
0061       : TypedText(TypedText), MatcherDecl(MatcherDecl),
0062         Specificity(Specificity) {}
0063 
0064   bool operator==(const MatcherCompletion &Other) const {
0065     return TypedText == Other.TypedText && MatcherDecl == Other.MatcherDecl;
0066   }
0067 
0068   /// The text to type to select this matcher.
0069   std::string TypedText;
0070 
0071   /// The "declaration" of the matcher, with type information.
0072   std::string MatcherDecl;
0073 
0074   /// Value corresponding to the "specificity" of the converted matcher.
0075   ///
0076   /// Zero specificity indicates that this conversion would produce a trivial
0077   /// matcher that will either always or never match.
0078   /// Such matchers are excluded from code completion results.
0079   unsigned Specificity;
0080 };
0081 
0082 class Registry {
0083 public:
0084   Registry() = delete;
0085 
0086   static ASTNodeKind nodeMatcherType(MatcherCtor);
0087 
0088   static bool isBuilderMatcher(MatcherCtor Ctor);
0089 
0090   static internal::MatcherDescriptorPtr
0091   buildMatcherCtor(MatcherCtor, SourceRange NameRange,
0092                    ArrayRef<ParserValue> Args, Diagnostics *Error);
0093 
0094   /// Look up a matcher in the registry by name,
0095   ///
0096   /// \return An opaque value which may be used to refer to the matcher
0097   /// constructor, or std::optional<MatcherCtor>() if not found.
0098   static std::optional<MatcherCtor> lookupMatcherCtor(StringRef MatcherName);
0099 
0100   /// Compute the list of completion types for \p Context.
0101   ///
0102   /// Each element of \p Context represents a matcher invocation, going from
0103   /// outermost to innermost. Elements are pairs consisting of a reference to
0104   /// the matcher constructor and the index of the next element in the
0105   /// argument list of that matcher (or for the last element, the index of
0106   /// the completion point in the argument list). An empty list requests
0107   /// completion for the root matcher.
0108   static std::vector<ArgKind> getAcceptedCompletionTypes(
0109       llvm::ArrayRef<std::pair<MatcherCtor, unsigned>> Context);
0110 
0111   /// Compute the list of completions that match any of
0112   /// \p AcceptedTypes.
0113   ///
0114   /// \param AcceptedTypes All types accepted for this completion.
0115   ///
0116   /// \return All completions for the specified types.
0117   /// Completions should be valid when used in \c lookupMatcherCtor().
0118   /// The matcher constructed from the return of \c lookupMatcherCtor()
0119   /// should be convertible to some type in \p AcceptedTypes.
0120   static std::vector<MatcherCompletion>
0121   getMatcherCompletions(ArrayRef<ArgKind> AcceptedTypes);
0122 
0123   /// Construct a matcher from the registry.
0124   ///
0125   /// \param Ctor The matcher constructor to instantiate.
0126   ///
0127   /// \param NameRange The location of the name in the matcher source.
0128   ///   Useful for error reporting.
0129   ///
0130   /// \param Args The argument list for the matcher. The number and types of the
0131   ///   values must be valid for the matcher requested. Otherwise, the function
0132   ///   will return an error.
0133   ///
0134   /// \return The matcher object constructed if no error was found.
0135   ///   A null matcher if the number of arguments or argument types do not match
0136   ///   the signature.  In that case \c Error will contain the description of
0137   ///   the error.
0138   static VariantMatcher constructMatcher(MatcherCtor Ctor,
0139                                          SourceRange NameRange,
0140                                          ArrayRef<ParserValue> Args,
0141                                          Diagnostics *Error);
0142 
0143   /// Construct a matcher from the registry and bind it.
0144   ///
0145   /// Similar the \c constructMatcher() above, but it then tries to bind the
0146   /// matcher to the specified \c BindID.
0147   /// If the matcher is not bindable, it sets an error in \c Error and returns
0148   /// a null matcher.
0149   static VariantMatcher constructBoundMatcher(MatcherCtor Ctor,
0150                                               SourceRange NameRange,
0151                                               StringRef BindID,
0152                                               ArrayRef<ParserValue> Args,
0153                                               Diagnostics *Error);
0154 };
0155 
0156 } // namespace dynamic
0157 } // namespace ast_matchers
0158 } // namespace clang
0159 
0160 #endif // LLVM_CLANG_ASTMATCHERS_DYNAMIC_REGISTRY_H