本文整理汇总了Golang中go/types.Var.Type方法的典型用法代码示例。如果您正苦于以下问题:Golang Var.Type方法的具体用法?Golang Var.Type怎么用?Golang Var.Type使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类go/types.Var
的用法示例。
在下文中一共展示了Var.Type方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: genVar
func (g *goGen) genVar(o *types.Var) {
if t := o.Type(); !g.isSupported(t) {
g.Printf("// skipped variable %s with unsupported type %T\n\n", o.Name(), t)
return
}
// TODO(hyangah): non-struct pointer types (*int), struct type.
v := fmt.Sprintf("%s%s", g.pkgName(g.Pkg), o.Name())
// var I int
//
// func var_setI(v int)
g.Printf("//export var_set%s_%s\n", g.pkgPrefix, o.Name())
g.Printf("func var_set%s_%s(v C.%s) {\n", g.pkgPrefix, o.Name(), g.cgoType(o.Type()))
g.Indent()
g.genRead("_v", "v", o.Type(), modeRetained)
g.Printf("%s = _v\n", v)
g.Outdent()
g.Printf("}\n")
// func var_getI() int
g.Printf("//export var_get%s_%s\n", g.pkgPrefix, o.Name())
g.Printf("func var_get%s_%s() C.%s {\n", g.pkgPrefix, o.Name(), g.cgoType(o.Type()))
g.Indent()
g.Printf("v := %s\n", v)
g.genWrite("_v", "v", o.Type(), modeRetained)
g.Printf("return _v\n")
g.Outdent()
g.Printf("}\n")
}
示例2: field
func (p *exporter) field(f *types.Var) {
if !f.IsField() {
log.Fatalf("gcimporter: field expected")
}
p.pos(f)
p.fieldName(f)
p.typ(f.Type())
}
示例3: addVariable
func (b *Builder) addVariable(u types.Universe, useName *types.Name, in *tc.Var) *types.Type {
name := tcVarNameToName(in.String())
if useName != nil {
name = *useName
}
out := u.Variable(name)
out.Kind = types.DeclarationOf
out.Underlying = b.walkType(u, nil, in.Type())
return out
}
示例4: field
func (p *exporter) field(f *types.Var) {
// anonymous fields have "" name
name := ""
if !f.Anonymous() {
name = f.Name()
}
// qualifiedName will always emit the field package for
// anonymous fields because "" is not an exported name.
p.qualifiedName(f.Pkg(), name)
p.typ(f.Type())
}
示例5: genVar
func (g *javaGen) genVar(o *types.Var) {
if t := o.Type(); !g.isSupported(t) {
g.Printf("// skipped variable %s with unsupported type: %T\n\n", o.Name(), t)
return
}
jType := g.javaType(o.Type())
// setter
g.Printf("public static native void set%s(%s v);\n", o.Name(), jType)
// getter
g.Printf("public static native %s get%s();\n\n", jType, o.Name())
}
示例6: matchWildcard
func (tr *Transformer) matchWildcard(xobj *types.Var, y ast.Expr) bool {
name := xobj.Name()
if tr.verbose {
fmt.Fprintf(os.Stderr, "%s: wildcard %s -> %s?: ",
tr.fset.Position(y.Pos()), name, astString(tr.fset, y))
}
// Check that y is assignable to the declared type of the param.
yt := tr.info.TypeOf(y)
if yt == nil {
// y has no type.
// Perhaps it is an *ast.Ellipsis in [...]T{}, or
// an *ast.KeyValueExpr in T{k: v}.
// Clearly these pseudo-expressions cannot match a
// wildcard, but it would nice if we had a way to ignore
// the difference between T{v} and T{k:v} for structs.
return false
}
if !types.AssignableTo(yt, xobj.Type()) {
if tr.verbose {
fmt.Fprintf(os.Stderr, "%s not assignable to %s\n", yt, xobj.Type())
}
return false
}
// A wildcard matches any expression.
// If it appears multiple times in the pattern, it must match
// the same expression each time.
if old, ok := tr.env[name]; ok {
// found existing binding
tr.allowWildcards = false
r := tr.matchExpr(old, y)
if tr.verbose {
fmt.Fprintf(os.Stderr, "%t secondary match, primary was %s\n",
r, astString(tr.fset, old))
}
tr.allowWildcards = true
return r
}
if tr.verbose {
fmt.Fprintf(os.Stderr, "primary match\n")
}
tr.env[name] = y // record binding
return true
}
示例7: convertVar
func (c *converter) convertVar(v *gotypes.Var) *types.Var {
if v == nil {
return nil
}
if v, ok := c.converted[v]; ok {
return v.(*types.Var)
}
ret := types.NewVar(
token.Pos(v.Pos()),
c.ret,
v.Name(),
c.convertType(v.Type()),
)
c.converted[v] = ret
return ret
}
示例8: genGetter
func (g *objcGen) genGetter(desc string, f *types.Var) {
t := f.Type()
if isErrorType(t) {
t = types.Typ[types.String]
}
s := &funcSummary{
name: lowerFirst(f.Name()),
ret: g.objcType(t),
retParams: []paramInfo{{typ: t, name: "ret_"}},
}
g.Printf("- %s {\n", s.asMethod(g))
g.Indent()
g.genFunc(desc+"_DESCRIPTOR_", desc+"_FIELD_"+f.Name()+"_GET_", s, true)
g.Outdent()
g.Printf("}\n\n")
}
示例9: genSetter
func (g *objcGen) genSetter(desc string, f *types.Var) {
t := f.Type()
if isErrorType(t) {
t = types.Typ[types.String]
}
s := &funcSummary{
name: "set" + f.Name(),
ret: "void",
params: []paramInfo{{typ: t, name: "v"}},
}
g.Printf("- %s {\n", s.asMethod(g))
g.Indent()
g.genFunc(desc+"_DESCRIPTOR_", desc+"_FIELD_"+f.Name()+"_SET_", s, true)
g.Outdent()
g.Printf("}\n\n")
}
示例10: fieldName
// fieldName is like qualifiedName but it doesn't record the package
// for blank (_) or exported names.
func (p *exporter) fieldName(f *types.Var) {
name := f.Name()
// anonymous field with unexported base type name: use "?" as field name
// (bname != "" per spec, but we are conservative in case of errors)
if f.Anonymous() {
base := f.Type()
if ptr, ok := base.(*types.Pointer); ok {
base = ptr.Elem()
}
if named, ok := base.(*types.Named); ok && !named.Obj().Exported() {
name = "?"
}
}
p.string(name)
if name == "?" || name != "_" && !f.Exported() {
p.pkg(f.Pkg(), false)
}
}
示例11: genVarM
func (g *objcGen) genVarM(o *types.Var) {
varDesc := fmt.Sprintf("%q", g.pkg.Name()+"."+o.Name())
objcType := g.objcType(o.Type())
// setter
s1 := &funcSummary{
name: "set" + o.Name(),
ret: "void",
params: []paramInfo{{typ: o.Type(), name: "v"}},
}
g.Printf("+ (void) %s:(%s)v {\n", s1.name, objcType)
g.Indent()
g.genFunc(varDesc, "1", s1, false) // false: not instance method.
g.Outdent()
g.Printf("}\n\n")
// getter
s2 := &funcSummary{
name: lowerFirst(o.Name()),
ret: objcType,
retParams: []paramInfo{{typ: o.Type(), name: "ret"}},
}
g.Printf("+ (%s) %s {\n", s2.ret, s2.name)
g.Indent()
g.genFunc(varDesc, "2", s2, false)
g.Outdent()
g.Printf("}\n\n")
}
示例12: genVarM
func (g *objcGen) genVarM(o *types.Var) {
varDesc := fmt.Sprintf("%q", g.pkg.Name()+"."+o.Name())
objcType := g.objcType(o.Type())
// setter
s1 := &funcSummary{
name: g.namePrefix + "_set" + o.Name(),
ret: "void",
params: []paramInfo{{typ: o.Type(), name: "v"}},
}
g.Printf("void %s(%s v) {\n", s1.name, objcType)
g.Indent()
g.genFunc(varDesc, "1", s1, false)
g.Outdent()
g.Printf("}\n\n")
// getter
s2 := &funcSummary{
name: g.namePrefix + o.Name(),
ret: objcType,
retParams: []paramInfo{{typ: o.Type(), name: "ret"}},
}
g.Printf("%s %s() {\n", s2.ret, s2.name)
g.Indent()
g.genFunc(varDesc, "2", s2, false)
g.Outdent()
g.Printf("}\n\n")
}
示例13: genVar
func (g *javaGen) genVar(o *types.Var) {
jType := g.javaType(o.Type())
varDesc := fmt.Sprintf("%s.%s", g.pkg.Name(), o.Name())
// setter
g.Printf("public static void set%s(%s v) {\n", o.Name(), jType)
g.Indent()
g.Printf("Seq in = new Seq();\n")
g.Printf("Seq out = new Seq();\n")
g.Printf("in.write%s;\n", seqWrite(o.Type(), "v"))
g.Printf("Seq.send(%q, 1, in, out);\n", varDesc)
g.Outdent()
g.Printf("}\n")
g.Printf("\n")
// getter
g.Printf("public static %s get%s() {\n", jType, o.Name())
g.Indent()
g.Printf("Seq in = new Seq();\n")
g.Printf("Seq out = new Seq();\n")
g.Printf("Seq.send(%q, 2, in, out);\n", varDesc)
g.Printf("%s ", jType)
g.genRead("v", "out", o.Type())
g.Printf("return v;\n")
g.Outdent()
g.Printf("}\n")
g.Printf("\n")
}
示例14: genVar
func (g *goGen) genVar(o *types.Var) {
// TODO(hyangah): non-struct pointer types (*int), struct type.
v := fmt.Sprintf("%s.%s", g.pkg.Name(), o.Name())
// var I int
//
// func var_setI(out, in *seq.Buffer)
g.Printf("func var_set%s(out, in *seq.Buffer) {\n", o.Name())
g.Indent()
g.genRead("v", "in", o.Type())
g.Printf("%s = v\n", v)
g.Outdent()
g.Printf("}\n")
// func var_getI(out, in *seq.Buffer)
g.Printf("func var_get%s(out, in *seq.Buffer) {\n", o.Name())
g.Indent()
g.genWrite(v, "out", o.Type())
g.Outdent()
g.Printf("}\n")
}
示例15: checkVarValue
func checkVarValue(t *testing.T, prog *ssa.Program, pkg *ssa.Package, ref []ast.Node, obj *types.Var, expKind string, wantAddr bool) {
// The prefix of all assertions messages.
prefix := fmt.Sprintf("VarValue(%s @ L%d)",
obj, prog.Fset.Position(ref[0].Pos()).Line)
v, gotAddr := prog.VarValue(obj, pkg, ref)
// Kind is the concrete type of the ssa Value.
gotKind := "nil"
if v != nil {
gotKind = fmt.Sprintf("%T", v)[len("*ssa."):]
}
// fmt.Printf("%s = %v (kind %q; expect %q) wantAddr=%t gotAddr=%t\n", prefix, v, gotKind, expKind, wantAddr, gotAddr) // debugging
// Check the kinds match.
// "nil" indicates expected failure (e.g. optimized away).
if expKind != gotKind {
t.Errorf("%s concrete type == %s, want %s", prefix, gotKind, expKind)
}
// Check the types match.
// If wantAddr, the expected type is the object's address.
if v != nil {
expType := obj.Type()
if wantAddr {
expType = types.NewPointer(expType)
if !gotAddr {
t.Errorf("%s: got value, want address", prefix)
}
} else if gotAddr {
t.Errorf("%s: got address, want value", prefix)
}
if !types.Identical(v.Type(), expType) {
t.Errorf("%s.Type() == %s, want %s", prefix, v.Type(), expType)
}
}
}