Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9471f80791d9863e60107856dfdfdf69ac657438 (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
/*******************************************************************************
 * Copyright (c) 2008, 2014 Institute for Software, HSR Hochschule fuer Technik
 * Rapperswil, University of applied sciences and others
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Institute for Software - initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *     Sergey Prigogin (Google)
 *     Thomas Corbat (IFS)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;

import java.util.EnumSet;

import org.eclipse.cdt.core.dom.ast.IASTASMDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTAttributeOwner;
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.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTProblemDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTAliasDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCatchHandler;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorChainInitializer;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTExplicitTemplateInstantiation;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionWithTryBlock;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLinkageSpecification;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceAlias;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTStaticAssertDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateSpecialization;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTypeId;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDirective;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTVisibilityLabel;
import org.eclipse.cdt.core.parser.Keywords;
import org.eclipse.cdt.internal.core.dom.parser.ASTQueries;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;

/**
 * Generates source code of declaration nodes. The actual string operations are delegated
 * to the <code>Scribe</code> class.
 *
 * @see Scribe
 * @see IASTDeclaration
 * @author Emanuel Graf IFS
 */
public class DeclarationWriter extends NodeWriter {
	private static final char OPEN_PAREN = '(';
	private static final char CLOSE_PAREN = ')';
	private static final String ASM_START = "asm" + OPEN_PAREN; //$NON-NLS-1$
	private static final String TEMPLATE_DECLARATION = "template<"; //$NON-NLS-1$
	private static final String TEMPLATE_SPECIALIZATION = "template<> "; //$NON-NLS-1$
	private static final char COMMA = ',';
	private boolean printSemicolon;

	public DeclarationWriter(Scribe scribe, ASTWriterVisitor visitor, NodeCommentMap commentMap) {
		super(scribe, visitor, commentMap);
	}

	protected void writeDeclaration(IASTDeclaration declaration) throws ProblemRuntimeException {
		writeDeclaration(declaration, true);
	}

	protected void writeDeclaration(IASTDeclaration declaration, boolean writeSemicolon) {
		boolean addNewLine = true;
		printSemicolon = writeSemicolon;
		if (declaration instanceof IASTASMDeclaration) {
			writeASMDeclatation((IASTASMDeclaration) declaration);
		} else if (declaration instanceof IASTFunctionDefinition) {
			writeFunctionDefinition((IASTFunctionDefinition) declaration);
			addNewLine = false;
		} else if (declaration instanceof IASTProblemDeclaration) {
			throw new ProblemRuntimeException((IASTProblemDeclaration) declaration);
		} else if (declaration instanceof IASTSimpleDeclaration) {
			writeSimpleDeclaration((IASTSimpleDeclaration) declaration);
		} else if (declaration instanceof ICPPASTExplicitTemplateInstantiation) {
			writeExplicitTemplateInstantiation((ICPPASTExplicitTemplateInstantiation) declaration);
			addNewLine = false;
		} else if (declaration instanceof ICPPASTLinkageSpecification) {
			writeLinkageSpecification((ICPPASTLinkageSpecification) declaration);
		} else if (declaration instanceof ICPPASTNamespaceAlias) {
			writeNamespaceAlias((ICPPASTNamespaceAlias) declaration);
		} else if (declaration instanceof ICPPASTTemplateDeclaration) {
			writeTemplateDeclaration((ICPPASTTemplateDeclaration) declaration);
			addNewLine = false;
		} else if (declaration instanceof ICPPASTTemplateSpecialization) {
			writeTemplateSpecialization((ICPPASTTemplateSpecialization) declaration);
			addNewLine = false;
		} else if (declaration instanceof ICPPASTUsingDeclaration) {
			writeUsingDeclaration((ICPPASTUsingDeclaration) declaration);
		} else if (declaration instanceof ICPPASTUsingDirective) {
			writeUsingDirective((ICPPASTUsingDirective) declaration);
		} else if (declaration instanceof ICPPASTVisibilityLabel) {
			writeVisibilityLabel((ICPPASTVisibilityLabel) declaration);
		} else if (declaration instanceof ICPPASTAliasDeclaration) {
			writeAliasDeclaration((ICPPASTAliasDeclaration) declaration);
		} else if (declaration instanceof ICPPASTStaticAssertDeclaration) {
			writeStaticAssertDeclaration((ICPPASTStaticAssertDeclaration) declaration);
		}

		writeTrailingComments(declaration, addNewLine);
		if (hasFreestandingComments(declaration)) {
			if (declaration instanceof IASTFunctionDefinition) {
				scribe.newLine();
			}
			writeFreestandingComments(declaration);
		}
	}

	private void writeAliasDeclaration(ICPPASTAliasDeclaration aliasDeclaration) {
		scribe.printStringSpace(Keywords.USING);
		IASTName alias = aliasDeclaration.getAlias();
		if (alias != null) {
			alias.accept(visitor);
		}
		writeAttributes(aliasDeclaration, EnumSet.of(SpaceLocation.BEFORE));
		scribe.print(EQUALS);
		ICPPASTTypeId aliasedType = aliasDeclaration.getMappingTypeId();
		if (aliasedType != null) {
			aliasedType.accept(visitor);
		}
		scribe.printSemicolon();
	}

	private void writeVisibilityLabel(ICPPASTVisibilityLabel visiblityLabel) {
		scribe.decrementIndentationLevel();
		switch (visiblityLabel.getVisibility()) {
		case ICPPASTVisibilityLabel.v_private:
			scribe.print(Keywords.PRIVATE);
			scribe.print(':');
			break;
		case ICPPASTVisibilityLabel.v_protected:
			scribe.print(Keywords.PROTECTED);
			scribe.print(':');
			break;
		case ICPPASTVisibilityLabel.v_public:
			scribe.print(Keywords.PUBLIC);
			scribe.print(':');
			break;
		default:
			return;
		}
		scribe.incrementIndentationLevel();
	}

	private void writeUsingDirective(ICPPASTUsingDirective usingDirective) {
		writeCPPAttributes(usingDirective, EnumSet.of(SpaceLocation.AFTER));
		scribe.printStringSpace(Keywords.USING);
		scribe.printStringSpace(Keywords.NAMESPACE);
		usingDirective.getQualifiedName().accept(visitor);
		writeGCCAttributes(usingDirective, EnumSet.of(SpaceLocation.BEFORE));
		scribe.printSemicolon();
	}

	private void writeUsingDeclaration(ICPPASTUsingDeclaration usingDeclaration) {
		scribe.printStringSpace(Keywords.USING);
		if (usingDeclaration.isTypename()) {
			scribe.printStringSpace(Keywords.TYPENAME);
		}
		usingDeclaration.getName().accept(visitor);
		scribe.printSemicolon();
	}

	private void writeTemplateSpecialization(ICPPASTTemplateSpecialization templateSpecialization) {
		scribe.print(TEMPLATE_SPECIALIZATION);
		templateSpecialization.getDeclaration().accept(visitor);
	}

	protected void writeTemplateDeclaration(ICPPASTTemplateDeclaration templateDeclaration) {
		if (templateDeclaration.isExported()) {
			scribe.printStringSpace(Keywords.EXPORT);
		}
		scribe.print(TEMPLATE_DECLARATION);
		ICPPASTTemplateParameter[] paraDecls = templateDeclaration.getTemplateParameters();
		for (int i = 0; i < paraDecls.length; ++i) {
			paraDecls[i].accept(visitor);
			if (i + 1 < paraDecls.length) {
				scribe.print(',');
				scribe.printSpaces(1);
			}
		}
		scribe.print('>');
		scribe.printSpace();
		templateDeclaration.getDeclaration().accept(visitor);
	}

	protected void writeDeclaration(ICPPASTNamespaceDefinition declaration) {
		printSemicolon = true;
		writeNamespaceDefinition(declaration);
	}

	private void writeNamespaceDefinition(ICPPASTNamespaceDefinition namespaceDefinition) {
		if (namespaceDefinition.isInline()) {
			scribe.printStringSpace(Keywords.INLINE);
		}
		scribe.printStringSpace(Keywords.NAMESPACE);
		writeCPPAttributes(namespaceDefinition, EnumSet.of(SpaceLocation.AFTER));
		namespaceDefinition.getName().accept(visitor);
		writeGCCAttributes(namespaceDefinition, EnumSet.of(SpaceLocation.BEFORE));
		if (!hasTrailingComments(namespaceDefinition.getName())) {
			scribe.newLine();
		}
		scribe.print('{');
		scribe.newLine(2);
		writeDeclarationsInNamespace(namespaceDefinition, namespaceDefinition.getDeclarations());
		if (hasFreestandingComments(namespaceDefinition)) {
			writeFreestandingComments(namespaceDefinition);
		}
		scribe.newLine();
		scribe.print('}');

		if (hasTrailingComments(namespaceDefinition)) {
			writeTrailingComments(namespaceDefinition);
		} else {
			scribe.newLine();
		}
	}

	protected void writeDeclarationsInNamespace(ICPPASTNamespaceDefinition namespaceDefinition,
			IASTDeclaration[] declarations) {
		for (IASTDeclaration declaration : declarations) {
			declaration.accept(visitor);
		}
	}

	private void writeNamespaceAlias(ICPPASTNamespaceAlias namespaceAliasDefinition) {
		scribe.printStringSpace(Keywords.NAMESPACE);
		namespaceAliasDefinition.getAlias().accept(visitor);
		scribe.print(EQUALS);
		namespaceAliasDefinition.getMappingName().accept(visitor);
		printSemicolon();
	}

	private void writeLinkageSpecification(ICPPASTLinkageSpecification linkageSpecification) {
		scribe.printStringSpace(Keywords.EXTERN);
		scribe.printStringSpace(linkageSpecification.getLiteral());

		IASTDeclaration[] declarations = linkageSpecification.getDeclarations();
		if (declarations.length > 1) {
			scribe.printLBrace();
			scribe.decrementIndentationLevel();
			scribe.newLine();
			for (IASTDeclaration declaration : declarations) {
				declaration.accept(visitor);
			}
			scribe.printRBrace();
			scribe.incrementIndentationLevel();
		} else if (declarations.length > 0) {
			visitNodeIfNotNull(declarations[0]);
		}
	}

	private void writeExplicitTemplateInstantiation(
			ICPPASTExplicitTemplateInstantiation explicitTemplateInstantiation) {
		switch (explicitTemplateInstantiation.getModifier()) {
		case ICPPASTExplicitTemplateInstantiation.EXTERN:
			scribe.printStringSpace(Keywords.EXTERN);
			break;
		case ICPPASTExplicitTemplateInstantiation.INLINE:
			scribe.printStringSpace(Keywords.INLINE);
			break;
		case ICPPASTExplicitTemplateInstantiation.STATIC:
			scribe.printStringSpace(Keywords.STATIC);
			break;
		}

		scribe.printStringSpace(Keywords.TEMPLATE);
		explicitTemplateInstantiation.getDeclaration().accept(visitor);
	}

	private void writeASMDeclatation(IASTASMDeclaration asmDeclaration) {
		scribe.print(ASM_START);
		scribe.print(asmDeclaration.getAssembly());
		scribe.print(CLOSE_PAREN);
		printSemicolon();
	}

	private void printSemicolon() {
		if (printSemicolon) {
			scribe.printSemicolon();
		}
	}

	private void writeFunctionDefinition(IASTFunctionDefinition funcDef) {
		if (funcDef instanceof IASTAttributeOwner) {
			writeAttributes((IASTAttributeOwner) funcDef, EnumSet.of(SpaceLocation.AFTER));
		}
		IASTDeclSpecifier declSpecifier = funcDef.getDeclSpecifier();
		if (declSpecifier != null)
			declSpecifier.accept(visitor);
		if (declSpecifier instanceof IASTSimpleDeclSpecifier) {
			IASTSimpleDeclSpecifier simDeclSpec = (IASTSimpleDeclSpecifier) declSpecifier;
			if (simDeclSpec.getType() != IASTSimpleDeclSpecifier.t_unspecified) {
				visitor.setSpaceNeededBeforeName(true);
			}
		} else {
			visitor.setSpaceNeededBeforeName(true);
		}
		IASTDeclarator declarator = ASTQueries.findOutermostDeclarator(funcDef.getDeclarator());
		declarator.accept(visitor);

		if (funcDef instanceof ICPPASTFunctionWithTryBlock) {
			scribe.newLine();
			scribe.print(Keywords.TRY);
		}

		if (funcDef instanceof ICPPASTFunctionDefinition) {
			ICPPASTFunctionDefinition cppFuncDef = (ICPPASTFunctionDefinition) funcDef;
			writeCtorChainInitializer(cppFuncDef, cppFuncDef.getMemberInitializers());
			if (cppFuncDef.isDefaulted()) {
				scribe.print(EQUALS);
				scribe.print(Keywords.DEFAULT);
				scribe.printSemicolon();
			} else if (cppFuncDef.isDeleted()) {
				scribe.print(EQUALS);
				scribe.print(Keywords.DELETE);
				scribe.printSemicolon();
			}
		}
		scribe.newLine();

		IASTStatement body = funcDef.getBody();
		if (body != null) {
			body.accept(visitor);
		}

		if (funcDef instanceof ICPPASTFunctionWithTryBlock) {
			ICPPASTFunctionWithTryBlock tryblock = (ICPPASTFunctionWithTryBlock) funcDef;
			ICPPASTCatchHandler[] catches = tryblock.getCatchHandlers();
			for (ICPPASTCatchHandler handler : catches) {
				handler.accept(visitor);
			}
		}
	}

	protected void writeCtorChainInitializer(ICPPASTFunctionDefinition funcDec,
			ICPPASTConstructorChainInitializer[] ctorInitChain) {
		if (ctorInitChain.length != 0) {
			scribe.newLine();
			scribe.print(':');
		}
		for (int i = 0; i < ctorInitChain.length; ++i) {
			ICPPASTConstructorChainInitializer initializer = ctorInitChain[i];
			initializer.accept(visitor);
			if (i + 1 < ctorInitChain.length) {
				scribe.print(COMMA_SPACE);
			}
		}
	}

	private void writeSimpleDeclaration(IASTSimpleDeclaration simpDec) {
		IASTDeclSpecifier declSpecifier = simpDec.getDeclSpecifier();
		IASTDeclarator[] decls = simpDec.getDeclarators();

		writeAttributes(simpDec, EnumSet.of(SpaceLocation.AFTER));
		declSpecifier.accept(visitor);
		boolean noSpace = false;
		if (declSpecifier instanceof IASTSimpleDeclSpecifier) {
			IASTSimpleDeclSpecifier simpleDeclSpecifier = (IASTSimpleDeclSpecifier) declSpecifier;
			if (simpleDeclSpecifier.getType() == IASTSimpleDeclSpecifier.t_unspecified) {
				noSpace = true;
			}
		}

		if (decls.length > 0) {
			if (decls.length == 1) {
				if (!noSpace)
					visitor.setSpaceNeededBeforeName(true);
				decls[0].accept(visitor);
			} else {
				if (!noSpace) {
					scribe.printSpace();
				}
				writeNodeList(decls);
			}
		}

		printSemicolon();
	}

	private void writeStaticAssertDeclaration(ICPPASTStaticAssertDeclaration staticAssertDeclaration) {
		scribe.print(Keywords.STATIC_ASSERT);
		scribe.print(OPEN_PAREN);
		staticAssertDeclaration.getCondition().accept(visitor);

		ICPPASTLiteralExpression message = staticAssertDeclaration.getMessage();
		if (message != null) {
			scribe.print(COMMA);
			scribe.printSpace();
			message.accept(visitor);
		}

		scribe.print(CLOSE_PAREN);
		scribe.printSemicolon();
	}
}

Back to the top