| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of dart2js.js_emitter; | 5 part of dart2js.js_emitter; |
| 6 | 6 |
| 7 class MetadataEmitter extends CodeEmitterHelper { | 7 class MetadataEmitter extends CodeEmitterHelper { |
| 8 /// A list of JS expressions that represent metadata, parameter names and | 8 /// A list of JS expressions that represent metadata, parameter names and |
| 9 /// type, and return types. | 9 /// type, and return types. |
| 10 final List<String> globalMetadata = []; | 10 final List<String> globalMetadata = []; |
| 11 | 11 |
| 12 /// A map used to canonicalize the entries of globalMetadata. | 12 /// A map used to canonicalize the entries of globalMetadata. |
| 13 final Map<String, int> globalMetadataMap = <String, int>{}; | 13 final Map<String, int> globalMetadataMap = <String, int>{}; |
| 14 | 14 |
| 15 bool mustEmitMetadataFor(Element element) { | 15 bool mustEmitMetadataFor(Element element) { |
| 16 return backend.mustRetainMetadata && | 16 return backend.mustRetainMetadata && |
| 17 backend.referencedFromMirrorSystem(element); | 17 backend.referencedFromMirrorSystem(element); |
| 18 } | 18 } |
| 19 | 19 |
| 20 /// The metadata function returns the metadata associated with | 20 /// The metadata function returns the metadata associated with |
| 21 /// [element] in generated code. The metadata needs to be wrapped | 21 /// [element] in generated code. The metadata needs to be wrapped |
| 22 /// in a function as it refers to constants that may not have been | 22 /// in a function as it refers to constants that may not have been |
| 23 /// constructed yet. For example, a class is allowed to be | 23 /// constructed yet. For example, a class is allowed to be |
| 24 /// annotated with itself. The metadata function is used by | 24 /// annotated with itself. The metadata function is used by |
| 25 /// mirrors_patch to implement DeclarationMirror.metadata. | 25 /// mirrors_patch to implement DeclarationMirror.metadata. |
| 26 jsAst.Fun buildMetadataFunction(Element element) { | 26 jsAst.Fun buildMetadataFunction(Element element) { |
| 27 if (!mustEmitMetadataFor(element)) return null; | 27 if (!mustEmitMetadataFor(element)) return null; |
| 28 return compiler.withCurrentElement(element, () { | 28 return compiler.withCurrentElement(element, () { |
| 29 var metadata = []; | 29 List<jsAst.Expression> metadata = <jsAst.Expression>[]; |
| 30 Link link = element.metadata; | 30 Link link = element.metadata; |
| 31 // TODO(ahe): Why is metadata sometimes null? | 31 // TODO(ahe): Why is metadata sometimes null? |
| 32 if (link != null) { | 32 if (link != null) { |
| 33 for (; !link.isEmpty; link = link.tail) { | 33 for (; !link.isEmpty; link = link.tail) { |
| 34 MetadataAnnotation annotation = link.head; | 34 MetadataAnnotation annotation = link.head; |
| 35 ConstantExpression constant = | 35 ConstantExpression constant = |
| 36 backend.constants.getConstantForMetadata(annotation); | 36 backend.constants.getConstantForMetadata(annotation); |
| 37 if (constant == null) { | 37 if (constant == null) { |
| 38 compiler.internalError(annotation, 'Annotation value is null.'); | 38 compiler.internalError(annotation, 'Annotation value is null.'); |
| 39 } else { | 39 } else { |
| 40 metadata.add(emitter.constantReference(constant.value)); | 40 metadata.add(emitter.constantReference(constant.value)); |
| 41 } | 41 } |
| 42 } | 42 } |
| 43 } | 43 } |
| 44 if (metadata.isEmpty) return null; | 44 if (metadata.isEmpty) return null; |
| 45 return js('function() { return # }', | 45 return js('function() { return # }', |
| 46 new jsAst.ArrayInitializer.from(metadata)); | 46 new jsAst.ArrayInitializer(metadata)); |
| 47 }); | 47 }); |
| 48 } | 48 } |
| 49 | 49 |
| 50 List<int> reifyDefaultArguments(FunctionElement function) { | 50 List<int> reifyDefaultArguments(FunctionElement function) { |
| 51 FunctionSignature signature = function.functionSignature; | 51 FunctionSignature signature = function.functionSignature; |
| 52 if (signature.optionalParameterCount == 0) return const []; | 52 if (signature.optionalParameterCount == 0) return const []; |
| 53 List<int> defaultValues = <int>[]; | 53 List<int> defaultValues = <int>[]; |
| 54 for (ParameterElement element in signature.optionalParameters) { | 54 for (ParameterElement element in signature.optionalParameters) { |
| 55 ConstantExpression constant = | 55 ConstantExpression constant = |
| 56 backend.constants.getConstantForVariable(element); | 56 backend.constants.getConstantForVariable(element); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 // TODO(ahe): Why is metadata sometimes null? | 128 // TODO(ahe): Why is metadata sometimes null? |
| 129 if (link != null) { | 129 if (link != null) { |
| 130 for (; !link.isEmpty; link = link.tail) { | 130 for (; !link.isEmpty; link = link.tail) { |
| 131 metadata.add(reifyMetadata(link.head)); | 131 metadata.add(reifyMetadata(link.head)); |
| 132 } | 132 } |
| 133 } | 133 } |
| 134 return metadata; | 134 return metadata; |
| 135 }); | 135 }); |
| 136 } | 136 } |
| 137 } | 137 } |
| OLD | NEW |