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..870b4875e6f0956c7549181f06687b5eedb42e14 |
--- /dev/null |
+++ b/tools/json_schema_compiler/cc_generator.py |
@@ -0,0 +1,298 @@ |
+# 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_generator |
+import cpp_util |
+ |
+class CCGenerator(object): |
Yoyo Zhou
2012/01/19 02:19:40
General comment: have a read through the style gui
|
+ """A .cc generator for a namespace. |
+ """ |
+ def __init__(self, namespace, model, root_namespace): |
+ self.__cpp_type_generator = cpp_type_generator.CppTypeGenerator( |
+ namespace, model) |
+ self.__namespace = namespace |
+ self.__root_namespace = root_namespace |
+ |
+ def generate(self): |
+ """Generates a code.Code object with the .cc for a single namespace. |
+ """ |
+ target_namespace = self.__namespace.target_namespace |
+ c = code.Code() |
+ (c.append(cpp_util.CHROMIUM_LICENSE) |
+ .append() |
+ .append(cpp_util.GENERATED_FILE_MESSAGE % self.__namespace.source_file) |
+ .append() |
+ .append('#include "tools/json_schema_compiler/util.h"') |
+ .append('#include "%s/%s.h"' % |
+ (self.__namespace.source_file_dir, target_namespace)) |
+ .append() |
+ .append('namespace %s {' % self.__root_namespace) |
+ .append('namespace %s {' % target_namespace) |
+ .append() |
+ .append('//') |
+ .append('// Types') |
+ .append('//') |
+ .append() |
+ ) |
+ for tipe in self.__namespace.types.values(): |
+ (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.__root_namespace) |
+ .append('} // namespace %s' % target_namespace) |
+ .append() |
+ ) |
+ # TODO(calamity): Events |
+ return c |
+ |
+ def __generate_type(self, tipe): |
Yoyo Zhou
2012/01/19 02:19:40
type_ is preferred over tipe.
calamity
2012/01/20 01:10:25
Done.
|
+ """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_generator.get_type(prop.item_type, |
+ pad_for_generics=True) |
+ elif prop.item_type.type == PropertyType.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.type) |
+ elif prop.type.is_fundamental: |
+ c.append('if (!dict->%s)' % cpp_util.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.is_fundamental: |
+ c.append('Value* %s_value = %s;' % |
+ (name, cpp_util.create_fundamental_value(prop, var))) |
+ elif prop.type == PropertyType.ARRAY: |
+ if prop.item_type.type == PropertyType.STRING: |
+ if prop.optional: |
+ c.append('json_schema_compiler::util::' |
+ 'SetOptionalStrings(%s, "%s", value);' % (var, prop.name)) |
+ else: |
+ c.append('json_schema_compiler::util::' |
+ 'SetStrings(%s, "%s", value);' % (var, prop.name)) |
+ 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_generator.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: |
Yoyo Zhou
2012/01/19 02:19:40
This also doesn't handle single properties, right?
calamity
2012/01/20 01:10:25
fundamental types and REF types are the single pro
|
+ 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_generator.get_type(param) |
+ elif param.type.is_fundamental: |
+ #XXX THIS IS WRONG |
+ c.append('// TODO Needs some sort of casting') |
Yoyo Zhou
2012/01/19 02:19:40
Seems like you could raise NotImplementedError her
calamity
2012/01/23 05:14:45
Done.
|
+ c.append('if (!%(name)s_param->' + |
+ cpp_util.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_generator.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' |
+ else: |
+ arg = 'const %(type)s %(name)s' |
+ arg = arg % {'type': self.__cpp_type_generator.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.is_fundamental: |
+ c.append('return %s;' % |
+ cpp_util.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 |