1{{/* 2 Copyright 2021 The Dawn Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15*/}} 16 17{{- /* 18-------------------------------------------------------------------------------- 19Template file for use with src/dawn_node/tools/cmd/idlgen/main.go. 20This file provides common template definitions and is included by WebGPU.h.tmpl 21and WebGPU.cpp.tmpl. 22 23See: 24* https://github.com/ben-clayton/webidlparser/blob/main/ast/ast.go for the AST 25 types used by this template 26* src/dawn_node/tools/cmd/idlgen/main.go for additional structures and functions 27 used by this template 28* https://golang.org/pkg/text/template/ for documentation on the template syntax 29-------------------------------------------------------------------------------- 30*/ -}} 31 32 33{{- /* 34-------------------------------------------------------------------------------- 35-- Type generates the C++ type for the given ast.Type 36-------------------------------------------------------------------------------- 37*/ -}} 38{{- define "Type" -}} 39{{- if IsUndefinedType $}}void 40{{- else if IsTypeName $}} 41{{- if eq $.Name "boolean" }}bool 42{{- else if eq $.Name "long" }}int32_t 43{{- else if eq $.Name "unsigned long" }}uint32_t 44{{- else if eq $.Name "long long" }}int64_t 45{{- else if eq $.Name "unsigned long long" }}uint64_t 46{{- else if eq $.Name "object" }}Object 47{{- else if eq $.Name "DOMString" }}std::string 48{{- else if eq $.Name "USVString" }}std::string 49{{- else if eq $.Name "ArrayBuffer" }}ArrayBuffer 50{{- else if IsInterface (Lookup $.Name) }}Interface<{{$.Name}}> 51{{- else }}{{$.Name}} 52{{- end }} 53{{- else if IsParametrizedType $}}{{$.Name}}<{{template "TypeList" $.Elems}}> 54{{- else if IsNullableType $}}std::optional<{{template "Type" $.Type}}> 55{{- else if IsUnionType $}}std::variant<{{template "VariantTypeList" $.Types}}> 56{{- else if IsSequenceType $}}std::vector<{{template "Type" $.Elem}}> 57{{- else if IsRecordType $}}std::unordered_map<{{template "Type" $.Key}}, {{template "Type" $.Elem}}> 58{{- else }} /* Unhandled Type {{printf "%T" $}} */ 59{{- end -}} 60{{- end }} 61 62 63{{- /* 64-------------------------------------------------------------------------------- 65-- AttributeType generates the C++ type for the given ast.Member 66-------------------------------------------------------------------------------- 67*/ -}} 68{{- define "AttributeType" -}} 69{{- if $.Required }}{{template "Type" $.Type}} 70{{- else if $.Init }}{{template "Type" $.Type}} 71{{- else }}std::optional<{{template "Type" $.Type}}> 72{{- end}} 73{{- end }} 74 75 76{{- /* 77-------------------------------------------------------------------------------- 78-- Literal generates a C++ literal value using the following arguments: 79-- Value - the ast.Literal 80-- Type - the ast.Type of the literal 81-------------------------------------------------------------------------------- 82*/ -}} 83{{- define "Literal" -}} 84{{- if IsDefaultDictionaryLiteral $.Value}}{{template "Type" $.Type}}{} 85{{- else if IsTypeName $.Type }} 86{{- $ty := Lookup $.Type.Name}} 87{{- if IsEnum $ty }}{{$.Type.Name}}::{{EnumEntryName $.Value.Value}} 88{{- else if IsBasicLiteral $.Value }}{{$.Value.Value}} 89{{- else }}/* Unhandled Type {{printf "ty: %v $.Type.Name: %T $.Value: %T" $ty $.Type.Name $.Value}} */ 90{{- end }} 91{{- else if IsSequenceType $.Type }}{{template "Type" $.Type}}{} {{- /* TODO: Assumes the initialiser is empty */}} 92{{- else if IsBasicLiteral $.Value }}{{$.Value.Value}} 93{{- else }} /* Unhandled Type {{printf "%T %T" $.Type $.Value}} */ 94{{- end}} 95{{- end }} 96 97 98{{- /* 99-------------------------------------------------------------------------------- 100-- TypeList generates a C++ comma separated list of types from the given 101-- []ast.Type 102-------------------------------------------------------------------------------- 103*/ -}} 104{{- define "TypeList" -}} 105{{- range $i, $ty := $}} 106{{- if $i }}, {{end}} 107{{- template "Type" $ty}} 108{{- end}} 109{{- end }} 110 111 112{{- /* 113-------------------------------------------------------------------------------- 114-- VariantTypeList generates a C++ comma separated list of types from the given 115-- []ast.Type, skipping any 'undefined' types 116-------------------------------------------------------------------------------- 117*/ -}} 118{{- define "VariantTypeList" -}} 119{{- range $i, $ty := $}} 120{{- if not (IsUndefinedType $ty)}} 121{{- if $i }}, {{end}} 122{{- template "Type" $ty}} 123{{- end}} 124{{- end}} 125{{- end }} 126 127