本文整理匯總了Golang中code/google/com/p/go/tools/go/exact.Imag函數的典型用法代碼示例。如果您正苦於以下問題:Golang Imag函數的具體用法?Golang Imag怎麽用?Golang Imag使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Imag函數的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: value
func (p *exporter) value(x exact.Value) {
if trace {
p.tracef("value { ")
defer p.tracef("} ")
}
switch kind := x.Kind(); kind {
case exact.Bool:
tag := falseTag
if exact.BoolVal(x) {
tag = trueTag
}
p.int(tag)
case exact.Int:
if i, ok := exact.Int64Val(x); ok {
p.int(int64Tag)
p.int64(i)
return
}
p.int(floatTag)
p.float(x)
case exact.Float:
p.int(fractionTag)
p.fraction(x)
case exact.Complex:
p.int(complexTag)
p.fraction(exact.Real(x))
p.fraction(exact.Imag(x))
case exact.String:
p.int(stringTag)
p.string(exact.StringVal(x))
default:
panic(fmt.Sprintf("unexpected value kind %d", kind))
}
}
示例2: valString
// valString returns the string representation for the value v.
// Setting floatFmt forces an integer value to be formatted in
// normalized floating-point format.
// TODO(gri) Move this code into package exact.
func valString(v exact.Value, floatFmt bool) string {
switch v.Kind() {
case exact.Int:
if floatFmt {
return floatString(v)
}
case exact.Float:
return floatString(v)
case exact.Complex:
re := exact.Real(v)
im := exact.Imag(v)
var s string
if exact.Sign(re) != 0 {
s = floatString(re)
if exact.Sign(im) >= 0 {
s += " + "
} else {
s += " - "
im = exact.UnaryOp(token.SUB, im, 0) // negate im
}
}
// im != 0, otherwise v would be exact.Int or exact.Float
return s + floatString(im) + "i"
}
return v.String()
}
示例3: Value
func (cdd *CDD) Value(w *bytes.Buffer, ev exact.Value, t types.Type) {
k := t.Underlying().(*types.Basic).Kind()
// TODO: use t instead ev.Kind() in following switch
switch ev.Kind() {
case exact.Int:
writeInt(w, ev, k)
case exact.Float:
writeFloat(w, ev, k)
case exact.Complex:
switch k {
case types.Complex64:
k = types.Float32
case types.Complex128:
k = types.Float64
default:
k = types.UntypedFloat
}
writeFloat(w, exact.Real(ev), k)
im := exact.Imag(ev)
if exact.Sign(im) != -1 {
w.WriteByte('+')
}
writeFloat(w, im, k)
w.WriteByte('i')
case exact.String:
w.WriteString("EGSTR(")
w.WriteString(ev.String())
w.WriteByte(')')
default:
w.WriteString(ev.String())
}
}
示例4: representableConst
//.........這裏部分代碼省略.........
return 0 <= x && x <= 1<<s-1
case Uint32:
const s = 32
return 0 <= x && x <= 1<<s-1
case Uint64:
return 0 <= x
case Float32, Float64, Complex64, Complex128,
UntypedInt, UntypedFloat, UntypedComplex:
return true
}
}
n := exact.BitLen(x)
switch as {
case Uint, Uintptr:
var s = uint(conf.sizeof(Typ[as])) * 8
return exact.Sign(x) >= 0 && n <= int(s)
case Uint64:
return exact.Sign(x) >= 0 && n <= 64
case Float32, Complex64:
if rounded == nil {
return fitsFloat32(x)
}
r := roundFloat32(x)
if r != nil {
*rounded = r
return true
}
case Float64, Complex128:
if rounded == nil {
return fitsFloat64(x)
}
r := roundFloat64(x)
if r != nil {
*rounded = r
return true
}
case UntypedInt, UntypedFloat, UntypedComplex:
return true
}
case exact.Float:
switch as {
case Float32, Complex64:
if rounded == nil {
return fitsFloat32(x)
}
r := roundFloat32(x)
if r != nil {
*rounded = r
return true
}
case Float64, Complex128:
if rounded == nil {
return fitsFloat64(x)
}
r := roundFloat64(x)
if r != nil {
*rounded = r
return true
}
case UntypedFloat, UntypedComplex:
return true
}
case exact.Complex:
switch as {
case Complex64:
if rounded == nil {
return fitsFloat32(exact.Real(x)) && fitsFloat32(exact.Imag(x))
}
re := roundFloat32(exact.Real(x))
im := roundFloat32(exact.Imag(x))
if re != nil && im != nil {
*rounded = exact.BinaryOp(re, token.ADD, exact.MakeImag(im))
return true
}
case Complex128:
if rounded == nil {
return fitsFloat64(exact.Real(x)) && fitsFloat64(exact.Imag(x))
}
re := roundFloat64(exact.Real(x))
im := roundFloat64(exact.Imag(x))
if re != nil && im != nil {
*rounded = exact.BinaryOp(re, token.ADD, exact.MakeImag(im))
return true
}
case UntypedComplex:
return true
}
case exact.String:
return as == String || as == UntypedString
default:
unreachable()
}
return false
}
示例5: Complex128
// Complex128 returns the complex value of this constant truncated to
// fit a complex128.
//
func (c *Const) Complex128() complex128 {
re, _ := exact.Float64Val(exact.Real(c.Value))
im, _ := exact.Float64Val(exact.Imag(c.Value))
return complex(re, im)
}
示例6: 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))
}
示例7: Complex128
// Complex128 returns the complex value of this literal truncated to
// fit a complex128.
//
func (l *Literal) Complex128() complex128 {
re, _ := exact.Float64Val(exact.Real(l.Value))
im, _ := exact.Float64Val(exact.Imag(l.Value))
return complex(re, im)
}
示例8: builtin
//.........這裏部分代碼省略.........
check.recordBuiltinType(call.Fun, makeSig(nil, c))
}
case _Complex:
// complex(x, y realT) complexT
if !check.complexArg(x) {
return
}
var y operand
arg(&y, 1)
if y.mode == invalid {
return
}
if !check.complexArg(&y) {
return
}
check.convertUntyped(x, y.typ)
if x.mode == invalid {
return
}
check.convertUntyped(&y, x.typ)
if y.mode == invalid {
return
}
if !Identical(x.typ, y.typ) {
check.invalidArg(x.pos(), "mismatched types %s and %s", x.typ, y.typ)
return
}
if x.mode == constant && y.mode == constant {
x.val = exact.BinaryOp(x.val, token.ADD, exact.MakeImag(y.val))
} else {
x.mode = value
}
realT := x.typ
complexT := Typ[Invalid]
switch realT.Underlying().(*Basic).kind {
case Float32:
complexT = Typ[Complex64]
case Float64:
complexT = Typ[Complex128]
case UntypedInt, UntypedRune, UntypedFloat:
if x.mode == constant {
realT = defaultType(realT).(*Basic)
complexT = Typ[UntypedComplex]
} else {
// untyped but not constant; probably because one
// operand is a non-constant shift of untyped lhs
realT = Typ[Float64]
complexT = Typ[Complex128]
}
default:
check.invalidArg(x.pos(), "float32 or float64 arguments expected")
return
}
x.typ = complexT
if check.Types != nil && x.mode != constant {
check.recordBuiltinType(call.Fun, makeSig(complexT, realT, realT))
}
if x.mode != constant {
示例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.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 {
//.........這裏部分代碼省略.........
示例10: formatExprInternal
//.........這裏部分代碼省略.........
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))
return
}
out.WriteString(strconv.FormatUint(d>>32, 10))
return
}
writeExpr(".$high")
case 'l':
if val := c.p.info.Types[a[n].(ast.Expr)].Value; val != nil {
d, _ := exact.Uint64Val(val)
out.WriteString(strconv.FormatUint(d&(1<<32-1), 10))
return
}
writeExpr(".$low")
case 'r':
if val := c.p.info.Types[a[n].(ast.Expr)].Value; val != nil {
r, _ := exact.Float64Val(exact.Real(val))
out.WriteString(strconv.FormatFloat(r, 'g', -1, 64))
return
}
writeExpr(".$real")
case 'i':
if val := c.p.info.Types[a[n].(ast.Expr)].Value; val != nil {
i, _ := exact.Float64Val(exact.Imag(val))
out.WriteString(strconv.FormatFloat(i, 'g', -1, 64))
return
}
writeExpr(".$imag")
case '%':
out.WriteRune('%')
default:
panic(fmt.Sprintf("formatExpr: %%%c%d", k, n))
}
})
if hasAssignments {
out.WriteByte(')')
}
return &expression{str: out.String(), parens: parens}
}
示例11: 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:
//.........這裏部分代碼省略.........
示例12: 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 {
//.........這裏部分代碼省略.........
示例13: builtin
//.........這裏部分代碼省略.........
}
x.mode = novalue
case _Complex:
if !check.complexArg(x) {
goto Error
}
var y operand
check.expr(&y, args[1])
if y.mode == invalid {
goto Error
}
if !check.complexArg(&y) {
goto Error
}
check.convertUntyped(x, y.typ)
if x.mode == invalid {
goto Error
}
check.convertUntyped(&y, x.typ)
if y.mode == invalid {
goto Error
}
if !IsIdentical(x.typ, y.typ) {
check.invalidArg(x.pos(), "mismatched types %s and %s", x.typ, y.typ)
goto Error
}
typ := x.typ.Underlying().(*Basic)
if x.mode == constant && y.mode == constant {
x.val = exact.BinaryOp(x.val, token.ADD, exact.MakeImag(y.val))
} else {
x.mode = value
}
switch typ.kind {
case Float32:
x.typ = Typ[Complex64]
case Float64:
x.typ = Typ[Complex128]
case UntypedInt, UntypedRune, UntypedFloat:
if x.mode == constant {
typ = defaultType(typ).(*Basic)
x.typ = Typ[UntypedComplex]
} else {
// untyped but not constant; probably because one
// operand is a non-constant shift of untyped lhs
typ = Typ[Float64]
x.typ = Typ[Complex128]
}
default:
check.invalidArg(x.pos(), "float32 or float64 arguments expected")
goto Error
}
if x.mode != constant {
// The arguments have now their final types, which at run-
// time will be materialized. Update the expression trees.
// If the current types are untyped, the materialized type
// is the respective default type.
// (If the result is constant, the arguments are never
// materialized and there is nothing to do.)
check.updateExprType(args[0], typ, true)
示例14: 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")
}