Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix generation of docs on array members #447

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 11 additions & 2 deletions gen/cheader.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ typedef struct WGPUChainedStructOut {
* @{
*/

/**
/**
* \defgroup WGPUCallbackInfo Callback Info Structs
* \brief Callback info structures that are used in asynchronous functions.
*
Expand Down Expand Up @@ -323,7 +323,7 @@ typedef struct WGPU{{.Name | PascalCase}}CallbackInfo{{$.ExtSuffix}} {
})
{{ end}}{{"\n" -}}

/** @} */
/** @} */

{{- "\n"}}
{{- range $struct := .Structs}}
Expand All @@ -345,8 +345,17 @@ typedef struct WGPU{{.Name | PascalCase}}{{$.ExtSuffix}} {
WGPUChainedStructOut chain;
{{- end}}
{{- range $memberIndex, $_ := .Members}}
{{- if (.Type | IsArray)}}
/**
* Array count for `{{.Name | CamelCase}}`. The `INIT` macro sets this to 0.
*/
{{ StructMemberArrayCount $struct $memberIndex}}
{{- MCommentMember . 4 }}
{{ StructMemberArrayData $struct $memberIndex}}
{{- else}}
{{- MCommentMember . 4 }}
{{ StructMember $struct $memberIndex}}
{{- end}}
{{- end}}
} WGPU{{.Name | PascalCase}}{{$.ExtSuffix}} WGPU_STRUCTURE_ATTRIBUTE;

Expand Down
85 changes: 53 additions & 32 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ func (g *Generator) Gen(dst io.Writer) error {
"FunctionArgs": g.FunctionArgs,
"CallbackArgs": g.CallbackArgs,
"StructMember": g.StructMember,
"StructMemberArrayCount": g.StructMemberArrayCount,
"StructMemberArrayData": g.StructMemberArrayData,
"StructMemberInitializer": g.StructMemberInitializer,
})
t, err := t.Parse(tmpl)
Expand Down Expand Up @@ -286,12 +288,7 @@ func (g *Generator) CType(typ string, pointerType PointerType, suffix string) st
func (g *Generator) FunctionArgs(f Function, o *Object) string {
sb := &strings.Builder{}
if o != nil {
var typeSuffix string
if o.Namespace == "" {
typeSuffix = ConstantCase(g.ExtSuffix)
} else if o.Namespace != "webgpu" {
typeSuffix = ConstantCase(o.Namespace)
}
typeSuffix := g.TypeSuffixForNamespace(o.Namespace)
if len(f.Args) > 0 {
fmt.Fprintf(sb, "WGPU%s%s %s, ", PascalCase(o.Name), typeSuffix, CamelCase(o.Name))
} else {
Expand All @@ -302,12 +299,7 @@ func (g *Generator) FunctionArgs(f Function, o *Object) string {
if arg.Optional {
sb.WriteString("WGPU_NULLABLE ")
}
var typeSuffix string
if arg.Namespace == "" {
typeSuffix = ConstantCase(g.ExtSuffix)
} else if arg.Namespace != "webgpu" {
typeSuffix = ConstantCase(arg.Namespace)
}
typeSuffix := g.TypeSuffixForNamespace(arg.Namespace)
matches := arrayTypeRegexp.FindStringSubmatch(arg.Type)
if len(matches) == 2 {
fmt.Fprintf(sb, "size_t %sCount, ", CamelCase(Singularize(arg.Name)))
Expand All @@ -331,12 +323,7 @@ func (g *Generator) CallbackArgs(f Callback) string {
if arg.Optional {
sb.WriteString("WGPU_NULLABLE ")
}
var typeSuffix string
if arg.Namespace == "" {
typeSuffix = ConstantCase(g.ExtSuffix)
} else if arg.Namespace != "webgpu" {
typeSuffix = ConstantCase(arg.Namespace)
}
typeSuffix := g.TypeSuffixForNamespace(arg.Namespace)
var structPrefix string
if strings.HasPrefix(arg.Type, "struct.") {
structPrefix = "struct "
Expand Down Expand Up @@ -438,29 +425,63 @@ func (g *Generator) BitflagValue(b Bitflag, entryIndex int) (string, error) {
return entryValue, nil
}

func (g *Generator) TypeSuffixForNamespace(namespace string) string {
switch namespace {
case "":
return ConstantCase(g.ExtSuffix)
case "webgpu":
return ""
default:
return ConstantCase(namespace)
}
}

func (g *Generator) StructMember(s Struct, memberIndex int) (string, error) {
member := s.Members[memberIndex]
typeSuffix := g.TypeSuffixForNamespace(member.Namespace)

matches := arrayTypeRegexp.FindStringSubmatch(member.Type)
if len(matches) == 2 {
panic("StructMember used on array type")
}

sb := &strings.Builder{}
if member.Optional {
sb.WriteString("WGPU_NULLABLE ")
}
var typeSuffix string
if member.Namespace == "" {
typeSuffix = ConstantCase(g.ExtSuffix)
} else if member.Namespace != "webgpu" {
typeSuffix = ConstantCase(member.Namespace)
if strings.HasPrefix(member.Type, "callback.") {
fmt.Fprintf(sb, "%s %s;", g.CType(member.Type, "", "Info"), CamelCase(member.Name))
} else {
fmt.Fprintf(sb, "%s %s;", g.CType(member.Type, member.Pointer, typeSuffix), CamelCase(member.Name))
}
return sb.String(), nil
}

func (g *Generator) StructMemberArrayCount(s Struct, memberIndex int) (string, error) {
member := s.Members[memberIndex]

matches := arrayTypeRegexp.FindStringSubmatch(member.Type)
if len(matches) == 2 {
fmt.Fprintf(sb, "size_t %sCount;\n", CamelCase(Singularize(member.Name)))
fmt.Fprintf(sb, " %s %s;", g.CType(matches[1], member.Pointer, typeSuffix), CamelCase(member.Name))
} else {
if strings.HasPrefix(member.Type, "callback.") {
fmt.Fprintf(sb, "%s %s;", g.CType(member.Type, "", "Info"), CamelCase(member.Name))
} else {
fmt.Fprintf(sb, "%s %s;", g.CType(member.Type, member.Pointer, typeSuffix), CamelCase(member.Name))
}
if len(matches) != 2 {
panic("StructMemberArrayCount used on non-array")
}

return fmt.Sprintf("size_t %sCount;", CamelCase(Singularize(member.Name))), nil
}

func (g *Generator) StructMemberArrayData(s Struct, memberIndex int) (string, error) {
member := s.Members[memberIndex]
typeSuffix := g.TypeSuffixForNamespace(member.Namespace)

matches := arrayTypeRegexp.FindStringSubmatch(member.Type)
if len(matches) != 2 {
panic("StructMemberArrayCount used on non-array")
}

sb := &strings.Builder{}
if member.Optional {
sb.WriteString("WGPU_NULLABLE ")
}
fmt.Fprintf(sb, "%s %s;", g.CType(matches[1], member.Pointer, typeSuffix), CamelCase(member.Name))
return sb.String(), nil
}

Expand Down
Loading
Loading