@@ -9,6 +9,14 @@ import (
9
9
10
10
// ----------------------------------------------------------------------------
11
11
12
+ // NamePrefix config
13
+ type NamePrefix struct {
14
+ BuiltinType string // builtin type
15
+ TypeExtend string // type extend
16
+ Operator string // operator
17
+ TypeConv string // type convert
18
+ }
19
+
12
20
// Config type
13
21
type Config struct {
14
22
// Context specifies the context for the load operation.
@@ -62,30 +70,144 @@ type Config struct {
62
70
63
71
// LoadPkgs is called to load all import packages.
64
72
LoadPkgs func (at * Package , importPkgs map [string ]* PkgRef , pkgPaths ... string ) int
73
+
74
+ // Prefix is name prefix.
75
+ Prefix * NamePrefix
76
+
77
+ // Builtin is the builin package.
78
+ Builtin * types.Package
79
+
80
+ // CheckBuiltinType checks a type is builtin type or not.
81
+ CheckBuiltinType func (typ types.Type ) (name string , is bool )
65
82
}
66
83
67
84
// Package type
68
85
type Package struct {
69
86
PkgRef
70
- decls []ast.Decl
71
- cb CodeBuilder
72
- importPkgs map [string ]* PkgRef
73
- pkgPaths []string
74
- conf * Config
87
+ decls []ast.Decl
88
+ cb CodeBuilder
89
+ importPkgs map [string ]* PkgRef
90
+ pkgPaths []string
91
+ conf * Config
92
+ prefix * NamePrefix
93
+ builtin * types.Package
94
+ checkBuiltin func (typ types.Type ) (name string , ok bool )
75
95
}
76
96
77
97
// NewPackage func
78
98
func NewPackage (pkgPath , name string , conf * Config ) * Package {
79
99
if conf == nil {
80
100
conf = & Config {}
81
101
}
102
+ prefix := conf .Prefix
103
+ if prefix == nil {
104
+ prefix = defaultNamePrefix
105
+ }
106
+ builtin := conf .Builtin
107
+ if builtin == nil {
108
+ builtin = newBuiltinDefault (prefix )
109
+ }
110
+ checkBuiltin := conf .CheckBuiltinType
111
+ if checkBuiltin == nil {
112
+ checkBuiltin = defaultCheckBuiltinType
113
+ }
82
114
pkg := & Package {
83
- importPkgs : make (map [string ]* PkgRef ),
84
- conf : conf ,
115
+ importPkgs : make (map [string ]* PkgRef ),
116
+ conf : conf ,
117
+ prefix : prefix ,
118
+ builtin : builtin ,
119
+ checkBuiltin : checkBuiltin ,
85
120
}
86
121
pkg .Types = types .NewPackage (pkgPath , name )
87
122
pkg .cb .init (pkg )
88
123
return pkg
89
124
}
90
125
126
+ func Init_untyped_uint (builtin * types.Package , prefix * NamePrefix ) types.Type {
127
+ name := types .NewTypeName (token .NoPos , builtin , prefix .BuiltinType + "untyped_uint" , nil )
128
+ typ := types .NewNamed (name , types .Typ [types .Uint ], nil )
129
+ builtin .Scope ().Insert (name )
130
+ return typ
131
+ }
132
+
133
+ // ----------------------------------------------------------------------------
134
+
135
+ var (
136
+ defaultNamePrefix = & NamePrefix {
137
+ BuiltinType : "Gopb_" ,
138
+ TypeExtend : "Gope_" ,
139
+ Operator : "Gopo_" ,
140
+ TypeConv : "Gopc_" ,
141
+ }
142
+ )
143
+
144
+ var (
145
+ intBinaryOps = []string {
146
+ "_Add" , "_Sub" , "_Mul" , "_Quo" , "_Rem" , "_Or" , "_Xor" , "_And" , "_AndNot" }
147
+ intBooleanOps = []string {
148
+ "_LT" , "_LE" , "_GT" , "_GE" , "_EQ" , "_NE" }
149
+ intTypes = []types.BasicKind {
150
+ types .Int , types .Int64 , types .Int32 , types .Int16 , types .Int8 ,
151
+ types .Uint , types .Uintptr , types .Uint64 , types .Uint32 , types .Uint16 , types .Uint8 ,
152
+ }
153
+ )
154
+
155
+ func addIntType (builtin * types.Package , typ , untypedUint types.Type , prefix * NamePrefix ) {
156
+ gbl := builtin .Scope ()
157
+ opPrefix := prefix .Operator + typ .String ()
158
+
159
+ a := types .NewVar (token .NoPos , builtin , "a" , typ )
160
+ b := types .NewVar (token .NoPos , builtin , "b" , typ )
161
+ args := types .NewTuple (a , b )
162
+ ret := types .NewTuple (types .NewVar (token .NoPos , builtin , "" , typ ))
163
+ sig := types .NewSignature (nil , args , ret , false )
164
+
165
+ // func opPrefix_type_op(a, b type) type
166
+ for _ , op := range intBinaryOps {
167
+ gbl .Insert (types .NewFunc (token .NoPos , builtin , opPrefix + op , sig ))
168
+ }
169
+
170
+ // func opPrefix_type_op(a type, n untyped_uint) type
171
+ n := types .NewVar (token .NoPos , builtin , "n" , untypedUint )
172
+ args2 := types .NewTuple (a , n )
173
+ sig2 := types .NewSignature (nil , args2 , ret , false )
174
+ gbl .Insert (types .NewFunc (token .NoPos , builtin , opPrefix + "Lsh" , sig2 ))
175
+ gbl .Insert (types .NewFunc (token .NoPos , builtin , opPrefix + "Rsh" , sig2 ))
176
+
177
+ // func opPrefix_type_op(a, b type) bool
178
+ ret3 := types .NewTuple (types .NewVar (token .NoPos , builtin , "" , types .Typ [types .Bool ]))
179
+ sig3 := types .NewSignature (nil , args , ret3 , false )
180
+ for _ , op := range intBooleanOps {
181
+ gbl .Insert (types .NewFunc (token .NoPos , builtin , opPrefix + op , sig3 ))
182
+ }
183
+
184
+ // func opPrefix_type_op(a type) type
185
+ args4 := types .NewTuple (a )
186
+ sig4 := types .NewSignature (nil , args4 , ret , false )
187
+ gbl .Insert (types .NewFunc (token .NoPos , builtin , opPrefix + "Neg" , sig4 ))
188
+ gbl .Insert (types .NewFunc (token .NoPos , builtin , opPrefix + "Not" , sig4 ))
189
+ }
190
+
191
+ func addUntypedType (builtin * types.Package , typ types.Type , prefix * NamePrefix ) {
192
+ }
193
+
194
+ func newBuiltinDefault (prefix * NamePrefix ) * types.Package {
195
+ builtin := types .NewPackage ("" , "" )
196
+ untypedUint := Init_untyped_uint (builtin , prefix )
197
+ for _ , intTy := range intTypes {
198
+ addIntType (builtin , types .Typ [intTy ], untypedUint , prefix )
199
+ }
200
+ return builtin
201
+ }
202
+
203
+ func defaultCheckBuiltinType (typ types.Type ) (name string , is bool ) {
204
+ if t , ok := typ .(* types.Basic ); ok {
205
+ if t .Kind () == types .UnsafePointer {
206
+ return
207
+ }
208
+ return t .Name (), true
209
+ }
210
+ return
211
+ }
212
+
91
213
// ----------------------------------------------------------------------------
0 commit comments