This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
struct.go
67 lines (58 loc) · 1.44 KB
/
struct.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"bytes"
"regexp"
"strings"
)
func (self *Generator) GenStructs(ns *Namespace) {
/*
for _, s := range ns.Records {
p("%s\n", s.Name)
p("%s\n", s.CType)
p("%v\n", s.Constructors)
p("%v\n", s.Methods)
p("%v\n", s.Functions)
p("<<<\n")
}
*/
}
func (self *Generator) GenStructTypes(ns *Namespace) {
output := new(bytes.Buffer)
w(output, "package %s\n\n", self.PackageName)
w(output, "/*\n")
for _, include := range self.Includes {
w(output, "#include <%s>\n", include)
}
w(output, "*/\n")
w(output, "import \"C\"\n")
for _, s := range ns.Records {
goType := cTypeToGoType(s.CType)
typeName := s.Name
// skip
for _, t := range self.TypesIgnorePatterns {
matched, err := regexp.MatchString(t, typeName)
checkError(err)
if matched {
goto next
}
}
if s.IsGTypeStruct != "" {
goto next
}
if strings.HasSuffix(s.Name, "Private") {
goto next
}
// type mapping
TypeMapping["*"+goType] = "*" + typeName
InParamMapping[fs("*%s -> *%s", typeName, goType)] = func(param *Param) {
param.CgoParam = fs("(*%s)(unsafe.Pointer(%s))", goType, param.GoName)
}
OutParamMapping[fs("*%s -> *%s", goType, typeName)] = func(param *Param) {
param.CgoAfterStmt += fs("%s = (*%s)(unsafe.Pointer(__cgo__%s))", param.GoName, typeName, param.GoName)
}
// type declaration
w(output, "type %s %s\n", typeName, goType)
next:
}
self.formatAndOutput("struct_types", output.Bytes())
}