Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: mojo/public/tools/bindings/pylib/mojom/generate/module.py

Issue 2339413004: Allow Mojo structs as map keys (Closed)
Patch Set: Fix hash unit test on Windows Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/tools/bindings/generators/mojom_cpp_generator.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 if self.attributes else None 287 if self.attributes else None
288 288
289 289
290 class StructField(Field): pass 290 class StructField(Field): pass
291 291
292 292
293 class UnionField(Field): pass 293 class UnionField(Field): pass
294 294
295 295
296 class Struct(ReferenceKind): 296 class Struct(ReferenceKind):
297 """A struct with typed fields.
298
299 Attributes:
300 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 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 union.
307 attributes: {dict} Additional information about the struct, such as
308 if it's a native struct.
309 """
310
297 ReferenceKind.AddSharedProperty('name') 311 ReferenceKind.AddSharedProperty('name')
298 ReferenceKind.AddSharedProperty('native_only') 312 ReferenceKind.AddSharedProperty('native_only')
299 ReferenceKind.AddSharedProperty('module') 313 ReferenceKind.AddSharedProperty('module')
300 ReferenceKind.AddSharedProperty('imported_from') 314 ReferenceKind.AddSharedProperty('imported_from')
301 ReferenceKind.AddSharedProperty('fields') 315 ReferenceKind.AddSharedProperty('fields')
302 ReferenceKind.AddSharedProperty('attributes') 316 ReferenceKind.AddSharedProperty('attributes')
303 317
304 def __init__(self, name=None, module=None, attributes=None): 318 def __init__(self, name=None, module=None, attributes=None):
305 if name is not None: 319 if name is not None:
306 spec = 'x:' + name 320 spec = 'x:' + name
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 ReferenceKind.AddSharedProperty('key_kind') 432 ReferenceKind.AddSharedProperty('key_kind')
419 ReferenceKind.AddSharedProperty('value_kind') 433 ReferenceKind.AddSharedProperty('value_kind')
420 434
421 def __init__(self, key_kind=None, value_kind=None): 435 def __init__(self, key_kind=None, value_kind=None):
422 if (key_kind is not None and value_kind is not None): 436 if (key_kind is not None and value_kind is not None):
423 ReferenceKind.__init__(self, 437 ReferenceKind.__init__(self,
424 'm[' + key_kind.spec + '][' + value_kind.spec + 438 'm[' + key_kind.spec + '][' + value_kind.spec +
425 ']') 439 ']')
426 if IsNullableKind(key_kind): 440 if IsNullableKind(key_kind):
427 raise Exception("Nullable kinds cannot be keys in maps.") 441 raise Exception("Nullable kinds cannot be keys in maps.")
428 if IsStructKind(key_kind):
429 # TODO(erg): It would sometimes be nice if we could key on struct
430 # values. However, what happens if the struct has a handle in it? Or
431 # non-copyable data like an array?
432 raise Exception("Structs cannot be keys in maps.")
433 if IsAnyHandleKind(key_kind): 442 if IsAnyHandleKind(key_kind):
434 raise Exception("Handles cannot be keys in maps.") 443 raise Exception("Handles cannot be keys in maps.")
435 if IsAnyInterfaceKind(key_kind): 444 if IsAnyInterfaceKind(key_kind):
436 raise Exception("Interfaces cannot be keys in maps.") 445 raise Exception("Interfaces cannot be keys in maps.")
437 if IsArrayKind(key_kind): 446 if IsArrayKind(key_kind):
438 raise Exception("Arrays cannot be keys in maps.") 447 raise Exception("Arrays cannot be keys in maps.")
439 else: 448 else:
440 ReferenceKind.__init__(self) 449 ReferenceKind.__init__(self)
441 450
442 self.key_kind = key_kind 451 self.key_kind = key_kind
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after
833 if _ContainsAssociatedKinds(param.kind, visited_kinds): 842 if _ContainsAssociatedKinds(param.kind, visited_kinds):
834 return True 843 return True
835 return False 844 return False
836 845
837 846
838 def HasSyncMethods(interface): 847 def HasSyncMethods(interface):
839 for method in interface.methods: 848 for method in interface.methods:
840 if method.sync: 849 if method.sync:
841 return True 850 return True
842 return False 851 return False
852
853
854 def ContainsHandlesOrInterfaces(kind):
855 """Check if the kind contains any handles.
856
857 This check is recursive so it checks all struct fields, containers elements,
858 etc.
859
860 Args:
861 struct: {Kind} The kind to check.
862
863 Returns:
864 {bool}: True if the kind contains handles.
865 """
866 # We remember the types we already checked to avoid infinite recursion when
867 # checking recursive (or mutually recursive) types:
868 checked = set()
869 def Check(kind):
870 if kind.spec in checked:
871 return False
872 checked.add(kind.spec)
873 if IsStructKind(kind):
874 return any(Check(field.kind) for field in kind.fields)
875 elif IsUnionKind(kind):
876 return any(Check(field.kind) for field in kind.fields)
877 elif IsAnyHandleKind(kind):
878 return True
879 elif IsAnyInterfaceKind(kind):
880 return True
881 elif IsArrayKind(kind):
882 return Check(kind.kind)
883 elif IsMapKind(kind):
884 return Check(kind.key_kind) or Check(kind.value_kind)
885 else:
886 return False
887 return Check(kind)
OLDNEW
« no previous file with comments | « mojo/public/tools/bindings/generators/mojom_cpp_generator.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698