diff --git a/flang/include/flang/Optimizer/Dialect/FIROps.td b/flang/include/flang/Optimizer/Dialect/FIROps.td --- a/flang/include/flang/Optimizer/Dialect/FIROps.td +++ b/flang/include/flang/Optimizer/Dialect/FIROps.td @@ -1827,66 +1827,15 @@ let arguments = (ins StrAttr:$field_id, TypeAttr:$on_type, - Variadic:$lenparams + Variadic:$typeparams ); - let parser = [{ - llvm::StringRef fieldName; - auto &builder = parser.getBuilder(); - mlir::Type recty; - if (parser.parseOptionalKeyword(&fieldName) || - parser.parseComma() || - parser.parseType(recty)) - return mlir::failure(); - result.addAttribute(fieldAttrName(), builder.getStringAttr(fieldName)); - if (!recty.dyn_cast()) - return mlir::failure(); - result.addAttribute(typeAttrName(), mlir::TypeAttr::get(recty)); - if (!parser.parseOptionalLParen()) { - llvm::SmallVector operands; - llvm::SmallVector types; - auto loc = parser.getNameLoc(); - if (parser.parseOperandList(operands, - mlir::OpAsmParser::Delimiter::None) || - parser.parseColonTypeList(types) || - parser.parseRParen() || - parser.resolveOperands(operands, types, loc, result.operands)) - return mlir::failure(); - } - mlir::Type fieldType = fir::FieldType::get(builder.getContext()); - if (parser.addTypeToList(fieldType, result.types)) - return mlir::failure(); - return mlir::success(); - }]; + let parser = "return parseFieldIndexOp(parser, result);"; - let printer = [{ - p << ' ' - << (*this)->getAttrOfType(fieldAttrName()).getValue() - << ", " << (*this)->getAttr(typeAttrName()); - if (getNumOperands()) { - p << '('; - p.printOperands(lenparams()); - auto sep = ") : "; - for (auto op : lenparams()) { - p << sep; - if (op) - p.printType(op.getType()); - else - p << "()"; - sep = ", "; - } - } - }]; + let printer = "::print(p, *this);"; let builders = [OpBuilder<(ins "llvm::StringRef":$fieldName, - "mlir::Type":$recTy, CArg<"mlir::ValueRange","{}">:$operands), - [{ - $_state.addAttribute(fieldAttrName(), - $_builder.getStringAttr(fieldName)); - $_state.addAttribute(typeAttrName(), TypeAttr::get(recTy)); - $_state.addOperands(operands); - }] - >]; + "mlir::Type":$recTy, CArg<"mlir::ValueRange","{}">:$operands)>]; let extraClassDeclaration = [{ static constexpr llvm::StringRef fieldAttrName() { return "field_id"; } diff --git a/flang/lib/Optimizer/Dialect/FIROps.cpp b/flang/lib/Optimizer/Dialect/FIROps.cpp --- a/flang/lib/Optimizer/Dialect/FIROps.cpp +++ b/flang/lib/Optimizer/Dialect/FIROps.cpp @@ -1225,6 +1225,69 @@ build(builder, result, resTy, aggVal, aa); } +//===----------------------------------------------------------------------===// +// FieldIndexOp +//===----------------------------------------------------------------------===// + +static mlir::ParseResult parseFieldIndexOp(mlir::OpAsmParser &parser, + mlir::OperationState &result) { + llvm::StringRef fieldName; + auto &builder = parser.getBuilder(); + mlir::Type recty; + if (parser.parseOptionalKeyword(&fieldName) || parser.parseComma() || + parser.parseType(recty)) + return mlir::failure(); + result.addAttribute(fir::FieldIndexOp::fieldAttrName(), + builder.getStringAttr(fieldName)); + if (!recty.dyn_cast()) + return mlir::failure(); + result.addAttribute(fir::FieldIndexOp::typeAttrName(), + mlir::TypeAttr::get(recty)); + if (!parser.parseOptionalLParen()) { + llvm::SmallVector operands; + llvm::SmallVector types; + auto loc = parser.getNameLoc(); + if (parser.parseOperandList(operands, mlir::OpAsmParser::Delimiter::None) || + parser.parseColonTypeList(types) || parser.parseRParen() || + parser.resolveOperands(operands, types, loc, result.operands)) + return mlir::failure(); + } + mlir::Type fieldType = fir::FieldType::get(builder.getContext()); + if (parser.addTypeToList(fieldType, result.types)) + return mlir::failure(); + return mlir::success(); +} + +static void print(mlir::OpAsmPrinter &p, fir::FieldIndexOp &op) { + p << ' ' + << op.getOperation() + ->getAttrOfType(fir::FieldIndexOp::fieldAttrName()) + .getValue() + << ", " << op.getOperation()->getAttr(fir::FieldIndexOp::typeAttrName()); + if (op.getNumOperands()) { + p << '('; + p.printOperands(op.typeparams()); + const auto *sep = ") : "; + for (auto op : op.typeparams()) { + p << sep; + if (op) + p.printType(op.getType()); + else + p << "()"; + sep = ", "; + } + } +} + +void fir::FieldIndexOp::build(mlir::OpBuilder &builder, + mlir::OperationState &result, + llvm::StringRef fieldName, mlir::Type recTy, + mlir::ValueRange operands) { + result.addAttribute(fieldAttrName(), builder.getStringAttr(fieldName)); + result.addAttribute(typeAttrName(), TypeAttr::get(recTy)); + result.addOperands(operands); +} + //===----------------------------------------------------------------------===// // InsertOnRangeOp //===----------------------------------------------------------------------===//