aboutsummaryrefslogtreecommitdiffhomepage
path: root/cgo
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2019-12-30 21:17:15 +0100
committerRon Evans <[email protected]>2020-01-03 23:44:58 +0100
commitd735df6e169a0513ecf43323a2e2e628d9d95a85 (patch)
tree565fbe2931c7295e6266ad5e0a12b9c105cd7edc /cgo
parentd37bbadb542710eff9ae576581fb9950349979d7 (diff)
downloadtinygo-d735df6e169a0513ecf43323a2e2e628d9d95a85.tar.gz
tinygo-d735df6e169a0513ecf43323a2e2e628d9d95a85.zip
cgo: add support for symbols
Diffstat (limited to 'cgo')
-rw-r--r--cgo/cgo_test.go2
-rw-r--r--cgo/const.go22
-rw-r--r--cgo/const_test.go4
-rw-r--r--cgo/testdata/const.go12
-rw-r--r--cgo/testdata/const.out.go29
5 files changed, 66 insertions, 3 deletions
diff --git a/cgo/cgo_test.go b/cgo/cgo_test.go
index d057584cb..e9ae2f43e 100644
--- a/cgo/cgo_test.go
+++ b/cgo/cgo_test.go
@@ -22,7 +22,7 @@ var flagUpdate = flag.Bool("update", false, "Update images based on test output.
func TestCGo(t *testing.T) {
var cflags = []string{"--target=armv6m-none-eabi"}
- for _, name := range []string{"basic", "errors", "types", "flags"} {
+ for _, name := range []string{"basic", "errors", "types", "flags", "const"} {
name := name // avoid a race condition
t.Run(name, func(t *testing.T) {
// Read the AST in memory.
diff --git a/cgo/const.go b/cgo/const.go
index 97483a5cf..f69bbb817 100644
--- a/cgo/const.go
+++ b/cgo/const.go
@@ -52,6 +52,13 @@ func parseConstExpr(t *tokenizer) (ast.Expr, *scanner.Error) {
}
t.Next()
return expr, nil
+ case token.IDENT:
+ expr := &ast.Ident{
+ NamePos: t.pos,
+ Name: "C." + t.value,
+ }
+ t.Next()
+ return expr, nil
case token.EOF:
return nil, &scanner.Error{
Pos: t.fset.Position(t.pos),
@@ -150,6 +157,21 @@ func (t *tokenizer) Next() {
t.value = strings.TrimRight(t.value, "uUlL")
}
return
+ case c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c == '_':
+ // Identifier. Find all remaining tokens that are part of this
+ // identifier.
+ tokenLen := len(t.buf)
+ for i, c := range t.buf {
+ if c >= '0' && c <= '9' || c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c == '_' {
+ tokenLen = i + 1
+ } else {
+ break
+ }
+ }
+ t.value = t.buf[:tokenLen]
+ t.buf = t.buf[tokenLen:]
+ t.token = token.IDENT
+ return
case c == '"':
// String constant. Find the first '"' character that is not
// preceded by a backslash.
diff --git a/cgo/const_test.go b/cgo/const_test.go
index 235a7117b..eaac3047f 100644
--- a/cgo/const_test.go
+++ b/cgo/const_test.go
@@ -21,8 +21,8 @@ func TestParseConst(t *testing.T) {
{`5)`, `error: 1:2: unexpected token )`},
{" \t)", `error: 1:4: unexpected token )`},
{`5.8f`, `5.8`},
- {`foo`, `error: 1:1: unexpected token ILLEGAL`}, // identifiers unimplemented
- {``, `error: 1:1: empty constant`}, // empty constants not allowed in Go
+ {`foo`, `C.foo`},
+ {``, `error: 1:1: empty constant`}, // empty constants not allowed in Go
{`"foo"`, `"foo"`},
{`"a\\n"`, `"a\\n"`},
{`"a\n"`, `"a\n"`},
diff --git a/cgo/testdata/const.go b/cgo/testdata/const.go
new file mode 100644
index 000000000..43645d3ac
--- /dev/null
+++ b/cgo/testdata/const.go
@@ -0,0 +1,12 @@
+package main
+
+/*
+#define foo 3
+#define bar foo
+*/
+import "C"
+
+const (
+ Foo = C.foo
+ Bar = C.bar
+)
diff --git a/cgo/testdata/const.out.go b/cgo/testdata/const.out.go
new file mode 100644
index 000000000..d213a6723
--- /dev/null
+++ b/cgo/testdata/const.out.go
@@ -0,0 +1,29 @@
+package main
+
+import "unsafe"
+
+var _ unsafe.Pointer
+
+const C.bar = C.foo
+const C.foo = 3
+
+type C.int16_t = int16
+type C.int32_t = int32
+type C.int64_t = int64
+type C.int8_t = int8
+type C.uint16_t = uint16
+type C.uint32_t = uint32
+type C.uint64_t = uint64
+type C.uint8_t = uint8
+type C.uintptr_t = uintptr
+type C.char uint8
+type C.int int32
+type C.long int32
+type C.longlong int64
+type C.schar int8
+type C.short int16
+type C.uchar uint8
+type C.uint uint32
+type C.ulong uint32
+type C.ulonglong uint64
+type C.ushort uint16