OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 dart2js.js_emitter; | 5 part of dart2js.js_emitter; |
6 | 6 |
7 /** | 7 /** |
8 * A data structure for collecting fragments of a class definition. | 8 * A data structure for collecting fragments of a class definition. |
9 */ | 9 */ |
10 class ClassBuilder { | 10 class ClassBuilder { |
11 final List<jsAst.Property> properties = <jsAst.Property>[]; | 11 final List<jsAst.Property> properties = <jsAst.Property>[]; |
12 final List<jsAst.Literal> fields = <jsAst.Literal>[]; | 12 final List<jsAst.Literal> fields = <jsAst.Literal>[]; |
13 | 13 |
14 String superName; | 14 jsAst.Name superName; |
15 jsAst.Node functionType; | 15 jsAst.Node functionType; |
16 List<jsAst.Node> fieldMetadata; | 16 List<jsAst.Node> fieldMetadata; |
17 | 17 |
18 final Element element; | 18 final Element element; |
19 final Namer namer; | 19 final Namer namer; |
| 20 final bool isForActualClass; |
20 | 21 |
21 ClassBuilder(this.element, this.namer); | 22 ClassBuilder(this.element, this.namer, this.isForActualClass); |
22 | 23 |
23 jsAst.Property addProperty(String name, jsAst.Expression value) { | 24 ClassBuilder.forClass(ClassElement cls, this.namer) |
| 25 : isForActualClass = true, |
| 26 element = cls; |
| 27 |
| 28 ClassBuilder.forStatics(this.element, this.namer) : isForActualClass = false; |
| 29 |
| 30 jsAst.Property addProperty(jsAst.Literal name, jsAst.Expression value) { |
| 31 jsAst.Property property = new jsAst.Property(js.quoteName(name), value); |
| 32 properties.add(property); |
| 33 return property; |
| 34 } |
| 35 |
| 36 jsAst.Property addPropertyByName(String name, jsAst.Expression value) { |
24 jsAst.Property property = new jsAst.Property(js.string(name), value); | 37 jsAst.Property property = new jsAst.Property(js.string(name), value); |
25 properties.add(property); | 38 properties.add(property); |
26 return property; | 39 return property; |
27 } | 40 } |
28 | 41 |
29 void addField(jsAst.Literal field) { | 42 void addField(jsAst.Literal field) { |
30 fields.add(field); | 43 fields.add(field); |
31 } | 44 } |
32 | 45 |
33 static String functionTypeEncodingDescription = | 46 static String functionTypeEncodingDescription = |
34 'For simple function types the function type is stored in the metadata ' | 47 'For simple function types the function type is stored in the metadata ' |
35 'and the index is encoded into the superclass field.'; | 48 'and the index is encoded into the superclass field.'; |
36 | 49 |
37 static String fieldEncodingDescription = | 50 static String fieldEncodingDescription = |
38 'Fields are encoded as a comma separated list. If there is a superclass ' | 51 'Fields are encoded as a comma separated list. If there is a superclass ' |
39 '(and possibly a function type encoding) the fields are separated from ' | 52 '(and possibly a function type encoding) the fields are separated from ' |
40 'the superclass by a semicolon.'; | 53 'the superclass by a semicolon.'; |
41 | 54 |
42 jsAst.ObjectInitializer toObjectInitializer( | 55 jsAst.ObjectInitializer toObjectInitializer( |
43 {bool emitClassDescriptor: true}) { | 56 {bool emitClassDescriptor: true}) { |
44 List<jsAst.Literal> parts = <jsAst.Literal>[]; | 57 List<jsAst.Literal> parts = <jsAst.Literal>[]; |
45 if (superName != null) { | 58 if (isForActualClass) { |
46 parts.add(js.stringPart(superName)); | 59 if (superName != null) { |
47 if (functionType != null) { | 60 parts.add(superName); |
48 // See [functionTypeEncodingDescription] above. | 61 if (functionType != null) { |
49 parts.add(js.stringPart(':')); | 62 // See [functionTypeEncodingDescription] above. |
50 parts.add(functionType); | 63 parts.add(js.stringPart(':')); |
| 64 parts.add(functionType); |
| 65 } |
51 } | 66 } |
52 parts.add(js.stringPart(';')); | 67 parts.add(js.stringPart(';')); |
53 } | 68 } |
54 // See [fieldEncodingDescription] above. | 69 // See [fieldEncodingDescription] above. |
55 parts.addAll(js.joinLiterals(fields, js.stringPart(','))); | 70 parts.addAll(js.joinLiterals(fields, js.stringPart(','))); |
56 var classData = js.concatenateStrings(parts, addQuotes: true); | 71 var classData = js.concatenateStrings(parts, addQuotes: true); |
57 if (fieldMetadata != null) { | 72 if (fieldMetadata != null) { |
58 // If we need to store fieldMetadata, classData is turned into an array, | 73 // If we need to store fieldMetadata, classData is turned into an array, |
59 // and the field metadata is appended. So if classData is just a string, | 74 // and the field metadata is appended. So if classData is just a string, |
60 // there is no field metadata. | 75 // there is no field metadata. |
61 classData = | 76 classData = |
62 new jsAst.ArrayInitializer([classData]..addAll(fieldMetadata)); | 77 new jsAst.ArrayInitializer([classData]..addAll(fieldMetadata)); |
63 } | 78 } |
64 List<jsAst.Property> fieldsAndProperties; | 79 List<jsAst.Property> fieldsAndProperties; |
65 if (emitClassDescriptor) { | 80 if (emitClassDescriptor) { |
66 fieldsAndProperties = <jsAst.Property>[]; | 81 fieldsAndProperties = <jsAst.Property>[]; |
67 fieldsAndProperties.add( | 82 fieldsAndProperties.add( |
68 new jsAst.Property( | 83 new jsAst.Property( |
69 js.string(namer.classDescriptorProperty), classData)); | 84 js.string(namer.classDescriptorProperty), classData)); |
70 fieldsAndProperties.addAll(properties); | 85 fieldsAndProperties.addAll(properties); |
71 } else { | 86 } else { |
72 fieldsAndProperties = properties; | 87 fieldsAndProperties = properties; |
73 } | 88 } |
74 return new jsAst.ObjectInitializer(fieldsAndProperties, isOneLiner: false); | 89 return new jsAst.ObjectInitializer(fieldsAndProperties, isOneLiner: false); |
75 } | 90 } |
76 } | 91 } |
OLD | NEW |