Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 library ddc.src.codegen.js_codegen; | 5 library ddc.src.codegen.js_codegen; |
| 6 | 6 |
| 7 import 'dart:collection' show HashSet; | 7 import 'dart:collection' show HashSet; |
| 8 import 'dart:io' show Directory, File; | 8 import 'dart:io' show Directory, File; |
| 9 | 9 |
| 10 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; | 10 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; |
| (...skipping 1029 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1040 rules.isNumType(t)); | 1040 rules.isNumType(t)); |
| 1041 | 1041 |
| 1042 bool typeIsNonNullablePrimitiveInJS(DartType t) => | 1042 bool typeIsNonNullablePrimitiveInJS(DartType t) => |
| 1043 typeIsPrimitiveInJS(t) && rules.isNonNullableType(t); | 1043 typeIsPrimitiveInJS(t) && rules.isNonNullableType(t); |
| 1044 | 1044 |
| 1045 bool binaryOperationIsPrimitive(DartType leftT, DartType rightT) => | 1045 bool binaryOperationIsPrimitive(DartType leftT, DartType rightT) => |
| 1046 typeIsPrimitiveInJS(leftT) && typeIsPrimitiveInJS(rightT); | 1046 typeIsPrimitiveInJS(leftT) && typeIsPrimitiveInJS(rightT); |
| 1047 | 1047 |
| 1048 bool unaryOperationIsPrimitive(DartType t) => typeIsPrimitiveInJS(t); | 1048 bool unaryOperationIsPrimitive(DartType t) => typeIsPrimitiveInJS(t); |
| 1049 | 1049 |
| 1050 bool _isNonNullableExpression(Expression expr) { | |
| 1051 // TODO(vsm): Revisit whether we really need this when we get | |
| 1052 // better non-nullability in the type system. | |
| 1053 | |
| 1054 if (expr is Literal && expr is! NullLiteral) { | |
| 1055 return true; | |
| 1056 } | |
| 1057 if (expr is ParenthesizedExpression) { | |
| 1058 return _isNonNullableExpression(expr.expression); | |
| 1059 } | |
| 1060 DartType type = null; | |
| 1061 if (expr is BinaryExpression) { | |
| 1062 type = rules.getStaticType(expr.leftOperand); | |
| 1063 } else if (expr is PrefixExpression) { | |
| 1064 type = rules.getStaticType(expr.operand); | |
| 1065 } else if (expr is PostfixExpression) { | |
| 1066 type = rules.getStaticType(expr.operand); | |
| 1067 } | |
| 1068 if (type != null && typeIsPrimitiveInJS(type)) { | |
| 1069 return true; | |
| 1070 } | |
| 1071 if (expr is MethodInvocation) { | |
| 1072 // TODO(vsm): This logic overlaps with the resolver. | |
| 1073 // Where is the best place to put this? | |
| 1074 var e = expr.methodName.staticElement; | |
| 1075 if (e is FunctionElement && | |
|
Jennifer Messerly
2015/03/03 20:05:01
at some point I wonder if we want a helper for thi
| |
| 1076 e.library.name == '_foreign_helper' && | |
| 1077 e.name == 'JS') { | |
| 1078 // Fix types for JS builtin calls. | |
| 1079 // | |
| 1080 // This code was taken from analyzer. It's not super sophisticated: | |
| 1081 // only looks for the type name in dart:core, so we just copy it here. | |
| 1082 // | |
| 1083 // TODO(jmesserly): we'll likely need something that can handle a wider | |
| 1084 // variety of types, especially when we get to JS interop. | |
| 1085 var args = expr.argumentList.arguments; | |
| 1086 if (args.isNotEmpty && args.first is SimpleStringLiteral) { | |
| 1087 var types = args.first.stringValue; | |
| 1088 if (!types.split('|').contains('Null')) { | |
| 1089 return true; | |
| 1090 } | |
| 1091 } | |
| 1092 } | |
| 1093 } | |
| 1094 return false; | |
| 1095 } | |
| 1096 | |
| 1050 JS.Expression notNull(Expression expr) { | 1097 JS.Expression notNull(Expression expr) { |
| 1051 var type = rules.getStaticType(expr); | 1098 var type = rules.getStaticType(expr); |
| 1052 if (rules.isNonNullableType(type)) { | 1099 if (rules.isNonNullableType(type) || _isNonNullableExpression(expr)) { |
| 1053 return _visit(expr); | 1100 return _visit(expr); |
| 1054 } else { | 1101 } else { |
| 1055 return js.call('dart.notNull(#)', _visit(expr)); | 1102 return js.call('dart.notNull(#)', _visit(expr)); |
| 1056 } | 1103 } |
| 1057 } | 1104 } |
| 1058 | 1105 |
| 1059 @override | 1106 @override |
| 1060 JS.Expression visitBinaryExpression(BinaryExpression node) { | 1107 JS.Expression visitBinaryExpression(BinaryExpression node) { |
| 1061 var op = node.operator; | 1108 var op = node.operator; |
| 1062 var left = node.leftOperand; | 1109 var left = node.leftOperand; |
| (...skipping 853 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1916 | 1963 |
| 1917 // TODO(jmesserly): in many cases marking the end will be unncessary. | 1964 // TODO(jmesserly): in many cases marking the end will be unncessary. |
| 1918 printer.mark(_location(node.end)); | 1965 printer.mark(_location(node.end)); |
| 1919 } | 1966 } |
| 1920 | 1967 |
| 1921 String _getIdentifier(AstNode node) { | 1968 String _getIdentifier(AstNode node) { |
| 1922 if (node is SimpleIdentifier) return node.name; | 1969 if (node is SimpleIdentifier) return node.name; |
| 1923 return null; | 1970 return null; |
| 1924 } | 1971 } |
| 1925 } | 1972 } |
| OLD | NEW |