本文整理汇总了Golang中go/types.Type.Underlying方法的典型用法代码示例。如果您正苦于以下问题:Golang Type.Underlying方法的具体用法?Golang Type.Underlying怎么用?Golang Type.Underlying使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类go/types.Type
的用法示例。
在下文中一共展示了Type.Underlying方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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
}
示例3: 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))
}
示例4: 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),
}
}
示例5: validVarDeclType
func (f *File) validVarDeclType(typ types.Type) *Error {
if e := f.validResultType(typ); e != nil {
switch typ.(type) {
default:
return e
case *types.Interface:
return f.validVarDeclType(typ.Underlying())
case *types.Named:
named, ok := typ.(*types.Named)
if !ok {
panic("ERROR can't cast to named type")
}
tname := named.Obj()
/*i := Int(0)
simdInt := reflect.TypeOf(i)
var i4 Int4
simdInt4 := reflect.TypeOf(i4)
switch tname.Name() {
default:*/
return &Error{errors.New(fmt.Sprintf("invalid type (%v)", tname.Name())), 0}
/*case simdInt.Name():
return nil
case simdInt4.Name():
return nil
}*/
}
}
return nil
}
示例6: 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
}
示例7: encodeType
func encodeType(t types.Type) Type {
t = t.Underlying()
switch t.(type) {
case *types.Basic:
b := t.(*types.Basic)
untyped := (b.Info() & types.IsUntyped) != 0
return NewBasic(basicKindString(b), untyped)
case *types.Pointer:
p := t.(*types.Pointer)
pt := encodeType(p.Elem())
return NewPointer(pt)
case *types.Array:
a := t.(*types.Array)
at := encodeType(a.Elem())
return NewArray(at, a.Len())
case *types.Slice:
s := t.(*types.Slice)
st := encodeType(s.Elem())
return NewSlice(st)
case *types.Signature:
sig := t.(*types.Signature)
v := sig.Recv()
var vt *Type
if v != nil {
t := encodeType(v.Type())
vt = &t
}
return NewSignature(
sig.Variadic(),
vt,
tupleToSlice(sig.Params()),
tupleToSlice(sig.Results()))
case *types.Named:
n := t.(*types.Named)
return NewNamed(
n.Obj().Pkg().Name(),
n.Obj().Name(),
n.Underlying())
case *types.Interface:
i := t.(*types.Interface)
if i.Empty() {
return NewInterface()
} else {
return NewUnsupported("Interfaces")
}
case *types.Tuple:
return NewUnsupported("Tuples")
case *types.Map:
return NewUnsupported("Maps")
case *types.Chan:
return NewUnsupported("Channels")
case *types.Struct:
return NewUnsupported("Structs")
default:
return NewUnsupported(t.String())
}
}
示例8: 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
}
示例9: 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))
}
示例10: CanHaveDynamicTypes
// CanHaveDynamicTypes reports whether the type T can "hold" dynamic types,
// i.e. is an interface (incl. reflect.Type) or a reflect.Value.
//
func CanHaveDynamicTypes(T types.Type) bool {
switch T := T.(type) {
case *types.Named:
if obj := T.Obj(); obj.Name() == "Value" && obj.Pkg().Path() == "reflect" {
return true // reflect.Value
}
return CanHaveDynamicTypes(T.Underlying())
case *types.Interface:
return true
}
return false
}
示例11: CanPoint
// CanPoint reports whether the type T is pointerlike,
// for the purposes of this analysis.
func CanPoint(T types.Type) bool {
switch T := T.(type) {
case *types.Named:
if obj := T.Obj(); obj.Name() == "Value" && obj.Pkg().Path() == "reflect" {
return true // treat reflect.Value like interface{}
}
return CanPoint(T.Underlying())
case *types.Pointer, *types.Interface, *types.Map, *types.Chan, *types.Signature, *types.Slice:
return true
}
return false // array struct tuple builtin basic
}
示例12: getTypeStruct
// getTypeStruct will take a type and the package scope, and return the
// (innermost) struct if the type is considered a RR type (currently defined as
// those structs beginning with a RR_Header, could be redefined as implementing
// the RR interface). The bool return value indicates if embedded structs were
// resolved.
func getTypeStruct(t types.Type, scope *types.Scope) (*types.Struct, bool) {
st, ok := t.Underlying().(*types.Struct)
if !ok {
return nil, false
}
if st.Field(0).Type() == scope.Lookup("RR_Header").Type() {
return st, false
}
if st.Field(0).Anonymous() {
st, _ := getTypeStruct(st.Field(0).Type(), scope)
return st, true
}
return nil, false
}
示例13: translateImplicitConversion
func (c *funcContext) translateImplicitConversion(expr ast.Expr, desiredType types.Type) *expression {
if desiredType == nil {
return c.translateExpr(expr)
}
exprType := c.p.TypeOf(expr)
if types.Identical(exprType, desiredType) {
return c.translateExpr(expr)
}
basicExprType, isBasicExpr := exprType.Underlying().(*types.Basic)
if isBasicExpr && basicExprType.Kind() == types.UntypedNil {
return c.formatExpr("%e", c.zeroValue(desiredType))
}
switch desiredType.Underlying().(type) {
case *types.Slice:
return c.formatExpr("$subslice(new %1s(%2e.$array), %2e.$offset, %2e.$offset + %2e.$length)", c.typeName(desiredType), expr)
case *types.Interface:
if typesutil.IsJsObject(exprType) {
// wrap JS object into js.Object struct when converting to interface
return c.formatExpr("new $jsObjectPtr(%e)", expr)
}
if isWrapped(exprType) {
return c.formatExpr("new %s(%e)", c.typeName(exprType), expr)
}
if _, isStruct := exprType.Underlying().(*types.Struct); isStruct {
return c.formatExpr("new %1e.constructor.elem(%1e)", expr)
}
}
return c.translateExpr(expr)
}
示例14: eqnil
// eqnil returns the comparison x == y using the equivalence relation
// appropriate for type t.
// If t is a reference type, at most one of x or y may be a nil value
// of that type.
//
func eqnil(t types.Type, x, y value) bool {
switch t.Underlying().(type) {
case *types.Map, *types.Signature, *types.Slice:
// Since these types don't support comparison,
// one of the operands must be a literal nil.
switch x := x.(type) {
case *hashmap:
return (x != nil) == (y.(*hashmap) != nil)
case map[value]value:
return (x != nil) == (y.(map[value]value) != nil)
case *ssa.Function:
switch y := y.(type) {
case *ssa.Function:
return (x != nil) == (y != nil)
case *closure:
return true
}
case *closure:
return (x != nil) == (y.(*ssa.Function) != nil)
case []value:
return (x != nil) == (y.([]value) != nil)
}
panic(fmt.Sprintf("eqnil(%s): illegal dynamic type: %T", t, x))
}
return equals(t, x, y)
}
示例15: zeroValue
func (c *funcContext) zeroValue(ty types.Type) ast.Expr {
switch t := ty.Underlying().(type) {
case *types.Basic:
switch {
case isBoolean(t):
return c.newConst(ty, constant.MakeBool(false))
case isNumeric(t):
return c.newConst(ty, constant.MakeInt64(0))
case isString(t):
return c.newConst(ty, constant.MakeString(""))
case t.Kind() == types.UnsafePointer:
// fall through to "nil"
case t.Kind() == types.UntypedNil:
panic("Zero value for untyped nil.")
default:
panic(fmt.Sprintf("Unhandled basic type: %v\n", t))
}
case *types.Array, *types.Struct:
return c.setType(&ast.CompositeLit{}, ty)
case *types.Chan, *types.Interface, *types.Map, *types.Signature, *types.Slice, *types.Pointer:
// fall through to "nil"
default:
panic(fmt.Sprintf("Unhandled type: %T\n", t))
}
id := c.newIdent("nil", ty)
c.p.Uses[id] = nilObj
return id
}