ast - Missing Ident.Obj

121 views
Skip to first unread message

Diego Medina

unread,
Apr 17, 2017, 9:45:23 AM4/17/17
to golang-nuts
Hi,

I'm using go/ast (and token/types. etc) to parse a Go project, it all works well when my program only has one package, but there are times where I have code like this:

file-a.go
=========
package a

...

selector := b.Selector(CompanyID)

...
========

and then

file-b.go
========
package b

....

func  Selector(n string) string{
...
}
========

Simple, in package a I call a function from package b, the issue is that when I'm walking the ast for pacakge a, I reach selector as an *ast.Ident and then I check 

the .Obj value and it is nil.

Is there a way to "fill in" this information?
For now, the information I'm looking for is the Decl value, 

 
I tried using golang.org/x/tools/go/loader to solve this but it has the same issue, it doesn't "fill in" the missing information, it does give me a different way to access the declaration, but it also does a lot more work that I don't need and makes my too run slower.

Hope I was clear.

Thanks

Diego


mhh...@gmail.com

unread,
Apr 17, 2017, 12:55:52 PM4/17/17
to golang-nuts
Can you clarify your expectation about ast ?

in my very humble understanding, Obj is for type checker more than ast.
I re call when i worked with it, i had to hide it when i wanted to visualize the tree
I even created a gist,
https://gist.github.com/mh-cbon/3ed5d9c39e9635cfed0f896000098133
to use in place of https://golang.org/pkg/go/ast/#example_Print

Diego Medina

unread,
Apr 17, 2017, 2:29:22 PM4/17/17
to golang-nuts
Sorry, I didn't include enough information, you are right, this is more about the type checker,

I have a call to

conf.Check(pkg.path, fset, astFiles, &info)

that does the typecheck gets the astFiles from the given package (a in this example) , but I didn't find a way to pass multiple packages to conf.Check

If I mix files from diff packages in astFiles, I get an error from the type checker saying something like, expected package a but found b for file xyz.go


conf is:

conf := &types.Config{
Error: func(e error) {
fmt.Println("failed to typecheck: ", e)
os.Exit(1)
},
Importer: importer.Default(),
}


Thanks

mhh...@gmail.com

unread,
Apr 17, 2017, 5:59:25 PM4/17/17
to golang-nuts
Hi,

no problem.

I think you are looking to load multiple package at once, something that d load import declaration as well,

https://godoc.org/golang.org/x/tools/go/loader#Config.FromArgs

https://godoc.org/golang.org/x/tools/go/loader#Config.Import

https://play.golang.org/p/TzbncKLd7b

I think we can help better if you can uncover,

- "expected package a but found b for file xyz.go"
- "For now, the information I'm looking for is the Decl value, "

Must be a simple difficulty to fix.

Diego Medina

unread,
Apr 17, 2017, 11:18:23 PM4/17/17
to golang-nuts
Thanks, I'll go ahead and prepare a small sample project that shows exactly what I'm trying to do and then I'll post back.

Regards,

Diego
Reply all
Reply to author
Forward
0 new messages