]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: update to handle ast.IndexListExpr
authorMatthew Dempsky <mdempsky@google.com>
Mon, 4 Oct 2021 23:20:57 +0000 (16:20 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Thu, 7 Oct 2021 18:02:14 +0000 (18:02 +0000)
Allows cgo to work with generics.

Updates #47781.

Change-Id: Id1a5d1a0a8193c5b157e3e671b1490d687d10384
Reviewed-on: https://go-review.googlesource.com/c/go/+/353882
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
misc/cgo/test/typeparam.go [new file with mode: 0644]
misc/go.mod
src/cmd/cgo/ast.go
src/cmd/cgo/ast_go1.go [new file with mode: 0644]
src/cmd/cgo/ast_go118.go [new file with mode: 0644]
src/cmd/cgo/gcc.go

diff --git a/misc/cgo/test/typeparam.go b/misc/cgo/test/typeparam.go
new file mode 100644 (file)
index 0000000..5f766c2
--- /dev/null
@@ -0,0 +1,17 @@
+// Copyright 2021 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 cgotest
+
+// #include <stddef.h>
+import "C"
+
+func generic[T, U any](t T, u U) {}
+
+func useGeneric() {
+       const zero C.size_t = 0
+
+       generic(zero, zero)
+       generic[C.size_t, C.size_t](0, 0)
+}
index fc9f1133a4608ae4ca0143f4a8fc4414866d9400..712a051f4573c1fb521d67cb8e96be0008eb7c89 100644 (file)
@@ -8,4 +8,4 @@
 // directory.)
 module misc
 
-go 1.12
+go 1.18
index a073407a961e15af2a7b4cbe92afd925712e82b3..28879e349c49cf371b40a7fb0d09f2e4b285f549 100644 (file)
@@ -338,8 +338,7 @@ func (f *File) walk(x interface{}, context astContext, visit func(*File, interfa
 
        // everything else just recurs
        default:
-               error_(token.NoPos, "unexpected type %T in walk", x)
-               panic("unexpected type")
+               f.walkUnexpected(x, context, visit)
 
        case nil:
 
diff --git a/src/cmd/cgo/ast_go1.go b/src/cmd/cgo/ast_go1.go
new file mode 100644 (file)
index 0000000..cf6d99f
--- /dev/null
@@ -0,0 +1,17 @@
+// Copyright 2021 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.
+
+//go:build !go1.18
+// +build !go1.18
+
+package main
+
+import (
+       "go/token"
+)
+
+func (f *File) walkUnexpected(x interface{}, context astContext, visit func(*File, interface{}, astContext)) {
+       error_(token.NoPos, "unexpected type %T in walk", x)
+       panic("unexpected type")
+}
diff --git a/src/cmd/cgo/ast_go118.go b/src/cmd/cgo/ast_go118.go
new file mode 100644 (file)
index 0000000..2e3ce27
--- /dev/null
@@ -0,0 +1,25 @@
+// Copyright 2021 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.
+
+//go:build go1.18
+// +build go1.18
+
+package main
+
+import (
+       "go/ast"
+       "go/token"
+)
+
+func (f *File) walkUnexpected(x interface{}, context astContext, visit func(*File, interface{}, astContext)) {
+       switch n := x.(type) {
+       default:
+               error_(token.NoPos, "unexpected type %T in walk", x)
+               panic("unexpected type")
+
+       case *ast.IndexListExpr:
+               f.walk(&n.X, ctxExpr, visit)
+               f.walk(n.Indices, ctxExpr, visit)
+       }
+}
index f5682c0997ddf713b2a30ad22edab3fa12713a81..c78197896c39a44abf7f3ca1b75d03ea0d723853 100644 (file)
@@ -1506,7 +1506,7 @@ func (p *Package) rewriteName(f *File, r *Ref, addPosition bool) ast.Expr {
                                Args: []ast.Expr{getNewIdent(name.Mangle)},
                        }
                case "type":
-                       // Okay - might be new(T)
+                       // Okay - might be new(T), T(x), Generic[T], etc.
                        if r.Name.Type == nil {
                                error_(r.Pos(), "expression C.%s: undefined C type '%s'", fixGo(r.Name.Go), r.Name.C)
                        }