This patch enables cgo utility to correctly convert enums in the C source

into consts in the resulting Go source.  Previously known as issue 161047,
which I deleted accidentally.  Fixes issue 207.

R=rsc
https://golang.org/cl/166059
This commit is contained in:
Moriyoshi Koizumi 2009-12-15 21:24:17 -08:00 committed by Russ Cox
parent 52114724b7
commit a8fbf5dc2c
6 changed files with 77 additions and 29 deletions

View file

@ -71,6 +71,7 @@ func main() {
p.loadDebugInfo()
p.Vardef = make(map[string]*Type)
p.Funcdef = make(map[string]*FuncType)
p.Enumdef = make(map[string]int64)
for _, cref := range p.Crefs {
switch cref.Context {
@ -86,6 +87,14 @@ func main() {
if cref.TypeName {
error((*cref.Expr).Pos(), "type C.%s used as expression", cref.Name)
}
// If the expression refers to an enumerated value, then
// place the identifier for the value and add it to Enumdef so
// it will be declared as a constant in the later stage.
if cref.Type.EnumValues != nil {
*cref.Expr = &ast.Ident{Value: cref.Name}
p.Enumdef[cref.Name] = cref.Type.EnumValues[cref.Name]
break
}
// Reference to C variable.
// We declare a pointer and arrange to have it filled in.
*cref.Expr = &ast.StarExpr{X: &ast.Ident{Value: "_C_" + cref.Name}}