MLIR  20.0.0git
Query.h
Go to the documentation of this file.
1 //===--- Query.h ------------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef MLIR_TOOLS_MLIRQUERY_QUERY_H
10 #define MLIR_TOOLS_MLIRQUERY_QUERY_H
11 
12 #include "Matcher/VariantValue.h"
13 #include "llvm/ADT/IntrusiveRefCntPtr.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/LineEditor/LineEditor.h"
16 #include <string>
17 
18 namespace mlir::query {
19 
20 enum class QueryKind { Invalid, NoOp, Help, Match, Quit };
21 
22 class QuerySession;
23 
24 struct Query : llvm::RefCountedBase<Query> {
26  virtual ~Query();
27 
28  // Perform the query on qs and print output to os.
29  virtual llvm::LogicalResult run(llvm::raw_ostream &os,
30  QuerySession &qs) const = 0;
31 
32  llvm::StringRef remainingContent;
33  const QueryKind kind;
34 };
35 
36 typedef llvm::IntrusiveRefCntPtr<Query> QueryRef;
37 
38 QueryRef parse(llvm::StringRef line, const QuerySession &qs);
39 
40 std::vector<llvm::LineEditor::Completion>
41 complete(llvm::StringRef line, size_t pos, const QuerySession &qs);
42 
43 // Any query which resulted in a parse error. The error message is in ErrStr.
44 struct InvalidQuery : Query {
45  InvalidQuery(const llvm::Twine &errStr)
46  : Query(QueryKind::Invalid), errStr(errStr.str()) {}
47  llvm::LogicalResult run(llvm::raw_ostream &os,
48  QuerySession &qs) const override;
49 
50  std::string errStr;
51 
52  static bool classof(const Query *query) {
53  return query->kind == QueryKind::Invalid;
54  }
55 };
56 
57 // No-op query (i.e. a blank line).
58 struct NoOpQuery : Query {
60  llvm::LogicalResult run(llvm::raw_ostream &os,
61  QuerySession &qs) const override;
62 
63  static bool classof(const Query *query) {
64  return query->kind == QueryKind::NoOp;
65  }
66 };
67 
68 // Query for "help".
69 struct HelpQuery : Query {
71  llvm::LogicalResult run(llvm::raw_ostream &os,
72  QuerySession &qs) const override;
73 
74  static bool classof(const Query *query) {
75  return query->kind == QueryKind::Help;
76  }
77 };
78 
79 // Query for "quit".
80 struct QuitQuery : Query {
82  llvm::LogicalResult run(llvm::raw_ostream &os,
83  QuerySession &qs) const override;
84 
85  static bool classof(const Query *query) {
86  return query->kind == QueryKind::Quit;
87  }
88 };
89 
90 // Query for "match MATCHER".
91 struct MatchQuery : Query {
92  MatchQuery(llvm::StringRef source, const matcher::DynMatcher &matcher)
94  llvm::LogicalResult run(llvm::raw_ostream &os,
95  QuerySession &qs) const override;
96 
98 
99  llvm::StringRef source;
100 
101  static bool classof(const Query *query) {
102  return query->kind == QueryKind::Match;
103  }
104 };
105 
106 } // namespace mlir::query
107 
108 #endif
FailureOr< Attribute > query(Operation *op, ArrayRef< DataLayoutEntryKey > keys, bool emitError=false)
Perform a DLTI-query at op, recursively querying each key of keys on query interface-implementing att...
Definition: DLTI.cpp:510
QueryRef parse(llvm::StringRef line, const QuerySession &qs)
Definition: Query.cpp:20
llvm::IntrusiveRefCntPtr< Query > QueryRef
Definition: Query.h:36
std::vector< llvm::LineEditor::Completion > complete(llvm::StringRef line, size_t pos, const QuerySession &qs)
Definition: Query.cpp:25
llvm::LogicalResult run(llvm::raw_ostream &os, QuerySession &qs) const override
Definition: Query.cpp:113
static bool classof(const Query *query)
Definition: Query.h:74
static bool classof(const Query *query)
Definition: Query.h:52
InvalidQuery(const llvm::Twine &errStr)
Definition: Query.h:45
llvm::LogicalResult run(llvm::raw_ostream &os, QuerySession &qs) const override
Definition: Query.cpp:104
std::string errStr
Definition: Query.h:50
llvm::LogicalResult run(llvm::raw_ostream &os, QuerySession &qs) const override
Definition: Query.cpp:127
MatchQuery(llvm::StringRef source, const matcher::DynMatcher &matcher)
Definition: Query.h:92
llvm::StringRef source
Definition: Query.h:99
const matcher::DynMatcher matcher
Definition: Query.h:97
static bool classof(const Query *query)
Definition: Query.h:101
llvm::LogicalResult run(llvm::raw_ostream &os, QuerySession &qs) const override
Definition: Query.cpp:109
static bool classof(const Query *query)
Definition: Query.h:63
Query(QueryKind kind)
Definition: Query.h:25
virtual llvm::LogicalResult run(llvm::raw_ostream &os, QuerySession &qs) const =0
llvm::StringRef remainingContent
Definition: Query.h:32
const QueryKind kind
Definition: Query.h:33
llvm::LogicalResult run(llvm::raw_ostream &os, QuerySession &qs) const override
Definition: Query.cpp:122
static bool classof(const Query *query)
Definition: Query.h:85