本文整理汇总了Golang中go/ast.Expr.Name方法的典型用法代码示例。如果您正苦于以下问题:Golang Expr.Name方法的具体用法?Golang Expr.Name怎么用?Golang Expr.Name使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类go/ast.Expr
的用法示例。
在下文中一共展示了Expr.Name方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: compile
//.........这里部分代码省略.........
case *ast.CallExpr:
l := a.compile(x.Fun, true)
args := make([]*expr, len(x.Args))
bad := false
for i, arg := range x.Args {
if i == 0 && l != nil && (l.t == Type(makeType) || l.t == Type(newType)) {
argei := &exprInfo{a.compiler, arg.Pos()}
args[i] = argei.exprFromType(a.compileType(a.block, arg))
} else {
args[i] = a.compile(arg, false)
}
if args[i] == nil {
bad = true
}
}
if bad || l == nil {
return nil
}
if a.constant {
a.diagAt(x, "function call in constant context")
return nil
}
if l.valType != nil {
a.diagAt(x, "type conversions not implemented")
return nil
} else if ft, ok := l.t.(*FuncType); ok && ft.builtin != "" {
return ei.compileBuiltinCallExpr(a.block, ft, args)
} else {
return ei.compileCallExpr(a.block, l, args)
}
case *ast.Ident:
return ei.compileIdent(a.block, a.constant, callCtx, x.Name())
case *ast.IndexExpr:
l, r := a.compile(x.X, false), a.compile(x.Index, false)
if l == nil || r == nil {
return nil
}
return ei.compileIndexExpr(l, r)
case *ast.SliceExpr:
end := x.End
if end == nil {
// TODO: set end to len(x.X)
panic("unimplemented")
}
arr := a.compile(x.X, false)
lo := a.compile(x.Index, false)
hi := a.compile(end, false)
if arr == nil || lo == nil || hi == nil {
return nil
}
return ei.compileSliceExpr(arr, lo, hi)
case *ast.KeyValueExpr:
goto notimpl
case *ast.ParenExpr:
return a.compile(x.X, callCtx)
case *ast.SelectorExpr:
v := a.compile(x.X, false)
if v == nil {
return nil
示例2: MangleExpr
func (sc *PackageScoping) MangleExpr(e ast.Expr) ast.Expr {
switch e := e.(type) {
case *ast.CallExpr:
for i := range e.Args {
e.Args[i] = sc.MangleExpr(e.Args[i])
}
sc.MangleExpr(e.Fun)
if fn, ok := e.Fun.(*ast.Ident); ok {
switch fn.Name {
case "print", "println":
sc.Do("runtime.print")
sc.Do("runtime.print_int")
case "new":
sc.Do("runtime.malloc")
}
}
case *ast.Ident:
if pkg, ok := sc.Globals[e.Name]; ok {
// It's a global identifier, so we need to mangle it...
sc.Do(pkg + "." + e.Name)
oldname := e.Name
e.Name = ManglePackageAndName(pkg, e.Name)
fmt.Println("Name is", e.Name, "from", pkg, oldname)
} else {
// Nothing to do here, it is a local identifier or builtin.
}
case *ast.BasicLit:
// Nothing to do here, a literal is never imported.
case *ast.BinaryExpr:
e.X = sc.MangleExpr(e.X)
e.Y = sc.MangleExpr(e.Y)
// FIXME: We could do better here if we had type information...
switch e.Op {
case token.EQL:
sc.Do("runtime.strings_equal")
case token.NEQ:
sc.Do("runtime.strings_unequal")
}
case *ast.UnaryExpr:
sc.MangleExpr(e.X)
case *ast.ParenExpr:
sc.MangleExpr(e.X)
case *ast.IndexExpr:
e.X = sc.MangleExpr(e.X)
e.Index = sc.MangleExpr(e.Index)
case *ast.SliceExpr:
e.X = sc.MangleExpr(e.X)
e.Low = sc.MangleExpr(e.Low)
e.High = sc.MangleExpr(e.High)
sc.Do("runtime.slice_string")
case *ast.StarExpr:
sc.MangleExpr(e.X)
case *ast.SelectorExpr:
if b, ok := e.X.(*ast.Ident); ok {
if theimp, ok := sc.Imports[b.Name]; ok {
sc.Do(theimp + "." + e.Sel.Name)
e.Sel.Name = ManglePackageAndName(theimp, e.Sel.Name)
return e.Sel
} else {
fmt.Println("not a package: ", b.Name)
fmt.Println("Imports are", sc.Imports)
sc.MangleExpr(e.X)
}
} else {
sc.MangleExpr(e.X)
}
case *ast.StructType:
for _, f := range e.Fields.List {
sc.MangleExpr(f.Type)
}
case *ast.CompositeLit:
e.Type = sc.MangleExpr(e.Type)
for i := range e.Elts {
e.Elts[i] = sc.MangleExpr(e.Elts[i])
}
case *ast.MapType:
e.Key = sc.MangleExpr(e.Key)
e.Value = sc.MangleExpr(e.Value)
case *ast.Ellipsis:
e.Elt = sc.MangleExpr(e.Elt)
case *ast.InterfaceType:
for _, field := range e.Methods.List {
field.Type = sc.MangleExpr(field.Type)
}
case *ast.ArrayType:
e.Len = sc.MangleExpr(e.Len)
e.Elt = sc.MangleExpr(e.Elt)
case *ast.TypeAssertExpr:
e.X = sc.MangleExpr(e.X)
e.Type = sc.MangleExpr(e.Type)
case *ast.FuncType:
for _, field := range e.Params.List {
field.Type = sc.MangleExpr(field.Type)
}
for _, field := range e.Results.List {
field.Type = sc.MangleExpr(field.Type)
}
case *ast.FuncLit:
for _, field := range e.Type.Params.List {
field.Type = sc.MangleExpr(field.Type)
//.........这里部分代码省略.........