summaryrefslogtreecommitdiffstats
path: root/tools/json_schema_compiler/cc_generator.py
blob: d0c57decdf8deabcf78dd63adb105fc611407945 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from model import PropertyType
import code
import cpp_util

class CCGenerator(object):
  """A .cc generator for a namespace.
  """
  def __init__(self, namespace, cpp_type_generator):
    self._cpp_type_generator = cpp_type_generator
    self._namespace = namespace
    self._target_namespace = (
        self._cpp_type_generator.GetCppNamespaceName(self._namespace))

  def Generate(self):
    """Generates a code.Code object with the .cc for a single namespace.
    """
    c = code.Code()
    (c.Append(cpp_util.CHROMIUM_LICENSE)
      .Append()
      .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file)
      .Append()
      .Append('#include "tools/json_schema_compiler/util.h"')
      .Append('#include "%s/%s.h"' %
          (self._namespace.source_file_dir, self._target_namespace))
      .Append()
      .Concat(self._cpp_type_generator.GetCppNamespaceStart())
      .Append()
      .Append('//')
      .Append('// Types')
      .Append('//')
      .Append()
    )
    for type_ in self._namespace.types.values():
      (c.Concat(self._GenerateType(type_))
        .Append()
      )
    (c.Append('//')
      .Append('// Functions')
      .Append('//')
      .Append()
    )
    for function in self._namespace.functions.values():
      (c.Concat(self._GenerateFunction(function))
        .Append()
      )
    (c.Concat(self._cpp_type_generator.GetCppNamespaceEnd())
      .Append()
    )
    # TODO(calamity): Events
    return c

  def _GenerateType(self, type_):
    """Generates the function definitions for a type.
    """
    c = code.Code()

    (c.Append('%(classname)s::%(classname)s() {}')
      .Append('%(classname)s::~%(classname)s() {}')
      .Append()
    )
    c.Substitute({'classname': type_.name})

    c.Concat(self._GenerateTypePopulate(type_))
    c.Append()
    # TODO(calamity): deal with non-serializable
    c.Concat(self._GenerateTypeTovalue(type_))
    c.Append()

    return c

  def _GenerateTypePopulate(self, type_):
    """Generates the function for populating a type given a pointer to it.
    """
    c = code.Code()
    (c.Append('// static')
      .Sblock('bool %(name)s::Populate(const Value& value, %(name)s* out) {')
        .Append('if (!value.IsType(Value::TYPE_DICTIONARY))')
        .Append('  return false;')
        .Append('const DictionaryValue* dict = '
                'static_cast<const DictionaryValue*>(&value);')
        .Append()
    )
    c.Substitute({'name': type_.name})

    # TODO(calamity): this handle single PropertyType.REF properties.
    # add ALL the types
    for prop in type_.properties.values():
      sub = {'name': prop.name}
      if prop.type_ == PropertyType.ARRAY:
        if prop.item_type.type_ == PropertyType.REF:
          if prop.optional:
            (c.Append('if (!json_schema_compiler::util::'
                      'GetOptionalTypes<%(type)s>(*dict,')
              .Append('    "%(name)s", &out->%(name)s))')
              .Append('  return false;')
            )
          else:
            (c.Append('if (!json_schema_compiler::util::'
                      'GetTypes<%(type)s>(*dict,')
              .Append('    "%(name)s", &out->%(name)s))')
              .Append('  return false;')
            )
          sub['type'] = self._cpp_type_generator.GetType(prop.item_type,
              pad_for_generics=True)
        elif prop.item_type.type_ == PropertyType.STRING:
          if prop.optional:
            (c.Append('if (!json_schema_compiler::util::GetOptionalStrings'
                      '(*dict, "%(name)s", &out->%(name)s))')
              .Append('  return false;')
            )
          else:
            (c.Append('if (!json_schema_compiler::util::GetStrings'
                      '(*dict, "%(name)s", &out->%(name)s))')
              .Append('  return false;')
            )
        else:
          raise NotImplementedError(prop.item_type.type_)
      elif prop.type_.is_fundamental:
        c.Append('if (!dict->%s)' %
            cpp_util.GetFundamentalValue(prop, '&out->%s' % prop.name))
        c.Append('  return false;')
      else:
        raise NotImplementedError(prop.type_)
      c.Substitute(sub)
    (c.Append('return true;')
    .Eblock('}')
    )
    return c

  def _GenerateTypeTovalue(self, type_):
    """Generates a function that serializes the type into a |DictionaryValue|.
    """
    c = code.Code()
    (c.Sblock('DictionaryValue* %s::ToValue() const {' % type_.name)
      .Append('DictionaryValue* value = new DictionaryValue();')
      .Append()
    )
    name = type_.name.lower()
    for prop in type_.properties.values():
      prop_name = name + '_' + prop.name if name else prop.name
      this_var =  prop.name
      c.Concat(self._CreateValueFromProperty(prop_name, prop, this_var))
    (c.Append()
      .Append('return value;')
      .Eblock('}')
    )
    return c

  # TODO(calamity): object and choices prop types
  def _CreateValueFromProperty(self, name, prop, var):
    """Generates code to serialize a single property in a type.
    """
    c = code.Code()
    if prop.type_.is_fundamental:
      c.Append('Value* %s_value = %s;' %
          (name, cpp_util.CreateFundamentalValue(prop, var)))
    elif prop.type_ == PropertyType.ARRAY:
      if prop.item_type.type_ == PropertyType.STRING:
        if prop.optional:
          c.Append('json_schema_compiler::util::'
                   'SetOptionalStrings(%s, "%s", value);' % (var, prop.name))
        else:
          c.Append('json_schema_compiler::util::'
                   'SetStrings(%s, "%s", value);' % (var, prop.name))
      else:
        item_name = name + '_single'
        (c.Append('ListValue* %(name)s_value = new ListValue();')
          .Append('for (%(it_type)s::iterator it = %(var)s->begin();')
          .Sblock('    it != %(var)s->end(); ++it) {')
          .Concat(self._CreateValueFromProperty(item_name, prop.item_type,
                  '*it'))
          .Append('%(name)s_value->Append(%(prop_val)s_value);')
          .Eblock('}')
        )
        c.Substitute(
            {'it_type': self._cpp_type_generator.GetType(prop),
             'name': name, 'var': var, 'prop_val': item_name})
    elif prop.type_ == PropertyType.REF:
      c.Append('Value* %s_value = %s.ToValue();' % (name, var))
    else:
      raise NotImplementedError
    return c

  def _GenerateFunction(self, function):
    """Generates the definitions for function structs.
    """
    classname = cpp_util.CppName(function.name)
    c = code.Code()

    # Params::Populate function
    if function.params:
      (c.Append('%(name)s::Params::Params() {}')
        .Append('%(name)s::Params::~Params() {}')
        .Append()
        .Concat(self._GenerateFunctionParamsCreate(function))
        .Append()
      )

    # Result::Create function
    c.Concat(self._GenerateFunctionResultCreate(function))

    c.Substitute({'name': classname})

    return c

  def _GenerateFunctionParamsCreate(self, function):
    """Generate function to create an instance of Params given a pointer.
    """
    classname = cpp_util.CppName(function.name)
    c = code.Code()
    (c.Append('// static')
      .Sblock('scoped_ptr<%(classname)s::Params> %(classname)s::Params::Create'
               '(const ListValue& args) {')
      .Append('if (args.GetSize() != %d)' % len(function.params))
      .Append('  return scoped_ptr<Params>();')
      .Append()
      .Append('scoped_ptr<Params> params(new Params());')
    )
    c.Substitute({'classname': classname})

    # TODO(calamity): generalize, needs to move to function to do populates for
    # wider variety of args
    for i, param in enumerate(function.params):
      sub = {'name': param.name, 'pos': i}
      c.Append()
      # TODO(calamity): Make valid for not just objects
      c.Append('DictionaryValue* %(name)s_param = NULL;')
      c.Append('if (!args.GetDictionary(%(pos)d, &%(name)s_param))')
      c.Append('  return scoped_ptr<Params>();')
      if param.type_ == PropertyType.REF:
        c.Append('if (!%(ctype)s::Populate(*%(name)s_param, '
                 '&params->%(name)s))')
        c.Append('  return scoped_ptr<Params>();')
        sub['ctype'] = self._cpp_type_generator.GetType(param)
      elif param.type_.is_fundamental:
        raise NotImplementedError('Fundamental types are unimplemented')
      elif param.type_ == PropertyType.OBJECT:
        c.Append('if (!%(ctype)s::Populate(*%(name)s_param, '
                 '&params->%(name)s))')
        c.Append('  return scoped_ptr<Params>();')
        sub['ctype'] = self._cpp_type_generator.GetType(param)
      elif param.type_ == PropertyType.CHOICES:
        raise NotImplementedError('Choices is unimplemented')
      else:
        raise NotImplementedError(param.type_)
      c.Substitute(sub)
    c.Append()
    c.Append('return params.Pass();')
    c.Eblock('}')

    return c

  def _GenerateFunctionResultCreate(self, function):
    """Generate function to create a Result given the return value.
    """
    classname = cpp_util.CppName(function.name)
    c = code.Code()
    c.Append('// static')
    param = function.callback.param
    arg = ''
    if param:
      if param.type_ == PropertyType.REF:
        arg = 'const %(type)s& %(name)s'
      else:
        arg = 'const %(type)s %(name)s'
      arg = arg % {
          'type': self._cpp_type_generator.GetType(param),
          'name': param.name
      }
    c.Sblock('Value* %(classname)s::Result::Create(%(arg)s) {')
    sub = {'classname': classname, 'arg': arg}
    # TODO(calamity): Choices
    if not param:
      c.Append('return Value::CreateNullValue();')
    else:
      sub['argname'] = param.name
      if param.type_.is_fundamental:
        c.Append('return %s;' %
            cpp_util.CreateFundamentalValue(param, param.name))
      elif param.type_ == PropertyType.REF:
        c.Append('return %(argname)s.ToValue();')
      elif param.type_ == PropertyType.OBJECT:
        raise NotImplementedError('Objects not implemented')
      elif param.type_ == PropertyType.ARRAY:
        raise NotImplementedError('Arrays not implemented')
      else:
        raise NotImplementedError(param.type_)
    c.Substitute(sub)
    c.Eblock('}')
    return c