func (g *javaGen) genConst(o *types.Const) { if _, ok := o.Type().(*types.Basic); !ok { g.Printf("// skipped const %s with unsupported type: %T\n\n", o.Name(), o) return } // TODO(hyangah): should const names use upper cases + "_"? // TODO(hyangah): check invalid names. jType := g.javaType(o.Type()) val := constExactString(o) 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) }
func (g *ObjcGen) genConstM(o *types.Const) { if _, ok := o.Type().(*types.Basic); !ok { g.Printf("// skipped const %s with unsupported type: %T\n\n", o.Name(), o) return } cName := fmt.Sprintf("%s%s", g.namePrefix, o.Name()) objcType := 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, constExactString(o)) case types.Int, types.Int8, types.Int16, types.Int32: g.Printf("const %s %s = %s;\n", objcType, 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(), objcType) 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", objcType, cName, f) default: g.errorf("unsupported const type %s for %s", b, o.Name()) } }
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 }
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 } } }
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, } }