OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library dart2js.resolution.members; | 5 library dart2js.resolution.members; |
6 | 6 |
7 import '../common.dart'; | 7 import '../common.dart'; |
8 import '../common/names.dart' show Selectors; | 8 import '../common/names.dart' show Selectors; |
9 import '../common/resolution.dart' show Feature; | 9 import '../common/resolution.dart' show Feature; |
10 import '../compiler.dart' show Compiler; | 10 import '../compiler.dart' show Compiler; |
11 import '../constants/constructors.dart' | 11 import '../constants/constructors.dart' |
12 show RedirectingFactoryConstantConstructor; | 12 show RedirectingFactoryConstantConstructor; |
13 import '../constants/expressions.dart'; | 13 import '../constants/expressions.dart'; |
14 import '../constants/values.dart'; | 14 import '../constants/values.dart'; |
15 import '../core_types.dart'; | 15 import '../core_types.dart'; |
16 import '../dart_types.dart'; | 16 import '../dart_types.dart'; |
17 import '../elements/elements.dart'; | 17 import '../elements/elements.dart'; |
18 import '../elements/modelx.dart' | 18 import '../elements/modelx.dart' |
19 show | 19 show |
20 BaseFunctionElementX, | |
21 ConstructorElementX, | 20 ConstructorElementX, |
22 ErroneousElementX, | 21 ErroneousElementX, |
23 FunctionElementX, | 22 FunctionElementX, |
24 JumpTargetX, | 23 JumpTargetX, |
25 LocalFunctionElementX, | 24 LocalFunctionElementX, |
26 LocalParameterElementX, | 25 LocalParameterElementX, |
27 MethodElementX, | 26 MethodElementX, |
28 ParameterElementX, | 27 ParameterElementX, |
29 VariableElementX, | 28 VariableElementX, |
30 VariableList; | 29 VariableList; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 r'>|' | 136 r'>|' |
138 r'<=|' | 137 r'<=|' |
139 r'<|' | 138 r'<|' |
140 r'&|' | 139 r'&|' |
141 r'\^|' | 140 r'\^|' |
142 r'\|' | 141 r'\|' |
143 r')$'); | 142 r')$'); |
144 | 143 |
145 ResolverVisitor( | 144 ResolverVisitor( |
146 Compiler compiler, Element element, ResolutionRegistry registry, | 145 Compiler compiler, Element element, ResolutionRegistry registry, |
147 {Scope scope, bool useEnclosingScope: false}) | 146 {bool useEnclosingScope: false}) |
148 : this.enclosingElement = element, | 147 : this.enclosingElement = element, |
149 // When the element is a field, we are actually resolving its | 148 // When the element is a field, we are actually resolving its |
150 // initial value, which should not have access to instance | 149 // initial value, which should not have access to instance |
151 // fields. | 150 // fields. |
152 inInstanceContext = (element.isInstanceMember && !element.isField) || | 151 inInstanceContext = (element.isInstanceMember && !element.isField) || |
153 element.isGenerativeConstructor, | 152 element.isGenerativeConstructor, |
154 this.currentClass = | 153 this.currentClass = |
155 element.isClassMember ? element.enclosingClass : null, | 154 element.isClassMember ? element.enclosingClass : null, |
156 this.statementScope = new StatementScope(), | 155 this.statementScope = new StatementScope(), |
157 this.scope = scope ?? (useEnclosingScope | 156 scope = useEnclosingScope |
158 ? Scope.buildEnclosingScope(element) | 157 ? Scope.buildEnclosingScope(element) |
159 : element.buildScope()), | 158 : element.buildScope(), |
160 // The type annotations on a typedef do not imply type checks. | 159 // The type annotations on a typedef do not imply type checks. |
161 // TODO(karlklose): clean this up (dartbug.com/8870). | 160 // TODO(karlklose): clean this up (dartbug.com/8870). |
162 inCheckContext = compiler.options.enableTypeAssertions && | 161 inCheckContext = compiler.options.enableTypeAssertions && |
163 !element.isLibrary && | 162 !element.isLibrary && |
164 !element.isTypedef && | 163 !element.isTypedef && |
165 !element.enclosingElement.isTypedef, | 164 !element.enclosingElement.isTypedef, |
166 inCatchBlock = false, | 165 inCatchBlock = false, |
167 constantState = element.isConst | 166 constantState = element.isConst |
168 ? ConstantState.CONSTANT | 167 ? ConstantState.CONSTANT |
169 : ConstantState.NON_CONSTANT, | 168 : ConstantState.NON_CONSTANT, |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 return classElement.lookupConstructor(name.text); | 401 return classElement.lookupConstructor(name.text); |
403 } | 402 } |
404 return null; | 403 return null; |
405 } | 404 } |
406 | 405 |
407 void setupFunction(FunctionExpression node, FunctionElement function) { | 406 void setupFunction(FunctionExpression node, FunctionElement function) { |
408 Element enclosingElement = function.enclosingElement; | 407 Element enclosingElement = function.enclosingElement; |
409 if (node.modifiers.isStatic && enclosingElement.kind != ElementKind.CLASS) { | 408 if (node.modifiers.isStatic && enclosingElement.kind != ElementKind.CLASS) { |
410 reporter.reportErrorMessage(node, MessageKind.ILLEGAL_STATIC); | 409 reporter.reportErrorMessage(node, MessageKind.ILLEGAL_STATIC); |
411 } | 410 } |
412 FunctionSignature functionSignature = function.functionSignature; | |
413 | 411 |
414 // Create the scope where the type variables are introduced, if any. | |
415 scope = new MethodScope(scope, function); | 412 scope = new MethodScope(scope, function); |
416 functionSignature.typeVariables.forEach( | 413 // Put the parameters in scope. |
417 (DartType type) => addToScope(type.element)); | 414 FunctionSignature functionParameters = function.functionSignature; |
418 | |
419 // Create the scope for the function body, and put the parameters in scope. | |
420 scope = new BlockScope(scope); | |
421 Link<Node> parameterNodes = | 415 Link<Node> parameterNodes = |
422 (node.parameters == null) ? const Link<Node>() : node.parameters.nodes; | 416 (node.parameters == null) ? const Link<Node>() : node.parameters.nodes; |
423 functionSignature.forEachParameter((ParameterElementX element) { | 417 functionParameters.forEachParameter((ParameterElementX element) { |
424 // TODO(karlklose): should be a list of [FormalElement]s, but the actual | 418 // TODO(karlklose): should be a list of [FormalElement]s, but the actual |
425 // implementation uses [Element]. | 419 // implementation uses [Element]. |
426 List<Element> optionals = functionSignature.optionalParameters; | 420 List<Element> optionals = functionParameters.optionalParameters; |
427 if (!optionals.isEmpty && element == optionals.first) { | 421 if (!optionals.isEmpty && element == optionals.first) { |
428 NodeList nodes = parameterNodes.head; | 422 NodeList nodes = parameterNodes.head; |
429 parameterNodes = nodes.nodes; | 423 parameterNodes = nodes.nodes; |
430 } | 424 } |
431 if (element.isOptional) { | 425 if (element.isOptional) { |
432 if (element.initializer != null) { | 426 if (element.initializer != null) { |
433 ResolutionResult result = visitInConstantContext(element.initializer); | 427 ResolutionResult result = visitInConstantContext(element.initializer); |
434 if (result.isConstant) { | 428 if (result.isConstant) { |
435 element.constant = result.constant; | 429 element.constant = result.constant; |
436 } | 430 } |
437 } else { | 431 } else { |
438 element.constant = new NullConstantExpression(); | 432 element.constant = new NullConstantExpression(); |
439 } | 433 } |
440 } | 434 } |
441 VariableDefinitions variableDefinitions = parameterNodes.head; | 435 VariableDefinitions variableDefinitions = parameterNodes.head; |
442 Node parameterNode = variableDefinitions.definitions.nodes.head; | 436 Node parameterNode = variableDefinitions.definitions.nodes.head; |
443 // Field parameters (this.x) are not visible inside the constructor. The | 437 // Field parameters (this.x) are not visible inside the constructor. The |
444 // fields they reference are visible, but must be resolved independently. | 438 // fields they reference are visible, but must be resolved independently. |
445 if (element.isInitializingFormal) { | 439 if (element.isInitializingFormal) { |
446 registry.useElement(parameterNode, element); | 440 registry.useElement(parameterNode, element); |
447 } else { | 441 } else { |
448 LocalParameterElementX parameterElement = element; | 442 LocalParameterElementX parameterElement = element; |
449 defineLocalVariable(parameterNode, parameterElement); | 443 defineLocalVariable(parameterNode, parameterElement); |
450 addToScope(parameterElement); | 444 addToScope(parameterElement); |
451 } | 445 } |
452 parameterNodes = parameterNodes.tail; | 446 parameterNodes = parameterNodes.tail; |
453 }); | 447 }); |
454 addDeferredAction(enclosingElement, () { | 448 addDeferredAction(enclosingElement, () { |
455 functionSignature | 449 functionParameters |
456 .forEachOptionalParameter((ParameterElementX parameter) { | 450 .forEachOptionalParameter((ParameterElementX parameter) { |
457 parameter.constant = | 451 parameter.constant = |
458 compiler.resolver.constantCompiler.compileConstant(parameter); | 452 compiler.resolver.constantCompiler.compileConstant(parameter); |
459 }); | 453 }); |
460 }); | 454 }); |
461 if (inCheckContext) { | 455 if (inCheckContext) { |
462 functionSignature.forEachParameter((ParameterElement element) { | 456 functionParameters.forEachParameter((ParameterElement element) { |
463 registry.registerTypeUse(new TypeUse.checkedModeCheck(element.type)); | 457 registry.registerTypeUse(new TypeUse.checkedModeCheck(element.type)); |
464 }); | 458 }); |
465 } | 459 } |
466 } | 460 } |
467 | 461 |
468 ResolutionResult visitAssert(Assert node) { | 462 ResolutionResult visitAssert(Assert node) { |
469 if (!compiler.options.enableAssertMessage) { | 463 if (!compiler.options.enableAssertMessage) { |
470 if (node.hasMessage) { | 464 if (node.hasMessage) { |
471 reporter.reportErrorMessage( | 465 reporter.reportErrorMessage( |
472 node, MessageKind.EXPERIMENTAL_ASSERT_MESSAGE); | 466 node, MessageKind.EXPERIMENTAL_ASSERT_MESSAGE); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
569 String name; | 563 String name; |
570 if (node.name == null) { | 564 if (node.name == null) { |
571 name = ""; | 565 name = ""; |
572 } else { | 566 } else { |
573 name = node.name.asIdentifier().source; | 567 name = node.name.asIdentifier().source; |
574 } | 568 } |
575 LocalFunctionElementX function = new LocalFunctionElementX( | 569 LocalFunctionElementX function = new LocalFunctionElementX( |
576 name, node, ElementKind.FUNCTION, Modifiers.EMPTY, enclosingElement); | 570 name, node, ElementKind.FUNCTION, Modifiers.EMPTY, enclosingElement); |
577 ResolverTask.processAsyncMarker(compiler, function, registry); | 571 ResolverTask.processAsyncMarker(compiler, function, registry); |
578 function.functionSignature = SignatureResolver.analyze( | 572 function.functionSignature = SignatureResolver.analyze( |
579 compiler, | 573 compiler, node.parameters, node.returnType, function, registry, |
580 scope, | |
581 node.typeVariables, | |
582 node.parameters, | |
583 node.returnType, | |
584 function, | |
585 registry, | |
586 createRealParameters: true, | 574 createRealParameters: true, |
587 isFunctionExpression: !inFunctionDeclaration); | 575 isFunctionExpression: !inFunctionDeclaration); |
588 checkLocalDefinitionName(node, function); | 576 checkLocalDefinitionName(node, function); |
589 registry.defineFunction(node, function); | 577 registry.defineFunction(node, function); |
590 if (doAddToScope) { | 578 if (doAddToScope) { |
591 addToScope(function); | 579 addToScope(function); |
592 } | 580 } |
593 Scope oldScope = scope; // The scope is modified by [setupFunction]. | 581 Scope oldScope = scope; // The scope is modified by [setupFunction]. |
594 setupFunction(node, function); | 582 setupFunction(node, function); |
595 | 583 |
(...skipping 1273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1869 | 1857 |
1870 /// Handle access to a type literal of type variable [element]. Like `T` or | 1858 /// Handle access to a type literal of type variable [element]. Like `T` or |
1871 /// `T()` where 'T' is type variable. | 1859 /// `T()` where 'T' is type variable. |
1872 // TODO(johnniwinther): Remove [name] when [Selector] is not required for the | 1860 // TODO(johnniwinther): Remove [name] when [Selector] is not required for the |
1873 // the [GetStructure]. | 1861 // the [GetStructure]. |
1874 // TODO(johnniwinther): Remove [element] when it is no longer needed for | 1862 // TODO(johnniwinther): Remove [element] when it is no longer needed for |
1875 // evaluating constants. | 1863 // evaluating constants. |
1876 ResolutionResult handleTypeVariableTypeLiteralAccess( | 1864 ResolutionResult handleTypeVariableTypeLiteralAccess( |
1877 Send node, Name name, TypeVariableElement element) { | 1865 Send node, Name name, TypeVariableElement element) { |
1878 AccessSemantics semantics; | 1866 AccessSemantics semantics; |
1879 if (!Elements.hasAccessToTypeVariable(enclosingElement, element)) { | 1867 if (!Elements.hasAccessToTypeVariables(enclosingElement)) { |
1880 // TODO(johnniwinther): Add another access semantics for this. | 1868 // TODO(johnniwinther): Add another access semantics for this. |
1881 ErroneousElement error = reportAndCreateErroneousElement( | 1869 ErroneousElement error = reportAndCreateErroneousElement( |
1882 node, | 1870 node, |
1883 name.text, | 1871 name.text, |
1884 MessageKind.TYPE_VARIABLE_WITHIN_STATIC_MEMBER, | 1872 MessageKind.TYPE_VARIABLE_WITHIN_STATIC_MEMBER, |
1885 {'typeVariableName': name}, | 1873 {'typeVariableName': name}, |
1886 isError: true); | 1874 isError: true); |
1887 registry.registerFeature(Feature.COMPILE_TIME_ERROR); | 1875 registry.registerFeature(Feature.COMPILE_TIME_ERROR); |
1888 semantics = new StaticAccess.invalid(error); | 1876 semantics = new StaticAccess.invalid(error); |
1889 // TODO(johnniwinther): Clean up registration of elements and selectors | 1877 // TODO(johnniwinther): Clean up registration of elements and selectors |
(...skipping 20 matching lines...) Expand all Loading... |
1910 registry.registerSendStructure(node, new GetStructure(semantics)); | 1898 registry.registerSendStructure(node, new GetStructure(semantics)); |
1911 } | 1899 } |
1912 return const NoneResult(); | 1900 return const NoneResult(); |
1913 } | 1901 } |
1914 | 1902 |
1915 /// Handle access to a type literal of type variable [element]. Like `T = b`, | 1903 /// Handle access to a type literal of type variable [element]. Like `T = b`, |
1916 /// `T++` or `T += b` where 'T' is type variable. | 1904 /// `T++` or `T += b` where 'T' is type variable. |
1917 ResolutionResult handleTypeVariableTypeLiteralUpdate( | 1905 ResolutionResult handleTypeVariableTypeLiteralUpdate( |
1918 SendSet node, Name name, TypeVariableElement element) { | 1906 SendSet node, Name name, TypeVariableElement element) { |
1919 AccessSemantics semantics; | 1907 AccessSemantics semantics; |
1920 if (!Elements.hasAccessToTypeVariable(enclosingElement, element)) { | 1908 if (!Elements.hasAccessToTypeVariables(enclosingElement)) { |
1921 // TODO(johnniwinther): Add another access semantics for this. | 1909 // TODO(johnniwinther): Add another access semantics for this. |
1922 ErroneousElement error = reportAndCreateErroneousElement( | 1910 ErroneousElement error = reportAndCreateErroneousElement( |
1923 node, | 1911 node, |
1924 name.text, | 1912 name.text, |
1925 MessageKind.TYPE_VARIABLE_WITHIN_STATIC_MEMBER, | 1913 MessageKind.TYPE_VARIABLE_WITHIN_STATIC_MEMBER, |
1926 {'typeVariableName': name}, | 1914 {'typeVariableName': name}, |
1927 isError: true); | 1915 isError: true); |
1928 registry.registerFeature(Feature.COMPILE_TIME_ERROR); | 1916 registry.registerFeature(Feature.COMPILE_TIME_ERROR); |
1929 semantics = new StaticAccess.invalid(error); | 1917 semantics = new StaticAccess.invalid(error); |
1930 } else { | 1918 } else { |
(...skipping 2766 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4697 } | 4685 } |
4698 return const NoneResult(); | 4686 return const NoneResult(); |
4699 } | 4687 } |
4700 } | 4688 } |
4701 | 4689 |
4702 /// Looks up [name] in [scope] and unwraps the result. | 4690 /// Looks up [name] in [scope] and unwraps the result. |
4703 Element lookupInScope( | 4691 Element lookupInScope( |
4704 DiagnosticReporter reporter, Node node, Scope scope, String name) { | 4692 DiagnosticReporter reporter, Node node, Scope scope, String name) { |
4705 return Elements.unwrap(scope.lookup(name), reporter, node); | 4693 return Elements.unwrap(scope.lookup(name), reporter, node); |
4706 } | 4694 } |
OLD | NEW |