2015-10-24 18:18:24 +03:00
|
|
|
package templater
|
|
|
|
|
|
|
|
import (
|
2016-04-28 20:43:44 +03:00
|
|
|
"bytes"
|
2017-01-29 20:43:19 +03:00
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/hex"
|
2015-10-24 18:18:24 +03:00
|
|
|
"fmt"
|
2015-12-06 02:56:16 +03:00
|
|
|
"sort"
|
2015-10-24 18:18:24 +03:00
|
|
|
"strings"
|
|
|
|
|
2017-07-19 19:45:25 +03:00
|
|
|
"github.com/therecipe/qt/internal/binding/converter"
|
2015-10-24 18:18:24 +03:00
|
|
|
"github.com/therecipe/qt/internal/binding/parser"
|
2017-01-27 22:22:58 +03:00
|
|
|
"github.com/therecipe/qt/internal/utils"
|
2015-10-24 18:18:24 +03:00
|
|
|
)
|
|
|
|
|
2017-05-16 01:06:35 +03:00
|
|
|
func CppTemplate(module string, mode int, target, tags string) []byte {
|
2017-03-28 18:41:57 +03:00
|
|
|
utils.Log.WithField("module", module).Debug("generating cpp")
|
2017-03-21 00:27:56 +03:00
|
|
|
parser.State.Target = target
|
2017-01-27 22:22:58 +03:00
|
|
|
|
2016-04-28 20:43:44 +03:00
|
|
|
var bb = new(bytes.Buffer)
|
|
|
|
defer bb.Reset()
|
2016-01-08 04:44:20 +03:00
|
|
|
|
2017-03-04 21:01:53 +03:00
|
|
|
if mode != MOC {
|
2016-12-31 01:37:32 +03:00
|
|
|
module = "Qt" + module
|
2017-01-29 20:43:19 +03:00
|
|
|
} else {
|
2017-02-15 19:48:35 +03:00
|
|
|
for _, c := range parser.SortedClassNamesForModule(module, true) {
|
2017-01-29 20:43:19 +03:00
|
|
|
var class, e = parser.State.ClassMap[c]
|
|
|
|
if !e {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var typeMap = make(map[string]string)
|
|
|
|
for _, f := range class.Functions {
|
|
|
|
if parser.IsPackedMap(f.Output) {
|
|
|
|
var tHash = sha1.New()
|
|
|
|
tHash.Write([]byte(f.Output))
|
|
|
|
typeMap[f.Output] = hex.EncodeToString(tHash.Sum(nil)[:3])
|
|
|
|
}
|
|
|
|
for _, p := range f.Parameters {
|
|
|
|
if parser.IsPackedMap(p.Value) {
|
|
|
|
var tHash = sha1.New()
|
|
|
|
tHash.Write([]byte(p.Value))
|
|
|
|
typeMap[p.Value] = hex.EncodeToString(tHash.Sum(nil)[:3])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 01:01:46 +03:00
|
|
|
for _, p := range class.Properties {
|
|
|
|
if parser.IsPackedMap(p.Output) {
|
|
|
|
var tHash = sha1.New()
|
|
|
|
tHash.Write([]byte(p.Output))
|
|
|
|
typeMap[p.Output] = hex.EncodeToString(tHash.Sum(nil)[:3])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-29 20:43:19 +03:00
|
|
|
for typ, hash := range typeMap {
|
|
|
|
fmt.Fprintf(bb, "typedef %v type%v;\n", typ, hash)
|
|
|
|
}
|
|
|
|
}
|
2016-12-31 01:37:32 +03:00
|
|
|
}
|
|
|
|
|
2016-12-17 03:18:44 +03:00
|
|
|
if module == "QtCharts" || module == "QtDataVisualization" {
|
2017-02-15 19:48:35 +03:00
|
|
|
for _, classname := range parser.SortedClassNamesForModule(module, true) {
|
2016-12-17 03:18:44 +03:00
|
|
|
fmt.Fprintf(bb, "typedef %v::%v %v;\n", module, classname, classname)
|
|
|
|
}
|
|
|
|
fmt.Fprint(bb, "\n")
|
|
|
|
}
|
|
|
|
|
2017-02-15 19:48:35 +03:00
|
|
|
for _, className := range parser.SortedClassNamesForModule(module, true) {
|
2017-01-02 19:01:18 +03:00
|
|
|
var class = parser.State.ClassMap[className]
|
2016-04-16 19:48:26 +03:00
|
|
|
|
2017-01-02 19:01:18 +03:00
|
|
|
if class.IsSupported() {
|
2016-04-28 20:43:44 +03:00
|
|
|
|
2017-03-04 21:01:53 +03:00
|
|
|
if class.HasCallbackFunctions() || mode == MOC {
|
2015-12-15 04:01:51 +03:00
|
|
|
|
2017-01-13 22:14:08 +03:00
|
|
|
//TODO: split
|
2016-04-28 20:43:44 +03:00
|
|
|
fmt.Fprintf(bb,
|
|
|
|
`class %v%v: public %v
|
|
|
|
{
|
|
|
|
%vpublic:
|
|
|
|
`,
|
|
|
|
func() string {
|
2017-03-04 21:01:53 +03:00
|
|
|
if mode == MOC {
|
2016-04-28 20:43:44 +03:00
|
|
|
return ""
|
2016-01-04 04:05:31 +03:00
|
|
|
}
|
2016-04-28 20:43:44 +03:00
|
|
|
return "My"
|
|
|
|
}(),
|
|
|
|
|
|
|
|
class.Name,
|
|
|
|
|
|
|
|
func() string {
|
2017-03-04 21:01:53 +03:00
|
|
|
if mode == MOC {
|
2016-04-28 20:43:44 +03:00
|
|
|
return class.GetBases()[0]
|
|
|
|
}
|
|
|
|
return class.Name
|
|
|
|
}(),
|
|
|
|
|
|
|
|
func() string {
|
2017-03-04 21:01:53 +03:00
|
|
|
if mode == MOC {
|
2017-01-13 22:14:08 +03:00
|
|
|
var bb = new(bytes.Buffer)
|
|
|
|
defer bb.Reset()
|
|
|
|
fmt.Fprintln(bb, "Q_OBJECT")
|
|
|
|
|
|
|
|
for _, p := range class.Properties {
|
2017-02-03 01:01:46 +03:00
|
|
|
|
|
|
|
var ty = p.Output
|
|
|
|
if parser.IsPackedMap(p.Output) {
|
|
|
|
var tHash = sha1.New()
|
|
|
|
tHash.Write([]byte(p.Output))
|
|
|
|
ty = fmt.Sprintf("type%v", hex.EncodeToString(tHash.Sum(nil)[:3]))
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(bb, "Q_PROPERTY(%v %v READ %v WRITE set%v NOTIFY %vChanged)\n", ty, p.Name,
|
2017-01-13 22:14:08 +03:00
|
|
|
func() string {
|
2017-06-21 23:05:59 +03:00
|
|
|
if p.Output == "bool" && !strings.HasPrefix(strings.ToLower(p.Name), "is") {
|
2017-01-13 22:14:08 +03:00
|
|
|
return "is" + strings.Title(p.Name)
|
|
|
|
}
|
|
|
|
return p.Name
|
|
|
|
}(), strings.Title(p.Name), p.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bb.String()
|
2016-04-28 20:43:44 +03:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}())
|
|
|
|
|
|
|
|
if !hasUnimplementedPureVirtualFunctions(class.Name) {
|
|
|
|
for _, function := range class.Functions {
|
2017-01-18 21:28:40 +03:00
|
|
|
if function.Meta != parser.CONSTRUCTOR || !function.IsSupported() {
|
|
|
|
continue
|
|
|
|
}
|
2016-01-04 04:05:31 +03:00
|
|
|
|
2017-05-31 16:05:10 +03:00
|
|
|
var out = fmt.Sprintf("\t%v%v(%v) : %v(%v) {%v};\n",
|
2017-01-18 21:28:40 +03:00
|
|
|
func() string {
|
2017-03-04 21:01:53 +03:00
|
|
|
if mode == MOC {
|
2017-01-18 21:28:40 +03:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return "My"
|
|
|
|
}(),
|
2016-04-28 20:43:44 +03:00
|
|
|
|
2017-01-18 21:28:40 +03:00
|
|
|
function.ClassName(),
|
2016-04-28 20:43:44 +03:00
|
|
|
|
2017-04-05 18:13:51 +03:00
|
|
|
func() string {
|
|
|
|
var input []string
|
|
|
|
for _, p := range function.OgParameters {
|
2017-08-01 20:43:48 +03:00
|
|
|
name := parser.CleanName(p.Name, p.Value)
|
2017-04-05 18:13:51 +03:00
|
|
|
if p.Default != "" {
|
|
|
|
if strings.HasSuffix(p.Value, "*") || strings.HasSuffix(p.Value, "&") {
|
2017-08-01 20:43:48 +03:00
|
|
|
input = append(input, p.Value+name+" = "+p.Default)
|
2017-04-05 18:13:51 +03:00
|
|
|
} else {
|
2017-08-01 20:43:48 +03:00
|
|
|
input = append(input, p.Value+" "+name+" = "+p.Default)
|
2017-04-05 18:13:51 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if strings.HasSuffix(p.Value, "*") || strings.HasSuffix(p.Value, "&") {
|
2017-08-01 20:43:48 +03:00
|
|
|
input = append(input, p.Value+name)
|
2017-04-05 18:13:51 +03:00
|
|
|
} else {
|
2017-08-01 20:43:48 +03:00
|
|
|
input = append(input, p.Value+" "+name)
|
2017-04-05 18:13:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(input, ", ")
|
|
|
|
}(),
|
2016-04-28 20:43:44 +03:00
|
|
|
|
2017-01-18 21:28:40 +03:00
|
|
|
func() string {
|
2017-03-04 21:01:53 +03:00
|
|
|
if mode == MOC {
|
2017-01-18 21:28:40 +03:00
|
|
|
return class.GetBases()[0]
|
|
|
|
}
|
|
|
|
return function.ClassName()
|
|
|
|
}(),
|
2016-04-28 20:43:44 +03:00
|
|
|
|
2017-08-01 20:43:48 +03:00
|
|
|
func() string {
|
|
|
|
input := make([]string, len(function.Parameters))
|
|
|
|
for i, p := range function.Parameters {
|
|
|
|
input[i] = parser.CleanName(p.Name, p.Value)
|
|
|
|
}
|
|
|
|
return strings.Join(input, ", ")
|
|
|
|
}(),
|
2017-05-31 16:05:10 +03:00
|
|
|
|
|
|
|
func() string {
|
2017-06-10 22:37:03 +03:00
|
|
|
var pre string
|
|
|
|
if class.IsSubClassOfQObject() {
|
2017-06-14 17:12:17 +03:00
|
|
|
pre = fmt.Sprintf("%[1]v_%[1]v_QRegisterMetaType();", className)
|
2017-06-10 22:37:03 +03:00
|
|
|
}
|
2017-05-31 16:05:10 +03:00
|
|
|
if mode != MOC {
|
2017-06-10 22:37:03 +03:00
|
|
|
return pre
|
2017-05-31 16:05:10 +03:00
|
|
|
}
|
2017-06-23 23:07:09 +03:00
|
|
|
return fmt.Sprintf("qRegisterMetaType<quintptr>(\"quintptr\");%v%[2]v_%[2]v_QRegisterMetaTypes();callback%[2]v_Constructor(this);", pre, className)
|
2017-05-31 16:05:10 +03:00
|
|
|
}(),
|
2017-01-18 21:28:40 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
fmt.Fprint(bb, out)
|
2015-12-15 04:01:51 +03:00
|
|
|
}
|
2016-01-04 04:05:31 +03:00
|
|
|
}
|
2015-12-15 04:01:51 +03:00
|
|
|
|
2017-02-15 19:48:35 +03:00
|
|
|
//callback functions
|
|
|
|
var implementedVirtuals = make(map[string]struct{})
|
|
|
|
for i, parentClassName := range append([]string{class.Name}, class.GetAllBases()...) {
|
|
|
|
var parentClass, e = parser.State.ClassMap[parentClassName]
|
|
|
|
if !e || !parentClass.IsSupported() {
|
|
|
|
continue
|
2015-12-23 18:22:00 +03:00
|
|
|
}
|
|
|
|
|
2017-02-15 19:48:35 +03:00
|
|
|
for _, f := range parentClass.Functions {
|
|
|
|
var _, e = implementedVirtuals[f.Name+f.OverloadNumber]
|
|
|
|
if e || !f.IsSupported() {
|
|
|
|
continue
|
|
|
|
}
|
2016-04-28 20:43:44 +03:00
|
|
|
|
2017-03-23 02:45:45 +03:00
|
|
|
if (parentClass.Module == parser.MOC || parentClass.Pkg != "") && f.Meta == parser.SLOT {
|
2017-02-15 19:48:35 +03:00
|
|
|
continue
|
|
|
|
}
|
2016-04-28 20:43:44 +03:00
|
|
|
|
2017-02-15 19:48:35 +03:00
|
|
|
if i > 0 && (f.Meta == parser.CONSTRUCTOR || f.Meta == parser.DESTRUCTOR) {
|
|
|
|
continue
|
2015-12-06 02:56:16 +03:00
|
|
|
}
|
2016-04-28 20:43:44 +03:00
|
|
|
|
2017-05-10 17:29:50 +03:00
|
|
|
implementedVirtuals[f.Name+f.OverloadNumber] = struct{}{}
|
|
|
|
|
2017-02-15 19:48:35 +03:00
|
|
|
var f = *f
|
|
|
|
f.SignalMode = parser.CALLBACK
|
|
|
|
f.Fullname = fmt.Sprintf("%v::%v", class.Name, f.Name)
|
|
|
|
f.Fullname = fmt.Sprintf("%v::%v", f.FindDeepestImplementation(), f.Name)
|
|
|
|
|
|
|
|
if f.Meta == parser.SLOT || f.Meta == parser.SIGNAL || f.Virtual == parser.IMPURE || f.Virtual == parser.PURE {
|
|
|
|
fmt.Fprintf(bb, "\t%v\n", cppFunctionCallback(&f))
|
|
|
|
}
|
2015-10-24 18:18:24 +03:00
|
|
|
}
|
|
|
|
}
|
2016-01-04 04:05:31 +03:00
|
|
|
|
2017-03-04 21:01:53 +03:00
|
|
|
if mode == MOC {
|
2017-01-13 22:14:08 +03:00
|
|
|
for _, p := range class.Properties {
|
2017-02-03 01:01:46 +03:00
|
|
|
|
|
|
|
var ty = p.Output
|
|
|
|
if parser.IsPackedMap(p.Output) {
|
|
|
|
var tHash = sha1.New()
|
|
|
|
tHash.Write([]byte(p.Output))
|
|
|
|
ty = fmt.Sprintf("type%v", hex.EncodeToString(tHash.Sum(nil)[:3]))
|
|
|
|
}
|
|
|
|
|
2017-06-12 20:52:01 +03:00
|
|
|
fmt.Fprintf(bb, "\t%v %v%v() { return _%v; };\n",
|
|
|
|
ty,
|
|
|
|
func() string {
|
2017-06-21 23:05:59 +03:00
|
|
|
if p.Output == "bool" && !strings.HasPrefix(strings.ToLower(p.Name), "is") {
|
2017-06-12 20:52:01 +03:00
|
|
|
return "is" + strings.Title(p.Name)
|
|
|
|
}
|
|
|
|
return p.Name
|
|
|
|
}(),
|
|
|
|
func() string {
|
|
|
|
if p.IsMocSynthetic {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return "Default"
|
|
|
|
}(),
|
|
|
|
p.Name,
|
|
|
|
)
|
|
|
|
fmt.Fprintf(bb, "\tvoid set%v%v(%v p) { if (p != _%v) { _%v = p; %vChanged(_%v); } };\n",
|
|
|
|
strings.Title(p.Name),
|
|
|
|
func() string {
|
|
|
|
if p.IsMocSynthetic {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return "Default"
|
|
|
|
}(),
|
|
|
|
ty,
|
|
|
|
p.Name,
|
|
|
|
p.Name,
|
|
|
|
p.Name,
|
|
|
|
p.Name,
|
|
|
|
)
|
2017-01-13 22:14:08 +03:00
|
|
|
}
|
|
|
|
|
2016-04-28 20:43:44 +03:00
|
|
|
fmt.Fprintln(bb, "signals:")
|
|
|
|
for _, function := range class.Functions {
|
|
|
|
if function.Meta == parser.SIGNAL {
|
|
|
|
var function = *function
|
|
|
|
function.Meta = parser.SLOT
|
|
|
|
fmt.Fprintf(bb, "\t%v;\n", cppFunctionCallbackHeader(&function))
|
2016-04-16 19:48:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 20:43:44 +03:00
|
|
|
fmt.Fprintln(bb, "public slots:")
|
|
|
|
for _, function := range class.Functions {
|
|
|
|
if function.Meta == parser.SLOT {
|
|
|
|
fmt.Fprintf(bb, "\t%v\n", cppFunctionCallback(function))
|
2016-04-16 19:48:26 +03:00
|
|
|
}
|
|
|
|
}
|
2017-01-13 22:14:08 +03:00
|
|
|
|
|
|
|
fmt.Fprintln(bb, "private:")
|
|
|
|
for _, p := range class.Properties {
|
2017-02-03 01:01:46 +03:00
|
|
|
var ty = p.Output
|
|
|
|
if parser.IsPackedMap(p.Output) {
|
|
|
|
var tHash = sha1.New()
|
|
|
|
tHash.Write([]byte(p.Output))
|
|
|
|
ty = fmt.Sprintf("type%v", hex.EncodeToString(tHash.Sum(nil)[:3]))
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(bb, "\t%v _%v;\n", ty, p.Name)
|
2017-01-13 22:14:08 +03:00
|
|
|
}
|
2016-04-16 19:48:26 +03:00
|
|
|
}
|
|
|
|
|
2016-04-28 20:43:44 +03:00
|
|
|
fmt.Fprint(bb, "};\n\n")
|
2015-10-24 18:18:24 +03:00
|
|
|
}
|
2017-06-10 22:37:03 +03:00
|
|
|
if class.IsSubClassOfQObject() {
|
|
|
|
fmt.Fprintf(bb, "Q_DECLARE_METATYPE(%v%v*)\n\n",
|
|
|
|
func() string {
|
|
|
|
if mode != MOC {
|
|
|
|
return "My"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}(), class.Name)
|
|
|
|
|
|
|
|
if mode != MOC {
|
2017-06-12 17:32:15 +03:00
|
|
|
if strings.HasPrefix(class.Name, "QMac") && !strings.HasPrefix(parser.State.ClassMap[class.Name].Module, "QtMac") {
|
2017-07-12 00:33:08 +03:00
|
|
|
fmt.Fprintf(bb, "int %[1]v_%[1]v_QRegisterMetaType(){\n\t#ifdef Q_OS_OSX\n\t\tqRegisterMetaType<%[1]v*>(); return qRegisterMetaType<My%[1]v*>();\n\t#else\n\t\treturn 0;\n\t#endif\n}\n\n", class.Name)
|
2017-06-12 17:32:15 +03:00
|
|
|
} else {
|
2017-07-12 00:33:08 +03:00
|
|
|
fmt.Fprintf(bb, "int %[1]v_%[1]v_QRegisterMetaType(){qRegisterMetaType<%[1]v*>(); return qRegisterMetaType<My%[1]v*>();}\n\n", class.Name)
|
2017-06-12 17:32:15 +03:00
|
|
|
}
|
2017-06-14 17:12:17 +03:00
|
|
|
} else {
|
|
|
|
var typeMap = make(map[string]string)
|
|
|
|
for _, f := range class.Functions {
|
2017-06-23 23:07:09 +03:00
|
|
|
if parser.IsPackedMap(f.Output) {
|
2017-06-14 17:12:17 +03:00
|
|
|
var tHash = sha1.New()
|
|
|
|
tHash.Write([]byte(f.Output))
|
|
|
|
typeMap[f.Output] = hex.EncodeToString(tHash.Sum(nil)[:3])
|
|
|
|
}
|
|
|
|
for _, p := range f.Parameters {
|
2017-06-23 23:07:09 +03:00
|
|
|
if parser.IsPackedMap(p.Value) {
|
2017-06-14 17:12:17 +03:00
|
|
|
var tHash = sha1.New()
|
|
|
|
tHash.Write([]byte(p.Value))
|
|
|
|
typeMap[p.Value] = hex.EncodeToString(tHash.Sum(nil)[:3])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-19 19:45:25 +03:00
|
|
|
propTypes := make(map[string]struct{})
|
2017-06-14 17:12:17 +03:00
|
|
|
for _, p := range class.Properties {
|
2017-06-23 23:07:09 +03:00
|
|
|
if parser.IsPackedMap(p.Output) {
|
2017-06-14 17:12:17 +03:00
|
|
|
var tHash = sha1.New()
|
|
|
|
tHash.Write([]byte(p.Output))
|
|
|
|
typeMap[p.Output] = hex.EncodeToString(tHash.Sum(nil)[:3])
|
|
|
|
}
|
2017-07-19 19:45:25 +03:00
|
|
|
if o := converter.CppRegisterMetaTypeProp(p); o != "" {
|
|
|
|
propTypes[o] = struct{}{}
|
|
|
|
}
|
2017-06-14 17:12:17 +03:00
|
|
|
}
|
|
|
|
|
2017-06-23 23:07:09 +03:00
|
|
|
for _, hash := range typeMap {
|
2017-06-28 20:58:42 +03:00
|
|
|
if hash == "30021d" || //QHash<quintptr, quintptr>
|
|
|
|
hash == "95ad14" || //QHash<qint32, quintptr>
|
|
|
|
hash == "d01680" { //QHash<qint32, QByteArray>
|
2017-06-23 23:07:09 +03:00
|
|
|
continue
|
2017-06-14 17:12:17 +03:00
|
|
|
}
|
2017-06-23 23:07:09 +03:00
|
|
|
fmt.Fprintf(bb, "Q_DECLARE_METATYPE(type%v)\n", hash)
|
2017-06-14 17:12:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(bb, "\nvoid %[1]v_%[1]v_QRegisterMetaTypes() {\n", class.Name)
|
2017-06-23 23:07:09 +03:00
|
|
|
for _, hash := range typeMap {
|
|
|
|
fmt.Fprintf(bb, "\tqRegisterMetaType<type%v>(\"type%v\");\n", hash, hash)
|
2017-06-14 17:12:17 +03:00
|
|
|
}
|
2017-07-19 19:45:25 +03:00
|
|
|
for t := range propTypes {
|
|
|
|
fmt.Fprintf(bb, "\tqRegisterMetaType<%v>();\n", t)
|
|
|
|
}
|
2017-06-14 17:12:17 +03:00
|
|
|
fmt.Fprint(bb, "}\n\n")
|
2017-06-10 22:37:03 +03:00
|
|
|
}
|
2016-11-18 22:10:11 +03:00
|
|
|
}
|
2015-10-24 18:18:24 +03:00
|
|
|
}
|
2016-04-28 20:43:44 +03:00
|
|
|
|
2017-03-04 21:01:53 +03:00
|
|
|
if mode != MOC {
|
2017-02-15 19:48:35 +03:00
|
|
|
cTemplate(bb, class, cppEnum, cppFunction, "\n\n", false)
|
|
|
|
}
|
2017-01-18 21:28:40 +03:00
|
|
|
}
|
2016-12-14 23:12:51 +03:00
|
|
|
|
2017-03-04 21:01:53 +03:00
|
|
|
if mode == MOC {
|
2017-02-15 19:48:35 +03:00
|
|
|
for _, className := range parser.SortedClassNamesForModule(module, true) {
|
|
|
|
var class = parser.State.ClassMap[className]
|
|
|
|
|
|
|
|
if class.IsSupported() {
|
|
|
|
cTemplate(bb, class, cppEnum, cppFunction, "\n\n", false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 21:28:40 +03:00
|
|
|
fmt.Fprintln(bb, "#include \"moc_moc.h\"")
|
2015-10-24 18:18:24 +03:00
|
|
|
}
|
|
|
|
|
2017-05-16 01:06:35 +03:00
|
|
|
return preambleCpp(module, bb.Bytes(), mode, tags)
|
2015-10-24 18:18:24 +03:00
|
|
|
}
|
|
|
|
|
2017-05-16 01:06:35 +03:00
|
|
|
func preambleCpp(module string, input []byte, mode int, tags string) []byte {
|
2016-04-28 20:43:44 +03:00
|
|
|
var bb = new(bytes.Buffer)
|
|
|
|
defer bb.Reset()
|
2015-10-24 18:18:24 +03:00
|
|
|
|
2017-04-17 22:13:53 +03:00
|
|
|
if mode == MOC {
|
|
|
|
libsm := make(map[string]struct{}, 0)
|
|
|
|
for _, c := range parser.State.ClassMap {
|
|
|
|
if c.Pkg != "" && c.IsSubClassOfQObject() {
|
|
|
|
libsm[c.Module] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var libs []string
|
|
|
|
for k := range libsm {
|
|
|
|
libs = append(libs, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range parser.SortedClassesForModule(strings.Join(libs, ","), true) {
|
|
|
|
if c.Pkg == "" || !strings.Contains(string(input), c.Name) /*|| !c.HasConstructor()*/ {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(bb, "class %v: public %v{\npublic:\n", c.Name, c.GetBases()[0])
|
|
|
|
|
|
|
|
for _, function := range c.Functions {
|
|
|
|
if function.Meta != parser.CONSTRUCTOR || !function.IsSupported() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var input = make([]string, len(function.Parameters))
|
|
|
|
for i, p := range function.Parameters {
|
|
|
|
input[i] = p.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(bb, "\t%v%v(%v) : %v(%v) {};\n",
|
|
|
|
func() string {
|
|
|
|
if mode == MOC {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return "My"
|
|
|
|
}(),
|
|
|
|
|
|
|
|
function.ClassName(),
|
|
|
|
|
|
|
|
strings.Split(strings.Split(function.Signature, "(")[1], ")")[0],
|
|
|
|
|
|
|
|
func() string {
|
|
|
|
if mode == MOC {
|
|
|
|
return c.GetBases()[0]
|
|
|
|
}
|
|
|
|
return function.ClassName()
|
|
|
|
}(),
|
|
|
|
|
|
|
|
strings.Join(input, ", "),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
fmt.Fprint(bb, "\n};\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprint(bb, "\n")
|
|
|
|
|
|
|
|
bb.Write(input)
|
|
|
|
input = []byte(bb.String())
|
|
|
|
bb.Reset()
|
|
|
|
}
|
|
|
|
|
2016-05-28 19:18:42 +03:00
|
|
|
fmt.Fprintf(bb, `%v
|
|
|
|
|
|
|
|
#define protected public
|
2016-04-28 20:43:44 +03:00
|
|
|
#define private public
|
2015-10-24 18:18:24 +03:00
|
|
|
|
2016-04-28 20:43:44 +03:00
|
|
|
#include "%v.h"
|
|
|
|
#include "_cgo_export.h"
|
2015-12-06 02:56:16 +03:00
|
|
|
|
2016-04-28 20:43:44 +03:00
|
|
|
`,
|
2017-05-16 01:06:35 +03:00
|
|
|
buildTags(module, false, mode, tags),
|
2016-05-28 19:18:42 +03:00
|
|
|
|
2016-04-28 20:43:44 +03:00
|
|
|
func() string {
|
|
|
|
switch module {
|
|
|
|
case "QtAndroidExtras":
|
|
|
|
{
|
2017-01-02 19:01:18 +03:00
|
|
|
return fmt.Sprintf("%v_android", goModule(module))
|
2016-04-28 20:43:44 +03:00
|
|
|
}
|
2016-01-04 04:05:31 +03:00
|
|
|
|
2016-06-19 05:24:38 +03:00
|
|
|
case "QtSailfish":
|
|
|
|
{
|
2017-01-02 19:01:18 +03:00
|
|
|
return fmt.Sprintf("%v_sailfish", goModule(module))
|
2016-06-19 05:24:38 +03:00
|
|
|
}
|
|
|
|
|
2016-04-28 20:43:44 +03:00
|
|
|
default:
|
|
|
|
{
|
2017-03-04 21:01:53 +03:00
|
|
|
if mode == MINIMAL {
|
2017-01-02 19:01:18 +03:00
|
|
|
return fmt.Sprintf("%v-minimal", goModule(module))
|
2016-10-14 20:06:35 +03:00
|
|
|
}
|
2017-01-02 19:01:18 +03:00
|
|
|
|
2017-03-04 21:01:53 +03:00
|
|
|
if mode == MOC {
|
2017-01-02 19:01:18 +03:00
|
|
|
return "moc"
|
|
|
|
}
|
|
|
|
|
|
|
|
return goModule(module)
|
2016-04-28 20:43:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}(),
|
|
|
|
)
|
|
|
|
|
|
|
|
var classes = make([]string, 0)
|
2017-01-02 19:01:18 +03:00
|
|
|
for _, class := range parser.State.ClassMap {
|
2017-02-17 20:55:39 +03:00
|
|
|
if strings.Contains(string(input), class.Name) && class.Module != parser.MOC {
|
2016-04-28 20:43:44 +03:00
|
|
|
classes = append(classes, class.Name)
|
2016-04-16 19:48:26 +03:00
|
|
|
}
|
2015-12-06 02:56:16 +03:00
|
|
|
}
|
2016-04-28 20:43:44 +03:00
|
|
|
sort.Stable(sort.StringSlice(classes))
|
2015-12-06 02:56:16 +03:00
|
|
|
|
2016-04-28 20:43:44 +03:00
|
|
|
for _, class := range classes {
|
2016-06-19 05:24:38 +03:00
|
|
|
if class == "SailfishApp" {
|
2017-01-22 20:34:39 +03:00
|
|
|
fmt.Fprint(bb, "#include <sailfishapp.h>\n")
|
2016-06-19 05:24:38 +03:00
|
|
|
} else {
|
2017-02-17 20:55:39 +03:00
|
|
|
var c, _ = parser.State.ClassMap[class]
|
2017-03-23 02:45:45 +03:00
|
|
|
if strings.HasPrefix(c.Module, "custom_") {
|
|
|
|
continue
|
|
|
|
}
|
2017-02-17 20:55:39 +03:00
|
|
|
switch c.Name {
|
|
|
|
case
|
|
|
|
"Qt",
|
|
|
|
"QPdf",
|
|
|
|
"QDBus",
|
|
|
|
"QAudio",
|
|
|
|
"QMultimedia",
|
|
|
|
"QSsl",
|
|
|
|
"QPrint",
|
|
|
|
"QScript",
|
|
|
|
"QSql",
|
|
|
|
"QTest",
|
|
|
|
"QWebSocketProtocol",
|
|
|
|
"OSXBluetooth",
|
|
|
|
"QBluetooth",
|
|
|
|
"PaintContext",
|
|
|
|
"QPlatformGraphicsBuffer",
|
2017-06-10 22:37:03 +03:00
|
|
|
"QDBusPendingReplyTypes",
|
|
|
|
"QRemoteObjectPackets",
|
2017-12-30 15:32:06 +03:00
|
|
|
"QRemoteObjectStringLiterals",
|
2018-01-17 22:38:03 +03:00
|
|
|
"ui",
|
|
|
|
"QStringList":
|
2017-02-17 20:55:39 +03:00
|
|
|
{
|
|
|
|
continue
|
|
|
|
}
|
2016-12-01 20:35:01 +03:00
|
|
|
}
|
2018-03-26 10:59:04 +03:00
|
|
|
|
|
|
|
if utils.QT_VERSION_NUM() <= 5042 {
|
|
|
|
switch c.Name {
|
|
|
|
case
|
|
|
|
"QQmlAbstractProfilerAdapter",
|
|
|
|
"QQuickAsyncImageProvider",
|
|
|
|
"QQuickImageResponse":
|
|
|
|
{
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 20:55:39 +03:00
|
|
|
fmt.Fprintf(bb, "#include <%v>\n", class)
|
|
|
|
}
|
2017-03-20 20:33:55 +03:00
|
|
|
|
|
|
|
c, ok := parser.State.ClassMap[class]
|
|
|
|
if ok && !strings.Contains(strings.Join(parser.LibDeps[strings.TrimPrefix(module, "Qt")], " "), strings.TrimPrefix(c.Module, "Qt")) {
|
2017-03-23 02:45:45 +03:00
|
|
|
if strings.HasPrefix(c.Module, "custom_") {
|
|
|
|
continue
|
|
|
|
}
|
2017-03-20 20:33:55 +03:00
|
|
|
|
2017-03-23 02:45:45 +03:00
|
|
|
utils.Log.Debugf("%v add dependency: %v", module, c.Module)
|
2017-03-20 20:33:55 +03:00
|
|
|
parser.LibDeps[strings.TrimPrefix(module, "Qt")] = append(parser.LibDeps[strings.TrimPrefix(module, "Qt")], strings.TrimPrefix(c.Module, "Qt"))
|
|
|
|
switch c.Module {
|
|
|
|
case "QtMultimedia":
|
|
|
|
parser.LibDeps[strings.TrimPrefix(module, "Qt")] = append(parser.LibDeps[strings.TrimPrefix(module, "Qt")], "MultimediaWidgets")
|
|
|
|
case "QtWebEngine":
|
|
|
|
parser.LibDeps[strings.TrimPrefix(module, "Qt")] = append(parser.LibDeps[strings.TrimPrefix(module, "Qt")], "WebEngineWidgets")
|
|
|
|
case "QtQuick":
|
|
|
|
parser.LibDeps[strings.TrimPrefix(module, "Qt")] = append(parser.LibDeps[strings.TrimPrefix(module, "Qt")], "QuickWidgets")
|
|
|
|
}
|
|
|
|
}
|
2017-02-17 20:55:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if module == "QtCore" {
|
|
|
|
if !strings.Contains(bb.String(), "QTextDocument") {
|
|
|
|
fmt.Fprint(bb, "#include <QTextDocument>\n")
|
2016-06-19 05:24:38 +03:00
|
|
|
}
|
2015-10-24 18:18:24 +03:00
|
|
|
}
|
2017-02-16 18:38:33 +03:00
|
|
|
|
2017-03-04 21:01:53 +03:00
|
|
|
if mode == MINIMAL {
|
2017-02-16 18:38:33 +03:00
|
|
|
if module == "QtCore" {
|
|
|
|
fmt.Fprint(bb, "#include <QObject>\n")
|
|
|
|
} else if module == "QtNetwork" {
|
2017-02-19 23:11:49 +03:00
|
|
|
fmt.Fprint(bb, "#include <QSsl>\n")
|
2017-02-16 18:38:33 +03:00
|
|
|
}
|
2017-02-21 16:13:05 +03:00
|
|
|
|
|
|
|
if !strings.Contains(bb.String(), "QStringList") {
|
|
|
|
fmt.Fprint(bb, "#include <QStringList>\n")
|
|
|
|
}
|
2017-02-16 18:38:33 +03:00
|
|
|
}
|
|
|
|
|
2016-04-28 20:43:44 +03:00
|
|
|
fmt.Fprint(bb, "\n")
|
|
|
|
|
|
|
|
bb.Write(input)
|
2015-10-24 18:18:24 +03:00
|
|
|
|
2016-04-28 20:43:44 +03:00
|
|
|
return bb.Bytes()
|
2015-10-24 18:18:24 +03:00
|
|
|
}
|