1f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek//===--- Parser.h - Matcher expression parser -----*- C++ -*-===//
2f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek//
3f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek//                     The LLVM Compiler Infrastructure
4f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek//
5f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek// This file is distributed under the University of Illinois Open Source
6f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek// License. See LICENSE.TXT for details.
7f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek//
8f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek//===----------------------------------------------------------------------===//
9f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek///
10f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek/// \file
11f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek/// \brief Simple matcher expression parser.
12f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek///
13f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek/// The parser understands matcher expressions of the form:
14f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek///   MatcherName(Arg0, Arg1, ..., ArgN)
15f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek/// as well as simple types like strings.
16f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek/// The parser does not know how to process the matchers. It delegates this task
17f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek/// to a Sema object received as an argument.
18f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek///
19f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek/// \code
20f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek/// Grammar for the expressions supported:
216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// <Expression>        := <Literal> | <NamedValue> | <MatcherExpression>
227a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel Benzaquen/// <Literal>           := <StringLiteral> | <Unsigned>
23f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek/// <StringLiteral>     := "quoted string"
247a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel Benzaquen/// <Unsigned>          := [0-9]+
256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// <NamedValue>        := <Identifier>
266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// <MatcherExpression> := <Identifier>(<ArgumentList>) |
276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines///                        <Identifier>(<ArgumentList>).bind(<StringLiteral>)
286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/// <Identifier>        := [a-zA-Z]+
29f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek/// <ArgumentList>      := <Expression> | <Expression>,<ArgumentList>
30f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek/// \endcode
31f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek///
32f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek//===----------------------------------------------------------------------===//
33f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
34176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#ifndef LLVM_CLANG_ASTMATCHERS_DYNAMIC_PARSER_H
35176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines#define LLVM_CLANG_ASTMATCHERS_DYNAMIC_PARSER_H
36f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
37f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek#include "clang/ASTMatchers/Dynamic/Diagnostics.h"
38651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "clang/ASTMatchers/Dynamic/Registry.h"
39f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek#include "clang/ASTMatchers/Dynamic/VariantValue.h"
40f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek#include "clang/Basic/LLVM.h"
41f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek#include "llvm/ADT/ArrayRef.h"
42b7488d77414b000ce2506b520a6b29f845fb3950Samuel Benzaquen#include "llvm/ADT/Optional.h"
43f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek#include "llvm/ADT/StringRef.h"
44f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
45f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimeknamespace clang {
46f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimeknamespace ast_matchers {
47f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimeknamespace dynamic {
48f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
49f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek/// \brief Matcher expression parser.
50f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimekclass Parser {
51f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimekpublic:
52f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  /// \brief Interface to connect the parser with the registry and more.
53f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  ///
54f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  /// The parser uses the Sema instance passed into
55f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  /// parseMatcherExpression() to handle all matcher tokens. The simplest
56f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  /// processor implementation would simply call into the registry to create
57f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  /// the matchers.
58f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  /// However, a more complex processor might decide to intercept the matcher
59f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  /// creation and do some extra work. For example, it could apply some
60f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  /// transformation to the matcher by adding some id() nodes, or could detect
61f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  /// specific matcher nodes for more efficient lookup.
62f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  class Sema {
63f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  public:
64f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek    virtual ~Sema();
65f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
66f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek    /// \brief Process a matcher expression.
67f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek    ///
68f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek    /// All the arguments passed here have already been processed.
69f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek    ///
70651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// \param Ctor A matcher constructor looked up by lookupMatcherCtor.
71f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek    ///
72f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek    /// \param NameRange The location of the name in the matcher source.
73f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek    ///   Useful for error reporting.
74f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek    ///
754f37d925927dfdd0c770702ffb22de38fc2007dcSamuel Benzaquen    /// \param BindID The ID to use to bind the matcher, or a null \c StringRef
764f37d925927dfdd0c770702ffb22de38fc2007dcSamuel Benzaquen    ///   if no ID is specified.
774f37d925927dfdd0c770702ffb22de38fc2007dcSamuel Benzaquen    ///
78f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek    /// \param Args The argument list for the matcher.
79f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek    ///
809d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen    /// \return The matcher objects constructed by the processor, or a null
819d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen    ///   matcher if an error occurred. In that case, \c Error will contain a
82f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek    ///   description of the error.
83651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    virtual VariantMatcher actOnMatcherExpression(MatcherCtor Ctor,
849d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen                                                  const SourceRange &NameRange,
859d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen                                                  StringRef BindID,
869d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen                                                  ArrayRef<ParserValue> Args,
879d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen                                                  Diagnostics *Error) = 0;
88651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
89651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// \brief Look up a matcher by name.
90651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    ///
91651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    /// \param MatcherName The matcher name found by the parser.
92651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    ///
936bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    /// \return The matcher constructor, or Optional<MatcherCtor>() if not
946bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    /// found.
95651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    virtual llvm::Optional<MatcherCtor>
966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    lookupMatcherCtor(StringRef MatcherName) = 0;
97176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
98176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// \brief Compute the list of completion types for \p Context.
99176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    ///
100176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// Each element of \p Context represents a matcher invocation, going from
101176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// outermost to innermost. Elements are pairs consisting of a reference to
102176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// the matcher constructor and the index of the next element in the
103176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// argument list of that matcher (or for the last element, the index of
104176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// the completion point in the argument list). An empty list requests
105176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// completion for the root matcher.
106176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    virtual std::vector<ArgKind> getAcceptedCompletionTypes(
107176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        llvm::ArrayRef<std::pair<MatcherCtor, unsigned>> Context);
108176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
109176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// \brief Compute the list of completions that match any of
110176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// \p AcceptedTypes.
111176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    ///
112176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// \param AcceptedTypes All types accepted for this completion.
113176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    ///
114176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// \return All completions for the specified types.
115176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// Completions should be valid when used in \c lookupMatcherCtor().
116176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// The matcher constructed from the return of \c lookupMatcherCtor()
117176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    /// should be convertible to some type in \p AcceptedTypes.
118176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    virtual std::vector<MatcherCompletion>
119176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    getMatcherCompletions(llvm::ArrayRef<ArgKind> AcceptedTypes);
1206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  };
1216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
1226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  /// \brief Sema implementation that uses the matcher registry to process the
1236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  ///   tokens.
1246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  class RegistrySema : public Parser::Sema {
1256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines   public:
12633337ca4d89605025818daf83390ab4271d598d9Pirama Arumuga Nainar     ~RegistrySema() override;
1276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
1286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    llvm::Optional<MatcherCtor>
1296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    lookupMatcherCtor(StringRef MatcherName) override;
1306bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
1316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    VariantMatcher actOnMatcherExpression(MatcherCtor Ctor,
1326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                          const SourceRange &NameRange,
1336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                          StringRef BindID,
1346bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                          ArrayRef<ParserValue> Args,
1356bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                          Diagnostics *Error) override;
136176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
137176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    std::vector<ArgKind> getAcceptedCompletionTypes(
138176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines        llvm::ArrayRef<std::pair<MatcherCtor, unsigned>> Context) override;
139176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
140176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    std::vector<MatcherCompletion>
141176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    getMatcherCompletions(llvm::ArrayRef<ArgKind> AcceptedTypes) override;
142f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  };
143f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
144176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  typedef llvm::StringMap<VariantValue> NamedValueMap;
145f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
146f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  /// \brief Parse a matcher expression.
147f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  ///
148f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  /// \param MatcherCode The matcher expression to parse.
149f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  ///
150f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  /// \param S The Sema instance that will help the parser
151176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ///   construct the matchers. If null, it uses the default registry.
152176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ///
153176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  /// \param NamedValues A map of precomputed named values.  This provides
154176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ///   the dictionary for the <NamedValue> rule of the grammar.
155176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ///   If null, it is ignored.
156176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ///
157b7488d77414b000ce2506b520a6b29f845fb3950Samuel Benzaquen  /// \return The matcher object constructed by the processor, or an empty
158b7488d77414b000ce2506b520a6b29f845fb3950Samuel Benzaquen  ///   Optional if an error occurred. In that case, \c Error will contain a
159f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  ///   description of the error.
160f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  ///   The caller takes ownership of the DynTypedMatcher object returned.
161b7488d77414b000ce2506b520a6b29f845fb3950Samuel Benzaquen  static llvm::Optional<DynTypedMatcher>
162176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  parseMatcherExpression(StringRef MatcherCode, Sema *S,
163176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                         const NamedValueMap *NamedValues,
164176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                         Diagnostics *Error);
165176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  static llvm::Optional<DynTypedMatcher>
166176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  parseMatcherExpression(StringRef MatcherCode, Sema *S,
167176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                         Diagnostics *Error) {
168176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return parseMatcherExpression(MatcherCode, S, nullptr, Error);
169176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
170176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  static llvm::Optional<DynTypedMatcher>
171176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  parseMatcherExpression(StringRef MatcherCode, Diagnostics *Error) {
172176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return parseMatcherExpression(MatcherCode, nullptr, Error);
173176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
174f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
175f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  /// \brief Parse an expression.
176f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  ///
177f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  /// Parses any expression supported by this parser. In general, the
178f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  /// \c parseMatcherExpression function is a better approach to get a matcher
179f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  /// object.
180176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ///
181176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  /// \param S The Sema instance that will help the parser
182176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ///   construct the matchers. If null, it uses the default registry.
183176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ///
184176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  /// \param NamedValues A map of precomputed named values.  This provides
185176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ///   the dictionary for the <NamedValue> rule of the grammar.
186176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ///   If null, it is ignored.
187f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  static bool parseExpression(StringRef Code, Sema *S,
188176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                              const NamedValueMap *NamedValues,
189f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek                              VariantValue *Value, Diagnostics *Error);
190176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  static bool parseExpression(StringRef Code, Sema *S,
191176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                              VariantValue *Value, Diagnostics *Error) {
192176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return parseExpression(Code, S, nullptr, Value, Error);
193176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
194176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  static bool parseExpression(StringRef Code, VariantValue *Value,
195176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                              Diagnostics *Error) {
196176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return parseExpression(Code, nullptr, Value, Error);
197176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
198f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
199651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// \brief Complete an expression at the given offset.
200651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ///
201176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  /// \param S The Sema instance that will help the parser
202176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ///   construct the matchers. If null, it uses the default registry.
203176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ///
204176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  /// \param NamedValues A map of precomputed named values.  This provides
205176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ///   the dictionary for the <NamedValue> rule of the grammar.
206176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ///   If null, it is ignored.
207176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  ///
208651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// \return The list of completions, which may be empty if there are no
209651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  /// available completions or if an error occurred.
210651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  static std::vector<MatcherCompletion>
211176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  completeExpression(StringRef Code, unsigned CompletionOffset, Sema *S,
212176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                     const NamedValueMap *NamedValues);
213176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  static std::vector<MatcherCompletion>
214176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  completeExpression(StringRef Code, unsigned CompletionOffset, Sema *S) {
215176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return completeExpression(Code, CompletionOffset, S, nullptr);
216176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
217176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  static std::vector<MatcherCompletion>
218176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  completeExpression(StringRef Code, unsigned CompletionOffset) {
219176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines    return completeExpression(Code, CompletionOffset, nullptr);
220176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  }
221651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
222f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimekprivate:
223f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  class CodeTokenizer;
224651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  struct ScopedContextEntry;
225f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  struct TokenInfo;
226f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
227f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  Parser(CodeTokenizer *Tokenizer, Sema *S,
228176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines         const NamedValueMap *NamedValues,
229f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek         Diagnostics *Error);
230f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
231f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  bool parseExpressionImpl(VariantValue *Value);
2326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  bool parseMatcherExpressionImpl(const TokenInfo &NameToken,
2336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                  VariantValue *Value);
2346bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  bool parseIdentifierPrefixImpl(VariantValue *Value);
235f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
236176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  void addCompletion(const TokenInfo &CompToken,
237176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines                     const MatcherCompletion &Completion);
238651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void addExpressionCompletions();
239651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
240176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  std::vector<MatcherCompletion>
241176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  getNamedValueCompletions(ArrayRef<ArgKind> AcceptedTypes);
242176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines
243f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  CodeTokenizer *const Tokenizer;
244f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  Sema *const S;
245176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines  const NamedValueMap *const NamedValues;
246f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  Diagnostics *const Error;
247651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
248651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  typedef std::vector<std::pair<MatcherCtor, unsigned> > ContextStackTy;
249651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  ContextStackTy ContextStack;
250651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::vector<MatcherCompletion> Completions;
251f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek};
252f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
253f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek}  // namespace dynamic
254f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek}  // namespace ast_matchers
255f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek}  // namespace clang
256f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
257f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek#endif  // LLVM_CLANG_AST_MATCHERS_DYNAMIC_PARSER_H
258