14#include <mlir/IR/Builders.h>
16#include <llvm/ADT/StringExtras.h>
17#include <llvm/ADT/TypeSwitch.h>
27static bool isSetInfoValue(Attribute attr) {
28 return TypeSwitch<Attribute, bool>(attr)
29 .Case<BoolAttr, IntegerAttr, StringAttr, KeywordAttr, SymbolAttr>([](
auto) {
return true; })
30 .Case<ArrayAttr>([](ArrayAttr values) {
31 return llvm::all_of(values, isSetInfoValue);
32 }).Default([](Attribute) {
return false; });
35static void printValue(AsmPrinter &printer, Attribute value) {
36 TypeSwitch<Attribute>(value)
37 .Case<KeywordAttr, SymbolAttr>([&](
auto attr) { printer << attr.getValue(); })
38 .Case<StringAttr, BoolAttr>([&](
auto attr) { printer.printAttribute(attr); })
39 .Case<IntegerAttr>([&](
auto attr) {
41 attr.getValue().toStringSigned(text);
43 }).Case<ArrayAttr>([&](ArrayAttr values) {
45 llvm::interleave(values, [&](Attribute attr) { printValue(printer, attr); }, [&] {
52static ParseResult parseValue(OpAsmParser &parser, Attribute &value) {
53 Builder builder(parser.getContext());
54 if (succeeded(parser.parseOptionalLParen())) {
55 SmallVector<Attribute> values;
56 while (failed(parser.parseOptionalRParen())) {
58 if (parseValue(parser, element)) {
61 values.push_back(element);
63 value = builder.getArrayAttr(values);
66 if (succeeded(parser.parseOptionalColon())) {
68 if (parser.parseKeyword(&keyword)) {
75 if (
auto parsed = parser.parseOptionalInteger(number); parsed.has_value()) {
76 if (failed(*parsed)) {
79 value = IntegerAttr::get(IntegerType::get(parser.getContext(), number.getBitWidth()), number);
83 if (
auto parsed = parser.parseOptionalAttribute(
string, Type()); parsed.has_value()) {
84 if (failed(*parsed)) {
91 if (succeeded(parser.parseOptionalKeyword(&symbol))) {
92 value = symbol ==
"true" || symbol ==
"false"
93 ? Attribute(builder.getBoolAttr(symbol ==
"true"))
97 return parser.emitError(parser.getCurrentLocation()) <<
"expected SMT-LIB set-info value";
103 SMLoc loc = parser.getCurrentLocation();
106 if (parser.parseAttribute(key) || parseValue(parser, value) ||
107 parser.parseOptionalAttrDict(result.attributes)) {
111 return parser.emitError(loc);
112 }, key.getValue()))) {
118 result.addAttribute(
"value", value);
119 result.location = parser.getEncodedSourceLoc(loc);
125 printer.printAttribute(StringAttr::get(getContext(),
getKey().
getValue()));
128 printer.printOptionalAttrDict((*this)->getAttrs(), {
"key",
"value"});
134 "requires an SMT-LIB set-info value built from strings, booleans, integers, SMT keywords, "
135 "SMT symbols, or nested lists"
::mlir::ParseResult parse(::mlir::OpAsmParser &parser, ::mlir::OperationState &result)
::mlir::Attribute getValueAttr()
void print(::mlir::OpAsmPrinter &p)
::llvm::LogicalResult verify()
::mlir::Attribute getValue()
::llzk::smt_info::KeywordAttr getKey()
mlir::Attribute getSymbolAttr(mlir::MLIRContext *context, llvm::StringRef value)
Creates a symbol attribute without exposing generated attribute storage to operation implementation f...
mlir::Attribute getKeywordAttr(mlir::MLIRContext *context, llvm::StringRef value)
Creates a keyword attribute without exposing generated attribute storage to operation implementation ...
mlir::LogicalResult verifyKeyword(llvm::function_ref< mlir::InFlightDiagnostic()> emitError, llvm::StringRef value)
Verifies that value is a valid SMT-LIB keyword and reports failures through emitError.