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

Side by Side Diff: frog/member.dart

Issue 8729018: Fixing string compile-time constant evaluation (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: john comments Created 9 years 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
« no previous file with comments | « frog/gen.dart ('k') | frog/utils.dart » ('j') | 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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 /** A formal parameter to a [Method]. */ 5 /** A formal parameter to a [Method]. */
6 class Parameter { 6 class Parameter {
7 FormalNode definition; 7 FormalNode definition;
8 Member method; 8 Member method;
9 9
10 String name; 10 String name;
(...skipping 1158 matching lines...) Expand 10 before | Expand all | Expand 10 after
1169 return new EvaluatedValue(inferredResult, value, "$value", node.span); 1169 return new EvaluatedValue(inferredResult, value, "$value", node.span);
1170 } 1170 }
1171 } else if (declaringType.isString) { 1171 } else if (declaringType.isString) {
1172 if (name == '\$index') { 1172 if (name == '\$index') {
1173 // Note: this could technically propagate constness, but that's not 1173 // Note: this could technically propagate constness, but that's not
1174 // specified explicitly and the VM doesn't do that. 1174 // specified explicitly and the VM doesn't do that.
1175 return new Value(declaringType, '${target.code}[${argsCode[0]}]', 1175 return new Value(declaringType, '${target.code}[${argsCode[0]}]',
1176 node.span); 1176 node.span);
1177 } else if (name == '\$add') { 1177 } else if (name == '\$add') {
1178 if (allConst) { 1178 if (allConst) {
1179 var val0 = target.dynamic.actualValue; 1179 final value = _normConcat(target, args.values[0]);
1180 val0 = val0.substring(1, val0.length - 1);
1181 var val1 = args.values[0].dynamic.actualValue;
1182 if (args.values[0].type.isString) {
1183 val1 = val1.substring(1, val1.length - 1);
1184 }
1185 var value = '${val0}${val1}';
1186 value = '"' + value.replaceAll('"', '\\"') + '"';
1187 return new EvaluatedValue(world.stringType, value, value, node.span); 1180 return new EvaluatedValue(world.stringType, value, value, node.span);
1188 } 1181 }
1189 1182
1190 // Ensure we generate toString on the right side 1183 // Ensure we generate toString on the right side
1191 return new Value(declaringType, '${target.code} + ${argsCode[0]}', 1184 return new Value(declaringType, '${target.code} + ${argsCode[0]}',
1192 node.span); 1185 node.span);
1193 } 1186 }
1194 } else if (declaringType.isNative) { 1187 } else if (declaringType.isNative) {
1195 if (name == '\$index') { 1188 if (name == '\$index') {
1196 // Note: this could technically propagate constness, but that's not 1189 // Note: this could technically propagate constness, but that's not
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1242 } else if (name == '\$setindex') { 1235 } else if (name == '\$setindex') {
1243 world.gen.corejs.useSetIndex = true; 1236 world.gen.corejs.useSetIndex = true;
1244 } 1237 }
1245 1238
1246 // Fall back to normal method invocation. 1239 // Fall back to normal method invocation.
1247 var argsString = Strings.join(argsCode, ', '); 1240 var argsString = Strings.join(argsCode, ', ');
1248 return new Value(inferredResult, '${target.code}.$jsname($argsString)', 1241 return new Value(inferredResult, '${target.code}.$jsname($argsString)',
1249 node.span); 1242 node.span);
1250 } 1243 }
1251 1244
1245 /**
1246 * Return the string concatenation of two values, which is normalized to use
1247 * double-quotes if any of the input strings used double-quotes.
1248 */
1249 String _normConcat(Value a, Value b) {
1250 assert(b.type.isString);
1251 var val0 = a.dynamic.actualValue;
1252 var quote0 = val0[0];
1253 val0 = val0.substring(1, val0.length - 1);
1254 var val1 = b.dynamic.actualValue;
1255 var quote1 = null;
1256 if (b.type.isString) {
1257 quote1 = val1[0];
1258 val1 = val1.substring(1, val1.length - 1);
1259 }
1260 var value;
1261 if (quote0 == quote1 || quote1 == null) {
1262 // If both strings use the same quote, then keep using it.
1263 value = '$quote0${val0}${val1}$quote0';
1264 } else if (quote0 == '"') {
1265 // If they are different, escape the single-quote to be double-quote
1266 // the choice of single vs double is arbitrary, but choosing one
1267 // ensures that we only do this escaping once on a string portion.
1268 assert(quote1 == "'");
1269 value = '$quote0${val0}${toDoubleQuote(val1)}$quote0';
1270 } else {
1271 assert(quote1 == '"');
1272 value = '$quote1${toDoubleQuote(val0)}${val1}$quote1';
1273 }
1274 return value;
1275 }
1252 1276
1253 resolve() { 1277 resolve() {
1254 // TODO(jimhug): cut-and-paste-and-edit from Field.resolve 1278 // TODO(jimhug): cut-and-paste-and-edit from Field.resolve
1255 isStatic = declaringType.isTop; 1279 isStatic = declaringType.isTop;
1256 isConst = false; 1280 isConst = false;
1257 isFactory = false; 1281 isFactory = false;
1258 isAbstract = !declaringType.isClass; 1282 isAbstract = !declaringType.isClass;
1259 if (definition.modifiers != null) { 1283 if (definition.modifiers != null) {
1260 for (var mod in definition.modifiers) { 1284 for (var mod in definition.modifiers) {
1261 if (mod.kind == TokenKind.STATIC) { 1285 if (mod.kind == TokenKind.STATIC) {
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after
1658 } 1682 }
1659 1683
1660 void forEach(void f(Member member)) { 1684 void forEach(void f(Member member)) {
1661 factories.forEach((_, Map constructors) { 1685 factories.forEach((_, Map constructors) {
1662 constructors.forEach((_, Member member) { 1686 constructors.forEach((_, Member member) {
1663 f(member); 1687 f(member);
1664 }); 1688 });
1665 }); 1689 });
1666 } 1690 }
1667 } 1691 }
OLDNEW
« no previous file with comments | « frog/gen.dart ('k') | frog/utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698