Chromium Code Reviews| 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('elements'); | 5 #library('elements'); |
| 6 | 6 |
| 7 #import('../tree/tree.dart'); | 7 #import('../tree/tree.dart'); |
| 8 #import('../scanner/scannerlib.dart'); | 8 #import('../scanner/scannerlib.dart'); |
| 9 #import('../leg.dart'); // TODO(karlklose): we only need type. | 9 #import('../leg.dart'); // TODO(karlklose): we only need type. |
| 10 #import('../util/util.dart'); | 10 #import('../util/util.dart'); |
| (...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 493 // the compilation unit of the abstract element. | 493 // the compilation unit of the abstract element. |
| 494 if (getter !== null && getter.enclosingElement === enclosingElement) { | 494 if (getter !== null && getter.enclosingElement === enclosingElement) { |
| 495 return getter.position(); | 495 return getter.position(); |
| 496 } else if (setter != null) { | 496 } else if (setter != null) { |
| 497 // TODO(ahe): checking for null should not be necessary. | 497 // TODO(ahe): checking for null should not be necessary. |
| 498 return setter.position(); | 498 return setter.position(); |
| 499 } | 499 } |
| 500 } | 500 } |
| 501 } | 501 } |
| 502 | 502 |
| 503 class FunctionParameters { | 503 class FunctionSignature { |
| 504 Link<Element> requiredParameters; | 504 Link<Element> requiredParameters; |
| 505 Link<Element> optionalParameters; | 505 Link<Element> optionalParameters; |
| 506 Link<Type> parameterTypes; | |
|
ngeoffray
2012/05/07 08:36:09
As discussed, we can avoid having this Link object
karlklose
2012/05/07 09:08:29
Done.
| |
| 507 Type returnType; | |
| 506 int requiredParameterCount; | 508 int requiredParameterCount; |
| 507 int optionalParameterCount; | 509 int optionalParameterCount; |
| 508 FunctionParameters(this.requiredParameters, | 510 FunctionSignature(this.requiredParameters, |
| 509 this.optionalParameters, | 511 this.optionalParameters, |
| 510 this.requiredParameterCount, | 512 this.requiredParameterCount, |
| 511 this.optionalParameterCount); | 513 this.optionalParameterCount, |
| 514 this.parameterTypes, | |
| 515 this.returnType); | |
| 512 | 516 |
| 513 void forEachParameter(void function(Element parameter)) { | 517 void forEachParameter(void function(Element parameter)) { |
| 514 for (Link<Element> link = requiredParameters; | 518 for (Link<Element> link = requiredParameters; |
| 515 !link.isEmpty(); | 519 !link.isEmpty(); |
| 516 link = link.tail) { | 520 link = link.tail) { |
| 517 function(link.head); | 521 function(link.head); |
| 518 } | 522 } |
| 519 for (Link<Element> link = optionalParameters; | 523 for (Link<Element> link = optionalParameters; |
| 520 !link.isEmpty(); | 524 !link.isEmpty(); |
| 521 link = link.tail) { | 525 link = link.tail) { |
| 522 function(link.head); | 526 function(link.head); |
| 523 } | 527 } |
| 524 } | 528 } |
| 525 | 529 |
| 526 int get parameterCount() => requiredParameterCount + optionalParameterCount; | 530 int get parameterCount() => requiredParameterCount + optionalParameterCount; |
| 527 } | 531 } |
| 528 | 532 |
| 529 class FunctionElement extends Element { | 533 class FunctionElement extends Element { |
| 530 FunctionExpression cachedNode; | 534 FunctionExpression cachedNode; |
| 531 Type type; | 535 Type type; |
| 532 final Modifiers modifiers; | 536 final Modifiers modifiers; |
| 533 | 537 |
| 534 FunctionParameters functionParameters; | 538 FunctionSignature functionSignature; |
| 535 | 539 |
| 536 /** | 540 /** |
| 537 * If this is an interface constructor, [defaultImplementation] will | 541 * If this is an interface constructor, [defaultImplementation] will |
| 538 * changed by the resolver to point to the default | 542 * changed by the resolver to point to the default |
| 539 * implementation. Otherwise, [:defaultImplementation === this:]. | 543 * implementation. Otherwise, [:defaultImplementation === this:]. |
| 540 */ | 544 */ |
| 541 FunctionElement defaultImplementation; | 545 FunctionElement defaultImplementation; |
| 542 | 546 |
| 543 FunctionElement(SourceString name, | 547 FunctionElement(SourceString name, |
| 544 ElementKind kind, | 548 ElementKind kind, |
| 545 Modifiers modifiers, | 549 Modifiers modifiers, |
| 546 Element enclosing) | 550 Element enclosing) |
| 547 : this.tooMuchOverloading(name, null, kind, modifiers, enclosing, null); | 551 : this.tooMuchOverloading(name, null, kind, modifiers, enclosing, null); |
| 548 | 552 |
| 549 FunctionElement.node(SourceString name, | 553 FunctionElement.node(SourceString name, |
| 550 FunctionExpression node, | 554 FunctionExpression node, |
| 551 ElementKind kind, | 555 ElementKind kind, |
| 552 Modifiers modifiers, | 556 Modifiers modifiers, |
| 553 Element enclosing) | 557 Element enclosing) |
| 554 : this.tooMuchOverloading(name, node, kind, modifiers, enclosing, null); | 558 : this.tooMuchOverloading(name, node, kind, modifiers, enclosing, null); |
| 555 | 559 |
| 556 FunctionElement.from(SourceString name, | 560 FunctionElement.from(SourceString name, |
| 557 FunctionElement other, | 561 FunctionElement other, |
| 558 Element enclosing) | 562 Element enclosing) |
| 559 : this.tooMuchOverloading(name, other.cachedNode, other.kind, | 563 : this.tooMuchOverloading(name, other.cachedNode, other.kind, |
| 560 other.modifiers, enclosing, | 564 other.modifiers, enclosing, |
| 561 other.functionParameters); | 565 other.functionSignature); |
| 562 | 566 |
| 563 FunctionElement.tooMuchOverloading(SourceString name, | 567 FunctionElement.tooMuchOverloading(SourceString name, |
| 564 FunctionExpression this.cachedNode, | 568 FunctionExpression this.cachedNode, |
| 565 ElementKind kind, | 569 ElementKind kind, |
| 566 Modifiers this.modifiers, | 570 Modifiers this.modifiers, |
| 567 Element enclosing, | 571 Element enclosing, |
| 568 FunctionParameters this.functionParameters) | 572 FunctionSignature this.functionSignature) |
| 569 : super(name, kind, enclosing) | 573 : super(name, kind, enclosing) |
| 570 { | 574 { |
| 571 defaultImplementation = this; | 575 defaultImplementation = this; |
| 572 } | 576 } |
| 573 | 577 |
| 574 bool isInstanceMember() { | 578 bool isInstanceMember() { |
| 575 return isMember() | 579 return isMember() |
| 576 && kind != ElementKind.GENERATIVE_CONSTRUCTOR | 580 && kind != ElementKind.GENERATIVE_CONSTRUCTOR |
| 577 && !modifiers.isFactory() | 581 && !modifiers.isFactory() |
| 578 && !modifiers.isStatic(); | 582 && !modifiers.isStatic(); |
| 579 } | 583 } |
| 580 | 584 |
| 581 FunctionParameters computeParameters(Compiler compiler) { | 585 FunctionSignature computeSignature(Compiler compiler) { |
| 582 if (functionParameters !== null) return functionParameters; | 586 if (functionSignature !== null) return functionSignature; |
| 583 functionParameters = compiler.resolveSignature(this); | 587 compiler.withCurrentElement(this, () { |
| 584 return functionParameters; | 588 functionSignature = compiler.resolveSignature(this); |
| 589 }); | |
| 590 return functionSignature; | |
| 585 } | 591 } |
| 586 | 592 |
| 587 int requiredParameterCount(Compiler compiler) { | 593 int requiredParameterCount(Compiler compiler) { |
| 588 return computeParameters(compiler).requiredParameterCount; | 594 return computeSignature(compiler).requiredParameterCount; |
| 589 } | 595 } |
| 590 | 596 |
| 591 int optionalParameterCount(Compiler compiler) { | 597 int optionalParameterCount(Compiler compiler) { |
| 592 return computeParameters(compiler).optionalParameterCount; | 598 return computeSignature(compiler).optionalParameterCount; |
| 593 } | 599 } |
| 594 | 600 |
| 595 int parameterCount(Compiler compiler) { | 601 int parameterCount(Compiler compiler) { |
| 596 return computeParameters(compiler).parameterCount; | 602 return computeSignature(compiler).parameterCount; |
| 597 } | 603 } |
| 598 | 604 |
| 599 FunctionType computeType(Compiler compiler) { | 605 FunctionType computeType(Compiler compiler) { |
| 600 if (type != null) return type; | 606 if (type != null) return type; |
| 601 return compiler.withCurrentElement(this, () { | 607 FunctionSignature signature = computeSignature(compiler); |
| 602 FunctionParameters parameters = computeParameters(compiler); | 608 // TODO(karlklose): optional parameters. |
| 603 Types types = compiler.types; | 609 type = new FunctionType(signature.returnType, |
| 604 FunctionExpression node = | 610 signature.parameterTypes, |
| 605 compiler.parser.measure(() => parseNode(compiler)); | 611 this); |
| 606 Type returnType = compiler.resolveTypeAnnotation(this, node.returnType); | 612 return type; |
| 607 | |
| 608 LinkBuilder<Type> parameterTypes = new LinkBuilder<Type>(); | |
| 609 for (Link<Element> link = parameters.requiredParameters; | |
| 610 !link.isEmpty(); | |
| 611 link = link.tail) { | |
| 612 parameterTypes.addLast(link.head.computeType(compiler)); | |
| 613 } | |
| 614 type = new FunctionType(returnType, parameterTypes.toLink(), this); | |
| 615 return type; | |
| 616 }); | |
| 617 } | 613 } |
| 618 | 614 |
| 619 Node parseNode(DiagnosticListener listener) => cachedNode; | 615 Node parseNode(DiagnosticListener listener) => cachedNode; |
| 620 | 616 |
| 621 Token position() => cachedNode.getBeginToken(); | 617 Token position() => cachedNode.getBeginToken(); |
| 622 } | 618 } |
| 623 | 619 |
| 624 class ConstructorBodyElement extends FunctionElement { | 620 class ConstructorBodyElement extends FunctionElement { |
| 625 FunctionElement constructor; | 621 FunctionElement constructor; |
| 626 | 622 |
| 627 ConstructorBodyElement(FunctionElement constructor) | 623 ConstructorBodyElement(FunctionElement constructor) |
| 628 : this.constructor = constructor, | 624 : this.constructor = constructor, |
| 629 super(constructor.name, | 625 super(constructor.name, |
| 630 ElementKind.GENERATIVE_CONSTRUCTOR_BODY, | 626 ElementKind.GENERATIVE_CONSTRUCTOR_BODY, |
| 631 null, | 627 null, |
| 632 constructor.enclosingElement) { | 628 constructor.enclosingElement) { |
| 633 functionParameters = constructor.functionParameters; | 629 functionSignature = constructor.functionSignature; |
| 634 } | 630 } |
| 635 | 631 |
| 636 bool isInstanceMember() => true; | 632 bool isInstanceMember() => true; |
| 637 | 633 |
| 638 FunctionType computeType(Compiler compiler) { unreachable(); } | 634 FunctionType computeType(Compiler compiler) { unreachable(); } |
| 639 | 635 |
| 640 Node parseNode(DiagnosticListener listener) { | 636 Node parseNode(DiagnosticListener listener) { |
| 641 if (cachedNode !== null) return cachedNode; | 637 if (cachedNode !== null) return cachedNode; |
| 642 cachedNode = constructor.parseNode(listener); | 638 cachedNode = constructor.parseNode(listener); |
| 643 assert(cachedNode !== null); | 639 assert(cachedNode !== null); |
| (...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1021 final Node node; | 1017 final Node node; |
| 1022 Type bound; | 1018 Type bound; |
| 1023 Type type; | 1019 Type type; |
| 1024 TypeVariableElement(name, Element enclosing, this.node, this.type, | 1020 TypeVariableElement(name, Element enclosing, this.node, this.type, |
| 1025 [this.bound]) | 1021 [this.bound]) |
| 1026 : super(name, ElementKind.TYPE_VARIABLE, enclosing); | 1022 : super(name, ElementKind.TYPE_VARIABLE, enclosing); |
| 1027 Type computeType(compiler) => type; | 1023 Type computeType(compiler) => type; |
| 1028 Node parseNode(compiler) => node; | 1024 Node parseNode(compiler) => node; |
| 1029 toString() => "${enclosingElement.toString()}.${name.slowToString()}"; | 1025 toString() => "${enclosingElement.toString()}.${name.slowToString()}"; |
| 1030 } | 1026 } |
| OLD | NEW |