| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 class ScannerTask extends CompilerTask { | 5 class ScannerTask extends CompilerTask { |
| 6 ScannerTask(Compiler compiler) : super(compiler); | 6 ScannerTask(Compiler compiler) : super(compiler); |
| 7 String get name() => 'Scanner'; | 7 String get name() => 'Scanner'; |
| 8 | 8 |
| 9 void scanLibrary(LibraryElement library) { |
| 10 var compilationUnit = library.entryCompilationUnit; |
| 11 compiler.log("scanning library ${compilationUnit.script.name}"); |
| 12 scan(compilationUnit); |
| 13 processScriptTags(library); |
| 14 } |
| 15 |
| 9 void scan(CompilationUnitElement compilationUnit) { | 16 void scan(CompilationUnitElement compilationUnit) { |
| 10 measure(() { | 17 measure(() { |
| 11 if (compilationUnit.kind === ElementKind.LIBRARY) { | |
| 12 compiler.log("scanning library ${compilationUnit.script.name}"); | |
| 13 } | |
| 14 scanElements(compilationUnit); | 18 scanElements(compilationUnit); |
| 15 if (compilationUnit.kind === ElementKind.LIBRARY) { | |
| 16 processScriptTags(compilationUnit); | |
| 17 } | |
| 18 }); | 19 }); |
| 19 } | 20 } |
| 20 | 21 |
| 21 void processScriptTags(LibraryElement library) { | 22 void processScriptTags(LibraryElement library) { |
| 22 int tagState = TagState.NO_TAG_SEEN; | 23 int tagState = TagState.NO_TAG_SEEN; |
| 23 | 24 |
| 24 /** | 25 /** |
| 25 * If [value] is less than [tagState] complain and return | 26 * If [value] is less than [tagState] complain and return |
| 26 * [tagState]. Otherwise return the new value for [tagState] | 27 * [tagState]. Otherwise return the new value for [tagState] |
| 27 * (transition function for state machine). | 28 * (transition function for state machine). |
| 28 */ | 29 */ |
| 29 int checkTag(int value, ScriptTag tag) { | 30 int checkTag(int value, ScriptTag tag) { |
| 30 if (tagState > value) { | 31 if (tagState > value) { |
| 31 compiler.reportError(tag, 'out of order'); | 32 compiler.reportError(tag, 'out of order'); |
| 32 return tagState; | 33 return tagState; |
| 33 } | 34 } |
| 34 return TagState.NEXT[value]; | 35 return TagState.NEXT[value]; |
| 35 } | 36 } |
| 36 | 37 |
| 37 LinkBuilder<ScriptTag> imports = new LinkBuilder<ScriptTag>(); | 38 LinkBuilder<ScriptTag> imports = new LinkBuilder<ScriptTag>(); |
| 38 Uri base = library.script.uri; | 39 Uri base = library.uri; |
| 39 for (ScriptTag tag in library.tags.reverse()) { | 40 for (ScriptTag tag in library.tags.reverse()) { |
| 40 StringNode argument = tag.argument; | 41 StringNode argument = tag.argument; |
| 41 // TODO(lrn): Support interpolations here. We need access to the | 42 // TODO(lrn): Support interpolations here. We need access to the |
| 42 // special constants that can be inserted into script tag strings. | 43 // special constants that can be inserted into script tag strings. |
| 43 Uri resolved = base.resolve(argument.dartString.slowToString()); | 44 Uri resolved = base.resolve(argument.dartString.slowToString()); |
| 44 if (tag.isImport()) { | 45 if (tag.isImport()) { |
| 45 tagState = checkTag(TagState.IMPORT, tag); | 46 tagState = checkTag(TagState.IMPORT, tag); |
| 46 // It is not safe to import other libraries at this point as | 47 // It is not safe to import other libraries at this point as |
| 47 // another library could then observe the current library | 48 // another library could then observe the current library |
| 48 // before it fully declares all the members that are sourced | 49 // before it fully declares all the members that are sourced |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 LibraryElement library = | 116 LibraryElement library = |
| 116 compiler.libraries.putIfAbsent(uri.toString(), () { | 117 compiler.libraries.putIfAbsent(uri.toString(), () { |
| 117 newLibrary = true; | 118 newLibrary = true; |
| 118 Script script = compiler.readScript(uri, node); | 119 Script script = compiler.readScript(uri, node); |
| 119 LibraryElement element = new LibraryElement(script); | 120 LibraryElement element = new LibraryElement(script); |
| 120 native.maybeEnableNative(compiler, element, uri); | 121 native.maybeEnableNative(compiler, element, uri); |
| 121 return element; | 122 return element; |
| 122 }); | 123 }); |
| 123 if (newLibrary) { | 124 if (newLibrary) { |
| 124 compiler.withCurrentElement(library, () { | 125 compiler.withCurrentElement(library, () { |
| 125 scan(library); | 126 scanLibrary(library); |
| 126 compiler.onLibraryLoaded(library, uri); | 127 compiler.onLibraryLoaded(library, uri); |
| 127 }); | 128 }); |
| 128 } | 129 } |
| 129 return library; | 130 return library; |
| 130 } | 131 } |
| 131 | 132 |
| 132 void importLibrary(LibraryElement library, LibraryElement imported, | 133 void importLibrary(LibraryElement library, LibraryElement imported, |
| 133 ScriptTag tag, [CompilationUnitElement patch]) { | 134 ScriptTag tag, |
| 135 [CompilationUnitElement patchCompilationUnit]) { |
| 134 if (!imported.hasLibraryName()) { | 136 if (!imported.hasLibraryName()) { |
| 135 compiler.withCurrentElement(library, () { | 137 compiler.withCurrentElement(library, () { |
| 136 compiler.reportError(tag === null ? null : tag.argument, | 138 compiler.reportError(tag === null ? null : tag.argument, |
| 137 'no #library tag found in ${imported.script.uri}'); | 139 'no #library tag found in ${imported.uri}'); |
| 138 }); | 140 }); |
| 139 } | 141 } |
| 140 if (tag !== null && tag.prefix !== null) { | 142 if (tag !== null && tag.prefix !== null) { |
| 141 SourceString prefix = | 143 SourceString prefix = |
| 142 new SourceString(tag.prefix.dartString.slowToString()); | 144 new SourceString(tag.prefix.dartString.slowToString()); |
| 143 Element e = library.find(prefix); | 145 Element e = library.find(prefix); |
| 144 if (e === null) { | 146 if (e === null) { |
| 145 e = new PrefixElement(prefix, library, tag.getBeginToken(), patch); | 147 e = new PrefixElement(prefix, library, tag.getBeginToken(), |
| 146 library.define(e, compiler); | 148 patchCompilationUnit); |
| 149 library.addToScope(e, compiler); |
| 147 } | 150 } |
| 148 if (e.kind !== ElementKind.PREFIX) { | 151 if (e.kind !== ElementKind.PREFIX) { |
| 149 compiler.withCurrentElement(e, () { | 152 compiler.withCurrentElement(e, () { |
| 150 compiler.reportWarning(new Identifier(e.position()), | 153 compiler.reportWarning(new Identifier(e.position()), |
| 151 'duplicated definition'); | 154 'duplicated definition'); |
| 152 }); | 155 }); |
| 153 compiler.reportError(tag.prefix, 'duplicate defintion'); | 156 compiler.reportError(tag.prefix, 'duplicate defintion'); |
| 154 } | 157 } |
| 155 PrefixElement prefixElement = e; | 158 PrefixElement prefixElement = e; |
| 156 imported.forEachExport((Element element) { | 159 imported.forEachExport((Element element) { |
| 157 Element existing = | 160 Element existing = |
| 158 prefixElement.imported.putIfAbsent(element.name, () => element); | 161 prefixElement.imported.putIfAbsent(element.name, () => element); |
| 159 if (existing !== element) { | 162 if (existing !== element) { |
| 160 compiler.withCurrentElement(existing, () { | 163 compiler.withCurrentElement(existing, () { |
| 161 compiler.reportWarning(new Identifier(existing.position()), | 164 compiler.reportWarning(new Identifier(existing.position()), |
| 162 'duplicated import'); | 165 'duplicated import'); |
| 163 }); | 166 }); |
| 164 compiler.withCurrentElement(element, () { | 167 compiler.withCurrentElement(element, () { |
| 165 compiler.reportError(new Identifier(element.position()), | 168 compiler.reportError(new Identifier(element.position()), |
| 166 'duplicated import'); | 169 'duplicated import'); |
| 167 }); | 170 }); |
| 168 } | 171 } |
| 169 }); | 172 }); |
| 170 } else { | 173 } else { |
| 171 imported.forEachExport((Element element) { | 174 imported.forEachExport((Element element) { |
| 172 compiler.withCurrentElement(element, () { | 175 compiler.withCurrentElement(element, () { |
| 173 library.define(element, compiler); | 176 library.addToScope(element, compiler); |
| 174 }); | 177 }); |
| 175 }); | 178 }); |
| 176 } | 179 } |
| 177 } | 180 } |
| 178 } | 181 } |
| 179 | 182 |
| 180 class DietParserTask extends CompilerTask { | 183 class DietParserTask extends CompilerTask { |
| 181 DietParserTask(Compiler compiler) : super(compiler); | 184 DietParserTask(Compiler compiler) : super(compiler); |
| 182 final String name = 'Diet Parser'; | 185 final String name = 'Diet Parser'; |
| 183 | 186 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 204 static final int RESOURCE = 4; | 207 static final int RESOURCE = 4; |
| 205 | 208 |
| 206 /** Next state. */ | 209 /** Next state. */ |
| 207 static final List<int> NEXT = | 210 static final List<int> NEXT = |
| 208 const <int>[NO_TAG_SEEN, | 211 const <int>[NO_TAG_SEEN, |
| 209 IMPORT, // Only one library tag is allowed. | 212 IMPORT, // Only one library tag is allowed. |
| 210 IMPORT, | 213 IMPORT, |
| 211 SOURCE, | 214 SOURCE, |
| 212 RESOURCE]; | 215 RESOURCE]; |
| 213 } | 216 } |
| OLD | NEW |