Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libgo / go / exp / eval / expr.go
blob823f240188c6583d833d895c915904d9274472a3
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 package eval
7 import (
8 "big"
9 "fmt"
10 "go/ast"
11 "go/token"
12 "log"
13 "strconv"
14 "strings"
15 "os"
18 var (
19 idealZero = big.NewInt(0)
20 idealOne = big.NewInt(1)
23 // An expr is the result of compiling an expression. It stores the
24 // type of the expression and its evaluator function.
25 type expr struct {
26 *exprInfo
27 t Type
29 // Evaluate this node as the given type.
30 eval interface{}
32 // Map index expressions permit special forms of assignment,
33 // for which we need to know the Map and key.
34 evalMapValue func(t *Thread) (Map, interface{})
36 // Evaluate to the "address of" this value; that is, the
37 // settable Value object. nil for expressions whose address
38 // cannot be taken.
39 evalAddr func(t *Thread) Value
41 // Execute this expression as a statement. Only expressions
42 // that are valid expression statements should set this.
43 exec func(t *Thread)
45 // If this expression is a type, this is its compiled type.
46 // This is only permitted in the function position of a call
47 // expression. In this case, t should be nil.
48 valType Type
50 // A short string describing this expression for error
51 // messages.
52 desc string
55 // exprInfo stores information needed to compile any expression node.
56 // Each expr also stores its exprInfo so further expressions can be
57 // compiled from it.
58 type exprInfo struct {
59 *compiler
60 pos token.Position
63 func (a *exprInfo) newExpr(t Type, desc string) *expr {
64 return &expr{exprInfo: a, t: t, desc: desc}
67 func (a *exprInfo) diag(format string, args ...interface{}) {
68 a.diagAt(&a.pos, format, args...)
71 func (a *exprInfo) diagOpType(op token.Token, vt Type) {
72 a.diag("illegal operand type for '%v' operator\n\t%v", op, vt)
75 func (a *exprInfo) diagOpTypes(op token.Token, lt Type, rt Type) {
76 a.diag("illegal operand types for '%v' operator\n\t%v\n\t%v", op, lt, rt)
80 * Common expression manipulations
83 // a.convertTo(t) converts the value of the analyzed expression a,
84 // which must be a constant, ideal number, to a new analyzed
85 // expression with a constant value of type t.
87 // TODO(austin) Rename to resolveIdeal or something?
88 func (a *expr) convertTo(t Type) *expr {
89 if !a.t.isIdeal() {
90 log.Panicf("attempted to convert from %v, expected ideal", a.t)
93 var rat *big.Rat
95 // XXX(Spec) The spec says "It is erroneous".
97 // It is an error to assign a value with a non-zero fractional
98 // part to an integer, or if the assignment would overflow or
99 // underflow, or in general if the value cannot be represented
100 // by the type of the variable.
101 switch a.t {
102 case IdealFloatType:
103 rat = a.asIdealFloat()()
104 if t.isInteger() && !rat.IsInt() {
105 a.diag("constant %v truncated to integer", rat.FloatString(6))
106 return nil
108 case IdealIntType:
109 i := a.asIdealInt()()
110 rat = new(big.Rat).SetInt(i)
111 default:
112 log.Panicf("unexpected ideal type %v", a.t)
115 // Check bounds
116 if t, ok := t.lit().(BoundedType); ok {
117 if rat.Cmp(t.minVal()) < 0 {
118 a.diag("constant %v underflows %v", rat.FloatString(6), t)
119 return nil
121 if rat.Cmp(t.maxVal()) > 0 {
122 a.diag("constant %v overflows %v", rat.FloatString(6), t)
123 return nil
127 // Convert rat to type t.
128 res := a.newExpr(t, a.desc)
129 switch t := t.lit().(type) {
130 case *uintType:
131 n, d := rat.Num(), rat.Denom()
132 f := new(big.Int).Quo(n, d)
133 f = f.Abs(f)
134 v := uint64(f.Int64())
135 res.eval = func(*Thread) uint64 { return v }
136 case *intType:
137 n, d := rat.Num(), rat.Denom()
138 f := new(big.Int).Quo(n, d)
139 v := f.Int64()
140 res.eval = func(*Thread) int64 { return v }
141 case *idealIntType:
142 n, d := rat.Num(), rat.Denom()
143 f := new(big.Int).Quo(n, d)
144 res.eval = func() *big.Int { return f }
145 case *floatType:
146 n, d := rat.Num(), rat.Denom()
147 v := float64(n.Int64()) / float64(d.Int64())
148 res.eval = func(*Thread) float64 { return v }
149 case *idealFloatType:
150 res.eval = func() *big.Rat { return rat }
151 default:
152 log.Panicf("cannot convert to type %T", t)
155 return res
158 // convertToInt converts this expression to an integer, if possible,
159 // or produces an error if not. This accepts ideal ints, uints, and
160 // ints. If max is not -1, produces an error if possible if the value
161 // exceeds max. If negErr is not "", produces an error if possible if
162 // the value is negative.
163 func (a *expr) convertToInt(max int64, negErr string, errOp string) *expr {
164 switch a.t.lit().(type) {
165 case *idealIntType:
166 val := a.asIdealInt()()
167 if negErr != "" && val.Sign() < 0 {
168 a.diag("negative %s: %s", negErr, val)
169 return nil
171 bound := max
172 if negErr == "slice" {
173 bound++
175 if max != -1 && val.Cmp(big.NewInt(bound)) >= 0 {
176 a.diag("index %s exceeds length %d", val, max)
177 return nil
179 return a.convertTo(IntType)
181 case *uintType:
182 // Convert to int
183 na := a.newExpr(IntType, a.desc)
184 af := a.asUint()
185 na.eval = func(t *Thread) int64 { return int64(af(t)) }
186 return na
188 case *intType:
189 // Good as is
190 return a
193 a.diag("illegal operand type for %s\n\t%v", errOp, a.t)
194 return nil
197 // derefArray returns an expression of array type if the given
198 // expression is a *array type. Otherwise, returns the given
199 // expression.
200 func (a *expr) derefArray() *expr {
201 if pt, ok := a.t.lit().(*PtrType); ok {
202 if _, ok := pt.Elem.lit().(*ArrayType); ok {
203 deref := a.compileStarExpr(a)
204 if deref == nil {
205 log.Panicf("failed to dereference *array")
207 return deref
210 return a
214 * Assignments
217 // An assignCompiler compiles assignment operations. Anything other
218 // than short declarations should use the compileAssign wrapper.
220 // There are three valid types of assignment:
221 // 1) T = T
222 // Assigning a single expression with single-valued type to a
223 // single-valued type.
224 // 2) MT = T, T, ...
225 // Assigning multiple expressions with single-valued types to a
226 // multi-valued type.
227 // 3) MT = MT
228 // Assigning a single expression with multi-valued type to a
229 // multi-valued type.
230 type assignCompiler struct {
231 *compiler
232 pos token.Position
233 // The RHS expressions. This may include nil's for
234 // expressions that failed to compile.
235 rs []*expr
236 // The (possibly unary) MultiType of the RHS.
237 rmt *MultiType
238 // Whether this is an unpack assignment (case 3).
239 isUnpack bool
240 // Whether map special assignment forms are allowed.
241 allowMap bool
242 // Whether this is a "r, ok = a[x]" assignment.
243 isMapUnpack bool
244 // The operation name to use in error messages, such as
245 // "assignment" or "function call".
246 errOp string
247 // The name to use for positions in error messages, such as
248 // "argument".
249 errPosName string
252 // Type check the RHS of an assignment, returning a new assignCompiler
253 // and indicating if the type check succeeded. This always returns an
254 // assignCompiler with rmt set, but if type checking fails, slots in
255 // the MultiType may be nil. If rs contains nil's, type checking will
256 // fail and these expressions given a nil type.
257 func (a *compiler) checkAssign(pos token.Position, rs []*expr, errOp, errPosName string) (*assignCompiler, bool) {
258 c := &assignCompiler{
259 compiler: a,
260 pos: pos,
261 rs: rs,
262 errOp: errOp,
263 errPosName: errPosName,
266 // Is this an unpack?
267 if len(rs) == 1 && rs[0] != nil {
268 if rmt, isUnpack := rs[0].t.(*MultiType); isUnpack {
269 c.rmt = rmt
270 c.isUnpack = true
271 return c, true
275 // Create MultiType for RHS and check that all RHS expressions
276 // are single-valued.
277 rts := make([]Type, len(rs))
278 ok := true
279 for i, r := range rs {
280 if r == nil {
281 ok = false
282 continue
285 if _, isMT := r.t.(*MultiType); isMT {
286 r.diag("multi-valued expression not allowed in %s", errOp)
287 ok = false
288 continue
291 rts[i] = r.t
294 c.rmt = NewMultiType(rts)
295 return c, ok
298 func (a *assignCompiler) allowMapForms(nls int) {
299 a.allowMap = true
301 // Update unpacking info if this is r, ok = a[x]
302 if nls == 2 && len(a.rs) == 1 && a.rs[0] != nil && a.rs[0].evalMapValue != nil {
303 a.isUnpack = true
304 a.rmt = NewMultiType([]Type{a.rs[0].t, BoolType})
305 a.isMapUnpack = true
309 // compile type checks and compiles an assignment operation, returning
310 // a function that expects an l-value and the frame in which to
311 // evaluate the RHS expressions. The l-value must have exactly the
312 // type given by lt. Returns nil if type checking fails.
313 func (a *assignCompiler) compile(b *block, lt Type) func(Value, *Thread) {
314 lmt, isMT := lt.(*MultiType)
315 rmt, isUnpack := a.rmt, a.isUnpack
317 // Create unary MultiType for single LHS
318 if !isMT {
319 lmt = NewMultiType([]Type{lt})
322 // Check that the assignment count matches
323 lcount := len(lmt.Elems)
324 rcount := len(rmt.Elems)
325 if lcount != rcount {
326 msg := "not enough"
327 pos := a.pos
328 if rcount > lcount {
329 msg = "too many"
330 if lcount > 0 {
331 pos = a.rs[lcount-1].pos
334 a.diagAt(&pos, "%s %ss for %s\n\t%s\n\t%s", msg, a.errPosName, a.errOp, lt, rmt)
335 return nil
338 bad := false
340 // If this is an unpack, create a temporary to store the
341 // multi-value and replace the RHS with expressions to pull
342 // out values from the temporary. Technically, this is only
343 // necessary when we need to perform assignment conversions.
344 var effect func(*Thread)
345 if isUnpack {
346 // This leaks a slot, but is definitely safe.
347 temp := b.DefineTemp(a.rmt)
348 tempIdx := temp.Index
349 if tempIdx < 0 {
350 panic(fmt.Sprintln("tempidx", tempIdx))
352 if a.isMapUnpack {
353 rf := a.rs[0].evalMapValue
354 vt := a.rmt.Elems[0]
355 effect = func(t *Thread) {
356 m, k := rf(t)
357 v := m.Elem(t, k)
358 found := boolV(true)
359 if v == nil {
360 found = boolV(false)
361 v = vt.Zero()
363 t.f.Vars[tempIdx] = multiV([]Value{v, &found})
365 } else {
366 rf := a.rs[0].asMulti()
367 effect = func(t *Thread) { t.f.Vars[tempIdx] = multiV(rf(t)) }
369 orig := a.rs[0]
370 a.rs = make([]*expr, len(a.rmt.Elems))
371 for i, t := range a.rmt.Elems {
372 if t.isIdeal() {
373 log.Panicf("Right side of unpack contains ideal: %s", rmt)
375 a.rs[i] = orig.newExpr(t, orig.desc)
376 index := i
377 a.rs[i].genValue(func(t *Thread) Value { return t.f.Vars[tempIdx].(multiV)[index] })
380 // Now len(a.rs) == len(a.rmt) and we've reduced any unpacking
381 // to multi-assignment.
383 // TODO(austin) Deal with assignment special cases.
385 // Values of any type may always be assigned to variables of
386 // compatible static type.
387 for i, lt := range lmt.Elems {
388 rt := rmt.Elems[i]
390 // When [an ideal is] (used in an expression) assigned
391 // to a variable or typed constant, the destination
392 // must be able to represent the assigned value.
393 if rt.isIdeal() {
394 a.rs[i] = a.rs[i].convertTo(lmt.Elems[i])
395 if a.rs[i] == nil {
396 bad = true
397 continue
399 rt = a.rs[i].t
402 // A pointer p to an array can be assigned to a slice
403 // variable v with compatible element type if the type
404 // of p or v is unnamed.
405 if rpt, ok := rt.lit().(*PtrType); ok {
406 if at, ok := rpt.Elem.lit().(*ArrayType); ok {
407 if lst, ok := lt.lit().(*SliceType); ok {
408 if lst.Elem.compat(at.Elem, false) && (rt.lit() == Type(rt) || lt.lit() == Type(lt)) {
409 rf := a.rs[i].asPtr()
410 a.rs[i] = a.rs[i].newExpr(lt, a.rs[i].desc)
411 len := at.Len
412 a.rs[i].eval = func(t *Thread) Slice { return Slice{rf(t).(ArrayValue), len, len} }
413 rt = a.rs[i].t
419 if !lt.compat(rt, false) {
420 if len(a.rs) == 1 {
421 a.rs[0].diag("illegal operand types for %s\n\t%v\n\t%v", a.errOp, lt, rt)
422 } else {
423 a.rs[i].diag("illegal operand types in %s %d of %s\n\t%v\n\t%v", a.errPosName, i+1, a.errOp, lt, rt)
425 bad = true
428 if bad {
429 return nil
432 // Compile
433 if !isMT {
434 // Case 1
435 return genAssign(lt, a.rs[0])
437 // Case 2 or 3
438 as := make([]func(lv Value, t *Thread), len(a.rs))
439 for i, r := range a.rs {
440 as[i] = genAssign(lmt.Elems[i], r)
442 return func(lv Value, t *Thread) {
443 if effect != nil {
444 effect(t)
446 lmv := lv.(multiV)
447 for i, a := range as {
448 a(lmv[i], t)
453 // compileAssign compiles an assignment operation without the full
454 // generality of an assignCompiler. See assignCompiler for a
455 // description of the arguments.
456 func (a *compiler) compileAssign(pos token.Position, b *block, lt Type, rs []*expr, errOp, errPosName string) func(Value, *Thread) {
457 ac, ok := a.checkAssign(pos, rs, errOp, errPosName)
458 if !ok {
459 return nil
461 return ac.compile(b, lt)
465 * Expression compiler
468 // An exprCompiler stores information used throughout the compilation
469 // of a single expression. It does not embed funcCompiler because
470 // expressions can appear at top level.
471 type exprCompiler struct {
472 *compiler
473 // The block this expression is being compiled in.
474 block *block
475 // Whether this expression is used in a constant context.
476 constant bool
479 // compile compiles an expression AST. callCtx should be true if this
480 // AST is in the function position of a function call node; it allows
481 // the returned expression to be a type or a built-in function (which
482 // otherwise result in errors).
483 func (a *exprCompiler) compile(x ast.Expr, callCtx bool) *expr {
484 ei := &exprInfo{a.compiler, x.Pos()}
486 switch x := x.(type) {
487 // Literals
488 case *ast.BasicLit:
489 switch x.Kind {
490 case token.INT:
491 return ei.compileIntLit(string(x.Value))
492 case token.FLOAT:
493 return ei.compileFloatLit(string(x.Value))
494 case token.CHAR:
495 return ei.compileCharLit(string(x.Value))
496 case token.STRING:
497 return ei.compileStringLit(string(x.Value))
498 default:
499 log.Panicf("unexpected basic literal type %v", x.Kind)
502 case *ast.CompositeLit:
503 goto notimpl
505 case *ast.FuncLit:
506 decl := ei.compileFuncType(a.block, x.Type)
507 if decl == nil {
508 // TODO(austin) Try compiling the body,
509 // perhaps with dummy argument definitions
510 return nil
512 fn := ei.compileFunc(a.block, decl, x.Body)
513 if fn == nil {
514 return nil
516 if a.constant {
517 a.diagAt(x, "function literal used in constant expression")
518 return nil
520 return ei.compileFuncLit(decl, fn)
522 // Types
523 case *ast.ArrayType:
524 // TODO(austin) Use a multi-type case
525 goto typeexpr
527 case *ast.ChanType:
528 goto typeexpr
530 case *ast.Ellipsis:
531 goto typeexpr
533 case *ast.FuncType:
534 goto typeexpr
536 case *ast.InterfaceType:
537 goto typeexpr
539 case *ast.MapType:
540 goto typeexpr
542 // Remaining expressions
543 case *ast.BadExpr:
544 // Error already reported by parser
545 a.silentErrors++
546 return nil
548 case *ast.BinaryExpr:
549 l, r := a.compile(x.X, false), a.compile(x.Y, false)
550 if l == nil || r == nil {
551 return nil
553 return ei.compileBinaryExpr(x.Op, l, r)
555 case *ast.CallExpr:
556 l := a.compile(x.Fun, true)
557 args := make([]*expr, len(x.Args))
558 bad := false
559 for i, arg := range x.Args {
560 if i == 0 && l != nil && (l.t == Type(makeType) || l.t == Type(newType)) {
561 argei := &exprInfo{a.compiler, arg.Pos()}
562 args[i] = argei.exprFromType(a.compileType(a.block, arg))
563 } else {
564 args[i] = a.compile(arg, false)
566 if args[i] == nil {
567 bad = true
570 if bad || l == nil {
571 return nil
573 if a.constant {
574 a.diagAt(x, "function call in constant context")
575 return nil
578 if l.valType != nil {
579 a.diagAt(x, "type conversions not implemented")
580 return nil
581 } else if ft, ok := l.t.(*FuncType); ok && ft.builtin != "" {
582 return ei.compileBuiltinCallExpr(a.block, ft, args)
583 } else {
584 return ei.compileCallExpr(a.block, l, args)
587 case *ast.Ident:
588 return ei.compileIdent(a.block, a.constant, callCtx, x.Name)
590 case *ast.IndexExpr:
591 l, r := a.compile(x.X, false), a.compile(x.Index, false)
592 if l == nil || r == nil {
593 return nil
595 return ei.compileIndexExpr(l, r)
597 case *ast.SliceExpr:
598 var hi *expr
599 arr := a.compile(x.X, false)
600 lo := a.compile(x.Index, false)
601 if x.End == nil {
602 // End was omitted, so we need to compute len(x.X)
603 ei := &exprInfo{a.compiler, x.Pos()}
604 hi = ei.compileBuiltinCallExpr(a.block, lenType, []*expr{arr})
605 } else {
606 hi = a.compile(x.End, false)
608 if arr == nil || lo == nil || hi == nil {
609 return nil
611 return ei.compileSliceExpr(arr, lo, hi)
613 case *ast.KeyValueExpr:
614 goto notimpl
616 case *ast.ParenExpr:
617 return a.compile(x.X, callCtx)
619 case *ast.SelectorExpr:
620 v := a.compile(x.X, false)
621 if v == nil {
622 return nil
624 return ei.compileSelectorExpr(v, x.Sel.Name)
626 case *ast.StarExpr:
627 // We pass down our call context because this could be
628 // a pointer type (and thus a type conversion)
629 v := a.compile(x.X, callCtx)
630 if v == nil {
631 return nil
633 if v.valType != nil {
634 // Turns out this was a pointer type, not a dereference
635 return ei.exprFromType(NewPtrType(v.valType))
637 return ei.compileStarExpr(v)
639 case *ast.StructType:
640 goto notimpl
642 case *ast.TypeAssertExpr:
643 goto notimpl
645 case *ast.UnaryExpr:
646 v := a.compile(x.X, false)
647 if v == nil {
648 return nil
650 return ei.compileUnaryExpr(x.Op, v)
652 log.Panicf("unexpected ast node type %T", x)
653 panic("unreachable")
655 typeexpr:
656 if !callCtx {
657 a.diagAt(x, "type used as expression")
658 return nil
660 return ei.exprFromType(a.compileType(a.block, x))
662 notimpl:
663 a.diagAt(x, "%T expression node not implemented", x)
664 return nil
667 func (a *exprInfo) exprFromType(t Type) *expr {
668 if t == nil {
669 return nil
671 expr := a.newExpr(nil, "type")
672 expr.valType = t
673 return expr
676 func (a *exprInfo) compileIdent(b *block, constant bool, callCtx bool, name string) *expr {
677 bl, level, def := b.Lookup(name)
678 if def == nil {
679 a.diag("%s: undefined", name)
680 return nil
682 switch def := def.(type) {
683 case *Constant:
684 expr := a.newExpr(def.Type, "constant")
685 if ft, ok := def.Type.(*FuncType); ok && ft.builtin != "" {
686 // XXX(Spec) I don't think anything says that
687 // built-in functions can't be used as values.
688 if !callCtx {
689 a.diag("built-in function %s cannot be used as a value", ft.builtin)
690 return nil
692 // Otherwise, we leave the evaluators empty
693 // because this is handled specially
694 } else {
695 expr.genConstant(def.Value)
697 return expr
698 case *Variable:
699 if constant {
700 a.diag("variable %s used in constant expression", name)
701 return nil
703 if bl.global {
704 return a.compileGlobalVariable(def)
706 return a.compileVariable(level, def)
707 case Type:
708 if callCtx {
709 return a.exprFromType(def)
711 a.diag("type %v used as expression", name)
712 return nil
714 log.Panicf("name %s has unknown type %T", name, def)
715 panic("unreachable")
718 func (a *exprInfo) compileVariable(level int, v *Variable) *expr {
719 if v.Type == nil {
720 // Placeholder definition from an earlier error
721 a.silentErrors++
722 return nil
724 expr := a.newExpr(v.Type, "variable")
725 expr.genIdentOp(level, v.Index)
726 return expr
729 func (a *exprInfo) compileGlobalVariable(v *Variable) *expr {
730 if v.Type == nil {
731 // Placeholder definition from an earlier error
732 a.silentErrors++
733 return nil
735 if v.Init == nil {
736 v.Init = v.Type.Zero()
738 expr := a.newExpr(v.Type, "variable")
739 val := v.Init
740 expr.genValue(func(t *Thread) Value { return val })
741 return expr
744 func (a *exprInfo) compileIdealInt(i *big.Int, desc string) *expr {
745 expr := a.newExpr(IdealIntType, desc)
746 expr.eval = func() *big.Int { return i }
747 return expr
750 func (a *exprInfo) compileIntLit(lit string) *expr {
751 i, _ := new(big.Int).SetString(lit, 0)
752 return a.compileIdealInt(i, "integer literal")
755 func (a *exprInfo) compileCharLit(lit string) *expr {
756 if lit[0] != '\'' {
757 // Caught by parser
758 a.silentErrors++
759 return nil
761 v, _, tail, err := strconv.UnquoteChar(lit[1:], '\'')
762 if err != nil || tail != "'" {
763 // Caught by parser
764 a.silentErrors++
765 return nil
767 return a.compileIdealInt(big.NewInt(int64(v)), "character literal")
770 func (a *exprInfo) compileFloatLit(lit string) *expr {
771 f, ok := new(big.Rat).SetString(lit)
772 if !ok {
773 log.Panicf("malformed float literal %s at %v passed parser", lit, a.pos)
775 expr := a.newExpr(IdealFloatType, "float literal")
776 expr.eval = func() *big.Rat { return f }
777 return expr
780 func (a *exprInfo) compileString(s string) *expr {
781 // Ideal strings don't have a named type but they are
782 // compatible with type string.
784 // TODO(austin) Use unnamed string type.
785 expr := a.newExpr(StringType, "string literal")
786 expr.eval = func(*Thread) string { return s }
787 return expr
790 func (a *exprInfo) compileStringLit(lit string) *expr {
791 s, err := strconv.Unquote(lit)
792 if err != nil {
793 a.diag("illegal string literal, %v", err)
794 return nil
796 return a.compileString(s)
799 func (a *exprInfo) compileStringList(list []*expr) *expr {
800 ss := make([]string, len(list))
801 for i, s := range list {
802 ss[i] = s.asString()(nil)
804 return a.compileString(strings.Join(ss, ""))
807 func (a *exprInfo) compileFuncLit(decl *FuncDecl, fn func(*Thread) Func) *expr {
808 expr := a.newExpr(decl.Type, "function literal")
809 expr.eval = fn
810 return expr
813 func (a *exprInfo) compileSelectorExpr(v *expr, name string) *expr {
814 // mark marks a field that matches the selector name. It
815 // tracks the best depth found so far and whether more than
816 // one field has been found at that depth.
817 bestDepth := -1
818 ambig := false
819 amberr := ""
820 mark := func(depth int, pathName string) {
821 switch {
822 case bestDepth == -1 || depth < bestDepth:
823 bestDepth = depth
824 ambig = false
825 amberr = ""
827 case depth == bestDepth:
828 ambig = true
830 default:
831 log.Panicf("Marked field at depth %d, but already found one at depth %d", depth, bestDepth)
833 amberr += "\n\t" + pathName[1:]
836 visited := make(map[Type]bool)
838 // find recursively searches for the named field, starting at
839 // type t. If it finds the named field, it returns a function
840 // which takes an expr that represents a value of type 't' and
841 // returns an expr that retrieves the named field. We delay
842 // expr construction to avoid producing lots of useless expr's
843 // as we search.
845 // TODO(austin) Now that the expression compiler works on
846 // semantic values instead of AST's, there should be a much
847 // better way of doing this.
848 var find func(Type, int, string) func(*expr) *expr
849 find = func(t Type, depth int, pathName string) func(*expr) *expr {
850 // Don't bother looking if we've found something shallower
851 if bestDepth != -1 && bestDepth < depth {
852 return nil
855 // Don't check the same type twice and avoid loops
856 if visited[t] {
857 return nil
859 visited[t] = true
861 // Implicit dereference
862 deref := false
863 if ti, ok := t.(*PtrType); ok {
864 deref = true
865 t = ti.Elem
868 // If it's a named type, look for methods
869 if ti, ok := t.(*NamedType); ok {
870 _, ok := ti.methods[name]
871 if ok {
872 mark(depth, pathName+"."+name)
873 log.Panic("Methods not implemented")
875 t = ti.Def
878 // If it's a struct type, check fields and embedded types
879 var builder func(*expr) *expr
880 if t, ok := t.(*StructType); ok {
881 for i, f := range t.Elems {
882 var sub func(*expr) *expr
883 switch {
884 case f.Name == name:
885 mark(depth, pathName+"."+name)
886 sub = func(e *expr) *expr { return e }
888 case f.Anonymous:
889 sub = find(f.Type, depth+1, pathName+"."+f.Name)
890 if sub == nil {
891 continue
894 default:
895 continue
898 // We found something. Create a
899 // builder for accessing this field.
900 ft := f.Type
901 index := i
902 builder = func(parent *expr) *expr {
903 if deref {
904 parent = a.compileStarExpr(parent)
906 expr := a.newExpr(ft, "selector expression")
907 pf := parent.asStruct()
908 evalAddr := func(t *Thread) Value { return pf(t).Field(t, index) }
909 expr.genValue(evalAddr)
910 return sub(expr)
915 return builder
918 builder := find(v.t, 0, "")
919 if builder == nil {
920 a.diag("type %v has no field or method %s", v.t, name)
921 return nil
923 if ambig {
924 a.diag("field %s is ambiguous in type %v%s", name, v.t, amberr)
925 return nil
928 return builder(v)
931 func (a *exprInfo) compileSliceExpr(arr, lo, hi *expr) *expr {
932 // Type check object
933 arr = arr.derefArray()
935 var at Type
936 var maxIndex int64 = -1
938 switch lt := arr.t.lit().(type) {
939 case *ArrayType:
940 at = NewSliceType(lt.Elem)
941 maxIndex = lt.Len
943 case *SliceType:
944 at = lt
946 case *stringType:
947 at = lt
949 default:
950 a.diag("cannot slice %v", arr.t)
951 return nil
954 // Type check index and convert to int
955 // XXX(Spec) It's unclear if ideal floats with no
956 // fractional part are allowed here. 6g allows it. I
957 // believe that's wrong.
958 lo = lo.convertToInt(maxIndex, "slice", "slice")
959 hi = hi.convertToInt(maxIndex, "slice", "slice")
960 if lo == nil || hi == nil {
961 return nil
964 expr := a.newExpr(at, "slice expression")
966 // Compile
967 lof := lo.asInt()
968 hif := hi.asInt()
969 switch lt := arr.t.lit().(type) {
970 case *ArrayType:
971 arrf := arr.asArray()
972 bound := lt.Len
973 expr.eval = func(t *Thread) Slice {
974 arr, lo, hi := arrf(t), lof(t), hif(t)
975 if lo > hi || hi > bound || lo < 0 {
976 t.Abort(SliceError{lo, hi, bound})
978 return Slice{arr.Sub(lo, bound-lo), hi - lo, bound - lo}
981 case *SliceType:
982 arrf := arr.asSlice()
983 expr.eval = func(t *Thread) Slice {
984 arr, lo, hi := arrf(t), lof(t), hif(t)
985 if lo > hi || hi > arr.Cap || lo < 0 {
986 t.Abort(SliceError{lo, hi, arr.Cap})
988 return Slice{arr.Base.Sub(lo, arr.Cap-lo), hi - lo, arr.Cap - lo}
991 case *stringType:
992 arrf := arr.asString()
993 // TODO(austin) This pulls over the whole string in a
994 // remote setting, instead of creating a substring backed
995 // by remote memory.
996 expr.eval = func(t *Thread) string {
997 arr, lo, hi := arrf(t), lof(t), hif(t)
998 if lo > hi || hi > int64(len(arr)) || lo < 0 {
999 t.Abort(SliceError{lo, hi, int64(len(arr))})
1001 return arr[lo:hi]
1004 default:
1005 log.Panicf("unexpected left operand type %T", arr.t.lit())
1008 return expr
1011 func (a *exprInfo) compileIndexExpr(l, r *expr) *expr {
1012 // Type check object
1013 l = l.derefArray()
1015 var at Type
1016 intIndex := false
1017 var maxIndex int64 = -1
1019 switch lt := l.t.lit().(type) {
1020 case *ArrayType:
1021 at = lt.Elem
1022 intIndex = true
1023 maxIndex = lt.Len
1025 case *SliceType:
1026 at = lt.Elem
1027 intIndex = true
1029 case *stringType:
1030 at = Uint8Type
1031 intIndex = true
1033 case *MapType:
1034 at = lt.Elem
1035 if r.t.isIdeal() {
1036 r = r.convertTo(lt.Key)
1037 if r == nil {
1038 return nil
1041 if !lt.Key.compat(r.t, false) {
1042 a.diag("cannot use %s as index into %s", r.t, lt)
1043 return nil
1046 default:
1047 a.diag("cannot index into %v", l.t)
1048 return nil
1051 // Type check index and convert to int if necessary
1052 if intIndex {
1053 // XXX(Spec) It's unclear if ideal floats with no
1054 // fractional part are allowed here. 6g allows it. I
1055 // believe that's wrong.
1056 r = r.convertToInt(maxIndex, "index", "index")
1057 if r == nil {
1058 return nil
1062 expr := a.newExpr(at, "index expression")
1064 // Compile
1065 switch lt := l.t.lit().(type) {
1066 case *ArrayType:
1067 lf := l.asArray()
1068 rf := r.asInt()
1069 bound := lt.Len
1070 expr.genValue(func(t *Thread) Value {
1071 l, r := lf(t), rf(t)
1072 if r < 0 || r >= bound {
1073 t.Abort(IndexError{r, bound})
1075 return l.Elem(t, r)
1078 case *SliceType:
1079 lf := l.asSlice()
1080 rf := r.asInt()
1081 expr.genValue(func(t *Thread) Value {
1082 l, r := lf(t), rf(t)
1083 if l.Base == nil {
1084 t.Abort(NilPointerError{})
1086 if r < 0 || r >= l.Len {
1087 t.Abort(IndexError{r, l.Len})
1089 return l.Base.Elem(t, r)
1092 case *stringType:
1093 lf := l.asString()
1094 rf := r.asInt()
1095 // TODO(austin) This pulls over the whole string in a
1096 // remote setting, instead of just the one character.
1097 expr.eval = func(t *Thread) uint64 {
1098 l, r := lf(t), rf(t)
1099 if r < 0 || r >= int64(len(l)) {
1100 t.Abort(IndexError{r, int64(len(l))})
1102 return uint64(l[r])
1105 case *MapType:
1106 lf := l.asMap()
1107 rf := r.asInterface()
1108 expr.genValue(func(t *Thread) Value {
1109 m := lf(t)
1110 k := rf(t)
1111 if m == nil {
1112 t.Abort(NilPointerError{})
1114 e := m.Elem(t, k)
1115 if e == nil {
1116 t.Abort(KeyError{k})
1118 return e
1120 // genValue makes things addressable, but map values
1121 // aren't addressable.
1122 expr.evalAddr = nil
1123 expr.evalMapValue = func(t *Thread) (Map, interface{}) {
1124 // TODO(austin) Key check? nil check?
1125 return lf(t), rf(t)
1128 default:
1129 log.Panicf("unexpected left operand type %T", l.t.lit())
1132 return expr
1135 func (a *exprInfo) compileCallExpr(b *block, l *expr, as []*expr) *expr {
1136 // TODO(austin) Variadic functions.
1138 // Type check
1140 // XXX(Spec) Calling a named function type is okay. I really
1141 // think there needs to be a general discussion of named
1142 // types. A named type creates a new, distinct type, but the
1143 // type of that type is still whatever it's defined to. Thus,
1144 // in "type Foo int", Foo is still an integer type and in
1145 // "type Foo func()", Foo is a function type.
1146 lt, ok := l.t.lit().(*FuncType)
1147 if !ok {
1148 a.diag("cannot call non-function type %v", l.t)
1149 return nil
1152 // The arguments must be single-valued expressions assignment
1153 // compatible with the parameters of F.
1155 // XXX(Spec) The spec is wrong. It can also be a single
1156 // multi-valued expression.
1157 nin := len(lt.In)
1158 assign := a.compileAssign(a.pos, b, NewMultiType(lt.In), as, "function call", "argument")
1159 if assign == nil {
1160 return nil
1163 var t Type
1164 nout := len(lt.Out)
1165 switch nout {
1166 case 0:
1167 t = EmptyType
1168 case 1:
1169 t = lt.Out[0]
1170 default:
1171 t = NewMultiType(lt.Out)
1173 expr := a.newExpr(t, "function call")
1175 // Gather argument and out types to initialize frame variables
1176 vts := make([]Type, nin+nout)
1177 copy(vts, lt.In)
1178 copy(vts[nin:], lt.Out)
1180 // Compile
1181 lf := l.asFunc()
1182 call := func(t *Thread) []Value {
1183 fun := lf(t)
1184 fr := fun.NewFrame()
1185 for i, t := range vts {
1186 fr.Vars[i] = t.Zero()
1188 assign(multiV(fr.Vars[0:nin]), t)
1189 oldf := t.f
1190 t.f = fr
1191 fun.Call(t)
1192 t.f = oldf
1193 return fr.Vars[nin : nin+nout]
1195 expr.genFuncCall(call)
1197 return expr
1200 func (a *exprInfo) compileBuiltinCallExpr(b *block, ft *FuncType, as []*expr) *expr {
1201 checkCount := func(min, max int) bool {
1202 if len(as) < min {
1203 a.diag("not enough arguments to %s", ft.builtin)
1204 return false
1205 } else if len(as) > max {
1206 a.diag("too many arguments to %s", ft.builtin)
1207 return false
1209 return true
1212 switch ft {
1213 case capType:
1214 if !checkCount(1, 1) {
1215 return nil
1217 arg := as[0].derefArray()
1218 expr := a.newExpr(IntType, "function call")
1219 switch t := arg.t.lit().(type) {
1220 case *ArrayType:
1221 // TODO(austin) It would be nice if this could
1222 // be a constant int.
1223 v := t.Len
1224 expr.eval = func(t *Thread) int64 { return v }
1226 case *SliceType:
1227 vf := arg.asSlice()
1228 expr.eval = func(t *Thread) int64 { return vf(t).Cap }
1230 //case *ChanType:
1232 default:
1233 a.diag("illegal argument type for cap function\n\t%v", arg.t)
1234 return nil
1236 return expr
1238 case copyType:
1239 if !checkCount(2, 2) {
1240 return nil
1242 src := as[1]
1243 dst := as[0]
1244 if src.t != dst.t {
1245 a.diag("arguments to built-in function 'copy' must have same type\nsrc: %s\ndst: %s\n", src.t, dst.t)
1246 return nil
1248 if _, ok := src.t.lit().(*SliceType); !ok {
1249 a.diag("src argument to 'copy' must be a slice (got: %s)", src.t)
1250 return nil
1252 if _, ok := dst.t.lit().(*SliceType); !ok {
1253 a.diag("dst argument to 'copy' must be a slice (got: %s)", dst.t)
1254 return nil
1256 expr := a.newExpr(IntType, "function call")
1257 srcf := src.asSlice()
1258 dstf := dst.asSlice()
1259 expr.eval = func(t *Thread) int64 {
1260 src, dst := srcf(t), dstf(t)
1261 nelems := src.Len
1262 if nelems > dst.Len {
1263 nelems = dst.Len
1265 dst.Base.Sub(0, nelems).Assign(t, src.Base.Sub(0, nelems))
1266 return nelems
1268 return expr
1270 case lenType:
1271 if !checkCount(1, 1) {
1272 return nil
1274 arg := as[0].derefArray()
1275 expr := a.newExpr(IntType, "function call")
1276 switch t := arg.t.lit().(type) {
1277 case *stringType:
1278 vf := arg.asString()
1279 expr.eval = func(t *Thread) int64 { return int64(len(vf(t))) }
1281 case *ArrayType:
1282 // TODO(austin) It would be nice if this could
1283 // be a constant int.
1284 v := t.Len
1285 expr.eval = func(t *Thread) int64 { return v }
1287 case *SliceType:
1288 vf := arg.asSlice()
1289 expr.eval = func(t *Thread) int64 { return vf(t).Len }
1291 case *MapType:
1292 vf := arg.asMap()
1293 expr.eval = func(t *Thread) int64 {
1294 // XXX(Spec) What's the len of an
1295 // uninitialized map?
1296 m := vf(t)
1297 if m == nil {
1298 return 0
1300 return m.Len(t)
1303 //case *ChanType:
1305 default:
1306 a.diag("illegal argument type for len function\n\t%v", arg.t)
1307 return nil
1309 return expr
1311 case makeType:
1312 if !checkCount(1, 3) {
1313 return nil
1315 // XXX(Spec) What are the types of the
1316 // arguments? Do they have to be ints? 6g
1317 // accepts any integral type.
1318 var lenexpr, capexpr *expr
1319 var lenf, capf func(*Thread) int64
1320 if len(as) > 1 {
1321 lenexpr = as[1].convertToInt(-1, "length", "make function")
1322 if lenexpr == nil {
1323 return nil
1325 lenf = lenexpr.asInt()
1327 if len(as) > 2 {
1328 capexpr = as[2].convertToInt(-1, "capacity", "make function")
1329 if capexpr == nil {
1330 return nil
1332 capf = capexpr.asInt()
1335 switch t := as[0].valType.lit().(type) {
1336 case *SliceType:
1337 // A new, initialized slice value for a given
1338 // element type T is made using the built-in
1339 // function make, which takes a slice type and
1340 // parameters specifying the length and
1341 // optionally the capacity.
1342 if !checkCount(2, 3) {
1343 return nil
1345 et := t.Elem
1346 expr := a.newExpr(t, "function call")
1347 expr.eval = func(t *Thread) Slice {
1348 l := lenf(t)
1349 // XXX(Spec) What if len or cap is
1350 // negative? The runtime panics.
1351 if l < 0 {
1352 t.Abort(NegativeLengthError{l})
1354 c := l
1355 if capf != nil {
1356 c = capf(t)
1357 if c < 0 {
1358 t.Abort(NegativeCapacityError{c})
1360 // XXX(Spec) What happens if
1361 // len > cap? The runtime
1362 // sets cap to len.
1363 if l > c {
1364 c = l
1367 base := arrayV(make([]Value, c))
1368 for i := int64(0); i < c; i++ {
1369 base[i] = et.Zero()
1371 return Slice{&base, l, c}
1373 return expr
1375 case *MapType:
1376 // A new, empty map value is made using the
1377 // built-in function make, which takes the map
1378 // type and an optional capacity hint as
1379 // arguments.
1380 if !checkCount(1, 2) {
1381 return nil
1383 expr := a.newExpr(t, "function call")
1384 expr.eval = func(t *Thread) Map {
1385 if lenf == nil {
1386 return make(evalMap)
1388 l := lenf(t)
1389 return make(evalMap, l)
1391 return expr
1393 //case *ChanType:
1395 default:
1396 a.diag("illegal argument type for make function\n\t%v", as[0].valType)
1397 return nil
1400 case closeType, closedType:
1401 a.diag("built-in function %s not implemented", ft.builtin)
1402 return nil
1404 case newType:
1405 if !checkCount(1, 1) {
1406 return nil
1409 t := as[0].valType
1410 expr := a.newExpr(NewPtrType(t), "new")
1411 expr.eval = func(*Thread) Value { return t.Zero() }
1412 return expr
1414 case panicType, printType, printlnType:
1415 evals := make([]func(*Thread) interface{}, len(as))
1416 for i, x := range as {
1417 evals[i] = x.asInterface()
1419 spaces := ft == printlnType
1420 newline := ft != printType
1421 printer := func(t *Thread) {
1422 for i, eval := range evals {
1423 if i > 0 && spaces {
1424 print(" ")
1426 v := eval(t)
1427 type stringer interface {
1428 String() string
1430 switch v1 := v.(type) {
1431 case bool:
1432 print(v1)
1433 case uint64:
1434 print(v1)
1435 case int64:
1436 print(v1)
1437 case float64:
1438 print(v1)
1439 case string:
1440 print(v1)
1441 case stringer:
1442 print(v1.String())
1443 default:
1444 print("???")
1447 if newline {
1448 print("\n")
1451 expr := a.newExpr(EmptyType, "print")
1452 expr.exec = printer
1453 if ft == panicType {
1454 expr.exec = func(t *Thread) {
1455 printer(t)
1456 t.Abort(os.NewError("panic"))
1459 return expr
1462 log.Panicf("unexpected built-in function '%s'", ft.builtin)
1463 panic("unreachable")
1466 func (a *exprInfo) compileStarExpr(v *expr) *expr {
1467 switch vt := v.t.lit().(type) {
1468 case *PtrType:
1469 expr := a.newExpr(vt.Elem, "indirect expression")
1470 vf := v.asPtr()
1471 expr.genValue(func(t *Thread) Value {
1472 v := vf(t)
1473 if v == nil {
1474 t.Abort(NilPointerError{})
1476 return v
1478 return expr
1481 a.diagOpType(token.MUL, v.t)
1482 return nil
1485 var unaryOpDescs = make(map[token.Token]string)
1487 func (a *exprInfo) compileUnaryExpr(op token.Token, v *expr) *expr {
1488 // Type check
1489 var t Type
1490 switch op {
1491 case token.ADD, token.SUB:
1492 if !v.t.isInteger() && !v.t.isFloat() {
1493 a.diagOpType(op, v.t)
1494 return nil
1496 t = v.t
1498 case token.NOT:
1499 if !v.t.isBoolean() {
1500 a.diagOpType(op, v.t)
1501 return nil
1503 t = BoolType
1505 case token.XOR:
1506 if !v.t.isInteger() {
1507 a.diagOpType(op, v.t)
1508 return nil
1510 t = v.t
1512 case token.AND:
1513 // The unary prefix address-of operator & generates
1514 // the address of its operand, which must be a
1515 // variable, pointer indirection, field selector, or
1516 // array or slice indexing operation.
1517 if v.evalAddr == nil {
1518 a.diag("cannot take the address of %s", v.desc)
1519 return nil
1522 // TODO(austin) Implement "It is illegal to take the
1523 // address of a function result variable" once I have
1524 // function result variables.
1526 t = NewPtrType(v.t)
1528 case token.ARROW:
1529 log.Panicf("Unary op %v not implemented", op)
1531 default:
1532 log.Panicf("unknown unary operator %v", op)
1535 desc, ok := unaryOpDescs[op]
1536 if !ok {
1537 desc = "unary " + op.String() + " expression"
1538 unaryOpDescs[op] = desc
1541 // Compile
1542 expr := a.newExpr(t, desc)
1543 switch op {
1544 case token.ADD:
1545 // Just compile it out
1546 expr = v
1547 expr.desc = desc
1549 case token.SUB:
1550 expr.genUnaryOpNeg(v)
1552 case token.NOT:
1553 expr.genUnaryOpNot(v)
1555 case token.XOR:
1556 expr.genUnaryOpXor(v)
1558 case token.AND:
1559 vf := v.evalAddr
1560 expr.eval = func(t *Thread) Value { return vf(t) }
1562 default:
1563 log.Panicf("Compilation of unary op %v not implemented", op)
1566 return expr
1569 var binOpDescs = make(map[token.Token]string)
1571 func (a *exprInfo) compileBinaryExpr(op token.Token, l, r *expr) *expr {
1572 // Save the original types of l.t and r.t for error messages.
1573 origlt := l.t
1574 origrt := r.t
1576 // XXX(Spec) What is the exact definition of a "named type"?
1578 // XXX(Spec) Arithmetic operators: "Integer types" apparently
1579 // means all types compatible with basic integer types, though
1580 // this is never explained. Likewise for float types, etc.
1581 // This relates to the missing explanation of named types.
1583 // XXX(Spec) Operators: "If both operands are ideal numbers,
1584 // the conversion is to ideal floats if one of the operands is
1585 // an ideal float (relevant for / and %)." How is that
1586 // relevant only for / and %? If I add an ideal int and an
1587 // ideal float, I get an ideal float.
1589 if op != token.SHL && op != token.SHR {
1590 // Except in shift expressions, if one operand has
1591 // numeric type and the other operand is an ideal
1592 // number, the ideal number is converted to match the
1593 // type of the other operand.
1594 if (l.t.isInteger() || l.t.isFloat()) && !l.t.isIdeal() && r.t.isIdeal() {
1595 r = r.convertTo(l.t)
1596 } else if (r.t.isInteger() || r.t.isFloat()) && !r.t.isIdeal() && l.t.isIdeal() {
1597 l = l.convertTo(r.t)
1599 if l == nil || r == nil {
1600 return nil
1603 // Except in shift expressions, if both operands are
1604 // ideal numbers and one is an ideal float, the other
1605 // is converted to ideal float.
1606 if l.t.isIdeal() && r.t.isIdeal() {
1607 if l.t.isInteger() && r.t.isFloat() {
1608 l = l.convertTo(r.t)
1609 } else if l.t.isFloat() && r.t.isInteger() {
1610 r = r.convertTo(l.t)
1612 if l == nil || r == nil {
1613 return nil
1618 // Useful type predicates
1619 // TODO(austin) CL 33668 mandates identical types except for comparisons.
1620 compat := func() bool { return l.t.compat(r.t, false) }
1621 integers := func() bool { return l.t.isInteger() && r.t.isInteger() }
1622 floats := func() bool { return l.t.isFloat() && r.t.isFloat() }
1623 strings := func() bool {
1624 // TODO(austin) Deal with named types
1625 return l.t == StringType && r.t == StringType
1627 booleans := func() bool { return l.t.isBoolean() && r.t.isBoolean() }
1629 // Type check
1630 var t Type
1631 switch op {
1632 case token.ADD:
1633 if !compat() || (!integers() && !floats() && !strings()) {
1634 a.diagOpTypes(op, origlt, origrt)
1635 return nil
1637 t = l.t
1639 case token.SUB, token.MUL, token.QUO:
1640 if !compat() || (!integers() && !floats()) {
1641 a.diagOpTypes(op, origlt, origrt)
1642 return nil
1644 t = l.t
1646 case token.REM, token.AND, token.OR, token.XOR, token.AND_NOT:
1647 if !compat() || !integers() {
1648 a.diagOpTypes(op, origlt, origrt)
1649 return nil
1651 t = l.t
1653 case token.SHL, token.SHR:
1654 // XXX(Spec) Is it okay for the right operand to be an
1655 // ideal float with no fractional part? "The right
1656 // operand in a shift operation must be always be of
1657 // unsigned integer type or an ideal number that can
1658 // be safely converted into an unsigned integer type
1659 // (§Arithmetic operators)" suggests so and 6g agrees.
1661 if !l.t.isInteger() || !(r.t.isInteger() || r.t.isIdeal()) {
1662 a.diagOpTypes(op, origlt, origrt)
1663 return nil
1666 // The right operand in a shift operation must be
1667 // always be of unsigned integer type or an ideal
1668 // number that can be safely converted into an
1669 // unsigned integer type.
1670 if r.t.isIdeal() {
1671 r2 := r.convertTo(UintType)
1672 if r2 == nil {
1673 return nil
1676 // If the left operand is not ideal, convert
1677 // the right to not ideal.
1678 if !l.t.isIdeal() {
1679 r = r2
1682 // If both are ideal, but the right side isn't
1683 // an ideal int, convert it to simplify things.
1684 if l.t.isIdeal() && !r.t.isInteger() {
1685 r = r.convertTo(IdealIntType)
1686 if r == nil {
1687 log.Panicf("conversion to uintType succeeded, but conversion to idealIntType failed")
1690 } else if _, ok := r.t.lit().(*uintType); !ok {
1691 a.diag("right operand of shift must be unsigned")
1692 return nil
1695 if l.t.isIdeal() && !r.t.isIdeal() {
1696 // XXX(Spec) What is the meaning of "ideal >>
1697 // non-ideal"? Russ says the ideal should be
1698 // converted to an int. 6g propagates the
1699 // type down from assignments as a hint.
1701 l = l.convertTo(IntType)
1702 if l == nil {
1703 return nil
1707 // At this point, we should have one of three cases:
1708 // 1) uint SHIFT uint
1709 // 2) int SHIFT uint
1710 // 3) ideal int SHIFT ideal int
1712 t = l.t
1714 case token.LOR, token.LAND:
1715 if !booleans() {
1716 return nil
1718 // XXX(Spec) There's no mention of *which* boolean
1719 // type the logical operators return. From poking at
1720 // 6g, it appears to be the named boolean type, NOT
1721 // the type of the left operand, and NOT an unnamed
1722 // boolean type.
1724 t = BoolType
1726 case token.ARROW:
1727 // The operands in channel sends differ in type: one
1728 // is always a channel and the other is a variable or
1729 // value of the channel's element type.
1730 log.Panic("Binary op <- not implemented")
1731 t = BoolType
1733 case token.LSS, token.GTR, token.LEQ, token.GEQ:
1734 // XXX(Spec) It's really unclear what types which
1735 // comparison operators apply to. I feel like the
1736 // text is trying to paint a Venn diagram for me,
1737 // which it's really pretty simple: <, <=, >, >= apply
1738 // only to numeric types and strings. == and != apply
1739 // to everything except arrays and structs, and there
1740 // are some restrictions on when it applies to slices.
1742 if !compat() || (!integers() && !floats() && !strings()) {
1743 a.diagOpTypes(op, origlt, origrt)
1744 return nil
1746 t = BoolType
1748 case token.EQL, token.NEQ:
1749 // XXX(Spec) The rules for type checking comparison
1750 // operators are spread across three places that all
1751 // partially overlap with each other: the Comparison
1752 // Compatibility section, the Operators section, and
1753 // the Comparison Operators section. The Operators
1754 // section should just say that operators require
1755 // identical types (as it does currently) except that
1756 // there a few special cases for comparison, which are
1757 // described in section X. Currently it includes just
1758 // one of the four special cases. The Comparison
1759 // Compatibility section and the Comparison Operators
1760 // section should either be merged, or at least the
1761 // Comparison Compatibility section should be
1762 // exclusively about type checking and the Comparison
1763 // Operators section should be exclusively about
1764 // semantics.
1766 // XXX(Spec) Comparison operators: "All comparison
1767 // operators apply to basic types except bools." This
1768 // is very difficult to parse. It's explained much
1769 // better in the Comparison Compatibility section.
1771 // XXX(Spec) Comparison compatibility: "Function
1772 // values are equal if they refer to the same
1773 // function." is rather vague. It should probably be
1774 // similar to the way the rule for map values is
1775 // written: Function values are equal if they were
1776 // created by the same execution of a function literal
1777 // or refer to the same function declaration. This is
1778 // *almost* but not quite waht 6g implements. If a
1779 // function literals does not capture any variables,
1780 // then multiple executions of it will result in the
1781 // same closure. Russ says he'll change that.
1783 // TODO(austin) Deal with remaining special cases
1785 if !compat() {
1786 a.diagOpTypes(op, origlt, origrt)
1787 return nil
1789 // Arrays and structs may not be compared to anything.
1790 switch l.t.(type) {
1791 case *ArrayType, *StructType:
1792 a.diagOpTypes(op, origlt, origrt)
1793 return nil
1795 t = BoolType
1797 default:
1798 log.Panicf("unknown binary operator %v", op)
1801 desc, ok := binOpDescs[op]
1802 if !ok {
1803 desc = op.String() + " expression"
1804 binOpDescs[op] = desc
1807 // Check for ideal divide by zero
1808 switch op {
1809 case token.QUO, token.REM:
1810 if r.t.isIdeal() {
1811 if (r.t.isInteger() && r.asIdealInt()().Sign() == 0) ||
1812 (r.t.isFloat() && r.asIdealFloat()().Sign() == 0) {
1813 a.diag("divide by zero")
1814 return nil
1819 // Compile
1820 expr := a.newExpr(t, desc)
1821 switch op {
1822 case token.ADD:
1823 expr.genBinOpAdd(l, r)
1825 case token.SUB:
1826 expr.genBinOpSub(l, r)
1828 case token.MUL:
1829 expr.genBinOpMul(l, r)
1831 case token.QUO:
1832 expr.genBinOpQuo(l, r)
1834 case token.REM:
1835 expr.genBinOpRem(l, r)
1837 case token.AND:
1838 expr.genBinOpAnd(l, r)
1840 case token.OR:
1841 expr.genBinOpOr(l, r)
1843 case token.XOR:
1844 expr.genBinOpXor(l, r)
1846 case token.AND_NOT:
1847 expr.genBinOpAndNot(l, r)
1849 case token.SHL:
1850 if l.t.isIdeal() {
1851 lv := l.asIdealInt()()
1852 rv := r.asIdealInt()()
1853 const maxShift = 99999
1854 if rv.Cmp(big.NewInt(maxShift)) > 0 {
1855 a.diag("left shift by %v; exceeds implementation limit of %v", rv, maxShift)
1856 expr.t = nil
1857 return nil
1859 val := new(big.Int).Lsh(lv, uint(rv.Int64()))
1860 expr.eval = func() *big.Int { return val }
1861 } else {
1862 expr.genBinOpShl(l, r)
1865 case token.SHR:
1866 if l.t.isIdeal() {
1867 lv := l.asIdealInt()()
1868 rv := r.asIdealInt()()
1869 val := new(big.Int).Rsh(lv, uint(rv.Int64()))
1870 expr.eval = func() *big.Int { return val }
1871 } else {
1872 expr.genBinOpShr(l, r)
1875 case token.LSS:
1876 expr.genBinOpLss(l, r)
1878 case token.GTR:
1879 expr.genBinOpGtr(l, r)
1881 case token.LEQ:
1882 expr.genBinOpLeq(l, r)
1884 case token.GEQ:
1885 expr.genBinOpGeq(l, r)
1887 case token.EQL:
1888 expr.genBinOpEql(l, r)
1890 case token.NEQ:
1891 expr.genBinOpNeq(l, r)
1893 case token.LAND:
1894 expr.genBinOpLogAnd(l, r)
1896 case token.LOR:
1897 expr.genBinOpLogOr(l, r)
1899 default:
1900 log.Panicf("Compilation of binary op %v not implemented", op)
1903 return expr
1906 // TODO(austin) This is a hack to eliminate a circular dependency
1907 // between type.go and expr.go
1908 func (a *compiler) compileArrayLen(b *block, expr ast.Expr) (int64, bool) {
1909 lenExpr := a.compileExpr(b, true, expr)
1910 if lenExpr == nil {
1911 return 0, false
1914 // XXX(Spec) Are ideal floats with no fractional part okay?
1915 if lenExpr.t.isIdeal() {
1916 lenExpr = lenExpr.convertTo(IntType)
1917 if lenExpr == nil {
1918 return 0, false
1922 if !lenExpr.t.isInteger() {
1923 a.diagAt(expr, "array size must be an integer")
1924 return 0, false
1927 switch lenExpr.t.lit().(type) {
1928 case *intType:
1929 return lenExpr.asInt()(nil), true
1930 case *uintType:
1931 return int64(lenExpr.asUint()(nil)), true
1933 log.Panicf("unexpected integer type %T", lenExpr.t)
1934 return 0, false
1937 func (a *compiler) compileExpr(b *block, constant bool, expr ast.Expr) *expr {
1938 ec := &exprCompiler{a, b, constant}
1939 nerr := a.numError()
1940 e := ec.compile(expr, false)
1941 if e == nil && nerr == a.numError() {
1942 log.Panicf("expression compilation failed without reporting errors")
1944 return e
1947 // extractEffect separates out any effects that the expression may
1948 // have, returning a function that will perform those effects and a
1949 // new exprCompiler that is guaranteed to be side-effect free. These
1950 // are the moral equivalents of "temp := expr" and "temp" (or "temp :=
1951 // &expr" and "*temp" for addressable exprs). Because this creates a
1952 // temporary variable, the caller should create a temporary block for
1953 // the compilation of this expression and the evaluation of the
1954 // results.
1955 func (a *expr) extractEffect(b *block, errOp string) (func(*Thread), *expr) {
1956 // Create "&a" if a is addressable
1957 rhs := a
1958 if a.evalAddr != nil {
1959 rhs = a.compileUnaryExpr(token.AND, rhs)
1962 // Create temp
1963 ac, ok := a.checkAssign(a.pos, []*expr{rhs}, errOp, "")
1964 if !ok {
1965 return nil, nil
1967 if len(ac.rmt.Elems) != 1 {
1968 a.diag("multi-valued expression not allowed in %s", errOp)
1969 return nil, nil
1971 tempType := ac.rmt.Elems[0]
1972 if tempType.isIdeal() {
1973 // It's too bad we have to duplicate this rule.
1974 switch {
1975 case tempType.isInteger():
1976 tempType = IntType
1977 case tempType.isFloat():
1978 tempType = FloatType
1979 default:
1980 log.Panicf("unexpected ideal type %v", tempType)
1983 temp := b.DefineTemp(tempType)
1984 tempIdx := temp.Index
1986 // Create "temp := rhs"
1987 assign := ac.compile(b, tempType)
1988 if assign == nil {
1989 log.Panicf("compileAssign type check failed")
1992 effect := func(t *Thread) {
1993 tempVal := tempType.Zero()
1994 t.f.Vars[tempIdx] = tempVal
1995 assign(tempVal, t)
1998 // Generate "temp" or "*temp"
1999 getTemp := a.compileVariable(0, temp)
2000 if a.evalAddr == nil {
2001 return effect, getTemp
2004 deref := a.compileStarExpr(getTemp)
2005 if deref == nil {
2006 return nil, nil
2008 return effect, deref