genbindings: add c++ includes for all referenced Qt types

This commit is contained in:
mappu 2024-08-14 17:31:30 +12:00
parent c7f119df80
commit 21f1914244

View File

@ -130,28 +130,8 @@ func emitParametersCABI2CppForwarding(params []CppParameter) (preamble string, f
return preamble, strings.Join(tmp, ", ") return preamble, strings.Join(tmp, ", ")
} }
func emitBindingHeader(src *CppParsedHeader, filename string) (string, error) { func getReferencedTypes(src *CppParsedHeader) []string {
ret := strings.Builder{} // Find all referenced Qt types in this file
includeGuard := "GEN_" + strings.ToUpper(strings.Replace(filename, `.`, `_`, -1))
ret.WriteString(`#ifndef ` + includeGuard + `
#define ` + includeGuard + `
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#ifdef __cplusplus
extern "C" {
#endif
`)
// Find all referenced Qt types in this file, and generate PVoid typedefs
// for them
foundTypes := map[string]struct{}{} foundTypes := map[string]struct{}{}
for _, c := range src.Classes { for _, c := range src.Classes {
foundTypes[c.ClassName] = struct{}{} foundTypes[c.ClassName] = struct{}{}
@ -175,16 +155,52 @@ extern "C" {
} }
foundTypesList := make([]string, 0, len(foundTypes)) foundTypesList := make([]string, 0, len(foundTypes))
for ft := range foundTypes { for ft := range foundTypes {
foundTypesList = append(foundTypesList, ft)
}
sort.Strings(foundTypesList)
for _, ft := range foundTypesList {
if strings.HasPrefix(ft, "QList<") { if strings.HasPrefix(ft, "QList<") {
continue continue
} }
ret.WriteString(`typedef void* P` + ft + ";\n") foundTypesList = append(foundTypesList, ft)
} }
sort.Strings(foundTypesList)
return foundTypesList
}
func emitBindingHeader(src *CppParsedHeader, filename string) (string, error) {
ret := strings.Builder{}
includeGuard := "GEN_" + strings.ToUpper(strings.Replace(filename, `.`, `_`, -1))
ret.WriteString(`#ifndef ` + includeGuard + `
#define ` + includeGuard + `
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#ifdef __cplusplus
extern "C" {
#endif
`)
foundTypesList := getReferencedTypes(src)
ret.WriteString("#ifdef __cplusplus\n")
for _, ft := range foundTypesList {
ret.WriteString(`class ` + ft + ";\n")
}
ret.WriteString("#else\n")
for _, ft := range foundTypesList {
ret.WriteString(`typedef struct ` + ft + " " + ft + ";\n")
}
ret.WriteString("#endif\n")
ret.WriteString("\n") ret.WriteString("\n")
@ -222,6 +238,12 @@ func emitBindingCpp(src *CppParsedHeader, filename string) (string, error) {
`) `)
for _, ref := range getReferencedTypes(src) {
ret.WriteString(`#include <` + ref + ">\n")
}
ret.WriteString("\n")
for _, c := range src.Classes { for _, c := range src.Classes {
for i, ctor := range c.Ctors { for i, ctor := range c.Ctors {