本文整理汇总了Golang中go/ast.Stmt.Pos方法的典型用法代码示例。如果您正苦于以下问题:Golang Stmt.Pos方法的具体用法?Golang Stmt.Pos怎么用?Golang Stmt.Pos使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类go/ast.Stmt
的用法示例。
在下文中一共展示了Stmt.Pos方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: insertInWithin
// insertInWithin places before/after advice around a statement
func (wb *WithinBlock) insertInWithin(a ast.Stmt, w *Weave) string {
rout := ""
mName := grabMethodName(a)
// begin line
begin := wb.fset.Position(a.Pos()).Line - 1
after := wb.fset.Position(a.End()).Line + 1
// until this is refactored - any lines we add in our
// advice need to be accounted for w/begin
before_advice := formatAdvice(wb.aspect.advize.before, mName)
after_advice := formatAdvice(wb.aspect.advize.after, mName)
if before_advice != "" {
rout = w.writeAtLine(wb.fname, begin+wb.linecnt, before_advice)
wb.linecnt += strings.Count(before_advice, "\n") + 1
}
if after_advice != "" {
rout = w.writeAtLine(wb.fname, after+wb.linecnt-1, after_advice)
wb.linecnt += strings.Count(after_advice, "\n") + 1
}
for t := 0; t < len(wb.aspect.importz); t++ {
wb.importsNeeded = append(wb.importsNeeded, wb.aspect.importz[t])
}
return rout
}
示例2: VisitStmt
func (c *compiler) VisitStmt(stmt ast.Stmt) {
if c.logger != nil {
c.logger.Println("Compile statement:", reflect.TypeOf(stmt),
"@", c.fileset.Position(stmt.Pos()))
}
switch x := stmt.(type) {
case *ast.ReturnStmt:
c.VisitReturnStmt(x)
case *ast.AssignStmt:
c.VisitAssignStmt(x)
case *ast.IncDecStmt:
c.VisitIncDecStmt(x)
case *ast.IfStmt:
c.VisitIfStmt(x)
case *ast.ForStmt:
c.VisitForStmt(x)
case *ast.ExprStmt:
c.VisitExpr(x.X)
case *ast.BlockStmt:
c.VisitBlockStmt(x)
case *ast.DeclStmt:
c.VisitDecl(x.Decl)
case *ast.GoStmt:
c.VisitGoStmt(x)
case *ast.SwitchStmt:
c.VisitSwitchStmt(x)
default:
panic(fmt.Sprintf("Unhandled Stmt node: %s", reflect.TypeOf(stmt)))
}
}
示例3: multipleDefaults
func (check *checker) multipleDefaults(list []ast.Stmt) {
var first ast.Stmt
for _, s := range list {
var d ast.Stmt
switch c := s.(type) {
case *ast.CaseClause:
if len(c.List) == 0 {
d = s
}
case *ast.CommClause:
if c.Comm == nil {
d = s
}
default:
check.invalidAST(s.Pos(), "case/communication clause expected")
}
if d != nil {
if first != nil {
check.errorf(d.Pos(), "multiple defaults (first at %s)", first.Pos())
} else {
first = d
}
}
}
}
示例4: emitTraceStmt
func emitTraceStmt(f *Function, event TraceEvent, syntax ast.Stmt) Value {
t := &Trace{
Event: event,
Start: syntax.Pos(),
End: syntax.End(),
Breakpoint: false,
syntax: syntax,
}
return emitTraceCommon(f, t)
}
示例5: makeExpr
func (p *parser) makeExpr(s ast.Stmt) ast.Expr {
if s == nil {
return nil
}
if es, isExpr := s.(*ast.ExprStmt); isExpr {
return p.checkExpr(es.X)
}
p.Error(s.Pos(), "expected condition, found simple statement")
return &ast.BadExpr{s.Pos()}
}
示例6: wrapLoop
func (v *visitor) wrapLoop(node ast.Stmt, body *ast.BlockStmt) (block *ast.BlockStmt, loop ast.Stmt) {
block = astPrintf(`
{
scope := %s.EnteringNewChildScope()
_ = scope // placeholder
godebug.Line(ctx, scope, %s)
}`, v.scopeVar, pos2lineString(node.Pos()))[0].(*ast.BlockStmt)
block.List[1] = node
loop = node
return
}
示例7: validStmt
func (f *File) validStmt(stmt ast.Stmt) *Error {
if stmt == nil {
return nil
}
if assign, ok := stmt.(*ast.AssignStmt); ok {
if len(assign.Lhs) != 1 || len(assign.Rhs) != 1 {
return &Error{errors.New("Invalid assigment statment"), assign.Pos()}
}
return nil
}
if ifstmt, ok := stmt.(*ast.IfStmt); ok {
if err := f.validExpr(ifstmt.Cond); err != nil {
return err
}
// initialization clause not allowed for if statements
if ifstmt.Init != nil {
return &Error{errors.New("ifstmt cannot have initialization clause"), ifstmt.Init.Pos()}
}
if err := f.validStmt(ifstmt.Body); err != nil {
return err
}
if err := f.validStmt(ifstmt.Else); err != nil {
return err
}
}
if blk, ok := stmt.(*ast.BlockStmt); ok {
for _, s := range blk.List {
if err := f.validStmt(s); err != nil {
return err
}
}
}
if _, ok := stmt.(*ast.EmptyStmt); ok {
return nil
}
if ret, ok := stmt.(*ast.ReturnStmt); ok {
if ret.Results == nil || len(ret.Results) == 0 {
return nil
}
if len(ret.Results) > 1 {
return &Error{errors.New("Return statement doesn't allow multiple return values"), ret.Pos()}
}
return f.validRetExpr(ret.Results[0])
}
if indec, ok := stmt.(*ast.IncDecStmt); ok {
// TODO specialize
if err := f.validExpr(indec.X); err != nil {
return err
}
}
return &Error{errors.New(fmt.Sprintf("Invalid stmt:%v", stmt)), stmt.Pos()}
}
示例8: generateCheckedAssign
func generateCheckedAssign(stmt ast.Stmt, place ast.Expr) ast.Stmt {
pos := stmt.Pos()
return &ast.IfStmt{
If: pos,
Init: stmt,
Cond: &ast.BinaryExpr{
X: place,
OpPos: pos,
Op: token.NEQ,
Y: ast.NewIdent("nil")},
Body: &ast.BlockStmt{
Lbrace: pos,
List: []ast.Stmt{&ast.ReturnStmt{}},
Rbrace: pos},
}
}
示例9: VisitStmt
func (v *ShortError) VisitStmt(scope *ast.Scope, stmt ast.Stmt) ScopeVisitor {
v.stmt = stmt
switch stmt := stmt.(type) {
case *ast.BlockStmt:
return &ShortError{v.file, v.patches, v.stmt, stmt, 0, new([]byte)}
case *ast.ExprStmt:
if call := calltomust(stmt.X); call != nil {
// TODO(elazarl): depends on number of variables it returns, currently we assume one
pos := v.file.Fset.Position(stmt.Pos())
fmt.Printf("%s:%d:%d: 'must' builtin must be assigned into variable\n",
pos.Filename, pos.Line, pos.Column)
}
case *ast.AssignStmt:
if len(stmt.Rhs) != 1 {
return v
}
if rhs, ok := stmt.Rhs[0].(*ast.CallExpr); ok {
if fun, ok := rhs.Fun.(*ast.Ident); ok && fun.Name == MustKeyword {
if stmt.Tok == token.DEFINE {
tmpVar := v.tempVar("assignerr_", scope)
*v.patches = append(*v.patches,
patch.Insert(stmt.TokPos, ", "+tmpVar+" "),
patch.Replace(fun, ""),
patch.Insert(stmt.End(),
"; if "+tmpVar+" != nil "+
"{ panic("+tmpVar+") };"),
)
for _, arg := range rhs.Args {
v.VisitExpr(scope, arg)
}
return nil
} else if stmt.Tok == token.ASSIGN {
vars := []string{}
for i := 0; i < len(stmt.Lhs); i++ {
vars = append(vars, v.tempVar(fmt.Sprint("assgn", i, "_"), scope))
}
assgnerr := v.tempVar("assgnErr_", scope)
*v.patches = append(*v.patches,
patch.Insert(stmt.Pos(),
strings.Join(append(vars, assgnerr), ", ")+":="),
patch.InsertNode(stmt.Pos(), rhs.Args[0]),
patch.Insert(stmt.Pos(),
"; if "+assgnerr+" != nil "+
"{ panic("+assgnerr+") };"),
patch.Replace(rhs, strings.Join(vars, ", ")),
)
v.VisitExpr(scope, rhs.Args[0])
return nil
}
}
}
}
return v
}
示例10: rewriteRecvStmt
func (t *rewriteVisitor) rewriteRecvStmt(stmt ast.Stmt) []ast.Stmt {
// Prohibit inner blocks from having channel operation nodes
switch q := stmt.(type) {
case *ast.AssignStmt:
for _, expr := range q.Lhs {
// TODO: Handle channel operations inside LHS of assignments
RecurseProhibit(t, expr)
}
for _, expr := range q.Rhs {
if expr == nil {
continue
}
// TODO: Handle channel operations inside RHS of assignments
if ue := filterRecvExpr(expr); ue != nil {
RecurseProhibit(t, ue.X)
} else {
RecurseProhibit(t, expr)
}
}
case *ast.ExprStmt:
if q == nil {
break
}
// TODO: Handle channel operations inside RHS of assignments
if ue := filterRecvExpr(q.X); ue != nil {
RecurseProhibit(t, ue.X)
} else {
RecurseProhibit(t, q)
}
default:
panic("unreach")
}
// Rewrite receive statement itself
return []ast.Stmt{
makeSimpleCallStmt("vtime", "Block", stmt.Pos()),
stmt,
makeSimpleCallStmt("vtime", "Unblock", stmt.Pos()),
}
}
示例11: compileStmt
// compiles a statement
func (w *World) compileStmt(st ast.Stmt) Expr {
switch st := st.(type) {
default:
panic(err(st.Pos(), "not allowed:", typ(st)))
case *ast.EmptyStmt:
return &emptyStmt{}
case *ast.AssignStmt:
return w.compileAssignStmt(st)
case *ast.ExprStmt:
return w.compileExpr(st.X)
case *ast.IfStmt:
return w.compileIfStmt(st)
case *ast.ForStmt:
return w.compileForStmt(st)
case *ast.IncDecStmt:
return w.compileIncDecStmt(st)
case *ast.BlockStmt:
w.EnterScope()
defer w.ExitScope()
return w.compileBlockStmt_noScope(st)
}
}
示例12: stmt
func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool) {
p.print(stmt.Pos())
switch s := stmt.(type) {
case *ast.BadStmt:
p.print("BadStmt")
case *ast.DeclStmt:
p.decl(s.Decl)
case *ast.EmptyStmt:
// nothing to do
case *ast.LabeledStmt:
// a "correcting" unindent immediately following a line break
// is applied before the line break if there is no comment
// between (see writeWhitespace)
p.print(unindent)
p.expr(s.Label)
p.print(s.Colon, token.COLON, indent)
if e, isEmpty := s.Stmt.(*ast.EmptyStmt); isEmpty {
if !nextIsRBrace {
p.print(newline, e.Pos(), token.SEMICOLON)
break
}
} else {
p.linebreak(p.lineFor(s.Stmt.Pos()), 1, ignore, true)
}
p.stmt(s.Stmt, nextIsRBrace)
case *ast.ExprStmt:
const depth = 1
p.expr0(s.X, depth)
case *ast.SendStmt:
const depth = 1
p.expr0(s.Chan, depth)
p.print(blank, s.Arrow, token.ARROW, blank)
p.expr0(s.Value, depth)
case *ast.IncDecStmt:
const depth = 1
p.expr0(s.X, depth+1)
p.print(s.TokPos, s.Tok)
case *ast.AssignStmt:
var depth = 1
if len(s.Lhs) > 1 && len(s.Rhs) > 1 {
depth++
}
p.exprList(s.Pos(), s.Lhs, depth, 0, s.TokPos)
p.print(blank, s.TokPos, s.Tok, blank)
p.exprList(s.TokPos, s.Rhs, depth, 0, token.NoPos)
case *ast.GoStmt:
p.print(token.GO, blank)
p.expr(s.Call)
case *ast.DeferStmt:
p.print(token.DEFER, blank)
p.expr(s.Call)
case *ast.ReturnStmt:
p.print(token.RETURN)
if s.Results != nil {
p.print(blank)
p.exprList(s.Pos(), s.Results, 1, 0, token.NoPos)
}
case *ast.BranchStmt:
p.print(s.Tok)
if s.Label != nil {
p.print(blank)
p.expr(s.Label)
}
case *ast.BlockStmt:
p.block(s, 1)
case *ast.IfStmt:
p.print(token.IF)
p.controlClause(false, s.Init, s.Cond, nil)
p.block(s.Body, 1)
if s.Else != nil {
p.print(blank, token.ELSE, blank)
switch s.Else.(type) {
case *ast.BlockStmt, *ast.IfStmt:
p.stmt(s.Else, nextIsRBrace)
default:
p.print(token.LBRACE, indent, formfeed)
p.stmt(s.Else, true)
p.print(unindent, formfeed, token.RBRACE)
}
}
case *ast.CaseClause:
if s.List != nil {
p.print(token.CASE, blank)
p.exprList(s.Pos(), s.List, 1, 0, s.Colon)
} else {
//.........这里部分代码省略.........
示例13: checkStmt
// checkStmt type checks a statement.
func (c *checker) checkStmt(s ast.Stmt) {
switch s := s.(type) {
case *ast.AssignStmt:
// TODO Each left-hand side operand must be addressable,
// a map index expression, or the blank identifier. Operands
// may be parenthesized.
if len(s.Rhs) == 1 {
idents := make([]*ast.Ident, len(s.Lhs))
for i, e := range s.Lhs {
if ident, ok := e.(*ast.Ident); ok {
if ident.Obj != nil {
idents[i] = ident
}
} else {
c.checkExpr(e, nil)
}
}
c.checkExpr(s.Rhs[0], idents)
} else {
idents := make([]*ast.Ident, 1)
for i, e := range s.Rhs {
if ident, ok := s.Lhs[i].(*ast.Ident); ok && ident.Obj != nil {
idents[0] = ident
c.checkExpr(e, idents)
} else {
c.checkExpr(e, nil)
}
}
}
case *ast.BlockStmt:
for _, s := range s.List {
c.checkStmt(s)
}
case *ast.ExprStmt:
c.checkExpr(s.X, nil)
case *ast.BranchStmt:
// no-op
case *ast.DeclStmt:
// Only a GenDecl/ValueSpec is permissible in a statement.
decl := s.Decl.(*ast.GenDecl)
for _, spec := range decl.Specs {
spec := spec.(*ast.ValueSpec)
for _, name := range spec.Names {
c.checkObj(name.Obj, true)
}
}
//case *ast.DeferStmt:
//case *ast.EmptyStmt:
case *ast.ForStmt:
if s.Init != nil {
c.checkStmt(s.Init)
}
// TODO make sure cond expr is some sort of bool.
if s.Cond != nil {
c.checkExpr(s.Cond, nil)
}
if s.Post != nil {
c.checkStmt(s.Post)
}
c.checkStmt(s.Body)
//case *ast.IncDecStmt:
//case *ast.GoStmt:
case *ast.IfStmt:
if s.Init != nil {
c.checkStmt(s.Init)
}
// TODO make sure cond expr is some sort of bool.
c.checkExpr(s.Cond, nil)
c.checkStmt(s.Body)
if s.Else != nil {
c.checkStmt(s.Else)
}
case *ast.IncDecStmt:
// TODO check operand is addressable.
// TODO check operand type is suitable for inc/dec.
c.checkExpr(s.X, nil)
case *ast.LabeledStmt:
c.checkStmt(s.Stmt)
case *ast.RangeStmt:
var k, v Type
switch x := Underlying(c.checkExpr(s.X, nil)).(type) {
case *Pointer:
if x, ok := Underlying(x.Base).(*Array); ok {
k, v = Int, x.Elt
} else {
c.errorf(s.Pos(), "invalid type for range")
return
}
case *Array:
//.........这里部分代码省略.........
示例14: stmt
// stmt typechecks statement s.
func (check *checker) stmt(s ast.Stmt) {
switch s := s.(type) {
case *ast.BadStmt, *ast.EmptyStmt:
// ignore
case *ast.DeclStmt:
check.decl(s.Decl)
case *ast.LabeledStmt:
// TODO(gri) anything to do with label itself?
check.stmt(s.Stmt)
case *ast.ExprStmt:
var x operand
used := false
switch e := unparen(s.X).(type) {
case *ast.CallExpr:
// function calls are permitted
used = true
// but some builtins are excluded
check.expr(&x, e.Fun, nil, -1)
if x.mode != invalid {
if b, ok := x.typ.(*builtin); ok && !b.isStatement {
used = false
}
}
case *ast.UnaryExpr:
// receive operations are permitted
if e.Op == token.ARROW {
used = true
}
}
if !used {
check.errorf(s.Pos(), "%s not used", s.X)
// ok to continue
}
check.rawExpr(&x, s.X, nil, -1, false)
if x.mode == typexpr {
check.errorf(x.pos(), "%s is not an expression", &x)
}
case *ast.SendStmt:
var ch, x operand
check.expr(&ch, s.Chan, nil, -1)
check.expr(&x, s.Value, nil, -1)
if ch.mode == invalid || x.mode == invalid {
return
}
if tch, ok := underlying(ch.typ).(*Chan); !ok || tch.Dir&ast.SEND == 0 || !x.isAssignable(tch.Elt) {
check.invalidOp(ch.pos(), "cannot send %s to channel %s", &x, &ch)
}
case *ast.IncDecStmt:
var op token.Token
switch s.Tok {
case token.INC:
op = token.ADD
case token.DEC:
op = token.SUB
default:
check.invalidAST(s.TokPos, "unknown inc/dec operation %s", s.Tok)
return
}
var x, y operand
check.expr(&x, s.X, nil, -1)
check.expr(&y, &ast.BasicLit{ValuePos: x.pos(), Kind: token.INT, Value: "1"}, nil, -1) // use x's position
check.binary(&x, &y, op, nil)
check.assign1to1(s.X, nil, &x, false, -1)
case *ast.AssignStmt:
switch s.Tok {
case token.ASSIGN, token.DEFINE:
if len(s.Lhs) == 0 {
check.invalidAST(s.Pos(), "missing lhs in assignment")
return
}
check.assignNtoM(s.Lhs, s.Rhs, s.Tok == token.DEFINE, -1)
default:
// assignment operations
if len(s.Lhs) != 1 || len(s.Rhs) != 1 {
check.errorf(s.TokPos, "assignment operation %s requires single-valued expressions", s.Tok)
return
}
// TODO(gri) make this conversion more efficient
var op token.Token
switch s.Tok {
case token.ADD_ASSIGN:
op = token.ADD
case token.SUB_ASSIGN:
op = token.SUB
case token.MUL_ASSIGN:
op = token.MUL
case token.QUO_ASSIGN:
op = token.QUO
case token.REM_ASSIGN:
op = token.REM
case token.AND_ASSIGN:
op = token.AND
case token.OR_ASSIGN:
//.........这里部分代码省略.........
示例15: VisitStmt
func (v *stmtVisitor) VisitStmt(s ast.Stmt) {
var statements *[]ast.Stmt
switch s := s.(type) {
case *ast.BlockStmt:
statements = &s.List
case *ast.CaseClause:
statements = &s.Body
case *ast.CommClause:
statements = &s.Body
case *ast.ForStmt:
if s.Init != nil {
v.VisitStmt(s.Init)
}
if s.Post != nil {
v.VisitStmt(s.Post)
}
v.VisitStmt(s.Body)
case *ast.IfStmt:
if s.Init != nil {
v.VisitStmt(s.Init)
}
v.VisitStmt(s.Body)
if s.Else != nil {
v.VisitStmt(s.Else)
}
case *ast.LabeledStmt:
v.VisitStmt(s.Stmt)
case *ast.RangeStmt:
v.VisitStmt(s.Body)
case *ast.SelectStmt:
v.VisitStmt(s.Body)
case *ast.SwitchStmt:
if s.Init != nil {
v.VisitStmt(s.Init)
}
v.VisitStmt(s.Body)
case *ast.TypeSwitchStmt:
if s.Init != nil {
v.VisitStmt(s.Init)
}
v.VisitStmt(s.Assign)
v.VisitStmt(s.Body)
}
if statements == nil {
return
}
for i := 0; i < len(*statements); i++ {
s := (*statements)[i]
switch s.(type) {
case *ast.CaseClause, *ast.CommClause, *ast.BlockStmt:
break
default:
start, end := v.fset.Position(s.Pos()), v.fset.Position(s.End())
stmtObj := v.functions[len(v.functions)-1].RegisterStatement(start.Offset, end.Offset)
expr := makeCall(fmt.Sprint(stmtObj, ".At"))
stmt := &ast.ExprStmt{X: expr}
item := []ast.Stmt{stmt}
*statements = append((*statements)[:i], append(item, (*statements)[i:]...)...)
i++
}
v.VisitStmt(s)
}
}