OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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.semantics_visitor.resolver; | 5 library dart2js.semantics_visitor.resolver; |
6 | 6 |
7 import '../common.dart'; | 7 import '../common.dart'; |
8 import '../constants/expressions.dart'; | 8 import '../constants/expressions.dart'; |
9 import '../dart_types.dart'; | 9 import '../dart_types.dart'; |
10 import '../elements/elements.dart'; | 10 import '../elements/elements.dart'; |
11 import '../tree/tree.dart'; | 11 import '../tree/tree.dart'; |
12 | 12 |
13 import 'semantic_visitor.dart'; | 13 import 'semantic_visitor.dart'; |
14 import 'send_structure.dart'; | 14 import 'send_structure.dart'; |
15 import 'tree_elements.dart'; | 15 import 'tree_elements.dart'; |
16 | 16 |
17 abstract class DeclStructure<R, A> { | 17 abstract class DeclStructure<R, A> { |
18 final FunctionElement element; | 18 final FunctionElement element; |
19 | 19 |
20 DeclStructure(this.element); | 20 DeclStructure(this.element); |
21 | 21 |
22 /// Calls the matching visit method on [visitor] with [node] and [arg]. | 22 /// Calls the matching visit method on [visitor] with [node] and [arg]. |
23 R dispatch(SemanticDeclarationVisitor<R, A> visitor, | 23 R dispatch( |
24 FunctionExpression node, | 24 SemanticDeclarationVisitor<R, A> visitor, FunctionExpression node, A arg); |
25 A arg); | |
26 } | 25 } |
27 | 26 |
28 enum ConstructorKind { | 27 enum ConstructorKind { |
29 GENERATIVE, | 28 GENERATIVE, |
30 REDIRECTING_GENERATIVE, | 29 REDIRECTING_GENERATIVE, |
31 FACTORY, | 30 FACTORY, |
32 REDIRECTING_FACTORY, | 31 REDIRECTING_FACTORY, |
33 } | 32 } |
34 | 33 |
35 class ConstructorDeclStructure<R, A> extends DeclStructure<R, A> { | 34 class ConstructorDeclStructure<R, A> extends DeclStructure<R, A> { |
36 final ConstructorKind kind; | 35 final ConstructorKind kind; |
37 | 36 |
38 ConstructorDeclStructure(this.kind, ConstructorElement constructor) | 37 ConstructorDeclStructure(this.kind, ConstructorElement constructor) |
39 : super(constructor); | 38 : super(constructor); |
40 | 39 |
41 R dispatch(SemanticDeclarationVisitor<R, A> visitor, | 40 R dispatch(SemanticDeclarationVisitor<R, A> visitor, FunctionExpression node, |
42 FunctionExpression node, | 41 A arg) { |
43 A arg) { | |
44 switch (kind) { | 42 switch (kind) { |
45 case ConstructorKind.GENERATIVE: | 43 case ConstructorKind.GENERATIVE: |
46 return visitor.visitGenerativeConstructorDeclaration( | 44 return visitor.visitGenerativeConstructorDeclaration( |
47 node, element, node.parameters, node.initializers, node.body, arg); | 45 node, element, node.parameters, node.initializers, node.body, arg); |
48 case ConstructorKind.REDIRECTING_GENERATIVE: | 46 case ConstructorKind.REDIRECTING_GENERATIVE: |
49 return visitor.visitRedirectingGenerativeConstructorDeclaration( | 47 return visitor.visitRedirectingGenerativeConstructorDeclaration( |
50 node, element, node.parameters, node.initializers, arg); | 48 node, element, node.parameters, node.initializers, arg); |
51 case ConstructorKind.FACTORY: | 49 case ConstructorKind.FACTORY: |
52 return visitor.visitFactoryConstructorDeclaration( | 50 return visitor.visitFactoryConstructorDeclaration( |
53 node, element, node.parameters, node.body, arg); | 51 node, element, node.parameters, node.body, arg); |
54 default: | 52 default: |
55 break; | 53 break; |
56 } | 54 } |
57 throw new SpannableAssertionFailure(node, | 55 throw new SpannableAssertionFailure( |
58 "Unhandled constructor declaration kind: ${kind}"); | 56 node, "Unhandled constructor declaration kind: ${kind}"); |
59 } | 57 } |
60 } | 58 } |
61 | 59 |
62 class RedirectingFactoryConstructorDeclStructure<R, A> | 60 class RedirectingFactoryConstructorDeclStructure<R, A> |
63 extends DeclStructure<R, A> { | 61 extends DeclStructure<R, A> { |
64 InterfaceType redirectionTargetType; | 62 InterfaceType redirectionTargetType; |
65 ConstructorElement redirectionTarget; | 63 ConstructorElement redirectionTarget; |
66 | 64 |
67 RedirectingFactoryConstructorDeclStructure( | 65 RedirectingFactoryConstructorDeclStructure(ConstructorElement constructor, |
68 ConstructorElement constructor, | 66 this.redirectionTargetType, this.redirectionTarget) |
69 this.redirectionTargetType, | |
70 this.redirectionTarget) | |
71 : super(constructor); | 67 : super(constructor); |
72 | 68 |
73 R dispatch(SemanticDeclarationVisitor<R, A> visitor, | 69 R dispatch(SemanticDeclarationVisitor<R, A> visitor, FunctionExpression node, |
74 FunctionExpression node, | 70 A arg) { |
75 A arg) { | 71 return visitor.visitRedirectingFactoryConstructorDeclaration(node, element, |
76 return visitor.visitRedirectingFactoryConstructorDeclaration( | 72 node.parameters, redirectionTargetType, redirectionTarget, arg); |
77 node, element, node.parameters, | |
78 redirectionTargetType, redirectionTarget, arg); | |
79 } | 73 } |
80 } | 74 } |
81 | 75 |
82 enum FunctionKind { | 76 enum FunctionKind { |
83 TOP_LEVEL_GETTER, | 77 TOP_LEVEL_GETTER, |
84 TOP_LEVEL_SETTER, | 78 TOP_LEVEL_SETTER, |
85 TOP_LEVEL_FUNCTION, | 79 TOP_LEVEL_FUNCTION, |
86 STATIC_GETTER, | 80 STATIC_GETTER, |
87 STATIC_SETTER, | 81 STATIC_SETTER, |
88 STATIC_FUNCTION, | 82 STATIC_FUNCTION, |
89 ABSTRACT_GETTER, | 83 ABSTRACT_GETTER, |
90 ABSTRACT_SETTER, | 84 ABSTRACT_SETTER, |
91 ABSTRACT_METHOD, | 85 ABSTRACT_METHOD, |
92 INSTANCE_GETTER, | 86 INSTANCE_GETTER, |
93 INSTANCE_SETTER, | 87 INSTANCE_SETTER, |
94 INSTANCE_METHOD, | 88 INSTANCE_METHOD, |
95 LOCAL_FUNCTION, | 89 LOCAL_FUNCTION, |
96 CLOSURE, | 90 CLOSURE, |
97 } | 91 } |
98 | 92 |
99 class FunctionDeclStructure<R, A> | 93 class FunctionDeclStructure<R, A> extends DeclStructure<R, A> { |
100 extends DeclStructure<R, A> { | |
101 final FunctionKind kind; | 94 final FunctionKind kind; |
102 | 95 |
103 FunctionDeclStructure(this.kind, FunctionElement function) | 96 FunctionDeclStructure(this.kind, FunctionElement function) : super(function); |
104 : super(function); | |
105 | 97 |
106 R dispatch(SemanticDeclarationVisitor<R, A> visitor, | 98 R dispatch(SemanticDeclarationVisitor<R, A> visitor, FunctionExpression node, |
107 FunctionExpression node, | 99 A arg) { |
108 A arg) { | |
109 switch (kind) { | 100 switch (kind) { |
110 case FunctionKind.TOP_LEVEL_GETTER: | 101 case FunctionKind.TOP_LEVEL_GETTER: |
111 return visitor.visitTopLevelGetterDeclaration( | 102 return visitor.visitTopLevelGetterDeclaration( |
112 node, element, node.body, arg); | 103 node, element, node.body, arg); |
113 case FunctionKind.TOP_LEVEL_SETTER: | 104 case FunctionKind.TOP_LEVEL_SETTER: |
114 return visitor.visitTopLevelSetterDeclaration( | 105 return visitor.visitTopLevelSetterDeclaration( |
115 node, element, node.parameters, node.body, arg); | 106 node, element, node.parameters, node.body, arg); |
116 case FunctionKind.TOP_LEVEL_FUNCTION: | 107 case FunctionKind.TOP_LEVEL_FUNCTION: |
117 return visitor.visitTopLevelFunctionDeclaration( | 108 return visitor.visitTopLevelFunctionDeclaration( |
118 node, element, node.parameters, node.body, arg); | 109 node, element, node.parameters, node.body, arg); |
119 case FunctionKind.STATIC_GETTER: | 110 case FunctionKind.STATIC_GETTER: |
120 return visitor.visitStaticGetterDeclaration( | 111 return visitor.visitStaticGetterDeclaration( |
121 node, element, node.body, arg); | 112 node, element, node.body, arg); |
122 case FunctionKind.STATIC_SETTER: | 113 case FunctionKind.STATIC_SETTER: |
123 return visitor.visitStaticSetterDeclaration( | 114 return visitor.visitStaticSetterDeclaration( |
124 node, element, node.parameters, node.body, arg); | 115 node, element, node.parameters, node.body, arg); |
125 case FunctionKind.STATIC_FUNCTION: | 116 case FunctionKind.STATIC_FUNCTION: |
126 return visitor.visitStaticFunctionDeclaration( | 117 return visitor.visitStaticFunctionDeclaration( |
127 node, element, node.parameters, node.body, arg); | 118 node, element, node.parameters, node.body, arg); |
128 case FunctionKind.ABSTRACT_GETTER: | 119 case FunctionKind.ABSTRACT_GETTER: |
129 return visitor.visitAbstractGetterDeclaration( | 120 return visitor.visitAbstractGetterDeclaration(node, element, arg); |
130 node, element, arg); | |
131 case FunctionKind.ABSTRACT_SETTER: | 121 case FunctionKind.ABSTRACT_SETTER: |
132 return visitor.visitAbstractSetterDeclaration( | 122 return visitor.visitAbstractSetterDeclaration( |
133 node, element, node.parameters, arg); | 123 node, element, node.parameters, arg); |
134 case FunctionKind.ABSTRACT_METHOD: | 124 case FunctionKind.ABSTRACT_METHOD: |
135 return visitor.visitAbstractMethodDeclaration( | 125 return visitor.visitAbstractMethodDeclaration( |
136 node, element, node.parameters, arg); | 126 node, element, node.parameters, arg); |
137 case FunctionKind.INSTANCE_GETTER: | 127 case FunctionKind.INSTANCE_GETTER: |
138 return visitor.visitInstanceGetterDeclaration( | 128 return visitor.visitInstanceGetterDeclaration( |
139 node, element, node.body, arg); | 129 node, element, node.body, arg); |
140 case FunctionKind.INSTANCE_SETTER: | 130 case FunctionKind.INSTANCE_SETTER: |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 } | 265 } |
276 | 266 |
277 List<ParameterStructure> computeParameterStructures(NodeList parameters) { | 267 List<ParameterStructure> computeParameterStructures(NodeList parameters) { |
278 List<ParameterStructure> list = <ParameterStructure>[]; | 268 List<ParameterStructure> list = <ParameterStructure>[]; |
279 int index = 0; | 269 int index = 0; |
280 for (Node node in parameters) { | 270 for (Node node in parameters) { |
281 NodeList optionalParameters = node.asNodeList(); | 271 NodeList optionalParameters = node.asNodeList(); |
282 if (optionalParameters != null) { | 272 if (optionalParameters != null) { |
283 bool isNamed = optionalParameters.beginToken.stringValue == '{'; | 273 bool isNamed = optionalParameters.beginToken.stringValue == '{'; |
284 for (Node node in optionalParameters) { | 274 for (Node node in optionalParameters) { |
285 list.add(computeParameterStructure( | 275 list.add(computeParameterStructure(node, index++, |
286 node, index++, isRequired: false, isNamed: isNamed)); | 276 isRequired: false, isNamed: isNamed)); |
287 } | 277 } |
288 } else { | 278 } else { |
289 list.add(computeParameterStructure(node, index++)); | 279 list.add(computeParameterStructure(node, index++)); |
290 } | 280 } |
291 } | 281 } |
292 return list; | 282 return list; |
293 } | 283 } |
294 | 284 |
295 ParameterStructure computeParameterStructure( | 285 ParameterStructure computeParameterStructure( |
296 VariableDefinitions definitions, | 286 VariableDefinitions definitions, int index, |
297 int index, | |
298 {bool isRequired: true, bool isNamed: false}) { | 287 {bool isRequired: true, bool isNamed: false}) { |
299 Node node = definitions.definitions.nodes.single; | 288 Node node = definitions.definitions.nodes.single; |
300 ParameterElement element = elements[node]; | 289 ParameterElement element = elements[node]; |
301 if (element == null) { | 290 if (element == null) { |
302 throw new SpannableAssertionFailure( | 291 throw new SpannableAssertionFailure( |
303 node, "No parameter structure for $node."); | 292 node, "No parameter structure for $node."); |
304 } | 293 } |
305 if (isRequired) { | 294 if (isRequired) { |
306 return new RequiredParameterStructure( | 295 return new RequiredParameterStructure(definitions, node, element, index); |
307 definitions, node, element, index); | |
308 } else { | 296 } else { |
309 // TODO(johnniwinther): Should we differentiate between implicit (null) | 297 // TODO(johnniwinther): Should we differentiate between implicit (null) |
310 // and explicit values? What about optional parameters on redirecting | 298 // and explicit values? What about optional parameters on redirecting |
311 // factories? | 299 // factories? |
312 if (isNamed) { | 300 if (isNamed) { |
313 return new NamedParameterStructure( | 301 return new NamedParameterStructure( |
314 definitions, node, element, element.constant); | 302 definitions, node, element, element.constant); |
315 } else { | 303 } else { |
316 return new OptionalParameterStructure( | 304 return new OptionalParameterStructure( |
317 definitions, node, element, element.constant, index); | 305 definitions, node, element, element.constant, index); |
318 } | 306 } |
319 } | 307 } |
320 } | 308 } |
321 | 309 |
322 void computeVariableStructures( | 310 void computeVariableStructures(VariableDefinitions definitions, |
323 VariableDefinitions definitions, | |
324 void callback(Node node, VariableStructure structure)) { | 311 void callback(Node node, VariableStructure structure)) { |
325 for (Node node in definitions.definitions) { | 312 for (Node node in definitions.definitions) { |
326 callback(definitions, computeVariableStructure(node)); | 313 callback(definitions, computeVariableStructure(node)); |
327 } | 314 } |
328 } | 315 } |
329 | 316 |
330 VariableStructure computeVariableStructure(Node node) { | 317 VariableStructure computeVariableStructure(Node node) { |
331 VariableElement element = elements[node]; | 318 VariableElement element = elements[node]; |
332 VariableKind kind; | 319 VariableKind kind; |
333 if (element.isLocal) { | 320 if (element.isLocal) { |
334 kind = VariableKind.LOCAL_VARIABLE; | 321 kind = VariableKind.LOCAL_VARIABLE; |
335 } else if (element.isInstanceMember) { | 322 } else if (element.isInstanceMember) { |
336 kind = VariableKind.INSTANCE_FIELD; | 323 kind = VariableKind.INSTANCE_FIELD; |
337 } else if (element.isStatic) { | 324 } else if (element.isStatic) { |
338 kind = VariableKind.STATIC_FIELD; | 325 kind = VariableKind.STATIC_FIELD; |
339 } else if (element.isTopLevel) { | 326 } else if (element.isTopLevel) { |
340 kind = VariableKind.TOP_LEVEL_FIELD; | 327 kind = VariableKind.TOP_LEVEL_FIELD; |
341 } else { | 328 } else { |
342 return internalError(node, "Unexpected variable $element."); | 329 return internalError(node, "Unexpected variable $element."); |
343 } | 330 } |
344 if (element.isConst) { | 331 if (element.isConst) { |
345 ConstantExpression constant = elements.getConstant(element.initializer); | 332 ConstantExpression constant = elements.getConstant(element.initializer); |
346 return new ConstantVariableStructure(kind, node, element, constant); | 333 return new ConstantVariableStructure(kind, node, element, constant); |
347 } else { | 334 } else { |
348 return new NonConstantVariableStructure(kind, node, element); | 335 return new NonConstantVariableStructure(kind, node, element); |
349 } | 336 } |
350 } | 337 } |
351 } | 338 } |
OLD | NEW |