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

Side by Side Diff: tools/json_schema_compiler/cc_generator.py

Issue 23549025: Clean up JSON Schema Compiler. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Removed enum test and added blank lines. Created 7 years, 3 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 unified diff | Download patch
« no previous file with comments | « no previous file | tools/json_schema_compiler/compiler.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 from code import Code 5 from code import Code
6 from model import PropertyType, Type 6 from model import PropertyType
7 import cpp_util 7 import cpp_util
8 import model
9 import schema_util 8 import schema_util
10 import sys
11 import util_cc_helper 9 import util_cc_helper
12 10
13 class CCGenerator(object): 11 class CCGenerator(object):
14 def __init__(self, type_generator, cpp_namespace): 12 def __init__(self, type_generator, cpp_namespace):
15 self._type_generator = type_generator 13 self._type_generator = type_generator
16 self._cpp_namespace = cpp_namespace 14 self._cpp_namespace = cpp_namespace
17 15
18 def Generate(self, namespace): 16 def Generate(self, namespace):
19 return _Generator(namespace, 17 return _Generator(namespace,
20 self._type_generator, 18 self._type_generator,
21 self._cpp_namespace).Generate() 19 self._cpp_namespace).Generate()
22 20
21
23 class _Generator(object): 22 class _Generator(object):
24 """A .cc generator for a namespace. 23 """A .cc generator for a namespace.
25 """ 24 """
26 def __init__(self, namespace, cpp_type_generator, cpp_namespace): 25 def __init__(self, namespace, cpp_type_generator, cpp_namespace):
27 self._namespace = namespace 26 self._namespace = namespace
28 self._type_helper = cpp_type_generator 27 self._type_helper = cpp_type_generator
29 self._cpp_namespace = cpp_namespace 28 self._cpp_namespace = cpp_namespace
30 self._target_namespace = ( 29 self._target_namespace = (
31 self._type_helper.GetCppNamespaceName(self._namespace)) 30 self._type_helper.GetCppNamespaceName(self._namespace))
32 self._util_cc_helper = ( 31 self._util_cc_helper = (
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 # ANY is a base::Value which is abstract and cannot be a direct member, so 341 # ANY is a base::Value which is abstract and cannot be a direct member, so
343 # it will always be a pointer. 342 # it will always be a pointer.
344 is_ptr = prop.optional or prop.type_.property_type == PropertyType.ANY 343 is_ptr = prop.optional or prop.type_.property_type == PropertyType.ANY
345 c.Append('value->SetWithoutPathExpansion("%s", %s);' % ( 344 c.Append('value->SetWithoutPathExpansion("%s", %s);' % (
346 prop.name, 345 prop.name,
347 self._CreateValueFromType(prop.type_, 346 self._CreateValueFromType(prop.type_,
348 'this->%s' % prop.unix_name, 347 'this->%s' % prop.unix_name,
349 is_ptr=is_ptr))) 348 is_ptr=is_ptr)))
350 349
351 if prop.optional: 350 if prop.optional:
352 c.Eblock('}'); 351 c.Eblock('}')
353 352
354 if type_.additional_properties is not None: 353 if type_.additional_properties is not None:
355 if type_.additional_properties.property_type == PropertyType.ANY: 354 if type_.additional_properties.property_type == PropertyType.ANY:
356 c.Append('value->MergeDictionary(&additional_properties);') 355 c.Append('value->MergeDictionary(&additional_properties);')
357 else: 356 else:
358 # Non-copyable types will be wrapped in a linked_ptr for inclusion in 357 # Non-copyable types will be wrapped in a linked_ptr for inclusion in
359 # maps, so we need to unwrap them. 358 # maps, so we need to unwrap them.
360 needs_unwrap = ( 359 needs_unwrap = (
361 not self._type_helper.IsCopyable(type_.additional_properties)) 360 not self._type_helper.IsCopyable(type_.additional_properties))
362 cpp_type = self._type_helper.GetCppType(type_.additional_properties, 361 cpp_type = self._type_helper.GetCppType(type_.additional_properties,
(...skipping 12 matching lines...) Expand all
375 return (c.Append() 374 return (c.Append()
376 .Append('return value.Pass();') 375 .Append('return value.Pass();')
377 .Eblock('}')) 376 .Eblock('}'))
378 377
379 def _GenerateChoiceTypeToValue(self, cpp_namespace, type_): 378 def _GenerateChoiceTypeToValue(self, cpp_namespace, type_):
380 """Generates a function that serializes a choice-representing type 379 """Generates a function that serializes a choice-representing type
381 into a base::Value. 380 into a base::Value.
382 """ 381 """
383 c = Code() 382 c = Code()
384 c.Sblock('scoped_ptr<base::Value> %s::ToValue() const {' % cpp_namespace) 383 c.Sblock('scoped_ptr<base::Value> %s::ToValue() const {' % cpp_namespace)
385 c.Append('scoped_ptr<base::Value> result;'); 384 c.Append('scoped_ptr<base::Value> result;')
386 for choice in type_.choices: 385 for choice in type_.choices:
387 choice_var = 'as_%s' % choice.unix_name 386 choice_var = 'as_%s' % choice.unix_name
388 (c.Sblock('if (%s) {' % choice_var) 387 (c.Sblock('if (%s) {' % choice_var)
389 .Append('DCHECK(!result) << "Cannot set multiple choices for %s";' % 388 .Append('DCHECK(!result) << "Cannot set multiple choices for %s";' %
390 type_.unix_name) 389 type_.unix_name)
391 .Append('result.reset(%s);' % 390 .Append('result.reset(%s);' %
392 self._CreateValueFromType(choice, '*%s' % choice_var)) 391 self._CreateValueFromType(choice, '*%s' % choice_var))
393 .Eblock('}') 392 .Eblock('}')
394 ) 393 )
395 (c.Append('DCHECK(result) << "Must set at least one choice for %s";' % 394 (c.Append('DCHECK(result) << "Must set at least one choice for %s";' %
396 type_.unix_name) 395 type_.unix_name)
397 .Append('return result.Pass();') 396 .Append('return result.Pass();')
398 .Eblock('}') 397 .Eblock('}')
399 ) 398 )
400 return c 399 return c
401 400
402 def _GenerateFunction(self, function): 401 def _GenerateFunction(self, function):
403 """Generates the definitions for function structs. 402 """Generates the definitions for function structs.
404 """ 403 """
405 c = Code() 404 c = Code()
406 405
407 # TODO(kalman): use function.unix_name not Classname. 406 # TODO(kalman): use function.unix_name not Classname.
408 function_namespace = cpp_util.Classname(function.name) 407 function_namespace = cpp_util.Classname(function.name)
409 """Windows has a #define for SendMessage, so to avoid any issues, we need 408 # Windows has a #define for SendMessage, so to avoid any issues, we need
410 to not use the name. 409 # to not use the name.
411 """
412 if function_namespace == 'SendMessage': 410 if function_namespace == 'SendMessage':
413 function_namespace = 'PassMessage' 411 function_namespace = 'PassMessage'
414 (c.Append('namespace %s {' % function_namespace) 412 (c.Append('namespace %s {' % function_namespace)
415 .Append() 413 .Append()
416 ) 414 )
417 415
418 # Params::Populate function 416 # Params::Populate function
419 if function.params: 417 if function.params:
420 c.Concat(self._GeneratePropertyFunctions('Params', function.params)) 418 c.Concat(self._GeneratePropertyFunctions('Params', function.params))
421 (c.Append('Params::Params() {}') 419 (c.Append('Params::Params() {}')
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
731 'key': type_.name, 729 'key': type_.name,
732 'parent_key': type_.parent.name 730 'parent_key': type_.parent.name
733 }) 731 })
734 732
735 def _GenerateListValueToEnumArrayConversion(self, 733 def _GenerateListValueToEnumArrayConversion(self,
736 item_type, 734 item_type,
737 src_var, 735 src_var,
738 dst_var, 736 dst_var,
739 failure_value, 737 failure_value,
740 is_ptr=False): 738 is_ptr=False):
741 """Returns Code that converts a ListValue of string constants from 739 """Returns Code that converts a ListValue of string constants from
742 |src_var| into an array of enums of |type_| in |dst_var|. On failure, 740 |src_var| into an array of enums of |type_| in |dst_var|. On failure,
743 returns |failure_value|. 741 returns |failure_value|.
744 """ 742 """
745 c = Code() 743 c = Code()
746 accessor = '.' 744 accessor = '.'
747 if is_ptr: 745 if is_ptr:
748 accessor = '->' 746 accessor = '->'
749 cpp_type = self._type_helper.GetCppType(item_type, is_in_container=True) 747 cpp_type = self._type_helper.GetCppType(item_type, is_in_container=True)
750 c.Append('%s.reset(new std::vector<%s>);' % 748 c.Append('%s.reset(new std::vector<%s>);' %
751 (dst_var, cpp_util.PadForGenerics(cpp_type))) 749 (dst_var, cpp_util.PadForGenerics(cpp_type)))
752 (c.Sblock('for (base::ListValue::const_iterator it = %s->begin(); ' 750 (c.Sblock('for (base::ListValue::const_iterator it = %s->begin(); '
753 'it != %s->end(); ++it) {' % (src_var, src_var)) 751 'it != %s->end(); ++it) {' % (src_var, src_var))
754 .Append('%s tmp;' % self._type_helper.GetCppType(item_type)) 752 .Append('%s tmp;' % self._type_helper.GetCppType(item_type))
755 .Concat(self._GenerateStringToEnumConversion(item_type, 753 .Concat(self._GenerateStringToEnumConversion(item_type,
756 '(*it)', 754 '(*it)',
757 'tmp', 755 'tmp',
758 failure_value)) 756 failure_value))
759 .Append('%s%spush_back(tmp);' % (dst_var, accessor)) 757 .Append('%s%spush_back(tmp);' % (dst_var, accessor))
760 .Eblock('}') 758 .Eblock('}')
761 ) 759 )
762 return c 760 return c
763 761
764 def _GenerateStringToEnumConversion(self, 762 def _GenerateStringToEnumConversion(self,
765 type_, 763 type_,
766 src_var, 764 src_var,
767 dst_var, 765 dst_var,
768 failure_value): 766 failure_value):
769 """Returns Code that converts a string type in |src_var| to an enum with 767 """Returns Code that converts a string type in |src_var| to an enum with
770 type |type_| in |dst_var|. In the generated code, if |src_var| is not 768 type |type_| in |dst_var|. In the generated code, if |src_var| is not
771 a valid enum name then the function will return |failure_value|. 769 a valid enum name then the function will return |failure_value|.
772 """ 770 """
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
935 if self._generate_error_messages: 933 if self._generate_error_messages:
936 params = list(params) + ['base::string16* error'] 934 params = list(params) + ['base::string16* error']
937 return ', '.join(str(p) for p in params) 935 return ', '.join(str(p) for p in params)
938 936
939 def _GenerateArgs(self, args): 937 def _GenerateArgs(self, args):
940 """Builds the argument list for a function, given an array of arguments. 938 """Builds the argument list for a function, given an array of arguments.
941 """ 939 """
942 if self._generate_error_messages: 940 if self._generate_error_messages:
943 args = list(args) + ['error'] 941 args = list(args) + ['error']
944 return ', '.join(str(a) for a in args) 942 return ', '.join(str(a) for a in args)
OLDNEW
« no previous file with comments | « no previous file | tools/json_schema_compiler/compiler.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698