本文整理汇总了Golang中go/types.Label类的典型用法代码示例。如果您正苦于以下问题:Golang Label类的具体用法?Golang Label怎么用?Golang Label使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Label类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: translateLoopingStmt
func (c *funcContext) translateLoopingStmt(cond func() string, body *ast.BlockStmt, bodyPrefix, post func(), label *types.Label, flatten bool) {
prevFlowData := c.flowDatas[nil]
data := &flowData{
postStmt: post,
}
if flatten {
data.beginCase = c.caseCounter
data.endCase = c.caseCounter + 1
c.caseCounter += 2
}
c.flowDatas[nil] = data
c.flowDatas[label] = data
defer func() {
delete(c.flowDatas, label)
c.flowDatas[nil] = prevFlowData
}()
if !flatten && label != nil {
c.Printf("%s:", label.Name())
}
c.PrintCond(!flatten, "while (true) {", fmt.Sprintf("case %d:", data.beginCase))
c.Indent(func() {
condStr := cond()
if condStr != "true" {
c.PrintCond(!flatten, fmt.Sprintf("if (!(%s)) { break; }", condStr), fmt.Sprintf("if(!(%s)) { $s = %d; continue; }", condStr, data.endCase))
}
prevEV := c.p.escapingVars
c.handleEscapingVars(body)
if bodyPrefix != nil {
bodyPrefix()
}
c.translateStmtList(body.List)
isTerminated := false
if len(body.List) != 0 {
switch body.List[len(body.List)-1].(type) {
case *ast.ReturnStmt, *ast.BranchStmt:
isTerminated = true
}
}
if !isTerminated {
post()
}
c.p.escapingVars = prevEV
})
c.PrintCond(!flatten, "}", fmt.Sprintf("$s = %d; continue; case %d:", data.beginCase, data.endCase))
}
示例2: checkLabel
func (r *renamer) checkLabel(label *types.Label) {
// Check there are no identical labels in the function's label block.
// (Label blocks don't nest, so this is easy.)
if prev := label.Parent().Lookup(r.to); prev != nil {
r.errorf(label.Pos(), "renaming this label %q to %q", label.Name(), prev.Name())
r.errorf(prev.Pos(), "\twould conflict with this one")
}
}
示例3: translateBranchingStmt
func (c *funcContext) translateBranchingStmt(caseClauses []*ast.CaseClause, defaultClause *ast.CaseClause, canBreak bool, translateCond func(ast.Expr) *expression, label *types.Label, flatten bool) {
var caseOffset, defaultCase, endCase int
if flatten {
caseOffset = c.caseCounter
defaultCase = caseOffset + len(caseClauses)
endCase = defaultCase
if defaultClause != nil {
endCase++
}
c.caseCounter = endCase + 1
}
hasBreak := false
if canBreak {
prevFlowData := c.flowDatas[nil]
data := &flowData{
postStmt: prevFlowData.postStmt, // for "continue" of outer loop
beginCase: prevFlowData.beginCase, // same
endCase: endCase,
}
c.flowDatas[nil] = data
c.flowDatas[label] = data
defer func() {
delete(c.flowDatas, label)
c.flowDatas[nil] = prevFlowData
}()
for _, child := range caseClauses {
if analysis.HasBreak(child) {
hasBreak = true
break
}
}
if defaultClause != nil && analysis.HasBreak(defaultClause) {
hasBreak = true
}
}
if label != nil && !flatten {
c.Printf("%s:", label.Name())
}
condStrs := make([]string, len(caseClauses))
for i, clause := range caseClauses {
conds := make([]string, len(clause.List))
for j, cond := range clause.List {
conds[j] = translateCond(cond).String()
}
condStrs[i] = strings.Join(conds, " || ")
if flatten {
c.Printf("/* */ if (%s) { $s = %d; continue; }", condStrs[i], caseOffset+i)
}
}
if flatten {
c.Printf("/* */ $s = %d; continue;", defaultCase)
}
prefix := ""
suffix := ""
if label != nil || hasBreak {
prefix = "switch (0) { default: "
suffix = " }"
}
for i, clause := range caseClauses {
c.SetPos(clause.Pos())
c.PrintCond(!flatten, fmt.Sprintf("%sif (%s) {", prefix, condStrs[i]), fmt.Sprintf("case %d:", caseOffset+i))
c.Indent(func() {
c.translateStmtList(clause.Body)
if flatten && (i < len(caseClauses)-1 || defaultClause != nil) && !endsWithReturn(clause.Body) {
c.Printf("$s = %d; continue;", endCase)
}
})
prefix = "} else "
}
if defaultClause != nil {
c.PrintCond(!flatten, prefix+"{", fmt.Sprintf("case %d:", caseOffset+len(caseClauses)))
c.Indent(func() {
c.translateStmtList(defaultClause.Body)
})
}
c.PrintCond(!flatten, "}"+suffix, fmt.Sprintf("case %d:", endCase))
}
示例4: translateStmt
func (c *funcContext) translateStmt(stmt ast.Stmt, label *types.Label) {
c.SetPos(stmt.Pos())
stmt = filter.IncDecStmt(stmt, c.p.Info.Info)
stmt = filter.Assign(stmt, c.p.Info.Info, c.p.Info.Pkg)
switch s := stmt.(type) {
case *ast.BlockStmt:
c.translateStmtList(s.List)
case *ast.IfStmt:
var caseClauses []*ast.CaseClause
ifStmt := s
for {
if ifStmt.Init != nil {
panic("simplification error")
}
caseClauses = append(caseClauses, &ast.CaseClause{List: []ast.Expr{ifStmt.Cond}, Body: ifStmt.Body.List})
elseStmt, ok := ifStmt.Else.(*ast.IfStmt)
if !ok {
break
}
ifStmt = elseStmt
}
var defaultClause *ast.CaseClause
if block, ok := ifStmt.Else.(*ast.BlockStmt); ok {
defaultClause = &ast.CaseClause{Body: block.List}
}
c.translateBranchingStmt(caseClauses, defaultClause, false, c.translateExpr, nil, c.Flattened[s])
case *ast.SwitchStmt:
if s.Init != nil || s.Tag != nil || len(s.Body.List) != 1 {
panic("simplification error")
}
clause := s.Body.List[0].(*ast.CaseClause)
if len(clause.List) != 0 {
panic("simplification error")
}
prevFlowData := c.flowDatas[nil]
data := &flowData{
postStmt: prevFlowData.postStmt, // for "continue" of outer loop
beginCase: prevFlowData.beginCase, // same
}
c.flowDatas[nil] = data
c.flowDatas[label] = data
defer func() {
delete(c.flowDatas, label)
c.flowDatas[nil] = prevFlowData
}()
if c.Flattened[s] {
data.endCase = c.caseCounter
c.caseCounter++
c.Indent(func() {
c.translateStmtList(clause.Body)
})
c.Printf("case %d:", data.endCase)
return
}
if label != nil || analysis.HasBreak(clause) {
if label != nil {
c.Printf("%s:", label.Name())
}
c.Printf("switch (0) { default:")
c.Indent(func() {
c.translateStmtList(clause.Body)
})
c.Printf("}")
return
}
c.translateStmtList(clause.Body)
case *ast.TypeSwitchStmt:
if s.Init != nil {
c.translateStmt(s.Init, nil)
}
refVar := c.newVariable("_ref")
var expr ast.Expr
switch a := s.Assign.(type) {
case *ast.AssignStmt:
expr = a.Rhs[0].(*ast.TypeAssertExpr).X
case *ast.ExprStmt:
expr = a.X.(*ast.TypeAssertExpr).X
}
c.Printf("%s = %s;", refVar, c.translateExpr(expr))
translateCond := func(cond ast.Expr) *expression {
if types.Identical(c.p.TypeOf(cond), types.Typ[types.UntypedNil]) {
return c.formatExpr("%s === $ifaceNil", refVar)
}
return c.formatExpr("$assertType(%s, %s, true)[1]", refVar, c.typeName(c.p.TypeOf(cond)))
}
var caseClauses []*ast.CaseClause
var defaultClause *ast.CaseClause
for _, cc := range s.Body.List {
clause := cc.(*ast.CaseClause)
var bodyPrefix []ast.Stmt
//.........这里部分代码省略.........
示例5: translateBranchingStmt
func (c *funcContext) translateBranchingStmt(caseClauses []ast.Stmt, isSwitch bool, translateCond func(ast.Expr) *expression, printCaseBodyPrefix func(int), label *types.Label, flatten bool) {
var branches []*branch
var defaultBranch *branch
var openBranches []*branch
clauseLoop:
for i, cc := range caseClauses {
clause := cc.(*ast.CaseClause)
branch := &branch{index: i, clause: clause}
openBranches = append(openBranches, branch)
for _, openBranch := range openBranches {
openBranch.body = append(openBranch.body, clause.Body...)
}
if !hasFallthrough(clause) {
openBranches = nil
}
if len(clause.List) == 0 {
defaultBranch = branch
continue
}
for _, cond := range clause.List {
if translateCond == nil {
if b, ok := analysis.BoolValue(cond, c.p.Info.Info); ok {
if b {
defaultBranch = branch
break clauseLoop
}
continue
}
}
branch.conds = append(branch.conds, cond)
}
if len(branch.conds) == 0 {
continue
}
branches = append(branches, branch)
}
for defaultBranch == nil && len(branches) != 0 && len(branches[len(branches)-1].body) == 0 && printCaseBodyPrefix == nil {
branches = branches[:len(branches)-1]
}
if len(branches) == 0 {
if defaultBranch != nil {
c.translateStmtList(defaultBranch.body)
return
}
return
}
hasBreak := false
if isSwitch {
switch label {
case nil:
for _, child := range caseClauses {
if analysis.HasBreak(child) {
hasBreak = true
break
}
}
default:
hasBreak = true // always assume break if label is given
}
}
var caseOffset, endCase int
if flatten {
caseOffset = c.caseCounter
endCase = caseOffset + len(branches)
if defaultBranch != nil {
endCase++
}
c.caseCounter = endCase + 1
}
if isSwitch {
prevFlowData := c.flowDatas[nil]
data := &flowData{
postStmt: prevFlowData.postStmt, // for "continue" of outer loop
beginCase: prevFlowData.beginCase, // same
endCase: endCase,
}
c.flowDatas[nil] = data
c.flowDatas[label] = data
defer func() {
delete(c.flowDatas, label)
c.flowDatas[nil] = prevFlowData
}()
}
if isSwitch && !flatten && label != nil {
c.Printf("%s:", label.Name())
}
prefix := ""
if hasBreak {
prefix = "switch (0) { default: "
}
//.........这里部分代码省略.........