DirectiveEmitter.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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/ADT/StringSwitch.h"
  18. #include "llvm/TableGen/Error.h"
  19. #include "llvm/TableGen/Record.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.insert(ClauseFormattedName).second) {
  325. OS << " case " << DirLang.getClausePrefix() << ClauseFormattedName
  326. << ":\n";
  327. OS << " return " << VerClause.getMinVersion()
  328. << " <= Version && " << VerClause.getMaxVersion() << " >= Version;\n";
  329. }
  330. }
  331. }
  332. // Generate the isAllowedClauseForDirective function implementation.
  333. void GenerateIsAllowedClause(const DirectiveLanguage &DirLang,
  334. raw_ostream &OS) {
  335. OS << "\n";
  336. OS << "bool llvm::" << DirLang.getCppNamespace()
  337. << "::isAllowedClauseForDirective("
  338. << "Directive D, Clause C, unsigned Version) {\n";
  339. OS << " assert(unsigned(D) <= llvm::" << DirLang.getCppNamespace()
  340. << "::Directive_enumSize);\n";
  341. OS << " assert(unsigned(C) <= llvm::" << DirLang.getCppNamespace()
  342. << "::Clause_enumSize);\n";
  343. OS << " switch (D) {\n";
  344. for (const auto &D : DirLang.getDirectives()) {
  345. Directive Dir{D};
  346. OS << " case " << DirLang.getDirectivePrefix() << Dir.getFormattedName()
  347. << ":\n";
  348. if (Dir.getAllowedClauses().size() == 0 &&
  349. Dir.getAllowedOnceClauses().size() == 0 &&
  350. Dir.getAllowedExclusiveClauses().size() == 0 &&
  351. Dir.getRequiredClauses().size() == 0) {
  352. OS << " return false;\n";
  353. } else {
  354. OS << " switch (C) {\n";
  355. llvm::StringSet<> Cases;
  356. GenerateCaseForVersionedClauses(Dir.getAllowedClauses(), OS,
  357. Dir.getName(), DirLang, Cases);
  358. GenerateCaseForVersionedClauses(Dir.getAllowedOnceClauses(), OS,
  359. Dir.getName(), DirLang, Cases);
  360. GenerateCaseForVersionedClauses(Dir.getAllowedExclusiveClauses(), OS,
  361. Dir.getName(), DirLang, Cases);
  362. GenerateCaseForVersionedClauses(Dir.getRequiredClauses(), OS,
  363. Dir.getName(), DirLang, Cases);
  364. OS << " default:\n";
  365. OS << " return false;\n";
  366. OS << " }\n"; // End of clauses switch
  367. }
  368. OS << " break;\n";
  369. }
  370. OS << " }\n"; // End of directives switch
  371. OS << " llvm_unreachable(\"Invalid " << DirLang.getName()
  372. << " Directive kind\");\n";
  373. OS << "}\n"; // End of function isAllowedClauseForDirective
  374. }
  375. // Generate a simple enum set with the give clauses.
  376. void GenerateClauseSet(const std::vector<Record *> &Clauses, raw_ostream &OS,
  377. StringRef ClauseSetPrefix, Directive &Dir,
  378. const DirectiveLanguage &DirLang) {
  379. OS << "\n";
  380. OS << " static " << DirLang.getClauseEnumSetClass() << " " << ClauseSetPrefix
  381. << DirLang.getDirectivePrefix() << Dir.getFormattedName() << " {\n";
  382. for (const auto &C : Clauses) {
  383. VersionedClause VerClause{C};
  384. OS << " llvm::" << DirLang.getCppNamespace()
  385. << "::Clause::" << DirLang.getClausePrefix()
  386. << VerClause.getClause().getFormattedName() << ",\n";
  387. }
  388. OS << " };\n";
  389. }
  390. // Generate an enum set for the 4 kinds of clauses linked to a directive.
  391. void GenerateDirectiveClauseSets(const DirectiveLanguage &DirLang,
  392. raw_ostream &OS) {
  393. IfDefScope Scope("GEN_FLANG_DIRECTIVE_CLAUSE_SETS", OS);
  394. OS << "\n";
  395. OS << "namespace llvm {\n";
  396. // Open namespaces defined in the directive language.
  397. llvm::SmallVector<StringRef, 2> Namespaces;
  398. llvm::SplitString(DirLang.getCppNamespace(), Namespaces, "::");
  399. for (auto Ns : Namespaces)
  400. OS << "namespace " << Ns << " {\n";
  401. for (const auto &D : DirLang.getDirectives()) {
  402. Directive Dir{D};
  403. OS << "\n";
  404. OS << " // Sets for " << Dir.getName() << "\n";
  405. GenerateClauseSet(Dir.getAllowedClauses(), OS, "allowedClauses_", Dir,
  406. DirLang);
  407. GenerateClauseSet(Dir.getAllowedOnceClauses(), OS, "allowedOnceClauses_",
  408. Dir, DirLang);
  409. GenerateClauseSet(Dir.getAllowedExclusiveClauses(), OS,
  410. "allowedExclusiveClauses_", Dir, DirLang);
  411. GenerateClauseSet(Dir.getRequiredClauses(), OS, "requiredClauses_", Dir,
  412. DirLang);
  413. }
  414. // Closing namespaces
  415. for (auto Ns : llvm::reverse(Namespaces))
  416. OS << "} // namespace " << Ns << "\n";
  417. OS << "} // namespace llvm\n";
  418. }
  419. // Generate a map of directive (key) with DirectiveClauses struct as values.
  420. // The struct holds the 4 sets of enumeration for the 4 kinds of clauses
  421. // allowances (allowed, allowed once, allowed exclusive and required).
  422. void GenerateDirectiveClauseMap(const DirectiveLanguage &DirLang,
  423. raw_ostream &OS) {
  424. IfDefScope Scope("GEN_FLANG_DIRECTIVE_CLAUSE_MAP", OS);
  425. OS << "\n";
  426. OS << "{\n";
  427. for (const auto &D : DirLang.getDirectives()) {
  428. Directive Dir{D};
  429. OS << " {llvm::" << DirLang.getCppNamespace()
  430. << "::Directive::" << DirLang.getDirectivePrefix()
  431. << Dir.getFormattedName() << ",\n";
  432. OS << " {\n";
  433. OS << " llvm::" << DirLang.getCppNamespace() << "::allowedClauses_"
  434. << DirLang.getDirectivePrefix() << Dir.getFormattedName() << ",\n";
  435. OS << " llvm::" << DirLang.getCppNamespace() << "::allowedOnceClauses_"
  436. << DirLang.getDirectivePrefix() << Dir.getFormattedName() << ",\n";
  437. OS << " llvm::" << DirLang.getCppNamespace()
  438. << "::allowedExclusiveClauses_" << DirLang.getDirectivePrefix()
  439. << Dir.getFormattedName() << ",\n";
  440. OS << " llvm::" << DirLang.getCppNamespace() << "::requiredClauses_"
  441. << DirLang.getDirectivePrefix() << Dir.getFormattedName() << ",\n";
  442. OS << " }\n";
  443. OS << " },\n";
  444. }
  445. OS << "}\n";
  446. }
  447. // Generate classes entry for Flang clauses in the Flang parse-tree
  448. // If the clause as a non-generic class, no entry is generated.
  449. // If the clause does not hold a value, an EMPTY_CLASS is used.
  450. // If the clause class is generic then a WRAPPER_CLASS is used. When the value
  451. // is optional, the value class is wrapped into a std::optional.
  452. void GenerateFlangClauseParserClass(const DirectiveLanguage &DirLang,
  453. raw_ostream &OS) {
  454. IfDefScope Scope("GEN_FLANG_CLAUSE_PARSER_CLASSES", OS);
  455. OS << "\n";
  456. for (const auto &C : DirLang.getClauses()) {
  457. Clause Clause{C};
  458. if (!Clause.getFlangClass().empty()) {
  459. OS << "WRAPPER_CLASS(" << Clause.getFormattedParserClassName() << ", ";
  460. if (Clause.isValueOptional() && Clause.isValueList()) {
  461. OS << "std::optional<std::list<" << Clause.getFlangClass() << ">>";
  462. } else if (Clause.isValueOptional()) {
  463. OS << "std::optional<" << Clause.getFlangClass() << ">";
  464. } else if (Clause.isValueList()) {
  465. OS << "std::list<" << Clause.getFlangClass() << ">";
  466. } else {
  467. OS << Clause.getFlangClass();
  468. }
  469. } else {
  470. OS << "EMPTY_CLASS(" << Clause.getFormattedParserClassName();
  471. }
  472. OS << ");\n";
  473. }
  474. }
  475. // Generate a list of the different clause classes for Flang.
  476. void GenerateFlangClauseParserClassList(const DirectiveLanguage &DirLang,
  477. raw_ostream &OS) {
  478. IfDefScope Scope("GEN_FLANG_CLAUSE_PARSER_CLASSES_LIST", OS);
  479. OS << "\n";
  480. llvm::interleaveComma(DirLang.getClauses(), OS, [&](Record *C) {
  481. Clause Clause{C};
  482. OS << Clause.getFormattedParserClassName() << "\n";
  483. });
  484. }
  485. // Generate dump node list for the clauses holding a generic class name.
  486. void GenerateFlangClauseDump(const DirectiveLanguage &DirLang,
  487. raw_ostream &OS) {
  488. IfDefScope Scope("GEN_FLANG_DUMP_PARSE_TREE_CLAUSES", OS);
  489. OS << "\n";
  490. for (const auto &C : DirLang.getClauses()) {
  491. Clause Clause{C};
  492. OS << "NODE(" << DirLang.getFlangClauseBaseClass() << ", "
  493. << Clause.getFormattedParserClassName() << ")\n";
  494. }
  495. }
  496. // Generate Unparse functions for clauses classes in the Flang parse-tree
  497. // If the clause is a non-generic class, no entry is generated.
  498. void GenerateFlangClauseUnparse(const DirectiveLanguage &DirLang,
  499. raw_ostream &OS) {
  500. IfDefScope Scope("GEN_FLANG_CLAUSE_UNPARSE", OS);
  501. OS << "\n";
  502. for (const auto &C : DirLang.getClauses()) {
  503. Clause Clause{C};
  504. if (!Clause.getFlangClass().empty()) {
  505. if (Clause.isValueOptional() && Clause.getDefaultValue().empty()) {
  506. OS << "void Unparse(const " << DirLang.getFlangClauseBaseClass()
  507. << "::" << Clause.getFormattedParserClassName() << " &x) {\n";
  508. OS << " Word(\"" << Clause.getName().upper() << "\");\n";
  509. OS << " Walk(\"(\", x.v, \")\");\n";
  510. OS << "}\n";
  511. } else if (Clause.isValueOptional()) {
  512. OS << "void Unparse(const " << DirLang.getFlangClauseBaseClass()
  513. << "::" << Clause.getFormattedParserClassName() << " &x) {\n";
  514. OS << " Word(\"" << Clause.getName().upper() << "\");\n";
  515. OS << " Put(\"(\");\n";
  516. OS << " if (x.v.has_value())\n";
  517. if (Clause.isValueList())
  518. OS << " Walk(x.v, \",\");\n";
  519. else
  520. OS << " Walk(x.v);\n";
  521. OS << " else\n";
  522. OS << " Put(\"" << Clause.getDefaultValue() << "\");\n";
  523. OS << " Put(\")\");\n";
  524. OS << "}\n";
  525. } else {
  526. OS << "void Unparse(const " << DirLang.getFlangClauseBaseClass()
  527. << "::" << Clause.getFormattedParserClassName() << " &x) {\n";
  528. OS << " Word(\"" << Clause.getName().upper() << "\");\n";
  529. OS << " Put(\"(\");\n";
  530. if (Clause.isValueList())
  531. OS << " Walk(x.v, \",\");\n";
  532. else
  533. OS << " Walk(x.v);\n";
  534. OS << " Put(\")\");\n";
  535. OS << "}\n";
  536. }
  537. } else {
  538. OS << "void Before(const " << DirLang.getFlangClauseBaseClass()
  539. << "::" << Clause.getFormattedParserClassName() << " &) { Word(\""
  540. << Clause.getName().upper() << "\"); }\n";
  541. }
  542. }
  543. }
  544. // Generate check in the Enter functions for clauses classes.
  545. void GenerateFlangClauseCheckPrototypes(const DirectiveLanguage &DirLang,
  546. raw_ostream &OS) {
  547. IfDefScope Scope("GEN_FLANG_CLAUSE_CHECK_ENTER", OS);
  548. OS << "\n";
  549. for (const auto &C : DirLang.getClauses()) {
  550. Clause Clause{C};
  551. OS << "void Enter(const parser::" << DirLang.getFlangClauseBaseClass()
  552. << "::" << Clause.getFormattedParserClassName() << " &);\n";
  553. }
  554. }
  555. // Generate the mapping for clauses between the parser class and the
  556. // corresponding clause Kind
  557. void GenerateFlangClauseParserKindMap(const DirectiveLanguage &DirLang,
  558. raw_ostream &OS) {
  559. IfDefScope Scope("GEN_FLANG_CLAUSE_PARSER_KIND_MAP", OS);
  560. OS << "\n";
  561. for (const auto &C : DirLang.getClauses()) {
  562. Clause Clause{C};
  563. OS << "if constexpr (std::is_same_v<A, parser::"
  564. << DirLang.getFlangClauseBaseClass()
  565. << "::" << Clause.getFormattedParserClassName();
  566. OS << ">)\n";
  567. OS << " return llvm::" << DirLang.getCppNamespace()
  568. << "::Clause::" << DirLang.getClausePrefix() << Clause.getFormattedName()
  569. << ";\n";
  570. }
  571. OS << "llvm_unreachable(\"Invalid " << DirLang.getName()
  572. << " Parser clause\");\n";
  573. }
  574. bool compareClauseName(Record *R1, Record *R2) {
  575. Clause C1{R1};
  576. Clause C2{R2};
  577. return (C1.getName() > C2.getName());
  578. }
  579. // Generate the parser for the clauses.
  580. void GenerateFlangClausesParser(const DirectiveLanguage &DirLang,
  581. raw_ostream &OS) {
  582. std::vector<Record *> Clauses = DirLang.getClauses();
  583. // Sort clauses in reverse alphabetical order so with clauses with same
  584. // beginning, the longer option is tried before.
  585. llvm::sort(Clauses, compareClauseName);
  586. IfDefScope Scope("GEN_FLANG_CLAUSES_PARSER", OS);
  587. OS << "\n";
  588. unsigned index = 0;
  589. unsigned lastClauseIndex = DirLang.getClauses().size() - 1;
  590. OS << "TYPE_PARSER(\n";
  591. for (const auto &C : Clauses) {
  592. Clause Clause{C};
  593. if (Clause.getAliases().empty()) {
  594. OS << " \"" << Clause.getName() << "\"";
  595. } else {
  596. OS << " ("
  597. << "\"" << Clause.getName() << "\"_tok";
  598. for (StringRef alias : Clause.getAliases()) {
  599. OS << " || \"" << alias << "\"_tok";
  600. }
  601. OS << ")";
  602. }
  603. OS << " >> construct<" << DirLang.getFlangClauseBaseClass()
  604. << ">(construct<" << DirLang.getFlangClauseBaseClass()
  605. << "::" << Clause.getFormattedParserClassName() << ">(";
  606. if (Clause.getFlangClass().empty()) {
  607. OS << "))";
  608. if (index != lastClauseIndex)
  609. OS << " ||";
  610. OS << "\n";
  611. ++index;
  612. continue;
  613. }
  614. if (Clause.isValueOptional())
  615. OS << "maybe(";
  616. OS << "parenthesized(";
  617. if (!Clause.getPrefix().empty())
  618. OS << "\"" << Clause.getPrefix() << ":\" >> ";
  619. // The common Flang parser are used directly. Their name is identical to
  620. // the Flang class with first letter as lowercase. If the Flang class is
  621. // not a common class, we assume there is a specific Parser<>{} with the
  622. // Flang class name provided.
  623. llvm::SmallString<128> Scratch;
  624. StringRef Parser =
  625. llvm::StringSwitch<StringRef>(Clause.getFlangClass())
  626. .Case("Name", "name")
  627. .Case("ScalarIntConstantExpr", "scalarIntConstantExpr")
  628. .Case("ScalarIntExpr", "scalarIntExpr")
  629. .Case("ScalarLogicalExpr", "scalarLogicalExpr")
  630. .Default(("Parser<" + Clause.getFlangClass() + ">{}")
  631. .toStringRef(Scratch));
  632. OS << Parser;
  633. if (!Clause.getPrefix().empty() && Clause.isPrefixOptional())
  634. OS << " || " << Parser;
  635. OS << ")"; // close parenthesized(.
  636. if (Clause.isValueOptional()) // close maybe(.
  637. OS << ")";
  638. OS << "))";
  639. if (index != lastClauseIndex)
  640. OS << " ||";
  641. OS << "\n";
  642. ++index;
  643. }
  644. OS << ")\n";
  645. }
  646. // Generate the implementation section for the enumeration in the directive
  647. // language
  648. void EmitDirectivesFlangImpl(const DirectiveLanguage &DirLang,
  649. raw_ostream &OS) {
  650. GenerateDirectiveClauseSets(DirLang, OS);
  651. GenerateDirectiveClauseMap(DirLang, OS);
  652. GenerateFlangClauseParserClass(DirLang, OS);
  653. GenerateFlangClauseParserClassList(DirLang, OS);
  654. GenerateFlangClauseDump(DirLang, OS);
  655. GenerateFlangClauseUnparse(DirLang, OS);
  656. GenerateFlangClauseCheckPrototypes(DirLang, OS);
  657. GenerateFlangClauseParserKindMap(DirLang, OS);
  658. GenerateFlangClausesParser(DirLang, OS);
  659. }
  660. void GenerateClauseClassMacro(const DirectiveLanguage &DirLang,
  661. raw_ostream &OS) {
  662. // Generate macros style information for legacy code in clang
  663. IfDefScope Scope("GEN_CLANG_CLAUSE_CLASS", OS);
  664. OS << "\n";
  665. OS << "#ifndef CLAUSE\n";
  666. OS << "#define CLAUSE(Enum, Str, Implicit)\n";
  667. OS << "#endif\n";
  668. OS << "#ifndef CLAUSE_CLASS\n";
  669. OS << "#define CLAUSE_CLASS(Enum, Str, Class)\n";
  670. OS << "#endif\n";
  671. OS << "#ifndef CLAUSE_NO_CLASS\n";
  672. OS << "#define CLAUSE_NO_CLASS(Enum, Str)\n";
  673. OS << "#endif\n";
  674. OS << "\n";
  675. OS << "#define __CLAUSE(Name, Class) \\\n";
  676. OS << " CLAUSE(" << DirLang.getClausePrefix()
  677. << "##Name, #Name, /* Implicit */ false) \\\n";
  678. OS << " CLAUSE_CLASS(" << DirLang.getClausePrefix()
  679. << "##Name, #Name, Class)\n";
  680. OS << "#define __CLAUSE_NO_CLASS(Name) \\\n";
  681. OS << " CLAUSE(" << DirLang.getClausePrefix()
  682. << "##Name, #Name, /* Implicit */ false) \\\n";
  683. OS << " CLAUSE_NO_CLASS(" << DirLang.getClausePrefix() << "##Name, #Name)\n";
  684. OS << "#define __IMPLICIT_CLAUSE_CLASS(Name, Str, Class) \\\n";
  685. OS << " CLAUSE(" << DirLang.getClausePrefix()
  686. << "##Name, Str, /* Implicit */ true) \\\n";
  687. OS << " CLAUSE_CLASS(" << DirLang.getClausePrefix()
  688. << "##Name, Str, Class)\n";
  689. OS << "#define __IMPLICIT_CLAUSE_NO_CLASS(Name, Str) \\\n";
  690. OS << " CLAUSE(" << DirLang.getClausePrefix()
  691. << "##Name, Str, /* Implicit */ true) \\\n";
  692. OS << " CLAUSE_NO_CLASS(" << DirLang.getClausePrefix() << "##Name, Str)\n";
  693. OS << "\n";
  694. for (const auto &R : DirLang.getClauses()) {
  695. Clause C{R};
  696. if (C.getClangClass().empty()) { // NO_CLASS
  697. if (C.isImplicit()) {
  698. OS << "__IMPLICIT_CLAUSE_NO_CLASS(" << C.getFormattedName() << ", \""
  699. << C.getFormattedName() << "\")\n";
  700. } else {
  701. OS << "__CLAUSE_NO_CLASS(" << C.getFormattedName() << ")\n";
  702. }
  703. } else { // CLASS
  704. if (C.isImplicit()) {
  705. OS << "__IMPLICIT_CLAUSE_CLASS(" << C.getFormattedName() << ", \""
  706. << C.getFormattedName() << "\", " << C.getClangClass() << ")\n";
  707. } else {
  708. OS << "__CLAUSE(" << C.getFormattedName() << ", " << C.getClangClass()
  709. << ")\n";
  710. }
  711. }
  712. }
  713. OS << "\n";
  714. OS << "#undef __IMPLICIT_CLAUSE_NO_CLASS\n";
  715. OS << "#undef __IMPLICIT_CLAUSE_CLASS\n";
  716. OS << "#undef __CLAUSE\n";
  717. OS << "#undef CLAUSE_NO_CLASS\n";
  718. OS << "#undef CLAUSE_CLASS\n";
  719. OS << "#undef CLAUSE\n";
  720. }
  721. // Generate the implemenation for the enumeration in the directive
  722. // language. This code can be included in library.
  723. void EmitDirectivesBasicImpl(const DirectiveLanguage &DirLang,
  724. raw_ostream &OS) {
  725. IfDefScope Scope("GEN_DIRECTIVES_IMPL", OS);
  726. // getDirectiveKind(StringRef Str)
  727. GenerateGetKind(DirLang.getDirectives(), OS, "Directive", DirLang,
  728. DirLang.getDirectivePrefix(), /*ImplicitAsUnknown=*/false);
  729. // getDirectiveName(Directive Kind)
  730. GenerateGetName(DirLang.getDirectives(), OS, "Directive", DirLang,
  731. DirLang.getDirectivePrefix());
  732. // getClauseKind(StringRef Str)
  733. GenerateGetKind(DirLang.getClauses(), OS, "Clause", DirLang,
  734. DirLang.getClausePrefix(),
  735. /*ImplicitAsUnknown=*/true);
  736. // getClauseName(Clause Kind)
  737. GenerateGetName(DirLang.getClauses(), OS, "Clause", DirLang,
  738. DirLang.getClausePrefix());
  739. // get<ClauseVal>Kind(StringRef Str)
  740. GenerateGetKindClauseVal(DirLang, OS);
  741. // isAllowedClauseForDirective(Directive D, Clause C, unsigned Version)
  742. GenerateIsAllowedClause(DirLang, OS);
  743. }
  744. // Generate the implemenation section for the enumeration in the directive
  745. // language.
  746. void EmitDirectivesImpl(RecordKeeper &Records, raw_ostream &OS) {
  747. const auto DirLang = DirectiveLanguage{Records};
  748. if (DirLang.HasValidityErrors())
  749. return;
  750. EmitDirectivesFlangImpl(DirLang, OS);
  751. GenerateClauseClassMacro(DirLang, OS);
  752. EmitDirectivesBasicImpl(DirLang, OS);
  753. }
  754. } // namespace llvm