This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
export.go
98 lines (88 loc) · 2.17 KB
/
export.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"strings"
"unicode/utf8"
"rsc.io/c2go/cc"
)
func exportDecls(cfg *Config, prog *cc.Prog) {
for _, d := range cfg.topDecls {
if shouldExport(cfg, d.Name) {
exportDecl(d)
}
pkg := d.GoPackage
if pkg == "" {
continue
}
cc.Preorder(d, func(x cc.Syntax) {
switch x := x.(type) {
case *cc.Expr:
if x.Op == cc.Name && x.XDecl != nil && x.XDecl.GoPackage != "" && x.XDecl.GoPackage != pkg {
exportDecl(x.XDecl)
}
case *cc.Type:
if x.Kind == cc.TypedefType && x.TypeDecl != nil && x.TypeDecl.GoPackage != "" && x.TypeDecl.GoPackage != pkg {
exportDecl(x.TypeDecl)
x.Name = x.TypeDecl.Name
}
}
})
}
for _, d := range cfg.topDecls {
renameDecl(cfg, d)
}
}
func shouldExport(cfg *Config, name string) bool {
for _, s := range cfg.exports {
if s == name {
return true
}
}
return false
}
func exportDecl(d *cc.Decl) {
d.Name = exportName(d.Name)
if d.Storage&cc.Typedef != 0 && d.Type.Kind == cc.Struct {
for _, dd := range d.Type.Decls {
exportDecl(dd)
// type became type_, became Type_. Drop underscore now that it's not needed.
if strings.HasSuffix(dd.Name, "_") && goKeyword[strings.ToLower(dd.Name[:len(dd.Name)-1])] {
dd.Name = dd.Name[:len(dd.Name)-1]
}
if dd.Name == "U" {
for _, dd := range dd.Type.Decls {
exportDecl(dd)
}
}
}
}
}
func exportName(name string) string {
_, size := utf8.DecodeRuneInString(name)
return strings.ToUpper(name[:size]) + name[size:]
}
func renameDecl(cfg *Config, d *cc.Decl) {
key := declKey(d)
if cfg.rename[key] != "" {
d.Name = cfg.rename[key]
}
if d.Storage&cc.Typedef != 0 && d.Type.Kind == cc.Struct {
for _, dd := range d.Type.Decls {
renameDecl(cfg, dd)
if dd.Name == "U" {
for _, dd := range dd.Type.Decls {
renameDecl(cfg, dd)
}
}
}
}
if d.Type != nil && d.Type.Kind == cc.Func && d.Body != nil {
for _, s := range d.Body.Block {
if s.Op == cc.StmtDecl && s.Decl.Storage&cc.Static != 0 {
renameDecl(cfg, s.Decl)
}
}
}
}