Skip to content

Commit 850240e

Browse files
mpontilloxlab
authored andcommitted
gen_const: add ability to specify 'unsigned' enum tip
1 parent 38432bd commit 850240e

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

generator/gen_const.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ func (gen *Generator) expandEnumAnonymous(wr io.Writer, decl *tl.CDecl, namesSee
6969
spec := decl.Spec.(*tl.CEnumSpec)
7070
if hasType {
7171
enumType := gen.tr.TranslateSpec(&spec.Type)
72+
if tips, ok := gen.tr.TypeTipRx(tl.TipScopeEnum, string(typeName)); ok {
73+
if tips.HasTip(tl.TipTypeUnsigned) {
74+
enumType.Unsigned = true
75+
}
76+
}
7277
fmt.Fprintf(wr, "// %s as declared in %s\n", typeName,
7378
filepath.ToSlash(gen.tr.SrcLocation(tl.TargetConst, decl.Name, decl.Position)))
7479
fmt.Fprintf(wr, "type %s %s\n", typeName, enumType)
@@ -132,6 +137,11 @@ func (gen *Generator) expandEnum(wr io.Writer, decl *tl.CDecl, namesSeen map[str
132137
spec := decl.Spec.(*tl.CEnumSpec)
133138
tagName := gen.tr.TransformName(tl.TargetType, decl.Spec.GetBase())
134139
enumType := gen.tr.TranslateSpec(&spec.Type)
140+
if tips, ok := gen.tr.TypeTipRx(tl.TipScopeEnum, string(tagName)); ok {
141+
if tips.HasTip(tl.TipTypeUnsigned) {
142+
enumType.Unsigned = true
143+
}
144+
}
135145
fmt.Fprintf(wr, "// %s as declared in %s\n", tagName,
136146
filepath.ToSlash(gen.tr.SrcLocation(tl.TargetConst, decl.Name, decl.Position)))
137147
fmt.Fprintf(wr, "type %s %s\n", tagName, enumType)

translator/rules.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,16 @@ const (
8686
type Tip string
8787

8888
const (
89-
TipPtrSRef Tip = "sref"
90-
TipPtrRef Tip = "ref"
91-
TipPtrArr Tip = "arr"
92-
TipPtrInst Tip = "inst"
93-
TipMemRaw Tip = "raw"
94-
TipTypeNamed Tip = "named"
95-
TipTypePlain Tip = "plain"
96-
TipTypeString Tip = "string"
97-
NoTip Tip = ""
89+
TipPtrSRef Tip = "sref"
90+
TipPtrRef Tip = "ref"
91+
TipPtrArr Tip = "arr"
92+
TipPtrInst Tip = "inst"
93+
TipMemRaw Tip = "raw"
94+
TipTypeNamed Tip = "named"
95+
TipTypePlain Tip = "plain"
96+
TipTypeString Tip = "string"
97+
TipTypeUnsigned Tip = "unsigned"
98+
NoTip Tip = ""
9899
)
99100

100101
type TipKind string
@@ -110,7 +111,7 @@ func (t Tip) Kind() TipKind {
110111
switch t {
111112
case TipPtrArr, TipPtrRef, TipPtrSRef, TipPtrInst:
112113
return TipKindPtr
113-
case TipTypePlain, TipTypeNamed, TipTypeString:
114+
case TipTypePlain, TipTypeNamed, TipTypeString, TipTypeUnsigned:
114115
return TipKindType
115116
case TipMemRaw:
116117
return TipKindMem
@@ -127,6 +128,8 @@ func (t Tip) IsValid() bool {
127128
return true
128129
case TipMemRaw:
129130
return true
131+
case TipTypeUnsigned:
132+
return true
130133
default:
131134
return false
132135
}
@@ -145,6 +148,7 @@ const (
145148
TipScopeAny TipScope = "any"
146149
TipScopeStruct TipScope = "struct"
147150
TipScopeType TipScope = "type"
151+
TipScopeEnum TipScope = "enum"
148152
TipScopeFunction TipScope = "function"
149153
)
150154

0 commit comments

Comments
 (0)