summaryrefslogtreecommitdiffstats
blob: dd5de13f038bcb9bb223467d21d84d21684b5cfb (plain)
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
/*******************************************************************************
 * 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
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;

import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTEnumerationSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTTypedefNameSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.parser.Keywords;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;

/**
 * Generates source code of declaration specifier nodes. The actual string operations are delegated
 * to the <code>Scribe</code> class.
 * 
 * @see Scribe
 * @see IASTDeclSpecifier
 * @author Emanuel Graf IFS
 */
public class DeclSpecWriter extends NodeWriter {
	
	public DeclSpecWriter(Scribe scribe, ASTWriterVisitor visitor, NodeCommentMap commentMap) {
		super(scribe, visitor, commentMap);
	}

	protected void writeDelcSpec(IASTDeclSpecifier declSpec) {
		// Write general DelcSpec Keywords
		writeDeclSpec(declSpec);
		if (declSpec instanceof ICPPASTDeclSpecifier) {
			writeCPPDeclSpec((ICPPASTDeclSpecifier) declSpec);
		} else if (declSpec instanceof ICASTDeclSpecifier) {
			writeCDeclSpec((ICASTDeclSpecifier) declSpec);
		}
	}

	private String getCPPSimpleDecSpecifier(ICPPASTSimpleDeclSpecifier simpDeclSpec) {
		return getASTSimpleDecSpecifier(simpDeclSpec.getType(), true);
	}
	
	private String getCSimpleDecSpecifier(ICASTSimpleDeclSpecifier simpDeclSpec) {
		return getASTSimpleDecSpecifier(simpDeclSpec.getType(), false);
	}

	private String getASTSimpleDecSpecifier(int type, boolean isCpp) {
		switch (type) {
		case IASTSimpleDeclSpecifier.t_unspecified:
			return ""; //$NON-NLS-1$
		case IASTSimpleDeclSpecifier.t_void:
			return Keywords.VOID;
		case IASTSimpleDeclSpecifier.t_char:
			return Keywords.CHAR;
		case IASTSimpleDeclSpecifier.t_int:
			return Keywords.INT;

		case IASTSimpleDeclSpecifier.t_float:
			return Keywords.FLOAT;
		case IASTSimpleDeclSpecifier.t_double:
			return Keywords.DOUBLE;
			
		case IASTSimpleDeclSpecifier.t_bool:
			return isCpp ? Keywords.BOOL : Keywords._BOOL;
			
		case IASTSimpleDeclSpecifier.t_wchar_t:
			if (isCpp)
				return Keywords.WCHAR_T;
			break;
		case IASTSimpleDeclSpecifier.t_char16_t:
			if (isCpp)
				return Keywords.CHAR16_T;
			break;
		case IASTSimpleDeclSpecifier.t_char32_t:
			if (isCpp)
				return Keywords.CHAR32_T;
			break;
		case IASTSimpleDeclSpecifier.t_auto:
			if (isCpp)
				return Keywords.AUTO;
			break;
		case IASTSimpleDeclSpecifier.t_typeof:
			if (isCpp)
				return Keywords.TYPEOF;
			break;
		case IASTSimpleDeclSpecifier.t_decltype:
			if (isCpp)
				return Keywords.DECLTYPE;
			break;
		}

		throw new IllegalArgumentException("Unknown specifier type: " + type); //$NON-NLS-1$
	}

	private void writeCDeclSpec(ICASTDeclSpecifier cDeclSpec) {
		if (cDeclSpec.isRestrict()) {
			scribe.printStringSpace(Keywords.RESTRICT);
		}
		
		if (cDeclSpec instanceof ICASTCompositeTypeSpecifier) {
			writeCompositeTypeSpecifier((ICASTCompositeTypeSpecifier) cDeclSpec);
		} else if (cDeclSpec instanceof ICASTEnumerationSpecifier) {
			writeEnumSpec((ICASTEnumerationSpecifier) cDeclSpec);
		} else if (cDeclSpec instanceof ICASTElaboratedTypeSpecifier) {
			writeElaboratedTypeSec((ICASTElaboratedTypeSpecifier) cDeclSpec);
		} else if (cDeclSpec instanceof ICASTSimpleDeclSpecifier) {
			writeCSimpleDeclSpec((ICASTSimpleDeclSpecifier) cDeclSpec);
		} else if (cDeclSpec instanceof ICASTTypedefNameSpecifier) {
			writeNamedTypeSpecifier((ICASTTypedefNameSpecifier) cDeclSpec);
		}
	}

	private void writeNamedTypeSpecifier(ICPPASTNamedTypeSpecifier namedSpc) {
		if (namedSpc.isTypename()) {
			scribe.printStringSpace(Keywords.TYPENAME);
		}
		namedSpc.getName().accept(visitor);
	}

	private void writeNamedTypeSpecifier(IASTNamedTypeSpecifier namedSpc) {
		namedSpc.getName().accept(visitor);
	}

