| 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 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 return hasattr(kind, "name") and \ | 135 return hasattr(kind, "name") and \ |
| 136 GetFullMojomNameForKind(kind) in _current_typemap | 136 GetFullMojomNameForKind(kind) in _current_typemap |
| 137 | 137 |
| 138 def IsNativeOnlyKind(kind): | 138 def IsNativeOnlyKind(kind): |
| 139 return (mojom.IsStructKind(kind) or mojom.IsEnumKind(kind)) and \ | 139 return (mojom.IsStructKind(kind) or mojom.IsEnumKind(kind)) and \ |
| 140 kind.native_only | 140 kind.native_only |
| 141 | 141 |
| 142 def GetNativeTypeName(typemapped_kind): | 142 def GetNativeTypeName(typemapped_kind): |
| 143 return _current_typemap[GetFullMojomNameForKind(typemapped_kind)]["typename"] | 143 return _current_typemap[GetFullMojomNameForKind(typemapped_kind)]["typename"] |
| 144 | 144 |
| 145 def GetCppType(kind): | |
| 146 if mojom.IsArrayKind(kind): | |
| 147 return "mojo::internal::Array_Data<%s>*" % GetCppType(kind.kind) | |
| 148 if mojom.IsMapKind(kind): | |
| 149 return "mojo::internal::Map_Data<%s, %s>*" % ( | |
| 150 GetCppType(kind.key_kind), GetCppType(kind.value_kind)) | |
| 151 if mojom.IsStructKind(kind): | |
| 152 return "%s*" % GetNameForKind(kind, internal=True) | |
| 153 if mojom.IsUnionKind(kind): | |
| 154 return "%s" % GetNameForKind(kind, internal=True) | |
| 155 if mojom.IsInterfaceKind(kind): | |
| 156 return "mojo::internal::Interface_Data" | |
| 157 if mojom.IsInterfaceRequestKind(kind): | |
| 158 return "mojo::internal::Handle_Data" | |
| 159 if mojom.IsAssociatedInterfaceKind(kind): | |
| 160 return "mojo::internal::AssociatedInterface_Data" | |
| 161 if mojom.IsAssociatedInterfaceRequestKind(kind): | |
| 162 return "mojo::internal::AssociatedInterfaceRequest_Data" | |
| 163 if mojom.IsEnumKind(kind): | |
| 164 return "int32_t" | |
| 165 if mojom.IsStringKind(kind): | |
| 166 return "mojo::internal::String_Data*" | |
| 167 if mojom.IsAnyHandleKind(kind): | |
| 168 return "mojo::internal::Handle_Data" | |
| 169 return _kind_to_cpp_type[kind] | |
| 170 | |
| 171 def GetCppPodType(kind): | 145 def GetCppPodType(kind): |
| 172 if mojom.IsStringKind(kind): | 146 if mojom.IsStringKind(kind): |
| 173 return "char*" | 147 return "char*" |
| 174 return _kind_to_cpp_type[kind] | 148 return _kind_to_cpp_type[kind] |
| 175 | 149 |
| 176 def GetCppWrapperType(kind): | 150 def GetCppWrapperType(kind): |
| 177 if IsTypemappedKind(kind): | 151 if IsTypemappedKind(kind): |
| 178 return GetNativeTypeName(kind) | 152 return GetNativeTypeName(kind) |
| 179 if mojom.IsEnumKind(kind): | 153 if mojom.IsEnumKind(kind): |
| 180 return GetNameForKind(kind) | 154 return GetNameForKind(kind) |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 else "const %s&" % cpp_wrapper_type) | 198 else "const %s&" % cpp_wrapper_type) |
| 225 | 199 |
| 226 def GetCppFieldType(kind): | 200 def GetCppFieldType(kind): |
| 227 if mojom.IsStructKind(kind): | 201 if mojom.IsStructKind(kind): |
| 228 return ("mojo::internal::Pointer<%s>" % | 202 return ("mojo::internal::Pointer<%s>" % |
| 229 GetNameForKind(kind, internal=True)) | 203 GetNameForKind(kind, internal=True)) |
| 230 if mojom.IsUnionKind(kind): | 204 if mojom.IsUnionKind(kind): |
| 231 return "%s" % GetNameForKind(kind, internal=True) | 205 return "%s" % GetNameForKind(kind, internal=True) |
| 232 if mojom.IsArrayKind(kind): | 206 if mojom.IsArrayKind(kind): |
| 233 return ("mojo::internal::Pointer<mojo::internal::Array_Data<%s>>" % | 207 return ("mojo::internal::Pointer<mojo::internal::Array_Data<%s>>" % |
| 234 GetCppType(kind.kind)) | 208 GetCppFieldType(kind.kind)) |
| 235 if mojom.IsMapKind(kind): | 209 if mojom.IsMapKind(kind): |
| 236 return ("mojo::internal::Pointer<mojo::internal::Map_Data<%s, %s>>" % | 210 return ("mojo::internal::Pointer<mojo::internal::Map_Data<%s, %s>>" % |
| 237 (GetCppType(kind.key_kind), GetCppType(kind.value_kind))) | 211 (GetCppFieldType(kind.key_kind), GetCppFieldType(kind.value_kind))) |
| 238 if mojom.IsInterfaceKind(kind): | 212 if mojom.IsInterfaceKind(kind): |
| 239 return "mojo::internal::Interface_Data" | 213 return "mojo::internal::Interface_Data" |
| 240 if mojom.IsInterfaceRequestKind(kind): | 214 if mojom.IsInterfaceRequestKind(kind): |
| 241 return "mojo::internal::Handle_Data" | 215 return "mojo::internal::Handle_Data" |
| 242 if mojom.IsAssociatedInterfaceKind(kind): | 216 if mojom.IsAssociatedInterfaceKind(kind): |
| 243 return "mojo::internal::AssociatedInterface_Data" | 217 return "mojo::internal::AssociatedInterface_Data" |
| 244 if mojom.IsAssociatedInterfaceRequestKind(kind): | 218 if mojom.IsAssociatedInterfaceRequestKind(kind): |
| 245 return "mojo::internal::AssociatedInterfaceRequest_Data" | 219 return "mojo::internal::AssociatedInterfaceRequest_Data" |
| 246 if mojom.IsEnumKind(kind): | 220 if mojom.IsEnumKind(kind): |
| 247 return "int32_t" | 221 return "int32_t" |
| 248 if mojom.IsStringKind(kind): | 222 if mojom.IsStringKind(kind): |
| 249 return "mojo::internal::Pointer<mojo::internal::String_Data>" | 223 return "mojo::internal::Pointer<mojo::internal::String_Data>" |
| 250 if mojom.IsAnyHandleKind(kind): | 224 if mojom.IsAnyHandleKind(kind): |
| 251 return "mojo::internal::Handle_Data" | 225 return "mojo::internal::Handle_Data" |
| 252 return _kind_to_cpp_type[kind] | 226 return _kind_to_cpp_type[kind] |
| 253 | 227 |
| 254 def GetCppUnionFieldType(kind): | 228 def GetCppUnionFieldType(kind): |
| 255 if mojom.IsUnionKind(kind): | 229 if mojom.IsUnionKind(kind): |
| 256 return ("mojo::internal::Pointer<%s>" % GetNameForKind(kind, internal=True)) | 230 return ("mojo::internal::Pointer<%s>" % GetNameForKind(kind, internal=True)) |
| 257 return GetCppFieldType(kind) | 231 return GetCppFieldType(kind) |
| 258 | 232 |
| 259 def GetUnionGetterReturnType(kind): | 233 def GetUnionGetterReturnType(kind): |
| 260 if mojom.IsReferenceKind(kind): | 234 if mojom.IsReferenceKind(kind): |
| 261 return "%s&" % GetCppWrapperType(kind) | 235 return "%s&" % GetCppWrapperType(kind) |
| 262 return GetCppWrapperType(kind) | 236 return GetCppWrapperType(kind) |
| 263 | 237 |
| 264 # TODO(yzshen): It is unfortunate that we have so many functions for returning | |
| 265 # types. Refactor them. | |
| 266 def GetUnmappedTypeForSerializer(kind): | 238 def GetUnmappedTypeForSerializer(kind): |
| 267 if mojom.IsEnumKind(kind): | 239 if mojom.IsEnumKind(kind): |
| 268 return GetQualifiedNameForKind(kind) | 240 return GetQualifiedNameForKind(kind) |
| 269 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): | 241 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): |
| 270 return "%sPtr" % GetQualifiedNameForKind(kind) | 242 return "%sPtr" % GetQualifiedNameForKind(kind) |
| 271 if mojom.IsArrayKind(kind): | 243 if mojom.IsArrayKind(kind): |
| 272 return "mojo::Array<%s>" % GetUnmappedTypeForSerializer(kind.kind) | 244 return "mojo::Array<%s>" % GetUnmappedTypeForSerializer(kind.kind) |
| 273 if mojom.IsMapKind(kind): | 245 if mojom.IsMapKind(kind): |
| 274 return "mojo::Map<%s, %s>" % ( | 246 return "mojo::Map<%s, %s>" % ( |
| 275 GetUnmappedTypeForSerializer(kind.key_kind), | 247 GetUnmappedTypeForSerializer(kind.key_kind), |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 485 _for_blink = self.for_blink | 457 _for_blink = self.for_blink |
| 486 global _variant | 458 global _variant |
| 487 _variant = self.variant | 459 _variant = self.variant |
| 488 suffix = "-%s" % self.variant if self.variant else "" | 460 suffix = "-%s" % self.variant if self.variant else "" |
| 489 self.Write(self.GenerateModuleHeader(), | 461 self.Write(self.GenerateModuleHeader(), |
| 490 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) | 462 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) |
| 491 self.Write(self.GenerateModuleInternalHeader(), | 463 self.Write(self.GenerateModuleInternalHeader(), |
| 492 self.MatchMojomFilePath("%s%s-internal.h" % (self.module.name, suffix))) | 464 self.MatchMojomFilePath("%s%s-internal.h" % (self.module.name, suffix))) |
| 493 self.Write(self.GenerateModuleSource(), | 465 self.Write(self.GenerateModuleSource(), |
| 494 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) | 466 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) |
| OLD | NEW |