本文整理匯總了Golang中github.com/mewkiz/pkg/errutil.Newf函數的典型用法代碼示例。如果您正苦於以下問題:Golang Newf函數的具體用法?Golang Newf怎麽用?Golang Newf使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Newf函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: expand
// expand attempts to locate and return the definition of the provided
// identifier expression. The assignment statement defining the identifier is
// removed from the statement list of the basic block.
func expand(bb BasicBlock, ident ast.Expr) (ast.Expr, error) {
id, ok := ident.(*ast.Ident)
if !ok {
return nil, errutil.Newf("unable to expand expression; expected identifier, got %T", ident)
}
var stmts []ast.Stmt
var expr ast.Expr
for _, stmt := range bb.Stmts() {
switch stmt := stmt.(type) {
case *ast.AssignStmt:
if sameIdent(stmt.Lhs, id) {
if len(stmt.Rhs) != 1 {
return nil, errutil.Newf("invalid right-hand side; expected length 1, got %d", len(stmt.Rhs))
}
expr = stmt.Rhs[0]
// TODO: Verify if the identifier is used by any other statement or
// terminator instruction before removing it; this includes the use
// within other basic blocks.
// Remove the variable definition of the right-hand side expression
// by not appending the assignment statement to the statement list.
continue
}
}
stmts = append(stmts, stmt)
}
if expr == nil {
return nil, errutil.Newf("unable to expand definition of identifier %q", id.Name)
}
bb.SetStmts(stmts)
return expr, nil
}
示例2: NewGetElementPtrInst
// NewGetElementPtrInst returns a new getelementptr instruction based on the
// given element type, address and element indices.
func NewGetElementPtrInst(elem, srcAddrType, srcAddr, elemIndices interface{}) (*instruction.GetElementPtr, error) {
if elem, ok := elem.(types.Type); ok {
srcAddr, err := NewValue(srcAddrType, srcAddr)
if err != nil {
return nil, errutil.Err(err)
}
var indices []value.Value
switch elemIndices := elemIndices.(type) {
case []value.Value:
indices = elemIndices
case nil:
default:
panic(fmt.Sprintf("support for element indices type %T not yet implemented", elemIndices))
}
// Validate that elem is of identical type as the element type of srcAddr.
srcAddrType, ok := srcAddr.Type().(*types.Pointer)
if !ok {
return nil, errutil.Newf("invalid source address pointer type; expected *types.Pointer, got %T", srcAddr.Type())
}
if !types.Equal(elem, srcAddrType.Elem()) {
return nil, errutil.Newf("type mismatch between element type (%v) and source address element type (%v)", elem, srcAddrType.Elem())
}
return instruction.NewGetElementPtr(srcAddr, indices)
}
return nil, errutil.Newf("invalid operand type; expected types.Type, got %T", elem)
}
示例3: NewBasicBlock
// NewBasicBlock returns a new basic block based on the given name, non-
// terminating instructions and terminator.
func NewBasicBlock(nameToken, insts, term interface{}) (*ir.BasicBlock, error) {
// Get label name.
var name string
switch nameToken := nameToken.(type) {
case *token.Token:
// Strip ":" suffix.
s, err := stripLabelSuffix(nameToken.Lit)
if err != nil {
return nil, errutil.Err(err)
}
name = s
case nil:
// Unnamed basic block.
default:
return nil, errutil.Newf("invalid basic block name type; expected *token.Token, got %T", nameToken)
}
if term, ok := term.(instruction.Terminator); ok {
switch insts := insts.(type) {
case []instruction.Instruction:
block := ir.NewBasicBlock(name)
block.SetInsts(insts)
block.SetTerm(term)
return block, nil
case nil:
block := ir.NewBasicBlock(name)
block.SetTerm(term)
return block, nil
default:
return nil, errutil.Newf("invalid non-terminating instructions type; expected []instruction.Instruction, got %T", insts)
}
}
return nil, errutil.Newf("invalid terminator type; expected instruction.Terminator, got %T", term)
}
示例4: NewSubGraph
// NewSubGraph returns a new subgraph based on graph with a dedicated entry and
// exit node. The entry and exit nodes are identified using the node "label"
// attribute, e.g.
//
// digraph if {
// A->B [label="true"]
// A->C [label="false"]
// B->C
// A [label="entry"]
// B
// C [label="exit"]
// }
func NewSubGraph(graph *dot.Graph) (*SubGraph, error) {
sub := &SubGraph{Graph: graph}
// Locate entry and exit nodes.
var hasEntry, hasExit bool
for _, node := range graph.Nodes.Nodes {
label, ok := node.Attrs["label"]
if !ok {
continue
}
switch label {
case "entry":
if hasEntry {
return nil, errutil.Newf(`redefinition of node with "entry" label; previous node %q, new node %q`, sub.entry, node.Name)
}
sub.entry = node.Name
hasEntry = true
case "exit":
if hasExit {
return nil, errutil.Newf(`redefinition of node with "exit" label; previous node %q, new node %q`, sub.exit, node.Name)
}
sub.exit = node.Name
hasExit = true
}
}
if !hasEntry {
return nil, errutil.New(`unable to locate node with "entry" label`)
}
if !hasExit {
return nil, errutil.New(`unable to locate node with "exit" label`)
}
return sub, nil
}
示例5: candidates
// candidates locates node pair candidates for an isomorphism of sub in graph
// which starts at the entry node.
func candidates(graph *dot.Graph, entry string, sub *graphs.SubGraph) (*equation, error) {
// Sanity checks.
g, ok := graph.Nodes.Lookup[entry]
if !ok {
return nil, errutil.Newf("unable to locate entry node %q in graph", entry)
}
s, ok := sub.Nodes.Lookup[sub.Entry()]
if !ok {
panic(fmt.Sprintf("unable to locate entry node %q in sub", sub.Entry()))
}
if !isPotential(g, s, sub) {
return nil, errutil.Newf("invalid entry node candidate %q; expected %d successors, got %d", g.Name, len(s.Succs), len(g.Succs))
}
// Locate candidate node pairs.
eq := &equation{
c: make(map[string]map[string]bool),
m: make(map[string]string),
}
eq.findCandidates(g, s, sub)
if len(eq.c) != len(sub.Nodes.Nodes) {
return nil, errutil.Newf("incomplete candidate mapping; expected %d map entites, got %d", len(sub.Nodes.Nodes), len(eq.c))
}
return eq, nil
}
示例6: NewArrayType
// NewArrayType returns a new array type based on the given element type and
// length.
func NewArrayType(elem, lbracket, length, rbracket interface{}) (*ast.ArrayType, error) {
len, ok := length.(int)
if !ok {
return nil, errutil.Newf("invalid array length type; %T", length)
}
var lbrack, rbrack int
switch lbracket := lbracket.(type) {
case *gocctoken.Token:
lbrack = lbracket.Offset
case int:
lbrack = lbracket
default:
return nil, errutil.Newf("invalid left-bracket type; expectd *gocctoken.Token or int, got %T", lbracket)
}
switch rbracket := rbracket.(type) {
case *gocctoken.Token:
rbrack = rbracket.Offset
case int:
rbrack = rbracket
default:
return nil, errutil.Newf("invalid right-bracket type; expectd *gocctoken.Token or int, got %T", rbracket)
}
elemType, err := NewType(elem)
if err != nil {
return nil, errutil.Newf("invalid array element type; %v", err)
}
return &ast.ArrayType{Elem: elemType, Lbracket: lbrack, Len: len, Rbracket: rbrack}, nil
}
示例7: createListPrim
// createListPrim creates a list primitive containing a slice of Go statements
// based on the identified subgraph, its node pair mapping and its basic blocks.
// The new control flow primitive conceptually represents a basic block with the
// given name.
//
// Contents of "list.dot":
//
// digraph list {
// entry [label="entry"]
// exit [label="exit"]
// entry->exit
// }
func createListPrim(m map[string]string, bbs map[string]BasicBlock, newName string) (*primitive, error) {
// Locate graph nodes.
nameEntry, ok := m["entry"]
if !ok {
return nil, errutil.New(`unable to locate node pair for sub node "entry"`)
}
nameExit, ok := m["exit"]
if !ok {
return nil, errutil.New(`unable to locate node pair for sub node "exit"`)
}
bbEntry, ok := bbs[nameEntry]
if !ok {
return nil, errutil.Newf("unable to locate basic block %q", nameEntry)
}
bbExit, ok := bbs[nameExit]
if !ok {
return nil, errutil.Newf("unable to locate basic block %q", nameExit)
}
// Create and return new primitive.
//
// entry
// exit
stmts := append(bbEntry.Stmts(), bbExit.Stmts()...)
prim := &primitive{
name: newName,
stmts: stmts,
term: bbExit.Term(),
}
return prim, nil
}
示例8: getBBName
// getBBName returns the name (or ID if unnamed) of a basic block.
func getBBName(v llvm.Value) (string, error) {
if !v.IsBasicBlock() {
return "", errutil.Newf("invalid value type; expected basic block, got %v", v.Type())
}
// Locate the name of a named basic block.
if name := v.Name(); len(name) > 0 {
return name, nil
}
// Locate the ID of an unnamed basic block by parsing the value dump in
// search for its basic block label.
//
// Example value dump:
// 0:
// br i1 true, label %1, label %2
//
// Each basic block is expected to have a label, which requires the
// "unnamed.patch" to be applied to the llvm.org/llvm/bindings/go/llvm code
// base.
s, err := hackDump(v)
if err != nil {
return "", errutil.Err(err)
}
tokens := lexer.ParseString(s)
if len(tokens) < 1 {
return "", errutil.Newf("unable to locate basic block label in %q", s)
}
tok := tokens[0]
if tok.Kind != token.Label {
return "", errutil.Newf("invalid token; expected %v, got %v", token.Label, tok.Kind)
}
return tok.Val, nil
}
示例9: NewFunc
// NewFunc returns a new function based on the given result type, function name,
// and function parameters.
func NewFunc(result, gname, params interface{}) (*ir.Function, error) {
if result, ok := result.(types.Type); ok {
name, err := getGlobalName(gname)
if err != nil {
return nil, errutil.Err(err)
}
var sig *types.Func
switch params := params.(type) {
case *Params:
sig, err = types.NewFunc(result, params.params, params.variadic)
if err != nil {
return nil, errutil.Err(err)
}
case nil:
sig, err = types.NewFunc(result, nil, false)
if err != nil {
return nil, errutil.Err(err)
}
default:
return nil, errutil.Newf("invalid function parameters specifier type; expected *Params, got %T", params)
}
return ir.NewFunction(name, sig), nil
}
return nil, errutil.Newf("invalid function result type; expected types.Type, got %T", result)
}
示例10: getCmpPred
// getCmpPred parses the provided comparison instruction and returns a Go token
// equivalent of the comparison predicate.
//
// Syntax:
// <result> = icmp <pred> <type> <op1>, <op2>
func getCmpPred(inst llvm.Value) (token.Token, error) {
// Parse and validate tokens.
tokens, err := getTokens(inst)
if err != nil {
return 0, errutil.Err(err)
}
if len(tokens) < 4 {
return 0, errutil.Newf("unable to parse comparison instruction; expected >= 4 tokens, got %d", len(tokens))
}
// TODO: Handle signed and unsigned predicates separately.
switch pred := tokens[3]; pred.Kind {
// Int predicates.
case lltoken.KwEq: // eq: equal
return token.EQL, nil // ==
case lltoken.KwNe: // ne: not equal
return token.NEQ, nil // !=
case lltoken.KwUgt: // ugt: unsigned greater than
return token.GTR, nil // >
case lltoken.KwUge: // uge: unsigned greater or equal
return token.GEQ, nil // >=
case lltoken.KwUlt: // ult: unsigned less than
return token.LSS, nil // <
case lltoken.KwUle: // ule: unsigned less or equal
return token.LEQ, nil // <=
case lltoken.KwSgt: // sgt: signed greater than
return token.GTR, nil // >
case lltoken.KwSge: // sge: signed greater or equal
return token.GEQ, nil // >=
case lltoken.KwSlt: // slt: signed less than
return token.LSS, nil // <
case lltoken.KwSle: // sle: signed less or equal
return token.LEQ, nil // <=
// Float predicates.
case lltoken.KwOeq: // oeq: ordered and equal
return token.EQL, nil // ==
case lltoken.KwOgt: // ogt: ordered and greater than
return token.GTR, nil // >
case lltoken.KwOge: // oge: ordered and greater than or equal
return token.GEQ, nil // >=
case lltoken.KwOlt: // olt: ordered and less than
return token.LSS, nil // <
case lltoken.KwOle: // ole: ordered and less than or equal
return token.LEQ, nil // <=
case lltoken.KwOne: // one: ordered and not equal
return token.NEQ, nil // !=
case lltoken.KwOrd: // ord: ordered (no nans)
return 0, errutil.Newf(`support for the floating point comparison predicate "ord" not yet implemented`)
case lltoken.KwUeq: // ueq: unordered or equal
return token.EQL, nil // ==
case lltoken.KwUne: // une: unordered or not equal
return token.NEQ, nil // !=
case lltoken.KwUno: // uno: unordered (either nans)
return 0, errutil.Newf(`support for the floating point comparison predicate "uno" not yet implemented`)
default:
return 0, errutil.Newf("invalid token; expected comparison predicate, got %q", pred)
}
}
示例11: AppendIncoming
// AppendIncoming appends inc to the incoming values list.
func AppendIncoming(list, inc interface{}) ([]*Incoming, error) {
if list, ok := list.([]*Incoming); ok {
if inc, ok := inc.(*Incoming); ok {
return append(list, inc), nil
}
return nil, errutil.Newf("invalid incoming values list incoming value type; expected *Incoming, got %T", inc)
}
return nil, errutil.Newf("invalid incoming values list type; expected []*Incoming, got %T", list)
}
示例12: AppendValue
// AppendValue appends val to the value list.
func AppendValue(list, val interface{}) ([]value.Value, error) {
if list, ok := list.([]value.Value); ok {
if val, ok := val.(value.Value); ok {
return append(list, val), nil
}
return nil, errutil.Newf("invalid value list value type; expected value.Value, got %T", val)
}
return nil, errutil.Newf("invalid value list type; expected []value.Value, got %T", list)
}
示例13: AppendParam
// AppendParam appends param to the function parameter list.
func AppendParam(list, param interface{}) ([]*types.Param, error) {
if list, ok := list.([]*types.Param); ok {
if param, ok := param.(*types.Param); ok {
return append(list, param), nil
}
return nil, errutil.Newf("invalid function parameter list parameter type; expected *types.Param, got %T", param)
}
return nil, errutil.Newf("invalid function parameter list type; expected []*types.Param, got %T", list)
}
示例14: parseFunc
// parseFunc parses the given function and attempts to construct an equivalent
// Go function declaration AST node.
func parseFunc(graph *dot.Graph, module llvm.Module, funcName string, hprims []*xprimitive.Primitive) (*ast.FuncDecl, error) {
llFunc := module.NamedFunction(funcName)
if llFunc.IsNil() {
return nil, errutil.Newf("unable to locate function %q", funcName)
}
if llFunc.IsDeclaration() {
return nil, errutil.Newf("unable to create AST for %q; expected function definition, got function declaration (e.g. no body)", funcName)
}
// Parse each basic block.
bbs := make(map[string]BasicBlock)
for _, llBB := range llFunc.BasicBlocks() {
bb, err := parseBasicBlock(llBB)
if err != nil {
return nil, err
}
bbs[bb.Name()] = bb
if flagVerbose && !flagQuiet {
printBB(bb)
}
}
// Replace PHI instructions with assignment statements in the appropriate
// basic blocks.
for _, bb := range bbs {
block, ok := bb.(*basicBlock)
if !ok {
return nil, errutil.Newf("invalid basic block type; expected *basicBlock, got %T", bb)
}
for ident, defs := range block.phis {
for _, def := range defs {
assign := &ast.AssignStmt{
Lhs: []ast.Expr{newIdent(ident)},
Tok: token.ASSIGN,
Rhs: []ast.Expr{def.expr},
}
bbSrc := bbs[def.bb]
stmts := bbSrc.Stmts()
stmts = append(stmts, assign)
bbSrc.SetStmts(stmts)
}
}
}
// Perform control flow analysis.
body, err := restructure(graph, bbs, hprims)
if err != nil {
return nil, errutil.Err(err)
}
sig := &ast.FuncType{
Params: &ast.FieldList{},
}
if funcName != "main" {
// TODO: Implement parsing of function signature.
}
return createFunc(funcName, sig, body)
}
示例15: AppendTopLevelDecl
// AppendTopLevelDecl appends decl to the top-level declaration list.
func AppendTopLevelDecl(list, decl interface{}) ([]TopLevelDecl, error) {
if list, ok := list.([]TopLevelDecl); ok {
if decl, ok := decl.(TopLevelDecl); ok {
return append(list, decl), nil
}
return nil, errutil.Newf("invalid top-level declaration list top-level declaration type; expected TopLevelDecl, got %T", decl)
}
return nil, errutil.Newf("invalid top-level declaration list type; expected []TopLevelDecl, got %T", list)
}