DirectiveEmitter.h 5.8 KB

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