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 | |
9 | 8 |
10 class HGenerator(object): | 9 class HGenerator(object): |
11 """A .h generator for a namespace. | 10 """A .h generator for a namespace. |
12 """ | 11 """ |
13 def __init__(self, namespace, cpp_type_generator): | 12 def __init__(self, namespace, cpp_type_generator): |
14 self._cpp_type_generator = cpp_type_generator | 13 self._cpp_type_generator = cpp_type_generator |
15 self._namespace = namespace | 14 self._namespace = namespace |
16 self._target_namespace = ( | 15 self._target_namespace = ( |
17 self._cpp_type_generator.GetCppNamespaceName(self._namespace)) | 16 self._cpp_type_generator.GetCppNamespaceName(self._namespace)) |
18 | 17 |
19 def Generate(self): | 18 def Generate(self): |
20 """Generates a code.Code object with the .h for a single namespace. | 19 """Generates a code.Code object with the .h for a single namespace. |
21 """ | 20 """ |
22 c = code.Code() | 21 c = code.Code() |
23 (c.Append(cpp_util.CHROMIUM_LICENSE) | 22 (c.Append(cpp_util.CHROMIUM_LICENSE) |
24 .Append() | 23 .Append() |
25 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) | 24 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) |
26 .Append() | 25 .Append() |
27 ) | 26 ) |
28 | 27 |
29 ifndef_name = self._GenerateIfndefName() | 28 ifndef_name = self._GenerateIfndefName() |
30 (c.Append('#ifndef %s' % ifndef_name) | 29 (c.Append('#ifndef %s' % ifndef_name) |
31 .Append('#define %s' % ifndef_name) | 30 .Append('#define %s' % ifndef_name) |
32 .Append('#pragma once') | 31 .Append('#pragma once') |
33 .Append() | 32 .Append() |
34 .Append('#include <string>') | 33 .Append('#include <string>') |
35 .Append('#include <vector>') | 34 .Append('#include <vector>') |
36 .Append() | 35 .Append() |
37 .Append('#include "base/basictypes.h"') | 36 .Append('#include "base/basictypes.h"') |
37 .Append('#include "base/memory/linked_ptr.h"') | |
38 .Append('#include "base/memory/scoped_ptr.h"') | 38 .Append('#include "base/memory/scoped_ptr.h"') |
39 .Append('#include "base/values.h"') | 39 .Append('#include "base/values.h"') |
40 .Append() | 40 .Append() |
41 .Append('using base::Value;') | 41 .Append('using base::Value;') |
42 .Append('using base::DictionaryValue;') | 42 .Append('using base::DictionaryValue;') |
43 .Append('using base::ListValue;') | 43 .Append('using base::ListValue;') |
44 .Append() | 44 .Append() |
45 ) | 45 ) |
46 | 46 |
47 includes = self._cpp_type_generator.GenerateCppIncludes() | 47 c.Concat(self._cpp_type_generator.GetRootNamespaceStart()) |
48 if not includes.IsEmpty(): | 48 forward_declarations = self._cpp_type_generator.GenerateForwardDeclarations( ); |
not at google - send to devlin
2012/02/05 23:42:12
line wrap
calamity
2012/02/06 11:51:18
Done.
| |
49 (c.Concat(includes) | 49 if not forward_declarations.IsEmpty(): |
50 (c.Append() | |
51 .Concat(forward_declarations) | |
50 .Append() | 52 .Append() |
51 ) | 53 ) |
52 | 54 |
53 (c.Concat(self._cpp_type_generator.GetCppNamespaceStart()) | 55 (c.Concat(self._cpp_type_generator.GetNamespaceStart()) |
54 .Append() | 56 .Append() |
55 .Append('//') | 57 .Append('//') |
56 .Append('// Types') | 58 .Append('// Types') |
57 .Append('//') | 59 .Append('//') |
58 .Append() | 60 .Append() |
59 ) | 61 ) |
60 for type_ in self._namespace.types.values(): | 62 for type_ in self._namespace.types.values(): |
61 (c.Concat(self._GenerateType(type_)) | 63 (c.Concat(self._GenerateType(type_)) |
62 .Append() | 64 .Append() |
63 ) | 65 ) |
64 (c.Append('//') | 66 (c.Append('//') |
65 .Append('// Functions') | 67 .Append('// Functions') |
66 .Append('//') | 68 .Append('//') |
67 .Append() | 69 .Append() |
68 ) | 70 ) |
69 for function in self._namespace.functions.values(): | 71 for function in self._namespace.functions.values(): |
70 (c.Concat(self._GenerateFunction(function)) | 72 (c.Concat(self._GenerateFunction(function)) |
71 .Append() | 73 .Append() |
72 ) | 74 ) |
73 (c.Append() | 75 (c.Append() |
74 .Append() | 76 .Concat(self._cpp_type_generator.GetNamespaceEnd()) |
75 .Concat(self._cpp_type_generator.GetCppNamespaceEnd()) | 77 .Concat(self._cpp_type_generator.GetRootNamespaceEnd()) |
76 .Append() | 78 .Append() |
77 .Append('#endif // %s' % ifndef_name) | 79 .Append('#endif // %s' % ifndef_name) |
78 .Append() | 80 .Append() |
79 ) | 81 ) |
80 return c | 82 return c |
81 | 83 |
82 def _GenerateType(self, type_, serializable=True): | 84 def _GenerateType(self, type_, serializable=True): |
83 """Generates a struct for a type. | 85 """Generates a struct for a type. |
84 """ | 86 """ |
85 classname = cpp_util.CppName(type_.name) | 87 classname = cpp_util.Classname(type_.name) |
86 c = code.Code() | 88 c = code.Code() |
87 if type_.description: | 89 if type_.description: |
88 c.Comment(type_.description) | 90 c.Comment(type_.description) |
89 (c.Sblock('struct %(classname)s {') | 91 (c.Sblock('struct %(classname)s {') |
90 .Append('~%(classname)s();') | 92 .Append('~%(classname)s();') |
91 .Append('%(classname)s();') | 93 .Append('%(classname)s();') |
92 .Append() | 94 .Append() |
93 ) | 95 ) |
94 | 96 |
95 for prop in type_.properties.values(): | 97 for prop in type_.properties.values(): |
96 if prop.description: | 98 if prop.description: |
97 c.Comment(prop.description) | 99 c.Comment(prop.description) |
98 if prop.optional: | 100 c.Append('%s %s;' % |
99 c.Append('scoped_ptr<%s> %s;' % | 101 (self._cpp_type_generator.GetType(prop, wrap_optional=True), prop.unix _name)) |
not at google - send to devlin
2012/02/05 23:42:12
line wrap
calamity
2012/02/06 11:51:18
Done.
| |
100 (self._cpp_type_generator.GetType(prop, pad_for_generics=True), | |
101 prop.name)) | |
102 else: | |
103 c.Append('%s %s;' % | |
104 (self._cpp_type_generator.GetType(prop), prop.name)) | |
105 c.Append() | 102 c.Append() |
106 | 103 |
107 (c.Comment('Populates a %(classname)s object from a Value. Returns' | 104 (c.Comment('Populates a %(classname)s object from a Value. Returns' |
108 ' whether |out| was successfully populated.') | 105 ' whether |out| was successfully populated.') |
109 .Append('static bool Populate(const Value& value, %(classname)s* out);') | 106 .Append('static bool Populate(const Value& value, %(classname)s* out);') |
110 .Append() | 107 .Append() |
111 ) | 108 ) |
112 | 109 |
113 if serializable: | 110 if serializable: |
114 (c.Comment('Returns a new DictionaryValue representing the' | 111 (c.Comment('Returns a new DictionaryValue representing the' |
115 ' serialized form of this %(classname)s object. Passes' | 112 ' serialized form of this %(classname)s object. Passes' |
116 'ownership to caller.') | 113 'ownership to caller.') |
117 .Append('DictionaryValue* ToValue() const;') | 114 .Append('DictionaryValue* ToValue() const;') |
118 ) | 115 ) |
119 (c.Eblock() | 116 (c.Eblock() |
120 .Sblock(' private:') | 117 .Sblock(' private:') |
121 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);') | 118 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);') |
122 .Eblock('};') | 119 .Eblock('};') |
123 ) | 120 ) |
124 c.Substitute({'classname': classname}) | 121 c.Substitute({'classname': classname}) |
125 return c | 122 return c |
126 | 123 |
127 def _GenerateFunction(self, function): | 124 def _GenerateFunction(self, function): |
128 """Generates the structs for a function. | 125 """Generates the structs for a function. |
129 """ | 126 """ |
130 c = code.Code() | 127 c = code.Code() |
131 (c.Sblock('namespace %s {' % cpp_util.CppName(function.name)) | 128 (c.Sblock('namespace %s {' % cpp_util.Classname(function.name)) |
132 .Concat(self._GenerateFunctionParams(function)) | 129 .Concat(self._GenerateFunctionParams(function)) |
133 .Append() | 130 .Append() |
134 .Concat(self._GenerateFunctionResult(function)) | 131 .Concat(self._GenerateFunctionResult(function)) |
135 .Append() | 132 .Append() |
136 .Eblock('};') | 133 .Eblock('};') |
137 ) | 134 ) |
138 return c | 135 return c |
139 | 136 |
140 def _GenerateFunctionParams(self, function): | 137 def _GenerateFunctionParams(self, function): |
141 """Generates the struct for passing parameters into a function. | 138 """Generates the struct for passing parameters into a function. |
142 """ | 139 """ |
143 c = code.Code() | 140 c = code.Code() |
144 | 141 |
145 if function.params: | 142 if function.params: |
146 c.Sblock('struct Params {') | 143 c.Sblock('struct Params {') |
147 for param in function.params: | 144 for param in function.params: |
148 if param.description: | 145 if param.description: |
149 c.Comment(param.description) | 146 c.Comment(param.description) |
150 if param.type_ == PropertyType.OBJECT: | 147 if param.type_ == PropertyType.OBJECT: |
151 c.Concat(self._GenerateType(param, serializable=False)) | 148 c.Concat(self._GenerateType(param, serializable=False)) |
152 c.Append() | 149 c.Append() |
153 for param in function.params: | 150 for param in function.params: |
154 c.Append('%s %s;' % | 151 c.Append('%s %s;' % |
155 (self._cpp_type_generator.GetType(param), param.name)) | 152 (self._cpp_type_generator.GetType(param, wrap_optional=True), param. unix_name)) |
156 | 153 |
not at google - send to devlin
2012/02/05 23:42:12
line wrap
calamity
2012/02/06 11:51:18
Done.
| |
157 (c.Append() | 154 (c.Append() |
158 .Append('~Params();') | 155 .Append('~Params();') |
159 .Append() | 156 .Append() |
160 .Append('static scoped_ptr<Params> Create(const ListValue& args);') | 157 .Append('static scoped_ptr<Params> Create(const ListValue& args);') |
161 .Eblock() | 158 .Eblock() |
162 .Sblock(' private:') | 159 .Sblock(' private:') |
163 .Append('Params();') | 160 .Append('Params();') |
164 .Append() | 161 .Append() |
165 .Append('DISALLOW_COPY_AND_ASSIGN(Params);') | 162 .Append('DISALLOW_COPY_AND_ASSIGN(Params);') |
163 .Eblock('};') | |
166 ) | 164 ) |
167 | 165 |
168 c.Eblock('};') | |
169 | |
170 return c | 166 return c |
171 | 167 |
172 def _GenerateFunctionResult(self, function): | 168 def _GenerateFunctionResult(self, function): |
173 """Generates the struct for passing a function's result back. | 169 """Generates the struct for passing a function's result back. |
174 """ | 170 """ |
175 # TODO(calamity): Handle returning an object | |
176 c = code.Code() | 171 c = code.Code() |
177 | 172 |
178 param = function.callback.param | |
179 # TODO(calamity): Put this description comment in less stupid place | |
180 if param.description: | |
181 c.Comment(param.description) | |
182 (c.Append('class Result {') | 173 (c.Append('class Result {') |
183 .Sblock(' public:') | 174 .Sblock(' public:') |
184 ) | 175 ) |
185 arg = '' | 176 params = function.callback.params |
186 # TODO(calamity): handle object | 177 if not params: |
187 if param: | 178 c.Append('static Value* Create();') |
188 if param.type_ == PropertyType.REF: | 179 else: |
189 arg = 'const %(type)s& %(name)s' | 180 for param in params: |
190 else: | 181 sub = { |
182 'type': self._cpp_type_generator.GetType(param), | |
183 'name': param.unix_name, | |
184 } | |
191 arg = 'const %(type)s %(name)s' | 185 arg = 'const %(type)s %(name)s' |
192 arg = arg % { | 186 if param.type_ == PropertyType.REF: |
193 'type': self._cpp_type_generator.GetType(param), | 187 arg = 'const %(type)s& %(name)s' |
194 'name': param.name | 188 arg = arg % sub |
195 } | 189 if param.description: |
196 (c.Append('static Value* Create(%s);' % arg) | 190 c.Comment(param.description) |
197 .Eblock() | 191 c.Append('static Value* Create(%s);' % arg) |
198 .Sblock(' private:') | 192 (c.Eblock() |
199 .Append('Result() {};') | 193 .Sblock(' private:') |
200 .Append('DISALLOW_COPY_AND_ASSIGN(Result);') | 194 .Append('Result() {};') |
201 .Eblock('};') | 195 .Append('DISALLOW_COPY_AND_ASSIGN(Result);') |
196 .Eblock('};') | |
202 ) | 197 ) |
203 | 198 |
204 return c | 199 return c |
205 | 200 |
206 def _GenerateIfndefName(self): | 201 def _GenerateIfndefName(self): |
207 """Formats a path and filename as a #define name. | 202 """Formats a path and filename as a #define name. |
208 | 203 |
209 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__. | 204 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__. |
210 """ | 205 """ |
211 return (('%s_%s_H__' % | 206 return (('%s/%s_H__' % |
212 (self._namespace.source_file_dir, self._target_namespace)) | 207 (self._namespace.source_file_dir, self._target_namespace)) |
213 .upper().replace(os.sep, '_')) | 208 .upper().replace('/', '_')) |
214 | 209 |
OLD | NEW |