-
Notifications
You must be signed in to change notification settings - Fork 5
/
lookup.go
83 lines (68 loc) · 1.88 KB
/
lookup.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package gen
import "fmt"
// Lookup represents a in-memory lookup store.
type Lookup struct {
lookupEnum map[string]*Enum
lookupNonTypedefs map[string]string
lookupStruct map[string]*Struct
}
// NewLookup returns the initialized Lookup.
func NewLookup() Lookup {
return Lookup{
lookupEnum: map[string]*Enum{},
lookupNonTypedefs: map[string]string{},
lookupStruct: map[string]*Struct{
"cxstring": {
Name: "cxstring",
CName: "CXString",
},
},
}
}
// RegisterEnum registers e *Enum to Lookup.
func (l *Lookup) RegisterEnum(e *Enum) {
if _, ok := l.lookupEnum[e.Name]; !ok {
l.lookupEnum[e.Name] = e
l.lookupNonTypedefs[fmt.Sprintf("enum %s", e.CName)] = e.Name
l.lookupEnum[e.CName] = e
}
}
// HasEnum reports whether the n has Enum.
func (l *Lookup) HasEnum(n string) (*Enum, bool) {
e, ok := l.lookupEnum[n]
return e, ok
}
// RegisterStruct registers s *Struct to Lookup.
func (l *Lookup) RegisterStruct(s *Struct) {
if _, ok := l.lookupStruct[s.Name]; !ok {
l.lookupStruct[s.Name] = s
l.lookupNonTypedefs[fmt.Sprintf("struct %s", s.CName)] = s.Name
l.lookupStruct[s.CName] = s
}
}
// HasStruct reports whether the n has Struct.
func (l *Lookup) HasStruct(n string) (*Struct, bool) {
s, ok := l.lookupStruct[n]
return s, ok
}
// RemoveStruct removes s *Struct from Lookup.
func (l *Lookup) RemoveStruct(s *Struct) {
delete(l.lookupStruct, s.Name)
delete(l.lookupNonTypedefs, fmt.Sprintf("struct %s", s.CName))
delete(l.lookupStruct, s.CName)
}
// LookupNonTypedef lookups non typedef from Lookup.
func (l *Lookup) LookupNonTypedef(s string) (string, bool) {
n, ok := l.lookupNonTypedefs[s]
return n, ok
}
// IsEnumOrStruct reports whether the name is Enum or Struct.
func (l *Lookup) IsEnumOrStruct(name string) bool {
if _, ok := l.HasEnum(name); ok {
return true
}
if _, ok := l.HasStruct(name); ok {
return true
}
return false
}