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