1 //===- DirectiveEmitter.cpp - Directive Language Emitter ------------------===// 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 // DirectiveEmitter uses the descriptions of directives and clauses to construct 10 // common code declarations to be used in Frontends. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/TableGen/DirectiveEmitter.h" 15 #include "llvm/ADT/STLExtras.h" 16 #include "llvm/ADT/SmallVector.h" 17 #include "llvm/ADT/StringSet.h" 18 #include "llvm/TableGen/Error.h" 19 #include "llvm/TableGen/Record.h" 20 21 using namespace llvm; 22 23 namespace { 24 // Simple RAII helper for defining ifdef-undef-endif scopes. 25 class IfDefScope { 26 public: 27 IfDefScope(StringRef Name, raw_ostream &OS) : Name(Name), OS(OS) { 28 OS << "#ifdef " << Name << "\n" 29 << "#undef " << Name << "\n"; 30 } 31 32 ~IfDefScope() { OS << "\n#endif // " << Name << "\n\n"; } 33 34 private: 35 StringRef Name; 36 raw_ostream &OS; 37 }; 38 } // end anonymous namespace 39 40 namespace llvm { 41 42 // Generate enum class 43 void GenerateEnumClass(const std::vector<Record *> &Records, raw_ostream &OS, 44 StringRef Enum, StringRef Prefix, 45 const DirectiveLanguage &DirLang) { 46 OS << "\n"; 47 OS << "enum class " << Enum << " {\n"; 48 for (const auto &R : Records) { 49 BaseRecord Rec{R}; 50 OS << " " << Prefix << Rec.getFormattedName() << ",\n"; 51 } 52 OS << "};\n"; 53 OS << "\n"; 54 OS << "static constexpr std::size_t " << Enum 55 << "_enumSize = " << Records.size() << ";\n"; 56 57 // Make the enum values available in the defined namespace. This allows us to 58 // write something like Enum_X if we have a `using namespace <CppNamespace>`. 59 // At the same time we do not loose the strong type guarantees of the enum 60 // class, that is we cannot pass an unsigned as Directive without an explicit 61 // cast. 62 if (DirLang.hasMakeEnumAvailableInNamespace()) { 63 OS << "\n"; 64 for (const auto &R : Records) { 65 BaseRecord Rec{R}; 66 OS << "constexpr auto " << Prefix << Rec.getFormattedName() << " = " 67 << "llvm::" << DirLang.getCppNamespace() << "::" << Enum 68 << "::" << Prefix << Rec.getFormattedName() << ";\n"; 69 } 70 } 71 } 72 73 // Generate enums for values that clauses can take. 74 // Also generate function declarations for get<Enum>Name(StringRef Str). 75 void GenerateEnumClauseVal(const std::vector<Record *> &Records, 76 raw_ostream &OS, const DirectiveLanguage &DirLang, 77 std::string &EnumHelperFuncs) { 78 for (const auto &R : Records) { 79 Clause C{R}; 80 const auto &ClauseVals = C.getClauseVals(); 81 if (ClauseVals.size() <= 0) 82 continue; 83 84 const auto &EnumName = C.getEnumName(); 85 if (EnumName.size() == 0) { 86 PrintError("enumClauseValue field not set in Clause" + 87 C.getFormattedName() + "."); 88 return; 89 } 90 91 OS << "\n"; 92 OS << "enum class " << EnumName << " {\n"; 93 for (const auto &CV : ClauseVals) { 94 ClauseVal CVal{CV}; 95 OS << " " << CV->getName() << "=" << CVal.getValue() << ",\n"; 96 } 97 OS << "};\n"; 98 99 if (DirLang.hasMakeEnumAvailableInNamespace()) { 100 OS << "\n"; 101 for (const auto &CV : ClauseVals) { 102 OS << "constexpr auto " << CV->getName() << " = " 103 << "llvm::" << DirLang.getCppNamespace() << "::" << EnumName 104 << "::" << CV->getName() << ";\n"; 105 } 106 EnumHelperFuncs += (llvm::Twine(EnumName) + llvm::Twine(" get") + 107 llvm::Twine(EnumName) + llvm::Twine("(StringRef);\n")) 108 .str(); 109 110 EnumHelperFuncs += 111 (llvm::Twine("llvm::StringRef get") + llvm::Twine(DirLang.getName()) + 112 llvm::Twine(EnumName) + llvm::Twine("Name(") + 113 llvm::Twine(EnumName) + llvm::Twine(");\n")) 114 .str(); 115 } 116 } 117 } 118 119 bool HasDuplicateClauses(const std::vector<Record *> &Clauses, 120 const Directive &Directive, 121 llvm::StringSet<> &CrtClauses) { 122 bool HasError = false; 123 for (const auto &C : Clauses) { 124 VersionedClause VerClause{C}; 125 const auto insRes = CrtClauses.insert(VerClause.getClause().getName()); 126 if (!insRes.second) { 127 PrintError("Clause " + VerClause.getClause().getRecordName() + 128 " already defined on directive " + Directive.getRecordName()); 129 HasError = true; 130 } 131 } 132 return HasError; 133 } 134 135 // Check for duplicate clauses in lists. Clauses cannot appear twice in the 136 // three allowed list. Also, since required implies allowed, clauses cannot 137 // appear in both the allowedClauses and requiredClauses lists. 138 bool HasDuplicateClausesInDirectives(const std::vector<Record *> &Directives) { 139 bool HasDuplicate = false; 140 for (const auto &D : Directives) { 141 Directive Dir{D}; 142 llvm::StringSet<> Clauses; 143 // Check for duplicates in the three allowed lists. 144 if (HasDuplicateClauses(Dir.getAllowedClauses(), Dir, Clauses) || 145 HasDuplicateClauses(Dir.getAllowedOnceClauses(), Dir, Clauses) || 146 HasDuplicateClauses(Dir.getAllowedExclusiveClauses(), Dir, Clauses)) { 147 HasDuplicate = true; 148 } 149 // Check for duplicate between allowedClauses and required 150 Clauses.clear(); 151 if (HasDuplicateClauses(Dir.getAllowedClauses(), Dir, Clauses) || 152 HasDuplicateClauses(Dir.getRequiredClauses(), Dir, Clauses)) { 153 HasDuplicate = true; 154 } 155 if (HasDuplicate) 156 PrintFatalError("One or more clauses are defined multiple times on" 157 " directive " + 158 Dir.getRecordName()); 159 } 160 161 return HasDuplicate; 162 } 163 164 // Check consitency of records. Return true if an error has been detected. 165 // Return false if the records are valid. 166 bool DirectiveLanguage::HasValidityErrors() const { 167 if (getDirectiveLanguages().size() != 1) { 168 PrintFatalError("A single definition of DirectiveLanguage is needed."); 169 return true; 170 } 171 172 return HasDuplicateClausesInDirectives(getDirectives()); 173 } 174 175 // Generate the declaration section for the enumeration in the directive 176 // language 177 void EmitDirectivesDecl(RecordKeeper &Records, raw_ostream &OS) { 178 const auto DirLang = DirectiveLanguage{Records}; 179 if (DirLang.HasValidityErrors()) 180 return; 181 182 OS << "#ifndef LLVM_" << DirLang.getName() << "_INC\n"; 183 OS << "#define LLVM_" << DirLang.getName() << "_INC\n"; 184 185 if (DirLang.hasEnableBitmaskEnumInNamespace()) 186 OS << "\n#include \"llvm/ADT/BitmaskEnum.h\"\n"; 187 188 OS << "\n"; 189 OS << "namespace llvm {\n"; 190 OS << "class StringRef;\n"; 191 192 // Open namespaces defined in the directive language 193 llvm::SmallVector<StringRef, 2> Namespaces; 194 llvm::SplitString(DirLang.getCppNamespace(), Namespaces, "::"); 195 for (auto Ns : Namespaces) 196 OS << "namespace " << Ns << " {\n"; 197 198 if (DirLang.hasEnableBitmaskEnumInNamespace()) 199 OS << "\nLLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();\n"; 200 201 // Emit Directive enumeration 202 GenerateEnumClass(DirLang.getDirectives(), OS, "Directive", 203 DirLang.getDirectivePrefix(), DirLang); 204 205 // Emit Clause enumeration 206 GenerateEnumClass(DirLang.getClauses(), OS, "Clause", 207 DirLang.getClausePrefix(), DirLang); 208 209 // Emit ClauseVal enumeration 210 std::string EnumHelperFuncs; 211 GenerateEnumClauseVal(DirLang.getClauses(), OS, DirLang, EnumHelperFuncs); 212 213 // Generic function signatures 214 OS << "\n"; 215 OS << "// Enumeration helper functions\n"; 216 OS << "Directive get" << DirLang.getName() 217 << "DirectiveKind(llvm::StringRef Str);\n"; 218 OS << "\n"; 219 OS << "llvm::StringRef get" << DirLang.getName() 220 << "DirectiveName(Directive D);\n"; 221 OS << "\n"; 222 OS << "Clause get" << DirLang.getName() 223 << "ClauseKind(llvm::StringRef Str);\n"; 224 OS << "\n"; 225 OS << "llvm::StringRef get" << DirLang.getName() << "ClauseName(Clause C);\n"; 226 OS << "\n"; 227 OS << "/// Return true if \\p C is a valid clause for \\p D in version \\p " 228 << "Version.\n"; 229 OS << "bool isAllowedClauseForDirective(Directive D, " 230 << "Clause C, unsigned Version);\n"; 231 OS << "\n"; 232 if (EnumHelperFuncs.length() > 0) { 233 OS << EnumHelperFuncs; 234 OS << "\n"; 235 } 236 237 // Closing namespaces 238 for (auto Ns : llvm::reverse(Namespaces)) 239 OS << "} // namespace " << Ns << "\n"; 240 241 OS << "} // namespace llvm\n"; 242 243 OS << "#endif // LLVM_" << DirLang.getName() << "_INC\n"; 244 } 245 246 // Generate function implementation for get<Enum>Name(StringRef Str) 247 void GenerateGetName(const std::vector<Record *> &Records, raw_ostream &OS, 248 StringRef Enum, const DirectiveLanguage &DirLang, 249 StringRef Prefix) { 250 OS << "\n"; 251 OS << "llvm::StringRef llvm::" << DirLang.getCppNamespace() << "::get" 252 << DirLang.getName() << Enum << "Name(" << Enum << " Kind) {\n"; 253 OS << " switch (Kind) {\n"; 254 for (const auto &R : Records) { 255 BaseRecord Rec{R}; 256 OS << " case " << Prefix << Rec.getFormattedName() << ":\n"; 257 OS << " return \""; 258 if (Rec.getAlternativeName().empty()) 259 OS << Rec.getName(); 260 else 261 OS << Rec.getAlternativeName(); 262 OS << "\";\n"; 263 } 264 OS << " }\n"; // switch 265 OS << " llvm_unreachable(\"Invalid " << DirLang.getName() << " " << Enum 266 << " kind\");\n"; 267 OS << "}\n"; 268 } 269 270 // Generate function implementation for get<Enum>Kind(StringRef Str) 271 void GenerateGetKind(const std::vector<Record *> &Records, raw_ostream &OS, 272 StringRef Enum, const DirectiveLanguage &DirLang, 273 StringRef Prefix, bool ImplicitAsUnknown) { 274 275 auto DefaultIt = llvm::find_if( 276 Records, [](Record *R) { return R->getValueAsBit("isDefault") == true; }); 277 278 if (DefaultIt == Records.end()) { 279 PrintError("At least one " + Enum + " must be defined as default."); 280 return; 281 } 282 283 BaseRecord DefaultRec{(*DefaultIt)}; 284 285 OS << "\n"; 286 OS << Enum << " llvm::" << DirLang.getCppNamespace() << "::get" 287 << DirLang.getName() << Enum << "Kind(llvm::StringRef Str) {\n"; 288 OS << " return llvm::StringSwitch<" << Enum << ">(Str)\n"; 289 290 for (const auto &R : Records) { 291 BaseRecord Rec{R}; 292 if (ImplicitAsUnknown && R->getValueAsBit("isImplicit")) { 293 OS << " .Case(\"" << Rec.getName() << "\"," << Prefix 294 << DefaultRec.getFormattedName() << ")\n"; 295 } else { 296 OS << " .Case(\"" << Rec.getName() << "\"," << Prefix 297 << Rec.getFormattedName() << ")\n"; 298 } 299 } 300 OS << " .Default(" << Prefix << DefaultRec.getFormattedName() << ");\n"; 301 OS << "}\n"; 302 } 303 304 // Generate function implementation for get<ClauseVal>Kind(StringRef Str) 305 void GenerateGetKindClauseVal(const DirectiveLanguage &DirLang, 306 raw_ostream &OS) { 307 for (const auto &R : DirLang.getClauses()) { 308 Clause C{R}; 309 const auto &ClauseVals = C.getClauseVals(); 310 if (ClauseVals.size() <= 0) 311 continue; 312 313 auto DefaultIt = llvm::find_if(ClauseVals, [](Record *CV) { 314 return CV->getValueAsBit("isDefault") == true; 315 }); 316 317 if (DefaultIt == ClauseVals.end()) { 318 PrintError("At least one val in Clause " + C.getFormattedName() + 319 " must be defined as default."); 320 return; 321 } 322 const auto DefaultName = (*DefaultIt)->getName(); 323 324 const auto &EnumName = C.getEnumName(); 325 if (EnumName.size() == 0) { 326 PrintError("enumClauseValue field not set in Clause" + 327 C.getFormattedName() + "."); 328 return; 329 } 330 331 OS << "\n"; 332 OS << EnumName << " llvm::" << DirLang.getCppNamespace() << "::get" 333 << EnumName << "(llvm::StringRef Str) {\n"; 334 OS << " return llvm::StringSwitch<" << EnumName << ">(Str)\n"; 335 for (const auto &CV : ClauseVals) { 336 ClauseVal CVal{CV}; 337 OS << " .Case(\"" << CVal.getFormattedName() << "\"," << CV->getName() 338 << ")\n"; 339 } 340 OS << " .Default(" << DefaultName << ");\n"; 341 OS << "}\n"; 342 343 OS << "\n"; 344 OS << "llvm::StringRef llvm::" << DirLang.getCppNamespace() << "::get" 345 << DirLang.getName() << EnumName 346 << "Name(llvm::" << DirLang.getCppNamespace() << "::" << EnumName 347 << " x) {\n"; 348 OS << " switch (x) {\n"; 349 for (const auto &CV : ClauseVals) { 350 ClauseVal CVal{CV}; 351 OS << " case " << CV->getName() << ":\n"; 352 OS << " return \"" << CVal.getFormattedName() << "\";\n"; 353 } 354 OS << " }\n"; // switch 355 OS << " llvm_unreachable(\"Invalid " << DirLang.getName() << " " 356 << EnumName << " kind\");\n"; 357 OS << "}\n"; 358 } 359 } 360 361 void GenerateCaseForVersionedClauses(const std::vector<Record *> &Clauses, 362 raw_ostream &OS, StringRef DirectiveName, 363 const DirectiveLanguage &DirLang, 364 llvm::StringSet<> &Cases) { 365 for (const auto &C : Clauses) { 366 VersionedClause VerClause{C}; 367 368 const auto ClauseFormattedName = VerClause.getClause().getFormattedName(); 369 370 if (Cases.insert(ClauseFormattedName).second) { 371 OS << " case " << DirLang.getClausePrefix() << ClauseFormattedName 372 << ":\n"; 373 OS << " return " << VerClause.getMinVersion() 374 << " <= Version && " << VerClause.getMaxVersion() << " >= Version;\n"; 375 } 376 } 377 } 378 379 // Generate the isAllowedClauseForDirective function implementation. 380 void GenerateIsAllowedClause(const DirectiveLanguage &DirLang, 381 raw_ostream &OS) { 382 OS << "\n"; 383 OS << "bool llvm::" << DirLang.getCppNamespace() 384 << "::isAllowedClauseForDirective(" 385 << "Directive D, Clause C, unsigned Version) {\n"; 386 OS << " assert(unsigned(D) <= llvm::" << DirLang.getCppNamespace() 387 << "::Directive_enumSize);\n"; 388 OS << " assert(unsigned(C) <= llvm::" << DirLang.getCppNamespace() 389 << "::Clause_enumSize);\n"; 390 391 OS << " switch (D) {\n"; 392 393 for (const auto &D : DirLang.getDirectives()) { 394 Directive Dir{D}; 395 396 OS << " case " << DirLang.getDirectivePrefix() << Dir.getFormattedName() 397 << ":\n"; 398 if (Dir.getAllowedClauses().size() == 0 && 399 Dir.getAllowedOnceClauses().size() == 0 && 400 Dir.getAllowedExclusiveClauses().size() == 0 && 401 Dir.getRequiredClauses().size() == 0) { 402 OS << " return false;\n"; 403 } else { 404 OS << " switch (C) {\n"; 405 406 llvm::StringSet<> Cases; 407 408 GenerateCaseForVersionedClauses(Dir.getAllowedClauses(), OS, 409 Dir.getName(), DirLang, Cases); 410 411 GenerateCaseForVersionedClauses(Dir.getAllowedOnceClauses(), OS, 412 Dir.getName(), DirLang, Cases); 413 414 GenerateCaseForVersionedClauses(Dir.getAllowedExclusiveClauses(), OS, 415 Dir.getName(), DirLang, Cases); 416 417 GenerateCaseForVersionedClauses(Dir.getRequiredClauses(), OS, 418 Dir.getName(), DirLang, Cases); 419 420 OS << " default:\n"; 421 OS << " return false;\n"; 422 OS << " }\n"; // End of clauses switch 423 } 424 OS << " break;\n"; 425 } 426 427 OS << " }\n"; // End of directives switch 428 OS << " llvm_unreachable(\"Invalid " << DirLang.getName() 429 << " Directive kind\");\n"; 430 OS << "}\n"; // End of function isAllowedClauseForDirective 431 } 432 433 // Generate a simple enum set with the give clauses. 434 void GenerateClauseSet(const std::vector<Record *> &Clauses, raw_ostream &OS, 435 StringRef ClauseSetPrefix, Directive &Dir, 436 const DirectiveLanguage &DirLang) { 437 438 OS << "\n"; 439 OS << " static " << DirLang.getClauseEnumSetClass() << " " << ClauseSetPrefix 440 << DirLang.getDirectivePrefix() << Dir.getFormattedName() << " {\n"; 441 442 for (const auto &C : Clauses) { 443 VersionedClause VerClause{C}; 444 OS << " llvm::" << DirLang.getCppNamespace() 445 << "::Clause::" << DirLang.getClausePrefix() 446 << VerClause.getClause().getFormattedName() << ",\n"; 447 } 448 OS << " };\n"; 449 } 450 451 // Generate an enum set for the 4 kinds of clauses linked to a directive. 452 void GenerateDirectiveClauseSets(const DirectiveLanguage &DirLang, 453 raw_ostream &OS) { 454 455 IfDefScope Scope("GEN_FLANG_DIRECTIVE_CLAUSE_SETS", OS); 456 457 OS << "\n"; 458 OS << "namespace llvm {\n"; 459 460 // Open namespaces defined in the directive language. 461 llvm::SmallVector<StringRef, 2> Namespaces; 462 llvm::SplitString(DirLang.getCppNamespace(), Namespaces, "::"); 463 for (auto Ns : Namespaces) 464 OS << "namespace " << Ns << " {\n"; 465 466 for (const auto &D : DirLang.getDirectives()) { 467 Directive Dir{D}; 468 469 OS << "\n"; 470 OS << " // Sets for " << Dir.getName() << "\n"; 471 472 GenerateClauseSet(Dir.getAllowedClauses(), OS, "allowedClauses_", Dir, 473 DirLang); 474 GenerateClauseSet(Dir.getAllowedOnceClauses(), OS, "allowedOnceClauses_", 475 Dir, DirLang); 476 GenerateClauseSet(Dir.getAllowedExclusiveClauses(), OS, 477 "allowedExclusiveClauses_", Dir, DirLang); 478 GenerateClauseSet(Dir.getRequiredClauses(), OS, "requiredClauses_", Dir, 479 DirLang); 480 } 481 482 // Closing namespaces 483 for (auto Ns : llvm::reverse(Namespaces)) 484 OS << "} // namespace " << Ns << "\n"; 485 486 OS << "} // namespace llvm\n"; 487 } 488 489 // Generate a map of directive (key) with DirectiveClauses struct as values. 490 // The struct holds the 4 sets of enumeration for the 4 kinds of clauses 491 // allowances (allowed, allowed once, allowed exclusive and required). 492 void GenerateDirectiveClauseMap(const DirectiveLanguage &DirLang, 493 raw_ostream &OS) { 494 495 IfDefScope Scope("GEN_FLANG_DIRECTIVE_CLAUSE_MAP", OS); 496 497 OS << "\n"; 498 OS << "{\n"; 499 500 for (const auto &D : DirLang.getDirectives()) { 501 Directive Dir{D}; 502 OS << " {llvm::" << DirLang.getCppNamespace() 503 << "::Directive::" << DirLang.getDirectivePrefix() 504 << Dir.getFormattedName() << ",\n"; 505 OS << " {\n"; 506 OS << " llvm::" << DirLang.getCppNamespace() << "::allowedClauses_" 507 << DirLang.getDirectivePrefix() << Dir.getFormattedName() << ",\n"; 508 OS << " llvm::" << DirLang.getCppNamespace() << "::allowedOnceClauses_" 509 << DirLang.getDirectivePrefix() << Dir.getFormattedName() << ",\n"; 510 OS << " llvm::" << DirLang.getCppNamespace() 511 << "::allowedExclusiveClauses_" << DirLang.getDirectivePrefix() 512 << Dir.getFormattedName() << ",\n"; 513 OS << " llvm::" << DirLang.getCppNamespace() << "::requiredClauses_" 514 << DirLang.getDirectivePrefix() << Dir.getFormattedName() << ",\n"; 515 OS << " }\n"; 516 OS << " },\n"; 517 } 518 519 OS << "}\n"; 520 } 521 522 // Generate classes entry for Flang clauses in the Flang parse-tree 523 // If the clause as a non-generic class, no entry is generated. 524 // If the clause does not hold a value, an EMPTY_CLASS is used. 525 // If the clause class is generic then a WRAPPER_CLASS is used. When the value 526 // is optional, the value class is wrapped into a std::optional. 527 void GenerateFlangClauseParserClass(const DirectiveLanguage &DirLang, 528 raw_ostream &OS) { 529 530 IfDefScope Scope("GEN_FLANG_CLAUSE_PARSER_CLASSES", OS); 531 532 OS << "\n"; 533 534 for (const auto &C : DirLang.getClauses()) { 535 Clause Clause{C}; 536 if (!Clause.getFlangClass().empty()) { 537 OS << "WRAPPER_CLASS(" << Clause.getFormattedParserClassName() << ", "; 538 if (Clause.isValueOptional() && Clause.isValueList()) { 539 OS << "std::optional<std::list<" << Clause.getFlangClass() << ">>"; 540 } else if (Clause.isValueOptional()) { 541 OS << "std::optional<" << Clause.getFlangClass() << ">"; 542 } else if (Clause.isValueList()) { 543 OS << "std::list<" << Clause.getFlangClass() << ">"; 544 } else { 545 OS << Clause.getFlangClass(); 546 } 547 } else { 548 OS << "EMPTY_CLASS(" << Clause.getFormattedParserClassName(); 549 } 550 OS << ");\n"; 551 } 552 } 553 554 // Generate a list of the different clause classes for Flang. 555 void GenerateFlangClauseParserClassList(const DirectiveLanguage &DirLang, 556 raw_ostream &OS) { 557 558 IfDefScope Scope("GEN_FLANG_CLAUSE_PARSER_CLASSES_LIST", OS); 559 560 OS << "\n"; 561 llvm::interleaveComma(DirLang.getClauses(), OS, [&](Record *C) { 562 Clause Clause{C}; 563 OS << Clause.getFormattedParserClassName() << "\n"; 564 }); 565 } 566 567 // Generate dump node list for the clauses holding a generic class name. 568 void GenerateFlangClauseDump(const DirectiveLanguage &DirLang, 569 raw_ostream &OS) { 570 571 IfDefScope Scope("GEN_FLANG_DUMP_PARSE_TREE_CLAUSES", OS); 572 573 OS << "\n"; 574 for (const auto &C : DirLang.getClauses()) { 575 Clause Clause{C}; 576 OS << "NODE(" << DirLang.getFlangClauseBaseClass() << ", " 577 << Clause.getFormattedParserClassName() << ")\n"; 578 } 579 } 580 581 // Generate Unparse functions for clauses classes in the Flang parse-tree 582 // If the clause is a non-generic class, no entry is generated. 583 void GenerateFlangClauseUnparse(const DirectiveLanguage &DirLang, 584 raw_ostream &OS) { 585 586 IfDefScope Scope("GEN_FLANG_CLAUSE_UNPARSE", OS); 587 588 OS << "\n"; 589 590 for (const auto &C : DirLang.getClauses()) { 591 Clause Clause{C}; 592 if (!Clause.getFlangClass().empty()) { 593 if (Clause.isValueOptional() && Clause.getDefaultValue().empty()) { 594 OS << "void Unparse(const " << DirLang.getFlangClauseBaseClass() 595 << "::" << Clause.getFormattedParserClassName() << " &x) {\n"; 596 OS << " Word(\"" << Clause.getName().upper() << "\");\n"; 597 598 OS << " Walk(\"(\", x.v, \")\");\n"; 599 OS << "}\n"; 600 } else if (Clause.isValueOptional()) { 601 OS << "void Unparse(const " << DirLang.getFlangClauseBaseClass() 602 << "::" << Clause.getFormattedParserClassName() << " &x) {\n"; 603 OS << " Word(\"" << Clause.getName().upper() << "\");\n"; 604 OS << " Put(\"(\");\n"; 605 OS << " if (x.v.has_value())\n"; 606 if (Clause.isValueList()) 607 OS << " Walk(x.v, \",\");\n"; 608 else 609 OS << " Walk(x.v);\n"; 610 OS << " else\n"; 611 OS << " Put(\"" << Clause.getDefaultValue() << "\");\n"; 612 OS << " Put(\")\");\n"; 613 OS << "}\n"; 614 } else { 615 OS << "void Unparse(const " << DirLang.getFlangClauseBaseClass() 616 << "::" << Clause.getFormattedParserClassName() << " &x) {\n"; 617 OS << " Word(\"" << Clause.getName().upper() << "\");\n"; 618 OS << " Put(\"(\");\n"; 619 if (Clause.isValueList()) 620 OS << " Walk(x.v, \",\");\n"; 621 else 622 OS << " Walk(x.v);\n"; 623 OS << " Put(\")\");\n"; 624 OS << "}\n"; 625 } 626 } else { 627 OS << "void Before(const " << DirLang.getFlangClauseBaseClass() 628 << "::" << Clause.getFormattedParserClassName() << " &) { Word(\"" 629 << Clause.getName().upper() << "\"); }\n"; 630 } 631 } 632 } 633 634 // Generate check in the Enter functions for clauses classes. 635 void GenerateFlangClauseCheckPrototypes(const DirectiveLanguage &DirLang, 636 raw_ostream &OS) { 637 638 IfDefScope Scope("GEN_FLANG_CLAUSE_CHECK_ENTER", OS); 639 640 OS << "\n"; 641 for (const auto &C : DirLang.getClauses()) { 642 Clause Clause{C}; 643 OS << "void Enter(const parser::" << DirLang.getFlangClauseBaseClass() 644 << "::" << Clause.getFormattedParserClassName() << " &);\n"; 645 } 646 } 647 648 // Generate the mapping for clauses between the parser class and the 649 // corresponding clause Kind 650 void GenerateFlangClauseParserKindMap(const DirectiveLanguage &DirLang, 651 raw_ostream &OS) { 652 653 IfDefScope Scope("GEN_FLANG_CLAUSE_PARSER_KIND_MAP", OS); 654 655 OS << "\n"; 656 for (const auto &C : DirLang.getClauses()) { 657 Clause Clause{C}; 658 OS << "if constexpr (std::is_same_v<A, parser::" 659 << DirLang.getFlangClauseBaseClass() 660 << "::" << Clause.getFormattedParserClassName(); 661 OS << ">)\n"; 662 OS << " return llvm::" << DirLang.getCppNamespace() 663 << "::Clause::" << DirLang.getClausePrefix() << Clause.getFormattedName() 664 << ";\n"; 665 } 666 667 OS << "llvm_unreachable(\"Invalid " << DirLang.getName() 668 << " Parser clause\");\n"; 669 } 670 671 // Generate the implementation section for the enumeration in the directive 672 // language 673 void EmitDirectivesFlangImpl(const DirectiveLanguage &DirLang, 674 raw_ostream &OS) { 675 676 GenerateDirectiveClauseSets(DirLang, OS); 677 678 GenerateDirectiveClauseMap(DirLang, OS); 679 680 GenerateFlangClauseParserClass(DirLang, OS); 681 682 GenerateFlangClauseParserClassList(DirLang, OS); 683 684 GenerateFlangClauseDump(DirLang, OS); 685 686 GenerateFlangClauseUnparse(DirLang, OS); 687 688 GenerateFlangClauseCheckPrototypes(DirLang, OS); 689 690 GenerateFlangClauseParserKindMap(DirLang, OS); 691 } 692 693 void GenerateClauseClassMacro(const DirectiveLanguage &DirLang, 694 raw_ostream &OS) { 695 // Generate macros style information for legacy code in clang 696 IfDefScope Scope("GEN_CLANG_CLAUSE_CLASS", OS); 697 698 OS << "\n"; 699 700 OS << "#ifndef CLAUSE\n"; 701 OS << "#define CLAUSE(Enum, Str, Implicit)\n"; 702 OS << "#endif\n"; 703 OS << "#ifndef CLAUSE_CLASS\n"; 704 OS << "#define CLAUSE_CLASS(Enum, Str, Class)\n"; 705 OS << "#endif\n"; 706 OS << "#ifndef CLAUSE_NO_CLASS\n"; 707 OS << "#define CLAUSE_NO_CLASS(Enum, Str)\n"; 708 OS << "#endif\n"; 709 OS << "\n"; 710 OS << "#define __CLAUSE(Name, Class) \\\n"; 711 OS << " CLAUSE(" << DirLang.getClausePrefix() 712 << "##Name, #Name, /* Implicit */ false) \\\n"; 713 OS << " CLAUSE_CLASS(" << DirLang.getClausePrefix() 714 << "##Name, #Name, Class)\n"; 715 OS << "#define __CLAUSE_NO_CLASS(Name) \\\n"; 716 OS << " CLAUSE(" << DirLang.getClausePrefix() 717 << "##Name, #Name, /* Implicit */ false) \\\n"; 718 OS << " CLAUSE_NO_CLASS(" << DirLang.getClausePrefix() << "##Name, #Name)\n"; 719 OS << "#define __IMPLICIT_CLAUSE_CLASS(Name, Str, Class) \\\n"; 720 OS << " CLAUSE(" << DirLang.getClausePrefix() 721 << "##Name, Str, /* Implicit */ true) \\\n"; 722 OS << " CLAUSE_CLASS(" << DirLang.getClausePrefix() 723 << "##Name, Str, Class)\n"; 724 OS << "#define __IMPLICIT_CLAUSE_NO_CLASS(Name, Str) \\\n"; 725 OS << " CLAUSE(" << DirLang.getClausePrefix() 726 << "##Name, Str, /* Implicit */ true) \\\n"; 727 OS << " CLAUSE_NO_CLASS(" << DirLang.getClausePrefix() << "##Name, Str)\n"; 728 OS << "\n"; 729 730 for (const auto &R : DirLang.getClauses()) { 731 Clause C{R}; 732 if (C.getClangClass().empty()) { // NO_CLASS 733 if (C.isImplicit()) { 734 OS << "__IMPLICIT_CLAUSE_NO_CLASS(" << C.getFormattedName() << ", \"" 735 << C.getFormattedName() << "\")\n"; 736 } else { 737 OS << "__CLAUSE_NO_CLASS(" << C.getFormattedName() << ")\n"; 738 } 739 } else { // CLASS 740 if (C.isImplicit()) { 741 OS << "__IMPLICIT_CLAUSE_CLASS(" << C.getFormattedName() << ", \"" 742 << C.getFormattedName() << "\", " << C.getClangClass() << ")\n"; 743 } else { 744 OS << "__CLAUSE(" << C.getFormattedName() << ", " << C.getClangClass() 745 << ")\n"; 746 } 747 } 748 } 749 750 OS << "\n"; 751 OS << "#undef __IMPLICIT_CLAUSE_NO_CLASS\n"; 752 OS << "#undef __IMPLICIT_CLAUSE_CLASS\n"; 753 OS << "#undef __CLAUSE\n"; 754 OS << "#undef CLAUSE_NO_CLASS\n"; 755 OS << "#undef CLAUSE_CLASS\n"; 756 OS << "#undef CLAUSE\n"; 757 } 758 759 // Generate the implemenation for the enumeration in the directive 760 // language. This code can be included in library. 761 void EmitDirectivesBasicImpl(const DirectiveLanguage &DirLang, 762 raw_ostream &OS) { 763 IfDefScope Scope("GEN_DIRECTIVES_IMPL", OS); 764 765 // getDirectiveKind(StringRef Str) 766 GenerateGetKind(DirLang.getDirectives(), OS, "Directive", DirLang, 767 DirLang.getDirectivePrefix(), /*ImplicitAsUnknown=*/false); 768 769 // getDirectiveName(Directive Kind) 770 GenerateGetName(DirLang.getDirectives(), OS, "Directive", DirLang, 771 DirLang.getDirectivePrefix()); 772 773 // getClauseKind(StringRef Str) 774 GenerateGetKind(DirLang.getClauses(), OS, "Clause", DirLang, 775 DirLang.getClausePrefix(), 776 /*ImplicitAsUnknown=*/true); 777 778 // getClauseName(Clause Kind) 779 GenerateGetName(DirLang.getClauses(), OS, "Clause", DirLang, 780 DirLang.getClausePrefix()); 781 782 // get<ClauseVal>Kind(StringRef Str) 783 GenerateGetKindClauseVal(DirLang, OS); 784 785 // isAllowedClauseForDirective(Directive D, Clause C, unsigned Version) 786 GenerateIsAllowedClause(DirLang, OS); 787 } 788 789 // Generate the implemenation section for the enumeration in the directive 790 // language. 791 void EmitDirectivesImpl(RecordKeeper &Records, raw_ostream &OS) { 792 const auto DirLang = DirectiveLanguage{Records}; 793 if (DirLang.HasValidityErrors()) 794 return; 795 796 EmitDirectivesFlangImpl(DirLang, OS); 797 798 GenerateClauseClassMacro(DirLang, OS); 799 800 EmitDirectivesBasicImpl(DirLang, OS); 801 } 802 803 } // namespace llvm 804