Skip to content

Commit d20becf

Browse files
committed
WIP: maing gomgen-luawrap work for ImGui_ext
1 parent fc20a25 commit d20becf

4 files changed

Lines changed: 114 additions & 76 deletions

File tree

src/bin/gomgen/CMakeLists.txt

100755100644
File mode changed.

src/bin/gomgen/generate_luawrap.cpp

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,22 @@ namespace {
9090
);
9191

9292
ogf_declare_pointer_type<char*>("char*");
93-
ogf_declare_pointer_type<const char*>("const char*");
9493
ogf_declare_pointer_type<bool*>("bool*");
9594
ogf_declare_pointer_type<int*>("int*");
9695
ogf_declare_pointer_type<float*>("float*");
9796
ogf_declare_pointer_type<double*>("double*");
9897

99-
// quick and dirty fix for constness problem with
100-
// AcceptDragDropPayload() and GetDragDropLayload()
101-
unsupported_types_.insert("ImGuiPayload*");
98+
ogf_declare_pointer_type<const char*>("const char*");
99+
ogf_declare_pointer_type<const bool*>("const bool*");
100+
ogf_declare_pointer_type<const int*>("const int*");
101+
ogf_declare_pointer_type<const float*>("const float*");
102+
ogf_declare_pointer_type<const double*>("const double*");
103+
104+
unsupported_types_.insert("void*");
105+
106+
supported_types_.insert("ImVec2");
107+
supported_types_.insert("ImVec4");
108+
supported_types_.insert("ImTextureRef");
102109
}
103110

104111

