1 //===- LLDBPropertyDefEmitter.cpp -----------------------------------------===//
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 // These tablegen backends emits LLDB's PropertyDefinition values.
11 //===----------------------------------------------------------------------===//
13 #include "LLDBTableGenBackends.h"
14 #include "LLDBTableGenUtils.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/TableGen/Record.h"
17 #include "llvm/TableGen/StringMatcher.h"
18 #include "llvm/TableGen/TableGenBackend.h"
22 using namespace lldb_private
;
24 static void emitPropertyEnum(Record
*Property
, raw_ostream
&OS
) {
26 OS
<< Property
->getName();
30 static void emitProperty(Record
*Property
, raw_ostream
&OS
) {
33 // Emit the property name.
34 OS
<< "\"" << Property
->getValueAsString("Name") << "\"";
37 // Emit the property type.
38 llvm::StringRef type
= Property
->getValueAsString("Type");
39 OS
<< "OptionValue::eType";
43 // Emit the property's global value.
44 OS
<< (Property
->getValue("Global") ? "true" : "false");
47 bool hasDefaultUnsignedValue
= Property
->getValue("HasDefaultUnsignedValue");
48 bool hasDefaultEnumValue
= Property
->getValue("HasDefaultEnumValue");
49 bool hasDefaultStringValue
= Property
->getValue("HasDefaultStringValue");
50 bool hasElementType
= Property
->getValue("HasElementType");
52 // Guarantee that every property has a default value.
53 assert((hasDefaultUnsignedValue
|| hasDefaultEnumValue
||
54 hasDefaultStringValue
|| hasElementType
) &&
55 "Property must have a default value or an element type");
57 // Guarantee that no property has both a default unsigned value and a default
58 // enum value, since they're bothed stored in the same field.
59 assert(!(hasDefaultUnsignedValue
&& hasDefaultEnumValue
) &&
60 "Property cannot have both a unsigned and enum default value.");
62 // Guarantee that every boolean property has a boolean default value.
63 assert(!(Property
->getValueAsString("Type") == "Boolean" &&
64 !Property
->getValue("HasDefaultBooleanValue")) &&
65 "Boolean property must have a boolean default value.");
67 // Guarantee that every string property has a string default value.
68 assert(!(Property
->getValueAsString("Type") == "String" &&
69 !hasDefaultStringValue
) &&
70 "String property must have a string default value.");
72 // Guarantee that every enum property has an enum default value.
74 !(Property
->getValueAsString("Type") == "Enum" && !hasDefaultEnumValue
) &&
75 "Enum property must have a enum default value.");
77 // Guarantee that only arrays and dictionaries have an element type;
78 assert(((type
!= "Array" && type
!= "Dictionary") || hasElementType
) &&
79 "Only dictionaries and arrays can have an element type.");
81 // Emit the default uint value.
82 if (hasDefaultUnsignedValue
) {
83 OS
<< std::to_string(Property
->getValueAsInt("DefaultUnsignedValue"));
84 } else if (hasDefaultEnumValue
) {
85 OS
<< Property
->getValueAsString("DefaultEnumValue");
86 } else if (hasElementType
) {
87 OS
<< "OptionValue::eType";
88 OS
<< Property
->getValueAsString("ElementType");
94 // Emit the default string value.
95 if (hasDefaultStringValue
) {
96 if (auto D
= Property
->getValue("DefaultStringValue")) {
98 OS
<< D
->getValue()->getAsUnquotedString();
108 // Emit the enum values value.
109 if (Property
->getValue("EnumValues"))
110 OS
<< Property
->getValueAsString("EnumValues");
115 // Emit the property description.
116 if (auto D
= Property
->getValue("Description")) {
118 OS
<< D
->getValue()->getAsUnquotedString();
127 /// Emits all property initializers to the raw_ostream.
128 static void emityProperties(std::string PropertyName
,
129 std::vector
<Record
*> PropertyRecords
,
131 // Generate the macro that the user needs to define before including the
133 std::string NeededMacro
= "LLDB_PROPERTIES_" + PropertyName
;
134 std::replace(NeededMacro
.begin(), NeededMacro
.end(), ' ', '_');
136 // All options are in one file, so we need put them behind macros and ask the
137 // user to define the macro for the options that are needed.
138 OS
<< "// Property definitions for " << PropertyName
<< "\n";
139 OS
<< "#ifdef " << NeededMacro
<< "\n";
140 OS
<< "static constexpr PropertyDefinition g_" << PropertyName
141 << "_properties[] = {\n";
142 for (Record
*R
: PropertyRecords
)
145 // We undefine the macro for the user like Clang's include files are doing it.
146 OS
<< "#undef " << NeededMacro
<< "\n";
147 OS
<< "#endif // " << PropertyName
<< " Property\n\n";
150 /// Emits all property initializers to the raw_ostream.
151 static void emitPropertyEnum(std::string PropertyName
,
152 std::vector
<Record
*> PropertyRecords
,
154 // Generate the macro that the user needs to define before including the
156 std::string NeededMacro
= "LLDB_PROPERTIES_" + PropertyName
;
157 std::replace(NeededMacro
.begin(), NeededMacro
.end(), ' ', '_');
159 // All options are in one file, so we need put them behind macros and ask the
160 // user to define the macro for the options that are needed.
161 OS
<< "// Property enum cases for " << PropertyName
<< "\n";
162 OS
<< "#ifdef " << NeededMacro
<< "\n";
163 for (Record
*R
: PropertyRecords
)
164 emitPropertyEnum(R
, OS
);
165 // We undefine the macro for the user like Clang's include files are doing it.
166 OS
<< "#undef " << NeededMacro
<< "\n";
167 OS
<< "#endif // " << PropertyName
<< " Property\n\n";
170 void lldb_private::EmitPropertyDefs(RecordKeeper
&Records
, raw_ostream
&OS
) {
171 emitSourceFileHeader("Property definitions for LLDB.", OS
, Records
);
173 std::vector
<Record
*> Properties
=
174 Records
.getAllDerivedDefinitions("Property");
175 for (auto &PropertyRecordPair
: getRecordsByName(Properties
, "Definition")) {
176 emityProperties(PropertyRecordPair
.first
, PropertyRecordPair
.second
, OS
);
180 void lldb_private::EmitPropertyEnumDefs(RecordKeeper
&Records
,
182 emitSourceFileHeader("Property definition enum for LLDB.", OS
, Records
);
184 std::vector
<Record
*> Properties
=
185 Records
.getAllDerivedDefinitions("Property");
186 for (auto &PropertyRecordPair
: getRecordsByName(Properties
, "Definition")) {
187 emitPropertyEnum(PropertyRecordPair
.first
, PropertyRecordPair
.second
, OS
);