Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 from model import PropertyType | 5 from model import PropertyType |
| 6 import code | 6 import code |
| 7 import cpp_util | 7 import cpp_util |
| 8 import os | 8 import os |
| 9 | 9 |
| 10 class HGenerator(object): | 10 class HGenerator(object): |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 31 .Append('#define %s' % ifndef_name) | 31 .Append('#define %s' % ifndef_name) |
| 32 .Append('#pragma once') | 32 .Append('#pragma once') |
| 33 .Append() | 33 .Append() |
| 34 .Append('#include <string>') | 34 .Append('#include <string>') |
| 35 .Append('#include <vector>') | 35 .Append('#include <vector>') |
| 36 .Append() | 36 .Append() |
| 37 .Append('#include "base/basictypes.h"') | 37 .Append('#include "base/basictypes.h"') |
| 38 .Append('#include "base/memory/linked_ptr.h"') | 38 .Append('#include "base/memory/linked_ptr.h"') |
| 39 .Append('#include "base/memory/scoped_ptr.h"') | 39 .Append('#include "base/memory/scoped_ptr.h"') |
| 40 .Append('#include "base/values.h"') | 40 .Append('#include "base/values.h"') |
| 41 .Append('#include "tools/json_schema_compiler/any.h"') | |
| 41 .Append() | 42 .Append() |
| 42 ) | 43 ) |
| 43 | 44 |
| 44 c.Concat(self._cpp_type_generator.GetRootNamespaceStart()) | 45 c.Concat(self._cpp_type_generator.GetRootNamespaceStart()) |
| 45 # TODO(calamity): These forward declarations should be #includes to allow | 46 # TODO(calamity): These forward declarations should be #includes to allow |
| 46 # $ref types from other files to be used as required params. This requires | 47 # $ref types from other files to be used as required params. This requires |
| 47 # some detangling of windows and tabs which will currently lead to circular | 48 # some detangling of windows and tabs which will currently lead to circular |
| 48 # #includes. | 49 # #includes. |
| 49 forward_declarations = ( | 50 forward_declarations = ( |
| 50 self._cpp_type_generator.GenerateForwardDeclarations()) | 51 self._cpp_type_generator.GenerateForwardDeclarations()) |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 69 if self._namespace.functions: | 70 if self._namespace.functions: |
| 70 (c.Append('//') | 71 (c.Append('//') |
| 71 .Append('// Functions') | 72 .Append('// Functions') |
| 72 .Append('//') | 73 .Append('//') |
| 73 .Append() | 74 .Append() |
| 74 ) | 75 ) |
| 75 for function in self._namespace.functions.values(): | 76 for function in self._namespace.functions.values(): |
| 76 (c.Concat(self._GenerateFunction(function)) | 77 (c.Concat(self._GenerateFunction(function)) |
| 77 .Append() | 78 .Append() |
| 78 ) | 79 ) |
| 79 (c.Append() | 80 (c.Concat(self._cpp_type_generator.GetNamespaceEnd()) |
| 80 .Concat(self._cpp_type_generator.GetNamespaceEnd()) | |
| 81 .Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | 81 .Concat(self._cpp_type_generator.GetRootNamespaceEnd()) |
| 82 .Append() | 82 .Append() |
| 83 .Append('#endif // %s' % ifndef_name) | 83 .Append('#endif // %s' % ifndef_name) |
| 84 .Append() | 84 .Append() |
| 85 ) | 85 ) |
| 86 return c | 86 return c |
| 87 | 87 |
| 88 def _GenerateEnumDeclaration(self, enum_name, prop, values): | 88 def _GenerateEnumDeclaration(self, enum_name, prop, values): |
| 89 """Generate the declaration of a C++ enum for the given property and | 89 """Generate the declaration of a C++ enum for the given property and |
| 90 values. | 90 values. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 101 return c | 101 return c |
| 102 | 102 |
| 103 def _GenerateFields(self, props): | 103 def _GenerateFields(self, props): |
| 104 """Generates the field declarations when declaring a type. | 104 """Generates the field declarations when declaring a type. |
| 105 """ | 105 """ |
| 106 c = code.Code() | 106 c = code.Code() |
| 107 # Generate the enums needed for any fields with "choices" | 107 # Generate the enums needed for any fields with "choices" |
| 108 for prop in props: | 108 for prop in props: |
| 109 if prop.type_ == PropertyType.CHOICES: | 109 if prop.type_ == PropertyType.CHOICES: |
| 110 enum_name = self._cpp_type_generator.GetChoicesEnumType(prop) | 110 enum_name = self._cpp_type_generator.GetChoicesEnumType(prop) |
| 111 c.Append('%s %s_type;' % (enum_name, prop.unix_name)) | 111 c.Append('%s %s_type;' % (enum_name, prop.unix_name)) |
|
not at google - send to devlin
2012/02/28 22:41:17
browsing through generated code (specifically the
calamity
2012/03/01 04:47:09
Done.
| |
| 112 for prop in self._cpp_type_generator.GetExpandedChoicesInParams(props): | 112 for prop in self._cpp_type_generator.GetExpandedChoicesInParams(props): |
| 113 if prop.description: | 113 if prop.description: |
| 114 c.Comment(prop.description) | 114 c.Comment(prop.description) |
| 115 c.Append('%s %s;' % ( | 115 c.Append('%s %s;' % ( |
| 116 self._cpp_type_generator.GetType(prop, wrap_optional=True), | 116 self._cpp_type_generator.GetType(prop, wrap_optional=True), |
| 117 prop.unix_name)) | 117 prop.unix_name)) |
| 118 c.Append() | 118 c.Append() |
| 119 return c | 119 return c |
| 120 | 120 |
| 121 def _GenerateType(self, type_): | 121 def _GenerateType(self, type_): |
| 122 """Generates a struct for a type. | 122 """Generates a struct for a type. |
| 123 """ | 123 """ |
| 124 classname = cpp_util.Classname(type_.name) | 124 classname = cpp_util.Classname(type_.name) |
| 125 c = code.Code() | 125 c = code.Code() |
| 126 if type_.description: | 126 # Functions on types cannot have properties on them and are not |
| 127 c.Comment(type_.description) | 127 # instantiable because they're dealt with renderer side. |
|
not at google - send to devlin
2012/02/28 22:41:17
See my comment about this comment in model.py. Ma
calamity
2012/03/01 04:47:09
Done.
| |
| 128 (c.Sblock('struct %(classname)s {') | 128 if type_.functions: |
| 129 .Append('~%(classname)s();') | 129 assert not type_.properties |
|
not at google - send to devlin
2012/02/28 22:41:17
Also: again, asserts are for internal errors not e
calamity
2012/03/01 04:47:09
Done.
| |
| 130 .Append('%(classname)s();') | 130 c.Sblock('namespace %(classname)s {') |
| 131 .Append() | 131 for function in type_.functions.values(): |
| 132 .Concat(self._GeneratePropertyStructures(type_.properties.values())) | 132 (c.Concat(self._GenerateFunction(function)) |
| 133 .Concat(self._GenerateFields(type_.properties.values())) | 133 .Append() |
| 134 ) | 134 ) |
| 135 if type_.from_json: | 135 c.Eblock('}') |
| 136 (c.Comment('Populates a %s object from a Value. Returns' | 136 else: |
| 137 ' whether |out| was successfully populated.' % classname) | 137 if type_.description: |
| 138 .Append('static bool Populate(const Value& value, %(classname)s* out);') | 138 c.Comment(type_.description) |
| 139 .Append() | 139 (c.Sblock('struct %(classname)s {') |
| 140 .Append('~%(classname)s();') | |
| 141 .Append('%(classname)s();') | |
| 142 .Append() | |
| 143 .Concat(self._GeneratePropertyStructures(type_.properties.values())) | |
| 144 .Concat(self._GenerateFields(type_.properties.values())) | |
| 140 ) | 145 ) |
| 146 if type_.from_json: | |
| 147 (c.Comment('Populates a %s object from a Value. Returns' | |
| 148 ' whether |out| was successfully populated.' % classname) | |
| 149 .Append( | |
| 150 'static bool Populate(const Value& value, %(classname)s* out);') | |
| 151 .Append() | |
| 152 ) | |
| 141 | 153 |
| 142 if type_.from_client: | 154 if type_.from_client: |
| 143 (c.Comment('Returns a new DictionaryValue representing the' | 155 (c.Comment('Returns a new DictionaryValue representing the' |
| 144 ' serialized form of this %s object. Passes ' | 156 ' serialized form of this %s object. Passes ' |
| 145 'ownership to caller.' % classname) | 157 'ownership to caller.' % classname) |
| 146 .Append('scoped_ptr<DictionaryValue> ToValue() const;') | 158 .Append('scoped_ptr<DictionaryValue> ToValue() const;') |
| 159 ) | |
| 160 | |
| 161 (c.Eblock() | |
| 162 .Sblock(' private:') | |
| 163 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);') | |
| 164 .Eblock('};') | |
| 147 ) | 165 ) |
| 148 (c.Eblock() | |
| 149 .Sblock(' private:') | |
| 150 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);') | |
| 151 .Eblock('};') | |
| 152 ) | |
| 153 c.Substitute({'classname': classname}) | 166 c.Substitute({'classname': classname}) |
| 154 return c | 167 return c |
| 155 | 168 |
| 156 def _GenerateFunction(self, function): | 169 def _GenerateFunction(self, function): |
| 157 """Generates the structs for a function. | 170 """Generates the structs for a function. |
| 158 """ | 171 """ |
| 159 c = code.Code() | 172 c = code.Code() |
| 160 (c.Sblock('namespace %s {' % cpp_util.Classname(function.name)) | 173 (c.Sblock('namespace %s {' % cpp_util.Classname(function.name)) |
| 161 .Concat(self._GenerateFunctionParams(function)) | 174 .Concat(self._GenerateFunctionParams(function)) |
| 162 .Append() | 175 .Append() |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 else: | 240 else: |
| 228 c.Concat(self._GeneratePropertyStructures(params)) | 241 c.Concat(self._GeneratePropertyStructures(params)) |
| 229 | 242 |
| 230 # If there is a single parameter, this is straightforward. However, if | 243 # If there is a single parameter, this is straightforward. However, if |
| 231 # the callback parameter is of 'choices', this generates a Create method | 244 # the callback parameter is of 'choices', this generates a Create method |
| 232 # for each choice. This works because only 1 choice can be returned at a | 245 # for each choice. This works because only 1 choice can be returned at a |
| 233 # time. | 246 # time. |
| 234 for param in self._cpp_type_generator.GetExpandedChoicesInParams(params): | 247 for param in self._cpp_type_generator.GetExpandedChoicesInParams(params): |
| 235 if param.description: | 248 if param.description: |
| 236 c.Comment(param.description) | 249 c.Comment(param.description) |
| 250 if param.type_ == PropertyType.ANY: | |
| 251 c.Comment("Value* Result::Create(Value*) not generated " | |
| 252 "because it's redundant.") | |
|
Yoyo Zhou
2012/02/28 22:23:41
nit: indent to paren
calamity
2012/03/01 04:47:09
Done.
| |
| 253 continue | |
| 237 c.Append('Value* Create(const %s);' % cpp_util.GetParameterDeclaration( | 254 c.Append('Value* Create(const %s);' % cpp_util.GetParameterDeclaration( |
| 238 param, self._cpp_type_generator.GetType(param))) | 255 param, self._cpp_type_generator.GetType(param))) |
| 239 c.Eblock('};') | 256 c.Eblock('};') |
| 240 | 257 |
| 241 return c | 258 return c |
| 242 | 259 |
| 243 def _GenerateIfndefName(self): | 260 def _GenerateIfndefName(self): |
| 244 """Formats a path and filename as a #define name. | 261 """Formats a path and filename as a #define name. |
| 245 | 262 |
| 246 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__. | 263 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__. |
| 247 """ | 264 """ |
| 248 return (('%s_%s_H__' % | 265 return (('%s_%s_H__' % |
| 249 (self._namespace.source_file_dir, self._target_namespace)) | 266 (self._namespace.source_file_dir, self._target_namespace)) |
| 250 .upper().replace(os.sep, '_')) | 267 .upper().replace(os.sep, '_')) |
| 251 | 268 |
| OLD | NEW |