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

Side by Side Diff: lib/src/compiler/code_generator.dart

Issue 2039173005: Refactoring assignment (Closed) Base URL: https://github.com/dart-lang/dev_compiler@master
Patch Set: Created 4 years, 6 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) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, 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 'dart:collection' show HashMap, HashSet; 5 import 'dart:collection' show HashMap, HashSet;
6 import 'dart:math' show min, max; 6 import 'dart:math' show min, max;
7 7
8 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; 8 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator;
9 import 'package:analyzer/dart/ast/ast.dart'; 9 import 'package:analyzer/dart/ast/ast.dart';
10 import 'package:analyzer/dart/ast/token.dart' show Token, TokenType; 10 import 'package:analyzer/dart/ast/token.dart' show Token, TokenType;
(...skipping 2697 matching lines...) Expand 10 before | Expand all | Expand 10 after
2708 JS.Expression _emitSet(Expression lhs, Expression rhs) { 2708 JS.Expression _emitSet(Expression lhs, Expression rhs) {
2709 if (lhs is IndexExpression) { 2709 if (lhs is IndexExpression) {
2710 var target = _getTarget(lhs); 2710 var target = _getTarget(lhs);
2711 if (_useNativeJsIndexer(target.staticType)) { 2711 if (_useNativeJsIndexer(target.staticType)) {
2712 return js 2712 return js
2713 .call('#[#] = #', [_visit(target), _visit(lhs.index), _visit(rhs)]); 2713 .call('#[#] = #', [_visit(target), _visit(lhs.index), _visit(rhs)]);
2714 } 2714 }
2715 return _emitSend(target, '[]=', [lhs.index, rhs]); 2715 return _emitSend(target, '[]=', [lhs.index, rhs]);
2716 } 2716 }
2717 2717
2718 if (lhs is SimpleIdentifier) {
2719 return _emitSetSimpleIdentifier(lhs, rhs);
2720 }
2721
2718 Expression target = null; 2722 Expression target = null;
2719 SimpleIdentifier id; 2723 SimpleIdentifier id;
2720 if (lhs is PropertyAccess) { 2724 if (lhs is PropertyAccess) {
2721 if (lhs.operator.lexeme == '?.') { 2725 if (lhs.operator.lexeme == '?.') {
2722 return _emitNullSafeSet(lhs, rhs); 2726 return _emitNullSafeSet(lhs, rhs);
2723 } 2727 }
2724
2725 target = _getTarget(lhs); 2728 target = _getTarget(lhs);
2726 id = lhs.propertyName; 2729 id = lhs.propertyName;
2727 } else if (lhs is PrefixedIdentifier) { 2730 } else if (lhs is PrefixedIdentifier) {
2731 if (isLibraryPrefix(lhs.prefix)) {
2732 return _emitSet(lhs.identifier, rhs);
2733 }
2728 target = lhs.prefix; 2734 target = lhs.prefix;
2729 id = lhs.identifier; 2735 id = lhs.identifier;
2736 } else {
2737 assert(false);
2730 } 2738 }
2731 2739
2732 if (target != null && DynamicInvoke.get(target)) { 2740 assert(target != null);
2741
2742 if (target is SuperExpression) {
2743 return _emitSetSuper(lhs, target, id, rhs);
2744 }
2745
2746 if (DynamicInvoke.get(target)) {
2733 if (_inWhitelistCode(lhs)) { 2747 if (_inWhitelistCode(lhs)) {
2734 var vars = <JS.MetaLetVariable, JS.Expression>{}; 2748 var vars = <JS.MetaLetVariable, JS.Expression>{};
2735 var l = _visit(_bindValue(vars, 'l', target)); 2749 var l = _visit(_bindValue(vars, 'l', target));
2736 var name = _emitMemberName(id.name); 2750 var name = _emitMemberName(id.name);
2737 return new JS.MetaLet(vars, [ 2751 return new JS.MetaLet(vars, [
2738 js.call('(#[(#[dart._extensionType]) ? dartx[#] : #] = #)', 2752 js.call('(#[(#[dart._extensionType]) ? dartx[#] : #] = #)',
2739 [l, l, name, name, _visit(rhs)]) 2753 [l, l, name, name, _visit(rhs)])
2740 ]); 2754 ]);
2741 } 2755 }
2742 return js.call('dart.dput(#, #, #)', 2756 return js.call('dart.dput(#, #, #)',
2743 [_visit(target), _emitMemberName(id.name), _visit(rhs)]); 2757 [_visit(target), _emitMemberName(id.name), _visit(rhs)]);
2744 } 2758 }
2745 2759
2760 var accessor = id.staticElement;
2761 var element =
2762 accessor is PropertyAccessorElement ? accessor.variable : accessor;
2763
2764 if (element is ClassMemberElement && element is! ConstructorElement) {
2765 bool isStatic = element.isStatic;
2766 if (isStatic) {
2767 if (element is FieldElement) {
2768 return _emitSetStaticProperty(lhs, element, rhs);
2769 }
2770 return _badAssignment('Unknown static: $element', lhs, rhs);
2771 }
2772 if (element is FieldElement) {
2773 return _emitWriteInstanceProperty(
2774 lhs, _visit(target), element, _visit(rhs));
2775 }
2776 }
2777
2778 return _badAssignment('Unhandled assignment', lhs, rhs);
2779 }
2780
2781 JS.Expression _badAssignment(String problem, Expression lhs, Expression rhs) {
Jennifer Messerly 2016/06/07 23:04:20 do we know what can hit this? Is it a compiler bug
sra1 2016/06/07 23:56:07 It should be a compiler bug (e.g. List = null;). I
2782 return js.call('dart.throw((#, #, #))',
2783 [js.string('$lhs ='), _visit(rhs), js.string(problem)]);
2784 }
2785
2786 /// Emits assignment to a simple identifier. Handles all legal simple
2787 /// identifier assignment targets (local, top level library member, implicit
2788 /// `this` or class, etc.).
2789 JS.Expression _emitSetSimpleIdentifier(
2790 SimpleIdentifier node, Expression rhs) {
2791 JS.Expression unimplemented() {
2792 return _badAssignment("Unimplemented: unknown name '$node'", node, rhs);
2793 }
2794
2795 var accessor = node.staticElement;
2796 if (accessor == null) return unimplemented();
2797
2798 // Get the original declaring element. If we had a property accessor, this
2799 // indirects back to a (possibly synthetic) field.
2800 var element = accessor;
2801 if (accessor is PropertyAccessorElement) element = accessor.variable;
2802
2803 _declareBeforeUse(element);
2804
2805 if (element is LocalVariableElement || element is ParameterElement) {
2806 return _emitSetLocal(node, element, rhs);
2807 }
2808
2809 if (element.enclosingElement is CompilationUnitElement) {
2810 // Top level library member.
2811 return _emitSetTopLevel(node, element, rhs);
2812 }
2813
2814 // Unqualified class member. This could mean implicit `this`, or implicit
2815 // static from the same class.
2816 if (element is ClassMemberElement) {
2817 bool isStatic = element.isStatic;
2818 if (isStatic) {
2819 if (element is FieldElement) {
2820 return _emitSetStaticProperty(node, element, rhs);
2821 }
2822 return unimplemented();
2823 }
2824
2825 // For instance members, we add implicit-this.
2826 if (element is FieldElement) {
2827 return _emitWriteInstanceProperty(
2828 node, new JS.This(), element, _visit(rhs));
2829 }
2830 return unimplemented();
2831 }
2832
2833 // We should not get here.
2834 return unimplemented();
2835 }
2836
2837 /// Emits assignment to a simple local variable or parameter.
2838 JS.Expression _emitSetLocal(
2839 SimpleIdentifier node, Element element, Expression rhs) {
2840 JS.Expression target;
2841 if (element is TemporaryVariableElement) {
2842 // If this is one of our compiler's temporary variables, use its JS form.
2843 target = element.jsVariable;
2844 } else if (element is ParameterElement) {
2845 target = _emitParameter(element);
2846 } else {
2847 target = new JS.Identifier(element.name);
2848 }
2849
2850 return _visit(rhs).toAssignExpression(annotate(target, node));
2851 }
2852
2853 /// Emits assignment to library scope element [element].
2854 JS.Expression _emitSetTopLevel(
2855 Expression lhs, Element element, Expression rhs) {
2856 return _visit(rhs)
2857 .toAssignExpression(annotate(_emitTopLevelName(element), lhs));
2858 }
2859
2860 /// Emits assignment to a static field element or property.
2861 JS.Expression _emitSetStaticProperty(
2862 Expression lhs, Element element, Expression rhs) {
2863 // For static methods, we add the raw type name, without generics or
2864 // library prefix. We don't need those because static calls can't use
2865 // the generic type.
2866 ClassElement classElement = element.enclosingElement;
2867 var type = classElement.type;
2868 var dynType = _emitType(fillDynamicTypeArgs(type));
2869 var member = _emitMemberName(element.name, isStatic: true, type: type);
2870 return _visit(rhs).toAssignExpression(
2871 annotate(new JS.PropertyAccess(dynType, member), lhs));
2872 }
2873
2874 /// Emits an assignment to the [element] property of instance referenced by
2875 /// [jsTarget].
2876 JS.Expression _emitWriteInstanceProperty(Expression lhs,
2877 JS.Expression jsTarget, Element element, JS.Expression value) {
2878 String memberName = element.name;
2879 var type = (element.enclosingElement as ClassElement).type;
2880 var name = _emitMemberName(memberName, type: type);
2881 return value.toAssignExpression(
2882 annotate(new JS.PropertyAccess(jsTarget, name), lhs));
2883 }
2884
2885 JS.Expression _emitSetSuper(Expression lhs, SuperExpression target,
2886 SimpleIdentifier id, Expression rhs) {
2887 // TODO(sra): Fix.
Jennifer Messerly 2016/06/07 23:04:20 Fix what? :)
sra1 2016/06/07 23:56:07 Done.
2746 return _visit(rhs).toAssignExpression(_visit(lhs)); 2888 return _visit(rhs).toAssignExpression(_visit(lhs));
2747 } 2889 }
2748 2890
2749 JS.Expression _emitNullSafeSet(PropertyAccess node, Expression right) { 2891 JS.Expression _emitNullSafeSet(PropertyAccess node, Expression right) {
2750 // Emit `obj?.prop = expr` as: 2892 // Emit `obj?.prop = expr` as:
2751 // 2893 //
2752 // (_ => _ == null ? null : _.prop = expr)(obj). 2894 // (_ => _ == null ? null : _.prop = expr)(obj).
2753 // 2895 //
2754 // We could use a helper, e.g.: `nullSafeSet(e1, _ => _.v = e2)` 2896 // We could use a helper, e.g.: `nullSafeSet(e1, _ => _.v = e2)`
2755 // 2897 //
(...skipping 2230 matching lines...) Expand 10 before | Expand all | Expand 10 after
4986 } 5128 }
4987 5129
4988 bool isLibraryPrefix(Expression node) => 5130 bool isLibraryPrefix(Expression node) =>
4989 node is SimpleIdentifier && node.staticElement is PrefixElement; 5131 node is SimpleIdentifier && node.staticElement is PrefixElement;
4990 5132
4991 LibraryElement _getLibrary(AnalysisContext c, String uri) => 5133 LibraryElement _getLibrary(AnalysisContext c, String uri) =>
4992 c.computeLibraryElement(c.sourceFactory.forUri(uri)); 5134 c.computeLibraryElement(c.sourceFactory.forUri(uri));
4993 5135
4994 bool _isDartRuntime(LibraryElement l) => 5136 bool _isDartRuntime(LibraryElement l) =>
4995 l.isInSdk && l.source.uri.toString() == 'dart:_runtime'; 5137 l.isInSdk && l.source.uri.toString() == 'dart:_runtime';
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