| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 library analyzer.src.task.dart; | |
| 6 | |
| 7 import 'dart:collection'; | |
| 8 | |
| 9 import 'package:analyzer/src/context/cache.dart'; | |
| 10 import 'package:analyzer/src/generated/ast.dart'; | |
| 11 import 'package:analyzer/src/generated/constant.dart'; | |
| 12 import 'package:analyzer/src/generated/element.dart'; | |
| 13 import 'package:analyzer/src/generated/engine.dart' | |
| 14 hide AnalysisCache, AnalysisTask; | |
| 15 import 'package:analyzer/src/generated/error.dart'; | |
| 16 import 'package:analyzer/src/generated/error_verifier.dart'; | |
| 17 import 'package:analyzer/src/generated/java_engine.dart'; | |
| 18 import 'package:analyzer/src/generated/parser.dart'; | |
| 19 import 'package:analyzer/src/generated/resolver.dart'; | |
| 20 import 'package:analyzer/src/generated/scanner.dart'; | |
| 21 import 'package:analyzer/src/generated/sdk.dart'; | |
| 22 import 'package:analyzer/src/generated/source.dart'; | |
| 23 import 'package:analyzer/src/task/driver.dart'; | |
| 24 import 'package:analyzer/src/task/general.dart'; | |
| 25 import 'package:analyzer/src/task/html.dart'; | |
| 26 import 'package:analyzer/src/task/inputs.dart'; | |
| 27 import 'package:analyzer/src/task/model.dart'; | |
| 28 import 'package:analyzer/task/dart.dart'; | |
| 29 import 'package:analyzer/task/general.dart'; | |
| 30 import 'package:analyzer/task/model.dart'; | |
| 31 | |
| 32 /** | |
| 33 * The [ResultCachingPolicy] for ASTs. | |
| 34 */ | |
| 35 const ResultCachingPolicy AST_CACHING_POLICY = | |
| 36 const SimpleResultCachingPolicy(8192, 8192); | |
| 37 | |
| 38 /** | |
| 39 * The errors produced while resolving a library directives. | |
| 40 * | |
| 41 * The list will be empty if there were no errors, but will not be `null`. | |
| 42 * | |
| 43 * The result is only available for [Source]s representing a library. | |
| 44 */ | |
| 45 final ListResultDescriptor<AnalysisError> BUILD_DIRECTIVES_ERRORS = | |
| 46 new ListResultDescriptor<AnalysisError>( | |
| 47 'BUILD_DIRECTIVES_ERRORS', AnalysisError.NO_ERRORS); | |
| 48 | |
| 49 /** | |
| 50 * The errors produced while building a library element. | |
| 51 * | |
| 52 * The list will be empty if there were no errors, but will not be `null`. | |
| 53 * | |
| 54 * The result is only available for [Source]s representing a library. | |
| 55 */ | |
| 56 final ListResultDescriptor<AnalysisError> BUILD_LIBRARY_ERRORS = | |
| 57 new ListResultDescriptor<AnalysisError>( | |
| 58 'BUILD_LIBRARY_ERRORS', AnalysisError.NO_ERRORS); | |
| 59 | |
| 60 /** | |
| 61 * A list of the [ConstantEvaluationTarget]s defined in a unit. This includes | |
| 62 * constants defined at top level, statically inside classes, and local to | |
| 63 * functions, as well as constant constructors, annotations, and default values | |
| 64 * of parameters to constant constructors. | |
| 65 */ | |
| 66 final ListResultDescriptor<ConstantEvaluationTarget> COMPILATION_UNIT_CONSTANTS
= | |
| 67 new ListResultDescriptor<ConstantEvaluationTarget>( | |
| 68 'COMPILATION_UNIT_CONSTANTS', null, | |
| 69 cachingPolicy: ELEMENT_CACHING_POLICY); | |
| 70 | |
| 71 /** | |
| 72 * The element model associated with a single compilation unit. | |
| 73 * | |
| 74 * The result is only available for [LibrarySpecificUnit]s. | |
| 75 */ | |
| 76 final ResultDescriptor<CompilationUnitElement> COMPILATION_UNIT_ELEMENT = | |
| 77 new ResultDescriptor<CompilationUnitElement>( | |
| 78 'COMPILATION_UNIT_ELEMENT', null, | |
| 79 cachingPolicy: ELEMENT_CACHING_POLICY); | |
| 80 | |
| 81 /** | |
| 82 * The list of [ConstantEvaluationTarget]s on which the target constant element | |
| 83 * depends. | |
| 84 * | |
| 85 * The result is only available for targets representing a | |
| 86 * [ConstantEvaluationTarget] (i.e. a constant variable declaration, a constant | |
| 87 * constructor, or a parameter element with a default value). | |
| 88 */ | |
| 89 final ListResultDescriptor<ConstantEvaluationTarget> CONSTANT_DEPENDENCIES = | |
| 90 new ListResultDescriptor<ConstantEvaluationTarget>( | |
| 91 'CONSTANT_DEPENDENCIES', const <ConstantEvaluationTarget>[]); | |
| 92 | |
| 93 /** | |
| 94 * A [ConstantEvaluationTarget] that has been successfully constant-evaluated. | |
| 95 * | |
| 96 * TODO(paulberry): is ELEMENT_CACHING_POLICY the correct caching policy? | |
| 97 */ | |
| 98 final ResultDescriptor<ConstantEvaluationTarget> CONSTANT_VALUE = | |
| 99 new ResultDescriptor<ConstantEvaluationTarget>('CONSTANT_VALUE', null, | |
| 100 cachingPolicy: ELEMENT_CACHING_POLICY); | |
| 101 | |
| 102 /** | |
| 103 * The sources representing the libraries that include a given source as a part. | |
| 104 * | |
| 105 * The result is only available for [Source]s representing a compilation unit. | |
| 106 */ | |
| 107 final ListResultDescriptor<Source> CONTAINING_LIBRARIES = | |
| 108 new ListResultDescriptor<Source>('CONTAINING_LIBRARIES', Source.EMPTY_LIST); | |
| 109 | |
| 110 /** | |
| 111 * The [ResultCachingPolicy] for [Element]s. | |
| 112 */ | |
| 113 const ResultCachingPolicy ELEMENT_CACHING_POLICY = | |
| 114 const SimpleResultCachingPolicy(-1, -1); | |
| 115 | |
| 116 /** | |
| 117 * The sources representing the export closure of a library. | |
| 118 * The [Source]s include only library sources, not their units. | |
| 119 * | |
| 120 * The result is only available for [Source]s representing a library. | |
| 121 */ | |
| 122 final ListResultDescriptor<Source> EXPORT_SOURCE_CLOSURE = | |
| 123 new ListResultDescriptor<Source>('EXPORT_SOURCE_CLOSURE', null); | |
| 124 | |
| 125 /** | |
| 126 * The errors produced while generating hints a compilation unit. | |
| 127 * | |
| 128 * The list will be empty if there were no errors, but will not be `null`. | |
| 129 * | |
| 130 * The result is only available for [LibrarySpecificUnit]s. | |
| 131 */ | |
| 132 final ListResultDescriptor<AnalysisError> HINTS = | |
| 133 new ListResultDescriptor<AnalysisError>( | |
| 134 'HINT_ERRORS', AnalysisError.NO_ERRORS); | |
| 135 | |
| 136 /** | |
| 137 * The sources representing the combined import/export closure of a library. | |
| 138 * The [Source]s include only library sources, not their units. | |
| 139 * | |
| 140 * The result is only available for [Source]s representing a library. | |
| 141 */ | |
| 142 final ListResultDescriptor<Source> IMPORT_EXPORT_SOURCE_CLOSURE = | |
| 143 new ListResultDescriptor<Source>('IMPORT_EXPORT_SOURCE_CLOSURE', null); | |
| 144 | |
| 145 /** | |
| 146 * The partial [LibraryElement] associated with a library. | |
| 147 * | |
| 148 * The [LibraryElement] and its [CompilationUnitElement]s are attached to each | |
| 149 * other. Directives 'library', 'part' and 'part of' are resolved. | |
| 150 * | |
| 151 * The result is only available for [Source]s representing a library. | |
| 152 */ | |
| 153 final ResultDescriptor<LibraryElement> LIBRARY_ELEMENT1 = | |
| 154 new ResultDescriptor<LibraryElement>('LIBRARY_ELEMENT1', null, | |
| 155 cachingPolicy: ELEMENT_CACHING_POLICY); | |
| 156 | |
| 157 /** | |
| 158 * The partial [LibraryElement] associated with a library. | |
| 159 * | |
| 160 * In addition to [LIBRARY_ELEMENT1] [LibraryElement.imports] and | |
| 161 * [LibraryElement.exports] are set. | |
| 162 * | |
| 163 * The result is only available for [Source]s representing a library. | |
| 164 */ | |
| 165 final ResultDescriptor<LibraryElement> LIBRARY_ELEMENT2 = | |
| 166 new ResultDescriptor<LibraryElement>('LIBRARY_ELEMENT2', null, | |
| 167 cachingPolicy: ELEMENT_CACHING_POLICY); | |
| 168 | |
| 169 /** | |
| 170 * The partial [LibraryElement] associated with a library. | |
| 171 * | |
| 172 * In addition to [LIBRARY_ELEMENT2] the [LibraryElement.publicNamespace] is set
. | |
| 173 * | |
| 174 * The result is only available for [Source]s representing a library. | |
| 175 */ | |
| 176 final ResultDescriptor<LibraryElement> LIBRARY_ELEMENT3 = | |
| 177 new ResultDescriptor<LibraryElement>('LIBRARY_ELEMENT3', null, | |
| 178 cachingPolicy: ELEMENT_CACHING_POLICY); | |
| 179 | |
| 180 /** | |
| 181 * The partial [LibraryElement] associated with a library. | |
| 182 * | |
| 183 * In addition to [LIBRARY_ELEMENT3] the [LibraryElement.entryPoint] is set, | |
| 184 * if the library does not declare one already and one of the exported | |
| 185 * libraries exports one. | |
| 186 * | |
| 187 * Also [LibraryElement.exportNamespace] is set. | |
| 188 * | |
| 189 * The result is only available for [Source]s representing a library. | |
| 190 */ | |
| 191 final ResultDescriptor<LibraryElement> LIBRARY_ELEMENT4 = | |
| 192 new ResultDescriptor<LibraryElement>('LIBRARY_ELEMENT4', null, | |
| 193 cachingPolicy: ELEMENT_CACHING_POLICY); | |
| 194 | |
| 195 /** | |
| 196 * The partial [LibraryElement] associated with a library. | |
| 197 * | |
| 198 * [LIBRARY_ELEMENT4] plus resolved types for every element. | |
| 199 * | |
| 200 * The result is only available for [Source]s representing a library. | |
| 201 */ | |
| 202 final ResultDescriptor<LibraryElement> LIBRARY_ELEMENT5 = | |
| 203 new ResultDescriptor<LibraryElement>('LIBRARY_ELEMENT5', null, | |
| 204 cachingPolicy: ELEMENT_CACHING_POLICY); | |
| 205 | |
| 206 /** | |
| 207 * The flag specifying whether all analysis errors are computed in a specific | |
| 208 * library. | |
| 209 * | |
| 210 * The result is only available for [Source]s representing a library. | |
| 211 */ | |
| 212 final ResultDescriptor<bool> LIBRARY_ERRORS_READY = | |
| 213 new ResultDescriptor<bool>('LIBRARY_ERRORS_READY', false); | |
| 214 | |
| 215 /** | |
| 216 * The analysis errors associated with a compilation unit in a specific library. | |
| 217 * | |
| 218 * The result is only available for [LibrarySpecificUnit]s. | |
| 219 */ | |
| 220 final ListResultDescriptor<AnalysisError> LIBRARY_UNIT_ERRORS = | |
| 221 new ListResultDescriptor<AnalysisError>( | |
| 222 'LIBRARY_UNIT_ERRORS', AnalysisError.NO_ERRORS); | |
| 223 | |
| 224 /** | |
| 225 * The errors produced while parsing a compilation unit. | |
| 226 * | |
| 227 * The list will be empty if there were no errors, but will not be `null`. | |
| 228 * | |
| 229 * The result is only available for [Source]s representing a compilation unit. | |
| 230 */ | |
| 231 final ListResultDescriptor<AnalysisError> PARSE_ERRORS = | |
| 232 new ListResultDescriptor<AnalysisError>( | |
| 233 'PARSE_ERRORS', AnalysisError.NO_ERRORS); | |
| 234 | |
| 235 /** | |
| 236 * The names (resolved and not) referenced by a unit. | |
| 237 * | |
| 238 * The result is only available for [Source]s representing a compilation unit. | |
| 239 */ | |
| 240 final ResultDescriptor<ReferencedNames> REFERENCED_NAMES = | |
| 241 new ResultDescriptor<ReferencedNames>('REFERENCED_NAMES', null); | |
| 242 | |
| 243 /** | |
| 244 * The errors produced while resolving references. | |
| 245 * | |
| 246 * The list will be empty if there were no errors, but will not be `null`. | |
| 247 * | |
| 248 * The result is only available for [LibrarySpecificUnit]s. | |
| 249 */ | |
| 250 final ListResultDescriptor<AnalysisError> RESOLVE_REFERENCES_ERRORS = | |
| 251 new ListResultDescriptor<AnalysisError>( | |
| 252 'RESOLVE_REFERENCES_ERRORS', AnalysisError.NO_ERRORS); | |
| 253 | |
| 254 /** | |
| 255 * The errors produced while resolving type names. | |
| 256 * | |
| 257 * The list will be empty if there were no errors, but will not be `null`. | |
| 258 * | |
| 259 * The result is only available for [LibrarySpecificUnit]s. | |
| 260 */ | |
| 261 final ListResultDescriptor<AnalysisError> RESOLVE_TYPE_NAMES_ERRORS = | |
| 262 new ListResultDescriptor<AnalysisError>( | |
| 263 'RESOLVE_TYPE_NAMES_ERRORS', AnalysisError.NO_ERRORS); | |
| 264 | |
| 265 /** | |
| 266 * The partially resolved [CompilationUnit] associated with a unit. | |
| 267 * | |
| 268 * All declarations bound to the element defined by the declaration. | |
| 269 * | |
| 270 * The result is only available for [LibrarySpecificUnit]s. | |
| 271 */ | |
| 272 final ResultDescriptor<CompilationUnit> RESOLVED_UNIT1 = | |
| 273 new ResultDescriptor<CompilationUnit>('RESOLVED_UNIT1', null, | |
| 274 cachingPolicy: AST_CACHING_POLICY); | |
| 275 | |
| 276 /** | |
| 277 * The partially resolved [CompilationUnit] associated with a unit. | |
| 278 * | |
| 279 * All the enum member elements are built. | |
| 280 * | |
| 281 * The result is only available for [LibrarySpecificUnit]s. | |
| 282 */ | |
| 283 final ResultDescriptor<CompilationUnit> RESOLVED_UNIT2 = | |
| 284 new ResultDescriptor<CompilationUnit>('RESOLVED_UNIT2', null, | |
| 285 cachingPolicy: AST_CACHING_POLICY); | |
| 286 | |
| 287 /** | |
| 288 * The partially resolved [CompilationUnit] associated with a unit. | |
| 289 * | |
| 290 * [RESOLVED_UNIT2] with resolved type names. | |
| 291 * | |
| 292 * The result is only available for [LibrarySpecificUnit]s. | |
| 293 */ | |
| 294 final ResultDescriptor<CompilationUnit> RESOLVED_UNIT3 = | |
| 295 new ResultDescriptor<CompilationUnit>('RESOLVED_UNIT3', null, | |
| 296 cachingPolicy: AST_CACHING_POLICY); | |
| 297 | |
| 298 /** | |
| 299 * The partially resolved [CompilationUnit] associated with a unit. | |
| 300 * | |
| 301 * [RESOLVED_UNIT3] plus resolved local variables and formal parameters. | |
| 302 * | |
| 303 * The result is only available for [LibrarySpecificUnit]s. | |
| 304 */ | |
| 305 final ResultDescriptor<CompilationUnit> RESOLVED_UNIT4 = | |
| 306 new ResultDescriptor<CompilationUnit>('RESOLVED_UNIT4', null, | |
| 307 cachingPolicy: AST_CACHING_POLICY); | |
| 308 | |
| 309 /** | |
| 310 * The resolved [CompilationUnit] associated with a compilation unit, with | |
| 311 * constants not yet resolved. | |
| 312 * | |
| 313 * The result is only available for [LibrarySpecificUnit]s. | |
| 314 */ | |
| 315 final ResultDescriptor<CompilationUnit> RESOLVED_UNIT5 = | |
| 316 new ResultDescriptor<CompilationUnit>('RESOLVED_UNIT5', null, | |
| 317 cachingPolicy: AST_CACHING_POLICY); | |
| 318 | |
| 319 /** | |
| 320 * The errors produced while scanning a compilation unit. | |
| 321 * | |
| 322 * The list will be empty if there were no errors, but will not be `null`. | |
| 323 * | |
| 324 * The result is only available for [Source]s representing a compilation unit. | |
| 325 */ | |
| 326 final ListResultDescriptor<AnalysisError> SCAN_ERRORS = | |
| 327 new ListResultDescriptor<AnalysisError>( | |
| 328 'SCAN_ERRORS', AnalysisError.NO_ERRORS); | |
| 329 | |
| 330 /** | |
| 331 * The [ResultCachingPolicy] for [TOKEN_STREAM]. | |
| 332 */ | |
| 333 const ResultCachingPolicy TOKEN_STREAM_CACHING_POLICY = | |
| 334 const SimpleResultCachingPolicy(1, 1); | |
| 335 | |
| 336 /** | |
| 337 * The [TypeProvider] of the [AnalysisContext]. | |
| 338 */ | |
| 339 final ResultDescriptor<TypeProvider> TYPE_PROVIDER = | |
| 340 new ResultDescriptor<TypeProvider>('TYPE_PROVIDER', null); | |
| 341 | |
| 342 /** | |
| 343 * The [UsedImportedElements] of a [LibrarySpecificUnit]. | |
| 344 */ | |
| 345 final ResultDescriptor<UsedImportedElements> USED_IMPORTED_ELEMENTS = | |
| 346 new ResultDescriptor<UsedImportedElements>('USED_IMPORTED_ELEMENTS', null, | |
| 347 cachingPolicy: ELEMENT_CACHING_POLICY); | |
| 348 | |
| 349 /** | |
| 350 * The [UsedLocalElements] of a [LibrarySpecificUnit]. | |
| 351 */ | |
| 352 final ResultDescriptor<UsedLocalElements> USED_LOCAL_ELEMENTS = | |
| 353 new ResultDescriptor<UsedLocalElements>('USED_LOCAL_ELEMENTS', null, | |
| 354 cachingPolicy: ELEMENT_CACHING_POLICY); | |
| 355 | |
| 356 /** | |
| 357 * The errors produced while resolving variable references in a compilation unit
. | |
| 358 * | |
| 359 * The list will be empty if there were no errors, but will not be `null`. | |
| 360 * | |
| 361 * The result is only available for [LibrarySpecificUnit]s. | |
| 362 */ | |
| 363 final ListResultDescriptor<AnalysisError> VARIABLE_REFERENCE_ERRORS = | |
| 364 new ListResultDescriptor<AnalysisError>( | |
| 365 'VARIABLE_REFERENCE_ERRORS', AnalysisError.NO_ERRORS); | |
| 366 | |
| 367 /** | |
| 368 * The errors produced while verifying a compilation unit. | |
| 369 * | |
| 370 * The list will be empty if there were no errors, but will not be `null`. | |
| 371 * | |
| 372 * The result is only available for [LibrarySpecificUnit]s. | |
| 373 */ | |
| 374 final ListResultDescriptor<AnalysisError> VERIFY_ERRORS = | |
| 375 new ListResultDescriptor<AnalysisError>( | |
| 376 'VERIFY_ERRORS', AnalysisError.NO_ERRORS); | |
| 377 | |
| 378 /** | |
| 379 * Return a list of errors containing the errors from the given [errors] list | |
| 380 * but with duplications removed. | |
| 381 */ | |
| 382 List<AnalysisError> removeDuplicateErrors(List<AnalysisError> errors) { | |
| 383 if (errors.isEmpty) { | |
| 384 return errors; | |
| 385 } | |
| 386 return errors.toSet().toList(); | |
| 387 } | |
| 388 | |
| 389 /** | |
| 390 * A task that builds a compilation unit element for a single compilation unit. | |
| 391 */ | |
| 392 class BuildCompilationUnitElementTask extends SourceBasedAnalysisTask { | |
| 393 /** | |
| 394 * The name of the input whose value is the AST for the compilation unit. | |
| 395 */ | |
| 396 static const String PARSED_UNIT_INPUT_NAME = 'PARSED_UNIT_INPUT_NAME'; | |
| 397 | |
| 398 /** | |
| 399 * The task descriptor describing this kind of task. | |
| 400 */ | |
| 401 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 402 'BuildCompilationUnitElementTask', createTask, buildInputs, | |
| 403 <ResultDescriptor>[ | |
| 404 COMPILATION_UNIT_ELEMENT, | |
| 405 RESOLVED_UNIT1, | |
| 406 COMPILATION_UNIT_CONSTANTS | |
| 407 ]); | |
| 408 | |
| 409 /** | |
| 410 * Initialize a newly created task to build a compilation unit element for | |
| 411 * the given [target] in the given [context]. | |
| 412 */ | |
| 413 BuildCompilationUnitElementTask( | |
| 414 InternalAnalysisContext context, AnalysisTarget target) | |
| 415 : super(context, target); | |
| 416 | |
| 417 @override | |
| 418 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 419 | |
| 420 @override | |
| 421 void internalPerform() { | |
| 422 // | |
| 423 // Prepare inputs. | |
| 424 // | |
| 425 LibrarySpecificUnit librarySpecificUnit = target; | |
| 426 Source source = getRequiredSource(); | |
| 427 CompilationUnit unit = getRequiredInput(PARSED_UNIT_INPUT_NAME); | |
| 428 // | |
| 429 // Build or reuse CompilationUnitElement. | |
| 430 // | |
| 431 unit = AstCloner.clone(unit); | |
| 432 AnalysisCache analysisCache = | |
| 433 (context as InternalAnalysisContext).analysisCache; | |
| 434 CompilationUnitElement element = | |
| 435 analysisCache.getValue(target, COMPILATION_UNIT_ELEMENT); | |
| 436 if (element == null) { | |
| 437 CompilationUnitBuilder builder = new CompilationUnitBuilder(); | |
| 438 element = builder.buildCompilationUnit( | |
| 439 source, unit, librarySpecificUnit.library); | |
| 440 } else { | |
| 441 new DeclarationResolver().resolve(unit, element); | |
| 442 } | |
| 443 // | |
| 444 // Prepare constants. | |
| 445 // | |
| 446 ConstantFinder constantFinder = | |
| 447 new ConstantFinder(context, source, librarySpecificUnit.library); | |
| 448 unit.accept(constantFinder); | |
| 449 List<ConstantEvaluationTarget> constants = | |
| 450 new List<ConstantEvaluationTarget>.from( | |
| 451 constantFinder.constantsToCompute); | |
| 452 // | |
| 453 // Record outputs. | |
| 454 // | |
| 455 outputs[COMPILATION_UNIT_ELEMENT] = element; | |
| 456 outputs[RESOLVED_UNIT1] = unit; | |
| 457 outputs[COMPILATION_UNIT_CONSTANTS] = constants; | |
| 458 } | |
| 459 | |
| 460 /** | |
| 461 * Return a map from the names of the inputs of this kind of task to the task | |
| 462 * input descriptors describing those inputs for a task with the given | |
| 463 * [target]. | |
| 464 */ | |
| 465 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 466 LibrarySpecificUnit unit = target; | |
| 467 return <String, TaskInput>{ | |
| 468 PARSED_UNIT_INPUT_NAME: PARSED_UNIT.of(unit.unit) | |
| 469 }; | |
| 470 } | |
| 471 | |
| 472 /** | |
| 473 * Create a [BuildCompilationUnitElementTask] based on the given [target] in | |
| 474 * the given [context]. | |
| 475 */ | |
| 476 static BuildCompilationUnitElementTask createTask( | |
| 477 AnalysisContext context, AnalysisTarget target) { | |
| 478 return new BuildCompilationUnitElementTask(context, target); | |
| 479 } | |
| 480 } | |
| 481 | |
| 482 /** | |
| 483 * A task that builds imports and export directive elements for a library. | |
| 484 */ | |
| 485 class BuildDirectiveElementsTask extends SourceBasedAnalysisTask { | |
| 486 /** | |
| 487 * The name of the input whose value is the defining [LIBRARY_ELEMENT1]. | |
| 488 */ | |
| 489 static const String LIBRARY_INPUT = 'LIBRARY_INPUT'; | |
| 490 | |
| 491 /** | |
| 492 * The name of the input for [RESOLVED_UNIT1] of a library unit. | |
| 493 */ | |
| 494 static const String UNIT_INPUT_NAME = 'UNIT_INPUT_NAME'; | |
| 495 | |
| 496 /** | |
| 497 * The input with a list of [LIBRARY_ELEMENT3]s of imported libraries. | |
| 498 */ | |
| 499 static const String IMPORTS_LIBRARY_ELEMENT_INPUT_NAME = | |
| 500 'IMPORTS_LIBRARY_ELEMENT1_INPUT_NAME'; | |
| 501 | |
| 502 /** | |
| 503 * The input with a list of [LIBRARY_ELEMENT3]s of exported libraries. | |
| 504 */ | |
| 505 static const String EXPORTS_LIBRARY_ELEMENT_INPUT_NAME = | |
| 506 'EXPORTS_LIBRARY_ELEMENT_INPUT_NAME'; | |
| 507 | |
| 508 /** | |
| 509 * The input with a list of [SOURCE_KIND]s of imported libraries. | |
| 510 */ | |
| 511 static const String IMPORTS_SOURCE_KIND_INPUT_NAME = | |
| 512 'IMPORTS_SOURCE_KIND_INPUT_NAME'; | |
| 513 | |
| 514 /** | |
| 515 * The input with a list of [SOURCE_KIND]s of exported libraries. | |
| 516 */ | |
| 517 static const String EXPORTS_SOURCE_KIND_INPUT_NAME = | |
| 518 'EXPORTS_SOURCE_KIND_INPUT_NAME'; | |
| 519 | |
| 520 /** | |
| 521 * The task descriptor describing this kind of task. | |
| 522 */ | |
| 523 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 524 'BuildDirectiveElementsTask', createTask, buildInputs, <ResultDescriptor>[ | |
| 525 LIBRARY_ELEMENT2, | |
| 526 BUILD_DIRECTIVES_ERRORS | |
| 527 ]); | |
| 528 | |
| 529 BuildDirectiveElementsTask( | |
| 530 InternalAnalysisContext context, AnalysisTarget target) | |
| 531 : super(context, target); | |
| 532 | |
| 533 @override | |
| 534 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 535 | |
| 536 @override | |
| 537 void internalPerform() { | |
| 538 List<AnalysisError> errors = <AnalysisError>[]; | |
| 539 // | |
| 540 // Prepare inputs. | |
| 541 // | |
| 542 LibraryElementImpl libraryElement = getRequiredInput(LIBRARY_INPUT); | |
| 543 CompilationUnit libraryUnit = getRequiredInput(UNIT_INPUT_NAME); | |
| 544 Map<Source, LibraryElement> importLibraryMap = | |
| 545 getRequiredInput(IMPORTS_LIBRARY_ELEMENT_INPUT_NAME); | |
| 546 Map<Source, LibraryElement> exportLibraryMap = | |
| 547 getRequiredInput(EXPORTS_LIBRARY_ELEMENT_INPUT_NAME); | |
| 548 Map<Source, SourceKind> importSourceKindMap = | |
| 549 getRequiredInput(IMPORTS_SOURCE_KIND_INPUT_NAME); | |
| 550 Map<Source, SourceKind> exportSourceKindMap = | |
| 551 getRequiredInput(EXPORTS_SOURCE_KIND_INPUT_NAME); | |
| 552 Source librarySource = libraryElement.source; | |
| 553 // | |
| 554 // Resolve directives. | |
| 555 // | |
| 556 HashMap<String, PrefixElementImpl> nameToPrefixMap = | |
| 557 new HashMap<String, PrefixElementImpl>(); | |
| 558 List<ImportElement> imports = <ImportElement>[]; | |
| 559 List<ExportElement> exports = <ExportElement>[]; | |
| 560 bool explicitlyImportsCore = false; | |
| 561 for (Directive directive in libraryUnit.directives) { | |
| 562 if (directive is ImportDirective) { | |
| 563 ImportDirective importDirective = directive; | |
| 564 String uriContent = importDirective.uriContent; | |
| 565 if (DartUriResolver.isDartExtUri(uriContent)) { | |
| 566 libraryElement.hasExtUri = true; | |
| 567 } | |
| 568 Source importedSource = importDirective.source; | |
| 569 if (importedSource != null && context.exists(importedSource)) { | |
| 570 // The imported source will be null if the URI in the import | |
| 571 // directive was invalid. | |
| 572 LibraryElement importedLibrary = importLibraryMap[importedSource]; | |
| 573 if (importedLibrary != null) { | |
| 574 if (importedLibrary.isDartCore) { | |
| 575 explicitlyImportsCore = true; | |
| 576 } | |
| 577 ImportElementImpl importElement = | |
| 578 new ImportElementImpl(directive.offset); | |
| 579 StringLiteral uriLiteral = importDirective.uri; | |
| 580 if (uriLiteral != null) { | |
| 581 importElement.uriOffset = uriLiteral.offset; | |
| 582 importElement.uriEnd = uriLiteral.end; | |
| 583 } | |
| 584 importElement.uri = uriContent; | |
| 585 importElement.deferred = importDirective.deferredKeyword != null; | |
| 586 importElement.combinators = _buildCombinators(importDirective); | |
| 587 importElement.importedLibrary = importedLibrary; | |
| 588 SimpleIdentifier prefixNode = directive.prefix; | |
| 589 if (prefixNode != null) { | |
| 590 importElement.prefixOffset = prefixNode.offset; | |
| 591 String prefixName = prefixNode.name; | |
| 592 PrefixElementImpl prefix = nameToPrefixMap[prefixName]; | |
| 593 if (prefix == null) { | |
| 594 prefix = new PrefixElementImpl.forNode(prefixNode); | |
| 595 nameToPrefixMap[prefixName] = prefix; | |
| 596 } | |
| 597 importElement.prefix = prefix; | |
| 598 prefixNode.staticElement = prefix; | |
| 599 } | |
| 600 directive.element = importElement; | |
| 601 imports.add(importElement); | |
| 602 if (importSourceKindMap[importedSource] != SourceKind.LIBRARY) { | |
| 603 ErrorCode errorCode = (importElement.isDeferred | |
| 604 ? StaticWarningCode.IMPORT_OF_NON_LIBRARY | |
| 605 : CompileTimeErrorCode.IMPORT_OF_NON_LIBRARY); | |
| 606 errors.add(new AnalysisError(importedSource, uriLiteral.offset, | |
| 607 uriLiteral.length, errorCode, [uriLiteral.toSource()])); | |
| 608 } | |
| 609 } | |
| 610 } | |
| 611 } else if (directive is ExportDirective) { | |
| 612 ExportDirective exportDirective = directive; | |
| 613 Source exportedSource = exportDirective.source; | |
| 614 if (exportedSource != null && context.exists(exportedSource)) { | |
| 615 // The exported source will be null if the URI in the export | |
| 616 // directive was invalid. | |
| 617 LibraryElement exportedLibrary = exportLibraryMap[exportedSource]; | |
| 618 if (exportedLibrary != null) { | |
| 619 ExportElementImpl exportElement = | |
| 620 new ExportElementImpl(directive.offset); | |
| 621 StringLiteral uriLiteral = exportDirective.uri; | |
| 622 if (uriLiteral != null) { | |
| 623 exportElement.uriOffset = uriLiteral.offset; | |
| 624 exportElement.uriEnd = uriLiteral.end; | |
| 625 } | |
| 626 exportElement.uri = exportDirective.uriContent; | |
| 627 exportElement.combinators = _buildCombinators(exportDirective); | |
| 628 exportElement.exportedLibrary = exportedLibrary; | |
| 629 directive.element = exportElement; | |
| 630 exports.add(exportElement); | |
| 631 if (exportSourceKindMap[exportedSource] != SourceKind.LIBRARY) { | |
| 632 errors.add(new AnalysisError(exportedSource, uriLiteral.offset, | |
| 633 uriLiteral.length, CompileTimeErrorCode.EXPORT_OF_NON_LIBRARY, | |
| 634 [uriLiteral.toSource()])); | |
| 635 } | |
| 636 } | |
| 637 } | |
| 638 } | |
| 639 } | |
| 640 // | |
| 641 // Ensure "dart:core" import. | |
| 642 // | |
| 643 Source coreLibrarySource = context.sourceFactory.forUri(DartSdk.DART_CORE); | |
| 644 if (!explicitlyImportsCore && coreLibrarySource != librarySource) { | |
| 645 ImportElementImpl importElement = new ImportElementImpl(-1); | |
| 646 importElement.importedLibrary = importLibraryMap[coreLibrarySource]; | |
| 647 importElement.synthetic = true; | |
| 648 imports.add(importElement); | |
| 649 } | |
| 650 // | |
| 651 // Populate the library element. | |
| 652 // | |
| 653 libraryElement.imports = imports; | |
| 654 libraryElement.exports = exports; | |
| 655 // | |
| 656 // Record outputs. | |
| 657 // | |
| 658 outputs[LIBRARY_ELEMENT2] = libraryElement; | |
| 659 outputs[BUILD_DIRECTIVES_ERRORS] = errors; | |
| 660 } | |
| 661 | |
| 662 /** | |
| 663 * Return a map from the names of the inputs of this kind of task to the task | |
| 664 * input descriptors describing those inputs for a task with the | |
| 665 * given library [libSource]. | |
| 666 */ | |
| 667 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 668 Source source = target; | |
| 669 return <String, TaskInput>{ | |
| 670 LIBRARY_INPUT: LIBRARY_ELEMENT1.of(source), | |
| 671 UNIT_INPUT_NAME: | |
| 672 RESOLVED_UNIT1.of(new LibrarySpecificUnit(source, source)), | |
| 673 IMPORTS_LIBRARY_ELEMENT_INPUT_NAME: | |
| 674 IMPORTED_LIBRARIES.of(source).toMapOf(LIBRARY_ELEMENT1), | |
| 675 EXPORTS_LIBRARY_ELEMENT_INPUT_NAME: | |
| 676 EXPORTED_LIBRARIES.of(source).toMapOf(LIBRARY_ELEMENT1), | |
| 677 IMPORTS_SOURCE_KIND_INPUT_NAME: | |
| 678 IMPORTED_LIBRARIES.of(source).toMapOf(SOURCE_KIND), | |
| 679 EXPORTS_SOURCE_KIND_INPUT_NAME: | |
| 680 EXPORTED_LIBRARIES.of(source).toMapOf(SOURCE_KIND) | |
| 681 }; | |
| 682 } | |
| 683 | |
| 684 /** | |
| 685 * Create a [BuildDirectiveElementsTask] based on the given [target] in | |
| 686 * the given [context]. | |
| 687 */ | |
| 688 static BuildDirectiveElementsTask createTask( | |
| 689 AnalysisContext context, AnalysisTarget target) { | |
| 690 return new BuildDirectiveElementsTask(context, target); | |
| 691 } | |
| 692 | |
| 693 /** | |
| 694 * Build the element model representing the combinators declared by | |
| 695 * the given [directive]. | |
| 696 */ | |
| 697 static List<NamespaceCombinator> _buildCombinators( | |
| 698 NamespaceDirective directive) { | |
| 699 List<NamespaceCombinator> combinators = <NamespaceCombinator>[]; | |
| 700 for (Combinator combinator in directive.combinators) { | |
| 701 if (combinator is ShowCombinator) { | |
| 702 ShowElementCombinatorImpl show = new ShowElementCombinatorImpl(); | |
| 703 show.offset = combinator.offset; | |
| 704 show.end = combinator.end; | |
| 705 show.shownNames = _getIdentifiers(combinator.shownNames); | |
| 706 combinators.add(show); | |
| 707 } else if (combinator is HideCombinator) { | |
| 708 HideElementCombinatorImpl hide = new HideElementCombinatorImpl(); | |
| 709 hide.hiddenNames = _getIdentifiers(combinator.hiddenNames); | |
| 710 combinators.add(hide); | |
| 711 } | |
| 712 } | |
| 713 return combinators; | |
| 714 } | |
| 715 | |
| 716 /** | |
| 717 * Return the lexical identifiers associated with the given [identifiers]. | |
| 718 */ | |
| 719 static List<String> _getIdentifiers(NodeList<SimpleIdentifier> identifiers) { | |
| 720 return identifiers.map((identifier) => identifier.name).toList(); | |
| 721 } | |
| 722 } | |
| 723 | |
| 724 /** | |
| 725 * A task that builds the elements representing the members of enum | |
| 726 * declarations. | |
| 727 */ | |
| 728 class BuildEnumMemberElementsTask extends SourceBasedAnalysisTask { | |
| 729 /** | |
| 730 * The name of the [TYPE_PROVIDER] input. | |
| 731 */ | |
| 732 static const String TYPE_PROVIDER_INPUT = 'TYPE_PROVIDER_INPUT'; | |
| 733 | |
| 734 /** | |
| 735 * The name of the [RESOLVED_UNIT1] input. | |
| 736 */ | |
| 737 static const String UNIT_INPUT = 'UNIT_INPUT'; | |
| 738 | |
| 739 /** | |
| 740 * The task descriptor describing this kind of task. | |
| 741 */ | |
| 742 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 743 'BuildEnumMemberElementsTask', createTask, buildInputs, | |
| 744 <ResultDescriptor>[RESOLVED_UNIT2]); | |
| 745 | |
| 746 BuildEnumMemberElementsTask( | |
| 747 InternalAnalysisContext context, AnalysisTarget target) | |
| 748 : super(context, target); | |
| 749 | |
| 750 @override | |
| 751 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 752 | |
| 753 @override | |
| 754 void internalPerform() { | |
| 755 // | |
| 756 // Prepare inputs. | |
| 757 // | |
| 758 TypeProvider typeProvider = getRequiredInput(TYPE_PROVIDER_INPUT); | |
| 759 CompilationUnit unit = getRequiredInput(UNIT_INPUT); | |
| 760 // | |
| 761 // Record outputs. | |
| 762 // | |
| 763 EnumMemberBuilder builder = new EnumMemberBuilder(typeProvider); | |
| 764 unit.accept(builder); | |
| 765 outputs[RESOLVED_UNIT2] = unit; | |
| 766 } | |
| 767 | |
| 768 /** | |
| 769 * Return a map from the names of the inputs of this kind of task to the task | |
| 770 * input descriptors describing those inputs for a task with the | |
| 771 * given [target]. | |
| 772 */ | |
| 773 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 774 LibrarySpecificUnit unit = target; | |
| 775 return <String, TaskInput>{ | |
| 776 TYPE_PROVIDER_INPUT: TYPE_PROVIDER.of(AnalysisContextTarget.request), | |
| 777 UNIT_INPUT: RESOLVED_UNIT1.of(unit) | |
| 778 }; | |
| 779 } | |
| 780 | |
| 781 /** | |
| 782 * Create a [BuildEnumMemberElementsTask] based on the given [target] in | |
| 783 * the given [context]. | |
| 784 */ | |
| 785 static BuildEnumMemberElementsTask createTask( | |
| 786 AnalysisContext context, AnalysisTarget target) { | |
| 787 return new BuildEnumMemberElementsTask(context, target); | |
| 788 } | |
| 789 } | |
| 790 | |
| 791 /** | |
| 792 * A task that builds [EXPORT_NAMESPACE] and [LIBRARY_ELEMENT4] for a library. | |
| 793 */ | |
| 794 class BuildExportNamespaceTask extends SourceBasedAnalysisTask { | |
| 795 /** | |
| 796 * The name of the input for [LIBRARY_ELEMENT3] of a library. | |
| 797 */ | |
| 798 static const String LIBRARY_INPUT = 'LIBRARY_INPUT'; | |
| 799 | |
| 800 /** | |
| 801 * The task descriptor describing this kind of task. | |
| 802 */ | |
| 803 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 804 'BuildExportNamespaceTask', createTask, buildInputs, | |
| 805 <ResultDescriptor>[LIBRARY_ELEMENT4]); | |
| 806 | |
| 807 BuildExportNamespaceTask( | |
| 808 InternalAnalysisContext context, AnalysisTarget target) | |
| 809 : super(context, target); | |
| 810 | |
| 811 @override | |
| 812 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 813 | |
| 814 @override | |
| 815 void internalPerform() { | |
| 816 LibraryElementImpl library = getRequiredInput(LIBRARY_INPUT); | |
| 817 // | |
| 818 // Compute export namespace. | |
| 819 // | |
| 820 ExportNamespaceBuilder builder = new ExportNamespaceBuilder(); | |
| 821 Namespace namespace = builder.build(library); | |
| 822 library.exportNamespace = namespace; | |
| 823 // | |
| 824 // Update entry point. | |
| 825 // | |
| 826 if (library.entryPoint == null) { | |
| 827 Iterable<Element> exportedElements = namespace.definedNames.values; | |
| 828 library.entryPoint = exportedElements.firstWhere( | |
| 829 (element) => element is FunctionElement && element.isEntryPoint, | |
| 830 orElse: () => null); | |
| 831 } | |
| 832 // | |
| 833 // Record outputs. | |
| 834 // | |
| 835 outputs[LIBRARY_ELEMENT4] = library; | |
| 836 } | |
| 837 | |
| 838 /** | |
| 839 * Return a map from the names of the inputs of this kind of task to the task | |
| 840 * input descriptors describing those inputs for a task with the | |
| 841 * given library [libSource]. | |
| 842 */ | |
| 843 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 844 Source source = target; | |
| 845 return <String, TaskInput>{ | |
| 846 LIBRARY_INPUT: LIBRARY_ELEMENT3.of(source), | |
| 847 'exportsLibraryPublicNamespace': | |
| 848 EXPORT_SOURCE_CLOSURE.of(source).toMapOf(LIBRARY_ELEMENT3) | |
| 849 }; | |
| 850 } | |
| 851 | |
| 852 /** | |
| 853 * Create a [BuildExportNamespaceTask] based on the given [target] in | |
| 854 * the given [context]. | |
| 855 */ | |
| 856 static BuildExportNamespaceTask createTask( | |
| 857 AnalysisContext context, AnalysisTarget target) { | |
| 858 return new BuildExportNamespaceTask(context, target); | |
| 859 } | |
| 860 } | |
| 861 | |
| 862 /** | |
| 863 * A task that builds a library element for a Dart library. | |
| 864 */ | |
| 865 class BuildLibraryElementTask extends SourceBasedAnalysisTask { | |
| 866 /** | |
| 867 * The name of the input whose value is the defining [RESOLVED_UNIT1]. | |
| 868 */ | |
| 869 static const String DEFINING_UNIT_INPUT = 'DEFINING_UNIT_INPUT'; | |
| 870 | |
| 871 /** | |
| 872 * The name of the input whose value is a list of built [RESOLVED_UNIT1]s | |
| 873 * of the parts sourced by a library. | |
| 874 */ | |
| 875 static const String PARTS_UNIT_INPUT = 'PARTS_UNIT_INPUT'; | |
| 876 | |
| 877 /** | |
| 878 * The task descriptor describing this kind of task. | |
| 879 */ | |
| 880 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 881 'BuildLibraryElementTask', createTask, buildInputs, <ResultDescriptor>[ | |
| 882 BUILD_LIBRARY_ERRORS, | |
| 883 LIBRARY_ELEMENT1, | |
| 884 IS_LAUNCHABLE | |
| 885 ]); | |
| 886 | |
| 887 /** | |
| 888 * The constant used as an unknown common library name in parts. | |
| 889 */ | |
| 890 static const String _UNKNOWN_LIBRARY_NAME = 'unknown-library-name'; | |
| 891 | |
| 892 /** | |
| 893 * Initialize a newly created task to build a library element for the given | |
| 894 * [target] in the given [context]. | |
| 895 */ | |
| 896 BuildLibraryElementTask( | |
| 897 InternalAnalysisContext context, AnalysisTarget target) | |
| 898 : super(context, target); | |
| 899 | |
| 900 @override | |
| 901 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 902 | |
| 903 @override | |
| 904 void internalPerform() { | |
| 905 List<AnalysisError> errors = <AnalysisError>[]; | |
| 906 // | |
| 907 // Prepare inputs. | |
| 908 // | |
| 909 Source librarySource = getRequiredSource(); | |
| 910 CompilationUnit definingCompilationUnit = | |
| 911 getRequiredInput(DEFINING_UNIT_INPUT); | |
| 912 List<CompilationUnit> partUnits = getRequiredInput(PARTS_UNIT_INPUT); | |
| 913 // | |
| 914 // Process inputs. | |
| 915 // | |
| 916 CompilationUnitElementImpl definingCompilationUnitElement = | |
| 917 definingCompilationUnit.element; | |
| 918 Map<Source, CompilationUnit> partUnitMap = | |
| 919 new HashMap<Source, CompilationUnit>(); | |
| 920 for (CompilationUnit partUnit in partUnits) { | |
| 921 Source partSource = partUnit.element.source; | |
| 922 partUnitMap[partSource] = partUnit; | |
| 923 } | |
| 924 // | |
| 925 // Update "part" directives. | |
| 926 // | |
| 927 LibraryIdentifier libraryNameNode = null; | |
| 928 String partsLibraryName = _UNKNOWN_LIBRARY_NAME; | |
| 929 bool hasPartDirective = false; | |
| 930 FunctionElement entryPoint = | |
| 931 _findEntryPoint(definingCompilationUnitElement); | |
| 932 List<Directive> directivesToResolve = <Directive>[]; | |
| 933 List<CompilationUnitElementImpl> sourcedCompilationUnits = | |
| 934 <CompilationUnitElementImpl>[]; | |
| 935 for (Directive directive in definingCompilationUnit.directives) { | |
| 936 if (directive is LibraryDirective) { | |
| 937 if (libraryNameNode == null) { | |
| 938 libraryNameNode = directive.name; | |
| 939 directivesToResolve.add(directive); | |
| 940 } | |
| 941 } else if (directive is PartDirective) { | |
| 942 PartDirective partDirective = directive; | |
| 943 StringLiteral partUri = partDirective.uri; | |
| 944 Source partSource = partDirective.source; | |
| 945 hasPartDirective = true; | |
| 946 CompilationUnit partUnit = partUnitMap[partSource]; | |
| 947 if (partUnit != null) { | |
| 948 CompilationUnitElementImpl partElement = partUnit.element; | |
| 949 partElement.uriOffset = partUri.offset; | |
| 950 partElement.uriEnd = partUri.end; | |
| 951 partElement.uri = partDirective.uriContent; | |
| 952 // | |
| 953 // Validate that the part contains a part-of directive with the same | |
| 954 // name as the library. | |
| 955 // | |
| 956 if (context.exists(partSource)) { | |
| 957 String partLibraryName = | |
| 958 _getPartLibraryName(partSource, partUnit, directivesToResolve); | |
| 959 if (partLibraryName == null) { | |
| 960 errors.add(new AnalysisError(librarySource, partUri.offset, | |
| 961 partUri.length, CompileTimeErrorCode.PART_OF_NON_PART, | |
| 962 [partUri.toSource()])); | |
| 963 } else if (libraryNameNode == null) { | |
| 964 if (partsLibraryName == _UNKNOWN_LIBRARY_NAME) { | |
| 965 partsLibraryName = partLibraryName; | |
| 966 } else if (partsLibraryName != partLibraryName) { | |
| 967 partsLibraryName = null; | |
| 968 } | |
| 969 } else if (libraryNameNode.name != partLibraryName) { | |
| 970 errors.add(new AnalysisError(librarySource, partUri.offset, | |
| 971 partUri.length, StaticWarningCode.PART_OF_DIFFERENT_LIBRARY, [ | |
| 972 libraryNameNode.name, | |
| 973 partLibraryName | |
| 974 ])); | |
| 975 } | |
| 976 } | |
| 977 if (entryPoint == null) { | |
| 978 entryPoint = _findEntryPoint(partElement); | |
| 979 } | |
| 980 directive.element = partElement; | |
| 981 sourcedCompilationUnits.add(partElement); | |
| 982 } | |
| 983 } | |
| 984 } | |
| 985 if (hasPartDirective && libraryNameNode == null) { | |
| 986 AnalysisError error; | |
| 987 if (partsLibraryName != _UNKNOWN_LIBRARY_NAME && | |
| 988 partsLibraryName != null) { | |
| 989 error = new AnalysisErrorWithProperties(librarySource, 0, 0, | |
| 990 ResolverErrorCode.MISSING_LIBRARY_DIRECTIVE_WITH_PART) | |
| 991 ..setProperty(ErrorProperty.PARTS_LIBRARY_NAME, partsLibraryName); | |
| 992 } else { | |
| 993 error = new AnalysisError(librarySource, 0, 0, | |
| 994 ResolverErrorCode.MISSING_LIBRARY_DIRECTIVE_WITH_PART); | |
| 995 } | |
| 996 errors.add(error); | |
| 997 } | |
| 998 // | |
| 999 // Create and populate the library element. | |
| 1000 // | |
| 1001 AnalysisContext owningContext = context; | |
| 1002 if (context is InternalAnalysisContext) { | |
| 1003 InternalAnalysisContext internalContext = context; | |
| 1004 owningContext = internalContext.getContextFor(librarySource); | |
| 1005 } | |
| 1006 LibraryElementImpl libraryElement = | |
| 1007 new LibraryElementImpl.forNode(owningContext, libraryNameNode); | |
| 1008 libraryElement.definingCompilationUnit = definingCompilationUnitElement; | |
| 1009 libraryElement.entryPoint = entryPoint; | |
| 1010 libraryElement.parts = sourcedCompilationUnits; | |
| 1011 for (Directive directive in directivesToResolve) { | |
| 1012 directive.element = libraryElement; | |
| 1013 } | |
| 1014 if (sourcedCompilationUnits.isNotEmpty) { | |
| 1015 _patchTopLevelAccessors(libraryElement); | |
| 1016 } | |
| 1017 // | |
| 1018 // Record outputs. | |
| 1019 // | |
| 1020 outputs[BUILD_LIBRARY_ERRORS] = errors; | |
| 1021 outputs[LIBRARY_ELEMENT1] = libraryElement; | |
| 1022 outputs[IS_LAUNCHABLE] = entryPoint != null; | |
| 1023 } | |
| 1024 | |
| 1025 /** | |
| 1026 * Add all of the non-synthetic [getters] and [setters] defined in the given | |
| 1027 * [unit] that have no corresponding accessor to one of the given collections. | |
| 1028 */ | |
| 1029 void _collectAccessors(Map<String, PropertyAccessorElement> getters, | |
| 1030 List<PropertyAccessorElement> setters, CompilationUnitElement unit) { | |
| 1031 for (PropertyAccessorElement accessor in unit.accessors) { | |
| 1032 if (accessor.isGetter) { | |
| 1033 if (!accessor.isSynthetic && accessor.correspondingSetter == null) { | |
| 1034 getters[accessor.displayName] = accessor; | |
| 1035 } | |
| 1036 } else { | |
| 1037 if (!accessor.isSynthetic && accessor.correspondingGetter == null) { | |
| 1038 setters.add(accessor); | |
| 1039 } | |
| 1040 } | |
| 1041 } | |
| 1042 } | |
| 1043 | |
| 1044 /** | |
| 1045 * Return the top-level [FunctionElement] entry point, or `null` if the given | |
| 1046 * [element] does not define an entry point. | |
| 1047 */ | |
| 1048 FunctionElement _findEntryPoint(CompilationUnitElementImpl element) { | |
| 1049 for (FunctionElement function in element.functions) { | |
| 1050 if (function.isEntryPoint) { | |
| 1051 return function; | |
| 1052 } | |
| 1053 } | |
| 1054 return null; | |
| 1055 } | |
| 1056 | |
| 1057 /** | |
| 1058 * Return the name of the library that the given part is declared to be a | |
| 1059 * part of, or `null` if the part does not contain a part-of directive. | |
| 1060 */ | |
| 1061 String _getPartLibraryName(Source partSource, CompilationUnit partUnit, | |
| 1062 List<Directive> directivesToResolve) { | |
| 1063 for (Directive directive in partUnit.directives) { | |
| 1064 if (directive is PartOfDirective) { | |
| 1065 directivesToResolve.add(directive); | |
| 1066 LibraryIdentifier libraryName = directive.libraryName; | |
| 1067 if (libraryName != null) { | |
| 1068 return libraryName.name; | |
| 1069 } | |
| 1070 } | |
| 1071 } | |
| 1072 return null; | |
| 1073 } | |
| 1074 | |
| 1075 /** | |
| 1076 * Look through all of the compilation units defined for the given [library], | |
| 1077 * looking for getters and setters that are defined in different compilation | |
| 1078 * units but that have the same names. If any are found, make sure that they | |
| 1079 * have the same variable element. | |
| 1080 */ | |
| 1081 void _patchTopLevelAccessors(LibraryElementImpl library) { | |
| 1082 HashMap<String, PropertyAccessorElement> getters = | |
| 1083 new HashMap<String, PropertyAccessorElement>(); | |
| 1084 List<PropertyAccessorElement> setters = <PropertyAccessorElement>[]; | |
| 1085 _collectAccessors(getters, setters, library.definingCompilationUnit); | |
| 1086 for (CompilationUnitElement unit in library.parts) { | |
| 1087 _collectAccessors(getters, setters, unit); | |
| 1088 } | |
| 1089 for (PropertyAccessorElement setter in setters) { | |
| 1090 PropertyAccessorElement getter = getters[setter.displayName]; | |
| 1091 if (getter != null) { | |
| 1092 TopLevelVariableElementImpl variable = getter.variable; | |
| 1093 TopLevelVariableElementImpl setterVariable = setter.variable; | |
| 1094 CompilationUnitElementImpl setterUnit = setterVariable.enclosingElement; | |
| 1095 setterUnit.replaceTopLevelVariable(setterVariable, variable); | |
| 1096 variable.setter = setter; | |
| 1097 (setter as PropertyAccessorElementImpl).variable = variable; | |
| 1098 } | |
| 1099 } | |
| 1100 } | |
| 1101 | |
| 1102 /** | |
| 1103 * Return a map from the names of the inputs of this kind of task to the task | |
| 1104 * input descriptors describing those inputs for a task with the given | |
| 1105 * [libSource]. | |
| 1106 */ | |
| 1107 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 1108 Source source = target; | |
| 1109 return <String, TaskInput>{ | |
| 1110 DEFINING_UNIT_INPUT: | |
| 1111 RESOLVED_UNIT1.of(new LibrarySpecificUnit(source, source)), | |
| 1112 PARTS_UNIT_INPUT: INCLUDED_PARTS.of(source).toList((Source unit) { | |
| 1113 return RESOLVED_UNIT1.of(new LibrarySpecificUnit(source, unit)); | |
| 1114 }) | |
| 1115 }; | |
| 1116 } | |
| 1117 | |
| 1118 /** | |
| 1119 * Create a [BuildLibraryElementTask] based on the given [target] in the | |
| 1120 * given [context]. | |
| 1121 */ | |
| 1122 static BuildLibraryElementTask createTask( | |
| 1123 AnalysisContext context, AnalysisTarget target) { | |
| 1124 return new BuildLibraryElementTask(context, target); | |
| 1125 } | |
| 1126 } | |
| 1127 | |
| 1128 /** | |
| 1129 * A task that builds [PUBLIC_NAMESPACE] for a library. | |
| 1130 */ | |
| 1131 class BuildPublicNamespaceTask extends SourceBasedAnalysisTask { | |
| 1132 /** | |
| 1133 * The name of the input for [LIBRARY_ELEMENT2] of a library. | |
| 1134 */ | |
| 1135 static const String LIBRARY_INPUT = 'LIBRARY_INPUT'; | |
| 1136 | |
| 1137 /** | |
| 1138 * The task descriptor describing this kind of task. | |
| 1139 */ | |
| 1140 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 1141 'BuildPublicNamespaceTask', createTask, buildInputs, | |
| 1142 <ResultDescriptor>[LIBRARY_ELEMENT3]); | |
| 1143 | |
| 1144 BuildPublicNamespaceTask( | |
| 1145 InternalAnalysisContext context, AnalysisTarget target) | |
| 1146 : super(context, target); | |
| 1147 | |
| 1148 @override | |
| 1149 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 1150 | |
| 1151 @override | |
| 1152 void internalPerform() { | |
| 1153 LibraryElementImpl library = getRequiredInput(LIBRARY_INPUT); | |
| 1154 library.publicNamespace = new PublicNamespaceBuilder().build(library); | |
| 1155 outputs[LIBRARY_ELEMENT3] = library; | |
| 1156 } | |
| 1157 | |
| 1158 /** | |
| 1159 * Return a map from the names of the inputs of this kind of task to the task | |
| 1160 * input descriptors describing those inputs for a task with the | |
| 1161 * given library [libSource]. | |
| 1162 */ | |
| 1163 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 1164 Source source = target; | |
| 1165 return <String, TaskInput>{LIBRARY_INPUT: LIBRARY_ELEMENT2.of(source)}; | |
| 1166 } | |
| 1167 | |
| 1168 /** | |
| 1169 * Create a [BuildPublicNamespaceTask] based on the given [target] in | |
| 1170 * the given [context]. | |
| 1171 */ | |
| 1172 static BuildPublicNamespaceTask createTask( | |
| 1173 AnalysisContext context, AnalysisTarget target) { | |
| 1174 return new BuildPublicNamespaceTask(context, target); | |
| 1175 } | |
| 1176 } | |
| 1177 | |
| 1178 /** | |
| 1179 * A task that builds [EXPORT_SOURCE_CLOSURE] of a library. | |
| 1180 */ | |
| 1181 class BuildSourceExportClosureTask extends SourceBasedAnalysisTask { | |
| 1182 /** | |
| 1183 * The name of the export closure. | |
| 1184 */ | |
| 1185 static const String EXPORT_INPUT = 'EXPORT_INPUT'; | |
| 1186 | |
| 1187 /** | |
| 1188 * The task descriptor describing this kind of task. | |
| 1189 */ | |
| 1190 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 1191 'BuildSourceExportClosureTask', createTask, buildInputs, | |
| 1192 <ResultDescriptor>[EXPORT_SOURCE_CLOSURE]); | |
| 1193 | |
| 1194 BuildSourceExportClosureTask( | |
| 1195 InternalAnalysisContext context, AnalysisTarget target) | |
| 1196 : super(context, target); | |
| 1197 | |
| 1198 @override | |
| 1199 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 1200 | |
| 1201 @override | |
| 1202 void internalPerform() { | |
| 1203 List<Source> exportClosure = getRequiredInput(EXPORT_INPUT); | |
| 1204 // | |
| 1205 // Record output. | |
| 1206 // | |
| 1207 outputs[EXPORT_SOURCE_CLOSURE] = exportClosure; | |
| 1208 } | |
| 1209 | |
| 1210 /** | |
| 1211 * Return a map from the names of the inputs of this kind of task to the task | |
| 1212 * input descriptors describing those inputs for a task with the | |
| 1213 * given library [libSource]. | |
| 1214 */ | |
| 1215 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 1216 Source source = target; | |
| 1217 return <String, TaskInput>{ | |
| 1218 EXPORT_INPUT: new _ExportSourceClosureTaskInput(source, LIBRARY_ELEMENT2) | |
| 1219 }; | |
| 1220 } | |
| 1221 | |
| 1222 /** | |
| 1223 * Create a [BuildSourceExportClosureTask] based on the given [target] in | |
| 1224 * the given [context]. | |
| 1225 */ | |
| 1226 static BuildSourceExportClosureTask createTask( | |
| 1227 AnalysisContext context, AnalysisTarget target) { | |
| 1228 return new BuildSourceExportClosureTask(context, target); | |
| 1229 } | |
| 1230 } | |
| 1231 | |
| 1232 /** | |
| 1233 * A task that builds [IMPORT_EXPORT_SOURCE_CLOSURE] of a library, and also | |
| 1234 * sets [IS_CLIENT]. | |
| 1235 */ | |
| 1236 class BuildSourceImportExportClosureTask extends SourceBasedAnalysisTask { | |
| 1237 /** | |
| 1238 * The name of the import/export closure. | |
| 1239 */ | |
| 1240 static const String IMPORT_EXPORT_INPUT = 'IMPORT_EXPORT_INPUT'; | |
| 1241 | |
| 1242 /** | |
| 1243 * The task descriptor describing this kind of task. | |
| 1244 */ | |
| 1245 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 1246 'BuildSourceImportExportClosureTask', createTask, buildInputs, | |
| 1247 <ResultDescriptor>[IMPORT_EXPORT_SOURCE_CLOSURE, IS_CLIENT]); | |
| 1248 | |
| 1249 BuildSourceImportExportClosureTask( | |
| 1250 InternalAnalysisContext context, AnalysisTarget target) | |
| 1251 : super(context, target); | |
| 1252 | |
| 1253 @override | |
| 1254 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 1255 | |
| 1256 @override | |
| 1257 void internalPerform() { | |
| 1258 List<Source> importExportClosure = getRequiredInput(IMPORT_EXPORT_INPUT); | |
| 1259 Source htmlSource = context.sourceFactory.forUri(DartSdk.DART_HTML); | |
| 1260 // | |
| 1261 // Record outputs. | |
| 1262 // | |
| 1263 outputs[IMPORT_EXPORT_SOURCE_CLOSURE] = importExportClosure; | |
| 1264 outputs[IS_CLIENT] = importExportClosure.contains(htmlSource); | |
| 1265 } | |
| 1266 | |
| 1267 /** | |
| 1268 * Return a map from the names of the inputs of this kind of task to the task | |
| 1269 * input descriptors describing those inputs for a task with the | |
| 1270 * given library [libSource]. | |
| 1271 */ | |
| 1272 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 1273 Source source = target; | |
| 1274 return <String, TaskInput>{ | |
| 1275 IMPORT_EXPORT_INPUT: | |
| 1276 new _ImportExportSourceClosureTaskInput(source, LIBRARY_ELEMENT2) | |
| 1277 }; | |
| 1278 } | |
| 1279 | |
| 1280 /** | |
| 1281 * Create a [BuildSourceImportExportClosureTask] based on the given [target] | |
| 1282 * in the given [context]. | |
| 1283 */ | |
| 1284 static BuildSourceImportExportClosureTask createTask( | |
| 1285 AnalysisContext context, AnalysisTarget target) { | |
| 1286 return new BuildSourceImportExportClosureTask(context, target); | |
| 1287 } | |
| 1288 } | |
| 1289 | |
| 1290 /** | |
| 1291 * A task that builds [TYPE_PROVIDER] for a context. | |
| 1292 */ | |
| 1293 class BuildTypeProviderTask extends SourceBasedAnalysisTask { | |
| 1294 /** | |
| 1295 * The [PUBLIC_NAMESPACE] input of the `dart:core` library. | |
| 1296 */ | |
| 1297 static const String CORE_INPUT = 'CORE_INPUT'; | |
| 1298 | |
| 1299 /** | |
| 1300 * The [PUBLIC_NAMESPACE] input of the `dart:async` library. | |
| 1301 */ | |
| 1302 static const String ASYNC_INPUT = 'ASYNC_INPUT'; | |
| 1303 | |
| 1304 /** | |
| 1305 * The task descriptor describing this kind of task. | |
| 1306 */ | |
| 1307 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 1308 'BuildTypeProviderTask', createTask, buildInputs, | |
| 1309 <ResultDescriptor>[TYPE_PROVIDER]); | |
| 1310 | |
| 1311 BuildTypeProviderTask( | |
| 1312 InternalAnalysisContext context, AnalysisContextTarget target) | |
| 1313 : super(context, target); | |
| 1314 | |
| 1315 @override | |
| 1316 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 1317 | |
| 1318 @override | |
| 1319 void internalPerform() { | |
| 1320 LibraryElement coreLibrary = getRequiredInput(CORE_INPUT); | |
| 1321 LibraryElement asyncLibrary = getRequiredInput(ASYNC_INPUT); | |
| 1322 Namespace coreNamespace = coreLibrary.publicNamespace; | |
| 1323 Namespace asyncNamespace = asyncLibrary.publicNamespace; | |
| 1324 // | |
| 1325 // Record outputs. | |
| 1326 // | |
| 1327 TypeProvider typeProvider = | |
| 1328 new TypeProviderImpl.forNamespaces(coreNamespace, asyncNamespace); | |
| 1329 (context as InternalAnalysisContext).typeProvider = typeProvider; | |
| 1330 outputs[TYPE_PROVIDER] = typeProvider; | |
| 1331 } | |
| 1332 | |
| 1333 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 1334 AnalysisContextTarget contextTarget = target; | |
| 1335 SourceFactory sourceFactory = contextTarget.context.sourceFactory; | |
| 1336 Source coreSource = sourceFactory.forUri(DartSdk.DART_CORE); | |
| 1337 Source asyncSource = sourceFactory.forUri(DartSdk.DART_ASYNC); | |
| 1338 return <String, TaskInput>{ | |
| 1339 CORE_INPUT: LIBRARY_ELEMENT3.of(coreSource), | |
| 1340 ASYNC_INPUT: LIBRARY_ELEMENT3.of(asyncSource) | |
| 1341 }; | |
| 1342 } | |
| 1343 | |
| 1344 /** | |
| 1345 * Create a [BuildTypeProviderTask] based on the given [context]. | |
| 1346 */ | |
| 1347 static BuildTypeProviderTask createTask( | |
| 1348 AnalysisContext context, AnalysisTarget target) { | |
| 1349 return new BuildTypeProviderTask(context, target); | |
| 1350 } | |
| 1351 } | |
| 1352 | |
| 1353 /** | |
| 1354 * A task that computes [CONSTANT_DEPENDENCIES] for a constant. | |
| 1355 */ | |
| 1356 class ComputeConstantDependenciesTask extends ConstantEvaluationAnalysisTask { | |
| 1357 /** | |
| 1358 * The name of the [RESOLVED_UNIT5] input. | |
| 1359 */ | |
| 1360 static const String UNIT_INPUT = 'UNIT_INPUT'; | |
| 1361 | |
| 1362 /** | |
| 1363 * The name of the [TYPE_PROVIDER] input. | |
| 1364 */ | |
| 1365 static const String TYPE_PROVIDER_INPUT = 'TYPE_PROVIDER_INPUT'; | |
| 1366 | |
| 1367 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 1368 'ComputeConstantDependenciesTask', createTask, buildInputs, | |
| 1369 <ResultDescriptor>[CONSTANT_DEPENDENCIES]); | |
| 1370 | |
| 1371 ComputeConstantDependenciesTask( | |
| 1372 InternalAnalysisContext context, ConstantEvaluationTarget constant) | |
| 1373 : super(context, constant); | |
| 1374 | |
| 1375 @override | |
| 1376 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 1377 | |
| 1378 @override | |
| 1379 void internalPerform() { | |
| 1380 // | |
| 1381 // Prepare inputs. | |
| 1382 // | |
| 1383 // Note: UNIT_INPUT is not needed. It is merely a bookkeeping dependency | |
| 1384 // to ensure that resolution has occurred before we attempt to determine | |
| 1385 // constant dependencies. | |
| 1386 // | |
| 1387 ConstantEvaluationTarget constant = target; | |
| 1388 AnalysisContext context = constant.context; | |
| 1389 TypeProvider typeProvider = getRequiredInput(TYPE_PROVIDER_INPUT); | |
| 1390 // | |
| 1391 // Compute dependencies. | |
| 1392 // | |
| 1393 List<ConstantEvaluationTarget> dependencies = <ConstantEvaluationTarget>[]; | |
| 1394 new ConstantEvaluationEngine(typeProvider, context.declaredVariables) | |
| 1395 .computeDependencies(constant, dependencies.add); | |
| 1396 // | |
| 1397 // Record outputs. | |
| 1398 // | |
| 1399 outputs[CONSTANT_DEPENDENCIES] = dependencies; | |
| 1400 } | |
| 1401 | |
| 1402 /** | |
| 1403 * Return a map from the names of the inputs of this kind of task to the task | |
| 1404 * input descriptors describing those inputs for a task with the | |
| 1405 * given [target]. | |
| 1406 */ | |
| 1407 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 1408 if (target is Element) { | |
| 1409 CompilationUnitElementImpl unit = target | |
| 1410 .getAncestor((Element element) => element is CompilationUnitElement); | |
| 1411 return <String, TaskInput>{ | |
| 1412 UNIT_INPUT: RESOLVED_UNIT5 | |
| 1413 .of(new LibrarySpecificUnit(unit.librarySource, target.source)), | |
| 1414 TYPE_PROVIDER_INPUT: TYPE_PROVIDER.of(AnalysisContextTarget.request) | |
| 1415 }; | |
| 1416 } else if (target is ConstantEvaluationTarget_Annotation) { | |
| 1417 return <String, TaskInput>{ | |
| 1418 UNIT_INPUT: RESOLVED_UNIT5 | |
| 1419 .of(new LibrarySpecificUnit(target.librarySource, target.source)), | |
| 1420 TYPE_PROVIDER_INPUT: TYPE_PROVIDER.of(AnalysisContextTarget.request) | |
| 1421 }; | |
| 1422 } | |
| 1423 throw new AnalysisException( | |
| 1424 'Cannot build inputs for a ${target.runtimeType}'); | |
| 1425 } | |
| 1426 | |
| 1427 /** | |
| 1428 * Create a [ResolveUnitReferencesTask] based on the given [target] in | |
| 1429 * the given [context]. | |
| 1430 */ | |
| 1431 static ComputeConstantDependenciesTask createTask( | |
| 1432 AnalysisContext context, AnalysisTarget target) { | |
| 1433 return new ComputeConstantDependenciesTask(context, target); | |
| 1434 } | |
| 1435 } | |
| 1436 | |
| 1437 /** | |
| 1438 * A task that computes the value of a constant ([CONSTANT_VALUE]) and | |
| 1439 * stores it in the element model. | |
| 1440 */ | |
| 1441 class ComputeConstantValueTask extends ConstantEvaluationAnalysisTask { | |
| 1442 /** | |
| 1443 * The name of the input which ensures that dependent constants are evaluated | |
| 1444 * before the target. | |
| 1445 */ | |
| 1446 static const String DEPENDENCIES_INPUT = 'DEPENDENCIES_INPUT'; | |
| 1447 | |
| 1448 /** | |
| 1449 * The name of the [TYPE_PROVIDER] input. | |
| 1450 */ | |
| 1451 static const String TYPE_PROVIDER_INPUT = 'TYPE_PROVIDER_INPUT'; | |
| 1452 | |
| 1453 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 1454 'ComputeConstantValueTask', createTask, buildInputs, | |
| 1455 <ResultDescriptor>[CONSTANT_VALUE]); | |
| 1456 | |
| 1457 ComputeConstantValueTask( | |
| 1458 InternalAnalysisContext context, ConstantEvaluationTarget constant) | |
| 1459 : super(context, constant); | |
| 1460 | |
| 1461 @override | |
| 1462 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 1463 | |
| 1464 @override | |
| 1465 bool get handlesDependencyCycles => true; | |
| 1466 | |
| 1467 @override | |
| 1468 void internalPerform() { | |
| 1469 // | |
| 1470 // Prepare inputs. | |
| 1471 // | |
| 1472 // Note: DEPENDENCIES_INPUT is not needed. It is merely a bookkeeping | |
| 1473 // dependency to ensure that the constants that this constant depends on | |
| 1474 // are computed first. | |
| 1475 ConstantEvaluationTarget constant = target; | |
| 1476 AnalysisContext context = constant.context; | |
| 1477 TypeProvider typeProvider = getRequiredInput(TYPE_PROVIDER_INPUT); | |
| 1478 // | |
| 1479 // Compute the value of the constant, or report an error if there was a | |
| 1480 // cycle. | |
| 1481 // | |
| 1482 ConstantEvaluationEngine constantEvaluationEngine = | |
| 1483 new ConstantEvaluationEngine(typeProvider, context.declaredVariables); | |
| 1484 if (dependencyCycle == null) { | |
| 1485 constantEvaluationEngine.computeConstantValue(constant); | |
| 1486 } else { | |
| 1487 List<ConstantEvaluationTarget> constantsInCycle = | |
| 1488 <ConstantEvaluationTarget>[]; | |
| 1489 for (WorkItem workItem in dependencyCycle) { | |
| 1490 if (workItem.descriptor == DESCRIPTOR) { | |
| 1491 constantsInCycle.add(workItem.target); | |
| 1492 } | |
| 1493 } | |
| 1494 assert(constantsInCycle.isNotEmpty); | |
| 1495 constantEvaluationEngine.generateCycleError(constantsInCycle, constant); | |
| 1496 } | |
| 1497 // | |
| 1498 // Record outputs. | |
| 1499 // | |
| 1500 outputs[CONSTANT_VALUE] = constant; | |
| 1501 } | |
| 1502 | |
| 1503 /** | |
| 1504 * Return a map from the names of the inputs of this kind of task to the task | |
| 1505 * input descriptors describing those inputs for a task with the given | |
| 1506 * [target]. | |
| 1507 */ | |
| 1508 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 1509 ConstantEvaluationTarget evaluationTarget = target; | |
| 1510 return <String, TaskInput>{ | |
| 1511 DEPENDENCIES_INPUT: | |
| 1512 CONSTANT_DEPENDENCIES.of(evaluationTarget).toListOf(CONSTANT_VALUE), | |
| 1513 TYPE_PROVIDER_INPUT: TYPE_PROVIDER.of(AnalysisContextTarget.request) | |
| 1514 }; | |
| 1515 } | |
| 1516 | |
| 1517 /** | |
| 1518 * Create a [ComputeConstantValueTask] based on the given [target] in the | |
| 1519 * given [context]. | |
| 1520 */ | |
| 1521 static ComputeConstantValueTask createTask( | |
| 1522 AnalysisContext context, AnalysisTarget target) { | |
| 1523 return new ComputeConstantValueTask(context, target); | |
| 1524 } | |
| 1525 } | |
| 1526 | |
| 1527 /** | |
| 1528 * A base class for analysis tasks whose target is expected to be a | |
| 1529 * [ConstantEvaluationTarget]. | |
| 1530 */ | |
| 1531 abstract class ConstantEvaluationAnalysisTask extends AnalysisTask { | |
| 1532 /** | |
| 1533 * Initialize a newly created task to perform analysis within the given | |
| 1534 * [context] in order to produce results for the given [constant]. | |
| 1535 */ | |
| 1536 ConstantEvaluationAnalysisTask( | |
| 1537 AnalysisContext context, ConstantEvaluationTarget constant) | |
| 1538 : super(context, constant); | |
| 1539 | |
| 1540 @override | |
| 1541 String get description { | |
| 1542 Source source = target.source; | |
| 1543 String sourceName = source == null ? '<unknown source>' : source.fullName; | |
| 1544 return '${descriptor.name} for element $target in source $sourceName'; | |
| 1545 } | |
| 1546 } | |
| 1547 | |
| 1548 /** | |
| 1549 * Interface for [AnalysisTarget]s for which constant evaluation can be | |
| 1550 * performed. | |
| 1551 */ | |
| 1552 abstract class ConstantEvaluationTarget extends AnalysisTarget { | |
| 1553 /** | |
| 1554 * Return the [AnalysisContext] which should be used to evaluate this | |
| 1555 * constant. | |
| 1556 */ | |
| 1557 AnalysisContext get context; | |
| 1558 } | |
| 1559 | |
| 1560 /** | |
| 1561 * A task that computes a list of the libraries containing the target source. | |
| 1562 */ | |
| 1563 class ContainingLibrariesTask extends SourceBasedAnalysisTask { | |
| 1564 /** | |
| 1565 * The task descriptor describing this kind of task. | |
| 1566 */ | |
| 1567 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 1568 'ContainingLibrariesTask', createTask, buildInputs, | |
| 1569 <ResultDescriptor>[CONTAINING_LIBRARIES]); | |
| 1570 | |
| 1571 ContainingLibrariesTask( | |
| 1572 InternalAnalysisContext context, AnalysisTarget target) | |
| 1573 : super(context, target); | |
| 1574 | |
| 1575 @override | |
| 1576 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 1577 | |
| 1578 @override | |
| 1579 void internalPerform() { | |
| 1580 // TODO(brianwilkerson) This value can change as new libraries are analyzed | |
| 1581 // so we need some way of making sure that this result is removed from the | |
| 1582 // cache appropriately. | |
| 1583 Source source = getRequiredSource(); | |
| 1584 outputs[CONTAINING_LIBRARIES] = context.getLibrariesContaining(source); | |
| 1585 } | |
| 1586 | |
| 1587 /** | |
| 1588 * Return a map from the names of the inputs of this kind of task to the task | |
| 1589 * input descriptors describing those inputs for a task with the | |
| 1590 * given [target]. | |
| 1591 */ | |
| 1592 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 1593 return <String, TaskInput>{}; | |
| 1594 } | |
| 1595 | |
| 1596 /** | |
| 1597 * Create a [ContainingLibrariesTask] based on the given [target] in the given | |
| 1598 * [context]. | |
| 1599 */ | |
| 1600 static ContainingLibrariesTask createTask( | |
| 1601 AnalysisContext context, AnalysisTarget target) { | |
| 1602 return new ContainingLibrariesTask(context, target); | |
| 1603 } | |
| 1604 } | |
| 1605 | |
| 1606 /** | |
| 1607 * The description for a change in a Dart source. | |
| 1608 */ | |
| 1609 class DartDelta extends Delta { | |
| 1610 bool hasDirectiveChange = false; | |
| 1611 | |
| 1612 final Set<String> addedNames = new Set<String>(); | |
| 1613 final Set<String> changedNames = new Set<String>(); | |
| 1614 final Set<String> removedNames = new Set<String>(); | |
| 1615 | |
| 1616 final Set<Source> invalidatedSources = new Set<Source>(); | |
| 1617 | |
| 1618 DartDelta(Source source) : super(source) { | |
| 1619 invalidatedSources.add(source); | |
| 1620 } | |
| 1621 | |
| 1622 void elementAdded(Element element) { | |
| 1623 addedNames.add(element.name); | |
| 1624 } | |
| 1625 | |
| 1626 void elementChanged(Element element) { | |
| 1627 changedNames.add(element.name); | |
| 1628 } | |
| 1629 | |
| 1630 void elementRemoved(Element element) { | |
| 1631 removedNames.add(element.name); | |
| 1632 } | |
| 1633 | |
| 1634 bool isNameAffected(String name) { | |
| 1635 return addedNames.contains(name) || | |
| 1636 changedNames.contains(name) || | |
| 1637 removedNames.contains(name); | |
| 1638 } | |
| 1639 | |
| 1640 bool nameChanged(String name) { | |
| 1641 return changedNames.add(name); | |
| 1642 } | |
| 1643 | |
| 1644 @override | |
| 1645 DeltaResult validate(InternalAnalysisContext context, AnalysisTarget target, | |
| 1646 ResultDescriptor descriptor) { | |
| 1647 if (hasDirectiveChange) { | |
| 1648 return DeltaResult.INVALIDATE; | |
| 1649 } | |
| 1650 // Prepare target source. | |
| 1651 Source targetSource = null; | |
| 1652 if (target is Source) { | |
| 1653 targetSource = target; | |
| 1654 } | |
| 1655 if (target is LibrarySpecificUnit) { | |
| 1656 targetSource = target.library; | |
| 1657 } | |
| 1658 if (target is Element) { | |
| 1659 targetSource = target.source; | |
| 1660 } | |
| 1661 // Keep results that are updated incrementally. | |
| 1662 // If we want to analyze only some references to the source being changed, | |
| 1663 // we need to keep the same instances of CompilationUnitElement and | |
| 1664 // LibraryElement. | |
| 1665 if (targetSource == source) { | |
| 1666 if (ParseDartTask.DESCRIPTOR.results.contains(descriptor)) { | |
| 1667 return DeltaResult.KEEP_CONTINUE; | |
| 1668 } | |
| 1669 if (BuildCompilationUnitElementTask.DESCRIPTOR.results | |
| 1670 .contains(descriptor)) { | |
| 1671 return DeltaResult.KEEP_CONTINUE; | |
| 1672 } | |
| 1673 if (BuildLibraryElementTask.DESCRIPTOR.results.contains(descriptor)) { | |
| 1674 return DeltaResult.KEEP_CONTINUE; | |
| 1675 } | |
| 1676 return DeltaResult.INVALIDATE; | |
| 1677 } | |
| 1678 // Use the target library dependency information to decide whether | |
| 1679 // the delta affects the library. | |
| 1680 if (targetSource != null) { | |
| 1681 List<Source> librarySources = | |
| 1682 context.getLibrariesContaining(targetSource); | |
| 1683 for (Source librarySource in librarySources) { | |
| 1684 AnalysisCache cache = context.analysisCache; | |
| 1685 ReferencedNames referencedNames = | |
| 1686 cache.getValue(librarySource, REFERENCED_NAMES); | |
| 1687 if (referencedNames == null) { | |
| 1688 return DeltaResult.INVALIDATE; | |
| 1689 } | |
| 1690 referencedNames.addChangedElements(this); | |
| 1691 if (referencedNames.isAffectedBy(this)) { | |
| 1692 return DeltaResult.INVALIDATE; | |
| 1693 } | |
| 1694 } | |
| 1695 return DeltaResult.STOP; | |
| 1696 } | |
| 1697 // We don't know what to do with the given target, invalidate it. | |
| 1698 return DeltaResult.INVALIDATE; | |
| 1699 } | |
| 1700 } | |
| 1701 | |
| 1702 /** | |
| 1703 * A task that merges all of the errors for a single source into a single list | |
| 1704 * of errors. | |
| 1705 */ | |
| 1706 class DartErrorsTask extends SourceBasedAnalysisTask { | |
| 1707 /** | |
| 1708 * The name of the [BUILD_DIRECTIVES_ERRORS] input. | |
| 1709 */ | |
| 1710 static const String BUILD_DIRECTIVES_ERRORS_INPUT = 'BUILD_DIRECTIVES_ERRORS'; | |
| 1711 | |
| 1712 /** | |
| 1713 * The name of the [BUILD_LIBRARY_ERRORS] input. | |
| 1714 */ | |
| 1715 static const String BUILD_LIBRARY_ERRORS_INPUT = 'BUILD_LIBRARY_ERRORS'; | |
| 1716 | |
| 1717 /** | |
| 1718 * The name of the [LIBRARY_UNIT_ERRORS] input. | |
| 1719 */ | |
| 1720 static const String LIBRARY_UNIT_ERRORS_INPUT = 'LIBRARY_UNIT_ERRORS'; | |
| 1721 | |
| 1722 /** | |
| 1723 * The name of the [PARSE_ERRORS] input. | |
| 1724 */ | |
| 1725 static const String PARSE_ERRORS_INPUT = 'PARSE_ERRORS'; | |
| 1726 | |
| 1727 /** | |
| 1728 * The name of the [SCAN_ERRORS] input. | |
| 1729 */ | |
| 1730 static const String SCAN_ERRORS_INPUT = 'SCAN_ERRORS'; | |
| 1731 | |
| 1732 /** | |
| 1733 * The task descriptor describing this kind of task. | |
| 1734 */ | |
| 1735 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor('DartErrorsTask', | |
| 1736 createTask, buildInputs, <ResultDescriptor>[DART_ERRORS]); | |
| 1737 | |
| 1738 DartErrorsTask(InternalAnalysisContext context, AnalysisTarget target) | |
| 1739 : super(context, target); | |
| 1740 | |
| 1741 @override | |
| 1742 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 1743 | |
| 1744 @override | |
| 1745 void internalPerform() { | |
| 1746 // | |
| 1747 // Prepare inputs. | |
| 1748 // | |
| 1749 List<List<AnalysisError>> errorLists = <List<AnalysisError>>[]; | |
| 1750 errorLists.add(getRequiredInput(BUILD_DIRECTIVES_ERRORS_INPUT)); | |
| 1751 errorLists.add(getRequiredInput(BUILD_LIBRARY_ERRORS_INPUT)); | |
| 1752 errorLists.add(getRequiredInput(PARSE_ERRORS_INPUT)); | |
| 1753 errorLists.add(getRequiredInput(SCAN_ERRORS_INPUT)); | |
| 1754 Map<Source, List<AnalysisError>> unitErrors = | |
| 1755 getRequiredInput(LIBRARY_UNIT_ERRORS_INPUT); | |
| 1756 for (List<AnalysisError> errors in unitErrors.values) { | |
| 1757 errorLists.add(errors); | |
| 1758 } | |
| 1759 // | |
| 1760 // Record outputs. | |
| 1761 // | |
| 1762 outputs[DART_ERRORS] = AnalysisError.mergeLists(errorLists); | |
| 1763 } | |
| 1764 | |
| 1765 /** | |
| 1766 * Return a map from the names of the inputs of this kind of task to the task | |
| 1767 * input descriptors describing those inputs for a task with the | |
| 1768 * given [target]. | |
| 1769 */ | |
| 1770 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 1771 Source source = target; | |
| 1772 return <String, TaskInput>{ | |
| 1773 BUILD_DIRECTIVES_ERRORS_INPUT: BUILD_DIRECTIVES_ERRORS.of(source), | |
| 1774 BUILD_LIBRARY_ERRORS_INPUT: BUILD_LIBRARY_ERRORS.of(source), | |
| 1775 PARSE_ERRORS_INPUT: PARSE_ERRORS.of(source), | |
| 1776 SCAN_ERRORS_INPUT: SCAN_ERRORS.of(source), | |
| 1777 LIBRARY_UNIT_ERRORS_INPUT: CONTAINING_LIBRARIES | |
| 1778 .of(source) | |
| 1779 .toMap((Source library) { | |
| 1780 LibrarySpecificUnit unit = new LibrarySpecificUnit(library, source); | |
| 1781 return LIBRARY_UNIT_ERRORS.of(unit); | |
| 1782 }) | |
| 1783 }; | |
| 1784 } | |
| 1785 | |
| 1786 /** | |
| 1787 * Create a [DartErrorsTask] based on the given [target] in the given | |
| 1788 * [context]. | |
| 1789 */ | |
| 1790 static DartErrorsTask createTask( | |
| 1791 AnalysisContext context, AnalysisTarget target) { | |
| 1792 return new DartErrorsTask(context, target); | |
| 1793 } | |
| 1794 } | |
| 1795 | |
| 1796 /** | |
| 1797 * A task that builds [RESOLVED_UNIT] for a unit. | |
| 1798 */ | |
| 1799 class EvaluateUnitConstantsTask extends SourceBasedAnalysisTask { | |
| 1800 /** | |
| 1801 * The name of the [RESOLVED_UNIT5] input. | |
| 1802 */ | |
| 1803 static const String UNIT_INPUT = 'UNIT_INPUT'; | |
| 1804 | |
| 1805 /** | |
| 1806 * The name of the [CONSTANT_VALUE] input. | |
| 1807 */ | |
| 1808 static const String CONSTANT_VALUES = 'CONSTANT_VALUES'; | |
| 1809 | |
| 1810 /** | |
| 1811 * The task descriptor describing this kind of task. | |
| 1812 */ | |
| 1813 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 1814 'EvaluateUnitConstantsTask', createTask, buildInputs, | |
| 1815 <ResultDescriptor>[RESOLVED_UNIT]); | |
| 1816 | |
| 1817 EvaluateUnitConstantsTask(AnalysisContext context, LibrarySpecificUnit target) | |
| 1818 : super(context, target); | |
| 1819 | |
| 1820 @override | |
| 1821 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 1822 | |
| 1823 @override | |
| 1824 void internalPerform() { | |
| 1825 // No actual work needs to be performed; the task manager will ensure that | |
| 1826 // all constants are evaluated before this method is called. | |
| 1827 CompilationUnit unit = getRequiredInput(UNIT_INPUT); | |
| 1828 outputs[RESOLVED_UNIT] = unit; | |
| 1829 } | |
| 1830 | |
| 1831 /** | |
| 1832 * Return a map from the names of the inputs of this kind of task to the task | |
| 1833 * input descriptors describing those inputs for a task with the | |
| 1834 * given [target]. | |
| 1835 */ | |
| 1836 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 1837 LibrarySpecificUnit unit = target; | |
| 1838 return <String, TaskInput>{ | |
| 1839 'libraryElement': LIBRARY_ELEMENT.of(unit.library), | |
| 1840 UNIT_INPUT: RESOLVED_UNIT5.of(unit), | |
| 1841 CONSTANT_VALUES: | |
| 1842 COMPILATION_UNIT_CONSTANTS.of(unit).toListOf(CONSTANT_VALUE) | |
| 1843 }; | |
| 1844 } | |
| 1845 | |
| 1846 /** | |
| 1847 * Create an [EvaluateUnitConstantsTask] based on the given [target] in | |
| 1848 * the given [context]. | |
| 1849 */ | |
| 1850 static EvaluateUnitConstantsTask createTask( | |
| 1851 AnalysisContext context, AnalysisTarget target) { | |
| 1852 return new EvaluateUnitConstantsTask(context, target); | |
| 1853 } | |
| 1854 } | |
| 1855 | |
| 1856 /** | |
| 1857 * The helper for building the export [Namespace] of a [LibraryElement]. | |
| 1858 */ | |
| 1859 class ExportNamespaceBuilder { | |
| 1860 /** | |
| 1861 * Build the export [Namespace] of the given [LibraryElement]. | |
| 1862 */ | |
| 1863 Namespace build(LibraryElement library) { | |
| 1864 return new Namespace( | |
| 1865 _createExportMapping(library, new HashSet<LibraryElement>())); | |
| 1866 } | |
| 1867 | |
| 1868 /** | |
| 1869 * Create a mapping table representing the export namespace of the given | |
| 1870 * [library]. | |
| 1871 * | |
| 1872 * The given [visitedElements] a set of libraries that do not need to be | |
| 1873 * visited when processing the export directives of the given library because | |
| 1874 * all of the names defined by them will be added by another library. | |
| 1875 */ | |
| 1876 HashMap<String, Element> _createExportMapping( | |
| 1877 LibraryElement library, HashSet<LibraryElement> visitedElements) { | |
| 1878 visitedElements.add(library); | |
| 1879 try { | |
| 1880 HashMap<String, Element> definedNames = new HashMap<String, Element>(); | |
| 1881 // Add names of the export directives. | |
| 1882 for (ExportElement element in library.exports) { | |
| 1883 LibraryElement exportedLibrary = element.exportedLibrary; | |
| 1884 if (exportedLibrary != null && | |
| 1885 !visitedElements.contains(exportedLibrary)) { | |
| 1886 // | |
| 1887 // The exported library will be null if the URI does not reference a | |
| 1888 // valid library. | |
| 1889 // | |
| 1890 HashMap<String, Element> exportedNames = | |
| 1891 _createExportMapping(exportedLibrary, visitedElements); | |
| 1892 exportedNames = _applyCombinators(exportedNames, element.combinators); | |
| 1893 definedNames.addAll(exportedNames); | |
| 1894 } | |
| 1895 } | |
| 1896 // Add names of the public namespace. | |
| 1897 { | |
| 1898 Namespace publicNamespace = library.publicNamespace; | |
| 1899 if (publicNamespace != null) { | |
| 1900 definedNames.addAll(publicNamespace.definedNames); | |
| 1901 } | |
| 1902 } | |
| 1903 return definedNames; | |
| 1904 } finally { | |
| 1905 visitedElements.remove(library); | |
| 1906 } | |
| 1907 } | |
| 1908 | |
| 1909 /** | |
| 1910 * Apply the given [combinators] to all of the names in [definedNames]. | |
| 1911 */ | |
| 1912 static HashMap<String, Element> _applyCombinators( | |
| 1913 HashMap<String, Element> definedNames, | |
| 1914 List<NamespaceCombinator> combinators) { | |
| 1915 for (NamespaceCombinator combinator in combinators) { | |
| 1916 if (combinator is HideElementCombinator) { | |
| 1917 _hide(definedNames, combinator.hiddenNames); | |
| 1918 } else if (combinator is ShowElementCombinator) { | |
| 1919 definedNames = _show(definedNames, combinator.shownNames); | |
| 1920 } | |
| 1921 } | |
| 1922 return definedNames; | |
| 1923 } | |
| 1924 | |
| 1925 /** | |
| 1926 * Hide all of the [hiddenNames] by removing them from the given | |
| 1927 * [definedNames]. | |
| 1928 */ | |
| 1929 static void _hide( | |
| 1930 HashMap<String, Element> definedNames, List<String> hiddenNames) { | |
| 1931 for (String name in hiddenNames) { | |
| 1932 definedNames.remove(name); | |
| 1933 definedNames.remove('$name='); | |
| 1934 } | |
| 1935 } | |
| 1936 | |
| 1937 /** | |
| 1938 * Show only the given [shownNames] by removing all other names from the given | |
| 1939 * [definedNames]. | |
| 1940 */ | |
| 1941 static HashMap<String, Element> _show( | |
| 1942 HashMap<String, Element> definedNames, List<String> shownNames) { | |
| 1943 HashMap<String, Element> newNames = new HashMap<String, Element>(); | |
| 1944 for (String name in shownNames) { | |
| 1945 Element element = definedNames[name]; | |
| 1946 if (element != null) { | |
| 1947 newNames[name] = element; | |
| 1948 } | |
| 1949 String setterName = '$name='; | |
| 1950 element = definedNames[setterName]; | |
| 1951 if (element != null) { | |
| 1952 newNames[setterName] = element; | |
| 1953 } | |
| 1954 } | |
| 1955 return newNames; | |
| 1956 } | |
| 1957 } | |
| 1958 | |
| 1959 /** | |
| 1960 * A task that builds [USED_IMPORTED_ELEMENTS] for a unit. | |
| 1961 */ | |
| 1962 class GatherUsedImportedElementsTask extends SourceBasedAnalysisTask { | |
| 1963 /** | |
| 1964 * The name of the [RESOLVED_UNIT5] input. | |
| 1965 */ | |
| 1966 static const String UNIT_INPUT = 'UNIT_INPUT'; | |
| 1967 | |
| 1968 /** | |
| 1969 * The task descriptor describing this kind of task. | |
| 1970 */ | |
| 1971 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 1972 'GatherUsedImportedElementsTask', createTask, buildInputs, | |
| 1973 <ResultDescriptor>[USED_IMPORTED_ELEMENTS]); | |
| 1974 | |
| 1975 GatherUsedImportedElementsTask( | |
| 1976 InternalAnalysisContext context, AnalysisTarget target) | |
| 1977 : super(context, target); | |
| 1978 | |
| 1979 @override | |
| 1980 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 1981 | |
| 1982 @override | |
| 1983 void internalPerform() { | |
| 1984 CompilationUnit unit = getRequiredInput(UNIT_INPUT); | |
| 1985 CompilationUnitElement unitElement = unit.element; | |
| 1986 LibraryElement libraryElement = unitElement.library; | |
| 1987 // | |
| 1988 // Prepare used imported elements. | |
| 1989 // | |
| 1990 GatherUsedImportedElementsVisitor visitor = | |
| 1991 new GatherUsedImportedElementsVisitor(libraryElement); | |
| 1992 unit.accept(visitor); | |
| 1993 // | |
| 1994 // Record outputs. | |
| 1995 // | |
| 1996 outputs[USED_IMPORTED_ELEMENTS] = visitor.usedElements; | |
| 1997 } | |
| 1998 | |
| 1999 /** | |
| 2000 * Return a map from the names of the inputs of this kind of task to the task | |
| 2001 * input descriptors describing those inputs for a task with the | |
| 2002 * given [target]. | |
| 2003 */ | |
| 2004 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 2005 LibrarySpecificUnit unit = target; | |
| 2006 return <String, TaskInput>{UNIT_INPUT: RESOLVED_UNIT5.of(unit)}; | |
| 2007 } | |
| 2008 | |
| 2009 /** | |
| 2010 * Create a [GatherUsedImportedElementsTask] based on the given [target] in | |
| 2011 * the given [context]. | |
| 2012 */ | |
| 2013 static GatherUsedImportedElementsTask createTask( | |
| 2014 AnalysisContext context, AnalysisTarget target) { | |
| 2015 return new GatherUsedImportedElementsTask(context, target); | |
| 2016 } | |
| 2017 } | |
| 2018 | |
| 2019 /** | |
| 2020 * A task that builds [USED_LOCAL_ELEMENTS] for a unit. | |
| 2021 */ | |
| 2022 class GatherUsedLocalElementsTask extends SourceBasedAnalysisTask { | |
| 2023 /** | |
| 2024 * The name of the [RESOLVED_UNIT5] input. | |
| 2025 */ | |
| 2026 static const String UNIT_INPUT = 'UNIT_INPUT'; | |
| 2027 | |
| 2028 /** | |
| 2029 * The task descriptor describing this kind of task. | |
| 2030 */ | |
| 2031 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 2032 'GatherUsedLocalElementsTask', createTask, buildInputs, | |
| 2033 <ResultDescriptor>[USED_LOCAL_ELEMENTS]); | |
| 2034 | |
| 2035 GatherUsedLocalElementsTask( | |
| 2036 InternalAnalysisContext context, AnalysisTarget target) | |
| 2037 : super(context, target); | |
| 2038 | |
| 2039 @override | |
| 2040 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 2041 | |
| 2042 @override | |
| 2043 void internalPerform() { | |
| 2044 CompilationUnit unit = getRequiredInput(UNIT_INPUT); | |
| 2045 CompilationUnitElement unitElement = unit.element; | |
| 2046 LibraryElement libraryElement = unitElement.library; | |
| 2047 // | |
| 2048 // Prepare used local elements. | |
| 2049 // | |
| 2050 GatherUsedLocalElementsVisitor visitor = | |
| 2051 new GatherUsedLocalElementsVisitor(libraryElement); | |
| 2052 unit.accept(visitor); | |
| 2053 // | |
| 2054 // Record outputs. | |
| 2055 // | |
| 2056 outputs[USED_LOCAL_ELEMENTS] = visitor.usedElements; | |
| 2057 } | |
| 2058 | |
| 2059 /** | |
| 2060 * Return a map from the names of the inputs of this kind of task to the task | |
| 2061 * input descriptors describing those inputs for a task with the | |
| 2062 * given [target]. | |
| 2063 */ | |
| 2064 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 2065 LibrarySpecificUnit unit = target; | |
| 2066 return <String, TaskInput>{UNIT_INPUT: RESOLVED_UNIT5.of(unit)}; | |
| 2067 } | |
| 2068 | |
| 2069 /** | |
| 2070 * Create a [GatherUsedLocalElementsTask] based on the given [target] in | |
| 2071 * the given [context]. | |
| 2072 */ | |
| 2073 static GatherUsedLocalElementsTask createTask( | |
| 2074 AnalysisContext context, AnalysisTarget target) { | |
| 2075 return new GatherUsedLocalElementsTask(context, target); | |
| 2076 } | |
| 2077 } | |
| 2078 | |
| 2079 /** | |
| 2080 * A task that generates [HINTS] for a unit. | |
| 2081 */ | |
| 2082 class GenerateHintsTask extends SourceBasedAnalysisTask { | |
| 2083 /** | |
| 2084 * The name of the [RESOLVED_UNIT5] input. | |
| 2085 */ | |
| 2086 static const String RESOLVED_UNIT_INPUT = 'RESOLVED_UNIT'; | |
| 2087 | |
| 2088 /** | |
| 2089 * The name of a list of [USED_LOCAL_ELEMENTS] for each library unit input. | |
| 2090 */ | |
| 2091 static const String USED_LOCAL_ELEMENTS_INPUT = 'USED_LOCAL_ELEMENTS'; | |
| 2092 | |
| 2093 /** | |
| 2094 * The name of a list of [USED_IMPORTED_ELEMENTS] for each library unit input. | |
| 2095 */ | |
| 2096 static const String USED_IMPORTED_ELEMENTS_INPUT = 'USED_IMPORTED_ELEMENTS'; | |
| 2097 | |
| 2098 /** | |
| 2099 * The name of the [TYPE_PROVIDER] input. | |
| 2100 */ | |
| 2101 static const String TYPE_PROVIDER_INPUT = 'TYPE_PROVIDER_INPUT'; | |
| 2102 | |
| 2103 /** | |
| 2104 * The task descriptor describing this kind of task. | |
| 2105 */ | |
| 2106 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 2107 'GenerateHintsTask', createTask, buildInputs, <ResultDescriptor>[HINTS]); | |
| 2108 | |
| 2109 GenerateHintsTask(InternalAnalysisContext context, AnalysisTarget target) | |
| 2110 : super(context, target); | |
| 2111 | |
| 2112 @override | |
| 2113 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 2114 | |
| 2115 @override | |
| 2116 void internalPerform() { | |
| 2117 AnalysisOptions analysisOptions = context.analysisOptions; | |
| 2118 if (!analysisOptions.hint) { | |
| 2119 outputs[HINTS] = AnalysisError.NO_ERRORS; | |
| 2120 return; | |
| 2121 } | |
| 2122 // | |
| 2123 // Prepare collectors. | |
| 2124 // | |
| 2125 RecordingErrorListener errorListener = new RecordingErrorListener(); | |
| 2126 Source source = getRequiredSource(); | |
| 2127 ErrorReporter errorReporter = new ErrorReporter(errorListener, source); | |
| 2128 // | |
| 2129 // Prepare inputs. | |
| 2130 // | |
| 2131 CompilationUnit unit = getRequiredInput(RESOLVED_UNIT_INPUT); | |
| 2132 List<UsedImportedElements> usedImportedElementsList = | |
| 2133 getRequiredInput(USED_IMPORTED_ELEMENTS_INPUT); | |
| 2134 List<UsedLocalElements> usedLocalElementsList = | |
| 2135 getRequiredInput(USED_LOCAL_ELEMENTS_INPUT); | |
| 2136 CompilationUnitElement unitElement = unit.element; | |
| 2137 LibraryElement libraryElement = unitElement.library; | |
| 2138 // | |
| 2139 // Generate errors. | |
| 2140 // | |
| 2141 unit.accept(new DeadCodeVerifier(errorReporter)); | |
| 2142 // Verify imports. | |
| 2143 { | |
| 2144 ImportsVerifier verifier = new ImportsVerifier(); | |
| 2145 verifier.addImports(unit); | |
| 2146 usedImportedElementsList.forEach(verifier.removeUsedElements); | |
| 2147 verifier.generateDuplicateImportHints(errorReporter); | |
| 2148 verifier.generateUnusedImportHints(errorReporter); | |
| 2149 } | |
| 2150 // Unused local elements. | |
| 2151 { | |
| 2152 UsedLocalElements usedElements = | |
| 2153 new UsedLocalElements.merge(usedLocalElementsList); | |
| 2154 UnusedLocalElementsVerifier visitor = | |
| 2155 new UnusedLocalElementsVerifier(errorListener, usedElements); | |
| 2156 unitElement.accept(visitor); | |
| 2157 } | |
| 2158 // Dart2js analysis. | |
| 2159 if (analysisOptions.dart2jsHint) { | |
| 2160 unit.accept(new Dart2JSVerifier(errorReporter)); | |
| 2161 } | |
| 2162 // Dart best practices. | |
| 2163 InheritanceManager inheritanceManager = | |
| 2164 new InheritanceManager(libraryElement); | |
| 2165 TypeProvider typeProvider = getRequiredInput(TYPE_PROVIDER_INPUT); | |
| 2166 unit.accept(new BestPracticesVerifier(errorReporter, typeProvider)); | |
| 2167 unit.accept(new OverrideVerifier(errorReporter, inheritanceManager)); | |
| 2168 // Find to-do comments. | |
| 2169 new ToDoFinder(errorReporter).findIn(unit); | |
| 2170 // | |
| 2171 // Record outputs. | |
| 2172 // | |
| 2173 outputs[HINTS] = errorListener.errors; | |
| 2174 } | |
| 2175 | |
| 2176 /** | |
| 2177 * Return a map from the names of the inputs of this kind of task to the task | |
| 2178 * input descriptors describing those inputs for a task with the | |
| 2179 * given [target]. | |
| 2180 */ | |
| 2181 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 2182 LibrarySpecificUnit unit = target; | |
| 2183 Source libSource = unit.library; | |
| 2184 return <String, TaskInput>{ | |
| 2185 RESOLVED_UNIT_INPUT: RESOLVED_UNIT.of(unit), | |
| 2186 USED_LOCAL_ELEMENTS_INPUT: UNITS.of(libSource).toList((unit) { | |
| 2187 LibrarySpecificUnit target = new LibrarySpecificUnit(libSource, unit); | |
| 2188 return USED_LOCAL_ELEMENTS.of(target); | |
| 2189 }), | |
| 2190 USED_IMPORTED_ELEMENTS_INPUT: UNITS.of(libSource).toList((unit) { | |
| 2191 LibrarySpecificUnit target = new LibrarySpecificUnit(libSource, unit); | |
| 2192 return USED_IMPORTED_ELEMENTS.of(target); | |
| 2193 }), | |
| 2194 TYPE_PROVIDER_INPUT: TYPE_PROVIDER.of(AnalysisContextTarget.request) | |
| 2195 }; | |
| 2196 } | |
| 2197 | |
| 2198 /** | |
| 2199 * Create a [GenerateHintsTask] based on the given [target] in | |
| 2200 * the given [context]. | |
| 2201 */ | |
| 2202 static GenerateHintsTask createTask( | |
| 2203 AnalysisContext context, AnalysisTarget target) { | |
| 2204 return new GenerateHintsTask(context, target); | |
| 2205 } | |
| 2206 } | |
| 2207 | |
| 2208 /** | |
| 2209 * A task computes all of the errors of all of the units for a single | |
| 2210 * library source and sets the [LIBRARY_ERRORS_READY] flag. | |
| 2211 */ | |
| 2212 class LibraryErrorsReadyTask extends SourceBasedAnalysisTask { | |
| 2213 /** | |
| 2214 * The task descriptor describing this kind of task. | |
| 2215 */ | |
| 2216 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 2217 'LibraryErrorsReadyTask', createTask, buildInputs, | |
| 2218 <ResultDescriptor>[LIBRARY_ERRORS_READY]); | |
| 2219 | |
| 2220 LibraryErrorsReadyTask(InternalAnalysisContext context, AnalysisTarget target) | |
| 2221 : super(context, target); | |
| 2222 | |
| 2223 @override | |
| 2224 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 2225 | |
| 2226 @override | |
| 2227 void internalPerform() { | |
| 2228 outputs[LIBRARY_ERRORS_READY] = true; | |
| 2229 } | |
| 2230 | |
| 2231 /** | |
| 2232 * Return a map from the names of the inputs of this kind of task to the task | |
| 2233 * input descriptors describing those inputs for a task with the | |
| 2234 * given [library]. | |
| 2235 */ | |
| 2236 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 2237 Source source = target; | |
| 2238 return <String, TaskInput>{ | |
| 2239 'allErrors': UNITS.of(source).toListOf(DART_ERRORS) | |
| 2240 }; | |
| 2241 } | |
| 2242 | |
| 2243 /** | |
| 2244 * Create a [LibraryErrorsReadyTask] based on the given [target] in the given | |
| 2245 * [context]. | |
| 2246 */ | |
| 2247 static LibraryErrorsReadyTask createTask( | |
| 2248 AnalysisContext context, AnalysisTarget target) { | |
| 2249 return new LibraryErrorsReadyTask(context, target); | |
| 2250 } | |
| 2251 } | |
| 2252 | |
| 2253 /** | |
| 2254 * A task that merges all of the errors for a single source into a single list | |
| 2255 * of errors. | |
| 2256 */ | |
| 2257 class LibraryUnitErrorsTask extends SourceBasedAnalysisTask { | |
| 2258 /** | |
| 2259 * The name of the [HINTS] input. | |
| 2260 */ | |
| 2261 static const String HINTS_INPUT = 'HINTS'; | |
| 2262 | |
| 2263 /** | |
| 2264 * The name of the [RESOLVE_REFERENCES_ERRORS] input. | |
| 2265 */ | |
| 2266 static const String RESOLVE_REFERENCES_ERRORS_INPUT = | |
| 2267 'RESOLVE_REFERENCES_ERRORS'; | |
| 2268 | |
| 2269 /** | |
| 2270 * The name of the [RESOLVE_TYPE_NAMES_ERRORS] input. | |
| 2271 */ | |
| 2272 static const String RESOLVE_TYPE_NAMES_ERRORS_INPUT = | |
| 2273 'RESOLVE_TYPE_NAMES_ERRORS'; | |
| 2274 | |
| 2275 /** | |
| 2276 * The name of the [VARIABLE_REFERENCE_ERRORS] input. | |
| 2277 */ | |
| 2278 static const String VARIABLE_REFERENCE_ERRORS_INPUT = | |
| 2279 'VARIABLE_REFERENCE_ERRORS'; | |
| 2280 | |
| 2281 /** | |
| 2282 * The name of the [VERIFY_ERRORS] input. | |
| 2283 */ | |
| 2284 static const String VERIFY_ERRORS_INPUT = 'VERIFY_ERRORS'; | |
| 2285 | |
| 2286 /** | |
| 2287 * The task descriptor describing this kind of task. | |
| 2288 */ | |
| 2289 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 2290 'LibraryUnitErrorsTask', createTask, buildInputs, | |
| 2291 <ResultDescriptor>[LIBRARY_UNIT_ERRORS]); | |
| 2292 | |
| 2293 LibraryUnitErrorsTask(InternalAnalysisContext context, AnalysisTarget target) | |
| 2294 : super(context, target); | |
| 2295 | |
| 2296 @override | |
| 2297 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 2298 | |
| 2299 @override | |
| 2300 void internalPerform() { | |
| 2301 // | |
| 2302 // Prepare inputs. | |
| 2303 // | |
| 2304 List<List<AnalysisError>> errorLists = <List<AnalysisError>>[]; | |
| 2305 errorLists.add(getRequiredInput(HINTS_INPUT)); | |
| 2306 errorLists.add(getRequiredInput(RESOLVE_REFERENCES_ERRORS_INPUT)); | |
| 2307 errorLists.add(getRequiredInput(RESOLVE_TYPE_NAMES_ERRORS_INPUT)); | |
| 2308 errorLists.add(getRequiredInput(VARIABLE_REFERENCE_ERRORS_INPUT)); | |
| 2309 errorLists.add(getRequiredInput(VERIFY_ERRORS_INPUT)); | |
| 2310 // | |
| 2311 // Record outputs. | |
| 2312 // | |
| 2313 outputs[LIBRARY_UNIT_ERRORS] = AnalysisError.mergeLists(errorLists); | |
| 2314 } | |
| 2315 | |
| 2316 /** | |
| 2317 * Return a map from the names of the inputs of this kind of task to the task | |
| 2318 * input descriptors describing those inputs for a task with the | |
| 2319 * given [unit]. | |
| 2320 */ | |
| 2321 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 2322 LibrarySpecificUnit unit = target; | |
| 2323 return <String, TaskInput>{ | |
| 2324 HINTS_INPUT: HINTS.of(unit), | |
| 2325 RESOLVE_REFERENCES_ERRORS_INPUT: RESOLVE_REFERENCES_ERRORS.of(unit), | |
| 2326 RESOLVE_TYPE_NAMES_ERRORS_INPUT: RESOLVE_TYPE_NAMES_ERRORS.of(unit), | |
| 2327 VARIABLE_REFERENCE_ERRORS_INPUT: VARIABLE_REFERENCE_ERRORS.of(unit), | |
| 2328 VERIFY_ERRORS_INPUT: VERIFY_ERRORS.of(unit) | |
| 2329 }; | |
| 2330 } | |
| 2331 | |
| 2332 /** | |
| 2333 * Create a [LibraryUnitErrorsTask] based on the given [target] in the given | |
| 2334 * [context]. | |
| 2335 */ | |
| 2336 static LibraryUnitErrorsTask createTask( | |
| 2337 AnalysisContext context, AnalysisTarget target) { | |
| 2338 return new LibraryUnitErrorsTask(context, target); | |
| 2339 } | |
| 2340 } | |
| 2341 | |
| 2342 /** | |
| 2343 * A task that parses the content of a Dart file, producing an AST structure. | |
| 2344 */ | |
| 2345 class ParseDartTask extends SourceBasedAnalysisTask { | |
| 2346 /** | |
| 2347 * The name of the input whose value is the line information produced for the | |
| 2348 * file. | |
| 2349 */ | |
| 2350 static const String LINE_INFO_INPUT_NAME = 'LINE_INFO_INPUT_NAME'; | |
| 2351 | |
| 2352 /** | |
| 2353 * The name of the input whose value is the modification time of the file. | |
| 2354 */ | |
| 2355 static const String MODIFICATION_TIME_INPUT_NAME = | |
| 2356 'MODIFICATION_TIME_INPUT_NAME'; | |
| 2357 | |
| 2358 /** | |
| 2359 * The name of the input whose value is the token stream produced for the file
. | |
| 2360 */ | |
| 2361 static const String TOKEN_STREAM_INPUT_NAME = 'TOKEN_STREAM_INPUT_NAME'; | |
| 2362 | |
| 2363 /** | |
| 2364 * The task descriptor describing this kind of task. | |
| 2365 */ | |
| 2366 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor('ParseDartTask', | |
| 2367 createTask, buildInputs, <ResultDescriptor>[ | |
| 2368 EXPLICITLY_IMPORTED_LIBRARIES, | |
| 2369 EXPORTED_LIBRARIES, | |
| 2370 IMPORTED_LIBRARIES, | |
| 2371 INCLUDED_PARTS, | |
| 2372 PARSE_ERRORS, | |
| 2373 PARSED_UNIT, | |
| 2374 SOURCE_KIND, | |
| 2375 UNITS | |
| 2376 ]); | |
| 2377 | |
| 2378 /** | |
| 2379 * Initialize a newly created task to parse the content of the Dart file | |
| 2380 * associated with the given [target] in the given [context]. | |
| 2381 */ | |
| 2382 ParseDartTask(InternalAnalysisContext context, AnalysisTarget target) | |
| 2383 : super(context, target); | |
| 2384 | |
| 2385 @override | |
| 2386 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 2387 | |
| 2388 @override | |
| 2389 void internalPerform() { | |
| 2390 Source source = getRequiredSource(); | |
| 2391 LineInfo lineInfo = getRequiredInput(LINE_INFO_INPUT_NAME); | |
| 2392 int modificationTime = getRequiredInput(MODIFICATION_TIME_INPUT_NAME); | |
| 2393 Token tokenStream = getRequiredInput(TOKEN_STREAM_INPUT_NAME); | |
| 2394 | |
| 2395 RecordingErrorListener errorListener = new RecordingErrorListener(); | |
| 2396 Parser parser = new Parser(source, errorListener); | |
| 2397 AnalysisOptions options = context.analysisOptions; | |
| 2398 parser.parseFunctionBodies = options.analyzeFunctionBodiesPredicate(source); | |
| 2399 parser.parseGenericMethods = options.enableGenericMethods; | |
| 2400 CompilationUnit unit = parser.parseCompilationUnit(tokenStream); | |
| 2401 unit.lineInfo = lineInfo; | |
| 2402 | |
| 2403 bool hasNonPartOfDirective = false; | |
| 2404 bool hasPartOfDirective = false; | |
| 2405 HashSet<Source> explicitlyImportedSourceSet = new HashSet<Source>(); | |
| 2406 HashSet<Source> exportedSourceSet = new HashSet<Source>(); | |
| 2407 HashSet<Source> includedSourceSet = new HashSet<Source>(); | |
| 2408 for (Directive directive in unit.directives) { | |
| 2409 if (directive is PartOfDirective) { | |
| 2410 hasPartOfDirective = true; | |
| 2411 } else { | |
| 2412 hasNonPartOfDirective = true; | |
| 2413 if (directive is UriBasedDirective) { | |
| 2414 Source referencedSource = | |
| 2415 resolveDirective(context, source, directive, errorListener); | |
| 2416 if (referencedSource != null) { | |
| 2417 if (directive is ExportDirective) { | |
| 2418 exportedSourceSet.add(referencedSource); | |
| 2419 } else if (directive is ImportDirective) { | |
| 2420 explicitlyImportedSourceSet.add(referencedSource); | |
| 2421 } else if (directive is PartDirective) { | |
| 2422 includedSourceSet.add(referencedSource); | |
| 2423 } else { | |
| 2424 throw new AnalysisException( | |
| 2425 '$runtimeType failed to handle a ${directive.runtimeType}'); | |
| 2426 } | |
| 2427 } | |
| 2428 } | |
| 2429 } | |
| 2430 } | |
| 2431 // | |
| 2432 // Always include "dart:core" source. | |
| 2433 // | |
| 2434 HashSet<Source> importedSourceSet = | |
| 2435 new HashSet.from(explicitlyImportedSourceSet); | |
| 2436 Source coreLibrarySource = context.sourceFactory.forUri(DartSdk.DART_CORE); | |
| 2437 importedSourceSet.add(coreLibrarySource); | |
| 2438 // | |
| 2439 // Compute kind. | |
| 2440 // | |
| 2441 SourceKind sourceKind = SourceKind.LIBRARY; | |
| 2442 if (modificationTime == -1) { | |
| 2443 sourceKind = SourceKind.UNKNOWN; | |
| 2444 } else if (hasPartOfDirective && !hasNonPartOfDirective) { | |
| 2445 sourceKind = SourceKind.PART; | |
| 2446 } | |
| 2447 // | |
| 2448 // Record outputs. | |
| 2449 // | |
| 2450 List<Source> explicitlyImportedSources = | |
| 2451 explicitlyImportedSourceSet.toList(); | |
| 2452 List<Source> exportedSources = exportedSourceSet.toList(); | |
| 2453 List<Source> importedSources = importedSourceSet.toList(); | |
| 2454 List<Source> includedSources = includedSourceSet.toList(); | |
| 2455 List<AnalysisError> parseErrors = | |
| 2456 removeDuplicateErrors(errorListener.errors); | |
| 2457 List<Source> unitSources = <Source>[source]..addAll(includedSourceSet); | |
| 2458 outputs[EXPLICITLY_IMPORTED_LIBRARIES] = explicitlyImportedSources; | |
| 2459 outputs[EXPORTED_LIBRARIES] = exportedSources; | |
| 2460 outputs[IMPORTED_LIBRARIES] = importedSources; | |
| 2461 outputs[INCLUDED_PARTS] = includedSources; | |
| 2462 outputs[PARSE_ERRORS] = parseErrors; | |
| 2463 outputs[PARSED_UNIT] = unit; | |
| 2464 outputs[SOURCE_KIND] = sourceKind; | |
| 2465 outputs[UNITS] = unitSources; | |
| 2466 } | |
| 2467 | |
| 2468 /** | |
| 2469 * Return a map from the names of the inputs of this kind of task to the task | |
| 2470 * input descriptors describing those inputs for a task with the given | |
| 2471 * [source]. | |
| 2472 */ | |
| 2473 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 2474 return <String, TaskInput>{ | |
| 2475 LINE_INFO_INPUT_NAME: LINE_INFO.of(target), | |
| 2476 MODIFICATION_TIME_INPUT_NAME: MODIFICATION_TIME.of(target), | |
| 2477 TOKEN_STREAM_INPUT_NAME: TOKEN_STREAM.of(target) | |
| 2478 }; | |
| 2479 } | |
| 2480 | |
| 2481 /** | |
| 2482 * Create a [ParseDartTask] based on the given [target] in the given | |
| 2483 * [context]. | |
| 2484 */ | |
| 2485 static ParseDartTask createTask( | |
| 2486 AnalysisContext context, AnalysisTarget target) { | |
| 2487 return new ParseDartTask(context, target); | |
| 2488 } | |
| 2489 | |
| 2490 /** | |
| 2491 * Return the result of resolving the URI of the given URI-based [directive] | |
| 2492 * against the URI of the given library, or `null` if the URI is not valid. | |
| 2493 * | |
| 2494 * Resolution is to be performed in the given [context]. Errors should be | |
| 2495 * reported to the [errorListener]. | |
| 2496 */ | |
| 2497 static Source resolveDirective(AnalysisContext context, Source librarySource, | |
| 2498 UriBasedDirective directive, AnalysisErrorListener errorListener) { | |
| 2499 StringLiteral uriLiteral = directive.uri; | |
| 2500 String uriContent = uriLiteral.stringValue; | |
| 2501 if (uriContent != null) { | |
| 2502 uriContent = uriContent.trim(); | |
| 2503 directive.uriContent = uriContent; | |
| 2504 } | |
| 2505 UriValidationCode code = directive.validate(); | |
| 2506 if (code == null) { | |
| 2507 String encodedUriContent = Uri.encodeFull(uriContent); | |
| 2508 Source source = | |
| 2509 context.sourceFactory.resolveUri(librarySource, encodedUriContent); | |
| 2510 directive.source = source; | |
| 2511 return source; | |
| 2512 } | |
| 2513 if (code == UriValidationCode.URI_WITH_DART_EXT_SCHEME) { | |
| 2514 return null; | |
| 2515 } | |
| 2516 if (code == UriValidationCode.URI_WITH_INTERPOLATION) { | |
| 2517 errorListener.onError(new AnalysisError(librarySource, uriLiteral.offset, | |
| 2518 uriLiteral.length, CompileTimeErrorCode.URI_WITH_INTERPOLATION)); | |
| 2519 return null; | |
| 2520 } | |
| 2521 if (code == UriValidationCode.INVALID_URI) { | |
| 2522 errorListener.onError(new AnalysisError(librarySource, uriLiteral.offset, | |
| 2523 uriLiteral.length, CompileTimeErrorCode.INVALID_URI, [uriContent])); | |
| 2524 return null; | |
| 2525 } | |
| 2526 throw new AnalysisException('Failed to handle validation code: $code'); | |
| 2527 } | |
| 2528 } | |
| 2529 | |
| 2530 /** | |
| 2531 * The helper for building the public [Namespace] of a [LibraryElement]. | |
| 2532 */ | |
| 2533 class PublicNamespaceBuilder { | |
| 2534 final HashMap<String, Element> definedNames = new HashMap<String, Element>(); | |
| 2535 | |
| 2536 /** | |
| 2537 * Build a public [Namespace] of the given [library]. | |
| 2538 */ | |
| 2539 Namespace build(LibraryElement library) { | |
| 2540 definedNames.clear(); | |
| 2541 _addPublicNames(library.definingCompilationUnit); | |
| 2542 library.parts.forEach(_addPublicNames); | |
| 2543 return new Namespace(definedNames); | |
| 2544 } | |
| 2545 | |
| 2546 /** | |
| 2547 * Add the given [element] if it has a publicly visible name. | |
| 2548 */ | |
| 2549 void _addIfPublic(Element element) { | |
| 2550 String name = element.name; | |
| 2551 if (name != null && !Scope.isPrivateName(name)) { | |
| 2552 definedNames[name] = element; | |
| 2553 } | |
| 2554 } | |
| 2555 | |
| 2556 /** | |
| 2557 * Add all of the public top-level names that are defined in the given | |
| 2558 * [compilationUnit]. | |
| 2559 */ | |
| 2560 void _addPublicNames(CompilationUnitElement compilationUnit) { | |
| 2561 compilationUnit.accessors.forEach(_addIfPublic); | |
| 2562 compilationUnit.enums.forEach(_addIfPublic); | |
| 2563 compilationUnit.functions.forEach(_addIfPublic); | |
| 2564 compilationUnit.functionTypeAliases.forEach(_addIfPublic); | |
| 2565 compilationUnit.types.forEach(_addIfPublic); | |
| 2566 } | |
| 2567 } | |
| 2568 | |
| 2569 /** | |
| 2570 * Information about a library - which names it uses, which names it defines | |
| 2571 * with their externally visible dependencies. | |
| 2572 */ | |
| 2573 class ReferencedNames { | |
| 2574 final Set<String> names = new Set<String>(); | |
| 2575 final Map<String, Set<String>> userToDependsOn = <String, Set<String>>{}; | |
| 2576 | |
| 2577 /** | |
| 2578 * Updates [delta] by adding names that are changed in this library. | |
| 2579 */ | |
| 2580 void addChangedElements(DartDelta delta) { | |
| 2581 bool hasProgress = true; | |
| 2582 while (hasProgress) { | |
| 2583 hasProgress = false; | |
| 2584 userToDependsOn.forEach((user, dependencies) { | |
| 2585 for (String dependency in dependencies) { | |
| 2586 if (delta.isNameAffected(dependency)) { | |
| 2587 if (delta.nameChanged(user)) { | |
| 2588 hasProgress = true; | |
| 2589 } | |
| 2590 } | |
| 2591 } | |
| 2592 }); | |
| 2593 } | |
| 2594 } | |
| 2595 | |
| 2596 /** | |
| 2597 * Returns `true` if the library described by this object is affected by | |
| 2598 * the given [delta]. | |
| 2599 */ | |
| 2600 bool isAffectedBy(DartDelta delta) { | |
| 2601 for (String name in names) { | |
| 2602 if (delta.isNameAffected(name)) { | |
| 2603 return true; | |
| 2604 } | |
| 2605 } | |
| 2606 return false; | |
| 2607 } | |
| 2608 } | |
| 2609 | |
| 2610 /** | |
| 2611 * A builder for creating [ReferencedNames]. | |
| 2612 * | |
| 2613 * TODO(scheglov) Record dependencies for all other top-level declarations. | |
| 2614 */ | |
| 2615 class ReferencedNamesBuilder extends RecursiveAstVisitor { | |
| 2616 final ReferencedNames names; | |
| 2617 int bodyLevel = 0; | |
| 2618 Set<String> dependsOn; | |
| 2619 | |
| 2620 ReferencedNamesBuilder(this.names); | |
| 2621 | |
| 2622 ReferencedNames build(CompilationUnit unit) { | |
| 2623 unit.accept(this); | |
| 2624 return names; | |
| 2625 } | |
| 2626 | |
| 2627 @override | |
| 2628 visitBlockFunctionBody(BlockFunctionBody node) { | |
| 2629 try { | |
| 2630 bodyLevel++; | |
| 2631 super.visitBlockFunctionBody(node); | |
| 2632 } finally { | |
| 2633 bodyLevel--; | |
| 2634 } | |
| 2635 } | |
| 2636 | |
| 2637 @override | |
| 2638 visitClassDeclaration(ClassDeclaration node) { | |
| 2639 dependsOn = new Set<String>(); | |
| 2640 super.visitClassDeclaration(node); | |
| 2641 names.userToDependsOn[node.name.name] = dependsOn; | |
| 2642 dependsOn = null; | |
| 2643 } | |
| 2644 | |
| 2645 @override | |
| 2646 visitExpressionFunctionBody(ExpressionFunctionBody node) { | |
| 2647 try { | |
| 2648 bodyLevel++; | |
| 2649 super.visitExpressionFunctionBody(node); | |
| 2650 } finally { | |
| 2651 bodyLevel--; | |
| 2652 } | |
| 2653 } | |
| 2654 | |
| 2655 @override | |
| 2656 visitSimpleIdentifier(SimpleIdentifier node) { | |
| 2657 if (!node.inDeclarationContext()) { | |
| 2658 String name = node.name; | |
| 2659 names.names.add(name); | |
| 2660 if (dependsOn != null && bodyLevel == 0) { | |
| 2661 dependsOn.add(name); | |
| 2662 } | |
| 2663 } | |
| 2664 } | |
| 2665 } | |
| 2666 | |
| 2667 /** | |
| 2668 * A task that finishes resolution by requesting [RESOLVED_UNIT_NO_CONSTANTS] fo
r every | |
| 2669 * unit in the libraries closure and produces [LIBRARY_ELEMENT]. | |
| 2670 */ | |
| 2671 class ResolveLibraryReferencesTask extends SourceBasedAnalysisTask { | |
| 2672 /** | |
| 2673 * The name of the [LIBRARY_ELEMENT5] input. | |
| 2674 */ | |
| 2675 static const String LIBRARY_INPUT = 'LIBRARY_INPUT'; | |
| 2676 | |
| 2677 /** | |
| 2678 * The name of the list of [RESOLVED_UNIT5] input. | |
| 2679 */ | |
| 2680 static const String UNITS_INPUT = 'UNITS_INPUT'; | |
| 2681 | |
| 2682 /** | |
| 2683 * The task descriptor describing this kind of task. | |
| 2684 */ | |
| 2685 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 2686 'ResolveLibraryReferencesTask', createTask, buildInputs, | |
| 2687 <ResultDescriptor>[LIBRARY_ELEMENT, REFERENCED_NAMES]); | |
| 2688 | |
| 2689 ResolveLibraryReferencesTask( | |
| 2690 InternalAnalysisContext context, AnalysisTarget target) | |
| 2691 : super(context, target); | |
| 2692 | |
| 2693 @override | |
| 2694 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 2695 | |
| 2696 @override | |
| 2697 void internalPerform() { | |
| 2698 // | |
| 2699 // Prepare inputs. | |
| 2700 // | |
| 2701 LibraryElement library = getRequiredInput(LIBRARY_INPUT); | |
| 2702 List<CompilationUnit> units = getRequiredInput(UNITS_INPUT); | |
| 2703 // Compute referenced names. | |
| 2704 ReferencedNames referencedNames = new ReferencedNames(); | |
| 2705 for (CompilationUnit unit in units) { | |
| 2706 new ReferencedNamesBuilder(referencedNames).build(unit); | |
| 2707 } | |
| 2708 // | |
| 2709 // Record outputs. | |
| 2710 // | |
| 2711 outputs[LIBRARY_ELEMENT] = library; | |
| 2712 outputs[REFERENCED_NAMES] = referencedNames; | |
| 2713 } | |
| 2714 | |
| 2715 /** | |
| 2716 * Return a map from the names of the inputs of this kind of task to the task | |
| 2717 * input descriptors describing those inputs for a task with the | |
| 2718 * given [target]. | |
| 2719 */ | |
| 2720 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 2721 Source source = target; | |
| 2722 return <String, TaskInput>{ | |
| 2723 LIBRARY_INPUT: LIBRARY_ELEMENT5.of(source), | |
| 2724 UNITS_INPUT: UNITS.of(source).toList((Source unit) => | |
| 2725 RESOLVED_UNIT5.of(new LibrarySpecificUnit(source, unit))), | |
| 2726 'resolvedUnits': IMPORT_EXPORT_SOURCE_CLOSURE | |
| 2727 .of(source) | |
| 2728 .toMapOf(UNITS) | |
| 2729 .toFlattenList((Source library, Source unit) => | |
| 2730 RESOLVED_UNIT5.of(new LibrarySpecificUnit(library, unit))), | |
| 2731 }; | |
| 2732 } | |
| 2733 | |
| 2734 /** | |
| 2735 * Create a [ResolveLibraryReferencesTask] based on the given [target] in | |
| 2736 * the given [context]. | |
| 2737 */ | |
| 2738 static ResolveLibraryReferencesTask createTask( | |
| 2739 AnalysisContext context, AnalysisTarget target) { | |
| 2740 return new ResolveLibraryReferencesTask(context, target); | |
| 2741 } | |
| 2742 } | |
| 2743 | |
| 2744 /** | |
| 2745 * An artifitial task that does nothing except to force type names resolution | |
| 2746 * for the defining and part units of a library. | |
| 2747 */ | |
| 2748 class ResolveLibraryTypeNamesTask extends SourceBasedAnalysisTask { | |
| 2749 /** | |
| 2750 * The name of the [LIBRARY_ELEMENT4] input. | |
| 2751 */ | |
| 2752 static const String LIBRARY_INPUT = 'LIBRARY_INPUT'; | |
| 2753 | |
| 2754 /** | |
| 2755 * The task descriptor describing this kind of task. | |
| 2756 */ | |
| 2757 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 2758 'ResolveLibraryTypeNamesTask', createTask, buildInputs, | |
| 2759 <ResultDescriptor>[LIBRARY_ELEMENT5]); | |
| 2760 | |
| 2761 ResolveLibraryTypeNamesTask( | |
| 2762 InternalAnalysisContext context, AnalysisTarget target) | |
| 2763 : super(context, target); | |
| 2764 | |
| 2765 @override | |
| 2766 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 2767 | |
| 2768 @override | |
| 2769 void internalPerform() { | |
| 2770 LibraryElement library = getRequiredInput(LIBRARY_INPUT); | |
| 2771 outputs[LIBRARY_ELEMENT5] = library; | |
| 2772 } | |
| 2773 | |
| 2774 /** | |
| 2775 * Return a map from the names of the inputs of this kind of task to the task | |
| 2776 * input descriptors describing those inputs for a task with the | |
| 2777 * given [target]. | |
| 2778 */ | |
| 2779 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 2780 Source source = target; | |
| 2781 return <String, TaskInput>{ | |
| 2782 'resolvedUnit': UNITS.of(source).toList((Source unit) => | |
| 2783 RESOLVED_UNIT3.of(new LibrarySpecificUnit(source, unit))), | |
| 2784 LIBRARY_INPUT: LIBRARY_ELEMENT4.of(source) | |
| 2785 }; | |
| 2786 } | |
| 2787 | |
| 2788 /** | |
| 2789 * Create a [ResolveLibraryTypeNamesTask] based on the given [target] in | |
| 2790 * the given [context]. | |
| 2791 */ | |
| 2792 static ResolveLibraryTypeNamesTask createTask( | |
| 2793 AnalysisContext context, AnalysisTarget target) { | |
| 2794 return new ResolveLibraryTypeNamesTask(context, target); | |
| 2795 } | |
| 2796 } | |
| 2797 | |
| 2798 /** | |
| 2799 * A task that builds [RESOLVED_UNIT5] for a unit. | |
| 2800 */ | |
| 2801 class ResolveUnitReferencesTask extends SourceBasedAnalysisTask { | |
| 2802 /** | |
| 2803 * The name of the [LIBRARY_ELEMENT5] input. | |
| 2804 */ | |
| 2805 static const String LIBRARY_INPUT = 'LIBRARY_INPUT'; | |
| 2806 | |
| 2807 /** | |
| 2808 * The name of the [RESOLVED_UNIT4] input. | |
| 2809 */ | |
| 2810 static const String UNIT_INPUT = 'UNIT_INPUT'; | |
| 2811 | |
| 2812 /** | |
| 2813 * The name of the [TYPE_PROVIDER] input. | |
| 2814 */ | |
| 2815 static const String TYPE_PROVIDER_INPUT = 'TYPE_PROVIDER_INPUT'; | |
| 2816 | |
| 2817 /** | |
| 2818 * The task descriptor describing this kind of task. | |
| 2819 */ | |
| 2820 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 2821 'ResolveUnitReferencesTask', createTask, buildInputs, <ResultDescriptor>[ | |
| 2822 RESOLVE_REFERENCES_ERRORS, | |
| 2823 RESOLVED_UNIT5 | |
| 2824 ]); | |
| 2825 | |
| 2826 ResolveUnitReferencesTask( | |
| 2827 InternalAnalysisContext context, AnalysisTarget target) | |
| 2828 : super(context, target); | |
| 2829 | |
| 2830 @override | |
| 2831 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 2832 | |
| 2833 @override | |
| 2834 void internalPerform() { | |
| 2835 RecordingErrorListener errorListener = new RecordingErrorListener(); | |
| 2836 // | |
| 2837 // Prepare inputs. | |
| 2838 // | |
| 2839 LibraryElement libraryElement = getRequiredInput(LIBRARY_INPUT); | |
| 2840 CompilationUnit unit = getRequiredInput(UNIT_INPUT); | |
| 2841 CompilationUnitElement unitElement = unit.element; | |
| 2842 TypeProvider typeProvider = getRequiredInput(TYPE_PROVIDER_INPUT); | |
| 2843 // | |
| 2844 // Resolve references. | |
| 2845 // | |
| 2846 InheritanceManager inheritanceManager = | |
| 2847 new InheritanceManager(libraryElement); | |
| 2848 AstVisitor visitor = new ResolverVisitor( | |
| 2849 libraryElement, unitElement.source, typeProvider, errorListener, | |
| 2850 inheritanceManager: inheritanceManager); | |
| 2851 unit.accept(visitor); | |
| 2852 // | |
| 2853 // Record outputs. | |
| 2854 // | |
| 2855 outputs[RESOLVE_REFERENCES_ERRORS] = | |
| 2856 removeDuplicateErrors(errorListener.errors); | |
| 2857 outputs[RESOLVED_UNIT5] = unit; | |
| 2858 } | |
| 2859 | |
| 2860 /** | |
| 2861 * Return a map from the names of the inputs of this kind of task to the task | |
| 2862 * input descriptors describing those inputs for a task with the | |
| 2863 * given [target]. | |
| 2864 */ | |
| 2865 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 2866 LibrarySpecificUnit unit = target; | |
| 2867 return <String, TaskInput>{ | |
| 2868 'fullyBuiltLibraryElements': IMPORT_EXPORT_SOURCE_CLOSURE | |
| 2869 .of(unit.library) | |
| 2870 .toListOf(LIBRARY_ELEMENT5), | |
| 2871 LIBRARY_INPUT: LIBRARY_ELEMENT5.of(unit.library), | |
| 2872 UNIT_INPUT: RESOLVED_UNIT4.of(unit), | |
| 2873 TYPE_PROVIDER_INPUT: TYPE_PROVIDER.of(AnalysisContextTarget.request) | |
| 2874 }; | |
| 2875 } | |
| 2876 | |
| 2877 /** | |
| 2878 * Create a [ResolveUnitReferencesTask] based on the given [target] in | |
| 2879 * the given [context]. | |
| 2880 */ | |
| 2881 static ResolveUnitReferencesTask createTask( | |
| 2882 AnalysisContext context, AnalysisTarget target) { | |
| 2883 return new ResolveUnitReferencesTask(context, target); | |
| 2884 } | |
| 2885 } | |
| 2886 | |
| 2887 /** | |
| 2888 * A task that builds [RESOLVED_UNIT3] for a unit. | |
| 2889 */ | |
| 2890 class ResolveUnitTypeNamesTask extends SourceBasedAnalysisTask { | |
| 2891 /** | |
| 2892 * The name of the input whose value is the defining [LIBRARY_ELEMENT4]. | |
| 2893 */ | |
| 2894 static const String LIBRARY_INPUT = 'LIBRARY_INPUT'; | |
| 2895 | |
| 2896 /** | |
| 2897 * The name of the [RESOLVED_UNIT2] input. | |
| 2898 */ | |
| 2899 static const String UNIT_INPUT = 'UNIT_INPUT'; | |
| 2900 | |
| 2901 /** | |
| 2902 * The name of the [TYPE_PROVIDER] input. | |
| 2903 */ | |
| 2904 static const String TYPE_PROVIDER_INPUT = 'TYPE_PROVIDER_INPUT'; | |
| 2905 | |
| 2906 /** | |
| 2907 * The task descriptor describing this kind of task. | |
| 2908 */ | |
| 2909 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 2910 'ResolveUnitTypeNamesTask', createTask, buildInputs, <ResultDescriptor>[ | |
| 2911 RESOLVE_TYPE_NAMES_ERRORS, | |
| 2912 RESOLVED_UNIT3 | |
| 2913 ]); | |
| 2914 | |
| 2915 ResolveUnitTypeNamesTask( | |
| 2916 InternalAnalysisContext context, AnalysisTarget target) | |
| 2917 : super(context, target); | |
| 2918 | |
| 2919 @override | |
| 2920 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 2921 | |
| 2922 @override | |
| 2923 void internalPerform() { | |
| 2924 RecordingErrorListener errorListener = new RecordingErrorListener(); | |
| 2925 // | |
| 2926 // Prepare inputs. | |
| 2927 // | |
| 2928 LibraryElement library = getRequiredInput(LIBRARY_INPUT); | |
| 2929 CompilationUnit unit = getRequiredInput(UNIT_INPUT); | |
| 2930 CompilationUnitElement unitElement = unit.element; | |
| 2931 TypeProvider typeProvider = getRequiredInput(TYPE_PROVIDER_INPUT); | |
| 2932 // | |
| 2933 // Resolve TypeName nodes. | |
| 2934 // | |
| 2935 TypeResolverVisitor visitor = new TypeResolverVisitor( | |
| 2936 library, unitElement.source, typeProvider, errorListener); | |
| 2937 unit.accept(visitor); | |
| 2938 // | |
| 2939 // Record outputs. | |
| 2940 // | |
| 2941 outputs[RESOLVE_TYPE_NAMES_ERRORS] = | |
| 2942 removeDuplicateErrors(errorListener.errors); | |
| 2943 outputs[RESOLVED_UNIT3] = unit; | |
| 2944 } | |
| 2945 | |
| 2946 /** | |
| 2947 * Return a map from the names of the inputs of this kind of task to the task | |
| 2948 * input descriptors describing those inputs for a task with the | |
| 2949 * given [target]. | |
| 2950 */ | |
| 2951 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 2952 LibrarySpecificUnit unit = target; | |
| 2953 return <String, TaskInput>{ | |
| 2954 'importsExportNamespace': | |
| 2955 IMPORTED_LIBRARIES.of(unit.library).toMapOf(LIBRARY_ELEMENT4), | |
| 2956 LIBRARY_INPUT: LIBRARY_ELEMENT4.of(unit.library), | |
| 2957 UNIT_INPUT: RESOLVED_UNIT2.of(unit), | |
| 2958 TYPE_PROVIDER_INPUT: TYPE_PROVIDER.of(AnalysisContextTarget.request) | |
| 2959 }; | |
| 2960 } | |
| 2961 | |
| 2962 /** | |
| 2963 * Create a [ResolveUnitTypeNamesTask] based on the given [target] in | |
| 2964 * the given [context]. | |
| 2965 */ | |
| 2966 static ResolveUnitTypeNamesTask createTask( | |
| 2967 AnalysisContext context, AnalysisTarget target) { | |
| 2968 return new ResolveUnitTypeNamesTask(context, target); | |
| 2969 } | |
| 2970 } | |
| 2971 | |
| 2972 /** | |
| 2973 * A task that builds [RESOLVED_UNIT4] for a unit. | |
| 2974 */ | |
| 2975 class ResolveVariableReferencesTask extends SourceBasedAnalysisTask { | |
| 2976 /** | |
| 2977 * The name of the [LIBRARY_ELEMENT1] input. | |
| 2978 */ | |
| 2979 static const String LIBRARY_INPUT = 'LIBRARY_INPUT'; | |
| 2980 | |
| 2981 /** | |
| 2982 * The name of the [RESOLVED_UNIT3] input. | |
| 2983 */ | |
| 2984 static const String UNIT_INPUT = 'UNIT_INPUT'; | |
| 2985 | |
| 2986 /** | |
| 2987 * The name of the [TYPE_PROVIDER] input. | |
| 2988 */ | |
| 2989 static const String TYPE_PROVIDER_INPUT = 'TYPE_PROVIDER_INPUT'; | |
| 2990 | |
| 2991 /** | |
| 2992 * The task descriptor describing this kind of task. | |
| 2993 */ | |
| 2994 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | |
| 2995 'ResolveVariableReferencesTask', createTask, buildInputs, | |
| 2996 <ResultDescriptor>[RESOLVED_UNIT4, VARIABLE_REFERENCE_ERRORS]); | |
| 2997 | |
| 2998 ResolveVariableReferencesTask( | |
| 2999 InternalAnalysisContext context, AnalysisTarget target) | |
| 3000 : super(context, target); | |
| 3001 | |
| 3002 @override | |
| 3003 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 3004 | |
| 3005 @override | |
| 3006 void internalPerform() { | |
| 3007 RecordingErrorListener errorListener = new RecordingErrorListener(); | |
| 3008 // | |
| 3009 // Prepare inputs. | |
| 3010 // | |
| 3011 LibraryElement libraryElement = getRequiredInput(LIBRARY_INPUT); | |
| 3012 CompilationUnit unit = getRequiredInput(UNIT_INPUT); | |
| 3013 CompilationUnitElement unitElement = unit.element; | |
| 3014 // | |
| 3015 // Resolve local variables. | |
| 3016 // | |
| 3017 TypeProvider typeProvider = getRequiredInput(TYPE_PROVIDER_INPUT); | |
| 3018 Scope nameScope = new LibraryScope(libraryElement, errorListener); | |
| 3019 AstVisitor visitor = new VariableResolverVisitor( | |
| 3020 libraryElement, unitElement.source, typeProvider, errorListener, | |
| 3021 nameScope: nameScope); | |
| 3022 unit.accept(visitor); | |
| 3023 // | |
| 3024 // Record outputs. | |
| 3025 // | |
| 3026 outputs[RESOLVED_UNIT4] = unit; | |
| 3027 outputs[VARIABLE_REFERENCE_ERRORS] = | |
| 3028 removeDuplicateErrors(errorListener.errors); | |
| 3029 } | |
| 3030 | |
| 3031 /** | |
| 3032 * Return a map from the names of the inputs of this kind of task to the task | |
| 3033 * input descriptors describing those inputs for a task with the | |
| 3034 * given [target]. | |
| 3035 */ | |
| 3036 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 3037 LibrarySpecificUnit unit = target; | |
| 3038 return <String, TaskInput>{ | |
| 3039 LIBRARY_INPUT: LIBRARY_ELEMENT1.of(unit.library), | |
| 3040 UNIT_INPUT: RESOLVED_UNIT1.of(unit), | |
| 3041 TYPE_PROVIDER_INPUT: TYPE_PROVIDER.of(AnalysisContextTarget.request) | |
| 3042 }; | |
| 3043 } | |
| 3044 | |
| 3045 /** | |
| 3046 * Create a [ResolveVariableReferencesTask] based on the given [target] in | |
| 3047 * the given [context]. | |
| 3048 */ | |
| 3049 static ResolveVariableReferencesTask createTask( | |
| 3050 AnalysisContext context, AnalysisTarget target) { | |
| 3051 return new ResolveVariableReferencesTask(context, target); | |
| 3052 } | |
| 3053 } | |
| 3054 | |
| 3055 /** | |
| 3056 * A task that scans the content of a file, producing a set of Dart tokens. | |
| 3057 */ | |
| 3058 class ScanDartTask extends SourceBasedAnalysisTask { | |
| 3059 /** | |
| 3060 * The name of the input whose value is the content of the file. | |
| 3061 */ | |
| 3062 static const String CONTENT_INPUT_NAME = 'CONTENT_INPUT_NAME'; | |
| 3063 | |
| 3064 /** | |
| 3065 * The task descriptor describing this kind of task. | |
| 3066 */ | |
| 3067 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor('ScanDartTask', | |
| 3068 createTask, buildInputs, <ResultDescriptor>[ | |
| 3069 LINE_INFO, | |
| 3070 SCAN_ERRORS, | |
| 3071 TOKEN_STREAM | |
| 3072 ]); | |
| 3073 | |
| 3074 /** | |
| 3075 * Initialize a newly created task to access the content of the source | |
| 3076 * associated with the given [target] in the given [context]. | |
| 3077 */ | |
| 3078 ScanDartTask(InternalAnalysisContext context, AnalysisTarget target) | |
| 3079 : super(context, target); | |
| 3080 | |
| 3081 @override | |
| 3082 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 3083 | |
| 3084 @override | |
| 3085 void internalPerform() { | |
| 3086 Source source = getRequiredSource(); | |
| 3087 | |
| 3088 RecordingErrorListener errorListener = new RecordingErrorListener(); | |
| 3089 if (context.getModificationStamp(target.source) < 0) { | |
| 3090 String message = 'Content could not be read'; | |
| 3091 if (context is InternalAnalysisContext) { | |
| 3092 CacheEntry entry = | |
| 3093 (context as InternalAnalysisContext).getCacheEntry(target); | |
| 3094 CaughtException exception = entry.exception; | |
| 3095 if (exception != null) { | |
| 3096 message = exception.toString(); | |
| 3097 } | |
| 3098 } | |
| 3099 errorListener.onError(new AnalysisError( | |
| 3100 source, 0, 0, ScannerErrorCode.UNABLE_GET_CONTENT, [message])); | |
| 3101 } | |
| 3102 if (target is DartScript) { | |
| 3103 DartScript script = target; | |
| 3104 List<ScriptFragment> fragments = script.fragments; | |
| 3105 if (fragments.length < 1) { | |
| 3106 throw new AnalysisException('Cannot scan scripts with no fragments'); | |
| 3107 } else if (fragments.length > 1) { | |
| 3108 throw new AnalysisException( | |
| 3109 'Cannot scan scripts with multiple fragments'); | |
| 3110 } | |
| 3111 ScriptFragment fragment = fragments[0]; | |
| 3112 | |
| 3113 Scanner scanner = new Scanner(source, | |
| 3114 new SubSequenceReader(fragment.content, fragment.offset), | |
| 3115 errorListener); | |
| 3116 scanner.setSourceStart(fragment.line, fragment.column); | |
| 3117 scanner.preserveComments = context.analysisOptions.preserveComments; | |
| 3118 | |
| 3119 outputs[TOKEN_STREAM] = scanner.tokenize(); | |
| 3120 outputs[LINE_INFO] = new LineInfo(scanner.lineStarts); | |
| 3121 outputs[SCAN_ERRORS] = removeDuplicateErrors(errorListener.errors); | |
| 3122 } else if (target is Source) { | |
| 3123 String content = getRequiredInput(CONTENT_INPUT_NAME); | |
| 3124 | |
| 3125 Scanner scanner = | |
| 3126 new Scanner(source, new CharSequenceReader(content), errorListener); | |
| 3127 scanner.preserveComments = context.analysisOptions.preserveComments; | |
| 3128 | |
| 3129 outputs[TOKEN_STREAM] = scanner.tokenize(); | |
| 3130 outputs[LINE_INFO] = new LineInfo(scanner.lineStarts); | |
| 3131 outputs[SCAN_ERRORS] = removeDuplicateErrors(errorListener.errors); | |
| 3132 } else { | |
| 3133 throw new AnalysisException( | |
| 3134 'Cannot scan Dart code from a ${target.runtimeType}'); | |
| 3135 } | |
| 3136 } | |
| 3137 | |
| 3138 /** | |
| 3139 * Return a map from the names of the inputs of this kind of task to the task | |
| 3140 * input descriptors describing those inputs for a task with the given | |
| 3141 * [source]. | |
| 3142 */ | |
| 3143 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 3144 if (target is Source) { | |
| 3145 return <String, TaskInput>{CONTENT_INPUT_NAME: CONTENT.of(target)}; | |
| 3146 } else if (target is DartScript) { | |
| 3147 // This task does not use the following input; it is included only to add | |
| 3148 // a dependency between this value and the containing source so that when | |
| 3149 // the containing source is modified these results will be invalidated. | |
| 3150 return <String, TaskInput>{'-': DART_SCRIPTS.of(target.source)}; | |
| 3151 } | |
| 3152 throw new AnalysisException( | |
| 3153 'Cannot build inputs for a ${target.runtimeType}'); | |
| 3154 } | |
| 3155 | |
| 3156 /** | |
| 3157 * Create a [ScanDartTask] based on the given [target] in the given [context]. | |
| 3158 */ | |
| 3159 static ScanDartTask createTask( | |
| 3160 AnalysisContext context, AnalysisTarget target) { | |
| 3161 return new ScanDartTask(context, target); | |
| 3162 } | |
| 3163 } | |
| 3164 | |
| 3165 /** | |
| 3166 * A task that builds [VERIFY_ERRORS] for a unit. | |
| 3167 */ | |
| 3168 class VerifyUnitTask extends SourceBasedAnalysisTask { | |
| 3169 /** | |
| 3170 * The name of the [RESOLVED_UNIT] input. | |
| 3171 */ | |
| 3172 static const String UNIT_INPUT = 'UNIT_INPUT'; | |
| 3173 | |
| 3174 /** | |
| 3175 * The name of the [TYPE_PROVIDER] input. | |
| 3176 */ | |
| 3177 static const String TYPE_PROVIDER_INPUT = 'TYPE_PROVIDER_INPUT'; | |
| 3178 | |
| 3179 /** | |
| 3180 * The task descriptor describing this kind of task. | |
| 3181 */ | |
| 3182 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor('VerifyUnitTask', | |
| 3183 createTask, buildInputs, <ResultDescriptor>[VERIFY_ERRORS]); | |
| 3184 | |
| 3185 /** | |
| 3186 * The [ErrorReporter] to report errors to. | |
| 3187 */ | |
| 3188 ErrorReporter errorReporter; | |
| 3189 | |
| 3190 VerifyUnitTask(InternalAnalysisContext context, AnalysisTarget target) | |
| 3191 : super(context, target); | |
| 3192 | |
| 3193 @override | |
| 3194 TaskDescriptor get descriptor => DESCRIPTOR; | |
| 3195 | |
| 3196 @override | |
| 3197 void internalPerform() { | |
| 3198 RecordingErrorListener errorListener = new RecordingErrorListener(); | |
| 3199 Source source = getRequiredSource(); | |
| 3200 errorReporter = new ErrorReporter(errorListener, source); | |
| 3201 // | |
| 3202 // Prepare inputs. | |
| 3203 // | |
| 3204 TypeProvider typeProvider = getRequiredInput(TYPE_PROVIDER_INPUT); | |
| 3205 CompilationUnit unit = getRequiredInput(UNIT_INPUT); | |
| 3206 CompilationUnitElement unitElement = unit.element; | |
| 3207 LibraryElement libraryElement = unitElement.library; | |
| 3208 // | |
| 3209 // Validate the directives. | |
| 3210 // | |
| 3211 validateDirectives(unit); | |
| 3212 // | |
| 3213 // Use the ConstantVerifier to compute errors. | |
| 3214 // | |
| 3215 ConstantVerifier constantVerifier = new ConstantVerifier( | |
| 3216 errorReporter, libraryElement, typeProvider, context.declaredVariables); | |
| 3217 unit.accept(constantVerifier); | |
| 3218 // | |
| 3219 // Use the ErrorVerifier to compute errors. | |
| 3220 // | |
| 3221 ErrorVerifier errorVerifier = new ErrorVerifier(errorReporter, | |
| 3222 libraryElement, typeProvider, new InheritanceManager(libraryElement)); | |
| 3223 unit.accept(errorVerifier); | |
| 3224 // | |
| 3225 // Record outputs. | |
| 3226 // | |
| 3227 outputs[VERIFY_ERRORS] = removeDuplicateErrors(errorListener.errors); | |
| 3228 } | |
| 3229 | |
| 3230 /** | |
| 3231 * Check each directive in the given [unit] to see if the referenced source | |
| 3232 * exists and report an error if it does not. | |
| 3233 */ | |
| 3234 void validateDirectives(CompilationUnit unit) { | |
| 3235 for (Directive directive in unit.directives) { | |
| 3236 if (directive is UriBasedDirective) { | |
| 3237 validateReferencedSource(directive); | |
| 3238 } | |
| 3239 } | |
| 3240 } | |
| 3241 | |
| 3242 /** | |
| 3243 * Check the given [directive] to see if the referenced source exists and | |
| 3244 * report an error if it does not. | |
| 3245 */ | |
| 3246 void validateReferencedSource(UriBasedDirective directive) { | |
| 3247 Source source = directive.source; | |
| 3248 if (source != null) { | |
| 3249 if (context.exists(source)) { | |
| 3250 return; | |
| 3251 } | |
| 3252 } else { | |
| 3253 // Don't report errors already reported by ParseDartTask.resolveDirective | |
| 3254 if (directive.validate() != null) { | |
| 3255 return; | |
| 3256 } | |
| 3257 } | |
| 3258 StringLiteral uriLiteral = directive.uri; | |
| 3259 errorReporter.reportErrorForNode(CompileTimeErrorCode.URI_DOES_NOT_EXIST, | |
| 3260 uriLiteral, [directive.uriContent]); | |
| 3261 } | |
| 3262 | |
| 3263 /** | |
| 3264 * Return a map from the names of the inputs of this kind of task to the task | |
| 3265 * input descriptors describing those inputs for a task with the | |
| 3266 * given [target]. | |
| 3267 */ | |
| 3268 static Map<String, TaskInput> buildInputs(AnalysisTarget target) { | |
| 3269 LibrarySpecificUnit unit = target; | |
| 3270 return <String, TaskInput>{ | |
| 3271 'resolvedUnits': IMPORT_EXPORT_SOURCE_CLOSURE | |
| 3272 .of(unit.library) | |
| 3273 .toMapOf(UNITS) | |
| 3274 .toFlattenList((Source library, Source unit) => | |
| 3275 RESOLVED_UNIT.of(new LibrarySpecificUnit(library, unit))), | |
| 3276 UNIT_INPUT: RESOLVED_UNIT.of(unit), | |
| 3277 TYPE_PROVIDER_INPUT: TYPE_PROVIDER.of(AnalysisContextTarget.request) | |
| 3278 }; | |
| 3279 } | |
| 3280 | |
| 3281 /** | |
| 3282 * Create a [VerifyUnitTask] based on the given [target] in | |
| 3283 * the given [context]. | |
| 3284 */ | |
| 3285 static VerifyUnitTask createTask( | |
| 3286 AnalysisContext context, AnalysisTarget target) { | |
| 3287 return new VerifyUnitTask(context, target); | |
| 3288 } | |
| 3289 } | |
| 3290 | |
| 3291 /** | |
| 3292 * A [TaskInput] whose value is a list of library sources exported directly | |
| 3293 * or indirectly by the target [Source]. | |
| 3294 * | |
| 3295 * [resultDescriptor] is the type of result which should be produced for each | |
| 3296 * target [Source]. | |
| 3297 */ | |
| 3298 class _ExportSourceClosureTaskInput extends TaskInputImpl<List<Source>> { | |
| 3299 final Source target; | |
| 3300 final ResultDescriptor resultDescriptor; | |
| 3301 | |
| 3302 _ExportSourceClosureTaskInput(this.target, this.resultDescriptor); | |
| 3303 | |
| 3304 @override | |
| 3305 TaskInputBuilder<List<Source>> createBuilder() => | |
| 3306 new _SourceClosureTaskInputBuilder( | |
| 3307 target, _SourceClosureKind.EXPORT, resultDescriptor); | |
| 3308 } | |
| 3309 | |
| 3310 /** | |
| 3311 * A [TaskInput] whose value is a list of library sources imported or exported, | |
| 3312 * directly or indirectly by the target [Source]. | |
| 3313 * | |
| 3314 * [resultDescriptor] is the type of result which should be produced for each | |
| 3315 * target [Source]. | |
| 3316 */ | |
| 3317 class _ImportExportSourceClosureTaskInput extends TaskInputImpl<List<Source>> { | |
| 3318 final Source target; | |
| 3319 final ResultDescriptor resultDescriptor; | |
| 3320 | |
| 3321 _ImportExportSourceClosureTaskInput(this.target, this.resultDescriptor); | |
| 3322 | |
| 3323 @override | |
| 3324 TaskInputBuilder<List<Source>> createBuilder() => | |
| 3325 new _SourceClosureTaskInputBuilder( | |
| 3326 target, _SourceClosureKind.IMPORT_EXPORT, resultDescriptor); | |
| 3327 } | |
| 3328 | |
| 3329 /** | |
| 3330 * A [TaskInput] whose value is a list of library sources imported directly | |
| 3331 * or indirectly by the target [Source]. | |
| 3332 * | |
| 3333 * [resultDescriptor] is the type of result which should be produced for each | |
| 3334 * target [Source]. | |
| 3335 */ | |
| 3336 class _ImportSourceClosureTaskInput extends TaskInputImpl<List<Source>> { | |
| 3337 final Source target; | |
| 3338 final ResultDescriptor resultDescriptor; | |
| 3339 | |
| 3340 _ImportSourceClosureTaskInput(this.target, this.resultDescriptor); | |
| 3341 | |
| 3342 @override | |
| 3343 TaskInputBuilder<List<Source>> createBuilder() => | |
| 3344 new _SourceClosureTaskInputBuilder( | |
| 3345 target, _SourceClosureKind.IMPORT, resultDescriptor); | |
| 3346 } | |
| 3347 | |
| 3348 /** | |
| 3349 * The kind of the source closure to build. | |
| 3350 */ | |
| 3351 enum _SourceClosureKind { IMPORT, EXPORT, IMPORT_EXPORT } | |
| 3352 | |
| 3353 /** | |
| 3354 * A [TaskInputBuilder] to build values for [_ImportSourceClosureTaskInput]. | |
| 3355 */ | |
| 3356 class _SourceClosureTaskInputBuilder implements TaskInputBuilder<List<Source>> { | |
| 3357 final _SourceClosureKind kind; | |
| 3358 final Set<LibraryElement> _libraries = new HashSet<LibraryElement>(); | |
| 3359 final List<Source> _newSources = <Source>[]; | |
| 3360 | |
| 3361 @override | |
| 3362 final ResultDescriptor currentResult; | |
| 3363 | |
| 3364 Source currentTarget; | |
| 3365 | |
| 3366 _SourceClosureTaskInputBuilder( | |
| 3367 Source librarySource, this.kind, this.currentResult) { | |
| 3368 _newSources.add(librarySource); | |
| 3369 } | |
| 3370 | |
| 3371 @override | |
| 3372 void set currentValue(Object value) { | |
| 3373 LibraryElement library = value; | |
| 3374 if (_libraries.add(library)) { | |
| 3375 if (kind == _SourceClosureKind.IMPORT || | |
| 3376 kind == _SourceClosureKind.IMPORT_EXPORT) { | |
| 3377 for (ImportElement importElement in library.imports) { | |
| 3378 Source importedSource = importElement.importedLibrary.source; | |
| 3379 _newSources.add(importedSource); | |
| 3380 } | |
| 3381 } | |
| 3382 if (kind == _SourceClosureKind.EXPORT || | |
| 3383 kind == _SourceClosureKind.IMPORT_EXPORT) { | |
| 3384 for (ExportElement exportElement in library.exports) { | |
| 3385 Source exportedSource = exportElement.exportedLibrary.source; | |
| 3386 _newSources.add(exportedSource); | |
| 3387 } | |
| 3388 } | |
| 3389 } | |
| 3390 } | |
| 3391 | |
| 3392 @override | |
| 3393 List<Source> get inputValue { | |
| 3394 return _libraries.map((LibraryElement library) => library.source).toList(); | |
| 3395 } | |
| 3396 | |
| 3397 @override | |
| 3398 void currentValueNotAvailable() { | |
| 3399 // Nothing needs to be done. moveNext() will simply go on to the next new | |
| 3400 // source. | |
| 3401 } | |
| 3402 | |
| 3403 @override | |
| 3404 bool moveNext() { | |
| 3405 if (_newSources.isEmpty) { | |
| 3406 return false; | |
| 3407 } | |
| 3408 currentTarget = _newSources.removeLast(); | |
| 3409 return true; | |
| 3410 } | |
| 3411 } | |
| OLD | NEW |