@@ -92,18 +92,18 @@ type noescapingFunc struct {
9292// cgoAliases list type aliases between Go and C, for types that are equivalent
9393// in both languages. See addTypeAliases.
9494var 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" ,
107107}
108108
109109// builtinAliases are handled specially because they only exist on the Go side
@@ -145,48 +145,46 @@ typedef unsigned long long _Cgo_ulonglong;
145145// The string/bytes functions below implement C.CString etc. To make sure the
146146// runtime doesn't need to know the C int type, lengths are converted to uintptr
147147// first.
148- // These functions will be modified to get a "C." prefix, so the source below
149- // doesn't reflect the final AST.
150148const generatedGoFilePrefixBase = `
151149import "syscall"
152150import "unsafe"
153151
154152var _ unsafe.Pointer
155153
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
158156
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
161159
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
164162
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))
167165}
168166
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
171169
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))
174172}
175173
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
178176
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)
181179}
182180
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
185183`
186184
187185const 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 ())
190188}
191189`
192190
@@ -197,7 +195,7 @@ func __get_errno() error {
197195// map the errno values to match the values in the syscall package.
198196// Source of the errno values: lib/mingw-w64/mingw-w64-headers/crt/errno.h
199197const generatedGoFilePrefixWindows = generatedGoFilePrefixBase + `
200- var __errno_mapping = [...]syscall.Errno{
198+ var _Cgo___errno_mapping = [...]syscall.Errno{
201199 1: syscall.EPERM,
202200 2: syscall.ENOENT,
203201 3: syscall.ESRCH,
@@ -238,10 +236,10 @@ var __errno_mapping = [...]syscall.Errno{
238236 42: syscall.EILSEQ,
239237}
240238
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 {
245243 return mapped
246244 }
247245 }
@@ -304,23 +302,6 @@ func Process(files []*ast.File, dir, importPath string, fset *token.FileSet, cfl
304302 // If the Comments field is not set to nil, the go/format package will get
305303 // confused about where comments should go.
306304 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 )
324305
325306 // Find `import "C"` C fragments in the file.
326307 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
399380 Tok : token .TYPE ,
400381 }
401382 for _ , name := range builtinAliases {
402- typeSpec := p .getIntegerType ("C. " + name , names ["_Cgo_" + name ])
383+ typeSpec := p .getIntegerType ("_Cgo_ " + name , names ["_Cgo_" + name ])
403384 gen .Specs = append (gen .Specs , typeSpec )
404385 }
405386 p .generated .Decls = append (p .generated .Decls , gen )
@@ -1272,7 +1253,7 @@ func (p *cgoPackage) getUnnamedDeclName(prefix string, itf interface{}) string {
12721253func (f * cgoFile ) getASTDeclName (name string , found clangCursor , iscall bool ) string {
12731254 // Some types are defined in stdint.h and map directly to a particular Go
12741255 // type.
1275- if alias := cgoAliases ["C. " + name ]; alias != "" {
1256+ if alias := cgoAliases ["_Cgo_ " + name ]; alias != "" {
12761257 return alias
12771258 }
12781259 node := f .getASTDeclNode (name , found )
@@ -1282,7 +1263,7 @@ func (f *cgoFile) getASTDeclName(name string, found clangCursor, iscall bool) st
12821263 }
12831264 return node .Name .Name
12841265 }
1285- return "C. " + name
1266+ return "_Cgo_ " + name
12861267}
12871268
12881269// getASTDeclNode will declare the given C AST node (if not already defined) and
@@ -1382,8 +1363,8 @@ extern __typeof(%s) %s __attribute__((alias(%#v)));
13821363 case * elaboratedTypeInfo :
13831364 // Add struct bitfields.
13841365 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 )
13871368 }
13881369 if elaboratedType .unionSize != 0 {
13891370 // Create union getters/setters.
@@ -1392,7 +1373,7 @@ extern __typeof(%s) %s __attribute__((alias(%#v)));
13921373 f .addError (elaboratedType .pos , fmt .Sprintf ("union must have field with a single name, it has %d names" , len (field .Names )))
13931374 continue
13941375 }
1395- f .createUnionAccessor (field , "C. " + name )
1376+ f .createUnionAccessor (field , "_Cgo_ " + name )
13961377 }
13971378 }
13981379 }
@@ -1441,7 +1422,7 @@ func (f *cgoFile) walker(cursor *astutil.Cursor, names map[string]clangCursor) b
14411422 node .Rhs = append (node .Rhs , & ast.CallExpr {
14421423 Fun : & ast.Ident {
14431424 NamePos : node .Lhs [1 ].End (),
1444- Name : "C.__get_errno " ,
1425+ Name : "_Cgo___get_errno " ,
14451426 },
14461427 })
14471428 }
@@ -1466,7 +1447,7 @@ func (f *cgoFile) walker(cursor *astutil.Cursor, names map[string]clangCursor) b
14661447 return true
14671448 }
14681449 if x .Name == "C" {
1469- name := "C. " + node .Sel .Name
1450+ name := "_Cgo_ " + node .Sel .Name
14701451 if found , ok := names [node .Sel .Name ]; ok {
14711452 name = f .getASTDeclName (node .Sel .Name , found , false )
14721453 }
0 commit comments