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 #import("../../../lib/compiler/implementation/leg.dart"); | 5 #import("../../../lib/compiler/implementation/leg.dart"); |
6 #import("../../../lib/compiler/implementation/elements/elements.dart"); | 6 #import("../../../lib/compiler/implementation/elements/elements.dart"); |
7 #import("../../../lib/compiler/implementation/tree/tree.dart"); | 7 #import("../../../lib/compiler/implementation/tree/tree.dart"); |
8 #import("../../../lib/compiler/implementation/util/util.dart"); | 8 #import("../../../lib/compiler/implementation/util/util.dart"); |
9 #import("mock_compiler.dart"); | 9 #import("mock_compiler.dart"); |
10 #import("parser_helper.dart"); | 10 #import("parser_helper.dart"); |
(...skipping 24 matching lines...) Expand all Loading... |
35 ResolverVisitor visitor = compiler.resolverVisitor(); | 35 ResolverVisitor visitor = compiler.resolverVisitor(); |
36 Element element = visitor.visit(createLocals(variables)); | 36 Element element = visitor.visit(createLocals(variables)); |
37 // A VariableDefinitions does not have an element. | 37 // A VariableDefinitions does not have an element. |
38 Expect.equals(null, element); | 38 Expect.equals(null, element); |
39 Expect.equals(variables.length, map(visitor).length); | 39 Expect.equals(variables.length, map(visitor).length); |
40 | 40 |
41 for (final variable in variables) { | 41 for (final variable in variables) { |
42 final name = variable[0]; | 42 final name = variable[0]; |
43 Identifier id = buildIdentifier(name); | 43 Identifier id = buildIdentifier(name); |
44 final VariableElement variableElement = visitor.visit(id); | 44 final VariableElement variableElement = visitor.visit(id); |
45 MethodScope scope = visitor.context; | 45 MethodScope scope = visitor.scope; |
46 Expect.equals(variableElement, scope.elements[buildSourceString(name)]); | 46 Expect.equals(variableElement, scope.elements[buildSourceString(name)]); |
47 } | 47 } |
48 return compiler; | 48 return compiler; |
49 } | 49 } |
50 | 50 |
51 main() { | 51 main() { |
52 testLocalsOne(); | 52 testLocalsOne(); |
53 testLocalsTwo(); | 53 testLocalsTwo(); |
54 testLocalsThree(); | 54 testLocalsThree(); |
55 testLocalsFour(); | 55 testLocalsFour(); |
(...skipping 14 matching lines...) Expand all Loading... |
70 testSuperCalls(); | 70 testSuperCalls(); |
71 testTypeVariables(); | 71 testTypeVariables(); |
72 } | 72 } |
73 | 73 |
74 testTypeVariables() { | 74 testTypeVariables() { |
75 matchResolvedTypes(visitor, text, name, expectedElements) { | 75 matchResolvedTypes(visitor, text, name, expectedElements) { |
76 VariableDefinitions definition = parseStatement(text); | 76 VariableDefinitions definition = parseStatement(text); |
77 visitor.visit(definition.type); | 77 visitor.visit(definition.type); |
78 InterfaceType type = visitor.mapping.getType(definition.type); | 78 InterfaceType type = visitor.mapping.getType(definition.type); |
79 Expect.equals(definition.type.typeArguments.length(), | 79 Expect.equals(definition.type.typeArguments.length(), |
80 length(type.arguments)); | 80 length(type.typeArguments)); |
81 int index = 0; | 81 int index = 0; |
82 Link<Type> arguments = type.arguments; | 82 Link<Type> arguments = type.typeArguments; |
83 while (!arguments.isEmpty()) { | 83 while (!arguments.isEmpty()) { |
84 Expect.equals(true, index < expectedElements.length); | 84 Expect.equals(true, index < expectedElements.length); |
85 Expect.equals(expectedElements[index], arguments.head.element); | 85 Expect.equals(expectedElements[index], arguments.head.element); |
86 index++; | 86 index++; |
87 arguments = arguments.tail; | 87 arguments = arguments.tail; |
88 } | 88 } |
89 Expect.equals(index, expectedElements.length); | 89 Expect.equals(index, expectedElements.length); |
90 } | 90 } |
91 | 91 |
92 MockCompiler compiler = new MockCompiler(); | 92 MockCompiler compiler = new MockCompiler(); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 compiler.errors[0].message); | 209 compiler.errors[0].message); |
210 } | 210 } |
211 | 211 |
212 | 212 |
213 testLocalsTwo() { | 213 testLocalsTwo() { |
214 MockCompiler compiler = new MockCompiler(); | 214 MockCompiler compiler = new MockCompiler(); |
215 ResolverVisitor visitor = compiler.resolverVisitor(); | 215 ResolverVisitor visitor = compiler.resolverVisitor(); |
216 Node tree = parseStatement("if (true) { var a = 1; var b = 2; }"); | 216 Node tree = parseStatement("if (true) { var a = 1; var b = 2; }"); |
217 Element element = visitor.visit(tree); | 217 Element element = visitor.visit(tree); |
218 Expect.equals(null, element); | 218 Expect.equals(null, element); |
219 BlockScope scope = visitor.context; | 219 BlockScope scope = visitor.scope; |
220 Expect.equals(0, scope.elements.length); | 220 Expect.equals(0, scope.elements.length); |
221 Expect.equals(2, map(visitor).length); | 221 Expect.equals(2, map(visitor).length); |
222 | 222 |
223 List<Element> elements = map(visitor).getValues(); | 223 List<Element> elements = map(visitor).getValues(); |
224 Expect.notEquals(elements[0], elements[1]); | 224 Expect.notEquals(elements[0], elements[1]); |
225 } | 225 } |
226 | 226 |
227 testLocalsThree() { | 227 testLocalsThree() { |
228 MockCompiler compiler = new MockCompiler(); | 228 MockCompiler compiler = new MockCompiler(); |
229 ResolverVisitor visitor = compiler.resolverVisitor(); | 229 ResolverVisitor visitor = compiler.resolverVisitor(); |
230 Node tree = parseStatement("{ var a = 1; if (true) { a; } }"); | 230 Node tree = parseStatement("{ var a = 1; if (true) { a; } }"); |
231 Element element = visitor.visit(tree); | 231 Element element = visitor.visit(tree); |
232 Expect.equals(null, element); | 232 Expect.equals(null, element); |
233 BlockScope scope = visitor.context; | 233 BlockScope scope = visitor.scope; |
234 Expect.equals(0, scope.elements.length); | 234 Expect.equals(0, scope.elements.length); |
235 Expect.equals(3, map(visitor).length); | 235 Expect.equals(3, map(visitor).length); |
236 List<Element> elements = map(visitor).getValues(); | 236 List<Element> elements = map(visitor).getValues(); |
237 Expect.equals(elements[0], elements[1]); | 237 Expect.equals(elements[0], elements[1]); |
238 } | 238 } |
239 | 239 |
240 testLocalsFour() { | 240 testLocalsFour() { |
241 MockCompiler compiler = new MockCompiler(); | 241 MockCompiler compiler = new MockCompiler(); |
242 ResolverVisitor visitor = compiler.resolverVisitor(); | 242 ResolverVisitor visitor = compiler.resolverVisitor(); |
243 Node tree = parseStatement("{ var a = 1; if (true) { var a = 1; } }"); | 243 Node tree = parseStatement("{ var a = 1; if (true) { var a = 1; } }"); |
244 Element element = visitor.visit(tree); | 244 Element element = visitor.visit(tree); |
245 Expect.equals(null, element); | 245 Expect.equals(null, element); |
246 BlockScope scope = visitor.context; | 246 BlockScope scope = visitor.scope; |
247 Expect.equals(0, scope.elements.length); | 247 Expect.equals(0, scope.elements.length); |
248 Expect.equals(2, map(visitor).length); | 248 Expect.equals(2, map(visitor).length); |
249 List<Element> elements = map(visitor).getValues(); | 249 List<Element> elements = map(visitor).getValues(); |
250 Expect.notEquals(elements[0], elements[1]); | 250 Expect.notEquals(elements[0], elements[1]); |
251 } | 251 } |
252 | 252 |
253 testLocalsFive() { | 253 testLocalsFive() { |
254 MockCompiler compiler = new MockCompiler(); | 254 MockCompiler compiler = new MockCompiler(); |
255 ResolverVisitor visitor = compiler.resolverVisitor(); | 255 ResolverVisitor visitor = compiler.resolverVisitor(); |
256 If tree = parseStatement("if (true) { var a = 1; a; } else { var a = 2; a;}"); | 256 If tree = parseStatement("if (true) { var a = 1; a; } else { var a = 2; a;}"); |
257 Element element = visitor.visit(tree); | 257 Element element = visitor.visit(tree); |
258 Expect.equals(null, element); | 258 Expect.equals(null, element); |
259 BlockScope scope = visitor.context; | 259 BlockScope scope = visitor.scope; |
260 Expect.equals(0, scope.elements.length); | 260 Expect.equals(0, scope.elements.length); |
261 Expect.equals(6, map(visitor).length); | 261 Expect.equals(6, map(visitor).length); |
262 | 262 |
263 Block thenPart = tree.thenPart; | 263 Block thenPart = tree.thenPart; |
264 List statements1 = thenPart.statements.nodes.toList(); | 264 List statements1 = thenPart.statements.nodes.toList(); |
265 Node def1 = statements1[0].definitions.nodes.head; | 265 Node def1 = statements1[0].definitions.nodes.head; |
266 Node id1 = statements1[1].expression; | 266 Node id1 = statements1[1].expression; |
267 Expect.equals(visitor.mapping[def1], visitor.mapping[id1]); | 267 Expect.equals(visitor.mapping[def1], visitor.mapping[id1]); |
268 | 268 |
269 Block elsePart = tree.elsePart; | 269 Block elsePart = tree.elsePart; |
(...skipping 25 matching lines...) Expand all Loading... |
295 Expect.equals(ElementKind.PARAMETER, visitor.mapping[use].kind); | 295 Expect.equals(ElementKind.PARAMETER, visitor.mapping[use].kind); |
296 Expect.equals(visitor.mapping[param], visitor.mapping[use]); | 296 Expect.equals(visitor.mapping[param], visitor.mapping[use]); |
297 } | 297 } |
298 | 298 |
299 testFor() { | 299 testFor() { |
300 MockCompiler compiler = new MockCompiler(); | 300 MockCompiler compiler = new MockCompiler(); |
301 ResolverVisitor visitor = compiler.resolverVisitor(); | 301 ResolverVisitor visitor = compiler.resolverVisitor(); |
302 For tree = parseStatement("for (int i = 0; i < 10; i = i + 1) { i = 5; }"); | 302 For tree = parseStatement("for (int i = 0; i < 10; i = i + 1) { i = 5; }"); |
303 visitor.visit(tree); | 303 visitor.visit(tree); |
304 | 304 |
305 BlockScope scope = visitor.context; | 305 BlockScope scope = visitor.scope; |
306 Expect.equals(0, scope.elements.length); | 306 Expect.equals(0, scope.elements.length); |
307 Expect.equals(10, map(visitor).length); | 307 Expect.equals(10, map(visitor).length); |
308 | 308 |
309 VariableDefinitions initializer = tree.initializer; | 309 VariableDefinitions initializer = tree.initializer; |
310 Node iNode = initializer.definitions.nodes.head; | 310 Node iNode = initializer.definitions.nodes.head; |
311 Element iElement = visitor.mapping[iNode]; | 311 Element iElement = visitor.mapping[iNode]; |
312 | 312 |
313 // Check that we have the expected nodes. This test relies on the mapping | 313 // Check that we have the expected nodes. This test relies on the mapping |
314 // field to be a linked hash map (preserving insertion order). | 314 // field to be a linked hash map (preserving insertion order). |
315 Expect.isTrue(map(visitor) is LinkedHashMap); | 315 Expect.isTrue(map(visitor) is LinkedHashMap); |
(...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
711 length(Link link) => link.isEmpty() ? 0 : length(link.tail) + 1; | 711 length(Link link) => link.isEmpty() ? 0 : length(link.tail) + 1; |
712 | 712 |
713 at(Link link, int index) => (index == 0) ? link.head : at(link.tail, index - 1); | 713 at(Link link, int index) => (index == 0) ? link.head : at(link.tail, index - 1); |
714 | 714 |
715 List<String> asSortedStrings(Link link) { | 715 List<String> asSortedStrings(Link link) { |
716 List<String> result = <String>[]; | 716 List<String> result = <String>[]; |
717 for (; !link.isEmpty(); link = link.tail) result.add(link.head.toString()); | 717 for (; !link.isEmpty(); link = link.tail) result.add(link.head.toString()); |
718 result.sort((s1, s2) => s1.compareTo(s2)); | 718 result.sort((s1, s2) => s1.compareTo(s2)); |
719 return result; | 719 return result; |
720 } | 720 } |
OLD | NEW |