Rename token.IDMap to token.Map.

This commit is contained in:
Nigel Tao committed 2017-06-08 13:10:01 +10:00
1 parent f6cc7950ff
commit 6e7e566232
15 files changed
+230 -230

No files matched your search

+7 -7
View File
@@ -135,8 +135,8 @@ func simplify(n *a.Expr) *a.Expr {
return n
}
func argValue(m *t.IDMap, args []*a.Node, name string) *a.Expr {
if x := m.ByName(name); x != 0 {
func argValue(tm *t.Map, args []*a.Node, name string) *a.Expr {
if x := tm.ByName(name); x != 0 {
for _, a := range args {
if a.Arg().Name() == x {
return a.Arg().Value()
@@ -251,15 +251,15 @@ var reasons = [...]struct {
return errFailed
}
if !q.proveBinaryOp(t.KeyXBinaryLessThan, a, c) {
return fmt.Errorf("cannot prove \"%s < %s\"", a.String(q.idMap), c.String(q.idMap))
return fmt.Errorf("cannot prove \"%s < %s\"", a.String(q.tm), c.String(q.tm))
}
if !q.proveBinaryOp(t.KeyXBinaryLessEq, zeroExpr, b) {
return fmt.Errorf("cannot prove \"%s <= %s\"", zeroExpr.String(q.idMap), b.String(q.idMap))
return fmt.Errorf("cannot prove \"%s <= %s\"", zeroExpr.String(q.tm), b.String(q.tm))
}
return nil
}},
{`"a < b: a < c; c <= b"`, func(q *checker, n *a.Assert) error {
c := argValue(q.idMap, n.Args(), "c")
c := argValue(q.tm, n.Args(), "c")
if c == nil {
return errFailed
}
@@ -268,10 +268,10 @@ var reasons = [...]struct {
return errFailed
}
if !q.proveBinaryOp(t.KeyXBinaryLessThan, a, c) {
return fmt.Errorf("cannot prove \"%s < %s\"", a.String(q.idMap), c.String(q.idMap))
return fmt.Errorf("cannot prove \"%s < %s\"", a.String(q.tm), c.String(q.tm))
}
if !q.proveBinaryOp(t.KeyXBinaryLessEq, c, b) {
return fmt.Errorf("cannot prove \"%s <= %s\"", c.String(q.idMap), b.String(q.idMap))
return fmt.Errorf("cannot prove \"%s <= %s\"", c.String(q.tm), b.String(q.tm))
}
return nil
}},
+23 -23
View File
@@ -92,12 +92,12 @@ func bitMask(nBits int) *big.Int {
return z.Sub(z, one)
}
func invert(m *t.IDMap, n *a.Expr) (*a.Expr, error) {
func invert(tm *t.Map, n *a.Expr) (*a.Expr, error) {
if !n.MType().IsBool() {
return nil, fmt.Errorf("check: invert(%q) called on non-bool-typed expression", n.String(m))
return nil, fmt.Errorf("check: invert(%q) called on non-bool-typed expression", n.String(tm))
}
if cv := n.ConstValue(); cv != nil {
return nil, fmt.Errorf("check: invert(%q) called on constant expression", n.String(m))
return nil, fmt.Errorf("check: invert(%q) called on constant expression", n.String(tm))
}
id0, lhs, rhs := n.ID0(), n.LHS().Expr(), n.RHS().Expr()
switch id0.Key() {
@@ -124,7 +124,7 @@ func invert(m *t.IDMap, n *a.Expr) (*a.Expr, error) {
return o, nil
}
func bcheckField(idMap *t.IDMap, n *a.Field) error {
func bcheckField(tm *t.Map, n *a.Field) error {
x := n.XType()
for ; x.Inner() != nil; x = x.Inner() {
}
@@ -140,7 +140,7 @@ func bcheckField(idMap *t.IDMap, n *a.Field) error {
}
if dv.Cmp(nMin) < 0 || dv.Cmp(nMax) > 0 {
return fmt.Errorf("check: default value %v is not within bounds [%v..%v] for field %q",
dv, nMin, nMax, n.Name().String(idMap))
dv, nMin, nMax, n.Name().String(tm))
}
return nil
}
@@ -225,7 +225,7 @@ func (q *checker) bcheckAssert(n *a.Assert) error {
if reasonFunc := q.reasonMap[reasonID.Key()]; reasonFunc != nil {
err = reasonFunc(q, n)
} else {
err = fmt.Errorf("no such reason %s", reasonID.String(q.idMap))
err = fmt.Errorf("no such reason %s", reasonID.String(q.tm))
}
} else if condition.ID0().IsBinaryOp() && condition.ID0().Key() != t.KeyAs {
if q.proveBinaryOp(condition.ID0().Key(), condition.LHS().Expr(), condition.RHS().Expr()) {
@@ -234,9 +234,9 @@ func (q *checker) bcheckAssert(n *a.Assert) error {
}
if err != nil {
if err == errFailed {
return fmt.Errorf("check: cannot prove %q", condition.String(q.idMap))
return fmt.Errorf("check: cannot prove %q", condition.String(q.tm))
}
return fmt.Errorf("check: cannot prove %q: %v", condition.String(q.idMap), err)
return fmt.Errorf("check: cannot prove %q: %v", condition.String(q.tm), err)
}
q.facts.appendFact(condition)
return nil
@@ -322,10 +322,10 @@ func (q *checker) bcheckAssignment1(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
if rMin.Cmp(lMin) < 0 || rMax.Cmp(lMax) > 0 {
if op == t.IDEq {
return fmt.Errorf("check: expression %q bounds [%v..%v] is not within bounds [%v..%v]",
rhs.String(q.idMap), rMin, rMax, lMin, lMax)
rhs.String(q.tm), rMin, rMax, lMin, lMax)
} else {
return fmt.Errorf("check: assignment %q bounds [%v..%v] is not within bounds [%v..%v]",
lhs.String(q.idMap)+" "+op.String(q.idMap)+" "+rhs.String(q.idMap),
lhs.String(q.tm)+" "+op.String(q.tm)+" "+rhs.String(q.tm),
rMin, rMax, lMin, lMax)
}
}
@@ -406,7 +406,7 @@ func (q *checker) bcheckIf(n *a.If) error {
// Check the if-false branch, assuming the inverted if condition.
q.facts = append(q.facts[:0], snap...)
if inverse, err := invert(q.idMap, n.Condition()); err != nil {
if inverse, err := invert(q.tm, n.Condition()); err != nil {
return err
} else {
q.facts.appendFact(inverse)
@@ -471,7 +471,7 @@ func (q *checker) bcheckWhile(n *a.While) error {
}
q.facts.appendFact(o.Assert().Condition())
}
if inverse, err := invert(q.idMap, n.Condition()); err != nil {
if inverse, err := invert(q.tm, n.Condition()); err != nil {
return err
} else {
q.facts.appendFact(inverse)
@@ -577,7 +577,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
case t.KeyOpenParen:
// TODO: delete this hack that only matches "in.src.read_u8?()".
if isInSrcReadU8(q.idMap, n.LHS().Expr()) && n.CallSuspendible() && len(n.Args()) == 0 {
if isInSrcReadU8(q.tm, n.LHS().Expr()) && n.CallSuspendible() && len(n.Args()) == 0 {
break
}
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprOther", n.ID0().Key())
@@ -672,22 +672,22 @@ func (q *checker) bcheckExprBinaryOp(lhs *a.Expr, op t.Key, rhs *a.Expr, depth u
case t.KeyXBinaryShiftL:
if lMin.Cmp(zero) < 0 {
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", lhs.String(q.idMap))
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", lhs.String(q.tm))
}
if rMin.Cmp(zero) < 0 {
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", rhs.String(q.idMap))
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", rhs.String(q.tm))
}
if rMax.Cmp(ffff) > 0 {
return nil, nil, fmt.Errorf("check: shift %q out of range", rhs.String(q.idMap))
return nil, nil, fmt.Errorf("check: shift %q out of range", rhs.String(q.tm))
}
return big.NewInt(0).Lsh(lMin, uint(rMin.Uint64())), big.NewInt(0).Lsh(lMax, uint(rMax.Uint64())), nil
case t.KeyXBinaryShiftR:
if lMin.Cmp(zero) < 0 {
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", lhs.String(q.idMap))
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", lhs.String(q.tm))
}
if rMin.Cmp(zero) < 0 {
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", rhs.String(q.idMap))
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", rhs.String(q.tm))
}
if rMin.Cmp(maxIntBits) >= 0 {
return zero, zero, nil
@@ -702,16 +702,16 @@ func (q *checker) bcheckExprBinaryOp(lhs *a.Expr, op t.Key, rhs *a.Expr, depth u
// TODO: should type-checking ensure that bitwise ops only apply to
// *unsigned* integer types?
if lMin.Cmp(zero) < 0 {
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly negative", lhs.String(q.idMap))
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly negative", lhs.String(q.tm))
}
if lMax.Cmp(numTypeBounds[t.KeyU64][1]) > 0 {
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly too large", lhs.String(q.idMap))
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly too large", lhs.String(q.tm))
}
if rMin.Cmp(zero) < 0 {
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly negative", rhs.String(q.idMap))
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly negative", rhs.String(q.tm))
}
if rMax.Cmp(numTypeBounds[t.KeyU64][1]) > 0 {
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly too large", rhs.String(q.idMap))
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly too large", rhs.String(q.tm))
}
z := (*big.Int)(nil)
if op == t.KeyXBinaryAmp {
@@ -750,7 +750,7 @@ func (q *checker) bcheckTypeExpr(n *a.TypeExpr) (*big.Int, *big.Int, error) {
b = numTypeBounds[key]
}
if b[0] == nil || b[1] == nil {
return nil, nil, fmt.Errorf("check: internal error: unknown bounds for %q", n.String(q.idMap))
return nil, nil, fmt.Errorf("check: internal error: unknown bounds for %q", n.String(q.tm))
}
if n.IsRefined() {
if x := n.Min(); x != nil {
+29 -29
View File
@@ -24,7 +24,7 @@ type Error struct {
OtherFilename string
OtherLine uint32
IDMap *t.IDMap
TMap *t.Map
Facts []*a.Expr
}
@@ -36,13 +36,13 @@ func (e *Error) Error() string {
} else {
s = fmt.Sprintf("%s at %s:%d", e.Err, e.Filename, e.Line)
}
if e.IDMap == nil {
if e.TMap == nil {
return s
}
b := append([]byte(s), ". Facts:\n"...)
for _, f := range e.Facts {
b = append(b, '\t')
b = append(b, f.String(e.IDMap)...)
b = append(b, f.String(e.TMap)...)
b = append(b, '\n')
}
return string(b)
@@ -71,7 +71,7 @@ type Func struct {
LocalVars TypeMap
}
func Check(m *t.IDMap, flags Flags, files ...*a.File) (*Checker, error) {
func Check(tm *t.Map, flags Flags, files ...*a.File) (*Checker, error) {
for _, f := range files {
if f == nil {
return nil, errors.New("check: Check given a nil *ast.File")
@@ -90,13 +90,13 @@ func Check(m *t.IDMap, flags Flags, files ...*a.File) (*Checker, error) {
rMap := reasonMap{}
for _, r := range reasons {
if id := m.ByName(r.s); id != 0 {
if id := tm.ByName(r.s); id != 0 {
rMap[id.Key()] = r.r
}
}
c := &Checker{
flags: flags,
idMap: m,
tm: tm,
reasonMap: rMap,
structs: map[t.ID]Struct{},
funcs: map[t.QID]Func{},
@@ -131,7 +131,7 @@ var phases = [...]struct {
type Checker struct {
flags Flags
idMap *t.IDMap
tm *t.Map
reasonMap reasonMap
structs map[t.ID]Struct
funcs map[t.QID]Func
@@ -150,38 +150,38 @@ func (c *Checker) checkFields(fields []*a.Node, banPtrTypes bool) error {
}
q := &checker{
c: c,
idMap: c.idMap,
c: c,
tm: c.tm,
}
fieldNames := map[t.ID]bool{}
for _, n := range fields {
f := n.Field()
if _, ok := fieldNames[f.Name()]; ok {
return fmt.Errorf("check: duplicate field %q", f.Name().String(c.idMap))
return fmt.Errorf("check: duplicate field %q", f.Name().String(c.tm))
}
if err := q.tcheckTypeExpr(f.XType(), 0); err != nil {
return fmt.Errorf("%v in field %q", err, f.Name().String(c.idMap))
return fmt.Errorf("%v in field %q", err, f.Name().String(c.tm))
}
if banPtrTypes {
for x := f.XType(); x.Inner() != nil; x = x.Inner() {
if x.PackageOrDecorator().Key() == t.KeyPtr {
// TODO: implement nptr (nullable pointer) types.
return fmt.Errorf("check: ptr type %q not allowed for field %q; use nptr instead",
x.String(c.idMap), f.Name().String(c.idMap))
x.String(c.tm), f.Name().String(c.tm))
}
}
}
if dv := f.DefaultValue(); dv != nil {
if f.XType().PackageOrDecorator() != 0 {
return fmt.Errorf("check: cannot set default value for qualified type %q for field %q",
f.XType().String(c.idMap), f.Name().String(c.idMap))
f.XType().String(c.tm), f.Name().String(c.tm))
}
if err := q.tcheckExpr(dv, 0); err != nil {
return err
}
}
if c.flags&FlagsOnlyTypeCheck == 0 {
if err := bcheckField(c.idMap, f); err != nil {
if err := bcheckField(c.tm, f); err != nil {
return err
}
}
@@ -196,7 +196,7 @@ func (c *Checker) checkStruct(node *a.Node) error {
n := node.Struct()
if err := c.checkFields(n.Fields(), true); err != nil {
return &Error{
Err: fmt.Errorf("%v in struct %q", err, n.Name().String(c.idMap)),
Err: fmt.Errorf("%v in struct %q", err, n.Name().String(c.tm)),
Filename: n.Filename(),
Line: n.Line(),
}
@@ -204,7 +204,7 @@ func (c *Checker) checkStruct(node *a.Node) error {
id := n.Name()
if other, ok := c.structs[id]; ok {
return &Error{
Err: fmt.Errorf("check: duplicate struct %q", id.String(c.idMap)),
Err: fmt.Errorf("check: duplicate struct %q", id.String(c.tm)),
Filename: n.Filename(),
Line: n.Line(),
OtherFilename: other.Struct.Filename(),
@@ -223,7 +223,7 @@ func (c *Checker) checkFuncSignature(node *a.Node) error {
n := node.Func()
if err := c.checkFields(n.In().Fields(), false); err != nil {
return &Error{
Err: fmt.Errorf("%v in in-params for func %q", err, n.Name().String(c.idMap)),
Err: fmt.Errorf("%v in in-params for func %q", err, n.Name().String(c.tm)),
Filename: n.Filename(),
Line: n.Line(),
}
@@ -231,7 +231,7 @@ func (c *Checker) checkFuncSignature(node *a.Node) error {
n.In().Node().SetTypeChecked()
if err := c.checkFields(n.Out().Fields(), false); err != nil {
return &Error{
Err: fmt.Errorf("%v in out-params for func %q", err, n.Name().String(c.idMap)),
Err: fmt.Errorf("%v in out-params for func %q", err, n.Name().String(c.tm)),
Filename: n.Filename(),
Line: n.Line(),
}
@@ -241,7 +241,7 @@ func (c *Checker) checkFuncSignature(node *a.Node) error {
qid := n.QID()
if other, ok := c.funcs[qid]; ok {
return &Error{
Err: fmt.Errorf("check: duplicate function %q", qid.String(c.idMap)),
Err: fmt.Errorf("check: duplicate function %q", qid.String(c.tm)),
Filename: n.Filename(),
Line: n.Line(),
OtherFilename: other.Func.Filename(),
@@ -260,7 +260,7 @@ func (c *Checker) checkFuncSignature(node *a.Node) error {
if qid[0] != 0 {
if _, ok := c.structs[qid[0]]; !ok {
return &Error{
Err: fmt.Errorf("check: no receiver struct defined for function %q", qid.String(c.idMap)),
Err: fmt.Errorf("check: no receiver struct defined for function %q", qid.String(c.tm)),
Filename: n.Filename(),
Line: n.Line(),
}
@@ -285,8 +285,8 @@ func (c *Checker) checkFuncContract(node *a.Node) error {
return nil
}
q := &checker{
c: c,
idMap: c.idMap,
c: c,
tm: c.tm,
}
for _, o := range n.Asserts() {
if err := q.tcheckAssert(o.Assert()); err != nil {
@@ -300,7 +300,7 @@ func (c *Checker) checkFuncBody(node *a.Node) error {
n := node.Func()
q := &checker{
c: c,
idMap: c.idMap,
tm: c.tm,
reasonMap: c.reasonMap,
f: c.funcs[n.QID()],
}
@@ -338,7 +338,7 @@ func (c *Checker) checkFuncBody(node *a.Node) error {
Err: err,
Filename: q.errFilename,
Line: q.errLine,
IDMap: c.idMap,
TMap: c.tm,
Facts: q.facts,
}
}
@@ -350,10 +350,10 @@ func (c *Checker) checkFuncBody(node *a.Node) error {
switch o.Kind() {
case a.KExpr:
return fmt.Errorf("check: internal error: unchecked %s node %q",
o.Kind(), o.Expr().String(q.idMap))
o.Kind(), o.Expr().String(q.tm))
case a.KTypeExpr:
return fmt.Errorf("check: internal error: unchecked %s node %q",
o.Kind(), o.TypeExpr().String(q.idMap))
o.Kind(), o.TypeExpr().String(q.tm))
}
return fmt.Errorf("check: internal error: unchecked %s node", o.Kind())
}
@@ -361,10 +361,10 @@ func (c *Checker) checkFuncBody(node *a.Node) error {
o := o.Expr()
if typ := o.MType(); typ == nil {
return fmt.Errorf("check: internal error: expression %q has no (implicit) type",
o.String(q.idMap))
o.String(q.tm))
} else if typ == TypeExprIdeal && o.ConstValue() == nil {
return fmt.Errorf("check: internal error: expression %q has ideal number type "+
"but no const value", o.String(q.idMap))
"but no const value", o.String(q.tm))
}
}
return nil
@@ -377,7 +377,7 @@ func (c *Checker) checkFuncBody(node *a.Node) error {
type checker struct {
c *Checker
idMap *t.IDMap
tm *t.Map
reasonMap reasonMap
f Func
+14 -14
View File
@@ -18,9 +18,9 @@ import (
"github.com/google/puffs/lang/token"
)
func compareToPuffsfmt(idMap *token.IDMap, tokens []token.Token, comments []string, src string) error {
func compareToPuffsfmt(tm *token.Map, tokens []token.Token, comments []string, src string) error {
buf := &bytes.Buffer{}
if err := render.Render(buf, idMap, tokens, comments); err != nil {
if err := render.Render(buf, tm, tokens, comments); err != nil {
return err
}
got := strings.Split(buf.String(), "\n")
@@ -81,23 +81,23 @@ func TestCheck(t *testing.T) {
}
`) + "\n"
idMap := &token.IDMap{}
tm := &token.Map{}
tokens, comments, err := token.Tokenize(idMap, filename, []byte(src))
tokens, comments, err := token.Tokenize(tm, filename, []byte(src))
if err != nil {
t.Fatalf("Tokenize: %v", err)
}
file, err := parse.Parse(idMap, filename, tokens)
file, err := parse.Parse(tm, filename, tokens)
if err != nil {
t.Fatalf("Parse: %v", err)
}
if err := compareToPuffsfmt(idMap, tokens, comments, src); err != nil {
if err := compareToPuffsfmt(tm, tokens, comments, src); err != nil {
t.Fatalf("compareToPuffsfmt: %v", err)
}
c, err := Check(idMap, 0, file)
c, err := Check(tm, 0, file)
if err != nil {
t.Fatalf("Check: %v", err)
}
@@ -112,15 +112,15 @@ func TestCheck(t *testing.T) {
break
}
if got, want := fooBar.QID.String(idMap), "foo.bar"; got != want {
if got, want := fooBar.QID.String(tm), "foo.bar"; got != want {
t.Fatalf("Funcs[0] name: got %q, want %q", got, want)
}
got := [][2]string(nil)
for id, typ := range fooBar.LocalVars {
got = append(got, [2]string{
id.String(idMap),
typ.String(idMap),
id.String(tm),
typ.String(tm),
})
}
sort.Slice(got, func(i, j int) bool {
@@ -178,23 +178,23 @@ func TestConstValues(t *testing.T) {
"var b bool = false or true": 1,
}
idMap := &token.IDMap{}
tm := &token.Map{}
for s, wantInt64 := range testCases {
src := "pri func foo()() {\n\t" + s + "\n}\n"
tokens, _, err := token.Tokenize(idMap, filename, []byte(src))
tokens, _, err := token.Tokenize(tm, filename, []byte(src))
if err != nil {
t.Errorf("%q: Tokenize: %v", s, err)
continue
}
file, err := parse.Parse(idMap, filename, tokens)
file, err := parse.Parse(tm, filename, tokens)
if err != nil {
t.Errorf("%q: Parse: %v", s, err)
continue
}
c, err := Check(idMap, 0, file)
c, err := Check(tm, 0, file)
if err != nil {
t.Errorf("%q: Check: %v", s, err)
continue
+39 -39
View File
@@ -18,7 +18,7 @@ func (q *checker) tcheckVars(n *a.Node) error {
n := n.Var()
name := n.Name()
if _, ok := q.f.LocalVars[name]; ok {
return fmt.Errorf("check: duplicate var %q", name.String(q.idMap))
return fmt.Errorf("check: duplicate var %q", name.String(q.tm))
}
if err := q.tcheckTypeExpr(n.XType(), 0); err != nil {
return err
@@ -58,7 +58,7 @@ func (q *checker) tcheckStatement(n *a.Node) error {
}
if !cond.MType().Eq(TypeExprBool) {
return fmt.Errorf("check: if condition %q, of type %q, does not have a boolean type",
cond.String(q.idMap), cond.MType().String(q.idMap))
cond.String(q.tm), cond.MType().String(q.tm))
}
for _, o := range n.BodyIfTrue() {
if err := q.tcheckStatement(o); err != nil {
@@ -88,10 +88,10 @@ func (q *checker) tcheckStatement(n *a.Node) error {
if jumpTarget == nil {
sepStr, labelStr := "", ""
if id := n.Label(); id != 0 {
sepStr, labelStr = ":", id.String(q.idMap)
sepStr, labelStr = ":", id.String(q.tm)
}
return fmt.Errorf("no matching while statement for %s%s%s",
n.Keyword().String(q.idMap), sepStr, labelStr)
n.Keyword().String(q.tm), sepStr, labelStr)
}
if n.Keyword().Key() == t.KeyBreak {
jumpTarget.SetHasBreak()
@@ -113,7 +113,7 @@ func (q *checker) tcheckStatement(n *a.Node) error {
case a.KVar:
n := n.Var()
if !n.XType().Node().TypeChecked() {
return fmt.Errorf("check: internal error: unchecked type expression %q", n.XType().String(q.idMap))
return fmt.Errorf("check: internal error: unchecked type expression %q", n.XType().String(q.tm))
}
if value := n.Value(); value != nil {
if err := q.tcheckExpr(value, 0); err != nil {
@@ -130,7 +130,7 @@ func (q *checker) tcheckStatement(n *a.Node) error {
}
if !cond.MType().Eq(TypeExprBool) {
return fmt.Errorf("check: for-loop condition %q, of type %q, does not have a boolean type",
cond.String(q.idMap), cond.MType().String(q.idMap))
cond.String(q.tm), cond.MType().String(q.tm))
}
for _, o := range n.Asserts() {
if err := q.tcheckAssert(o.Assert()); err != nil {
@@ -163,7 +163,7 @@ func (q *checker) tcheckAssert(n *a.Assert) error {
}
if !cond.MType().Eq(TypeExprBool) {
return fmt.Errorf("check: assert condition %q, of type %q, does not have a boolean type",
cond.String(q.idMap), cond.MType().String(q.idMap))
cond.String(q.tm), cond.MType().String(q.tm))
}
for _, o := range n.Args() {
if err := q.tcheckExpr(o.Arg().Value(), 0); err != nil {
@@ -192,12 +192,12 @@ func (q *checker) tcheckAssign(n *a.Assign) error {
return nil
}
return fmt.Errorf("check: cannot assign %q of type %q to %q of type %q",
rhs.String(q.idMap), rTyp.String(q.idMap), lhs.String(q.idMap), lTyp.String(q.idMap))
rhs.String(q.tm), rTyp.String(q.tm), lhs.String(q.tm), lTyp.String(q.tm))
}
if !lTyp.IsNumType() {
return fmt.Errorf("check: assignment %q: assignee %q, of type %q, does not have numeric type",
n.Operator().String(q.idMap), lhs.String(q.idMap), lTyp.String(q.idMap))
n.Operator().String(q.tm), lhs.String(q.tm), lTyp.String(q.tm))
}
switch n.Operator().Key() {
@@ -206,16 +206,16 @@ func (q *checker) tcheckAssign(n *a.Assign) error {
return nil
}
return fmt.Errorf("check: assignment %q: shift %q, of type %q, does not have numeric type",
n.Operator().String(q.idMap), rhs.String(q.idMap), rTyp.String(q.idMap))
n.Operator().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm))
}
if rTyp == TypeExprIdeal || lTyp.EqIgnoringRefinements(rTyp) {
return nil
}
return fmt.Errorf("check: assignment %q: %q and %q, of types %q and %q, do not have compatible types",
n.Operator().String(q.idMap),
lhs.String(q.idMap), rhs.String(q.idMap),
lTyp.String(q.idMap), rTyp.String(q.idMap),
n.Operator().String(q.tm),
lhs.String(q.tm), rhs.String(q.tm),
lTyp.String(q.tm), rTyp.String(q.tm),
)
}
@@ -255,7 +255,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
id1 := n.ID1()
if id1.IsNumLiteral() {
z := big.NewInt(0)
s := id1.String(q.idMap)
s := id1.String(q.tm)
if _, ok := z.SetString(s, 0); !ok {
return fmt.Errorf("check: invalid numeric literal %q", s)
}
@@ -271,7 +271,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
}
}
// TODO: look for (global) names (constants, funcs, structs).
return fmt.Errorf("check: unrecognized identifier %q", id1.String(q.idMap))
return fmt.Errorf("check: unrecognized identifier %q", id1.String(q.tm))
}
switch id1.Key() {
case t.KeyFalse:
@@ -294,7 +294,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
case t.KeyOpenParen:
// n is a function call.
// TODO: delete this hack that only matches "in.src.read_u8?()".
if isInSrcReadU8(q.idMap, n.LHS().Expr()) && n.CallSuspendible() && len(n.Args()) == 0 {
if isInSrcReadU8(q.tm, n.LHS().Expr()) && n.CallSuspendible() && len(n.Args()) == 0 {
if err := q.tcheckExpr(n.LHS().Expr(), depth); err != nil {
return err
}
@@ -316,12 +316,12 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return fmt.Errorf("check: unrecognized token.Key (0x%X) for tcheckExprOther", n.ID0().Key())
}
func isInSrcReadU8(m *t.IDMap, n *a.Expr) bool {
func isInSrcReadU8(tm *t.Map, n *a.Expr) bool {
if n.ID0().Key() != t.KeyDot || n.ID1().Key() != t.KeyReadU8 {
return false
}
n = n.LHS().Expr()
if n.ID0().Key() != t.KeyDot || n.ID1() != m.ByName("src") {
if n.ID0().Key() != t.KeyDot || n.ID1() != tm.ByName("src") {
return false
}
n = n.LHS().Expr()
@@ -363,7 +363,7 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
}
if s == nil {
return fmt.Errorf("check: no struct type %q found for expression %q",
lTyp.Name().String(q.idMap), lhs.String(q.idMap))
lTyp.Name().String(q.tm), lhs.String(q.tm))
}
for _, field := range s.Fields() {
@@ -374,7 +374,7 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
}
}
return fmt.Errorf("check: no field named %q found in struct type %q for expression %q",
n.ID1().String(q.idMap), lTyp.Name().String(q.idMap), n.String(q.idMap))
n.ID1().String(q.tm), lTyp.Name().String(q.tm), n.String(q.tm))
}
func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
@@ -388,7 +388,7 @@ func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
case t.KeyXUnaryPlus, t.KeyXUnaryMinus:
if !numeric(rTyp) {
return fmt.Errorf("check: unary %q: %q, of type %q, does not have a numeric type",
n.ID0().AmbiguousForm().String(q.idMap), rhs.String(q.idMap), rTyp.String(q.idMap))
n.ID0().AmbiguousForm().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm))
}
if cv := rhs.ConstValue(); cv != nil {
if n.ID0().Key() == t.KeyXUnaryMinus {
@@ -402,7 +402,7 @@ func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
case t.KeyXUnaryNot:
if !rTyp.Eq(TypeExprBool) {
return fmt.Errorf("check: unary %q: %q, of type %q, does not have a boolean type",
n.ID0().AmbiguousForm().String(q.idMap), rhs.String(q.idMap), rTyp.String(q.idMap))
n.ID0().AmbiguousForm().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm))
}
if cv := rhs.ConstValue(); cv != nil {
n.SetConstValue(btoi(cv.Cmp(zero) == 0))
@@ -430,7 +430,7 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
return nil
}
return fmt.Errorf("check: cannot convert expression %q, of type %q, as type %q",
lhs.String(q.idMap), lTyp.String(q.idMap), rhs.String(q.idMap))
lhs.String(q.tm), lTyp.String(q.tm), rhs.String(q.tm))
}
rhs := n.RHS().Expr()
if err := q.tcheckExpr(rhs, depth); err != nil {
@@ -442,22 +442,22 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
default:
if !numeric(lTyp) {
return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type",
op.AmbiguousForm().String(q.idMap), lhs.String(q.idMap), lTyp.String(q.idMap))
op.AmbiguousForm().String(q.tm), lhs.String(q.tm), lTyp.String(q.tm))
}
if !numeric(rTyp) {
return fmt.Errorf("check: binary %q: %q, of type %q, does not have a numeric type",
op.AmbiguousForm().String(q.idMap), rhs.String(q.idMap), rTyp.String(q.idMap))
op.AmbiguousForm().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm))
}
case t.KeyXBinaryNotEq, t.KeyXBinaryEqEq:
// No-op.
case t.KeyXBinaryAnd, t.KeyXBinaryOr:
if lTyp != TypeExprBool {
return fmt.Errorf("check: binary %q: %q, of type %q, does not have a boolean type",
op.AmbiguousForm().String(q.idMap), lhs.String(q.idMap), lTyp.String(q.idMap))
op.AmbiguousForm().String(q.tm), lhs.String(q.tm), lTyp.String(q.tm))
}
if rTyp != TypeExprBool {
return fmt.Errorf("check: binary %q: %q, of type %q, does not have a boolean type",
op.AmbiguousForm().String(q.idMap), rhs.String(q.idMap), rTyp.String(q.idMap))
op.AmbiguousForm().String(q.tm), rhs.String(q.tm), rTyp.String(q.tm))
}
}
@@ -465,18 +465,18 @@ func (q *checker) tcheckExprBinaryOp(n *a.Expr, depth uint32) error {
default:
if !lTyp.EqIgnoringRefinements(rTyp) && lTyp != TypeExprIdeal && rTyp != TypeExprIdeal {
return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q, do not have compatible types",
op.AmbiguousForm().String(q.idMap),
lhs.String(q.idMap), rhs.String(q.idMap),
lTyp.String(q.idMap), rTyp.String(q.idMap),
op.AmbiguousForm().String(q.tm),
lhs.String(q.tm), rhs.String(q.tm),
lTyp.String(q.tm), rTyp.String(q.tm),
)
}
case t.KeyXBinaryShiftL, t.KeyXBinaryShiftR:
if (lTyp == TypeExprIdeal) && (rTyp != TypeExprIdeal) {
return fmt.Errorf("check: binary %q: %q and %q, of types %q and %q; "+
"cannot shift an ideal number by a non-ideal number",
op.AmbiguousForm().String(q.idMap),
lhs.String(q.idMap), rhs.String(q.idMap),
lTyp.String(q.idMap), rTyp.String(q.idMap),
op.AmbiguousForm().String(q.tm),
lhs.String(q.tm), rhs.String(q.tm),
lTyp.String(q.tm), rTyp.String(q.tm),
)
}
}
@@ -508,7 +508,7 @@ func (q *checker) setConstValueBinaryOp(n *a.Expr, l *big.Int, r *big.Int) error
n.SetConstValue(big.NewInt(0).Mul(l, r))
case t.KeyXBinarySlash:
if r.Cmp(zero) == 0 {
return fmt.Errorf("check: division by zero in const expression %q", n.String(q.idMap))
return fmt.Errorf("check: division by zero in const expression %q", n.String(q.tm))
}
// TODO: decide on Euclidean division vs other definitions. See "go doc
// math/big int.divmod" for details.
@@ -516,13 +516,13 @@ func (q *checker) setConstValueBinaryOp(n *a.Expr, l *big.Int, r *big.Int) error
case t.KeyXBinaryShiftL:
if r.Cmp(zero) < 0 || r.Cmp(ffff) > 0 {
return fmt.Errorf("check: shift %q out of range in const expression %q",
n.RHS().Expr().String(q.idMap), n.String(q.idMap))
n.RHS().Expr().String(q.tm), n.String(q.tm))
}
n.SetConstValue(big.NewInt(0).Lsh(l, uint(r.Uint64())))
case t.KeyXBinaryShiftR:
if r.Cmp(zero) < 0 || r.Cmp(ffff) > 0 {
return fmt.Errorf("check: shift %q out of range in const expression %q",
n.RHS().Expr().String(q.idMap), n.String(q.idMap))
n.RHS().Expr().String(q.tm), n.String(q.tm))
}
n.SetConstValue(big.NewInt(0).Rsh(l, uint(r.Uint64())))
case t.KeyXBinaryAmp:
@@ -575,7 +575,7 @@ func (q *checker) tcheckTypeExpr(n *a.TypeExpr, depth uint32) error {
return err
}
if b.ConstValue() == nil {
return fmt.Errorf("check: %q is not constant", b.String(q.idMap))
return fmt.Errorf("check: %q is not constant", b.String(q.tm))
}
}
break
@@ -587,7 +587,7 @@ func (q *checker) tcheckTypeExpr(n *a.TypeExpr, depth uint32) error {
break
}
// TODO: see if name refers to a struct type.
return fmt.Errorf("check: %q is not a type", n.Name().String(q.idMap))
return fmt.Errorf("check: %q is not a type", n.Name().String(q.tm))
case t.KeyPtr:
if err := q.tcheckTypeExpr(n.Inner(), depth); err != nil {
@@ -600,7 +600,7 @@ func (q *checker) tcheckTypeExpr(n *a.TypeExpr, depth uint32) error {
return err
}
if aLen.ConstValue() == nil {
return fmt.Errorf("check: %q is not constant", aLen.String(q.idMap))
return fmt.Errorf("check: %q is not constant", aLen.String(q.tm))
}
if err := q.tcheckTypeExpr(n.Inner(), depth); err != nil {
return err