OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Library to generate code that can initialize the `StaticConfiguration` in | 5 /// Library to generate code that can initialize the `StaticConfiguration` in |
6 /// `package:smoke/static.dart`. | 6 /// `package:smoke/static.dart`. |
7 /// | 7 /// |
8 /// This library doesn't have any specific logic to extract information from | 8 /// This library doesn't have any specific logic to extract information from |
9 /// Dart source code. To extract code using the analyzer, take a look at the | 9 /// Dart source code. To extract code using the analyzer, take a look at the |
10 /// `smoke.codegen.recorder` library. | 10 /// `smoke.codegen.recorder` library. |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 /// sorted alphabetically. | 175 /// sorted alphabetically. |
176 /// | 176 /// |
177 /// **Note**: this code assumes that imports from [writeImports] and top-level | 177 /// **Note**: this code assumes that imports from [writeImports] and top-level |
178 /// declarations from [writeTopLevelDeclarations] are included in the same | 178 /// declarations from [writeTopLevelDeclarations] are included in the same |
179 /// library where this code will live. | 179 /// library where this code will live. |
180 void writeInitCall(StringBuffer buffer, [int indent = 2]) { | 180 void writeInitCall(StringBuffer buffer, [int indent = 2]) { |
181 final spaces = new List.filled(indent, ' ').join(''); | 181 final spaces = new List.filled(indent, ' ').join(''); |
182 var args = {}; | 182 var args = {}; |
183 | 183 |
184 if (_getters.isNotEmpty) { | 184 if (_getters.isNotEmpty) { |
185 args['getters'] = _getters.map((n) => '#$n: (o) => o.$n'); | 185 args['getters'] = _getters.map((n) => '${_symbol(n)}: (o) => o.$n'); |
186 } | 186 } |
187 if (_setters.isNotEmpty) { | 187 if (_setters.isNotEmpty) { |
188 args['setters'] = _setters.map((n) => '#$n: (o, v) { o.$n = v; }'); | 188 args['setters'] = _setters.map( |
| 189 (n) => '${_symbol(n)}: (o, v) { o.$n = v; }'); |
189 } | 190 } |
190 | 191 |
191 if (_parents.isNotEmpty) { | 192 if (_parents.isNotEmpty) { |
192 var parentsMap = []; | 193 var parentsMap = []; |
193 _parents.forEach((child, parent) { | 194 _parents.forEach((child, parent) { |
194 var parent = _parents[child]; | 195 var parent = _parents[child]; |
195 parentsMap.add('${child.asCode(_libraryPrefix)}: ' | 196 parentsMap.add('${child.asCode(_libraryPrefix)}: ' |
196 '${parent.asCode(_libraryPrefix)}'); | 197 '${parent.asCode(_libraryPrefix)}'); |
197 }); | 198 }); |
198 args['parents'] = parentsMap; | 199 args['parents'] = parentsMap; |
199 } | 200 } |
200 | 201 |
201 if (_declarations.isNotEmpty) { | 202 if (_declarations.isNotEmpty) { |
202 var declarations = []; | 203 var declarations = []; |
203 _declarations.forEach((type, members) { | 204 _declarations.forEach((type, members) { |
204 final sb = new StringBuffer() | 205 final sb = new StringBuffer() |
205 ..write(type.asCode(_libraryPrefix)) | 206 ..write(type.asCode(_libraryPrefix)) |
206 ..write(': '); | 207 ..write(': '); |
207 if (members.isEmpty) { | 208 if (members.isEmpty) { |
208 sb.write('const {}'); | 209 sb.write('const {}'); |
209 } else { | 210 } else { |
210 sb.write('{\n'); | 211 sb.write('{\n'); |
211 members.forEach((name, decl) { | 212 members.forEach((name, decl) { |
212 var decl = members[name].asCode(_libraryPrefix); | 213 var decl = members[name].asCode(_libraryPrefix); |
213 sb.write('$spaces #$name: $decl,\n'); | 214 sb.write('$spaces ${_symbol(name)}: $decl,\n'); |
214 }); | 215 }); |
215 sb.write('$spaces }'); | 216 sb.write('$spaces }'); |
216 } | 217 } |
217 declarations.add(sb.toString()); | 218 declarations.add(sb.toString()); |
218 }); | 219 }); |
219 args['declarations'] = declarations; | 220 args['declarations'] = declarations; |
220 } | 221 } |
221 | 222 |
222 if (_staticMethods.isNotEmpty) { | 223 if (_staticMethods.isNotEmpty) { |
223 var methods = []; | 224 var methods = []; |
224 _staticMethods.forEach((type, members) { | 225 _staticMethods.forEach((type, members) { |
225 var className = type.asCode(_libraryPrefix); | 226 var className = type.asCode(_libraryPrefix); |
226 final sb = new StringBuffer() | 227 final sb = new StringBuffer() |
227 ..write(className) | 228 ..write(className) |
228 ..write(': '); | 229 ..write(': '); |
229 if (members.isEmpty) { | 230 if (members.isEmpty) { |
230 sb.write('const {}'); | 231 sb.write('const {}'); |
231 } else { | 232 } else { |
232 sb.write('{\n'); | 233 sb.write('{\n'); |
233 for (var name in members) { | 234 for (var name in members) { |
234 sb.write('$spaces #$name: $className.$name,\n'); | 235 sb.write('$spaces ${_symbol(name)}: $className.$name,\n'); |
235 } | 236 } |
236 sb.write('$spaces }'); | 237 sb.write('$spaces }'); |
237 } | 238 } |
238 methods.add(sb.toString()); | 239 methods.add(sb.toString()); |
239 }); | 240 }); |
240 args['staticMethods'] = methods; | 241 args['staticMethods'] = methods; |
241 } | 242 } |
242 | 243 |
243 if (_names.isNotEmpty) { | 244 if (_names.isNotEmpty) { |
244 args['names'] = _names.map((n) => "#$n: r'$n'"); | 245 args['names'] = _names.map((n) => "${_symbol(n)}: r'$n'"); |
245 } | 246 } |
246 | 247 |
247 buffer..write(spaces) | 248 buffer..write(spaces) |
248 ..writeln('useGeneratedCode(new StaticConfiguration(') | 249 ..writeln('useGeneratedCode(new StaticConfiguration(') |
249 ..write('$spaces checkedMode: false'); | 250 ..write('$spaces checkedMode: false'); |
250 | 251 |
251 args.forEach((name, mapContents) { | 252 args.forEach((name, mapContents) { |
252 buffer.writeln(','); | 253 buffer.writeln(','); |
253 // TODO(sigmund): use const map when Type can be keys (dartbug.com/17123) | 254 // TODO(sigmund): use const map when Type can be keys (dartbug.com/17123) |
254 buffer.writeln('$spaces $name: {'); | 255 buffer.writeln('$spaces $name: {'); |
(...skipping 23 matching lines...) Expand all Loading... |
278 | 279 |
279 _DeclarationCode(this.name, this.type, this.kind, this.isFinal, this.isStatic, | 280 _DeclarationCode(this.name, this.type, this.kind, this.isFinal, this.isStatic, |
280 this.annotations); | 281 this.annotations); |
281 | 282 |
282 List<String> get librariesUsed => [] | 283 List<String> get librariesUsed => [] |
283 ..addAll(type.librariesUsed) | 284 ..addAll(type.librariesUsed) |
284 ..addAll(annotations.expand((a) => a.librariesUsed)); | 285 ..addAll(annotations.expand((a) => a.librariesUsed)); |
285 | 286 |
286 String asCode(Map<String, String> libraryPrefixes) { | 287 String asCode(Map<String, String> libraryPrefixes) { |
287 var sb = new StringBuffer(); | 288 var sb = new StringBuffer(); |
288 sb.write("const Declaration(#$name, ${type.asCode(libraryPrefixes)}"); | 289 sb.write('const Declaration(${_symbol(name)}, ' |
| 290 '${type.asCode(libraryPrefixes)}'); |
289 if (kind != 'FIELD') sb.write(', kind: $kind'); | 291 if (kind != 'FIELD') sb.write(', kind: $kind'); |
290 if (isFinal) sb.write(', isFinal: true'); | 292 if (isFinal) sb.write(', isFinal: true'); |
291 if (isStatic) sb.write(', isStatic: true'); | 293 if (isStatic) sb.write(', isStatic: true'); |
292 if (annotations != null && annotations.isNotEmpty) { | 294 if (annotations != null && annotations.isNotEmpty) { |
293 sb.write(', annotations: const ['); | 295 sb.write(', annotations: const ['); |
294 bool first = true; | 296 bool first = true; |
295 for (var e in annotations) { | 297 for (var e in annotations) { |
296 if (!first) sb.write(', '); | 298 if (!first) sb.write(', '); |
297 first = false; | 299 first = false; |
298 sb.write(e.asCode(libraryPrefixes)); | 300 sb.write(e.asCode(libraryPrefixes)); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 comment == other.comment; | 450 comment == other.comment; |
449 int get hashCode => super.hashCode; | 451 int get hashCode => super.hashCode; |
450 } | 452 } |
451 | 453 |
452 /// Default set of imports added by [SmokeCodeGenerator]. | 454 /// Default set of imports added by [SmokeCodeGenerator]. |
453 const DEFAULT_IMPORTS = const [ | 455 const DEFAULT_IMPORTS = const [ |
454 "import 'package:smoke/smoke.dart' show Declaration, PROPERTY, METHOD;", | 456 "import 'package:smoke/smoke.dart' show Declaration, PROPERTY, METHOD;", |
455 "import 'package:smoke/static.dart' show " | 457 "import 'package:smoke/static.dart' show " |
456 "useGeneratedCode, StaticConfiguration;", | 458 "useGeneratedCode, StaticConfiguration;", |
457 ]; | 459 ]; |
| 460 |
| 461 _symbol(String name) => |
| 462 name.contains('[') ? "const Symbol('$name')" : '#$name'; |
OLD | NEW |