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