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/ADT/StringSwitch.h"
19 #include "llvm/TableGen/Error.h"
20 #include "llvm/TableGen/Record.h"
21 #include "llvm/TableGen/TableGenBackend.h"
22 
23 using namespace llvm;
24 
25 namespace {
26 // Simple RAII helper for defining ifdef-undef-endif scopes.
27 class IfDefScope {
28 public:
29   IfDefScope(StringRef Name, raw_ostream &OS) : Name(Name), OS(OS) {
30     OS << "#ifdef " << Name << "\n"
31        << "#undef " << Name << "\n";
32   }
33 
34   ~IfDefScope() { OS << "\n#endif // " << Name << "\n\n"; }
35 
36 private:
37   StringRef Name;
38   raw_ostream &OS;
39 };
40 } // namespace
41 
42 // Generate enum class
43 static void GenerateEnumClass(const std::vector<Record *> &Records,
44                               raw_ostream &OS, 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 static void GenerateEnumClauseVal(const std::vector<Record *> &Records,
76                                   raw_ostream &OS,
77                                   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;
84 
85     const auto &EnumName = C.getEnumName();
86     if (EnumName.size() == 0) {
87       PrintError("enumClauseValue field not set in Clause" +
88                  C.getFormattedName() + ".");
89       return;
90     }
91 
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";
97     }
98     OS << "};\n";
99 
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";
106       }
107       EnumHelperFuncs += (llvm::Twine(EnumName) + llvm::Twine(" get") +
108                           llvm::Twine(EnumName) + llvm::Twine("(StringRef);\n"))
109                              .str();
110 
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();
116     }
117   }
118 }
119 
120 static 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;
131     }
132   }
133   return HasError;
134 }
135 
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 static bool
140 HasDuplicateClausesInDirectives(const std::vector<Record *> &Directives) {
141   bool HasDuplicate = false;
142   for (const auto &D : Directives) {
143     Directive Dir{D};
144     llvm::StringSet<> Clauses;
145     // Check for duplicates in the three allowed lists.
146     if (HasDuplicateClauses(Dir.getAllowedClauses(), Dir, Clauses) ||
147         HasDuplicateClauses(Dir.getAllowedOnceClauses(), Dir, Clauses) ||
148         HasDuplicateClauses(Dir.getAllowedExclusiveClauses(), Dir, Clauses)) {
149       HasDuplicate = true;
150     }
151     // Check for duplicate between allowedClauses and required
152     Clauses.clear();
153     if (HasDuplicateClauses(Dir.getAllowedClauses(), Dir, Clauses) ||
154         HasDuplicateClauses(Dir.getRequiredClauses(), Dir, Clauses)) {
155       HasDuplicate = true;
156     }
157     if (HasDuplicate)
158       PrintFatalError("One or more clauses are defined multiple times on"
159                       " directive " +
160                       Dir.getRecordName());
161   }
162 
163   return HasDuplicate;
164 }
165 
166 // Check consitency of records. Return true if an error has been detected.
167 // Return false if the records are valid.
168 bool DirectiveLanguage::HasValidityErrors() const {
169   if (getDirectiveLanguages().size() != 1) {
170     PrintFatalError("A single definition of DirectiveLanguage is needed.");
171     return true;
172   }
173 
174   return HasDuplicateClausesInDirectives(getDirectives());
175 }
176 
177 // Generate the declaration section for the enumeration in the directive
178 // language
179 static void EmitDirectivesDecl(RecordKeeper &Records, raw_ostream &OS) {
180   const auto DirLang = DirectiveLanguage{Records};
181   if (DirLang.HasValidityErrors())
182     return;
183 
184   OS << "#ifndef LLVM_" << DirLang.getName() << "_INC\n";
185   OS << "#define LLVM_" << DirLang.getName() << "_INC\n";
186 
187   if (DirLang.hasEnableBitmaskEnumInNamespace())
188     OS << "\n#include \"llvm/ADT/BitmaskEnum.h\"\n";
189 
190   OS << "\n";
191   OS << "namespace llvm {\n";
192   OS << "class StringRef;\n";
193 
194   // Open namespaces defined in the directive language
195   llvm::SmallVector<StringRef, 2> Namespaces;
196   llvm::SplitString(DirLang.getCppNamespace(), Namespaces, "::");
197   for (auto Ns : Namespaces)
198     OS << "namespace " << Ns << " {\n";
199 
200   if (DirLang.hasEnableBitmaskEnumInNamespace())
201     OS << "\nLLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();\n";
202 
203   // Emit Directive enumeration
204   GenerateEnumClass(DirLang.getDirectives(), OS, "Directive",
205                     DirLang.getDirectivePrefix(), DirLang);
206 
207   // Emit Clause enumeration
208   GenerateEnumClass(DirLang.getClauses(), OS, "Clause",
209                     DirLang.getClausePrefix(), DirLang);
210 
211   // Emit ClauseVal enumeration
212   std::string EnumHelperFuncs;
213   GenerateEnumClauseVal(DirLang.getClauses(), OS, DirLang, EnumHelperFuncs);
214 
215   // Generic function signatures
216   OS << "\n";
217   OS << "// Enumeration helper functions\n";
218   OS << "Directive get" << DirLang.getName()
219      << "DirectiveKind(llvm::StringRef Str);\n";
220   OS << "\n";
221   OS << "llvm::StringRef get" << DirLang.getName()
222      << "DirectiveName(Directive D);\n";
223   OS << "\n";
224   OS << "Clause get" << DirLang.getName()
225      << "ClauseKind(llvm::StringRef Str);\n";
226   OS << "\n";
227   OS << "llvm::StringRef get" << DirLang.getName() << "ClauseName(Clause C);\n";
228   OS << "\n";
229   OS << "/// Return true if \\p C is a valid clause for \\p D in version \\p "
230      << "Version.\n";
231   OS << "bool isAllowedClauseForDirective(Directive D, "
232      << "Clause C, unsigned Version);\n";
233   OS << "\n";
234   if (EnumHelperFuncs.length() > 0) {
235     OS << EnumHelperFuncs;
236     OS << "\n";
237   }
238 
239   // Closing namespaces
240   for (auto Ns : llvm::reverse(Namespaces))
241     OS << "} // namespace " << Ns << "\n";
242 
243   OS << "} // namespace llvm\n";
244 
245   OS << "#endif // LLVM_" << DirLang.getName() << "_INC\n";
246 }
247 
248 // Generate function implementation for get<Enum>Name(StringRef Str)
249 static void GenerateGetName(const std::vector<Record *> &Records,
250                             raw_ostream &OS, StringRef Enum,
251                             const DirectiveLanguage &DirLang,
252                             StringRef Prefix) {
253   OS << "\n";
254   OS << "llvm::StringRef llvm::" << DirLang.getCppNamespace() << "::get"
255      << DirLang.getName() << Enum << "Name(" << Enum << " Kind) {\n";
256   OS << "  switch (Kind) {\n";
257   for (const auto &R : Records) {
258     BaseRecord Rec{R};
259     OS << "    case " << Prefix << Rec.getFormattedName() << ":\n";
260     OS << "      return \"";
261     if (Rec.getAlternativeName().empty())
262       OS << Rec.getName();
263     else
264       OS << Rec.getAlternativeName();
265     OS << "\";\n";
266   }
267   OS << "  }\n"; // switch
268   OS << "  llvm_unreachable(\"Invalid " << DirLang.getName() << " " << Enum
269      << " kind\");\n";
270   OS << "}\n";
271 }
272 
273 // Generate function implementation for get<Enum>Kind(StringRef Str)
274 static void GenerateGetKind(const std::vector<Record *> &Records,
275                             raw_ostream &OS, StringRef Enum,
276                             const DirectiveLanguage &DirLang, StringRef Prefix,
277                             bool ImplicitAsUnknown) {
278 
279   auto DefaultIt = llvm::find_if(
280       Records, [](Record *R) { return R->getValueAsBit("isDefault") == true; });
281 
282   if (DefaultIt == Records.end()) {
283     PrintError("At least one " + Enum + " must be defined as default.");
284     return;
285   }
286 
287   BaseRecord DefaultRec{(*DefaultIt)};
288 
289   OS << "\n";
290   OS << Enum << " llvm::" << DirLang.getCppNamespace() << "::get"
291      << DirLang.getName() << Enum << "Kind(llvm::StringRef Str) {\n";
292   OS << "  return llvm::StringSwitch<" << Enum << ">(Str)\n";
293 
294   for (const auto &R : Records) {
295     BaseRecord Rec{R};
296     if (ImplicitAsUnknown && R->getValueAsBit("isImplicit")) {
297       OS << "    .Case(\"" << Rec.getName() << "\"," << Prefix
298          << DefaultRec.getFormattedName() << ")\n";
299     } else {
300       OS << "    .Case(\"" << Rec.getName() << "\"," << Prefix
301          << Rec.getFormattedName() << ")\n";
302     }
303   }
304   OS << "    .Default(" << Prefix << DefaultRec.getFormattedName() << ");\n";
305   OS << "}\n";
306 }
307 
308 // Generate function implementation for get<ClauseVal>Kind(StringRef Str)
309 static void GenerateGetKindClauseVal(const DirectiveLanguage &DirLang,
310                                      raw_ostream &OS) {
311   for (const auto &R : DirLang.getClauses()) {
312     Clause C{R};
313     const auto &ClauseVals = C.getClauseVals();
314     if (ClauseVals.size() <= 0)
315       continue;
316 
317     auto DefaultIt = llvm::find_if(ClauseVals, [](Record *CV) {
318       return CV->getValueAsBit("isDefault") == true;
319     });
320 
321     if (DefaultIt == ClauseVals.end()) {
322       PrintError("At least one val in Clause " + C.getFormattedName() +
323                  " must be defined as default.");
324       return;
325     }
326     const auto DefaultName = (*DefaultIt)->getName();
327 
328     const auto &EnumName = C.getEnumName();
329     if (EnumName.size() == 0) {
330       PrintError("enumClauseValue field not set in Clause" +
331                  C.getFormattedName() + ".");
332       return;
333     }
334 
335     OS << "\n";
336     OS << EnumName << " llvm::" << DirLang.getCppNamespace() << "::get"
337        << EnumName << "(llvm::StringRef Str) {\n";
338     OS << "  return llvm::StringSwitch<" << EnumName << ">(Str)\n";
339     for (const auto &CV : ClauseVals) {
340       ClauseVal CVal{CV};
341       OS << "    .Case(\"" << CVal.getFormattedName() << "\"," << CV->getName()
342          << ")\n";
343     }
344     OS << "    .Default(" << DefaultName << ");\n";
345     OS << "}\n";
346 
347     OS << "\n";
348     OS << "llvm::StringRef llvm::" << DirLang.getCppNamespace() << "::get"
349        << DirLang.getName() << EnumName
350        << "Name(llvm::" << DirLang.getCppNamespace() << "::" << EnumName
351        << " x) {\n";
352     OS << "  switch (x) {\n";
353     for (const auto &CV : ClauseVals) {
354       ClauseVal CVal{CV};
355       OS << "    case " << CV->getName() << ":\n";
356       OS << "      return \"" << CVal.getFormattedName() << "\";\n";
357     }
358     OS << "  }\n"; // switch
359     OS << "  llvm_unreachable(\"Invalid " << DirLang.getName() << " "
360        << EnumName << " kind\");\n";
361     OS << "}\n";
362   }
363 }
364 
365 static void
366 GenerateCaseForVersionedClauses(const std::vector<Record *> &Clauses,
367                                 raw_ostream &OS, StringRef DirectiveName,
368                                 const DirectiveLanguage &DirLang,
369                                 llvm::StringSet<> &Cases) {
370   for (const auto &C : Clauses) {
371     VersionedClause VerClause{C};
372 
373     const auto ClauseFormattedName = VerClause.getClause().getFormattedName();
374 
375     if (Cases.insert(ClauseFormattedName).second) {
376       OS << "        case " << DirLang.getClausePrefix() << ClauseFormattedName
377          << ":\n";
378       OS << "          return " << VerClause.getMinVersion()
379          << " <= Version && " << VerClause.getMaxVersion() << " >= Version;\n";
380     }
381   }
382 }
383 
384 // Generate the isAllowedClauseForDirective function implementation.
385 static void GenerateIsAllowedClause(const DirectiveLanguage &DirLang,
386                                     raw_ostream &OS) {
387   OS << "\n";
388   OS << "bool llvm::" << DirLang.getCppNamespace()
389      << "::isAllowedClauseForDirective("
390      << "Directive D, Clause C, unsigned Version) {\n";
391   OS << "  assert(unsigned(D) <= llvm::" << DirLang.getCppNamespace()
392      << "::Directive_enumSize);\n";
393   OS << "  assert(unsigned(C) <= llvm::" << DirLang.getCppNamespace()
394      << "::Clause_enumSize);\n";
395 
396   OS << "  switch (D) {\n";
397 
398   for (const auto &D : DirLang.getDirectives()) {
399     Directive Dir{D};
400 
401     OS << "    case " << DirLang.getDirectivePrefix() << Dir.getFormattedName()
402        << ":\n";
403     if (Dir.getAllowedClauses().size() == 0 &&
404         Dir.getAllowedOnceClauses().size() == 0 &&
405         Dir.getAllowedExclusiveClauses().size() == 0 &&
406         Dir.getRequiredClauses().size() == 0) {
407       OS << "      return false;\n";
408     } else {
409       OS << "      switch (C) {\n";
410 
411       llvm::StringSet<> Cases;
412 
413       GenerateCaseForVersionedClauses(Dir.getAllowedClauses(), OS,
414                                       Dir.getName(), DirLang, Cases);
415 
416       GenerateCaseForVersionedClauses(Dir.getAllowedOnceClauses(), OS,
417                                       Dir.getName(), DirLang, Cases);
418 
419       GenerateCaseForVersionedClauses(Dir.getAllowedExclusiveClauses(), OS,
420                                       Dir.getName(), DirLang, Cases);
421 
422       GenerateCaseForVersionedClauses(Dir.getRequiredClauses(), OS,
423                                       Dir.getName(), DirLang, Cases);
424 
425       OS << "        default:\n";
426       OS << "          return false;\n";
427       OS << "      }\n"; // End of clauses switch
428     }
429     OS << "      break;\n";
430   }
431 
432   OS << "  }\n"; // End of directives switch
433   OS << "  llvm_unreachable(\"Invalid " << DirLang.getName()
434      << " Directive kind\");\n";
435   OS << "}\n"; // End of function isAllowedClauseForDirective
436 }
437 
438 // Generate a simple enum set with the give clauses.
439 static void GenerateClauseSet(const std::vector<Record *> &Clauses,
440                               raw_ostream &OS, StringRef ClauseSetPrefix,
441                               Directive &Dir,
442                               const DirectiveLanguage &DirLang) {
443 
444   OS << "\n";
445   OS << "  static " << DirLang.getClauseEnumSetClass() << " " << ClauseSetPrefix
446      << DirLang.getDirectivePrefix() << Dir.getFormattedName() << " {\n";
447 
448   for (const auto &C : Clauses) {
449     VersionedClause VerClause{C};
450     OS << "    llvm::" << DirLang.getCppNamespace()
451        << "::Clause::" << DirLang.getClausePrefix()
452        << VerClause.getClause().getFormattedName() << ",\n";
453   }
454   OS << "  };\n";
455 }
456 
457 // Generate an enum set for the 4 kinds of clauses linked to a directive.
458 static void GenerateDirectiveClauseSets(const DirectiveLanguage &DirLang,
459                                         raw_ostream &OS) {
460 
461   IfDefScope Scope("GEN_FLANG_DIRECTIVE_CLAUSE_SETS", OS);
462 
463   OS << "\n";
464   OS << "namespace llvm {\n";
465 
466   // Open namespaces defined in the directive language.
467   llvm::SmallVector<StringRef, 2> Namespaces;
468   llvm::SplitString(DirLang.getCppNamespace(), Namespaces, "::");
469   for (auto Ns : Namespaces)
470     OS << "namespace " << Ns << " {\n";
471 
472   for (const auto &D : DirLang.getDirectives()) {
473     Directive Dir{D};
474 
475     OS << "\n";
476     OS << "  // Sets for " << Dir.getName() << "\n";
477 
478     GenerateClauseSet(Dir.getAllowedClauses(), OS, "allowedClauses_", Dir,
479                       DirLang);
480     GenerateClauseSet(Dir.getAllowedOnceClauses(), OS, "allowedOnceClauses_",
481                       Dir, DirLang);
482     GenerateClauseSet(Dir.getAllowedExclusiveClauses(), OS,
483                       "allowedExclusiveClauses_", Dir, DirLang);
484     GenerateClauseSet(Dir.getRequiredClauses(), OS, "requiredClauses_", Dir,
485                       DirLang);
486   }
487 
488   // Closing namespaces
489   for (auto Ns : llvm::reverse(Namespaces))
490     OS << "} // namespace " << Ns << "\n";
491 
492   OS << "} // namespace llvm\n";
493 }
494 
495 // Generate a map of directive (key) with DirectiveClauses struct as values.
496 // The struct holds the 4 sets of enumeration for the 4 kinds of clauses
497 // allowances (allowed, allowed once, allowed exclusive and required).
498 static void GenerateDirectiveClauseMap(const DirectiveLanguage &DirLang,
499                                        raw_ostream &OS) {
500 
501   IfDefScope Scope("GEN_FLANG_DIRECTIVE_CLAUSE_MAP", OS);
502 
503   OS << "\n";
504   OS << "{\n";
505 
506   for (const auto &D : DirLang.getDirectives()) {
507     Directive Dir{D};
508     OS << "  {llvm::" << DirLang.getCppNamespace()
509        << "::Directive::" << DirLang.getDirectivePrefix()
510        << Dir.getFormattedName() << ",\n";
511     OS << "    {\n";
512     OS << "      llvm::" << DirLang.getCppNamespace() << "::allowedClauses_"
513        << DirLang.getDirectivePrefix() << Dir.getFormattedName() << ",\n";
514     OS << "      llvm::" << DirLang.getCppNamespace() << "::allowedOnceClauses_"
515        << DirLang.getDirectivePrefix() << Dir.getFormattedName() << ",\n";
516     OS << "      llvm::" << DirLang.getCppNamespace()
517        << "::allowedExclusiveClauses_" << DirLang.getDirectivePrefix()
518        << Dir.getFormattedName() << ",\n";
519     OS << "      llvm::" << DirLang.getCppNamespace() << "::requiredClauses_"
520        << DirLang.getDirectivePrefix() << Dir.getFormattedName() << ",\n";
521     OS << "    }\n";
522     OS << "  },\n";
523   }
524 
525   OS << "}\n";
526 }
527 
528 // Generate classes entry for Flang clauses in the Flang parse-tree
529 // If the clause as a non-generic class, no entry is generated.
530 // If the clause does not hold a value, an EMPTY_CLASS is used.
531 // If the clause class is generic then a WRAPPER_CLASS is used. When the value
532 // is optional, the value class is wrapped into a std::optional.
533 static void GenerateFlangClauseParserClass(const DirectiveLanguage &DirLang,
534                                            raw_ostream &OS) {
535 
536   IfDefScope Scope("GEN_FLANG_CLAUSE_PARSER_CLASSES", OS);
537 
538   OS << "\n";
539 
540   for (const auto &C : DirLang.getClauses()) {
541     Clause Clause{C};
542     if (!Clause.getFlangClass().empty()) {
543       OS << "WRAPPER_CLASS(" << Clause.getFormattedParserClassName() << ", ";
544       if (Clause.isValueOptional() && Clause.isValueList()) {
545         OS << "std::optional<std::list<" << Clause.getFlangClass() << ">>";
546       } else if (Clause.isValueOptional()) {
547         OS << "std::optional<" << Clause.getFlangClass() << ">";
548       } else if (Clause.isValueList()) {
549         OS << "std::list<" << Clause.getFlangClass() << ">";
550       } else {
551         OS << Clause.getFlangClass();
552       }
553     } else {
554       OS << "EMPTY_CLASS(" << Clause.getFormattedParserClassName();
555     }
556     OS << ");\n";
557   }
558 }
559 
560 // Generate a list of the different clause classes for Flang.
561 static void GenerateFlangClauseParserClassList(const DirectiveLanguage &DirLang,
562                                                raw_ostream &OS) {
563 
564   IfDefScope Scope("GEN_FLANG_CLAUSE_PARSER_CLASSES_LIST", OS);
565 
566   OS << "\n";
567   llvm::interleaveComma(DirLang.getClauses(), OS, [&](Record *C) {
568     Clause Clause{C};
569     OS << Clause.getFormattedParserClassName() << "\n";
570   });
571 }
572 
573 // Generate dump node list for the clauses holding a generic class name.
574 static void GenerateFlangClauseDump(const DirectiveLanguage &DirLang,
575                                     raw_ostream &OS) {
576 
577   IfDefScope Scope("GEN_FLANG_DUMP_PARSE_TREE_CLAUSES", OS);
578 
579   OS << "\n";
580   for (const auto &C : DirLang.getClauses()) {
581     Clause Clause{C};
582     OS << "NODE(" << DirLang.getFlangClauseBaseClass() << ", "
583        << Clause.getFormattedParserClassName() << ")\n";
584   }
585 }
586 
587 // Generate Unparse functions for clauses classes in the Flang parse-tree
588 // If the clause is a non-generic class, no entry is generated.
589 static void GenerateFlangClauseUnparse(const DirectiveLanguage &DirLang,
590                                        raw_ostream &OS) {
591 
592   IfDefScope Scope("GEN_FLANG_CLAUSE_UNPARSE", OS);
593 
594   OS << "\n";
595 
596   for (const auto &C : DirLang.getClauses()) {
597     Clause Clause{C};
598     if (!Clause.getFlangClass().empty()) {
599       if (Clause.isValueOptional() && Clause.getDefaultValue().empty()) {
600         OS << "void Unparse(const " << DirLang.getFlangClauseBaseClass()
601            << "::" << Clause.getFormattedParserClassName() << " &x) {\n";
602         OS << "  Word(\"" << Clause.getName().upper() << "\");\n";
603 
604         OS << "  Walk(\"(\", x.v, \")\");\n";
605         OS << "}\n";
606       } else if (Clause.isValueOptional()) {
607         OS << "void Unparse(const " << DirLang.getFlangClauseBaseClass()
608            << "::" << Clause.getFormattedParserClassName() << " &x) {\n";
609         OS << "  Word(\"" << Clause.getName().upper() << "\");\n";
610         OS << "  Put(\"(\");\n";
611         OS << "  if (x.v.has_value())\n";
612         if (Clause.isValueList())
613           OS << "    Walk(x.v, \",\");\n";
614         else
615           OS << "    Walk(x.v);\n";
616         OS << "  else\n";
617         OS << "    Put(\"" << Clause.getDefaultValue() << "\");\n";
618         OS << "  Put(\")\");\n";
619         OS << "}\n";
620       } else {
621         OS << "void Unparse(const " << DirLang.getFlangClauseBaseClass()
622            << "::" << Clause.getFormattedParserClassName() << " &x) {\n";
623         OS << "  Word(\"" << Clause.getName().upper() << "\");\n";
624         OS << "  Put(\"(\");\n";
625         if (Clause.isValueList())
626           OS << "  Walk(x.v, \",\");\n";
627         else
628           OS << "  Walk(x.v);\n";
629         OS << "  Put(\")\");\n";
630         OS << "}\n";
631       }
632     } else {
633       OS << "void Before(const " << DirLang.getFlangClauseBaseClass()
634          << "::" << Clause.getFormattedParserClassName() << " &) { Word(\""
635          << Clause.getName().upper() << "\"); }\n";
636     }
637   }
638 }
639 
640 // Generate check in the Enter functions for clauses classes.
641 static void GenerateFlangClauseCheckPrototypes(const DirectiveLanguage &DirLang,
642                                                raw_ostream &OS) {
643 
644   IfDefScope Scope("GEN_FLANG_CLAUSE_CHECK_ENTER", OS);
645 
646   OS << "\n";
647   for (const auto &C : DirLang.getClauses()) {
648     Clause Clause{C};
649     OS << "void Enter(const parser::" << DirLang.getFlangClauseBaseClass()
650        << "::" << Clause.getFormattedParserClassName() << " &);\n";
651   }
652 }
653 
654 // Generate the mapping for clauses between the parser class and the
655 // corresponding clause Kind
656 static void GenerateFlangClauseParserKindMap(const DirectiveLanguage &DirLang,
657                                              raw_ostream &OS) {
658 
659   IfDefScope Scope("GEN_FLANG_CLAUSE_PARSER_KIND_MAP", OS);
660 
661   OS << "\n";
662   for (const auto &C : DirLang.getClauses()) {
663     Clause Clause{C};
664     OS << "if constexpr (std::is_same_v<A, parser::"
665        << DirLang.getFlangClauseBaseClass()
666        << "::" << Clause.getFormattedParserClassName();
667     OS << ">)\n";
668     OS << "  return llvm::" << DirLang.getCppNamespace()
669        << "::Clause::" << DirLang.getClausePrefix() << Clause.getFormattedName()
670        << ";\n";
671   }
672 
673   OS << "llvm_unreachable(\"Invalid " << DirLang.getName()
674      << " Parser clause\");\n";
675 }
676 
677 static bool compareClauseName(Record *R1, Record *R2) {
678   Clause C1{R1};
679   Clause C2{R2};
680   return (C1.getName() > C2.getName());
681 }
682 
683 // Generate the parser for the clauses.
684 static void GenerateFlangClausesParser(const DirectiveLanguage &DirLang,
685                                        raw_ostream &OS) {
686   std::vector<Record *> Clauses = DirLang.getClauses();
687   // Sort clauses in reverse alphabetical order so with clauses with same
688   // beginning, the longer option is tried before.
689   llvm::sort(Clauses, compareClauseName);
690   IfDefScope Scope("GEN_FLANG_CLAUSES_PARSER", OS);
691   OS << "\n";
692   unsigned index = 0;
693   unsigned lastClauseIndex = DirLang.getClauses().size() - 1;
694   OS << "TYPE_PARSER(\n";
695   for (const auto &C : Clauses) {
696     Clause Clause{C};
697     if (Clause.getAliases().empty()) {
698       OS << "  \"" << Clause.getName() << "\"";
699     } else {
700       OS << "  ("
701          << "\"" << Clause.getName() << "\"_tok";
702       for (StringRef alias : Clause.getAliases()) {
703         OS << " || \"" << alias << "\"_tok";
704       }
705       OS << ")";
706     }
707 
708     OS << " >> construct<" << DirLang.getFlangClauseBaseClass()
709        << ">(construct<" << DirLang.getFlangClauseBaseClass()
710        << "::" << Clause.getFormattedParserClassName() << ">(";
711     if (Clause.getFlangClass().empty()) {
712       OS << "))";
713       if (index != lastClauseIndex)
714         OS << " ||";
715       OS << "\n";
716       ++index;
717       continue;
718     }
719 
720     if (Clause.isValueOptional())
721       OS << "maybe(";
722     OS << "parenthesized(";
723     if (Clause.isValueList())
724       OS << "nonemptyList(";
725 
726     if (!Clause.getPrefix().empty())
727       OS << "\"" << Clause.getPrefix() << ":\" >> ";
728 
729     // The common Flang parser are used directly. Their name is identical to
730     // the Flang class with first letter as lowercase. If the Flang class is
731     // not a common class, we assume there is a specific Parser<>{} with the
732     // Flang class name provided.
733     llvm::SmallString<128> Scratch;
734     StringRef Parser =
735         llvm::StringSwitch<StringRef>(Clause.getFlangClass())
736             .Case("Name", "name")
737             .Case("ScalarIntConstantExpr", "scalarIntConstantExpr")
738             .Case("ScalarIntExpr", "scalarIntExpr")
739             .Case("ScalarLogicalExpr", "scalarLogicalExpr")
740             .Default(("Parser<" + Clause.getFlangClass() + ">{}")
741                          .toStringRef(Scratch));
742     OS << Parser;
743     if (!Clause.getPrefix().empty() && Clause.isPrefixOptional())
744       OS << " || " << Parser;
745     if (Clause.isValueList()) // close nonemptyList(.
746       OS << ")";
747     OS << ")"; // close parenthesized(.
748 
749     if (Clause.isValueOptional()) // close maybe(.
750       OS << ")";
751     OS << "))";
752     if (index != lastClauseIndex)
753       OS << " ||";
754     OS << "\n";
755     ++index;
756   }
757   OS << ")\n";
758 }
759 
760 // Generate the implementation section for the enumeration in the directive
761 // language
762 static void EmitDirectivesFlangImpl(const DirectiveLanguage &DirLang,
763                                     raw_ostream &OS) {
764 
765   GenerateDirectiveClauseSets(DirLang, OS);
766 
767   GenerateDirectiveClauseMap(DirLang, OS);
768 
769   GenerateFlangClauseParserClass(DirLang, OS);
770 
771   GenerateFlangClauseParserClassList(DirLang, OS);
772 
773   GenerateFlangClauseDump(DirLang, OS);
774 
775   GenerateFlangClauseUnparse(DirLang, OS);
776 
777   GenerateFlangClauseCheckPrototypes(DirLang, OS);
778 
779   GenerateFlangClauseParserKindMap(DirLang, OS);
780 
781   GenerateFlangClausesParser(DirLang, OS);
782 }
783 
784 static void GenerateClauseClassMacro(const DirectiveLanguage &DirLang,
785                                      raw_ostream &OS) {
786   // Generate macros style information for legacy code in clang
787   IfDefScope Scope("GEN_CLANG_CLAUSE_CLASS", OS);
788 
789   OS << "\n";
790 
791   OS << "#ifndef CLAUSE\n";
792   OS << "#define CLAUSE(Enum, Str, Implicit)\n";
793   OS << "#endif\n";
794   OS << "#ifndef CLAUSE_CLASS\n";
795   OS << "#define CLAUSE_CLASS(Enum, Str, Class)\n";
796   OS << "#endif\n";
797   OS << "#ifndef CLAUSE_NO_CLASS\n";
798   OS << "#define CLAUSE_NO_CLASS(Enum, Str)\n";
799   OS << "#endif\n";
800   OS << "\n";
801   OS << "#define __CLAUSE(Name, Class)                      \\\n";
802   OS << "  CLAUSE(" << DirLang.getClausePrefix()
803      << "##Name, #Name, /* Implicit */ false) \\\n";
804   OS << "  CLAUSE_CLASS(" << DirLang.getClausePrefix()
805      << "##Name, #Name, Class)\n";
806   OS << "#define __CLAUSE_NO_CLASS(Name)                    \\\n";
807   OS << "  CLAUSE(" << DirLang.getClausePrefix()
808      << "##Name, #Name, /* Implicit */ false) \\\n";
809   OS << "  CLAUSE_NO_CLASS(" << DirLang.getClausePrefix() << "##Name, #Name)\n";
810   OS << "#define __IMPLICIT_CLAUSE_CLASS(Name, Str, Class)  \\\n";
811   OS << "  CLAUSE(" << DirLang.getClausePrefix()
812      << "##Name, Str, /* Implicit */ true)    \\\n";
813   OS << "  CLAUSE_CLASS(" << DirLang.getClausePrefix()
814      << "##Name, Str, Class)\n";
815   OS << "#define __IMPLICIT_CLAUSE_NO_CLASS(Name, Str)      \\\n";
816   OS << "  CLAUSE(" << DirLang.getClausePrefix()
817      << "##Name, Str, /* Implicit */ true)    \\\n";
818   OS << "  CLAUSE_NO_CLASS(" << DirLang.getClausePrefix() << "##Name, Str)\n";
819   OS << "\n";
820 
821   for (const auto &R : DirLang.getClauses()) {
822     Clause C{R};
823     if (C.getClangClass().empty()) { // NO_CLASS
824       if (C.isImplicit()) {
825         OS << "__IMPLICIT_CLAUSE_NO_CLASS(" << C.getFormattedName() << ", \""
826            << C.getFormattedName() << "\")\n";
827       } else {
828         OS << "__CLAUSE_NO_CLASS(" << C.getFormattedName() << ")\n";
829       }
830     } else { // CLASS
831       if (C.isImplicit()) {
832         OS << "__IMPLICIT_CLAUSE_CLASS(" << C.getFormattedName() << ", \""
833            << C.getFormattedName() << "\", " << C.getClangClass() << ")\n";
834       } else {
835         OS << "__CLAUSE(" << C.getFormattedName() << ", " << C.getClangClass()
836            << ")\n";
837       }
838     }
839   }
840 
841   OS << "\n";
842   OS << "#undef __IMPLICIT_CLAUSE_NO_CLASS\n";
843   OS << "#undef __IMPLICIT_CLAUSE_CLASS\n";
844   OS << "#undef __CLAUSE\n";
845   OS << "#undef CLAUSE_NO_CLASS\n";
846   OS << "#undef CLAUSE_CLASS\n";
847   OS << "#undef CLAUSE\n";
848 }
849 
850 // Generate the implemenation for the enumeration in the directive
851 // language. This code can be included in library.
852 void EmitDirectivesBasicImpl(const DirectiveLanguage &DirLang,
853                              raw_ostream &OS) {
854   IfDefScope Scope("GEN_DIRECTIVES_IMPL", OS);
855 
856   // getDirectiveKind(StringRef Str)
857   GenerateGetKind(DirLang.getDirectives(), OS, "Directive", DirLang,
858                   DirLang.getDirectivePrefix(), /*ImplicitAsUnknown=*/false);
859 
860   // getDirectiveName(Directive Kind)
861   GenerateGetName(DirLang.getDirectives(), OS, "Directive", DirLang,
862                   DirLang.getDirectivePrefix());
863 
864   // getClauseKind(StringRef Str)
865   GenerateGetKind(DirLang.getClauses(), OS, "Clause", DirLang,
866                   DirLang.getClausePrefix(),
867                   /*ImplicitAsUnknown=*/true);
868 
869   // getClauseName(Clause Kind)
870   GenerateGetName(DirLang.getClauses(), OS, "Clause", DirLang,
871                   DirLang.getClausePrefix());
872 
873   // get<ClauseVal>Kind(StringRef Str)
874   GenerateGetKindClauseVal(DirLang, OS);
875 
876   // isAllowedClauseForDirective(Directive D, Clause C, unsigned Version)
877   GenerateIsAllowedClause(DirLang, OS);
878 }
879 
880 // Generate the implemenation section for the enumeration in the directive
881 // language.
882 static void EmitDirectivesImpl(RecordKeeper &Records, raw_ostream &OS) {
883   const auto DirLang = DirectiveLanguage{Records};
884   if (DirLang.HasValidityErrors())
885     return;
886 
887   EmitDirectivesFlangImpl(DirLang, OS);
888 
889   GenerateClauseClassMacro(DirLang, OS);
890 
891   EmitDirectivesBasicImpl(DirLang, OS);
892 }
893 
894 static TableGen::Emitter::Opt
895     X("gen-directive-decl", EmitDirectivesDecl,
896       "Generate directive related declaration code (header file)");
897 
898 static TableGen::Emitter::Opt
899     Y("gen-directive-impl", EmitDirectivesImpl,
900       "Generate directive related implementation code");
901