BaseNode.java revision 1152:5f6a840fc19d
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;
32
33/**
34 * IR base for accessing/indexing nodes.
35 *
36 * @see AccessNode
37 * @see IndexNode
38 */
39@Immutable
40public abstract class BaseNode extends Expression implements FunctionCall, Optimistic {
41    private static final long serialVersionUID = 1L;
42
43    /** Base Node. */
44    protected final Expression base;
45
46    private final boolean isFunction;
47
48    /** Callsite type for this node, if overridden optimistically or conservatively depending on coercion */
49    protected final Type type;
50
51    /** Program point id */
52    protected final int programPoint;
53
54    /**
55     * Constructor
56     *
57     * @param token  token
58     * @param finish finish
59     * @param base   base node
60     * @param isFunction is this a function
61     */
62    public BaseNode(final long token, final int finish, final Expression base, final boolean isFunction) {
63        super(token, base.getStart(), finish);
64        this.base           = base;
65        this.isFunction     = isFunction;
66        this.type = null;
67        this.programPoint   = INVALID_PROGRAM_POINT;
68    }
69
70    /**
71     * Copy constructor for immutable nodes
72     * @param baseNode node to inherit from
73     * @param base base
74     * @param isFunction is this a function
75     * @param callSiteType  the callsite type for this base node, either optimistic or conservative
76     * @param programPoint  program point id
77     */
78    protected BaseNode(final BaseNode baseNode, final Expression base, final boolean isFunction, final Type callSiteType, final int programPoint) {
79        super(baseNode);
80        this.base           = base;
81        this.isFunction     = isFunction;
82        this.type = callSiteType;
83        this.programPoint   = programPoint;
84    }
85
86    /**
87     * Get the base node for this access
88     * @return the base node
89     */
90    public Expression getBase() {
91        return base;
92    }
93
94    @Override
95    public boolean isFunction() {
96        return isFunction;
97    }
98
99    @Override
100    public Type getType() {
101        return type == null ? getMostPessimisticType() : type;
102    }
103
104    @Override
105    public int getProgramPoint() {
106        return programPoint;
107    }
108
109    @Override
110    public Type getMostOptimisticType() {
111        return Type.INT;
112    }
113
114    @Override
115    public Type getMostPessimisticType() {
116        return Type.OBJECT;
117    }
118
119    @Override
120    public boolean canBeOptimistic() {
121        return true;
122    }
123
124    /**
125     * Mark this node as being the callee operand of a {@link CallNode}.
126     * @return a base node identical to this one in all aspects except with its function flag set.
127     */
128    public abstract BaseNode setIsFunction();
129
130}
131