mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 08:47:56 +01:00
Type check built-in functions more thoroughly
This commit is contained in:
1 parent
4877279ee7
commit
d14b56ea39
15 files changed
+291
-163
No files matched your search
@@ -0,0 +1,121 @@
|
||||
// Copyright 2017 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:generate go run gen.go
|
||||
|
||||
package check
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/google/wuffs/lang/builtin"
|
||||
"github.com/google/wuffs/lang/parse"
|
||||
|
||||
a "github.com/google/wuffs/lang/ast"
|
||||
t "github.com/google/wuffs/lang/token"
|
||||
)
|
||||
|
||||
var (
|
||||
exprIn = a.NewExpr(a.FlagsTypeChecked, 0, t.IDIn, nil, nil, nil, nil)
|
||||
exprOut = a.NewExpr(a.FlagsTypeChecked, 0, t.IDOut, nil, nil, nil, nil)
|
||||
exprThis = a.NewExpr(a.FlagsTypeChecked, 0, t.IDThis, nil, nil, nil, nil)
|
||||
)
|
||||
|
||||
// typeExprFoo is an *ast.Node MType (implicit type).
|
||||
var (
|
||||
typeExprIdeal = a.NewTypeExpr(0, t.IDDoubleZ, nil, nil, nil)
|
||||
typeExprList = a.NewTypeExpr(0, t.IDDollar, nil, nil, nil)
|
||||
|
||||
typeExprU8 = a.NewTypeExpr(0, t.IDU8, nil, nil, nil)
|
||||
typeExprU16 = a.NewTypeExpr(0, t.IDU16, nil, nil, nil)
|
||||
typeExprU32 = a.NewTypeExpr(0, t.IDU32, nil, nil, nil)
|
||||
typeExprU64 = a.NewTypeExpr(0, t.IDU64, nil, nil, nil)
|
||||
typeExprBool = a.NewTypeExpr(0, t.IDBool, nil, nil, nil)
|
||||
typeExprStatus = a.NewTypeExpr(0, t.IDStatus, nil, nil, nil)
|
||||
typeExprReader1 = a.NewTypeExpr(0, t.IDReader1, nil, nil, nil)
|
||||
typeExprWriter1 = a.NewTypeExpr(0, t.IDWriter1, nil, nil, nil)
|
||||
|
||||
typeExprSliceU8 = a.NewTypeExpr(t.IDColon, 0, nil, nil, typeExprU8)
|
||||
|
||||
// TODO: delete this.
|
||||
typeExprPlaceholder = a.NewTypeExpr(0, t.IDU8, nil, nil, nil)
|
||||
typeExprPlaceholder16 = a.NewTypeExpr(0, t.IDU16, nil, nil, nil)
|
||||
typeExprPlaceholder32 = a.NewTypeExpr(0, t.IDU32, nil, nil, nil)
|
||||
)
|
||||
|
||||
// TypeMap maps from variable names (as token IDs) to types.
|
||||
type TypeMap map[t.ID]*a.TypeExpr
|
||||
|
||||
var builtInTypeMap = TypeMap{
|
||||
t.IDU8: typeExprU8,
|
||||
t.IDU16: typeExprU16,
|
||||
t.IDU32: typeExprU32,
|
||||
t.IDU64: typeExprU64,
|
||||
t.IDBool: typeExprBool,
|
||||
t.IDStatus: typeExprStatus,
|
||||
t.IDReader1: typeExprReader1,
|
||||
t.IDWriter1: typeExprWriter1,
|
||||
}
|
||||
|
||||
func (c *Checker) builtInFunc(qid t.QID) (*a.Func, error) {
|
||||
if c.builtInFuncs == nil {
|
||||
c.builtInFuncs = map[t.QID]*a.Func{}
|
||||
|
||||
buf := []byte(nil)
|
||||
opts := parse.Options{
|
||||
AllowBuiltIns: true,
|
||||
}
|
||||
for _, s := range builtin.Funcs {
|
||||
buf = buf[:0]
|
||||
buf = append(buf, "pub func "...)
|
||||
buf = append(buf, s...)
|
||||
buf = append(buf, "{}\n"...)
|
||||
|
||||
const filename = "builtin.wuffs"
|
||||
tokens, _, err := t.Tokenize(c.tm, filename, buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
file, err := parse.Parse(c.tm, filename, tokens, &opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tlds := file.TopLevelDecls()
|
||||
if len(tlds) != 1 || tlds[0].Kind() != a.KFunc {
|
||||
return nil, fmt.Errorf("parsing %c: got %d top level decls, want %d", s, len(tlds), 1)
|
||||
}
|
||||
f := tlds[0].Func()
|
||||
c.builtInFuncs[f.QID()] = f
|
||||
}
|
||||
}
|
||||
return c.builtInFuncs[qid], nil
|
||||
}
|
||||
|
||||
func (c *Checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) {
|
||||
// TODO: look up funcs in used packages.
|
||||
if typ.Decorator().Key() != t.KeyOpenParen {
|
||||
return nil, fmt.Errorf("check: resolveFunc cannot look up non-func TypeExpr %q", typ.Str(c.tm))
|
||||
}
|
||||
qid := t.QID{typ.Receiver().Name(), typ.Name()}
|
||||
if f, err := c.builtInFunc(qid); err != nil {
|
||||
return nil, err
|
||||
} else if f != nil {
|
||||
return f, nil
|
||||
}
|
||||
if f := c.funcs[qid].Func; f != nil {
|
||||
return f, nil
|
||||
}
|
||||
return nil, fmt.Errorf("check: resolveFunc cannot look up %q", typ.Str(c.tm))
|
||||
}
|
||||
Reference in new issue
Block a user