summaryrefslogtreecommitdiffstats
path: root/tools/json_schema_compiler/cc_generator.py
diff options
context:
space:
mode:
authorhebert.christopherj@chromium.org <hebert.christopherj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-08-29 04:07:13 +0000
committerhebert.christopherj@chromium.org <hebert.christopherj@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-08-29 04:07:13 +0000
commite7ae7017cb69c66e43e08dedede8f9f5376dbe02 (patch)
tree0dbfecc0a86884aa44c7d5cca2c9d69746e1e5e8 /tools/json_schema_compiler/cc_generator.py
parent6c56fde5bf676be3cee74fc3587163f067a28e04 (diff)
downloadchromium_src-e7ae7017cb69c66e43e08dedede8f9f5376dbe02.zip
chromium_src-e7ae7017cb69c66e43e08dedede8f9f5376dbe02.tar.gz
chromium_src-e7ae7017cb69c66e43e08dedede8f9f5376dbe02.tar.bz2
Changes:
1) IsEnumOrEnumRef is refactored to be in the cpp_type_generator. 2) Refactored GeneratePopulatePropertyFromValue to call private methods (it was getting to be unwieldy.) 3) Refactored most of the unittests to put values on the stack instead of scoped_ptrs. 4) Implemented Enums as types. Enums all have the ENUMERATION_NONE. BUG=141940 Review URL: https://chromiumcodereview.appspot.com/10828407 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@153849 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/json_schema_compiler/cc_generator.py')
-rw-r--r--tools/json_schema_compiler/cc_generator.py313
1 files changed, 188 insertions, 125 deletions
diff --git a/tools/json_schema_compiler/cc_generator.py b/tools/json_schema_compiler/cc_generator.py
index d09d325..f01479a 100644
--- a/tools/json_schema_compiler/cc_generator.py
+++ b/tools/json_schema_compiler/cc_generator.py
@@ -127,6 +127,9 @@ class CCGenerator(object):
(c.Concat(self._GenerateTypeToValue(cpp_namespace, type_))
.Append()
)
+ elif self._cpp_type_generator.IsEnumOrEnumRef(type_):
+ c.Concat(self._GenerateCreateEnumTypeValue(cpp_namespace, type_))
+ c.Append()
c.Substitute({'classname': classname, 'namespace': cpp_namespace})
return c
@@ -252,7 +255,7 @@ class CCGenerator(object):
c.Append('value->MergeDictionary(&%s);' % prop.unix_name)
else:
if prop.optional:
- if prop.type_ == PropertyType.ENUM:
+ if self._cpp_type_generator.IsEnumOrEnumRef(prop):
c.Sblock('if (%s != %s) {' %
(prop.unix_name,
self._cpp_type_generator.GetEnumNoneValue(prop)))
@@ -339,7 +342,7 @@ class CCGenerator(object):
else:
vardot = var + '.'
return '%sDeepCopy()' % vardot
- elif prop.type_ == PropertyType.ENUM:
+ elif self._cpp_type_generator.IsEnumOrEnumRef(prop):
return 'CreateEnumValue(%s).release()' % var
elif prop.type_ == PropertyType.BINARY:
if prop.optional:
@@ -460,127 +463,21 @@ class CCGenerator(object):
c.Sblock('{')
if self._IsFundamentalOrFundamentalRef(prop):
- if prop.optional:
- (c.Append('%(ctype)s temp;')
- .Append('if (!%s)' %
- cpp_util.GetAsFundamentalValue(
- self._cpp_type_generator.GetReferencedProperty(prop),
- value_var,
- '&temp'))
- .Append(' return %(failure_value)s;')
- )
- if prop.type_ != prop.compiled_type:
- (c.Append('%(compiled_ctype)s temp2;')
- .Append('if (!%s)' %
- cpp_util.GenerateTypeToCompiledTypeConversion(
- self._cpp_type_generator.GetReferencedProperty(prop),
- 'temp',
- 'temp2'))
- .Append(' return %(failure_value)s;')
- .Append('%(dst)s->%(name)s.reset(new %(compiled_ctype)s(temp2));')
- )
- else:
- c.Append('%(dst)s->%(name)s.reset(new %(ctype)s(temp));')
-
- else:
- if prop.type_ == prop.compiled_type:
- assignment_target = '&%s->%s' % (dst, prop.unix_name)
- else:
- c.Append('%(ctype)s temp;')
- assignment_target = '&temp'
- (c.Append('if (!%s)' %
- cpp_util.GetAsFundamentalValue(
- self._cpp_type_generator.GetReferencedProperty(prop),
- value_var,
- assignment_target))
- .Append(' return %(failure_value)s;')
- )
- if prop.type_ != prop.compiled_type:
- (c.Append('if (!%s)' %
- cpp_util.GenerateTypeToCompiledTypeConversion(
- self._cpp_type_generator.GetReferencedProperty(prop),
- 'temp',
- '%s->%s' % (dst, prop.unix_name)))
- .Append(' return %(failure_value)s;')
- )
-
+ self._GenerateFundamentalOrFundamentalRefPopulate(c, prop, value_var, dst)
elif self._IsObjectOrObjectRef(prop):
- if prop.optional:
- (c.Append('const base::DictionaryValue* dictionary = NULL;')
- .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))')
- .Append(' return %(failure_value)s;')
- .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());')
- .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))')
- .Append(' return %(failure_value)s;')
- .Append('%(dst)s->%(name)s = temp.Pass();')
- )
- else:
- (c.Append('const base::DictionaryValue* dictionary = NULL;')
- .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))')
- .Append(' return %(failure_value)s;')
- .Append(
- 'if (!%(ctype)s::Populate(*dictionary, &%(dst)s->%(name)s))')
- .Append(' return %(failure_value)s;')
- )
+ self._GenerateObjectOrObjectRefPopulate(c, prop)
elif prop.type_ == PropertyType.FUNCTION:
- if prop.optional:
- c.Append('%(dst)s->%(name)s.reset(new base::DictionaryValue());')
+ self._GenerateFunctionPopulate(c, prop)
elif prop.type_ == PropertyType.ANY:
- if prop.optional:
- c.Append('%(dst)s->%(name)s.reset(new ' + any_helper.ANY_CLASS + '());')
- c.Append(self._any_helper.Init(prop, value_var, dst) + ';')
+ self._GenerateAnyPopulate(c, prop, value_var, dst)
elif self._IsArrayOrArrayRef(prop):
- # util_cc_helper deals with optional and required arrays
- (c.Append('const base::ListValue* list = NULL;')
- .Append('if (!%(value_var)s->GetAsList(&list))')
- .Append(' return %(failure_value)s;'))
- if prop.item_type.type_ == PropertyType.ENUM:
- self._GenerateListValueToEnumArrayConversion(c, prop)
- else:
- (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList(
- self._cpp_type_generator.GetReferencedProperty(prop), 'list',
- dst + '->' + prop.unix_name, prop.optional))
- .Append(' return %(failure_value)s;')
- )
+ self._GenerateArrayOrArrayRefPopulate(c, prop, dst)
elif prop.type_ == PropertyType.CHOICES:
- type_var = '%(dst)s->%(name)s_type'
- c.Sblock('switch (%(value_var)s->GetType()) {')
- for choice in self._cpp_type_generator.ExpandParams([prop]):
- (c.Sblock('case %s: {' % cpp_util.GetValueType(
- self._cpp_type_generator.GetReferencedProperty(choice).type_))
- .Concat(self._GeneratePopulatePropertyFromValue(
- choice, value_var, dst, failure_value, check_type=False))
- .Append('%s = %s;' %
- (type_var,
- self._cpp_type_generator.GetEnumValue(
- prop, choice.type_.name)))
- .Append('break;')
- .Eblock('}')
- )
- (c.Append('default:')
- .Append(' return %(failure_value)s;')
- )
- c.Eblock('}')
- elif prop.type_ == PropertyType.ENUM:
- c.Sblock('{')
- self._GenerateStringToEnumConversion(c, prop, value_var, 'enum_temp')
- c.Append('%(dst)s->%(name)s = enum_temp;')
- c.Eblock('}')
+ self._GenerateChoicePopulate(c, prop, value_var, dst, failure_value)
+ elif self._cpp_type_generator.IsEnumOrEnumRef(prop):
+ self._GenerateEnumPopulate(c, prop, value_var)
elif prop.type_ == PropertyType.BINARY:
- (c.Append('if (!%(value_var)s->IsType(%(value_type)s))')
- .Append(' return %(failure_value)s;')
- .Append('const base::BinaryValue* binary_value =')
- .Append(' static_cast<const base::BinaryValue*>(%(value_var)s);')
- )
- if prop.optional:
- (c.Append('%(dst)s->%(name)s.reset(')
- .Append(' new std::string(binary_value->GetBuffer(),')
- .Append(' binary_value->GetSize()));')
- )
- else:
- (c.Append('%(dst)s->%(name)s.assign(binary_value->GetBuffer(),')
- .Append(' binary_value->GetSize());')
- )
+ self._GenerateBinaryPopulate(c, prop)
else:
raise NotImplementedError(prop.type_)
c.Eblock('}')
@@ -598,6 +495,139 @@ class CCGenerator(object):
c.Substitute(sub)
return c
+ def _GenerateFundamentalOrFundamentalRefPopulate(self,
+ c,
+ prop,
+ value_var,
+ dst):
+ if prop.optional:
+ (c.Append('%(ctype)s temp;')
+ .Append('if (!%s)' %
+ cpp_util.GetAsFundamentalValue(
+ self._cpp_type_generator.GetReferencedProperty(prop),
+ value_var,
+ '&temp'))
+ .Append(' return %(failure_value)s;')
+ )
+ if prop.type_ != prop.compiled_type:
+ (c.Append('%(compiled_ctype)s temp2;')
+ .Append('if (!%s)' %
+ cpp_util.GenerateTypeToCompiledTypeConversion(
+ self._cpp_type_generator.GetReferencedProperty(prop),
+ 'temp',
+ 'temp2'))
+ .Append(' return %(failure_value)s;')
+ .Append('%(dst)s->%(name)s.reset(new %(compiled_ctype)s(temp2));')
+ )
+ else:
+ c.Append('%(dst)s->%(name)s.reset(new %(ctype)s(temp));')
+
+ else:
+ if prop.type_ == prop.compiled_type:
+ assignment_target = '&%s->%s' % (dst, prop.unix_name)
+ else:
+ c.Append('%(ctype)s temp;')
+ assignment_target = '&temp'
+ (c.Append('if (!%s)' %
+ cpp_util.GetAsFundamentalValue(
+ self._cpp_type_generator.GetReferencedProperty(prop),
+ value_var,
+ assignment_target))
+ .Append(' return %(failure_value)s;')
+ )
+ if prop.type_ != prop.compiled_type:
+ (c.Append('if (!%s)' %
+ cpp_util.GenerateTypeToCompiledTypeConversion(
+ self._cpp_type_generator.GetReferencedProperty(prop),
+ 'temp',
+ '%s->%s' % (dst, prop.unix_name)))
+ .Append(' return %(failure_value)s;')
+ )
+
+ def _GenerateObjectOrObjectRefPopulate(self, c, prop):
+ if prop.optional:
+ (c.Append('const base::DictionaryValue* dictionary = NULL;')
+ .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))')
+ .Append(' return %(failure_value)s;')
+ .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());')
+ .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))')
+ .Append(' return %(failure_value)s;')
+ .Append('%(dst)s->%(name)s = temp.Pass();')
+ )
+ else:
+ (c.Append('const base::DictionaryValue* dictionary = NULL;')
+ .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))')
+ .Append(' return %(failure_value)s;')
+ .Append(
+ 'if (!%(ctype)s::Populate(*dictionary, &%(dst)s->%(name)s))')
+ .Append(' return %(failure_value)s;')
+ )
+
+ def _GenerateFunctionPopulate(self, c, prop):
+ if prop.optional:
+ c.Append('%(dst)s->%(name)s.reset(new base::DictionaryValue());')
+
+ def _GenerateAnyPopulate(self, c, prop, value_var, dst):
+ if prop.optional:
+ c.Append('%(dst)s->%(name)s.reset(new ' + any_helper.ANY_CLASS + '());')
+ c.Append(self._any_helper.Init(prop, value_var, dst) + ';')
+
+ def _GenerateArrayOrArrayRefPopulate(self, c, prop, dst):
+ # util_cc_helper deals with optional and required arrays
+ (c.Append('const base::ListValue* list = NULL;')
+ .Append('if (!%(value_var)s->GetAsList(&list))')
+ .Append(' return %(failure_value)s;'))
+ if prop.item_type.type_ == PropertyType.ENUM:
+ self._GenerateListValueToEnumArrayConversion(c, prop)
+ else:
+ (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList(
+ self._cpp_type_generator.GetReferencedProperty(prop), 'list',
+ dst + '->' + prop.unix_name, prop.optional))
+ .Append(' return %(failure_value)s;')
+ )
+
+ def _GenerateChoicePopulate(self, c, prop, value_var, dst, failure_value):
+ type_var = '%(dst)s->%(name)s_type'
+ c.Sblock('switch (%(value_var)s->GetType()) {')
+ for choice in self._cpp_type_generator.ExpandParams([prop]):
+ (c.Sblock('case %s: {' % cpp_util.GetValueType(
+ self._cpp_type_generator.GetReferencedProperty(choice).type_))
+ .Concat(self._GeneratePopulatePropertyFromValue(
+ choice, value_var, dst, failure_value, check_type=False))
+ .Append('%s = %s;' %
+ (type_var,
+ self._cpp_type_generator.GetEnumValue(
+ prop, choice.type_.name)))
+ .Append('break;')
+ .Eblock('}')
+ )
+ (c.Append('default:')
+ .Append(' return %(failure_value)s;')
+ )
+ c.Eblock('}')
+
+ def _GenerateEnumPopulate(self, c, prop, value_var):
+ c.Sblock('{')
+ self._GenerateStringToEnumConversion(c, prop, value_var, 'enum_temp')
+ c.Append('%(dst)s->%(name)s = enum_temp;')
+ c.Eblock('}')
+
+ def _GenerateBinaryPopulate(self, c, prop):
+ (c.Append('if (!%(value_var)s->IsType(%(value_type)s))')
+ .Append(' return %(failure_value)s;')
+ .Append('const base::BinaryValue* binary_value =')
+ .Append(' static_cast<const base::BinaryValue*>(%(value_var)s);')
+ )
+ if prop.optional:
+ (c.Append('%(dst)s->%(name)s.reset(')
+ .Append(' new std::string(binary_value->GetBuffer(),')
+ .Append(' binary_value->GetSize()));')
+ )
+ else:
+ (c.Append('%(dst)s->%(name)s.assign(binary_value->GetBuffer(),')
+ .Append(' binary_value->GetSize());')
+ )
+
def _GenerateListValueToEnumArrayConversion(self, c, prop):
"""Appends code that converts a ListValue of string contstants to
an array of enums in dst.
@@ -613,31 +643,37 @@ class CCGenerator(object):
accessor = '->'
c.Sblock('for (ListValue::const_iterator it = list->begin(); '
'it != list->end(); ++it) {')
- self._GenerateStringToEnumConversion(c, prop.item_type,
- '(*it)', 'enum_temp')
+ self._GenerateStringToEnumConversion(
+ c, prop.item_type, '(*it)', 'enum_temp')
c.Append('%(dst)s->%(name)s' + accessor + 'push_back(enum_temp);')
c.Eblock('}')
- def _GenerateStringToEnumConversion(self, c, prop, value_var, enum_temp):
+ def _GenerateStringToEnumConversion(self,
+ c,
+ prop,
+ value_var,
+ enum_temp):
"""Appends code that converts a string to an enum.
- Leaves failure_value unsubstituded.
+ Leaves failure_value unsubstituted.
c: the code that is appended to.
prop: the property that the code is populating.
value_var: the string value that is being converted.
enum_temp: the name used to store the temporary enum value.
"""
- (c.Append('%s %s;' % (self._cpp_type_generator.GetType(prop), enum_temp))
+ (c.Append('%s %s;' % (self._cpp_type_generator.GetCompiledType(prop),
+ enum_temp))
.Append('std::string enum_as_string;')
.Append('if (!%s->GetAsString(&enum_as_string))' % value_var)
.Append(' return %(failure_value)s;')
)
- for i, enum_value in enumerate(prop.enum_values):
+ for i, enum_value in enumerate(
+ self._cpp_type_generator.GetReferencedProperty(prop).enum_values):
(c.Append(
('if' if i == 0 else 'else if') +
'(enum_as_string == "%s")' % enum_value)
.Append(' ' + enum_temp + ' = %s;' % (
- self._cpp_type_generator.GetEnumValue(prop, enum_value)))
+ self._cpp_type_generator.GetEnumValue(prop, enum_value)))
)
(c.Append('else')
.Append(' return %(failure_value)s;')
@@ -697,6 +733,32 @@ class CCGenerator(object):
)
return c
+ def _GenerateCreateEnumTypeValue(self, cpp_namespace, prop):
+ """Generates CreateEnumValue() that returns the base::StringValue
+ representation of an enum type.
+ """
+ c = Code()
+ classname = cpp_util.Classname(schema_util.StripSchemaNamespace(prop.name))
+ c.Sblock('scoped_ptr<base::Value> CreateEnumValue(%s %s) {' % (
+ classname, classname.lower()))
+ c.Sblock('switch (%s) {' % classname.lower())
+
+ enum_prop = self._cpp_type_generator.GetReferencedProperty(prop)
+ for enum_value in enum_prop.enum_values:
+ c.Concat(self._GenerateReturnCase(
+ '%s_%s' % (classname.upper(), enum_value.upper()),
+ 'scoped_ptr<base::Value>(base::Value::CreateStringValue("%s"))' %
+ enum_value))
+ (c.Eblock('}')
+ .Append('NOTREACHED();')
+ .Append('return scoped_ptr<base::Value>();')
+ .Eblock('}')
+ )
+ return c
+
+ # TODO(chebert): This is basically the same as GenerateCreateEnumTypeValue().
+ # The plan is to phase out the old-style enums, and make all enums into REF
+ # types.
def _GenerateCreateEnumValue(self, cpp_namespace, prop):
"""Generates CreateEnumValue() that returns the base::StringValue
representation of an enum.
@@ -813,7 +875,8 @@ class CCGenerator(object):
dst: Type*
"""
c = Code()
- if prop.type_ in (PropertyType.ENUM, PropertyType.CHOICES):
+ if (self._cpp_type_generator.IsEnumOrEnumRef(prop) or
+ prop.type_ == PropertyType.CHOICES):
if prop.optional:
prop_name = prop.unix_name
if prop.type_ == PropertyType.CHOICES: