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

Side by Side Diff: lib/compiler/implementation/native_emitter.dart

Issue 10386161: Start compiler refactoring to support more than a single backend. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 class NativeEmitter { 5 class NativeEmitter {
6 6
7 Compiler compiler; 7 CodeEmitterTask emitter;
8 StringBuffer nativeBuffer; 8 StringBuffer nativeBuffer;
9 9
10 // Classes that participate in dynamic dispatch. These are the 10 // Classes that participate in dynamic dispatch. These are the
11 // classes that contain used members. 11 // classes that contain used members.
12 Set<ClassElement> classesWithDynamicDispatch; 12 Set<ClassElement> classesWithDynamicDispatch;
13 13
14 // Native classes found in the application. 14 // Native classes found in the application.
15 Set<ClassElement> nativeClasses; 15 Set<ClassElement> nativeClasses;
16 16
17 // Caches the native subtypes of a native class. 17 // Caches the native subtypes of a native class.
18 Map<ClassElement, List<ClassElement>> subtypes; 18 Map<ClassElement, List<ClassElement>> subtypes;
19 19
20 // Caches the direct native subtypes of a native class. 20 // Caches the direct native subtypes of a native class.
21 Map<ClassElement, List<ClassElement>> directSubtypes; 21 Map<ClassElement, List<ClassElement>> directSubtypes;
22 22
23 // Caches the native methods that are overridden by a native class. 23 // Caches the native methods that are overridden by a native class.
24 // Note that the method that overrides does not have to be native: 24 // Note that the method that overrides does not have to be native:
25 // it's the overridden method that must make sure it will dispatch 25 // it's the overridden method that must make sure it will dispatch
26 // to its subclass if it sees an instance whose class is a subclass. 26 // to its subclass if it sees an instance whose class is a subclass.
27 Set<FunctionElement> overriddenMethods; 27 Set<FunctionElement> overriddenMethods;
28 28
29 // Caches the methods that should just call a native JS 29 // Caches the methods that should just call a native JS
30 // implementation. 30 // implementation.
31 Set<FunctionElement> nativeMethods; 31 Set<FunctionElement> nativeMethods;
32 32
33 NativeEmitter(this.compiler) 33 NativeEmitter(this.emitter)
34 : classesWithDynamicDispatch = new Set<ClassElement>(), 34 : classesWithDynamicDispatch = new Set<ClassElement>(),
35 nativeClasses = new Set<ClassElement>(), 35 nativeClasses = new Set<ClassElement>(),
36 subtypes = new Map<ClassElement, List<ClassElement>>(), 36 subtypes = new Map<ClassElement, List<ClassElement>>(),
37 directSubtypes = new Map<ClassElement, List<ClassElement>>(), 37 directSubtypes = new Map<ClassElement, List<ClassElement>>(),
38 overriddenMethods = new Set<FunctionElement>(), 38 overriddenMethods = new Set<FunctionElement>(),
39 nativeMethods = new Set<FunctionElement>(), 39 nativeMethods = new Set<FunctionElement>(),
40 nativeBuffer = new StringBuffer(); 40 nativeBuffer = new StringBuffer();
41 41
42 Compiler get compiler() => emitter.compiler;
43
42 String get dynamicName() { 44 String get dynamicName() {
43 Element element = compiler.findHelper( 45 Element element = compiler.findHelper(
44 const SourceString('dynamicFunction')); 46 const SourceString('dynamicFunction'));
45 return compiler.namer.isolateAccess(element); 47 return compiler.namer.isolateAccess(element);
46 } 48 }
47 49
48 String get dynamicSetMetadataName() { 50 String get dynamicSetMetadataName() {
49 Element element = compiler.findHelper( 51 Element element = compiler.findHelper(
50 const SourceString('dynamicSetMetadata')); 52 const SourceString('dynamicSetMetadata'));
51 return compiler.namer.isolateAccess(element); 53 return compiler.namer.isolateAccess(element);
(...skipping 16 matching lines...) Expand all
68 const SourceString('toStringForNativeObject')); 70 const SourceString('toStringForNativeObject'));
69 return compiler.namer.isolateAccess(element); 71 return compiler.namer.isolateAccess(element);
70 } 72 }
71 73
72 String get defineNativeClassName() 74 String get defineNativeClassName()
73 => '${compiler.namer.CURRENT_ISOLATE}.\$defineNativeClass'; 75 => '${compiler.namer.CURRENT_ISOLATE}.\$defineNativeClass';
74 76
75 String get defineNativeClassFunction() { 77 String get defineNativeClassFunction() {
76 return """ 78 return """
77 function(cls, fields, methods) { 79 function(cls, fields, methods) {
78 var generateGetterSetter = ${compiler.emitter.generateGetterSetterFunction}; 80 var generateGetterSetter = ${emitter.generateGetterSetterFunction};
79 for (var i = 0; i < fields.length; i++) { 81 for (var i = 0; i < fields.length; i++) {
80 generateGetterSetter(fields[i], methods); 82 generateGetterSetter(fields[i], methods);
81 } 83 }
82 for (var method in methods) { 84 for (var method in methods) {
83 $dynamicName(method)[cls] = methods[method]; 85 $dynamicName(method)[cls] = methods[method];
84 } 86 }
85 }"""; 87 }""";
86 } 88 }
87 89
88 void generateNativeLiteral(ClassElement classElement) { 90 void generateNativeLiteral(ClassElement classElement) {
89 String quotedNative = classElement.nativeName.slowToString(); 91 String quotedNative = classElement.nativeName.slowToString();
90 String nativeCode = quotedNative.substring(2, quotedNative.length - 1); 92 String nativeCode = quotedNative.substring(2, quotedNative.length - 1);
91 String className = compiler.namer.getName(classElement); 93 String className = compiler.namer.getName(classElement);
92 nativeBuffer.add(className); 94 nativeBuffer.add(className);
93 nativeBuffer.add(' = '); 95 nativeBuffer.add(' = ');
94 nativeBuffer.add(nativeCode); 96 nativeBuffer.add(nativeCode);
95 nativeBuffer.add(';\n'); 97 nativeBuffer.add(';\n');
96 98
97 void defineInstanceMember(String name, String value) { 99 void defineInstanceMember(String name, String value) {
98 nativeBuffer.add("$className.$name = $value;\n"); 100 nativeBuffer.add("$className.$name = $value;\n");
99 } 101 }
100 102
101 for (Element member in classElement.members) { 103 for (Element member in classElement.members) {
102 if (member.isInstanceMember()) { 104 if (member.isInstanceMember()) {
103 compiler.emitter.addInstanceMember(member, defineInstanceMember); 105 emitter.addInstanceMember(member, defineInstanceMember);
104 } 106 }
105 } 107 }
106 } 108 }
107 109
108 bool isNativeLiteral(String quotedName) { 110 bool isNativeLiteral(String quotedName) {
109 return quotedName[1] === '='; 111 return quotedName[1] === '=';
110 } 112 }
111 113
112 bool isNativeGlobal(String quotedName) { 114 bool isNativeGlobal(String quotedName) {
113 return quotedName[1] === '@'; 115 return quotedName[1] === '@';
(...skipping 15 matching lines...) Expand all
129 assert(classElement.backendMembers.isEmpty()); 131 assert(classElement.backendMembers.isEmpty());
130 String quotedName = classElement.nativeName.slowToString(); 132 String quotedName = classElement.nativeName.slowToString();
131 if (isNativeLiteral(quotedName)) { 133 if (isNativeLiteral(quotedName)) {
132 generateNativeLiteral(classElement); 134 generateNativeLiteral(classElement);
133 // The native literal kind needs to be dealt with specially when 135 // The native literal kind needs to be dealt with specially when
134 // generating code for it. 136 // generating code for it.
135 return; 137 return;
136 } 138 }
137 139
138 StringBuffer fieldBuffer = new StringBuffer(); 140 StringBuffer fieldBuffer = new StringBuffer();
139 compiler.emitter.emitClassFields(classElement, fieldBuffer); 141 emitter.emitClassFields(classElement, fieldBuffer);
140 142
141 StringBuffer methodBuffer = new StringBuffer(); 143 StringBuffer methodBuffer = new StringBuffer();
142 compiler.emitter.emitInstanceMembers(classElement, methodBuffer); 144 emitter.emitInstanceMembers(classElement, methodBuffer);
143 145
144 if (methodBuffer.isEmpty() && fieldBuffer.isEmpty()) return; 146 if (methodBuffer.isEmpty() && fieldBuffer.isEmpty()) return;
145 147
146 String nativeName = toNativeName(classElement); 148 String nativeName = toNativeName(classElement);
147 nativeBuffer.add("$defineNativeClassName('$nativeName', ["); 149 nativeBuffer.add("$defineNativeClassName('$nativeName', [");
148 nativeBuffer.add(fieldBuffer); 150 nativeBuffer.add(fieldBuffer);
149 nativeBuffer.add('], {'); 151 nativeBuffer.add('], {');
150 nativeBuffer.add(methodBuffer); 152 nativeBuffer.add(methodBuffer);
151 nativeBuffer.add('\n});\n\n'); 153 nativeBuffer.add('\n});\n\n');
152 154
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 String toStringName = compiler.namer.instanceMethodName( 385 String toStringName = compiler.namer.instanceMethodName(
384 null, const SourceString('toString'), 0); 386 null, const SourceString('toString'), 0);
385 objectProperties.add("$defPropName(Object.prototype, '$toStringName', "); 387 objectProperties.add("$defPropName(Object.prototype, '$toStringName', ");
386 objectProperties.add( 388 objectProperties.add(
387 'function() { return $toStringHelperName(this); });\n'); 389 'function() { return $toStringHelperName(this); });\n');
388 390
389 targetBuffer.add('$defineNativeClassName = $defineNativeClassFunction;\n'); 391 targetBuffer.add('$defineNativeClassName = $defineNativeClassFunction;\n');
390 targetBuffer.add('$objectProperties$nativeBuffer\n'); 392 targetBuffer.add('$objectProperties$nativeBuffer\n');
391 } 393 }
392 } 394 }
OLDNEW
« no previous file with comments | « lib/compiler/implementation/emitter.dart ('k') | lib/compiler/implementation/native_handler.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698