Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4f63e427e7c06cceb944e4c1a70737cfe3cd1483 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*******************************************************************************
 * Copyright (c) 2008, 2012 Institute for Software, HSR Hochschule fuer Technik
 * Rapperswil, University of applied sciences and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Institute for Software - initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;

import java.util.List;

import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTArrayModifier;
import org.eclipse.cdt.core.dom.ast.IASTComment;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter;
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTCompoundStatementExpression;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTLiteralNode;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;

/**
 * Visits all nodes, prints leading comments and handles macro expansions. The
 * source code generation is delegated to severals {@code NodeWriter}s.
 *
 * @see MacroExpansionHandler
 *
 * @author Emanuel Graf IFS
 */
public class ASTWriterVisitor extends ASTVisitor {
	protected final Scribe scribe = new Scribe();
	protected NodeCommentMap commentMap;
	protected ExpressionWriter expWriter;
	protected DeclSpecWriter declSpecWriter;
	protected StatementWriter statementWriter;
	protected DeclaratorWriter declaratorWriter;
	protected DeclarationWriter declarationWriter;
	protected InitializerWriter initializerWriter;
	protected NameWriter nameWriter;
	protected TemplateParameterWriter tempParameterWriter;
	protected MacroExpansionHandler macroHandler;
	private boolean insertLeadingBlankLine;
	private boolean suppressLeadingBlankLine;
	private boolean spaceNeededBeforeName;

	{
		shouldVisitArrayModifiers = true;
		shouldVisitBaseSpecifiers = true;
		shouldVisitDeclarations = true;
		shouldVisitDeclarators = true;
		shouldVisitDeclSpecifiers = true;
		shouldVisitExpressions = true;
		shouldVisitInitializers = true;
		shouldVisitNames = true;
		shouldVisitNamespaces = true;
		shouldVisitParameterDeclarations = true;
		shouldVisitPointerOperators = true;
		shouldVisitStatements = true;
		shouldVisitTemplateParameters = true;
		shouldVisitTranslationUnit = true;
		shouldVisitTypeIds = true;
	}

	/**
	 * Creates a writer with an empty comment map.
	 */
	public ASTWriterVisitor() {
		this(new NodeCommentMap());
	}

	public ASTWriterVisitor(NodeCommentMap commentMap) {
		super();
		init(commentMap);
		this.commentMap = commentMap;
		this.suppressLeadingBlankLine = true;
	}

	private void init(NodeCommentMap commentMap) {
		macroHandler = new MacroExpansionHandler(scribe);
		statementWriter = new StatementWriter(scribe, this, commentMap);
		declaratorWriter = new DeclaratorWriter(scribe, this, commentMap);
		declarationWriter = new DeclarationWriter(scribe, this, commentMap);
		declSpecWriter = new DeclSpecWriter(scribe, this, commentMap);
		expWriter = new ExpressionWriter(scribe, this, macroHandler, commentMap);
		initializerWriter = new InitializerWriter (scribe, this, commentMap);
//		ppStmtWriter = new PreprocessorStatementWriter(scribe, this, commentMap);
		nameWriter = new NameWriter(scribe, this, commentMap);
		tempParameterWriter = new TemplateParameterWriter(scribe, this, commentMap);
	}

	@Override
	public String toString() {
		return scribe.toString();
	}

	@Override
	public int leave(IASTTranslationUnit tu) {
		for (IASTComment comment : commentMap.getFreestandingCommentsForNode(tu)) {
			scribe.print(comment.getComment());
			scribe.newLine();
		}
		return super.leave(tu);
	}

	private void writeLeadingComments(IASTNode node) {
		for (IASTComment comment : getLeadingComments(node)) {
			scribe.print(comment.getComment());
			scribe.newLine();
		}
	}

	private List<IASTComment> getLeadingComments(IASTNode node) {
		List<IASTComment> leadingComments = commentMap.getLeadingCommentsForNode(node);
		IASTNode originalNode = node.getOriginalNode();
		if (originalNode != node)
			leadingComments.addAll(commentMap.getLeadingCommentsForNode(originalNode));
		return leadingComments;
	}

	public void visit(ASTLiteralNode lit) {
		insertBlankLineIfNeeded(lit);
		scribe.print(lit.getRawSignature());
	}

	@Override
	public int visit(IASTName name) {
		if (spaceNeededBeforeName && name.getSimpleID().length != 0) {
			scribe.printSpace();
			spaceNeededBeforeName = false;
		}
		writeLeadingComments(name);
		if (!macroHandler.checkisMacroExpansionNode(name)) {
			nameWriter.writeName(name);
		}
		return ASTVisitor.PROCESS_SKIP;
	}

	@Override
	public int visit(IASTDeclSpecifier declSpec) {
		writeLeadingComments(declSpec);
		declSpecWriter.writeDelcSpec(declSpec);
		return ASTVisitor.PROCESS_SKIP;
	}

	@Override
	public int visit(IASTExpression expression) {
		writeLeadingComments(expression);
		if (!macroHandler.checkisMacroExpansionNode(expression)) {
			if (expression instanceof IGNUASTCompoundStatementExpression) {
				IGNUASTCompoundStatementExpression gnuCompStmtExp =
						(IGNUASTCompoundStatementExpression) expression;
				gnuCompStmtExp.getCompoundStatement().accept(this);
			} else {
				expWriter.writeExpression(expression);
			}
		}
		return ASTVisitor.PROCESS_SKIP;
	}

