当前位置: 首页>>代码示例>>Golang>>正文


Golang cc.Type类代码示例

本文整理汇总了Golang中code/google/com/p/rsc/cc.Type的典型用法代码示例。如果您正苦于以下问题:Golang Type类的具体用法?Golang Type怎么用?Golang Type使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Type类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: sameType

func sameType(t, u *cc.Type) bool {
	t = t.Def()
	u = u.Def()
	if t == u {
		return true
	}
	if t == nil || u == nil {
		return false
	}
	if t.Kind != u.Kind {
		return false
	}
	if t.Name != "" || u.Name != "" {
		return t.Name == u.Name
	}
	if !sameType(t.Base, u.Base) || len(t.Decls) != len(u.Decls) {
		return false
	}
	for i, td := range t.Decls {
		ud := u.Decls[i]
		if !sameType(td.Type, ud.Type) || t.Kind == cc.Struct && td.Name != ud.Name {
			return false
		}
	}
	return true
}
开发者ID:0x7cc,项目名称:rsc,代码行数:26,代码来源:main.go

示例2: zeroFor

func zeroFor(targ *cc.Type) *cc.Expr {
	if targ != nil {
		k := targ.Def().Kind
		switch k {
		case c2go.String:
			return &cc.Expr{Op: cc.String, Texts: []string{`""`}}

		case c2go.Slice, cc.Ptr:
			return &cc.Expr{Op: cc.Name, Text: "nil"}

		case cc.Struct, cc.Array:
			return &cc.Expr{Op: cc.CastInit, Type: targ, Init: &cc.Init{}}

		case c2go.Bool:
			return &cc.Expr{Op: cc.Name, Text: "false"}
		}

		if c2go.Int8 <= k && k <= c2go.Float64 {
			return &cc.Expr{Op: cc.Number, Text: "0"}
		}
		return &cc.Expr{Op: cc.Number, Text: "0 /*" + targ.String() + "*/"}
	}

	return &cc.Expr{Op: cc.Number, Text: "0 /*untyped*/"}
}
开发者ID:0x7cc,项目名称:rsc,代码行数:25,代码来源:main.go

示例3: inferCompatible

func inferCompatible(t1, t2 *cc.Type) bool {
	t1 = t1.Def()
	t2 = t2.Def()
	if isNumericCType(t1) && isNumericCType(t2) {
		return true
	}
	if t1.Kind == cc.Ptr && t1.Base.Kind == cc.Func {
		t1 = t1.Base
	}
	if t2.Kind == cc.Ptr && t2.Base.Kind == cc.Func {
		t2 = t2.Base
	}
	if sameType(t1, t2) {
		return true
	}
	if t1.Kind > t2.Kind {
		t1, t2 = t2, t1
	}
	if t1.Kind == cc.Ptr && t2.Kind == cc.Array && (t1.Base.Is(cc.Void) || sameType(t1.Base, t2.Base)) {
		return true
	}
	if t1.Kind == cc.Ptr && t2.Kind == cc.Ptr && (t1.Base.Is(cc.Void) || t2.Base.Is(cc.Void)) {
		return true
	}
	return false
}
开发者ID:0x7cc,项目名称:rsc,代码行数:26,代码来源:infer2.go

示例4: printType

func (p *Printer) printType(t *cc.Type) {
	// Shouldn't happen but handle in case it does.
	p.Print(t.Comments.Before)
	defer p.Print(t.Comments.Suffix, t.Comments.After)

	if t == cc.BoolType {
		p.Print("bool")
		return
	}
	if typemap[t.Kind] != "" {
		p.Print(typemap[t.Kind])
		return
	}

	switch t.Kind {
	default:
		p.Print(t.String()) // hope for the best

	case cc.TypedefType:
		if typemap[t.Base.Kind] != "" && strings.ToLower(t.Name) == t.Name {
			p.Print(typemap[t.Base.Kind])
			return
		}
		p.Print(t.Name)

	case cc.Ptr:
		if t.Base.Is(cc.Func) {
			p.Print(t.Base)
			return
		}
		p.Print("*", t.Base)

	case cc.Func:
		p.Print("func(")
		for i, arg := range t.Decls {
			if i > 0 {
				p.Print(", ")
			}
			if arg.Name == "..." {
				p.Print("...interface{}")
				continue
			}
			p.Print(arg.Type)
		}
		p.Print(")")
		if !t.Base.Is(cc.Void) {
			p.Print(" ", t.Base)
		}

	case cc.Array:
		if t.Width == nil {
			p.Print("[]", t.Base) // TODO
			return
		}
		p.Print("[", t.Width, "]", t.Base)
	}
}
开发者ID:RubanDeventhiran,项目名称:discovery.etcd.io,代码行数:57,代码来源:main.go

