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

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

Issue 12334070: Support runtime check of function types. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Minor fix Created 7 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 | 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 ssa; 5 part of ssa;
6 6
7 class SsaCodeGeneratorTask extends CompilerTask { 7 class SsaCodeGeneratorTask extends CompilerTask {
8 8
9 final JavaScriptBackend backend; 9 final JavaScriptBackend backend;
10 10
(...skipping 2196 matching lines...) Expand 10 before | Expand all | Expand 10 after
2207 world.registerStaticUse(helper); 2207 world.registerStaticUse(helper);
2208 String helperName = backend.namer.isolateAccess(helper); 2208 String helperName = backend.namer.isolateAccess(helper);
2209 push(new js.Call(new js.VariableUse(helperName), arguments)); 2209 push(new js.Call(new js.VariableUse(helperName), arguments));
2210 if (negative) push(new js.Prefix('!', pop())); 2210 if (negative) push(new js.Prefix('!', pop()));
2211 } 2211 }
2212 2212
2213 void checkType(HInstruction input, DartType type, {bool negative: false}) { 2213 void checkType(HInstruction input, DartType type, {bool negative: false}) {
2214 assert(invariant(input, !type.isMalformed, 2214 assert(invariant(input, !type.isMalformed,
2215 message: 'Attempt to check malformed type $type')); 2215 message: 'Attempt to check malformed type $type'));
2216 Element element = type.element; 2216 Element element = type.element;
2217
2218 if (element == backend.jsArrayClass) { 2217 if (element == backend.jsArrayClass) {
2219 checkArray(input, negative ? '!==': '==='); 2218 checkArray(input, negative ? '!==': '===');
2220 return; 2219 return;
2221 } else if (element == backend.jsMutableArrayClass) { 2220 } else if (element == backend.jsMutableArrayClass) {
2222 if (negative) { 2221 if (negative) {
2223 checkImmutableArray(input); 2222 checkImmutableArray(input);
2224 } else { 2223 } else {
2225 checkMutableArray(input); 2224 checkMutableArray(input);
2226 } 2225 }
2227 return; 2226 return;
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
2511 pushStatement(new js.Throw(call)); 2510 pushStatement(new js.Throw(call));
2512 } 2511 }
2513 currentContainer = oldContainer; 2512 currentContainer = oldContainer;
2514 body = unwrapStatement(body); 2513 body = unwrapStatement(body);
2515 pushStatement(new js.If.noElse(test, body), node); 2514 pushStatement(new js.If.noElse(test, body), node);
2516 return; 2515 return;
2517 } 2516 }
2518 2517
2519 assert(node.isCheckedModeCheck || node.isCastTypeCheck); 2518 assert(node.isCheckedModeCheck || node.isCastTypeCheck);
2520 DartType type = node.typeExpression; 2519 DartType type = node.typeExpression;
2520 assert(type.kind != TypeKind.TYPEDEF);
2521 if (type.kind == TypeKind.FUNCTION) { 2521 if (type.kind == TypeKind.FUNCTION) {
2522 // TODO(5022): We currently generate $isFunction checks for 2522 // TODO(5022): We currently generate $isFunction checks for
2523 // function types. 2523 // function types.
2524 world.registerIsCheck( 2524 world.registerIsCheck(
2525 compiler.functionClass.computeType(compiler), work.resolutionTree); 2525 compiler.functionClass.computeType(compiler), work.resolutionTree);
2526 } 2526 }
2527 world.registerIsCheck(type, work.resolutionTree); 2527 world.registerIsCheck(type, work.resolutionTree);
2528 2528
2529 CheckedModeHelper helper;
2529 FunctionElement helperElement; 2530 FunctionElement helperElement;
2530 if (node.isBooleanConversionCheck) { 2531 if (node.isBooleanConversionCheck) {
2531 helperElement = 2532 helper =
2532 compiler.findHelper(const SourceString('boolConversionCheck')); 2533 const CheckedModeHelper(const SourceString('boolConversionCheck'));
2533 } else { 2534 } else {
2534 helperElement = backend.getCheckedModeHelper(type, 2535 helper =
2535 typeCast: node.isCastTypeCheck); 2536 backend.getCheckedModeHelper(type, typeCast: node.isCastTypeCheck);
2536 } 2537 }
2537 world.registerStaticUse(helperElement); 2538
2538 List<js.Expression> arguments = <js.Expression>[]; 2539 push(helper.generateCall(this, node));
2539 use(node.checkedInput);
2540 arguments.add(pop());
2541 int parameterCount =
2542 helperElement.computeSignature(compiler).parameterCount;
2543 // TODO(johnniwinther): Refactor this to avoid using the parameter count
2544 // to determine how the helper should be called.
2545 if (node.typeExpression.kind == TypeKind.TYPE_VARIABLE) {
2546 assert(parameterCount == 2);
2547 use(node.typeRepresentation);
2548 arguments.add(pop());
2549 } else if (parameterCount == 2) {
2550 // 2 arguments implies that the method is either [propertyTypeCheck],
2551 // [propertyTypeCast] or [assertObjectIsSubtype].
2552 assert(!type.isMalformed);
2553 String additionalArgument = backend.namer.operatorIs(type.element);
2554 arguments.add(js.string(additionalArgument));
2555 } else if (parameterCount == 3) {
2556 // 3 arguments implies that the method is [malformedTypeCheck].
2557 assert(type.isMalformed);
2558 String reasons = Types.fetchReasonsFromMalformedType(type);
2559 arguments.add(js.string('$type'));
2560 // TODO(johnniwinther): Handle escaping correctly.
2561 arguments.add(js.string(reasons));
2562 } else if (parameterCount == 4) {
2563 Element element = type.element;
2564 String isField = backend.namer.operatorIs(element);
2565 arguments.add(js.string(isField));
2566 use(node.typeRepresentation);
2567 arguments.add(pop());
2568 String asField = backend.namer.substitutionName(element);
2569 arguments.add(js.string(asField));
2570 } else {
2571 assert(!type.isMalformed);
2572 // No additional arguments needed.
2573 }
2574 String helperName = backend.namer.isolateAccess(helperElement);
2575 push(new js.Call(new js.VariableUse(helperName), arguments));
2576 } 2540 }
2577 } 2541 }
2578 2542
2579 class SsaOptimizedCodeGenerator extends SsaCodeGenerator { 2543 class SsaOptimizedCodeGenerator extends SsaCodeGenerator {
2580 SsaOptimizedCodeGenerator(backend, work) : super(backend, work); 2544 SsaOptimizedCodeGenerator(backend, work) : super(backend, work);
2581 2545
2582 HBasicBlock beginGraph(HGraph graph) { 2546 HBasicBlock beginGraph(HGraph graph) {
2583 return graph.entry; 2547 return graph.entry;
2584 } 2548 }
2585 2549
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after
3015 if (leftType.canBeNull() && rightType.canBeNull()) { 2979 if (leftType.canBeNull() && rightType.canBeNull()) {
3016 if (left.isConstantNull() || right.isConstantNull() || 2980 if (left.isConstantNull() || right.isConstantNull() ||
3017 (leftType.isPrimitive(compiler) && leftType == rightType)) { 2981 (leftType.isPrimitive(compiler) && leftType == rightType)) {
3018 return '=='; 2982 return '==';
3019 } 2983 }
3020 return null; 2984 return null;
3021 } else { 2985 } else {
3022 return '==='; 2986 return '===';
3023 } 2987 }
3024 } 2988 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698