Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(gnovm/gnofmt): Replace usage of ast.Object with ast.Ident In gnofmt #2546

Merged
merged 5 commits into from
Oct 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions gnovm/pkg/gnofmt/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ import (

const tabWidth = 8

type (
declMap map[*ast.Ident]ast.Decl
fileMap map[string]*ast.File
)

type parsedPackage struct {
error error
files map[string]*ast.File
decls map[*ast.Object]ast.Decl
files fileMap
decls declMap
}

type Processor struct {
Expand Down Expand Up @@ -52,7 +57,7 @@ func (p *Processor) FormatImportFromSource(filename string, src any) ([]byte, er
}

// Collect top level declarations within the source
pkgDecls := make(map[*ast.Object]ast.Decl)
pkgDecls := make(declMap)
collectTopDeclaration(nodefile, pkgDecls)

// Process and format the parsed node.
Expand Down Expand Up @@ -129,7 +134,7 @@ func (p *Processor) parseFile(path string, src any) (file *ast.File, err error)
}

// Helper function to process and format a parsed AST node.
func (p *Processor) processAndFormat(file *ast.File, filename string, topDecls map[*ast.Object]ast.Decl) ([]byte, error) {
func (p *Processor) processAndFormat(file *ast.File, filename string, topDecls declMap) ([]byte, error) {
// Collect unresolved
unresolved := collectUnresolved(file, topDecls)

Expand Down Expand Up @@ -167,8 +172,8 @@ func (p *Processor) processPackageFiles(path string, pkg Package) *parsedPackage
}

pkgc = &parsedPackage{
decls: make(map[*ast.Object]ast.Decl),
files: map[string]*ast.File{},
decls: make(declMap),
files: make(fileMap),
}
pkgc.error = ReadWalkPackage(pkg, func(filename string, r io.Reader, err error) error {
if err != nil {
Expand All @@ -190,31 +195,31 @@ func (p *Processor) processPackageFiles(path string, pkg Package) *parsedPackage
}

// collectTopDeclaration collects top-level declarations from a single file.
func collectTopDeclaration(file *ast.File, topDecls map[*ast.Object]ast.Decl) {
func collectTopDeclaration(file *ast.File, topDecls declMap) {
for _, decl := range file.Decls {
switch d := decl.(type) {
case *ast.GenDecl:
for _, spec := range d.Specs {
switch s := spec.(type) {
case *ast.TypeSpec:
topDecls[s.Name.Obj] = d
topDecls[s.Name] = d
case *ast.ValueSpec:
for _, name := range s.Names {
topDecls[name.Obj] = d
topDecls[name] = d
}
}
}
case *ast.FuncDecl:
// Check for top-level function
if d.Recv == nil && d.Name != nil && d.Name.Obj != nil {
topDecls[d.Name.Obj] = d
topDecls[d.Name] = d
}
}
}
}

// collectUnresolved collects unresolved identifiers and declarations.
func collectUnresolved(file *ast.File, topDecls map[*ast.Object]ast.Decl) map[string]map[string]bool {
func collectUnresolved(file *ast.File, topDecls declMap) map[string]map[string]bool {
unresolved := map[string]map[string]bool{}
unresolvedList := []*ast.Ident{}
for _, u := range file.Unresolved {
Expand All @@ -233,7 +238,7 @@ func collectUnresolved(file *ast.File, topDecls map[*ast.Object]ast.Decl) map[st
ast.Inspect(file, func(n ast.Node) bool {
switch e := n.(type) {
case *ast.Ident:
if d := topDecls[e.Obj]; d != nil {
if _, ok := topDecls[e]; ok {
delete(unresolved, e.Name)
}
case *ast.SelectorExpr:
Expand All @@ -260,7 +265,7 @@ func collectUnresolved(file *ast.File, topDecls map[*ast.Object]ast.Decl) map[st
}

// cleanupPreviousImports removes resolved imports from the unresolved list.
func (p *Processor) cleanupPreviousImports(node *ast.File, knownDecls map[*ast.Object]ast.Decl, unresolved map[string]map[string]bool) {
func (p *Processor) cleanupPreviousImports(node *ast.File, knownDecls declMap, unresolved map[string]map[string]bool) {
imports := astutil.Imports(p.fset, node)
for _, imps := range imports {
for _, imp := range imps {
Expand Down Expand Up @@ -290,8 +295,8 @@ func (p *Processor) cleanupPreviousImports(node *ast.File, knownDecls map[*ast.O
}

// Mark knownDecls as resolved
for obj := range knownDecls {
delete(unresolved, obj.Name)
for ident := range knownDecls {
delete(unresolved, ident.Name)
}
}

Expand Down
Loading