OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package eval | 5 package eval |
6 | 6 |
7 import ( | 7 import ( |
8 "big" | 8 "big" |
9 "go/ast" | 9 "go/ast" |
10 "go/token" | 10 "go/token" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 isInteger() bool | 47 isInteger() bool |
48 // isFloat returns true if this is a floating type. | 48 // isFloat returns true if this is a floating type. |
49 isFloat() bool | 49 isFloat() bool |
50 // isIdeal returns true if this is an ideal int or float. | 50 // isIdeal returns true if this is an ideal int or float. |
51 isIdeal() bool | 51 isIdeal() bool |
52 // Zero returns a new zero value of this type. | 52 // Zero returns a new zero value of this type. |
53 Zero() Value | 53 Zero() Value |
54 // String returns the string representation of this type. | 54 // String returns the string representation of this type. |
55 String() string | 55 String() string |
56 // The position where this type was defined, if any. | 56 // The position where this type was defined, if any. |
57 » Pos() token.Position | 57 » Pos() token.Pos |
58 } | 58 } |
59 | 59 |
60 type BoundedType interface { | 60 type BoundedType interface { |
61 Type | 61 Type |
62 // minVal returns the smallest value of this type. | 62 // minVal returns the smallest value of this type. |
63 minVal() *big.Rat | 63 minVal() *big.Rat |
64 // maxVal returns the largest value of this type. | 64 // maxVal returns the largest value of this type. |
65 maxVal() *big.Rat | 65 maxVal() *big.Rat |
66 } | 66 } |
67 | 67 |
68 var universePos = token.Position{"<universe>", 0, 0, 0} | 68 var universePos = token.NoPos |
69 | 69 |
70 /* | 70 /* |
71 * Type array maps. These are used to memoize composite types. | 71 * Type array maps. These are used to memoize composite types. |
72 */ | 72 */ |
73 | 73 |
74 type typeArrayMapEntry struct { | 74 type typeArrayMapEntry struct { |
75 key []Type | 75 key []Type |
76 v interface{} | 76 v interface{} |
77 next *typeArrayMapEntry | 77 next *typeArrayMapEntry |
78 } | 78 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 type commonType struct{} | 133 type commonType struct{} |
134 | 134 |
135 func (commonType) isBoolean() bool { return false } | 135 func (commonType) isBoolean() bool { return false } |
136 | 136 |
137 func (commonType) isInteger() bool { return false } | 137 func (commonType) isInteger() bool { return false } |
138 | 138 |
139 func (commonType) isFloat() bool { return false } | 139 func (commonType) isFloat() bool { return false } |
140 | 140 |
141 func (commonType) isIdeal() bool { return false } | 141 func (commonType) isIdeal() bool { return false } |
142 | 142 |
143 func (commonType) Pos() token.Position { return token.Position{} } | 143 func (commonType) Pos() token.Pos { return token.NoPos } |
144 | 144 |
145 /* | 145 /* |
146 * Bool | 146 * Bool |
147 */ | 147 */ |
148 | 148 |
149 type boolType struct { | 149 type boolType struct { |
150 commonType | 150 commonType |
151 } | 151 } |
152 | 152 |
153 var BoolType = universe.DefineType("bool", universePos, &boolType{}) | 153 var BoolType = universe.DefineType("bool", universePos, &boolType{}) |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 | 365 |
366 // 0 for architecture-dependent type | 366 // 0 for architecture-dependent type |
367 Bits uint | 367 Bits uint |
368 | 368 |
369 name string | 369 name string |
370 } | 370 } |
371 | 371 |
372 var ( | 372 var ( |
373 Float32Type = universe.DefineType("float32", universePos, &floatType{com
monType{}, 32, "float32"}) | 373 Float32Type = universe.DefineType("float32", universePos, &floatType{com
monType{}, 32, "float32"}) |
374 Float64Type = universe.DefineType("float64", universePos, &floatType{com
monType{}, 64, "float64"}) | 374 Float64Type = universe.DefineType("float64", universePos, &floatType{com
monType{}, 64, "float64"}) |
375 FloatType = universe.DefineType("float", universePos, &floatType{commo
nType{}, 0, "float"}) | |
376 ) | 375 ) |
377 | 376 |
378 func (t *floatType) compat(o Type, conv bool) bool { | 377 func (t *floatType) compat(o Type, conv bool) bool { |
379 t2, ok := o.lit().(*floatType) | 378 t2, ok := o.lit().(*floatType) |
380 return ok && t == t2 | 379 return ok && t == t2 |
381 } | 380 } |
382 | 381 |
383 func (t *floatType) lit() Type { return t } | 382 func (t *floatType) lit() Type { return t } |
384 | 383 |
385 func (t *floatType) isFloat() bool { return true } | 384 func (t *floatType) isFloat() bool { return true } |
386 | 385 |
387 func (t *floatType) String() string { return "<" + t.name + ">" } | 386 func (t *floatType) String() string { return "<" + t.name + ">" } |
388 | 387 |
389 func (t *floatType) Zero() Value { | 388 func (t *floatType) Zero() Value { |
390 switch t.Bits { | 389 switch t.Bits { |
391 case 32: | 390 case 32: |
392 res := float32V(0) | 391 res := float32V(0) |
393 return &res | 392 return &res |
394 case 64: | 393 case 64: |
395 res := float64V(0) | 394 res := float64V(0) |
396 return &res | 395 return &res |
397 case 0: | |
398 res := floatV(0) | |
399 return &res | |
400 } | 396 } |
401 panic("unexpected float bit count") | 397 panic("unexpected float bit count") |
402 } | 398 } |
403 | 399 |
404 var maxFloat32Val *big.Rat | 400 var maxFloat32Val *big.Rat |
405 var maxFloat64Val *big.Rat | 401 var maxFloat64Val *big.Rat |
406 var minFloat32Val *big.Rat | 402 var minFloat32Val *big.Rat |
407 var minFloat64Val *big.Rat | 403 var minFloat64Val *big.Rat |
408 | 404 |
409 func (t *floatType) minVal() *big.Rat { | 405 func (t *floatType) minVal() *big.Rat { |
410 bits := t.Bits | 406 bits := t.Bits |
411 if bits == 0 { | |
412 bits = uint(8 * unsafe.Sizeof(float(0))) | |
413 } | |
414 switch bits { | 407 switch bits { |
415 case 32: | 408 case 32: |
416 return minFloat32Val | 409 return minFloat32Val |
417 case 64: | 410 case 64: |
418 return minFloat64Val | 411 return minFloat64Val |
419 } | 412 } |
420 log.Panicf("unexpected floating point bit count: %d", bits) | 413 log.Panicf("unexpected floating point bit count: %d", bits) |
421 panic("unreachable") | 414 panic("unreachable") |
422 } | 415 } |
423 | 416 |
424 func (t *floatType) maxVal() *big.Rat { | 417 func (t *floatType) maxVal() *big.Rat { |
425 bits := t.Bits | 418 bits := t.Bits |
426 if bits == 0 { | |
427 bits = uint(8 * unsafe.Sizeof(float(0))) | |
428 } | |
429 switch bits { | 419 switch bits { |
430 case 32: | 420 case 32: |
431 return maxFloat32Val | 421 return maxFloat32Val |
432 case 64: | 422 case 64: |
433 return maxFloat64Val | 423 return maxFloat64Val |
434 } | 424 } |
435 log.Panicf("unexpected floating point bit count: %d", bits) | 425 log.Panicf("unexpected floating point bit count: %d", bits) |
436 panic("unreachable") | 426 panic("unreachable") |
437 } | 427 } |
438 | 428 |
(...skipping 654 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1093 /* | 1083 /* |
1094 * Named types | 1084 * Named types |
1095 */ | 1085 */ |
1096 | 1086 |
1097 type Method struct { | 1087 type Method struct { |
1098 decl *FuncDecl | 1088 decl *FuncDecl |
1099 fn Func | 1089 fn Func |
1100 } | 1090 } |
1101 | 1091 |
1102 type NamedType struct { | 1092 type NamedType struct { |
1103 » token.Position | 1093 » NamePos token.Pos |
1104 » Name string | 1094 » Name string |
1105 // Underlying type. If incomplete is true, this will be nil. | 1095 // Underlying type. If incomplete is true, this will be nil. |
1106 // If incomplete is false and this is still nil, then this is | 1096 // If incomplete is false and this is still nil, then this is |
1107 // a placeholder type representing an error. | 1097 // a placeholder type representing an error. |
1108 Def Type | 1098 Def Type |
1109 // True while this type is being defined. | 1099 // True while this type is being defined. |
1110 incomplete bool | 1100 incomplete bool |
1111 methods map[string]Method | 1101 methods map[string]Method |
1112 } | 1102 } |
1113 | 1103 |
1114 // TODO(austin) This is temporarily needed by the debugger's remote | 1104 // TODO(austin) This is temporarily needed by the debugger's remote |
1115 // type parser. This should only be possible with block.DefineType. | 1105 // type parser. This should only be possible with block.DefineType. |
1116 func NewNamedType(name string) *NamedType { | 1106 func NewNamedType(name string) *NamedType { |
1117 » return &NamedType{token.Position{}, name, nil, true, make(map[string]Met
hod)} | 1107 » return &NamedType{token.NoPos, name, nil, true, make(map[string]Method)} |
| 1108 } |
| 1109 |
| 1110 func (t *NamedType) Pos() token.Pos { |
| 1111 » return t.NamePos |
1118 } | 1112 } |
1119 | 1113 |
1120 func (t *NamedType) Complete(def Type) { | 1114 func (t *NamedType) Complete(def Type) { |
1121 if !t.incomplete { | 1115 if !t.incomplete { |
1122 log.Panicf("cannot complete already completed NamedType %+v", *t
) | 1116 log.Panicf("cannot complete already completed NamedType %+v", *t
) |
1123 } | 1117 } |
1124 // We strip the name from def because multiple levels of | 1118 // We strip the name from def because multiple levels of |
1125 // naming are useless. | 1119 // naming are useless. |
1126 if ndef, ok := def.(*NamedType); ok { | 1120 if ndef, ok := def.(*NamedType); ok { |
1127 def = ndef.Def | 1121 def = ndef.Def |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1249 universe.DefineConst("close", universePos, closeType, nil) | 1243 universe.DefineConst("close", universePos, closeType, nil) |
1250 universe.DefineConst("closed", universePos, closedType, nil) | 1244 universe.DefineConst("closed", universePos, closedType, nil) |
1251 universe.DefineConst("copy", universePos, copyType, nil) | 1245 universe.DefineConst("copy", universePos, copyType, nil) |
1252 universe.DefineConst("len", universePos, lenType, nil) | 1246 universe.DefineConst("len", universePos, lenType, nil) |
1253 universe.DefineConst("make", universePos, makeType, nil) | 1247 universe.DefineConst("make", universePos, makeType, nil) |
1254 universe.DefineConst("new", universePos, newType, nil) | 1248 universe.DefineConst("new", universePos, newType, nil) |
1255 universe.DefineConst("panic", universePos, panicType, nil) | 1249 universe.DefineConst("panic", universePos, panicType, nil) |
1256 universe.DefineConst("print", universePos, printType, nil) | 1250 universe.DefineConst("print", universePos, printType, nil) |
1257 universe.DefineConst("println", universePos, printlnType, nil) | 1251 universe.DefineConst("println", universePos, printlnType, nil) |
1258 } | 1252 } |
OLD | NEW |