Replace the generated json string with a literal qobject. The later is
easier to deal with, at run time as well as compile time: adding #if
conditionals will be easier than in a json string.
Signed-off-by: Marc-André Lureau
Reviewed-by: Markus Armbruster
---
scripts/qapi-introspect.py | 83 ++
monitor.c | 2 +-
tests/test-qobject-input-visitor.c | 11 +++--
docs/devel/qapi-code-gen.txt | 29 -
4 files changed, 76 insertions(+), 49 deletions(-)
diff --git a/scripts/qapi-introspect.py b/scripts/qapi-introspect.py
index 032bcea491..0002bc1a68 100644
--- a/scripts/qapi-introspect.py
+++ b/scripts/qapi-introspect.py
@@ -12,26 +12,36 @@
from qapi import *
-# Caveman's json.dumps() replacement (we're stuck at Python 2.4)
-# TODO try to use json.dumps() once we get unstuck
-def to_json(obj, level=0):
+def to_qlit(obj, level=0, suppress_first_indent=False):
+
+def indent(level):
+return level * 4 * ' '
+
+ret = ''
+if not suppress_first_indent:
+ret += indent(level)
if obj is None:
-ret = 'null'
+ret += 'QLIT_QNULL'
elif isinstance(obj, str):
-ret = '"' + obj.replace('"', r'\"') + '"'
+ret += 'QLIT_QSTR(' + to_c_string(obj) + ')'
elif isinstance(obj, list):
-elts = [to_json(elt, level + 1)
+elts = [to_qlit(elt, level + 1)
for elt in obj]
-ret = '[' + ', '.join(elts) + ']'
+elts.append(indent(level + 1) + "{}")
+ret += 'QLIT_QLIST(((QLitObject[]) {\n'
+ret += ',\n'.join(elts) + '\n'
+ret += indent(level) + '}))'
elif isinstance(obj, dict):
-elts = ['"%s": %s' % (key.replace('"', r'\"'),
- to_json(obj[key], level + 1))
-for key in sorted(obj.keys())]
-ret = '{' + ', '.join(elts) + '}'
+elts = []
+for key, value in sorted(obj.iteritems()):
+elts.append(indent(level + 1) + '{ %s, %s }' %
+(to_c_string(key), to_qlit(value, level + 1, True)))
+elts.append(indent(level + 1) + '{}')
+ret += 'QLIT_QDICT(((QLitDictEntry[]) {\n'
+ret += ',\n'.join(elts) + '\n'
+ret += indent(level) + '}))'
else:
assert False# not implemented
-if level == 1:
-ret = '\n' + ret
return ret
@@ -45,39 +55,37 @@ class QAPISchemaGenIntrospectVisitor(QAPISchemaVisitor):
self.defn = None
self.decl = None
self._schema = None
-self._jsons = None
+self._qlits = None
self._used_types = None
self._name_map = None
def visit_begin(self, schema):
self._schema = schema
-self._jsons = []
+self._qlits = []
self._used_types = []
self._name_map = {}
def visit_end(self):
# visit the types that are actually used
-jsons = self._jsons
-self._jsons = []
+qlits = self._qlits
+self._qlits = []
for typ in self._used_types:
typ.visit(self)
# generate C
# TODO can generate awfully long lines
-jsons.extend(self._jsons)
-name = c_name(prefix, protect=False) + 'qmp_schema_json'
+qlits.extend(self._qlits)
+name = c_name(prefix, protect=False) + 'qmp_schema_qlit'
self.decl = mcgen('''
-extern const char %(c_name)s[];
+extern const QLitObject %(c_name)s;
''',
c_name=c_name(name))
-lines = to_json(jsons).split('\n')
-c_string = '\n'.join([to_c_string(line) for line in lines])
self.defn = mcgen('''
-const char %(c_name)s[] = %(c_string)s;
+const QLitObject %(c_name)s = %(c_string)s;
''',
c_name=c_name(name),
- c_string=c_string)
+ c_string=to_qlit(qlits))
self._schema = None
-self._jsons = None
+self._qlits = None
self._used_types = None
self._name_map = None
@@ -111,12 +119,12 @@ const char %(c_name)s[] = %(c_string)s;
return '[' + self._use_type(typ.element_type) + ']'
return self._name(typ.name)
-def _gen_json(self, name, mtype, obj):
+def _gen_qlit(self, name, mtype, obj):
if mtype not in ('command', 'event', 'builtin', 'array'):
name = self._name(name)
obj['name'] = name
obj['meta-type'] = mtype
-self._jsons.append(obj)
+self._qlits.append(obj)
def _gen_member(self, member):
ret = {'name': member.name, 'type': self._use_type(member.type)}
@@ -132,24 +140,24 @@ const char %(c_name)s[] = %(c_string)s;
return {'case': variant.name, 'type': self._use_type(variant.type)}
def visit_builtin_type(self, name, info, json_type):
-self._gen_json(name, 'builtin', {'json-type': json_type})
+self._gen_qlit(na