From 850240e7600120df48027167e8338b81f4433ce3 Mon Sep 17 00:00:00 2001 From: Mike Pontillo Date: Mon, 26 Feb 2024 10:53:48 -0800 Subject: [PATCH] gen_const: add ability to specify 'unsigned' enum tip --- generator/gen_const.go | 10 ++++++++++ translator/rules.go | 24 ++++++++++++++---------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/generator/gen_const.go b/generator/gen_const.go index 996a7bc..ff1bd7f 100644 --- a/generator/gen_const.go +++ b/generator/gen_const.go @@ -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) @@ -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) diff --git a/translator/rules.go b/translator/rules.go index 41324d3..ad2edad 100644 --- a/translator/rules.go +++ b/translator/rules.go @@ -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 @@ -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 @@ -127,6 +128,8 @@ func (t Tip) IsValid() bool { return true case TipMemRaw: return true + case TipTypeUnsigned: + return true default: return false } @@ -145,6 +148,7 @@ const ( TipScopeAny TipScope = "any" TipScopeStruct TipScope = "struct" TipScopeType TipScope = "type" + TipScopeEnum TipScope = "enum" TipScopeFunction TipScope = "function" )