| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Generates C++ source files from a mojom.Module.""" | 5 """Generates C++ source files from a mojom.Module.""" |
| 6 | 6 |
| 7 import mojom.generate.generator as generator | 7 import mojom.generate.generator as generator |
| 8 import mojom.generate.module as mojom | 8 import mojom.generate.module as mojom |
| 9 import mojom.generate.pack as pack | 9 import mojom.generate.pack as pack |
| 10 from mojom.generate.template_expander import UseJinja | 10 from mojom.generate.template_expander import UseJinja |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 return "char*" | 85 return "char*" |
| 86 return _kind_to_cpp_type[kind] | 86 return _kind_to_cpp_type[kind] |
| 87 | 87 |
| 88 def GetCppArrayArgWrapperType(kind): | 88 def GetCppArrayArgWrapperType(kind): |
| 89 if mojom.IsEnumKind(kind): | 89 if mojom.IsEnumKind(kind): |
| 90 return GetNameForKind(kind) | 90 return GetNameForKind(kind) |
| 91 if mojom.IsStructKind(kind): | 91 if mojom.IsStructKind(kind): |
| 92 return "%sPtr" % GetNameForKind(kind) | 92 return "%sPtr" % GetNameForKind(kind) |
| 93 if mojom.IsAnyArrayKind(kind): | 93 if mojom.IsAnyArrayKind(kind): |
| 94 return "mojo::Array<%s> " % GetCppArrayArgWrapperType(kind.kind) | 94 return "mojo::Array<%s> " % GetCppArrayArgWrapperType(kind.kind) |
| 95 if mojom.IsMapKind(kind): |
| 96 return "mojo::Map<%s, %s> " % (GetCppArrayArgWrapperType(kind.key_kind), |
| 97 GetCppArrayArgWrapperType(kind.value_kind)) |
| 95 if mojom.IsInterfaceKind(kind): | 98 if mojom.IsInterfaceKind(kind): |
| 96 raise Exception("Arrays of interfaces not yet supported!") | 99 raise Exception("Arrays of interfaces not yet supported!") |
| 97 if mojom.IsInterfaceRequestKind(kind): | 100 if mojom.IsInterfaceRequestKind(kind): |
| 98 raise Exception("Arrays of interface requests not yet supported!") | 101 raise Exception("Arrays of interface requests not yet supported!") |
| 99 if mojom.IsStringKind(kind): | 102 if mojom.IsStringKind(kind): |
| 100 return "mojo::String" | 103 return "mojo::String" |
| 101 if mojom.IsHandleKind(kind): | 104 if mojom.IsHandleKind(kind): |
| 102 return "mojo::ScopedHandle" | 105 return "mojo::ScopedHandle" |
| 103 if mojom.IsDataPipeConsumerKind(kind): | 106 if mojom.IsDataPipeConsumerKind(kind): |
| 104 return "mojo::ScopedDataPipeConsumerHandle" | 107 return "mojo::ScopedDataPipeConsumerHandle" |
| 105 if mojom.IsDataPipeProducerKind(kind): | 108 if mojom.IsDataPipeProducerKind(kind): |
| 106 return "mojo::ScopedDataPipeProducerHandle" | 109 return "mojo::ScopedDataPipeProducerHandle" |
| 107 if mojom.IsMessagePipeKind(kind): | 110 if mojom.IsMessagePipeKind(kind): |
| 108 return "mojo::ScopedMessagePipeHandle" | 111 return "mojo::ScopedMessagePipeHandle" |
| 109 if mojom.IsSharedBufferKind(kind): | 112 if mojom.IsSharedBufferKind(kind): |
| 110 return "mojo::ScopedSharedBufferHandle" | 113 return "mojo::ScopedSharedBufferHandle" |
| 111 return _kind_to_cpp_type[kind] | 114 return _kind_to_cpp_type[kind] |
| 112 | 115 |
| 113 def GetCppResultWrapperType(kind): | 116 def GetCppResultWrapperType(kind): |
| 114 if mojom.IsEnumKind(kind): | 117 if mojom.IsEnumKind(kind): |
| 115 return GetNameForKind(kind) | 118 return GetNameForKind(kind) |
| 116 if mojom.IsStructKind(kind): | 119 if mojom.IsStructKind(kind): |
| 117 return "%sPtr" % GetNameForKind(kind) | 120 return "%sPtr" % GetNameForKind(kind) |
| 118 if mojom.IsAnyArrayKind(kind): | 121 if mojom.IsAnyArrayKind(kind): |
| 119 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind) | 122 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind) |
| 123 if mojom.IsMapKind(kind): |
| 124 return "mojo::Map<%s, %s>" % (GetCppArrayArgWrapperType(kind.key_kind), |
| 125 GetCppArrayArgWrapperType(kind.value_kind)) |
| 120 if mojom.IsInterfaceKind(kind): | 126 if mojom.IsInterfaceKind(kind): |
| 121 return "%sPtr" % GetNameForKind(kind) | 127 return "%sPtr" % GetNameForKind(kind) |
| 122 if mojom.IsInterfaceRequestKind(kind): | 128 if mojom.IsInterfaceRequestKind(kind): |
| 123 return "mojo::InterfaceRequest<%s>" % GetNameForKind(kind.kind) | 129 return "mojo::InterfaceRequest<%s>" % GetNameForKind(kind.kind) |
| 124 if mojom.IsStringKind(kind): | 130 if mojom.IsStringKind(kind): |
| 125 return "mojo::String" | 131 return "mojo::String" |
| 126 if mojom.IsHandleKind(kind): | 132 if mojom.IsHandleKind(kind): |
| 127 return "mojo::ScopedHandle" | 133 return "mojo::ScopedHandle" |
| 128 if mojom.IsDataPipeConsumerKind(kind): | 134 if mojom.IsDataPipeConsumerKind(kind): |
| 129 return "mojo::ScopedDataPipeConsumerHandle" | 135 return "mojo::ScopedDataPipeConsumerHandle" |
| 130 if mojom.IsDataPipeProducerKind(kind): | 136 if mojom.IsDataPipeProducerKind(kind): |
| 131 return "mojo::ScopedDataPipeProducerHandle" | 137 return "mojo::ScopedDataPipeProducerHandle" |
| 132 if mojom.IsMessagePipeKind(kind): | 138 if mojom.IsMessagePipeKind(kind): |
| 133 return "mojo::ScopedMessagePipeHandle" | 139 return "mojo::ScopedMessagePipeHandle" |
| 134 if mojom.IsSharedBufferKind(kind): | 140 if mojom.IsSharedBufferKind(kind): |
| 135 return "mojo::ScopedSharedBufferHandle" | 141 return "mojo::ScopedSharedBufferHandle" |
| 136 return _kind_to_cpp_type[kind] | 142 return _kind_to_cpp_type[kind] |
| 137 | 143 |
| 138 def GetCppWrapperType(kind): | 144 def GetCppWrapperType(kind): |
| 139 if mojom.IsEnumKind(kind): | 145 if mojom.IsEnumKind(kind): |
| 140 return GetNameForKind(kind) | 146 return GetNameForKind(kind) |
| 141 if mojom.IsStructKind(kind): | 147 if mojom.IsStructKind(kind): |
| 142 return "%sPtr" % GetNameForKind(kind) | 148 return "%sPtr" % GetNameForKind(kind) |
| 143 if mojom.IsAnyArrayKind(kind): | 149 if mojom.IsAnyArrayKind(kind): |
| 144 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind) | 150 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind) |
| 151 if mojom.IsMapKind(kind): |
| 152 return "mojo::Map<%s, %s>" % (GetCppArrayArgWrapperType(kind.key_kind), |
| 153 GetCppArrayArgWrapperType(kind.value_kind)) |
| 145 if mojom.IsInterfaceKind(kind): | 154 if mojom.IsInterfaceKind(kind): |
| 146 return "%sPtr" % GetNameForKind(kind) | 155 return "%sPtr" % GetNameForKind(kind) |
| 147 if mojom.IsInterfaceRequestKind(kind): | 156 if mojom.IsInterfaceRequestKind(kind): |
| 148 raise Exception("InterfaceRequest fields not supported!") | 157 raise Exception("InterfaceRequest fields not supported!") |
| 149 if mojom.IsStringKind(kind): | 158 if mojom.IsStringKind(kind): |
| 150 return "mojo::String" | 159 return "mojo::String" |
| 151 if mojom.IsHandleKind(kind): | 160 if mojom.IsHandleKind(kind): |
| 152 return "mojo::ScopedHandle" | 161 return "mojo::ScopedHandle" |
| 153 if mojom.IsDataPipeConsumerKind(kind): | 162 if mojom.IsDataPipeConsumerKind(kind): |
| 154 return "mojo::ScopedDataPipeConsumerHandle" | 163 return "mojo::ScopedDataPipeConsumerHandle" |
| 155 if mojom.IsDataPipeProducerKind(kind): | 164 if mojom.IsDataPipeProducerKind(kind): |
| 156 return "mojo::ScopedDataPipeProducerHandle" | 165 return "mojo::ScopedDataPipeProducerHandle" |
| 157 if mojom.IsMessagePipeKind(kind): | 166 if mojom.IsMessagePipeKind(kind): |
| 158 return "mojo::ScopedMessagePipeHandle" | 167 return "mojo::ScopedMessagePipeHandle" |
| 159 if mojom.IsSharedBufferKind(kind): | 168 if mojom.IsSharedBufferKind(kind): |
| 160 return "mojo::ScopedSharedBufferHandle" | 169 return "mojo::ScopedSharedBufferHandle" |
| 161 return _kind_to_cpp_type[kind] | 170 return _kind_to_cpp_type[kind] |
| 162 | 171 |
| 163 def GetCppConstWrapperType(kind): | 172 def GetCppConstWrapperType(kind): |
| 164 if mojom.IsStructKind(kind): | 173 if mojom.IsStructKind(kind): |
| 165 return "%sPtr" % GetNameForKind(kind) | 174 return "%sPtr" % GetNameForKind(kind) |
| 166 if mojom.IsAnyArrayKind(kind): | 175 if mojom.IsAnyArrayKind(kind): |
| 167 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind) | 176 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind) |
| 177 if mojom.IsMapKind(kind): |
| 178 return "mojo::Map<%s, %s>" % (GetCppArrayArgWrapperType(kind.key_kind), |
| 179 GetCppArrayArgWrapperType(kind.value_kind)) |
| 168 if mojom.IsInterfaceKind(kind): | 180 if mojom.IsInterfaceKind(kind): |
| 169 return "%sPtr" % GetNameForKind(kind) | 181 return "%sPtr" % GetNameForKind(kind) |
| 170 if mojom.IsInterfaceRequestKind(kind): | 182 if mojom.IsInterfaceRequestKind(kind): |
| 171 return "mojo::InterfaceRequest<%s>" % GetNameForKind(kind.kind) | 183 return "mojo::InterfaceRequest<%s>" % GetNameForKind(kind.kind) |
| 172 if mojom.IsEnumKind(kind): | 184 if mojom.IsEnumKind(kind): |
| 173 return GetNameForKind(kind) | 185 return GetNameForKind(kind) |
| 174 if mojom.IsStringKind(kind): | 186 if mojom.IsStringKind(kind): |
| 175 return "const mojo::String&" | 187 return "const mojo::String&" |
| 176 if mojom.IsHandleKind(kind): | 188 if mojom.IsHandleKind(kind): |
| 177 return "mojo::ScopedHandle" | 189 return "mojo::ScopedHandle" |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 else: | 273 else: |
| 262 expected_num_elements = generator.ExpectedArraySize(kind) | 274 expected_num_elements = generator.ExpectedArraySize(kind) |
| 263 element_is_nullable = mojom.IsNullableKind(kind.kind) | 275 element_is_nullable = mojom.IsNullableKind(kind.kind) |
| 264 element_validate_params = GetArrayValidateParams(kind.kind) | 276 element_validate_params = GetArrayValidateParams(kind.kind) |
| 265 | 277 |
| 266 return "mojo::internal::ArrayValidateParams<%d, %s,\n%s> " % ( | 278 return "mojo::internal::ArrayValidateParams<%d, %s,\n%s> " % ( |
| 267 expected_num_elements, | 279 expected_num_elements, |
| 268 'true' if element_is_nullable else 'false', | 280 'true' if element_is_nullable else 'false', |
| 269 element_validate_params) | 281 element_validate_params) |
| 270 | 282 |
| 283 def GetMapValidateParams(kind): |
| 284 # Unlike GetArrayValidateParams, we are given the wrapped kind, instead of |
| 285 # the raw array kind. So we wrap the return value of GetArrayValidateParams. |
| 286 element_is_nullable = mojom.IsNullableKind(kind) |
| 287 return "mojo::internal::ArrayValidateParams<0, %s,\n%s> " % ( |
| 288 'true' if element_is_nullable else 'false', |
| 289 GetArrayValidateParams(kind)) |
| 290 |
| 271 _HEADER_SIZE = 8 | 291 _HEADER_SIZE = 8 |
| 272 | 292 |
| 273 class Generator(generator.Generator): | 293 class Generator(generator.Generator): |
| 274 | 294 |
| 275 cpp_filters = { | 295 cpp_filters = { |
| 276 "constant_value": ConstantValue, | 296 "constant_value": ConstantValue, |
| 277 "cpp_const_wrapper_type": GetCppConstWrapperType, | 297 "cpp_const_wrapper_type": GetCppConstWrapperType, |
| 278 "cpp_field_type": GetCppFieldType, | 298 "cpp_field_type": GetCppFieldType, |
| 279 "cpp_pod_type": GetCppPodType, | 299 "cpp_pod_type": GetCppPodType, |
| 280 "cpp_result_type": GetCppResultWrapperType, | 300 "cpp_result_type": GetCppResultWrapperType, |
| 281 "cpp_type": GetCppType, | 301 "cpp_type": GetCppType, |
| 282 "cpp_wrapper_type": GetCppWrapperType, | 302 "cpp_wrapper_type": GetCppWrapperType, |
| 283 "default_value": DefaultValue, | 303 "default_value": DefaultValue, |
| 284 "expected_array_size": generator.ExpectedArraySize, | 304 "expected_array_size": generator.ExpectedArraySize, |
| 285 "expression_to_text": ExpressionToText, | 305 "expression_to_text": ExpressionToText, |
| 286 "get_array_validate_params": GetArrayValidateParams, | 306 "get_array_validate_params": GetArrayValidateParams, |
| 307 "get_map_validate_params": GetMapValidateParams, |
| 287 "get_name_for_kind": GetNameForKind, | 308 "get_name_for_kind": GetNameForKind, |
| 288 "get_pad": pack.GetPad, | 309 "get_pad": pack.GetPad, |
| 289 "has_callbacks": mojom.HasCallbacks, | 310 "has_callbacks": mojom.HasCallbacks, |
| 290 "should_inline": ShouldInlineStruct, | 311 "should_inline": ShouldInlineStruct, |
| 291 "is_any_array_kind": mojom.IsAnyArrayKind, | 312 "is_any_array_kind": mojom.IsAnyArrayKind, |
| 292 "is_enum_kind": mojom.IsEnumKind, | 313 "is_enum_kind": mojom.IsEnumKind, |
| 293 "is_move_only_kind": mojom.IsMoveOnlyKind, | 314 "is_move_only_kind": mojom.IsMoveOnlyKind, |
| 294 "is_any_handle_kind": mojom.IsAnyHandleKind, | 315 "is_any_handle_kind": mojom.IsAnyHandleKind, |
| 295 "is_interface_kind": mojom.IsInterfaceKind, | 316 "is_interface_kind": mojom.IsInterfaceKind, |
| 296 "is_interface_request_kind": mojom.IsInterfaceRequestKind, | 317 "is_interface_request_kind": mojom.IsInterfaceRequestKind, |
| 318 "is_map_kind": mojom.IsMapKind, |
| 297 "is_nullable_kind": mojom.IsNullableKind, | 319 "is_nullable_kind": mojom.IsNullableKind, |
| 298 "is_object_kind": mojom.IsObjectKind, | 320 "is_object_kind": mojom.IsObjectKind, |
| 299 "is_string_kind": mojom.IsStringKind, | 321 "is_string_kind": mojom.IsStringKind, |
| 300 "is_struct_with_handles": IsStructWithHandles, | 322 "is_struct_with_handles": IsStructWithHandles, |
| 301 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, | 323 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, |
| 302 "struct_from_method": generator.GetStructFromMethod, | 324 "struct_from_method": generator.GetStructFromMethod, |
| 303 "response_struct_from_method": generator.GetResponseStructFromMethod, | 325 "response_struct_from_method": generator.GetResponseStructFromMethod, |
| 304 "stylize_method": generator.StudlyCapsToCamel, | 326 "stylize_method": generator.StudlyCapsToCamel, |
| 305 "to_all_caps": generator.CamelCaseToAllCaps, | 327 "to_all_caps": generator.CamelCaseToAllCaps, |
| 306 } | 328 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 327 | 349 |
| 328 @UseJinja("cpp_templates/module.cc.tmpl", filters=cpp_filters) | 350 @UseJinja("cpp_templates/module.cc.tmpl", filters=cpp_filters) |
| 329 def GenerateModuleSource(self): | 351 def GenerateModuleSource(self): |
| 330 return self.GetJinjaExports() | 352 return self.GetJinjaExports() |
| 331 | 353 |
| 332 def GenerateFiles(self, args): | 354 def GenerateFiles(self, args): |
| 333 self.Write(self.GenerateModuleHeader(), "%s.h" % self.module.name) | 355 self.Write(self.GenerateModuleHeader(), "%s.h" % self.module.name) |
| 334 self.Write(self.GenerateModuleInternalHeader(), | 356 self.Write(self.GenerateModuleInternalHeader(), |
| 335 "%s-internal.h" % self.module.name) | 357 "%s-internal.h" % self.module.name) |
| 336 self.Write(self.GenerateModuleSource(), "%s.cc" % self.module.name) | 358 self.Write(self.GenerateModuleSource(), "%s.cc" % self.module.name) |
| OLD | NEW |