Esempio n. 1
0
func buildPkgSym(b *builder, m *tast.MemberExpr, pkg *types.Pkg) *ref {
	sym := m.Symbol
	if pkg.Lang == "asm8" {
		switch sym.Type {
		case asm8.SymVar:
			if m.Type() != types.Uint {
				panic("bug")
			}
			ptr := b.newTemp(types.Uint)
			s := ir.NewHeapSym(sym.Pkg(), sym.Name(), 0, false, false)
			b.b.Arith(ptr.IR(), nil, "&", s)
			return ptr
		case asm8.SymFunc:
			if !types.SameType(m.Type(), types.VoidFunc) {
				panic("bug")
			}
			return newRef(
				types.VoidFunc,
				ir.NewFuncSym(sym.Pkg(), sym.Name(), ir.VoidFuncSig),
			)
		}
		panic("bug")
	}

	switch sym.Type {
	case tast.SymConst:
		return sym.Obj.(*objConst).ref
	case tast.SymVar:
		return sym.Obj.(*objVar).ref
	case tast.SymFunc:
		return sym.Obj.(*objFunc).ref
	}
	panic("bug")
}
Esempio n. 2
0
func findFunc(b *builder, name string, t types.T) *objFunc {
	s := b.scope.Query(name)
	if s == nil {
		return nil
	}
	f, isFunc := s.Obj.(*objFunc)
	if !isFunc {
		return nil
	}
	if !types.SameType(f.ref.Type(), t) {
		return nil
	}
	return f
}
Esempio n. 3
0
func listTests(tops *sym8.Table) []*objFunc {
	var list []*objFunc

	syms := tops.List()
	for _, s := range syms {
		if s.Type != tast.SymFunc {
			continue
		}
		f := s.Obj.(*objFunc)
		if f.isMethod {
			panic("bug") // a top level function should never be a method
		}
		if !types.SameType(f.ref.Type(), types.VoidFunc) {
			continue
		}
		name := s.Name()
		if isTestName(name) {
			list = append(list, f)
		}
	}

	return list
}
Esempio n. 4
0
func buildCallMake(b *builder, expr *ast.CallExpr, f tast.Expr) tast.Expr {
	args := buildExprList(b, expr.Args)
	if args == nil {
		return nil
	}

	n := args.R().Len()
	if n == 0 {
		b.Errorf(expr.Lparen.Pos, "make() takes at least one argument")
		return nil
	}

	argsList, ok := tast.MakeExprList(args)
	if !ok {
		b.Errorf(expr.Lparen.Pos, "make() only takes a literal list")
		return nil
	}

	arg0 := argsList.Exprs[0]
	t, ok := arg0.R().T.(*types.Type)
	if !ok {
		b.Errorf(expr.Lparen.Pos, "make() takes a type as the 1st argument")
		return nil
	}
	switch st := t.T.(type) {
	case *types.Slice:
		if n != 3 {
			b.Errorf(expr.Lparen.Pos, "make() slice takes 3 arguments")
			return nil
		}

		size := argsList.Exprs[1]
		pos := ast.ExprPos(expr.Args.Exprs[1])
		size = checkArrayIndex(b, size, pos)
		if size == nil {
			return nil
		}

		start := argsList.Exprs[2]
		startType := start.R().T
		startPos := ast.ExprPos(expr.Args.Exprs[2])
		if v, ok := types.NumConst(startType); ok {
			start = constCastUint(b, startPos, v, start)
			if start == nil {
				return nil
			}
		} else if !types.IsBasic(startType, types.Uint) {
			pt := types.PointerOf(startType)
			if pt == nil || !types.SameType(pt, st.T) {
				b.Errorf(startPos,
					"make() takes an uint or a typed pointer as the 3rd arg",
				)
				return nil
			}
		}

		callArgs := tast.NewExprList()
		callArgs.Append(arg0)
		callArgs.Append(size)
		callArgs.Append(start)

		r := tast.NewRef(st)
		return &tast.CallExpr{f, callArgs, r}
	}

	b.Errorf(expr.Lparen.Pos, "cannot make() type %s", t.T)
	return nil
}