	private void writeElaboratedTypeSec(IASTElaboratedTypeSpecifier elabType) {
		scribe.printStringSpace(getElabTypeString(elabType.getKind()));
		elabType.getName().accept(visitor);
	}

	private String getElabTypeString(int kind) {
		switch (kind) {
		case IASTElaboratedTypeSpecifier.k_enum:
			return Keywords.ENUM;
		case IASTElaboratedTypeSpecifier.k_struct:
			return Keywords.STRUCT;
		case IASTElaboratedTypeSpecifier.k_union:
			return Keywords.UNION;
		case ICPPASTElaboratedTypeSpecifier.k_class:
			return Keywords.CLASS;
			
		default:
			throw new IllegalArgumentException("Unknown elaborated type: " + kind); //$NON-NLS-1$
		}
	}

	private void writeCPPDeclSpec(ICPPASTDeclSpecifier cppDelcSpec) {
		if (cppDelcSpec.isVirtual()) {
			scribe.printStringSpace(Keywords.VIRTUAL);
		}
		if (cppDelcSpec.isConstexpr()) {
			scribe.printStringSpace(Keywords.CONSTEXPR);
		}
		if (cppDelcSpec.isExplicit()) {
			scribe.printStringSpace(Keywords.EXPLICIT);
		}
		if (cppDelcSpec.isFriend()) {
			scribe.printStringSpace(Keywords.FRIEND);
		}
		if (cppDelcSpec.isThreadLocal()) {
			scribe.printStringSpace(Keywords.THREAD_LOCAL);
		}
		if (cppDelcSpec.getStorageClass() == IASTDeclSpecifier.sc_mutable) {
			scribe.printStringSpace(Keywords.MUTABLE);
		}
		
		if (cppDelcSpec instanceof ICPPASTCompositeTypeSpecifier) {
			writeCompositeTypeSpecifier((ICPPASTCompositeTypeSpecifier) cppDelcSpec);
		} else if (cppDelcSpec instanceof IASTEnumerationSpecifier) {
			writeEnumSpec((IASTEnumerationSpecifier) cppDelcSpec);
		} else if (cppDelcSpec instanceof ICPPASTElaboratedTypeSpecifier) {
			writeElaboratedTypeSec((ICPPASTElaboratedTypeSpecifier) cppDelcSpec);
		} else if (cppDelcSpec instanceof ICPPASTSimpleDeclSpecifier) {
			writeCPPSimpleDeclSpec((ICPPASTSimpleDeclSpecifier) cppDelcSpec);
		} else if (cppDelcSpec instanceof ICPPASTNamedTypeSpecifier) {
			writeNamedTypeSpecifier((ICPPASTNamedTypeSpecifier) cppDelcSpec);
		}
	}

	private void writeEnumSpec(IASTEnumerationSpecifier enumSpec) {
		scribe.printStringSpace(Keywords.ENUM);
		enumSpec.getName().accept(visitor);
		scribe.print('{');
		scribe.printSpace();
		IASTEnumerator[] enums = enumSpec.getEnumerators();
		for (int i = 0; i < enums.length; ++i) {
			writeEnumerator(enums[i]);
			if (i + 1 < enums.length) {
				scribe.print(NodeWriter.COMMA_SPACE);
			}
		}
		scribe.print('}');
	}

	private void writeEnumerator(IASTEnumerator enumerator) {
		enumerator.getName().accept(visitor);
		
		IASTExpression value = enumerator.getValue();
		if (value != null) {
			scribe.print(EQUALS);
			value.accept(visitor);
		}		
	}

	private void writeCompositeTypeSpecifier(IASTCompositeTypeSpecifier compDeclSpec) {
		boolean hasTrailingComments = hasTrailingComments(compDeclSpec.getName());
		scribe.printStringSpace(getCPPCompositeTypeString(compDeclSpec.getKey()));
		compDeclSpec.getName().accept(visitor);
		if (compDeclSpec instanceof ICPPASTCompositeTypeSpecifier) {
			ICPPASTCompositeTypeSpecifier cppComp = (ICPPASTCompositeTypeSpecifier) compDeclSpec;
			ICPPASTBaseSpecifier[] baseSpecifiers = cppComp.getBaseSpecifiers();
			if (baseSpecifiers.length > 0) {
				scribe.print(SPACE_COLON_SPACE);
				for (int i = 0; i < baseSpecifiers.length; ++i) {
					writeBaseSpecifiers(baseSpecifiers[i]);
					if (i + 1 < baseSpecifiers.length) {
						scribe.print(COMMA_SPACE);
					}
				}
				hasTrailingComments = hasTrailingComments(baseSpecifiers[baseSpecifiers.length-1].getName());
			}
		}
		if (!hasTrailingComments) {
			scribe.newLine();
		}
		scribe.print('{');
		scribe.newLine();
		scribe.incrementIndentationLevel();
		visitor.setSuppressLeadingBlankLine(true);
		IASTDeclaration[] decls = getMembers(compDeclSpec);
		
		if (decls.length > 0) {
			for (IASTDeclaration declaration : decls) {
				declaration.accept(visitor);
			}
		}

		if (hasFreestandingComments(compDeclSpec)) {
			writeFreestandingComments(compDeclSpec);			
		}
		scribe.decrementIndentationLevel();
		scribe.print('}');

		if (hasTrailingComments(compDeclSpec)) {
			writeTrailingComments(compDeclSpec);			
		}
	}

