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

Unified Diff: mojo/public/tools/bindings/pylib/mojom/generate/data.py

Issue 268363003: Mojo: Add support for constants to the IDL compiler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: JS export constants Created 6 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: mojo/public/tools/bindings/pylib/mojom/generate/data.py
diff --git a/mojo/public/tools/bindings/pylib/mojom/generate/data.py b/mojo/public/tools/bindings/pylib/mojom/generate/data.py
index cbe65a7c51d1b375866493ff1fa887d82f0c3451..aa7ddcaca335fd7ba65e81f62a35678996079c12 100644
--- a/mojo/public/tools/bindings/pylib/mojom/generate/data.py
+++ b/mojo/public/tools/bindings/pylib/mojom/generate/data.py
@@ -65,17 +65,17 @@ def LookupKind(kinds, spec, scope):
return kinds.get(spec)
-def LookupConstant(constants, name, scope):
- """Like LookupKind, but for constants."""
+def LookupValue(values, name, scope):
+ """Like LookupKind, but for constant values."""
for i in xrange(len(scope), -1, -1):
if i > 0:
test_spec = '.'.join(scope[:i]) + '.'
test_spec += name
- constant = constants.get(test_spec)
- if constant:
- return constant
+ value = values.get(test_spec)
+ if value:
+ return value
- return constants.get(name)
+ return values.get(name)
def KindToData(kind):
return kind.spec
@@ -114,12 +114,12 @@ def ImportFromData(module, data):
kind.imported_from is None):
kind = KindFromImport(kind, import_item)
module.kinds[kind.spec] = kind
- # Ditto for constants.
- for constant in import_module.constants.itervalues():
- if constant.imported_from is None:
- constant = copy.deepcopy(constant)
- constant.imported_from = import_item
- module.constants[constant.GetSpec()] = constant
+ # Ditto for values.
+ for value in import_module.values.itervalues():
+ if value.imported_from is None:
+ value = copy.deepcopy(value)
+ value.imported_from = import_item
+ module.values[value.GetSpec()] = value
return import_item
@@ -137,6 +137,8 @@ def StructFromData(module, data):
module.kinds[struct.spec] = struct
struct.enums = map(lambda enum:
EnumFromData(module, enum, struct), data['enums'])
+ struct.constants = map(lambda constant:
+ ConstantFromData(module, constant, struct), data['constants'])
# Stash fields data here temporarily.
struct.fields_data = data['fields']
return struct
@@ -160,9 +162,9 @@ def FixupExpression(module, value, scope):
else:
value[i] = FixupExpression(module, value[i], scope)
elif value:
- constant = LookupConstant(module.constants, value, scope)
- if constant:
- return constant
+ result = LookupValue(module.values, value, scope)
+ if result:
+ return result
return value
def FieldFromData(module, data, struct):
@@ -235,6 +237,8 @@ def InterfaceFromData(module, data):
module.kinds[interface.spec] = interface
interface.enums = map(lambda enum:
EnumFromData(module, enum, interface), data['enums'])
+ interface.constants = map(lambda constant:
+ ConstantFromData(module, constant, interface), data['constants'])
# Stash methods data here temporarily.
interface.methods_data = data['methods']
return interface
@@ -242,14 +246,18 @@ def InterfaceFromData(module, data):
def EnumFieldFromData(module, enum, data, parent_kind):
field = mojom.EnumField()
field.name = data['name']
+ # TODO(mpcomplete): FixupExpression should be done in the second pass,
+ # so constants and enums can refer to each other.
+ # TODO(mpcomplete): But then, what if constants are initialized to an enum? Or
+ # vice versa?
if parent_kind:
field.value = FixupExpression(
module, data['value'], (module.namespace, parent_kind.name))
else:
field.value = FixupExpression(
module, data['value'], (module.namespace, ))
- constant = mojom.Constant(module, enum, field)
- module.constants[constant.GetSpec()] = constant
+ value = mojom.EnumValue(module, enum, field)
+ module.values[value.GetSpec()] = value
return field
def EnumFromData(module, data, parent_kind):
@@ -267,6 +275,21 @@ def EnumFromData(module, data, parent_kind):
module.kinds[enum.spec] = enum
return enum
+def ConstantFromData(module, data, parent_kind):
+ constant = mojom.Constant()
+ constant.name = data['name']
+ if parent_kind:
+ scope = (module.namespace, parent_kind.name)
+ else:
+ scope = (module.namespace, )
+ # TODO(mpcomplete): maybe we should only support POD kinds.
+ constant.kind = KindFromData(module.kinds, data['kind'], scope)
+ constant.value = FixupExpression(module, data.get('value'), scope)
+
+ value = mojom.NamedValue(module, parent_kind, constant.name)
+ module.values[value.GetSpec()] = value
+ return constant
+
def ModuleToData(module):
return {
istr(0, 'name'): module.name,
@@ -281,7 +304,7 @@ def ModuleFromData(data):
for kind in mojom.PRIMITIVES:
module.kinds[kind.spec] = kind
- module.constants = {}
+ module.values = {}
module.name = data['name']
module.namespace = data['namespace']
@@ -300,6 +323,9 @@ def ModuleFromData(data):
module.interfaces = map(
lambda interface: InterfaceFromData(module, interface),
data['interfaces'])
+ module.constants = map(
+ lambda constant: ConstantFromData(module, constant, None),
+ data['constants'])
# Second pass expands fields and methods. This allows fields and parameters
# to refer to kinds defined anywhere in the mojom.
« no previous file with comments | « mojo/public/tools/bindings/generators/mojom_js_generator.py ('k') | mojo/public/tools/bindings/pylib/mojom/generate/module.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698