本文整理汇总了Golang中go/types.Const.Val方法的典型用法代码示例。如果您正苦于以下问题:Golang Const.Val方法的具体用法?Golang Const.Val怎么用?Golang Const.Val使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类go/types.Const
的用法示例。
在下文中一共展示了Const.Val方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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()
}
示例2: 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)
}
示例3: 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())
}
示例4: 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
}
示例5: 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
}
}
}
示例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())
}
}