本文整理汇总了Golang中go/types.Const类的典型用法代码示例。如果您正苦于以下问题:Golang Const类的具体用法?Golang Const怎么用?Golang Const使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Const类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: ConstValue
// ConstValue returns the SSA Value denoted by the source-level named
// constant obj.
//
func (prog *Program) ConstValue(obj *types.Const) *Const {
// TODO(adonovan): opt: share (don't reallocate)
// Consts for const objects and constant ast.Exprs.
// Universal constant? {true,false,nil}
if obj.Parent() == types.Universe {
return NewConst(obj.Val(), obj.Type())
}
// Package-level named constant?
if v := prog.packageLevelValue(obj); v != nil {
return v.(*Const)
}
return NewConst(obj.Val(), obj.Type())
}
开发者ID:ChloeTigre,项目名称:golang-tools,代码行数:17,代码来源:source.go
示例2: constExactString
func constExactString(o *types.Const) string {
// TODO(hyangah): this is a temporary fix for golang.org/issues/14615.
// Clean this up when we can require at least go 1.6 or above.
type exactStringer interface {
ExactString() string
}
v := o.Val()
if v, ok := v.(exactStringer); ok {
return v.ExactString()
}
// TODO: warning?
return v.String()
}
开发者ID:ych1,项目名称:mobile,代码行数:14,代码来源:gen.go
示例3: convertConst
func (c *converter) convertConst(v *gotypes.Const) *types.Const {
if v == nil {
return nil
}
if v, ok := c.converted[v]; ok {
return v.(*types.Const)
}
ret := types.NewConst(
token.Pos(v.Pos()),
c.ret,
v.Name(),
c.convertType(v.Type()),
c.convertConstantValue(v.Val()),
)
c.converted[v] = ret
return ret
}
开发者ID:tcard,项目名称:sgo,代码行数:17,代码来源:importer.go
示例4: checkConstValue
func checkConstValue(t *testing.T, prog *ssa.Program, obj *types.Const) {
c := prog.ConstValue(obj)
// fmt.Printf("ConstValue(%s) = %s\n", obj, c) // debugging
if c == nil {
t.Errorf("ConstValue(%s) == nil", obj)
return
}
if !types.Identical(c.Type(), obj.Type()) {
t.Errorf("ConstValue(%s).Type() == %s", obj, c.Type())
return
}
if obj.Name() != "nil" {
if !exact.Compare(c.Value, token.EQL, obj.Val()) {
t.Errorf("ConstValue(%s).Value (%s) != %s",
obj, c.Value, obj.Val())
return
}
}
}
开发者ID:ChloeTigre,项目名称:golang-tools,代码行数:19,代码来源:source_test.go
示例5: genConst
func (g *javaGen) genConst(o *types.Const) {
// TODO(hyangah): should const names use upper cases + "_"?
// TODO(hyangah): check invalid names.
jType := g.javaType(o.Type())
val := o.Val().String()
switch b := o.Type().(*types.Basic); b.Kind() {
case types.Int64, types.UntypedInt:
i, exact := constant.Int64Val(o.Val())
if !exact {
g.errorf("const value %s for %s cannot be represented as %s", val, o.Name(), jType)
return
}
val = fmt.Sprintf("%dL", i)
case types.Float32:
f, _ := constant.Float32Val(o.Val())
val = fmt.Sprintf("%gf", f)
case types.Float64, types.UntypedFloat:
f, _ := constant.Float64Val(o.Val())
if math.IsInf(f, 0) || math.Abs(f) > math.MaxFloat64 {
g.errorf("const value %s for %s cannot be represented as %s", val, o.Name(), jType)
return
}
val = fmt.Sprintf("%g", f)
}
g.Printf("public static final %s %s = %s;\n", g.javaType(o.Type()), o.Name(), val)
}
开发者ID:dylanpoe,项目名称:golang.org,代码行数:28,代码来源:genjava.go
示例6: genConstM
func (g *objcGen) genConstM(o *types.Const) {
cName := fmt.Sprintf("%s%s", g.namePrefix, o.Name())
cType := g.objcType(o.Type())
switch b := o.Type().(*types.Basic); b.Kind() {
case types.Bool, types.UntypedBool:
v := "NO"
if constant.BoolVal(o.Val()) {
v = "YES"
}
g.Printf("const BOOL %s = %s;\n", cName, v)
case types.String, types.UntypedString:
g.Printf("NSString* const %s = @%s;\n", cName, o.Val())
case types.Int, types.Int8, types.Int16, types.Int32:
g.Printf("const %s %s = %s;\n", cType, cName, o.Val())
case types.Int64, types.UntypedInt:
i, exact := constant.Int64Val(o.Val())
if !exact {
g.errorf("const value %s for %s cannot be represented as %s", o.Val(), o.Name(), cType)
return
}
if i == math.MinInt64 {
// -9223372036854775808LL does not work because 922337203685477508 is
// larger than max int64.
g.Printf("const int64_t %s = %dLL-1;\n", cName, i+1)
} else {
g.Printf("const int64_t %s = %dLL;\n", cName, i)
}
case types.Float32, types.Float64, types.UntypedFloat:
f, _ := constant.Float64Val(o.Val())
if math.IsInf(f, 0) || math.Abs(f) > math.MaxFloat64 {
g.errorf("const value %s for %s cannot be represented as double", o.Val(), o.Name())
return
}
g.Printf("const %s %s = %g;\n", cType, cName, f)
default:
g.errorf("unsupported const type %s for %s", b, o.Name())
}
}
开发者ID:2722,项目名称:lantern,代码行数:44,代码来源:genobjc.go
示例7: newConst
func newConst(p *Package, o *types.Const) Const {
pkg := o.Pkg()
sym := p.syms.symtype(o.Type())
id := pkg.Name() + "_" + o.Name()
doc := p.getDoc("", o)
res := []*Var{newVar(p, o.Type(), "ret", o.Name(), doc)}
sig := newSignature(p, nil, nil, res)
fct := Func{
pkg: p,
sig: sig,
typ: nil,
name: o.Name(),
id: id + "_get",
doc: doc,
ret: o.Type(),
err: false,
}
return Const{
pkg: p,
sym: sym,
obj: o,
id: id,
doc: doc,
f: fct,
}
}
开发者ID:ashrafulratul,项目名称:gopy,代码行数:28,代码来源:package.go
注:本文中的go/types.Const类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论