|
1 | 1 | //===- IRDLToCpp.cpp - Converts IRDL definitions to C++ -------------------===// |
2 | 2 | // |
3 | | -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 3 | +// Part of the LLVM Project, under the A0ache License v2.0 with LLVM Exceptions. |
4 | 4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | 6 | // |
@@ -31,14 +31,14 @@ constexpr char definitionMacroFlag[] = "GEN_DIALECT_DEF"; |
31 | 31 | namespace { |
32 | 32 |
|
33 | 33 | struct DialectStrings { |
34 | | - StringRef dialectName; |
35 | | - StringRef dialectCppName; |
36 | | - StringRef dialectCppShortName; |
37 | | - StringRef dialectBaseTypeName; |
38 | | - |
39 | | - StringRef namespaceOpen; |
40 | | - StringRef namespaceClose; |
41 | | - StringRef namespacePath; |
| 34 | + std::string dialectName; |
| 35 | + std::string dialectCppName; |
| 36 | + std::string dialectCppShortName; |
| 37 | + std::string dialectBaseTypeName; |
| 38 | + |
| 39 | + std::string namespaceOpen; |
| 40 | + std::string namespaceClose; |
| 41 | + std::string namespacePath; |
42 | 42 | }; |
43 | 43 |
|
44 | 44 | struct TypeStrings { |
@@ -248,8 +248,6 @@ static LogicalResult generateInclude(irdl::DialectOp dialect, |
248 | 248 | static const auto typeHeaderDeclTemplate = irdl::detail::Template( |
249 | 249 | #include "Templates/TypeHeaderDecl.txt" |
250 | 250 | ); |
251 | | - output << "#ifdef " << declarationMacroFlag << "\n#undef " |
252 | | - << declarationMacroFlag << "\n"; |
253 | 251 |
|
254 | 252 | irdl::detail::dictionary dict; |
255 | 253 | fillDict(dict, dialectStrings); |
@@ -285,8 +283,6 @@ static LogicalResult generateInclude(irdl::DialectOp dialect, |
285 | 283 | return failure(); |
286 | 284 | } |
287 | 285 |
|
288 | | - output << "#endif // " << declarationMacroFlag << "\n"; |
289 | | - |
290 | 286 | return success(); |
291 | 287 | } |
292 | 288 |
|
@@ -375,9 +371,6 @@ static LogicalResult generateLib(irdl::DialectOp dialect, raw_ostream &output, |
375 | 371 | irdl::detail::dictionary dict; |
376 | 372 | fillDict(dict, dialectStrings); |
377 | 373 |
|
378 | | - output << "#ifdef " << definitionMacroFlag << "\n#undef " |
379 | | - << definitionMacroFlag << "\n"; |
380 | | - |
381 | 374 | typeHeaderDefTemplate.render(output, dict); |
382 | 375 |
|
383 | 376 | SmallVector<std::string> typeNames; |
@@ -465,7 +458,6 @@ static LogicalResult generateLib(irdl::DialectOp dialect, raw_ostream &output, |
465 | 458 | output << perOpDefinitions; |
466 | 459 | dialectDefTemplate.render(output, dict); |
467 | 460 |
|
468 | | - output << "#endif // " << definitionMacroFlag << "\n"; |
469 | 461 | return success(); |
470 | 462 | } |
471 | 463 |
|
@@ -517,14 +509,13 @@ irdl::translateIRDLDialectToCpp(llvm::ArrayRef<irdl::DialectOp> dialects, |
517 | 509 | #include "Templates/TypeDef.txt" |
518 | 510 | ); |
519 | 511 |
|
520 | | - llvm::LogicalResult result = success(); |
| 512 | + llvm::SmallMapVector<DialectOp, DialectStrings, 2> dialectStringTable; |
521 | 513 |
|
522 | 514 | for (auto dialect : dialects) { |
523 | | - StringRef dialectName = dialect.getSymName(); |
524 | | - |
525 | | - if (failed(verifySupported(dialect))) { |
| 515 | + if (failed(verifySupported(dialect))) |
526 | 516 | return failure(); |
527 | | - } |
| 517 | + |
| 518 | + StringRef dialectName = dialect.getSymName(); |
528 | 519 |
|
529 | 520 | SmallVector<SmallString<8>> namespaceAbsolutePath{{"mlir"}, dialectName}; |
530 | 521 | std::string namespaceOpen; |
@@ -553,18 +544,34 @@ irdl::translateIRDLDialectToCpp(llvm::ArrayRef<irdl::DialectOp> dialects, |
553 | 544 | dialectStrings.namespaceClose = namespaceClose; |
554 | 545 | dialectStrings.namespacePath = namespacePath; |
555 | 546 |
|
556 | | - output << headerTemplateText; |
| 547 | + dialectStringTable[dialect] = std::move(dialectStrings); |
| 548 | + } |
| 549 | + |
| 550 | + // generate the actual header |
| 551 | + output << headerTemplateText; |
557 | 552 |
|
558 | | - if (failed(generateInclude(dialect, output, dialectStrings))) { |
| 553 | + output << llvm::formatv("#ifdef {0}\n#undef {0}\n", declarationMacroFlag); |
| 554 | + for (auto dialect : dialects) { |
| 555 | + |
| 556 | + auto &dialectStrings = dialectStringTable[dialect]; |
| 557 | + auto &dialectName = dialectStrings.dialectName; |
| 558 | + |
| 559 | + if (failed(generateInclude(dialect, output, dialectStrings))) |
559 | 560 | return dialect->emitError("Error in Dialect " + dialectName + |
560 | 561 | " while generating headers"); |
561 | | - } |
| 562 | + } |
| 563 | + output << llvm::formatv("#endif // #ifdef {}\n", declarationMacroFlag); |
| 564 | + |
| 565 | + output << llvm::formatv("#ifdef {0}\n#undef {0}\n ", definitionMacroFlag); |
| 566 | + for (auto &dialect : dialects) { |
| 567 | + auto &dialectStrings = dialectStringTable[dialect]; |
| 568 | + auto &dialectName = dialectStrings.dialectName; |
562 | 569 |
|
563 | | - if (failed(generateLib(dialect, output, dialectStrings))) { |
| 570 | + if (failed(generateLib(dialect, output, dialectStrings))) |
564 | 571 | return dialect->emitError("Error in Dialect " + dialectName + |
565 | 572 | " while generating library"); |
566 | | - } |
567 | 573 | } |
| 574 | + output << llvm::formatv("#endif // #ifdef {}\n", definitionMacroFlag); |
568 | 575 |
|
569 | | - return result; |
| 576 | + return success(); |
570 | 577 | } |
0 commit comments