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

Side by Side Diff: pkg/compiler/lib/src/js_backend/constant_emitter.dart

Issue 2981113002: Use entities in constant_emitter (Closed)
Patch Set: Created 3 years, 5 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 import '../common.dart'; 5 import '../common.dart';
6 import '../common_elements.dart'; 6 import '../common_elements.dart';
7 import '../constants/values.dart'; 7 import '../constants/values.dart';
8 import '../elements/elements.dart';
9 import '../elements/types.dart' show TypeVariableType;
10 import '../elements/entities.dart'; 8 import '../elements/entities.dart';
11 import '../elements/resolution_types.dart'; 9 import '../elements/resolution_types.dart' show ResolutionTypedefType;
10 import '../elements/types.dart';
12 import '../io/code_output.dart'; 11 import '../io/code_output.dart';
13 import '../js/js.dart' as jsAst; 12 import '../js/js.dart' as jsAst;
14 import '../js/js.dart' show js; 13 import '../js/js.dart' show js;
15 import '../js_emitter/code_emitter_task.dart'; 14 import '../js_emitter/code_emitter_task.dart';
16 import '../options.dart'; 15 import '../options.dart';
17 import '../universe/world_builder.dart'; 16 import '../universe/world_builder.dart';
18 import 'constant_system_javascript.dart'; 17 import 'constant_system_javascript.dart';
19 import 'js_backend.dart'; 18 import 'js_backend.dart';
20 import 'namer.dart'; 19 import 'namer.dart';
21 import 'runtime_types.dart'; 20 import 'runtime_types.dart';
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 jsAst.Expression keyExpression = 217 jsAst.Expression keyExpression =
219 constantReferenceGenerator(constant.keys[i]); 218 constantReferenceGenerator(constant.keys[i]);
220 jsAst.Expression valueExpression = 219 jsAst.Expression valueExpression =
221 constantReferenceGenerator(constant.values[i]); 220 constantReferenceGenerator(constant.values[i]);
222 data.add(keyExpression); 221 data.add(keyExpression);
223 data.add(valueExpression); 222 data.add(valueExpression);
224 } 223 }
225 return new jsAst.ArrayInitializer(data); 224 return new jsAst.ArrayInitializer(data);
226 } 225 }
227 226
228 ClassElement classElement = constant.type.element; 227 ClassEntity classElement = constant.type.element;
229 String className = classElement.name; 228 String className = classElement.name;
230 229
231 List<jsAst.Expression> arguments = <jsAst.Expression>[]; 230 List<jsAst.Expression> arguments = <jsAst.Expression>[];
232 231
233 // The arguments of the JavaScript constructor for any given Dart class 232 // The arguments of the JavaScript constructor for any given Dart class
234 // are in the same order as the members of the class element. 233 // are in the same order as the members of the class element.
235 int emittedArgumentCount = 0; 234 int emittedArgumentCount = 0;
236 classElement.implementation.forEachInstanceField( 235 _worldBuilder.forEachInstanceField(classElement,
237 (ClassElement enclosing, Element field) { 236 (ClassEntity enclosing, FieldEntity field) {
238 if (field.name == JavaScriptMapConstant.LENGTH_NAME) { 237 if (field.name == JavaScriptMapConstant.LENGTH_NAME) {
239 arguments 238 arguments
240 .add(new jsAst.LiteralNumber('${constant.keyList.entries.length}')); 239 .add(new jsAst.LiteralNumber('${constant.keyList.entries.length}'));
241 } else if (field.name == JavaScriptMapConstant.JS_OBJECT_NAME) { 240 } else if (field.name == JavaScriptMapConstant.JS_OBJECT_NAME) {
242 arguments.add(jsMap()); 241 arguments.add(jsMap());
243 } else if (field.name == JavaScriptMapConstant.KEYS_NAME) { 242 } else if (field.name == JavaScriptMapConstant.KEYS_NAME) {
244 arguments.add(constantReferenceGenerator(constant.keyList)); 243 arguments.add(constantReferenceGenerator(constant.keyList));
245 } else if (field.name == JavaScriptMapConstant.PROTO_VALUE) { 244 } else if (field.name == JavaScriptMapConstant.PROTO_VALUE) {
246 assert(constant.protoValue != null); 245 assert(constant.protoValue != null);
247 arguments.add(constantReferenceGenerator(constant.protoValue)); 246 arguments.add(constantReferenceGenerator(constant.protoValue));
248 } else if (field.name == JavaScriptMapConstant.JS_DATA_NAME) { 247 } else if (field.name == JavaScriptMapConstant.JS_DATA_NAME) {
249 arguments.add(jsGeneralMap()); 248 arguments.add(jsGeneralMap());
250 } else { 249 } else {
251 failedAt(field, 250 failedAt(field,
252 "Compiler has unexpected field ${field.name} for ${className}."); 251 "Compiler has unexpected field ${field.name} for ${className}.");
253 } 252 }
254 emittedArgumentCount++; 253 emittedArgumentCount++;
255 }, includeSuperAndInjectedMembers: true); 254 });
256 if ((className == JavaScriptMapConstant.DART_STRING_CLASS && 255 if ((className == JavaScriptMapConstant.DART_STRING_CLASS &&
257 emittedArgumentCount != 3) || 256 emittedArgumentCount != 3) ||
258 (className == JavaScriptMapConstant.DART_PROTO_CLASS && 257 (className == JavaScriptMapConstant.DART_PROTO_CLASS &&
259 emittedArgumentCount != 4) || 258 emittedArgumentCount != 4) ||
260 (className == JavaScriptMapConstant.DART_GENERAL_CLASS && 259 (className == JavaScriptMapConstant.DART_GENERAL_CLASS &&
261 emittedArgumentCount != 1)) { 260 emittedArgumentCount != 1)) {
262 failedAt(classElement, 261 failedAt(classElement,
263 "Compiler and ${className} disagree on number of fields."); 262 "Compiler and ${className} disagree on number of fields.");
264 } 263 }
265 264
266 if (_rtiNeed.classNeedsRtiField(classElement)) { 265 if (_rtiNeed.classNeedsRtiField(classElement)) {
267 arguments.add(_reifiedTypeArguments(constant.type)); 266 arguments.add(_reifiedTypeArguments(constant.type));
268 } 267 }
269 268
270 jsAst.Expression constructor = _emitter.constructorAccess(classElement); 269 jsAst.Expression constructor = _emitter.constructorAccess(classElement);
271 jsAst.Expression value = new jsAst.New(constructor, arguments); 270 jsAst.Expression value = new jsAst.New(constructor, arguments);
272 return value; 271 return value;
273 } 272 }
274 273
275 jsAst.PropertyAccess getHelperProperty(MethodElement helper) { 274 jsAst.PropertyAccess getHelperProperty(FunctionEntity helper) {
276 return _emitter.staticFunctionAccess(helper); 275 return _emitter.staticFunctionAccess(helper);
277 } 276 }
278 277
279 @override 278 @override
280 jsAst.Expression visitType(TypeConstantValue constant, [_]) { 279 jsAst.Expression visitType(TypeConstantValue constant, [_]) {
281 ResolutionDartType type = constant.representedType; 280 DartType type = constant.representedType;
282 jsAst.Name typeName = _namer.runtimeTypeName(type.element); 281 jsAst.Name typeName;
282 Entity element;
283 if (type is InterfaceType) {
284 element = type.element;
285 } else if (type is ResolutionTypedefType) {
286 // TODO(redemption): Handle typedef type literals from .dill.
287 element = type.element;
288 } else {
289 assert(type is DynamicType);
290 }
291 typeName = _namer.runtimeTypeName(element);
283 return new jsAst.Call(getHelperProperty(_commonElements.createRuntimeType), 292 return new jsAst.Call(getHelperProperty(_commonElements.createRuntimeType),
284 [js.quoteName(typeName)]); 293 [js.quoteName(typeName)]);
285 } 294 }
286 295
287 @override 296 @override
288 jsAst.Expression visitInterceptor(InterceptorConstantValue constant, [_]) { 297 jsAst.Expression visitInterceptor(InterceptorConstantValue constant, [_]) {
289 ClassEntity interceptorClass = constant.cls; 298 ClassEntity interceptorClass = constant.cls;
290 return _task.interceptorPrototypeAccess(interceptorClass); 299 return _task.interceptorPrototypeAccess(interceptorClass);
291 } 300 }
292 301
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 } 333 }
325 jsAst.New instantiation = new jsAst.New(constructor, fields); 334 jsAst.New instantiation = new jsAst.New(constructor, fields);
326 return instantiation; 335 return instantiation;
327 } 336 }
328 337
329 String stripComments(String rawJavaScript) { 338 String stripComments(String rawJavaScript) {
330 return rawJavaScript.replaceAll(COMMENT_RE, ''); 339 return rawJavaScript.replaceAll(COMMENT_RE, '');
331 } 340 }
332 341
333 jsAst.Expression maybeAddTypeArguments( 342 jsAst.Expression maybeAddTypeArguments(
334 ResolutionInterfaceType type, jsAst.Expression value) { 343 InterfaceType type, jsAst.Expression value) {
335 if (type is ResolutionInterfaceType && 344 if (type is InterfaceType &&
336 !type.treatAsRaw && 345 !type.treatAsRaw &&
337 _rtiNeed.classNeedsRti(type.element)) { 346 _rtiNeed.classNeedsRti(type.element)) {
338 return new jsAst.Call( 347 return new jsAst.Call(
339 getHelperProperty(_commonElements.setRuntimeTypeInfo), 348 getHelperProperty(_commonElements.setRuntimeTypeInfo),
340 [value, _reifiedTypeArguments(type)]); 349 [value, _reifiedTypeArguments(type)]);
341 } 350 }
342 return value; 351 return value;
343 } 352 }
344 353
345 jsAst.Expression _reifiedTypeArguments(ResolutionInterfaceType type) { 354 jsAst.Expression _reifiedTypeArguments(InterfaceType type) {
346 jsAst.Expression unexpected(TypeVariableType _variable) { 355 jsAst.Expression unexpected(TypeVariableType _variable) {
347 ResolutionTypeVariableType variable = _variable; 356 TypeVariableType variable = _variable;
348 throw failedAt( 357 throw failedAt(
349 NO_LOCATION_SPANNABLE, 358 NO_LOCATION_SPANNABLE,
350 "Unexpected type variable '${variable.getStringAsDeclared(null)}'" 359 "Unexpected type variable '${variable}'"
351 " in constant type '${type.getStringAsDeclared(null)}'"); 360 " in constant type '${type}'");
352 } 361 }
353 362
354 List<jsAst.Expression> arguments = <jsAst.Expression>[]; 363 List<jsAst.Expression> arguments = <jsAst.Expression>[];
355 for (ResolutionDartType argument in type.typeArguments) { 364 for (DartType argument in type.typeArguments) {
356 arguments.add( 365 arguments.add(
357 _rtiEncoder.getTypeRepresentation(_emitter, argument, unexpected)); 366 _rtiEncoder.getTypeRepresentation(_emitter, argument, unexpected));
358 } 367 }
359 return new jsAst.ArrayInitializer(arguments); 368 return new jsAst.ArrayInitializer(arguments);
360 } 369 }
361 370
362 @override 371 @override
363 jsAst.Expression visitDeferred(DeferredConstantValue constant, [_]) { 372 jsAst.Expression visitDeferred(DeferredConstantValue constant, [_]) {
364 return constantReferenceGenerator(constant.referenced); 373 return constantReferenceGenerator(constant.referenced);
365 } 374 }
366 } 375 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698