1 //===- Attributes.cpp - Generate attributes -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/MemoryBuffer.h"
11 #include "llvm/TableGen/Record.h"
17 #define DEBUG_TYPE "attr-enum"
23 Attributes(RecordKeeper &R) : Records(R) {}
24 void emit(raw_ostream &OS);
27 void emitTargetIndependentEnums(raw_ostream &OS);
28 void emitConversionFn(raw_ostream &OS);
29 void emitFnAttrCompatCheck(raw_ostream &OS, bool IsStringAttr);
31 void printEnumAttrClasses(raw_ostream &OS,
32 const std::vector<Record *> &Records);
33 void printStrBoolAttrClasses(raw_ostream &OS,
34 const std::vector<Record *> &Records);
36 RecordKeeper &Records;
39 } // End anonymous namespace.
41 void Attributes::emitTargetIndependentEnums(raw_ostream &OS) {
42 OS << "#ifdef GET_ATTR_ENUM\n";
43 OS << "#undef GET_ATTR_ENUM\n";
45 std::vector<Record*> Attrs =
46 Records.getAllDerivedDefinitions("EnumAttr");
49 OS << A->getName() << ",\n";
54 void Attributes::emitConversionFn(raw_ostream &OS) {
55 OS << "#ifdef GET_ATTR_KIND_FROM_NAME\n";
56 OS << "#undef GET_ATTR_KIND_FROM_NAME\n";
58 std::vector<Record*> Attrs =
59 Records.getAllDerivedDefinitions("EnumAttr");
61 OS << "static Attribute::AttrKind getAttrKindFromName(StringRef AttrName) {\n";
62 OS << " return StringSwitch<Attribute::AttrKind>(AttrName)\n";
64 for (auto A : Attrs) {
65 OS << " .Case(\"" << A->getValueAsString("AttrString");
66 OS << "\", Attribute::" << A->getName() << ")\n";
69 OS << " .Default(Attribute::None);\n";
75 void Attributes::emitFnAttrCompatCheck(raw_ostream &OS, bool IsStringAttr) {
76 OS << "#ifdef GET_ATTR_COMPAT_FUNC\n";
77 OS << "#undef GET_ATTR_COMPAT_FUNC\n";
79 OS << "struct EnumAttr {\n";
80 OS << " static bool isSet(const Function &Fn,\n";
81 OS << " Attribute::AttrKind Kind) {\n";
82 OS << " return Fn.hasFnAttribute(Kind);\n";
84 OS << " static void set(Function &Fn,\n";
85 OS << " Attribute::AttrKind Kind, bool Val) {\n";
87 OS << " Fn.addFnAttr(Kind);\n";
89 OS << " Fn.removeFnAttr(Kind);\n";
93 OS << "struct StrBoolAttr {\n";
94 OS << " static bool isSet(const Function &Fn,\n";
95 OS << " StringRef Kind) {\n";
96 OS << " auto A = Fn.getFnAttribute(Kind);\n";
97 OS << " return A.getValueAsString().equals(\"true\");\n";
99 OS << " static void set(Function &Fn,\n";
100 OS << " StringRef Kind, bool Val) {\n";
101 OS << " Fn.addFnAttr(Kind, Val ? \"true\" : \"false\");\n";
105 printEnumAttrClasses(OS ,Records.getAllDerivedDefinitions("EnumAttr"));
106 printStrBoolAttrClasses(OS , Records.getAllDerivedDefinitions("StrBoolAttr"));
108 OS << "static inline bool hasCompatibleFnAttrs(const Function &Caller,\n"
109 << " const Function &Callee) {\n";
110 OS << " bool Ret = true;\n\n";
112 std::vector<Record *> CompatRules =
113 Records.getAllDerivedDefinitions("CompatRule");
115 for (auto *Rule : CompatRules) {
116 StringRef FuncName = Rule->getValueAsString("CompatFunc");
117 OS << " Ret &= " << FuncName << "(Caller, Callee);\n";
121 OS << " return Ret;\n";
124 std::vector<Record *> MergeRules =
125 Records.getAllDerivedDefinitions("MergeRule");
126 OS << "static inline void mergeFnAttrs(Function &Caller,\n"
127 << " const Function &Callee) {\n";
129 for (auto *Rule : MergeRules) {
130 StringRef FuncName = Rule->getValueAsString("MergeFunc");
131 OS << " " << FuncName << "(Caller, Callee);\n";
139 void Attributes::printEnumAttrClasses(raw_ostream &OS,
140 const std::vector<Record *> &Records) {
141 OS << "// EnumAttr classes\n";
142 for (const auto *R : Records) {
143 OS << "struct " << R->getName() << "Attr : EnumAttr {\n";
144 OS << " static enum Attribute::AttrKind getKind() {\n";
145 OS << " return llvm::Attribute::" << R->getName() << ";\n";
152 void Attributes::printStrBoolAttrClasses(raw_ostream &OS,
153 const std::vector<Record *> &Records) {
154 OS << "// StrBoolAttr classes\n";
155 for (const auto *R : Records) {
156 OS << "struct " << R->getName() << "Attr : StrBoolAttr {\n";
157 OS << " static StringRef getKind() {\n";
158 OS << " return \"" << R->getValueAsString("AttrString") << "\";\n";
165 void Attributes::emit(raw_ostream &OS) {
166 emitTargetIndependentEnums(OS);
167 emitConversionFn(OS);
168 emitFnAttrCompatCheck(OS, false);
173 void EmitAttributes(RecordKeeper &RK, raw_ostream &OS) {
174 Attributes(RK).emit(OS);
177 } // End llvm namespace.