{"action":"created","issue":{"url":"https://api.github.com/repos/golang/go/issues/16623","repository_url":"https://api.github.com/repos/golang/go","labels_url":"https://api.github.com/repos/golang/go/issues/16623/labels{/name}","comments_url":"https://api.github.com/repos/golang/go/issues/16623/comments","events_url":"https://api.github.com/repos/golang/go/issues/16623/events","html_url":"https://github.com/golang/go/issues/16623","id":169700944,"number":16623,"title":"proposal: refactor cmd/cgo into an internal helper package","user":{"login":"mdempsky","id":38349,"avatar_url":"https://avatars.githubusercontent.com/u/38349?v=3","gravatar_id":"","url":"https://api.github.com/users/mdempsky","html_url":"https://github.com/mdempsky","followers_url":"https://api.github.com/users/mdempsky/followers","following_url":"https://api.github.com/users/mdempsky/following{/other_user}","gists_url":"https://api.github.com/users/mdempsky/gists{/gist_id}","starred_url":"https://api.github.com/users/mdempsky/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mdempsky/subscriptions","organizations_url":"https://api.github.com/users/mdempsky/orgs","repos_url":"https://api.github.com/users/mdempsky/repos","events_url":"https://api.github.com/users/mdempsky/events{/privacy}","received_events_url":"https://api.github.com/users/mdempsky/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/golang/go/labels/Proposal","name":"Proposal","color":"ededed"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/golang/go/milestones/30","html_url":"https://github.com/golang/go/milestones/Proposal","labels_url":"https://api.github.com/repos/golang/go/milestones/30/labels","id":1373555,"number":30,"title":"Proposal","description":"Proposals that are pending (not yet accepted/rejected).\r\n","creator":{"login":"rsc","id":104030,"avatar_url":"https://avatars.githubusercontent.com/u/104030?v=3","gravatar_id":"","url":"https://api.github.com/users/rsc","html_url":"https://github.com/rsc","followers_url":"https://api.github.com/users/rsc/followers","following_url":"https://api.github.com/users/rsc/following{/other_user}","gists_url":"https://api.github.com/users/rsc/gists{/gist_id}","starred_url":"https://api.github.com/users/rsc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rsc/subscriptions","organizations_url":"https://api.github.com/users/rsc/orgs","repos_url":"https://api.github.com/users/rsc/repos","events_url":"https://api.github.com/users/rsc/events{/privacy}","received_events_url":"https://api.github.com/users/rsc/received_events","type":"User","site_admin":false},"open_issues":54,"closed_issues":55,"state":"open","created_at":"2015-10-24T01:40:49Z","updated_at":"2016-09-22T10:16:46Z","due_on":null,"closed_at":null},"comments":8,"created_at":"2016-08-05T21:34:33Z","updated_at":"2016-09-26T23:15:51Z","closed_at":null,"body":"Currently cgo support is implemented with cmd/cgo as a separate tool that analyzes cgo-using Go code and transforms it into standard Go code.  This proposal is to extract the analysis logic into a separate internal package that can be reused by cmd/compile and go/types directly without needing source rewrites.  I.e., turn them into native cgo compilers, rather than needing a separate \"cgofront\" preprocessor step.\r\n\r\nThe expected benefits are:\r\n\r\n  1. Better error messages.  E.g., currently cmd/compile emits error messages about calls to _Cfunc_foo instead of C.foo.\r\n  2. Better position information.  Currently when using golang.org/x/tools/go/loader to analyze cgo-using source, the column location and byte offset information is wrong because it applies go/types to the post-transformation Go source files.  Thanks to //line directives, we can at least identify the original line number, but it's troublesome/imprecise to write type-sensitive refactoring tools that rely on column information and want to support cgo-using packages.\r\n  3. Better desugaring of cgo calls.  Currently cmd/cgo needs to rewrite expressions into valid Go code to be accepted by cmd/compile, which is sometimes subtle (e.g., #7757, #9557, #13635, #13930, #16591).  By desugaring within the compiler itself, we have more flexibility about the sorts of fundamental operations we can use.\r\n\r\nPotential downsides and counter-arguments:\r\n\r\n  1. cmd/cgo still needs to exist for gccgo, but cmd/cgo already has substantially different behavior for supporting cmd/compile vs gccgo.\r\n  2. Might complicate #15681 (scheduling cmd/cgo earlier), but I expect an alternative strategy is equally viable: schedule the C compilations later.  Currently the only reason we need to schedule the C compilations before the Go compiler is to produce _cgo_imports.go.  But this file only contains //go:cgo_import_dynamic and //go:cgo_dynamic_linker directives, which don't actually affect Go compilation; cmd/compile just writes them back out again so cmd/link can ultimately handle them.\r\n  3. Increases coupling between cmd/compile and cgo, but cmd/compile already is / needs to be cgo-aware, and cmd/go already has extensive special case code for cmd/cgo (unlike any other tool that generates Go source).\r\n\r\nAlternative ideas that might achieve similar benefits while generalizing to tools other than cmd/cgo:\r\n\r\n  1. Add a //go:errorname directive so cmd/cgo can express that _Cfunc_foo should be referred to as \"C.foo\" in error messages.  It's not obvious this would generalize fully though; e.g., cmd/yacc rewrites $1 to yyDollar[1], which would need something more powerful to express.\r\n  2. Generalize //line directives to something that records column/offset information too (similar to JavaScript Source Maps).  Again, I'm not sure how useful in practice this would actually be for tools other than cmd/cgo.\r\n\r\n/cc @ianlancetaylor @alandonovan @griesemer "},"comment":{"url":"https://api.github.com/repos/golang/go/issues/comments/249724900","html_url":"https://github.com/golang/go/issues/16623#issuecomment-249724900","issue_url":"https://api.github.com/repos/golang/go/issues/16623","id":249724900,"user":{"login":"adg","id":8446613,"avatar_url":"https://avatars.githubusercontent.com/u/8446613?v=3","gravatar_id":"","url":"https://api.github.com/users/adg","html_url":"https://github.com/adg","followers_url":"https://api.github.com/users/adg/followers","following_url":"https://api.github.com/users/adg/following{/other_user}","gists_url":"https://api.github.com/users/adg/gists{/gist_id}","starred_url":"https://api.github.com/users/adg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adg/subscriptions","organizations_url":"https://api.github.com/users/adg/orgs","repos_url":"https://api.github.com/users/adg/repos","events_url":"https://api.github.com/users/adg/events{/privacy}","received_events_url":"https://api.github.com/users/adg/received_events","type":"User","site_admin":false},"created_at":"2016-09-26T23:15:51Z","updated_at":"2016-09-26T23:15:51Z","body":"Anyone have more comments for @mdempsky?"}}
{"actor":{"display_login":"adg"}}