| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 class TypeCheckerTask extends CompilerTask { | 5 class TypeCheckerTask extends CompilerTask { |
| 6 TypeCheckerTask(Compiler compiler) : super(compiler); | 6 TypeCheckerTask(Compiler compiler) : super(compiler); |
| 7 String get name() => "Type checker"; | 7 String get name() => "Type checker"; |
| 8 | 8 |
| 9 void check(Node tree, TreeElements elements) { | 9 void check(Node tree, TreeElements elements) { |
| 10 measure(() { | 10 measure(() { |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 /** Dart Programming Language Specification: 11.5.1 For Loop */ | 231 /** Dart Programming Language Specification: 11.5.1 For Loop */ |
| 232 Type visitFor(For node) { | 232 Type visitFor(For node) { |
| 233 analyzeWithDefault(node.initializer, StatementType.NOT_RETURNING); | 233 analyzeWithDefault(node.initializer, StatementType.NOT_RETURNING); |
| 234 checkCondition(node.condition); | 234 checkCondition(node.condition); |
| 235 analyzeWithDefault(node.update, StatementType.NOT_RETURNING); | 235 analyzeWithDefault(node.update, StatementType.NOT_RETURNING); |
| 236 StatementType bodyType = analyze(node.body); | 236 StatementType bodyType = analyze(node.body); |
| 237 return bodyType.join(StatementType.NOT_RETURNING); | 237 return bodyType.join(StatementType.NOT_RETURNING); |
| 238 } | 238 } |
| 239 | 239 |
| 240 Type visitFunctionExpression(FunctionExpression node) { | 240 Type visitFunctionExpression(FunctionExpression node) { |
| 241 Type type; |
| 242 Type returnType; |
| 243 Type previousType; |
| 241 final FunctionElement element = elements[node]; | 244 final FunctionElement element = elements[node]; |
| 242 FunctionType functionType = computeType(element); | 245 if (element.kind === ElementKind.GENERATIVE_CONSTRUCTOR || |
| 243 Type returnType = functionType.returnType; | 246 element.kind === ElementKind.GENERATIVE_CONSTRUCTOR_BODY) { |
| 247 type = types.dynamicType; |
| 248 returnType = types.voidType; |
| 249 } else { |
| 250 FunctionType functionType = computeType(element); |
| 251 returnType = functionType.returnType; |
| 252 type = functionType; |
| 253 } |
| 244 Type previous = expectedReturnType; | 254 Type previous = expectedReturnType; |
| 245 expectedReturnType = returnType; | 255 expectedReturnType = returnType; |
| 246 if (element.isMember()) currentClass = element.enclosingElement; | 256 if (element.isMember()) currentClass = element.enclosingElement; |
| 247 StatementType bodyType = analyze(node.body); | 257 StatementType bodyType = analyze(node.body); |
| 248 if (returnType != types.voidType && returnType != types.dynamicType | 258 if (returnType != types.voidType && returnType != types.dynamicType |
| 249 && bodyType != StatementType.RETURNING) { | 259 && bodyType != StatementType.RETURNING) { |
| 250 MessageKind kind; | 260 MessageKind kind; |
| 251 if (bodyType == StatementType.MAYBE_RETURNING) { | 261 if (bodyType == StatementType.MAYBE_RETURNING) { |
| 252 kind = MessageKind.MAYBE_MISSING_RETURN; | 262 kind = MessageKind.MAYBE_MISSING_RETURN; |
| 253 } else { | 263 } else { |
| 254 kind = MessageKind.MISSING_RETURN; | 264 kind = MessageKind.MISSING_RETURN; |
| 255 } | 265 } |
| 256 reportTypeWarning(node.name, kind); | 266 reportTypeWarning(node.name, kind); |
| 257 } | 267 } |
| 258 expectedReturnType = previous; | 268 expectedReturnType = previous; |
| 259 return functionType; | 269 return type; |
| 260 } | 270 } |
| 261 | 271 |
| 262 Type visitIdentifier(Identifier node) { | 272 Type visitIdentifier(Identifier node) { |
| 263 if (node.isThis()) { | 273 if (node.isThis()) { |
| 264 return currentClass.computeType(compiler); | 274 return currentClass.computeType(compiler); |
| 265 } else { | 275 } else { |
| 266 fail(node, 'internal error: unexpected identifier'); | 276 fail(node, 'internal error: unexpected identifier'); |
| 267 } | 277 } |
| 268 } | 278 } |
| 269 | 279 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 return boolType; | 344 return boolType; |
| 335 } else if (name === 'is') { | 345 } else if (name === 'is') { |
| 336 return boolType; | 346 return boolType; |
| 337 } | 347 } |
| 338 fail(selector, 'unexpected operator ${name}'); | 348 fail(selector, 'unexpected operator ${name}'); |
| 339 | 349 |
| 340 } else if (node.isPropertyAccess) { | 350 } else if (node.isPropertyAccess) { |
| 341 if (node.receiver !== null) fail(node, 'cannot handle fields'); | 351 if (node.receiver !== null) fail(node, 'cannot handle fields'); |
| 342 Element element = elements[node]; | 352 Element element = elements[node]; |
| 343 if (element === null) fail(node.selector, 'unresolved property'); | 353 if (element === null) fail(node.selector, 'unresolved property'); |
| 344 return element.computeType(compiler); | 354 return computeType(element); |
| 345 | 355 |
| 346 } else if (node.isFunctionObjectInvocation) { | 356 } else if (node.isFunctionObjectInvocation) { |
| 347 fail(node.receiver, 'function object invocation unimplemented'); | 357 fail(node.receiver, 'function object invocation unimplemented'); |
| 348 | 358 |
| 349 } else { | 359 } else { |
| 350 Link<Type> argumentTypes = analyzeArguments(node.arguments); | 360 Link<Type> argumentTypes = analyzeArguments(node.arguments); |
| 351 FunctionType funType; | 361 FunctionType funType; |
| 352 if (node.receiver !== null) { | 362 if (node.receiver !== null) { |
| 353 Type receiverType = analyze(node.receiver); | 363 Type receiverType = analyze(node.receiver); |
| 354 if (receiverType === types.dynamicType) return types.dynamicType; | 364 if (receiverType === types.dynamicType) return types.dynamicType; |
| 355 if (receiverType === null) { | 365 if (receiverType === null) { |
| 356 fail(node.receiver, 'receivertype is null'); | 366 fail(node.receiver, 'receivertype is null'); |
| 357 } | 367 } |
| 358 ClassElement classElement = receiverType.element; | 368 ClassElement classElement = receiverType.element; |
| 359 // TODO(karlklose): substitute type arguments. | 369 // TODO(karlklose): substitute type arguments. |
| 360 Type memberType = lookupMethodType(node, classElement, selector.source); | 370 Type memberType = lookupMethodType(node, classElement, selector.source); |
| 361 if (memberType === types.dynamicType) return types.dynamicType; | 371 if (memberType === types.dynamicType) return types.dynamicType; |
| 362 if (memberType is !FunctionType) { | 372 if (memberType is !FunctionType) { |
| 363 fail(node, 'can only handle function types'); | 373 fail(node, 'can only handle function types'); |
| 364 } | 374 } |
| 365 funType = memberType; | 375 funType = memberType; |
| 366 } else { | 376 } else { |
| 367 Element element = elements[node]; | 377 Element element = elements[node]; |
| 368 if (element.kind === ElementKind.FUNCTION) { | 378 if (element.kind === ElementKind.FUNCTION) { |
| 369 funType = element.computeType(compiler); | 379 funType = computeType(element); |
| 370 } else if (element.kind === ElementKind.FOREIGN) { | 380 } else if (element.kind === ElementKind.FOREIGN) { |
| 371 return types.dynamicType; | 381 return types.dynamicType; |
| 372 } else { | 382 } else { |
| 373 fail(node, 'unexpected element kind ${element.kind}'); | 383 fail(node, 'unexpected element kind ${element.kind}'); |
| 374 } | 384 } |
| 375 } | 385 } |
| 376 Link<Type> parameterTypes = funType.parameterTypes; | 386 Link<Type> parameterTypes = funType.parameterTypes; |
| 377 Link<Node> argumentNodes = node.arguments; | 387 Link<Node> argumentNodes = node.arguments; |
| 378 while (!argumentTypes.isEmpty() && !parameterTypes.isEmpty()) { | 388 while (!argumentTypes.isEmpty() && !parameterTypes.isEmpty()) { |
| 379 checkAssignable(argumentNodes.head, parameterTypes.head, | 389 checkAssignable(argumentNodes.head, parameterTypes.head, |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 return StatementType.RETURNING; | 500 return StatementType.RETURNING; |
| 491 } | 501 } |
| 492 | 502 |
| 493 Type visitThrow(Throw node) { | 503 Type visitThrow(Throw node) { |
| 494 if (node.expression !== null) analyze(node.expression); | 504 if (node.expression !== null) analyze(node.expression); |
| 495 return StatementType.RETURNING; | 505 return StatementType.RETURNING; |
| 496 } | 506 } |
| 497 | 507 |
| 498 Type computeType(Element element) { | 508 Type computeType(Element element) { |
| 499 if (element === null) return types.dynamicType; | 509 if (element === null) return types.dynamicType; |
| 500 return element.computeType(compiler); | 510 Type result = element.computeType(compiler); |
| 511 return (result !== null) ? result : types.dynamicType; |
| 501 } | 512 } |
| 502 | 513 |
| 503 Type visitTypeAnnotation(TypeAnnotation node) { | 514 Type visitTypeAnnotation(TypeAnnotation node) { |
| 504 if (node.typeName === null) return types.dynamicType; | 515 if (node.typeName === null) return types.dynamicType; |
| 505 Identifier identifier = node.typeName.asIdentifier(); | 516 Identifier identifier = node.typeName.asIdentifier(); |
| 506 if (identifier === null) { | 517 if (identifier === null) { |
| 507 fail(node.typeName, 'library prefix not implemented'); | 518 fail(node.typeName, 'library prefix not implemented'); |
| 508 } | 519 } |
| 509 // TODO(ahe): Why wasn't this resolved by the resolver? | 520 // TODO(ahe): Why wasn't this resolved by the resolver? |
| 510 Type type = lookupType(identifier.source, compiler, types); | 521 Type type = lookupType(identifier.source, compiler, types); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 } | 625 } |
| 615 | 626 |
| 616 visitCatchBlock(CatchBlock node) { | 627 visitCatchBlock(CatchBlock node) { |
| 617 compiler.unimplemented('visitCatchBlock', node: node); | 628 compiler.unimplemented('visitCatchBlock', node: node); |
| 618 } | 629 } |
| 619 | 630 |
| 620 visitTypedef(Typedef node) { | 631 visitTypedef(Typedef node) { |
| 621 compiler.unimplemented('visitTypedef', node: node); | 632 compiler.unimplemented('visitTypedef', node: node); |
| 622 } | 633 } |
| 623 } | 634 } |
| OLD | NEW |