Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions gen/cheader.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,15 @@ struct {{CType .Base ""}}CallbackInfo;
{{- range $entryIndex, $_ := .Entries}}
{{- if .}}
{{- MCommentEnumValue .Doc 0 $enum $entryIndex }}
_wgpu_EXTEND_ENUM({{CType $enum.Base ""}}, {{CEnumName $enum.Base .Base}}, {{EnumValue32 $enum $entryIndex | printf "0x%.8X"}});
_wgpu_EXTEND_ENUM({{CType $enum.Base ""}}, {{CEnumValueName $enum.Base .Base}}, {{EnumValue32 $enum $entryIndex | printf "0x%.8X"}});
{{- end}}
{{- end}}
{{- else}}
typedef enum {{CType .Base ""}} {
{{- range $entryIndex, $_ := .Entries}}
{{- if .}}
{{- MCommentEnumValue .Doc 4 $enum $entryIndex }}
{{CEnumName $enum.Base .Base}} = {{EnumValue32 $enum $entryIndex | printf "0x%.8X"}},
{{CEnumValueName $enum.Base .Base}} = {{EnumValue32 $enum $entryIndex | printf "0x%.8X"}},
{{- end}}
{{- end}}
{{CType $enum.Base ""}}_Force32 = 0x7FFFFFFF
Expand All @@ -230,7 +230,7 @@ typedef WGPUFlags {{CType .Base ""}};
{{- end}}
{{- range $entryIndex, $_ := .Entries}}
{{- MCommentBitflagValue .Doc 0 $bitflag $entryIndex }}
static const {{CType $bitflag.Base ""}} {{CEnumName $bitflag.Base .Base}} = {{BitflagValue $bitflag $entryIndex}};
static const {{CType $bitflag.Base ""}} {{CEnumValueName $bitflag.Base .Base}} = {{BitflagValue $bitflag $entryIndex}};
{{- end}}
{{- end}}

Expand Down
139 changes: 84 additions & 55 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
)

type Generator struct {
ExtPrefix string
HeaderName string
UseExtPrefix bool
HeaderName string
*Yml
}

Expand Down Expand Up @@ -194,7 +194,7 @@ func (g *Generator) Gen(dst io.Writer) error {
"CamelCase": CamelCase,
"ConstantCaseName": g.ConstantCaseName,
"PascalCaseName": g.PascalCaseName,
"CEnumName": g.CEnumName,
"CEnumValueName": g.CEnumValueName,
"CMethodName": g.CMethodName,
"CType": g.CType,
"CValue": g.CValue,
Expand Down Expand Up @@ -243,86 +243,105 @@ func (g *Generator) FindBaseType(typ string) Base {
case "constant":
idx := slices.IndexFunc(g.Constants, func(c Constant) bool { return c.Name == name })
if idx == -1 {
return Base{Name: name, Namespace: g.PrefixForNamespace("")}
return Base{Name: name, Namespace: "webgpu"}
}
return g.Constants[idx].Base
case "typedef":
idx := slices.IndexFunc(g.Typedefs, func(t Typedef) bool { return t.Name == name })
if idx == -1 {
return Base{Name: name, Namespace: g.PrefixForNamespace("")}
return Base{Name: name, Namespace: "webgpu"}
}
return g.Typedefs[idx].Base
case "enum":
idx := slices.IndexFunc(g.Enums, func(e Enum) bool { return e.Name == name })
if idx == -1 {
return Base{Name: name, Namespace: g.PrefixForNamespace("")}
return Base{Name: name, Namespace: "webgpu"}
}
return g.Enums[idx].Base
case "bitflag":
idx := slices.IndexFunc(g.Bitflags, func(b Bitflag) bool { return b.Name == name })
if idx == -1 {
return Base{Name: name, Namespace: g.PrefixForNamespace("")}
return Base{Name: name, Namespace: "webgpu"}
}
return g.Bitflags[idx].Base
case "struct":
idx := slices.IndexFunc(g.Structs, func(s Struct) bool { return s.Name == name })
if idx == -1 {
return Base{Name: name, Namespace: g.PrefixForNamespace("")}
return Base{Name: name, Namespace: "webgpu"}
}
return g.Structs[idx].Base
case "callback":
idx := slices.IndexFunc(g.Callbacks, func(c Callback) bool { return c.Name == name })
if idx == -1 {
return Base{Name: name, Namespace: g.PrefixForNamespace("")}
return Base{Name: name, Namespace: "webgpu"}
}
return g.Callbacks[idx].Base
case "object":
idx := slices.IndexFunc(g.Objects, func(o Object) bool { return o.Name == name })
if idx == -1 {
return Base{Name: name, Namespace: g.PrefixForNamespace("")}
return Base{Name: name, Namespace: "webgpu"}
}
return g.Objects[idx].Base
default:
panic("Unable to find unknown category type: " + category + " for identifier: " + typ)
}
}

