1 //===- Attributes.cpp - Generate attributes -------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/MemoryBuffer.h"
10 #include "llvm/TableGen/Record.h"
16 #define DEBUG_TYPE "attr-enum"
22 Attributes(RecordKeeper &R) : Records(R) {}
23 void emit(raw_ostream &OS);
26 void emitTargetIndependentEnums(raw_ostream &OS);
27 void emitConversionFn(raw_ostream &OS);
28 void emitFnAttrCompatCheck(raw_ostream &OS, bool IsStringAttr);
30 void printEnumAttrClasses(raw_ostream &OS,
31 const std::vector<Record *> &Records);
32 void printStrBoolAttrClasses(raw_ostream &OS,
33 const std::vector<Record *> &Records);
35 RecordKeeper &Records;
38 } // End anonymous namespace.
40 void Attributes::emitTargetIndependentEnums(raw_ostream &OS) {
41 OS << "#ifdef GET_ATTR_ENUM\n";
42 OS << "#undef GET_ATTR_ENUM\n";
44 std::vector<Record*> Attrs =
45 Records.getAllDerivedDefinitions("EnumAttr");
48 OS << A->getName() << ",\n";
53 void Attributes::emitConversionFn(raw_ostream &OS) {
54 OS << "#ifdef GET_ATTR_KIND_FROM_NAME\n";
55 OS << "#undef GET_ATTR_KIND_FROM_NAME\n";
57 std::vector<Record*> Attrs =
58 Records.getAllDerivedDefinitions("EnumAttr");
60 OS << "static Attribute::AttrKind getAttrKindFromName(StringRef AttrName) {\n";
61 OS << " return StringSwitch<Attribute::AttrKind>(AttrName)\n";
63 for (auto A : Attrs) {
64 OS << " .Case(\"" << A->getValueAsString("AttrString");
65 OS << "\", Attribute::" << A->getName() << ")\n";
68 OS << " .Default(Attribute::None);\n";
74 void Attributes::emitFnAttrCompatCheck(raw_ostream &OS, bool IsStringAttr) {
75 OS << "#ifdef GET_ATTR_COMPAT_FUNC\n";
76 OS << "#undef GET_ATTR_COMPAT_FUNC\n";
78 OS << "struct EnumAttr {\n";
79 OS << " static bool isSet(const Function &Fn,\n";
80 OS << " Attribute::AttrKind Kind) {\n";
81 OS << " return Fn.hasFnAttribute(Kind);\n";
83 OS << " static void set(Function &Fn,\n";
84 OS << " Attribute::AttrKind Kind, bool Val) {\n";
86 OS << " Fn.addFnAttr(Kind);\n";
88 OS << " Fn.removeFnAttr(Kind);\n";
92 OS << "struct StrBoolAttr {\n";
93 OS << " static bool isSet(const Function &Fn,\n";
94 OS << " StringRef Kind) {\n";
95 OS << " auto A = Fn.getFnAttribute(Kind);\n";
96 OS << " return A.getValueAsString().equals(\"true\");\n";
98 OS << " static void set(Function &Fn,\n";
99 OS << " StringRef Kind, bool Val) {\n";
100 OS << " Fn.addFnAttr(Kind, Val ? \"true\" : \"false\");\n";
104 printEnumAttrClasses(OS ,Records.getAllDerivedDefinitions("EnumAttr"));
105 printStrBoolAttrClasses(OS , Records.getAllDerivedDefinitions("StrBoolAttr"));
107 OS << "static inline bool hasCompatibleFnAttrs(const Function &Caller,\n"
108 << " const Function &Callee) {\n";
109 OS << " bool Ret = true;\n\n";
111 std::vector<Record *> CompatRules =
112 Records.getAllDerivedDefinitions("CompatRule");
114 for (auto *Rule : CompatRules) {
115 StringRef FuncName = Rule->getValueAsString("CompatFunc");
116 OS << " Ret &= " << FuncName << "(Caller, Callee);\n";
120 OS << " return Ret;\n";
123 std::vector<Record *> MergeRules =
124 Records.getAllDerivedDefinitions("MergeRule");
125 OS << "static inline void mergeFnAttrs(Function &Caller,\n"
126 << " const Function &Callee) {\n";
128 for (auto *Rule : MergeRules) {
129 StringRef FuncName = Rule->getValueAsString("MergeFunc");
130 OS << " " << FuncName << "(Caller, Callee);\n";
138 void Attributes::printEnumAttrClasses(raw_ostream &OS,
139 const std::vector<Record *> &Records) {
140 OS << "// EnumAttr classes\n";
141 for (const auto *R : Records) {
142 OS << "struct " << R->getName() << "Attr : EnumAttr {\n";
143 OS << " static enum Attribute::AttrKind getKind() {\n";
144 OS << " return llvm::Attribute::" << R->getName() << ";\n";
151 void Attributes::printStrBoolAttrClasses(raw_ostream &OS,
152 const std::vector<Record *> &Records) {
153 OS << "// StrBoolAttr classes\n";
154 for (const auto *R : Records) {
155 OS << "struct " << R->getName() << "Attr : StrBoolAttr {\n";
156 OS << " static StringRef getKind() {\n";
157 OS << " return \"" << R->getValueAsString("AttrString") << "\";\n";
164 void Attributes::emit(raw_ostream &OS) {
165 emitTargetIndependentEnums(OS);
166 emitConversionFn(OS);
167 emitFnAttrCompatCheck(OS, false);
172 void EmitAttributes(RecordKeeper &RK, raw_ostream &OS) {
173 Attributes(RK).emit(OS);
176 } // End llvm namespace.