2009-07-15 12:59:13 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bignum";
|
|
|
|
"go/ast";
|
|
|
|
"go/token";
|
|
|
|
"log";
|
|
|
|
"strconv";
|
|
|
|
"strings";
|
|
|
|
)
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
// An expr is the result of compiling an expression. It stores the
|
|
|
|
// type of the expression and its evaluator function.
|
|
|
|
type expr struct {
|
|
|
|
*exprInfo;
|
2009-07-15 12:59:13 -06:00
|
|
|
t Type;
|
2009-09-01 12:51:33 -06:00
|
|
|
|
2009-07-15 18:56:17 -06:00
|
|
|
// Evaluate this node as the given type.
|
2009-09-01 12:51:33 -06:00
|
|
|
eval interface{};
|
|
|
|
|
2009-08-21 19:37:38 -06:00
|
|
|
// Map index expressions permit special forms of assignment,
|
|
|
|
// for which we need to know the Map and key.
|
2009-09-02 13:03:20 -06:00
|
|
|
evalMapValue func(t *Thread) (Map, interface{});
|
2009-09-01 12:51:33 -06:00
|
|
|
|
2009-07-15 18:56:17 -06:00
|
|
|
// Evaluate to the "address of" this value; that is, the
|
|
|
|
// settable Value object. nil for expressions whose address
|
|
|
|
// cannot be taken.
|
2009-09-02 13:03:20 -06:00
|
|
|
evalAddr func(t *Thread) Value;
|
2009-09-01 12:51:33 -06:00
|
|
|
|
2009-07-27 14:01:23 -06:00
|
|
|
// Execute this expression as a statement. Only expressions
|
|
|
|
// that are valid expression statements should set this.
|
2009-09-02 13:03:20 -06:00
|
|
|
exec func(t *Thread);
|
2009-09-01 12:51:33 -06:00
|
|
|
|
2009-08-28 19:03:03 -06:00
|
|
|
// If this expression is a type, this is its compiled type.
|
|
|
|
// This is only permitted in the function position of a call
|
|
|
|
// expression. In this case, t should be nil.
|
|
|
|
valType Type;
|
2009-09-01 12:51:33 -06:00
|
|
|
|
2009-07-15 12:59:13 -06:00
|
|
|
// A short string describing this expression for error
|
2009-08-25 18:57:40 -06:00
|
|
|
// messages.
|
2009-07-15 12:59:13 -06:00
|
|
|
desc string;
|
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
// exprInfo stores information needed to compile any expression node.
|
|
|
|
// Each expr also stores its exprInfo so further expressions can be
|
|
|
|
// compiled from it.
|
|
|
|
type exprInfo struct {
|
|
|
|
*compiler;
|
|
|
|
pos token.Position;
|
2009-07-20 18:41:40 -06:00
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
func (a *exprInfo) newExpr(t Type, desc string) *expr {
|
|
|
|
return &expr{exprInfo: a, t: t, desc: desc};
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
func (a *exprInfo) diag(format string, args ...) {
|
2009-07-27 14:01:23 -06:00
|
|
|
a.diagAt(&a.pos, format, args);
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
func (a *exprInfo) diagOpType(op token.Token, vt Type) {
|
2009-07-15 12:59:13 -06:00
|
|
|
a.diag("illegal operand type for '%v' operator\n\t%v", op, vt);
|
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
func (a *exprInfo) diagOpTypes(op token.Token, lt Type, rt Type) {
|
2009-07-15 12:59:13 -06:00
|
|
|
a.diag("illegal operand types for '%v' operator\n\t%v\n\t%v", op, lt, rt);
|
|
|
|
}
|
|
|
|
|
2009-07-21 16:40:41 -06:00
|
|
|
/*
|
|
|
|
* Common expression manipulations
|
|
|
|
*/
|
|
|
|
|
|
|
|
// a.convertTo(t) converts the value of the analyzed expression a,
|
|
|
|
// which must be a constant, ideal number, to a new analyzed
|
|
|
|
// expression with a constant value of type t.
|
2009-07-27 18:32:35 -06:00
|
|
|
//
|
|
|
|
// TODO(austin) Rename to resolveIdeal or something?
|
2009-08-25 18:57:40 -06:00
|
|
|
func (a *expr) convertTo(t Type) *expr {
|
2009-07-21 16:40:41 -06:00
|
|
|
if !a.t.isIdeal() {
|
|
|
|
log.Crashf("attempted to convert from %v, expected ideal", a.t);
|
|
|
|
}
|
|
|
|
|
|
|
|
var rat *bignum.Rational;
|
|
|
|
|
|
|
|
// XXX(Spec) The spec says "It is erroneous".
|
|
|
|
//
|
|
|
|
// It is an error to assign a value with a non-zero fractional
|
|
|
|
// part to an integer, or if the assignment would overflow or
|
|
|
|
// underflow, or in general if the value cannot be represented
|
|
|
|
// by the type of the variable.
|
|
|
|
switch a.t {
|
|
|
|
case IdealFloatType:
|
|
|
|
rat = a.asIdealFloat()();
|
|
|
|
if t.isInteger() && !rat.IsInt() {
|
|
|
|
a.diag("constant %v truncated to integer", ratToString(rat));
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
case IdealIntType:
|
|
|
|
i := a.asIdealInt()();
|
|
|
|
rat = bignum.MakeRat(i, bignum.Nat(1));
|
|
|
|
default:
|
|
|
|
log.Crashf("unexpected ideal type %v", a.t);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check bounds
|
2009-07-31 18:11:34 -06:00
|
|
|
if t, ok := t.lit().(BoundedType); ok {
|
2009-07-21 16:40:41 -06:00
|
|
|
if rat.Cmp(t.minVal()) < 0 {
|
|
|
|
a.diag("constant %v underflows %v", ratToString(rat), t);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
if rat.Cmp(t.maxVal()) > 0 {
|
|
|
|
a.diag("constant %v overflows %v", ratToString(rat), t);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert rat to type t.
|
2009-08-25 18:57:40 -06:00
|
|
|
res := a.newExpr(t, a.desc);
|
2009-07-31 18:11:34 -06:00
|
|
|
switch t := t.lit().(type) {
|
2009-07-21 16:40:41 -06:00
|
|
|
case *uintType:
|
|
|
|
n, d := rat.Value();
|
|
|
|
f := n.Quo(bignum.MakeInt(false, d));
|
|
|
|
v := f.Abs().Value();
|
2009-09-02 13:03:20 -06:00
|
|
|
res.eval = func(*Thread) uint64 { return v };
|
2009-07-21 16:40:41 -06:00
|
|
|
case *intType:
|
|
|
|
n, d := rat.Value();
|
|
|
|
f := n.Quo(bignum.MakeInt(false, d));
|
|
|
|
v := f.Value();
|
2009-09-02 13:03:20 -06:00
|
|
|
res.eval = func(*Thread) int64 { return v };
|
2009-07-21 16:40:41 -06:00
|
|
|
case *idealIntType:
|
|
|
|
n, d := rat.Value();
|
|
|
|
f := n.Quo(bignum.MakeInt(false, d));
|
2009-09-01 12:51:33 -06:00
|
|
|
res.eval = func() *bignum.Integer { return f };
|
2009-07-21 16:40:41 -06:00
|
|
|
case *floatType:
|
|
|
|
n, d := rat.Value();
|
|
|
|
v := float64(n.Value())/float64(d.Value());
|
2009-09-02 13:03:20 -06:00
|
|
|
res.eval = func(*Thread) float64 { return v };
|
2009-07-21 16:40:41 -06:00
|
|
|
case *idealFloatType:
|
2009-09-01 12:51:33 -06:00
|
|
|
res.eval = func() *bignum.Rational { return rat };
|
2009-07-21 16:40:41 -06:00
|
|
|
default:
|
|
|
|
log.Crashf("cannot convert to type %T", t);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2009-08-28 19:03:03 -06:00
|
|
|
// convertToInt converts this expression to an integer, if possible,
|
|
|
|
// or produces an error if not. This accepts ideal ints, uints, and
|
|
|
|
// ints. If max is not -1, produces an error if possible if the value
|
|
|
|
// exceeds max. If negErr is not "", produces an error if possible if
|
|
|
|
// the value is negative.
|
|
|
|
func (a *expr) convertToInt(max int64, negErr string, errOp string) *expr {
|
2009-09-25 10:37:35 -06:00
|
|
|
switch a.t.lit().(type) {
|
2009-08-28 19:03:03 -06:00
|
|
|
case *idealIntType:
|
|
|
|
val := a.asIdealInt()();
|
|
|
|
if negErr != "" && val.IsNeg() {
|
|
|
|
a.diag("negative %s: %s", negErr, val);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
if max != -1 && val.Cmp(bignum.Int(max)) >= 0 {
|
|
|
|
a.diag("index %s exceeds length %d", val, max);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return a.convertTo(IntType);
|
|
|
|
|
|
|
|
case *uintType:
|
|
|
|
// Convert to int
|
|
|
|
na := a.newExpr(IntType, a.desc);
|
|
|
|
af := a.asUint();
|
2009-09-02 13:03:20 -06:00
|
|
|
na.eval = func(t *Thread) int64 {
|
|
|
|
return int64(af(t));
|
2009-08-28 19:03:03 -06:00
|
|
|
};
|
|
|
|
return na;
|
|
|
|
|
|
|
|
case *intType:
|
|
|
|
// Good as is
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
a.diag("illegal operand type for %s\n\t%v", errOp, a.t);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
// derefArray returns an expression of array type if the given
|
|
|
|
// expression is a *array type. Otherwise, returns the given
|
|
|
|
// expression.
|
|
|
|
func (a *expr) derefArray() *expr {
|
|
|
|
if pt, ok := a.t.lit().(*PtrType); ok {
|
2009-09-24 09:25:14 -06:00
|
|
|
if _, ok := pt.Elem.lit().(*ArrayType); ok {
|
2009-08-28 19:03:03 -06:00
|
|
|
deref := a.compileStarExpr(a);
|
|
|
|
if deref == nil {
|
|
|
|
log.Crashf("failed to dereference *array");
|
|
|
|
}
|
|
|
|
return deref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2009-07-27 18:32:35 -06:00
|
|
|
/*
|
|
|
|
* Assignments
|
|
|
|
*/
|
|
|
|
|
|
|
|
// An assignCompiler compiles assignment operations. Anything other
|
|
|
|
// than short declarations should use the compileAssign wrapper.
|
2009-07-21 16:40:41 -06:00
|
|
|
//
|
2009-07-27 18:32:35 -06:00
|
|
|
// There are three valid types of assignment:
|
|
|
|
// 1) T = T
|
|
|
|
// Assigning a single expression with single-valued type to a
|
|
|
|
// single-valued type.
|
|
|
|
// 2) MT = T, T, ...
|
|
|
|
// Assigning multiple expressions with single-valued types to a
|
|
|
|
// multi-valued type.
|
|
|
|
// 3) MT = MT
|
|
|
|
// Assigning a single expression with multi-valued type to a
|
|
|
|
// multi-valued type.
|
|
|
|
type assignCompiler struct {
|
|
|
|
*compiler;
|
|
|
|
pos token.Position;
|
|
|
|
// The RHS expressions. This may include nil's for
|
|
|
|
// expressions that failed to compile.
|
2009-08-25 18:57:40 -06:00
|
|
|
rs []*expr;
|
2009-07-27 18:32:35 -06:00
|
|
|
// The (possibly unary) MultiType of the RHS.
|
|
|
|
rmt *MultiType;
|
|
|
|
// Whether this is an unpack assignment (case 3).
|
|
|
|
isUnpack bool;
|
2009-08-21 19:37:38 -06:00
|
|
|
// Whether map special assignment forms are allowed.
|
|
|
|
allowMap bool;
|
|
|
|
// Whether this is a "r, ok = a[x]" assignment.
|
|
|
|
isMapUnpack bool;
|
2009-07-27 18:32:35 -06:00
|
|
|
// The operation name to use in error messages, such as
|
|
|
|
// "assignment" or "function call".
|
|
|
|
errOp string;
|
|
|
|
// The name to use for positions in error messages, such as
|
|
|
|
// "argument".
|
|
|
|
errPosName string;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type check the RHS of an assignment, returning a new assignCompiler
|
|
|
|
// and indicating if the type check succeeded. This always returns an
|
|
|
|
// assignCompiler with rmt set, but if type checking fails, slots in
|
|
|
|
// the MultiType may be nil. If rs contains nil's, type checking will
|
|
|
|
// fail and these expressions given a nil type.
|
2009-08-25 18:57:40 -06:00
|
|
|
func (a *compiler) checkAssign(pos token.Position, rs []*expr, errOp, errPosName string) (*assignCompiler, bool) {
|
2009-07-27 18:32:35 -06:00
|
|
|
c := &assignCompiler{
|
|
|
|
compiler: a,
|
|
|
|
pos: pos,
|
|
|
|
rs: rs,
|
|
|
|
errOp: errOp,
|
|
|
|
errPosName: errPosName,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Is this an unpack?
|
|
|
|
if len(rs) == 1 && rs[0] != nil {
|
|
|
|
if rmt, isUnpack := rs[0].t.(*MultiType); isUnpack {
|
|
|
|
c.rmt = rmt;
|
|
|
|
c.isUnpack = true;
|
|
|
|
return c, true;
|
2009-07-21 16:40:41 -06:00
|
|
|
}
|
2009-07-27 18:32:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create MultiType for RHS and check that all RHS expressions
|
|
|
|
// are single-valued.
|
|
|
|
rts := make([]Type, len(rs));
|
|
|
|
ok := true;
|
|
|
|
for i, r := range rs {
|
2009-07-21 16:40:41 -06:00
|
|
|
if r == nil {
|
2009-07-27 18:32:35 -06:00
|
|
|
ok = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, isMT := r.t.(*MultiType); isMT {
|
|
|
|
r.diag("multi-valued expression not allowed in %s", errOp);
|
|
|
|
ok = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
rts[i] = r.t;
|
|
|
|
}
|
|
|
|
|
|
|
|
c.rmt = NewMultiType(rts);
|
|
|
|
return c, ok;
|
|
|
|
}
|
|
|
|
|
2009-08-21 19:37:38 -06:00
|
|
|
func (a *assignCompiler) allowMapForms(nls int) {
|
|
|
|
a.allowMap = true;
|
|
|
|
|
|
|
|
// Update unpacking info if this is r, ok = a[x]
|
|
|
|
if nls == 2 && len(a.rs) == 1 && a.rs[0].evalMapValue != nil {
|
|
|
|
a.isUnpack = true;
|
|
|
|
a.rmt = NewMultiType([]Type {a.rs[0].t, BoolType});
|
|
|
|
a.isMapUnpack = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-27 18:32:35 -06:00
|
|
|
// compile type checks and compiles an assignment operation, returning
|
|
|
|
// a function that expects an l-value and the frame in which to
|
|
|
|
// evaluate the RHS expressions. The l-value must have exactly the
|
|
|
|
// type given by lt. Returns nil if type checking fails.
|
2009-09-02 13:03:20 -06:00
|
|
|
func (a *assignCompiler) compile(b *block, lt Type) (func(Value, *Thread)) {
|
2009-07-27 18:32:35 -06:00
|
|
|
lmt, isMT := lt.(*MultiType);
|
|
|
|
rmt, isUnpack := a.rmt, a.isUnpack;
|
|
|
|
|
|
|
|
// Create unary MultiType for single LHS
|
|
|
|
if !isMT {
|
|
|
|
lmt = NewMultiType([]Type{lt});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the assignment count matches
|
|
|
|
lcount := len(lmt.Elems);
|
|
|
|
rcount := len(rmt.Elems);
|
|
|
|
if lcount != rcount {
|
|
|
|
msg := "not enough";
|
|
|
|
pos := a.pos;
|
|
|
|
if rcount > lcount {
|
|
|
|
msg = "too many";
|
|
|
|
if lcount > 0 {
|
|
|
|
pos = a.rs[lcount-1].pos;
|
|
|
|
}
|
2009-07-21 16:40:41 -06:00
|
|
|
}
|
2009-07-27 18:32:35 -06:00
|
|
|
a.diagAt(&pos, "%s %ss for %s\n\t%s\n\t%s", msg, a.errPosName, a.errOp, lt, rmt);
|
|
|
|
return nil;
|
2009-07-21 16:40:41 -06:00
|
|
|
}
|
|
|
|
|
2009-07-27 18:32:35 -06:00
|
|
|
bad := false;
|
|
|
|
|
2009-08-17 12:29:12 -06:00
|
|
|
// If this is an unpack, create a temporary to store the
|
|
|
|
// multi-value and replace the RHS with expressions to pull
|
|
|
|
// out values from the temporary. Technically, this is only
|
|
|
|
// necessary when we need to perform assignment conversions.
|
2009-09-02 13:03:20 -06:00
|
|
|
var effect func(*Thread);
|
2009-08-17 12:29:12 -06:00
|
|
|
if isUnpack {
|
|
|
|
// This leaks a slot, but is definitely safe.
|
2009-09-03 17:20:49 -06:00
|
|
|
temp := b.DefineTemp(a.rmt);
|
2009-08-17 12:29:12 -06:00
|
|
|
tempIdx := temp.Index;
|
2009-09-03 17:20:49 -06:00
|
|
|
if tempIdx < 0 {
|
|
|
|
panicln("tempidx", tempIdx);
|
|
|
|
}
|
2009-08-21 19:37:38 -06:00
|
|
|
if a.isMapUnpack {
|
|
|
|
rf := a.rs[0].evalMapValue;
|
|
|
|
vt := a.rmt.Elems[0];
|
2009-09-02 13:03:20 -06:00
|
|
|
effect = func(t *Thread) {
|
|
|
|
m, k := rf(t);
|
2009-09-03 18:14:49 -06:00
|
|
|
v := m.Elem(t, k);
|
2009-08-21 19:37:38 -06:00
|
|
|
found := boolV(true);
|
|
|
|
if v == nil {
|
|
|
|
found = boolV(false);
|
|
|
|
v = vt.Zero();
|
|
|
|
}
|
2009-09-02 13:03:20 -06:00
|
|
|
t.f.Vars[tempIdx] = multiV([]Value {v, &found});
|
2009-08-21 19:37:38 -06:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
rf := a.rs[0].asMulti();
|
2009-09-02 13:03:20 -06:00
|
|
|
effect = func(t *Thread) {
|
|
|
|
t.f.Vars[tempIdx] = multiV(rf(t));
|
2009-08-21 19:37:38 -06:00
|
|
|
};
|
|
|
|
}
|
2009-08-17 12:29:12 -06:00
|
|
|
orig := a.rs[0];
|
2009-08-25 18:57:40 -06:00
|
|
|
a.rs = make([]*expr, len(a.rmt.Elems));
|
2009-08-17 12:29:12 -06:00
|
|
|
for i, t := range a.rmt.Elems {
|
|
|
|
if t.isIdeal() {
|
|
|
|
log.Crashf("Right side of unpack contains ideal: %s", rmt);
|
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
a.rs[i] = orig.newExpr(t, orig.desc);
|
2009-08-17 12:29:12 -06:00
|
|
|
index := i;
|
2009-09-02 13:03:20 -06:00
|
|
|
a.rs[i].genValue(func(t *Thread) Value { return t.f.Vars[tempIdx].(multiV)[index] });
|
2009-08-17 12:29:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now len(a.rs) == len(a.rmt) and we've reduced any unpacking
|
|
|
|
// to multi-assignment.
|
|
|
|
|
2009-08-21 19:37:38 -06:00
|
|
|
// TODO(austin) Deal with assignment special cases.
|
2009-07-27 18:32:35 -06:00
|
|
|
|
|
|
|
// Values of any type may always be assigned to variables of
|
|
|
|
// compatible static type.
|
|
|
|
for i, lt := range lmt.Elems {
|
|
|
|
rt := rmt.Elems[i];
|
|
|
|
|
|
|
|
// When [an ideal is] (used in an expression) assigned
|
|
|
|
// to a variable or typed constant, the destination
|
|
|
|
// must be able to represent the assigned value.
|
|
|
|
if rt.isIdeal() {
|
|
|
|
a.rs[i] = a.rs[i].convertTo(lmt.Elems[i]);
|
|
|
|
if a.rs[i] == nil {
|
|
|
|
bad = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
rt = a.rs[i].t;
|
|
|
|
}
|
2009-07-21 16:40:41 -06:00
|
|
|
|
2009-08-17 12:29:12 -06:00
|
|
|
// A pointer p to an array can be assigned to a slice
|
|
|
|
// variable v with compatible element type if the type
|
|
|
|
// of p or v is unnamed.
|
|
|
|
if rpt, ok := rt.lit().(*PtrType); ok {
|
|
|
|
if at, ok := rpt.Elem.lit().(*ArrayType); ok {
|
|
|
|
if lst, ok := lt.lit().(*SliceType); ok {
|
|
|
|
if lst.Elem.compat(at.Elem, false) && (rt.lit() == Type(rt) || lt.lit() == Type(lt)) {
|
|
|
|
rf := a.rs[i].asPtr();
|
2009-08-25 18:57:40 -06:00
|
|
|
a.rs[i] = a.rs[i].newExpr(lt, a.rs[i].desc);
|
2009-08-17 12:29:12 -06:00
|
|
|
len := at.Len;
|
2009-09-02 13:03:20 -06:00
|
|
|
a.rs[i].eval = func(t *Thread) Slice {
|
|
|
|
return Slice{rf(t).(ArrayValue), len, len};
|
2009-08-17 12:29:12 -06:00
|
|
|
};
|
|
|
|
rt = a.rs[i].t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-31 18:11:34 -06:00
|
|
|
if !lt.compat(rt, false) {
|
2009-07-27 18:32:35 -06:00
|
|
|
if len(a.rs) == 1 {
|
|
|
|
a.rs[0].diag("illegal operand types for %s\n\t%v\n\t%v", a.errOp, lt, rt);
|
2009-07-21 16:40:41 -06:00
|
|
|
} else {
|
2009-07-27 18:32:35 -06:00
|
|
|
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);
|
2009-07-21 16:40:41 -06:00
|
|
|
}
|
2009-07-27 18:32:35 -06:00
|
|
|
bad = true;
|
2009-07-21 16:40:41 -06:00
|
|
|
}
|
|
|
|
}
|
2009-07-27 18:32:35 -06:00
|
|
|
if bad {
|
|
|
|
return nil;
|
|
|
|
}
|
2009-07-21 16:40:41 -06:00
|
|
|
|
|
|
|
// Compile
|
2009-08-17 12:29:12 -06:00
|
|
|
if !isMT {
|
2009-07-27 18:32:35 -06:00
|
|
|
// Case 1
|
|
|
|
return genAssign(lt, a.rs[0]);
|
|
|
|
}
|
2009-08-17 12:29:12 -06:00
|
|
|
// Case 2 or 3
|
2009-09-02 13:03:20 -06:00
|
|
|
as := make([]func(lv Value, t *Thread), len(a.rs));
|
2009-08-17 12:29:12 -06:00
|
|
|
for i, r := range a.rs {
|
|
|
|
as[i] = genAssign(lmt.Elems[i], r);
|
|
|
|
}
|
2009-09-02 13:03:20 -06:00
|
|
|
return func(lv Value, t *Thread) {
|
2009-08-17 12:29:12 -06:00
|
|
|
if effect != nil {
|
2009-09-02 13:03:20 -06:00
|
|
|
effect(t);
|
2009-08-17 12:29:12 -06:00
|
|
|
}
|
|
|
|
lmv := lv.(multiV);
|
|
|
|
for i, a := range as {
|
2009-09-02 13:03:20 -06:00
|
|
|
a(lmv[i], t);
|
2009-08-17 12:29:12 -06:00
|
|
|
}
|
|
|
|
};
|
2009-07-27 18:32:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// compileAssign compiles an assignment operation without the full
|
|
|
|
// generality of an assignCompiler. See assignCompiler for a
|
|
|
|
// description of the arguments.
|
2009-09-02 13:03:20 -06:00
|
|
|
func (a *compiler) compileAssign(pos token.Position, b *block, lt Type, rs []*expr, errOp, errPosName string) (func(Value, *Thread)) {
|
2009-07-27 18:32:35 -06:00
|
|
|
ac, ok := a.checkAssign(pos, rs, errOp, errPosName);
|
|
|
|
if !ok {
|
|
|
|
return nil;
|
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
return ac.compile(b, lt);
|
2009-07-21 16:40:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-08-25 18:57:40 -06:00
|
|
|
* Expression compiler
|
2009-07-21 16:40:41 -06:00
|
|
|
*/
|
2009-07-20 18:41:40 -06:00
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
// An exprCompiler stores information used throughout the compilation
|
|
|
|
// of a single expression. It does not embed funcCompiler because
|
|
|
|
// expressions can appear at top level.
|
|
|
|
type exprCompiler struct {
|
|
|
|
*compiler;
|
|
|
|
// The block this expression is being compiled in.
|
|
|
|
block *block;
|
|
|
|
// Whether this expression is used in a constant context.
|
|
|
|
constant bool;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-08-28 19:03:03 -06:00
|
|
|
// compile compiles an expression AST. callCtx should be true if this
|
|
|
|
// AST is in the function position of a function call node; it allows
|
|
|
|
// the returned expression to be a type or a built-in function (which
|
|
|
|
// otherwise result in errors).
|
|
|
|
func (a *exprCompiler) compile(x ast.Expr, callCtx bool) *expr {
|
2009-08-25 18:57:40 -06:00
|
|
|
ei := &exprInfo{a.compiler, x.Pos()};
|
|
|
|
|
|
|
|
switch x := x.(type) {
|
|
|
|
// Literals
|
2009-09-24 09:25:14 -06:00
|
|
|
case *ast.BasicLit:
|
|
|
|
switch x.Kind {
|
|
|
|
case token.INT:
|
|
|
|
return ei.compileIntLit(string(x.Value));
|
|
|
|
case token.FLOAT:
|
|
|
|
return ei.compileFloatLit(string(x.Value));
|
|
|
|
case token.CHAR:
|
|
|
|
return ei.compileCharLit(string(x.Value));
|
|
|
|
case token.STRING:
|
|
|
|
return ei.compileStringLit(string(x.Value));
|
|
|
|
default:
|
|
|
|
log.Crashf("unexpected basic literal type %v", x.Kind);
|
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
|
|
|
|
case *ast.CompositeLit:
|
|
|
|
goto notimpl;
|
|
|
|
|
|
|
|
case *ast.FuncLit:
|
|
|
|
decl := ei.compileFuncType(a.block, x.Type);
|
|
|
|
if decl == nil {
|
|
|
|
// TODO(austin) Try compiling the body,
|
|
|
|
// perhaps with dummy argument definitions
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
fn := ei.compileFunc(a.block, decl, x.Body);
|
|
|
|
if fn == nil {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
if a.constant {
|
|
|
|
a.diagAt(x, "function literal used in constant expression");
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return ei.compileFuncLit(decl, fn);
|
|
|
|
|
|
|
|
// Types
|
|
|
|
case *ast.ArrayType:
|
2009-08-28 19:03:03 -06:00
|
|
|
// TODO(austin) Use a multi-type case
|
|
|
|
goto typeexpr;
|
2009-08-25 18:57:40 -06:00
|
|
|
|
|
|
|
case *ast.ChanType:
|
2009-08-28 19:03:03 -06:00
|
|
|
goto typeexpr;
|
2009-08-25 18:57:40 -06:00
|
|
|
|
|
|
|
case *ast.Ellipsis:
|
2009-08-28 19:03:03 -06:00
|
|
|
goto typeexpr;
|
2009-08-25 18:57:40 -06:00
|
|
|
|
|
|
|
case *ast.FuncType:
|
2009-08-28 19:03:03 -06:00
|
|
|
goto typeexpr;
|
2009-08-25 18:57:40 -06:00
|
|
|
|
|
|
|
case *ast.InterfaceType:
|
2009-08-28 19:03:03 -06:00
|
|
|
goto typeexpr;
|
2009-08-25 18:57:40 -06:00
|
|
|
|
|
|
|
case *ast.MapType:
|
2009-08-28 19:03:03 -06:00
|
|
|
goto typeexpr;
|
2009-08-25 18:57:40 -06:00
|
|
|
|
|
|
|
// Remaining expressions
|
|
|
|
case *ast.BadExpr:
|
|
|
|
// Error already reported by parser
|
2009-08-27 12:21:52 -06:00
|
|
|
a.silentErrors++;
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
|
|
|
|
|
|
|
case *ast.BinaryExpr:
|
2009-08-28 19:03:03 -06:00
|
|
|
l, r := a.compile(x.X, false), a.compile(x.Y, false);
|
2009-08-25 18:57:40 -06:00
|
|
|
if l == nil || r == nil {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return ei.compileBinaryExpr(x.Op, l, r);
|
|
|
|
|
|
|
|
case *ast.CallExpr:
|
2009-08-28 19:03:03 -06:00
|
|
|
l := a.compile(x.Fun, true);
|
2009-08-25 18:57:40 -06:00
|
|
|
args := make([]*expr, len(x.Args));
|
|
|
|
bad := false;
|
|
|
|
for i, arg := range x.Args {
|
2009-08-28 19:03:03 -06:00
|
|
|
if i == 0 && l.t == Type(makeType) {
|
|
|
|
argei := &exprInfo{a.compiler, arg.Pos()};
|
|
|
|
args[i] = argei.exprFromType(a.compileType(a.block, arg));
|
|
|
|
} else {
|
|
|
|
args[i] = a.compile(arg, false);
|
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
if args[i] == nil {
|
|
|
|
bad = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if l == nil || bad {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
if a.constant {
|
|
|
|
a.diagAt(x, "function call in constant context");
|
|
|
|
return nil;
|
|
|
|
}
|
2009-08-28 19:03:03 -06:00
|
|
|
|
|
|
|
if l.valType != nil {
|
|
|
|
a.diagAt(x, "type conversions not implemented");
|
|
|
|
return nil;
|
|
|
|
} else if ft, ok := l.t.(*FuncType); ok && ft.builtin != "" {
|
|
|
|
return ei.compileBuiltinCallExpr(a.block, ft, args);
|
|
|
|
} else {
|
|
|
|
return ei.compileCallExpr(a.block, l, args);
|
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
|
|
|
|
case *ast.Ident:
|
2009-08-28 19:03:03 -06:00
|
|
|
return ei.compileIdent(a.block, a.constant, callCtx, x.Value);
|
2009-08-25 18:57:40 -06:00
|
|
|
|
|
|
|
case *ast.IndexExpr:
|
|
|
|
if x.End != nil {
|
|
|
|
a.diagAt(x, "slice expression not implemented");
|
|
|
|
return nil;
|
|
|
|
}
|
2009-08-28 19:03:03 -06:00
|
|
|
l, r := a.compile(x.X, false), a.compile(x.Index, false);
|
2009-08-25 18:57:40 -06:00
|
|
|
if l == nil || r == nil {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return ei.compileIndexExpr(l, r);
|
|
|
|
|
|
|
|
case *ast.KeyValueExpr:
|
|
|
|
goto notimpl;
|
|
|
|
|
|
|
|
case *ast.ParenExpr:
|
2009-08-28 19:03:03 -06:00
|
|
|
return a.compile(x.X, callCtx);
|
2009-08-25 18:57:40 -06:00
|
|
|
|
|
|
|
case *ast.SelectorExpr:
|
2009-08-28 19:03:03 -06:00
|
|
|
v := a.compile(x.X, false);
|
2009-08-25 18:57:40 -06:00
|
|
|
if v == nil {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return ei.compileSelectorExpr(v, x.Sel.Value);
|
|
|
|
|
|
|
|
case *ast.StarExpr:
|
2009-08-28 19:03:03 -06:00
|
|
|
// We pass down our call context because this could be
|
|
|
|
// a pointer type (and thus a type conversion)
|
|
|
|
v := a.compile(x.X, callCtx);
|
2009-08-25 18:57:40 -06:00
|
|
|
if v == nil {
|
|
|
|
return nil;
|
|
|
|
}
|
2009-08-28 19:03:03 -06:00
|
|
|
if v.valType != nil {
|
|
|
|
// Turns out this was a pointer type, not a dereference
|
|
|
|
return ei.exprFromType(NewPtrType(v.valType));
|
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
return ei.compileStarExpr(v);
|
|
|
|
|
|
|
|
case *ast.StringList:
|
|
|
|
strings := make([]*expr, len(x.Strings));
|
|
|
|
bad := false;
|
|
|
|
for i, s := range x.Strings {
|
2009-08-28 19:03:03 -06:00
|
|
|
strings[i] = a.compile(s, false);
|
2009-08-25 18:57:40 -06:00
|
|
|
if strings[i] == nil {
|
|
|
|
bad = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if bad {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return ei.compileStringList(strings);
|
|
|
|
|
|
|
|
case *ast.StructType:
|
|
|
|
goto notimpl;
|
|
|
|
|
|
|
|
case *ast.TypeAssertExpr:
|
|
|
|
goto notimpl;
|
|
|
|
|
|
|
|
case *ast.UnaryExpr:
|
2009-08-28 19:03:03 -06:00
|
|
|
v := a.compile(x.X, false);
|
2009-08-25 18:57:40 -06:00
|
|
|
if v == nil {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return ei.compileUnaryExpr(x.Op, v);
|
|
|
|
}
|
|
|
|
log.Crashf("unexpected ast node type %T", x);
|
|
|
|
panic();
|
|
|
|
|
2009-08-28 19:03:03 -06:00
|
|
|
typeexpr:
|
|
|
|
if !callCtx {
|
|
|
|
a.diagAt(x, "type used as expression");
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return ei.exprFromType(a.compileType(a.block, x));
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
notimpl:
|
|
|
|
a.diagAt(x, "%T expression node not implemented", x);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2009-08-28 19:03:03 -06:00
|
|
|
func (a *exprInfo) exprFromType(t Type) *expr {
|
|
|
|
if t == nil {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
expr := a.newExpr(nil, "type");
|
|
|
|
expr.valType = t;
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *exprInfo) compileIdent(b *block, constant bool, callCtx bool, name string) *expr {
|
2009-09-02 18:15:43 -06:00
|
|
|
bl, level, def := b.Lookup(name);
|
2009-07-15 12:59:13 -06:00
|
|
|
if def == nil {
|
2009-08-25 18:57:40 -06:00
|
|
|
a.diag("%s: undefined", name);
|
|
|
|
return nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
switch def := def.(type) {
|
|
|
|
case *Constant:
|
2009-08-25 18:57:40 -06:00
|
|
|
expr := a.newExpr(def.Type, "constant");
|
2009-08-28 19:03:03 -06:00
|
|
|
if ft, ok := def.Type.(*FuncType); ok && ft.builtin != "" {
|
|
|
|
// XXX(Spec) I don't think anything says that
|
|
|
|
// built-in functions can't be used as values.
|
|
|
|
if !callCtx {
|
|
|
|
a.diag("built-in function %s cannot be used as a value", ft.builtin);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
// Otherwise, we leave the evaluators empty
|
|
|
|
// because this is handled specially
|
|
|
|
} else {
|
|
|
|
expr.genConstant(def.Value);
|
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
return expr;
|
2009-07-15 12:59:13 -06:00
|
|
|
case *Variable:
|
2009-08-25 18:57:40 -06:00
|
|
|
if constant {
|
|
|
|
a.diag("variable %s used in constant expression", name);
|
|
|
|
return nil;
|
2009-07-27 18:32:35 -06:00
|
|
|
}
|
2009-09-03 17:20:49 -06:00
|
|
|
if bl.global {
|
2009-09-02 18:15:43 -06:00
|
|
|
return a.compileGlobalVariable(def);
|
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
return a.compileVariable(level, def);
|
2009-07-15 12:59:13 -06:00
|
|
|
case Type:
|
2009-08-28 19:03:03 -06:00
|
|
|
if callCtx {
|
|
|
|
return a.exprFromType(def);
|
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
a.diag("type %v used as expression", name);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
log.Crashf("name %s has unknown type %T", name, def);
|
|
|
|
panic();
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *exprInfo) compileVariable(level int, v *Variable) *expr {
|
|
|
|
if v.Type == nil {
|
|
|
|
// Placeholder definition from an earlier error
|
2009-08-27 12:21:52 -06:00
|
|
|
a.silentErrors++;
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
expr := a.newExpr(v.Type, "variable");
|
|
|
|
expr.genIdentOp(level, v.Index);
|
|
|
|
return expr;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-09-02 18:15:43 -06:00
|
|
|
func (a *exprInfo) compileGlobalVariable(v *Variable) *expr {
|
|
|
|
if v.Type == nil {
|
|
|
|
// Placeholder definition from an earlier error
|
|
|
|
a.silentErrors++;
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
if v.Init == nil {
|
|
|
|
v.Init = v.Type.Zero();
|
|
|
|
}
|
|
|
|
expr := a.newExpr(v.Type, "variable");
|
|
|
|
val := v.Init;
|
|
|
|
expr.genValue(func(t *Thread) Value { return val });
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
func (a *exprInfo) compileIdealInt(i *bignum.Integer, desc string) *expr {
|
|
|
|
expr := a.newExpr(IdealIntType, desc);
|
2009-09-01 12:51:33 -06:00
|
|
|
expr.eval = func() *bignum.Integer { return i };
|
2009-08-25 18:57:40 -06:00
|
|
|
return expr;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
func (a *exprInfo) compileIntLit(lit string) *expr {
|
2009-09-24 09:25:14 -06:00
|
|
|
i, _, _ := bignum.IntFromString(lit, 0);
|
2009-08-25 18:57:40 -06:00
|
|
|
return a.compileIdealInt(i, "integer literal");
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
func (a *exprInfo) compileCharLit(lit string) *expr {
|
|
|
|
if lit[0] != '\'' {
|
2009-09-01 17:17:50 -06:00
|
|
|
// Caught by parser
|
|
|
|
a.silentErrors++;
|
|
|
|
return nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
2009-09-24 09:25:14 -06:00
|
|
|
v, _, tail, err := strconv.UnquoteChar(lit[1:len(lit)], '\'');
|
2009-07-17 17:58:59 -06:00
|
|
|
if err != nil || tail != "'" {
|
2009-09-01 17:17:50 -06:00
|
|
|
// Caught by parser
|
|
|
|
a.silentErrors++;
|
|
|
|
return nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
return a.compileIdealInt(bignum.Int(int64(v)), "character literal");
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
func (a *exprInfo) compileFloatLit(lit string) *expr {
|
|
|
|
f, _, n := bignum.RatFromString(lit, 0);
|
|
|
|
if n != len(lit) {
|
|
|
|
log.Crashf("malformed float literal %s at %v passed parser", lit, a.pos);
|
2009-07-17 17:58:59 -06:00
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
expr := a.newExpr(IdealFloatType, "float literal");
|
2009-09-01 12:51:33 -06:00
|
|
|
expr.eval = func() *bignum.Rational { return f };
|
2009-08-25 18:57:40 -06:00
|
|
|
return expr;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
func (a *exprInfo) compileString(s string) *expr {
|
2009-08-10 17:27:54 -06:00
|
|
|
// Ideal strings don't have a named type but they are
|
|
|
|
// compatible with type string.
|
|
|
|
|
|
|
|
// TODO(austin) Use unnamed string type.
|
2009-08-25 18:57:40 -06:00
|
|
|
expr := a.newExpr(StringType, "string literal");
|
2009-09-02 13:03:20 -06:00
|
|
|
expr.eval = func(*Thread) string { return s };
|
2009-08-25 18:57:40 -06:00
|
|
|
return expr;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
func (a *exprInfo) compileStringLit(lit string) *expr {
|
|
|
|
s, err := strconv.Unquote(lit);
|
2009-07-15 12:59:13 -06:00
|
|
|
if err != nil {
|
|
|
|
a.diag("illegal string literal, %v", err);
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
return a.compileString(s);
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
func (a *exprInfo) compileStringList(list []*expr) *expr {
|
|
|
|
ss := make([]string, len(list));
|
|
|
|
for i, s := range list {
|
|
|
|
ss[i] = s.asString()(nil);
|
2009-07-27 14:01:23 -06:00
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
return a.compileString(strings.Join(ss, ""));
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-09-02 13:03:20 -06:00
|
|
|
func (a *exprInfo) compileFuncLit(decl *FuncDecl, fn func(*Thread) Func) *expr {
|
2009-08-25 18:57:40 -06:00
|
|
|
expr := a.newExpr(decl.Type, "function literal");
|
2009-09-01 12:51:33 -06:00
|
|
|
expr.eval = fn;
|
2009-08-25 18:57:40 -06:00
|
|
|
return expr;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
func (a *exprInfo) compileSelectorExpr(v *expr, name string) *expr {
|
2009-08-05 12:49:51 -06:00
|
|
|
// mark marks a field that matches the selector name. It
|
|
|
|
// tracks the best depth found so far and whether more than
|
|
|
|
// one field has been found at that depth.
|
|
|
|
bestDepth := -1;
|
|
|
|
ambig := false;
|
|
|
|
amberr := "";
|
|
|
|
mark := func(depth int, pathName string) {
|
|
|
|
switch {
|
|
|
|
case bestDepth == -1 || depth < bestDepth:
|
|
|
|
bestDepth = depth;
|
|
|
|
ambig = false;
|
|
|
|
amberr = "";
|
|
|
|
|
|
|
|
case depth == bestDepth:
|
|
|
|
ambig = true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
log.Crashf("Marked field at depth %d, but already found one at depth %d", depth, bestDepth);
|
|
|
|
}
|
|
|
|
amberr += "\n\t" + pathName[1:len(pathName)];
|
|
|
|
};
|
|
|
|
|
|
|
|
visited := make(map[Type] bool);
|
|
|
|
|
|
|
|
// find recursively searches for the named field, starting at
|
|
|
|
// type t. If it finds the named field, it returns a function
|
2009-08-25 18:57:40 -06:00
|
|
|
// which takes an expr that represents a value of type 't' and
|
|
|
|
// returns an expr that retrieves the named field. We delay
|
|
|
|
// expr construction to avoid producing lots of useless expr's
|
|
|
|
// as we search.
|
|
|
|
//
|
|
|
|
// TODO(austin) Now that the expression compiler works on
|
|
|
|
// semantic values instead of AST's, there should be a much
|
|
|
|
// better way of doing this.
|
|
|
|
var find func(Type, int, string) (func (*expr) *expr);
|
|
|
|
find = func(t Type, depth int, pathName string) (func (*expr) *expr) {
|
2009-08-05 12:49:51 -06:00
|
|
|
// Don't bother looking if we've found something shallower
|
|
|
|
if bestDepth != -1 && bestDepth < depth {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't check the same type twice and avoid loops
|
|
|
|
if _, ok := visited[t]; ok {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
visited[t] = true;
|
|
|
|
|
|
|
|
// Implicit dereference
|
|
|
|
deref := false;
|
|
|
|
if ti, ok := t.(*PtrType); ok {
|
|
|
|
deref = true;
|
|
|
|
t = ti.Elem;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's a named type, look for methods
|
|
|
|
if ti, ok := t.(*NamedType); ok {
|
2009-09-24 09:25:14 -06:00
|
|
|
_, ok := ti.methods[name];
|
2009-08-05 12:49:51 -06:00
|
|
|
if ok {
|
|
|
|
mark(depth, pathName + "." + name);
|
|
|
|
log.Crash("Methods not implemented");
|
|
|
|
}
|
2009-08-28 13:45:30 -06:00
|
|
|
t = ti.Def;
|
2009-08-05 12:49:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// If it's a struct type, check fields and embedded types
|
2009-08-25 18:57:40 -06:00
|
|
|
var builder func(*expr) *expr;
|
2009-08-05 12:49:51 -06:00
|
|
|
if t, ok := t.(*StructType); ok {
|
2009-08-17 12:29:12 -06:00
|
|
|
for i, f := range t.Elems {
|
2009-08-25 18:57:40 -06:00
|
|
|
var sub func(*expr) *expr;
|
2009-08-05 12:49:51 -06:00
|
|
|
switch {
|
|
|
|
case f.Name == name:
|
|
|
|
mark(depth, pathName + "." + name);
|
2009-08-25 18:57:40 -06:00
|
|
|
sub = func(e *expr) *expr { return e };
|
2009-08-05 12:49:51 -06:00
|
|
|
|
|
|
|
case f.Anonymous:
|
|
|
|
sub = find(f.Type, depth+1, pathName + "." + f.Name);
|
|
|
|
if sub == nil {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We found something. Create a
|
|
|
|
// builder for accessing this field.
|
|
|
|
ft := f.Type;
|
|
|
|
index := i;
|
2009-08-25 18:57:40 -06:00
|
|
|
builder = func(parent *expr) *expr {
|
2009-08-05 12:49:51 -06:00
|
|
|
if deref {
|
2009-08-25 18:57:40 -06:00
|
|
|
parent = a.compileStarExpr(parent);
|
2009-08-05 12:49:51 -06:00
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
expr := a.newExpr(ft, "selector expression");
|
|
|
|
pf := parent.asStruct();
|
2009-09-02 13:03:20 -06:00
|
|
|
evalAddr := func(t *Thread) Value {
|
2009-09-03 18:14:49 -06:00
|
|
|
return pf(t).Field(t, index);
|
2009-08-25 18:57:40 -06:00
|
|
|
};
|
|
|
|
expr.genValue(evalAddr);
|
|
|
|
return sub(expr);
|
2009-08-05 12:49:51 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
};
|
|
|
|
|
|
|
|
builder := find(v.t, 0, "");
|
|
|
|
if builder == nil {
|
|
|
|
a.diag("type %v has no field or method %s", v.t, name);
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-08-05 12:49:51 -06:00
|
|
|
}
|
|
|
|
if ambig {
|
|
|
|
a.diag("field %s is ambiguous in type %v%s", name, v.t, amberr);
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-08-05 12:49:51 -06:00
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
return builder(v);
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
func (a *exprInfo) compileIndexExpr(l, r *expr) *expr {
|
2009-07-20 18:41:40 -06:00
|
|
|
// Type check object
|
2009-08-28 19:03:03 -06:00
|
|
|
l = l.derefArray();
|
2009-07-20 18:41:40 -06:00
|
|
|
|
|
|
|
var at Type;
|
|
|
|
intIndex := false;
|
|
|
|
var maxIndex int64 = -1;
|
|
|
|
|
2009-07-31 18:11:34 -06:00
|
|
|
switch lt := l.t.lit().(type) {
|
2009-07-20 18:41:40 -06:00
|
|
|
case *ArrayType:
|
|
|
|
at = lt.Elem;
|
|
|
|
intIndex = true;
|
|
|
|
maxIndex = lt.Len;
|
|
|
|
|
2009-08-17 12:29:12 -06:00
|
|
|
case *SliceType:
|
|
|
|
at = lt.Elem;
|
|
|
|
intIndex = true;
|
2009-07-20 18:41:40 -06:00
|
|
|
|
|
|
|
case *stringType:
|
|
|
|
at = Uint8Type;
|
|
|
|
intIndex = true;
|
|
|
|
|
2009-08-21 19:37:38 -06:00
|
|
|
case *MapType:
|
|
|
|
at = lt.Elem;
|
|
|
|
if r.t.isIdeal() {
|
|
|
|
r = r.convertTo(lt.Key);
|
|
|
|
if r == nil {
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-08-21 19:37:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !lt.Key.compat(r.t, false) {
|
|
|
|
a.diag("cannot use %s as index into %s", r.t, lt);
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-08-21 19:37:38 -06:00
|
|
|
}
|
2009-07-20 18:41:40 -06:00
|
|
|
|
|
|
|
default:
|
|
|
|
a.diag("cannot index into %v", l.t);
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-07-20 18:41:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Type check index and convert to int if necessary
|
|
|
|
if intIndex {
|
|
|
|
// XXX(Spec) It's unclear if ideal floats with no
|
|
|
|
// fractional part are allowed here. 6g allows it. I
|
|
|
|
// believe that's wrong.
|
2009-08-28 19:03:03 -06:00
|
|
|
r = r.convertToInt(maxIndex, "index", "index");
|
|
|
|
if r == nil {
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-07-20 18:41:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
expr := a.newExpr(at, "index expression");
|
2009-07-20 18:41:40 -06:00
|
|
|
|
|
|
|
// Compile
|
2009-07-31 18:11:34 -06:00
|
|
|
switch lt := l.t.lit().(type) {
|
2009-07-20 18:41:40 -06:00
|
|
|
case *ArrayType:
|
|
|
|
lf := l.asArray();
|
|
|
|
rf := r.asInt();
|
2009-08-28 11:39:57 -06:00
|
|
|
bound := lt.Len;
|
2009-09-02 13:03:20 -06:00
|
|
|
expr.genValue(func(t *Thread) Value {
|
|
|
|
l, r := lf(t), rf(t);
|
2009-08-28 11:39:57 -06:00
|
|
|
if r < 0 || r >= bound {
|
2009-09-02 15:11:40 -06:00
|
|
|
t.Abort(IndexError{r, bound});
|
2009-08-28 11:39:57 -06:00
|
|
|
}
|
2009-09-03 18:14:49 -06:00
|
|
|
return l.Elem(t, r);
|
2009-08-28 11:39:57 -06:00
|
|
|
});
|
2009-07-20 18:41:40 -06:00
|
|
|
|
2009-08-17 12:29:12 -06:00
|
|
|
case *SliceType:
|
|
|
|
lf := l.asSlice();
|
|
|
|
rf := r.asInt();
|
2009-09-02 13:03:20 -06:00
|
|
|
expr.genValue(func(t *Thread) Value {
|
|
|
|
l, r := lf(t), rf(t);
|
2009-08-28 11:39:57 -06:00
|
|
|
if l.Base == nil {
|
2009-09-02 15:11:40 -06:00
|
|
|
t.Abort(NilPointerError{});
|
2009-08-28 11:39:57 -06:00
|
|
|
}
|
|
|
|
if r < 0 || r >= l.Len {
|
2009-09-02 15:11:40 -06:00
|
|
|
t.Abort(IndexError{r, l.Len});
|
2009-08-28 11:39:57 -06:00
|
|
|
}
|
2009-09-03 18:14:49 -06:00
|
|
|
return l.Base.Elem(t, r);
|
2009-08-28 11:39:57 -06:00
|
|
|
});
|
2009-08-17 12:29:12 -06:00
|
|
|
|
2009-07-27 14:01:23 -06:00
|
|
|
case *stringType:
|
|
|
|
lf := l.asString();
|
|
|
|
rf := r.asInt();
|
|
|
|
// TODO(austin) This pulls over the whole string in a
|
|
|
|
// remote setting, instead of just the one character.
|
2009-09-02 13:03:20 -06:00
|
|
|
expr.eval = func(t *Thread) uint64 {
|
|
|
|
l, r := lf(t), rf(t);
|
2009-08-28 11:39:57 -06:00
|
|
|
if r < 0 || r >= int64(len(l)) {
|
2009-09-02 15:11:40 -06:00
|
|
|
t.Abort(IndexError{r, int64(len(l))});
|
2009-08-28 11:39:57 -06:00
|
|
|
}
|
|
|
|
return uint64(l[r]);
|
2009-07-27 14:01:23 -06:00
|
|
|
}
|
|
|
|
|
2009-08-21 19:37:38 -06:00
|
|
|
case *MapType:
|
|
|
|
lf := l.asMap();
|
|
|
|
rf := r.asInterface();
|
2009-09-02 13:03:20 -06:00
|
|
|
expr.genValue(func(t *Thread) Value {
|
|
|
|
m := lf(t);
|
|
|
|
k := rf(t);
|
2009-08-28 19:03:03 -06:00
|
|
|
if m == nil {
|
2009-09-02 15:11:40 -06:00
|
|
|
t.Abort(NilPointerError{});
|
2009-08-28 19:03:03 -06:00
|
|
|
}
|
2009-09-03 18:14:49 -06:00
|
|
|
e := m.Elem(t, k);
|
2009-08-21 19:37:38 -06:00
|
|
|
if e == nil {
|
2009-09-02 15:11:40 -06:00
|
|
|
t.Abort(KeyError{k});
|
2009-08-21 19:37:38 -06:00
|
|
|
}
|
|
|
|
return e;
|
|
|
|
});
|
|
|
|
// genValue makes things addressable, but map values
|
|
|
|
// aren't addressable.
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.evalAddr = nil;
|
2009-09-02 13:03:20 -06:00
|
|
|
expr.evalMapValue = func(t *Thread) (Map, interface{}) {
|
2009-08-28 19:03:03 -06:00
|
|
|
// TODO(austin) Key check? nil check?
|
2009-09-02 13:03:20 -06:00
|
|
|
return lf(t), rf(t);
|
2009-08-21 19:37:38 -06:00
|
|
|
};
|
|
|
|
|
2009-07-20 18:41:40 -06:00
|
|
|
default:
|
2009-08-21 19:37:38 -06:00
|
|
|
log.Crashf("unexpected left operand type %T", l.t.lit());
|
2009-07-20 18:41:40 -06:00
|
|
|
}
|
2009-07-15 12:59:13 -06:00
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
return expr;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
func (a *exprInfo) compileCallExpr(b *block, l *expr, as []*expr) *expr {
|
2009-07-27 14:01:23 -06:00
|
|
|
// TODO(austin) Variadic functions.
|
|
|
|
|
|
|
|
// Type check
|
|
|
|
|
|
|
|
// XXX(Spec) Calling a named function type is okay. I really
|
|
|
|
// think there needs to be a general discussion of named
|
|
|
|
// types. A named type creates a new, distinct type, but the
|
|
|
|
// type of that type is still whatever it's defined to. Thus,
|
|
|
|
// in "type Foo int", Foo is still an integer type and in
|
|
|
|
// "type Foo func()", Foo is a function type.
|
2009-07-31 18:11:34 -06:00
|
|
|
lt, ok := l.t.lit().(*FuncType);
|
2009-07-27 14:01:23 -06:00
|
|
|
if !ok {
|
|
|
|
a.diag("cannot call non-function type %v", l.t);
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-07-27 14:01:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// The arguments must be single-valued expressions assignment
|
|
|
|
// compatible with the parameters of F.
|
2009-07-27 18:32:35 -06:00
|
|
|
//
|
|
|
|
// XXX(Spec) The spec is wrong. It can also be a single
|
|
|
|
// multi-valued expression.
|
2009-07-29 12:57:46 -06:00
|
|
|
nin := len(lt.In);
|
2009-08-25 18:57:40 -06:00
|
|
|
assign := a.compileAssign(a.pos, b, NewMultiType(lt.In), as, "function call", "argument");
|
2009-07-27 18:32:35 -06:00
|
|
|
if assign == nil {
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-07-27 14:01:23 -06:00
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
var t Type;
|
2009-07-27 18:32:35 -06:00
|
|
|
nout := len(lt.Out);
|
|
|
|
switch nout {
|
|
|
|
case 0:
|
2009-08-25 18:57:40 -06:00
|
|
|
t = EmptyType;
|
2009-07-27 18:32:35 -06:00
|
|
|
case 1:
|
2009-08-25 18:57:40 -06:00
|
|
|
t = lt.Out[0];
|
2009-07-27 18:32:35 -06:00
|
|
|
default:
|
2009-08-25 18:57:40 -06:00
|
|
|
t = NewMultiType(lt.Out);
|
2009-07-27 14:01:23 -06:00
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
expr := a.newExpr(t, "function call");
|
2009-07-27 14:01:23 -06:00
|
|
|
|
2009-07-29 12:57:46 -06:00
|
|
|
// Gather argument and out types to initialize frame variables
|
|
|
|
vts := make([]Type, nin + nout);
|
|
|
|
for i, t := range lt.In {
|
|
|
|
vts[i] = t;
|
|
|
|
}
|
|
|
|
for i, t := range lt.Out {
|
|
|
|
vts[i+nin] = t;
|
|
|
|
}
|
|
|
|
|
2009-07-27 14:01:23 -06:00
|
|
|
// Compile
|
|
|
|
lf := l.asFunc();
|
2009-09-02 13:03:20 -06:00
|
|
|
call := func(t *Thread) []Value {
|
|
|
|
fun := lf(t);
|
2009-07-27 14:01:23 -06:00
|
|
|
fr := fun.NewFrame();
|
2009-07-29 12:57:46 -06:00
|
|
|
for i, t := range vts {
|
|
|
|
fr.Vars[i] = t.Zero();
|
|
|
|
}
|
2009-09-02 13:03:20 -06:00
|
|
|
assign(multiV(fr.Vars[0:nin]), t);
|
|
|
|
oldf := t.f;
|
|
|
|
t.f = fr;
|
|
|
|
fun.Call(t);
|
|
|
|
t.f = oldf;
|
2009-07-27 18:32:35 -06:00
|
|
|
return fr.Vars[nin:nin+nout];
|
2009-07-27 14:01:23 -06:00
|
|
|
};
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.genFuncCall(call);
|
2009-07-15 12:59:13 -06:00
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
return expr;
|
|
|
|
}
|
2009-07-15 12:59:13 -06:00
|
|
|
|
2009-08-28 19:03:03 -06:00
|
|
|
func (a *exprInfo) compileBuiltinCallExpr(b *block, ft *FuncType, as []*expr) *expr {
|
|
|
|
checkCount := func(min, max int) bool {
|
|
|
|
if len(as) < min {
|
|
|
|
a.diag("not enough arguments to %s", ft.builtin);
|
|
|
|
return false;
|
|
|
|
} else if len(as) > max {
|
|
|
|
a.diag("too many arguments to %s", ft.builtin);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch ft {
|
|
|
|
case capType:
|
|
|
|
if !checkCount(1, 1) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
arg := as[0].derefArray();
|
|
|
|
expr := a.newExpr(IntType, "function call");
|
|
|
|
switch t := arg.t.lit().(type) {
|
|
|
|
case *ArrayType:
|
|
|
|
// TODO(austin) It would be nice if this could
|
|
|
|
// be a constant int.
|
|
|
|
v := t.Len;
|
2009-09-02 13:03:20 -06:00
|
|
|
expr.eval = func(t *Thread) int64 {
|
2009-08-28 19:03:03 -06:00
|
|
|
return v;
|
|
|
|
};
|
|
|
|
|
|
|
|
case *SliceType:
|
|
|
|
vf := arg.asSlice();
|
2009-09-02 13:03:20 -06:00
|
|
|
expr.eval = func(t *Thread) int64 {
|
|
|
|
return vf(t).Cap;
|
2009-08-28 19:03:03 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
//case *ChanType:
|
|
|
|
|
|
|
|
default:
|
|
|
|
a.diag("illegal argument type for cap function\n\t%v", arg.t);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return expr;
|
|
|
|
|
|
|
|
case lenType:
|
|
|
|
if !checkCount(1, 1) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
arg := as[0].derefArray();
|
|
|
|
expr := a.newExpr(IntType, "function call");
|
|
|
|
switch t := arg.t.lit().(type) {
|
|
|
|
case *stringType:
|
|
|
|
vf := arg.asString();
|
2009-09-02 13:03:20 -06:00
|
|
|
expr.eval = func(t *Thread) int64 {
|
|
|
|
return int64(len(vf(t)));
|
2009-08-28 19:03:03 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
case *ArrayType:
|
|
|
|
// TODO(austin) It would be nice if this could
|
|
|
|
// be a constant int.
|
|
|
|
v := t.Len;
|
2009-09-02 13:03:20 -06:00
|
|
|
expr.eval = func(t *Thread) int64 {
|
2009-08-28 19:03:03 -06:00
|
|
|
return v;
|
|
|
|
};
|
|
|
|
|
|
|
|
case *SliceType:
|
|
|
|
vf := arg.asSlice();
|
2009-09-02 13:03:20 -06:00
|
|
|
expr.eval = func(t *Thread) int64 {
|
|
|
|
return vf(t).Len;
|
2009-08-28 19:03:03 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
case *MapType:
|
|
|
|
vf := arg.asMap();
|
2009-09-02 13:03:20 -06:00
|
|
|
expr.eval = func(t *Thread) int64 {
|
2009-08-28 19:03:03 -06:00
|
|
|
// XXX(Spec) What's the len of an
|
|
|
|
// uninitialized map?
|
2009-09-02 13:03:20 -06:00
|
|
|
m := vf(t);
|
2009-08-28 19:03:03 -06:00
|
|
|
if m == nil {
|
|
|
|
return 0;
|
|
|
|
}
|
2009-09-03 18:14:49 -06:00
|
|
|
return m.Len(t);
|
2009-08-28 19:03:03 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
//case *ChanType:
|
|
|
|
|
|
|
|
default:
|
|
|
|
a.diag("illegal argument type for len function\n\t%v", arg.t);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return expr;
|
|
|
|
|
|
|
|
case makeType:
|
|
|
|
if !checkCount(1, 3) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
// XXX(Spec) What are the types of the
|
|
|
|
// arguments? Do they have to be ints? 6g
|
|
|
|
// accepts any integral type.
|
|
|
|
var lenexpr, capexpr *expr;
|
2009-09-02 13:03:20 -06:00
|
|
|
var lenf, capf func(*Thread) int64;
|
2009-08-28 19:03:03 -06:00
|
|
|
if len(as) > 1 {
|
|
|
|
lenexpr = as[1].convertToInt(-1, "length", "make function");
|
|
|
|
if lenexpr == nil {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
lenf = lenexpr.asInt();
|
|
|
|
}
|
|
|
|
if len(as) > 2 {
|
|
|
|
capexpr = as[2].convertToInt(-1, "capacity", "make function");
|
|
|
|
if capexpr == nil {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
capf = capexpr.asInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch t := as[0].valType.lit().(type) {
|
|
|
|
case *SliceType:
|
|
|
|
// A new, initialized slice value for a given
|
|
|
|
// element type T is made using the built-in
|
|
|
|
// function make, which takes a slice type and
|
|
|
|
// parameters specifying the length and
|
|
|
|
// optionally the capacity.
|
|
|
|
if !checkCount(2, 3) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
et := t.Elem;
|
|
|
|
expr := a.newExpr(t, "function call");
|
2009-09-02 13:03:20 -06:00
|
|
|
expr.eval = func(t *Thread) Slice {
|
|
|
|
l := lenf(t);
|
2009-08-28 19:03:03 -06:00
|
|
|
// XXX(Spec) What if len or cap is
|
|
|
|
// negative? The runtime panics.
|
|
|
|
if l < 0 {
|
2009-09-02 15:11:40 -06:00
|
|
|
t.Abort(NegativeLengthError{l});
|
2009-08-28 19:03:03 -06:00
|
|
|
}
|
|
|
|
c := l;
|
|
|
|
if capf != nil {
|
2009-09-02 13:03:20 -06:00
|
|
|
c = capf(t);
|
2009-08-28 19:03:03 -06:00
|
|
|
if c < 0 {
|
2009-09-02 15:11:40 -06:00
|
|
|
t.Abort(NegativeCapacityError{c});
|
2009-08-28 19:03:03 -06:00
|
|
|
}
|
|
|
|
// XXX(Spec) What happens if
|
|
|
|
// len > cap? The runtime
|
|
|
|
// sets cap to len.
|
|
|
|
if l > c {
|
|
|
|
c = l;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
base := arrayV(make([]Value, c));
|
|
|
|
for i := int64(0); i < c; i++ {
|
|
|
|
base[i] = et.Zero();
|
|
|
|
}
|
|
|
|
return Slice{&base, l, c};
|
|
|
|
};
|
|
|
|
return expr;
|
|
|
|
|
|
|
|
case *MapType:
|
|
|
|
// A new, empty map value is made using the
|
|
|
|
// built-in function make, which takes the map
|
|
|
|
// type and an optional capacity hint as
|
|
|
|
// arguments.
|
|
|
|
if !checkCount(1, 2) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
expr := a.newExpr(t, "function call");
|
2009-09-02 13:03:20 -06:00
|
|
|
expr.eval = func(t *Thread) Map {
|
2009-08-28 19:03:03 -06:00
|
|
|
if lenf == nil {
|
|
|
|
return make(evalMap);
|
|
|
|
}
|
2009-09-02 13:03:20 -06:00
|
|
|
l := lenf(t);
|
2009-08-28 19:03:03 -06:00
|
|
|
return make(evalMap, l);
|
|
|
|
};
|
|
|
|
return expr;
|
|
|
|
|
|
|
|
//case *ChanType:
|
|
|
|
|
|
|
|
default:
|
|
|
|
a.diag("illegal argument type for make function\n\t%v", as[0].valType);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
case closeType, closedType, newType, panicType, paniclnType, printType, printlnType:
|
|
|
|
a.diag("built-in function %s not implemented", ft.builtin);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Crashf("unexpected built-in function '%s'", ft.builtin);
|
|
|
|
panic();
|
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
func (a *exprInfo) compileStarExpr(v *expr) *expr {
|
2009-07-31 18:11:34 -06:00
|
|
|
switch vt := v.t.lit().(type) {
|
2009-07-15 12:59:13 -06:00
|
|
|
case *PtrType:
|
2009-08-25 18:57:40 -06:00
|
|
|
expr := a.newExpr(vt.Elem, "indirect expression");
|
2009-08-28 11:39:57 -06:00
|
|
|
vf := v.asPtr();
|
2009-09-02 13:03:20 -06:00
|
|
|
expr.genValue(func(t *Thread) Value {
|
|
|
|
v := vf(t);
|
2009-08-28 11:39:57 -06:00
|
|
|
if v == nil {
|
2009-09-02 15:11:40 -06:00
|
|
|
t.Abort(NilPointerError{});
|
2009-08-28 11:39:57 -06:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
});
|
2009-08-25 18:57:40 -06:00
|
|
|
return expr;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
a.diagOpType(token.MUL, v.t);
|
|
|
|
return nil;
|
2009-07-27 14:01:23 -06:00
|
|
|
}
|
|
|
|
|
2009-07-17 11:38:44 -06:00
|
|
|
var unaryOpDescs = make(map[token.Token] string)
|
2009-07-15 18:56:17 -06:00
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
func (a *exprInfo) compileUnaryExpr(op token.Token, v *expr) *expr {
|
2009-07-17 11:38:44 -06:00
|
|
|
// Type check
|
2009-08-25 18:57:40 -06:00
|
|
|
var t Type;
|
|
|
|
switch op {
|
2009-07-17 11:38:44 -06:00
|
|
|
case token.ADD, token.SUB:
|
|
|
|
if !v.t.isInteger() && !v.t.isFloat() {
|
2009-08-25 18:57:40 -06:00
|
|
|
a.diagOpType(op, v.t);
|
|
|
|
return nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
t = v.t;
|
2009-07-15 12:59:13 -06:00
|
|
|
|
2009-07-17 11:38:44 -06:00
|
|
|
case token.NOT:
|
|
|
|
if !v.t.isBoolean() {
|
2009-08-25 18:57:40 -06:00
|
|
|
a.diagOpType(op, v.t);
|
|
|
|
return nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
t = BoolType;
|
2009-07-15 12:59:13 -06:00
|
|
|
|
2009-07-17 11:38:44 -06:00
|
|
|
case token.XOR:
|
|
|
|
if !v.t.isInteger() {
|
2009-08-25 18:57:40 -06:00
|
|
|
a.diagOpType(op, v.t);
|
|
|
|
return nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
t = v.t;
|
2009-07-15 12:59:13 -06:00
|
|
|
|
2009-07-17 11:38:44 -06:00
|
|
|
case token.AND:
|
2009-07-15 12:59:13 -06:00
|
|
|
// The unary prefix address-of operator & generates
|
|
|
|
// the address of its operand, which must be a
|
|
|
|
// variable, pointer indirection, field selector, or
|
|
|
|
// array or slice indexing operation.
|
2009-07-15 18:56:17 -06:00
|
|
|
if v.evalAddr == nil {
|
2009-07-15 12:59:13 -06:00
|
|
|
a.diag("cannot take the address of %s", v.desc);
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(austin) Implement "It is illegal to take the
|
|
|
|
// address of a function result variable" once I have
|
|
|
|
// function result variables.
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
t = NewPtrType(v.t);
|
2009-07-17 11:38:44 -06:00
|
|
|
|
|
|
|
case token.ARROW:
|
2009-08-25 18:57:40 -06:00
|
|
|
log.Crashf("Unary op %v not implemented", op);
|
2009-07-15 12:59:13 -06:00
|
|
|
|
2009-07-17 11:38:44 -06:00
|
|
|
default:
|
2009-08-25 18:57:40 -06:00
|
|
|
log.Crashf("unknown unary operator %v", op);
|
2009-07-17 11:38:44 -06:00
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
desc, ok := unaryOpDescs[op];
|
2009-07-17 11:38:44 -06:00
|
|
|
if !ok {
|
2009-08-25 18:57:40 -06:00
|
|
|
desc = "unary " + op.String() + " expression";
|
|
|
|
unaryOpDescs[op] = desc;
|
2009-07-17 11:38:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compile
|
2009-08-25 18:57:40 -06:00
|
|
|
expr := a.newExpr(t, desc);
|
|
|
|
switch op {
|
2009-07-17 11:38:44 -06:00
|
|
|
case token.ADD:
|
|
|
|
// Just compile it out
|
2009-08-25 18:57:40 -06:00
|
|
|
expr = v;
|
|
|
|
expr.desc = desc;
|
2009-07-17 11:38:44 -06:00
|
|
|
|
|
|
|
case token.SUB:
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.genUnaryOpNeg(v);
|
2009-07-17 11:38:44 -06:00
|
|
|
|
|
|
|
case token.NOT:
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.genUnaryOpNot(v);
|
2009-07-17 11:38:44 -06:00
|
|
|
|
|
|
|
case token.XOR:
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.genUnaryOpXor(v);
|
2009-07-17 11:38:44 -06:00
|
|
|
|
|
|
|
case token.AND:
|
2009-08-25 18:57:40 -06:00
|
|
|
vf := v.evalAddr;
|
2009-09-02 13:03:20 -06:00
|
|
|
expr.eval = func(t *Thread) Value { return vf(t) };
|
2009-07-15 12:59:13 -06:00
|
|
|
|
|
|
|
default:
|
2009-08-25 18:57:40 -06:00
|
|
|
log.Crashf("Compilation of unary op %v not implemented", op);
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
|
|
|
|
return expr;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-07-17 11:38:44 -06:00
|
|
|
var binOpDescs = make(map[token.Token] string)
|
2009-07-15 12:59:13 -06:00
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
func (a *exprInfo) compileBinaryExpr(op token.Token, l, r *expr) *expr {
|
2009-07-15 12:59:13 -06:00
|
|
|
// Save the original types of l.t and r.t for error messages.
|
|
|
|
origlt := l.t;
|
|
|
|
origrt := r.t;
|
|
|
|
|
|
|
|
// XXX(Spec) What is the exact definition of a "named type"?
|
|
|
|
|
|
|
|
// XXX(Spec) Arithmetic operators: "Integer types" apparently
|
|
|
|
// means all types compatible with basic integer types, though
|
|
|
|
// this is never explained. Likewise for float types, etc.
|
|
|
|
// This relates to the missing explanation of named types.
|
|
|
|
|
|
|
|
// XXX(Spec) Operators: "If both operands are ideal numbers,
|
|
|
|
// the conversion is to ideal floats if one of the operands is
|
|
|
|
// an ideal float (relevant for / and %)." How is that
|
|
|
|
// relevant only for / and %? If I add an ideal int and an
|
|
|
|
// ideal float, I get an ideal float.
|
|
|
|
|
2009-07-21 16:40:41 -06:00
|
|
|
if op != token.SHL && op != token.SHR {
|
2009-07-17 11:38:44 -06:00
|
|
|
// Except in shift expressions, if one operand has
|
|
|
|
// numeric type and the other operand is an ideal
|
|
|
|
// number, the ideal number is converted to match the
|
|
|
|
// type of the other operand.
|
2009-07-17 17:58:59 -06:00
|
|
|
if (l.t.isInteger() || l.t.isFloat()) && !l.t.isIdeal() && r.t.isIdeal() {
|
2009-07-15 12:59:13 -06:00
|
|
|
r = r.convertTo(l.t);
|
2009-07-17 17:58:59 -06:00
|
|
|
} else if (r.t.isInteger() || r.t.isFloat()) && !r.t.isIdeal() && l.t.isIdeal() {
|
2009-07-15 12:59:13 -06:00
|
|
|
l = l.convertTo(r.t);
|
|
|
|
}
|
|
|
|
if l == nil || r == nil {
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Except in shift expressions, if both operands are
|
|
|
|
// ideal numbers and one is an ideal float, the other
|
|
|
|
// is converted to ideal float.
|
|
|
|
if l.t.isIdeal() && r.t.isIdeal() {
|
|
|
|
if l.t.isInteger() && r.t.isFloat() {
|
|
|
|
l = l.convertTo(r.t);
|
|
|
|
} else if l.t.isFloat() && r.t.isInteger() {
|
|
|
|
r = r.convertTo(l.t);
|
|
|
|
}
|
|
|
|
if l == nil || r == nil {
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Useful type predicates
|
2009-08-21 19:37:38 -06:00
|
|
|
// TODO(austin) CL 33668 mandates identical types except for comparisons.
|
2009-07-31 18:11:34 -06:00
|
|
|
compat := func() bool {
|
|
|
|
return l.t.compat(r.t, false);
|
2009-07-15 12:59:13 -06:00
|
|
|
};
|
|
|
|
integers := func() bool {
|
|
|
|
return l.t.isInteger() && r.t.isInteger();
|
|
|
|
};
|
|
|
|
floats := func() bool {
|
|
|
|
return l.t.isFloat() && r.t.isFloat();
|
|
|
|
};
|
|
|
|
strings := func() bool {
|
|
|
|
// TODO(austin) Deal with named types
|
|
|
|
return l.t == StringType && r.t == StringType;
|
|
|
|
};
|
|
|
|
booleans := func() bool {
|
2009-07-27 14:01:23 -06:00
|
|
|
return l.t.isBoolean() && r.t.isBoolean();
|
2009-07-15 12:59:13 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// Type check
|
2009-08-25 18:57:40 -06:00
|
|
|
var t Type;
|
2009-07-21 16:40:41 -06:00
|
|
|
switch op {
|
2009-07-15 12:59:13 -06:00
|
|
|
case token.ADD:
|
2009-07-31 18:11:34 -06:00
|
|
|
if !compat() || (!integers() && !floats() && !strings()) {
|
2009-07-21 16:40:41 -06:00
|
|
|
a.diagOpTypes(op, origlt, origrt);
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
t = l.t;
|
2009-07-15 12:59:13 -06:00
|
|
|
|
|
|
|
case token.SUB, token.MUL, token.QUO:
|
2009-07-31 18:11:34 -06:00
|
|
|
if !compat() || (!integers() && !floats()) {
|
2009-07-21 16:40:41 -06:00
|
|
|
a.diagOpTypes(op, origlt, origrt);
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
t = l.t;
|
2009-07-15 12:59:13 -06:00
|
|
|
|
|
|
|
case token.REM, token.AND, token.OR, token.XOR, token.AND_NOT:
|
2009-07-31 18:11:34 -06:00
|
|
|
if !compat() || !integers() {
|
2009-07-21 16:40:41 -06:00
|
|
|
a.diagOpTypes(op, origlt, origrt);
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
t = l.t;
|
2009-07-15 12:59:13 -06:00
|
|
|
|
|
|
|
case token.SHL, token.SHR:
|
2009-07-17 11:38:44 -06:00
|
|
|
// XXX(Spec) Is it okay for the right operand to be an
|
|
|
|
// ideal float with no fractional part? "The right
|
|
|
|
// operand in a shift operation must be always be of
|
|
|
|
// unsigned integer type or an ideal number that can
|
|
|
|
// be safely converted into an unsigned integer type
|
2009-07-17 12:50:32 -06:00
|
|
|
// (§Arithmetic operators)" suggests so and 6g agrees.
|
2009-07-17 11:38:44 -06:00
|
|
|
|
|
|
|
if !l.t.isInteger() || !(r.t.isInteger() || r.t.isIdeal()) {
|
2009-07-21 16:40:41 -06:00
|
|
|
a.diagOpTypes(op, origlt, origrt);
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-07-17 11:38:44 -06:00
|
|
|
}
|
|
|
|
|
2009-07-15 12:59:13 -06:00
|
|
|
// The right operand in a shift operation must be
|
|
|
|
// always be of unsigned integer type or an ideal
|
|
|
|
// number that can be safely converted into an
|
|
|
|
// unsigned integer type.
|
|
|
|
if r.t.isIdeal() {
|
2009-07-17 11:38:44 -06:00
|
|
|
r2 := r.convertTo(UintType);
|
|
|
|
if r2 == nil {
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
2009-07-17 12:50:32 -06:00
|
|
|
|
|
|
|
// If the left operand is not ideal, convert
|
|
|
|
// the right to not ideal.
|
2009-07-17 11:38:44 -06:00
|
|
|
if !l.t.isIdeal() {
|
|
|
|
r = r2;
|
2009-07-17 12:50:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// If both are ideal, but the right side isn't
|
|
|
|
// an ideal int, convert it to simplify things.
|
|
|
|
if l.t.isIdeal() && !r.t.isInteger() {
|
2009-07-17 11:38:44 -06:00
|
|
|
r = r.convertTo(IdealIntType);
|
|
|
|
if r == nil {
|
|
|
|
log.Crashf("conversion to uintType succeeded, but conversion to idealIntType failed");
|
|
|
|
}
|
|
|
|
}
|
2009-07-31 18:11:34 -06:00
|
|
|
} else if _, ok := r.t.lit().(*uintType); !ok {
|
2009-07-15 12:59:13 -06:00
|
|
|
a.diag("right operand of shift must be unsigned");
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
2009-07-17 12:50:32 -06:00
|
|
|
|
|
|
|
if l.t.isIdeal() && !r.t.isIdeal() {
|
|
|
|
// XXX(Spec) What is the meaning of "ideal >>
|
|
|
|
// non-ideal"? Russ says the ideal should be
|
|
|
|
// converted to an int. 6g propagates the
|
|
|
|
// type down from assignments as a hint.
|
2009-07-27 14:01:23 -06:00
|
|
|
|
2009-07-17 12:50:32 -06:00
|
|
|
l = l.convertTo(IntType);
|
|
|
|
if l == nil {
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-07-17 12:50:32 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we should have one of three cases:
|
|
|
|
// 1) uint SHIFT uint
|
|
|
|
// 2) int SHIFT uint
|
|
|
|
// 3) ideal int SHIFT ideal int
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
t = l.t;
|
2009-07-15 12:59:13 -06:00
|
|
|
|
|
|
|
case token.LOR, token.LAND:
|
|
|
|
if !booleans() {
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
// XXX(Spec) There's no mention of *which* boolean
|
|
|
|
// type the logical operators return. From poking at
|
|
|
|
// 6g, it appears to be the named boolean type, NOT
|
|
|
|
// the type of the left operand, and NOT an unnamed
|
|
|
|
// boolean type.
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
t = BoolType;
|
2009-07-15 12:59:13 -06:00
|
|
|
|
|
|
|
case token.ARROW:
|
|
|
|
// The operands in channel sends differ in type: one
|
|
|
|
// is always a channel and the other is a variable or
|
|
|
|
// value of the channel's element type.
|
|
|
|
log.Crash("Binary op <- not implemented");
|
2009-08-25 18:57:40 -06:00
|
|
|
t = BoolType;
|
2009-07-15 12:59:13 -06:00
|
|
|
|
|
|
|
case token.LSS, token.GTR, token.LEQ, token.GEQ:
|
2009-07-28 17:40:23 -06:00
|
|
|
// XXX(Spec) It's really unclear what types which
|
|
|
|
// comparison operators apply to. I feel like the
|
|
|
|
// text is trying to paint a Venn diagram for me,
|
|
|
|
// which it's really pretty simple: <, <=, >, >= apply
|
|
|
|
// only to numeric types and strings. == and != apply
|
|
|
|
// to everything except arrays and structs, and there
|
|
|
|
// are some restrictions on when it applies to slices.
|
2009-07-27 14:01:23 -06:00
|
|
|
|
2009-07-31 18:11:34 -06:00
|
|
|
if !compat() || (!integers() && !floats() && !strings()) {
|
2009-07-21 16:40:41 -06:00
|
|
|
a.diagOpTypes(op, origlt, origrt);
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
t = BoolType;
|
2009-07-15 12:59:13 -06:00
|
|
|
|
|
|
|
case token.EQL, token.NEQ:
|
2009-07-28 17:40:23 -06:00
|
|
|
// XXX(Spec) The rules for type checking comparison
|
|
|
|
// operators are spread across three places that all
|
|
|
|
// partially overlap with each other: the Comparison
|
|
|
|
// Compatibility section, the Operators section, and
|
|
|
|
// the Comparison Operators section. The Operators
|
|
|
|
// section should just say that operators require
|
|
|
|
// identical types (as it does currently) except that
|
|
|
|
// there a few special cases for comparison, which are
|
|
|
|
// described in section X. Currently it includes just
|
|
|
|
// one of the four special cases. The Comparison
|
|
|
|
// Compatibility section and the Comparison Operators
|
|
|
|
// section should either be merged, or at least the
|
|
|
|
// Comparison Compatibility section should be
|
|
|
|
// exclusively about type checking and the Comparison
|
|
|
|
// Operators section should be exclusively about
|
|
|
|
// semantics.
|
2009-07-15 12:59:13 -06:00
|
|
|
|
|
|
|
// XXX(Spec) Comparison operators: "All comparison
|
2009-07-28 17:40:23 -06:00
|
|
|
// operators apply to basic types except bools." This
|
|
|
|
// is very difficult to parse. It's explained much
|
|
|
|
// better in the Comparison Compatibility section.
|
|
|
|
|
|
|
|
// XXX(Spec) Comparison compatibility: "Function
|
|
|
|
// values are equal if they refer to the same
|
|
|
|
// function." is rather vague. It should probably be
|
|
|
|
// similar to the way the rule for map values is
|
|
|
|
// written: Function values are equal if they were
|
|
|
|
// created by the same execution of a function literal
|
|
|
|
// or refer to the same function declaration. This is
|
|
|
|
// *almost* but not quite waht 6g implements. If a
|
|
|
|
// function literals does not capture any variables,
|
|
|
|
// then multiple executions of it will result in the
|
|
|
|
// same closure. Russ says he'll change that.
|
|
|
|
|
|
|
|
// TODO(austin) Deal with remaining special cases
|
|
|
|
|
2009-07-31 18:11:34 -06:00
|
|
|
if !compat() {
|
2009-07-28 17:40:23 -06:00
|
|
|
a.diagOpTypes(op, origlt, origrt);
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-07-28 17:40:23 -06:00
|
|
|
}
|
|
|
|
// Arrays and structs may not be compared to anything.
|
2009-09-24 09:25:14 -06:00
|
|
|
switch l.t.(type) {
|
|
|
|
case *ArrayType, *StructType:
|
2009-08-05 12:49:51 -06:00
|
|
|
a.diagOpTypes(op, origlt, origrt);
|
2009-08-25 18:57:40 -06:00
|
|
|
return nil;
|
2009-08-05 12:49:51 -06:00
|
|
|
}
|
2009-08-25 18:57:40 -06:00
|
|
|
t = BoolType;
|
2009-07-15 12:59:13 -06:00
|
|
|
|
|
|
|
default:
|
2009-07-21 16:40:41 -06:00
|
|
|
log.Crashf("unknown binary operator %v", op);
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
desc, ok := binOpDescs[op];
|
2009-07-15 12:59:13 -06:00
|
|
|
if !ok {
|
2009-08-25 18:57:40 -06:00
|
|
|
desc = op.String() + " expression";
|
|
|
|
binOpDescs[op] = desc;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-08-28 11:39:57 -06:00
|
|
|
// Check for ideal divide by zero
|
|
|
|
switch op {
|
|
|
|
case token.QUO, token.REM:
|
|
|
|
if r.t.isIdeal() {
|
|
|
|
if (r.t.isInteger() && r.asIdealInt()().IsZero()) ||
|
|
|
|
(r.t.isFloat() && r.asIdealFloat()().IsZero()) {
|
|
|
|
a.diag("divide by zero");
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-15 12:59:13 -06:00
|
|
|
// Compile
|
2009-08-25 18:57:40 -06:00
|
|
|
expr := a.newExpr(t, desc);
|
2009-07-21 16:40:41 -06:00
|
|
|
switch op {
|
2009-07-15 12:59:13 -06:00
|
|
|
case token.ADD:
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.genBinOpAdd(l, r);
|
2009-07-15 12:59:13 -06:00
|
|
|
|
|
|
|
case token.SUB:
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.genBinOpSub(l, r);
|
2009-07-15 12:59:13 -06:00
|
|
|
|
2009-07-17 11:38:44 -06:00
|
|
|
case token.MUL:
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.genBinOpMul(l, r);
|
2009-07-17 11:38:44 -06:00
|
|
|
|
2009-07-15 12:59:13 -06:00
|
|
|
case token.QUO:
|
2009-07-15 18:56:17 -06:00
|
|
|
// TODO(austin) Clear higher bits that may have
|
|
|
|
// accumulated in our temporary.
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.genBinOpQuo(l, r);
|
2009-07-15 12:59:13 -06:00
|
|
|
|
2009-07-17 11:38:44 -06:00
|
|
|
case token.REM:
|
|
|
|
// TODO(austin) Clear higher bits that may have
|
|
|
|
// accumulated in our temporary.
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.genBinOpRem(l, r);
|
2009-07-17 11:38:44 -06:00
|
|
|
|
|
|
|
case token.AND:
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.genBinOpAnd(l, r);
|
2009-07-17 11:38:44 -06:00
|
|
|
|
|
|
|
case token.OR:
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.genBinOpOr(l, r);
|
2009-07-17 11:38:44 -06:00
|
|
|
|
|
|
|
case token.XOR:
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.genBinOpXor(l, r);
|
2009-07-17 11:38:44 -06:00
|
|
|
|
|
|
|
case token.AND_NOT:
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.genBinOpAndNot(l, r);
|
2009-07-17 11:38:44 -06:00
|
|
|
|
|
|
|
case token.SHL:
|
2009-07-17 12:50:32 -06:00
|
|
|
if l.t.isIdeal() {
|
|
|
|
lv := l.asIdealInt()();
|
|
|
|
rv := r.asIdealInt()();
|
|
|
|
const maxShift = 99999;
|
|
|
|
if rv.Cmp(bignum.Int(maxShift)) > 0 {
|
|
|
|
a.diag("left shift by %v; exceeds implementation limit of %v", rv, maxShift);
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.t = nil;
|
|
|
|
return nil;
|
2009-07-17 12:50:32 -06:00
|
|
|
}
|
|
|
|
val := lv.Shl(uint(rv.Value()));
|
2009-09-01 12:51:33 -06:00
|
|
|
expr.eval = func() *bignum.Integer { return val };
|
2009-07-17 12:50:32 -06:00
|
|
|
} else {
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.genBinOpShl(l, r);
|
2009-07-17 11:38:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
case token.SHR:
|
2009-07-17 12:50:32 -06:00
|
|
|
if l.t.isIdeal() {
|
|
|
|
lv := l.asIdealInt()();
|
|
|
|
rv := r.asIdealInt()();
|
|
|
|
val := lv.Shr(uint(rv.Value()));
|
2009-09-01 12:51:33 -06:00
|
|
|
expr.eval = func() *bignum.Integer { return val };
|
2009-07-17 12:50:32 -06:00
|
|
|
} else {
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.genBinOpShr(l, r);
|
2009-07-17 11:38:44 -06:00
|
|
|
}
|
|
|
|
|
2009-07-28 17:40:23 -06:00
|
|
|
case token.LSS:
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.genBinOpLss(l, r);
|
2009-07-28 17:40:23 -06:00
|
|
|
|
|
|
|
case token.GTR:
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.genBinOpGtr(l, r);
|
2009-07-28 17:40:23 -06:00
|
|
|
|
|
|
|
case token.LEQ:
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.genBinOpLeq(l, r);
|
2009-07-28 17:40:23 -06:00
|
|
|
|
|
|
|
case token.GEQ:
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.genBinOpGeq(l, r);
|
2009-07-28 17:40:23 -06:00
|
|
|
|
|
|
|
case token.EQL:
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.genBinOpEql(l, r);
|
2009-07-28 17:40:23 -06:00
|
|
|
|
|
|
|
case token.NEQ:
|
2009-08-25 18:57:40 -06:00
|
|
|
expr.genBinOpNeq(l, r);
|
2009-07-28 17:40:23 -06:00
|
|
|
|
2009-07-15 12:59:13 -06:00
|
|
|
default:
|
2009-07-21 16:40:41 -06:00
|
|
|
log.Crashf("Compilation of binary op %v not implemented", op);
|
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
return expr;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-07-27 14:01:23 -06:00
|
|
|
// TODO(austin) This is a hack to eliminate a circular dependency
|
|
|
|
// between type.go and expr.go
|
2009-07-29 12:57:46 -06:00
|
|
|
func (a *compiler) compileArrayLen(b *block, expr ast.Expr) (int64, bool) {
|
2009-08-25 18:57:40 -06:00
|
|
|
lenExpr := a.compileExpr(b, true, expr);
|
2009-07-27 14:01:23 -06:00
|
|
|
if lenExpr == nil {
|
|
|
|
return 0, false;
|
|
|
|
}
|
|
|
|
|
2009-08-05 12:49:51 -06:00
|
|
|
// XXX(Spec) Are ideal floats with no fractional part okay?
|
2009-07-27 14:01:23 -06:00
|
|
|
if lenExpr.t.isIdeal() {
|
|
|
|
lenExpr = lenExpr.convertTo(IntType);
|
|
|
|
if lenExpr == nil {
|
|
|
|
return 0, false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-05 12:49:51 -06:00
|
|
|
if !lenExpr.t.isInteger() {
|
|
|
|
a.diagAt(expr, "array size must be an integer");
|
|
|
|
return 0, false;
|
|
|
|
}
|
|
|
|
|
2009-09-25 10:37:35 -06:00
|
|
|
switch lenExpr.t.lit().(type) {
|
2009-07-27 14:01:23 -06:00
|
|
|
case *intType:
|
2009-09-01 12:51:33 -06:00
|
|
|
return lenExpr.asInt()(nil), true;
|
2009-07-27 14:01:23 -06:00
|
|
|
case *uintType:
|
2009-09-01 12:51:33 -06:00
|
|
|
return int64(lenExpr.asUint()(nil)), true;
|
2009-07-27 14:01:23 -06:00
|
|
|
}
|
|
|
|
log.Crashf("unexpected integer type %T", lenExpr.t);
|
|
|
|
return 0, false;
|
|
|
|
}
|
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
func (a *compiler) compileExpr(b *block, constant bool, expr ast.Expr) *expr {
|
|
|
|
ec := &exprCompiler{a, b, constant};
|
2009-08-27 12:21:52 -06:00
|
|
|
nerr := a.numError();
|
2009-08-28 19:03:03 -06:00
|
|
|
e := ec.compile(expr, false);
|
2009-08-27 12:21:52 -06:00
|
|
|
if e == nil && nerr == a.numError() {
|
|
|
|
log.Crashf("expression compilation failed without reporting errors");
|
|
|
|
}
|
|
|
|
return e;
|
2009-07-15 12:59:13 -06:00
|
|
|
}
|
|
|
|
|
2009-07-27 14:01:23 -06:00
|
|
|
// extractEffect separates out any effects that the expression may
|
|
|
|
// have, returning a function that will perform those effects and a
|
|
|
|
// new exprCompiler that is guaranteed to be side-effect free. These
|
2009-08-10 17:27:54 -06:00
|
|
|
// are the moral equivalents of "temp := expr" and "temp" (or "temp :=
|
|
|
|
// &expr" and "*temp" for addressable exprs). Because this creates a
|
|
|
|
// temporary variable, the caller should create a temporary block for
|
|
|
|
// the compilation of this expression and the evaluation of the
|
|
|
|
// results.
|
2009-09-02 13:03:20 -06:00
|
|
|
func (a *expr) extractEffect(b *block, errOp string) (func(*Thread), *expr) {
|
2009-08-10 17:27:54 -06:00
|
|
|
// Create "&a" if a is addressable
|
|
|
|
rhs := a;
|
|
|
|
if a.evalAddr != nil {
|
2009-08-25 18:57:40 -06:00
|
|
|
rhs = a.compileUnaryExpr(token.AND, rhs);
|
2009-08-10 17:27:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create temp
|
2009-08-25 18:57:40 -06:00
|
|
|
ac, ok := a.checkAssign(a.pos, []*expr{rhs}, errOp, "");
|
2009-08-10 17:27:54 -06:00
|
|
|
if !ok {
|
|
|
|
return nil, nil;
|
|
|
|
}
|
|
|
|
if len(ac.rmt.Elems) != 1 {
|
|
|
|
a.diag("multi-valued expression not allowed in %s", errOp);
|
|
|
|
return nil, nil;
|
|
|
|
}
|
|
|
|
tempType := ac.rmt.Elems[0];
|
|
|
|
if tempType.isIdeal() {
|
|
|
|
// It's too bad we have to duplicate this rule.
|
|
|
|
switch {
|
|
|
|
case tempType.isInteger():
|
|
|
|
tempType = IntType;
|
|
|
|
case tempType.isFloat():
|
|
|
|
tempType = FloatType;
|
|
|
|
default:
|
|
|
|
log.Crashf("unexpected ideal type %v", tempType);
|
|
|
|
}
|
|
|
|
}
|
2009-09-03 17:20:49 -06:00
|
|
|
temp := b.DefineTemp(tempType);
|
2009-07-27 14:01:23 -06:00
|
|
|
tempIdx := temp.Index;
|
|
|
|
|
2009-08-10 17:27:54 -06:00
|
|
|
// Create "temp := rhs"
|
2009-08-25 18:57:40 -06:00
|
|
|
assign := ac.compile(b, tempType);
|
2009-07-27 14:01:23 -06:00
|
|
|
if assign == nil {
|
2009-07-27 18:32:35 -06:00
|
|
|
log.Crashf("compileAssign type check failed");
|
2009-07-27 14:01:23 -06:00
|
|
|
}
|
|
|
|
|
2009-09-02 13:03:20 -06:00
|
|
|
effect := func(t *Thread) {
|
2009-07-29 12:57:46 -06:00
|
|
|
tempVal := tempType.Zero();
|
2009-09-02 13:03:20 -06:00
|
|
|
t.f.Vars[tempIdx] = tempVal;
|
|
|
|
assign(tempVal, t);
|
2009-07-27 14:01:23 -06:00
|
|
|
};
|
|
|
|
|
2009-08-10 17:27:54 -06:00
|
|
|
// Generate "temp" or "*temp"
|
2009-08-25 18:57:40 -06:00
|
|
|
getTemp := a.compileVariable(0, temp);
|
2009-08-10 17:27:54 -06:00
|
|
|
if a.evalAddr == nil {
|
|
|
|
return effect, getTemp;
|
|
|
|
}
|
2009-07-27 14:01:23 -06:00
|
|
|
|
2009-08-25 18:57:40 -06:00
|
|
|
deref := a.compileStarExpr(getTemp);
|
|
|
|
if deref == nil {
|
|
|
|
return nil, nil;
|
|
|
|
}
|
2009-07-27 14:01:23 -06:00
|
|
|
return effect, deref;
|
|
|
|
}
|