OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 fasta.analyzer.element_store; | 5 library fasta.analyzer.element_store; |
6 | 6 |
7 import 'package:kernel/analyzer/loader.dart' show | 7 import 'package:kernel/analyzer/loader.dart' show ReferenceLevelLoader; |
8 ReferenceLevelLoader; | |
9 | 8 |
10 import 'package:kernel/ast.dart'; | 9 import 'package:kernel/ast.dart'; |
11 | 10 |
12 import 'package:analyzer/analyzer.dart' show | 11 import 'package:analyzer/analyzer.dart' show ParameterKind; |
13 ParameterKind; | |
14 | 12 |
15 import 'package:analyzer/analyzer.dart' as analyzer; | 13 import 'package:analyzer/analyzer.dart' as analyzer; |
16 | 14 |
17 import 'package:analyzer/dart/element/element.dart'; | 15 import 'package:analyzer/dart/element/element.dart'; |
18 | 16 |
19 import 'package:analyzer/dart/element/type.dart' as analyzer; | 17 import 'package:analyzer/dart/element/type.dart' as analyzer; |
20 | 18 |
21 import '../errors.dart' show | 19 import '../errors.dart' show internalError; |
22 internalError; | |
23 | 20 |
24 import '../kernel/kernel_builder.dart'; | 21 import '../kernel/kernel_builder.dart'; |
25 | 22 |
26 import '../dill/dill_member_builder.dart'; | 23 import '../dill/dill_member_builder.dart'; |
27 | 24 |
28 import 'mock_element.dart'; | 25 import 'mock_element.dart'; |
29 | 26 |
30 import 'mock_type.dart'; | 27 import 'mock_type.dart'; |
31 | 28 |
32 abstract class ElementStore implements ReferenceLevelLoader { | 29 abstract class ElementStore implements ReferenceLevelLoader { |
33 Element operator[] (Builder builder); | 30 Element operator [](Builder builder); |
34 | 31 |
35 factory ElementStore( | 32 factory ElementStore( |
36 LibraryBuilder coreLibrary, Map<Uri, LibraryBuilder> builders) = | 33 LibraryBuilder coreLibrary, Map<Uri, LibraryBuilder> builders) = |
37 ElementStoreImplementation; | 34 ElementStoreImplementation; |
38 } | 35 } |
39 | 36 |
40 // ClassElement, | 37 // ClassElement, |
41 // ClassMemberElement, | 38 // ClassMemberElement, |
42 // CompilationUnitElement, | 39 // CompilationUnitElement, |
43 // ConstructorElement, | 40 // ConstructorElement, |
44 // Element, | 41 // Element, |
45 // FieldElement, | 42 // FieldElement, |
46 // FieldFormalParameterElement, | 43 // FieldFormalParameterElement, |
47 // FunctionElement, | 44 // FunctionElement, |
48 // FunctionTypedElement, | 45 // FunctionTypedElement, |
49 // LibraryElement, | 46 // LibraryElement, |
50 // LocalElement, | 47 // LocalElement, |
51 // LocalVariableElement, | 48 // LocalVariableElement, |
52 // MethodElement, | 49 // MethodElement, |
53 // ParameterElement, | 50 // ParameterElement, |
54 // PrefixElement, | 51 // PrefixElement, |
55 // PropertyAccessorElement, | 52 // PropertyAccessorElement, |
56 // TopLevelVariableElement, | 53 // TopLevelVariableElement, |
57 // TypeParameterElement, | 54 // TypeParameterElement, |
58 // VariableElement; | 55 // VariableElement; |
59 class ElementStoreImplementation implements ElementStore { | 56 class ElementStoreImplementation implements ElementStore { |
60 final LibraryBuilder coreLibrary; | 57 final LibraryBuilder coreLibrary; |
61 | 58 |
62 final Map<Builder, Element> elements; | 59 final Map<Builder, Element> elements; |
63 | 60 |
64 ElementStoreImplementation.internal(this.coreLibrary, this.elements); | 61 ElementStoreImplementation.internal(this.coreLibrary, this.elements); |
65 | 62 |
66 Element operator[] (Builder builder) { | 63 Element operator [](Builder builder) { |
67 // Avoid storing local elements in the element store to reduce memory | 64 // Avoid storing local elements in the element store to reduce memory |
68 // usage. So they both implement [Element] and [Builder]. | 65 // usage. So they both implement [Element] and [Builder]. |
69 return builder is Element ? builder : elements[builder]; | 66 return builder is Element ? builder : elements[builder]; |
70 } | 67 } |
71 | 68 |
72 factory ElementStoreImplementation( | 69 factory ElementStoreImplementation( |
73 LibraryBuilder coreLibrary, Map<Uri, LibraryBuilder> libraries) { | 70 LibraryBuilder coreLibrary, Map<Uri, LibraryBuilder> libraries) { |
74 Map<Builder, Element> elements = <Builder, Element>{}; | 71 Map<Builder, Element> elements = <Builder, Element>{}; |
75 libraries.forEach((Uri uri, LibraryBuilder library) { | 72 libraries.forEach((Uri uri, LibraryBuilder library) { |
76 KernelCompilationUnitElement unit = | 73 KernelCompilationUnitElement unit = |
77 new KernelCompilationUnitElement(library); | 74 new KernelCompilationUnitElement(library); |
78 KernelLibraryElement element = new KernelLibraryElement(unit); | 75 KernelLibraryElement element = new KernelLibraryElement(unit); |
79 elements[library] = element; | 76 elements[library] = element; |
80 unit.library = element; | 77 unit.library = element; |
81 library.members.forEach((String name, Builder builder) { | 78 library.members.forEach((String name, Builder builder) { |
82 do { | 79 do { |
83 if (builder is ClassBuilder) { | 80 if (builder is ClassBuilder) { |
84 elements[builder] = new KernelClassElement(builder); | 81 elements[builder] = new KernelClassElement(builder); |
85 } else if (builder is DillMemberBuilder) { | 82 } else if (builder is DillMemberBuilder) { |
86 Member member = builder.member; | 83 Member member = builder.member; |
87 if (member is Field) { | 84 if (member is Field) {} else if (member is Procedure) { |
88 } else if (member is Procedure) { | |
89 buildDillFunctionElement(builder, unit, elements); | 85 buildDillFunctionElement(builder, unit, elements); |
90 } else { | 86 } else { |
91 internalError("Unhandled $name ${member.runtimeType} in $uri"); | 87 internalError("Unhandled $name ${member.runtimeType} in $uri"); |
92 } | 88 } |
93 } else if (builder is KernelProcedureBuilder) { | 89 } else if (builder is KernelProcedureBuilder) { |
94 buildKernelFunctionElement(builder, unit, elements); | 90 buildKernelFunctionElement(builder, unit, elements); |
95 } else { | 91 } else { |
96 internalError("Unhandled $name ${builder.runtimeType} in $uri"); | 92 internalError("Unhandled $name ${builder.runtimeType} in $uri"); |
97 } | 93 } |
98 builder = builder.next; | 94 builder = builder.next; |
99 } while (builder != null); | 95 } while (builder != null); |
100 }); | 96 }); |
101 }); | 97 }); |
102 return new ElementStoreImplementation.internal(coreLibrary, elements); | 98 return new ElementStoreImplementation.internal(coreLibrary, elements); |
103 } | 99 } |
104 | 100 |
105 bool get ignoreRedirectingFactories => false; | 101 bool get ignoreRedirectingFactories => false; |
106 | 102 |
107 Constructor getCoreClassConstructorReference(String className, | 103 Constructor getCoreClassConstructorReference(String className, |
108 {String constructorName, String library}) { | 104 {String constructorName, String library}) { |
109 assert(library == null); | 105 assert(library == null); |
110 KernelClassBuilder cls = coreLibrary.members[className]; | 106 KernelClassBuilder cls = coreLibrary.members[className]; |
111 Constructor constructor = constructorName == null | 107 Constructor constructor = constructorName == null |
112 ? cls.cls.constructors.first | 108 ? cls.cls.constructors.first |
113 : cls.cls.constructors.firstWhere( | 109 : cls.cls.constructors |
114 (Constructor c) => c.name.name == constructorName); | 110 .firstWhere((Constructor c) => c.name.name == constructorName); |
115 return constructor; | 111 return constructor; |
116 } | 112 } |
117 | 113 |
118 Library getLibraryReference(LibraryElement element) { | 114 Library getLibraryReference(LibraryElement element) { |
119 return internalError("not supported."); | 115 return internalError("not supported."); |
120 } | 116 } |
121 | 117 |
122 Class getClassReference(covariant KernelClassElement cls) => cls.builder.cls; | 118 Class getClassReference(covariant KernelClassElement cls) => cls.builder.cls; |
123 | 119 |
124 Member getMemberReference(Element element) { | 120 Member getMemberReference(Element element) { |
(...skipping 30 matching lines...) Expand all Loading... |
155 Procedure procedure = builder.member; | 151 Procedure procedure = builder.member; |
156 List<VariableDeclaration> positionalParameters = | 152 List<VariableDeclaration> positionalParameters = |
157 procedure.function.positionalParameters; | 153 procedure.function.positionalParameters; |
158 List<VariableDeclaration> namedParameters = | 154 List<VariableDeclaration> namedParameters = |
159 procedure.function.namedParameters; | 155 procedure.function.namedParameters; |
160 int requiredParameterCount = procedure.function.requiredParameterCount; | 156 int requiredParameterCount = procedure.function.requiredParameterCount; |
161 List<KernelParameterElement> parameters = new List<KernelParameterElement>( | 157 List<KernelParameterElement> parameters = new List<KernelParameterElement>( |
162 positionalParameters.length + namedParameters.length); | 158 positionalParameters.length + namedParameters.length); |
163 int i = 0; | 159 int i = 0; |
164 for (VariableDeclaration parameter in positionalParameters) { | 160 for (VariableDeclaration parameter in positionalParameters) { |
165 parameters[i] = buildFormalParameter( | 161 parameters[i] = buildFormalParameter(parameter, |
166 parameter, isOptional: i >= requiredParameterCount); | 162 isOptional: i >= requiredParameterCount); |
167 i++; | 163 i++; |
168 } | 164 } |
169 for (VariableDeclaration parameter in namedParameters) { | 165 for (VariableDeclaration parameter in namedParameters) { |
170 parameters[i++] = buildFormalParameter(parameter, isNamed: true); | 166 parameters[i++] = buildFormalParameter(parameter, isNamed: true); |
171 } | 167 } |
172 elements[builder] = new KernelFunctionElement(procedure, unit, parameters); | 168 elements[builder] = new KernelFunctionElement(procedure, unit, parameters); |
173 } | 169 } |
174 | 170 |
175 static void buildKernelFunctionElement(KernelProcedureBuilder builder, | 171 static void buildKernelFunctionElement(KernelProcedureBuilder builder, |
176 KernelCompilationUnitElement unit, Map<Builder, Element> elements) { | 172 KernelCompilationUnitElement unit, Map<Builder, Element> elements) { |
177 assert(builder.procedure != null); | 173 assert(builder.procedure != null); |
178 List<KernelParameterElement> parameters; | 174 List<KernelParameterElement> parameters; |
179 int i = 0; | 175 int i = 0; |
180 if (builder.formals != null) { | 176 if (builder.formals != null) { |
181 parameters = new List<KernelParameterElement>(builder.formals.length); | 177 parameters = new List<KernelParameterElement>(builder.formals.length); |
182 for (KernelFormalParameterBuilder parameter in builder.formals) { | 178 for (KernelFormalParameterBuilder parameter in builder.formals) { |
183 assert(parameter.declaration != null); | 179 assert(parameter.declaration != null); |
184 elements[parameter] = parameters[i++] = buildFormalParameter( | 180 elements[parameter] = parameters[i++] = buildFormalParameter( |
185 parameter.declaration, isOptional: parameter.isOptional, | 181 parameter.declaration, |
| 182 isOptional: parameter.isOptional, |
186 isNamed: parameter.isNamed); | 183 isNamed: parameter.isNamed); |
187 } | 184 } |
188 } else { | 185 } else { |
189 parameters = new List<KernelParameterElement>(0); | 186 parameters = new List<KernelParameterElement>(0); |
190 } | 187 } |
191 elements[builder] = new KernelFunctionElement( | 188 elements[builder] = |
192 builder.procedure, unit, parameters); | 189 new KernelFunctionElement(builder.procedure, unit, parameters); |
193 } | 190 } |
194 | 191 |
195 static KernelParameterElement buildFormalParameter( | 192 static KernelParameterElement buildFormalParameter( |
196 VariableDeclaration parameter, | 193 VariableDeclaration parameter, |
197 {bool isOptional: true, bool isNamed: false}) { | 194 {bool isOptional: true, |
| 195 bool isNamed: false}) { |
198 ParameterKind kind = isOptional | 196 ParameterKind kind = isOptional |
199 ? (isNamed ? ParameterKind.NAMED : ParameterKind.POSITIONAL) | 197 ? (isNamed ? ParameterKind.NAMED : ParameterKind.POSITIONAL) |
200 : ParameterKind.REQUIRED; | 198 : ParameterKind.REQUIRED; |
201 return new KernelParameterElement(parameter, kind); | 199 return new KernelParameterElement(parameter, kind); |
202 } | 200 } |
203 } | 201 } |
204 | 202 |
205 class KernelLibraryElement extends MockLibraryElement { | 203 class KernelLibraryElement extends MockLibraryElement { |
206 final KernelCompilationUnitElement definingCompilationUnit; | 204 final KernelCompilationUnitElement definingCompilationUnit; |
207 | 205 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 } | 290 } |
293 } | 291 } |
294 | 292 |
295 class KernelInterfaceType extends MockInterfaceType { | 293 class KernelInterfaceType extends MockInterfaceType { |
296 final KernelClassElement element; | 294 final KernelClassElement element; |
297 | 295 |
298 KernelInterfaceType(this.element); | 296 KernelInterfaceType(this.element); |
299 | 297 |
300 List<analyzer.DartType> get typeArguments => const <analyzer.DartType>[]; | 298 List<analyzer.DartType> get typeArguments => const <analyzer.DartType>[]; |
301 } | 299 } |
OLD | NEW |