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

Side by Side Diff: sdk/lib/_internal/compiler/implementation/compile_time_constants.dart

Issue 195983006: Constrain constant references from deferred libraries (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 9 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 part of dart2js; 5 part of dart2js;
6 6
7 /** 7 /**
8 * The [ConstantHandler] keeps track of compile-time constants, 8 * The [ConstantHandler] keeps track of compile-time constants,
9 * initializations of global and static fields, and default values of 9 * initializations of global and static fields, and default values of
10 * optional parameters. 10 * optional parameters.
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 node, type, compiler.symbolConstructor, createArguments); 404 node, type, compiler.symbolConstructor, createArguments);
405 } 405 }
406 406
407 Constant makeTypeConstant(Element element) { 407 Constant makeTypeConstant(Element element) {
408 DartType elementType = element.computeType(compiler).asRaw(); 408 DartType elementType = element.computeType(compiler).asRaw();
409 DartType constantType = 409 DartType constantType =
410 compiler.backend.typeImplementation.computeType(compiler); 410 compiler.backend.typeImplementation.computeType(compiler);
411 return new TypeConstant(elementType, constantType); 411 return new TypeConstant(elementType, constantType);
412 } 412 }
413 413
414 /// Returns true if the prefix of the send resolves to a deferred import
415 /// prefix.
416 bool isDeferredUse(Send send) {
417 // We handle:
418 // 1. Send(Send(Send(null, Prefix), className), staticName)
419 // 2. Send(Send(Prefix, Classname), staticName)
420 // 3. Send(Send(null, Prefix), staticName | className)
421 // 4. Send(Send(Prefix), staticName | className)
422 // Nodes of the forms 2,4 occur in metadata.
423 if (send == null) return false;
424 if (send.receiver is Send) {
floitsch 2014/03/12 16:48:13 Can't you just do a loop here? while (send.receive
425 send = send.receiver;
426 if (send.receiver is Send) {
427 send = send.receiver;
428 }
429 }
430 Identifier prefixNode;
431 if (send.receiver is Identifier) {
432 prefixNode = send.receiver;
433 } else if (send.receiver == null &&
434 send.selector is Identifier) {
435 prefixNode = send.selector;
436 }
437 if (prefixNode != null) {
438 Element maybePrefix = elements[prefixNode.asIdentifier()];
439 if (maybePrefix != null && maybePrefix.isPrefix() &&
440 (maybePrefix as PrefixElement).isDeferred) {
441 return true;
442 }
443 }
444 return false;
445 }
446
414 // TODO(floitsch): provide better error-messages. 447 // TODO(floitsch): provide better error-messages.
415 Constant visitSend(Send send) { 448 Constant visitSend(Send send) {
416 Element element = elements[send]; 449 Element element = elements[send];
417 if (send.isPropertyAccess) { 450 if (send.isPropertyAccess) {
451 if (isDeferredUse(send)) {
452 return signalNotCompileTimeConstant(send,
453 message: MessageKind.DEFERRED_COMPILE_TIME_CONSTANT);
454 }
418 if (Elements.isStaticOrTopLevelFunction(element)) { 455 if (Elements.isStaticOrTopLevelFunction(element)) {
419 return new FunctionConstant(element); 456 return new FunctionConstant(element);
420 } else if (Elements.isStaticOrTopLevelField(element)) { 457 } else if (Elements.isStaticOrTopLevelField(element)) {
421 Constant result; 458 Constant result;
422 if (element.modifiers.isConst()) { 459 if (element.modifiers.isConst()) {
423 result = handler.compileConstant(element); 460 result = handler.compileConstant(element);
424 } else if (element.modifiers.isFinal() && !isEvaluatingConstant) { 461 } else if (element.modifiers.isFinal() && !isEvaluatingConstant) {
425 result = handler.compileVariable(element); 462 result = handler.compileVariable(element);
426 } 463 }
427 if (result != null) return result; 464 if (result != null) return result;
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
623 } 660 }
624 661
625 Send send = node.send; 662 Send send = node.send;
626 FunctionElement constructor = elements[send]; 663 FunctionElement constructor = elements[send];
627 if (Elements.isUnresolved(constructor)) { 664 if (Elements.isUnresolved(constructor)) {
628 return signalNotCompileTimeConstant(node); 665 return signalNotCompileTimeConstant(node);
629 } 666 }
630 667
631 // Deferred types can not be used in const instance creation expressions. 668 // Deferred types can not be used in const instance creation expressions.
632 // Check if the constructor comes from a deferred library. 669 // Check if the constructor comes from a deferred library.
633 Send selectorSend = node.send.selector.asSend(); 670 if (isDeferredUse(node.send.selector.asSend())) {
634 if (selectorSend != null) { 671 return signalNotCompileTimeConstant(node,
635 Identifier receiver = selectorSend.receiver.asIdentifier(); 672 message: MessageKind.DEFERRED_COMPILE_TIME_CONSTANT_CONSTRUCTION);
636 if (receiver != null) {
637 Element element = elements[receiver];
638 if (element.isPrefix() && (element as PrefixElement).isDeferred) {
639 return signalNotCompileTimeConstant(node,
640 message: MessageKind.DEFERRED_COMPILE_TIME_CONSTANT);
641 }
642 }
643 } 673 }
644 674
645 // TODO(ahe): This is nasty: we must eagerly analyze the 675 // TODO(ahe): This is nasty: we must eagerly analyze the
646 // constructor to ensure the redirectionTarget has been computed 676 // constructor to ensure the redirectionTarget has been computed
647 // correctly. Find a way to avoid this. 677 // correctly. Find a way to avoid this.
648 compiler.analyzeElement(constructor.declaration); 678 compiler.analyzeElement(constructor.declaration);
649 679
650 InterfaceType type = elements.getType(node); 680 InterfaceType type = elements.getType(node);
651 List<Constant> evaluateArguments(FunctionElement constructor) { 681 List<Constant> evaluateArguments(FunctionElement constructor) {
652 Selector selector = elements.getSelector(send); 682 Selector selector = elements.getSelector(send);
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after
973 if (fieldValue == null) { 1003 if (fieldValue == null) {
974 // Use the default value. 1004 // Use the default value.
975 fieldValue = handler.compileConstant(field); 1005 fieldValue = handler.compileConstant(field);
976 } 1006 }
977 jsNewArguments.add(fieldValue); 1007 jsNewArguments.add(fieldValue);
978 }, 1008 },
979 includeSuperAndInjectedMembers: true); 1009 includeSuperAndInjectedMembers: true);
980 return jsNewArguments; 1010 return jsNewArguments;
981 } 1011 }
982 } 1012 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698