From 7a9554194c2666ec2529ebb59a82333b6294b032 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 11 May 2018 14:49:15 +1000 Subject: [PATCH] Allow built-in args' types to be refined --- lang/builtin/builtin.go | 17 ++++++++--------- lang/check/bounds.go | 36 +++++------------------------------- lang/check/check.go | 6 ++++++ lang/check/resolve.go | 15 +++++++++------ lang/check/type.go | 2 +- 5 files changed, 29 insertions(+), 47 deletions(-) diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index 39712a60..0de65bc8 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -82,15 +82,14 @@ var Funcs = []string{ // TODO: some methods like "mark" and "set_limit" should probably have a // trailing "!". - // TODO: refine n's type from u32 to u32[..8], u32[..16], etc. - "u8.high_bits(n u32)(ret u8)", - "u8.low_bits(n u32)(ret u8)", - "u16.high_bits(n u32)(ret u16)", - "u16.low_bits(n u32)(ret u16)", - "u32.high_bits(n u32)(ret u32)", - "u32.low_bits(n u32)(ret u32)", - "u64.high_bits(n u32)(ret u64)", - "u64.low_bits(n u32)(ret u64)", + "u8.high_bits(n u32[..8])(ret u8)", + "u8.low_bits(n u32[..8])(ret u8)", + "u16.high_bits(n u32[..16])(ret u16)", + "u16.low_bits(n u32[..16])(ret u16)", + "u32.high_bits(n u32[..32])(ret u32)", + "u32.low_bits(n u32[..32])(ret u32)", + "u64.high_bits(n u32[..64])(ret u64)", + "u64.low_bits(n u32[..64])(ret u64)", "status.is_error()(ret bool)", "status.is_ok()(ret bool)", diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 830ecaf7..ae40dc6d 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -841,7 +841,11 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, // and type system. if methodName := lhs.Ident(); methodName == t.IDLowBits || methodName == t.IDHighBits { if recv := lhs.MType().Receiver(); recv != nil && recv.IsNumType() { - return q.bcheckLowHighBitsCall(n, depth) + _, aMax, err := q.bcheckExpr(n.Args()[0].Arg().Value(), depth) + if err != nil { + return nil, nil, err + } + return zero, bitMask(int(aMax.Int64())), nil } } @@ -933,36 +937,6 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, return q.bcheckTypeExpr(n.MType()) } -func (q *checker) bcheckLowHighBitsCall(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) { - max := (*big.Int)(nil) - qid := n.LHS().Expr().MType().Receiver().QID() - switch qid[1] { - case t.IDU8: - max = eight - case t.IDU16: - max = sixteen - case t.IDU32: - max = thirtyTwo - case t.IDU64: - max = sixtyFour - } - if qid[0] != t.IDBase || max == nil { - return nil, nil, fmt.Errorf("check: internal error: bad low_bits / high_bits receiver") - } - - a := n.Args()[0].Arg().Value() - _, aMax, err := q.bcheckExpr(a, depth) - if err != nil { - return nil, nil, err - } - if aMax.Cmp(max) > 0 { - return nil, nil, fmt.Errorf( - "check: internal error: bad low_bits / high_bits argument, despite type checking") - } - - return zero, bitMask(int(aMax.Int64())), nil -} - func (q *checker) bcheckExprCall(n *a.Expr, depth uint32) error { // TODO: handle func pre/post conditions. // diff --git a/lang/check/check.go b/lang/check/check.go index 36cb1cbd..a603fc78 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -486,6 +486,12 @@ func (c *Checker) checkFuncSignature(node *a.Node) error { // implicit "return out"? qqid := n.QQID() + if qqid[0] == t.IDBase { + // No need to populate c.funcs and c.localVars for built-in funcs. In + // any case, the remaining type checking code in this function doesn't + // handle the base.◊ diamond type. + return nil + } if other, ok := c.funcs[qqid]; ok { return &Error{ Err: fmt.Errorf("check: duplicate function %s", qqid.Str(c.tm)), diff --git a/lang/check/resolve.go b/lang/check/resolve.go index aeb8db93..4f71ec0c 100644 --- a/lang/check/resolve.go +++ b/lang/check/resolve.go @@ -72,7 +72,7 @@ var builtInTypeMap = typeMap{ func (c *Checker) builtInFunc(qqid t.QQID) (*a.Func, error) { if c.builtInFuncs == nil { err := error(nil) - c.builtInFuncs, err = parseBuiltInFuncs(c.tm, builtin.Funcs, false) + c.builtInFuncs, err = c.parseBuiltInFuncs(builtin.Funcs, false) if err != nil { return nil, err } @@ -83,7 +83,7 @@ func (c *Checker) builtInFunc(qqid t.QQID) (*a.Func, error) { func (c *Checker) builtInSliceFunc(qqid t.QQID) (*a.Func, error) { if c.builtInSliceFuncs == nil { err := error(nil) - c.builtInSliceFuncs, err = parseBuiltInFuncs(c.tm, builtin.SliceFuncs, true) + c.builtInSliceFuncs, err = c.parseBuiltInFuncs(builtin.SliceFuncs, true) if err != nil { return nil, err } @@ -91,7 +91,7 @@ func (c *Checker) builtInSliceFunc(qqid t.QQID) (*a.Func, error) { return c.builtInSliceFuncs[qqid], nil } -func parseBuiltInFuncs(tm *t.Map, ss []string, placeholder bool) (map[t.QQID]*a.Func, error) { +func (c *Checker) parseBuiltInFuncs(ss []string, placeholder bool) (map[t.QQID]*a.Func, error) { m := map[t.QQID]*a.Func{} buf := []byte(nil) opts := parse.Options{ @@ -104,7 +104,7 @@ func parseBuiltInFuncs(tm *t.Map, ss []string, placeholder bool) (map[t.QQID]*a. buf = append(buf, "{}\n"...) const filename = "builtin.wuffs" - tokens, _, err := t.Tokenize(tm, filename, buf) + tokens, _, err := t.Tokenize(c.tm, filename, buf) if err != nil { return nil, fmt.Errorf("check: could not tokenize built-in funcs: %v", err) } @@ -115,7 +115,7 @@ func parseBuiltInFuncs(tm *t.Map, ss []string, placeholder bool) (map[t.QQID]*a. } } } - file, err := parse.Parse(tm, filename, tokens, &opts) + file, err := parse.Parse(c.tm, filename, tokens, &opts) if err != nil { return nil, fmt.Errorf("check: could not parse built-in funcs: %v", err) } @@ -125,7 +125,10 @@ func parseBuiltInFuncs(tm *t.Map, ss []string, placeholder bool) (map[t.QQID]*a. return nil, fmt.Errorf("check: parsing %q: got %d top level decls, want %d", s, len(tlds), 1) } f := tlds[0].Func() - f.Node().Raw().SetPackage(tm, t.IDBase) + f.Node().Raw().SetPackage(c.tm, t.IDBase) + if err := c.checkFuncSignature(f.Node()); err != nil { + return nil, err + } m[f.QQID()] = f } return m, nil diff --git a/lang/check/type.go b/lang/check/type.go index 0c821d94..f1f63674 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -999,7 +999,7 @@ swtch: // TODO: reject. You can only refine numeric types. } if qid[0] == t.IDBase { - if _, ok := builtInTypeMap[qid[1]]; ok { + if _, ok := builtInTypeMap[qid[1]]; ok || qid[1] == t.IDDiamond { break swtch } }