@@ -163,22 +170,31 @@ namespace {
163170
if(unsupported_types_.find(type_name) != unsupported_types_.end()) {
164171
return false;
165172
}
166-
if(
167-
type_name == "ImVec2" || type_name == "ImVec4" ||
168-
type_name == "ImTextureRef"
169-
) {
173+
174+
if(supported_types_.find(type_name) != supported_types_.end()) {
170175
return true;
171176
}
172-
if(type_name == "void*") {
173-
return false;
177+
178+
if(String::string_ends_with(type_name, "*")) {
179+
return (
180+
OGF::Meta::instance()->resolve_meta_type(type_name) !=
181+
nullptr
182+
);
183+
}
184+
185+
if(String::string_starts_with(type_name, "const ")) {
186+
return check_type(String::remove_prefix(type_name, "const "));
174187
}
188+
175189
MetaType* mtype = Meta::instance()->resolve_meta_type(type_name);
176190
if(mtype == nullptr) {
177191
return false;
178192
}
193+
179194
if(dynamic_cast<MetaClass*>(mtype) != nullptr) {
180195
return false;
181196
}
197+
182198
return true;
183199
}
184200

@@ -287,7 +303,6 @@ namespace {
287303

288304

289305
void generate_wrapper(MetaMethod* mmethod) {
290-
291306
MetaClass* mclass = mmethod->container_meta_class();
292307
bool is_in_class = (
293308
dynamic_cast<OGF::MetaNamespace*>(mclass) == nullptr
@@ -319,10 +334,8 @@ namespace {
319334
MetaArg* marg = mmethod->ith_arg(i);
320335
std::string type_name = marg->type_name();
321336
bool is_pointer = false;
322-
if(type_name == "char*") {
323-
type_name = "const char*"; // quick and dirty,
324-
// TODO: fix lang instead
325-
} else {
337+
338+
if(!OGF::String::string_starts_with(type_name, "const ")) {
326339
is_pointer = OGF::String::string_ends_with(type_name,"*");
327340
if(is_pointer) {
328341
// If type is a pointer to a struct or class, then
@@ -337,6 +350,7 @@ namespace {
337350
}
338351
}
339352
}
353+
340354
std::string default_value;
341355
if(is_pointer) {
342356
if(marg->has_default_value()) {
@@ -354,6 +368,11 @@ namespace {
354368
}
355369
}
356370
MetaType* mtype = Meta::instance()->resolve_meta_type(type_name);
371+
if(mtype == nullptr) {
372+
Logger::err("GomGen") << "FATAL: did not find type:"
373+
<< type_name
374+
<< std::endl;
375+
}
357376
geo_assert(mtype != nullptr);
358377

359378
arg_name.push_back(marg->name());
@@ -611,10 +630,15 @@ namespace {
611630
std::set<MetaType*> used_types_;
612631

613632
/**
614-
* \brief Quick fix for ignoring functions that manipulate
615-
* usupported types.
633+
* \brief Explicitly unsupported types.
616634
*/
617635
std::set<std::string> unsupported_types_;
636+
637+
/**
638+
* \brief Explicitly supported types.
639+
*/
640+
std::set<std::string> supported_types_;
641+
618642
};
619643
}
620644

src/bin/gomgen/gom_lang.cpp

100755100644
Lines changed: 72 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,6 @@ namespace {
133133
}
134134
}
135135

136-
inline std::string gom_type_name_from_string(String* s) {
137-
std::string result = std::string(Char(s));
138-
return result;
139-
}
140-
141136
inline std::string gom_value_from_string(String* s) {
142137
if(!Strncmp(s, "OGF::", 5)) {
143138
return std::string(Char(s)+5);
@@ -147,21 +142,6 @@ namespace {
147142

148143
void gom_msg(const char* msg, Node* n);
149144

150-
inline std::string gom_type_name(SwigType* ty) {
151-
// TODO: is there a simple way of getting the fully qualified
152-
// type name from Swig instead of adding one thing at a time
153-
// with SwigType_isxxx() ?
154-
String* s = SwigType_str(SwigType_base(ty),nullptr);
155-
std::string result = gom_type_name_from_string(s);
156-
if(SwigType_ispointer(ty)) {
157-
result = result + "*";
158-
}
159-
if(SwigType_isarray(ty)) {
160-
result = result + "[]";
161-
}
162-
return result;
163-
}
164-
165145
inline std::string gom_enum_name(Node* n) {
166146
String* s = Getattr(n, "name");
167147
if(s == nullptr) {
@@ -173,7 +153,7 @@ namespace {
173153

174154
inline OGF::MetaClass* gom_class_from_member(Node* n) {
175155
Node* clazz = Getattr(n,"parentNode");
176-
std::string class_name = gom_type_name_from_string(
156+
std::string class_name = gom_string(
177157
Getattr(clazz,"name")
178158
);
179159
OGF::MetaClass* result = dynamic_cast<OGF::MetaClass*>(
@@ -185,7 +165,7 @@ namespace {
185165

186166
inline OGF::MetaBuiltinStruct* gom_struct_from_member(Node* n) {
187167
Node* clazz = Getattr(n,"parentNode");
188-
std::string class_name = gom_type_name_from_string(
168+
std::string class_name = gom_string(
189169
Getattr(clazz,"name")
190170
);
191171
OGF::MetaBuiltinStruct* result = dynamic_cast<OGF::MetaBuiltinStruct*>(
@@ -258,39 +238,6 @@ namespace {
258238
}
259239
}
260240

261-
void copy_gom_args(Node* from, OGF::MetaMethod* to) {
262-
Parm* parms = Getattr(from,"parms");
263-
if(parms != nullptr) {
264-
int param_id = 1;
265-
for(Node* p = parms; p != nullptr; p = nextSibling(p)) {
266-
SwigType* type_in = Getattr(p,"type");
267-
String* name_in = Getattr(p,"name");
268-
String* value_in = Getattr(p,"value");
269-
270-
std::string name;
271-
if(name_in == nullptr) {
272-
OGF::Logger::warn("GomGen")
273-
<< "anonymous arg in signal/slot"
274-
<< std::endl;
275-
name = GEO::String::format("prm_%d",param_id);
276-
} else {
277-
name = gom_string(name_in);
278-
}
279-
280-
std::string type = gom_type_name(type_in);
281-
OGF::MetaArg arg(name, type);
282-
copy_gom_attributes(p, &arg);
283-
if(value_in != nullptr) {
284-
std::string default_value = gom_value_from_string(value_in);
285-
gom_unquote(default_value);
286-
//gom_unqualify(default_value);
287-
arg.default_value().set_value(default_value);
288-
}
289-
to->add_arg(arg);
290-
param_id++;
291-
}
292-
}
293-
}
294241

295242
//-------------------------------- Debugging ------------------------------
296243

@@ -913,7 +860,7 @@ namespace {
913860
virtual int classHandler(Node *n) {
914861

915862
if(!checkAttribute(n, "gom:kind", "class")) {
916-
std::string name = gom_type_name_from_string(Getattr(n, "name"));
863+
std::string name = gom_string(Getattr(n, "name"));
917864
if(mode_ == GOMGEN_LUAWRAP_MODE) {
918865
if(checkAttribute(n, "gom:kind", "namespace")) {
919866
OGF::MetaNamespace* mnamespace =
@@ -936,7 +883,7 @@ namespace {
936883
if(checkAttribute(n, "gom:kind", "class")) {
937884

938885
bool abstract = (checkAttribute(n, "gom:abstract", "true")!=0);
939-
std::string name = gom_type_name_from_string(Getattr(n, "name"));
886+
std::string name = gom_string(Getattr(n, "name"));
940887

941888
int nb_superclasses = 0;
942889
OGF::MetaClass* superclass = nullptr;
@@ -961,7 +908,7 @@ namespace {
961908
if(baselist != nullptr) {
962909
for(int i=0; i<Len(baselist); i++) {
963910
std::string cur_base_name =
964-
gom_type_name_from_string(Getitem(baselist, i));
911+
gom_string(Getitem(baselist, i));
965912

966913
OGF::MetaClass* cur_base =
967914
dynamic_cast<OGF::MetaClass*>(
@@ -1009,6 +956,10 @@ namespace {
1009956
new OGF::MetaBuiltinType(name + "*")
1010957
);
1011958

959+
OGF::Meta::instance()->bind_meta_type(
960+
new OGF::MetaBuiltinType("const " + name + "*")
961+
);
962+
1012963
OGF::MetaClass* mclass =
1013964
new OGF::MetaClass(name, superclass, abstract);
1014965
OGF::Meta::instance()->bind_meta_type(mclass);
@@ -1109,6 +1060,69 @@ namespace {
11091060
user_attributes = nullptr;
11101061
}
11111062

1063+
void copy_gom_args(Node* from, OGF::MetaMethod* to) {
1064+
Parm* parms = Getattr(from,"parms");
1065+
if(parms != nullptr) {
1066+
int param_id = 1;
1067+
for(Node* p = parms; p != nullptr; p = nextSibling(p)) {
1068+
SwigType* type_in = Getattr(p,"type");
1069+
String* name_in = Getattr(p,"name");
1070+
String* value_in = Getattr(p,"value");
1071+
1072+
std::string name;
1073+
if(name_in == nullptr) {
1074+
OGF::Logger::warn("GomGen")
1075+
<< "anonymous arg in signal/slot"
1076+
<< std::endl;
1077+
name = GEO::String::format("prm_%d",param_id);
1078+
} else {
1079+
name = gom_string(name_in);
1080+
}
1081+
1082+
std::string type = gom_type_name(type_in);
1083+
OGF::MetaArg arg(name, type);
1084+
copy_gom_attributes(p, &arg);
1085+
if(value_in != nullptr) {
1086+
std::string default_value =
1087+
gom_value_from_string(value_in);
1088+
gom_unquote(default_value);
1089+
//gom_unqualify(default_value);
1090+
arg.default_value().set_value(default_value);
1091+
}
1092+
to->add_arg(arg);
1093+
param_id++;
1094+
}
1095+
}
1096+
}
1097+
1098+
std::string gom_type_name(SwigType* ty) {
1099+
if(mode_ == GOMGEN_GOM_MODE) {
1100+
// in GOMGEN mode, use base type (and re-decorate with "*" if
1101+
// type was a pointer type).
1102+
String* s = SwigType_str(SwigType_base(ty),nullptr);
1103+
std::string result = gom_string(s);
1104+
if(SwigType_ispointer(ty)) {
1105+
result = result + "*";
1106+
}
1107+
return result;
1108+
} else {
1109+
std::string result = gom_string(SwigType_str(ty,nullptr));
1110+
result = OGF::String::remove_suffix(result, " const &");
1111+
if(OGF::String::string_ends_with(result, " *")) {
1112+
result = OGF::String::remove_suffix(result, " *");
1113+
if(OGF::String::string_ends_with(result, " const")) {
1114+
result = OGF::String::remove_suffix(result, " const");
1115+
result = "const " + result;
1116+
}
1117+
result += "*";
1118+
}
1119+
if(result == "char const*") {
1120+
result = "const char*";
1121+
}
1122+
return result;
1123+
}
1124+
}
1125+
11121126
private:
11131127
GomMemberType gom_member_type;
11141128
bool gomclass_flag;

0 commit comments

Comments
 (0)