DirectiveEmitter.cpp 28 KB

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