本文整理汇总了Golang中go/ast.Expr类的典型用法代码示例。如果您正苦于以下问题:Golang Expr类的具体用法?Golang Expr怎么用?Golang Expr使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Expr类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: recordCommaOkTypes
func (check *Checker) recordCommaOkTypes(x ast.Expr, a [2]Type) {
assert(x != nil)
if a[0] == nil || a[1] == nil {
return
}
assert(isTyped(a[0]) && isTyped(a[1]) && isBoolean(a[1]))
if m := check.Types; m != nil {
for {
tv := m[x]
assert(tv.Type != nil) // should have been recorded already
pos := x.Pos()
tv.Type = NewTuple(
NewVar(pos, check.pkg, "", a[0]),
NewVar(pos, check.pkg, "", a[1]),
)
m[x] = tv
// if x is a parenthesized expression (p.X), update p.X
p, _ := x.(*ast.ParenExpr)
if p == nil {
break
}
x = p.X
}
}
}
示例2: compileArrayLen
// TODO(austin) This is a hack to eliminate a circular dependency
// between type.go and expr.go
func (a *compiler) compileArrayLen(b *block, expr ast.Expr) (int64, bool) {
lenExpr := a.compileExpr(b, true, expr)
if lenExpr == nil {
return 0, false
}
// XXX(Spec) Are ideal floats with no fractional part okay?
if lenExpr.t.isIdeal() {
lenExpr = lenExpr.convertTo(IntType)
if lenExpr == nil {
return 0, false
}
}
if !lenExpr.t.isInteger() {
a.diagAt(expr.Pos(), "array size must be an integer")
return 0, false
}
switch lenExpr.t.lit().(type) {
case *intType:
return lenExpr.asInt()(nil), true
case *uintType:
return int64(lenExpr.asUint()(nil)), true
}
log.Panicf("unexpected integer type %T", lenExpr.t)
return 0, false
}
示例3: constDecl
func (check *Checker) constDecl(obj *Const, typ, init ast.Expr) {
assert(obj.typ == nil)
if obj.visited {
obj.typ = Typ[Invalid]
return
}
obj.visited = true
// use the correct value of iota
assert(check.iota == nil)
check.iota = obj.val
defer func() { check.iota = nil }()
// provide valid constant value under all circumstances
obj.val = exact.MakeUnknown()
// determine type, if any
if typ != nil {
t := check.typ(typ)
if !isConstType(t) {
check.errorf(typ.Pos(), "invalid constant type %s", t)
obj.typ = Typ[Invalid]
return
}
obj.typ = t
}
// check initialization
var x operand
if init != nil {
check.expr(&x, init)
}
check.initConst(obj, &x)
}
示例4: checkCopyLocksRangeVar
func checkCopyLocksRangeVar(f *File, rtok token.Token, e ast.Expr) {
if e == nil {
return
}
id, isId := e.(*ast.Ident)
if isId && id.Name == "_" {
return
}
var typ types.Type
if rtok == token.DEFINE {
if !isId {
return
}
obj := f.pkg.defs[id]
if obj == nil {
return
}
typ = obj.Type()
} else {
typ = f.pkg.types[e].Type
}
if typ == nil {
return
}
if path := lockPath(f.pkg.typesPkg, typ); path != nil {
f.Badf(e.Pos(), "range var %s copies lock: %v", f.gofmt(e), path)
}
}
示例5: eval
func (c *conditionEvaluator) eval(e ast.Expr) bool {
if c.stop {
return false
}
switch e := e.(type) {
case *ast.ParenExpr:
return c.eval(e.X)
case *ast.BinaryExpr:
if e.Op == token.LAND {
return c.eval(e.X) && c.eval(e.Y)
} else if e.Op == token.LOR {
return c.eval(e.X) || c.eval(e.Y)
}
assert(e.Op == token.EQL)
value := c.getVarValue(e.X.(*ast.Ident).Name)
if !value.isBound() {
c.stop = true
return false
}
eqValue := c.cond.equalityValues[e.Pos()]
assert(eqValue.isBound())
return value.compare(c.cond.equalityValues[e.Pos()]) == 0
default:
panic(errors.New("processCondition must have ensured condition is evaluatable"))
}
return false
}
示例6: constDecl
func (check *checker) constDecl(obj *Const, typ, init ast.Expr) {
if obj.visited {
check.errorf(obj.Pos(), "illegal cycle in initialization of constant %s", obj.name)
obj.typ = Typ[Invalid]
return
}
obj.visited = true
// use the correct value of iota
assert(check.iota == nil)
check.iota = obj.val
// determine type, if any
if typ != nil {
t := check.typ(typ, nil, false)
if !isConstType(t) {
check.errorf(typ.Pos(), "invalid constant type %s", t)
obj.typ = Typ[Invalid]
check.iota = nil
return
}
obj.typ = t
}
// check initialization
var x operand
if init != nil {
check.expr(&x, init)
}
check.initConst(obj, &x)
check.iota = nil
}
示例7: validRetExpr
func (f *File) validRetExpr(expr ast.Expr) *Error {
_, ok := expr.(*ast.Ident)
// can only return identifies, i.e. variables
if !ok {
return &Error{errors.New("Return expression only allows identifiers"), expr.Pos()}
}
return nil
}
示例8: emitTraceExpr
func emitTraceExpr(f *Function, event TraceEvent, syntax ast.Expr) Value {
t := &Trace{
Event: event,
Start: syntax.Pos(),
End: syntax.End(),
Breakpoint: false,
syntax: syntax,
}
return emitTraceCommon(f, t)
}
示例9: recordCommaOkTypes
func (check *checker) recordCommaOkTypes(x ast.Expr, t1, t2 Type) {
assert(x != nil && isTyped(t1) && isTyped(t2) && isBoolean(t2))
if m := check.Types; m != nil {
assert(m[x] != nil) // should have been recorded already
pos := x.Pos()
m[x] = NewTuple(
NewVar(pos, check.pkg, "", t1),
NewVar(pos, check.pkg, "", t2),
)
}
}
示例10: extractText
func extractText(ctx *Context, t ast.Expr) (string, error) {
pos := ctx.Fset.Position(t.Pos())
end := ctx.Fset.Position(t.End())
read, err := ioutil.ReadFile(pos.Filename)
if err != nil {
return "", err
}
return string(read[pos.Offset:end.Offset]), nil
}
示例11: matchArgType
func (f *File) matchArgType(t printfArgType, arg ast.Expr) bool {
// TODO: for now, we can only test builtin types and untyped constants.
typ := f.pkg.types[arg]
if typ == nil {
return true
}
basic, ok := typ.(*types.Basic)
if !ok {
return true
}
switch basic.Kind {
case types.Bool:
return t&argBool != 0
case types.Int, types.Int8, types.Int16, types.Int32, types.Int64:
fallthrough
case types.Uint, types.Uint8, types.Uint16, types.Uint32, types.Uint64, types.Uintptr:
return t&argInt != 0
case types.Float32, types.Float64, types.Complex64, types.Complex128:
return t&argFloat != 0
case types.String:
return t&argString != 0
case types.UnsafePointer:
return t&(argPointer|argInt) != 0
case types.UntypedBool:
return t&argBool != 0
case types.UntypedComplex:
return t&argFloat != 0
case types.UntypedFloat:
// If it's integral, we can use an int format.
switch f.pkg.values[arg].(type) {
case int, int8, int16, int32, int64:
return t&(argInt|argFloat) != 0
case uint, uint8, uint16, uint32, uint64:
return t&(argInt|argFloat) != 0
}
return t&argFloat != 0
case types.UntypedInt:
return t&argInt != 0
case types.UntypedRune:
return t&(argInt|argRune) != 0
case types.UntypedString:
return t&argString != 0
case types.UntypedNil:
return t&argPointer != 0 // TODO?
case types.Invalid:
if *verbose {
f.Warnf(arg.Pos(), "printf argument %v has invalid or unknown type", arg)
}
return true // Probably a type check problem.
}
return false
}
示例12: argument
// argument typechecks passing an argument arg (if arg != nil) or
// x (if arg == nil) to the i'th parameter of the given signature.
// If passSlice is set, the argument is followed by ... in the call.
//
func (check *checker) argument(sig *Signature, i int, arg ast.Expr, x *operand, passSlice bool) {
// determine parameter
var par *Var
n := sig.params.Len()
if i < n {
par = sig.params.vars[i]
} else if sig.isVariadic {
par = sig.params.vars[n-1]
} else {
var pos token.Pos
switch {
case arg != nil:
pos = arg.Pos()
case x != nil:
pos = x.pos()
default:
// TODO(gri) what position to use?
}
check.errorf(pos, "too many arguments")
return
}
// determine argument
var z operand
z.mode = variable
z.expr = nil // TODO(gri) can we do better here? (for good error messages)
z.typ = par.typ
if arg != nil {
check.expr(x, arg, z.typ, -1)
}
if x.mode == invalid {
return // ignore this argument
}
// check last argument of the form x...
if passSlice {
if i+1 != n {
check.errorf(x.pos(), "can only use ... with matching parameter")
return // ignore this argument
}
// spec: "If the final argument is assignable to a slice type []T,
// it may be passed unchanged as the value for a ...T parameter if
// the argument is followed by ..."
z.typ = &Slice{elt: z.typ} // change final parameter type to []T
}
if !check.assignment(x, z.typ) && x.mode != invalid {
check.errorf(x.pos(), "cannot pass argument %s to %s", x, &z)
}
}
示例13: checkAtomicAddAssignment
// checkAtomicAddAssignment walks the atomic.Add* method calls checking for assigning the return value
// to the same variable being used in the operation
func (f *File) checkAtomicAddAssignment(left ast.Expr, call *ast.CallExpr) {
arg := call.Args[0]
broken := false
if uarg, ok := arg.(*ast.UnaryExpr); ok && uarg.Op == token.AND {
broken = f.gofmt(left) == f.gofmt(uarg.X)
} else if star, ok := left.(*ast.StarExpr); ok {
broken = f.gofmt(star.X) == f.gofmt(arg)
}
if broken {
f.Warn(left.Pos(), "direct assignment to atomic value")
}
}
示例14: matchWildcard
func (tr *Transformer) matchWildcard(xobj *types.Var, y ast.Expr) bool {
name := xobj.Name()
if tr.verbose {
fmt.Fprintf(os.Stderr, "%s: wildcard %s -> %s?: ",
tr.fset.Position(y.Pos()), name, astString(tr.fset, y))
}
// Check that y is assignable to the declared type of the param.
yt := tr.info.TypeOf(y)
if yt == nil {
// y has no type.
// Perhaps it is an *ast.Ellipsis in [...]T{}, or
// an *ast.KeyValueExpr in T{k: v}.
// Clearly these pseudo-expressions cannot match a
// wildcard, but it would nice if we had a way to ignore
// the difference between T{v} and T{k:v} for structs.
return false
}
if !types.AssignableTo(yt, xobj.Type()) {
if tr.verbose {
fmt.Fprintf(os.Stderr, "%s not assignable to %s\n", yt, xobj.Type())
}
return false
}
// A wildcard matches any expression.
// If it appears multiple times in the pattern, it must match
// the same expression each time.
if old, ok := tr.env[name]; ok {
// found existing binding
tr.allowWildcards = false
r := tr.matchExpr(old, y)
if tr.verbose {
fmt.Fprintf(os.Stderr, "%t secondary match, primary was %s\n",
r, astString(tr.fset, old))
}
tr.allowWildcards = true
return r
}
if tr.verbose {
fmt.Fprintf(os.Stderr, "primary match\n")
}
tr.env[name] = y // record binding
return true
}
示例15: isUntypedConst
// isUntypedConst reports whether expr is an untyped constant,
// and indicates what its default type is.
// scope may be nil.
func (f *file) isUntypedConst(expr ast.Expr) (defType string, ok bool) {
// Re-evaluate expr outside of its context to see if it's untyped.
// (An expr evaluated within, for example, an assignment context will get the type of the LHS.)
exprStr := f.render(expr)
tv, err := types.Eval(f.fset, f.pkg.typesPkg, expr.Pos(), exprStr)
if err != nil {
return "", false
}
if b, ok := tv.Type.(*types.Basic); ok {
if dt, ok := basicTypeKinds[b.Kind()]; ok {
return dt, true
}
}
return "", false
}