OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library ir_type_inferrer; |
| 6 |
| 7 import '../ir/ir_nodes.dart'; |
| 8 import 'inferrer_visitor.dart' show |
| 9 TypeSystem; |
| 10 import 'simple_types_inferrer.dart' show |
| 11 InferrerEngine; |
| 12 import '../elements/elements.dart' show |
| 13 Element, FunctionElement, FunctionSignature; |
| 14 import '../dart2jslib.dart' show |
| 15 Compiler, Constant, ConstantSystem; |
| 16 |
| 17 class IrTypeInferrerVisitor<T> extends IrNodesVisitor<T> { |
| 18 final Compiler compiler; |
| 19 final Element analyzedElement; |
| 20 final TypeSystem<T> types; |
| 21 final InferrerEngine<T, TypeSystem<T>> inferrer; |
| 22 |
| 23 IrTypeInferrerVisitor(this.compiler, this.analyzedElement, |
| 24 InferrerEngine<T, TypeSystem<T>> inferrer) |
| 25 : inferrer = inferrer, |
| 26 types = inferrer.types; |
| 27 |
| 28 final Map<IrNode, T> analyzed = new Map<IrNode, T>(); |
| 29 |
| 30 T recordAnalyzed(IrNode node, T type) => analyzed[node] = type; |
| 31 |
| 32 T run() { |
| 33 if (analyzedElement.isField()) { |
| 34 // TODO(lry): handle fields. |
| 35 throw "Type infer from IR for field $analyzedElement"; |
| 36 } |
| 37 FunctionElement function = analyzedElement; |
| 38 FunctionSignature signature = function.computeSignature(compiler); |
| 39 IrFunction node = compiler.irBuilder.getIr(function); |
| 40 // TODO(lry): handle parameters. |
| 41 |
| 42 if (function.isNative()) { |
| 43 // TODO(lry): handle native. |
| 44 throw "Type infer from IR for native $analyzedElement"; |
| 45 } |
| 46 |
| 47 if (analyzedElement.isGenerativeConstructor()) { |
| 48 // TODO(lry): handle constructors. |
| 49 throw "Type infer from IR for constructor $analyzedElement"; |
| 50 } |
| 51 |
| 52 if (analyzedElement.isSynthesized) { |
| 53 // TODO(lry): handle synthethics. |
| 54 throw "Type infer from IR for synthetic $analyzedElement"; |
| 55 } |
| 56 |
| 57 visitAll(node.statements); |
| 58 } |
| 59 |
| 60 T typeOfConstant(Constant constant) { |
| 61 if (constant.isBool()) return types.boolType; |
| 62 if (constant.isNum()) { |
| 63 ConstantSystem constantSystem = compiler.backend.constantSystem; |
| 64 // The JavaScript backend may turn this literal into a double at runtime. |
| 65 if (constantSystem.isDouble(constant)) return types.doubleType; |
| 66 return types.intType; |
| 67 } |
| 68 if (constant.isString()) return types.stringType; |
| 69 if (constant.isNull()) return types.nullType; |
| 70 compiler.internalError("Unexpected constant: $constant"); |
| 71 } |
| 72 |
| 73 T visitIrConstant(IrConstant node) { |
| 74 return recordAnalyzed(node, typeOfConstant(node.value)); |
| 75 } |
| 76 |
| 77 T visitIrReturn(IrReturn node) { |
| 78 inferrer.addReturnTypeFor(analyzedElement, null, analyzed[node.value]); |
| 79 } |
| 80 |
| 81 T visitNode(IrNode node) { |
| 82 compiler.internalError('Unexpected IrNode $node'); |
| 83 } |
| 84 } |
OLD | NEW |