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