Index: tools/json_schema_compiler/cc_generator.py |
diff --git a/tools/json_schema_compiler/cc_generator.py b/tools/json_schema_compiler/cc_generator.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..81e797aba05c55c1633e974b67ce19720e151ea0 |
--- /dev/null |
+++ b/tools/json_schema_compiler/cc_generator.py |
@@ -0,0 +1,318 @@ |
+# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+from model import PropertyType |
+import code |
+import cpp_type_manager |
+import cpp_util |
+ |
+class CCGenerator(object): |
+ """A .cc generator for a namespace. |
+ """ |
+ def __init__(self, namespace, model): |
+ self.__cpp_type_manager = cpp_type_manager.CppTypeManager(namespace, model) |
+ self.__namespace = namespace |
+ |
+ def generate(self): |
+ """Generates a code.Code object with the .cc for a single namespace. |
+ """ |
+ include_path = self.__namespace.parent_dir |
+ filename = self.__namespace.filename |
+ c = code.Code() |
+ (c.append(cpp_util.CHROMIUM_LICENSE) |
+ .append() |
+ .append(cpp_util.GENERATED_FILE_MESSAGE % self.__namespace.parent_path) |
+ .append() |
+ .append('#include "tools/json_schema_compiler/util.h"') |
+ .append('#include "%s/%s.h"' % (include_path, filename)) |
+ .append() |
+ .append('namespace %s {' % self.__namespace.root_namespace) |
+ .append('namespace %s {' % filename) |
+ .append() |
+ .append('//') |
+ .append('// Types') |
+ .append('//') |
+ .append() |
+ ) |
+ for tipe in self.__namespace.types.values(): |
not at google - send to devlin
2012/01/17 05:42:32
do you actually need "tipe"? From what I can tell
calamity
2012/01/18 05:43:08
I guess.. but that's confusing? type is a built in
not at google - send to devlin
2012/01/18 06:57:28
Ah right. I only noted this because you have a pr
|
+ (c.concat(self.generate_type(tipe)) |
+ .append() |
+ ) |
+ (c.append('//') |
+ .append('// Functions') |
+ .append('//') |
+ .append() |
+ ) |
+ for function in self.__namespace.functions.values(): |
+ (c.concat(self.generate_function(function)) |
+ .append() |
+ ) |
+ (c.append('} // namespace %s' % self.__namespace.root_namespace) |
+ .append('} // namespace %s' % filename) |
+ ) |
+ # TODO(calamity): Events |
+ return c |
+ |
+ def generate_type(self, tipe): |
+ """Generates the function definitions for a type. |
+ """ |
+ c = code.Code() |
+ |
+ (c.append('%(classname)s::%(classname)s() {}') |
+ .append('%(classname)s::~%(classname)s() {}') |
+ .append() |
+ ) |
+ c.substitute({'classname': tipe.name}) |
+ |
+ c.concat(self.generate_type_populate(tipe)) |
+ c.append() |
+ # TODO(calamity): deal with non-serializable |
+ c.concat(self.generate_type_tovalue(tipe)) |
+ c.append() |
+ |
+ return c |
+ |
+ def generate_type_populate(self, tipe): |
+ """Generates the function for populating a type given a pointer to it. |
+ """ |
+ c = code.Code() |
+ (c.append('// static') |
+ .sblock('bool %(name)s::Populate(const Value& value, %(name)s* out) {') |
+ .append('if (!value.IsType(Value::TYPE_DICTIONARY))') |
+ .append(' return false;') |
+ .append('const DictionaryValue* dict = ' |
+ 'static_cast<const DictionaryValue*>(&value);') |
+ .append() |
+ ) |
+ c.substitute({'name': tipe.name}) |
+ |
+ # TODO(calamity): this doesn't even handle single properties. |
+ # add ALL the types |
+ for prop in tipe.properties.values(): |
+ sub = {'name': prop.name} |
+ if prop.type == PropertyType.ARRAY: |
+ if prop.item_type.type == PropertyType.REF: |
+ if prop.optional: |
+ (c.append('if (!json_schema_compiler::util::' |
+ 'GetOptionalTypes<%(type)s>(*dict,') |
+ .append(' "%(name)s", &out->%(name)s))') |
+ .append(' return false;') |
+ ) |
+ else: |
+ (c.append('if (!json_schema_compiler::util::' |
+ 'GetTypes<%(type)s>(*dict,') |
+ .append(' "%(name)s", &out->%(name)s))') |
+ .append(' return false;') |
+ ) |
+ sub['type'] = self.__cpp_type_manager.get_type(prop.item_type, |
+ pad_for_generics=True) |
+ elif prop.item_type.json_type == 'string': |
+ if prop.optional: |
+ (c.append('if (!json_schema_compiler::util::GetOptionalStrings' |
+ '(*dict, "%(name)s", &out->%(name)s))') |
+ .append(' return false;') |
+ ) |
+ else: |
+ (c.append('if (!json_schema_compiler::util::GetStrings' |
+ '(*dict, "%(name)s", &out->%(name)s))') |
+ .append(' return false;') |
+ ) |
+ else: |
+ raise NotImplementedError(prop.item_type.json_type) |
+ elif prop.type == PropertyType.FUNDAMENTAL: |
+ c.append('if(!dict->%s)' % get_fundamental_value(prop, |
+ '&out->%s' % prop.name)) |
+ c.append(' return false;') |
+ else: |
+ raise NotImplementedError(prop.type) |
+ c.substitute(sub) |
+ (c.append('return true;') |
+ .eblock('}') |
+ ) |
+ return c |
+ |
+ def generate_type_tovalue(self, tipe): |
+ """Generates a function that serializes the type into a |DictionaryValue|. |
+ """ |
+ c = code.Code() |
+ (c.sblock('DictionaryValue* %s::ToValue() const {' % tipe.name) |
+ .append('DictionaryValue* value = new DictionaryValue();') |
+ .append() |
+ ) |
+ name = tipe.name.lower() |
+ for prop in tipe.properties.values(): |
+ prop_name = name + '_' + prop.name if name else prop.name |
+ this_var = prop.name |
+ c.concat(self.create_value_from_property(prop_name, prop, this_var)) |
+ (c.append() |
+ .append('return value;') |
+ .eblock('}') |
+ ) |
+ return c |
+ |
+ # TODO(calamity): object and choices proptypes |
+ def create_value_from_property(self, name, prop, var): |
+ """Generates code to serialize a single property in a type. |
+ """ |
+ c = code.Code() |
+ if prop.type == PropertyType.FUNDAMENTAL: |
+ c.append( |
+ 'Value* %s_value = %s;' % (name, create_fundamental_value(prop, var))) |
+ elif prop.type == PropertyType.ARRAY: |
+ if prop.item_type.json_type == 'string': |
not at google - send to devlin
2012/01/17 05:42:32
e.g. when you change the way types are represented
calamity
2012/01/18 05:43:08
Done.
|
+ if prop.optional: |
+ c.append('json_schema_compiler::util::' |
+ 'SetOptionalStrings(%s, "%s", value);' |
+ % (var, prop.name)) |
not at google - send to devlin
2012/01/17 05:42:32
this line can be on the same as the one above
calamity
2012/01/18 05:43:08
Done.
|
+ else: |
+ c.append('json_schema_compiler::util::SetStrings(%s, "%s", value);' % |
+ (var, prop.name)) |
not at google - send to devlin
2012/01/17 05:42:32
line split in the same way as above
|
+ else: |
+ item_name = name + '_single' |
+ (c.append('ListValue* %(name)s_value = new ListValue();') |
+ .append('for (%(it_type)s::iterator it = %(var)s->begin();') |
+ .sblock(' it != %(var)s->end(); ++it) {') |
+ .concat(self.create_value_from_property(item_name, prop.item_type, |
+ '*it')) |
+ .append('%(name)s_value->Append(%(prop_val)s_value);') |
+ .eblock('}') |
+ ) |
+ c.substitute( |
+ {'it_type': self.__cpp_type_manager.get_type(prop), |
+ 'name': name, 'var': var, 'prop_val': item_name}) |
+ elif prop.type == PropertyType.REF: |
+ c.append('Value* %s_value = %s.ToValue();' % (name, var)) |
+ else: |
+ raise NotImplementedError |
+ return c |
+ |
+ def generate_function(self, function): |
+ """Generates the definitions for function structs. |
+ """ |
+ classname = cpp_util.cpp_name(function.name) |
+ c = code.Code() |
+ |
+ # Params::Populate function |
+ if function.params: |
+ (c.append('%(name)s::Params::Params() {}') |
+ .append('%(name)s::Params::~Params() {}') |
+ .append() |
+ .concat(self.generate_function_params_populate(function)) |
+ .append() |
+ ) |
+ |
+ # Result::Create function |
+ c.concat(self.generate_function_result_create(function)) |
+ |
+ c.substitute({'name': classname}) |
+ |
+ return c |
+ |
+ def generate_function_params_populate(self, function): |
+ """Generate function to populate an instance of Params given a pointer. |
+ """ |
+ classname = cpp_util.cpp_name(function.name) |
+ c = code.Code() |
+ c.append('// static') |
+ c.append('bool %(classname)s::Params::Populate(const ListValue& args,') |
+ c.sblock(' %(classname)s::Params* out) {') |
+ c.substitute({'classname': classname}) |
+ c.append('if (args.GetSize() != %d)' % len(function.params)) |
+ c.append(' return false;') |
+ |
+ # TODO(calamity): generalize, needs to move to function to do populates for |
+ # wider variety of args |
+ for i, param in enumerate(function.params): |
+ sub = {'name': param.name, 'pos': i} |
+ c.append() |
+ # TODO(calamity): Make valid for not just objects |
+ c.append('DictionaryValue* %(name)s_param = NULL;') |
+ c.append('if (!args.GetDictionary(%(pos)d, &%(name)s_param))') |
+ c.append(' return false;') |
+ if param.type == PropertyType.REF: |
+ c.append('if (!%(ctype)s::Populate(*%(name)s_param, &out->%(name)s))') |
+ c.append(' return false;') |
+ sub['ctype'] = self.__cpp_type_manager.get_type(param) |
+ elif param.type == PropertyType.FUNDAMENTAL: |
+ #XXX THIS IS WRONG |
+ c.append('// TODO Needs some sort of casting') |
+ c.append('if (!%(name)s_param->' + |
+ get_fundamental_value(param,'&out->%s' % param.name) +');') |
+ c.append(' return false;') |
+ elif param.type == PropertyType.OBJECT: |
+ c.append('if(!%(ctype)s::Populate(*%(name)s_param, &out->%(name)s))') |
+ c.append(' return false;') |
+ sub['ctype'] = self.__cpp_type_manager.get_type(param) |
+ elif param.type == PropertyType.CHOICES: |
+ c.append('// TODO handle chocies') |
+ else: |
+ raise NotImplementedError(param.type) |
+ c.substitute(sub) |
+ c.append() |
+ c.append('return true;') |
+ c.eblock('}') |
+ |
+ return c |
+ |
+ def generate_function_result_create(self, function): |
+ """Generate function to create a Result given the return value. |
+ """ |
+ classname = cpp_util.cpp_name(function.name) |
+ c = code.Code() |
+ c.append('// static') |
+ param = function.callback.param |
+ arg = '' |
+ if param: |
+ if param.type == PropertyType.REF: |
+ arg = 'const %(type)s& %(name)s' |
not at google - send to devlin
2012/01/17 05:42:32
extra space
calamity
2012/01/18 05:43:08
Done.
|
+ else: |
+ arg = 'const %(type)s %(name)s' |
+ arg = arg % {'type': self.__cpp_type_manager.get_type(param), |
+ 'name': param.name} |
+ c.sblock('Value* %(classname)s::Result::Create(%(arg)s) {') |
+ sub = {'classname': classname, 'arg': arg} |
+ # TODO(calamity): Choices |
+ if not param: |
+ c.append('return Value::CreateNullValue();') |
+ else: |
+ sub['argname'] = param.name |
+ if param.type == PropertyType.FUNDAMENTAL: |
+ c.append('return %s;' % create_fundamental_value(param, param.name)) |
+ elif param.type == PropertyType.REF: |
+ c.append('DictionaryValue* result = new DictionaryValue();') |
+ c.append('result->SetWithoutPathExpansion("%(argname)s",' |
+ '%(argname)s.ToValue());') |
+ c.append('return result;') |
+ elif param.type == PropertyType.OBJECT: |
+ c.append('// TODO object stuff') |
+ elif param.type == PropertyType.ARRAY: |
+ c.append('// TODO array stuff') |
+ else: |
+ raise NotImplementedError(param.type) |
+ c.substitute(sub) |
+ c.eblock('}') |
+ return c |
+ |
+def create_fundamental_value(prop, var): |
+ """Returns the C++ code for creating a value of the given property type |
+ using the given variable. |
+ """ |
+ return { |
+ 'string': 'Value::CreateStringValue(%s)', |
+ 'boolean': 'Value::CreateBooleanValue(%s)', |
+ 'integer': 'Value::CreateIntegerValue(%s)', |
+ 'double': 'Value::CreateDoubleValue(%s)', |
+ }[prop.json_type] % var |
+ |
+ |
+def get_fundamental_value(prop, var): |
+ """Returns the C++ code for retrieving a fundamental type from a Value |
+ into a variable. |
+ """ |
+ return { |
+ 'string': 'GetAsString(%s)', |
+ 'boolean': 'GetAsBoolean(%s)', |
+ 'integer': 'GetAsInteger(%s)', |
+ 'double': 'GetAsDouble(%s)', |
+ }[prop.json_type] % var |