Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(756)

Unified Diff: mojo/public/tools/bindings/generators/cpp_templates/struct_macros.tmpl

Issue 923033003: Implement unions as members of structs. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/public/tools/bindings/generators/cpp_templates/struct_macros.tmpl
diff --git a/mojo/public/tools/bindings/generators/cpp_templates/struct_macros.tmpl b/mojo/public/tools/bindings/generators/cpp_templates/struct_macros.tmpl
index 03911b9b39b47e10ae9386c56624cb41e2c08d09..c4640aad6416e8f7863d215c7420623ccd07c0c4 100644
--- a/mojo/public/tools/bindings/generators/cpp_templates/struct_macros.tmpl
+++ b/mojo/public/tools/bindings/generators/cpp_templates/struct_macros.tmpl
@@ -1,4 +1,226 @@
-{# TODO(yzshen): Make these templates more readable. #}
+{# TODO(yzshen): Consider merging the template code for user-defined and
+ parameter data structs. That way we can eliminate validate(), fields(),
+ encodes() and decodes(). #}
+
+{# Validates the specified struct field, which is supposed to be an object
+ (struct/array/string/map/union).
+ This macro is expanded by the validate() macro. #}
+{%- macro _validate_object(struct, packed_field) %}
+{%- set name = packed_field.field.name %}
+{%- set kind = packed_field.field.kind %}
+{%- set wrapper_type = kind|cpp_wrapper_type %}
+{%- if not kind|is_nullable_kind %}
+{%- if kind|is_union_kind %}
+ if (object->{{name}}.is_null()) {
+{%- else %}
+ if (!object->{{name}}.offset) {
+{%- endif %}
+ ReportValidationError(
+ mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
+ "null {{name}} field in {{struct.name}} struct");
+ return false;
+ }
+{%- endif %}
+{%- if not kind|is_union_kind %}
+ if (!mojo::internal::ValidateEncodedPointer(&object->{{name}}.offset)) {
+ ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER);
+ return false;
+ }
+{%- endif %}
+{%- if kind|is_array_kind or kind|is_string_kind %}
+ if (!{{wrapper_type}}::Data_::Validate<
+ {{kind|get_array_validate_params|indent(10)}}>(
+ mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
+ bounds_checker)) {
+{%- elif kind|is_map_kind %}
+ if (!{{wrapper_type}}::Data_::Validate<
+ {{kind.value_kind|get_map_validate_params|indent(10)}}>(
+ mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
+ bounds_checker)) {
+{%- elif kind|is_struct_kind %}
+ if (!{{kind|get_name_for_kind}}::Data_::Validate(
+ mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
+ bounds_checker)) {
+{%- elif kind|is_union_kind %}
+ // We already claimed the union's memory as part of the struct, so we don't
+ // try to claim the union's memory when validating the union.
+ if (!{{kind.name}}::Data_::Validate(&object->{{name}}, bounds_checker, true)) {
+{%- else %}
+ if (!{{wrapper_type}}::Data_::Validate(
+ mojo::internal::DecodePointerRaw(&object->{{name}}.offset),
+ bounds_checker)) {
+{%- endif %}
+ return false;
+ }
+{%- endmacro %}
+
+{# Validates the specified struct field, which is supposed to be a handle.
+ This macro is expanded by the validate() macro. #}
+{%- macro _validate_handle(struct, packed_field) %}
+{%- set name = packed_field.field.name %}
+{%- set kind = packed_field.field.kind %}
+{%- if not kind|is_nullable_kind %}
+ if (object->{{name}}.value() == mojo::internal::kEncodedInvalidHandleValue) {
+ ReportValidationError(
+ mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE,
+ "invalid {{name}} field in {{struct.name}} struct");
+ return false;
+ }
+{%- endif %}
+ if (!bounds_checker->ClaimHandle(object->{{name}})) {
+ ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_HANDLE);
+ return false;
+ }
+{%- endmacro %}
+
+{# Validates the specified struct.
+ This macro is expanded as the body of {{struct.name}}_Data::Validate(). #}
+{%- macro validate(struct) %}
+ if (!data)
+ return true;
+
+ if (!ValidateStructHeaderAndClaimMemory(data, bounds_checker))
+ return false;
+
+ // NOTE: The memory backing |object| may be smaller than |sizeof(*object)| if
+ // the message comes from an older version.
+ const {{struct.name}}_Data* object = static_cast<const {{struct.name}}_Data*>(data);
+
+ static const uint32_t kVersionSizePairs[{{struct.versions|length}}][2] = {
+{%- for version in struct.versions -%}
+ { {{version.version}}, {{version.num_bytes}} }{% if not loop.last %}, {% endif -%}
+{%- endfor -%}
+ };
+
+ if (object->header_.version <= {{struct.versions[-1].version}}) {
+ for (size_t i = 0; i < {{struct.versions|length}}; ++i) {
+ if (object->header_.version <= kVersionSizePairs[i][0]) {
+ if (object->header_.num_bytes == kVersionSizePairs[i][1])
+ break;
+
+ ReportValidationError(
+ mojo::internal::VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER);
+ return false;
+ }
+ }
+ } else if (object->header_.num_bytes < {{struct.versions[-1].num_bytes}}) {
+ ReportValidationError(
+ mojo::internal::VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER);
+ return false;
+ }
+
+{#- Before validating fields introduced at a certain version, we need to add
+ a version check, which makes sure we skip further validation if |object|
+ is from an earlier version. |last_checked_version| records the last
+ version that we have added such version check. #}
+{%- set last_checked_version = 0 %}
+{%- for packed_field in struct.packed.packed_fields_in_ordinal_order %}
+{%- set kind = packed_field.field.kind %}
+{%- if kind|is_object_kind or kind|is_any_handle_kind %}
+{%- if packed_field.min_version > last_checked_version %}
+{%- set last_checked_version = packed_field.min_version %}
+ if (object->header_.version < {{packed_field.min_version}})
+ return true;
+{%- endif %}
+{%- if kind|is_object_kind %}
+{{_validate_object(struct, packed_field)}}
+{%- elif kind|is_any_handle_kind %}
+{{_validate_handle(struct, packed_field)}}
+{%- endif %}
+{%- endif %}
+{%- endfor %}
+
+ return true;
+{%- endmacro %}
+
+{# Declares a data member for the specified struct field.
+ This macro is expanded by the fields() macro. #}
+{%- macro _field_line(field) %}
+{%- set type = field.kind|cpp_field_type %}
+{%- set name = field.name -%}
+{%- if field.kind.spec == 'b' -%}
+ uint8_t {{name}} : 1;
+{%- elif field.kind|is_enum_kind -%}
+ int32_t {{name}};
+{%- else -%}
+ {{type}} {{name}};
+{%- endif %}
+{%- endmacro %}
+
+{# Declares data members for the specified struct.
+ This macro is expanded in the {{struct.name}}_Data class declaration. #}
+{%- macro fields(struct) %}
+{%- for packed_field in struct.packed.packed_fields %}
+ {{_field_line(packed_field.field)}}
+{%- if not loop.last %}
+{%- set next_pf = struct.packed.packed_fields[loop.index0 + 1] %}
+{%- set pad = next_pf.offset - (packed_field.offset + packed_field.size) %}
+{%- if pad > 0 %}
+ uint8_t pad{{loop.index0}}_[{{pad}}];
+{%- endif %}
+{%- endif %}
+{%- endfor -%}
+
+{%- set num_fields = struct.packed.packed_fields|length %}
+{%- if num_fields > 0 %}
+{%- set last_field = struct.packed.packed_fields[num_fields - 1] %}
+{%- set offset = last_field.offset + last_field.size %}
+{%- set pad = offset|get_pad(8) -%}
+{%- if pad > 0 %}
+ uint8_t padfinal_[{{pad}}];
+{%- endif %}
+{%- endif %}
+{%- endmacro %}
+
+{# Encodes the specified struct.
+ Expands as the body of {{struct.name}}_Data::EncodePointersAndHandles(). #}
+{%- macro encodes(struct) -%}
+ MOJO_CHECK(header_.version == {{struct.versions[-1].version}});
+{%- for pf in struct.packed.packed_fields_in_ordinal_order %}
+{%- if pf.field.kind|is_object_kind %}
yzshen1 2015/03/26 07:30:15 This macro (and some others) are gone. There migh
azani 2015/03/26 22:27:39 Done.
+{%- if pf.field.kind|is_union_kind %}
+// TODO(azani): Encode pointers and handles.
+{%- else %}
+ mojo::internal::Encode(&{{pf.field.name}}, handles);
+{%- endif %}
+{%- elif pf.field.kind|is_any_handle_kind %}
+ mojo::internal::EncodeHandle(&{{pf.field.name}}, handles);
+{%- endif %}
+{%- endfor %}
+{%- endmacro -%}
+
+{# Decodes the specified struct.
+ This macro is expanded as the body of
+ {{struct.name}}_Data:DecodePointersAndHandles(). #}
+{%- macro decodes(struct) -%}
+ // NOTE: The memory backing |this| may has be smaller than |sizeof(*this)|, if
+ // the message comes from an older version.
+{#- Before decoding fields introduced at a certain version, we need to add
+ a version check, which makes sure we skip further decoding if |this|
+ is from an earlier version. |last_checked_version| records the last
+ version that we have added such version check. #}
+{%- set last_checked_version = 0 %}
+{%- for pf in struct.packed.packed_fields_in_ordinal_order %}
+{%- set name = pf.field.name %}
+{%- set kind = pf.field.kind %}
+{%- if kind|is_object_kind or kind|is_any_handle_kind %}
+{%- if pf.min_version > last_checked_version %}
+{%- set last_checked_version = pf.min_version %}
+ if (header_.version < {{pf.min_version}})
+ return;
+{%- endif %}
+{%- if kind|is_object_kind %}
+{%- if pf.field.kind|is_union_kind %}
+// TODO(azani): Decode pointers and handles.
+{%- else %}
+ mojo::internal::Decode(&{{name}}, handles);
+{%- endif %}
+{%- else %}
+ mojo::internal::DecodeHandle(&{{name}}, handles);
+{%- endif %}
+{%- endif %}
+{%- endfor %}
+{%- endmacro -%}
{# Computes the serialized size for the specified struct.
|struct| is the struct definition.
@@ -46,12 +268,19 @@
{%- elif kind|is_map_kind %}
mojo::SerializeMap_<{{kind.value_kind|get_map_validate_params|indent(24)}}>(
mojo::internal::Forward({{input_field}}), {{buffer}}, &{{output}}->{{name}}.ptr);
+{%- elif kind|is_union_kind %}
+ internal::{{kind.name}}_Data* {{name}}_ptr = &{{output}}->{{name}};
+ SerializeUnion_(mojo::internal::Forward({{input_field}}), {{buffer}}, &{{name}}_ptr, true);
{%- else %}
Serialize_(mojo::internal::Forward({{input_field}}), {{buffer}}, &{{output}}->{{name}}.ptr);
{%- endif %}
{%- if not kind|is_nullable_kind %}
MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING(
+{%- if kind|is_union_kind %}
+ {{output}}->{{name}}.is_null(),
+{%- else %}
!{{output}}->{{name}}.ptr,
+{%- endif %}
mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
"null {{name}} in {{struct_display_name}}");
{%- endif %}
@@ -102,7 +331,9 @@
if ({{input}}->header_.version < {{pf.min_version}})
break;
{%- endif %}
-{%- if kind|is_object_kind %}
+{%- if kind|is_union_kind %}
yzshen1 2015/03/26 07:30:15 Please move this if into the if for "is_object_kin
azani 2015/03/26 22:27:40 Done.
+ Deserialize_(&input->{{pf.field.name}}, &result->{{pf.field.name}});
yzshen1 2015/03/26 07:30:15 Please use {{output_field}}, {{input}}. Besides,
azani 2015/03/26 22:27:39 Done.
+{%- elif kind|is_object_kind %}
Deserialize_({{input}}->{{name}}.ptr, &{{output_field}});
{%- elif kind|is_interface_kind or kind|is_interface_request_kind %}
{{output_field}}.Bind(mojo::MakeScopedHandle(mojo::internal::FetchAndReset(&{{input}}->{{name}})));

Powered by Google App Engine
This is Rietveld 408576698