cutego/internal/binding/converter/output.go

536 lines
10 KiB
Go
Raw Normal View History

2015-10-24 18:18:24 +03:00
package converter
import (
"fmt"
"strings"
"github.com/therecipe/qt/internal/binding/parser"
)
func goOutput(name, value string, f *parser.Function) string {
2015-11-09 20:23:42 +03:00
var vOld = value
2015-10-24 18:18:24 +03:00
name = cleanName(name, value)
2016-05-28 19:18:42 +03:00
value = CleanValue(value)
2015-10-24 18:18:24 +03:00
switch value {
case "char", "qint8", "uchar", "quint8", "QString":
2015-10-24 18:18:24 +03:00
{
return fmt.Sprintf("C.GoString(%v)", name)
}
2016-08-21 18:04:39 +03:00
case "QByteArray":
{
return fmt.Sprintf("qt.HexDecodeToString(C.GoString(%v))", name)
}
case "QStringList":
2015-10-24 18:18:24 +03:00
{
return fmt.Sprintf("strings.Split(C.GoString(%v), \"|\")", name)
2015-10-24 18:18:24 +03:00
}
case "void", "":
{
2015-11-09 20:23:42 +03:00
if strings.Contains(vOld, "*") {
return fmt.Sprintf("unsafe.Pointer(%v)", name)
}
2015-10-24 18:18:24 +03:00
return name
}
2015-11-09 20:23:42 +03:00
case "bool":
{
return fmt.Sprintf("%v != 0", name)
}
case "short", "qint16":
{
return fmt.Sprintf("int16(%v)", name)
}
case "ushort", "unsigned short", "quint16":
{
return fmt.Sprintf("uint16(%v)", name)
}
case "int", "qint32":
{
return fmt.Sprintf("int(int32(%v))", name)
}
case "uint", "unsigned int", "quint32":
{
return fmt.Sprintf("uint(uint32(%v))", name)
}
case "long":
{
return fmt.Sprintf("int(int32(%v))", name)
}
case "ulong", "unsigned long":
{
return fmt.Sprintf("uint(uint32(%v))", name)
}
case "longlong", "long long", "qlonglong", "qint64":
{
return fmt.Sprintf("int64(%v)", name)
}
case "ulonglong", "unsigned long long", "qulonglong", "quint64":
{
return fmt.Sprintf("uint64(%v)", name)
}
case "float":
{
return fmt.Sprintf("float32(%v)", name)
}
case "double", "qreal":
{
return fmt.Sprintf("float64(%v)", name)
}
case "uintptr_t", "uintptr", "quintptr", "WId":
{
return fmt.Sprintf("uintptr(%v)", name)
}
//non std types
2015-11-19 21:29:30 +03:00
case "T", "JavaVM", "jclass", "jobject":
2015-11-09 20:23:42 +03:00
{
2015-11-19 21:29:30 +03:00
switch f.TemplateMode {
case "Boolean":
2015-11-19 21:29:30 +03:00
{
return fmt.Sprintf("int8(%v) != 0", name)
2015-11-19 21:29:30 +03:00
}
case "Int":
2015-11-19 21:29:30 +03:00
{
return fmt.Sprintf("int(int32(%v))", name)
2015-11-19 21:29:30 +03:00
}
2015-11-19 21:29:30 +03:00
case "Void":
{
return name
}
}
2016-02-11 00:23:13 +03:00
return fmt.Sprintf("unsafe.Pointer(%v)", name)
2016-02-11 00:23:13 +03:00
}
2015-10-24 18:18:24 +03:00
}
switch {
case isEnum(f.Class(), value):
{
if c, exists := parser.ClassMap[class(cppEnum(f, value, false))]; exists && module(c.Module) != module(f) && module(c.Module) != "" {
if parser.ClassMap[f.Class()].WeakLink[c.Module] {
return fmt.Sprintf("int64(%v)", name)
}
2015-10-24 18:18:24 +03:00
return fmt.Sprintf("%v.%v(%v)", module(c.Module), goEnum(f, value), name)
}
return fmt.Sprintf("%v(%v)", goEnum(f, value), name)
}
case isClass(value):
{
if m := module(parser.ClassMap[value].Module); m != module(f) {
if parser.ClassMap[f.Class()].WeakLink[parser.ClassMap[value].Module] {
return fmt.Sprintf("unsafe.Pointer(%v)", name)
}
2016-05-22 01:54:06 +03:00
return fmt.Sprintf("%v.New%vFromPointer(%v)", m, strings.Title(value), name)
2015-10-24 18:18:24 +03:00
}
2016-01-26 19:58:25 +03:00
2016-05-22 01:54:06 +03:00
return fmt.Sprintf("New%vFromPointer(%v)", strings.Title(value), name)
2015-10-24 18:18:24 +03:00
}
}
f.Access = fmt.Sprintf("unsupported_goOutput(%v)", value)
2015-10-24 18:18:24 +03:00
return f.Access
}
2015-11-19 21:29:30 +03:00
func goOutputFailed(value string, f *parser.Function) string {
var vOld = value
2016-05-28 19:18:42 +03:00
value = CleanValue(value)
2015-11-19 21:29:30 +03:00
switch value {
case "char", "qint8", "uchar", "quint8", "QString", "QByteArray":
{
return "\"\""
}
2015-11-19 21:29:30 +03:00
case "QStringList":
{
return "make([]string, 0)"
}
2015-11-19 21:29:30 +03:00
case "void", "":
{
if strings.Contains(vOld, "*") {
return "nil"
}
return ""
2015-11-19 21:29:30 +03:00
}
case "bool":
{
return "false"
}
case
"short", "qint16",
"ushort", "unsigned short", "quint16",
"int", "qint32",
"uint", "unsigned int", "quint32",
"long",
"ulong", "unsigned long",
"longlong", "long long", "qlonglong", "qint64",
"ulonglong", "unsigned long long", "qulonglong", "quint64",
"float",
"double", "qreal",
"uintptr_t", "uintptr", "quintptr", "WId":
{
return "0"
}
//non std types
2015-11-19 21:29:30 +03:00
case "T", "JavaVM", "jclass", "jobject":
{
switch f.TemplateMode {
case "Boolean":
{
return "false"
}
2015-11-19 21:29:30 +03:00
case "Int":
{
return "0"
}
case "Void":
{
return ""
}
2015-11-19 21:29:30 +03:00
}
return "nil"
}
2015-11-19 21:29:30 +03:00
}
switch {
case isEnum(f.Class(), value):
{
return "0"
}
2015-11-19 21:29:30 +03:00
case isClass(value):
{
if f.TemplateMode == "String" {
return "\"\""
}
2015-11-19 21:29:30 +03:00
return "nil"
}
2015-11-19 21:29:30 +03:00
}
f.Access = fmt.Sprintf("unsupported_goOutputFailed(%v)", value)
return f.Access
2015-11-19 21:29:30 +03:00
}
func cgoOutput(name, value string, f *parser.Function) string {
2015-10-24 18:18:24 +03:00
var vOld = value
name = cleanName(name, value)
2016-05-28 19:18:42 +03:00
value = CleanValue(value)
2015-10-24 18:18:24 +03:00
switch value {
case "char", "qint8", "uchar", "quint8", "QString":
2015-10-24 18:18:24 +03:00
{
return fmt.Sprintf("C.GoString(%v)", name)
}
2016-08-21 18:04:39 +03:00
case "QByteArray":
{
return fmt.Sprintf("qt.HexDecodeToString(C.GoString(%v))", name)
}
case "QStringList":
2015-10-24 18:18:24 +03:00
{
return fmt.Sprintf("strings.Split(C.GoString(%v), \"|\")", name)
2015-10-24 18:18:24 +03:00
}
case "void", "":
{
if strings.Contains(vOld, "*") {
return name
}
2015-10-24 18:18:24 +03:00
return ""
}
case "bool":
{
return fmt.Sprintf("int8(%v) != 0", name)
}
case "short", "qint16":
{
return fmt.Sprintf("int16(%v)", name)
}
case "ushort", "unsigned short", "quint16":
{
return fmt.Sprintf("uint16(%v)", name)
}
case "int", "qint32":
{
return fmt.Sprintf("int(int32(%v))", name)
}
case "uint", "unsigned int", "quint32":
{
return fmt.Sprintf("uint(uint32(%v))", name)
}
case "long":
{
return fmt.Sprintf("int(int32(%v))", name)
}
case "ulong", "unsigned long":
{
return fmt.Sprintf("uint(uint32(%v))", name)
}
case "longlong", "long long", "qlonglong", "qint64":
{
return fmt.Sprintf("int64(%v)", name)
}
case "ulonglong", "unsigned long long", "qulonglong", "quint64":
{
return fmt.Sprintf("uint64(%v)", name)
}
case "float":
{
return fmt.Sprintf("float32(%v)", name)
}
case "double", "qreal":
{
return fmt.Sprintf("float64(%v)", name)
}
case "uintptr_t", "uintptr", "quintptr", "WId":
{
return fmt.Sprintf("uintptr(%v)", name)
}
2015-10-24 18:18:24 +03:00
}
switch {
case isEnum(f.Class(), value):
{
if c, exists := parser.ClassMap[class(cppEnum(f, value, false))]; exists && module(c.Module) != module(f) && module(c.Module) != "" {
if parser.ClassMap[f.Class()].WeakLink[c.Module] {
return fmt.Sprintf("int64%v)", name)
}
2015-10-24 18:18:24 +03:00
return fmt.Sprintf("%v.%v(%v)", module(c.Module), goEnum(f, value), name)
}
return fmt.Sprintf("%v(%v)", goEnum(f, value), name)
}
case isClass(value):
{
if m := module(parser.ClassMap[value].Module); m != module(f) {
if parser.ClassMap[f.Class()].WeakLink[parser.ClassMap[value].Module] {
return fmt.Sprintf("unsafe.Pointer(%v)", name)
}
2016-05-22 01:54:06 +03:00
return fmt.Sprintf("%v.New%vFromPointer(%v)", m, strings.Title(value), name)
2015-10-24 18:18:24 +03:00
}
2016-05-22 01:54:06 +03:00
return fmt.Sprintf("New%vFromPointer(%v)", strings.Title(value), name)
2015-10-24 18:18:24 +03:00
}
}
f.Access = fmt.Sprintf("unsupported_cgoOutput(%v)", value)
2015-10-24 18:18:24 +03:00
return f.Access
}
func CppOutput(name, value string, f *parser.Function) string {
return cppOutput(name, value, f)
}
func cppOutput(name, value string, f *parser.Function) string {
2015-10-24 18:18:24 +03:00
var vOld = value
name = cleanName(name, value)
2016-05-28 19:18:42 +03:00
value = CleanValue(value)
2015-10-24 18:18:24 +03:00
switch value {
case "char", "qint8":
2015-10-24 18:18:24 +03:00
{
return cppOutput(fmt.Sprintf("QString(%v)", name), "QString", f)
2015-10-24 18:18:24 +03:00
}
case "uchar", "quint8":
2015-10-24 18:18:24 +03:00
{
if strings.Contains(vOld, "*") {
return cppOutput(fmt.Sprintf("QString(QChar(*%v))", name), "QString", f)
2015-10-24 18:18:24 +03:00
}
return cppOutput(fmt.Sprintf("QString(QChar(%v))", name), "QString", f)
2015-10-24 18:18:24 +03:00
}
case "QString":
2016-04-17 00:38:16 +03:00
{
if strings.Contains(vOld, "*") {
2016-10-13 18:14:49 +03:00
return fmt.Sprintf("const_cast<char*>(%v->toUtf8().prepend(\"WHITESPACE\").constData()+10)", name)
}
2016-10-13 18:14:49 +03:00
return fmt.Sprintf("const_cast<char*>(%v.toUtf8().prepend(\"WHITESPACE\").constData()+10)", name)
2016-04-17 00:38:16 +03:00
}
2016-08-21 18:04:39 +03:00
case "QByteArray":
{
2016-10-13 18:14:49 +03:00
return fmt.Sprintf("const_cast<char*>(%v.toHex().prepend(\"WHITESPACE\").constData()+10)", name)
2016-08-21 18:04:39 +03:00
}
case "QStringList":
2015-10-24 18:18:24 +03:00
{
return cppOutput(fmt.Sprintf("%v.join(\"|\")", name), "QString", f)
}
2016-06-13 22:23:56 +03:00
case
"bool",
2015-11-09 20:23:42 +03:00
"short", "qint16",
"ushort", "unsigned short", "quint16",
"int", "qint32",
"uint", "unsigned int", "quint32",
"long",
"ulong", "unsigned long",
2015-10-24 18:18:24 +03:00
"longlong", "long long", "qlonglong", "qint64",
"ulonglong", "unsigned long long", "qulonglong", "quint64",
"float",
"double", "qreal",
"uintptr_t", "uintptr", "quintptr", "WId":
2015-10-24 18:18:24 +03:00
{
if strings.Contains(vOld, "*") {
return fmt.Sprintf("*%v", name)
}
return name
}
2016-02-11 00:23:13 +03:00
//non std types
case "void", "", "T", "JavaVM", "jclass", "jobject":
2016-02-11 00:23:13 +03:00
{
if f.Fullname == "QMimeData::imageData" {
return fmt.Sprintf("new QImage(qvariant_cast<QImage>(%v))", name)
}
if value == "void" || value == "T" {
if strings.Contains(vOld, "*") && strings.Contains(vOld, "const") {
return fmt.Sprintf("const_cast<void*>(%v)", name)
}
}
return name
2016-02-11 00:23:13 +03:00
}
2015-10-24 18:18:24 +03:00
}
switch {
case isEnum(f.Class(), value):
{
return name
}
case isClass(value):
{
if strings.Contains(vOld, "*") {
if strings.Contains(vOld, "const") {
return fmt.Sprintf("const_cast<%v*>(%v)", value, name)
}
return name
}
2016-08-20 00:20:42 +03:00
if strings.Contains(vOld, "&") {
if strings.Contains(vOld, "const") {
return fmt.Sprintf("const_cast<%v*>(&%v)", value, name)
}
}
f.NeedsFinalizer = true
2015-10-24 18:18:24 +03:00
switch value {
case "QModelIndex", "QMetaMethod", "QItemSelection":
{
return fmt.Sprintf("new %v(%v)", value, name)
}
2015-11-12 03:29:32 +03:00
case "QAndroidJniObject":
{
2015-11-19 21:29:30 +03:00
return fmt.Sprintf("new %v(%v.object())", value, name)
2015-11-12 03:29:32 +03:00
}
2016-04-22 21:39:34 +03:00
case "QPoint", "QPointF":
{
2016-08-20 00:20:42 +03:00
return fmt.Sprintf("({ %v tmpValue = %v; new %v(tmpValue.x(), tmpValue.y()); })", value, name, value)
}
2016-04-22 21:39:34 +03:00
case "QSize", "QSizeF":
{
2016-08-20 00:20:42 +03:00
return fmt.Sprintf("({ %v tmpValue = %v; new %v(tmpValue.width(), tmpValue.height()); })", value, name, value)
}
2016-04-22 21:39:34 +03:00
case "QRect", "QRectF":
{
2016-08-20 00:20:42 +03:00
return fmt.Sprintf("({ %v tmpValue = %v; new %v(tmpValue.x(), tmpValue.y(), tmpValue.width(), tmpValue.height()); })", value, name, value)
}
case "QLine", "QLineF":
{
2016-08-20 00:20:42 +03:00
return fmt.Sprintf("({ %v tmpValue = %v; new %v(tmpValue.p1(), tmpValue.p2()); })", value, name, value)
}
case "QMargins", "QMarginsF":
{
2016-08-20 00:20:42 +03:00
return fmt.Sprintf("({ %v tmpValue = %v; new %v(tmpValue.left(), tmpValue.top(), tmpValue.right(), tmpValue.bottom()); })", value, name, value)
}
2015-10-24 18:18:24 +03:00
}
2016-04-17 00:38:16 +03:00
for _, f := range parser.ClassMap[value].Functions {
if f.Meta == parser.CONSTRUCTOR {
2016-04-17 00:38:16 +03:00
if len(f.Parameters) == 1 {
2016-05-28 19:18:42 +03:00
if CleanValue(f.Parameters[0].Value) == value {
2016-04-17 00:38:16 +03:00
return fmt.Sprintf("new %v(%v)", value, name)
}
}
}
}
2015-10-24 18:18:24 +03:00
}
}
f.Access = fmt.Sprintf("unsupported_cppOutput(%v)", value)
2015-10-24 18:18:24 +03:00
return f.Access
}