| 1 | /* |
| 2 | * Copyright 2010 The Closure Compiler Authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.google.javascript.jscomp; |
| 18 | |
| 19 | import com.google.common.base.Preconditions; |
| 20 | import com.google.javascript.rhino.IR; |
| 21 | import com.google.javascript.rhino.Node; |
| 22 | import com.google.javascript.rhino.Token; |
| 23 | import com.google.javascript.rhino.jstype.JSType; |
| 24 | |
| 25 | /** |
| 26 | * Performs type-aware peephole optimizations. |
| 27 | * |
| 28 | * These peephole optimizations are in their own class because |
| 29 | * type information may not always be available (such as during pre-processing) |
| 30 | * or may not be turned on. |
| 31 | * |
| 32 | * Currently only Token.TYPEOF is folded -- in the future it may be possible to |
| 33 | * fold Token.INSTANCEOF as well. Another possibility is folding when |
| 34 | * non-nullable objects are used in Boolean logic, such as: |
| 35 | * "if (x) {" or "(!x) ? a : b" or "x && foo()" |
| 36 | * |
| 37 | * TODO(dcc): Support folding Token.INSTANCEOF and non-nullable objects |
| 38 | * in Boolean logic. |
| 39 | * |
| 40 | * @author [email protected] (Devin Coughlin) |
| 41 | */ |
| 42 | class PeepholeFoldWithTypes extends AbstractPeepholeOptimization { |
| 43 | |
| 44 | @Override |
| 45 | Node optimizeSubtree(Node subtree) { |
| 46 | switch (subtree.getType()) { |
| 47 | case Token.TYPEOF: |
| 48 | return tryFoldTypeof(subtree); |
| 49 | default: |
| 50 | return subtree; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Folds "typeof expression" based on the JSType of "expression" if the |
| 56 | * expression has no side effects. |
| 57 | * |
| 58 | * <p>E.g., |
| 59 | * <pre> |
| 60 | * var x = 6; |
| 61 | * if (typeof(x) == "number") { |
| 62 | * } |
| 63 | * </pre> |
| 64 | * folds to |
| 65 | * <pre> |
| 66 | * var x = 6; |
| 67 | * if ("number" == "number") { |
| 68 | * } |
| 69 | * </pre> |
| 70 | * |
| 71 | * <p>This method doesn't fold literal values -- we leave that to |
| 72 | * PeepholeFoldConstants. |
| 73 | */ |
| 74 | private Node tryFoldTypeof(Node typeofNode) { |
| 75 | Preconditions.checkArgument(typeofNode.isTypeOf()); |
| 76 | Preconditions.checkArgument(typeofNode.getFirstChild() != null); |
| 77 | |
| 78 | Node argumentNode = typeofNode.getFirstChild(); |
| 79 | |
| 80 | // We'll let PeepholeFoldConstants handle folding literals |
| 81 | // and we can't remove arguments with possible side effects. |
| 82 | if (!NodeUtil.isLiteralValue(argumentNode, true) && |
| 83 | !mayHaveSideEffects(argumentNode)) { |
| 84 | JSType argumentType = argumentNode.getJSType(); |
| 85 | |
| 86 | String typeName = null; |
| 87 | |
| 88 | if (argumentType != null) { |
| 89 | // typeof null is "object" in JavaScript |
| 90 | if (argumentType.isObject() || argumentType.isNullType()) { |
| 91 | typeName = "object"; |
| 92 | } else if (argumentType.isStringValueType()) { |
| 93 | typeName = "string"; |
| 94 | } else if (argumentType.isNumberValueType()) { |
| 95 | typeName = "number"; |
| 96 | } else if (argumentType.isBooleanValueType()) { |
| 97 | typeName = "boolean"; |
| 98 | } else if (argumentType.isVoidType()) { |
| 99 | typeName = "undefined"; |
| 100 | } else if (argumentType.isUnionType()) { |
| 101 | // TODO(dcc): We don't handle union types, for now, |
| 102 | // but could support, say, unions of different object types |
| 103 | // in the future. |
| 104 | typeName = null; |
| 105 | } |
| 106 | |
| 107 | if (typeName != null) { |
| 108 | Node newNode = IR.string(typeName); |
| 109 | typeofNode.getParent().replaceChild(typeofNode, newNode); |
| 110 | reportCodeChange(); |
| 111 | |
| 112 | return newNode; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | return typeofNode; |
| 117 | } |
| 118 | } |