本文整理汇总了Golang中golang.org/x/tools/go/types.Func.Type方法的典型用法代码示例。如果您正苦于以下问题:Golang Func.Type方法的具体用法?Golang Func.Type怎么用?Golang Func.Type使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类golang.org/x/tools/go/types.Func
的用法示例。
在下文中一共展示了Func.Type方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: genFunc
func (g *goGen) genFunc(o *types.Func) {
sig := o.Type().(*types.Signature)
params := "(" + g.tupleString(sig.Params()) + ")"
ret := g.tupleString(sig.Results())
if sig.Results().Len() > 1 {
ret = "(" + ret + ") "
} else {
ret += " "
}
//funcName := o.Name()
g.Printf(`
//export GoPy_%[1]s
// GoPy_%[1]s wraps %[2]s
func GoPy_%[1]s%[3]v%[4]v{
`,
o.Name(), o.FullName(),
params,
ret,
)
g.Indent()
g.genFuncBody(o)
g.Outdent()
g.Printf("}\n\n")
}
示例2: interfaceMethod
// interfaceMethod returns a function and receiver pointer for the specified
// interface and method pair.
func (fr *frame) interfaceMethod(lliface llvm.Value, ifacety types.Type, method *types.Func) (fn, recv *govalue) {
llitab := fr.builder.CreateExtractValue(lliface, 0, "")
recv = newValue(fr.builder.CreateExtractValue(lliface, 1, ""), types.Typ[types.UnsafePointer])
methodset := fr.types.MethodSet(ifacety)
// TODO(axw) cache ordered method index
index := -1
for i, m := range orderedMethodSet(methodset) {
if m.Obj() == method {
index = i
break
}
}
if index == -1 {
panic("could not find method index")
}
llitab = fr.builder.CreateBitCast(llitab, llvm.PointerType(llvm.PointerType(llvm.Int8Type(), 0), 0), "")
// Skip runtime type pointer.
llifnptr := fr.builder.CreateGEP(llitab, []llvm.Value{
llvm.ConstInt(llvm.Int32Type(), uint64(index+1), false),
}, "")
llifn := fr.builder.CreateLoad(llifnptr, "")
// Replace receiver type with unsafe.Pointer.
recvparam := types.NewParam(0, nil, "", types.Typ[types.UnsafePointer])
sig := method.Type().(*types.Signature)
sig = types.NewSignature(nil, recvparam, sig.Params(), sig.Results(), sig.Variadic())
fn = newValue(llifn, sig)
return
}
示例3: genFuncBody
func (g *goGen) genFuncBody(o *types.Func, selectorLHS string) {
sig := o.Type().(*types.Signature)
params := sig.Params()
for i := 0; i < params.Len(); i++ {
p := params.At(i)
t := seqType(p.Type())
if t == "Ref" {
name := p.Type().(*types.Named).Obj().Name()
g.Printf("var param_%s %s.%s\n", p.Name(), g.pkg.Name(), name)
g.Printf("param_%s_ref := in.ReadRef()\n", p.Name())
g.Printf("if param_%s_ref.Num < 0 {\n", p.Name())
g.Printf(" param_%s = param_%s_ref.Get().(%s.%s)\n", p.Name(), p.Name(), g.pkg.Name(), name)
g.Printf("} else {\n")
g.Printf(" param_%s = (*proxy%s)(param_%s_ref)\n", p.Name(), name, p.Name())
g.Printf("}\n")
} else {
g.Printf("param_%s := in.Read%s()\n", p.Name(), t)
}
}
res := sig.Results()
if res.Len() > 2 || res.Len() == 2 && !isErrorType(res.At(1).Type()) {
g.errorf("functions and methods must return either zero or one values, and optionally an error")
return
}
returnsValue := false
returnsError := false
if res.Len() == 1 {
if isErrorType(res.At(0).Type()) {
returnsError = true
g.Printf("err := ")
} else {
returnsValue = true
g.Printf("res := ")
}
} else if res.Len() == 2 {
returnsValue = true
returnsError = true
g.Printf("res, err := ")
}
g.Printf("%s.%s(", selectorLHS, o.Name())
for i := 0; i < params.Len(); i++ {
if i > 0 {
g.Printf(", ")
}
g.Printf("param_%s", params.At(i).Name())
}
g.Printf(")\n")
if returnsValue {
g.genWrite("res", "out", res.At(0).Type())
}
if returnsError {
g.genWrite("err", "out", res.At(res.Len()-1).Type())
}
}
示例4: printFunc
func (p *printer) printFunc(recvType types.Type, obj *types.Func) {
p.print("func ")
sig := obj.Type().(*types.Signature)
if recvType != nil {
p.print("(")
p.writeType(p.pkg, recvType)
p.print(") ")
}
p.print(obj.Name())
p.writeSignature(p.pkg, sig)
}
示例5: genFunc
func (g *javaGen) genFunc(o *types.Func, method bool) {
if err := g.funcSignature(o, !method); err != nil {
g.errorf("%v", err)
return
}
sig := o.Type().(*types.Signature)
res := sig.Results()
g.Printf(" {\n")
g.Indent()
g.Printf("go.Seq _in = new go.Seq();\n")
g.Printf("go.Seq _out = new go.Seq();\n")
returnsError := false
var resultType types.Type
if res.Len() > 0 {
if !isErrorType(res.At(0).Type()) {
resultType = res.At(0).Type()
}
if res.Len() > 1 || isErrorType(res.At(0).Type()) {
returnsError = true
}
}
if resultType != nil {
t := g.javaType(resultType)
g.Printf("%s _result;\n", t)
}
if method {
g.Printf("_in.writeRef(ref);\n")
}
params := sig.Params()
for i := 0; i < params.Len(); i++ {
p := params.At(i)
g.Printf("_in.write%s;\n", seqWrite(p.Type(), paramName(params, i)))
}
g.Printf("Seq.send(DESCRIPTOR, CALL_%s, _in, _out);\n", o.Name())
if resultType != nil {
g.genRead("_result", "_out", resultType)
}
if returnsError {
g.Printf(`String _err = _out.readString();
if (_err != null) {
throw new Exception(_err);
}
`)
}
if resultType != nil {
g.Printf("return _result;\n")
}
g.Outdent()
g.Printf("}\n\n")
}
示例6: checkFunction
// check if function is a test function for the testing package
// we don't unexport those
func (e *Export) checkFunction(from *types.Func, to string) {
if !strings.HasPrefix(from.Name(), "Test") {
return
}
sig := from.Type().(*types.Signature)
if sig.Params().Len() != 1 {
return
}
if sig.Params().At(0).Type().String() == "*testing.T" {
e.Conflicting = true
return
}
}
示例7: genFuncBody
func (g *goGen) genFuncBody(o *types.Func) {
sig := o.Type().(*types.Signature)
results := newVars(sig.Results())
for i := range results {
if i > 0 {
g.Printf(", ")
}
g.Printf("_gopy_%03d", i)
}
if len(results) > 0 {
g.Printf(" := ")
}
g.Printf("%s.%s(", g.pkg.Name(), o.Name())
args := sig.Params()
for i := 0; i < args.Len(); i++ {
arg := args.At(i)
tail := ""
if i+1 < args.Len() {
tail = ", "
}
g.Printf("%s%s", arg.Name(), tail)
}
g.Printf(")\n")
if len(results) <= 0 {
return
}
g.Printf("return ")
for i, res := range results {
if i > 0 {
g.Printf(", ")
}
// if needWrap(res.GoType()) {
// g.Printf("")
// }
if res.needWrap() {
g.Printf("%s(unsafe.Pointer(&", res.dtype.cgotype)
}
g.Printf("_gopy_%03d /* %#v %v */", i, res, res.GoType().Underlying())
if res.needWrap() {
g.Printf("))")
}
}
g.Printf("\n")
}
示例8: funcSignature
func (g *javaGen) funcSignature(o *types.Func, static bool) error {
sig := o.Type().(*types.Signature)
res := sig.Results()
var returnsError bool
var ret string
switch res.Len() {
case 2:
if !isErrorType(res.At(1).Type()) {
return fmt.Errorf("second result value must be of type error: %s", o)
}
returnsError = true
ret = g.javaType(res.At(0).Type())
case 1:
if isErrorType(res.At(0).Type()) {
returnsError = true
ret = "void"
} else {
ret = g.javaType(res.At(0).Type())
}
case 0:
ret = "void"
default:
return fmt.Errorf("too many result values: %s", o)
}
g.Printf("public ")
if static {
g.Printf("static ")
}
g.Printf("%s %s(", ret, o.Name())
params := sig.Params()
for i := 0; i < params.Len(); i++ {
if i > 0 {
g.Printf(", ")
}
v := sig.Params().At(i)
name := paramName(params, i)
jt := g.javaType(v.Type())
g.Printf("%s %s", jt, name)
}
g.Printf(")")
if returnsError {
g.Printf(" throws Exception")
}
return nil
}
示例9: genFuncBody
func (g *goGen) genFuncBody(o *types.Func, selectorLHS string) {
sig := o.Type().(*types.Signature)
params := sig.Params()
for i := 0; i < params.Len(); i++ {
p := params.At(i)
g.genRead("param_"+paramName(params, i), "in", p.Type())
}
res := sig.Results()
if res.Len() > 2 || res.Len() == 2 && !isErrorType(res.At(1).Type()) {
g.errorf("functions and methods must return either zero or one values, and optionally an error")
return
}
returnsValue := false
returnsError := false
if res.Len() == 1 {
if isErrorType(res.At(0).Type()) {
returnsError = true
g.Printf("err := ")
} else {
returnsValue = true
g.Printf("res := ")
}
} else if res.Len() == 2 {
returnsValue = true
returnsError = true
g.Printf("res, err := ")
}
g.Printf("%s.%s(", selectorLHS, o.Name())
for i := 0; i < params.Len(); i++ {
if i > 0 {
g.Printf(", ")
}
g.Printf("param_%s", paramName(params, i))
}
g.Printf(")\n")
if returnsValue {
g.genWrite("res", "out", res.At(0).Type())
}
if returnsError {
g.genWrite("err", "out", res.At(res.Len()-1).Type())
}
}
示例10: checkFuncValue
func checkFuncValue(t *testing.T, prog *ssa.Program, obj *types.Func) {
fn := prog.FuncValue(obj)
// fmt.Printf("FuncValue(%s) = %s\n", obj, fn) // debugging
if fn == nil {
if obj.Name() != "interfaceMethod" {
t.Errorf("FuncValue(%s) == nil", obj)
}
return
}
if fnobj := fn.Object(); fnobj != obj {
t.Errorf("FuncValue(%s).Object() == %s; value was %s",
obj, fnobj, fn.Name())
return
}
if !types.Identical(fn.Type(), obj.Type()) {
t.Errorf("FuncValue(%s).Type() == %s", obj, fn.Type())
return
}
}
示例11: makeBound
// makeBound returns a bound method wrapper (or "bound"), a synthetic
// function that delegates to a concrete or interface method denoted
// by obj. The resulting function has no receiver, but has one free
// variable which will be used as the method's receiver in the
// tail-call.
//
// Use MakeClosure with such a wrapper to construct a bound method
// closure. e.g.:
//
// type T int or: type T interface { meth() }
// func (t T) meth()
// var t T
// f := t.meth
// f() // calls t.meth()
//
// f is a closure of a synthetic wrapper defined as if by:
//
// f := func() { return t.meth() }
//
// Unlike makeWrapper, makeBound need perform no indirection or field
// selections because that can be done before the closure is
// constructed.
//
// EXCLUSIVE_LOCKS_ACQUIRED(meth.Prog.methodsMu)
//
func makeBound(prog *Program, obj *types.Func) *Function {
prog.methodsMu.Lock()
defer prog.methodsMu.Unlock()
fn, ok := prog.bounds[obj]
if !ok {
description := fmt.Sprintf("bound method wrapper for %s", obj)
if prog.mode&LogSource != 0 {
defer logStack("%s", description)()
}
fn = &Function{
name: obj.Name() + "$bound",
object: obj,
Signature: changeRecv(obj.Type().(*types.Signature), nil), // drop receiver
Synthetic: description,
Prog: prog,
pos: obj.Pos(),
}
fv := &FreeVar{name: "recv", typ: recvType(obj), parent: fn}
fn.FreeVars = []*FreeVar{fv}
fn.startBody()
createParams(fn, 0)
var c Call
if !isInterface(recvType(obj)) { // concrete
c.Call.Value = prog.declaredFunc(obj)
c.Call.Args = []Value{fv}
} else {
c.Call.Value = fv
c.Call.Method = obj
}
for _, arg := range fn.Params {
c.Call.Args = append(c.Call.Args, arg)
}
emitTailCall(fn, &c)
fn.finishBody()
prog.bounds[obj] = fn
}
return fn
}
示例12: tryMatchNewFunc
func (p *Processor) tryMatchNewFunc(models []*Model, fun *types.Func) {
modelName := fun.Name()[len("new"):]
for _, m := range models {
if m.Name != modelName {
continue
}
sig := fun.Type().(*types.Signature)
if sig.Recv() != nil {
continue
}
res := sig.Results()
for i := 0; i < res.Len(); i++ {
if isTypeOrPtrTo(res.At(i).Type(), m.CheckedNode) {
m.NewFunc = fun
return
}
}
}
}
示例13: recv
// recv returns the method's receiver.
func recv(meth *types.Func) *types.Var {
return meth.Type().(*types.Signature).Recv()
}
示例14: funcSummary
func (g *objcGen) funcSummary(obj *types.Func) *funcSummary {
s := &funcSummary{name: obj.Name()}
sig := obj.Type().(*types.Signature)
params := sig.Params()
for i := 0; i < params.Len(); i++ {
p := params.At(i)
v := paramInfo{
typ: p.Type(),
name: paramName(params, i),
}
s.params = append(s.params, v)
}
res := sig.Results()
switch res.Len() {
case 0:
s.ret = "void"
case 1:
p := res.At(0)
if isErrorType(p.Type()) {
s.retParams = append(s.retParams, paramInfo{
typ: p.Type(),
name: "error",
})
s.ret = "BOOL"
} else {
name := p.Name()
if name == "" || paramRE.MatchString(name) {
name = "ret0_"
}
typ := p.Type()
s.retParams = append(s.retParams, paramInfo{typ: typ, name: name})
s.ret = g.objcType(typ)
}
case 2:
name := res.At(0).Name()
if name == "" || paramRE.MatchString(name) {
name = "ret0_"
}
s.retParams = append(s.retParams, paramInfo{
typ: res.At(0).Type(),
name: name,
})
if !isErrorType(res.At(1).Type()) {
g.errorf("second result value must be of type error: %s", obj)
return nil
}
s.retParams = append(s.retParams, paramInfo{
typ: res.At(1).Type(),
name: "error", // TODO(hyangah): name collision check.
})
s.ret = "BOOL"
default:
// TODO(hyangah): relax the constraint on multiple return params.
g.errorf("too many result values: %s", obj)
return nil
}
return s
}
示例15: recvType
// recvType returns the receiver type of method obj.
func recvType(obj *types.Func) types.Type {
return obj.Type().(*types.Signature).Recv().Type()
}