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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 | 146 |
147 def GetNativeTypeName(typemapped_kind): | 147 def GetNativeTypeName(typemapped_kind): |
148 return _current_typemap[GetFullMojomNameForKind(typemapped_kind)]["typename"] | 148 return _current_typemap[GetFullMojomNameForKind(typemapped_kind)]["typename"] |
149 | 149 |
150 def GetCppPodType(kind): | 150 def GetCppPodType(kind): |
151 if mojom.IsStringKind(kind): | 151 if mojom.IsStringKind(kind): |
152 return "char*" | 152 return "char*" |
153 return _kind_to_cpp_type[kind] | 153 return _kind_to_cpp_type[kind] |
154 | 154 |
155 def GetCppWrapperType(kind): | 155 def GetCppWrapperType(kind): |
| 156 def _AddOptional(type_name): |
| 157 pattern = "WTF::Optional<%s>" if _for_blink else "base::Optional<%s>" |
| 158 return pattern % type_name |
| 159 |
156 if IsTypemappedKind(kind): | 160 if IsTypemappedKind(kind): |
157 return GetNativeTypeName(kind) | 161 type_name = GetNativeTypeName(kind) |
| 162 if (mojom.IsNullableKind(kind) and |
| 163 not _current_typemap[GetFullMojomNameForKind(kind)][ |
| 164 "nullable_is_same_type"]): |
| 165 type_name = _AddOptional(type_name) |
| 166 return type_name |
158 if mojom.IsEnumKind(kind): | 167 if mojom.IsEnumKind(kind): |
159 return GetNameForKind(kind) | 168 return GetNameForKind(kind) |
160 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): | 169 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): |
161 return "%sPtr" % GetNameForKind(kind) | 170 return "%sPtr" % GetNameForKind(kind) |
162 if mojom.IsArrayKind(kind): | 171 if mojom.IsArrayKind(kind): |
163 pattern = None | 172 pattern = None |
164 if _use_new_wrapper_types: | 173 if _use_new_wrapper_types: |
| 174 pattern = "WTF::Vector<%s>" if _for_blink else "std::vector<%s>" |
165 if mojom.IsNullableKind(kind): | 175 if mojom.IsNullableKind(kind): |
166 pattern = ("WTF::Optional<WTF::Vector<%s>>" if _for_blink else | 176 pattern = _AddOptional(pattern) |
167 "base::Optional<std::vector<%s>>") | |
168 else: | |
169 pattern = "WTF::Vector<%s>" if _for_blink else "std::vector<%s>" | |
170 else: | 177 else: |
171 pattern = "mojo::WTFArray<%s>" if _for_blink else "mojo::Array<%s>" | 178 pattern = "mojo::WTFArray<%s>" if _for_blink else "mojo::Array<%s>" |
172 return pattern % GetCppWrapperType(kind.kind) | 179 return pattern % GetCppWrapperType(kind.kind) |
173 if mojom.IsMapKind(kind): | 180 if mojom.IsMapKind(kind): |
174 pattern = None | 181 pattern = None |
175 if _use_new_wrapper_types: | 182 if _use_new_wrapper_types: |
| 183 pattern = ("WTF::HashMap<%s, %s>" if _for_blink else |
| 184 "std::unordered_map<%s, %s>") |
176 if mojom.IsNullableKind(kind): | 185 if mojom.IsNullableKind(kind): |
177 pattern = ("WTF::Optional<WTF::HashMap<%s, %s>>" if _for_blink else | 186 pattern = _AddOptional(pattern) |
178 "base::Optional<std::unordered_map<%s, %s>>") | |
179 else: | |
180 pattern = ("WTF::HashMap<%s, %s>" if _for_blink else | |
181 "std::unordered_map<%s, %s>") | |
182 else: | 187 else: |
183 pattern = "mojo::WTFMap<%s, %s>" if _for_blink else "mojo::Map<%s, %s>" | 188 pattern = "mojo::WTFMap<%s, %s>" if _for_blink else "mojo::Map<%s, %s>" |
184 return pattern % (GetCppWrapperType(kind.key_kind), | 189 return pattern % (GetCppWrapperType(kind.key_kind), |
185 GetCppWrapperType(kind.value_kind)) | 190 GetCppWrapperType(kind.value_kind)) |
186 if mojom.IsInterfaceKind(kind): | 191 if mojom.IsInterfaceKind(kind): |
187 return "%sPtr" % GetNameForKind(kind) | 192 return "%sPtr" % GetNameForKind(kind) |
188 if mojom.IsInterfaceRequestKind(kind): | 193 if mojom.IsInterfaceRequestKind(kind): |
189 return "%sRequest" % GetNameForKind(kind.kind) | 194 return "%sRequest" % GetNameForKind(kind.kind) |
190 if mojom.IsAssociatedInterfaceKind(kind): | 195 if mojom.IsAssociatedInterfaceKind(kind): |
191 return "%sAssociatedPtrInfo" % GetNameForKind(kind.kind) | 196 return "%sAssociatedPtrInfo" % GetNameForKind(kind.kind) |
192 if mojom.IsAssociatedInterfaceRequestKind(kind): | 197 if mojom.IsAssociatedInterfaceRequestKind(kind): |
193 return "%sAssociatedRequest" % GetNameForKind(kind.kind) | 198 return "%sAssociatedRequest" % GetNameForKind(kind.kind) |
194 if mojom.IsStringKind(kind): | 199 if mojom.IsStringKind(kind): |
195 if _for_blink: | 200 if _for_blink: |
196 return "WTF::String" | 201 return "WTF::String" |
197 if not _use_new_wrapper_types: | 202 if not _use_new_wrapper_types: |
198 return "mojo::String" | 203 return "mojo::String" |
199 return ("base::Optional<std::string>" if mojom.IsNullableKind(kind) else | 204 type_name = "std::string" |
200 "std::string") | 205 return _AddOptional(type_name) if mojom.IsNullableKind(kind) else type_name |
201 if mojom.IsGenericHandleKind(kind): | 206 if mojom.IsGenericHandleKind(kind): |
202 return "mojo::ScopedHandle" | 207 return "mojo::ScopedHandle" |
203 if mojom.IsDataPipeConsumerKind(kind): | 208 if mojom.IsDataPipeConsumerKind(kind): |
204 return "mojo::ScopedDataPipeConsumerHandle" | 209 return "mojo::ScopedDataPipeConsumerHandle" |
205 if mojom.IsDataPipeProducerKind(kind): | 210 if mojom.IsDataPipeProducerKind(kind): |
206 return "mojo::ScopedDataPipeProducerHandle" | 211 return "mojo::ScopedDataPipeProducerHandle" |
207 if mojom.IsMessagePipeKind(kind): | 212 if mojom.IsMessagePipeKind(kind): |
208 return "mojo::ScopedMessagePipeHandle" | 213 return "mojo::ScopedMessagePipeHandle" |
209 if mojom.IsSharedBufferKind(kind): | 214 if mojom.IsSharedBufferKind(kind): |
210 return "mojo::ScopedSharedBufferHandle" | 215 return "mojo::ScopedSharedBufferHandle" |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 _use_new_wrapper_types = self.use_new_wrapper_types | 527 _use_new_wrapper_types = self.use_new_wrapper_types |
523 global _variant | 528 global _variant |
524 _variant = self.variant | 529 _variant = self.variant |
525 suffix = "-%s" % self.variant if self.variant else "" | 530 suffix = "-%s" % self.variant if self.variant else "" |
526 self.Write(self.GenerateModuleHeader(), | 531 self.Write(self.GenerateModuleHeader(), |
527 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) | 532 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) |
528 self.Write(self.GenerateModuleInternalHeader(), | 533 self.Write(self.GenerateModuleInternalHeader(), |
529 self.MatchMojomFilePath("%s%s-internal.h" % (self.module.name, suffix))) | 534 self.MatchMojomFilePath("%s%s-internal.h" % (self.module.name, suffix))) |
530 self.Write(self.GenerateModuleSource(), | 535 self.Write(self.GenerateModuleSource(), |
531 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) | 536 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) |
OLD | NEW |