@@ -92,18 +92,18 @@ type noescapingFunc struct {
92
92
// cgoAliases list type aliases between Go and C, for types that are equivalent
93
93
// in both languages. See addTypeAliases.
94
94
var cgoAliases = map [string ]string {
95
- "C.int8_t " : "int8" ,
96
- "C.int16_t " : "int16" ,
97
- "C.int32_t " : "int32" ,
98
- "C.int64_t " : "int64" ,
99
- "C.uint8_t " : "uint8" ,
100
- "C.uint16_t " : "uint16" ,
101
- "C.uint32_t " : "uint32" ,
102
- "C.uint64_t " : "uint64" ,
103
- "C.uintptr_t " : "uintptr" ,
104
- "C.float " : "float32" ,
105
- "C.double " : "float64" ,
106
- "C._Bool " : "bool" ,
95
+ "_Cgo_int8_t " : "int8" ,
96
+ "_Cgo_int16_t " : "int16" ,
97
+ "_Cgo_int32_t " : "int32" ,
98
+ "_Cgo_int64_t " : "int64" ,
99
+ "_Cgo_uint8_t " : "uint8" ,
100
+ "_Cgo_uint16_t " : "uint16" ,
101
+ "_Cgo_uint32_t " : "uint32" ,
102
+ "_Cgo_uint64_t " : "uint64" ,
103
+ "_Cgo_uintptr_t " : "uintptr" ,
104
+ "_Cgo_float " : "float32" ,
105
+ "_Cgo_double " : "float64" ,
106
+ "_Cgo__Bool " : "bool" ,
107
107
}
108
108
109
109
// builtinAliases are handled specially because they only exist on the Go side
@@ -145,48 +145,46 @@ typedef unsigned long long _Cgo_ulonglong;
145
145
// The string/bytes functions below implement C.CString etc. To make sure the
146
146
// runtime doesn't need to know the C int type, lengths are converted to uintptr
147
147
// first.
148
- // These functions will be modified to get a "C." prefix, so the source below
149
- // doesn't reflect the final AST.
150
148
const generatedGoFilePrefixBase = `
151
149
import "syscall"
152
150
import "unsafe"
153
151
154
152
var _ unsafe.Pointer
155
153
156
- //go:linkname C.CString runtime.cgo_CString
157
- func CString (string) *C.char
154
+ //go:linkname _Cgo_CString runtime.cgo_CString
155
+ func _Cgo_CString (string) *_Cgo_char
158
156
159
- //go:linkname C.GoString runtime.cgo_GoString
160
- func GoString(*C.char ) string
157
+ //go:linkname _Cgo_GoString runtime.cgo_GoString
158
+ func _Cgo_GoString(*_Cgo_char ) string
161
159
162
- //go:linkname C.__GoStringN runtime.cgo_GoStringN
163
- func __GoStringN(*C.char , uintptr) string
160
+ //go:linkname _Cgo___GoStringN runtime.cgo_GoStringN
161
+ func _Cgo___GoStringN(*_Cgo_char , uintptr) string
164
162
165
- func GoStringN (cstr *C.char , length C.int ) string {
166
- return C.__GoStringN (cstr, uintptr(length))
163
+ func _Cgo_GoStringN (cstr *_Cgo_char , length _Cgo_int ) string {
164
+ return _Cgo___GoStringN (cstr, uintptr(length))
167
165
}
168
166
169
- //go:linkname C.__GoBytes runtime.cgo_GoBytes
170
- func __GoBytes (unsafe.Pointer, uintptr) []byte
167
+ //go:linkname _Cgo___GoBytes runtime.cgo_GoBytes
168
+ func _Cgo___GoBytes (unsafe.Pointer, uintptr) []byte
171
169
172
- func GoBytes (ptr unsafe.Pointer, length C.int ) []byte {
173
- return C.__GoBytes (ptr, uintptr(length))
170
+ func _Cgo_GoBytes (ptr unsafe.Pointer, length _Cgo_int ) []byte {
171
+ return _Cgo___GoBytes (ptr, uintptr(length))
174
172
}
175
173
176
- //go:linkname C.__CBytes runtime.cgo_CBytes
177
- func __CBytes ([]byte) unsafe.Pointer
174
+ //go:linkname _Cgo___CBytes runtime.cgo_CBytes
175
+ func _Cgo___CBytes ([]byte) unsafe.Pointer
178
176
179
- func CBytes (b []byte) unsafe.Pointer {
180
- return C.__CBytes (b)
177
+ func _Cgo_CBytes (b []byte) unsafe.Pointer {
178
+ return _Cgo___CBytes (b)
181
179
}
182
180
183
- //go:linkname C.__get_errno_num runtime.cgo_errno
184
- func __get_errno_num () uintptr
181
+ //go:linkname _Cgo___get_errno_num runtime.cgo_errno
182
+ func _Cgo___get_errno_num () uintptr
185
183
`
186
184
187
185
const generatedGoFilePrefixOther = generatedGoFilePrefixBase + `
188
- func __get_errno () error {
189
- return syscall.Errno(C.__get_errno_num ())
186
+ func _Cgo___get_errno () error {
187
+ return syscall.Errno(_Cgo___get_errno_num ())
190
188
}
191
189
`
192
190
@@ -197,7 +195,7 @@ func __get_errno() error {
197
195
// map the errno values to match the values in the syscall package.
198
196
// Source of the errno values: lib/mingw-w64/mingw-w64-headers/crt/errno.h
199
197
const generatedGoFilePrefixWindows = generatedGoFilePrefixBase + `
200
- var __errno_mapping = [...]syscall.Errno{
198
+ var _Cgo___errno_mapping = [...]syscall.Errno{
201
199
1: syscall.EPERM,
202
200
2: syscall.ENOENT,
203
201
3: syscall.ESRCH,
@@ -238,10 +236,10 @@ var __errno_mapping = [...]syscall.Errno{
238
236
42: syscall.EILSEQ,
239
237
}
240
238
241
- func __get_errno () error {
242
- num := C.__get_errno_num ()
243
- if num < uintptr(len(__errno_mapping )) {
244
- if mapped := __errno_mapping [num]; mapped != 0 {
239
+ func _Cgo___get_errno () error {
240
+ num := _Cgo___get_errno_num ()
241
+ if num < uintptr(len(_Cgo___errno_mapping )) {
242
+ if mapped := _Cgo___errno_mapping [num]; mapped != 0 {
245
243
return mapped
246
244
}
247
245
}
@@ -304,23 +302,6 @@ func Process(files []*ast.File, dir, importPath string, fset *token.FileSet, cfl
304
302
// If the Comments field is not set to nil, the go/format package will get
305
303
// confused about where comments should go.
306
304
p .generated .Comments = nil
307
- // Adjust some of the functions in there.
308
- for _ , decl := range p .generated .Decls {
309
- switch decl := decl .(type ) {
310
- case * ast.FuncDecl :
311
- switch decl .Name .Name {
312
- case "CString" , "GoString" , "GoStringN" , "__GoStringN" , "GoBytes" , "__GoBytes" , "CBytes" , "__CBytes" , "__get_errno_num" , "__get_errno" , "__errno_mapping" :
313
- // Adjust the name to have a "C." prefix so it is correctly
314
- // resolved.
315
- decl .Name .Name = "C." + decl .Name .Name
316
- }
317
- }
318
- }
319
- // Patch some types, for example *C.char in C.CString.
320
- cf := p .newCGoFile (nil , - 1 ) // dummy *cgoFile for the walker
321
- astutil .Apply (p .generated , func (cursor * astutil.Cursor ) bool {
322
- return cf .walker (cursor , nil )
323
- }, nil )
324
305
325
306
// Find `import "C"` C fragments in the file.
326
307
p .cgoHeaders = make ([]string , len (files )) // combined CGo header fragment for each file
@@ -399,7 +380,7 @@ func Process(files []*ast.File, dir, importPath string, fset *token.FileSet, cfl
399
380
Tok : token .TYPE ,
400
381
}
401
382
for _ , name := range builtinAliases {
402
- typeSpec := p .getIntegerType ("C. " + name , names ["_Cgo_" + name ])
383
+ typeSpec := p .getIntegerType ("_Cgo_ " + name , names ["_Cgo_" + name ])
403
384
gen .Specs = append (gen .Specs , typeSpec )
404
385
}
405
386
p .generated .Decls = append (p .generated .Decls , gen )
@@ -1272,7 +1253,7 @@ func (p *cgoPackage) getUnnamedDeclName(prefix string, itf interface{}) string {
1272
1253
func (f * cgoFile ) getASTDeclName (name string , found clangCursor , iscall bool ) string {
1273
1254
// Some types are defined in stdint.h and map directly to a particular Go
1274
1255
// type.
1275
- if alias := cgoAliases ["C. " + name ]; alias != "" {
1256
+ if alias := cgoAliases ["_Cgo_ " + name ]; alias != "" {
1276
1257
return alias
1277
1258
}
1278
1259
node := f .getASTDeclNode (name , found )
@@ -1282,7 +1263,7 @@ func (f *cgoFile) getASTDeclName(name string, found clangCursor, iscall bool) st
1282
1263
}
1283
1264
return node .Name .Name
1284
1265
}
1285
- return "C. " + name
1266
+ return "_Cgo_ " + name
1286
1267
}
1287
1268
1288
1269
// getASTDeclNode will declare the given C AST node (if not already defined) and
@@ -1382,8 +1363,8 @@ extern __typeof(%s) %s __attribute__((alias(%#v)));
1382
1363
case * elaboratedTypeInfo :
1383
1364
// Add struct bitfields.
1384
1365
for _ , bitfield := range elaboratedType .bitfields {
1385
- f .createBitfieldGetter (bitfield , "C. " + name )
1386
- f .createBitfieldSetter (bitfield , "C. " + name )
1366
+ f .createBitfieldGetter (bitfield , "_Cgo_ " + name )
1367
+ f .createBitfieldSetter (bitfield , "_Cgo_ " + name )
1387
1368
}
1388
1369
if elaboratedType .unionSize != 0 {
1389
1370
// Create union getters/setters.
@@ -1392,7 +1373,7 @@ extern __typeof(%s) %s __attribute__((alias(%#v)));
1392
1373
f .addError (elaboratedType .pos , fmt .Sprintf ("union must have field with a single name, it has %d names" , len (field .Names )))
1393
1374
continue
1394
1375
}
1395
- f .createUnionAccessor (field , "C. " + name )
1376
+ f .createUnionAccessor (field , "_Cgo_ " + name )
1396
1377
}
1397
1378
}
1398
1379
}
@@ -1441,7 +1422,7 @@ func (f *cgoFile) walker(cursor *astutil.Cursor, names map[string]clangCursor) b
1441
1422
node .Rhs = append (node .Rhs , & ast.CallExpr {
1442
1423
Fun : & ast.Ident {
1443
1424
NamePos : node .Lhs [1 ].End (),
1444
- Name : "C.__get_errno " ,
1425
+ Name : "_Cgo___get_errno " ,
1445
1426
},
1446
1427
})
1447
1428
}
@@ -1466,7 +1447,7 @@ func (f *cgoFile) walker(cursor *astutil.Cursor, names map[string]clangCursor) b
1466
1447
return true
1467
1448
}
1468
1449
if x .Name == "C" {
1469
- name := "C. " + node .Sel .Name
1450
+ name := "_Cgo_ " + node .Sel .Name
1470
1451
if found , ok := names [node .Sel .Name ]; ok {
1471
1452
name = f .getASTDeclName (node .Sel .Name , found , false )
1472
1453
}
0 commit comments