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 "mlir/Support/LogicalResult.h"
14#include "llvm/ADT/IntrusiveRefCntPtr.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/LineEditor/LineEditor.h"
17#include <string>
18
19namespace mlir::query {
20
21enum class QueryKind { Invalid, NoOp, Help, Match, Quit };
22
23class QuerySession;
24
25struct Query : llvm::RefCountedBase<Query> {
26 Query(QueryKind kind) : kind(kind) {}
27 virtual ~Query();
28
29 // Perform the query on qs and print output to os.
30 virtual mlir::LogicalResult run(llvm::raw_ostream &os,
31 QuerySession &qs) const = 0;
32
33 llvm::StringRef remainingContent;
34 const QueryKind kind;
35};
36
37typedef llvm::IntrusiveRefCntPtr<Query> QueryRef;
38
39QueryRef parse(llvm::StringRef line, const QuerySession &qs);
40
41std::vector<llvm::LineEditor::Completion>
42complete(llvm::StringRef line, size_t pos, const QuerySession &qs);
43
44// Any query which resulted in a parse error. The error message is in ErrStr.
45struct InvalidQuery : Query {
46 InvalidQuery(const llvm::Twine &errStr)
47 : Query(QueryKind::Invalid), errStr(errStr.str()) {}
48 mlir::LogicalResult run(llvm::raw_ostream &os,
49 QuerySession &qs) const override;
50
51 std::string errStr;
52
53 static bool classof(const Query *query) {
54 return query->kind == QueryKind::Invalid;
55 }
56};
57
58// No-op query (i.e. a blank line).
59struct NoOpQuery : Query {
60 NoOpQuery() : Query(QueryKind::NoOp) {}
61 mlir::LogicalResult run(llvm::raw_ostream &os,
62 QuerySession &qs) const override;
63
64 static bool classof(const Query *query) {
65 return query->kind == QueryKind::NoOp;
66 }
67};
68
69// Query for "help".
70struct HelpQuery : Query {
71 HelpQuery() : Query(QueryKind::Help) {}
72 mlir::LogicalResult run(llvm::raw_ostream &os,
73 QuerySession &qs) const override;
74
75 static bool classof(const Query *query) {
76 return query->kind == QueryKind::Help;
77 }
78};
79
80// Query for "quit".
81struct QuitQuery : Query {
82 QuitQuery() : Query(QueryKind::Quit) {}
83 mlir::LogicalResult run(llvm::raw_ostream &os,
84 QuerySession &qs) const override;
85
86 static bool classof(const Query *query) {
87 return query->kind == QueryKind::Quit;
88 }
89};
90
91// Query for "match MATCHER".
92struct MatchQuery : Query {
93 MatchQuery(llvm::StringRef source, const matcher::DynMatcher &matcher)
94 : Query(QueryKind::Match), matcher(matcher), source(source) {}
95 mlir::LogicalResult run(llvm::raw_ostream &os,
96 QuerySession &qs) const override;
97
98 const matcher::DynMatcher matcher;
99
100 llvm::StringRef source;
101
102 static bool classof(const Query *query) {
103 return query->kind == QueryKind::Match;
104 }
105};
106
107} // namespace mlir::query
108
109#endif
110

source code of mlir/include/mlir/Query/Query.h