Exemple #1
0
// compiles a binary expression x 'op' y
func (w *World) compileBinaryExpr(n *ast.BinaryExpr) Expr {
	switch n.Op {
	default:
		panic(err(n.Pos(), "not allowed:", n.Op))
	case token.ADD:
		return &add{w.newBinExpr(n)}
	case token.SUB:
		return &sub{w.newBinExpr(n)}
	case token.MUL:
		return &mul{w.newBinExpr(n)}
	case token.QUO:
		return &quo{w.newBinExpr(n)}
	case token.LSS:
		return &lss{w.newComp(n)}
	case token.GTR:
		return &gtr{w.newComp(n)}
	case token.LEQ:
		return &leq{w.newComp(n)}
	case token.GEQ:
		return &geq{w.newComp(n)}
	case token.EQL:
		return &eql{w.newComp(n)}
	case token.NEQ:
		return &neq{w.newComp(n)}
	case token.LAND:
		return &and{w.newBoolOp(n)}
	case token.LOR:
		return &or{w.newBoolOp(n)}
	}
}
func (f *File) checkNilFuncComparison(e *ast.BinaryExpr) {
	if !vet("nilfunc") {
		return
	}

	// Only want == or != comparisons.
	if e.Op != token.EQL && e.Op != token.NEQ {
		return
	}

	// Only want comparisons with a nil identifier on one side.
	var e2 ast.Expr
	switch {
	case f.isNil(e.X):
		e2 = e.Y
	case f.isNil(e.Y):
		e2 = e.X
	default:
		return
	}

	// Only want identifiers or selector expressions.
	var obj types.Object
	switch v := e2.(type) {
	case *ast.Ident:
		obj = f.pkg.idents[v]
	case *ast.SelectorExpr:
		obj = f.pkg.idents[v.Sel]
	default:
		return
	}

	// Only want functions.
	if _, ok := obj.(*types.Func); !ok {
		return
	}

	f.Badf(e.Pos(), "comparison of function %v %v nil is always %v", obj.Name(), e.Op, e.Op == token.NEQ)
}
Exemple #3
0
func (w *World) newBoolOp(n *ast.BinaryExpr) boolOp {
	x := typeConv(n.Pos(), w.compileExpr(n.X), bool_t)
	y := typeConv(n.Pos(), w.compileExpr(n.Y), bool_t)
	return boolOp{x, y}
}
Exemple #4
0
func (w *World) newBinExpr(n *ast.BinaryExpr) binaryExpr {
	x := typeConv(n.Pos(), w.compileExpr(n.X), float64_t)
	y := typeConv(n.Pos(), w.compileExpr(n.Y), float64_t)
	return binaryExpr{x, y}
}