1 //===- DirectiveEmitter.cpp - Directive Language Emitter ------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
25 // Simple RAII helper for defining ifdef-undef-endif scopes.
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"; }
39 } // end anonymous namespace
43 // Generate enum class
44 void GenerateEnumClass(const std::vector
<Record
*> &Records
, raw_ostream
&OS
,
45 StringRef Enum
, StringRef Prefix
,
46 const DirectiveLanguage
&DirLang
) {
48 OS
<< "enum class " << Enum
<< " {\n";
49 for (const auto &R
: Records
) {
51 OS
<< " " << Prefix
<< Rec
.getFormattedName() << ",\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
63 if (DirLang
.hasMakeEnumAvailableInNamespace()) {
65 for (const auto &R
: Records
) {
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
) {
81 const auto &ClauseVals
= C
.getClauseVals();
82 if (ClauseVals
.size() <= 0)
85 const auto &EnumName
= C
.getEnumName();
86 if (EnumName
.size() == 0) {
87 PrintError("enumClauseValue field not set in Clause" +
88 C
.getFormattedName() + ".");
93 OS
<< "enum class " << EnumName
<< " {\n";
94 for (const auto &CV
: ClauseVals
) {
96 OS
<< " " << CV
->getName() << "=" << CVal
.getValue() << ",\n";
100 if (DirLang
.hasMakeEnumAvailableInNamespace()) {
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"))
112 (llvm::Twine("llvm::StringRef get") + llvm::Twine(DirLang
.getName()) +
113 llvm::Twine(EnumName
) + llvm::Twine("Name(") +
114 llvm::Twine(EnumName
) + llvm::Twine(");\n"))
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());
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
) {
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
)) {
150 // Check for duplicate between allowedClauses and required
152 if (HasDuplicateClauses(Dir
.getAllowedClauses(), Dir
, Clauses
) ||
153 HasDuplicateClauses(Dir
.getRequiredClauses(), Dir
, Clauses
)) {
157 PrintFatalError("One or more clauses are defined multiple times on"
159 Dir
.getRecordName());
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.");
173 return HasDuplicateClausesInDirectives(getDirectives());
176 // Generate the declaration section for the enumeration in the directive
178 void EmitDirectivesDecl(RecordKeeper
&Records
, raw_ostream
&OS
) {
179 const auto DirLang
= DirectiveLanguage
{Records
};
180 if (DirLang
.HasValidityErrors())
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";
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
216 OS
<< "// Enumeration helper functions\n";
217 OS
<< "Directive get" << DirLang
.getName()
218 << "DirectiveKind(llvm::StringRef Str);\n";
220 OS
<< "llvm::StringRef get" << DirLang
.getName()
221 << "DirectiveName(Directive D);\n";
223 OS
<< "Clause get" << DirLang
.getName()
224 << "ClauseKind(llvm::StringRef Str);\n";
226 OS
<< "llvm::StringRef get" << DirLang
.getName() << "ClauseName(Clause C);\n";
228 OS
<< "/// Return true if \\p C is a valid clause for \\p D in version \\p "
230 OS
<< "bool isAllowedClauseForDirective(Directive D, "
231 << "Clause C, unsigned Version);\n";
233 if (EnumHelperFuncs
.length() > 0) {
234 OS
<< EnumHelperFuncs
;
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
,
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
) {
257 OS
<< " case " << Prefix
<< Rec
.getFormattedName() << ":\n";
259 if (Rec
.getAlternativeName().empty())
262 OS
<< Rec
.getAlternativeName();
265 OS
<< " }\n"; // switch
266 OS
<< " llvm_unreachable(\"Invalid " << DirLang
.getName() << " " << Enum
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.");
284 BaseRecord DefaultRec
{(*DefaultIt
)};
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
) {
293 if (ImplicitAsUnknown
&& R
->getValueAsBit("isImplicit")) {
294 OS
<< " .Case(\"" << Rec
.getName() << "\"," << Prefix
295 << DefaultRec
.getFormattedName() << ")\n";
297 OS
<< " .Case(\"" << Rec
.getName() << "\"," << Prefix
298 << Rec
.getFormattedName() << ")\n";
301 OS
<< " .Default(" << Prefix
<< DefaultRec
.getFormattedName() << ");\n";
305 // Generate function implementation for get<ClauseVal>Kind(StringRef Str)
306 void GenerateGetKindClauseVal(const DirectiveLanguage
&DirLang
,
308 for (const auto &R
: DirLang
.getClauses()) {
310 const auto &ClauseVals
= C
.getClauseVals();
311 if (ClauseVals
.size() <= 0)
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.");
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() + ".");
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
) {
338 OS
<< " .Case(\"" << CVal
.getFormattedName() << "\"," << CV
->getName()
341 OS
<< " .Default(" << DefaultName
<< ");\n";
345 OS
<< "llvm::StringRef llvm::" << DirLang
.getCppNamespace() << "::get"
346 << DirLang
.getName() << EnumName
347 << "Name(llvm::" << DirLang
.getCppNamespace() << "::" << EnumName
349 OS
<< " switch (x) {\n";
350 for (const auto &CV
: ClauseVals
) {
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";
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
375 OS
<< " return " << VerClause
.getMinVersion()
376 << " <= Version && " << VerClause
.getMaxVersion() << " >= Version;\n";
381 // Generate the isAllowedClauseForDirective function implementation.
382 void GenerateIsAllowedClause(const DirectiveLanguage
&DirLang
,
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()) {
398 OS
<< " case " << DirLang
.getDirectivePrefix() << Dir
.getFormattedName()
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";
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
);
423 OS
<< " return false;\n";
424 OS
<< " }\n"; // End of clauses switch
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
) {
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";
453 // Generate an enum set for the 4 kinds of clauses linked to a directive.
454 void GenerateDirectiveClauseSets(const DirectiveLanguage
&DirLang
,
457 IfDefScope
Scope("GEN_FLANG_DIRECTIVE_CLAUSE_SETS", OS
);
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()) {
472 OS
<< " // Sets for " << Dir
.getName() << "\n";
474 GenerateClauseSet(Dir
.getAllowedClauses(), OS
, "allowedClauses_", Dir
,
476 GenerateClauseSet(Dir
.getAllowedOnceClauses(), OS
, "allowedOnceClauses_",
478 GenerateClauseSet(Dir
.getAllowedExclusiveClauses(), OS
,
479 "allowedExclusiveClauses_", Dir
, DirLang
);
480 GenerateClauseSet(Dir
.getRequiredClauses(), OS
, "requiredClauses_", Dir
,
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
,
497 IfDefScope
Scope("GEN_FLANG_DIRECTIVE_CLAUSE_MAP", OS
);
502 for (const auto &D
: DirLang
.getDirectives()) {
504 OS
<< " {llvm::" << DirLang
.getCppNamespace()
505 << "::Directive::" << DirLang
.getDirectivePrefix()
506 << Dir
.getFormattedName() << ",\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";
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
,
532 IfDefScope
Scope("GEN_FLANG_CLAUSE_PARSER_CLASSES", OS
);
536 for (const auto &C
: DirLang
.getClauses()) {
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() << ">";
547 OS
<< Clause
.getFlangClass();
550 OS
<< "EMPTY_CLASS(" << Clause
.getFormattedParserClassName();
556 // Generate a list of the different clause classes for Flang.
557 void GenerateFlangClauseParserClassList(const DirectiveLanguage
&DirLang
,
560 IfDefScope
Scope("GEN_FLANG_CLAUSE_PARSER_CLASSES_LIST", OS
);
563 llvm::interleaveComma(DirLang
.getClauses(), OS
, [&](Record
*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
,
573 IfDefScope
Scope("GEN_FLANG_DUMP_PARSE_TREE_CLAUSES", OS
);
576 for (const auto &C
: DirLang
.getClauses()) {
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
,
588 IfDefScope
Scope("GEN_FLANG_CLAUSE_UNPARSE", OS
);
592 for (const auto &C
: DirLang
.getClauses()) {
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";
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";
611 OS
<< " Walk(x.v);\n";
613 OS
<< " Put(\"" << Clause
.getDefaultValue() << "\");\n";
614 OS
<< " Put(\")\");\n";
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";
624 OS
<< " Walk(x.v);\n";
625 OS
<< " Put(\")\");\n";
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
,
640 IfDefScope
Scope("GEN_FLANG_CLAUSE_CHECK_ENTER", OS
);
643 for (const auto &C
: DirLang
.getClauses()) {
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
,
655 IfDefScope
Scope("GEN_FLANG_CLAUSE_PARSER_KIND_MAP", OS
);
658 for (const auto &C
: DirLang
.getClauses()) {
660 OS
<< "if constexpr (std::is_same_v<A, parser::"
661 << DirLang
.getFlangClauseBaseClass()
662 << "::" << Clause
.getFormattedParserClassName();
664 OS
<< " return llvm::" << DirLang
.getCppNamespace()
665 << "::Clause::" << DirLang
.getClausePrefix() << Clause
.getFormattedName()
669 OS
<< "llvm_unreachable(\"Invalid " << DirLang
.getName()
670 << " Parser clause\");\n";
673 // Generate the implementation section for the enumeration in the directive
675 void EmitDirectivesFlangImpl(const DirectiveLanguage
&DirLang
,
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
,
697 // Generate macros style information for legacy code in clang
698 IfDefScope
Scope("GEN_CLANG_CLAUSE_CLASS", OS
);
702 OS
<< "#ifndef CLAUSE\n";
703 OS
<< "#define CLAUSE(Enum, Str, Implicit)\n";
705 OS
<< "#ifndef CLAUSE_CLASS\n";
706 OS
<< "#define CLAUSE_CLASS(Enum, Str, Class)\n";
708 OS
<< "#ifndef CLAUSE_NO_CLASS\n";
709 OS
<< "#define CLAUSE_NO_CLASS(Enum, Str)\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";
732 for (const auto &R
: DirLang
.getClauses()) {
734 if (C
.getClangClass().empty()) { // NO_CLASS
735 if (C
.isImplicit()) {
736 OS
<< "__IMPLICIT_CLAUSE_NO_CLASS(" << C
.getFormattedName() << ", \""
737 << C
.getFormattedName() << "\")\n";
739 OS
<< "__CLAUSE_NO_CLASS(" << C
.getFormattedName() << ")\n";
742 if (C
.isImplicit()) {
743 OS
<< "__IMPLICIT_CLAUSE_CLASS(" << C
.getFormattedName() << ", \""
744 << C
.getFormattedName() << "\", " << C
.getClangClass() << ")\n";
746 OS
<< "__CLAUSE(" << C
.getFormattedName() << ", " << C
.getClangClass()
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
,
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
793 void EmitDirectivesImpl(RecordKeeper
&Records
, raw_ostream
&OS
) {
794 const auto DirLang
= DirectiveLanguage
{Records
};
795 if (DirLang
.HasValidityErrors())
798 EmitDirectivesFlangImpl(DirLang
, OS
);
800 GenerateClauseClassMacro(DirLang
, OS
);
802 EmitDirectivesBasicImpl(DirLang
, OS
);