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 25 matching lines...) Expand all Loading... |
36 /// Names used as setters via smoke. | 36 /// Names used as setters via smoke. |
37 final Set<String> _setters = new SplayTreeSet(); | 37 final Set<String> _setters = new SplayTreeSet(); |
38 | 38 |
39 /// Subclass relations needed to run smoke queries. | 39 /// Subclass relations needed to run smoke queries. |
40 final Map<TypeIdentifier, TypeIdentifier> _parents = new SplayTreeMap(); | 40 final Map<TypeIdentifier, TypeIdentifier> _parents = new SplayTreeMap(); |
41 | 41 |
42 /// Declarations requested via `smoke.getDeclaration or `smoke.query`. | 42 /// Declarations requested via `smoke.getDeclaration or `smoke.query`. |
43 final Map<TypeIdentifier, Map<String, _DeclarationCode>> _declarations = | 43 final Map<TypeIdentifier, Map<String, _DeclarationCode>> _declarations = |
44 new SplayTreeMap(); | 44 new SplayTreeMap(); |
45 | 45 |
| 46 /// Static methods used on each type. |
| 47 final Map<TypeIdentifier, Set<String>> _staticMethods = new SplayTreeMap(); |
| 48 |
46 /// Names that are used both as strings and symbols. | 49 /// Names that are used both as strings and symbols. |
47 final Set<String> _names = new SplayTreeSet(); | 50 final Set<String> _names = new SplayTreeSet(); |
48 | 51 |
49 /// Prefixes associated with imported libraries. | 52 /// Prefixes associated with imported libraries. |
50 final Map<String, String> _libraryPrefix = {}; | 53 final Map<String, String> _libraryPrefix = {}; |
51 | 54 |
52 /// Register that [name] is used as a getter in the code. | 55 /// Register that [name] is used as a getter in the code. |
53 void addGetter(String name) { _getters.add(name); } | 56 void addGetter(String name) { _getters.add(name); } |
54 | 57 |
55 /// Register that [name] is used as a setter in the code. | 58 /// Register that [name] is used as a setter in the code. |
56 void addSetter(String name) { _setters.add(name); } | 59 void addSetter(String name) { _setters.add(name); } |
57 | 60 |
58 /// Register that [name] might be needed as a symbol. | 61 /// Register that [name] might be needed as a symbol. |
59 void addSymbol(String name) { _names.add(name); } | 62 void addSymbol(String name) { _names.add(name); } |
60 | 63 |
| 64 /// Register that `cls.name` is used as a static method in the code. |
| 65 void addStaticMethod(TypeIdentifier cls, String name) { |
| 66 var methods = _staticMethods.putIfAbsent(cls, |
| 67 () => new SplayTreeSet<String>()); |
| 68 _addLibrary(cls.importUrl); |
| 69 methods.add(name); |
| 70 } |
| 71 |
61 int _mixins = 0; | 72 int _mixins = 0; |
62 | 73 |
63 /// Creates a new type to represent a mixin. Use [comment] to help users | 74 /// Creates a new type to represent a mixin. Use [comment] to help users |
64 /// figure out what mixin is being represented. | 75 /// figure out what mixin is being represented. |
65 TypeIdentifier createMixinType([String comment = '']) => | 76 TypeIdentifier createMixinType([String comment = '']) => |
66 new TypeIdentifier(null, '_M${_mixins++}', comment); | 77 new TypeIdentifier(null, '_M${_mixins++}', comment); |
67 | 78 |
68 /// Register that we care to know that [child] extends [parent]. | 79 /// Register that we care to know that [child] extends [parent]. |
69 void addParent(TypeIdentifier child, TypeIdentifier parent) { | 80 void addParent(TypeIdentifier child, TypeIdentifier parent) { |
70 var existing = _parents[child]; | 81 var existing = _parents[child]; |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 var decl = members[name].asCode(_libraryPrefix); | 212 var decl = members[name].asCode(_libraryPrefix); |
202 sb.write('$spaces #$name: $decl,\n'); | 213 sb.write('$spaces #$name: $decl,\n'); |
203 }); | 214 }); |
204 sb.write('$spaces }'); | 215 sb.write('$spaces }'); |
205 } | 216 } |
206 declarations.add(sb.toString()); | 217 declarations.add(sb.toString()); |
207 }); | 218 }); |
208 args['declarations'] = declarations; | 219 args['declarations'] = declarations; |
209 } | 220 } |
210 | 221 |
| 222 if (_staticMethods.isNotEmpty) { |
| 223 var methods = []; |
| 224 _staticMethods.forEach((type, members) { |
| 225 var className = type.asCode(_libraryPrefix); |
| 226 final sb = new StringBuffer() |
| 227 ..write(className) |
| 228 ..write(': '); |
| 229 if (members.isEmpty) { |
| 230 sb.write('const {}'); |
| 231 } else { |
| 232 sb.write('{\n'); |
| 233 for (var name in members) { |
| 234 sb.write('$spaces #$name: $className.$name,\n'); |
| 235 } |
| 236 sb.write('$spaces }'); |
| 237 } |
| 238 methods.add(sb.toString()); |
| 239 }); |
| 240 args['staticMethods'] = methods; |
| 241 } |
| 242 |
211 if (_names.isNotEmpty) { | 243 if (_names.isNotEmpty) { |
212 args['names'] = _names.map((n) => "#$n: '$n'"); | 244 args['names'] = _names.map((n) => "#$n: r'$n'"); |
213 } | 245 } |
214 | 246 |
215 buffer..write(spaces) | 247 buffer..write(spaces) |
216 ..writeln('useGeneratedCode(new StaticConfiguration(') | 248 ..writeln('useGeneratedCode(new StaticConfiguration(') |
217 ..write('$spaces checkedMode: false'); | 249 ..write('$spaces checkedMode: false'); |
218 | 250 |
219 args.forEach((name, mapContents) { | 251 args.forEach((name, mapContents) { |
220 buffer.writeln(','); | 252 buffer.writeln(','); |
221 // TODO(sigmund): use const map when Type can be keys (dartbug.com/17123) | 253 // TODO(sigmund): use const map when Type can be keys (dartbug.com/17123) |
222 buffer.writeln('$spaces $name: {'); | 254 buffer.writeln('$spaces $name: {'); |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 comment == other.comment; | 448 comment == other.comment; |
417 int get hashCode => super.hashCode; | 449 int get hashCode => super.hashCode; |
418 } | 450 } |
419 | 451 |
420 /// Default set of imports added by [SmokeCodeGenerator]. | 452 /// Default set of imports added by [SmokeCodeGenerator]. |
421 const DEFAULT_IMPORTS = const [ | 453 const DEFAULT_IMPORTS = const [ |
422 "import 'package:smoke/smoke.dart' show Declaration, PROPERTY, METHOD;", | 454 "import 'package:smoke/smoke.dart' show Declaration, PROPERTY, METHOD;", |
423 "import 'package:smoke/static.dart' show " | 455 "import 'package:smoke/static.dart' show " |
424 "useGeneratedCode, StaticConfiguration;", | 456 "useGeneratedCode, StaticConfiguration;", |
425 ]; | 457 ]; |
OLD | NEW |