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 SelectorMap<T> extends PartialTypeTree { | 5 class SelectorMap<T> extends PartialTypeTree { |
6 | 6 |
7 SelectorMap(Compiler compiler) : super(compiler); | 7 SelectorMap(Compiler compiler) : super(compiler); |
8 | 8 |
9 SelectorMapNode<T> newSpecializedNode(ClassElement type) | 9 SelectorMapNode<T> newSpecializedNode(ClassElement type) |
10 => new SelectorMapNode<T>(type); | 10 => new SelectorMapNode<T>(type); |
11 | 11 |
12 T operator [](Selector selector) { | 12 T operator [](Selector selector) { |
13 SelectorMapNode<T> node = findNode(selectorType(selector), false); | 13 SelectorMapNode<T> node = findNode(selectorType(selector), false); |
14 if (node == null) return null; | 14 if (node == null) return null; |
15 Link<SelectorValue<T>> selectors = node.selectorsByName[selector.name]; | 15 Link<SelectorValue<T>> selectors = node.selectorsByName[selector.name]; |
16 if (selectors == null) return null; | 16 if (selectors == null) return null; |
17 for (Link link = selectors; !link.isEmpty(); link = link.tail) { | 17 for (Link link = selectors; !link.isEmpty; link = link.tail) { |
18 SelectorValue<T> existing = link.head; | 18 SelectorValue<T> existing = link.head; |
19 if (existing.selector.equalsUntyped(selector)) return existing.value; | 19 if (existing.selector.equalsUntyped(selector)) return existing.value; |
20 } | 20 } |
21 return null; | 21 return null; |
22 } | 22 } |
23 | 23 |
24 void operator []=(Selector selector, T value) { | 24 void operator []=(Selector selector, T value) { |
25 SelectorMapNode<T> node = findNode(selectorType(selector), true); | 25 SelectorMapNode<T> node = findNode(selectorType(selector), true); |
26 Link<SelectorValue<T>> selectors = node.selectorsByName[selector.name]; | 26 Link<SelectorValue<T>> selectors = node.selectorsByName[selector.name]; |
27 if (selectors == null) { | 27 if (selectors == null) { |
28 // No existing selectors with the given name. Create a new | 28 // No existing selectors with the given name. Create a new |
29 // linked list. | 29 // linked list. |
30 SelectorValue<T> head = new SelectorValue<T>(selector, value); | 30 SelectorValue<T> head = new SelectorValue<T>(selector, value); |
31 node.selectorsByName[selector.name] = | 31 node.selectorsByName[selector.name] = |
32 new Link<SelectorValue<T>>().prepend(head); | 32 new Link<SelectorValue<T>>().prepend(head); |
33 } else { | 33 } else { |
34 // Run through the linked list of selectors with the same name. If | 34 // Run through the linked list of selectors with the same name. If |
35 // we find one that matches, we update the value in the mapping. | 35 // we find one that matches, we update the value in the mapping. |
36 for (Link link = selectors; !link.isEmpty(); link = link.tail) { | 36 for (Link link = selectors; !link.isEmpty; link = link.tail) { |
37 SelectorValue<T> existing = link.head; | 37 SelectorValue<T> existing = link.head; |
38 // It is safe to ignore the type here, because all selector | 38 // It is safe to ignore the type here, because all selector |
39 // mappings that are stored in a single node have the same type. | 39 // mappings that are stored in a single node have the same type. |
40 if (existing.selector.equalsUntyped(selector)) { | 40 if (existing.selector.equalsUntyped(selector)) { |
41 existing.value = value; | 41 existing.value = value; |
42 return; | 42 return; |
43 } | 43 } |
44 } | 44 } |
45 // We could not find an existing mapping for the selector, so | 45 // We could not find an existing mapping for the selector, so |
46 // we add a new one to the existing linked list. | 46 // we add a new one to the existing linked list. |
47 SelectorValue<T> head = new SelectorValue<T>(selector, value); | 47 SelectorValue<T> head = new SelectorValue<T>(selector, value); |
48 node.selectorsByName[selector.name] = selectors.prepend(head); | 48 node.selectorsByName[selector.name] = selectors.prepend(head); |
49 } | 49 } |
50 } | 50 } |
51 | 51 |
52 // TODO(kasperl): Share code with the [] operator? | 52 // TODO(kasperl): Share code with the [] operator? |
53 bool containsKey(Selector selector) { | 53 bool containsKey(Selector selector) { |
54 SelectorMapNode<T> node = findNode(selectorType(selector), false); | 54 SelectorMapNode<T> node = findNode(selectorType(selector), false); |
55 if (node == null) return false; | 55 if (node == null) return false; |
56 Link<SelectorValue<T>> selectors = node.selectorsByName[selector.name]; | 56 Link<SelectorValue<T>> selectors = node.selectorsByName[selector.name]; |
57 if (selectors == null) return false; | 57 if (selectors == null) return false; |
58 for (Link link = selectors; !link.isEmpty(); link = link.tail) { | 58 for (Link link = selectors; !link.isEmpty; link = link.tail) { |
59 SelectorValue<T> existing = link.head; | 59 SelectorValue<T> existing = link.head; |
60 if (existing.selector.equalsUntyped(selector)) return true; | 60 if (existing.selector.equalsUntyped(selector)) return true; |
61 } | 61 } |
62 return false; | 62 return false; |
63 } | 63 } |
64 | 64 |
65 /** | 65 /** |
66 * Visits all mappings for selectors that may be used to invoke the | 66 * Visits all mappings for selectors that may be used to invoke the |
67 * given [member] element. If the [visit] function ever returns false, | 67 * given [member] element. If the [visit] function ever returns false, |
68 * we abort the traversal early. | 68 * we abort the traversal early. |
69 */ | 69 */ |
70 void visitMatching(Element member, bool visit(Selector selector, T value)) { | 70 void visitMatching(Element member, bool visit(Selector selector, T value)) { |
71 assert(member.isMember()); | 71 assert(member.isMember()); |
72 if (root == null) return; | 72 if (root == null) return; |
73 // TODO(kasperl): For now, we use a different implementation for | 73 // TODO(kasperl): For now, we use a different implementation for |
74 // visiting if the tree contains interface subtypes. | 74 // visiting if the tree contains interface subtypes. |
75 if (containsInterfaceSubtypes) { | 75 if (containsInterfaceSubtypes) { |
76 visitAllMatching(member, visit); | 76 visitAllMatching(member, visit); |
77 } else { | 77 } else { |
78 visitHierarchyMatching(member, visit); | 78 visitHierarchyMatching(member, visit); |
79 } | 79 } |
80 } | 80 } |
81 | 81 |
82 void visitAllMatching(Element member, bool visit(selector, value)) { | 82 void visitAllMatching(Element member, bool visit(selector, value)) { |
83 root.visitRecursively((SelectorMapNode<T> node) { | 83 root.visitRecursively((SelectorMapNode<T> node) { |
84 Link<SelectorValue<T>> selectors = node.selectorsByName[member.name]; | 84 Link<SelectorValue<T>> selectors = node.selectorsByName[member.name]; |
85 if (selectors == null) return true; | 85 if (selectors == null) return true; |
86 for (Link link = selectors; !link.isEmpty(); link = link.tail) { | 86 for (Link link = selectors; !link.isEmpty; link = link.tail) { |
87 SelectorValue<T> existing = link.head; | 87 SelectorValue<T> existing = link.head; |
88 Selector selector = existing.selector; | 88 Selector selector = existing.selector; |
89 // Since we're running through the entire tree we have to use | 89 // Since we're running through the entire tree we have to use |
90 // the applies method that takes types into account. | 90 // the applies method that takes types into account. |
91 if (selector.applies(member, compiler)) { | 91 if (selector.applies(member, compiler)) { |
92 if (!visit(selector, existing.value)) return false; | 92 if (!visit(selector, existing.value)) return false; |
93 } | 93 } |
94 } | 94 } |
95 return true; | 95 return true; |
96 }); | 96 }); |
97 } | 97 } |
98 | 98 |
99 void visitHierarchyMatching(Element member, bool visit(selector, value)) { | 99 void visitHierarchyMatching(Element member, bool visit(selector, value)) { |
100 visitHierarchy(member.getEnclosingClass(), (SelectorMapNode<T> node) { | 100 visitHierarchy(member.getEnclosingClass(), (SelectorMapNode<T> node) { |
101 Link<SelectorValue<T>> selectors = node.selectorsByName[member.name]; | 101 Link<SelectorValue<T>> selectors = node.selectorsByName[member.name]; |
102 if (selectors == null) return true; | 102 if (selectors == null) return true; |
103 for (Link link = selectors; !link.isEmpty(); link = link.tail) { | 103 for (Link link = selectors; !link.isEmpty; link = link.tail) { |
104 SelectorValue<T> existing = link.head; | 104 SelectorValue<T> existing = link.head; |
105 Selector selector = existing.selector; | 105 Selector selector = existing.selector; |
106 if (selector.appliesUntyped(member, compiler)) { | 106 if (selector.appliesUntyped(member, compiler)) { |
107 if (!visit(selector, existing.value)) return false; | 107 if (!visit(selector, existing.value)) return false; |
108 } | 108 } |
109 } | 109 } |
110 return true; | 110 return true; |
111 }); | 111 }); |
112 } | 112 } |
113 | 113 |
114 } | 114 } |
115 | 115 |
116 class SelectorMapNode<T> extends PartialTypeTreeNode { | 116 class SelectorMapNode<T> extends PartialTypeTreeNode { |
117 | 117 |
118 final Map<SourceString, Link<SelectorValue<T>>> selectorsByName; | 118 final Map<SourceString, Link<SelectorValue<T>>> selectorsByName; |
119 | 119 |
120 SelectorMapNode(ClassElement type) : super(type), | 120 SelectorMapNode(ClassElement type) : super(type), |
121 selectorsByName = new Map<SourceString, Link<SelectorValue<T>>>(); | 121 selectorsByName = new Map<SourceString, Link<SelectorValue<T>>>(); |
122 | 122 |
123 } | 123 } |
124 | 124 |
125 class SelectorValue<T> { | 125 class SelectorValue<T> { |
126 final Selector selector; | 126 final Selector selector; |
127 T value; | 127 T value; |
128 SelectorValue(this.selector, this.value); | 128 SelectorValue(this.selector, this.value); |
129 toString() => "$selector -> $value"; | 129 toString() => "$selector -> $value"; |
130 } | 130 } |
OLD | NEW |