ClassNode.java revision 1668:bafd733be429
1/*
2 * Copyright (c) 2015, 2016, 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 java.util.Collections;
29import java.util.List;
30
31import jdk.nashorn.internal.codegen.types.Type;
32import jdk.nashorn.internal.ir.visitor.NodeVisitor;
33
34/**
35 * IR representation for class definitions.
36 */
37public class ClassNode extends Expression {
38    private static final long serialVersionUID = 1L;
39
40    private final IdentNode ident;
41    private final Expression classHeritage;
42    private final PropertyNode constructor;
43    private final List<PropertyNode> classElements;
44    private final int line;
45
46    /**
47     * Constructor.
48     *
49     * @param line line number
50     * @param token token
51     * @param finish finish
52     * @param ident ident
53     * @param classHeritage class heritage
54     * @param constructor constructor
55     * @param classElements class elements
56     */
57    public ClassNode(final int line, final long token, final int finish, final IdentNode ident, final Expression classHeritage, final PropertyNode constructor,
58                     final List<PropertyNode> classElements) {
59        super(token, finish);
60        this.line = line;
61        this.ident = ident;
62        this.classHeritage = classHeritage;
63        this.constructor = constructor;
64        this.classElements = classElements;
65    }
66
67    /**
68     * Class identifier. Optional.
69     *
70     * @return the class identifier
71     */
72    public IdentNode getIdent() {
73        return ident;
74    }
75
76    /**
77     * The expression of the {@code extends} clause. Optional.
78     *
79     * @return the class heritage
80     */
81    public Expression getClassHeritage() {
82        return classHeritage;
83    }
84
85    /**
86     * Get the constructor method definition.
87     *
88     * @return the constructor
89     */
90    public PropertyNode getConstructor() {
91        return constructor;
92    }
93
94    /**
95     * Get method definitions except the constructor.
96     *
97     * @return the class elements
98     */
99    public List<PropertyNode> getClassElements() {
100        return Collections.unmodifiableList(classElements);
101    }
102
103    /**
104     * Returns the line number.
105     *
106     * @return the line number
107     */
108    public int getLineNumber() {
109        return line;
110    }
111
112    @Override
113    public Type getType() {
114        return Type.OBJECT;
115    }
116
117    @Override
118    public Node accept(final NodeVisitor<? extends LexicalContext> visitor) {
119        if (visitor.enterClassNode(this)) {
120            return visitor.leaveClassNode(this);
121        }
122
123        return this;
124    }
125
126    @Override
127    public void toString(final StringBuilder sb, final boolean printType) {
128        sb.append("class");
129        if (ident != null) {
130            sb.append(' ');
131            ident.toString(sb, printType);
132        }
133        if (classHeritage != null) {
134            sb.append(" extends");
135            classHeritage.toString(sb, printType);
136        }
137        sb.append(" {");
138        if (constructor != null) {
139            constructor.toString(sb, printType);
140        }
141        for (final PropertyNode classElement : classElements) {
142            sb.append(" ");
143            classElement.toString(sb, printType);
144        }
145        sb.append("}");
146    }
147}
148