本文整理汇总了Golang中code/google/com/p/go/tools/go/exact.Uint64Val函数的典型用法代码示例。如果您正苦于以下问题:Golang Uint64Val函数的具体用法?Golang Uint64Val怎么用?Golang Uint64Val使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Uint64Val函数的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Uint64
// Uint64 returns the numeric value of this constant truncated to fit
// an unsigned 64-bit integer.
//
func (c *Const) Uint64() uint64 {
switch x := c.Value; x.Kind() {
case exact.Int:
if u, ok := exact.Uint64Val(x); ok {
return u
}
return 0
case exact.Float:
f, _ := exact.Float64Val(x)
return uint64(f)
}
panic(fmt.Sprintf("unexpected constant value: %T", c.Value))
}
示例2: Uint64
// Uint64 returns the numeric value of this literal truncated to fit
// an unsigned 64-bit integer.
//
func (l *Literal) Uint64() uint64 {
switch x := l.Value; x.Kind() {
case exact.Int:
if u, ok := exact.Uint64Val(x); ok {
return u
}
return 0
case exact.Float:
f, _ := exact.Float64Val(x)
return uint64(f)
}
panic(fmt.Sprintf("unexpected literal value: %T", l.Value))
}
示例3: writeInt
func writeInt(w *bytes.Buffer, ev exact.Value, k types.BasicKind) {
if k == types.Uintptr {
u, _ := exact.Uint64Val(ev)
w.WriteString("0x")
w.WriteString(strconv.FormatUint(u, 16))
return
}
w.WriteString(ev.String())
switch k {
case types.Int32:
w.WriteByte('L')
case types.Uint32:
w.WriteString("UL")
case types.Int64:
w.WriteString("LL")
case types.Uint64:
w.WriteString("ULL")
}
}
示例4: shift
func (check *Checker) shift(x, y *operand, op token.Token) {
untypedx := isUntyped(x.typ)
// The lhs must be of integer type or be representable
// as an integer; otherwise the shift has no chance.
if !isInteger(x.typ) && (!untypedx || !representableConst(x.val, nil, UntypedInt, nil)) {
check.invalidOp(x.pos(), "shifted operand %s must be integer", x)
x.mode = invalid
return
}
// spec: "The right operand in a shift expression must have unsigned
// integer type or be an untyped constant that can be converted to
// unsigned integer type."
switch {
case isInteger(y.typ) && isUnsigned(y.typ):
// nothing to do
case isUntyped(y.typ):
check.convertUntyped(y, Typ[UntypedInt])
if y.mode == invalid {
x.mode = invalid
return
}
default:
check.invalidOp(y.pos(), "shift count %s must be unsigned integer", y)
x.mode = invalid
return
}
if x.mode == constant {
if y.mode == constant {
// rhs must be within reasonable bounds
const stupidShift = 1023 - 1 + 52 // so we can express smallestFloat64
s, ok := exact.Uint64Val(y.val)
if !ok || s > stupidShift {
check.invalidOp(y.pos(), "stupid shift count %s", y)
x.mode = invalid
return
}
// The lhs is representable as an integer but may not be an integer
// (e.g., 2.0, an untyped float) - this can only happen for untyped
// non-integer numeric constants. Correct the type so that the shift
// result is of integer type.
if !isInteger(x.typ) {
x.typ = Typ[UntypedInt]
}
x.val = exact.Shift(x.val, op, uint(s))
return
}
// non-constant shift with constant lhs
if untypedx {
// spec: "If the left operand of a non-constant shift
// expression is an untyped constant, the type of the
// constant is what it would be if the shift expression
// were replaced by its left operand alone.".
//
// Delay operand checking until we know the final type:
// The lhs expression must be in the untyped map, mark
// the entry as lhs shift operand.
info, found := check.untyped[x.expr]
assert(found)
info.isLhs = true
check.untyped[x.expr] = info
// keep x's type
x.mode = value
return
}
}
// constant rhs must be >= 0
if y.mode == constant && exact.Sign(y.val) < 0 {
check.invalidOp(y.pos(), "shift count %s must not be negative", y)
}
// non-constant shift - lhs must be an integer
if !isInteger(x.typ) {
check.invalidOp(x.pos(), "shifted operand %s must be integer", x)
x.mode = invalid
return
}
x.mode = value
}
示例5: NewConstValue
func (c *compiler) NewConstValue(v exact.Value, typ types.Type) *LLVMValue {
switch {
case v.Kind() == exact.Unknown:
// TODO nil literals should be represented more appropriately once the exact-package supports it.
llvmtyp := c.types.ToLLVM(typ)
return c.NewValue(llvm.ConstNull(llvmtyp), typ)
case isString(typ):
if isUntyped(typ) {
typ = types.Typ[types.String]
}
llvmtyp := c.types.ToLLVM(typ)
strval := exact.StringVal(v)
strlen := len(strval)
i8ptr := llvm.PointerType(llvm.Int8Type(), 0)
var ptr llvm.Value
if strlen > 0 {
init := llvm.ConstString(strval, false)
ptr = llvm.AddGlobal(c.module.Module, init.Type(), "")
ptr.SetInitializer(init)
ptr = llvm.ConstBitCast(ptr, i8ptr)
} else {
ptr = llvm.ConstNull(i8ptr)
}
len_ := llvm.ConstInt(c.types.inttype, uint64(strlen), false)
llvmvalue := llvm.Undef(llvmtyp)
llvmvalue = llvm.ConstInsertValue(llvmvalue, ptr, []uint32{0})
llvmvalue = llvm.ConstInsertValue(llvmvalue, len_, []uint32{1})
return c.NewValue(llvmvalue, typ)
case isInteger(typ):
if isUntyped(typ) {
typ = types.Typ[types.Int]
}
llvmtyp := c.types.ToLLVM(typ)
var llvmvalue llvm.Value
if isUnsigned(typ) {
v, _ := exact.Uint64Val(v)
llvmvalue = llvm.ConstInt(llvmtyp, v, false)
} else {
v, _ := exact.Int64Val(v)
llvmvalue = llvm.ConstInt(llvmtyp, uint64(v), true)
}
return c.NewValue(llvmvalue, typ)
case isBoolean(typ):
if isUntyped(typ) {
typ = types.Typ[types.Bool]
}
var llvmvalue llvm.Value
if exact.BoolVal(v) {
llvmvalue = llvm.ConstAllOnes(llvm.Int1Type())
} else {
llvmvalue = llvm.ConstNull(llvm.Int1Type())
}
return c.NewValue(llvmvalue, typ)
case isFloat(typ):
if isUntyped(typ) {
typ = types.Typ[types.Float64]
}
llvmtyp := c.types.ToLLVM(typ)
floatval, _ := exact.Float64Val(v)
llvmvalue := llvm.ConstFloat(llvmtyp, floatval)
return c.NewValue(llvmvalue, typ)
case typ == types.Typ[types.UnsafePointer]:
llvmtyp := c.types.ToLLVM(typ)
v, _ := exact.Uint64Val(v)
llvmvalue := llvm.ConstInt(llvmtyp, v, false)
return c.NewValue(llvmvalue, typ)
case isComplex(typ):
if isUntyped(typ) {
typ = types.Typ[types.Complex128]
}
llvmtyp := c.types.ToLLVM(typ)
floattyp := llvmtyp.StructElementTypes()[0]
llvmvalue := llvm.ConstNull(llvmtyp)
realv := exact.Real(v)
imagv := exact.Imag(v)
realfloatval, _ := exact.Float64Val(realv)
imagfloatval, _ := exact.Float64Val(imagv)
llvmre := llvm.ConstFloat(floattyp, realfloatval)
llvmim := llvm.ConstFloat(floattyp, imagfloatval)
llvmvalue = llvm.ConstInsertValue(llvmvalue, llvmre, []uint32{0})
llvmvalue = llvm.ConstInsertValue(llvmvalue, llvmim, []uint32{1})
return c.NewValue(llvmvalue, typ)
}
// Special case for string -> [](byte|rune)
if u, ok := typ.Underlying().(*types.Slice); ok && isInteger(u.Elem()) {
if v.Kind() == exact.String {
strval := c.NewConstValue(v, types.Typ[types.String])
return strval.Convert(typ).(*LLVMValue)
}
}
panic(fmt.Sprintf("unhandled: t=%s(%T), v=%v(%T)", c.types.TypeString(typ), typ, v, v))
}
示例6: translateExpr
func (c *funcContext) translateExpr(expr ast.Expr) *expression {
exprType := c.p.info.Types[expr].Type
if value := c.p.info.Types[expr].Value; value != nil {
basic := types.Typ[types.String]
if value.Kind() != exact.String { // workaround for bug in go/types
basic = exprType.Underlying().(*types.Basic)
}
switch {
case basic.Info()&types.IsBoolean != 0:
return c.formatExpr("%s", strconv.FormatBool(exact.BoolVal(value)))
case basic.Info()&types.IsInteger != 0:
if is64Bit(basic) {
d, _ := exact.Uint64Val(value)
if basic.Kind() == types.Int64 {
return c.formatExpr("new %s(%s, %s)", c.typeName(exprType), strconv.FormatInt(int64(d)>>32, 10), strconv.FormatUint(d&(1<<32-1), 10))
}
return c.formatExpr("new %s(%s, %s)", c.typeName(exprType), strconv.FormatUint(d>>32, 10), strconv.FormatUint(d&(1<<32-1), 10))
}
d, _ := exact.Int64Val(value)
return c.formatExpr("%s", strconv.FormatInt(d, 10))
case basic.Info()&types.IsFloat != 0:
f, _ := exact.Float64Val(value)
return c.formatExpr("%s", strconv.FormatFloat(f, 'g', -1, 64))
case basic.Info()&types.IsComplex != 0:
r, _ := exact.Float64Val(exact.Real(value))
i, _ := exact.Float64Val(exact.Imag(value))
if basic.Kind() == types.UntypedComplex {
exprType = types.Typ[types.Complex128]
}
return c.formatExpr("new %s(%s, %s)", c.typeName(exprType), strconv.FormatFloat(r, 'g', -1, 64), strconv.FormatFloat(i, 'g', -1, 64))
case basic.Info()&types.IsString != 0:
return c.formatExpr("%s", encodeString(exact.StringVal(value)))
default:
panic("Unhandled constant type: " + basic.String())
}
}
switch e := expr.(type) {
case *ast.CompositeLit:
if ptrType, isPointer := exprType.(*types.Pointer); isPointer {
exprType = ptrType.Elem()
}
collectIndexedElements := func(elementType types.Type) []string {
elements := make([]string, 0)
i := 0
zero := c.zeroValue(elementType)
for _, element := range e.Elts {
if kve, isKve := element.(*ast.KeyValueExpr); isKve {
key, _ := exact.Int64Val(c.p.info.Types[kve.Key].Value)
i = int(key)
element = kve.Value
}
for len(elements) <= i {
elements = append(elements, zero)
}
elements[i] = c.translateImplicitConversionWithCloning(element, elementType).String()
i++
}
return elements
}
switch t := exprType.Underlying().(type) {
case *types.Array:
elements := collectIndexedElements(t.Elem())
if len(elements) == 0 {
return c.formatExpr("%s", c.zeroValue(t))
}
zero := c.zeroValue(t.Elem())
for len(elements) < int(t.Len()) {
elements = append(elements, zero)
}
return c.formatExpr(`$toNativeArray("%s", [%s])`, typeKind(t.Elem()), strings.Join(elements, ", "))
case *types.Slice:
return c.formatExpr("new %s([%s])", c.typeName(exprType), strings.Join(collectIndexedElements(t.Elem()), ", "))
case *types.Map:
mapVar := c.newVariable("_map")
keyVar := c.newVariable("_key")
assignments := ""
for _, element := range e.Elts {
kve := element.(*ast.KeyValueExpr)
assignments += c.formatExpr(`%s = %s, %s[%s] = { k: %s, v: %s }, `, keyVar, c.translateImplicitConversion(kve.Key, t.Key()), mapVar, c.makeKey(c.newIdent(keyVar, t.Key()), t.Key()), keyVar, c.translateImplicitConversion(kve.Value, t.Elem())).String()
}
return c.formatExpr("(%s = new $Map(), %s%s)", mapVar, assignments, mapVar)
case *types.Struct:
elements := make([]string, t.NumFields())
isKeyValue := true
if len(e.Elts) != 0 {
_, isKeyValue = e.Elts[0].(*ast.KeyValueExpr)
}
if !isKeyValue {
for i, element := range e.Elts {
elements[i] = c.translateImplicitConversion(element, t.Field(i).Type()).String()
}
}
if isKeyValue {
for i := range elements {
elements[i] = c.zeroValue(t.Field(i).Type())
}
for _, element := range e.Elts {
//.........这里部分代码省略.........
示例7: formatExprInternal
func (c *funcContext) formatExprInternal(format string, a []interface{}, parens bool) *expression {
processFormat := func(f func(uint8, uint8, int)) {
n := 0
for i := 0; i < len(format); i++ {
b := format[i]
if b == '%' {
i++
k := format[i]
if k >= '0' && k <= '9' {
n = int(k - '0' - 1)
i++
k = format[i]
}
f(0, k, n)
n++
continue
}
f(b, 0, 0)
}
}
counts := make([]int, len(a))
processFormat(func(b, k uint8, n int) {
switch k {
case 'e', 'f', 'h', 'l', 'r', 'i':
counts[n]++
}
})
out := bytes.NewBuffer(nil)
vars := make([]string, len(a))
hasAssignments := false
for i, e := range a {
if counts[i] <= 1 {
continue
}
if _, isIdent := e.(*ast.Ident); isIdent {
continue
}
if val := c.p.info.Types[e.(ast.Expr)].Value; val != nil {
continue
}
if !hasAssignments {
hasAssignments = true
out.WriteByte('(')
parens = false
}
v := c.newVariable("x")
out.WriteString(v + " = " + c.translateExpr(e.(ast.Expr)).String() + ", ")
vars[i] = v
}
processFormat(func(b, k uint8, n int) {
writeExpr := func(suffix string) {
if vars[n] != "" {
out.WriteString(vars[n] + suffix)
return
}
out.WriteString(c.translateExpr(a[n].(ast.Expr)).StringWithParens() + suffix)
}
switch k {
case 0:
out.WriteByte(b)
case 's':
if e, ok := a[n].(*expression); ok {
out.WriteString(e.StringWithParens())
return
}
out.WriteString(a[n].(string))
case 'd':
out.WriteString(strconv.Itoa(a[n].(int)))
case 't':
out.WriteString(a[n].(token.Token).String())
case 'e':
e := a[n].(ast.Expr)
if val := c.p.info.Types[e].Value; val != nil {
out.WriteString(c.translateExpr(e).String())
return
}
writeExpr("")
case 'f':
e := a[n].(ast.Expr)
if val := c.p.info.Types[e].Value; val != nil {
d, _ := exact.Int64Val(val)
out.WriteString(strconv.FormatInt(d, 10))
return
}
if is64Bit(c.p.info.Types[e].Type.Underlying().(*types.Basic)) {
out.WriteString("$flatten64(")
writeExpr("")
out.WriteString(")")
return
}
writeExpr("")
case 'h':
e := a[n].(ast.Expr)
if val := c.p.info.Types[e].Value; val != nil {
d, _ := exact.Uint64Val(val)
if c.p.info.Types[e].Type.Underlying().(*types.Basic).Kind() == types.Int64 {
out.WriteString(strconv.FormatInt(int64(d)>>32, 10))
//.........这里部分代码省略.........
示例8: translateExpr
func (c *PkgContext) translateExpr(expr ast.Expr) string {
exprType := c.info.Types[expr]
if value, valueFound := c.info.Values[expr]; valueFound {
basic := types.Typ[types.String]
if value.Kind() != exact.String { // workaround for bug in go/types
basic = exprType.Underlying().(*types.Basic)
}
switch {
case basic.Info()&types.IsBoolean != 0:
return strconv.FormatBool(exact.BoolVal(value))
case basic.Info()&types.IsInteger != 0:
if is64Bit(basic) {
d, _ := exact.Uint64Val(value)
return fmt.Sprintf("new %s(%d, %d)", c.typeName(exprType), d>>32, d&(1<<32-1))
}
d, _ := exact.Int64Val(value)
return strconv.FormatInt(d, 10)
case basic.Info()&types.IsFloat != 0:
f, _ := exact.Float64Val(value)
return strconv.FormatFloat(f, 'g', -1, 64)
case basic.Info()&types.IsComplex != 0:
r, _ := exact.Float64Val(exact.Real(value))
i, _ := exact.Float64Val(exact.Imag(value))
if basic.Kind() == types.UntypedComplex {
exprType = types.Typ[types.Complex128]
}
return fmt.Sprintf("new %s(%s, %s)", c.typeName(exprType), strconv.FormatFloat(r, 'g', -1, 64), strconv.FormatFloat(i, 'g', -1, 64))
case basic.Info()&types.IsString != 0:
buffer := bytes.NewBuffer(nil)
for _, r := range []byte(exact.StringVal(value)) {
switch r {
case '\b':
buffer.WriteString(`\b`)
case '\f':
buffer.WriteString(`\f`)
case '\n':
buffer.WriteString(`\n`)
case '\r':
buffer.WriteString(`\r`)
case '\t':
buffer.WriteString(`\t`)
case '\v':
buffer.WriteString(`\v`)
case '"':
buffer.WriteString(`\"`)
case '\\':
buffer.WriteString(`\\`)
default:
if r < 0x20 || r > 0x7E {
fmt.Fprintf(buffer, `\x%02X`, r)
continue
}
buffer.WriteByte(r)
}
}
return `"` + buffer.String() + `"`
default:
panic("Unhandled constant type: " + basic.String())
}
}
switch e := expr.(type) {
case *ast.CompositeLit:
if ptrType, isPointer := exprType.(*types.Pointer); isPointer {
exprType = ptrType.Elem()
}
collectIndexedElements := func(elementType types.Type) []string {
elements := make([]string, 0)
i := 0
zero := c.zeroValue(elementType)
for _, element := range e.Elts {
if kve, isKve := element.(*ast.KeyValueExpr); isKve {
key, _ := exact.Int64Val(c.info.Values[kve.Key])
i = int(key)
element = kve.Value
}
for len(elements) <= i {
elements = append(elements, zero)
}
elements[i] = c.translateExprToType(element, elementType)
i++
}
return elements
}
switch t := exprType.Underlying().(type) {
case *types.Array:
elements := collectIndexedElements(t.Elem())
if len(elements) != 0 {
zero := c.zeroValue(t.Elem())
for len(elements) < int(t.Len()) {
elements = append(elements, zero)
}
return createListComposite(t.Elem(), elements)
}
return fmt.Sprintf("Go$makeArray(%s, %d, function() { return %s; })", toArrayType(t.Elem()), t.Len(), c.zeroValue(t.Elem()))
case *types.Slice:
return fmt.Sprintf("new %s(%s)", c.typeName(exprType), createListComposite(t.Elem(), collectIndexedElements(t.Elem())))
case *types.Map:
//.........这里部分代码省略.........
示例9: translateExpr
func (c *funcContext) translateExpr(expr ast.Expr) *expression {
exprType := c.p.info.Types[expr].Type
if value := c.p.info.Types[expr].Value; value != nil {
basic := types.Typ[types.String]
if value.Kind() != exact.String { // workaround for bug in go/types
basic = exprType.Underlying().(*types.Basic)
}
switch {
case basic.Info()&types.IsBoolean != 0:
return c.formatExpr("%s", strconv.FormatBool(exact.BoolVal(value)))
case basic.Info()&types.IsInteger != 0:
if is64Bit(basic) {
d, _ := exact.Uint64Val(value)
if basic.Kind() == types.Int64 {
return c.formatExpr("new %s(%s, %s)", c.typeName(exprType), strconv.FormatInt(int64(d)>>32, 10), strconv.FormatUint(d&(1<<32-1), 10))
}
return c.formatExpr("new %s(%s, %s)", c.typeName(exprType), strconv.FormatUint(d>>32, 10), strconv.FormatUint(d&(1<<32-1), 10))
}
d, _ := exact.Int64Val(value)
return c.formatExpr("%s", strconv.FormatInt(d, 10))
case basic.Info()&types.IsFloat != 0:
f, _ := exact.Float64Val(value)
return c.formatExpr("%s", strconv.FormatFloat(f, 'g', -1, 64))
case basic.Info()&types.IsComplex != 0:
r, _ := exact.Float64Val(exact.Real(value))
i, _ := exact.Float64Val(exact.Imag(value))
if basic.Kind() == types.UntypedComplex {
exprType = types.Typ[types.Complex128]
}
return c.formatExpr("new %s(%s, %s)", c.typeName(exprType), strconv.FormatFloat(r, 'g', -1, 64), strconv.FormatFloat(i, 'g', -1, 64))
case basic.Info()&types.IsString != 0:
return c.formatExpr("%s", encodeString(exact.StringVal(value)))
default:
panic("Unhandled constant type: " + basic.String())
}
}
switch e := expr.(type) {
case *ast.CompositeLit:
if ptrType, isPointer := exprType.(*types.Pointer); isPointer {
exprType = ptrType.Elem()
}
collectIndexedElements := func(elementType types.Type) []string {
elements := make([]string, 0)
i := 0
zero := c.zeroValue(elementType)
for _, element := range e.Elts {
if kve, isKve := element.(*ast.KeyValueExpr); isKve {
key, _ := exact.Int64Val(c.p.info.Types[kve.Key].Value)
i = int(key)
element = kve.Value
}
for len(elements) <= i {
elements = append(elements, zero)
}
elements[i] = c.translateImplicitConversion(element, elementType).String()
i++
}
return elements
}
switch t := exprType.Underlying().(type) {
case *types.Array:
elements := collectIndexedElements(t.Elem())
if len(elements) != 0 {
zero := c.zeroValue(t.Elem())
for len(elements) < int(t.Len()) {
elements = append(elements, zero)
}
return c.formatExpr(`go$toNativeArray("%s", [%s])`, typeKind(t.Elem()), strings.Join(elements, ", "))
}
return c.formatExpr(`go$makeNativeArray("%s", %d, function() { return %s; })`, typeKind(t.Elem()), int(t.Len()), c.zeroValue(t.Elem()))
case *types.Slice:
return c.formatExpr("new %s([%s])", c.typeName(exprType), strings.Join(collectIndexedElements(t.Elem()), ", "))
case *types.Map:
mapVar := c.newVariable("_map")
keyVar := c.newVariable("_key")
assignments := ""
for _, element := range e.Elts {
kve := element.(*ast.KeyValueExpr)
assignments += c.formatExpr(`%s = %s, %s[%s] = { k: %s, v: %s }, `, keyVar, c.translateImplicitConversion(kve.Key, t.Key()), mapVar, c.makeKey(c.newIdent(keyVar, t.Key()), t.Key()), keyVar, c.translateImplicitConversion(kve.Value, t.Elem())).String()
}
return c.formatExpr("(%s = new Go$Map(), %s%s)", mapVar, assignments, mapVar)
case *types.Struct:
elements := make([]string, t.NumFields())
isKeyValue := true
if len(e.Elts) != 0 {
_, isKeyValue = e.Elts[0].(*ast.KeyValueExpr)
}
if !isKeyValue {
for i, element := range e.Elts {
elements[i] = c.translateImplicitConversion(element, t.Field(i).Type()).String()
}
}
if isKeyValue {
for i := range elements {
elements[i] = c.zeroValue(t.Field(i).Type())
}
for _, element := range e.Elts {
//.........这里部分代码省略.........
示例10: VisitCompositeLit
func (c *compiler) VisitCompositeLit(lit *ast.CompositeLit) (v *LLVMValue) {
typ := c.types.expr[lit].Type
var valuemap map[interface{}]Value
var valuelist []Value
if ptr, ok := typ.(*types.Pointer); ok {
typ = ptr.Elem()
defer func() {
v = v.pointer
}()
}
var isstruct, isarray, isslice, ismap bool
switch typ.Underlying().(type) {
case *types.Struct:
isstruct = true
case *types.Array:
isarray = true
case *types.Slice:
isslice = true
case *types.Map:
ismap = true
default:
panic(fmt.Errorf("Unhandled type: %s", typ))
}
if lit.Elts != nil {
for i, elt := range lit.Elts {
if kv, iskv := elt.(*ast.KeyValueExpr); iskv {
if valuemap == nil {
valuemap = make(map[interface{}]Value)
}
var key interface{}
var elttyp types.Type
switch {
case isstruct:
name := kv.Key.(*ast.Ident).Name
key = name
typ := typ.Underlying().(*types.Struct)
elttyp = typ.Field(fieldIndex(typ, name)).Type
case isarray:
key = c.types.expr[kv.Key].Value
typ := typ.Underlying().(*types.Array)
elttyp = typ.Elem()
case isslice:
key = c.types.expr[kv.Key].Value
typ := typ.Underlying().(*types.Slice)
elttyp = typ.Elem()
case ismap:
key = c.VisitExpr(kv.Key)
typ := typ.Underlying().(*types.Map)
elttyp = typ.Elem()
default:
panic("unreachable")
}
c.convertUntyped(kv.Value, elttyp)
valuemap[key] = c.VisitExpr(kv.Value)
} else {
switch {
case isstruct:
typ := typ.Underlying().(*types.Struct)
c.convertUntyped(elt, typ.Field(i).Type)
case isarray:
typ := typ.Underlying().(*types.Array)
c.convertUntyped(elt, typ.Elem())
case isslice:
typ := typ.Underlying().(*types.Slice)
c.convertUntyped(elt, typ.Elem())
}
value := c.VisitExpr(elt)
valuelist = append(valuelist, value)
}
}
}
// For array/slice types, convert key:value to contiguous
// values initialiser.
switch typ.Underlying().(type) {
case *types.Array, *types.Slice:
if len(valuemap) > 0 {
var maxkey uint64
for key, _ := range valuemap {
key, _ := exact.Uint64Val(key.(exact.Value))
if key > maxkey {
maxkey = key
}
}
valuelist = make([]Value, maxkey+1)
for key, value := range valuemap {
key, _ := exact.Uint64Val(key.(exact.Value))
valuelist[key] = value
}
}
}
origtyp := typ
switch typ := typ.Underlying().(type) {
case *types.Array:
elttype := typ.Elem()
llvmelttype := c.types.ToLLVM(elttype)
//.........这里部分代码省略.........
示例11: Write
func Write(pkg *types.Package, out io.Writer, sizes types.Sizes) {
fmt.Fprintf(out, "package %s\n", pkg.Name())
e := &exporter{pkg: pkg, imports: make(map[*types.Package]bool), out: out}
for _, imp := range pkg.Imports() {
e.addImport(imp)
}
for _, name := range pkg.Scope().Names() {
obj := pkg.Scope().Lookup(name)
_, isTypeName := obj.(*types.TypeName)
if obj.Exported() || isTypeName {
e.toExport = append(e.toExport, obj)
}
}
for i := 0; i < len(e.toExport); i++ {
switch o := e.toExport[i].(type) {
case *types.TypeName:
fmt.Fprintf(out, "type %s %s\n", e.makeName(o), e.makeType(o.Type().Underlying()))
if _, isInterface := o.Type().Underlying().(*types.Interface); !isInterface {
writeMethods := func(t types.Type) {
methods := types.NewMethodSet(t)
for i := 0; i < methods.Len(); i++ {
m := methods.At(i)
if len(m.Index()) > 1 {
continue // method of embedded field
}
out.Write([]byte("func (? " + e.makeType(m.Recv()) + ") " + e.makeName(m.Obj()) + e.makeSignature(m.Type()) + "\n"))
}
}
writeMethods(o.Type())
writeMethods(types.NewPointer(o.Type()))
}
case *types.Func:
out.Write([]byte("func " + e.makeName(o) + e.makeSignature(o.Type()) + "\n"))
case *types.Const:
optType := ""
basic, isBasic := o.Type().(*types.Basic)
if !isBasic || basic.Info()&types.IsUntyped == 0 {
optType = " " + e.makeType(o.Type())
}
basic = o.Type().Underlying().(*types.Basic)
var val string
switch {
case basic.Info()&types.IsBoolean != 0:
val = strconv.FormatBool(exact.BoolVal(o.Val()))
case basic.Info()&types.IsInteger != 0:
if basic.Kind() == types.Uint64 {
d, _ := exact.Uint64Val(o.Val())
val = fmt.Sprintf("%#x", d)
break
}
d, _ := exact.Int64Val(o.Val())
if basic.Kind() == types.UntypedRune {
switch {
case d < 0 || d > unicode.MaxRune:
val = fmt.Sprintf("('\\x00' + %d)", d)
case d > 0xffff:
val = fmt.Sprintf("'\\U%08x'", d)
default:
val = fmt.Sprintf("'\\u%04x'", d)
}
break
}
val = fmt.Sprintf("%#x", d)
case basic.Info()&types.IsFloat != 0:
f, _ := exact.Float64Val(o.Val())
val = strconv.FormatFloat(f, 'b', -1, 64)
case basic.Info()&types.IsComplex != 0:
r, _ := exact.Float64Val(exact.Real(o.Val()))
i, _ := exact.Float64Val(exact.Imag(o.Val()))
val = fmt.Sprintf("(%s+%si)", strconv.FormatFloat(r, 'b', -1, 64), strconv.FormatFloat(i, 'b', -1, 64))
case basic.Info()&types.IsString != 0:
val = fmt.Sprintf("%#v", exact.StringVal(o.Val()))
default:
panic("Unhandled constant type: " + basic.String())
}
out.Write([]byte("const " + e.makeName(o) + optType + " = " + val + "\n"))
case *types.Var:
out.Write([]byte("var " + e.makeName(o) + " " + e.makeType(o.Type()) + "\n"))
default:
panic(fmt.Sprintf("Unhandled object: %T\n", o))
}
}
fmt.Fprintf(out, "$$\n")
}