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: 's 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 {
// If this call expects two results, we have to
// adjust the results of the function we generated.
if ref.Context == "call2" {
if ref.Context == ctxCall2 {
if ftype.Results == nil {
// An explicit void argument
// looks odd but it seems to
......@@ -940,8 +940,8 @@ func (p *Package) checkAddrArgs(f *File, args []ast.Expr, x ast.Expr) []ast.Expr
// effect is a function call.
func (p *Package) hasSideEffects(f *File, x ast.Expr) bool {
found := false
f.walk(x, "expr",
func(f *File, x interface{}, context string) {
f.walk(x, ctxExpr,
func(f *File, x interface{}, context astContext) {
switch x.(type) {
case *ast.CallExpr:
found = true
......@@ -1080,10 +1080,10 @@ func (p *Package) rewriteRef(f *File) {
}
var expr ast.Expr = ast.NewIdent(r.Name.Mangle) // default
switch r.Context {
case "call", "call2":
case ctxCall, ctxCall2:
if r.Name.Kind != "func" {
if r.Name.Kind == "type" {
r.Context = "type"
r.Context = ctxType
if r.Name.Type == nil {
error_(r.Pos(), "invalid conversion to C.%s: undefined C type '%s'", fixGo(r.Name.Go), r.Name.C)
break
......@@ -1095,7 +1095,7 @@ func (p *Package) rewriteRef(f *File) {
break
}
functions[r.Name.Go] = true
if r.Context == "call2" {
if r.Context == ctxCall2 {
if r.Name.Go == "_CMalloc" {
error_(r.Pos(), "no two-result form for C.malloc")
break
......@@ -1113,7 +1113,7 @@ func (p *Package) rewriteRef(f *File) {
r.Name = n
break
}
case "expr":
case ctxExpr:
switch r.Name.Kind {
case "func":
if builtinDefs[r.Name.C] != "" {
......@@ -1154,13 +1154,13 @@ func (p *Package) rewriteRef(f *File) {
case "macro":
expr = &ast.CallExpr{Fun: expr}
}
case "selector":
case ctxSelector:
if r.Name.Kind == "var" {
expr = &ast.StarExpr{Star: (*r.Expr).Pos(), X: expr}
} else {
error_(r.Pos(), "only C variables allowed in selector expression %s", fixGo(r.Name.Go))
}
case "type":
case ctxType:
if r.Name.Kind != "type" {
error_(r.Pos(), "expression C.%s used as type", fixGo(r.Name.Go))
} else if r.Name.Type == nil {
......
......@@ -76,7 +76,7 @@ type Call struct {
type Ref struct {
Name *Name
Expr *ast.Expr
Context string // "type", "expr", "call", or "call2"
Context astContext
}
func (r *Ref) Pos() token.Pos {
......@@ -301,7 +301,7 @@ func main() {
p.Translate(f)
for _, cref := range f.Ref {
switch cref.Context {
case "call", "call2":
case ctxCall, ctxCall2:
if cref.Name.Kind != "type" {
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