Skip to content

Commit

Permalink
gen_const: add ability to specify 'unsigned' enum tip
Browse files Browse the repository at this point in the history
  • Loading branch information
mpontillo authored and xlab committed Feb 26, 2024
1 parent 38432bd commit 850240e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
10 changes: 10 additions & 0 deletions generator/gen_const.go
Expand Up @@ -69,6 +69,11 @@ func (gen *Generator) expandEnumAnonymous(wr io.Writer, decl *tl.CDecl, namesSee
spec := decl.Spec.(*tl.CEnumSpec)
if hasType {
enumType := gen.tr.TranslateSpec(&spec.Type)
if tips, ok := gen.tr.TypeTipRx(tl.TipScopeEnum, string(typeName)); ok {
if tips.HasTip(tl.TipTypeUnsigned) {
enumType.Unsigned = true
}
}
fmt.Fprintf(wr, "// %s as declared in %s\n", typeName,
filepath.ToSlash(gen.tr.SrcLocation(tl.TargetConst, decl.Name, decl.Position)))
fmt.Fprintf(wr, "type %s %s\n", typeName, enumType)
Expand Down Expand Up @@ -132,6 +137,11 @@ func (gen *Generator) expandEnum(wr io.Writer, decl *tl.CDecl, namesSeen map[str
spec := decl.Spec.(*tl.CEnumSpec)
tagName := gen.tr.TransformName(tl.TargetType, decl.Spec.GetBase())
enumType := gen.tr.TranslateSpec(&spec.Type)
if tips, ok := gen.tr.TypeTipRx(tl.TipScopeEnum, string(tagName)); ok {
if tips.HasTip(tl.TipTypeUnsigned) {
enumType.Unsigned = true
}
}
fmt.Fprintf(wr, "// %s as declared in %s\n", tagName,
filepath.ToSlash(gen.tr.SrcLocation(tl.TargetConst, decl.Name, decl.Position)))
fmt.Fprintf(wr, "type %s %s\n", tagName, enumType)
Expand Down
24 changes: 14 additions & 10 deletions translator/rules.go
Expand Up @@ -86,15 +86,16 @@ const (
type Tip string

const (
TipPtrSRef Tip = "sref"
TipPtrRef Tip = "ref"
TipPtrArr Tip = "arr"
TipPtrInst Tip = "inst"
TipMemRaw Tip = "raw"
TipTypeNamed Tip = "named"
TipTypePlain Tip = "plain"
TipTypeString Tip = "string"
NoTip Tip = ""
TipPtrSRef Tip = "sref"
TipPtrRef Tip = "ref"
TipPtrArr Tip = "arr"
TipPtrInst Tip = "inst"
TipMemRaw Tip = "raw"
TipTypeNamed Tip = "named"
TipTypePlain Tip = "plain"
TipTypeString Tip = "string"
TipTypeUnsigned Tip = "unsigned"
NoTip Tip = ""
)

type TipKind string
Expand All @@ -110,7 +111,7 @@ func (t Tip) Kind() TipKind {
switch t {
case TipPtrArr, TipPtrRef, TipPtrSRef, TipPtrInst:
return TipKindPtr
case TipTypePlain, TipTypeNamed, TipTypeString:
case TipTypePlain, TipTypeNamed, TipTypeString, TipTypeUnsigned:
return TipKindType
case TipMemRaw:
return TipKindMem
Expand All @@ -127,6 +128,8 @@ func (t Tip) IsValid() bool {
return true
case TipMemRaw:
return true
case TipTypeUnsigned:
return true
default:
return false
}
Expand All @@ -145,6 +148,7 @@ const (
TipScopeAny TipScope = "any"
TipScopeStruct TipScope = "struct"
TipScopeType TipScope = "type"
TipScopeEnum TipScope = "enum"
TipScopeFunction TipScope = "function"
)

Expand Down

0 comments on commit 850240e

Please sign in to comment.