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