1//===- AttrOrTypeFormatGen.cpp - MLIR attribute and type format generator -===//
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#include "AttrOrTypeFormatGen.h"
10#include "FormatGen.h"
11#include "mlir/Support/LLVM.h"
12#include "mlir/TableGen/AttrOrTypeDef.h"
13#include "mlir/TableGen/EnumInfo.h"
14#include "mlir/TableGen/Format.h"
15#include "mlir/TableGen/GenInfo.h"
16#include "llvm/ADT/BitVector.h"
17#include "llvm/ADT/SmallVectorExtras.h"
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/ADT/StringSwitch.h"
20#include "llvm/ADT/TypeSwitch.h"
21#include "llvm/Support/MemoryBuffer.h"
22#include "llvm/Support/SaveAndRestore.h"
23#include "llvm/Support/SourceMgr.h"
24#include "llvm/TableGen/Error.h"
25#include "llvm/TableGen/TableGenBackend.h"
26
27using namespace mlir;
28using namespace mlir::tblgen;
29
30using llvm::formatv;
31
32//===----------------------------------------------------------------------===//
33// Element
34//===----------------------------------------------------------------------===//
35
36namespace {
37/// This class represents an instance of a variable element. A variable refers
38/// to an attribute or type parameter.
39class ParameterElement
40 : public VariableElementBase<VariableElement::Parameter> {
41public:
42 ParameterElement(AttrOrTypeParameter param) : param(param) {}
43
44 /// Get the parameter in the element.
45 const AttrOrTypeParameter &getParam() const { return param; }
46
47 /// Indicate if this variable is printed "qualified" (that is it is
48 /// prefixed with the `#dialect.mnemonic`).
49 bool shouldBeQualified() { return shouldBeQualifiedFlag; }
50 void setShouldBeQualified(bool qualified = true) {
51 shouldBeQualifiedFlag = qualified;
52 }
53
54 /// Returns true if the element contains an optional parameter.
55 bool isOptional() const { return param.isOptional(); }
56
57 /// Returns the name of the parameter.
58 StringRef getName() const { return param.getName(); }
59
60 /// Return the code to check whether the parameter is present.
61 auto genIsPresent(FmtContext &ctx, const Twine &self) const {
62 assert(isOptional() && "cannot guard on a mandatory parameter");
63 std::string valueStr = tgfmt(fmt: *param.getDefaultValue(), ctx: &ctx).str();
64 ctx.addSubst(placeholder: "_lhs", subst: self).addSubst(placeholder: "_rhs", subst: valueStr);
65 return tgfmt(fmt: getParam().getComparator(), ctx: &ctx);
66 }
67
68 /// Generate the code to check whether the parameter should be printed.
69 MethodBody &genPrintGuard(FmtContext &ctx, MethodBody &os) const {
70 assert(isOptional() && "cannot guard on a mandatory parameter");
71 std::string self = param.getAccessorName() + "()";
72 return os << "!(" << genIsPresent(ctx, self) << ")";
73 }
74
75private:
76 bool shouldBeQualifiedFlag = false;
77 AttrOrTypeParameter param;
78};
79
80/// Utility to return the encapsulated parameter element for the provided format
81/// element. This parameter can originate from either a `ParameterElement`,
82/// `CustomDirective` with a single parameter argument or `RefDirective`.
83static ParameterElement *getEncapsulatedParameterElement(FormatElement *el) {
84 return TypeSwitch<FormatElement *, ParameterElement *>(el)
85 .Case(caseFn: [&](CustomDirective *custom) {
86 FailureOr<ParameterElement *> maybeParam =
87 custom->template getFrontAs<ParameterElement>();
88 return *maybeParam;
89 })
90 .Case(caseFn: [&](ParameterElement *param) { return param; })
91 .Case(caseFn: [&](RefDirective *ref) {
92 return cast<ParameterElement>(Val: ref->getArg());
93 })
94 .DefaultUnreachable(message: "unexpected struct element type");
95}
96
97/// Returns true if the parameter is an `ArrayRefParameter` or
98/// `OptionalArrayRefParameter` without a custom printer or parser. Such
99/// parameters use a comma-separated list as their default format, which is
100/// ambiguous when used in a `struct` directive followed by other parameters.
101static bool isUndelimitedArrayRefParam(const ParameterElement *el) {
102 // If the parameter has a custom printer or parser, the user controls the
103 // format and printer/parser symmetry is their responsibility.
104 if (el->getParam().getPrinter() || el->getParam().getParser())
105 return false;
106 const auto *defInit = dyn_cast<llvm::DefInit>(Val: el->getParam().getDef());
107 if (!defInit)
108 return false;
109 return defInit->getDef()->isSubClassOf(Name: "ArrayRefParameter") ||
110 defInit->getDef()->isSubClassOf(Name: "OptionalArrayRefParameter");
111}
112
113/// If the parameter wraps an EnumAttr, return its definition.
114static const llvm::Record *getEnumAttrDef(const ParameterElement *el) {
115 const auto *defInit = dyn_cast<llvm::DefInit>(Val: el->getParam().getDef());
116 if (!defInit || !defInit->getDef()->isSubClassOf(Name: "EnumAttrParameter"))
117 return nullptr;
118 return defInit->getDef()->getValueAsDef(FieldName: "attr");
119}
120
121/// Returns true if the parameter wraps an unquoted, comma-separated bit enum.
122/// Its underlying parser may consume the comma that separates struct entries.
123static bool isUndelimitedCommaBitEnumParam(const ParameterElement *el) {
124 const llvm::Record *enumAttr = getEnumAttrDef(el);
125 if (!enumAttr)
126 return false;
127 EnumInfo enumInfo(enumAttr->getValueAsDef(FieldName: "enum"));
128 return enumInfo.isNewStyleBitEnum() &&
129 enumInfo.getBitEnumSeparatorKind() ==
130 EnumInfo::BitEnumSeparatorKind::Comma;
131}
132
133/// Returns true if a non-final struct parameter needs brackets to separate its
134/// value from the following key-value pair.
135static bool needsStructBrackets(const ParameterElement *el) {
136 return isUndelimitedArrayRefParam(el) || isUndelimitedCommaBitEnumParam(el);
137}
138
139/// Shorthand functions that can be used with ranged-based conditions.
140static bool paramIsOptional(ParameterElement *el) { return el->isOptional(); }
141static bool formatIsOptional(FormatElement *el) {
142 ParameterElement *param = getEncapsulatedParameterElement(el);
143 return param != nullptr && param->isOptional();
144}
145static bool paramNotOptional(ParameterElement *el) { return !el->isOptional(); }
146static bool formatNotOptional(FormatElement *el) {
147 return !formatIsOptional(el);
148}
149
150/// This class represents a `params` directive that refers to all parameters
151/// of an attribute or type. When used as a top-level directive, it generates
152/// a format of the form:
153///
154/// (param-value (`,` param-value)*)?
155///
156/// When used as an argument to another directive that accepts variables,
157/// `params` can be used in place of manually listing all parameters of an
158/// attribute or type.
159class ParamsDirective
160 : public VectorDirectiveBase<DirectiveElement::Params, ParameterElement *> {
161public:
162 using Base::Base;
163
164 /// Returns true if there are optional parameters present.
165 bool hasOptionalElements() const {
166 return llvm::any_of(Range: getElements(), P: paramIsOptional);
167 }
168};
169
170/// This class represents a `struct` directive that generates a struct format
171/// of the form:
172///
173/// `{` param-name `=` param-value (`,` param-name `=` param-value)* `}`
174///
175class StructDirective
176 : public VectorDirectiveBase<DirectiveElement::Struct, FormatElement *> {
177public:
178 using Base::Base;
179
180 /// Returns true if there are optional format elements present.
181 bool hasOptionalElements() const {
182 return llvm::any_of(Range: getElements(), P: formatIsOptional);
183 }
184};
185
186} // namespace
187
188//===----------------------------------------------------------------------===//
189// Format Strings
190//===----------------------------------------------------------------------===//
191
192/// Default parser for attribute or type parameters.
193static const char *const defaultParameterParser =
194 "::mlir::FieldParser<$0>::parse($_parser)";
195
196/// Default printer for attribute or type parameters.
197static const char *const defaultParameterPrinter =
198 "$_printer.printStrippedAttrOrType($_self)";
199
200/// Qualified printer for attribute or type parameters: it does not elide
201/// dialect and mnemonic.
202static const char *const qualifiedParameterPrinter = "$_printer << $_self";
203
204/// Print an error when failing to parse an element.
205///
206/// $0: The parameter C++ class name.
207static const char *const parserErrorStr =
208 "$_parser.emitError($_parser.getCurrentLocation(), ";
209
210/// Code format to parse a variable. Separate by lines because variable parsers
211/// may be generated inside other directives, which requires indentation.
212///
213/// {0}: The parameter name.
214/// {1}: The parse code for the parameter.
215/// {2}: Code template for printing an error.
216/// {3}: Name of the attribute or type.
217/// {4}: C++ class of the parameter.
218/// {5}: Optional code to preload the dialect for this variable.
219static const char *const variableParser = R"(
220// Parse variable '{0}'{5}
221_result_{0} = {1};
222if (::mlir::failed(_result_{0})) {{
223 {2}"failed to parse {3} parameter '{0}' which is to be a `{4}`");
224 return {{};
225}
226)";
227
228//===----------------------------------------------------------------------===//
229// DefFormat
230//===----------------------------------------------------------------------===//
231
232namespace {
233class DefFormat {
234public:
235 DefFormat(const AttrOrTypeDef &def, std::vector<FormatElement *> &&elements)
236 : def(def), elements(std::move(elements)) {}
237
238 /// Generate the attribute or type parser.
239 void genParser(MethodBody &os);
240 /// Generate the attribute or type printer.
241 void genPrinter(MethodBody &os);
242
243private:
244 /// Generate the parser code for a specific format element.
245 void genElementParser(FormatElement *el, FmtContext &ctx, MethodBody &os);
246 /// Generate the parser code for a literal.
247 void genLiteralParser(StringRef value, FmtContext &ctx, MethodBody &os,
248 bool isOptional = false);
249 /// Generate the parser code for a variable.
250 void
251 genVariableParser(ParameterElement *el, FmtContext &ctx, MethodBody &os,
252 std::optional<StringRef> parserOverride = std::nullopt);
253 /// Generate a stripped parser for an EnumAttr parameter in a `struct`.
254 void genStructParameterParser(ParameterElement *el, FmtContext &ctx,
255 MethodBody &os);
256 /// Generate the parser code for a `params` directive.
257 void genParamsParser(ParamsDirective *el, FmtContext &ctx, MethodBody &os);
258 /// Generate the parser code for a `struct` directive.
259 void genStructParser(StructDirective *el, FmtContext &ctx, MethodBody &os);
260 /// Generate the parser code for a `custom` directive.
261 void genCustomParser(CustomDirective *el, FmtContext &ctx, MethodBody &os,
262 bool isOptional = false);
263 /// Generate the parser code for an optional group.
264 void genOptionalGroupParser(OptionalElement *el, FmtContext &ctx,
265 MethodBody &os);
266
267 /// Generate the printer code for a specific format element.
268 void genElementPrinter(FormatElement *el, FmtContext &ctx, MethodBody &os);
269 /// Generate the printer code for a literal.
270 void genLiteralPrinter(StringRef value, FmtContext &ctx, MethodBody &os);
271 /// Generate the printer code for a variable.
272 void
273 genVariablePrinter(ParameterElement *el, FmtContext &ctx, MethodBody &os,
274 bool skipGuard = false,
275 std::optional<StringRef> printerOverride = std::nullopt,
276 std::optional<StringRef> selfOverride = std::nullopt);
277 /// Generate a stripped printer for an EnumAttr parameter in a `struct`.
278 void genStructParameterPrinter(ParameterElement *el, FmtContext &ctx,
279 MethodBody &os);
280 /// Generate a printer for comma-separated format elements.
281 void genCommaSeparatedPrinter(
282 ArrayRef<FormatElement *> params, FmtContext &ctx, MethodBody &os,
283 function_ref<void(FormatElement *)> extra, bool stripEnumAttrs = false,
284 function_ref<void(FormatElement *)> extraPost = nullptr);
285 /// Generate the printer code for a `params` directive.
286 void genParamsPrinter(ParamsDirective *el, FmtContext &ctx, MethodBody &os);
287 /// Generate the printer code for a `struct` directive.
288 void genStructPrinter(StructDirective *el, FmtContext &ctx, MethodBody &os);
289 /// Generate the printer code for a `custom` directive.
290 void genCustomPrinter(CustomDirective *el, FmtContext &ctx, MethodBody &os);
291 /// Generate the printer code for an optional group.
292 void genOptionalGroupPrinter(OptionalElement *el, FmtContext &ctx,
293 MethodBody &os);
294 /// Generate a printer (or space eraser) for a whitespace element.
295 void genWhitespacePrinter(WhitespaceElement *el, FmtContext &ctx,
296 MethodBody &os);
297
298 /// The ODS definition of the attribute or type whose format is being used to
299 /// generate a parser and printer.
300 const AttrOrTypeDef &def;
301 /// The list of top-level format elements returned by the assembly format
302 /// parser.
303 std::vector<FormatElement *> elements;
304
305 /// Flags for printing spaces.
306 bool shouldEmitSpace = false;
307 bool lastWasPunctuation = false;
308};
309} // namespace
310
311//===----------------------------------------------------------------------===//
312// ParserGen
313//===----------------------------------------------------------------------===//
314
315/// Generate a special-case "parser" for an attribute's self type parameter. The
316/// self type parameter has special handling in the assembly format in that it
317/// is derived from the optional trailing colon type after the attribute.
318static void genAttrSelfTypeParser(MethodBody &os, const FmtContext &ctx,
319 const AttributeSelfTypeParameter &param) {
320 // "Parser" for an attribute self type parameter that checks the
321 // optionally-parsed trailing colon type.
322 //
323 // $0: The C++ storage class of the type parameter.
324 // $1: The self type parameter name.
325 const char *const selfTypeParser = R"(
326if ($_type) {
327 if (auto reqType = ::llvm::dyn_cast<$0>($_type)) {
328 _result_$1 = reqType;
329 } else {
330 $_parser.emitError($_loc, "invalid kind of type specified");
331 return {};
332 }
333})";
334
335 // If the attribute self type parameter is required, emit code that emits an
336 // error if the trailing type was not parsed.
337 const char *const selfTypeRequired = R"( else {
338 $_parser.emitError($_loc, "expected a trailing type");
339 return {};
340})";
341
342 os << tgfmt(fmt: selfTypeParser, ctx: &ctx, vals: param.getCppStorageType(), vals: param.getName());
343 if (!param.isOptional())
344 os << tgfmt(fmt: selfTypeRequired, ctx: &ctx);
345 os << "\n";
346}
347
348void DefFormat::genParser(MethodBody &os) {
349 FmtContext ctx;
350 ctx.addSubst(placeholder: "_parser", subst: "odsParser");
351 ctx.addSubst(placeholder: "_ctxt", subst: "odsParser.getContext()");
352 ctx.withBuilder(subst: "odsBuilder");
353 if (isa<AttrDef>(Val: def))
354 ctx.addSubst(placeholder: "_type", subst: "odsType");
355 os.indent();
356 os << "::mlir::Builder odsBuilder(odsParser.getContext());\n";
357
358 // Store the initial location of the parser.
359 ctx.addSubst(placeholder: "_loc", subst: "odsLoc");
360 os << tgfmt(fmt: "::llvm::SMLoc $_loc = $_parser.getCurrentLocation();\n"
361 "(void) $_loc;\n",
362 ctx: &ctx);
363
364 // Declare variables to store all of the parameters. Allocated parameters
365 // such as `ArrayRef` and `StringRef` must provide a `storageType`. Store
366 // FailureOr<T> to defer type construction for parameters that are parsed in
367 // a loop (parsers return FailureOr anyways).
368 ArrayRef<AttrOrTypeParameter> params = def.getParameters();
369 for (const AttrOrTypeParameter &param : params) {
370 os << formatv(Fmt: "::mlir::FailureOr<{0}> _result_{1};\n",
371 Vals: param.getCppStorageType(), Vals: param.getName());
372 if (auto *selfTypeParam = dyn_cast<AttributeSelfTypeParameter>(Val: &param))
373 genAttrSelfTypeParser(os, ctx, param: *selfTypeParam);
374 }
375
376 // Generate call to each parameter parser.
377 for (FormatElement *el : elements)
378 genElementParser(el, ctx, os);
379
380 // Emit an assert for each mandatory parameter. Triggering an assert means
381 // the generated parser is incorrect (i.e. there is a bug in this code).
382 for (const AttrOrTypeParameter &param : params) {
383 if (param.isOptional())
384 continue;
385 os << formatv(Fmt: "assert(::mlir::succeeded(_result_{0}));\n", Vals: param.getName());
386 }
387
388 // Generate call to the attribute or type builder. Use the checked getter
389 // if one was generated.
390 if (def.genVerifyDecl() || def.genVerifyInvariantsImpl()) {
391 os << tgfmt(fmt: "return $_parser.getChecked<$0>($_loc, $_parser.getContext()",
392 ctx: &ctx, vals: def.getCppClassName());
393 } else {
394 os << tgfmt(fmt: "return $0::get($_parser.getContext()", ctx: &ctx,
395 vals: def.getCppClassName());
396 }
397 for (const AttrOrTypeParameter &param : params) {
398 os << ",\n ";
399 std::string paramSelfStr;
400 llvm::raw_string_ostream selfOs(paramSelfStr);
401 if (std::optional<StringRef> defaultValue = param.getDefaultValue()) {
402 selfOs << formatv(Fmt: "(_result_{0}.value_or(", Vals: param.getName())
403 << tgfmt(fmt: *defaultValue, ctx: &ctx) << "))";
404 } else {
405 selfOs << formatv(Fmt: "(*_result_{0})", Vals: param.getName());
406 }
407 ctx.addSubst(placeholder: param.getName(), subst: selfOs.str());
408 os << param.getCppType() << "("
409 << tgfmt(fmt: param.getConvertFromStorage(), ctx: &ctx.withSelf(subst: selfOs.str()))
410 << ")";
411 }
412 os << ");";
413}
414
415void DefFormat::genElementParser(FormatElement *el, FmtContext &ctx,
416 MethodBody &os) {
417 if (auto *literal = dyn_cast<LiteralElement>(Val: el))
418 return genLiteralParser(value: literal->getSpelling(), ctx, os);
419 if (auto *var = dyn_cast<ParameterElement>(Val: el))
420 return genVariableParser(el: var, ctx, os);
421 if (auto *params = dyn_cast<ParamsDirective>(Val: el))
422 return genParamsParser(el: params, ctx, os);
423 if (auto *strct = dyn_cast<StructDirective>(Val: el))
424 return genStructParser(el: strct, ctx, os);
425 if (auto *custom = dyn_cast<CustomDirective>(Val: el))
426 return genCustomParser(el: custom, ctx, os);
427 if (auto *optional = dyn_cast<OptionalElement>(Val: el))
428 return genOptionalGroupParser(el: optional, ctx, os);
429 if (isa<WhitespaceElement>(Val: el))
430 return;
431
432 llvm_unreachable("unknown format element");
433}
434
435void DefFormat::genLiteralParser(StringRef value, FmtContext &ctx,
436 MethodBody &os, bool isOptional) {
437 os << "// Parse literal '" << value << "'\n";
438 os << tgfmt(fmt: "if ($_parser.parse", ctx: &ctx);
439 if (isOptional)
440 os << "Optional";
441 if (value.front() == '_' || isalpha(value.front())) {
442 os << "Keyword(\"" << value << "\")";
443 } else {
444 os << StringSwitch<StringRef>(value)
445 .Case(S: "->", Value: "Arrow")
446 .Case(S: ":", Value: "Colon")
447 .Case(S: ",", Value: "Comma")
448 .Case(S: "=", Value: "Equal")
449 .Case(S: "<", Value: "Less")
450 .Case(S: ">", Value: "Greater")
451 .Case(S: "{", Value: "LBrace")
452 .Case(S: "}", Value: "RBrace")
453 .Case(S: "(", Value: "LParen")
454 .Case(S: ")", Value: "RParen")
455 .Case(S: "[", Value: "LSquare")
456 .Case(S: "]", Value: "RSquare")
457 .Case(S: "?", Value: "Question")
458 .Case(S: "+", Value: "Plus")
459 .Case(S: "-", Value: "Minus")
460 .Case(S: "*", Value: "Star")
461 .Case(S: "...", Value: "Ellipsis")
462 << "()";
463 }
464 if (isOptional) {
465 // Leave the `if` unclosed to guard optional groups.
466 return;
467 }
468 // Parser will emit an error
469 os << ") return {};\n";
470}
471
472void DefFormat::genVariableParser(ParameterElement *el, FmtContext &ctx,
473 MethodBody &os,
474 std::optional<StringRef> parserOverride) {
475 // Check for a custom parser. Use the default attribute parser otherwise.
476 const AttrOrTypeParameter &param = el->getParam();
477 auto customParser = parserOverride ? parserOverride : param.getParser();
478 auto parser =
479 customParser ? *customParser : StringRef(defaultParameterParser);
480
481 // If the variable points to a dialect specific entity (type of attribute),
482 // we force load the dialect now before trying to parse it.
483 std::string dialectLoading;
484 if (auto *defInit = dyn_cast<llvm::DefInit>(Val: param.getDef())) {
485 auto *dialectValue = defInit->getDef()->getValue(Name: "dialect");
486 if (dialectValue) {
487 if (auto *dialectInit =
488 dyn_cast<llvm::DefInit>(Val: dialectValue->getValue())) {
489 Dialect dialect(dialectInit->getDef());
490 auto cppNamespace = dialect.getCppNamespace();
491 std::string name = dialect.getCppClassName();
492 if (name != "BuiltinDialect" || cppNamespace != "::mlir") {
493 dialectLoading = ("\nodsParser.getContext()->getOrLoadDialect<" +
494 cppNamespace + "::" + name + ">();")
495 .str();
496 }
497 }
498 }
499 }
500 os << formatv(Fmt: variableParser, Vals: param.getName(),
501 Vals: tgfmt(fmt: parser, ctx: &ctx, vals: param.getCppStorageType()),
502 Vals: tgfmt(fmt: parserErrorStr, ctx: &ctx), Vals: def.getName(), Vals: param.getCppType(),
503 Vals&: dialectLoading);
504}
505
506void DefFormat::genStructParameterParser(ParameterElement *el, FmtContext &ctx,
507 MethodBody &os) {
508 const llvm::Record *enumAttr = getEnumAttrDef(el);
509 if (!enumAttr)
510 return genVariableParser(el, ctx, os);
511
512 const AttrOrTypeParameter &param = el->getParam();
513 EnumInfo enumInfo(enumAttr->getValueAsDef(FieldName: "enum"));
514 std::string enumType =
515 (enumInfo.getCppNamespace() + "::" + enumInfo.getEnumClassName()).str();
516
517 std::string parser;
518 llvm::raw_string_ostream parserOS(parser);
519 parserOS << "[&]() -> ::mlir::FailureOr<" << param.getCppStorageType()
520 << "> {\n auto odsEnumValue = "
521 << tgfmt(fmt: defaultParameterParser, ctx: &ctx, vals&: enumType)
522 << ";\n if (::mlir::failed(odsEnumValue))\n"
523 " return ::mlir::failure();\n return "
524 << param.getCppStorageType() << "::get(" << tgfmt(fmt: "$_ctxt", ctx: &ctx)
525 << ", *odsEnumValue);\n}()";
526 genVariableParser(el, ctx, os, parserOverride: parserOS.str());
527}
528
529void DefFormat::genParamsParser(ParamsDirective *el, FmtContext &ctx,
530 MethodBody &os) {
531 os << "// Parse parameter list\n";
532
533 // If there are optional parameters, we need to switch to `parseOptionalComma`
534 // if there are no more required parameters after a certain point.
535 bool hasOptional = el->hasOptionalElements();
536 if (hasOptional) {
537 // Wrap everything in a do-while so that we can `break`.
538 os << "do {\n";
539 os.indent();
540 }
541
542 ArrayRef<ParameterElement *> params = el->getElements();
543 using IteratorT = ParameterElement *const *;
544 IteratorT it = params.begin();
545
546 // Find the last required parameter. Commas become optional aftewards.
547 // Note: IteratorT's copy assignment is deleted.
548 ParameterElement *lastReq = nullptr;
549 for (ParameterElement *param : params)
550 if (!param->isOptional())
551 lastReq = param;
552 IteratorT lastReqIt = lastReq ? llvm::find(Range&: params, Val: lastReq) : params.begin();
553
554 auto eachFn = [&](ParameterElement *el) { genVariableParser(el, ctx, os); };
555 auto betweenFn = [&](IteratorT it) {
556 ParameterElement *el = *std::prev(x: it);
557 // Parse a comma if the last optional parameter had a value.
558 if (el->isOptional()) {
559 os << formatv(Fmt: "if (::mlir::succeeded(_result_{0}) && !({1})) {{\n",
560 Vals: el->getName(),
561 Vals: el->genIsPresent(ctx, self: "(*_result_" + el->getName() + ")"));
562 os.indent();
563 }
564 if (it <= lastReqIt) {
565 genLiteralParser(value: ",", ctx, os);
566 } else {
567 genLiteralParser(value: ",", ctx, os, /*isOptional=*/true);
568 os << ") break;\n";
569 }
570 if (el->isOptional())
571 os.unindent() << "}\n";
572 };
573
574 // llvm::interleave
575 if (it != params.end()) {
576 eachFn(*it++);
577 for (IteratorT e = params.end(); it != e; ++it) {
578 betweenFn(it);
579 eachFn(*it);
580 }
581 }
582
583 if (hasOptional)
584 os.unindent() << "} while(false);\n";
585}
586
587void DefFormat::genStructParser(StructDirective *el, FmtContext &ctx,
588 MethodBody &os) {
589 // Loop declaration for struct parser with only required parameters.
590 //
591 // $0: Number of expected parameters.
592 const char *const loopHeader = R"(
593 for (unsigned odsStructIndex = 0; odsStructIndex < $0; ++odsStructIndex) {
594)";
595
596 // Loop body start for struct parser.
597 const char *const loopStart = R"(
598 ::llvm::StringRef _paramKey;
599 if ($_parser.parseKeyword(&_paramKey)) {
600 $_parser.emitError($_parser.getCurrentLocation(),
601 "expected a parameter name in struct");
602 return {};
603 }
604 if (!_loop_body(_paramKey)) return {};
605)";
606
607 // Struct parser loop end. Check for duplicate or unknown struct parameters.
608 //
609 // {0}: Code template for printing an error.
610 const char *const loopEnd = R"({{
611 {0}"duplicate or unknown struct parameter name: ") << _paramKey;
612 return {{};
613}
614)";
615
616 // Struct parser loop terminator. Parse a comma except on the last element.
617 //
618 // {0}: Number of elements in the struct.
619 const char *const loopTerminator = R"(
620 if ((odsStructIndex != {0} - 1) && odsParser.parseComma())
621 return {{};
622}
623)";
624
625 // Check that a mandatory parameter was parse.
626 //
627 // {0}: Name of the parameter.
628 const char *const checkParam = R"(
629 if (!_seen_{0}) {
630 {1}"struct is missing required parameter: ") << "{0}";
631 return {{};
632 }
633)";
634
635 // First iteration of the loop parsing an optional struct.
636 const char *const optionalStructFirst = R"(
637 ::llvm::StringRef _paramKey;
638 if (!$_parser.parseOptionalKeyword(&_paramKey)) {
639 if (!_loop_body(_paramKey)) return {};
640 while (!$_parser.parseOptionalComma()) {
641)";
642
643 const char *const checkParamKey = R"(
644 if (!_seen_{0} && _paramKey == "{0}") {
645 _seen_{0} = true;
646)";
647
648 os << "// Parse parameter struct\n";
649
650 // Declare a "seen" variable for each key.
651 for (FormatElement *arg : el->getElements()) {
652 ParameterElement *param = getEncapsulatedParameterElement(el: arg);
653 os << formatv(Fmt: "bool _seen_{0} = false;\n", Vals: param->getName());
654 }
655
656 // Generate the body of the parsing loop inside a lambda.
657 os << "{\n";
658 os.indent()
659 << "const auto _loop_body = [&](::llvm::StringRef _paramKey) -> bool {\n";
660 genLiteralParser(value: "=", ctx, os&: os.indent());
661 ArrayRef<FormatElement *> structElems = el->getElements();
662 for (auto [idx, arg] : llvm::enumerate(First&: structElems)) {
663 ParameterElement *param = getEncapsulatedParameterElement(el: arg);
664 os.getStream().printReindented(str: strfmt(fmt: checkParamKey, parameters: param->getName()));
665 // An `ArrayRefParameter` without a custom parser in a non-last position
666 // uses `[...]` delimiters to avoid ambiguity with the struct-level comma.
667 bool useBrackets = isa<ParameterElement>(Val: arg) &&
668 needsStructBrackets(el: param) &&
669 idx != structElems.size() - 1;
670 if (useBrackets) {
671 os.indent();
672 genLiteralParser(value: "[", ctx, os);
673 }
674 if (isa<ParameterElement>(Val: arg))
675 genStructParameterParser(el: param, ctx, os&: os.indent());
676 else if (auto *custom = dyn_cast<CustomDirective>(Val: arg))
677 genCustomParser(el: custom, ctx, os&: os.indent());
678 if (useBrackets) {
679 os.unindent();
680 genLiteralParser(value: "]", ctx, os);
681 }
682 os.unindent() << "} else ";
683 // Print the check for duplicate or unknown parameter.
684 }
685 os.getStream().printReindented(str: strfmt(fmt: loopEnd, parameters: tgfmt(fmt: parserErrorStr, ctx: &ctx)));
686 os << "return true;\n";
687 os.unindent() << "};\n";
688
689 // Generate the parsing loop. If optional parameters are present, then the
690 // parse loop is guarded by commas.
691 unsigned numOptional = llvm::count_if(Range: el->getElements(), P: formatIsOptional);
692 if (numOptional) {
693 // If the struct itself is optional, pull out the first iteration.
694 if (numOptional == el->getNumElements()) {
695 os.getStream().printReindented(str: tgfmt(fmt: optionalStructFirst, ctx: &ctx).str());
696 os.indent();
697 } else {
698 os << "do {\n";
699 }
700 } else {
701 os.getStream().printReindented(
702 str: tgfmt(fmt: loopHeader, ctx: &ctx, vals: el->getNumElements()).str());
703 }
704 os.indent();
705 os.getStream().printReindented(str: tgfmt(fmt: loopStart, ctx: &ctx).str());
706 os.unindent();
707
708 // Print the loop terminator. For optional parameters, we have to check that
709 // all mandatory parameters have been parsed.
710 // The whole struct is optional if all its parameters are optional.
711 if (numOptional) {
712 if (numOptional == el->getNumElements()) {
713 os << "}\n";
714 os.unindent() << "}\n";
715 } else {
716 os << tgfmt(fmt: "} while(!$_parser.parseOptionalComma());\n", ctx: &ctx);
717 for (FormatElement *arg : el->getElements()) {
718 ParameterElement *param = getEncapsulatedParameterElement(el: arg);
719 if (param->isOptional())
720 continue;
721 os.getStream().printReindented(
722 str: strfmt(fmt: checkParam, parameters: param->getName(), parameters: tgfmt(fmt: parserErrorStr, ctx: &ctx)));
723 }
724 }
725 } else {
726 // Because the loop loops N times and each non-failing iteration sets 1 of
727 // N flags, successfully exiting the loop means that all parameters have
728 // been seen. `parseOptionalComma` would cause issues with any formats that
729 // use "struct(...) `,`" beacuse structs aren't sounded by braces.
730 os.getStream().printReindented(
731 str: strfmt(fmt: loopTerminator, parameters: el->getNumElements()));
732 }
733 os.unindent() << "}\n";
734}
735
736void DefFormat::genCustomParser(CustomDirective *el, FmtContext &ctx,
737 MethodBody &os, bool isOptional) {
738 os << "{\n";
739 os.indent();
740
741 // Bound variables are passed directly to the parser as `FailureOr<T> &`.
742 // Referenced variables are passed as `T`. The custom parser fails if it
743 // returns failure or if any of the required parameters failed.
744 os << tgfmt(fmt: "auto odsCustomLoc = $_parser.getCurrentLocation();\n", ctx: &ctx);
745 os << "(void)odsCustomLoc;\n";
746 os << tgfmt(fmt: "auto odsCustomResult = parse$0($_parser", ctx: &ctx, vals: el->getName());
747 os.indent();
748 for (FormatElement *arg : el->getElements()) {
749 os << ",\n";
750 if (auto *param = dyn_cast<ParameterElement>(Val: arg))
751 os << "::mlir::detail::unwrapForCustomParse(_result_" << param->getName()
752 << ")";
753 else if (auto *ref = dyn_cast<RefDirective>(Val: arg))
754 os << "*_result_" << cast<ParameterElement>(Val: ref->getArg())->getName();
755 else
756 os << tgfmt(fmt: cast<StringElement>(Val: arg)->getValue(), ctx: &ctx);
757 }
758 os.unindent() << ");\n";
759 if (isOptional) {
760 os << "if (!odsCustomResult.has_value()) return {};\n";
761 os << "if (::mlir::failed(*odsCustomResult)) return ::mlir::failure();\n";
762 } else {
763 os << "if (::mlir::failed(odsCustomResult)) return {};\n";
764 }
765 for (FormatElement *arg : el->getElements()) {
766 if (auto *param = dyn_cast<ParameterElement>(Val: arg)) {
767 if (param->isOptional())
768 continue;
769 os << formatv(Fmt: "if (::mlir::failed(_result_{0})) {{\n", Vals: param->getName());
770 os.indent() << tgfmt(fmt: "$_parser.emitError(odsCustomLoc, ", ctx: &ctx)
771 << "\"custom parser failed to parse parameter '"
772 << param->getName() << "'\");\n";
773 os << "return " << (isOptional ? "::mlir::failure()" : "{}") << ";\n";
774 os.unindent() << "}\n";
775 }
776 }
777
778 os.unindent() << "}\n";
779}
780
781void DefFormat::genOptionalGroupParser(OptionalElement *el, FmtContext &ctx,
782 MethodBody &os) {
783 ArrayRef<FormatElement *> thenElements =
784 el->getThenElements(/*parseable=*/true);
785
786 FormatElement *first = thenElements.front();
787 const auto guardOn = [&](auto params) {
788 os << "if (!(";
789 llvm::interleave(
790 params, os,
791 [&](ParameterElement *el) {
792 os << formatv(Fmt: "(::mlir::succeeded(_result_{0}) && *_result_{0})",
793 Vals: el->getName());
794 },
795 " || ");
796 os << ")) {\n";
797 };
798 if (auto *literal = dyn_cast<LiteralElement>(Val: first)) {
799 genLiteralParser(value: literal->getSpelling(), ctx, os, /*isOptional=*/true);
800 os << ") {\n";
801 } else if (auto *param = dyn_cast<ParameterElement>(Val: first)) {
802 genVariableParser(el: param, ctx, os);
803 guardOn(llvm::ArrayRef(param));
804 } else if (auto *params = dyn_cast<ParamsDirective>(Val: first)) {
805 genParamsParser(el: params, ctx, os);
806 guardOn(params->getElements());
807 } else if (auto *custom = dyn_cast<CustomDirective>(Val: first)) {
808 os << "if (auto result = [&]() -> ::mlir::OptionalParseResult {\n";
809 os.indent();
810 genCustomParser(el: custom, ctx, os, /*isOptional=*/true);
811 os << "return ::mlir::success();\n";
812 os.unindent();
813 os << "}(); result.has_value() && ::mlir::failed(*result)) {\n";
814 os.indent();
815 os << "return {};\n";
816 os.unindent();
817 os << "} else if (result.has_value()) {\n";
818 } else {
819 auto *strct = cast<StructDirective>(Val: first);
820 genStructParser(el: strct, ctx, os);
821 guardOn(params->getElements());
822 }
823 os.indent();
824
825 // Generate the parsers for the rest of the thenElements.
826 for (FormatElement *element : el->getElseElements(/*parseable=*/true))
827 genElementParser(el: element, ctx, os);
828 os.unindent() << "} else {\n";
829 os.indent();
830 for (FormatElement *element : thenElements.drop_front())
831 genElementParser(el: element, ctx, os);
832 os.unindent() << "}\n";
833}
834
835//===----------------------------------------------------------------------===//
836// PrinterGen
837//===----------------------------------------------------------------------===//
838
839void DefFormat::genPrinter(MethodBody &os) {
840 FmtContext ctx;
841 ctx.addSubst(placeholder: "_printer", subst: "odsPrinter");
842 ctx.addSubst(placeholder: "_ctxt", subst: "getContext()");
843 ctx.withBuilder(subst: "odsBuilder");
844 os.indent();
845 os << "::mlir::Builder odsBuilder(getContext());\n";
846
847 // Start with no leading space: the generated dispatcher
848 // (`generatedAttributePrinter` / `generatedTypePrinter`) is responsible for
849 // emitting any space between the mnemonic and the first printed element.
850 shouldEmitSpace = false;
851 lastWasPunctuation = true;
852 for (FormatElement *el : elements)
853 genElementPrinter(el, ctx, os);
854}
855
856void DefFormat::genElementPrinter(FormatElement *el, FmtContext &ctx,
857 MethodBody &os) {
858 if (auto *literal = dyn_cast<LiteralElement>(Val: el))
859 return genLiteralPrinter(value: literal->getSpelling(), ctx, os);
860 if (auto *params = dyn_cast<ParamsDirective>(Val: el))
861 return genParamsPrinter(el: params, ctx, os);
862 if (auto *strct = dyn_cast<StructDirective>(Val: el))
863 return genStructPrinter(el: strct, ctx, os);
864 if (auto *custom = dyn_cast<CustomDirective>(Val: el))
865 return genCustomPrinter(el: custom, ctx, os);
866 if (auto *var = dyn_cast<ParameterElement>(Val: el))
867 return genVariablePrinter(el: var, ctx, os);
868 if (auto *optional = dyn_cast<OptionalElement>(Val: el))
869 return genOptionalGroupPrinter(el: optional, ctx, os);
870 if (auto *whitespace = dyn_cast<WhitespaceElement>(Val: el))
871 return genWhitespacePrinter(el: whitespace, ctx, os);
872
873 llvm::PrintFatalError(Msg: "unsupported format element");
874}
875
876void DefFormat::genLiteralPrinter(StringRef value, FmtContext &ctx,
877 MethodBody &os) {
878 // Don't insert a space before certain punctuation.
879 bool needSpace =
880 shouldEmitSpace && shouldEmitSpaceBefore(value, lastWasPunctuation);
881 os << tgfmt(fmt: "$_printer$0 << \"$1\";\n", ctx: &ctx, vals: needSpace ? " << ' '" : "",
882 vals&: value);
883
884 // Update the flags.
885 shouldEmitSpace =
886 value.size() != 1 || !StringRef("<({[").contains(C: value.front());
887 lastWasPunctuation = value.front() != '_' && !isalpha(value.front());
888}
889
890void DefFormat::genVariablePrinter(ParameterElement *el, FmtContext &ctx,
891 MethodBody &os, bool skipGuard,
892 std::optional<StringRef> printerOverride,
893 std::optional<StringRef> selfOverride) {
894 const AttrOrTypeParameter &param = el->getParam();
895 std::string self =
896 selfOverride ? selfOverride->str() : param.getAccessorName() + "()";
897 ctx.withSelf(subst: self);
898
899 // Guard the printer on the presence of optional parameters and that they
900 // aren't equal to their default values (if they have one).
901 if (el->isOptional() && !skipGuard) {
902 el->genPrintGuard(ctx, os&: os << "if (") << ") {\n";
903 os.indent();
904 }
905
906 // Insert a space before the next parameter, if necessary.
907 if (shouldEmitSpace || !lastWasPunctuation)
908 os << tgfmt(fmt: "$_printer << ' ';\n", ctx: &ctx);
909 shouldEmitSpace = true;
910 lastWasPunctuation = false;
911
912 if (printerOverride)
913 os << tgfmt(fmt: *printerOverride, ctx: &ctx) << ";\n";
914 else if (el->shouldBeQualified())
915 os << tgfmt(fmt: qualifiedParameterPrinter, ctx: &ctx) << ";\n";
916 else if (auto printer = param.getPrinter())
917 os << tgfmt(fmt: *printer, ctx: &ctx) << ";\n";
918 else
919 os << tgfmt(fmt: defaultParameterPrinter, ctx: &ctx) << ";\n";
920
921 if (el->isOptional() && !skipGuard)
922 os.unindent() << "}\n";
923}
924
925void DefFormat::genStructParameterPrinter(ParameterElement *el, FmtContext &ctx,
926 MethodBody &os) {
927 const llvm::Record *enumAttr = getEnumAttrDef(el);
928 if (!enumAttr || el->shouldBeQualified())
929 return genVariablePrinter(el, ctx, os);
930
931 std::string self = el->getParam().getAccessorName() + "().getValue()";
932 genVariablePrinter(el, ctx, os, /*skipGuard=*/false, printerOverride: "$_printer << $_self",
933 selfOverride: self);
934}
935
936/// Generate code to guard printing on the presence of any optional parameters.
937template <typename ParameterRange>
938static void guardOnAny(FmtContext &ctx, MethodBody &os, ParameterRange &&params,
939 bool inverted = false) {
940 os << "if (";
941 if (inverted)
942 os << "!(";
943 llvm::interleave(
944 params, os,
945 [&](ParameterElement *param) { param->genPrintGuard(ctx, os); }, " || ");
946 if (inverted)
947 os << ")";
948 os << ") {\n";
949 os.indent();
950}
951
952/// Generate code to guard printing on the presence of any optional format
953/// elements.
954template <typename FormatElemRange>
955static void guardOnAnyOptional(FmtContext &ctx, MethodBody &os,
956 FormatElemRange &&args, bool inverted = false) {
957 guardOnAny(ctx, os,
958 llvm::make_filter_range(
959 llvm::map_range(args, getEncapsulatedParameterElement),
960 [](ParameterElement *param) { return param->isOptional(); }),
961 inverted);
962}
963
964void DefFormat::genCommaSeparatedPrinter(
965 ArrayRef<FormatElement *> args, FmtContext &ctx, MethodBody &os,
966 function_ref<void(FormatElement *)> extra, bool stripEnumAttrs,
967 function_ref<void(FormatElement *)> extraPost) {
968 // Emit a space if necessary, but only if the struct is present.
969 if (shouldEmitSpace || !lastWasPunctuation) {
970 bool allOptional = llvm::all_of(Range&: args, P: formatIsOptional);
971 if (allOptional)
972 guardOnAnyOptional(ctx, os, args);
973 os << tgfmt(fmt: "$_printer << ' ';\n", ctx: &ctx);
974 if (allOptional)
975 os.unindent() << "}\n";
976 }
977
978 // The first printed element does not need to emit a comma.
979 if (!args.empty()) {
980 os << "{\n";
981 os.indent() << "bool _firstPrinted = true;\n";
982 for (FormatElement *arg : args) {
983 ParameterElement *param = getEncapsulatedParameterElement(el: arg);
984 if (param->isOptional()) {
985 param->genPrintGuard(ctx, os&: os << "if (") << ") {\n";
986 os.indent();
987 }
988 os << tgfmt(fmt: "if (!_firstPrinted) $_printer << \", \";\n", ctx: &ctx);
989 os << "_firstPrinted = false;\n";
990 extra(arg);
991 shouldEmitSpace = false;
992 lastWasPunctuation = true;
993 if (auto *realParam = dyn_cast<ParameterElement>(Val: arg)) {
994 if (stripEnumAttrs)
995 genStructParameterPrinter(el: realParam, ctx, os);
996 else
997 genVariablePrinter(el: realParam, ctx, os);
998 } else if (auto *custom = dyn_cast<CustomDirective>(Val: arg))
999 genCustomPrinter(el: custom, ctx, os);
1000 if (extraPost)
1001 extraPost(arg);
1002 if (param->isOptional())
1003 os.unindent() << "}\n";
1004 }
1005 os.unindent() << "}\n";
1006 }
1007}
1008
1009void DefFormat::genParamsPrinter(ParamsDirective *el, FmtContext &ctx,
1010 MethodBody &os) {
1011 SmallVector<FormatElement *> args = llvm::map_to_vector(
1012 C: el->getElements(), F: [](ParameterElement *param) -> FormatElement * {
1013 return static_cast<FormatElement *>(param);
1014 });
1015 genCommaSeparatedPrinter(
1016 args, ctx, os, extra: [&](FormatElement *param) {},
1017 /*stripEnumAttrs=*/false);
1018}
1019
1020void DefFormat::genStructPrinter(StructDirective *el, FmtContext &ctx,
1021 MethodBody &os) {
1022 ArrayRef<FormatElement *> elems = el->getElements();
1023 // A non-final parameter whose parser may consume commas must be wrapped in
1024 // `[...]` to avoid ambiguity with the struct-level comma separator. Track the
1025 // element index via elemIdx, which is incremented once per element in the
1026 // extraPost callback.
1027 size_t elemIdx = 0;
1028 genCommaSeparatedPrinter(
1029 args: elems, ctx, os,
1030 extra: [&](FormatElement *arg) {
1031 ParameterElement *param = getEncapsulatedParameterElement(el: arg);
1032 os << tgfmt(fmt: "$_printer << \"$0 = \";\n", ctx: &ctx, vals: param->getName());
1033 auto *paramEl = dyn_cast<ParameterElement>(Val: arg);
1034 if (paramEl && needsStructBrackets(el: paramEl) &&
1035 elemIdx + 1 < elems.size())
1036 os << tgfmt(fmt: "$_printer << \"[\";\n", ctx: &ctx);
1037 },
1038 /*stripEnumAttrs=*/true,
1039 extraPost: [&](FormatElement *arg) {
1040 auto *paramEl = dyn_cast<ParameterElement>(Val: arg);
1041 if (paramEl && needsStructBrackets(el: paramEl) &&
1042 elemIdx + 1 < elems.size())
1043 os << tgfmt(fmt: "$_printer << \"]\";\n", ctx: &ctx);
1044 ++elemIdx;
1045 });
1046}
1047
1048void DefFormat::genCustomPrinter(CustomDirective *el, FmtContext &ctx,
1049 MethodBody &os) {
1050 // Insert a space before the custom directive, if necessary.
1051 if (shouldEmitSpace || !lastWasPunctuation)
1052 os << tgfmt(fmt: "$_printer << ' ';\n", ctx: &ctx);
1053 shouldEmitSpace = true;
1054 lastWasPunctuation = false;
1055
1056 os << tgfmt(fmt: "print$0($_printer", ctx: &ctx, vals: el->getName());
1057 os.indent();
1058 for (FormatElement *arg : el->getElements()) {
1059 os << ",\n";
1060 if (auto *param = dyn_cast<ParameterElement>(Val: arg)) {
1061 os << param->getParam().getAccessorName() << "()";
1062 } else if (auto *ref = dyn_cast<RefDirective>(Val: arg)) {
1063 os << cast<ParameterElement>(Val: ref->getArg())->getParam().getAccessorName()
1064 << "()";
1065 } else {
1066 os << tgfmt(fmt: cast<StringElement>(Val: arg)->getValue(), ctx: &ctx);
1067 }
1068 }
1069 os.unindent() << ");\n";
1070}
1071
1072void DefFormat::genOptionalGroupPrinter(OptionalElement *el, FmtContext &ctx,
1073 MethodBody &os) {
1074 FormatElement *anchor = el->getAnchor();
1075 if (auto *param = dyn_cast<ParameterElement>(Val: anchor)) {
1076 guardOnAny(ctx, os, params: llvm::ArrayRef(param), inverted: el->isInverted());
1077 } else if (auto *params = dyn_cast<ParamsDirective>(Val: anchor)) {
1078 guardOnAny(ctx, os, params: params->getElements(), inverted: el->isInverted());
1079 } else if (auto *strct = dyn_cast<StructDirective>(Val: anchor)) {
1080 guardOnAnyOptional(ctx, os, args: strct->getElements(), inverted: el->isInverted());
1081 } else {
1082 auto *custom = cast<CustomDirective>(Val: anchor);
1083 guardOnAnyOptional(ctx, os, args: custom->getElements(), inverted: el->isInverted());
1084 }
1085 // Generate the printer for the contained elements.
1086 {
1087 llvm::SaveAndRestore shouldEmitSpaceFlag(shouldEmitSpace);
1088 llvm::SaveAndRestore lastWasPunctuationFlag(lastWasPunctuation);
1089 for (FormatElement *element : el->getThenElements())
1090 genElementPrinter(el: element, ctx, os);
1091 }
1092 os.unindent() << "} else {\n";
1093 os.indent();
1094 for (FormatElement *element : el->getElseElements())
1095 genElementPrinter(el: element, ctx, os);
1096 os.unindent() << "}\n";
1097}
1098
1099void DefFormat::genWhitespacePrinter(WhitespaceElement *el, FmtContext &ctx,
1100 MethodBody &os) {
1101 if (el->getValue() == "\\n") {
1102 os << tgfmt(fmt: "$_printer.printNewline();\n", ctx: &ctx);
1103 } else if (!el->getValue().empty()) {
1104 os << tgfmt(fmt: "$_printer << \"$0\";\n", ctx: &ctx, vals: el->getValue());
1105 } else {
1106 lastWasPunctuation = true;
1107 }
1108 shouldEmitSpace = false;
1109}
1110
1111//===----------------------------------------------------------------------===//
1112// DefFormatParser
1113//===----------------------------------------------------------------------===//
1114
1115namespace {
1116class DefFormatParser : public FormatParser {
1117public:
1118 DefFormatParser(llvm::SourceMgr &mgr, const AttrOrTypeDef &def)
1119 : FormatParser(mgr, def.getLoc()[0]), def(def),
1120 seenParams(def.getNumParameters()) {}
1121
1122 /// Parse the attribute or type format and create the format elements.
1123 FailureOr<DefFormat> parse();
1124
1125protected:
1126 /// Verify the parsed elements.
1127 LogicalResult verify(SMLoc loc, ArrayRef<FormatElement *> elements) override;
1128 /// Verify the elements of a custom directive.
1129 LogicalResult
1130 verifyCustomDirectiveArguments(SMLoc loc,
1131 ArrayRef<FormatElement *> arguments) override;
1132 /// Verify the elements of an optional group.
1133 LogicalResult verifyOptionalGroupElements(SMLoc loc,
1134 ArrayRef<FormatElement *> elements,
1135 FormatElement *anchor) override;
1136 /// Verify the arguments to a struct directive.
1137 LogicalResult verifyStructArguments(SMLoc loc,
1138 ArrayRef<FormatElement *> arguments);
1139
1140 LogicalResult markQualified(SMLoc loc, FormatElement *element) override;
1141
1142 /// Parse an attribute or type variable.
1143 FailureOr<FormatElement *> parseVariableImpl(SMLoc loc, StringRef name,
1144 Context ctx) override;
1145 /// Parse an attribute or type format directive.
1146 FailureOr<FormatElement *>
1147 parseDirectiveImpl(SMLoc loc, FormatToken::Kind kind, Context ctx) override;
1148
1149private:
1150 /// Parse a `params` directive.
1151 FailureOr<FormatElement *> parseParamsDirective(SMLoc loc, Context ctx);
1152 /// Parse a `struct` directive.
1153 FailureOr<FormatElement *> parseStructDirective(SMLoc loc, Context ctx);
1154
1155 /// Attribute or type tablegen def.
1156 const AttrOrTypeDef &def;
1157
1158 /// Seen attribute or type parameters.
1159 BitVector seenParams;
1160};
1161} // namespace
1162
1163LogicalResult DefFormatParser::verify(SMLoc loc,
1164 ArrayRef<FormatElement *> elements) {
1165 // Check that all parameters are referenced in the format.
1166 for (auto [index, param] : llvm::enumerate(First: def.getParameters())) {
1167 if (param.isOptional())
1168 continue;
1169 if (!seenParams.test(Idx: index)) {
1170 if (isa<AttributeSelfTypeParameter>(Val: param))
1171 continue;
1172 return emitError(loc, msg: "format is missing reference to parameter: " +
1173 param.getName());
1174 }
1175 if (isa<AttributeSelfTypeParameter>(Val: param)) {
1176 return emitError(loc,
1177 msg: "unexpected self type parameter in assembly format");
1178 }
1179 }
1180 if (elements.empty())
1181 return success();
1182 // A `struct` directive that contains optional parameters cannot be followed
1183 // by a comma literal, which is ambiguous.
1184 for (auto it : llvm::zip(t: elements.drop_back(), u: elements.drop_front())) {
1185 auto *structEl = dyn_cast<StructDirective>(Val: std::get<0>(t&: it));
1186 auto *literalEl = dyn_cast<LiteralElement>(Val: std::get<1>(t&: it));
1187 if (!structEl || !literalEl)
1188 continue;
1189 if (literalEl->getSpelling() == "," && structEl->hasOptionalElements()) {
1190 return emitError(loc, msg: "`struct` directive with optional parameters "
1191 "cannot be followed by a comma literal");
1192 }
1193 }
1194 return success();
1195}
1196
1197LogicalResult DefFormatParser::verifyCustomDirectiveArguments(
1198 SMLoc loc, ArrayRef<FormatElement *> arguments) {
1199 // Arguments are fully verified by the parser context.
1200 return success();
1201}
1202
1203LogicalResult
1204DefFormatParser::verifyOptionalGroupElements(llvm::SMLoc loc,
1205 ArrayRef<FormatElement *> elements,
1206 FormatElement *anchor) {
1207 // `params` and `struct` directives are allowed only if all the contained
1208 // parameters are optional.
1209 for (FormatElement *el : elements) {
1210 if (auto *param = dyn_cast<ParameterElement>(Val: el)) {
1211 if (!param->isOptional()) {
1212 return emitError(loc,
1213 msg: "parameters in an optional group must be optional");
1214 }
1215 } else if (auto *params = dyn_cast<ParamsDirective>(Val: el)) {
1216 if (llvm::any_of(Range: params->getElements(), P: paramNotOptional)) {
1217 return emitError(loc, msg: "`params` directive allowed in optional group "
1218 "only if all parameters are optional");
1219 }
1220 } else if (auto *strct = dyn_cast<StructDirective>(Val: el)) {
1221 if (llvm::any_of(Range: strct->getElements(), P: formatNotOptional)) {
1222 return emitError(loc, msg: "`struct` is only allowed in an optional group "
1223 "if all captured parameters are optional");
1224 }
1225 } else if (auto *custom = dyn_cast<CustomDirective>(Val: el)) {
1226 for (FormatElement *el : custom->getElements()) {
1227 // If the custom argument is a variable, then it must be optional.
1228 if (auto *param = dyn_cast<ParameterElement>(Val: el))
1229 if (!param->isOptional())
1230 return emitError(loc,
1231 msg: "`custom` is only allowed in an optional group if "
1232 "all captured parameters are optional");
1233 }
1234 }
1235 }
1236 // The anchor must be a parameter or one of the aforementioned directives.
1237 if (anchor) {
1238 if (!isa<ParameterElement, ParamsDirective, StructDirective,
1239 CustomDirective>(Val: anchor)) {
1240 return emitError(
1241 loc, msg: "optional group anchor must be a parameter or directive");
1242 }
1243 // If the anchor is a custom directive, make sure at least one of its
1244 // arguments is a bound parameter.
1245 if (auto *custom = dyn_cast<CustomDirective>(Val: anchor)) {
1246 const auto *bound =
1247 llvm::find_if(Range: custom->getElements(), P: [](FormatElement *el) {
1248 return isa<ParameterElement>(Val: el);
1249 });
1250 if (bound == custom->getElements().end())
1251 return emitError(loc, msg: "`custom` directive with no bound parameters "
1252 "cannot be used as optional group anchor");
1253 }
1254 }
1255 return success();
1256}
1257
1258LogicalResult
1259DefFormatParser::verifyStructArguments(SMLoc loc,
1260 ArrayRef<FormatElement *> arguments) {
1261 for (FormatElement *el : arguments) {
1262 if (!isa<ParameterElement, CustomDirective, ParamsDirective>(Val: el)) {
1263 return emitError(loc, msg: "expected a parameter, custom directive or params "
1264 "directive in `struct` arguments list");
1265 }
1266 if (auto *custom = dyn_cast<CustomDirective>(Val: el)) {
1267 if (custom->getNumElements() != 1) {
1268 return emitError(loc, msg: "`struct` can only contain `custom` directives "
1269 "with a single argument");
1270 }
1271 if (failed(Result: custom->getFrontAs<ParameterElement>())) {
1272 return emitError(loc, msg: "a `custom` directive nested within a `struct` "
1273 "must be passed a parameter");
1274 }
1275 }
1276 }
1277 return success();
1278}
1279
1280LogicalResult DefFormatParser::markQualified(SMLoc loc,
1281 FormatElement *element) {
1282 if (!isa<ParameterElement>(Val: element))
1283 return emitError(loc, msg: "`qualified` argument list expected a variable");
1284 cast<ParameterElement>(Val: element)->setShouldBeQualified();
1285 return success();
1286}
1287
1288FailureOr<DefFormat> DefFormatParser::parse() {
1289 FailureOr<std::vector<FormatElement *>> elements = FormatParser::parse();
1290 if (failed(Result: elements))
1291 return failure();
1292 return DefFormat(def, std::move(*elements));
1293}
1294
1295FailureOr<FormatElement *>
1296DefFormatParser::parseVariableImpl(SMLoc loc, StringRef name, Context ctx) {
1297 // Lookup the parameter.
1298 ArrayRef<AttrOrTypeParameter> params = def.getParameters();
1299 auto *it = llvm::find_if(
1300 Range&: params, P: [&](auto &param) { return param.getName() == name; });
1301
1302 // Check that the parameter reference is valid.
1303 if (it == params.end()) {
1304 return emitError(loc,
1305 msg: def.getName() + " has no parameter named '" + name + "'");
1306 }
1307 auto idx = std::distance(first: params.begin(), last: it);
1308
1309 if (ctx != RefDirectiveContext) {
1310 // Check that the variable has not already been bound.
1311 if (seenParams.test(Idx: idx))
1312 return emitError(loc, msg: "duplicate parameter '" + name + "'");
1313 seenParams.set(idx);
1314
1315 // Otherwise, to be referenced, a variable must have been bound.
1316 } else if (!seenParams.test(Idx: idx) && !isa<AttributeSelfTypeParameter>(Val: *it)) {
1317 return emitError(loc, msg: "parameter '" + name +
1318 "' must be bound before it is referenced");
1319 }
1320
1321 return create<ParameterElement>(args: *it);
1322}
1323
1324FailureOr<FormatElement *>
1325DefFormatParser::parseDirectiveImpl(SMLoc loc, FormatToken::Kind kind,
1326 Context ctx) {
1327
1328 switch (kind) {
1329 case FormatToken::kw_qualified:
1330 return parseQualifiedDirective(loc, ctx);
1331 case FormatToken::kw_params:
1332 return parseParamsDirective(loc, ctx);
1333 case FormatToken::kw_struct:
1334 return parseStructDirective(loc, ctx);
1335 default:
1336 return emitError(loc, msg: "unsupported directive kind");
1337 }
1338}
1339
1340FailureOr<FormatElement *> DefFormatParser::parseParamsDirective(SMLoc loc,
1341 Context ctx) {
1342 // It doesn't make sense to allow references to all parameters in a custom
1343 // directive because parameters are the only things that can be bound.
1344 if (ctx != TopLevelContext && ctx != StructDirectiveContext) {
1345 return emitError(loc, msg: "`params` can only be used at the top-level context "
1346 "or within a `struct` directive");
1347 }
1348
1349 // Collect all of the attribute's or type's parameters and ensure that none of
1350 // the parameters have already been captured.
1351 std::vector<ParameterElement *> vars;
1352 for (const auto &it : llvm::enumerate(First: def.getParameters())) {
1353 if (seenParams.test(Idx: it.index())) {
1354 return emitError(loc, msg: "`params` captures duplicate parameter: " +
1355 it.value().getName());
1356 }
1357 // Self-type parameters are handled separately from the rest of the
1358 // parameters.
1359 if (isa<AttributeSelfTypeParameter>(Val: it.value()))
1360 continue;
1361 seenParams.set(it.index());
1362 vars.push_back(x: create<ParameterElement>(args: it.value()));
1363 }
1364 return create<ParamsDirective>(args: std::move(vars));
1365}
1366
1367FailureOr<FormatElement *> DefFormatParser::parseStructDirective(SMLoc loc,
1368 Context ctx) {
1369 if (ctx != TopLevelContext)
1370 return emitError(loc, msg: "`struct` can only be used at the top-level context");
1371
1372 if (failed(Result: parseToken(kind: FormatToken::l_paren,
1373 msg: "expected '(' before `struct` argument list"))) {
1374 return failure();
1375 }
1376
1377 // Parse variables captured by `struct`.
1378 std::vector<FormatElement *> vars;
1379
1380 // Parse first captured parameter or a `params` directive.
1381 FailureOr<FormatElement *> var = parseElement(ctx: StructDirectiveContext);
1382 if (failed(Result: var) ||
1383 !isa<ParameterElement, ParamsDirective, CustomDirective>(Val: *var)) {
1384 return emitError(
1385 loc, msg: "`struct` argument list expected a parameter or directive");
1386 }
1387 if (isa<ParameterElement, CustomDirective>(Val: *var)) {
1388 // Parse any other parameters.
1389 vars.push_back(x: *var);
1390 while (peekToken().is(kind: FormatToken::comma)) {
1391 consumeToken();
1392 var = parseElement(ctx: StructDirectiveContext);
1393 if (failed(Result: var) || !isa<ParameterElement, CustomDirective>(Val: *var))
1394 return emitError(loc, msg: "expected a parameter or `custom` directive in "
1395 "`struct` argument list");
1396 vars.push_back(x: *var);
1397 }
1398 } else {
1399 // `struct(params)` captures all parameters in the attribute or type.
1400 ParamsDirective *params = cast<ParamsDirective>(Val: *var);
1401 vars.reserve(n: params->getNumElements());
1402 for (ParameterElement *el : params->takeElements())
1403 vars.push_back(x: cast<FormatElement>(Val: el));
1404 }
1405
1406 if (failed(Result: parseToken(kind: FormatToken::r_paren,
1407 msg: "expected ')' at the end of an argument list"))) {
1408 return failure();
1409 }
1410 if (failed(Result: verifyStructArguments(loc, arguments: vars)))
1411 return failure();
1412 return create<StructDirective>(args: std::move(vars));
1413}
1414
1415//===----------------------------------------------------------------------===//
1416// Interface
1417//===----------------------------------------------------------------------===//
1418
1419void mlir::tblgen::generateAttrOrTypeFormat(const AttrOrTypeDef &def,
1420 MethodBody &parser,
1421 MethodBody &printer) {
1422 llvm::SourceMgr mgr;
1423 mgr.AddNewSourceBuffer(
1424 F: llvm::MemoryBuffer::getMemBuffer(InputData: *def.getAssemblyFormat()), IncludeLoc: SMLoc());
1425
1426 // Parse the custom assembly format>
1427 DefFormatParser fmtParser(mgr, def);
1428 FailureOr<DefFormat> format = fmtParser.parse();
1429 if (failed(Result: format)) {
1430 if (formatErrorIsFatal)
1431 PrintFatalError(ErrorLoc: def.getLoc(), Msg: "failed to parse assembly format");
1432 return;
1433 }
1434
1435 // Generate the parser and printer.
1436 format->genParser(os&: parser);
1437 format->genPrinter(os&: printer);
1438}
1439