@@ -22,6 +22,7 @@ import (
22
22
"regexp"
23
23
"sort"
24
24
"strings"
25
+ "unicode"
25
26
)
26
27
27
28
var (
@@ -802,6 +803,24 @@ func (p *Package) packedAttribute() string {
802
803
return s + "))"
803
804
}
804
805
806
+ // exportParamName returns the value of param as it should be
807
+ // displayed in a c header file. If param contains any non-ASCII
808
+ // characters, this function will return the character p followed by
809
+ // the value of position; otherwise, this function will return the
810
+ // value of param.
811
+ func exportParamName (param string , position int ) string {
812
+ pname := param
813
+
814
+ for i := 0 ; i < len (param ); i ++ {
815
+ if param [i ] > unicode .MaxASCII {
816
+ pname = fmt .Sprintf ("p%d" , position )
817
+ break
818
+ }
819
+ }
820
+
821
+ return pname
822
+ }
823
+
805
824
// Write out the various stubs we need to support functions exported
806
825
// from Go so that they are callable from C.
807
826
func (p * Package ) writeExports (fgo2 , fm , fgcc , fgcch io.Writer ) {
@@ -915,7 +934,7 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
915
934
if i > 0 || fn .Recv != nil {
916
935
s += ", "
917
936
}
918
- s += fmt .Sprintf ("%s p%d " , p .cgoType (atype ).C , i )
937
+ s += fmt .Sprintf ("%s %s " , p .cgoType (atype ).C , exportParamName ( aname , i ) )
919
938
})
920
939
s += ")"
921
940
@@ -932,28 +951,28 @@ func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
932
951
fmt .Fprintf (fgcc , "\n %s\n " , s )
933
952
fmt .Fprintf (fgcc , "{\n " )
934
953
fmt .Fprintf (fgcc , "\t __SIZE_TYPE__ _cgo_ctxt = _cgo_wait_runtime_init_done();\n " )
935
- fmt .Fprintf (fgcc , "\t %s %v a ;\n " , ctype , p .packedAttribute ())
954
+ fmt .Fprintf (fgcc , "\t %s %v _cgo_a ;\n " , ctype , p .packedAttribute ())
936
955
if gccResult != "void" && (len (fntype .Results .List ) > 1 || len (fntype .Results .List [0 ].Names ) > 1 ) {
937
956
fmt .Fprintf (fgcc , "\t %s r;\n " , gccResult )
938
957
}
939
958
if fn .Recv != nil {
940
- fmt .Fprintf (fgcc , "\t a .recv = recv;\n " )
959
+ fmt .Fprintf (fgcc , "\t _cgo_a .recv = recv;\n " )
941
960
}
942
961
forFieldList (fntype .Params ,
943
962
func (i int , aname string , atype ast.Expr ) {
944
- fmt .Fprintf (fgcc , "\t a .p%d = p%d ;\n " , i , i )
963
+ fmt .Fprintf (fgcc , "\t _cgo_a .p%d = %s ;\n " , i , exportParamName ( aname , i ) )
945
964
})
946
965
fmt .Fprintf (fgcc , "\t _cgo_tsan_release();\n " )
947
- fmt .Fprintf (fgcc , "\t crosscall2(_cgoexp%s_%s, &a , %d, _cgo_ctxt);\n " , cPrefix , exp .ExpName , off )
966
+ fmt .Fprintf (fgcc , "\t crosscall2(_cgoexp%s_%s, &_cgo_a , %d, _cgo_ctxt);\n " , cPrefix , exp .ExpName , off )
948
967
fmt .Fprintf (fgcc , "\t _cgo_tsan_acquire();\n " )
949
968
fmt .Fprintf (fgcc , "\t _cgo_release_context(_cgo_ctxt);\n " )
950
969
if gccResult != "void" {
951
970
if len (fntype .Results .List ) == 1 && len (fntype .Results .List [0 ].Names ) <= 1 {
952
- fmt .Fprintf (fgcc , "\t return a .r0;\n " )
971
+ fmt .Fprintf (fgcc , "\t return _cgo_a .r0;\n " )
953
972
} else {
954
973
forFieldList (fntype .Results ,
955
974
func (i int , aname string , atype ast.Expr ) {
956
- fmt .Fprintf (fgcc , "\t r.r%d = a .r%d;\n " , i , i )
975
+ fmt .Fprintf (fgcc , "\t r.r%d = _cgo_a .r%d;\n " , i , i )
957
976
})
958
977
fmt .Fprintf (fgcc , "\t return r;\n " )
959
978
}
0 commit comments