| 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 part of resolution; | 5 part of resolution; |
| 6 | 6 |
| 7 abstract class TreeElements { | 7 abstract class TreeElements { |
| 8 Element get currentElement; | 8 Element get currentElement; |
| 9 Set<Node> get superUses; | 9 Set<Node> get superUses; |
| 10 | 10 |
| (...skipping 3455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3466 TreeElementMapping mapping) | 3466 TreeElementMapping mapping) |
| 3467 : super(compiler, typedefElement, mapping); | 3467 : super(compiler, typedefElement, mapping); |
| 3468 | 3468 |
| 3469 visitTypedef(Typedef node) { | 3469 visitTypedef(Typedef node) { |
| 3470 TypedefType type = element.computeType(compiler); | 3470 TypedefType type = element.computeType(compiler); |
| 3471 scope = new TypeDeclarationScope(scope, element); | 3471 scope = new TypeDeclarationScope(scope, element); |
| 3472 resolveTypeVariableBounds(node.typeParameters); | 3472 resolveTypeVariableBounds(node.typeParameters); |
| 3473 | 3473 |
| 3474 FunctionSignature signature = SignatureResolver.analyze( | 3474 FunctionSignature signature = SignatureResolver.analyze( |
| 3475 compiler, node.formals, node.returnType, element, | 3475 compiler, node.formals, node.returnType, element, |
| 3476 defaultValuesAllowed: false); | 3476 defaultValuesError: MessageKind.TYPEDEF_FORMAL_WITH_DEFAULT); |
| 3477 element.functionSignature = signature; | 3477 element.functionSignature = signature; |
| 3478 | 3478 |
| 3479 scope = new MethodScope(scope, element); | 3479 scope = new MethodScope(scope, element); |
| 3480 signature.forEachParameter((Element element) { | 3480 signature.forEachParameter((Element element) { |
| 3481 defineElement(element.parseNode(compiler), element); | 3481 defineElement(element.parseNode(compiler), element); |
| 3482 }); | 3482 }); |
| 3483 | 3483 |
| 3484 element.alias = compiler.computeFunctionType(element, signature); | 3484 element.alias = compiler.computeFunctionType(element, signature); |
| 3485 | 3485 |
| 3486 void checkCyclicReference() { | 3486 void checkCyclicReference() { |
| (...skipping 614 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4101 } | 4101 } |
| 4102 } | 4102 } |
| 4103 } | 4103 } |
| 4104 } | 4104 } |
| 4105 | 4105 |
| 4106 /** | 4106 /** |
| 4107 * [SignatureResolver] resolves function signatures. | 4107 * [SignatureResolver] resolves function signatures. |
| 4108 */ | 4108 */ |
| 4109 class SignatureResolver extends CommonResolverVisitor<Element> { | 4109 class SignatureResolver extends CommonResolverVisitor<Element> { |
| 4110 final Element enclosingElement; | 4110 final Element enclosingElement; |
| 4111 final bool defaultValuesAllowed; | 4111 final MessageKind defaultValuesError; |
| 4112 Link<Element> optionalParameters = const Link<Element>(); | 4112 Link<Element> optionalParameters = const Link<Element>(); |
| 4113 int optionalParameterCount = 0; | 4113 int optionalParameterCount = 0; |
| 4114 bool isOptionalParameter = false; | 4114 bool isOptionalParameter = false; |
| 4115 bool optionalParametersAreNamed = false; | 4115 bool optionalParametersAreNamed = false; |
| 4116 VariableDefinitions currentDefinitions; | 4116 VariableDefinitions currentDefinitions; |
| 4117 | 4117 |
| 4118 SignatureResolver(Compiler compiler, | 4118 SignatureResolver(Compiler compiler, |
| 4119 this.enclosingElement, | 4119 this.enclosingElement, |
| 4120 {this.defaultValuesAllowed: true}) | 4120 {this.defaultValuesError}) |
| 4121 : super(compiler); | 4121 : super(compiler); |
| 4122 | 4122 |
| 4123 bool get defaultValuesAllowed => defaultValuesError == null; |
| 4124 |
| 4123 Element visitNodeList(NodeList node) { | 4125 Element visitNodeList(NodeList node) { |
| 4124 // This must be a list of optional arguments. | 4126 // This must be a list of optional arguments. |
| 4125 String value = node.beginToken.stringValue; | 4127 String value = node.beginToken.stringValue; |
| 4126 if ((!identical(value, '[')) && (!identical(value, '{'))) { | 4128 if ((!identical(value, '[')) && (!identical(value, '{'))) { |
| 4127 internalError(node, "expected optional parameters"); | 4129 internalError(node, "expected optional parameters"); |
| 4128 } | 4130 } |
| 4129 optionalParametersAreNamed = (identical(value, '{')); | 4131 optionalParametersAreNamed = (identical(value, '{')); |
| 4130 isOptionalParameter = true; | 4132 isOptionalParameter = true; |
| 4131 LinkBuilder<Element> elements = analyzeNodes(node.nodes); | 4133 LinkBuilder<Element> elements = analyzeNodes(node.nodes); |
| 4132 optionalParameterCount = elements.length; | 4134 optionalParameterCount = elements.length; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4241 Identifier identifier = node.selector.asIdentifier() != null ? | 4243 Identifier identifier = node.selector.asIdentifier() != null ? |
| 4242 node.selector.asIdentifier() : | 4244 node.selector.asIdentifier() : |
| 4243 node.selector.asFunctionExpression().name.asIdentifier(); | 4245 node.selector.asFunctionExpression().name.asIdentifier(); |
| 4244 validateName(identifier); | 4246 validateName(identifier); |
| 4245 SourceString source = identifier.source; | 4247 SourceString source = identifier.source; |
| 4246 element = new VariableElementX(source, variables, | 4248 element = new VariableElementX(source, variables, |
| 4247 ElementKind.PARAMETER, node); | 4249 ElementKind.PARAMETER, node); |
| 4248 } | 4250 } |
| 4249 Node defaultValue = node.arguments.head; | 4251 Node defaultValue = node.arguments.head; |
| 4250 if (!defaultValuesAllowed) { | 4252 if (!defaultValuesAllowed) { |
| 4251 error(defaultValue, MessageKind.TYPEDEF_FORMAL_WITH_DEFAULT); | 4253 error(defaultValue, defaultValuesError); |
| 4252 } | 4254 } |
| 4253 // Visit the value. The compile time constant handler will | 4255 // Visit the value. The compile time constant handler will |
| 4254 // make sure it's a compile time constant. | 4256 // make sure it's a compile time constant. |
| 4255 resolveExpression(defaultValue); | 4257 resolveExpression(defaultValue); |
| 4256 return element; | 4258 return element; |
| 4257 } | 4259 } |
| 4258 | 4260 |
| 4259 Element visitFunctionExpression(FunctionExpression node) { | 4261 Element visitFunctionExpression(FunctionExpression node) { |
| 4262 // This is a function typed parameter. |
| 4260 Modifiers modifiers = currentDefinitions.modifiers; | 4263 Modifiers modifiers = currentDefinitions.modifiers; |
| 4261 if (modifiers.isFinal()) { | 4264 if (modifiers.isFinal()) { |
| 4262 compiler.reportError(modifiers, | 4265 compiler.reportError(modifiers, |
| 4263 MessageKind.FINAL_FUNCTION_TYPE_PARAMETER); | 4266 MessageKind.FINAL_FUNCTION_TYPE_PARAMETER); |
| 4264 } | 4267 } |
| 4265 if (modifiers.isVar()) { | 4268 if (modifiers.isVar()) { |
| 4266 compiler.reportError(modifiers, MessageKind.VAR_FUNCTION_TYPE_PARAMETER); | 4269 compiler.reportError(modifiers, MessageKind.VAR_FUNCTION_TYPE_PARAMETER); |
| 4267 } | 4270 } |
| 4268 // This is a function typed parameter. | 4271 |
| 4269 // TODO(ahe): Resolve the function type. | 4272 Element variable = visit(node.name); |
| 4270 return visit(node.name); | 4273 SignatureResolver.analyze(compiler, node.parameters, node.returnType, |
| 4274 variable, |
| 4275 defaultValuesError: MessageKind.FUNCTION_TYPE_FORMAL_WITH_DEFAULT); |
| 4276 // TODO(ahe): Resolve and record the function type in the correct |
| 4277 // [TreeElements]. |
| 4278 return variable; |
| 4271 } | 4279 } |
| 4272 | 4280 |
| 4273 LinkBuilder<Element> analyzeNodes(Link<Node> link) { | 4281 LinkBuilder<Element> analyzeNodes(Link<Node> link) { |
| 4274 LinkBuilder<Element> elements = new LinkBuilder<Element>(); | 4282 LinkBuilder<Element> elements = new LinkBuilder<Element>(); |
| 4275 for (; !link.isEmpty; link = link.tail) { | 4283 for (; !link.isEmpty; link = link.tail) { |
| 4276 Element element = link.head.accept(this); | 4284 Element element = link.head.accept(this); |
| 4277 if (element != null) { | 4285 if (element != null) { |
| 4278 elements.addLast(element); | 4286 elements.addLast(element); |
| 4279 } else { | 4287 } else { |
| 4280 // If parameter is null, the current node should be the last, | 4288 // If parameter is null, the current node should be the last, |
| 4281 // and a list of optional named parameters. | 4289 // and a list of optional named parameters. |
| 4282 if (!link.tail.isEmpty || (link.head is !NodeList)) { | 4290 if (!link.tail.isEmpty || (link.head is !NodeList)) { |
| 4283 internalError(link.head, "expected optional parameters"); | 4291 internalError(link.head, "expected optional parameters"); |
| 4284 } | 4292 } |
| 4285 } | 4293 } |
| 4286 } | 4294 } |
| 4287 return elements; | 4295 return elements; |
| 4288 } | 4296 } |
| 4289 | 4297 |
| 4290 /** | 4298 /** |
| 4291 * Resolves formal parameters and return type to a [FunctionSignature]. | 4299 * Resolves formal parameters and return type to a [FunctionSignature]. |
| 4292 */ | 4300 */ |
| 4293 static FunctionSignature analyze(Compiler compiler, | 4301 static FunctionSignature analyze(Compiler compiler, |
| 4294 NodeList formalParameters, | 4302 NodeList formalParameters, |
| 4295 Node returnNode, | 4303 Node returnNode, |
| 4296 Element element, | 4304 Element element, |
| 4297 {bool defaultValuesAllowed: true}) { | 4305 {MessageKind defaultValuesError}) { |
| 4298 SignatureResolver visitor = new SignatureResolver(compiler, element, | 4306 SignatureResolver visitor = new SignatureResolver(compiler, element, |
| 4299 defaultValuesAllowed: defaultValuesAllowed); | 4307 defaultValuesError: defaultValuesError); |
| 4300 Link<Element> parameters = const Link<Element>(); | 4308 Link<Element> parameters = const Link<Element>(); |
| 4301 int requiredParameterCount = 0; | 4309 int requiredParameterCount = 0; |
| 4302 if (formalParameters == null) { | 4310 if (formalParameters == null) { |
| 4303 if (!element.isGetter()) { | 4311 if (!element.isGetter()) { |
| 4304 compiler.reportError(element, MessageKind.MISSING_FORMALS); | 4312 compiler.reportError(element, MessageKind.MISSING_FORMALS); |
| 4305 } | 4313 } |
| 4306 } else { | 4314 } else { |
| 4307 if (element.isGetter()) { | 4315 if (element.isGetter()) { |
| 4308 if (!identical(formalParameters.getEndToken().next.stringValue, | 4316 if (!identical(formalParameters.getEndToken().next.stringValue, |
| 4309 // TODO(ahe): Remove the check for native keyword. | 4317 // TODO(ahe): Remove the check for native keyword. |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4527 return finishConstructorReference(visit(expression), | 4535 return finishConstructorReference(visit(expression), |
| 4528 expression, expression); | 4536 expression, expression); |
| 4529 } | 4537 } |
| 4530 } | 4538 } |
| 4531 | 4539 |
| 4532 /// Looks up [name] in [scope] and unwraps the result. | 4540 /// Looks up [name] in [scope] and unwraps the result. |
| 4533 Element lookupInScope(Compiler compiler, Node node, | 4541 Element lookupInScope(Compiler compiler, Node node, |
| 4534 Scope scope, SourceString name) { | 4542 Scope scope, SourceString name) { |
| 4535 return Elements.unwrap(scope.lookup(name), compiler, node); | 4543 return Elements.unwrap(scope.lookup(name), compiler, node); |
| 4536 } | 4544 } |
| OLD | NEW |