Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: packages/smoke/lib/codegen/generator.dart

Issue 2989763002: Update charted to 0.4.8 and roll (Closed)
Patch Set: Removed Cutch from list of reviewers Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « packages/smoke/codereview.settings ('k') | packages/smoke/lib/codegen/recorder.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 /// Library to generate code that can initialize the `StaticConfiguration` in
6 /// `package:smoke/static.dart`.
7 ///
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
10 /// `smoke.codegen.recorder` library.
11 library smoke.codegen.generator;
12
13 import 'dart:collection' show SplayTreeMap, SplayTreeSet;
14
15 import 'package:smoke/src/common.dart' show compareLists, compareMaps;
16
17 /// Collects the necessary information and generates code to initialize a
18 /// `StaticConfiguration`. After setting up the generator by calling
19 /// [addGetter], [addSetter], [addSymbol], and [addDeclaration], you can
20 /// retrieve the generated code using the following three methods:
21 ///
22 /// * [writeImports] writes a list of imports directives,
23 /// * [writeTopLevelDeclarations] writes additional declarations used to
24 /// represent mixin classes by name in the generated code.
25 /// * [writeStaticConfiguration] writes the actual code that allocates the
26 /// static configuration.
27 ///
28 /// You'd need to include all three in your generated code, since the
29 /// initialization code refers to symbols that are only available from the
30 /// generated imports or the generated top-level declarations.
31 class SmokeCodeGenerator {
32 // Note: we use SplayTreeSet/Map here and below to keep generated code sorted.
33 /// Names used as getters via smoke.
34 final Set<String> _getters = new SplayTreeSet();
35
36 /// Names used as setters via smoke.
37 final Set<String> _setters = new SplayTreeSet();
38
39 /// Subclass relations needed to run smoke queries.
40 final Map<TypeIdentifier, TypeIdentifier> _parents = new SplayTreeMap();
41
42 /// Declarations requested via `smoke.getDeclaration or `smoke.query`.
43 final Map<TypeIdentifier, Map<String, _DeclarationCode>> _declarations =
44 new SplayTreeMap();
45
46 /// Static methods used on each type.
47 final Map<TypeIdentifier, Set<String>> _staticMethods = new SplayTreeMap();
48
49 /// Names that are used both as strings and symbols.
50 final Set<String> _names = new SplayTreeSet();
51
52 /// Prefixes associated with imported libraries.
53 final Map<String, String> _libraryPrefix = {};
54
55 /// Register that [name] is used as a getter in the code.
56 void addGetter(String name) {
57 _getters.add(name);
58 }
59
60 /// Register that [name] is used as a setter in the code.
61 void addSetter(String name) {
62 _setters.add(name);
63 }
64
65 /// Register that [name] might be needed as a symbol.
66 void addSymbol(String name) {
67 _names.add(name);
68 }
69
70 /// Register that `cls.name` is used as a static method in the code.
71 void addStaticMethod(TypeIdentifier cls, String name) {
72 var methods =
73 _staticMethods.putIfAbsent(cls, () => new SplayTreeSet<String>());
74 _addLibrary(cls.importUrl);
75 methods.add(name);
76 }
77
78 int _mixins = 0;
79
80 /// Creates a new type to represent a mixin. Use [comment] to help users
81 /// figure out what mixin is being represented.
82 TypeIdentifier createMixinType([String comment = '']) =>
83 new TypeIdentifier(null, '_M${_mixins++}', comment);
84
85 /// Register that we care to know that [child] extends [parent].
86 void addParent(TypeIdentifier child, TypeIdentifier parent) {
87 var existing = _parents[child];
88 if (existing != null) {
89 if (existing == parent) return;
90 throw new StateError('$child already has a different parent associated'
91 '($existing instead of $parent)');
92 }
93 _addLibrary(child.importUrl);
94 _addLibrary(parent.importUrl);
95 _parents[child] = parent;
96 }
97
98 /// Register a declaration of a field, property, or method. Note that one and
99 /// only one of [isField], [isMethod], or [isProperty] should be set at a
100 /// given time.
101 void addDeclaration(TypeIdentifier cls, String name, TypeIdentifier type,
102 {bool isField: false, bool isProperty: false, bool isMethod: false,
103 bool isFinal: false, bool isStatic: false,
104 List<ConstExpression> annotations: const []}) {
105 final count = (isField ? 1 : 0) + (isProperty ? 1 : 0) + (isMethod ? 1 : 0);
106 if (count != 1) {
107 throw new ArgumentError('Declaration must be one (and only one) of the '
108 'following: a field, a property, or a method.');
109 }
110 var kind = isField ? 'FIELD' : isProperty ? 'PROPERTY' : 'METHOD';
111 _declarations.putIfAbsent(
112 cls, () => new SplayTreeMap<String, _DeclarationCode>());
113 _addLibrary(cls.importUrl);
114 var map = _declarations[cls];
115
116 for (var exp in annotations) {
117 for (var lib in exp.librariesUsed) {
118 _addLibrary(lib);
119 }
120 }
121
122 _addLibrary(type.importUrl);
123 var decl =
124 new _DeclarationCode(name, type, kind, isFinal, isStatic, annotations);
125 if (map.containsKey(name) && map[name] != decl) {
126 throw new StateError('$type.$name already has a different declaration'
127 ' (${map[name]} instead of $decl).');
128 }
129 map[name] = decl;
130 }
131
132 /// Register that we might try to read declarations of [type], even if no
133 /// declaration exists. This informs the smoke system that querying for a
134 /// member in this class might be intentional and not an error.
135 void addEmptyDeclaration(TypeIdentifier type) {
136 _addLibrary(type.importUrl);
137 _declarations.putIfAbsent(
138 type, () => new SplayTreeMap<String, _DeclarationCode>());
139 }
140
141 /// Writes to [buffer] a line for each import that is needed by the generated
142 /// code. The code added by [writeStaticConfiguration] depends on these
143 /// imports.
144 void writeImports(StringBuffer buffer) {
145 DEFAULT_IMPORTS.forEach((i) => buffer.writeln(i));
146 _libraryPrefix.forEach((url, prefix) {
147 buffer.writeln("import '$url' as $prefix;");
148 });
149 }
150
151 /// Writes to [buffer] top-level declarations that are used by the code
152 /// generated in [writeStaticConfiguration]. These are typically declarations
153 /// of empty classes that are then used as placeholders for mixin
154 /// superclasses.
155 void writeTopLevelDeclarations(StringBuffer buffer) {
156 var types = new Set()
157 ..addAll(_parents.keys)
158 ..addAll(_parents.values)
159 ..addAll(_declarations.keys)
160 ..addAll(_declarations.values.expand((m) => m.values.map((d) => d.type)))
161 ..removeWhere((t) => t.importUrl != null);
162 for (var type in types) {
163 buffer.write('abstract class ${type.name} {}');
164 if (type.comment != null) buffer.write(' // ${type.comment}');
165 buffer.writeln();
166 }
167 }
168
169 /// Appends to [buffer] code that will create smoke's static configuration.
170 /// For example, the code might be of the form:
171 ///
172 /// new StaticConfiguration(
173 /// getters: {
174 /// #i: (o) => o.i,
175 /// ...
176 /// names: {
177 /// #i: "i",
178 /// })
179 ///
180 /// Callers of this code can assign this expression to a variable, and should
181 /// generate code that invokes `useGeneratedCode`.
182 ///
183 /// The optional [indent] argument is used for formatting purposes. All
184 /// entries in each map (getters, setters, names, declarations, parents) are
185 /// sorted alphabetically.
186 ///
187 /// **Note**: this code assumes that imports from [writeImports] and top-level
188 /// declarations from [writeTopLevelDeclarations] are included in the same
189 /// library where this code will live.
190 void writeStaticConfiguration(StringBuffer buffer, [int indent = 2]) {
191 final spaces = ' ' * (indent + 4);
192 var args = {};
193
194 if (_getters.isNotEmpty) {
195 args['getters'] = _getters.map((n) => '${_symbol(n)}: (o) => o.$n');
196 }
197 if (_setters.isNotEmpty) {
198 args['setters'] =
199 _setters.map((n) => '${_symbol(n)}: (o, v) { o.$n = v; }');
200 }
201
202 if (_parents.isNotEmpty) {
203 var parentsMap = [];
204 _parents.forEach((child, parent) {
205 var parent = _parents[child];
206 parentsMap.add('${child.asCode(_libraryPrefix)}: '
207 '${parent.asCode(_libraryPrefix)}');
208 });
209 args['parents'] = parentsMap;
210 }
211
212 if (_declarations.isNotEmpty) {
213 var declarations = [];
214 _declarations.forEach((type, members) {
215 final sb = new StringBuffer()
216 ..write(type.asCode(_libraryPrefix))
217 ..write(': ');
218 if (members.isEmpty) {
219 sb.write('{}');
220 } else {
221 sb.write('{\n');
222 members.forEach((name, decl) {
223 var decl = members[name].asCode(_libraryPrefix);
224 sb.write('${spaces} ${_symbol(name)}: $decl,\n');
225 });
226 sb.write('${spaces} }');
227 }
228 declarations.add(sb.toString());
229 });
230 args['declarations'] = declarations;
231 }
232
233 if (_staticMethods.isNotEmpty) {
234 var methods = [];
235 _staticMethods.forEach((type, members) {
236 var className = type.asCode(_libraryPrefix);
237 final sb = new StringBuffer()
238 ..write(className)
239 ..write(': ');
240 if (members.isEmpty) {
241 sb.write('{}');
242 } else {
243 sb.write('{\n');
244 for (var name in members) {
245 sb.write('${spaces} ${_symbol(name)}: $className.$name,\n');
246 }
247 sb.write('${spaces} }');
248 }
249 methods.add(sb.toString());
250 });
251 args['staticMethods'] = methods;
252 }
253
254 if (_names.isNotEmpty) {
255 args['names'] = _names.map((n) => "${_symbol(n)}: r'$n'");
256 }
257
258 buffer
259 ..writeln('new StaticConfiguration(')
260 ..write('${spaces}checkedMode: false');
261
262 args.forEach((name, mapContents) {
263 buffer.writeln(',');
264 // TODO(sigmund): use const map when Type can be keys (dartbug.com/17123)
265 buffer.writeln('${spaces}$name: {');
266 for (var entry in mapContents) {
267 buffer.writeln('${spaces} $entry,');
268 }
269 buffer.write('${spaces}}');
270 });
271 buffer.write(')');
272 }
273
274 /// Adds a library that needs to be imported.
275 void _addLibrary(String url) {
276 if (url == null || url == 'dart:core') return;
277 _libraryPrefix.putIfAbsent(url, () => 'smoke_${_libraryPrefix.length}');
278 }
279 }
280
281 /// Information used to generate code that allocates a `Declaration` object.
282 class _DeclarationCode extends ConstExpression {
283 final String name;
284 final TypeIdentifier type;
285 final String kind;
286 final bool isFinal;
287 final bool isStatic;
288 final List<ConstExpression> annotations;
289
290 _DeclarationCode(this.name, this.type, this.kind, this.isFinal, this.isStatic,
291 this.annotations);
292
293 List<String> get librariesUsed => []
294 ..addAll(type.librariesUsed)
295 ..addAll(annotations.expand((a) => a.librariesUsed));
296
297 String asCode(Map<String, String> libraryPrefixes) {
298 var sb = new StringBuffer();
299 sb.write('const Declaration(${_symbol(name)}, '
300 '${type.asCode(libraryPrefixes)}');
301 if (kind != 'FIELD') sb.write(', kind: $kind');
302 if (isFinal) sb.write(', isFinal: true');
303 if (isStatic) sb.write(', isStatic: true');
304 if (annotations != null && annotations.isNotEmpty) {
305 sb.write(', annotations: const [');
306 bool first = true;
307 for (var e in annotations) {
308 if (!first) sb.write(', ');
309 first = false;
310 sb.write(e.asCode(libraryPrefixes));
311 }
312 sb.write(']');
313 }
314 sb.write(')');
315 return sb.toString();
316 }
317
318 String toString() =>
319 '(decl: $type.$name - $kind, $isFinal, $isStatic, $annotations)';
320 operator ==(other) => other is _DeclarationCode &&
321 name == other.name &&
322 type == other.type &&
323 kind == other.kind &&
324 isFinal == other.isFinal &&
325 isStatic == other.isStatic &&
326 compareLists(annotations, other.annotations);
327 int get hashCode => name.hashCode + (31 * type.hashCode);
328 }
329
330 /// A constant expression that can be used as an annotation.
331 abstract class ConstExpression {
332
333 /// Returns the library URLs that needs to be imported for this
334 /// [ConstExpression] to be a valid annotation.
335 List<String> get librariesUsed;
336
337 /// Return a string representation of the code in this expression.
338 /// [libraryPrefixes] describes what prefix has been associated with each
339 /// import url mentioned in [libraryUsed].
340 String asCode(Map<String, String> libraryPrefixes);
341
342 ConstExpression();
343
344 /// Create a string expression of the form `'string'`, where [string] is
345 /// normalized so we can correctly wrap it in single quotes.
346 factory ConstExpression.string(String string) {
347 var value = string.replaceAll(r'\', r'\\').replaceAll(r"'", r"\'");
348 return new CodeAsConstExpression("'$value'");
349 }
350
351 /// Create an expression of the form `prefix.variable_name`.
352 factory ConstExpression.identifier(String importUrl, String name) =>
353 new TopLevelIdentifier(importUrl, name);
354
355 /// Create an expression of the form `prefix.Constructor(v1, v2, p3: v3)`.
356 factory ConstExpression.constructor(String importUrl, String name,
357 List<ConstExpression> positionalArgs,
358 Map<String, ConstExpression> namedArgs) =>
359 new ConstructorExpression(importUrl, name, positionalArgs, namedArgs);
360 }
361
362 /// A constant expression written as a String. Used when the code is self
363 /// contained and it doesn't depend on any imported libraries.
364 class CodeAsConstExpression extends ConstExpression {
365 String code;
366 List<String> get librariesUsed => const [];
367
368 CodeAsConstExpression(this.code);
369
370 String asCode(Map<String, String> libraryPrefixes) => code;
371
372 String toString() => '(code: $code)';
373 operator ==(other) => other is CodeAsConstExpression && code == other.code;
374 int get hashCode => code.hashCode;
375 }
376
377 /// Describes a reference to some symbol that is exported from a library. This
378 /// is typically used to refer to a type or a top-level variable from that
379 /// library.
380 class TopLevelIdentifier extends ConstExpression {
381 final String importUrl;
382 final String name;
383 TopLevelIdentifier(this.importUrl, this.name);
384
385 List<String> get librariesUsed => [importUrl];
386 String asCode(Map<String, String> libraryPrefixes) {
387 if (importUrl == 'dart:core' || importUrl == null) {
388 // TODO(jmesserly): analyzer doesn't consider `dynamic` to be a constant,
389 // so use Object as a workaround:
390 // https://code.google.com/p/dart/issues/detail?id=22989
391 return name == 'dynamic' ? 'Object' : name;
392 }
393 return '${libraryPrefixes[importUrl]}.$name';
394 }
395
396 String toString() => '(identifier: $importUrl, $name)';
397 operator ==(other) => other is TopLevelIdentifier &&
398 name == other.name &&
399 importUrl == other.importUrl;
400 int get hashCode => 31 * importUrl.hashCode + name.hashCode;
401 }
402
403 /// Represents an expression that invokes a const constructor.
404 class ConstructorExpression extends ConstExpression {
405 final String importUrl;
406 final String name;
407 final List<ConstExpression> positionalArgs;
408 final Map<String, ConstExpression> namedArgs;
409 ConstructorExpression(
410 this.importUrl, this.name, this.positionalArgs, this.namedArgs);
411
412 List<String> get librariesUsed => [importUrl]
413 ..addAll(positionalArgs.expand((e) => e.librariesUsed))
414 ..addAll(namedArgs.values.expand((e) => e.librariesUsed));
415
416 String asCode(Map<String, String> libraryPrefixes) {
417 var sb = new StringBuffer();
418 sb.write('const ');
419 if (importUrl != 'dart:core' && importUrl != null) {
420 sb.write('${libraryPrefixes[importUrl]}.');
421 }
422 sb.write('$name(');
423 bool first = true;
424 for (var e in positionalArgs) {
425 if (!first) sb.write(', ');
426 first = false;
427 sb.write(e.asCode(libraryPrefixes));
428 }
429 namedArgs.forEach((name, value) {
430 if (!first) sb.write(', ');
431 first = false;
432 sb.write('$name: ');
433 sb.write(value.asCode(libraryPrefixes));
434 });
435 sb.write(')');
436 return sb.toString();
437 }
438
439 String toString() => '(ctor: $importUrl, $name, $positionalArgs, $namedArgs)';
440 operator ==(other) => other is ConstructorExpression &&
441 name == other.name &&
442 importUrl == other.importUrl &&
443 compareLists(positionalArgs, other.positionalArgs) &&
444 compareMaps(namedArgs, other.namedArgs);
445 int get hashCode => 31 * importUrl.hashCode + name.hashCode;
446 }
447
448 /// Describes a type identifier, with the library URL where the type is defined.
449 // TODO(sigmund): consider adding support for imprecise TypeIdentifiers, which
450 // may be used by tools that want to generate code without using the analyzer
451 // (they can syntactically tell the type comes from one of N imports).
452 class TypeIdentifier extends TopLevelIdentifier
453 implements Comparable<TypeIdentifier> {
454 final String comment;
455 TypeIdentifier(importUrl, typeName, [this.comment])
456 : super(importUrl, typeName);
457
458 // We implement [Comparable] to sort out entries in the generated code.
459 int compareTo(TypeIdentifier other) {
460 if (importUrl == null && other.importUrl != null) return 1;
461 if (importUrl != null && other.importUrl == null) return -1;
462 var c1 = importUrl == null ? 0 : importUrl.compareTo(other.importUrl);
463 return c1 != 0 ? c1 : name.compareTo(other.name);
464 }
465
466 String toString() => '(type-identifier: $importUrl, $name, $comment)';
467 bool operator ==(other) => other is TypeIdentifier &&
468 importUrl == other.importUrl &&
469 name == other.name &&
470 comment == other.comment;
471 int get hashCode => super.hashCode;
472 }
473
474 /// Default set of imports added by [SmokeCodeGenerator].
475 const DEFAULT_IMPORTS = const [
476 "import 'package:smoke/smoke.dart' show Declaration, PROPERTY, METHOD;",
477 "import 'package:smoke/static.dart' show "
478 "useGeneratedCode, StaticConfiguration;",
479 ];
480
481 _symbol(String name) {
482 if (!_publicSymbolPattern.hasMatch(name)) {
483 throw new StateError('invalid symbol name: "$name"');
484 }
485 return _literalSymbolPattern.hasMatch(name)
486 ? '#$name'
487 : "const Symbol('$name')";
488 }
489
490 // TODO(sigmund): is this included in some library we can import? I derived the
491 // definitions below from sdk/lib/internal/symbol.dart.
492
493 /// Reserved words in Dart.
494 const String _reservedWordRE =
495 r'(?:assert|break|c(?:a(?:se|tch)|lass|on(?:st|tinue))|d(?:efault|o)|'
496 r'e(?:lse|num|xtends)|f(?:alse|inal(?:ly)?|or)|i[fns]|n(?:ew|ull)|'
497 r'ret(?:hrow|urn)|s(?:uper|witch)|t(?:h(?:is|row)|r(?:ue|y))|'
498 r'v(?:ar|oid)|w(?:hile|ith))';
499
500 /// Public identifier: a valid identifier (not a reserved word) that doesn't
501 /// start with '_'.
502 const String _publicIdentifierRE =
503 r'(?!' '$_reservedWordRE' r'\b(?!\$))[a-zA-Z$][\w$]*';
504
505 /// Pattern that matches operators only.
506 final RegExp _literalSymbolPattern =
507 new RegExp('^(?:$_publicIdentifierRE(?:\$|[.](?!\$)))+?\$');
508
509 /// Operator names allowed as symbols. The name of the oeprators is the same as
510 /// the operator itself except for unary minus, where the name is "unary-".
511 const String _operatorRE =
512 r'(?:[\-+*/%&|^]|\[\]=?|==|~/?|<[<=]?|>[>=]?|unary-)';
513
514 /// Pattern that matches public symbols.
515 final RegExp _publicSymbolPattern = new RegExp(
516 '^(?:$_operatorRE\$|$_publicIdentifierRE(?:=?\$|[.](?!\$)))+?\$');
OLDNEW
« no previous file with comments | « packages/smoke/codereview.settings ('k') | packages/smoke/lib/codegen/recorder.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698