2024-08-06 13:03:23 +12:00
|
|
|
package main
|
|
|
|
|
2024-08-07 18:51:51 +12:00
|
|
|
import (
|
2024-08-18 15:27:03 +12:00
|
|
|
"regexp"
|
2024-08-07 18:51:51 +12:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-08-07 18:56:14 +12:00
|
|
|
type CppParameter struct {
|
|
|
|
ParameterName string
|
2024-08-08 17:51:10 +12:00
|
|
|
ParameterType string
|
2024-08-20 20:16:13 +12:00
|
|
|
TypeAlias string // If we rewrote QStringList->QList<String>, this field contains the original QStringList
|
2024-08-08 17:51:10 +12:00
|
|
|
Const bool
|
|
|
|
Pointer bool
|
|
|
|
ByRef bool
|
2024-08-08 18:55:22 +12:00
|
|
|
Optional bool
|
2024-08-08 17:51:10 +12:00
|
|
|
}
|
|
|
|
|
2024-08-08 19:06:14 +12:00
|
|
|
func (p CppParameter) QtClassType() bool {
|
2024-08-17 11:27:02 +12:00
|
|
|
return (p.ParameterType[0] == 'Q') && p.ParameterType != "QRgb"
|
2024-08-08 19:06:14 +12:00
|
|
|
}
|
|
|
|
|
2024-08-11 16:37:18 +12:00
|
|
|
func (p CppParameter) QListOf() (CppParameter, bool) {
|
|
|
|
if strings.HasPrefix(p.ParameterType, "QList<") && strings.HasSuffix(p.ParameterType, `>`) {
|
|
|
|
return parseSingleTypeString(p.ParameterType[6 : len(p.ParameterType)-1]), true
|
|
|
|
}
|
|
|
|
|
2024-08-17 11:29:31 +12:00
|
|
|
if strings.HasPrefix(p.ParameterType, "QVector<") && strings.HasSuffix(p.ParameterType, `>`) {
|
|
|
|
return parseSingleTypeString(p.ParameterType[8 : len(p.ParameterType)-1]), true
|
|
|
|
}
|
|
|
|
|
2024-08-11 16:37:18 +12:00
|
|
|
return CppParameter{}, false
|
|
|
|
}
|
|
|
|
|
2024-08-15 19:53:01 +12:00
|
|
|
func (p CppParameter) QMapOf() bool {
|
|
|
|
return strings.HasPrefix(p.ParameterType, `QMap<`) ||
|
|
|
|
strings.HasPrefix(p.ParameterType, `QHash<`) // TODO support this
|
|
|
|
}
|
|
|
|
|
2024-08-17 14:11:11 +12:00
|
|
|
func (p CppParameter) QPairOf() bool {
|
|
|
|
return strings.HasPrefix(p.ParameterType, `QPair<`) // TODO support this
|
|
|
|
}
|
|
|
|
|
2024-08-19 19:15:05 +12:00
|
|
|
func (p CppParameter) QSetOf() bool {
|
|
|
|
return strings.HasPrefix(p.ParameterType, `QSet<`) // TODO support this
|
|
|
|
}
|
|
|
|
|
2024-08-14 18:33:47 +12:00
|
|
|
func (p CppParameter) IntType() bool {
|
2024-08-25 15:31:21 +12:00
|
|
|
|
2024-08-14 18:33:47 +12:00
|
|
|
switch p.ParameterType {
|
2024-08-15 19:49:05 +12:00
|
|
|
case "int", "unsigned int", "uint",
|
|
|
|
"short", "unsigned short", "ushort", "qint16", "quint16",
|
|
|
|
"qint8", "quint8",
|
2024-08-25 15:31:21 +12:00
|
|
|
"unsigned char", "uchar",
|
2024-08-15 19:49:05 +12:00
|
|
|
"long", "unsigned long", "ulong", "qint32", "quint32",
|
|
|
|
"longlong", "ulonglong", "qlonglong", "qulonglong", "qint64", "quint64", "int64_t", "uint64_t", "long long", "unsigned long long",
|
2024-08-25 15:31:21 +12:00
|
|
|
"QRgb", // QRgb is an unsigned int
|
|
|
|
"qintptr", "quintptr", "uintptr_t", "intptr_t",
|
2024-08-24 11:45:36 +12:00
|
|
|
"qsizetype", "size_t",
|
2024-08-14 18:33:47 +12:00
|
|
|
"double", "float", "qreal":
|
|
|
|
return true
|
2024-08-25 15:31:21 +12:00
|
|
|
|
|
|
|
case "char":
|
|
|
|
// Only count char as an integer type with cast assertions if it's
|
|
|
|
// not possibly a char* string in disguise
|
|
|
|
// (However, unsigned chars are always like ints)
|
|
|
|
return !p.Pointer
|
|
|
|
|
2024-08-14 18:33:47 +12:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-07 18:56:14 +12:00
|
|
|
type CppProperty struct {
|
|
|
|
PropertyName string
|
|
|
|
PropertyType string
|
|
|
|
Visibility string
|
2024-08-06 13:03:23 +12:00
|
|
|
}
|
|
|
|
|
2024-08-07 18:56:14 +12:00
|
|
|
type CppMethod struct {
|
2024-08-22 19:20:12 +12:00
|
|
|
MethodName string // C++ method name, unless OverrideMethodName is set, in which case a nice alternative name
|
|
|
|
OverrideMethodName string // C++ method name, present only if we changed the target
|
2024-08-10 11:45:19 +12:00
|
|
|
ReturnType CppParameter // Name not used
|
|
|
|
Parameters []CppParameter
|
2024-08-18 15:24:04 +12:00
|
|
|
IsStatic bool
|
2024-08-18 15:56:48 +12:00
|
|
|
IsSignal bool
|
2024-08-18 18:55:28 +12:00
|
|
|
HasHiddenParams bool // Set to true if there is an overload with more parameters
|
2024-08-06 13:03:23 +12:00
|
|
|
}
|
|
|
|
|
2024-08-18 15:57:29 +12:00
|
|
|
func IsArgcArgv(params []CppParameter, pos int) bool {
|
|
|
|
// Check if the arguments starting at position=pos are the argc/argv pattern.
|
2024-08-17 14:10:59 +12:00
|
|
|
// QApplication/QGuiApplication constructors are the only expected example of this.
|
2024-08-18 15:57:29 +12:00
|
|
|
return (len(params) > pos+1 &&
|
|
|
|
params[pos].ParameterName == "argc" &&
|
|
|
|
params[pos].ParameterType == "int" &&
|
|
|
|
params[pos].ByRef &&
|
|
|
|
params[pos+1].ParameterName == "argv" &&
|
|
|
|
params[pos+1].ParameterType == "char **")
|
|
|
|
}
|
|
|
|
|
2024-08-18 15:57:40 +12:00
|
|
|
func IsReceiverMethod(params []CppParameter, pos int) bool {
|
|
|
|
// Check if the arguments starting at position=pos are the receiver/member pattern.
|
|
|
|
// QMenu->addAction is the main example of this
|
|
|
|
return (len(params) > pos+1 &&
|
|
|
|
params[pos].ParameterName == "receiver" &&
|
|
|
|
params[pos].ParameterType == "QObject" &&
|
|
|
|
params[pos].Pointer &&
|
|
|
|
params[pos+1].ParameterName == "member" &&
|
|
|
|
params[pos+1].ParameterType == "char" &&
|
|
|
|
params[pos+1].Pointer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nm CppMethod) IsReceiverMethod() bool {
|
|
|
|
// Returns true if any of the parameters use the receiever-method pattern
|
|
|
|
for i := 0; i < len(nm.Parameters); i++ {
|
|
|
|
if IsReceiverMethod(nm.Parameters, i) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2024-08-17 14:10:59 +12:00
|
|
|
}
|
|
|
|
|
2024-08-07 18:56:14 +12:00
|
|
|
func (nm CppMethod) SafeMethodName() string {
|
2024-08-18 19:01:23 +12:00
|
|
|
|
|
|
|
tmp := nm.MethodName
|
|
|
|
|
|
|
|
// Strip redundant Qt prefix, we know these are all Qt functions
|
|
|
|
if strings.HasPrefix(tmp, "qt_") {
|
|
|
|
tmp = tmp[3:]
|
|
|
|
}
|
|
|
|
|
2024-08-07 18:51:51 +12:00
|
|
|
// Operator-overload methods have names not representable in binding
|
|
|
|
// languages. Replace more specific cases first
|
|
|
|
replacer := strings.NewReplacer(
|
|
|
|
|
|
|
|
`==`, `Equal`,
|
2024-08-14 18:26:51 +12:00
|
|
|
`!=`, `NotEqual`,
|
2024-08-07 18:51:51 +12:00
|
|
|
`>=`, `GreaterOrEqual`,
|
|
|
|
`<=`, `LesserOrEqual`,
|
|
|
|
`=`, `Assign`,
|
2024-08-15 19:51:58 +12:00
|
|
|
|
|
|
|
`<<`, `ShiftLeft`, // Qt classes use it more for stream functions e.g. in QDataStream
|
|
|
|
`>>`, `ShiftRight`,
|
2024-08-07 18:51:51 +12:00
|
|
|
`>`, `Greater`,
|
|
|
|
`<`, `Lesser`,
|
|
|
|
|
|
|
|
`+`, `Plus`,
|
|
|
|
`-`, `Minus`,
|
|
|
|
`*`, `Multiply`,
|
|
|
|
`/`, `Divide`,
|
|
|
|
`%`, `Modulo`,
|
|
|
|
|
|
|
|
`&&`, `LogicalAnd`,
|
|
|
|
`||`, `LogicalOr`,
|
|
|
|
`!`, `Not`,
|
|
|
|
`&`, `BitwiseAnd`,
|
|
|
|
`|`, `BitwiseOr`,
|
|
|
|
`~`, `BitwiseXor`,
|
|
|
|
`^`, `BitwiseNot`,
|
|
|
|
|
|
|
|
`->`, `PointerDereference`,
|
|
|
|
`[]`, `Subscript`,
|
|
|
|
`()`, `Call`,
|
|
|
|
)
|
2024-08-18 19:01:23 +12:00
|
|
|
tmp = replacer.Replace(tmp)
|
2024-08-08 17:51:46 +12:00
|
|
|
|
|
|
|
// Also make the first letter uppercase so it becomes public in Go
|
2024-08-18 15:27:03 +12:00
|
|
|
tmp = titleCase(tmp)
|
|
|
|
|
|
|
|
// Also replace any underscore_case with CamelCase
|
|
|
|
tmp = regexp.MustCompile(`_([a-z])`).ReplaceAllStringFunc(tmp, func(match string) string { return strings.ToUpper(match[1:]) })
|
|
|
|
|
|
|
|
return tmp
|
2024-08-07 18:51:51 +12:00
|
|
|
}
|
|
|
|
|
2024-08-07 18:56:14 +12:00
|
|
|
type CppClass struct {
|
|
|
|
ClassName string
|
2024-08-10 12:53:26 +12:00
|
|
|
Abstract bool
|
2024-08-07 18:56:14 +12:00
|
|
|
Ctors []CppMethod // only use the parameters
|
2024-08-10 12:54:26 +12:00
|
|
|
Inherits []string // other class names
|
2024-08-07 18:56:14 +12:00
|
|
|
Methods []CppMethod
|
|
|
|
Props []CppProperty
|
2024-08-20 20:10:57 +12:00
|
|
|
CanDelete bool
|
2024-08-06 13:03:23 +12:00
|
|
|
}
|
|
|
|
|
2024-08-14 18:26:42 +12:00
|
|
|
type CppTypedef struct {
|
2024-08-17 11:25:54 +12:00
|
|
|
Alias string
|
|
|
|
UnderlyingType string
|
2024-08-14 18:26:42 +12:00
|
|
|
}
|
|
|
|
|
2024-08-07 18:56:14 +12:00
|
|
|
type CppParsedHeader struct {
|
2024-08-14 18:26:42 +12:00
|
|
|
Typedefs []CppTypedef
|
|
|
|
Classes []CppClass
|
2024-08-06 13:03:23 +12:00
|
|
|
}
|
2024-08-19 19:11:36 +12:00
|
|
|
|
|
|
|
func (c CppParsedHeader) Empty() bool {
|
|
|
|
return len(c.Typedefs) == 0 &&
|
|
|
|
len(c.Classes) == 0
|
|
|
|
}
|