Mark descriptor/options as optional for requestDevice/Adapter

These were not marked as optional previously because it would mean that
C++ methods would look like void F(a = null, b, c) which is invalid in C++
because default arguments must be contiguous and at the end of the
signature.

This commit adds a special case when optional=true where no_default=true
still marks the argument as optional, but doesn't produce a C++ default
argument.

Fixed: dawn:1502
Change-Id: I1b648d37edb94b8412109fc0a06a91bbbd31b8c4
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/97002
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
This commit is contained in:
Corentin Wallez 2022-07-26 17:36:44 +00:00 committed by Dawn LUCI CQ
parent d722e619b1
commit 1260a53018
3 changed files with 6 additions and 5 deletions

View File

@ -108,7 +108,7 @@
{
"name": "request device",
"args": [
{"name": "descriptor", "type": "device descriptor", "annotation": "const*"},
{"name": "descriptor", "type": "device descriptor", "annotation": "const*", "optional": true, "no_default": true},
{"name": "callback", "type": "request device callback"},
{"name": "userdata", "type": "void", "annotation": "*"}
]
@ -118,7 +118,7 @@
"tags": ["dawn"],
"returns": "device",
"args": [
{"name": "descriptor", "type": "device descriptor", "annotation": "const*", "optional": "true"}
{"name": "descriptor", "type": "device descriptor", "annotation": "const*", "optional": true}
]
}
]
@ -1440,7 +1440,7 @@
{
"name": "request adapter",
"args": [
{"name": "options", "type": "request adapter options", "annotation": "const*"},
{"name": "options", "type": "request adapter options", "annotation": "const*", "optional": true, "no_default": true},
{"name": "callback", "type": "request adapter callback"},
{"name": "userdata", "type": "void", "annotation": "*"}
]

View File

@ -42,7 +42,7 @@ A **record** is a list of **record members**, each of which is a dictionary with
- `"type"` a string, the name of the base type for this member
- `"annotation"` a string, default to "value". Define the C annotation to apply to the base type. Allowed annotations are `"value"` (the default), `"*"`, `"const*"`
- `"length"` (default to 1 if not set), a string. Defines length of the array pointed to for pointer arguments. If not set the length is implicitly 1 (so not an array), but otherwise it can be set to the name of another member in the same record that will contain the length of the array (this is heavily used in the `fooCount` `foos` pattern in the API). As a special case `"strlen"` can be used for `const char*` record members to denote that the length should be determined with `strlen`.
- `"optional"` (default to false) a boolean that says whether this member is optional. Member records can be optional if they are pointers (otherwise dawn_wire will always try to dereference them), objects (otherwise dawn_wire will always try to encode their ID and crash), or if they have a `"default"` key. Optional pointers and objects will always default to `nullptr`.
- `"optional"` (default to false) a boolean that says whether this member is optional. Member records can be optional if they are pointers (otherwise dawn_wire will always try to dereference them), objects (otherwise dawn_wire will always try to encode their ID and crash), or if they have a `"default"` key. Optional pointers and objects will always default to `nullptr` (unless `"no_default"` is set to `true`).
- `"default"` (optional) a number or string. If set the record member will use that value as default value. Depending on the member's category it can be a number, a string containing a number, or the name of an enum/bitmask value.
- `"wire_is_data_only"` (default to false) a boolean that says whether it is safe to directly return a pointer of this member that is pointing to a piece of memory in the transfer buffer into dawn_wire. To prevent TOCTOU attacks, by default in dawn_wire we must ensure every single value returned to dawn_native a copy of what's in the wire, so `"wire_is_data_only"` is set to true only when the member is data-only and don't impact control flow.

View File

@ -152,7 +152,8 @@ namespace {{metadata.namespace}} {
};
{% macro render_cpp_default_value(member, is_struct=True) -%}
{%- if member.annotation in ["*", "const*"] and member.optional or member.default_value == "nullptr" -%}
{%- if member.json_data.get("no_default", false) -%}
{%- elif member.annotation in ["*", "const*"] and member.optional or member.default_value == "nullptr" -%}
{{" "}}= nullptr
{%- elif member.type.category == "object" and member.optional and is_struct -%}
{{" "}}= nullptr