001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1/GPL 2.0
003: *
004: * The contents of this file are subject to the Mozilla Public License Version
005: * 1.1 (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS" basis,
010: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
011: * for the specific language governing rights and limitations under the
012: * License.
013: *
014: * The Original Code is Rhino code, released
015: * May 6, 1999.
016: *
017: * The Initial Developer of the Original Code is
018: * Netscape Communications Corporation.
019: * Portions created by the Initial Developer are Copyright (C) 1997-1999
020: * the Initial Developer. All Rights Reserved.
021: *
022: * Contributor(s):
023: * Norris Boyd
024: * Bob Jervis
025: * Roger Lawrence
026: *
027: * Alternatively, the contents of this file may be used under the terms of
028: * the GNU General Public License Version 2 or later (the "GPL"), in which
029: * case the provisions of the GPL are applicable instead of those above. If
030: * you wish to allow use of your version of this file only under the terms of
031: * the GPL and not to allow others to use your version of this file under the
032: * MPL, indicate your decision by deleting the provisions above and replacing
033: * them with the notice and other provisions required by the GPL. If you do
034: * not delete the provisions above, a recipient may use your version of this
035: * file under either the MPL or the GPL.
036: *
037: * ***** END LICENSE BLOCK ***** */
038:
039: package org.mozilla.javascript.optimizer;
040:
041: import org.mozilla.javascript.*;
042:
043: final class OptFunctionNode {
044: OptFunctionNode(FunctionNode fnode) {
045: this .fnode = fnode;
046: fnode.setCompilerData(this );
047: }
048:
049: static OptFunctionNode get(ScriptOrFnNode scriptOrFn, int i) {
050: FunctionNode fnode = scriptOrFn.getFunctionNode(i);
051: return (OptFunctionNode) fnode.getCompilerData();
052: }
053:
054: static OptFunctionNode get(ScriptOrFnNode scriptOrFn) {
055: return (OptFunctionNode) scriptOrFn.getCompilerData();
056: }
057:
058: boolean isTargetOfDirectCall() {
059: return directTargetIndex >= 0;
060: }
061:
062: int getDirectTargetIndex() {
063: return directTargetIndex;
064: }
065:
066: void setDirectTargetIndex(int directTargetIndex) {
067: // One time action
068: if (directTargetIndex < 0 || this .directTargetIndex >= 0)
069: Kit.codeBug();
070: this .directTargetIndex = directTargetIndex;
071: }
072:
073: void setParameterNumberContext(boolean b) {
074: itsParameterNumberContext = b;
075: }
076:
077: boolean getParameterNumberContext() {
078: return itsParameterNumberContext;
079: }
080:
081: int getVarCount() {
082: return fnode.getParamAndVarCount();
083: }
084:
085: boolean isParameter(int varIndex) {
086: return varIndex < fnode.getParamCount();
087: }
088:
089: boolean isNumberVar(int varIndex) {
090: varIndex -= fnode.getParamCount();
091: if (varIndex >= 0 && numberVarFlags != null) {
092: return numberVarFlags[varIndex];
093: }
094: return false;
095: }
096:
097: void setIsNumberVar(int varIndex) {
098: varIndex -= fnode.getParamCount();
099: // Can only be used with non-parameters
100: if (varIndex < 0)
101: Kit.codeBug();
102: if (numberVarFlags == null) {
103: int size = fnode.getParamAndVarCount()
104: - fnode.getParamCount();
105: numberVarFlags = new boolean[size];
106: }
107: numberVarFlags[varIndex] = true;
108: }
109:
110: int getVarIndex(Node n) {
111: int index = n.getIntProp(Node.VARIABLE_PROP, -1);
112: if (index == -1) {
113: Node node;
114: int type = n.getType();
115: if (type == Token.GETVAR) {
116: node = n;
117: } else if (type == Token.SETVAR
118: || type == Token.SETCONSTVAR) {
119: node = n.getFirstChild();
120: } else {
121: throw Kit.codeBug();
122: }
123: index = fnode.getIndexForNameNode(node);
124: if (index < 0)
125: throw Kit.codeBug();
126: n.putIntProp(Node.VARIABLE_PROP, index);
127: }
128: return index;
129: }
130:
131: FunctionNode fnode;
132: private boolean[] numberVarFlags;
133: private int directTargetIndex = -1;
134: private boolean itsParameterNumberContext;
135: boolean itsContainsCalls0;
136: boolean itsContainsCalls1;
137: }
|