func assign(b *builder, dest, src tast.Expr, op *lex8.Token) tast.Stmt { destRef := dest.R() srcRef := src.R() ndest := destRef.Len() nsrc := srcRef.Len() if ndest != nsrc { b.Errorf(op.Pos, "cannot assign %s to %s", nsrc, ndest) return nil } for i := 0; i < ndest; i++ { r := destRef.At(i) if !r.Addressable { b.Errorf(op.Pos, "assigning to non-addressable") return nil } destType := r.Type() srcType := srcRef.At(i).Type() if !types.CanAssign(destType, srcType) { b.Errorf(op.Pos, "cannot assign %s to %s", srcType, destType) return nil } } // insert casting if needed if srcList, ok := tast.MakeExprList(src); ok { newList := tast.NewExprList() for i, e := range srcList.Exprs { t := e.Type() if types.IsNil(t) { e = tast.NewCast(e, destRef.At(i).Type()) } else if v, ok := types.NumConst(t); ok { e = constCast(b, nil, v, e, destRef.At(i).Type()) if e == nil { panic("bug") } } newList.Append(e) } src = newList } return &tast.AssignStmt{dest, op, src} }
func buildReturnStmt(b *builder, stmt *ast.ReturnStmt) tast.Stmt { pos := stmt.Kw.Pos if stmt.Exprs == nil { if b.retType == nil || b.retNamed { return &tast.ReturnStmt{} } b.Errorf(pos, "expects return %s", fmt8.Join(b.retType, ",")) return nil } if b.retType == nil { b.Errorf(pos, "function expects no return value") return nil } src := b.buildExpr(stmt.Exprs) if src == nil { return nil } srcRef := src.R() nret := len(b.retType) nsrc := srcRef.Len() if nret != nsrc { b.Errorf(pos, "expect (%s), returning (%s)", fmt8.Join(b.retType, ","), srcRef, ) return nil } for i := 0; i < nret; i++ { t := b.retType[i] srcType := srcRef.At(i).Type() if !types.CanAssign(t, srcType) { b.Errorf(pos, "expect (%s), returning (%s)", fmt8.Join(b.retType, ","), srcRef, ) return nil } } // insert implicit type casts if srcList, ok := tast.MakeExprList(src); ok { newList := tast.NewExprList() for i, e := range srcList.Exprs { t := e.Type() if types.IsNil(t) { e = tast.NewCast(e, b.retType[i]) } else if v, ok := types.NumConst(t); ok { e = constCast(b, nil, v, e, b.retType[i]) if e == nil { panic("bug") } } newList.Append(e) } src = newList } return &tast.ReturnStmt{src} }
func buildVarDecl(b *builder, d *ast.VarDecl) *tast.Define { ids := d.Idents.Idents if d.Eq != nil { right := b.buildExpr(d.Exprs) if right == nil { return nil } if d.Type == nil { ret := define(b, ids, right, d.Eq) if ret == nil { return nil } return ret } tdest := b.buildType(d.Type) if tdest == nil { return nil } if !types.IsAllocable(tdest) { pos := ast.ExprPos(d.Type) b.Errorf(pos, "%s is not allocatable", tdest) return nil } // assignable check ts := right.R().TypeList() for _, t := range ts { if !types.CanAssign(tdest, t) { b.Errorf(d.Eq.Pos, "cannot assign %s to %s", t, tdest) return nil } } // cast literal expression lists // after the casting, all types should be matching to tdest if exprList, ok := tast.MakeExprList(right); ok { exprList = varDeclPrepare(b, ids, exprList, tdest) if exprList == nil { return nil } right = exprList } syms := declareVars(b, ids, tdest, false) if syms == nil { return nil } return &tast.Define{syms, right} } if d.Type == nil { panic("type missing") } t := b.buildType(d.Type) if t == nil { return nil } syms := declareVars(b, ids, t, false) if syms == nil { return nil } return &tast.Define{syms, nil} }
func buildCallExpr(b *builder, expr *ast.CallExpr) tast.Expr { f := b.buildExpr(expr.Func) if f == nil { return nil } pos := ast.ExprPos(expr.Func) fref := f.R() if !fref.IsSingle() { b.Errorf(pos, "%s is not callable", fref) return nil } if t, ok := fref.T.(*types.Type); ok { return buildCast(b, expr, t.T) } builtin, ok := fref.T.(*types.BuiltInFunc) if ok { switch builtin.Name { case "len": return buildCallLen(b, expr, f) case "make": return buildCallMake(b, expr, f) } b.Errorf(pos, "builtin %s() not implemented", builtin.Name) return nil } funcType, ok := fref.T.(*types.Func) if !ok { b.Errorf(pos, "function call on non-callable: %s", fref) return nil } args := buildExprList(b, expr.Args) if args == nil { return nil } argsRef := args.R() nargs := argsRef.Len() if nargs != len(funcType.Args) { b.Errorf(ast.ExprPos(expr), "argument expects (%s), got (%s)", fmt8.Join(funcType.Args, ","), args, ) return nil } // type check on each argument for i := 0; i < nargs; i++ { argType := argsRef.At(i).Type() expect := funcType.Args[i].T if !types.CanAssign(expect, argType) { pos := ast.ExprPos(expr.Args.Exprs[i]) b.Errorf(pos, "argument %d expects %s, got %s", i+1, expect, argType, ) return nil } } // insert casting when it is a literal expression list. callArgs, ok := tast.MakeExprList(args) if ok { castedArgs := tast.NewExprList() for i := 0; i < nargs; i++ { argExpr := callArgs.Exprs[i] argType := argExpr.Type() expect := funcType.Args[i].T // insert auto casts for consts if types.IsNil(argType) { castedArgs.Append(tast.NewCast(argExpr, expect)) } else if _, ok := types.NumConst(argType); ok { castedArgs.Append(tast.NewCast(argExpr, expect)) } else { castedArgs.Append(argExpr) } } args = castedArgs } retRef := tast.Void for _, t := range funcType.RetTypes { retRef = tast.AppendRef(retRef, tast.NewRef(t)) } return &tast.CallExpr{f, args, retRef} }