Example #1
0
func containsMethod(m *st.FunctionSymbol, sym st.ITypeSymbol) (bool, *errors.GoRefactorError) {
	candidate, ok := sym.Methods().LookUp(m.Name(), "")
	if !ok {
		return false, nil
	}
	cand, ok := candidate.(*st.FunctionSymbol)
	if !ok {
		panic("non-method symbol " + candidate.Name() + " in type " + sym.Name() + " methods()")
	}

	if !st.EqualsMethods(cand, m) {
		return true, &errors.GoRefactorError{ErrorType: "implement interface error", Message: "type has a method, named " + cand.Name() + "but it has signature, different from the necessary one"}
	}

	return true, nil
}
Example #2
0
func (pp *packageParser) moveData(resolvedType st.ITypeSymbol, unresType st.ITypeSymbol) {
	for ident, _ := range unresType.Identifiers() {
		resolvedType.AddIdent(ident)
		pp.IdentMap.AddIdent(ident, resolvedType)
	}
	for _, pos := range unresType.Positions() {
		resolvedType.AddPosition(pos)
	}
}
Example #3
0
func (p *Program) GetPointerType(t st.ITypeSymbol) *st.PointerTypeSymbol {
	pack := t.PackageFrom()
	res, ok := pack.Symbols.LookUpPointerType(t.Name(), 1)
	if ok {
		return res
	}
	return st.MakePointerType(t.Scope(), t)
}
Example #4
0
func (mv *methodsVisitor) Visit(node ast.Node) (w ast.Visitor) {
	w = mv
	switch f := node.(type) {
	case *ast.FuncDecl:

		fft, cyc := st.GetBaseType(mv.Parser.parseTypeSymbol(f.Type))
		if cyc {
			panic("unexpected cycle")
		}
		ft := fft.(*st.FunctionTypeSymbol)
		locals := st.NewSymbolTable(mv.Parser.Package)

		locals.AddOpenedScope(ft.Parameters)
		locals.AddOpenedScope(ft.Results)
		locals.AddOpenedScope(ft.Reciever)

		var basertype, rtype st.ITypeSymbol
		if f.Recv != nil {

			e_count := 0
			for _, field := range f.Recv.List {
				basertype = mv.Parser.parseTypeSymbol(field.Type)
				if prtype, ok := basertype.(*st.PointerTypeSymbol); ok {
					rtype = prtype.BaseType
				} else {
					rtype = basertype
				}

				if mv.Parser.Package.AstPackage.Name == "os" {
					// 					fmt.Printf("###@@@### (%s) %s\n", rtype.Name(), f.Name.Name)
				}
				if rtype.Methods() == nil {
					panic("ok, this is a test panic")
					rtype.SetMethods(st.NewSymbolTable(mv.Parser.Package))
				}

				if len(field.Names) == 0 {
					toAdd := st.MakeVariable("$unnamed receiver"+strconv.Itoa(e_count), ft.Reciever, basertype)
					ft.Reciever.AddSymbol(toAdd)
					e_count += 1
				}

				for _, name := range field.Names {

					toAdd := st.MakeVariable(name.Name, ft.Reciever, basertype)
					mv.Parser.registerIdent(toAdd, name)
					ft.Reciever.AddSymbol(toAdd)
				}
			}
		}

		toAdd := st.MakeFunction(f.Name.Name, nil, ft) // Scope is set 5 lines down
		toAdd.Locals = locals

		mv.Parser.registerIdent(toAdd, f.Name)

		if f.Recv != nil {
			rtype.AddMethod(toAdd)
			toAdd.Scope_ = rtype.Methods()
		} else {
			mv.Parser.RootSymbolTable.AddSymbol(toAdd)
			toAdd.Scope_ = mv.Parser.RootSymbolTable
		}
	}
	return
}
Example #5
0
func (pp *packageParser) getOrAddPointer(base st.ITypeSymbol) (result *st.PointerTypeSymbol) {

	nameToFind := base.Name()

	//anonymous type
	if base.Name() == st.NO_NAME {
		result = st.MakePointerType(base.Scope(), base)
		if s, ok := base.(*st.StructTypeSymbol); ok {
			result.Fields = s.Fields
		}
		return
	}

	pd, found := 0, false
	if p, ok := base.(*st.PointerTypeSymbol); ok {
		pd = p.Depth()
		nameToFind = p.BaseName()
	}

	var toLookUp *st.SymbolTable
	if base.PackageFrom() == pp.Package || base.PackageFrom() == nil {
		toLookUp = pp.Package.Symbols
	} else {
		toLookUp = base.PackageFrom().Symbols
	}
	// 	if base.Name() == "typesVisitor" {
	// 		fmt.Printf("goap %v and pp is in %v.  toLookUp is from %s\n", base.PackageFrom().AstPackage.Name, pp.Package.AstPackage.Name, toLookUp.Package.AstPackage.Name)
	// 	}

	if result, found = toLookUp.LookUpPointerType(nameToFind, pd+1); found {
		// 		fmt.Printf("Searched Pointer Type %s (%p) ,%d, from package %s\n", nameToFind, result, pd+1, func(p *st.Package) string {
		// 			if p == nil {
		// 				return "nil"
		// 			}
		// 			return p.AstPackage.Name
		// 		}(result.PackageFrom()))
		return
	}

	result = st.MakePointerType(toLookUp, base)
	if s, ok := base.(*st.StructTypeSymbol); ok {
		result.Fields = s.Fields
	}
	// 	fmt.Printf("Adding Pointer Type %s to %s at file of %s\n", result.Name(), func(p *st.Package) string {
	// 		if p == nil {
	// 			return "nil"
	// 		}
	// 		return p.AstPackage.Name
	// 	}(base.PackageFrom()),
	// 		pp.Package.AstPackage.Name)

	toLookUp.AddSymbol(result)
	return
}