Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9bae7f91158d07b0affcf6142fd40b074dfc964f (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*******************************************************************************
 * Copyright (c) 2008, 2010 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
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;

import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTBreakStatement;
import org.eclipse.cdt.core.dom.ast.IASTCaseStatement;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTContinueStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement;
import org.eclipse.cdt.core.dom.ast.IASTDefaultStatement;
import org.eclipse.cdt.core.dom.ast.IASTDoStatement;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
import org.eclipse.cdt.core.dom.ast.IASTForStatement;
import org.eclipse.cdt.core.dom.ast.IASTGotoStatement;
import org.eclipse.cdt.core.dom.ast.IASTIfStatement;
import org.eclipse.cdt.core.dom.ast.IASTLabelStatement;
import org.eclipse.cdt.core.dom.ast.IASTNullStatement;
import org.eclipse.cdt.core.dom.ast.IASTProblemStatement;
import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement;
import org.eclipse.cdt.core.dom.ast.IASTWhileStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCatchHandler;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTForStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTIfStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTRangeBasedForStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSwitchStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTryBlockStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTWhileStatement;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousStatement;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
import org.eclipse.cdt.internal.core.dom.rewrite.util.FileContentHelper;
import org.eclipse.cdt.internal.core.dom.rewrite.util.FileHelper;
import org.eclipse.core.resources.IFile;

/**
 * 
 * Generates source code of statement nodes. The actual string operations are delegated
 * to the <code>Scribe</code> class.
 * 
 * @see Scribe
 * @see IASTStatement
 * @author Emanuel Graf IFS
 * 
 */
public class StatementWriter extends NodeWriter{

	private static final String DEFAULT = "default:"; //$NON-NLS-1$
	private static final String CASE = "case "; //$NON-NLS-1$
	private static final String WHILE = "while("; //$NON-NLS-1$
	private static final String TRY = "try "; //$NON-NLS-1$
	private static final String CATCH = "catch("; //$NON-NLS-1$
	private static final String RETURN = "return"; //$NON-NLS-1$
	private static final String GOTO = "goto "; //$NON-NLS-1$
	private static final String CONTINUE = "continue"; //$NON-NLS-1$
	private static final String BREAK = "break"; //$NON-NLS-1$
	private static final String ELSE = "else"; //$NON-NLS-1$
	private static final String IF = "if("; //$NON-NLS-1$
	private static final String FOR = "for("; //$NON-NLS-1$
	private static final String DO_WHILE = " while("; //$NON-NLS-1$
	private static final String DO = "do"; //$NON-NLS-1$
	private static final String SWITCH_BRACKET = "switch ("; //$NON-NLS-1$
	private boolean compoundNoNewLine = false;
	private boolean switchIsNew;
	private boolean decrementIndentationLevelOneMore = false;
	private final DeclarationWriter declWriter;

	public StatementWriter(Scribe scribe, ASTVisitor visitor, NodeCommentMap commentMap) {
		super(scribe, visitor, commentMap);
		declWriter = new DeclarationWriter(scribe, visitor, commentMap);
	}
	
	/**
	 * 
	 * @param statement
	 * @param newLine if true print a newline if statment usually have one.
	 * @return {@link ASTVisitor#PROCESS_SKIP}
	 */
	protected int writeStatement(IASTStatement statement, boolean newLine) {
		if (statement instanceof IASTAmbiguousStatement) {
			//TODO HSR Leo test
			statement.accept(visitor);
			newLine = false;
		} else if (statement instanceof IASTExpressionStatement) {
			writeExpressionStatement((IASTExpressionStatement) statement);
			//usually newLine
		} else if (statement instanceof IASTDeclarationStatement) {
			writeDeclarationStatement((IASTDeclarationStatement) statement);
			newLine = false;
		} else if (statement instanceof IASTNullStatement) {
			writeNullStatement((IASTNullStatement)statement);
//			usually newLine
		} else if (statement instanceof IASTReturnStatement) {
			writeReturnStatement((IASTReturnStatement)statement);
//			usually newLine
		} else if (statement instanceof IASTGotoStatement) {
			writeGotoStatement((IASTGotoStatement) statement);
//			usually newLine
		} else if (statement instanceof IASTLabelStatement) {
			writeLabelStatement((IASTLabelStatement) statement);
			newLine = false;
		} else if (statement instanceof IASTCaseStatement) {
			writeCaseStatement((IASTCaseStatement) statement);
//			usually newLine			
		}else if (statement instanceof IASTDefaultStatement) {
			writeDefaultStatement((IASTDefaultStatement)statement);
		} else if (statement instanceof IASTContinueStatement){
			writeContinueStatement((IASTContinueStatement)statement);
//			usually newLine
		} else if (statement instanceof IASTCompoundStatement) {
			writeCompoundStatement((IASTCompoundStatement) statement);
			if(compoundNoNewLine){
				newLine = false;
				compoundNoNewLine = false;
			}
		} else if (statement instanceof IASTBreakStatement) {
			writeBreakStatement((IASTBreakStatement) statement);
//			usually newLine
		} else if (statement instanceof IASTSwitchStatement) {
			writeSwitchStatement((IASTSwitchStatement) statement);
			newLine = false;
		} else if (statement instanceof IASTIfStatement) {
			writeIfStatement((IASTIfStatement) statement);			
			newLine = false;
		} else if (statement instanceof IASTWhileStatement) {
			writeWhileStatement( (IASTWhileStatement) statement );
			newLine = false;
		} else if (statement instanceof IASTForStatement) {
			writeForStatement((IASTForStatement) statement);
			newLine = false;
		} else if (statement instanceof ICPPASTRangeBasedForStatement) {
			writeForStatement((ICPPASTRangeBasedForStatement) statement);
			newLine = false;
		} else if (statement instanceof IASTDoStatement) {
			writeDoStatement((IASTDoStatement) statement);
			newLine = true;
		} else if (statement instanceof ICPPASTTryBlockStatement) {
			writeTryBlockStatement((ICPPASTTryBlockStatement) statement);
			newLine = false;
		} else if (statement instanceof ICPPASTCatchHandler) {
			writeCatchHandler((ICPPASTCatchHandler) statement);
			newLine = false;
		} else if (statement instanceof IASTProblemStatement) {
			throw new ProblemRuntimeException((IASTProblemStatement)statement);
		} 
		
		if(hasTrailingComments(statement)) {
			writeTrailingComments(statement, newLine);			
		}
		else{
			if(newLine){
				scribe.newLine();
			}
		}
		
		return ASTVisitor.PROCESS_SKIP;
	}

	private void writeDoStatement(IASTDoStatement doStatement) {
		nextCompoundNoNewLine();
		
		scribe.print(DO);
		writeBodyStatement(doStatement.getBody(), true);
		scribe.print(DO_WHILE);
		doStatement.getCondition().accept(visitor);
		scribe.print(')');
		scribe.printSemicolon();
	}

	private void writeForStatement(IASTForStatement forStatment) {
		scribe.noNewLines();
		scribe.print(FOR);
		writeStatement(forStatment.getInitializerStatement(),false);
		if (forStatment instanceof ICPPASTForStatement) {
			ICPPASTForStatement cppForStatment = (ICPPASTForStatement) forStatment;
			IASTDeclaration cppConditionDeclaration = cppForStatment.getConditionDeclaration();
			if(cppConditionDeclaration == null) {
				visitNodeIfNotNull(cppForStatment.getConditionExpression());
				scribe.printSemicolon();
			} else {
				cppConditionDeclaration.accept(visitor);
			}
			
		} else {
			if(forStatment.getConditionExpression() != null) {
				forStatment.getConditionExpression().accept(visitor);
				scribe.printSemicolon();
			}
		}
		
		visitNodeIfNotNull(forStatment.getIterationExpression());
		scribe.print(')');
		scribe.newLines();
		nextCompoundNoNewLine();
		writeBodyStatement(forStatment.getBody(), false);
	}

	private void writeForStatement(ICPPASTRangeBasedForStatement forStatment) {
		scribe.noNewLines();
		scribe.print(FOR);
		writeDeclarationWithoutSemicolon(forStatment.getDeclaration());
		scribe.print(COLON_SPACE);
		visitNodeIfNotNull(forStatment.getInitializerClause());
		scribe.print(')');
		scribe.newLines();
		nextCompoundNoNewLine();
		writeBodyStatement(forStatment.getBody(), false);
	}

	private void writeIfStatement(IASTIfStatement ifStatement) {
		scribe.print(IF);
		scribe.noNewLines();
		if (ifStatement instanceof ICPPASTIfStatement) {
			ICPPASTIfStatement cppIfStatment = (ICPPASTIfStatement) ifStatement;

			if(cppIfStatment.getConditionDeclaration() == null) {
				cppIfStatment.getConditionExpression().accept(visitor);
			} else {
				writeDeclarationWithoutSemicolon(cppIfStatment.getConditionDeclaration());
			}
		} else {
			ifStatement.getConditionExpression().accept(visitor);
		}
		
		scribe.print(')');
		scribe.newLines();
		nextCompoundNoNewLine();
		IASTStatement elseClause = ifStatement.getElseClause();
		writeBodyStatement(ifStatement.getThenClause(), elseClause != null ? true : false);
		
		if(elseClause != null){
			scribe.print(ELSE);
			nextCompoundNoNewLine();
			writeBodyStatement(elseClause, false);
		}
	}

	protected void writeDeclarationWithoutSemicolon(
			IASTDeclaration declaration) {
		declWriter.writeDeclaration(declaration, false);
	}

	private void writeBreakStatement(IASTBreakStatement statement) {
		scribe.print(BREAK);
		scribe.printSemicolon();
	}

	private void writeContinueStatement(IASTContinueStatement statement) {
		scribe.print(CONTINUE);
		scribe.printSemicolon();
	}

	private void writeLabelStatement(IASTLabelStatement labelStatement) {
		labelStatement.getName().accept(visitor);
		scribe.print(':');
		scribe.newLine();			
		labelStatement.getNestedStatement().accept(visitor);
	}

	private void writeGotoStatement(IASTGotoStatement gotoStatement) {
		scribe.print(GOTO);
		gotoStatement.getName().accept(visitor);
		scribe.printSemicolon();
	}

	private void writeReturnStatement(IASTReturnStatement returnStatement) {
		scribe.noNewLines();
		scribe.print(RETURN);
		IASTExpression returnValue = returnStatement.getReturnValue();
		if(returnValue != null){
			scribe.printSpaces(1);
			returnValue.accept(visitor);
		}
		scribe.newLines();
		scribe.printSemicolon();
	}

	private void writeNullStatement(IASTNullStatement nullStmt) {
		scribe.printSemicolon();
	}
	
	private void writeDeclarationStatement(IASTDeclarationStatement decStmt) {
		decStmt.getDeclaration().accept(visitor);
	}

	private void writeExpressionStatement(IASTExpressionStatement expStmt) {
		expStmt.getExpression().accept(visitor);
		scribe.printSemicolon();
	}

	private void writeCatchHandler(ICPPASTCatchHandler catchStatement) {
		scribe.print(CATCH);
		if (catchStatement.isCatchAll()) {
			scribe.print(VAR_ARGS);
		} else {
			scribe.noSemicolon();
			scribe.noNewLines();
			catchStatement.getDeclaration().accept(visitor);
			scribe.newLines();
		}
		scribe.print(')');
		writeBodyStatement(catchStatement.getCatchBody(), true);
	}

	private void writeTryBlockStatement(ICPPASTTryBlockStatement tryStatement) {
		scribe.print(TRY);
		tryStatement.getTryBody().accept(visitor);
		for (ICPPASTCatchHandler catchStatement : tryStatement.getCatchHandlers()) {
			writeStatement(catchStatement, false);
		}
	}

	private void writeWhileStatement(IASTWhileStatement whileStatment) {
		scribe.print(WHILE);
		scribe.noNewLines();
		if (whileStatment instanceof ICPPASTWhileStatement) {
			ICPPASTWhileStatement cppWhileStatment = (ICPPASTWhileStatement) whileStatment;
			if(cppWhileStatment.getConditionDeclaration() == null) {
				cppWhileStatment.getCondition().accept(visitor);
			} else {
				writeDeclarationWithoutSemicolon(cppWhileStatment.getConditionDeclaration());
			}		
		} else {
			whileStatment.getCondition().accept(visitor);
		}
		scribe.print(')');
		scribe.newLines();
		nextCompoundNoNewLine();
		writeBodyStatement(whileStatment.getBody(), false);
	}

	private void writeCaseStatement(IASTCaseStatement caseStatement) {
		nextCompoundIndentationLevelOneMore();
		
		if(!switchIsNew){
			scribe.decrementIndentationLevel();
		}
		scribe.print(CASE);
		caseStatement.getExpression().accept(visitor);
		scribe.print(':');
		scribe.incrementIndentationLevel();
		switchIsNew = false;
	}

	private void writeSwitchStatement(IASTSwitchStatement switchStatement) {
		switchIsNew = true;
		
		scribe.print(SWITCH_BRACKET);
		scribe.noNewLines();
		if (switchStatement instanceof ICPPASTSwitchStatement) {
			ICPPASTSwitchStatement cppSwitchStatement = (ICPPASTSwitchStatement) switchStatement;
			if(cppSwitchStatement.getControllerDeclaration() == null) {
				cppSwitchStatement.getControllerExpression().accept(visitor);
			} else {
				declWriter.writeDeclaration(cppSwitchStatement.getControllerDeclaration(), false);
			}
		} else {
			switchStatement.getControllerExpression().accept(visitor);
		}
		scribe.print(')');
		scribe.newLines();
		nextCompoundNoNewLine();
		writeBodyStatement(switchStatement.getBody(), false);
		
		switchIsNew = false;
	}

	private void writeDefaultStatement(IASTDefaultStatement defaultStatement) {
		nextCompoundIndentationLevelOneMore();
		
		if(!switchIsNew){
			scribe.decrementIndentationLevel();
		}
		scribe.print(DEFAULT);
		scribe.incrementIndentationLevel();
		switchIsNew = false;
	}
	
	private void writeCompoundStatement(IASTCompoundStatement compoundStatement) {
		scribe.printLBrace();
		scribe.newLine();
		for (IASTStatement statements : getNestedStatements(compoundStatement)) {
			statements.accept(visitor);
		}
		
		if(hasFreestandingComments(compoundStatement)) {
			writeFreeStandingComments(compoundStatement);			
		}
		
		if(decrementIndentationLevelOneMore){
			scribe.decrementIndentationLevel();
			decrementIndentationLevelOneMore = false;
		}
		scribe.printRBrace();
	}

	protected IASTStatement[] getNestedStatements(IASTCompoundStatement compoundStatement) {
		return compoundStatement.getStatements();
	}	
	
	protected void writeBodyStatement(IASTStatement statement, boolean isDoStatement) {
		if (statement instanceof IASTCompoundStatement){
			//TODO hsr existiert noch eine methode
			statement.accept(visitor);
			if(!isDoStatement){
				scribe.newLine();
			}
			compoundNoNewLine = false;
		} else if (statement instanceof IASTNullStatement){
			statement.accept(visitor);
			scribe.newLine();
		} else {
			scribe.incrementIndentationLevel();	
			scribe.newLine();	
			statement.accept(visitor);
			scribe.decrementIndentationLevel();	
			scribe.newLine();
		}
	}
	
	/**
	 * Write no new Line after the next Compound-Statement 
	 *
	 */
	protected void nextCompoundNoNewLine(){
		compoundNoNewLine = true;
	}
	
	/**
	 * Indent one time more at the end (before the closing Brackets) 
	 * of a Compound-Statement 
	 *
	 */
	protected void nextCompoundIndentationLevelOneMore(){
		decrementIndentationLevelOneMore = true;
	}

	protected int writeMixedStatement(IASTStatement statement) {
		IFile file = FileHelper.getFileFromNode(statement);
		int offset = statement.getFileLocation().getNodeOffset();
		int length = statement.getFileLocation().getNodeLength();
		String code =FileContentHelper.getContent(file, offset, length);
		
		scribe.println(code);
		return ASTVisitor.PROCESS_SKIP;
	}
}

Back to the top