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

Unified Diff: lib/transformer.dart

Issue 865293002: support basic identifiers in constructors (Closed) Base URL: git@github.com:dart-lang/static-init.git@master
Patch Set: rebase Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « CHANGELOG.md ('k') | test/common.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/transformer.dart
diff --git a/lib/transformer.dart b/lib/transformer.dart
index ce06839bbcffa12a15390b210ef6e1e92e809229..392d3228a14fd429a86df1a884cce6a99f72d551 100644
--- a/lib/transformer.dart
+++ b/lib/transformer.dart
@@ -93,10 +93,11 @@ class InitializeTransformer extends Transformer {
transform.logger
.error('New entry point file $newEntryPointId already exists.');
} else {
- return _resolvers
- .get(transform)
- .then((resolver) => new _BootstrapFileBuilder(resolver, transform,
- transform.primaryInput.id, newEntryPointId).run());
+ return _resolvers.get(transform).then((resolver) {
+ new _BootstrapFileBuilder(resolver, transform,
+ transform.primaryInput.id, newEntryPointId).run();
+ resolver.release();
+ });
}
});
}
@@ -244,20 +245,24 @@ class _BootstrapFileBuilder {
// Import the static_loader and original entry point.
importsBuffer
.writeln("import 'package:initialize/src/static_loader.dart';");
- _maybeWriteImport(entryLib, libraryPrefixes, importsBuffer);
+ libraryPrefixes[entryLib] = 'i0';
initializersBuffer.writeln(' initializers.addAll([');
while (_initQueue.isNotEmpty) {
var next = _initQueue.removeFirst();
- _maybeWriteImport(next.element.library, libraryPrefixes, importsBuffer);
- _maybeWriteImport(
- next.annotation.element.library, libraryPrefixes, importsBuffer);
+ libraryPrefixes.putIfAbsent(
+ next.element.library, () => 'i${libraryPrefixes.length}');
+ libraryPrefixes.putIfAbsent(
+ next.annotation.element.library, () => 'i${libraryPrefixes.length}');
_writeInitializer(next, libraryPrefixes, initializersBuffer);
}
initializersBuffer.writeln(' ]);');
+ libraryPrefixes
+ .forEach((lib, prefix) => _writeImport(lib, prefix, importsBuffer));
+
// TODO(jakemac): copyright and library declaration
return '''
$importsBuffer
@@ -268,15 +273,6 @@ $initializersBuffer
''';
}
- // Writes an import to library if it doesn't exist yet if libraries.
- _maybeWriteImport(LibraryElement library,
- Map<LibraryElement, String> libraries, StringBuffer buffer) {
- if (libraries.containsKey(library)) return;
- var prefix = 'i${libraries.length}';
- libraries[library] = prefix;
- _writeImport(library, prefix, buffer);
- }
-
_writeImport(LibraryElement lib, String prefix, StringBuffer buffer) {
AssetId id = (lib.source as dynamic).assetId;
@@ -333,7 +329,7 @@ $initializersBuffer
: '.${annotation.constructorName}';
// TODO(jakemac): Support more than raw values here
// https://github.com/dart-lang/static_init/issues/5
- final args = annotation.arguments;
+ final args = _buildArgsString(annotation.arguments, libraryPrefixes);
buffer.write('''
new InitEntry(const $metaPrefix.${clazz}$constructor$args, $elementString),
''');
@@ -341,7 +337,97 @@ $initializersBuffer
buffer.write('''
new InitEntry($metaPrefix.${annotationElement.name}, $elementString),
''');
+ } else {
+ _logger.error('Unsupported annotation type. Only constructors and '
+ 'properties are supported as initializers.');
+ }
+ }
+
+ String _buildArgsString(
+ ArgumentList args, Map<LibraryElement, String> libraryPrefixes) {
+ var buffer = new StringBuffer();
+ buffer.write('(');
+ var first = true;
+ for (var arg in args.arguments) {
+ if (!first) buffer.write(', ');
+ first = false;
+
+ Expression expression;
+ if (arg is NamedExpression) {
+ buffer.write('${arg.name.label.name}: ');
+ expression = arg.expression;
+ } else {
+ expression = arg;
+ }
+
+ buffer.write(_expressionString(expression, libraryPrefixes));
+ }
+ buffer.write(')');
+ return buffer.toString();
+ }
+
+ String _expressionString(
+ Expression expression, Map<LibraryElement, String> libraryPrefixes) {
+ var buffer = new StringBuffer();
+ if (expression is StringLiteral) {
+ var value = expression.stringValue;
+ if (value == null) {
+ _logger.error('Only const strings are allowed in initializer '
+ 'expressions, found $expression');
+ }
+ value = value.replaceAll(r'\', r'\\').replaceAll(r"'", r"\'");
+ buffer.write("'$value'");
+ } else if (expression is BooleanLiteral ||
+ expression is DoubleLiteral ||
+ expression is IntegerLiteral ||
+ expression is NullLiteral) {
+ buffer.write('${expression}');
+ } else if (expression is ListLiteral) {
+ buffer.write('const [');
+ var first = true;
+ for (Expression listExpression in expression.elements) {
+ if (!first) buffer.write(', ');
+ first = false;
+ buffer.write(_expressionString(listExpression, libraryPrefixes));
+ }
+ buffer.write(']');
+ } else if (expression is MapLiteral) {
+ buffer.write('const {');
+ var first = true;
+ for (MapLiteralEntry entry in expression.entries) {
+ if (!first) buffer.write(', ');
+ first = false;
+ buffer.write(_expressionString(entry.key, libraryPrefixes));
+ buffer.write(': ');
+ buffer.write(_expressionString(entry.value, libraryPrefixes));
+ }
+ buffer.write('}');
+ } else if (expression is Identifier) {
+ var element = expression.bestElement;
+ if (element == null || !element.isPublic) {
+ _logger.error('Private constants are not supported in intializer '
+ 'constructors, found $element.');
+ }
+ libraryPrefixes.putIfAbsent(
+ element.library, () => 'i${libraryPrefixes.length}');
+
+ buffer.write('${libraryPrefixes[element.library]}.');
+ if (element is ClassElement) {
+ buffer.write(element.name);
+ } else if (element is PropertyAccessorElement) {
+ var variable = element.variable;
+ if (variable is FieldElement) {
+ buffer.write('${variable.enclosingElement.name}.');
+ }
+ buffer.write('${variable.name}');
+ } else {
+ _logger.error('Unsupported argument to initializer constructor.');
+ }
+ } else {
+ _logger.error('Only literals and identifiers are allowed for initializer '
+ 'expressions, found $expression.');
}
+ return buffer.toString();
}
bool _isInitializer(InterfaceType type) {
« no previous file with comments | « CHANGELOG.md ('k') | test/common.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698