| 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 part of resolution; | 5 part of resolution; |
| 6 | 6 |
| 7 abstract class Scope { | 7 abstract class Scope { |
| 8 /** | 8 /** |
| 9 * Adds [element] to this scope. This operation is only allowed on mutable | 9 * Adds [element] to this scope. This operation is only allowed on mutable |
| 10 * scopes such as [MethodScope] and [BlockScope]. | 10 * scopes such as [MethodScope] and [BlockScope]. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 } | 34 } |
| 35 | 35 |
| 36 Element localLookup(SourceString name); | 36 Element localLookup(SourceString name); |
| 37 | 37 |
| 38 static Scope buildEnclosingScope(Element element) { | 38 static Scope buildEnclosingScope(Element element) { |
| 39 return element.enclosingElement != null | 39 return element.enclosingElement != null |
| 40 ? element.enclosingElement.buildScope() : element.buildScope(); | 40 ? element.enclosingElement.buildScope() : element.buildScope(); |
| 41 } | 41 } |
| 42 } | 42 } |
| 43 | 43 |
| 44 class VariableDefinitionScope extends NestedScope { | |
| 45 final SourceString variableName; | |
| 46 bool variableReferencedInInitializer = false; | |
| 47 | |
| 48 VariableDefinitionScope(Scope parent, this.variableName) : super(parent); | |
| 49 | |
| 50 Element localLookup(SourceString name) { | |
| 51 if (name == variableName) { | |
| 52 variableReferencedInInitializer = true; | |
| 53 } | |
| 54 return null; | |
| 55 } | |
| 56 | |
| 57 Element add(Element newElement) { | |
| 58 throw "Cannot add element to VariableDefinitionScope"; | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 /** | 44 /** |
| 63 * [TypeDeclarationScope] defines the outer scope of a type declaration in | 45 * [TypeDeclarationScope] defines the outer scope of a type declaration in |
| 64 * which the declared type variables and the entities in the enclosing scope are | 46 * which the declared type variables and the entities in the enclosing scope are |
| 65 * available but where declared and inherited members are not available. This | 47 * available but where declared and inherited members are not available. This |
| 66 * scope is only used for class declarations during resolution of the | 48 * scope is only used for class declarations during resolution of the |
| 67 * class hierarchy. In all other cases [ClassScope] is used. | 49 * class hierarchy. In all other cases [ClassScope] is used. |
| 68 */ | 50 */ |
| 69 class TypeDeclarationScope extends NestedScope { | 51 class TypeDeclarationScope extends NestedScope { |
| 70 final TypeDeclarationElement element; | 52 final TypeDeclarationElement element; |
| 71 | 53 |
| 72 TypeDeclarationScope(parent, this.element) | 54 TypeDeclarationScope(parent, this.element) |
| 73 : super(parent) { | 55 : super(parent) { |
| 74 assert(parent != null); | 56 assert(parent != null); |
| 75 } | 57 } |
| 76 | 58 |
| 77 Element add(Element newElement) { | 59 Element add(Element newElement) { |
| 78 throw "Cannot add element to TypeDeclarationScope"; | 60 throw "Cannot add element to TypeDeclarationScope($element)"; |
| 79 } | 61 } |
| 80 | 62 |
| 81 Element lookupTypeVariable(SourceString name) { | 63 Element lookupTypeVariable(SourceString name) { |
| 82 Link<DartType> typeVariableLink = element.typeVariables; | 64 Link<DartType> typeVariableLink = element.typeVariables; |
| 83 while (!typeVariableLink.isEmpty) { | 65 while (!typeVariableLink.isEmpty) { |
| 84 TypeVariableType typeVariable = typeVariableLink.head; | 66 TypeVariableType typeVariable = typeVariableLink.head; |
| 85 if (typeVariable.name == name) { | 67 if (typeVariable.name == name) { |
| 86 return typeVariable.element; | 68 return typeVariable.element; |
| 87 } | 69 } |
| 88 typeVariableLink = typeVariableLink.tail; | 70 typeVariableLink = typeVariableLink.tail; |
| 89 } | 71 } |
| 90 return null; | 72 return null; |
| 91 } | 73 } |
| 92 | 74 |
| 93 Element localLookup(SourceString name) => lookupTypeVariable(name); | 75 Element localLookup(SourceString name) => lookupTypeVariable(name); |
| 94 | 76 |
| 95 String toString() => | 77 String toString() => |
| 96 'TypeDeclarationScope($element)'; | 78 'TypeDeclarationScope($element)'; |
| 97 } | 79 } |
| 98 | 80 |
| 99 abstract class MutableScope extends NestedScope { | 81 class BlockScope extends NestedScope { |
| 100 final Map<SourceString, Element> elements; | 82 final Map<SourceString, Element> elements; |
| 101 | 83 |
| 102 MutableScope(Scope parent) | 84 BlockScope(Scope parent) |
| 103 : super(parent), | 85 : elements = <SourceString, Element>{}, super(parent) { |
| 104 this.elements = new Map<SourceString, Element>() { | |
| 105 assert(parent != null); | 86 assert(parent != null); |
| 106 } | 87 } |
| 107 | 88 |
| 108 Element add(Element newElement) { | 89 Element add(Element element) { |
| 109 if (elements.containsKey(newElement.name)) { | 90 return elements.putIfAbsent(element.name, () => element); |
| 110 return elements[newElement.name]; | |
| 111 } | |
| 112 elements[newElement.name] = newElement; | |
| 113 return newElement; | |
| 114 } | 91 } |
| 115 | 92 |
| 116 Element localLookup(SourceString name) => elements[name]; | 93 Element localLookup(SourceString name) => elements[name]; |
| 94 |
| 95 String toString() => 'BlockScope(${elements.keys.toList()})'; |
| 117 } | 96 } |
| 118 | 97 |
| 119 class MethodScope extends MutableScope { | 98 class MethodScope extends BlockScope { |
| 120 final Element element; | 99 final Element element; |
| 121 | 100 |
| 122 MethodScope(Scope parent, this.element) | 101 MethodScope(Scope parent, this.element) : super(parent) { |
| 123 : super(parent); | 102 assert(elements != null); |
| 103 } |
| 124 | 104 |
| 125 String toString() => 'MethodScope($element${elements.keys.toList()})'; | 105 String toString() => 'MethodScope($element${elements.keys.toList()})'; |
| 126 } | 106 } |
| 127 | 107 |
| 128 class BlockScope extends MutableScope { | |
| 129 BlockScope(Scope parent) : super(parent); | |
| 130 | |
| 131 String toString() => 'BlockScope(${elements.keys.toList()})'; | |
| 132 } | |
| 133 | |
| 134 /** | 108 /** |
| 135 * [ClassScope] defines the inner scope of a class/interface declaration in | 109 * [ClassScope] defines the inner scope of a class/interface declaration in |
| 136 * which declared members, declared type variables, entities in the enclosing | 110 * which declared members, declared type variables, entities in the enclosing |
| 137 * scope and inherited members are available, in the given order. | 111 * scope and inherited members are available, in the given order. |
| 138 */ | 112 */ |
| 139 class ClassScope extends TypeDeclarationScope { | 113 class ClassScope extends TypeDeclarationScope { |
| 140 ClassElement get element => super.element; | 114 ClassElement get element => super.element; |
| 141 | 115 |
| 142 ClassScope(Scope parentScope, ClassElement element) | 116 ClassScope(Scope parentScope, ClassElement element) |
| 143 : super(parentScope, element) { | 117 : super(parentScope, element) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 172 | 146 |
| 173 Element localLookup(SourceString name) => library.find(name); | 147 Element localLookup(SourceString name) => library.find(name); |
| 174 Element lookup(SourceString name) => localLookup(name); | 148 Element lookup(SourceString name) => localLookup(name); |
| 175 | 149 |
| 176 Element add(Element newElement) { | 150 Element add(Element newElement) { |
| 177 throw "Cannot add an element to a library scope"; | 151 throw "Cannot add an element to a library scope"; |
| 178 } | 152 } |
| 179 | 153 |
| 180 String toString() => 'LibraryScope($library)'; | 154 String toString() => 'LibraryScope($library)'; |
| 181 } | 155 } |
| OLD | NEW |