Allow built-in args' types to be refined

This commit is contained in:
Nigel Tao committed 2018-05-11 14:49:15 +10:00
1 parent a69948b23d
commit 7a9554194c
5 files changed
+29 -47

No files matched your search

+8 -9
View File
@@ -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)",
+5 -31
View File
@@ -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.
//
+6
View File
@@ -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)),
+9 -6
View File
@@ -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
+1 -1
View File
@@ -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
}
}