cc_generator.py revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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
3# found in the LICENSE file.
4
5from code import Code
6from model import PropertyType
7import cpp_util
8import schema_util
9import util_cc_helper
10
11class CCGenerator(object):
12  def __init__(self, type_generator, cpp_namespace_pattern):
13    self._type_generator = type_generator
14    self._cpp_namespace_pattern = cpp_namespace_pattern
15
16  def Generate(self, namespace):
17    return _Generator(namespace,
18                      self._type_generator,
19                      self._cpp_namespace_pattern).Generate()
20
21
22class _Generator(object):
23  """A .cc generator for a namespace.
24  """
25  def __init__(self, namespace, cpp_type_generator, cpp_namespace_pattern):
26    self._namespace = namespace
27    self._type_helper = cpp_type_generator
28    self._cpp_namespace_pattern = cpp_namespace_pattern
29    self._util_cc_helper = (
30        util_cc_helper.UtilCCHelper(self._type_helper))
31    self._generate_error_messages = namespace.compiler_options.get(
32        'generate_error_messages', False)
33
34  def Generate(self):
35    """Generates a Code object with the .cc for a single namespace.
36    """
37    cpp_namespace = cpp_util.GetCppNamespace(self._cpp_namespace_pattern,
38                                             self._namespace.unix_name)
39
40    c = Code()
41    (c.Append(cpp_util.CHROMIUM_LICENSE)
42      .Append()
43      .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file)
44      .Append()
45      .Append(self._util_cc_helper.GetIncludePath())
46      .Append('#include "base/logging.h"')
47      .Append('#include "base/strings/string_number_conversions.h"')
48      .Append('#include "base/strings/utf_string_conversions.h"')
49      .Append('#include "%s/%s.h"' %
50              (self._namespace.source_file_dir, self._namespace.short_filename))
51      .Append('#include <set>')
52      .Cblock(self._type_helper.GenerateIncludes(include_soft=True))
53      .Append()
54      .Append('using base::UTF8ToUTF16;')
55      .Append()
56      .Concat(cpp_util.OpenNamespace(cpp_namespace))
57    )
58    if self._namespace.properties:
59      (c.Append('//')
60        .Append('// Properties')
61        .Append('//')
62        .Append()
63      )
64      for property in self._namespace.properties.values():
65        property_code = self._type_helper.GeneratePropertyValues(
66            property,
67            'const %(type)s %(name)s = %(value)s;',
68            nodoc=True)
69        if property_code:
70          c.Cblock(property_code)
71    if self._namespace.types:
72      (c.Append('//')
73        .Append('// Types')
74        .Append('//')
75        .Append()
76        .Cblock(self._GenerateTypes(None, self._namespace.types.values()))
77      )
78    if self._namespace.functions:
79      (c.Append('//')
80        .Append('// Functions')
81        .Append('//')
82        .Append()
83      )
84    for function in self._namespace.functions.values():
85      c.Cblock(self._GenerateFunction(function))
86    if self._namespace.events:
87      (c.Append('//')
88        .Append('// Events')
89        .Append('//')
90        .Append()
91      )
92      for event in self._namespace.events.values():
93        c.Cblock(self._GenerateEvent(event))
94    c.Cblock(cpp_util.CloseNamespace(cpp_namespace))
95    c.Append()
96    return c
97
98  def _GenerateType(self, cpp_namespace, type_):
99    """Generates the function definitions for a type.
100    """
101    classname = cpp_util.Classname(schema_util.StripNamespace(type_.name))
102    c = Code()
103
104    if type_.functions:
105      # Wrap functions within types in the type's namespace.
106      (c.Append('namespace %s {' % classname)
107        .Append())
108      for function in type_.functions.values():
109        c.Cblock(self._GenerateFunction(function))
110      c.Append('}  // namespace %s' % classname)
111    elif type_.property_type == PropertyType.ARRAY:
112      c.Cblock(self._GenerateType(cpp_namespace, type_.item_type))
113    elif type_.property_type in (PropertyType.CHOICES,
114                                 PropertyType.OBJECT):
115      if cpp_namespace is None:
116        classname_in_namespace = classname
117      else:
118        classname_in_namespace = '%s::%s' % (cpp_namespace, classname)
119
120      if type_.property_type == PropertyType.OBJECT:
121        c.Cblock(self._GeneratePropertyFunctions(classname_in_namespace,
122                                                 type_.properties.values()))
123      else:
124        c.Cblock(self._GenerateTypes(classname_in_namespace, type_.choices))
125
126      (c.Append('%s::%s()' % (classname_in_namespace, classname))
127        .Cblock(self._GenerateInitializersAndBody(type_))
128        .Append('%s::~%s() {}' % (classname_in_namespace, classname))
129        .Append()
130      )
131      if type_.origin.from_json:
132        c.Cblock(self._GenerateTypePopulate(classname_in_namespace, type_))
133        if cpp_namespace is None:  # only generate for top-level types
134          c.Cblock(self._GenerateTypeFromValue(classname_in_namespace, type_))
135      if type_.origin.from_client:
136        c.Cblock(self._GenerateTypeToValue(classname_in_namespace, type_))
137    elif type_.property_type == PropertyType.ENUM:
138      (c.Cblock(self._GenerateEnumToString(cpp_namespace, type_))
139        .Cblock(self._GenerateEnumFromString(cpp_namespace, type_))
140      )
141
142    return c
143
144  def _GenerateInitializersAndBody(self, type_):
145    items = []
146    for prop in type_.properties.values():
147      t = prop.type_
148
149      real_t = self._type_helper.FollowRef(t)
150      if real_t.property_type == PropertyType.ENUM:
151        items.append('%s(%s)' % (
152            prop.unix_name,
153            self._type_helper.GetEnumNoneValue(t)))
154      elif prop.optional:
155        continue
156      elif t.property_type == PropertyType.INTEGER:
157        items.append('%s(0)' % prop.unix_name)
158      elif t.property_type == PropertyType.DOUBLE:
159        items.append('%s(0.0)' % prop.unix_name)
160      elif t.property_type == PropertyType.BOOLEAN:
161        items.append('%s(false)' % prop.unix_name)
162      elif (t.property_type == PropertyType.ANY or
163            t.property_type == PropertyType.ARRAY or
164            t.property_type == PropertyType.BINARY or  # mapped to std::string
165            t.property_type == PropertyType.CHOICES or
166            t.property_type == PropertyType.OBJECT or
167            t.property_type == PropertyType.FUNCTION or
168            t.property_type == PropertyType.REF or
169            t.property_type == PropertyType.STRING):
170        # TODO(miket): It would be nice to initialize CHOICES, but we
171        # don't presently have the semantics to indicate which one of a set
172        # should be the default.
173        continue
174      else:
175        raise TypeError(t)
176
177    if items:
178      s = ': %s' % (', '.join(items))
179    else:
180      s = ''
181    s = s + ' {}'
182    return Code().Append(s)
183
184  def _GenerateTypePopulate(self, cpp_namespace, type_):
185    """Generates the function for populating a type given a pointer to it.
186
187    E.g for type "Foo", generates Foo::Populate()
188    """
189    classname = cpp_util.Classname(schema_util.StripNamespace(type_.name))
190    c = Code()
191    (c.Append('// static')
192      .Append('bool %(namespace)s::Populate(')
193      .Sblock('    %s) {' % self._GenerateParams(
194          ('const base::Value& value', '%(name)s* out'))))
195
196    if self._generate_error_messages:
197      c.Append('DCHECK(error);')
198
199    if type_.property_type == PropertyType.CHOICES:
200      for choice in type_.choices:
201        (c.Sblock('if (%s) {' % self._GenerateValueIsTypeExpression('value',
202                                                                    choice))
203            .Concat(self._GeneratePopulateVariableFromValue(
204                choice,
205                '(&value)',
206                'out->as_%s' % choice.unix_name,
207                'false',
208                is_ptr=True))
209            .Append('return true;')
210          .Eblock('}')
211        )
212      (c.Concat(self._GenerateError(
213          '"expected %s, got " +  %s' %
214              (" or ".join(choice.name for choice in type_.choices),
215              self._util_cc_helper.GetValueTypeString('value'))))
216        .Append('return false;'))
217    elif type_.property_type == PropertyType.OBJECT:
218      (c.Sblock('if (!value.IsType(base::Value::TYPE_DICTIONARY)) {')
219        .Concat(self._GenerateError(
220          '"expected dictionary, got " + ' +
221          self._util_cc_helper.GetValueTypeString('value')))
222        .Append('return false;')
223        .Eblock('}'))
224
225      if type_.properties or type_.additional_properties is not None:
226        c.Append('const base::DictionaryValue* dict = '
227                     'static_cast<const base::DictionaryValue*>(&value);')
228        if self._generate_error_messages:
229            c.Append('std::set<std::string> keys;')
230      for prop in type_.properties.itervalues():
231        c.Concat(self._InitializePropertyToDefault(prop, 'out'))
232      for prop in type_.properties.itervalues():
233        if self._generate_error_messages:
234          c.Append('keys.insert("%s");' % (prop.name))
235        c.Concat(self._GenerateTypePopulateProperty(prop, 'dict', 'out'))
236      # Check for extra values.
237      if self._generate_error_messages:
238        (c.Sblock('for (base::DictionaryValue::Iterator it(*dict); '
239                       '!it.IsAtEnd(); it.Advance()) {')
240          .Sblock('if (!keys.count(it.key())) {')
241          .Concat(self._GenerateError('"found unexpected key \'" + '
242                                          'it.key() + "\'"'))
243          .Eblock('}')
244          .Eblock('}')
245        )
246      if type_.additional_properties is not None:
247        if type_.additional_properties.property_type == PropertyType.ANY:
248          c.Append('out->additional_properties.MergeDictionary(dict);')
249        else:
250          cpp_type = self._type_helper.GetCppType(type_.additional_properties,
251                                                  is_in_container=True)
252          (c.Append('for (base::DictionaryValue::Iterator it(*dict);')
253            .Sblock('     !it.IsAtEnd(); it.Advance()) {')
254              .Append('%s tmp;' % cpp_type)
255              .Concat(self._GeneratePopulateVariableFromValue(
256                  type_.additional_properties,
257                  '(&it.value())',
258                  'tmp',
259                  'false'))
260              .Append('out->additional_properties[it.key()] = tmp;')
261            .Eblock('}')
262          )
263      c.Append('return true;')
264    (c.Eblock('}')
265      .Substitute({'namespace': cpp_namespace, 'name': classname}))
266    return c
267
268  def _GenerateValueIsTypeExpression(self, var, type_):
269    real_type = self._type_helper.FollowRef(type_)
270    if real_type.property_type is PropertyType.CHOICES:
271      return '(%s)' % ' || '.join(self._GenerateValueIsTypeExpression(var,
272                                                                      choice)
273                                  for choice in real_type.choices)
274    return '%s.IsType(%s)' % (var, cpp_util.GetValueType(real_type))
275
276  def _GenerateTypePopulateProperty(self, prop, src, dst):
277    """Generate the code to populate a single property in a type.
278
279    src: base::DictionaryValue*
280    dst: Type*
281    """
282    c = Code()
283    value_var = prop.unix_name + '_value'
284    c.Append('const base::Value* %(value_var)s = NULL;')
285    if prop.optional:
286      (c.Sblock(
287          'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {')
288        .Concat(self._GeneratePopulatePropertyFromValue(
289            prop, value_var, dst, 'false')))
290      underlying_type = self._type_helper.FollowRef(prop.type_)
291      if underlying_type.property_type == PropertyType.ENUM:
292        (c.Append('} else {')
293          .Append('%%(dst)s->%%(name)s = %s;' %
294              self._type_helper.GetEnumNoneValue(prop.type_)))
295      c.Eblock('}')
296    else:
297      (c.Sblock(
298          'if (!%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {')
299        .Concat(self._GenerateError('"\'%%(key)s\' is required"'))
300        .Append('return false;')
301        .Eblock('}')
302        .Concat(self._GeneratePopulatePropertyFromValue(
303            prop, value_var, dst, 'false'))
304      )
305    c.Append()
306    c.Substitute({
307      'value_var': value_var,
308      'key': prop.name,
309      'src': src,
310      'dst': dst,
311      'name': prop.unix_name
312    })
313    return c
314
315  def _GenerateTypeFromValue(self, cpp_namespace, type_):
316    classname = cpp_util.Classname(schema_util.StripNamespace(type_.name))
317    c = Code()
318    (c.Append('// static')
319      .Append('scoped_ptr<%s> %s::FromValue(%s) {' % (classname,
320        cpp_namespace, self._GenerateParams(('const base::Value& value',))))
321    )
322    if self._generate_error_messages:
323      c.Append('DCHECK(error);')
324    (c.Append('  scoped_ptr<%s> out(new %s());' % (classname, classname))
325      .Append('  if (!Populate(%s))' % self._GenerateArgs(
326          ('value', 'out.get()')))
327      .Append('    return scoped_ptr<%s>();' % classname)
328      .Append('  return out.Pass();')
329      .Append('}')
330    )
331    return c
332
333  def _GenerateTypeToValue(self, cpp_namespace, type_):
334    """Generates a function that serializes the type into a base::Value.
335    E.g. for type "Foo" generates Foo::ToValue()
336    """
337    if type_.property_type == PropertyType.OBJECT:
338      return self._GenerateObjectTypeToValue(cpp_namespace, type_)
339    elif type_.property_type == PropertyType.CHOICES:
340      return self._GenerateChoiceTypeToValue(cpp_namespace, type_)
341    else:
342      raise ValueError("Unsupported property type %s" % type_.type_)
343
344  def _GenerateObjectTypeToValue(self, cpp_namespace, type_):
345    """Generates a function that serializes an object-representing type
346    into a base::DictionaryValue.
347    """
348    c = Code()
349    (c.Sblock('scoped_ptr<base::DictionaryValue> %s::ToValue() const {' %
350          cpp_namespace)
351        .Append('scoped_ptr<base::DictionaryValue> value('
352                    'new base::DictionaryValue());')
353        .Append()
354    )
355
356    for prop in type_.properties.values():
357      prop_var = 'this->%s' % prop.unix_name
358      if prop.optional:
359        # Optional enum values are generated with a NONE enum value.
360        underlying_type = self._type_helper.FollowRef(prop.type_)
361        if underlying_type.property_type == PropertyType.ENUM:
362          c.Sblock('if (%s != %s) {' %
363              (prop_var,
364               self._type_helper.GetEnumNoneValue(prop.type_)))
365        else:
366          c.Sblock('if (%s.get()) {' % prop_var)
367
368      # ANY is a base::Value which is abstract and cannot be a direct member, so
369      # it will always be a pointer.
370      is_ptr = prop.optional or prop.type_.property_type == PropertyType.ANY
371      c.Cblock(self._CreateValueFromType(
372          'value->SetWithoutPathExpansion("%s", %%s);' % prop.name,
373          prop.name,
374          prop.type_,
375          prop_var,
376          is_ptr=is_ptr))
377
378      if prop.optional:
379        c.Eblock('}')
380
381    if type_.additional_properties is not None:
382      if type_.additional_properties.property_type == PropertyType.ANY:
383        c.Append('value->MergeDictionary(&additional_properties);')
384      else:
385        # Non-copyable types will be wrapped in a linked_ptr for inclusion in
386        # maps, so we need to unwrap them.
387        needs_unwrap = (
388            not self._type_helper.IsCopyable(type_.additional_properties))
389        cpp_type = self._type_helper.GetCppType(type_.additional_properties,
390                                                is_in_container=True)
391        (c.Sblock('for (std::map<std::string, %s>::const_iterator it =' %
392                      cpp_util.PadForGenerics(cpp_type))
393          .Append('       additional_properties.begin();')
394          .Append('   it != additional_properties.end(); ++it) {')
395          .Cblock(self._CreateValueFromType(
396              'value->SetWithoutPathExpansion(it->first, %s);',
397              type_.additional_properties.name,
398              type_.additional_properties,
399              '%sit->second' % ('*' if needs_unwrap else '')))
400          .Eblock('}')
401        )
402
403    return (c.Append()
404             .Append('return value.Pass();')
405           .Eblock('}'))
406
407  def _GenerateChoiceTypeToValue(self, cpp_namespace, type_):
408    """Generates a function that serializes a choice-representing type
409    into a base::Value.
410    """
411    c = Code()
412    c.Sblock('scoped_ptr<base::Value> %s::ToValue() const {' % cpp_namespace)
413    c.Append('scoped_ptr<base::Value> result;')
414    for choice in type_.choices:
415      choice_var = 'as_%s' % choice.unix_name
416      (c.Sblock('if (%s) {' % choice_var)
417          .Append('DCHECK(!result) << "Cannot set multiple choices for %s";' %
418                      type_.unix_name)
419          .Cblock(self._CreateValueFromType('result.reset(%s);',
420                                            choice.name,
421                                            choice,
422                                            '*%s' % choice_var))
423        .Eblock('}')
424      )
425    (c.Append('DCHECK(result) << "Must set at least one choice for %s";' %
426                  type_.unix_name)
427      .Append('return result.Pass();')
428      .Eblock('}')
429    )
430    return c
431
432  def _GenerateFunction(self, function):
433    """Generates the definitions for function structs.
434    """
435    c = Code()
436
437    # TODO(kalman): use function.unix_name not Classname.
438    function_namespace = cpp_util.Classname(function.name)
439    # Windows has a #define for SendMessage, so to avoid any issues, we need
440    # to not use the name.
441    if function_namespace == 'SendMessage':
442      function_namespace = 'PassMessage'
443    (c.Append('namespace %s {' % function_namespace)
444      .Append()
445    )
446
447    # Params::Populate function
448    if function.params:
449      c.Concat(self._GeneratePropertyFunctions('Params', function.params))
450      (c.Append('Params::Params() {}')
451        .Append('Params::~Params() {}')
452        .Append()
453        .Cblock(self._GenerateFunctionParamsCreate(function))
454      )
455
456    # Results::Create function
457    if function.callback:
458      c.Concat(self._GenerateCreateCallbackArguments(function_namespace,
459                                                     'Results',
460                                                     function.callback))
461
462    c.Append('}  // namespace %s' % function_namespace)
463    return c
464
465  def _GenerateEvent(self, event):
466    # TODO(kalman): use event.unix_name not Classname.
467    c = Code()
468    event_namespace = cpp_util.Classname(event.name)
469    (c.Append('namespace %s {' % event_namespace)
470      .Append()
471      .Cblock(self._GenerateEventNameConstant(None, event))
472      .Cblock(self._GenerateCreateCallbackArguments(event_namespace,
473                                                    None,
474                                                    event))
475      .Append('}  // namespace %s' % event_namespace)
476    )
477    return c
478
479  def _CreateValueFromType(self, code, prop_name, type_, var, is_ptr=False):
480    """Creates a base::Value given a type. Generated code passes ownership
481    to caller.
482
483    var: variable or variable*
484
485    E.g for std::string, generate new base::StringValue(var)
486    """
487    c = Code()
488    underlying_type = self._type_helper.FollowRef(type_)
489    if underlying_type.property_type == PropertyType.ARRAY:
490      # Enums are treated specially because C++ templating thinks that they're
491      # ints, but really they're strings. So we create a vector of strings and
492      # populate it with the names of the enum in the array. The |ToString|
493      # function of the enum can be in another namespace when the enum is
494      # referenced. Templates can not be used here because C++ templating does
495      # not support passing a namespace as an argument.
496      item_type = self._type_helper.FollowRef(underlying_type.item_type)
497      if item_type.property_type == PropertyType.ENUM:
498        vardot = '(%s)%s' % (var, '->' if is_ptr else '.')
499
500        maybe_namespace = ''
501        if type_.item_type.property_type == PropertyType.REF:
502          maybe_namespace = '%s::' % item_type.namespace.unix_name
503
504        enum_list_var = '%s_list' % prop_name
505        # Scope the std::vector variable declaration inside braces.
506        (c.Sblock('{')
507          .Append('std::vector<std::string> %s;' % enum_list_var)
508          .Append('for (std::vector<%s>::const_iterator it = %sbegin();'
509              % (self._type_helper.GetCppType(item_type), vardot))
510          .Sblock('    it != %send(); ++it) {' % vardot)
511          .Append('%s.push_back(%sToString(*it));' % (enum_list_var,
512                                                      maybe_namespace))
513          .Eblock('}'))
514
515        # Because the std::vector above is always created for both required and
516        # optional enum arrays, |is_ptr| is set to false and uses the
517        # std::vector to create the values.
518        (c.Append(code %
519            self._GenerateCreateValueFromType(type_, enum_list_var, False))
520          .Eblock('}'))
521        return c
522
523    c.Append(code % self._GenerateCreateValueFromType(type_, var, is_ptr))
524    return c
525
526  def _GenerateCreateValueFromType(self, type_, var, is_ptr):
527    """Generates the statement to create a base::Value given a type.
528
529    type_:  The type of the values being converted.
530    var:    The name of the variable.
531    is_ptr: Whether |type_| is optional.
532    """
533    underlying_type = self._type_helper.FollowRef(type_)
534    if (underlying_type.property_type == PropertyType.CHOICES or
535        underlying_type.property_type == PropertyType.OBJECT):
536      if is_ptr:
537        return '(%s)->ToValue().release()' % var
538      else:
539        return '(%s).ToValue().release()' % var
540    elif (underlying_type.property_type == PropertyType.ANY or
541          underlying_type.property_type == PropertyType.FUNCTION):
542      if is_ptr:
543        vardot = '(%s)->' % var
544      else:
545        vardot = '(%s).' % var
546      return '%sDeepCopy()' % vardot
547    elif underlying_type.property_type == PropertyType.ENUM:
548      maybe_namespace = ''
549      if type_.property_type == PropertyType.REF:
550        maybe_namespace = '%s::' % underlying_type.namespace.unix_name
551      return 'new base::StringValue(%sToString(%s))' % (maybe_namespace, var)
552    elif underlying_type.property_type == PropertyType.BINARY:
553      if is_ptr:
554        vardot = var + '->'
555      else:
556        vardot = var + '.'
557      return ('base::BinaryValue::CreateWithCopiedBuffer(%sdata(), %ssize())' %
558              (vardot, vardot))
559    elif underlying_type.property_type == PropertyType.ARRAY:
560      return '%s.release()' % self._util_cc_helper.CreateValueFromArray(
561          var,
562          is_ptr)
563    elif underlying_type.property_type.is_fundamental:
564      if is_ptr:
565        var = '*%s' % var
566      if underlying_type.property_type == PropertyType.STRING:
567        return 'new base::StringValue(%s)' % var
568      else:
569        return 'new base::FundamentalValue(%s)' % var
570    else:
571      raise NotImplementedError('Conversion of %s to base::Value not '
572                                'implemented' % repr(type_.type_))
573
574  def _GenerateParamsCheck(self, function, var):
575    """Generates a check for the correct number of arguments when creating
576    Params.
577    """
578    c = Code()
579    num_required = 0
580    for param in function.params:
581      if not param.optional:
582        num_required += 1
583    if num_required == len(function.params):
584      c.Sblock('if (%(var)s.GetSize() != %(total)d) {')
585    elif not num_required:
586      c.Sblock('if (%(var)s.GetSize() > %(total)d) {')
587    else:
588      c.Sblock('if (%(var)s.GetSize() < %(required)d'
589          ' || %(var)s.GetSize() > %(total)d) {')
590    (c.Concat(self._GenerateError(
591        '"expected %%(total)d arguments, got " '
592        '+ base::IntToString(%%(var)s.GetSize())'))
593      .Append('return scoped_ptr<Params>();')
594      .Eblock('}')
595      .Substitute({
596        'var': var,
597        'required': num_required,
598        'total': len(function.params),
599    }))
600    return c
601
602  def _GenerateFunctionParamsCreate(self, function):
603    """Generate function to create an instance of Params. The generated
604    function takes a base::ListValue of arguments.
605
606    E.g for function "Bar", generate Bar::Params::Create()
607    """
608    c = Code()
609    (c.Append('// static')
610      .Sblock('scoped_ptr<Params> Params::Create(%s) {' % self._GenerateParams(
611        ['const base::ListValue& args']))
612    )
613    if self._generate_error_messages:
614      c.Append('DCHECK(error);')
615    (c.Concat(self._GenerateParamsCheck(function, 'args'))
616      .Append('scoped_ptr<Params> params(new Params());')
617    )
618
619    for param in function.params:
620      c.Concat(self._InitializePropertyToDefault(param, 'params'))
621
622    for i, param in enumerate(function.params):
623      # Any failure will cause this function to return. If any argument is
624      # incorrect or missing, those following it are not processed. Note that
625      # for optional arguments, we allow missing arguments and proceed because
626      # there may be other arguments following it.
627      failure_value = 'scoped_ptr<Params>()'
628      c.Append()
629      value_var = param.unix_name + '_value'
630      (c.Append('const base::Value* %(value_var)s = NULL;')
631        .Append('if (args.Get(%(i)s, &%(value_var)s) &&')
632        .Sblock('    !%(value_var)s->IsType(base::Value::TYPE_NULL)) {')
633        .Concat(self._GeneratePopulatePropertyFromValue(
634            param, value_var, 'params', failure_value))
635        .Eblock('}')
636      )
637      if not param.optional:
638        (c.Sblock('else {')
639          .Concat(self._GenerateError('"\'%%(key)s\' is required"'))
640          .Append('return %s;' % failure_value)
641          .Eblock('}'))
642      c.Substitute({'value_var': value_var, 'i': i, 'key': param.name})
643    (c.Append()
644      .Append('return params.Pass();')
645      .Eblock('}')
646      .Append()
647    )
648
649    return c
650
651  def _GeneratePopulatePropertyFromValue(self,
652                                         prop,
653                                         src_var,
654                                         dst_class_var,
655                                         failure_value):
656    """Generates code to populate property |prop| of |dst_class_var| (a
657    pointer) from a Value*. See |_GeneratePopulateVariableFromValue| for
658    semantics.
659    """
660    return self._GeneratePopulateVariableFromValue(prop.type_,
661                                                   src_var,
662                                                   '%s->%s' % (dst_class_var,
663                                                               prop.unix_name),
664                                                   failure_value,
665                                                   is_ptr=prop.optional)
666
667  def _GeneratePopulateVariableFromValue(self,
668                                         type_,
669                                         src_var,
670                                         dst_var,
671                                         failure_value,
672                                         is_ptr=False):
673    """Generates code to populate a variable |dst_var| of type |type_| from a
674    Value* at |src_var|. The Value* is assumed to be non-NULL. In the generated
675    code, if |dst_var| fails to be populated then Populate will return
676    |failure_value|.
677    """
678    c = Code()
679
680    underlying_type = self._type_helper.FollowRef(type_)
681
682    if underlying_type.property_type.is_fundamental:
683      if is_ptr:
684        (c.Append('%(cpp_type)s temp;')
685          .Sblock('if (!%s) {' % cpp_util.GetAsFundamentalValue(
686                      self._type_helper.FollowRef(type_), src_var, '&temp'))
687          .Concat(self._GenerateError(
688            '"\'%%(key)s\': expected ' + '%s, got " + %s' % (
689                type_.name,
690                self._util_cc_helper.GetValueTypeString(
691                    '%%(src_var)s', True)))))
692        c.Append('%(dst_var)s.reset();')
693        if not self._generate_error_messages:
694          c.Append('return %(failure_value)s;')
695        (c.Eblock('}')
696          .Append('else')
697          .Append('  %(dst_var)s.reset(new %(cpp_type)s(temp));')
698        )
699      else:
700        (c.Sblock('if (!%s) {' % cpp_util.GetAsFundamentalValue(
701                      self._type_helper.FollowRef(type_),
702                      src_var,
703                      '&%s' % dst_var))
704          .Concat(self._GenerateError(
705            '"\'%%(key)s\': expected ' + '%s, got " + %s' % (
706                type_.name,
707                self._util_cc_helper.GetValueTypeString(
708                    '%%(src_var)s', True))))
709          .Append('return %(failure_value)s;')
710          .Eblock('}')
711        )
712    elif underlying_type.property_type == PropertyType.OBJECT:
713      if is_ptr:
714        (c.Append('const base::DictionaryValue* dictionary = NULL;')
715          .Sblock('if (!%(src_var)s->GetAsDictionary(&dictionary)) {')
716          .Concat(self._GenerateError(
717            '"\'%%(key)s\': expected dictionary, got " + ' +
718            self._util_cc_helper.GetValueTypeString('%%(src_var)s', True))))
719        # If an optional property fails to populate, the population can still
720        # succeed with a warning. If no error messages are generated, this
721        # warning is not set and we fail out instead.
722        if not self._generate_error_messages:
723          c.Append('return %(failure_value)s;')
724        (c.Eblock('}')
725          .Sblock('else {')
726          .Append('scoped_ptr<%(cpp_type)s> temp(new %(cpp_type)s());')
727          .Append('if (!%%(cpp_type)s::Populate(%s)) {' % self._GenerateArgs(
728            ('*dictionary', 'temp.get()')))
729          .Append('  return %(failure_value)s;')
730        )
731        (c.Append('}')
732          .Append('else')
733          .Append('  %(dst_var)s = temp.Pass();')
734          .Eblock('}')
735        )
736      else:
737        (c.Append('const base::DictionaryValue* dictionary = NULL;')
738          .Sblock('if (!%(src_var)s->GetAsDictionary(&dictionary)) {')
739          .Concat(self._GenerateError(
740            '"\'%%(key)s\': expected dictionary, got " + ' +
741            self._util_cc_helper.GetValueTypeString('%%(src_var)s', True)))
742          .Append('return %(failure_value)s;')
743          .Eblock('}')
744          .Append('if (!%%(cpp_type)s::Populate(%s)) {' % self._GenerateArgs(
745            ('*dictionary', '&%(dst_var)s')))
746          .Append('  return %(failure_value)s;')
747          .Append('}')
748        )
749    elif underlying_type.property_type == PropertyType.FUNCTION:
750      if is_ptr:
751        c.Append('%(dst_var)s.reset(new base::DictionaryValue());')
752    elif underlying_type.property_type == PropertyType.ANY:
753      c.Append('%(dst_var)s.reset(%(src_var)s->DeepCopy());')
754    elif underlying_type.property_type == PropertyType.ARRAY:
755      # util_cc_helper deals with optional and required arrays
756      (c.Append('const base::ListValue* list = NULL;')
757        .Sblock('if (!%(src_var)s->GetAsList(&list)) {')
758          .Concat(self._GenerateError(
759            '"\'%%(key)s\': expected list, got " + ' +
760            self._util_cc_helper.GetValueTypeString('%%(src_var)s', True)))
761      )
762      if is_ptr and self._generate_error_messages:
763        c.Append('%(dst_var)s.reset();')
764      else:
765        c.Append('return %(failure_value)s;')
766      c.Eblock('}')
767      c.Sblock('else {')
768      item_type = self._type_helper.FollowRef(underlying_type.item_type)
769      if item_type.property_type == PropertyType.ENUM:
770        c.Concat(self._GenerateListValueToEnumArrayConversion(
771                     item_type,
772                     'list',
773                     dst_var,
774                     failure_value,
775                     is_ptr=is_ptr))
776      else:
777        (c.Sblock('if (!%s) {' % self._util_cc_helper.PopulateArrayFromList(
778              'list',
779              dst_var,
780              is_ptr)))
781        c.Concat(self._GenerateError(
782            '"unable to populate array \'%%(parent_key)s\'"'))
783        if is_ptr and self._generate_error_messages:
784          c.Append('%(dst_var)s.reset();')
785        else:
786          c.Append('return %(failure_value)s;')
787        c.Eblock('}')
788      c.Eblock('}')
789    elif underlying_type.property_type == PropertyType.CHOICES:
790      if is_ptr:
791        (c.Append('scoped_ptr<%(cpp_type)s> temp(new %(cpp_type)s());')
792          .Append('if (!%%(cpp_type)s::Populate(%s))' % self._GenerateArgs(
793            ('*%(src_var)s', 'temp.get()')))
794          .Append('  return %(failure_value)s;')
795          .Append('%(dst_var)s = temp.Pass();')
796        )
797      else:
798        (c.Append('if (!%%(cpp_type)s::Populate(%s))' % self._GenerateArgs(
799            ('*%(src_var)s', '&%(dst_var)s')))
800          .Append('  return %(failure_value)s;'))
801    elif underlying_type.property_type == PropertyType.ENUM:
802      c.Concat(self._GenerateStringToEnumConversion(underlying_type,
803                                                    src_var,
804                                                    dst_var,
805                                                    failure_value))
806    elif underlying_type.property_type == PropertyType.BINARY:
807      (c.Append('const base::BinaryValue* binary_value = NULL;')
808        .Sblock('if (!%(src_var)s->IsType(base::Value::TYPE_BINARY)) {')
809        .Concat(self._GenerateError(
810          '"\'%%(key)s\': expected binary, got " + ' +
811          self._util_cc_helper.GetValueTypeString('%%(src_var)s', True)))
812      )
813      if not self._generate_error_messages:
814        c.Append('return %(failure_value)s;')
815      (c.Eblock('}')
816        .Sblock('else {')
817        .Append(' binary_value =')
818        .Append('   static_cast<const base::BinaryValue*>(%(src_var)s);')
819      )
820      if is_ptr:
821        (c.Append('%(dst_var)s.reset(')
822          .Append('    new std::string(binary_value->GetBuffer(),')
823          .Append('                    binary_value->GetSize()));')
824        )
825      else:
826        (c.Append('%(dst_var)s.assign(binary_value->GetBuffer(),')
827          .Append('                   binary_value->GetSize());')
828        )
829      c.Eblock('}')
830    else:
831      raise NotImplementedError(type_)
832    if c.IsEmpty():
833      return c
834    return Code().Sblock('{').Concat(c.Substitute({
835      'cpp_type': self._type_helper.GetCppType(type_),
836      'src_var': src_var,
837      'dst_var': dst_var,
838      'failure_value': failure_value,
839      'key': type_.name,
840      'parent_key': type_.parent.name,
841    })).Eblock('}')
842
843  def _GenerateListValueToEnumArrayConversion(self,
844                                              item_type,
845                                              src_var,
846                                              dst_var,
847                                              failure_value,
848                                              is_ptr=False):
849    """Returns Code that converts a ListValue of string constants from
850    |src_var| into an array of enums of |type_| in |dst_var|. On failure,
851    returns |failure_value|.
852    """
853    c = Code()
854    accessor = '.'
855    if is_ptr:
856      accessor = '->'
857      cpp_type = self._type_helper.GetCppType(item_type, is_in_container=True)
858      c.Append('%s.reset(new std::vector<%s>);' %
859                   (dst_var, cpp_util.PadForGenerics(cpp_type)))
860    (c.Sblock('for (base::ListValue::const_iterator it = %s->begin(); '
861                   'it != %s->end(); ++it) {' % (src_var, src_var))
862      .Append('%s tmp;' % self._type_helper.GetCppType(item_type))
863      .Concat(self._GenerateStringToEnumConversion(item_type,
864                                                   '(*it)',
865                                                   'tmp',
866                                                   failure_value))
867      .Append('%s%spush_back(tmp);' % (dst_var, accessor))
868      .Eblock('}')
869    )
870    return c
871
872  def _GenerateStringToEnumConversion(self,
873                                      type_,
874                                      src_var,
875                                      dst_var,
876                                      failure_value):
877    """Returns Code that converts a string type in |src_var| to an enum with
878    type |type_| in |dst_var|. In the generated code, if |src_var| is not
879    a valid enum name then the function will return |failure_value|.
880    """
881    if type_.property_type != PropertyType.ENUM:
882      raise TypeError(type_)
883    c = Code()
884    enum_as_string = '%s_as_string' % type_.unix_name
885    cpp_type_namespace = ''
886    if type_.namespace != self._namespace:
887      cpp_type_namespace = '%s::' % type_.namespace.unix_name
888    cpp_type_name = self._type_helper.GetCppType(type_)
889    (c.Append('std::string %s;' % enum_as_string)
890      .Sblock('if (!%s->GetAsString(&%s)) {' % (src_var, enum_as_string))
891      .Concat(self._GenerateError(
892        '"\'%%(key)s\': expected string, got " + ' +
893        self._util_cc_helper.GetValueTypeString('%%(src_var)s', True)))
894      .Append('return %s;' % failure_value)
895      .Eblock('}')
896      .Append('%s = %sParse%s(%s);' % (dst_var,
897                                       cpp_type_namespace,
898                                       cpp_util.Classname(type_.name),
899                                       enum_as_string))
900      .Sblock('if (%s == %s%s) {' % (dst_var,
901                                     cpp_type_namespace,
902                                     self._type_helper.GetEnumNoneValue(type_)))
903      .Concat(self._GenerateError(
904        '\"\'%%(key)s\': expected \\"' +
905        '\\" or \\"'.join(
906            enum_value.name
907            for enum_value in self._type_helper.FollowRef(type_).enum_values) +
908        '\\", got \\"" + %s + "\\""' % enum_as_string))
909      .Append('return %s;' % failure_value)
910      .Eblock('}')
911      .Substitute({'src_var': src_var, 'key': type_.name})
912    )
913    return c
914
915  def _GeneratePropertyFunctions(self, namespace, params):
916    """Generates the member functions for a list of parameters.
917    """
918    return self._GenerateTypes(namespace, (param.type_ for param in params))
919
920  def _GenerateTypes(self, namespace, types):
921    """Generates the member functions for a list of types.
922    """
923    c = Code()
924    for type_ in types:
925      c.Cblock(self._GenerateType(namespace, type_))
926    return c
927
928  def _GenerateEnumToString(self, cpp_namespace, type_):
929    """Generates ToString() which gets the string representation of an enum.
930    """
931    c = Code()
932    classname = cpp_util.Classname(schema_util.StripNamespace(type_.name))
933
934    if cpp_namespace is not None:
935      c.Append('// static')
936    maybe_namespace = '' if cpp_namespace is None else '%s::' % cpp_namespace
937
938    c.Sblock('std::string %sToString(%s enum_param) {' %
939                 (maybe_namespace, classname))
940    c.Sblock('switch (enum_param) {')
941    for enum_value in self._type_helper.FollowRef(type_).enum_values:
942      name = enum_value.name
943      if 'camel_case_enum_to_string' in self._namespace.compiler_options:
944        name = enum_value.CamelName()
945      (c.Append('case %s: ' % self._type_helper.GetEnumValue(type_, enum_value))
946        .Append('  return "%s";' % name))
947    (c.Append('case %s:' % self._type_helper.GetEnumNoneValue(type_))
948      .Append('  return "";')
949      .Eblock('}')
950      .Append('NOTREACHED();')
951      .Append('return "";')
952      .Eblock('}')
953    )
954    return c
955
956  def _GenerateEnumFromString(self, cpp_namespace, type_):
957    """Generates FromClassNameString() which gets an enum from its string
958    representation.
959    """
960    c = Code()
961    classname = cpp_util.Classname(schema_util.StripNamespace(type_.name))
962
963    if cpp_namespace is not None:
964      c.Append('// static')
965    maybe_namespace = '' if cpp_namespace is None else '%s::' % cpp_namespace
966
967    c.Sblock('%s%s %sParse%s(const std::string& enum_string) {' %
968                 (maybe_namespace, classname, maybe_namespace, classname))
969    for i, enum_value in enumerate(
970          self._type_helper.FollowRef(type_).enum_values):
971      # This is broken up into all ifs with no else ifs because we get
972      # "fatal error C1061: compiler limit : blocks nested too deeply"
973      # on Windows.
974      (c.Append('if (enum_string == "%s")' % enum_value.name)
975        .Append('  return %s;' %
976            self._type_helper.GetEnumValue(type_, enum_value)))
977    (c.Append('return %s;' % self._type_helper.GetEnumNoneValue(type_))
978      .Eblock('}')
979    )
980    return c
981
982  def _GenerateCreateCallbackArguments(self,
983                                       cpp_namespace,
984                                       function_scope,
985                                       callback):
986    """Generate all functions to create Value parameters for a callback.
987
988    E.g for function "Bar", generate Bar::Results::Create
989    E.g for event "Baz", generate Baz::Create
990
991    function_scope: the function scope path, e.g. Foo::Bar for the function
992                    Foo::Bar::Baz(). May be None if there is no function scope.
993    callback: the Function object we are creating callback arguments for.
994    """
995    c = Code()
996    params = callback.params
997    c.Concat(self._GeneratePropertyFunctions(function_scope, params))
998
999    (c.Sblock('scoped_ptr<base::ListValue> %(function_scope)s'
1000                  'Create(%(declaration_list)s) {')
1001      .Append('scoped_ptr<base::ListValue> create_results('
1002              'new base::ListValue());')
1003    )
1004    declaration_list = []
1005    for param in params:
1006      declaration_list.append(cpp_util.GetParameterDeclaration(
1007          param, self._type_helper.GetCppType(param.type_)))
1008      c.Cblock(self._CreateValueFromType('create_results->Append(%s);',
1009                                         param.name,
1010                                         param.type_,
1011                                         param.unix_name))
1012    c.Append('return create_results.Pass();')
1013    c.Eblock('}')
1014    c.Substitute({
1015        'function_scope': ('%s::' % function_scope) if function_scope else '',
1016        'declaration_list': ', '.join(declaration_list),
1017        'param_names': ', '.join(param.unix_name for param in params)
1018    })
1019    return c
1020
1021  def _GenerateEventNameConstant(self, function_scope, event):
1022    """Generates a constant string array for the event name.
1023    """
1024    c = Code()
1025    c.Append('const char kEventName[] = "%s.%s";' % (
1026                 self._namespace.name, event.name))
1027    return c
1028
1029  def _InitializePropertyToDefault(self, prop, dst):
1030    """Initialize a model.Property to its default value inside an object.
1031
1032    E.g for optional enum "state", generate dst->state = STATE_NONE;
1033
1034    dst: Type*
1035    """
1036    c = Code()
1037    underlying_type = self._type_helper.FollowRef(prop.type_)
1038    if (underlying_type.property_type == PropertyType.ENUM and
1039        prop.optional):
1040      c.Append('%s->%s = %s;' % (
1041        dst,
1042        prop.unix_name,
1043        self._type_helper.GetEnumNoneValue(prop.type_)))
1044    return c
1045
1046  def _GenerateError(self, body):
1047    """Generates an error message pertaining to population failure.
1048
1049    E.g 'expected bool, got int'
1050    """
1051    c = Code()
1052    if not self._generate_error_messages:
1053      return c
1054    (c.Append('if (error->length())')
1055      .Append('  error->append(UTF8ToUTF16("; "));')
1056      .Append('error->append(UTF8ToUTF16(%s));' % body))
1057    return c
1058
1059  def _GenerateParams(self, params):
1060    """Builds the parameter list for a function, given an array of parameters.
1061    """
1062    if self._generate_error_messages:
1063      params = list(params) + ['base::string16* error']
1064    return ', '.join(str(p) for p in params)
1065
1066  def _GenerateArgs(self, args):
1067    """Builds the argument list for a function, given an array of arguments.
1068    """
1069    if self._generate_error_messages:
1070      args = list(args) + ['error']
1071    return ', '.join(str(a) for a in args)
1072