	@Override
	public int visit(IASTStatement statement) {
		insertBlankLineIfNeeded(statement);
		writeLeadingComments(statement);
		try {
			if (macroHandler.isStatementWithMixedLocation(statement) &&
					!(statement instanceof IASTCompoundStatement)) {
				return statementWriter.writeMixedStatement(statement);
			}
			if (macroHandler.checkisMacroExpansionNode(statement)) {
				return ASTVisitor.PROCESS_SKIP;
			}
			return statementWriter.writeStatement(statement, true);
		} finally {
			setLeadingBlankLineFlags(statement);
		}
	}

	@Override
	public int visit(IASTDeclaration declaration) {
		insertBlankLineIfNeeded(declaration);
		writeLeadingComments(declaration);
		if (!macroHandler.checkisMacroExpansionNode(declaration)) {
			declarationWriter.writeDeclaration(declaration);
			setLeadingBlankLineFlags(declaration);
		}
		return ASTVisitor.PROCESS_SKIP;
	}

	@Override
	public int visit(IASTDeclarator declarator) {
		writeLeadingComments(declarator);
		if (!macroHandler.checkisMacroExpansionNode(declarator)) {
			declaratorWriter.writeDeclarator(declarator);
		}
		return ASTVisitor.PROCESS_SKIP;
	}

	@Override
	public int visit(IASTArrayModifier amod) {
		if (!macroHandler.checkisMacroExpansionNode(amod)) {
			declaratorWriter.writeArrayModifier(amod);
		}
		return ASTVisitor.PROCESS_SKIP;
	}

	@Override
	public int visit(IASTInitializer initializer) {
		writeLeadingComments(initializer);
		if (!macroHandler.checkisMacroExpansionNode(initializer)) {
			initializerWriter.writeInitializer(initializer);
		}
		return ASTVisitor.PROCESS_SKIP;
	}

	@Override
	public int visit(IASTParameterDeclaration parameterDeclaration) {
		writeLeadingComments(parameterDeclaration);
		if (!macroHandler.checkisMacroExpansionNode(parameterDeclaration)) {
			parameterDeclaration.getDeclSpecifier().accept(this);
			IASTDeclarator declarator = getParameterDeclarator(parameterDeclaration);

			spaceNeededBeforeName = true;
			declarator.accept(this);
		}
		return ASTVisitor.PROCESS_SKIP;
	}

	@Override
	public int visit(IASTPointerOperator pointerOperator) {
		writeLeadingComments(pointerOperator);
		if (!macroHandler.checkisMacroExpansionNode(pointerOperator)) {
			declaratorWriter.writePointerOperator(pointerOperator);
		}
		return ASTVisitor.PROCESS_SKIP;
	}

	protected IASTName getParameterName(IASTDeclarator declarator) {
		return declarator.getName();
	}

	protected IASTDeclarator getParameterDeclarator(IASTParameterDeclaration parameterDeclaration) {
		return parameterDeclaration.getDeclarator();
	}

	@Override
	public int visit(ICPPASTNamespaceDefinition namespace) {
		insertBlankLineIfNeeded(namespace);
		writeLeadingComments(namespace);
		if (!macroHandler.checkisMacroExpansionNode(namespace)) {
			declarationWriter.writeDeclaration(namespace);
			setLeadingBlankLineFlags(namespace);
		}
		return ASTVisitor.PROCESS_SKIP;
	}

	@Override
	public int visit(ICPPASTTemplateParameter parameter) {
		writeLeadingComments(parameter);
		if (!macroHandler.checkisMacroExpansionNode(parameter)) {
			tempParameterWriter.writeTemplateParameter(parameter);
		}
		return ASTVisitor.PROCESS_SKIP;
	}

	public void cleanCache() {
		scribe.cleanCache();
		macroHandler.reset();
	}

	private void insertBlankLineIfNeeded(IASTNode node) {
		if (!suppressLeadingBlankLine &&
				(insertLeadingBlankLine || ASTWriter.requiresLeadingBlankLine(node))) {
			scribe.newLine();
		}
		insertLeadingBlankLine = false;
		suppressLeadingBlankLine = false;
	}

	private void setLeadingBlankLineFlags(IASTNode node) {
		insertLeadingBlankLine = ASTWriter.requiresTrailingBlankLine(node);
		suppressLeadingBlankLine = ASTWriter.suppressesTrailingBlankLine(node);
	}

	public boolean isSuppressLeadingBlankLine() {
		return suppressLeadingBlankLine;
	}

	public void setSuppressLeadingBlankLine(boolean value) {
		this.suppressLeadingBlankLine = value;
	}

	public boolean isSpaceNeededBeforeName() {
		return spaceNeededBeforeName;
	}

	public void setSpaceNeededBeforeName(boolean value) {
		this.spaceNeededBeforeName = value;
	}

	public Scribe getScribe() {
		return scribe;
	}

	public void newLine() {
		scribe.newLine();
	}
}

Back to the top