func (g *Generator) ConstantCaseName(b Base) string {
if b.Extended {
return ConstantCase(b.Name)
}

prefix := g.PrefixForNamespace(b.Namespace)
switch prefix {
case "":
return ConstantCase(b.Name)
default:
return ConstantCase(prefix + "_" + b.Name)
func (g *Generator) ResolveNamespaceForTopLevel(b Base) string {
Comment thread
kainino0x marked this conversation as resolved.
Outdated
if b.Namespace != "" {
return b.Namespace
} else if b.Extended {
// If we're extending an enum, assume it's in the core namespace if not otherwise specified
return "webgpu"
} else {
return g.Name
}
}

func (g *Generator) PascalCaseName(b Base) string {
if b.Extended {
return PascalCase(b.Name)
func (g *Generator) ResolveNamespaceForSecondLevel(typ Base, entry Base) string {
if entry.Namespace != "" {
return entry.Namespace
} else if typ.Namespace != "" {
return typ.Namespace
} else {
return g.Name
}
}

prefix := g.PrefixForNamespace(b.Namespace)
func (g *Generator) CanonicalCaseName(prefix string, b Base) string {
switch prefix {
case "":
return PascalCase(b.Name)
return b.Name
default:
return PascalCase(prefix + "_" + b.Name)
return prefix + "_" + b.Name
}
}

func (g *Generator) CEnumName(typ Base, entry Base) string {
if !typ.Extended {
return g.CType(typ, "") + "_" + PascalCase(entry.Name)
func (g *Generator) ConstantCaseName(b Base) string {
prefix := g.PrefixForNamespace(g.ResolveNamespaceForTopLevel(b))
return ConstantCase(g.CanonicalCaseName(prefix, b))
}

func (g *Generator) PascalCaseName(b Base) string {
prefix := g.PrefixForNamespace(g.ResolveNamespaceForTopLevel(b))
return PascalCase(g.CanonicalCaseName(prefix, b))
}

func (g *Generator) PrefixForNamespaceAtSecondLevel(typ Base, entry Base) string {
outerNamespace := g.ResolveNamespaceForTopLevel(typ)
innerNamespace := g.ResolveNamespaceForSecondLevel(typ, entry)
if outerNamespace == innerNamespace {
return ""
} else {
return g.CType(typ, "") + "_" + g.PascalCaseName(entry)
return g.PrefixForNamespace(innerNamespace)
}
}

func (g *Generator) CEnumValueName(typ Base, entry Base) string {
entryPrefix := g.PrefixForNamespaceAtSecondLevel(typ, entry)
return g.CType(typ, "") + "_" + PascalCase(g.CanonicalCaseName(entryPrefix, entry))
}

func (g *Generator) CMethodName(o Object, m Function) string {
if !o.Extended {
return g.PascalCaseName(o.Base) + PascalCase(m.Name)
Expand Down Expand Up @@ -488,34 +507,38 @@ func (g *Generator) CallbackArgs(f Callback) string {
return sb.String()
}

func (g *Generator) EnumValue16(e Enum, entryIndex int) (uint16, error) {
func (g *Generator) EnumValue32(e Enum, entryIndex int) (uint32, error) {
entry := e.Entries[entryIndex]
if entry.Value == "" {
return uint16(entryIndex), nil
} else {
var num string
var base int
if strings.HasPrefix(entry.Value, "0x") {
base = 16
num = strings.TrimPrefix(entry.Value, "0x")
} else {
base = 10
num = entry.Value

var enum_prefix uint16
if entry.Namespace != "" {
if g.EnumPrefix != 0 {
return 0, fmt.Errorf("EnumValue32: entry %s with overridden namespace %s can only be used in core webgpu.h (with global enum_prefix 0)", entry.Name, entry.Namespace)
}
value, err := strconv.ParseUint(num, base, 16)
if err != nil {
return 0, err
if entry.Value == nil {
return 0, fmt.Errorf("EnumValue32: entry %s with overridden namespace %s must have an explicit value", entry.Name, entry.Namespace)
}
switch entry.Namespace {
case "compatibility_mode":
enum_prefix = 0x2000
default:
return 0, fmt.Errorf("EnumValue32: unknown namespace %s", entry.Namespace)
}
return uint16(value), nil
} else {
enum_prefix = g.EnumPrefix
}
}

func (g *Generator) EnumValue32(e Enum, entryIndex int) (uint32, error) {
value16, err := g.EnumValue16(e, entryIndex)
if err != nil {
return 0, err
var value16 uint16
if entry.Value == nil {
value16 = uint16(entryIndex)
if int(value16) != entryIndex {
return 0, fmt.Errorf("EnumValue32: entry %s default value (entry index %d) is too large", entry.Name, entryIndex)
}
} else {
value16 = *entry.Value
}
return uint32(g.EnumPrefix)<<16 | uint32(value16), nil

return uint32(enum_prefix)<<16 | uint32(value16), nil
}

func bitflagEntryValue(entry BitflagEntry, entryIndex int) (uint64, error) {
Expand Down Expand Up @@ -590,11 +613,17 @@ func (g *Generator) BitflagValue(b Bitflag, entryIndex int, isDocString bool) (s
func (g *Generator) PrefixForNamespace(namespace string) string {
switch namespace {
case "":
return g.ExtPrefix
panic("Missing namespace")
case "webgpu":
return ""
case "compatibility_mode":
return ""
default:
return namespace
if g.UseExtPrefix {
return namespace
} else {
return ""
}
}
}

Expand Down
10 changes: 3 additions & 7 deletions gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,10 @@ func main() {

SortAndTransform(&yml)

prefix := ""
if yml.Name != "webgpu" && extPrefix {
prefix = yml.Name
}
g := &Generator{
Yml: &yml,
HeaderName: outHeaderFileNameSplit[0],
ExtPrefix: prefix,
Yml: &yml,
HeaderName: outHeaderFileNameSplit[0],
UseExtPrefix: extPrefix,
}
if err := g.Gen(dst); err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion gen/yml.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Enum struct {
}
type EnumEntry struct {
Base `yaml:",inline"`
Value string `yaml:"value"`
Value *uint16 `yaml:"value"`
}

type Bitflag struct {
Expand Down
Loading