DirectiveEmitter.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. #ifndef LLVM_TABLEGEN_DIRECTIVEEMITTER_H
  7. #define LLVM_TABLEGEN_DIRECTIVEEMITTER_H
  8. #include "llvm/ADT/StringExtras.h"
  9. #include "llvm/TableGen/Record.h"
  10. namespace llvm {
  11. // Wrapper class that contains DirectiveLanguage's information defined in
  12. // DirectiveBase.td and provides helper methods for accessing it.
  13. class DirectiveLanguage {
  14. public:
  15. explicit DirectiveLanguage(const llvm::RecordKeeper &Records)
  16. : Records(Records) {
  17. const auto &DirectiveLanguages = getDirectiveLanguages();
  18. Def = DirectiveLanguages[0];
  19. }
  20. StringRef getName() const { return Def->getValueAsString("name"); }
  21. StringRef getCppNamespace() const {
  22. return Def->getValueAsString("cppNamespace");
  23. }
  24. StringRef getDirectivePrefix() const {
  25. return Def->getValueAsString("directivePrefix");
  26. }
  27. StringRef getClausePrefix() const {
  28. return Def->getValueAsString("clausePrefix");
  29. }
  30. StringRef getClauseEnumSetClass() const {
  31. return Def->getValueAsString("clauseEnumSetClass");
  32. }
  33. StringRef getFlangClauseBaseClass() const {
  34. return Def->getValueAsString("flangClauseBaseClass");
  35. }
  36. bool hasMakeEnumAvailableInNamespace() const {
  37. return Def->getValueAsBit("makeEnumAvailableInNamespace");
  38. }
  39. bool hasEnableBitmaskEnumInNamespace() const {
  40. return Def->getValueAsBit("enableBitmaskEnumInNamespace");
  41. }
  42. std::vector<Record *> getDirectives() const {
  43. return Records.getAllDerivedDefinitions("Directive");
  44. }
  45. std::vector<Record *> getClauses() const {
  46. return Records.getAllDerivedDefinitions("Clause");
  47. }
  48. bool HasValidityErrors() const;
  49. private:
  50. const llvm::Record *Def;
  51. const llvm::RecordKeeper &Records;
  52. std::vector<Record *> getDirectiveLanguages() const {
  53. return Records.getAllDerivedDefinitions("DirectiveLanguage");
  54. }
  55. };
  56. // Base record class used for Directive and Clause class defined in
  57. // DirectiveBase.td.
  58. class BaseRecord {
  59. public:
  60. explicit BaseRecord(const llvm::Record *Def) : Def(Def) {}
  61. StringRef getName() const { return Def->getValueAsString("name"); }
  62. StringRef getAlternativeName() const {
  63. return Def->getValueAsString("alternativeName");
  64. }
  65. // Returns the name of the directive formatted for output. Whitespace are
  66. // replaced with underscores.
  67. std::string getFormattedName() {
  68. StringRef Name = Def->getValueAsString("name");
  69. std::string N = Name.str();
  70. std::replace(N.begin(), N.end(), ' ', '_');
  71. return N;
  72. }
  73. bool isDefault() const { return Def->getValueAsBit("isDefault"); }
  74. // Returns the record name.
  75. StringRef getRecordName() const { return Def->getName(); }
  76. protected:
  77. const llvm::Record *Def;
  78. };
  79. // Wrapper class that contains a Directive's information defined in
  80. // DirectiveBase.td and provides helper methods for accessing it.
  81. class Directive : public BaseRecord {
  82. public:
  83. explicit Directive(const llvm::Record *Def) : BaseRecord(Def) {}
  84. std::vector<Record *> getAllowedClauses() const {
  85. return Def->getValueAsListOfDefs("allowedClauses");
  86. }
  87. std::vector<Record *> getAllowedOnceClauses() const {
  88. return Def->getValueAsListOfDefs("allowedOnceClauses");
  89. }
  90. std::vector<Record *> getAllowedExclusiveClauses() const {
  91. return Def->getValueAsListOfDefs("allowedExclusiveClauses");
  92. }
  93. std::vector<Record *> getRequiredClauses() const {
  94. return Def->getValueAsListOfDefs("requiredClauses");
  95. }
  96. };
  97. // Wrapper class that contains Clause's information defined in DirectiveBase.td
  98. // and provides helper methods for accessing it.
  99. class Clause : public BaseRecord {
  100. public:
  101. explicit Clause(const llvm::Record *Def) : BaseRecord(Def) {}
  102. // Optional field.
  103. StringRef getClangClass() const {
  104. return Def->getValueAsString("clangClass");
  105. }
  106. // Optional field.
  107. StringRef getFlangClass() const {
  108. return Def->getValueAsString("flangClass");
  109. }
  110. // Get the formatted name for Flang parser class. The generic formatted class
  111. // name is constructed from the name were the first letter of each word is
  112. // captitalized and the underscores are removed.
  113. // ex: async -> Async
  114. // num_threads -> NumThreads
  115. std::string getFormattedParserClassName() {
  116. StringRef Name = Def->getValueAsString("name");
  117. std::string N = Name.str();
  118. bool Cap = true;
  119. std::transform(N.begin(), N.end(), N.begin(), [&Cap](unsigned char C) {
  120. if (Cap == true) {
  121. C = llvm::toUpper(C);
  122. Cap = false;
  123. } else if (C == '_') {
  124. Cap = true;
  125. }
  126. return C;
  127. });
  128. llvm::erase_value(N, '_');
  129. return N;
  130. }
  131. // Optional field.
  132. StringRef getEnumName() const {
  133. return Def->getValueAsString("enumClauseValue");
  134. }
  135. std::vector<Record *> getClauseVals() const {
  136. return Def->getValueAsListOfDefs("allowedClauseValues");
  137. }
  138. bool isValueOptional() const { return Def->getValueAsBit("isValueOptional"); }
  139. bool isValueList() const { return Def->getValueAsBit("isValueList"); }
  140. StringRef getDefaultValue() const {
  141. return Def->getValueAsString("defaultValue");
  142. }
  143. bool isImplicit() const { return Def->getValueAsBit("isImplicit"); }
  144. std::vector<StringRef> getAliases() const {
  145. return Def->getValueAsListOfStrings("aliases");
  146. }
  147. StringRef getPrefix() const { return Def->getValueAsString("prefix"); }
  148. bool isPrefixOptional() const {
  149. return Def->getValueAsBit("isPrefixOptional");
  150. }
  151. };
  152. // Wrapper class that contains VersionedClause's information defined in
  153. // DirectiveBase.td and provides helper methods for accessing it.
  154. class VersionedClause {
  155. public:
  156. explicit VersionedClause(const llvm::Record *Def) : Def(Def) {}
  157. // Return the specific clause record wrapped in the Clause class.
  158. Clause getClause() const { return Clause{Def->getValueAsDef("clause")}; }
  159. int64_t getMinVersion() const { return Def->getValueAsInt("minVersion"); }
  160. int64_t getMaxVersion() const { return Def->getValueAsInt("maxVersion"); }
  161. private:
  162. const llvm::Record *Def;
  163. };
  164. class ClauseVal : public BaseRecord {
  165. public:
  166. explicit ClauseVal(const llvm::Record *Def) : BaseRecord(Def) {}
  167. int getValue() const { return Def->getValueAsInt("value"); }
  168. bool isUserVisible() const { return Def->getValueAsBit("isUserValue"); }
  169. };
  170. } // namespace llvm
  171. #endif
  172. #ifdef __GNUC__
  173. #pragma GCC diagnostic pop
  174. #endif