Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(381)

Side by Side Diff: pkg/compiler/lib/src/serialization/element_serialization.dart

Issue 1873823002: Update elements, nodes and visitors for serialization. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Rebased Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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.elements; 5 library dart2js.serialization.elements;
6 6
7 import '../common.dart'; 7 import '../common.dart';
8 import '../constants/constructors.dart'; 8 import '../constants/constructors.dart';
9 import '../constants/expressions.dart'; 9 import '../constants/expressions.dart';
10 import '../dart_types.dart'; 10 import '../dart_types.dart';
11 import '../elements/elements.dart'; 11 import '../elements/elements.dart';
12 import 'constant_serialization.dart'; 12 import 'constant_serialization.dart';
13 import 'keys.dart'; 13 import 'keys.dart';
14 import 'modelz.dart'; 14 import 'modelz.dart';
15 import 'serialization.dart'; 15 import 'serialization.dart';
16 16
17 /// Enum kinds used for encoding [Element]s. 17 /// Enum kinds used for encoding [Element]s.
18 enum SerializedElementKind { 18 enum SerializedElementKind {
19 LIBRARY, 19 LIBRARY,
20 COMPILATION_UNIT, 20 COMPILATION_UNIT,
21 CLASS, 21 CLASS,
22 ENUM, 22 ENUM,
23 NAMED_MIXIN_APPLICATION, 23 NAMED_MIXIN_APPLICATION,
24 GENERATIVE_CONSTRUCTOR, 24 GENERATIVE_CONSTRUCTOR,
25 FACTORY_CONSTRUCTOR, 25 FACTORY_CONSTRUCTOR,
26 TOPLEVEL_FIELD, 26 TOPLEVEL_FIELD,
27 STATIC_FIELD, 27 STATIC_FIELD,
28 INSTANCE_FIELD, 28 INSTANCE_FIELD,
29 ENUM_CONSTANT,
29 TOPLEVEL_FUNCTION, 30 TOPLEVEL_FUNCTION,
30 TOPLEVEL_GETTER, 31 TOPLEVEL_GETTER,
31 TOPLEVEL_SETTER, 32 TOPLEVEL_SETTER,
32 STATIC_FUNCTION, 33 STATIC_FUNCTION,
33 STATIC_GETTER, 34 STATIC_GETTER,
34 STATIC_SETTER, 35 STATIC_SETTER,
35 INSTANCE_FUNCTION, 36 INSTANCE_FUNCTION,
36 INSTANCE_GETTER, 37 INSTANCE_GETTER,
37 INSTANCE_SETTER, 38 INSTANCE_SETTER,
38 LOCAL_FUNCTION, 39 LOCAL_FUNCTION,
39 TYPEDEF, 40 TYPEDEF,
40 TYPEVARIABLE, 41 TYPEVARIABLE,
41 PARAMETER, 42 PARAMETER,
42 INITIALIZING_FORMAL, 43 INITIALIZING_FORMAL,
43 IMPORT, 44 IMPORT,
44 EXPORT, 45 EXPORT,
45 PREFIX, 46 PREFIX,
47 LOCAL_VARIABLE,
46 EXTERNAL_LIBRARY, 48 EXTERNAL_LIBRARY,
47 EXTERNAL_LIBRARY_MEMBER, 49 EXTERNAL_LIBRARY_MEMBER,
48 EXTERNAL_STATIC_MEMBER, 50 EXTERNAL_STATIC_MEMBER,
49 EXTERNAL_CONSTRUCTOR, 51 EXTERNAL_CONSTRUCTOR,
50 } 52 }
51 53
52 /// Set of serializers used to serialize different kinds of elements by 54 /// Set of serializers used to serialize different kinds of elements by
53 /// encoding into them into [ObjectEncoder]s. 55 /// encoding into them into [ObjectEncoder]s.
54 /// 56 ///
55 /// This class is called from the [Serializer] when an [Element] needs 57 /// This class is called from the [Serializer] when an [Element] needs
56 /// serialization. The [ObjectEncoder] ensures that any [Element], [DartType], 58 /// serialization. The [ObjectEncoder] ensures that any [Element], [DartType],
57 /// and [ConstantExpression] that the serialized [Element] depends upon are also 59 /// and [ConstantExpression] that the serialized [Element] depends upon are also
58 /// serialized. 60 /// serialized.
59 const List<ElementSerializer> ELEMENT_SERIALIZERS = const [ 61 const List<ElementSerializer> ELEMENT_SERIALIZERS = const [
60 const LibrarySerializer(), 62 const LibrarySerializer(),
61 const CompilationUnitSerializer(), 63 const CompilationUnitSerializer(),
62 const ClassSerializer(), 64 const ClassSerializer(),
63 const ConstructorSerializer(), 65 const ConstructorSerializer(),
64 const FieldSerializer(), 66 const FieldSerializer(),
65 const FunctionSerializer(), 67 const FunctionSerializer(),
66 const TypedefSerializer(), 68 const TypedefSerializer(),
67 const TypeVariableSerializer(), 69 const TypeVariableSerializer(),
68 const ParameterSerializer(), 70 const ParameterSerializer(),
69 const ImportSerializer(), 71 const ImportSerializer(),
70 const ExportSerializer(), 72 const ExportSerializer(),
71 const PrefixSerializer(), 73 const PrefixSerializer(),
74 const LocalVariableSerializer(),
72 ]; 75 ];
73 76
74 /// Interface for a function that can serialize a set of element kinds. 77 /// Interface for a function that can serialize a set of element kinds.
75 abstract class ElementSerializer { 78 abstract class ElementSerializer {
76 /// Returns the [SerializedElementKind] for [element] if this serializer 79 /// Returns the [SerializedElementKind] for [element] if this serializer
77 /// supports serialization of [element] or `null` otherwise. 80 /// supports serialization of [element] or `null` otherwise.
78 SerializedElementKind getSerializedKind(Element element); 81 SerializedElementKind getSerializedKind(Element element);
79 82
80 /// Serializes [element] into the [encoder] using the [kind] computed 83 /// Serializes [element] into the [encoder] using the [kind] computed
81 /// by [getSerializedKind]. 84 /// by [getSerializedKind].
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 } 349 }
347 } 350 }
348 } 351 }
349 352
350 class FieldSerializer implements ElementSerializer { 353 class FieldSerializer implements ElementSerializer {
351 const FieldSerializer(); 354 const FieldSerializer();
352 355
353 SerializedElementKind getSerializedKind(Element element) { 356 SerializedElementKind getSerializedKind(Element element) {
354 if (element.isField) { 357 if (element.isField) {
355 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_FIELD; 358 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_FIELD;
356 if (element.isStatic) return SerializedElementKind.STATIC_FIELD; 359 if (element.isStatic) {
360 if (element is EnumConstantElement) {
361 return SerializedElementKind.ENUM_CONSTANT;
362 }
363 return SerializedElementKind.STATIC_FIELD;
364 }
357 if (element.isInstanceMember) return SerializedElementKind.INSTANCE_FIELD; 365 if (element.isInstanceMember) return SerializedElementKind.INSTANCE_FIELD;
358 } 366 }
359 return null; 367 return null;
360 } 368 }
361 369
362 void serialize( 370 void serialize(
363 FieldElement element, ObjectEncoder encoder, SerializedElementKind kind) { 371 FieldElement element, ObjectEncoder encoder, SerializedElementKind kind) {
364 encoder.setString(Key.NAME, element.name); 372 encoder.setString(Key.NAME, element.name);
365 SerializerUtil.serializePosition(element, encoder); 373 SerializerUtil.serializePosition(element, encoder);
366 encoder.setType(Key.TYPE, element.type); 374 encoder.setType(Key.TYPE, element.type);
367 encoder.setBool(Key.IS_FINAL, element.isFinal); 375 encoder.setBool(Key.IS_FINAL, element.isFinal);
368 encoder.setBool(Key.IS_CONST, element.isConst); 376 encoder.setBool(Key.IS_CONST, element.isConst);
369 if (element.isConst) { 377 if (element.isConst) {
370 ConstantExpression constant = element.constant; 378 ConstantExpression constant = element.constant;
371 encoder.setConstant(Key.CONSTANT, constant); 379 encoder.setConstant(Key.CONSTANT, constant);
372 } 380 }
373 if (kind != SerializedElementKind.TOPLEVEL_FIELD) { 381 if (kind != SerializedElementKind.TOPLEVEL_FIELD) {
374 encoder.setElement(Key.CLASS, element.enclosingClass); 382 encoder.setElement(Key.CLASS, element.enclosingClass);
375 } else { 383 } else {
376 encoder.setElement(Key.LIBRARY, element.library); 384 encoder.setElement(Key.LIBRARY, element.library);
377 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit); 385 encoder.setElement(Key.COMPILATION_UNIT, element.compilationUnit);
378 } 386 }
387 if (element is EnumConstantElement) {
388 EnumConstantElement enumConstant = element;
389 encoder.setInt(Key.INDEX, enumConstant.index);
390 }
379 } 391 }
380 } 392 }
381 393
382 class FunctionSerializer implements ElementSerializer { 394 class FunctionSerializer implements ElementSerializer {
383 const FunctionSerializer(); 395 const FunctionSerializer();
384 396
385 SerializedElementKind getSerializedKind(Element element) { 397 SerializedElementKind getSerializedKind(Element element) {
386 if (element.isFunction) { 398 if (element.isFunction) {
387 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_FUNCTION; 399 if (element.isTopLevel) return SerializedElementKind.TOPLEVEL_FUNCTION;
388 if (element.isStatic) return SerializedElementKind.STATIC_FUNCTION; 400 if (element.isStatic) return SerializedElementKind.STATIC_FUNCTION;
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 if (element.isOptional) { 511 if (element.isOptional) {
500 encoder.setConstant(Key.CONSTANT, element.constant); 512 encoder.setConstant(Key.CONSTANT, element.constant);
501 } 513 }
502 if (element.isInitializingFormal) { 514 if (element.isInitializingFormal) {
503 InitializingFormalElement initializingFormal = element; 515 InitializingFormalElement initializingFormal = element;
504 encoder.setElement(Key.FIELD, initializingFormal.fieldElement); 516 encoder.setElement(Key.FIELD, initializingFormal.fieldElement);
505 } 517 }
506 } 518 }
507 } 519 }
508 520
521 class LocalVariableSerializer implements ElementSerializer {
522 const LocalVariableSerializer();
523
524 SerializedElementKind getSerializedKind(Element element) {
525 if (element.isVariable) {
526 return SerializedElementKind.LOCAL_VARIABLE;
527 }
528 return null;
529 }
530
531 void serialize(LocalVariableElement element, ObjectEncoder encoder,
532 SerializedElementKind kind) {
533 encoder.setString(Key.NAME, element.name);
534 SerializerUtil.serializePosition(element, encoder);
535 encoder.setType(Key.TYPE, element.type);
536 encoder.setBool(Key.IS_FINAL, element.isFinal);
537 encoder.setBool(Key.IS_CONST, element.isConst);
538 if (element.isConst) {
539 ConstantExpression constant = element.constant;
540 encoder.setConstant(Key.CONSTANT, constant);
541 }
542 encoder.setElement(Key.EXECUTABLE_CONTEXT, element.executableContext);
543 }
544 }
545
509 class ImportSerializer implements ElementSerializer { 546 class ImportSerializer implements ElementSerializer {
510 const ImportSerializer(); 547 const ImportSerializer();
511 548
512 SerializedElementKind getSerializedKind(Element element) { 549 SerializedElementKind getSerializedKind(Element element) {
513 if (element.isImport) { 550 if (element.isImport) {
514 return SerializedElementKind.IMPORT; 551 return SerializedElementKind.IMPORT;
515 } 552 }
516 return null; 553 return null;
517 } 554 }
518 555
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 case SerializedElementKind.CLASS: 629 case SerializedElementKind.CLASS:
593 return new ClassElementZ(decoder); 630 return new ClassElementZ(decoder);
594 case SerializedElementKind.ENUM: 631 case SerializedElementKind.ENUM:
595 return new EnumClassElementZ(decoder); 632 return new EnumClassElementZ(decoder);
596 case SerializedElementKind.NAMED_MIXIN_APPLICATION: 633 case SerializedElementKind.NAMED_MIXIN_APPLICATION:
597 return new NamedMixinApplicationElementZ(decoder); 634 return new NamedMixinApplicationElementZ(decoder);
598 case SerializedElementKind.TOPLEVEL_FIELD: 635 case SerializedElementKind.TOPLEVEL_FIELD:
599 return new TopLevelFieldElementZ(decoder); 636 return new TopLevelFieldElementZ(decoder);
600 case SerializedElementKind.STATIC_FIELD: 637 case SerializedElementKind.STATIC_FIELD:
601 return new StaticFieldElementZ(decoder); 638 return new StaticFieldElementZ(decoder);
639 case SerializedElementKind.ENUM_CONSTANT:
640 return new EnumConstantElementZ(decoder);
602 case SerializedElementKind.INSTANCE_FIELD: 641 case SerializedElementKind.INSTANCE_FIELD:
603 return new InstanceFieldElementZ(decoder); 642 return new InstanceFieldElementZ(decoder);
604 case SerializedElementKind.GENERATIVE_CONSTRUCTOR: 643 case SerializedElementKind.GENERATIVE_CONSTRUCTOR:
605 return new GenerativeConstructorElementZ(decoder); 644 return new GenerativeConstructorElementZ(decoder);
606 case SerializedElementKind.FACTORY_CONSTRUCTOR: 645 case SerializedElementKind.FACTORY_CONSTRUCTOR:
607 return new FactoryConstructorElementZ(decoder); 646 return new FactoryConstructorElementZ(decoder);
608 case SerializedElementKind.TOPLEVEL_FUNCTION: 647 case SerializedElementKind.TOPLEVEL_FUNCTION:
609 return new TopLevelFunctionElementZ(decoder); 648 return new TopLevelFunctionElementZ(decoder);
610 case SerializedElementKind.STATIC_FUNCTION: 649 case SerializedElementKind.STATIC_FUNCTION:
611 return new StaticFunctionElementZ(decoder); 650 return new StaticFunctionElementZ(decoder);
(...skipping 20 matching lines...) Expand all
632 case SerializedElementKind.PARAMETER: 671 case SerializedElementKind.PARAMETER:
633 return new ParameterElementZ(decoder); 672 return new ParameterElementZ(decoder);
634 case SerializedElementKind.INITIALIZING_FORMAL: 673 case SerializedElementKind.INITIALIZING_FORMAL:
635 return new InitializingFormalElementZ(decoder); 674 return new InitializingFormalElementZ(decoder);
636 case SerializedElementKind.IMPORT: 675 case SerializedElementKind.IMPORT:
637 return new ImportElementZ(decoder); 676 return new ImportElementZ(decoder);
638 case SerializedElementKind.EXPORT: 677 case SerializedElementKind.EXPORT:
639 return new ExportElementZ(decoder); 678 return new ExportElementZ(decoder);
640 case SerializedElementKind.PREFIX: 679 case SerializedElementKind.PREFIX:
641 return new PrefixElementZ(decoder); 680 return new PrefixElementZ(decoder);
681 case SerializedElementKind.LOCAL_VARIABLE:
682 return new LocalVariableElementZ(decoder);
642 case SerializedElementKind.EXTERNAL_LIBRARY: 683 case SerializedElementKind.EXTERNAL_LIBRARY:
643 case SerializedElementKind.EXTERNAL_LIBRARY_MEMBER: 684 case SerializedElementKind.EXTERNAL_LIBRARY_MEMBER:
644 case SerializedElementKind.EXTERNAL_STATIC_MEMBER: 685 case SerializedElementKind.EXTERNAL_STATIC_MEMBER:
645 case SerializedElementKind.EXTERNAL_CONSTRUCTOR: 686 case SerializedElementKind.EXTERNAL_CONSTRUCTOR:
646 break; 687 break;
647 } 688 }
648 throw new UnsupportedError("Unexpected element kind '${elementKind}."); 689 throw new UnsupportedError("Unexpected element kind '${elementKind}.");
649 } 690 }
650 } 691 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/resolution/enum_creator.dart ('k') | pkg/compiler/lib/src/serialization/equivalence.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698