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