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. |
11 library smoke.codegen.generator; | 11 library smoke.codegen.generator; |
12 | 12 |
13 import 'dart:collection' show SplayTreeMap, SplayTreeSet; | 13 import 'dart:collection' show SplayTreeMap, SplayTreeSet; |
14 | 14 |
| 15 import 'package:smoke/src/common.dart' show compareLists, compareMaps; |
| 16 |
15 /// Collects the necessary information and generates code to initialize a | 17 /// Collects the necessary information and generates code to initialize a |
16 /// `StaticConfiguration`. After setting up the generator by calling | 18 /// `StaticConfiguration`. After setting up the generator by calling |
17 /// [addGetter], [addSetter], [addSymbol], and [addDeclaration], you can | 19 /// [addGetter], [addSetter], [addSymbol], and [addDeclaration], you can |
18 /// retrieve the generated code using the following three methods: | 20 /// retrieve the generated code using the following three methods: |
19 /// | 21 /// |
20 /// * [writeImports] writes a list of imports directives, | 22 /// * [writeImports] writes a list of imports directives, |
21 /// * [writeTopLevelDeclarations] writes additional declarations used to | 23 /// * [writeTopLevelDeclarations] writes additional declarations used to |
22 /// represent mixin classes by name in the generated code. | 24 /// represent mixin classes by name in the generated code. |
23 /// * [writeInitCall] writes the actual code that allocates the static | 25 /// * [writeInitCall] writes the actual code that allocates the static |
24 /// configuration. | 26 /// configuration. |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 } | 269 } |
268 sb.write(')'); | 270 sb.write(')'); |
269 return sb.toString(); | 271 return sb.toString(); |
270 } | 272 } |
271 | 273 |
272 String toString() => | 274 String toString() => |
273 '(decl: $type.$name - $kind, $isFinal, $isStatic, $annotations)'; | 275 '(decl: $type.$name - $kind, $isFinal, $isStatic, $annotations)'; |
274 operator== (other) => other is _DeclarationCode && name == other.name && | 276 operator== (other) => other is _DeclarationCode && name == other.name && |
275 type == other.type && kind == other.kind && isFinal == other.isFinal && | 277 type == other.type && kind == other.kind && isFinal == other.isFinal && |
276 isStatic == other.isStatic && | 278 isStatic == other.isStatic && |
277 _compareLists(annotations, other.annotations); | 279 compareLists(annotations, other.annotations); |
278 int get hashCode => name.hashCode + (31 * type.hashCode); | 280 int get hashCode => name.hashCode + (31 * type.hashCode); |
279 } | 281 } |
280 | 282 |
281 /// A constant expression that can be used as an annotation. | 283 /// A constant expression that can be used as an annotation. |
282 abstract class ConstExpression { | 284 abstract class ConstExpression { |
283 | 285 |
284 /// Returns the library URLs that needs to be imported for this | 286 /// Returns the library URLs that needs to be imported for this |
285 /// [ConstExpression] to be a valid annotation. | 287 /// [ConstExpression] to be a valid annotation. |
286 List<String> get librariesUsed; | 288 List<String> get librariesUsed; |
287 | 289 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 sb.write('$name: '); | 379 sb.write('$name: '); |
378 sb.write(value.asCode(libraryPrefixes)); | 380 sb.write(value.asCode(libraryPrefixes)); |
379 }); | 381 }); |
380 sb.write(')'); | 382 sb.write(')'); |
381 return sb.toString(); | 383 return sb.toString(); |
382 } | 384 } |
383 | 385 |
384 String toString() => '(ctor: $importUrl, $name, $positionalArgs, $namedArgs)'; | 386 String toString() => '(ctor: $importUrl, $name, $positionalArgs, $namedArgs)'; |
385 operator== (other) => other is ConstructorExpression && name == other.name | 387 operator== (other) => other is ConstructorExpression && name == other.name |
386 && importUrl == other.importUrl && | 388 && importUrl == other.importUrl && |
387 _compareLists(positionalArgs, other.positionalArgs) && | 389 compareLists(positionalArgs, other.positionalArgs) && |
388 _compareMaps(namedArgs, other.namedArgs); | 390 compareMaps(namedArgs, other.namedArgs); |
389 int get hashCode => 31 * importUrl.hashCode + name.hashCode; | 391 int get hashCode => 31 * importUrl.hashCode + name.hashCode; |
390 } | 392 } |
391 | 393 |
392 | 394 |
393 /// Describes a type identifier, with the library URL where the type is defined. | 395 /// Describes a type identifier, with the library URL where the type is defined. |
394 // TODO(sigmund): consider adding support for imprecise TypeIdentifiers, which | 396 // TODO(sigmund): consider adding support for imprecise TypeIdentifiers, which |
395 // may be used by tools that want to generate code without using the analyzer | 397 // may be used by tools that want to generate code without using the analyzer |
396 // (they can syntactically tell the type comes from one of N imports). | 398 // (they can syntactically tell the type comes from one of N imports). |
397 class TypeIdentifier extends TopLevelIdentifier | 399 class TypeIdentifier extends TopLevelIdentifier |
398 implements Comparable<TypeIdentifier> { | 400 implements Comparable<TypeIdentifier> { |
(...skipping 15 matching lines...) Expand all Loading... |
414 comment == other.comment; | 416 comment == other.comment; |
415 int get hashCode => super.hashCode; | 417 int get hashCode => super.hashCode; |
416 } | 418 } |
417 | 419 |
418 /// Default set of imports added by [SmokeCodeGenerator]. | 420 /// Default set of imports added by [SmokeCodeGenerator]. |
419 const DEFAULT_IMPORTS = const [ | 421 const DEFAULT_IMPORTS = const [ |
420 "import 'package:smoke/smoke.dart' show Declaration, PROPERTY, METHOD;", | 422 "import 'package:smoke/smoke.dart' show Declaration, PROPERTY, METHOD;", |
421 "import 'package:smoke/static.dart' show " | 423 "import 'package:smoke/static.dart' show " |
422 "useGeneratedCode, StaticConfiguration;", | 424 "useGeneratedCode, StaticConfiguration;", |
423 ]; | 425 ]; |
424 | |
425 /// Shallow comparison of two lists. | |
426 bool _compareLists(List a, List b) { | |
427 if (a == null && b != null) return false; | |
428 if (a != null && b == null) return false; | |
429 if (a.length != b.length) return false; | |
430 for (int i = 0; i < a.length; i++) { | |
431 if (a[i] != b[i]) return false; | |
432 } | |
433 return true; | |
434 } | |
435 | |
436 /// Shallow comparison of two maps. | |
437 bool _compareMaps(Map a, Map b) { | |
438 if (a == null && b != null) return false; | |
439 if (a != null && b == null) return false; | |
440 if (a.length != b.length) return false; | |
441 for (var k in a.keys) { | |
442 if (!b.containsKey(k) || a[k] != b[k]) return false; | |
443 } | |
444 return true; | |
445 } | |
OLD | NEW |