1
0
mirror of https://github.com/golang/go synced 2024-11-19 02:34:44 -07:00
go/ssa/util.go

120 lines
2.7 KiB
Go
Raw Normal View History

// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ssa
// This file defines a number of miscellaneous utility functions.
import (
"fmt"
"go/ast"
"io"
"os"
"code.google.com/p/go.tools/go/types"
)
func unreachable() {
panic("unreachable")
}
//// AST utilities
// unparen returns e with any enclosing parentheses stripped.
func unparen(e ast.Expr) ast.Expr {
for {
p, ok := e.(*ast.ParenExpr)
if !ok {
break
}
e = p.X
}
return e
}
// isBlankIdent returns true iff e is an Ident with name "_".
// They have no associated types.Object, and thus no type.
//
func isBlankIdent(e ast.Expr) bool {
id, ok := e.(*ast.Ident)
return ok && id.Name == "_"
}
//// Type utilities. Some of these belong in go/types.
// isPointer returns true for types whose underlying type is a pointer.
func isPointer(typ types.Type) bool {
_, ok := typ.Underlying().(*types.Pointer)
return ok
}
// deref returns a pointer's element type; otherwise it returns typ.
func deref(typ types.Type) types.Type {
if p, ok := typ.Underlying().(*types.Pointer); ok {
return p.Elem()
}
return typ
}
// DefaultType returns the default "typed" type for an "untyped" type;
// it returns the incoming type for all other types. The default type
// for untyped nil is untyped nil.
//
// Exported to exp/ssa/interp.
//
// TODO(gri): this is a copy of go/types.defaultType; export that function.
//
func DefaultType(typ types.Type) types.Type {
if t, ok := typ.(*types.Basic); ok {
k := t.Kind()
switch k {
case types.UntypedBool:
k = types.Bool
case types.UntypedInt:
k = types.Int
case types.UntypedRune:
k = types.Rune
case types.UntypedFloat:
k = types.Float64
case types.UntypedComplex:
k = types.Complex128
case types.UntypedString:
k = types.String
}
typ = types.Typ[k]
}
return typ
}
// logStack prints the formatted "start" message to stderr and
// returns a closure that prints the corresponding "end" message.
// Call using 'defer logStack(...)()' to show builder stack on panic.
// Don't forget trailing parens!
//
func logStack(format string, args ...interface{}) func() {
msg := fmt.Sprintf(format, args...)
io.WriteString(os.Stderr, msg)
io.WriteString(os.Stderr, "\n")
return func() {
io.WriteString(os.Stderr, msg)
io.WriteString(os.Stderr, " end\n")
}
}
go.tools/ssa: implement correct control flow for recovered panic. A function such as this: func one() (x int) { defer func() { recover() }() x = 1 panic("return") } that combines named return parameters (NRPs) with deferred calls that call recover, may return non-zero values despite the fact it doesn't even contain a return statement. (!) This requires a change to the SSA API: all functions' control-flow graphs now have a second entry point, called Recover, which is the block at which control flow resumes after a recovered panic. The Recover block simply loads the NRPs and returns them. As an optimization, most functions don't need a Recover block, so it is omitted. In fact it is only needed for functions that have NRPs and defer a call to another function that _may_ call recover. Dataflow analysis of SSA now requires extra work, since every may-panic instruction has an implicit control-flow edge to the Recover block. The only dataflow analysis so far implemented is SSA renaming, for which we make the following simplifying assumption: the Recover block only loads the NRPs and returns. This means we don't really need to analyze it, we can just skip the "lifting" of such NRPs. We also special-case the Recover block in the dominance computation. Rejected alternative approaches: - Specifying a Recover block for every defer instruction (like a traditional exception handler). This seemed like excessive generality, since Go programs only need the same degenerate form of Recover block. - Adding an instruction to set the Recover block immediately after the named return values are set up, so that dominance can be computed without special-casing. This didn't seem worth the effort. Interpreter: - This CL completely reimplements the panic/recover/ defer logic in the interpreter. It's clearer and simpler and closer to the model in the spec. - Some runtime panic messages have been changed to be closer to gc's, since tests depend on it. - The interpreter now requires that the runtime.runtimeError type be part of the SSA program. This requires that clients import this package prior to invoking the interpreter. This in turn requires (Importer).ImportPackage(path string), which this CL adds. - All $GOROOT/test/recover{,1,2,3}.go tests are now passing. NB, the bug described in coverage.go (defer/recover in a concatenated init function) remains. Will be fixed in a follow-up. Fixes golang/go#6381 R=gri CC=crawshaw, golang-dev https://golang.org/cl/13844043
2013-10-14 13:38:56 -06:00
// callsRecover reports whether f contains a direct call to recover().
func callsRecover(f *Function) bool {
for _, b := range f.Blocks {
for _, instr := range b.Instrs {
if call, ok := instr.(*Call); ok {
if blt, ok := call.Call.Value.(*Builtin); ok {
if blt.Name() == "recover" {
return true
}
}
}
}
}
return false
}