forked from elliotchance/c2go
/
struct.go
55 lines (43 loc) · 1.25 KB
/
struct.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package program
import (
"fmt"
"github.com/elliotchance/c2go/ast"
)
// Struct represents the definition for a C struct.
type Struct struct {
// The name of the struct.
Name string
// True if the struct kind is an union.
// This field is used to avoid to dupplicate code for union case the type is the same.
// Plus, this field is used in collaboration with the method "c2go/program".*Program.GetStruct()
IsUnion bool
// Each of the fields and their C type. The field may be a string or an
// instance of Struct for nested structures.
Fields map[string]interface{}
}
// NewStruct creates a new Struct definition from an ast.RecordDecl.
func NewStruct(n *ast.RecordDecl) *Struct {
fields := make(map[string]interface{})
for _, field := range n.Children() {
switch f := field.(type) {
case *ast.FieldDecl:
fields[f.Name] = f.Type
case *ast.IndirectFieldDecl:
fields[f.Name] = f.Type
case *ast.RecordDecl:
fields[f.Name] = NewStruct(f)
case *ast.MaxFieldAlignmentAttr,
*ast.AlignedAttr,
*ast.TransparentUnionAttr,
*ast.FullComment:
// FIXME: Should these really be ignored?
default:
panic(fmt.Sprintf("cannot decode: %#v", f))
}
}
return &Struct{
Name: n.Name,
IsUnion: n.Kind == "union",
Fields: fields,
}
}