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