Example #1
0
func (this *SingleMover) CollectUnexportedObjs() {
	this.unexportedObjs = make(map[*ast.Object]bool)
	for node, obj := range this.moveNodes {
		//printer.Fprint(os.Stdout, token.NewFileSet(), node)
		//fmt.Printf("\n%v %T\n", obj, node)

		if !unicode.IsUpper(utf8.NewString(obj.Name).At(0)) {
			this.unexportedObjs[obj] = true
		}

		if ts, ok := node.(*ast.TypeSpec); ok {
			if st, ok := ts.Type.(*ast.StructType); ok {
				for _, field := range st.Fields.List {
					for _, name := range field.Names {
						if !name.IsExported() {
							obj, _ := types.ExprType(name, LocalImporter)
							this.unexportedObjs[obj] = true
						}
					}
				}
			}
		}
	}
	return
}
Example #2
0
func (this *FieldDeclFinder) Visit(node ast.Node) ast.Visitor {
	if this.Obj != nil {
		return nil
	}
	switch n := node.(type) {
	case *ast.BlockStmt:
		return nil
	case *ast.TypeSpec:
		if n.Name.Name == this.typename {
			if st, ok := n.Type.(*ast.StructType); ok {
				for _, field := range st.Fields.List {
					for _, nid := range field.Names {
						if nid.Name == this.newname {
							this.NameExists = true
							return nil
						}
						if nid.Name == this.oldname {
							nid.Name = this.newname
							this.Obj, _ = types.ExprType(nid, LocalImporter)
							this.Updated = true
							this.Name = nid
							return nil
						}
					}
				}
			}
			return this
		}
		return nil
	}
	return this
}
Example #3
0
func (this DepthWalker) Visit(node ast.Node) ast.Visitor {
	if node == nil {
		return this + 1
	}

	buffer := ""
	for i := 0; i < int(this); i++ {
		buffer += " "
	}

	fmt.Printf("%sPos: %d %s\n", buffer, node.Pos(), AllSources.Position(node.Pos()))
	fmt.Printf("%sEnd: %d %s\n", buffer, node.End(), AllSources.Position(node.End()))
	fmt.Printf("%s%T\n", buffer, node)
	fmt.Printf("%s%v\n", buffer, node)
	if e, ok := node.(ast.Expr); ok {
		obj, typ := types.ExprType(e, LocalImporter)
		fmt.Printf("%s%v\n", buffer, obj)
		fmt.Printf("%s%v\n", buffer, typ)
	}
	fmt.Println()

	switch n := node.(type) {

	}

	return this + 1
}
Example #4
0
func (this *RenameWalker) Visit(node ast.Node) ast.Visitor {
	switch n := node.(type) {
	case *ast.Ident:
		obj, _ := types.ExprType(n, LocalImporter)
		if obj == this.Obj {
			this.Updated = true
			n.Name = this.NewName
		}
	case *ast.SelectorExpr:
		obj, _ := types.ExprType(n, LocalImporter)
		if obj == this.Obj {
			this.Updated = true
			n.Sel.Name = this.NewName
		}
	}
	return this
}
Example #5
0
func (this *DeclFinder) Visit(node ast.Node) ast.Visitor {
	if this.Obj != nil {
		return nil
	}
	switch n := node.(type) {
	case *ast.BlockStmt:
		return nil
	case *ast.ValueSpec:
		for _, name := range n.Names {
			if name.Name == this.newname {
				this.NameExists = true
				return nil
			}
			if name.Name == this.oldname {
				//this.Name = name
				this.Obj, _ = types.ExprType(name, LocalImporter)
				this.Node = node
				return nil
			}
		}
		return nil
	case *ast.FuncDecl:
		if n.Name.Name == this.newname {
			this.NameExists = true
		}
		if n.Name.Name == this.oldname {
			//this.Name = n.Name
			this.Node = node
			this.Obj, _ = types.ExprType(n.Name, LocalImporter)
		}
		return nil
	case *ast.TypeSpec:
		if n.Name.Name == this.newname {
			this.NameExists = true
		}
		if n.Name.Name == this.oldname {
			//this.Name = n.Name
			this.Node = node
			this.Obj, _ = types.ExprType(n.Name, LocalImporter)
		}
		return nil
	}
	return this
}
Example #6
0
File: pkg.go Project: newblue/gorf
func (this *PkgChanger) Visit(node ast.Node) ast.Visitor {
	if this.Renamed {
		return nil
	}

	if node == nil {
		return this
	}

	switch n := node.(type) {
	case *ast.ImportSpec:
		if string(n.Path.Value) == QuotePath(this.path) {
			if n.Name != nil {

				this.Renamed = true
				return nil
			}

			if n.Name == nil && this.newname != this.pkgname {
				n.Name = &ast.Ident{
					Name:    this.newname,
					NamePos: n.Pos(),
				}
				this.Updated = true
			}
		}

	case *ast.Ident:
		if n.Name == this.oldname {
			obj, typ := types.ExprType(n, LocalImporter)
			if obj == nil {
				n.Name = this.newname
				this.Updated = true
			}
			if obj != nil {
				if typ.Kind == ast.Pkg && obj.Name == this.oldname {
					n.Name = this.newname
					this.Updated = true
				}
			}
		}

	}

	return this
}
Example #7
0
func (this *SingleMover) UpdateOther() (err os.Error) {
	for _, path := range ImportedBy[QuotePath(this.oldpath)] {
		opkg := LocalImporter(path)

		for fpath, file := range opkg.Files {
			rw := ReferenceWalker{
				UnexportedObjs:       make(map[*ast.Object]bool),
				MoveObjs:             this.moveObjs,
				SkipNodes:            make(map[ast.Node]*ast.Object),
				SkipNodeParents:      make(map[ast.Node]ast.Node),
				GoodReferenceParents: make(map[ast.Node]ast.Node),
				BadReferences:        &[]ast.Node{},
			}
			ast.Walk(&rw, file)

			if len(rw.GoodReferenceParents) == 0 {
				continue
			}

			newpkgname := GetUniqueIdent([]*ast.File{file}, this.pkg.Name)

			//construct the import
			nis := &ast.ImportSpec{
				Name: &ast.Ident{Name: newpkgname},
				Path: &ast.BasicLit{
					Kind:  token.STRING,
					Value: QuotePath(this.newpath),
				},
			}

			ngd := &ast.GenDecl{
				Tok:   token.IMPORT,
				Specs: []ast.Spec{nis},
			}
			file.Decls = append([]ast.Decl{ngd}, file.Decls...)

			for node, parent := range rw.GoodReferenceParents {
				getSel := func(sel *ast.SelectorExpr) *ast.SelectorExpr {
					obj, _ := types.ExprType(sel.X, LocalImporter)
					if obj.Kind == ast.Pkg {
						return &ast.SelectorExpr{
							X: &ast.Ident{
								Name:    newpkgname,
								NamePos: sel.X.Pos(),
							},
							Sel: sel.Sel,
						}
					}
					return sel
				}

				switch p := parent.(type) {
				case *ast.CallExpr:
					if sel, ok := node.(*ast.SelectorExpr); ok {

						p.Fun = getSel(sel)
					} else {

						return MakeErr("CallExpr w/ unexpected type %T\n", node)
					}
				case *ast.AssignStmt:
					for i, x := range p.Lhs {
						if x == node {
							if sel, ok := x.(*ast.SelectorExpr); ok {
								p.Lhs[i] = getSel(sel)
							}
						}
					}
					for i, x := range p.Rhs {
						if x == node {
							if sel, ok := x.(*ast.SelectorExpr); ok {
								p.Rhs[i] = getSel(sel)
							}
						}
					}
				case *ast.ValueSpec:
					if node == p.Type {
						if sel, ok := p.Type.(*ast.SelectorExpr); ok {
							p.Type = getSel(sel)
						}
					}
					for i, x := range p.Values {
						if x == node {
							if sel, ok := x.(*ast.SelectorExpr); ok {
								p.Values[i] = getSel(sel)
							}
						}
					}
				case *ast.StarExpr:
					if p.X == node {
						if sel, ok := p.X.(*ast.SelectorExpr); ok {
							p.X = getSel(sel)
						}
					}
				default:
					printer.Fprint(os.Stdout, AllSources, parent)
					return MakeErr("Unexpected remote parent %T\n", parent)
				}
			}

			//now that we've renamed some references, do we still need to import oldpath?
			oc := ObjChecker{
				Objs: this.remainingObjs,
			}
			ast.Walk(&oc, file)
			if !oc.Found {
				ast.Walk(&ImportRemover{nil, this.oldpath}, file)
			}

			err = RewriteSource(fpath, file)
			if err != nil {
				return
			}
		}
	}

	return
}
Example #8
0
func (this *SingleMover) CreateNewSource() (err os.Error) {

	liw := make(ListImportWalker)
	for n := range this.moveNodes {
		ast.Walk(liw, n)
	}
	finalImports := make(map[*ast.ImportSpec]bool)
	for obj, is := range liw {
		if _, ok := this.moveObjs[obj]; !ok {
			finalImports[is] = true
		}
	}

	newfile := &ast.File{
		Name: &ast.Ident{Name: this.pkg.Name},
	}

	if len(finalImports) != 0 {
		for is := range finalImports {
			gdl := &ast.GenDecl{
				Tok:   token.IMPORT,
				Specs: []ast.Spec{is},
			}
			newfile.Decls = append(newfile.Decls, gdl)
		}
	}

	var sortedNodes NodeSorter

	for mn := range this.moveNodes {
		sortedNodes = append(sortedNodes, mn)
	}
	sort.Sort(sortedNodes)

	for _, mn := range sortedNodes {
		switch m := mn.(type) {
		case ast.Decl:
			newfile.Decls = append(newfile.Decls, m)
		case *ast.TypeSpec:
			gdl := &ast.GenDecl{
				Tok:   token.TYPE,
				Specs: []ast.Spec{m},
			}
			newfile.Decls = append(newfile.Decls, gdl)
		}
	}

	npf := ExprParentFinder{
		ExprParents: make(map[ast.Expr]ast.Node),
	}
	for n := range this.moveNodes {
		ast.Walk(&npf, n)
	}

	var pkgfiles []*ast.File
	for _, pkgfile := range this.pkg.Files {
		pkgfiles = append(pkgfiles, pkgfile)
	}
	oldPkgNewName := GetUniqueIdent(pkgfiles, this.pkg.Name)

	needOldImport := false

	this.referenceBack = false

	for expr, parent := range npf.ExprParents {
		obj, _ := types.ExprType(expr, LocalImporter)
		if _, ok := this.moveObjs[obj]; ok {
			continue
		}

		if _, ok := this.allObjs[obj]; !ok {
			continue
		}

		if !unicode.IsUpper(utf8.NewString(obj.Name).At(0)) {
			position := AllSources.Position(expr.Pos())
			fmt.Printf("At %v ", position)
			printer.Fprint(os.Stdout, token.NewFileSet(), expr)
			fmt.Println()
			err = MakeErr("Can't move code that references unexported objects")
			return
		}

		needOldImport = true
		this.referenceBack = true

		getSel := func(idn *ast.Ident) *ast.SelectorExpr {
			return &ast.SelectorExpr{
				X: &ast.Ident{
					Name:    oldPkgNewName,
					NamePos: idn.NamePos,
				},
				Sel: idn,
			}
		}

		switch p := parent.(type) {
		case *ast.CallExpr:
			if idn, ok := expr.(*ast.Ident); ok {
				p.Fun = getSel(idn)
			} else {
				err = MakeErr("CallExpr w/ unexpected type %T\n", expr)
				return
			}
		case *ast.AssignStmt:
			for i, x := range p.Lhs {
				if x == expr {
					if idn, ok := x.(*ast.Ident); ok {
						p.Lhs[i] = getSel(idn)
					}
				}
			}
			for i, x := range p.Rhs {
				if x == expr {
					if idn, ok := x.(*ast.Ident); ok {
						p.Rhs[i] = getSel(idn)
					}
				}
			}
		default:
			err = MakeErr("Unexpected parent %T\n", parent)
			return
		}
	}

	if needOldImport {
		is := &ast.ImportSpec{
			Name: &ast.Ident{Name: oldPkgNewName},
			Path: &ast.BasicLit{Value: QuotePath(this.oldpath)},
		}
		gdl := &ast.GenDecl{
			Tok:   token.IMPORT,
			Specs: []ast.Spec{is},
		}
		newfile.Decls = append([]ast.Decl{gdl}, newfile.Decls...)
	}

	err = os.MkdirAll(this.newpath, 0755)
	if err != nil {
		return
	}
	newSourcePath := filepath.Join(this.newpath, this.pkg.Name+".go")

	containedComments := make(CommentCollector)
	for node := range this.moveNodes {
		ast.Walk(containedComments, node)
	}

	for _, file := range this.pkg.Files {
		for i := len(file.Comments) - 1; i >= 0; i-- {
			cg := file.Comments[i]
			add := func() {
				newfile.Comments = append([]*ast.CommentGroup{cg}, newfile.Comments...)
				file.Comments[i] = file.Comments[len(file.Comments)-1]
				file.Comments = file.Comments[:len(file.Comments)-1]
			}
			if containedComments[cg] {
				add()
			} else {
				for node := range this.moveNodes {
					if node.Pos() <= cg.Pos() && node.End() >= cg.End() {
						add()
						break
					}
				}
			}
		}

	}
	err = NewSource(newSourcePath, newfile)
	if err != nil {
		return
	}

	return
}