mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 16:52:18 +01:00
Change func out-type from struct to bare type
This commit is contained in:
1 parent
af7b15bc17
commit
9919d78245
19 files changed
+236
-239
No files matched your search
+33
-19
@@ -419,8 +419,7 @@ func (c *Checker) checkStructDecl(node *a.Node) error {
|
||||
|
||||
// A struct declaration implies a reset method.
|
||||
in := a.NewStruct(0, n.Filename(), n.Line(), t.IDIn, nil)
|
||||
out := a.NewStruct(0, n.Filename(), n.Line(), t.IDOut, nil)
|
||||
f := a.NewFunc(0, n.Filename(), n.Line(), qid[1], t.IDReset, in, out, nil, nil)
|
||||
f := a.NewFunc(0, n.Filename(), n.Line(), qid[1], t.IDReset, in, nil, nil, nil)
|
||||
if qid[0] != 0 {
|
||||
f.AsNode().AsRaw().SetPackage(c.tm, qid[0])
|
||||
}
|
||||
@@ -461,11 +460,8 @@ func (c *Checker) checkFields(fields []*a.Node, banPtrTypes bool, checkDefaultZe
|
||||
if _, ok := fieldNames[f.Name()]; ok {
|
||||
return fmt.Errorf("check: duplicate field %q", f.Name().Str(c.tm))
|
||||
}
|
||||
if err := q.tcheckTypeExpr(f.XType(), 0); err != nil {
|
||||
return fmt.Errorf("%v in field %q", err, f.Name().Str(c.tm))
|
||||
}
|
||||
if _, err := q.bcheckTypeExpr(f.XType()); err != nil {
|
||||
return fmt.Errorf("%v in field %q", err, f.Name().Str(c.tm))
|
||||
if err := checkTypeExpr(q, f.XType()); err != nil {
|
||||
return fmt.Errorf("%v for field %q", err, f.Name().Str(c.tm))
|
||||
}
|
||||
if banPtrTypes && f.XType().HasPointers() {
|
||||
return fmt.Errorf("check: pointer-containing type %q not allowed for field %q",
|
||||
@@ -487,6 +483,16 @@ func (c *Checker) checkFields(fields []*a.Node, banPtrTypes bool, checkDefaultZe
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkTypeExpr(q *checker, n *a.TypeExpr) error {
|
||||
if err := q.tcheckTypeExpr(n, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := q.bcheckTypeExpr(n); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Checker) checkFuncSignature(node *a.Node) error {
|
||||
n := node.AsFunc()
|
||||
if err := c.checkFields(n.In().Fields(), false, false); err != nil {
|
||||
@@ -497,17 +503,23 @@ func (c *Checker) checkFuncSignature(node *a.Node) error {
|
||||
}
|
||||
}
|
||||
setPlaceholderMBoundsMType(n.In().AsNode())
|
||||
if err := c.checkFields(n.Out().Fields(), false, true); err != nil {
|
||||
return &Error{
|
||||
Err: fmt.Errorf("%v in out-params for func %s", err, n.QQID().Str(c.tm)),
|
||||
Filename: n.Filename(),
|
||||
Line: n.Line(),
|
||||
if out := n.Out(); out != nil {
|
||||
// TODO: does checking a TypeExpr need a q?
|
||||
q := &checker{
|
||||
c: c,
|
||||
tm: c.tm,
|
||||
}
|
||||
if err := checkTypeExpr(q, out); err != nil {
|
||||
return &Error{
|
||||
Err: fmt.Errorf("%v in out-param for func %s", err, n.QQID().Str(c.tm)),
|
||||
Filename: n.Filename(),
|
||||
Line: n.Line(),
|
||||
}
|
||||
}
|
||||
}
|
||||
setPlaceholderMBoundsMType(n.Out().AsNode())
|
||||
setPlaceholderMBoundsMType(n.AsNode())
|
||||
|
||||
// TODO: check somewhere that, if n.Out() is non-empty (or we are
|
||||
// TODO: check somewhere that, if n.Out() is non-nil (or we are
|
||||
// suspendible), that we end with a return statement? Or is that an
|
||||
// implicit "return out"?
|
||||
|
||||
@@ -535,13 +547,15 @@ func (c *Checker) checkFuncSignature(node *a.Node) error {
|
||||
inTyp.AsNode().SetMBounds(a.Bounds{zero, zero})
|
||||
inTyp.AsNode().SetMType(typeExprTypeExpr)
|
||||
|
||||
oQID := n.Out().QID()
|
||||
outTyp := a.NewTypeExpr(0, oQID[0], oQID[1], nil, nil, nil)
|
||||
outTyp.AsNode().SetMBounds(a.Bounds{zero, zero})
|
||||
outTyp.AsNode().SetMType(typeExprTypeExpr)
|
||||
outTyp := n.Out()
|
||||
if outTyp == nil {
|
||||
outTyp = typeExprEmptyStruct
|
||||
}
|
||||
|
||||
localVars := typeMap{
|
||||
t.IDIn: inTyp,
|
||||
t.IDIn: inTyp,
|
||||
// TODO: drop the implicit "out" variable (and exprOut in this
|
||||
// package)?
|
||||
t.IDOut: outTyp,
|
||||
}
|
||||
if qqid[1] != 0 {
|
||||
|
||||
@@ -125,7 +125,7 @@ func (c *Checker) parseBuiltInFuncs(ss []string, generic bool) (map[t.QQID]*a.Fu
|
||||
const filename = "builtin.wuffs"
|
||||
tokens, _, err := t.Tokenize(c.tm, filename, buf)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("check: could not tokenize built-in funcs: %v", err)
|
||||
return nil, fmt.Errorf("check: parsing %q: could not tokenize built-in funcs: %v", s, err)
|
||||
}
|
||||
if generic {
|
||||
for i := range tokens {
|
||||
@@ -138,7 +138,7 @@ func (c *Checker) parseBuiltInFuncs(ss []string, generic bool) (map[t.QQID]*a.Fu
|
||||
}
|
||||
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)
|
||||
return nil, fmt.Errorf("check: parsing %q: could not parse built-in funcs: %v", s, err)
|
||||
}
|
||||
|
||||
tlds := file.TopLevelDecls()
|
||||
|
||||
+10
-23
@@ -581,23 +581,15 @@ func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error {
|
||||
if n.Operator() == t.IDTry {
|
||||
n.SetMType(typeExprStatus)
|
||||
} else {
|
||||
outFields := f.Out().Fields()
|
||||
switch len(outFields) {
|
||||
default:
|
||||
// TODO: figure out how to translate f.Out(), which is an
|
||||
// *ast.Struct, to an *ast.TypeExpr we can pass to n.SetMType.
|
||||
return fmt.Errorf("TODO: translate an *ast.Struct to an *ast.TypeExpr")
|
||||
case 0:
|
||||
oTyp := f.Out()
|
||||
if oTyp == nil {
|
||||
n.SetMType(typeExprEmptyStruct)
|
||||
case 1:
|
||||
oTyp := outFields[0].AsField().XType()
|
||||
if genericType1 != nil && oTyp.Eq(typeExprGeneric1) {
|
||||
n.SetMType(genericType1)
|
||||
} else if genericType2 != nil && oTyp.Eq(typeExprGeneric2) {
|
||||
n.SetMType(genericType2)
|
||||
} else {
|
||||
n.SetMType(oTyp)
|
||||
}
|
||||
} else if genericType1 != nil && oTyp.Eq(typeExprGeneric1) {
|
||||
n.SetMType(genericType1)
|
||||
} else if genericType2 != nil && oTyp.Eq(typeExprGeneric2) {
|
||||
n.SetMType(genericType2)
|
||||
} else {
|
||||
n.SetMType(oTyp)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -640,13 +632,8 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
|
||||
}
|
||||
|
||||
s := (*a.Struct)(nil)
|
||||
if q.astFunc != nil && lQID[0] == 0 {
|
||||
switch lQID[1] {
|
||||
case t.IDIn:
|
||||
s = q.astFunc.In()
|
||||
case t.IDOut:
|
||||
s = q.astFunc.Out()
|
||||
}
|
||||
if q.astFunc != nil && lQID[0] == 0 && lQID[1] == t.IDIn {
|
||||
s = q.astFunc.In()
|
||||
}
|
||||
if s == nil {
|
||||
s = q.c.structs[lQID]
|
||||
|
||||
Reference in new issue
Block a user