Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: lib/src/js/nodes.dart

Issue 1676463002: Type annotations instead of closure comments. (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 js_ast; 5 part of js_ast;
6 6
7 abstract class NodeVisitor<T> { 7 abstract class NodeVisitor<T> implements TypeRefVisitor<T> {
8 T visitProgram(Program node); 8 T visitProgram(Program node);
9 9
10 T visitBlock(Block node); 10 T visitBlock(Block node);
11 T visitExpressionStatement(ExpressionStatement node); 11 T visitExpressionStatement(ExpressionStatement node);
12 T visitEmptyStatement(EmptyStatement node); 12 T visitEmptyStatement(EmptyStatement node);
13 T visitIf(If node); 13 T visitIf(If node);
14 T visitFor(For node); 14 T visitFor(For node);
15 T visitForIn(ForIn node); 15 T visitForIn(ForIn node);
16 T visitForOf(ForOf node); 16 T visitForOf(ForOf node);
17 T visitWhile(While node); 17 T visitWhile(While node);
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 T visitInterpolatedSelector(InterpolatedSelector node); 87 T visitInterpolatedSelector(InterpolatedSelector node);
88 T visitInterpolatedStatement(InterpolatedStatement node); 88 T visitInterpolatedStatement(InterpolatedStatement node);
89 T visitInterpolatedMethod(InterpolatedMethod node); 89 T visitInterpolatedMethod(InterpolatedMethod node);
90 T visitInterpolatedIdentifier(InterpolatedIdentifier node); 90 T visitInterpolatedIdentifier(InterpolatedIdentifier node);
91 91
92 T visitArrayBindingPattern(ArrayBindingPattern node); 92 T visitArrayBindingPattern(ArrayBindingPattern node);
93 T visitObjectBindingPattern(ObjectBindingPattern node); 93 T visitObjectBindingPattern(ObjectBindingPattern node);
94 T visitDestructuredVariable(DestructuredVariable node); 94 T visitDestructuredVariable(DestructuredVariable node);
95 } 95 }
96 96
97 abstract class TypeRefVisitor<T> {
98 T visitQualifiedTypeRef(QualifiedTypeRef node);
99 T visitGenericTypeRef(GenericTypeRef node);
100 T visitUnionTypeRef(UnionTypeRef node);
101 T visitRecordTypeRef(RecordTypeRef node);
102 T visitOptionalTypeRef(OptionalTypeRef node);
103 T visitFunctionTypeRef(FunctionTypeRef node);
104 T visitAnyTypeRef(AnyTypeRef node);
105 T visitUnknownTypeRef(UnknownTypeRef node);
106 T visitArrayTypeRef(ArrayTypeRef node);
107 }
108
97 class BaseVisitor<T> implements NodeVisitor<T> { 109 class BaseVisitor<T> implements NodeVisitor<T> {
98 T visitNode(Node node) { 110 T visitNode(Node node) {
99 node.visitChildren(this); 111 node.visitChildren(this);
100 return null; 112 return null;
101 } 113 }
102 114
103 T visitProgram(Program node) => visitNode(node); 115 T visitProgram(Program node) => visitNode(node);
104 116
105 T visitStatement(Statement node) => visitModuleItem(node); 117 T visitStatement(Statement node) => visitModuleItem(node);
106 T visitLoop(Loop node) => visitStatement(node); 118 T visitLoop(Loop node) => visitStatement(node);
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 226
215 T visitAwait(Await node) => visitExpression(node); 227 T visitAwait(Await node) => visitExpression(node);
216 T visitDartYield(DartYield node) => visitStatement(node); 228 T visitDartYield(DartYield node) => visitStatement(node);
217 229
218 T visitBindingPattern(BindingPattern node) => visitNode(node); 230 T visitBindingPattern(BindingPattern node) => visitNode(node);
219 T visitArrayBindingPattern(ArrayBindingPattern node) 231 T visitArrayBindingPattern(ArrayBindingPattern node)
220 => visitBindingPattern(node); 232 => visitBindingPattern(node);
221 T visitObjectBindingPattern(ObjectBindingPattern node) 233 T visitObjectBindingPattern(ObjectBindingPattern node)
222 => visitBindingPattern(node); 234 => visitBindingPattern(node);
223 T visitDestructuredVariable(DestructuredVariable node) => visitNode(node); 235 T visitDestructuredVariable(DestructuredVariable node) => visitNode(node);
236
237 T visitTypeRef(TypeRef node) => visitNode(node);
238 T visitQualifiedTypeRef(QualifiedTypeRef node) => visitTypeRef(node);
239 T visitGenericTypeRef(GenericTypeRef node) => visitTypeRef(node);
240 T visitOptionalTypeRef(OptionalTypeRef node) => visitTypeRef(node);
241 T visitRecordTypeRef(RecordTypeRef node) => visitTypeRef(node);
242 T visitUnionTypeRef(UnionTypeRef node) => visitTypeRef(node);
243 T visitFunctionTypeRef(FunctionTypeRef node) => visitTypeRef(node);
244 T visitAnyTypeRef(AnyTypeRef node) => visitTypeRef(node);
245 T visitUnknownTypeRef(UnknownTypeRef node) => visitTypeRef(node);
246 T visitArrayTypeRef(ArrayTypeRef node) => visitTypeRef(node);
224 } 247 }
225 248
226 abstract class Node { 249 abstract class Node {
227 /// Sets the source location of this node. For performance reasons, we allow 250 /// Sets the source location of this node. For performance reasons, we allow
228 /// setting this after construction. 251 /// setting this after construction.
229 Object sourceInformation; 252 Object sourceInformation;
230 253
231 ClosureAnnotation _closureAnnotation; 254 ClosureAnnotation _closureAnnotation;
232 /// Closure annotation of this node. 255 /// Closure annotation of this node.
233 ClosureAnnotation get closureAnnotation => _closureAnnotation; 256 ClosureAnnotation get closureAnnotation => _closureAnnotation;
(...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after
773 new VariableInitialization(declaration, value); 796 new VariableInitialization(declaration, value);
774 } 797 }
775 798
776 abstract class VariableBinding extends Expression { 799 abstract class VariableBinding extends Expression {
777 } 800 }
778 801
779 class DestructuredVariable extends Expression implements Parameter { 802 class DestructuredVariable extends Expression implements Parameter {
780 final Identifier name; 803 final Identifier name;
781 final BindingPattern structure; 804 final BindingPattern structure;
782 final Expression defaultValue; 805 final Expression defaultValue;
783 DestructuredVariable({this.name, this.structure, this.defaultValue}) { 806 final TypeRef type;
807 DestructuredVariable({this.name, this.structure, this.defaultValue, this.type} ) {
784 assert(name != null || structure != null); 808 assert(name != null || structure != null);
785 } 809 }
786 810
787 accept(NodeVisitor visitor) => visitor.visitDestructuredVariable(this); 811 accept(NodeVisitor visitor) => visitor.visitDestructuredVariable(this);
788 void visitChildren(NodeVisitor visitor) { 812 void visitChildren(NodeVisitor visitor) {
789 name?.accept(visitor); 813 name?.accept(visitor);
790 structure?.accept(visitor); 814 structure?.accept(visitor);
791 defaultValue?.accept(visitor); 815 defaultValue?.accept(visitor);
792 } 816 }
793 817
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
1015 1039
1016 Postfix _clone() => new Postfix(op, argument); 1040 Postfix _clone() => new Postfix(op, argument);
1017 1041
1018 void visitChildren(NodeVisitor visitor) { 1042 void visitChildren(NodeVisitor visitor) {
1019 argument.accept(visitor); 1043 argument.accept(visitor);
1020 } 1044 }
1021 1045
1022 int get precedenceLevel => UNARY; 1046 int get precedenceLevel => UNARY;
1023 } 1047 }
1024 1048
1025 abstract class Parameter implements Expression, VariableBinding {} 1049 abstract class Parameter implements Expression, VariableBinding {
1050 TypeRef get type;
1051 }
1026 1052
1027 class Identifier extends Expression implements Parameter, VariableBinding { 1053 class Identifier extends Expression implements Parameter, VariableBinding {
1028 final String name; 1054 final String name;
1029 final bool allowRename; 1055 final bool allowRename;
1056 final TypeRef type;
1030 1057
1031 Identifier(this.name, {this.allowRename: true}) { 1058 Identifier(this.name, {this.allowRename: true, this.type}) {
1032 assert(_identifierRE.hasMatch(name)); 1059 if (!_identifierRE.hasMatch(name)) {
1060 throw new ArgumentError.value(name, "name", "not a valid identifier");
1061 }
1033 } 1062 }
1034 static RegExp _identifierRE = new RegExp(r'^[A-Za-z_$][A-Za-z_$0-9]*$'); 1063 static RegExp _identifierRE = new RegExp(r'^[A-Za-z_$][A-Za-z_$0-9]*$');
1035 1064
1036 Identifier _clone() => 1065 Identifier _clone() =>
1037 new Identifier(name, allowRename: allowRename); 1066 new Identifier(name, allowRename: allowRename);
1038 accept(NodeVisitor visitor) => visitor.visitIdentifier(this); 1067 accept(NodeVisitor visitor) => visitor.visitIdentifier(this);
1039 int get precedenceLevel => PRIMARY; 1068 int get precedenceLevel => PRIMARY;
1040 void visitChildren(NodeVisitor visitor) {} 1069 void visitChildren(NodeVisitor visitor) {}
1041 } 1070 }
1042 1071
1043 // This is an expression for convenience in the AST. 1072 // This is an expression for convenience in the AST.
1044 class RestParameter extends Expression implements Parameter { 1073 class RestParameter extends Expression implements Parameter {
1045 final Identifier parameter; 1074 final Identifier parameter;
1075 TypeRef get type => null;
1046 1076
1047 RestParameter(this.parameter); 1077 RestParameter(this.parameter);
1048 1078
1049 RestParameter _clone() => new RestParameter(parameter); 1079 RestParameter _clone() => new RestParameter(parameter);
1050 accept(NodeVisitor visitor) => visitor.visitRestParameter(this); 1080 accept(NodeVisitor visitor) => visitor.visitRestParameter(this);
1051 void visitChildren(NodeVisitor visitor) { 1081 void visitChildren(NodeVisitor visitor) {
1052 parameter.accept(visitor); 1082 parameter.accept(visitor);
1053 } 1083 }
1054 int get precedenceLevel => PRIMARY; 1084 int get precedenceLevel => PRIMARY;
1055 } 1085 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1100 name.accept(visitor); 1130 name.accept(visitor);
1101 function.accept(visitor); 1131 function.accept(visitor);
1102 } 1132 }
1103 NamedFunction _clone() => new NamedFunction(name, function); 1133 NamedFunction _clone() => new NamedFunction(name, function);
1104 1134
1105 int get precedenceLevel => PRIMARY_LOW_PRECEDENCE; 1135 int get precedenceLevel => PRIMARY_LOW_PRECEDENCE;
1106 } 1136 }
1107 1137
1108 abstract class FunctionExpression extends Expression { 1138 abstract class FunctionExpression extends Expression {
1109 List<Parameter> get params; 1139 List<Parameter> get params;
1140
1110 get body; // Expression or block 1141 get body; // Expression or block
1142 /// For TypeScript / Closure's ES6_TYPED.
Jennifer Messerly 2016/02/09 01:21:33 Maybe: /// Type parameters passed to this generic
ochafik 2016/02/10 18:12:45 Done.
1143 List<Identifier> get typeArgs;
Jennifer Messerly 2016/02/09 01:21:33 These are type parameters, right? I'd suggest nami
ochafik 2016/02/10 18:12:45 Both kinds of generic functions are valid TS / ES6
1144 /// For TypeScript / Closure's ES6_TYPED.
Jennifer Messerly 2016/02/09 01:21:33 /// The return type of the function, if any.
ochafik 2016/02/10 18:12:45 Done.
1145 TypeRef get returnType;
1111 } 1146 }
1112 1147
1113 class Fun extends FunctionExpression { 1148 class Fun extends FunctionExpression {
1114 final List<Parameter> params; 1149 final List<Parameter> params;
1115 final Block body; 1150 final Block body;
1151 final List<Identifier> typeArgs;
Jennifer Messerly 2016/02/09 01:21:33 Not for now, but, what do you think about using @o
ochafik 2016/02/10 18:12:45 I love @override, so since you mentioned it... don
1152 final TypeRef returnType;
1116 /** Whether this is a JS generator (`function*`) that may contain `yield`. */ 1153 /** Whether this is a JS generator (`function*`) that may contain `yield`. */
1117 final bool isGenerator; 1154 final bool isGenerator;
1118 1155
1119 final AsyncModifier asyncModifier; 1156 final AsyncModifier asyncModifier;
1120 1157
1121 Fun(this.params, this.body, {this.isGenerator: false, 1158 Fun(this.params, this.body, {this.isGenerator: false,
1122 this.asyncModifier: const AsyncModifier.sync()}); 1159 this.asyncModifier: const AsyncModifier.sync(),
1160 this.typeArgs, this.returnType});
1123 1161
1124 accept(NodeVisitor visitor) => visitor.visitFun(this); 1162 accept(NodeVisitor visitor) => visitor.visitFun(this);
1125 1163
1126 void visitChildren(NodeVisitor visitor) { 1164 void visitChildren(NodeVisitor visitor) {
1127 for (Parameter param in params) param.accept(visitor); 1165 for (Parameter param in params) param.accept(visitor);
1128 body.accept(visitor); 1166 body.accept(visitor);
1129 } 1167 }
1130 1168
1131 Fun _clone() => new Fun(params, body, 1169 Fun _clone() => new Fun(params, body,
1132 isGenerator: isGenerator, asyncModifier: asyncModifier); 1170 isGenerator: isGenerator, asyncModifier: asyncModifier);
1133 1171
1134 int get precedenceLevel => PRIMARY_LOW_PRECEDENCE; 1172 int get precedenceLevel => PRIMARY_LOW_PRECEDENCE;
1135 } 1173 }
1136 1174
1137 class ArrowFun extends FunctionExpression { 1175 class ArrowFun extends FunctionExpression {
1138 final List<Parameter> params; 1176 final List<Parameter> params;
1139 final body; // Expression or Block 1177 final body; // Expression or Block
1178 final List<Identifier> typeArgs;
1179 final TypeRef returnType;
1140 1180
1141 ArrowFun(this.params, this.body); 1181 ArrowFun(this.params, this.body, {this.typeArgs, this.returnType});
1142 1182
1143 accept(NodeVisitor visitor) => visitor.visitArrowFun(this); 1183 accept(NodeVisitor visitor) => visitor.visitArrowFun(this);
1144 1184
1145 void visitChildren(NodeVisitor visitor) { 1185 void visitChildren(NodeVisitor visitor) {
1146 for (Parameter param in params) param.accept(visitor); 1186 for (Parameter param in params) param.accept(visitor);
1147 body.accept(visitor); 1187 body.accept(visitor);
1148 } 1188 }
1149 1189
1150 int get precedenceLevel => PRIMARY_LOW_PRECEDENCE; 1190 int get precedenceLevel => PRIMARY_LOW_PRECEDENCE;
1151 1191
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
1418 1458
1419 accept(NodeVisitor visitor) => visitor.visitClassDeclaration(this); 1459 accept(NodeVisitor visitor) => visitor.visitClassDeclaration(this);
1420 visitChildren(NodeVisitor visitor) => classExpr.accept(visitor); 1460 visitChildren(NodeVisitor visitor) => classExpr.accept(visitor);
1421 ClassDeclaration _clone() => new ClassDeclaration(classExpr); 1461 ClassDeclaration _clone() => new ClassDeclaration(classExpr);
1422 } 1462 }
1423 1463
1424 class ClassExpression extends Expression { 1464 class ClassExpression extends Expression {
1425 final Identifier name; 1465 final Identifier name;
1426 final Expression heritage; // Can be null. 1466 final Expression heritage; // Can be null.
1427 final List<Method> methods; 1467 final List<Method> methods;
1468 /// For TypeScript / Closure's ES6_TYPED.
Jennifer Messerly 2016/02/09 01:21:33 (Same comment -- I think we should give this a pro
ochafik 2016/02/10 18:12:45 Done.
1469 final List<Identifier> typeArgs;
1470 /// For TypeScript / Closure's ES6_TYPED.
1471 final List<VariableDeclarationList> fields;
1428 1472
1429 ClassExpression(this.name, this.heritage, this.methods); 1473 ClassExpression(this.name, this.heritage, this.methods,
1474 [this.typeArgs = const [], this.fields = const []]);
Jennifer Messerly 2016/02/09 01:21:33 I think it'd be fine to use a normal list. In gene
ochafik 2016/02/10 18:12:45 Done.
1430 1475
1431 accept(NodeVisitor visitor) => visitor.visitClassExpression(this); 1476 accept(NodeVisitor visitor) => visitor.visitClassExpression(this);
1432 1477
1433 void visitChildren(NodeVisitor visitor) { 1478 void visitChildren(NodeVisitor visitor) {
1434 name.accept(visitor); 1479 name.accept(visitor);
1435 if (heritage != null) heritage.accept(visitor); 1480 if (heritage != null) heritage.accept(visitor);
1436 for (Method element in methods) element.accept(visitor); 1481 for (Method element in methods) element.accept(visitor);
1482 for (var field in fields) field.accept(visitor);
1483 for (var typeParam in typeArgs) typeParam.accept(visitor);
1437 } 1484 }
1438 1485
1439 ClassExpression _clone() => new ClassExpression(name, heritage, methods); 1486 ClassExpression _clone() => new ClassExpression(name, heritage, methods);
1440 1487
1441 int get precedenceLevel => PRIMARY_LOW_PRECEDENCE; 1488 int get precedenceLevel => PRIMARY_LOW_PRECEDENCE;
1442 } 1489 }
1443 1490
1444 class Method extends Property { 1491 class Method extends Property {
1445 final bool isGetter; 1492 final bool isGetter;
1446 final bool isSetter; 1493 final bool isSetter;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1494 InterpolatedLiteral(this.nameOrPosition); 1541 InterpolatedLiteral(this.nameOrPosition);
1495 1542
1496 accept(NodeVisitor visitor) => visitor.visitInterpolatedLiteral(this); 1543 accept(NodeVisitor visitor) => visitor.visitInterpolatedLiteral(this);
1497 void visitChildren(NodeVisitor visitor) {} 1544 void visitChildren(NodeVisitor visitor) {}
1498 InterpolatedLiteral _clone() => new InterpolatedLiteral(nameOrPosition); 1545 InterpolatedLiteral _clone() => new InterpolatedLiteral(nameOrPosition);
1499 } 1546 }
1500 1547
1501 class InterpolatedParameter extends Expression with InterpolatedNode 1548 class InterpolatedParameter extends Expression with InterpolatedNode
1502 implements Identifier { 1549 implements Identifier {
1503 final nameOrPosition; 1550 final nameOrPosition;
1551 TypeRef get type => null;
1504 1552
1505 String get name { throw "InterpolatedParameter.name must not be invoked"; } 1553 String get name { throw "InterpolatedParameter.name must not be invoked"; }
1506 bool get allowRename => false; 1554 bool get allowRename => false;
1507 1555
1508 InterpolatedParameter(this.nameOrPosition); 1556 InterpolatedParameter(this.nameOrPosition);
1509 1557
1510 accept(NodeVisitor visitor) => visitor.visitInterpolatedParameter(this); 1558 accept(NodeVisitor visitor) => visitor.visitInterpolatedParameter(this);
1511 void visitChildren(NodeVisitor visitor) {} 1559 void visitChildren(NodeVisitor visitor) {}
1512 InterpolatedParameter _clone() => new InterpolatedParameter(nameOrPosition); 1560 InterpolatedParameter _clone() => new InterpolatedParameter(nameOrPosition);
1513 1561
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1553 bool get isGetter => _unsupported; 1601 bool get isGetter => _unsupported;
1554 bool get isSetter => _unsupported; 1602 bool get isSetter => _unsupported;
1555 bool get isStatic => _unsupported; 1603 bool get isStatic => _unsupported;
1556 Fun get function => _unsupported; 1604 Fun get function => _unsupported;
1557 get _unsupported => throw '$runtimeType does not support this member.'; 1605 get _unsupported => throw '$runtimeType does not support this member.';
1558 } 1606 }
1559 1607
1560 class InterpolatedIdentifier extends Expression with InterpolatedNode 1608 class InterpolatedIdentifier extends Expression with InterpolatedNode
1561 implements Identifier { 1609 implements Identifier {
1562 final nameOrPosition; 1610 final nameOrPosition;
1611 TypeRef get type => null;
1563 1612
1564 InterpolatedIdentifier(this.nameOrPosition); 1613 InterpolatedIdentifier(this.nameOrPosition);
1565 1614
1566 accept(NodeVisitor visitor) => 1615 accept(NodeVisitor visitor) =>
1567 visitor.visitInterpolatedIdentifier(this); 1616 visitor.visitInterpolatedIdentifier(this);
1568 void visitChildren(NodeVisitor visitor) {} 1617 void visitChildren(NodeVisitor visitor) {}
1569 InterpolatedIdentifier _clone() => new InterpolatedIdentifier(nameOrPosition); 1618 InterpolatedIdentifier _clone() => new InterpolatedIdentifier(nameOrPosition);
1570 1619
1571 int get precedenceLevel => PRIMARY; 1620 int get precedenceLevel => PRIMARY;
1572 String get name => throw '$runtimeType does not support this member.'; 1621 String get name => throw '$runtimeType does not support this member.';
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
1755 1804
1756 final List<ModuleItem> body; 1805 final List<ModuleItem> body;
1757 Module(this.body, {this.name}); 1806 Module(this.body, {this.name});
1758 1807
1759 accept(NodeVisitor visitor) => visitor.visitModule(this); 1808 accept(NodeVisitor visitor) => visitor.visitModule(this);
1760 void visitChildren(NodeVisitor visitor) { 1809 void visitChildren(NodeVisitor visitor) {
1761 for (ModuleItem item in body) item.accept(visitor); 1810 for (ModuleItem item in body) item.accept(visitor);
1762 } 1811 }
1763 Module _clone() => new Module(body); 1812 Module _clone() => new Module(body);
1764 } 1813 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698