示例5: toGoType

func toGoType(g *flowGroup, x cc.Syntax, typ *cc.Type, cache map[*cc.Type]*cc.Type) (ret *cc.Type) {
	if typ == nil {
		return nil
	}

	// Array and func implicitly convert to pointer types, so don't
	// trust the group they are in - they'll turn into pointers incorrectly.
	if g != nil && typ.Kind != cc.Array && typ.Kind != cc.Func {
		if g.goType != nil {
			return g.goType
		}
		defer func() {
			if ret != nil && ret.Kind <= cc.Enum {
				panic("bad go type override")
			}
			g.goType = ret
		}()
	}

	// Look in cache first. This cuts off recursion for self-referential types.
	// The cache only contains aggregate types - numeric types are shared
	// by many expressions in the program and we might want to translate
	// them differently in different contexts.
	if cache[typ] != nil {
		return cache[typ]
	}

	var force *cc.Type

	if d, ok := x.(*cc.Decl); ok {
		key := declKey(d)
		force = override[key]
	}

	switch typ.Kind {
	default:
		panic(fmt.Sprintf("unexpected C type %s", typ))

	case c2go.Ideal:
		return typ

	case cc.Void:
		return &cc.Type{Kind: cc.Struct} // struct{}

	case cc.Char, cc.Uchar, cc.Short, cc.Ushort, cc.Int, cc.Uint, cc.Long, cc.Ulong, cc.Longlong, cc.Ulonglong, cc.Float, cc.Double, cc.Enum:
		// TODO: Use group.
		if force != nil {
			return force
		}
		return &cc.Type{Kind: c2goKind[typ.Kind]}

	case cc.Ptr:
		t := &cc.Type{Kind: cc.Ptr}
		cache[typ] = t
		t.Base = toGoType(nil, nil, typ.Base, cache)

		if g != nil {
			if g.goKind != 0 {
				t.Kind = g.goKind
				return t
			}
			for _, f := range g.syntax {
				if f.ptrAdd || f.ptrIndex {
					t.Kind = c2go.Slice
				}
			}
		}

		if force != nil {
			if force.Base != nil {
				return force
			}
			if force.Kind == cc.Ptr || force.Kind == c2go.Slice {
				t.Kind = force.Kind
				return t
			}
		}

		if typ.Base.Kind == cc.Char {
			t.Kind = c2go.String
			t.Base = nil
			return t
		}

		return t

	case cc.Array:
		if typ.Base.Def().Kind == cc.Char {
			return &cc.Type{Kind: c2go.String}
		}
		t := &cc.Type{Kind: cc.Array, Width: typ.Width}
		cache[typ] = t
		t.Base = toGoType(nil, nil, typ.Base, cache)
		return t

	case cc.TypedefType:
		// If this is a typedef like uchar, translate the base type directly.
		def := typ.Base
		if cc.Char <= def.Kind && def.Kind <= cc.Enum {
			return toGoType(g, x, def, cache)
//.........这里部分代码省略.........
开发者ID:0x7cc,项目名称:rsc,代码行数:101,代码来源:main.go

示例6: printType

func (p *Printer) printType(t *cc.Type) {
	if t == nil {
		p.Print("<nil type>")
		return
	}

	// Shouldn't happen but handle in case it does.
	p.Print(t.Comments.Before)
	defer p.Print(t.Comments.Suffix, t.Comments.After)

	if t == cc.BoolType {
		p.Print("bool")
		return
	}
	if typemap[t.Kind] != "" {
		p.Print(typemap[t.Kind])
		return
	}

	switch t.Kind {
	default:
		p.Print("C.", t.String()) // hope for the best

	case Slice:
		p.Print("[]", t.Base)

	case String:
		p.Print("string")

	case cc.Struct:
		if len(t.Decls) == 0 {
			p.Print("struct{}")
			break
		}
		p.Print("struct {", Indent)
		p.printStructBody(t)
		p.Print(Unindent, Newline, "}")

	case cc.Enum:
		if t.Tag != "" {
			p.Print(t.Tag)
		} else {
			p.Print("int")
		}

	case cc.TypedefType:
		if t.Base != nil && typemap[t.Base.Kind] != "" && strings.ToLower(t.Name) == t.Name {
			p.Print(typemap[t.Base.Kind])
			return
		}
		if t.TypeDecl != nil && t.TypeDecl.GoPackage != "" && p.Package != "" && t.TypeDecl.GoPackage != p.Package {
			p.Print(path.Base(t.TypeDecl.GoPackage) + "." + t.Name)
			break
		}
		p.Print(t.Name)

	case cc.Ptr:
		if t.Base.Is(cc.Func) {
			p.Print(t.Base)
			return
		}
		if t.Base.Is(cc.Void) {
			p.Print("*[0]byte")
			return
		}
		p.Print("*", t.Base)

	case cc.Func:
		p.Print("func(")
		for i, arg := range t.Decls {
			if i > 0 {
				p.Print(", ")
			}
			if arg.Name == "..." {
				p.Print("...interface{}")
				continue
			}
			if arg.Name == "" && arg.Type.Is(cc.Void) {
				continue
			}
			p.Print(arg.Type)
		}
		p.Print(")")
		if !t.Base.Is(cc.Void) {
			p.Print(" ", t.Base)
		}

	case cc.Array:
		if t.Width == nil {
			p.Print("[XXX]", t.Base)
			return
		}
		p.Print("[", t.Width, "]", t.Base)
	}
}
开发者ID:0x7cc,项目名称:rsc,代码行数:95,代码来源:printer.go

示例7: printInit

func (p *Printer) printInit(typ *cc.Type, x *cc.Init) {
	p.Print(x.Comments.Before)
	defer p.Print(x.Comments.Suffix, x.Comments.After)

	if len(x.Prefix) > 0 {
		for _, pre := range x.Prefix {
			p.Print(pre)
		}
	}
	if x.Expr != nil {
		if x.Expr.Op == cc.Number && (typ.Is(cc.Ptr) || typ.Is(Slice)) {
			p.Print("nil")
			return
		}
		p.printExpr(x.Expr, precComma)
		return
	}

	nl := len(x.Braced) > 0 && x.Braced[0].Span.Start.Line != x.Braced[len(x.Braced)-1].Span.End.Line
	if typ != nil {
		p.printType(typ)
	}
	p.Print("{")
	if nl {
		p.Print(Indent)
	}
	warned := false
	for i, y := range x.Braced {
		if nl {
			p.Print(Newline)
		} else if i > 0 {
			p.Print(" ")
		}
		var subtyp *cc.Type
		if typ != nil {
			if typ.Is(cc.Struct) && i < len(typ.Def().Decls) && len(y.Prefix) == 0 {
				subtyp = typ.Def().Decls[i].Type
			} else if typ.Is(cc.Struct) && len(y.Prefix) == 1 && y.Prefix[0].XDecl != nil {
				subtyp = y.Prefix[0].XDecl.Type
			} else if typ.Is(cc.Array) || typ.Is(Slice) {
				subtyp = typ.Def().Base
			} else if !warned {
				warned = true
				fprintf(x.Span, "too many fields in braced initializer of %s", GoString(typ))
			}
		}
		p.printInit(subtyp, y)
		p.Print(",")
	}
	if typ != nil && typ.Is(cc.Struct) && len(x.Braced) > 0 && len(x.Braced[0].Prefix) == 0 && len(x.Braced) < len(typ.Def().Decls) {
		for i := len(x.Braced); i < len(typ.Def().Decls); i++ {
			subtyp := typ.Def().Decls[i].Type
			if subtyp.Is(cc.Ptr) || subtyp.Is(Slice) {
				p.Print(" nil,")
			} else if subtyp.Is(cc.Array) {
				p.Print(" ", subtyp, "{},")
			} else {
				p.Print(" 0,")
			}
		}
	}
	if nl {
		p.Print(Unindent, Newline)
	}
	p.Print("}")
}
开发者ID:0x7cc,项目名称:rsc,代码行数:66,代码来源:printer.go


注:本文中的code/google/com/p/rsc/cc.Type类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。