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