Commit f2a5ed85 authored by Bryan C. Mills's avatar Bryan C. Mills Committed by Bryan Mills

cmd/cgo: use a named type to indicate syntactic context

We previously used bare strings, which made it difficult to see (and
to cross-reference) the set of allowed context values.

This change is purely cosmetic, but makes it easier for me to
understand how to address #21878.

updates #21878

Change-Id: I9027d94fd5997a0fe857c0055dea8719e1511f03
Reviewed-on: https://go-review.googlesource.com/63830
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 3066dbad
This diff is collapsed.
...@@ -748,7 +748,7 @@ func (p *Package) rewriteCall(f *File, call *Call, name *Name) bool { ...@@ -748,7 +748,7 @@ func (p *Package) rewriteCall(f *File, call *Call, name *Name) bool {
// If this call expects two results, we have to // If this call expects two results, we have to
// adjust the results of the function we generated. // adjust the results of the function we generated.
if ref.Context == "call2" { if ref.Context == ctxCall2 {
if ftype.Results == nil { if ftype.Results == nil {
// An explicit void argument // An explicit void argument
// looks odd but it seems to // looks odd but it seems to
...@@ -940,8 +940,8 @@ func (p *Package) checkAddrArgs(f *File, args []ast.Expr, x ast.Expr) []ast.Expr ...@@ -940,8 +940,8 @@ func (p *Package) checkAddrArgs(f *File, args []ast.Expr, x ast.Expr) []ast.Expr
// effect is a function call. // effect is a function call.
func (p *Package) hasSideEffects(f *File, x ast.Expr) bool { func (p *Package) hasSideEffects(f *File, x ast.Expr) bool {
found := false found := false
f.walk(x, "expr", f.walk(x, ctxExpr,
func(f *File, x interface{}, context string) { func(f *File, x interface{}, context astContext) {
switch x.(type) { switch x.(type) {
case *ast.CallExpr: case *ast.CallExpr:
found = true found = true
...@@ -1080,10 +1080,10 @@ func (p *Package) rewriteRef(f *File) { ...@@ -1080,10 +1080,10 @@ func (p *Package) rewriteRef(f *File) {
} }
var expr ast.Expr = ast.NewIdent(r.Name.Mangle) // default var expr ast.Expr = ast.NewIdent(r.Name.Mangle) // default
switch r.Context { switch r.Context {
case "call", "call2": case ctxCall, ctxCall2:
if r.Name.Kind != "func" { if r.Name.Kind != "func" {
if r.Name.Kind == "type" { if r.Name.Kind == "type" {
r.Context = "type" r.Context = ctxType
if r.Name.Type == nil { if r.Name.Type == nil {
error_(r.Pos(), "invalid conversion to C.%s: undefined C type '%s'", fixGo(r.Name.Go), r.Name.C) error_(r.Pos(), "invalid conversion to C.%s: undefined C type '%s'", fixGo(r.Name.Go), r.Name.C)
break break
...@@ -1095,7 +1095,7 @@ func (p *Package) rewriteRef(f *File) { ...@@ -1095,7 +1095,7 @@ func (p *Package) rewriteRef(f *File) {
break break
} }
functions[r.Name.Go] = true functions[r.Name.Go] = true
if r.Context == "call2" { if r.Context == ctxCall2 {
if r.Name.Go == "_CMalloc" { if r.Name.Go == "_CMalloc" {
error_(r.Pos(), "no two-result form for C.malloc") error_(r.Pos(), "no two-result form for C.malloc")
break break
...@@ -1113,7 +1113,7 @@ func (p *Package) rewriteRef(f *File) { ...@@ -1113,7 +1113,7 @@ func (p *Package) rewriteRef(f *File) {
r.Name = n r.Name = n
break break
} }
case "expr": case ctxExpr:
switch r.Name.Kind { switch r.Name.Kind {
case "func": case "func":
if builtinDefs[r.Name.C] != "" { if builtinDefs[r.Name.C] != "" {
...@@ -1154,13 +1154,13 @@ func (p *Package) rewriteRef(f *File) { ...@@ -1154,13 +1154,13 @@ func (p *Package) rewriteRef(f *File) {
case "macro": case "macro":
expr = &ast.CallExpr{Fun: expr} expr = &ast.CallExpr{Fun: expr}
} }
case "selector": case ctxSelector:
if r.Name.Kind == "var" { if r.Name.Kind == "var" {
expr = &ast.StarExpr{Star: (*r.Expr).Pos(), X: expr} expr = &ast.StarExpr{Star: (*r.Expr).Pos(), X: expr}
} else { } else {
error_(r.Pos(), "only C variables allowed in selector expression %s", fixGo(r.Name.Go)) error_(r.Pos(), "only C variables allowed in selector expression %s", fixGo(r.Name.Go))
} }
case "type": case ctxType:
if r.Name.Kind != "type" { if r.Name.Kind != "type" {
error_(r.Pos(), "expression C.%s used as type", fixGo(r.Name.Go)) error_(r.Pos(), "expression C.%s used as type", fixGo(r.Name.Go))
} else if r.Name.Type == nil { } else if r.Name.Type == nil {
......
...@@ -76,7 +76,7 @@ type Call struct { ...@@ -76,7 +76,7 @@ type Call struct {
type Ref struct { type Ref struct {
Name *Name Name *Name
Expr *ast.Expr Expr *ast.Expr
Context string // "type", "expr", "call", or "call2" Context astContext
} }
func (r *Ref) Pos() token.Pos { func (r *Ref) Pos() token.Pos {
...@@ -301,7 +301,7 @@ func main() { ...@@ -301,7 +301,7 @@ func main() {
p.Translate(f) p.Translate(f)
for _, cref := range f.Ref { for _, cref := range f.Ref {
switch cref.Context { switch cref.Context {
case "call", "call2": case ctxCall, ctxCall2:
if cref.Name.Kind != "type" { if cref.Name.Kind != "type" {
break break
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment