本文整理汇总了Golang中golang.org/x/tools/go/types.Type.Underlying方法的典型用法代码示例。如果您正苦于以下问题:Golang Type.Underlying方法的具体用法?Golang Type.Underlying怎么用?Golang Type.Underlying使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类golang.org/x/tools/go/types.Type
的用法示例。
在下文中一共展示了Type.Underlying方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: lockPath
// lockPath returns a typePath describing the location of a lock value
// contained in typ. If there is no contained lock, it returns nil.
func lockPath(tpkg *types.Package, typ types.Type) typePath {
if typ == nil {
return nil
}
// We're only interested in the case in which the underlying
// type is a struct. (Interfaces and pointers are safe to copy.)
styp, ok := typ.Underlying().(*types.Struct)
if !ok {
return nil
}
// We're looking for cases in which a reference to this type
// can be locked, but a value cannot. This differentiates
// embedded interfaces from embedded values.
if plock := types.NewMethodSet(types.NewPointer(typ)).Lookup(tpkg, "Lock"); plock != nil {
if lock := types.NewMethodSet(typ).Lookup(tpkg, "Lock"); lock == nil {
return []types.Type{typ}
}
}
nfields := styp.NumFields()
for i := 0; i < nfields; i++ {
ftyp := styp.Field(i).Type()
subpath := lockPath(tpkg, ftyp)
if subpath != nil {
return append(subpath, typ)
}
}
return nil
}
示例2: zeroConst
// zeroConst returns a new "zero" constant of the specified type,
// which must not be an array or struct type: the zero values of
// aggregates are well-defined but cannot be represented by Const.
//
func zeroConst(t types.Type) *Const {
switch t := t.(type) {
case *types.Basic:
switch {
case t.Info()&types.IsBoolean != 0:
return NewConst(exact.MakeBool(false), t)
case t.Info()&types.IsNumeric != 0:
return NewConst(exact.MakeInt64(0), t)
case t.Info()&types.IsString != 0:
return NewConst(exact.MakeString(""), t)
case t.Kind() == types.UnsafePointer:
fallthrough
case t.Kind() == types.UntypedNil:
return nilConst(t)
default:
panic(fmt.Sprint("zeroConst for unexpected type:", t))
}
case *types.Pointer, *types.Slice, *types.Interface, *types.Chan, *types.Map, *types.Signature:
return nilConst(t)
case *types.Named:
return NewConst(zeroConst(t.Underlying()).Value, t)
case *types.Array, *types.Struct, *types.Tuple:
panic(fmt.Sprint("zeroConst applied to aggregate:", t))
}
panic(fmt.Sprint("zeroConst: unexpected ", t))
}
示例3: addInterfaceType
func (sym *symtab) addInterfaceType(pkg *types.Package, obj types.Object, t types.Type, kind symkind, id, n string) {
fn := sym.typename(t, nil)
typ := t.Underlying().(*types.Interface)
kind |= skInterface
// special handling of 'error'
if isErrorType(typ) {
return
}
sym.syms[fn] = &symbol{
gopkg: pkg,
goobj: obj,
gotyp: t,
kind: kind,
id: id,
goname: n,
cgoname: "cgo_type_" + id,
cpyname: "cpy_type_" + id,
pyfmt: "O&",
pybuf: "P",
pysig: "object",
c2py: "cgopy_cnv_c2py_" + id,
py2c: "cgopy_cnv_py2c_" + id,
pychk: fmt.Sprintf("cpy_func_%[1]s_check(%%s)", id),
}
}
示例4: dump
func dump(path string, typ types.Type, st reflect.StructTag) IObj {
named, _ := typ.(*types.Named)
if named != nil {
typ = typ.Underlying()
}
if strings.Split(st.Get("json"), ",")[0] == "" {
if _, ok := typ.(*types.Struct); !ok {
if _, ok := typ.(*types.Pointer); !ok {
return nil
}
}
}
switch u := typ.(type) {
case *types.Struct:
return Struct(path, st, u, named)
case *types.Map:
return Map(path, st, u, named)
case *types.Slice:
return Slice(path, st, u)
case *types.Pointer:
return Pointer(path, st, u)
case *types.Basic:
return Basic(path, st, u, named)
default:
panic("unsupported")
}
}
示例5: needWrapType
func needWrapType(typ types.Type) bool {
switch typ := typ.(type) {
case *types.Basic:
return false
case *types.Struct:
return true
case *types.Named:
switch ut := typ.Underlying().(type) {
case *types.Basic:
return false
default:
return needWrapType(ut)
}
case *types.Array:
return true
case *types.Map:
return true
case *types.Slice:
return true
case *types.Interface:
wrap := true
if typ.Underlying() == universe.syms["error"].GoType().Underlying() {
wrap = false
}
return wrap
case *types.Signature:
return true
case *types.Pointer:
return needWrapType(typ.Elem())
}
return false
}
示例6: makeInterface
func (fr *frame) makeInterface(llv llvm.Value, vty types.Type, iface types.Type) *govalue {
if _, ok := vty.Underlying().(*types.Pointer); !ok {
ptr := fr.createTypeMalloc(vty)
fr.builder.CreateStore(llv, ptr)
llv = ptr
}
return fr.makeInterfaceFromPointer(llv, vty, iface)
}
示例7: getInterfaceValue
// Reads the value from the given interface type, assuming that the
// interface holds a value of the correct type.
func (fr *frame) getInterfaceValue(v *govalue, ty types.Type) *govalue {
val := fr.builder.CreateExtractValue(v.value, 1, "")
if _, ok := ty.Underlying().(*types.Pointer); !ok {
typedval := fr.builder.CreateBitCast(val, llvm.PointerType(fr.types.ToLLVM(ty), 0), "")
val = fr.builder.CreateLoad(typedval, "")
}
return newValue(val, ty)
}
示例8: hash
func (x array) hash(t types.Type) int {
h := 0
tElt := t.Underlying().(*types.Array).Elem()
for _, xi := range x {
h += hash(tElt, xi)
}
return h
}
示例9: catchReferencedTypes
func catchReferencedTypes(et types.Type) {
id := LogTypeUse(et)
_, seen := catchReferencedTypesSeen[id]
if seen {
return
}
catchReferencedTypesSeen[id] = true
// check that we have all the required methods?
/*
for t := 1; t < NextTypeID; t++ { // make sure we do this in a consistent order
for _, k := range TypesEncountered.Keys() {
if TypesEncountered.At(k).(int) == t {
switch k.(type) {
case *types.Interface:
if types.Implements(et,k.(*types.Interface)) {
// TODO call missing method?
}
}
}
}
}
*/
//LogTypeUse(types.NewPointer(et))
switch et.(type) {
case *types.Named:
catchReferencedTypes(et.Underlying())
for m := 0; m < et.(*types.Named).NumMethods(); m++ {
catchReferencedTypes(et.(*types.Named).Method(m).Type())
}
case *types.Array:
catchReferencedTypes(et.(*types.Array).Elem())
//catchReferencedTypes(types.NewSlice(et.(*types.Array).Elem()))
case *types.Pointer:
catchReferencedTypes(et.(*types.Pointer).Elem())
case *types.Slice:
catchReferencedTypes(et.(*types.Slice).Elem())
case *types.Struct:
for f := 0; f < et.(*types.Struct).NumFields(); f++ {
if et.(*types.Struct).Field(f).IsField() {
catchReferencedTypes(et.(*types.Struct).Field(f).Type())
}
}
case *types.Map:
catchReferencedTypes(et.(*types.Map).Key())
catchReferencedTypes(et.(*types.Map).Elem())
case *types.Signature:
for i := 0; i < et.(*types.Signature).Params().Len(); i++ {
catchReferencedTypes(et.(*types.Signature).Params().At(i).Type())
}
for o := 0; o < et.(*types.Signature).Results().Len(); o++ {
catchReferencedTypes(et.(*types.Signature).Results().At(o).Type())
}
case *types.Chan:
catchReferencedTypes(et.(*types.Chan).Elem())
}
}
示例10: makeInterfaceFromPointer
func (fr *frame) makeInterfaceFromPointer(vptr llvm.Value, vty types.Type, iface types.Type) *govalue {
i8ptr := llvm.PointerType(llvm.Int8Type(), 0)
llv := fr.builder.CreateBitCast(vptr, i8ptr, "")
value := llvm.Undef(fr.types.ToLLVM(iface))
itab := fr.types.getItabPointer(vty, iface.Underlying().(*types.Interface))
value = fr.builder.CreateInsertValue(value, itab, 0, "")
value = fr.builder.CreateInsertValue(value, llv, 1, "")
return newValue(value, iface)
}
示例11: getInterfaceValueOrNull
// If cond is true, reads the value from the given interface type, otherwise
// returns a nil value.
func (fr *frame) getInterfaceValueOrNull(cond llvm.Value, v *govalue, ty types.Type) *govalue {
val := fr.builder.CreateExtractValue(v.value, 1, "")
if _, ok := ty.Underlying().(*types.Pointer); ok {
val = fr.builder.CreateSelect(cond, val, llvm.ConstNull(val.Type()), "")
} else {
val = fr.loadOrNull(cond, val, ty).value
}
return newValue(val, ty)
}
示例12: eq
func (x array) eq(t types.Type, _y interface{}) bool {
y := _y.(array)
tElt := t.Underlying().(*types.Array).Elem()
for i, xi := range x {
if !equals(tElt, xi, y[i]) {
return false
}
}
return true
}
示例13: usesBuiltinMap
// usesBuiltinMap returns true if the built-in hash function and
// equivalence relation for type t are consistent with those of the
// interpreter's representation of type t. Such types are: all basic
// types (bool, numbers, string), pointers and channels.
//
// usesBuiltinMap returns false for types that require a custom map
// implementation: interfaces, arrays and structs.
//
// Panic ensues if t is an invalid map key type: function, map or slice.
func usesBuiltinMap(t types.Type) bool {
switch t := t.(type) {
case *types.Basic, *types.Chan, *types.Pointer:
return true
case *types.Named:
return usesBuiltinMap(t.Underlying())
case *types.Interface, *types.Array, *types.Struct:
return false
}
panic(fmt.Sprintf("invalid map key type: %T", t))
}
示例14: TypeChainString
// TypeChainString returns the full type chain as a string.
func TypeChainString(t types.Type) string {
out := fmt.Sprintf("%s", t)
for {
if t == t.Underlying() {
break
} else {
t = t.Underlying()
}
out += fmt.Sprintf(" -> %s", t)
}
return out
}
示例15: interfaceTypeAssert
func (fr *frame) interfaceTypeAssert(val *govalue, ty types.Type) *govalue {
if _, ok := ty.Underlying().(*types.Interface); ok {
return fr.changeInterface(val, ty, true)
} else {
valtytd := fr.types.ToRuntime(val.Type())
valtd := fr.getInterfaceTypeDescriptor(val)
tytd := fr.types.ToRuntime(ty)
fr.runtime.checkInterfaceType.call(fr, valtd, tytd, valtytd)
return fr.getInterfaceValue(val, ty)
}
}