1/*
2 * Copyright (c) 1998, 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 build.tools.jdwpgen;
27
28
29import java.util.*;
30import java.io.*;
31
32abstract class Node {
33
34    String kind;
35    List<Node> components;
36    int lineno;
37    List<String> commentList = new ArrayList<>();
38    Node parent = null;
39    Context context = null;
40
41    static final int maxStructIndent = 5;
42    static int structIndent = 0; // horrible hack
43
44    abstract void document(PrintWriter writer);
45
46    void set(String kind, List<Node> components, int lineno) {
47        this.kind = kind;
48        this.components = components;
49        this.lineno = lineno;
50    }
51
52    void parentAndExtractComments() {
53        for (Iterator<Node> it = components.iterator(); it.hasNext();) {
54            Node node = it.next();
55            if (node instanceof CommentNode) {
56                it.remove();
57                commentList.add(((CommentNode)node).text());
58            } else {
59                node.parent = this;
60                node.parentAndExtractComments();
61            }
62        }
63    }
64
65    void prune() {
66        for (Node node : components) {
67            node.prune();
68        }
69    }
70
71    void constrain(Context ctx) {
72        context = ctx;
73        for (Node node : components) {
74            constrainComponent(ctx, node);
75        }
76    }
77
78    void constrainComponent(Context ctx, Node node) {
79        node.constrain(ctx);
80    }
81
82    void indent(PrintWriter writer, int depth) {
83        for (int i = 0; i < depth; i++) {
84            writer.print("    ");
85        }
86    }
87
88    void documentIndex(PrintWriter writer) {
89    }
90
91    void docRowStart(PrintWriter writer) {
92        writer.println("<tr>");
93        if (structIndent > 0) {
94            writer.println("<td colspan=" + structIndent + ">");
95        }
96    }
97
98    String comment() {
99        StringBuffer comment = new StringBuffer();
100        for (String st : commentList) {
101            comment.append(st);
102        }
103        return comment.toString();
104    }
105
106    void genJavaComment(PrintWriter writer, int depth) {
107        if (commentList.size() > 0) {
108            indent(writer, depth);
109            writer.println("/**");
110            for (String comment : commentList) {
111                indent(writer, depth);
112                writer.println(" * " + comment);
113            }
114            indent(writer, depth);
115            writer.println(" */");
116        }
117    }
118
119    String javaType() {
120        return "-- WRONG ---";
121    }
122
123    void genJava(PrintWriter writer, int depth) {
124        for (Node node : components) {
125            node.genJava(writer, depth);
126        }
127    }
128
129    void genCInclude(PrintWriter writer) {
130        for (Node node : components) {
131            node.genCInclude(writer);
132        }
133    }
134
135    String debugValue(String label) {
136        return label;
137    }
138
139    void genJavaDebugWrite(PrintWriter writer, int depth,
140                           String writeLabel) {
141        genJavaDebugWrite(writer, depth, writeLabel, debugValue(writeLabel));
142    }
143
144    void genJavaDebugWrite(PrintWriter writer, int depth,
145                           String writeLabel, String displayValue) {
146        if (!Main.genDebug) {
147            return;
148        }
149        indent(writer, depth);
150        writer.println(
151          "if ((ps.vm.traceFlags & VirtualMachineImpl.TRACE_SENDS) != 0) {");
152        indent(writer, depth+1);
153        writer.print("ps.vm.printTrace(\"Sending: ");
154        indent(writer, depth);  // this is inside the quotes
155        writer.print(writeLabel + "(" + javaType() + "): \" + ");
156        writer.println(displayValue + ");");
157        indent(writer, depth);
158        writer.println("}");
159    }
160
161    public void genJavaRead(PrintWriter writer, int depth,
162                            String readLabel) {
163        error("Internal - Should not call Node.genJavaRead()");
164    }
165
166    void genJavaDebugRead(PrintWriter writer, int depth,
167                          String readLabel, String displayValue) {
168        if (!Main.genDebug) {
169            return;
170        }
171        indent(writer, depth);
172        writer.println(
173          "if (vm.traceReceives) {");
174        indent(writer, depth+1);
175        writer.print("vm.printReceiveTrace(" + depth + ", \"");
176        writer.print(readLabel + "(" + javaType() + "): \" + ");
177        writer.println(displayValue + ");");
178        indent(writer, depth);
179        writer.println("}");
180    }
181
182    void genJavaPreDef(PrintWriter writer, int depth) {
183        for (Node node : components) {
184            node.genJavaPreDef(writer, depth);
185        }
186    }
187
188    void error(String errmsg) {
189        System.err.println();
190        System.err.println(Main.specSource + ":" + lineno + ": " +
191                           kind + " - " + errmsg);
192        System.err.println();
193        throw new RuntimeException("Error: " + errmsg);
194    }
195}
196