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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 if kind.parent_kind: | 42 if kind.parent_kind: |
43 parts.append(kind.parent_kind.name) | 43 parts.append(kind.parent_kind.name) |
44 parts.append(kind.name) | 44 parts.append(kind.name) |
45 return "::".join(parts) | 45 return "::".join(parts) |
46 | 46 |
47 def GetCppType(kind): | 47 def GetCppType(kind): |
48 if isinstance(kind, mojom.Struct): | 48 if isinstance(kind, mojom.Struct): |
49 return "%s_Data*" % GetNameForKind(kind, internal=True) | 49 return "%s_Data*" % GetNameForKind(kind, internal=True) |
50 if isinstance(kind, mojom.Array): | 50 if isinstance(kind, mojom.Array): |
51 return "mojo::internal::Array_Data<%s>*" % GetCppType(kind.kind) | 51 return "mojo::internal::Array_Data<%s>*" % GetCppType(kind.kind) |
52 if isinstance(kind, mojom.Interface): | 52 if isinstance(kind, mojom.Interface) or \ |
| 53 isinstance(kind, mojom.InterfaceRequest): |
53 return "mojo::MessagePipeHandle" | 54 return "mojo::MessagePipeHandle" |
54 if isinstance(kind, mojom.Enum): | 55 if isinstance(kind, mojom.Enum): |
55 return "int32_t" | 56 return "int32_t" |
56 if kind.spec == 's': | 57 if kind.spec == 's': |
57 return "mojo::internal::String_Data*" | 58 return "mojo::internal::String_Data*" |
58 return _kind_to_cpp_type[kind] | 59 return _kind_to_cpp_type[kind] |
59 | 60 |
60 def GetCppPodType(kind): | 61 def GetCppPodType(kind): |
61 if kind.spec == 's': | 62 if kind.spec == 's': |
62 return "char*" | 63 return "char*" |
63 return _kind_to_cpp_type[kind] | 64 return _kind_to_cpp_type[kind] |
64 | 65 |
65 def GetCppArrayArgWrapperType(kind): | 66 def GetCppArrayArgWrapperType(kind): |
66 if isinstance(kind, mojom.Enum): | 67 if isinstance(kind, mojom.Enum): |
67 return GetNameForKind(kind) | 68 return GetNameForKind(kind) |
68 if isinstance(kind, mojom.Struct): | 69 if isinstance(kind, mojom.Struct): |
69 return "%sPtr" % GetNameForKind(kind) | 70 return "%sPtr" % GetNameForKind(kind) |
70 if isinstance(kind, mojom.Array): | 71 if isinstance(kind, mojom.Array): |
71 return "mojo::Array<%s> " % GetCppArrayArgWrapperType(kind.kind) | 72 return "mojo::Array<%s> " % GetCppArrayArgWrapperType(kind.kind) |
72 if isinstance(kind, mojom.Interface): | 73 if isinstance(kind, mojom.Interface): |
73 raise Exception("Arrays of interfaces not yet supported!") | 74 raise Exception("Arrays of interfaces not yet supported!") |
| 75 if isinstance(kind, mojom.InterfaceRequest): |
| 76 raise Exception("Arrays of interface requests not yet supported!") |
74 if kind.spec == 's': | 77 if kind.spec == 's': |
75 return "mojo::String" | 78 return "mojo::String" |
76 if kind.spec == 'h': | 79 if kind.spec == 'h': |
77 return "mojo::ScopedHandle" | 80 return "mojo::ScopedHandle" |
78 if kind.spec == 'h:d:c': | 81 if kind.spec == 'h:d:c': |
79 return "mojo::ScopedDataPipeConsumerHandle" | 82 return "mojo::ScopedDataPipeConsumerHandle" |
80 if kind.spec == 'h:d:p': | 83 if kind.spec == 'h:d:p': |
81 return "mojo::ScopedDataPipeProducerHandle" | 84 return "mojo::ScopedDataPipeProducerHandle" |
82 if kind.spec == 'h:m': | 85 if kind.spec == 'h:m': |
83 return "mojo::ScopedMessagePipeHandle" | 86 return "mojo::ScopedMessagePipeHandle" |
84 if kind.spec == 'h:s': | 87 if kind.spec == 'h:s': |
85 return "mojo::ScopedSharedBufferHandle" | 88 return "mojo::ScopedSharedBufferHandle" |
86 return _kind_to_cpp_type[kind] | 89 return _kind_to_cpp_type[kind] |
87 | 90 |
88 def GetCppResultWrapperType(kind): | 91 def GetCppResultWrapperType(kind): |
89 if isinstance(kind, mojom.Enum): | 92 if isinstance(kind, mojom.Enum): |
90 return GetNameForKind(kind) | 93 return GetNameForKind(kind) |
91 if isinstance(kind, mojom.Struct): | 94 if isinstance(kind, mojom.Struct): |
92 return "%sPtr" % GetNameForKind(kind) | 95 return "%sPtr" % GetNameForKind(kind) |
93 if isinstance(kind, mojom.Array): | 96 if isinstance(kind, mojom.Array): |
94 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind) | 97 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind) |
95 if isinstance(kind, mojom.Interface): | 98 if isinstance(kind, mojom.Interface): |
96 return "%sPtr" % kind.name | 99 return "%sPtr" % kind.name |
| 100 if isinstance(kind, mojom.InterfaceRequest): |
| 101 return "mojo::InterfaceRequest<%s>" % kind.kind.name |
97 if kind.spec == 's': | 102 if kind.spec == 's': |
98 return "mojo::String" | 103 return "mojo::String" |
99 if kind.spec == 'h': | 104 if kind.spec == 'h': |
100 return "mojo::ScopedHandle" | 105 return "mojo::ScopedHandle" |
101 if kind.spec == 'h:d:c': | 106 if kind.spec == 'h:d:c': |
102 return "mojo::ScopedDataPipeConsumerHandle" | 107 return "mojo::ScopedDataPipeConsumerHandle" |
103 if kind.spec == 'h:d:p': | 108 if kind.spec == 'h:d:p': |
104 return "mojo::ScopedDataPipeProducerHandle" | 109 return "mojo::ScopedDataPipeProducerHandle" |
105 if kind.spec == 'h:m': | 110 if kind.spec == 'h:m': |
106 return "mojo::ScopedMessagePipeHandle" | 111 return "mojo::ScopedMessagePipeHandle" |
107 if kind.spec == 'h:s': | 112 if kind.spec == 'h:s': |
108 return "mojo::ScopedSharedBufferHandle" | 113 return "mojo::ScopedSharedBufferHandle" |
109 return _kind_to_cpp_type[kind] | 114 return _kind_to_cpp_type[kind] |
110 | 115 |
111 def GetCppWrapperType(kind): | 116 def GetCppWrapperType(kind): |
112 if isinstance(kind, mojom.Enum): | 117 if isinstance(kind, mojom.Enum): |
113 return GetNameForKind(kind) | 118 return GetNameForKind(kind) |
114 if isinstance(kind, mojom.Struct): | 119 if isinstance(kind, mojom.Struct): |
115 return "%sPtr" % GetNameForKind(kind) | 120 return "%sPtr" % GetNameForKind(kind) |
116 if isinstance(kind, mojom.Array): | 121 if isinstance(kind, mojom.Array): |
117 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind) | 122 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind) |
118 if isinstance(kind, mojom.Interface): | 123 if isinstance(kind, mojom.Interface): |
119 return "mojo::ScopedMessagePipeHandle" | 124 return "mojo::ScopedMessagePipeHandle" |
| 125 if isinstance(kind, mojom.InterfaceRequest): |
| 126 raise Exception("InterfaceRequest fields not supported!") |
120 if kind.spec == 's': | 127 if kind.spec == 's': |
121 return "mojo::String" | 128 return "mojo::String" |
122 if kind.spec == 'h': | 129 if kind.spec == 'h': |
123 return "mojo::ScopedHandle" | 130 return "mojo::ScopedHandle" |
124 if kind.spec == 'h:d:c': | 131 if kind.spec == 'h:d:c': |
125 return "mojo::ScopedDataPipeConsumerHandle" | 132 return "mojo::ScopedDataPipeConsumerHandle" |
126 if kind.spec == 'h:d:p': | 133 if kind.spec == 'h:d:p': |
127 return "mojo::ScopedDataPipeProducerHandle" | 134 return "mojo::ScopedDataPipeProducerHandle" |
128 if kind.spec == 'h:m': | 135 if kind.spec == 'h:m': |
129 return "mojo::ScopedMessagePipeHandle" | 136 return "mojo::ScopedMessagePipeHandle" |
130 if kind.spec == 'h:s': | 137 if kind.spec == 'h:s': |
131 return "mojo::ScopedSharedBufferHandle" | 138 return "mojo::ScopedSharedBufferHandle" |
132 return _kind_to_cpp_type[kind] | 139 return _kind_to_cpp_type[kind] |
133 | 140 |
134 def GetCppConstWrapperType(kind): | 141 def GetCppConstWrapperType(kind): |
135 if isinstance(kind, mojom.Struct): | 142 if isinstance(kind, mojom.Struct): |
136 return "%sPtr" % GetNameForKind(kind) | 143 return "%sPtr" % GetNameForKind(kind) |
137 if isinstance(kind, mojom.Array): | 144 if isinstance(kind, mojom.Array): |
138 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind) | 145 return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind) |
139 if isinstance(kind, mojom.Interface): | 146 if isinstance(kind, mojom.Interface): |
140 return "%sPtr" % kind.name | 147 return "%sPtr" % kind.name |
| 148 if isinstance(kind, mojom.InterfaceRequest): |
| 149 return "mojo::InterfaceRequest<%s>" % kind.kind.name |
141 if isinstance(kind, mojom.Enum): | 150 if isinstance(kind, mojom.Enum): |
142 return GetNameForKind(kind) | 151 return GetNameForKind(kind) |
143 if kind.spec == 's': | 152 if kind.spec == 's': |
144 return "const mojo::String&" | 153 return "const mojo::String&" |
145 if kind.spec == 'h': | 154 if kind.spec == 'h': |
146 return "mojo::ScopedHandle" | 155 return "mojo::ScopedHandle" |
147 if kind.spec == 'h:d:c': | 156 if kind.spec == 'h:d:c': |
148 return "mojo::ScopedDataPipeConsumerHandle" | 157 return "mojo::ScopedDataPipeConsumerHandle" |
149 if kind.spec == 'h:d:p': | 158 if kind.spec == 'h:d:p': |
150 return "mojo::ScopedDataPipeProducerHandle" | 159 return "mojo::ScopedDataPipeProducerHandle" |
151 if kind.spec == 'h:m': | 160 if kind.spec == 'h:m': |
152 return "mojo::ScopedMessagePipeHandle" | 161 return "mojo::ScopedMessagePipeHandle" |
153 if kind.spec == 'h:s': | 162 if kind.spec == 'h:s': |
154 return "mojo::ScopedSharedBufferHandle" | 163 return "mojo::ScopedSharedBufferHandle" |
155 if not kind in _kind_to_cpp_type: | 164 if not kind in _kind_to_cpp_type: |
156 print "missing:", kind.spec | 165 print "missing:", kind.spec |
157 return _kind_to_cpp_type[kind] | 166 return _kind_to_cpp_type[kind] |
158 | 167 |
159 def GetCppFieldType(kind): | 168 def GetCppFieldType(kind): |
160 if isinstance(kind, mojom.Struct): | 169 if isinstance(kind, mojom.Struct): |
161 return ("mojo::internal::StructPointer<%s_Data>" % | 170 return ("mojo::internal::StructPointer<%s_Data>" % |
162 GetNameForKind(kind, internal=True)) | 171 GetNameForKind(kind, internal=True)) |
163 if isinstance(kind, mojom.Array): | 172 if isinstance(kind, mojom.Array): |
164 return "mojo::internal::ArrayPointer<%s>" % GetCppType(kind.kind) | 173 return "mojo::internal::ArrayPointer<%s>" % GetCppType(kind.kind) |
165 if isinstance(kind, mojom.Interface): | 174 if isinstance(kind, mojom.Interface) or \ |
| 175 isinstance(kind, mojom.InterfaceRequest): |
166 return "mojo::MessagePipeHandle" | 176 return "mojo::MessagePipeHandle" |
167 if isinstance(kind, mojom.Enum): | 177 if isinstance(kind, mojom.Enum): |
168 return GetNameForKind(kind) | 178 return GetNameForKind(kind) |
169 if kind.spec == 's': | 179 if kind.spec == 's': |
170 return "mojo::internal::StringPointer" | 180 return "mojo::internal::StringPointer" |
171 return _kind_to_cpp_type[kind] | 181 return _kind_to_cpp_type[kind] |
172 | 182 |
173 def IsStructWithHandles(struct): | 183 def IsStructWithHandles(struct): |
174 for pf in struct.packed.packed_fields: | 184 for pf in struct.packed.packed_fields: |
175 if generator.IsHandleKind(pf.field.kind): | 185 if generator.IsHandleKind(pf.field.kind): |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 "cpp_type": GetCppType, | 232 "cpp_type": GetCppType, |
223 "cpp_wrapper_type": GetCppWrapperType, | 233 "cpp_wrapper_type": GetCppWrapperType, |
224 "expression_to_text": ExpressionToText, | 234 "expression_to_text": ExpressionToText, |
225 "get_pad": pack.GetPad, | 235 "get_pad": pack.GetPad, |
226 "has_callbacks": HasCallbacks, | 236 "has_callbacks": HasCallbacks, |
227 "should_inline": ShouldInlineStruct, | 237 "should_inline": ShouldInlineStruct, |
228 "is_enum_kind": generator.IsEnumKind, | 238 "is_enum_kind": generator.IsEnumKind, |
229 "is_move_only_kind": generator.IsMoveOnlyKind, | 239 "is_move_only_kind": generator.IsMoveOnlyKind, |
230 "is_handle_kind": generator.IsHandleKind, | 240 "is_handle_kind": generator.IsHandleKind, |
231 "is_interface_kind": generator.IsInterfaceKind, | 241 "is_interface_kind": generator.IsInterfaceKind, |
| 242 "is_interface_request_kind": generator.IsInterfaceRequestKind, |
232 "is_object_kind": generator.IsObjectKind, | 243 "is_object_kind": generator.IsObjectKind, |
233 "is_string_kind": generator.IsStringKind, | 244 "is_string_kind": generator.IsStringKind, |
234 "is_array_kind": lambda kind: isinstance(kind, mojom.Array), | 245 "is_array_kind": lambda kind: isinstance(kind, mojom.Array), |
235 "is_struct_with_handles": IsStructWithHandles, | 246 "is_struct_with_handles": IsStructWithHandles, |
236 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, | 247 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, |
237 "struct_from_method": generator.GetStructFromMethod, | 248 "struct_from_method": generator.GetStructFromMethod, |
238 "response_struct_from_method": generator.GetResponseStructFromMethod, | 249 "response_struct_from_method": generator.GetResponseStructFromMethod, |
239 "stylize_method": generator.StudlyCapsToCamel, | 250 "stylize_method": generator.StudlyCapsToCamel, |
240 "verify_token_type": generator.VerifyTokenType, | 251 "verify_token_type": generator.VerifyTokenType, |
241 } | 252 } |
(...skipping 20 matching lines...) Expand all Loading... |
262 | 273 |
263 @UseJinja("cpp_templates/module.cc.tmpl", filters=cpp_filters) | 274 @UseJinja("cpp_templates/module.cc.tmpl", filters=cpp_filters) |
264 def GenerateModuleSource(self): | 275 def GenerateModuleSource(self): |
265 return self.GetJinjaExports() | 276 return self.GetJinjaExports() |
266 | 277 |
267 def GenerateFiles(self, args): | 278 def GenerateFiles(self, args): |
268 self.Write(self.GenerateModuleHeader(), "%s.h" % self.module.name) | 279 self.Write(self.GenerateModuleHeader(), "%s.h" % self.module.name) |
269 self.Write(self.GenerateModuleInternalHeader(), | 280 self.Write(self.GenerateModuleInternalHeader(), |
270 "%s-internal.h" % self.module.name) | 281 "%s-internal.h" % self.module.name) |
271 self.Write(self.GenerateModuleSource(), "%s.cc" % self.module.name) | 282 self.Write(self.GenerateModuleSource(), "%s.cc" % self.module.name) |
OLD | NEW |