	protected IASTDeclaration[] getMembers(IASTCompositeTypeSpecifier compDeclSpec) {
		return compDeclSpec.getMembers();
	}

	private void writeBaseSpecifiers(ICPPASTBaseSpecifier specifier) {
		switch (specifier.getVisibility()) {
		case ICPPASTBaseSpecifier.v_public:
			scribe.printStringSpace(Keywords.PUBLIC);
			break;
		case ICPPASTBaseSpecifier.v_protected:
			scribe.printStringSpace(Keywords.PROTECTED);
			break;
		case ICPPASTBaseSpecifier.v_private:
			scribe.printStringSpace(Keywords.PRIVATE);
			break;
		}
		specifier.getName().accept(visitor);
	}

	private String getCPPCompositeTypeString(int key) {
		if (key <= IASTCompositeTypeSpecifier.k_last) {
			return getCompositeTypeString(key);
		}
		switch (key) {
		case ICPPASTCompositeTypeSpecifier.k_class:
			return Keywords.CLASS;
		default:
			throw new IllegalArgumentException("Unknown type specifier: " + key); //$NON-NLS-1$
		}
	}

	private String getCompositeTypeString(int key) {
		switch (key) {
		case IASTCompositeTypeSpecifier.k_struct:
			return Keywords.STRUCT;
		case IASTCompositeTypeSpecifier.k_union:
			return Keywords.UNION;
		default:
			throw new IllegalArgumentException("Unknown type specifier: " + key); //$NON-NLS-1$
		}
	}

	private void writeDeclSpec(IASTDeclSpecifier declSpec) {
		if (declSpec.isInline()) {
			scribe.printStringSpace(Keywords.INLINE);
		}
		switch (declSpec.getStorageClass()) {
		case IASTDeclSpecifier.sc_typedef:
			scribe.printStringSpace(Keywords.TYPEDEF);
			break;
		case IASTDeclSpecifier.sc_extern:
			scribe.printStringSpace(Keywords.EXTERN);
			break;
		case IASTDeclSpecifier.sc_static:
			scribe.printStringSpace(Keywords.STATIC);
			break;
		case IASTDeclSpecifier.sc_auto:
			scribe.printStringSpace(Keywords.AUTO);
			break;
		case IASTDeclSpecifier.sc_register:
			scribe.printStringSpace(Keywords.REGISTER);
			break;
		}
		if (declSpec.isConst()) {
			scribe.printStringSpace(Keywords.CONST);
		}
		if (declSpec.isVolatile()) {
			scribe.printStringSpace(Keywords.VOLATILE);
		}
	}

	private void writeCPPSimpleDeclSpec(ICPPASTSimpleDeclSpecifier simpDeclSpec) {
		printQualifiers(simpDeclSpec);
		scribe.print(getCPPSimpleDecSpecifier(simpDeclSpec));
		if (simpDeclSpec.getType() == IASTSimpleDeclSpecifier.t_typeof) {
			scribe.printSpace();
			visitNodeIfNotNull(simpDeclSpec.getDeclTypeExpression());
		} else if (simpDeclSpec.getType() == IASTSimpleDeclSpecifier.t_decltype) {
			scribe.print('(');
			visitNodeIfNotNull(simpDeclSpec.getDeclTypeExpression());
			scribe.print(')');
		}
	}
	
	private void printQualifiers(IASTSimpleDeclSpecifier simpDeclSpec) {
		if (simpDeclSpec.isSigned()) {
			scribe.printStringSpace(Keywords.SIGNED);
		} else if (simpDeclSpec.isUnsigned()) {
			scribe.printStringSpace(Keywords.UNSIGNED);
		}
		
		if (simpDeclSpec.isShort()) {
			scribe.printStringSpace(Keywords.SHORT);
		} else if (simpDeclSpec.isLong()) {
			scribe.printStringSpace(Keywords.LONG);
		} else if (simpDeclSpec.isLongLong()) {			
			scribe.printStringSpace(Keywords.LONG);
			scribe.printStringSpace(Keywords.LONG);
		}
		if (simpDeclSpec instanceof ICASTSimpleDeclSpecifier) {
			ICASTSimpleDeclSpecifier cSimpDeclSpec = (ICASTSimpleDeclSpecifier) simpDeclSpec;
			if (cSimpDeclSpec.isComplex()) {
				scribe.printStringSpace(Keywords._COMPLEX);
			}
		}
	}

	private void writeCSimpleDeclSpec(ICASTSimpleDeclSpecifier simpDeclSpec) {
		printQualifiers(simpDeclSpec);
		scribe.print(getCSimpleDecSpecifier(simpDeclSpec));
	}
}