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 dart2js.serialization.resolved_ast; | 5 library dart2js.serialization.resolved_ast; |
6 | 6 |
7 import '../common.dart'; | 7 import '../common.dart'; |
8 import '../common/resolution.dart'; | 8 import '../common/resolution.dart'; |
9 import '../constants/expressions.dart'; | 9 import '../constants/expressions.dart'; |
10 import '../dart_types.dart'; | 10 import '../dart_types.dart'; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 | 66 |
67 TreeElements get elements => resolvedAst.elements; | 67 TreeElements get elements => resolvedAst.elements; |
68 | 68 |
69 Node get root => resolvedAst.node; | 69 Node get root => resolvedAst.node; |
70 | 70 |
71 Map<Node, int> get nodeIndices => indexComputer.nodeIndices; | 71 Map<Node, int> get nodeIndices => indexComputer.nodeIndices; |
72 List<Node> get nodeList => indexComputer.nodeList; | 72 List<Node> get nodeList => indexComputer.nodeList; |
73 | 73 |
74 /// Serializes [resolvedAst] into [objectEncoder]. | 74 /// Serializes [resolvedAst] into [objectEncoder]. |
75 void serialize() { | 75 void serialize() { |
| 76 objectEncoder.setEnum(Key.KIND, resolvedAst.kind); |
| 77 switch (resolvedAst.kind) { |
| 78 case ResolvedAstKind.PARSED: |
| 79 serializeParsed(); |
| 80 break; |
| 81 case ResolvedAstKind.DEFAULT_CONSTRUCTOR: |
| 82 case ResolvedAstKind.FORWARDING_CONSTRUCTOR: |
| 83 // No additional properties. |
| 84 break; |
| 85 } |
| 86 } |
| 87 |
| 88 /// Serialize [ResolvedAst] that is defined in terms of an AST together with |
| 89 /// [TreeElements]. |
| 90 void serializeParsed() { |
76 objectEncoder.setUri( | 91 objectEncoder.setUri( |
77 Key.URI, | 92 Key.URI, |
78 elements.analyzedElement.compilationUnit.script.resourceUri, | 93 elements.analyzedElement.compilationUnit.script.resourceUri, |
79 elements.analyzedElement.compilationUnit.script.resourceUri); | 94 elements.analyzedElement.compilationUnit.script.resourceUri); |
80 AstKind kind; | 95 AstKind kind; |
81 if (element.enclosingClass is EnumClassElement) { | 96 if (element.enclosingClass is EnumClassElement) { |
82 if (element.name == 'index') { | 97 if (element.name == 'index') { |
83 kind = AstKind.ENUM_INDEX_FIELD; | 98 kind = AstKind.ENUM_INDEX_FIELD; |
84 } else if (element.name == 'values') { | 99 } else if (element.name == 'values') { |
85 kind = AstKind.ENUM_VALUES_FIELD; | 100 kind = AstKind.ENUM_VALUES_FIELD; |
(...skipping 17 matching lines...) Expand all Loading... |
103 } else { | 118 } else { |
104 kind = AstKind.FUNCTION; | 119 kind = AstKind.FUNCTION; |
105 FunctionExpression functionExpression = root.asFunctionExpression(); | 120 FunctionExpression functionExpression = root.asFunctionExpression(); |
106 if (functionExpression.getOrSet != null) { | 121 if (functionExpression.getOrSet != null) { |
107 // Getters/setters need the get/set token to be parsed. | 122 // Getters/setters need the get/set token to be parsed. |
108 objectEncoder.setInt( | 123 objectEncoder.setInt( |
109 Key.GET_OR_SET, functionExpression.getOrSet.charOffset); | 124 Key.GET_OR_SET, functionExpression.getOrSet.charOffset); |
110 } | 125 } |
111 } | 126 } |
112 } | 127 } |
113 objectEncoder.setEnum(Key.KIND, kind); | 128 objectEncoder.setEnum(Key.SUB_KIND, kind); |
114 root.accept(indexComputer); | 129 root.accept(indexComputer); |
115 root.accept(this); | 130 root.accept(this); |
116 } | 131 } |
117 | 132 |
118 /// Computes the [ListEncoder] for serializing data for nodes. | 133 /// Computes the [ListEncoder] for serializing data for nodes. |
119 ListEncoder get nodeDataEncoder { | 134 ListEncoder get nodeDataEncoder { |
120 if (_nodeDataEncoder == null) { | 135 if (_nodeDataEncoder == null) { |
121 _nodeDataEncoder = objectEncoder.createList(Key.DATA); | 136 _nodeDataEncoder = objectEncoder.createList(Key.DATA); |
122 } | 137 } |
123 return _nodeDataEncoder; | 138 return _nodeDataEncoder; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 token = token.next; | 224 token = token.next; |
210 } | 225 } |
211 return null; | 226 return null; |
212 } | 227 } |
213 | 228 |
214 /// Deserializes the [ResolvedAst] for [element] from [objectDecoder]. | 229 /// Deserializes the [ResolvedAst] for [element] from [objectDecoder]. |
215 /// [parsing] and [getBeginToken] are used for parsing the [Node] for | 230 /// [parsing] and [getBeginToken] are used for parsing the [Node] for |
216 /// [element] from its source code. | 231 /// [element] from its source code. |
217 static ResolvedAst deserialize(Element element, ObjectDecoder objectDecoder, | 232 static ResolvedAst deserialize(Element element, ObjectDecoder objectDecoder, |
218 Parsing parsing, Token getBeginToken(Uri uri, int charOffset)) { | 233 Parsing parsing, Token getBeginToken(Uri uri, int charOffset)) { |
| 234 ResolvedAstKind kind = |
| 235 objectDecoder.getEnum(Key.KIND, ResolvedAstKind.values); |
| 236 switch (kind) { |
| 237 case ResolvedAstKind.PARSED: |
| 238 return deserializeParsed( |
| 239 element, objectDecoder, parsing, getBeginToken); |
| 240 case ResolvedAstKind.DEFAULT_CONSTRUCTOR: |
| 241 case ResolvedAstKind.FORWARDING_CONSTRUCTOR: |
| 242 return new SynthesizedResolvedAst(element, kind); |
| 243 } |
| 244 } |
| 245 |
| 246 /// Deserialize a [ResolvedAst] that is defined in terms of an AST together |
| 247 /// with [TreeElements]. |
| 248 static ResolvedAst deserializeParsed( |
| 249 Element element, |
| 250 ObjectDecoder objectDecoder, |
| 251 Parsing parsing, |
| 252 Token getBeginToken(Uri uri, int charOffset)) { |
219 CompilationUnitElement compilationUnit = element.compilationUnit; | 253 CompilationUnitElement compilationUnit = element.compilationUnit; |
220 DiagnosticReporter reporter = parsing.reporter; | 254 DiagnosticReporter reporter = parsing.reporter; |
221 | 255 |
222 /// Returns the first [Token] for parsing the [Node] for [element]. | 256 /// Returns the first [Token] for parsing the [Node] for [element]. |
223 Token readBeginToken() { | 257 Token readBeginToken() { |
224 Uri uri = objectDecoder.getUri(Key.URI); | 258 Uri uri = objectDecoder.getUri(Key.URI); |
225 int charOffset = objectDecoder.getInt(Key.OFFSET); | 259 int charOffset = objectDecoder.getInt(Key.OFFSET); |
226 Token beginToken = getBeginToken(uri, charOffset); | 260 Token beginToken = getBeginToken(uri, charOffset); |
227 if (beginToken == null) { | 261 if (beginToken == null) { |
228 reporter.internalError( | 262 reporter.internalError( |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 "No token found for $element in " | 393 "No token found for $element in " |
360 "${objectDecoder.getUri(Key.URI)} @ $getOrSetOffset"); | 394 "${objectDecoder.getUri(Key.URI)} @ $getOrSetOffset"); |
361 } | 395 } |
362 } | 396 } |
363 return doParse((parser) { | 397 return doParse((parser) { |
364 parser.parseFunction(beginToken, getOrSet); | 398 parser.parseFunction(beginToken, getOrSet); |
365 }); | 399 }); |
366 } | 400 } |
367 } | 401 } |
368 | 402 |
369 AstKind kind = objectDecoder.getEnum(Key.KIND, AstKind.values); | 403 AstKind kind = objectDecoder.getEnum(Key.SUB_KIND, AstKind.values); |
370 Node root = computeNode(kind); | 404 Node root = computeNode(kind); |
371 TreeElementMapping elements = new TreeElementMapping(element); | 405 TreeElementMapping elements = new TreeElementMapping(element); |
372 AstIndexComputer indexComputer = new AstIndexComputer(); | 406 AstIndexComputer indexComputer = new AstIndexComputer(); |
373 Map<Node, int> nodeIndices = indexComputer.nodeIndices; | 407 Map<Node, int> nodeIndices = indexComputer.nodeIndices; |
374 List<Node> nodeList = indexComputer.nodeList; | 408 List<Node> nodeList = indexComputer.nodeList; |
375 root.accept(indexComputer); | 409 root.accept(indexComputer); |
376 ListDecoder dataDecoder = objectDecoder.getList(Key.DATA); | 410 ListDecoder dataDecoder = objectDecoder.getList(Key.DATA); |
377 if (dataDecoder != null) { | 411 if (dataDecoder != null) { |
378 for (int i = 0; i < dataDecoder.length; i++) { | 412 for (int i = 0; i < dataDecoder.length; i++) { |
379 ObjectDecoder objectDecoder = dataDecoder.getObject(i); | 413 ObjectDecoder objectDecoder = dataDecoder.getObject(i); |
(...skipping 30 matching lines...) Expand all Loading... |
410 node, deserializeSendStructure(sendStructureDecoder)); | 444 node, deserializeSendStructure(sendStructureDecoder)); |
411 } | 445 } |
412 ObjectDecoder newStructureDecoder = | 446 ObjectDecoder newStructureDecoder = |
413 objectDecoder.getObject(Key.NEW_STRUCTURE, isOptional: true); | 447 objectDecoder.getObject(Key.NEW_STRUCTURE, isOptional: true); |
414 if (newStructureDecoder != null) { | 448 if (newStructureDecoder != null) { |
415 elements.setNewStructure( | 449 elements.setNewStructure( |
416 node, deserializeNewStructure(newStructureDecoder)); | 450 node, deserializeNewStructure(newStructureDecoder)); |
417 } | 451 } |
418 } | 452 } |
419 } | 453 } |
420 return new ResolvedAst(element, root, elements); | 454 return new ParsedResolvedAst(element, root, elements); |
421 } | 455 } |
422 } | 456 } |
OLD | NEW |