BaseNode.java revision 1263:044a0fe3944f
1/*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package jdk.nashorn.internal.ir;
27
28import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.INVALID_PROGRAM_POINT;
29
30import jdk.nashorn.internal.codegen.types.Type;
31import jdk.nashorn.internal.ir.annotations.Immutable;
32import jdk.nashorn.internal.parser.TokenType;
33
34/**
35 * IR base for accessing/indexing nodes.
36 *
37 * @see AccessNode
38 * @see IndexNode
39 */
40@Immutable
41public abstract class BaseNode extends Expression implements FunctionCall, Optimistic {
42    private static final long serialVersionUID = 1L;
43
44    /** Base Node. */
45    protected final Expression base;
46
47    private final boolean isFunction;
48
49    /** Callsite type for this node, if overridden optimistically or conservatively depending on coercion */
50    protected final Type type;
51
52    /** Program point id */
53    protected final int programPoint;
54
55    /**
56     * Constructor
57     *
58     * @param token  token
59     * @param finish finish
60     * @param base   base node
61     * @param isFunction is this a function
62     */
63    public BaseNode(final long token, final int finish, final Expression base, final boolean isFunction) {
64        super(token, base.getStart(), finish);
65        this.base           = base;
66        this.isFunction     = isFunction;
67        this.type = null;
68        this.programPoint   = INVALID_PROGRAM_POINT;
69    }
70
71    /**
72     * Copy constructor for immutable nodes
73     * @param baseNode node to inherit from
74     * @param base base
75     * @param isFunction is this a function
76     * @param callSiteType  the callsite type for this base node, either optimistic or conservative
77     * @param programPoint  program point id
78     */
79    protected BaseNode(final BaseNode baseNode, final Expression base, final boolean isFunction, final Type callSiteType, final int programPoint) {
80        super(baseNode);
81        this.base           = base;
82        this.isFunction     = isFunction;
83        this.type = callSiteType;
84        this.programPoint   = programPoint;
85    }
86
87    /**
88     * Get the base node for this access
89     * @return the base node
90     */
91    public Expression getBase() {
92        return base;
93    }
94
95    @Override
96    public boolean isFunction() {
97        return isFunction;
98    }
99
100    @Override
101    public Type getType() {
102        return type == null ? getMostPessimisticType() : type;
103    }
104
105    @Override
106    public int getProgramPoint() {
107        return programPoint;
108    }
109
110    @Override
111    public Type getMostOptimisticType() {
112        return Type.INT;
113    }
114
115    @Override
116    public Type getMostPessimisticType() {
117        return Type.OBJECT;
118    }
119
120    @Override
121    public boolean canBeOptimistic() {
122        return true;
123    }
124
125    /**
126     * Return true if this node represents an index operation normally represented as {@link IndexNode}.
127     * @return true if an index access.
128     */
129    public boolean isIndex() {
130        return isTokenType(TokenType.LBRACKET);
131    }
132
133    /**
134     * Mark this node as being the callee operand of a {@link CallNode}.
135     * @return a base node identical to this one in all aspects except with its function flag set.
136     */
137    public abstract BaseNode setIsFunction();
138
139}
140