Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cce9745ede883407f9b84c72f721de73d74f9424 (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
/*******************************************************************************
 * Copyright (c) 2008, 2009 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)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;

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.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
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>NodeWriters</code>.
 * 
 * @see NodeWriter
 * @see MacroExpansionHandler
 * 
 * @author Emanuel Graf IFS
 * 
 */
public class ASTWriterVisitor extends CPPASTVisitor {
	
	protected 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;
	{
		shouldVisitExpressions = true;
		
		shouldVisitStatements = true;
		
		shouldVisitNames = true;
		
		shouldVisitDeclarations = true;
		
		shouldVisitDeclSpecifiers = true;
		
		shouldVisitDeclarators = true;
		
		shouldVisitArrayModifiers= true;
		
		shouldVisitInitializers = true;
		
		shouldVisitBaseSpecifiers = true;

		shouldVisitNamespaces = true;

		shouldVisitTemplateParameters = true;
		
		shouldVisitParameterDeclarations = true;
		
		shouldVisitTranslationUnit = true;
	}
	
	
	
	public ASTWriterVisitor(NodeCommentMap commentMap) {
		this("", commentMap); //$NON-NLS-1$
	}



	public ASTWriterVisitor(String givenIndentation, NodeCommentMap commentMap) {
		super();
		scribe.setGivenIndentation(givenIndentation);
		init(commentMap);
		this.commentMap = commentMap;

	}

	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 : commentMap.getLeadingCommentsForNode(node)) {
			scribe.print(comment.getComment());
			scribe.newLine();
		}		
	}
	
	public void visit(ASTLiteralNode lit) {
		scribe.print(lit.getRawSignature());
	}
	
	@Override
	public int visit(IASTName name) {
		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) {
		writeLeadingComments(statement);
		if(macroHandler.isStatementWithMixedLocation(statement) && !(statement instanceof IASTCompoundStatement)){
			return statementWriter.writeMixedStatement(statement);
		}
		if(macroHandler.checkisMacroExpansionNode(statement)) {
			return ASTVisitor.PROCESS_SKIP;
		}
		return statementWriter.writeStatement(statement, true);
	}


	@Override
	public int visit(IASTDeclaration declaration) {
		writeLeadingComments(declaration);
		if(!macroHandler.checkisMacroExpansionNode(declaration)) {
			declarationWriter.writeDeclaration(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);
			
			if(getParameterName(declarator).toString().length() != 0){
				scribe.printSpaces(1);
			}
			declarator.accept(this);
		}
		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) {
		writeLeadingComments(namespace);
		if(!macroHandler.checkisMacroExpansionNode(namespace)) {
			declarationWriter.writeDeclaration(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();
	}

}

Back to the top