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 # This module's classes provide an interface to mojo modules. Modules are | 5 # This module's classes provide an interface to mojo modules. Modules are |
6 # collections of interfaces and structs to be used by mojo ipc clients and | 6 # collections of interfaces and structs to be used by mojo ipc clients and |
7 # servers. | 7 # servers. |
8 # | 8 # |
9 # A simple interface would be created this way: | 9 # A simple interface would be created this way: |
10 # module = mojom.generate.module.Module('Foo') | 10 # module = mojom.generate.module.Module('Foo') |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 obj.__class__.__name__, | 75 obj.__class__.__name__, |
76 ',\n'.join(ReprIndent(name, as_ref) | 76 ',\n'.join(ReprIndent(name, as_ref) |
77 for (name, as_ref) in names.iteritems())) | 77 for (name, as_ref) in names.iteritems())) |
78 | 78 |
79 | 79 |
80 class Kind(object): | 80 class Kind(object): |
81 """Kind represents a type (e.g. int8, string). | 81 """Kind represents a type (e.g. int8, string). |
82 | 82 |
83 Attributes: | 83 Attributes: |
84 spec: A string uniquely identifying the type. May be None. | 84 spec: A string uniquely identifying the type. May be None. |
| 85 module: {Module} The defining module. Set to None for built-in types. |
85 parent_kind: The enclosing type. For example, a struct defined | 86 parent_kind: The enclosing type. For example, a struct defined |
86 inside an interface has that interface as its parent. May be None. | 87 inside an interface has that interface as its parent. May be None. |
87 """ | 88 """ |
88 def __init__(self, spec=None): | 89 def __init__(self, spec=None, module=None): |
89 self.spec = spec | 90 self.spec = spec |
| 91 self.module = module |
90 self.parent_kind = None | 92 self.parent_kind = None |
91 | 93 |
92 def Repr(self, as_ref=True): | 94 def Repr(self, as_ref=True): |
93 return '<%s spec=%r>' % (self.__class__.__name__, self.spec) | 95 return '<%s spec=%r>' % (self.__class__.__name__, self.spec) |
94 | 96 |
95 def __repr__(self): | 97 def __repr__(self): |
96 # Gives us a decent __repr__ for all kinds. | 98 # Gives us a decent __repr__ for all kinds. |
97 return self.Repr() | 99 return self.Repr() |
98 | 100 |
99 | 101 |
100 class ReferenceKind(Kind): | 102 class ReferenceKind(Kind): |
101 """ReferenceKind represents pointer and handle types. | 103 """ReferenceKind represents pointer and handle types. |
102 | 104 |
103 A type is nullable if null (for pointer types) or invalid handle (for handle | 105 A type is nullable if null (for pointer types) or invalid handle (for handle |
104 types) is a legal value for the type. | 106 types) is a legal value for the type. |
105 | 107 |
106 Attributes: | 108 Attributes: |
107 is_nullable: True if the type is nullable. | 109 is_nullable: True if the type is nullable. |
108 """ | 110 """ |
109 | 111 def __init__(self, spec=None, is_nullable=False, module=None): |
110 def __init__(self, spec=None, is_nullable=False): | |
111 assert spec is None or is_nullable == spec.startswith('?') | 112 assert spec is None or is_nullable == spec.startswith('?') |
112 Kind.__init__(self, spec) | 113 Kind.__init__(self, spec, module) |
113 self.is_nullable = is_nullable | 114 self.is_nullable = is_nullable |
114 self.shared_definition = {} | 115 self.shared_definition = {} |
115 | 116 |
116 def Repr(self, as_ref=True): | 117 def Repr(self, as_ref=True): |
117 return '<%s spec=%r is_nullable=%r>' % (self.__class__.__name__, self.spec, | 118 return '<%s spec=%r is_nullable=%r>' % (self.__class__.__name__, self.spec, |
118 self.is_nullable) | 119 self.is_nullable) |
119 | 120 |
120 def MakeNullableKind(self): | 121 def MakeNullableKind(self): |
121 assert not self.is_nullable | 122 assert not self.is_nullable |
122 | 123 |
123 if self == STRING: | 124 if self == STRING: |
124 return NULLABLE_STRING | 125 return NULLABLE_STRING |
125 if self == HANDLE: | 126 if self == HANDLE: |
126 return NULLABLE_HANDLE | 127 return NULLABLE_HANDLE |
127 if self == DCPIPE: | 128 if self == DCPIPE: |
128 return NULLABLE_DCPIPE | 129 return NULLABLE_DCPIPE |
129 if self == DPPIPE: | 130 if self == DPPIPE: |
130 return NULLABLE_DPPIPE | 131 return NULLABLE_DPPIPE |
131 if self == MSGPIPE: | 132 if self == MSGPIPE: |
132 return NULLABLE_MSGPIPE | 133 return NULLABLE_MSGPIPE |
133 if self == SHAREDBUFFER: | 134 if self == SHAREDBUFFER: |
134 return NULLABLE_SHAREDBUFFER | 135 return NULLABLE_SHAREDBUFFER |
135 | 136 |
136 nullable_kind = type(self)() | 137 nullable_kind = type(self)() |
137 nullable_kind.shared_definition = self.shared_definition | 138 nullable_kind.shared_definition = self.shared_definition |
138 if self.spec is not None: | 139 if self.spec is not None: |
139 nullable_kind.spec = '?' + self.spec | 140 nullable_kind.spec = '?' + self.spec |
140 nullable_kind.is_nullable = True | 141 nullable_kind.is_nullable = True |
| 142 nullable_kind.parent_kind = self.parent_kind |
| 143 nullable_kind.module = self.module |
141 | 144 |
142 return nullable_kind | 145 return nullable_kind |
143 | 146 |
144 @classmethod | 147 @classmethod |
145 def AddSharedProperty(cls, name): | 148 def AddSharedProperty(cls, name): |
146 """Adds a property |name| to |cls|, which accesses the corresponding item in | 149 """Adds a property |name| to |cls|, which accesses the corresponding item in |
147 |shared_definition|. | 150 |shared_definition|. |
148 | 151 |
149 The reason of adding such indirection is to enable sharing definition | 152 The reason of adding such indirection is to enable sharing definition |
150 between a reference kind and its nullable variation. For example: | 153 between a reference kind and its nullable variation. For example: |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 | 220 |
218 | 221 |
219 ATTRIBUTE_MIN_VERSION = 'MinVersion' | 222 ATTRIBUTE_MIN_VERSION = 'MinVersion' |
220 ATTRIBUTE_EXTENSIBLE = 'Extensible' | 223 ATTRIBUTE_EXTENSIBLE = 'Extensible' |
221 ATTRIBUTE_SYNC = 'Sync' | 224 ATTRIBUTE_SYNC = 'Sync' |
222 | 225 |
223 | 226 |
224 class NamedValue(object): | 227 class NamedValue(object): |
225 def __init__(self, module, parent_kind, name): | 228 def __init__(self, module, parent_kind, name): |
226 self.module = module | 229 self.module = module |
227 self.namespace = module.namespace | |
228 self.parent_kind = parent_kind | 230 self.parent_kind = parent_kind |
229 self.name = name | 231 self.name = name |
230 self.imported_from = None | |
231 | 232 |
232 def GetSpec(self): | 233 def GetSpec(self): |
233 return (self.namespace + '.' + | 234 return (self.module.namespace + '.' + |
234 (self.parent_kind and (self.parent_kind.name + '.') or "") + | 235 (self.parent_kind and (self.parent_kind.name + '.') or "") + |
235 self.name) | 236 self.name) |
236 | 237 |
237 | 238 |
238 class BuiltinValue(object): | 239 class BuiltinValue(object): |
239 def __init__(self, value): | 240 def __init__(self, value): |
240 self.value = value | 241 self.value = value |
241 | 242 |
242 | 243 |
243 class ConstantValue(NamedValue): | 244 class ConstantValue(NamedValue): |
244 def __init__(self, module, parent_kind, constant): | 245 def __init__(self, module, parent_kind, constant): |
245 NamedValue.__init__(self, module, parent_kind, constant.name) | 246 NamedValue.__init__(self, module, parent_kind, constant.name) |
246 self.constant = constant | 247 self.constant = constant |
247 | 248 |
248 | 249 |
249 class EnumValue(NamedValue): | 250 class EnumValue(NamedValue): |
250 def __init__(self, module, enum, field): | 251 def __init__(self, module, enum, field): |
251 NamedValue.__init__(self, module, enum.parent_kind, field.name) | 252 NamedValue.__init__(self, module, enum.parent_kind, field.name) |
252 self.enum = enum | 253 self.enum = enum |
253 | 254 |
254 def GetSpec(self): | 255 def GetSpec(self): |
255 return (self.namespace + '.' + | 256 return (self.module.namespace + '.' + |
256 (self.parent_kind and (self.parent_kind.name + '.') or "") + | 257 (self.parent_kind and (self.parent_kind.name + '.') or "") + |
257 self.enum.name + '.' + self.name) | 258 self.enum.name + '.' + self.name) |
258 | 259 |
259 | 260 |
260 class Constant(object): | 261 class Constant(object): |
261 def __init__(self, name=None, kind=None, value=None, parent_kind=None): | 262 def __init__(self, name=None, kind=None, value=None, parent_kind=None): |
262 self.name = name | 263 self.name = name |
263 self.kind = kind | 264 self.kind = kind |
264 self.value = value | 265 self.value = value |
265 self.parent_kind = parent_kind | 266 self.parent_kind = parent_kind |
(...skipping 27 matching lines...) Expand all Loading... |
293 class UnionField(Field): pass | 294 class UnionField(Field): pass |
294 | 295 |
295 | 296 |
296 class Struct(ReferenceKind): | 297 class Struct(ReferenceKind): |
297 """A struct with typed fields. | 298 """A struct with typed fields. |
298 | 299 |
299 Attributes: | 300 Attributes: |
300 name: {str} The name of the struct type. | 301 name: {str} The name of the struct type. |
301 native_only: {bool} Does the struct have a body (i.e. any fields) or is it | 302 native_only: {bool} Does the struct have a body (i.e. any fields) or is it |
302 purely a native struct. | 303 purely a native struct. |
303 module: {Module} The defining module. | |
304 imported_from: {dict} Information about where this union was | |
305 imported from. | |
306 fields: {List[StructField]} The members of the struct. | 304 fields: {List[StructField]} The members of the struct. |
307 attributes: {dict} Additional information about the struct, such as | 305 attributes: {dict} Additional information about the struct, such as |
308 if it's a native struct. | 306 if it's a native struct. |
309 """ | 307 """ |
310 | 308 |
311 ReferenceKind.AddSharedProperty('name') | 309 ReferenceKind.AddSharedProperty('name') |
312 ReferenceKind.AddSharedProperty('native_only') | 310 ReferenceKind.AddSharedProperty('native_only') |
313 ReferenceKind.AddSharedProperty('module') | |
314 ReferenceKind.AddSharedProperty('imported_from') | |
315 ReferenceKind.AddSharedProperty('fields') | 311 ReferenceKind.AddSharedProperty('fields') |
316 ReferenceKind.AddSharedProperty('attributes') | 312 ReferenceKind.AddSharedProperty('attributes') |
317 | 313 |
318 def __init__(self, name=None, module=None, attributes=None): | 314 def __init__(self, name=None, module=None, attributes=None): |
319 if name is not None: | 315 if name is not None: |
320 spec = 'x:' + name | 316 spec = 'x:' + name |
321 else: | 317 else: |
322 spec = None | 318 spec = None |
323 ReferenceKind.__init__(self, spec) | 319 ReferenceKind.__init__(self, spec, False, module) |
324 self.name = name | 320 self.name = name |
325 self.native_only = False | 321 self.native_only = False |
326 self.module = module | |
327 self.imported_from = None | |
328 self.fields = [] | 322 self.fields = [] |
329 self.attributes = attributes | 323 self.attributes = attributes |
330 | 324 |
331 def Repr(self, as_ref=True): | 325 def Repr(self, as_ref=True): |
332 if as_ref: | 326 if as_ref: |
333 return '<%s name=%r imported_from=%s>' % ( | 327 return '<%s name=%r module=%s>' % ( |
334 self.__class__.__name__, self.name, | 328 self.__class__.__name__, self.name, |
335 Repr(self.imported_from, as_ref=True)) | 329 Repr(self.module, as_ref=True)) |
336 else: | 330 else: |
337 return GenericRepr(self, {'name': False, 'fields': False, | 331 return GenericRepr(self, {'name': False, 'fields': False, 'module': True}) |
338 'imported_from': True}) | |
339 | 332 |
340 def AddField(self, name, kind, ordinal=None, default=None, attributes=None): | 333 def AddField(self, name, kind, ordinal=None, default=None, attributes=None): |
341 field = StructField(name, kind, ordinal, default, attributes) | 334 field = StructField(name, kind, ordinal, default, attributes) |
342 self.fields.append(field) | 335 self.fields.append(field) |
343 return field | 336 return field |
344 | 337 |
345 | 338 |
346 class Union(ReferenceKind): | 339 class Union(ReferenceKind): |
347 """A union of several kinds. | 340 """A union of several kinds. |
348 | 341 |
349 Attributes: | 342 Attributes: |
350 name: {str} The name of the union type. | 343 name: {str} The name of the union type. |
351 module: {Module} The defining module. | |
352 imported_from: {dict} Information about where this union was | |
353 imported from. | |
354 fields: {List[UnionField]} The members of the union. | 344 fields: {List[UnionField]} The members of the union. |
355 attributes: {dict} Additional information about the union, such as | 345 attributes: {dict} Additional information about the union, such as |
356 which Java class name to use to represent it in the generated | 346 which Java class name to use to represent it in the generated |
357 bindings. | 347 bindings. |
358 """ | 348 """ |
359 ReferenceKind.AddSharedProperty('name') | 349 ReferenceKind.AddSharedProperty('name') |
360 ReferenceKind.AddSharedProperty('module') | |
361 ReferenceKind.AddSharedProperty('imported_from') | |
362 ReferenceKind.AddSharedProperty('fields') | 350 ReferenceKind.AddSharedProperty('fields') |
363 ReferenceKind.AddSharedProperty('attributes') | 351 ReferenceKind.AddSharedProperty('attributes') |
364 | 352 |
365 def __init__(self, name=None, module=None, attributes=None): | 353 def __init__(self, name=None, module=None, attributes=None): |
366 if name is not None: | 354 if name is not None: |
367 spec = 'x:' + name | 355 spec = 'x:' + name |
368 else: | 356 else: |
369 spec = None | 357 spec = None |
370 ReferenceKind.__init__(self, spec) | 358 ReferenceKind.__init__(self, spec, False, module) |
371 self.name = name | 359 self.name = name |
372 self.module = module | |
373 self.imported_from = None | |
374 self.fields = [] | 360 self.fields = [] |
375 self.attributes = attributes | 361 self.attributes = attributes |
376 | 362 |
377 def Repr(self, as_ref=True): | 363 def Repr(self, as_ref=True): |
378 if as_ref: | 364 if as_ref: |
379 return '<%s spec=%r is_nullable=%r fields=%s>' % ( | 365 return '<%s spec=%r is_nullable=%r fields=%s>' % ( |
380 self.__class__.__name__, self.spec, self.is_nullable, | 366 self.__class__.__name__, self.spec, self.is_nullable, |
381 Repr(self.fields)) | 367 Repr(self.fields)) |
382 else: | 368 else: |
383 return GenericRepr(self, {'fields': True, 'is_nullable': False}) | 369 return GenericRepr(self, {'fields': True, 'is_nullable': False}) |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 return self.attributes.get(ATTRIBUTE_MIN_VERSION) \ | 530 return self.attributes.get(ATTRIBUTE_MIN_VERSION) \ |
545 if self.attributes else None | 531 if self.attributes else None |
546 | 532 |
547 @property | 533 @property |
548 def sync(self): | 534 def sync(self): |
549 return self.attributes.get(ATTRIBUTE_SYNC) \ | 535 return self.attributes.get(ATTRIBUTE_SYNC) \ |
550 if self.attributes else None | 536 if self.attributes else None |
551 | 537 |
552 | 538 |
553 class Interface(ReferenceKind): | 539 class Interface(ReferenceKind): |
554 ReferenceKind.AddSharedProperty('module') | |
555 ReferenceKind.AddSharedProperty('name') | 540 ReferenceKind.AddSharedProperty('name') |
556 ReferenceKind.AddSharedProperty('imported_from') | |
557 ReferenceKind.AddSharedProperty('methods') | 541 ReferenceKind.AddSharedProperty('methods') |
558 ReferenceKind.AddSharedProperty('attributes') | 542 ReferenceKind.AddSharedProperty('attributes') |
559 | 543 |
560 def __init__(self, name=None, module=None, attributes=None): | 544 def __init__(self, name=None, module=None, attributes=None): |
561 if name is not None: | 545 if name is not None: |
562 spec = 'x:' + name | 546 spec = 'x:' + name |
563 else: | 547 else: |
564 spec = None | 548 spec = None |
565 ReferenceKind.__init__(self, spec) | 549 ReferenceKind.__init__(self, spec, False, module) |
566 self.module = module | |
567 self.name = name | 550 self.name = name |
568 self.imported_from = None | |
569 self.methods = [] | 551 self.methods = [] |
570 self.attributes = attributes | 552 self.attributes = attributes |
571 | 553 |
572 def Repr(self, as_ref=True): | 554 def Repr(self, as_ref=True): |
573 if as_ref: | 555 if as_ref: |
574 return '<%s name=%r>' % (self.__class__.__name__, self.name) | 556 return '<%s name=%r>' % (self.__class__.__name__, self.name) |
575 else: | 557 else: |
576 return GenericRepr(self, {'name': False, 'attributes': False, | 558 return GenericRepr(self, {'name': False, 'attributes': False, |
577 'methods': False}) | 559 'methods': False}) |
578 | 560 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
611 self.numeric_value = numeric_value | 593 self.numeric_value = numeric_value |
612 | 594 |
613 @property | 595 @property |
614 def min_version(self): | 596 def min_version(self): |
615 return self.attributes.get(ATTRIBUTE_MIN_VERSION) \ | 597 return self.attributes.get(ATTRIBUTE_MIN_VERSION) \ |
616 if self.attributes else None | 598 if self.attributes else None |
617 | 599 |
618 | 600 |
619 class Enum(Kind): | 601 class Enum(Kind): |
620 def __init__(self, name=None, module=None, attributes=None): | 602 def __init__(self, name=None, module=None, attributes=None): |
621 self.module = module | |
622 self.name = name | 603 self.name = name |
623 self.native_only = False | 604 self.native_only = False |
624 self.imported_from = None | |
625 if name is not None: | 605 if name is not None: |
626 spec = 'x:' + name | 606 spec = 'x:' + name |
627 else: | 607 else: |
628 spec = None | 608 spec = None |
629 Kind.__init__(self, spec) | 609 Kind.__init__(self, spec, module) |
630 self.fields = [] | 610 self.fields = [] |
631 self.attributes = attributes | 611 self.attributes = attributes |
632 | 612 |
633 def Repr(self, as_ref=True): | 613 def Repr(self, as_ref=True): |
634 if as_ref: | 614 if as_ref: |
635 return '<%s name=%r>' % (self.__class__.__name__, self.name) | 615 return '<%s name=%r>' % (self.__class__.__name__, self.name) |
636 else: | 616 else: |
637 return GenericRepr(self, {'name': False, 'fields': False}) | 617 return GenericRepr(self, {'name': False, 'fields': False}) |
638 | 618 |
639 @property | 619 @property |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
893 return True | 873 return True |
894 elif IsAnyInterfaceKind(kind): | 874 elif IsAnyInterfaceKind(kind): |
895 return True | 875 return True |
896 elif IsArrayKind(kind): | 876 elif IsArrayKind(kind): |
897 return Check(kind.kind) | 877 return Check(kind.kind) |
898 elif IsMapKind(kind): | 878 elif IsMapKind(kind): |
899 return Check(kind.key_kind) or Check(kind.value_kind) | 879 return Check(kind.key_kind) or Check(kind.value_kind) |
900 else: | 880 else: |
901 return False | 881 return False |
902 return Check(kind) | 882 return Check(kind) |
OLD | NEW |