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> { QueryQuery25 Query(QueryKind kind) : kind(kind) {} 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 { InvalidQueryInvalidQuery45 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 classofInvalidQuery52 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 { NoOpQueryNoOpQuery59 NoOpQuery() : Query(QueryKind::NoOp) {} 60 llvm::LogicalResult run(llvm::raw_ostream &os, 61 QuerySession &qs) const override; 62 classofNoOpQuery63 static bool classof(const Query *query) { 64 return query->kind == QueryKind::NoOp; 65 } 66 }; 67 68 // Query for "help". 69 struct HelpQuery : Query { HelpQueryHelpQuery70 HelpQuery() : Query(QueryKind::Help) {} 71 llvm::LogicalResult run(llvm::raw_ostream &os, 72 QuerySession &qs) const override; 73 classofHelpQuery74 static bool classof(const Query *query) { 75 return query->kind == QueryKind::Help; 76 } 77 }; 78 79 // Query for "quit". 80 struct QuitQuery : Query { QuitQueryQuitQuery81 QuitQuery() : Query(QueryKind::Quit) {} 82 llvm::LogicalResult run(llvm::raw_ostream &os, 83 QuerySession &qs) const override; 84 classofQuitQuery85 static bool classof(const Query *query) { 86 return query->kind == QueryKind::Quit; 87 } 88 }; 89 90 // Query for "match MATCHER". 91 struct MatchQuery : Query { MatchQueryMatchQuery92 MatchQuery(llvm::StringRef source, const matcher::DynMatcher &matcher) 93 : Query(QueryKind::Match), matcher(matcher), source(source) {} 94 llvm::LogicalResult run(llvm::raw_ostream &os, 95 QuerySession &qs) const override; 96 97 const matcher::DynMatcher matcher; 98 99 llvm::StringRef source; 100 classofMatchQuery101 static bool classof(const Query *query) { 102 return query->kind == QueryKind::Match; 103 } 104 }; 105 106 } // namespace mlir::query 107 108 #endif 109