Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8859680deed1c788a4aadf5baf255df4e6053035 (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
/*******************************************************************************
 * 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.ui.refactoring;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.Assert;

import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTArrayModifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNode.CopyStyle;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.INodeFactory;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNodeFactory;
import org.eclipse.cdt.core.dom.rewrite.TypeHelper;
import org.eclipse.cdt.core.model.ITranslationUnit;

import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriterVisitor;

/**
 * Additional information about an IASTName in code being refactored. 
 */
public class NameInformation {
	public static enum Indirection { NONE, POINTER, REFERENCE }

	public static final int INDEX_FOR_ADDED = -1;

	private final IASTName name;
	private IASTName declarationName;
	private final List<IASTName> referencesInSelection;
	private boolean isOutput;
	private boolean mustBeReturnValue;
	private boolean isWriteAccess;
	private boolean passOutputByPointer;
	private boolean isReturnValue;
	private String newName;
	private int newOrder;
	private boolean isDeleted;
	private String defaultValue;
	private String newTypeName;
	private Indirection indirection;

	public NameInformation(IASTName name) {
		this.name = name;
		this.newName = String.valueOf(name.getSimpleID());
		referencesInSelection = new ArrayList<IASTName>();
	}

	public static NameInformation createInfoForAddedParameter(String type, String name,
			String defaultValue) {
		NameInformation info= new NameInformation(null);
		info.setTypeName(type);
		info.setNewName(name);
		info.setDefaultValue(defaultValue);
		info.setNewOrder(INDEX_FOR_ADDED);
		return info;
	}

	/**
	 * For debugging only.
	 */
	@Override
	public String toString() {
		return name.toString();
	}

	public int getNewOrder() {
		return newOrder;
	}

	public void setNewOrder(int newOrder) {
		this.newOrder = newOrder;
	}

	/**
	 * Returns <code>true</code> if the value of the variable has to propagate to the outside world.
	 */
	public boolean isOutput() {
		return isOutput;
	}

	void setOutput(boolean isOutput) {
		this.isOutput = isOutput;
		indirection = null;
	}

	public boolean isOutputParameter() {
		return isOutput() && !isReturnValue();
	}

	public boolean mustBeReturnValue() {
		return mustBeReturnValue;
	}

	public void setMustBeReturnValue(boolean mustBeReturnValue) {
		this.mustBeReturnValue = mustBeReturnValue;
		indirection = null;
	}

	public boolean isReturnValue() {
		return mustBeReturnValue || isReturnValue;
	}

	public void setReturnValue(boolean isReturnValue) {
		Assert.isTrue(isReturnValue || !mustBeReturnValue);
		this.isReturnValue = isReturnValue;
		indirection = null;
	}

	public String getNewName() {
		return newName;
	}

	public void setNewName(String newName) {
		this.newName = newName;
	}

	public boolean isWriteAccess() {
		return isWriteAccess;
	}

	void setWriteAccess(boolean isWriteAceess) {
		this.isWriteAccess = isWriteAceess;
		indirection = null;
	}

	public boolean isDeleted() {
		return isDeleted;
	}

	public void markAsDeleted() {
		Assert.isTrue(!isAdded()); // Added parameters should be simply removed from the list
		isDeleted= true;
	}

	public boolean isAdded() {
		// TODO(sprigogin): Adding parameters is not supported yet.
		return false;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String value) {
		Assert.isNotNull(value);
		defaultValue= value;
	}

	public IASTName getDeclarationName() {
		return declarationName;
	}

	public IASTDeclarator getDeclarator() {
		return (IASTDeclarator) declarationName.getParent();
	}

	public IASTDeclSpecifier getDeclSpecifier() {
		IASTNode parent = getDeclarator().getParent();
		if (parent instanceof IASTSimpleDeclaration) {
			return ((IASTSimpleDeclaration) parent).getDeclSpecifier();
		} else if (parent instanceof IASTParameterDeclaration) {
			return ((IASTParameterDeclaration) parent).getDeclSpecifier();
		}
		return null;
	}

	void setDeclarationName(IASTName declarationName) {
		Assert.isTrue(declarationName.getParent() instanceof IASTDeclarator);
		this.declarationName = declarationName;
		indirection = null;
	}

	public IASTName getName() {
		return name;
	}

	public boolean isRenamed() {
		return name == null ? newName != null : !String.valueOf(name.getSimpleID()).equals(newName); 
	}

	void addReference(IASTName name, int startOffset, int endOffset) {
		int nodeOffset = name.getFileLocation().getNodeOffset();
		if (nodeOffset >= startOffset && nodeOffset < endOffset) {
			referencesInSelection.add(name);
		}
	}

	public String getTypeName() {
		if (newTypeName != null)
			return newTypeName;
		INodeFactory nodeFactory = name.getTranslationUnit().getASTNodeFactory();
		IASTParameterDeclaration declaration = getParameterDeclaration(nodeFactory, null);
		ASTWriterVisitor writer = new ASTWriterVisitor();
		declaration.accept(writer);
		return writer.toString();
	}

	public void setTypeName(String type) {
		Assert.isNotNull(type);
		newTypeName= type;
	}

	public String getReturnType() {
		if (!isReturnValue())
			return null;
		INodeFactory nodeFactory = name.getTranslationUnit().getASTNodeFactory();
		IASTDeclarator sourceDeclarator = getDeclarator();
		IASTDeclSpecifier declSpec = safeCopy(getDeclSpecifier());
		IASTDeclarator declarator = createDeclarator(nodeFactory, sourceDeclarator, null);
		IASTParameterDeclaration declaration = nodeFactory.newParameterDeclaration(declSpec, declarator);
		ASTWriterVisitor writer = new ASTWriterVisitor();
		declaration.accept(writer);
		return writer.toString();
	}

	public List<IASTName> getReferencesInSelection() {
		return referencesInSelection;
	}

	public IASTParameterDeclaration getParameterDeclaration(INodeFactory nodeFactory) {
		return getParameterDeclaration(nodeFactory, newName);
	}

	private IASTParameterDeclaration getParameterDeclaration(INodeFactory nodeFactory, String paramName) {
		IASTDeclarator sourceDeclarator = getDeclarator();
		IASTDeclSpecifier declSpec = safeCopy(getDeclSpecifier());
		IASTDeclarator declarator = createDeclarator(nodeFactory, sourceDeclarator, paramName);

		Indirection indirection = getIndirection();
		if (indirection == Indirection.POINTER) {
			declarator.addPointerOperator(nodeFactory.newPointer());
		} else if (indirection == Indirection.REFERENCE) {
			declarator.addPointerOperator(((ICPPNodeFactory) nodeFactory).newReferenceOperator(false));
		}

		if (indirection != Indirection.NONE && !isWriteAccess && declSpec != null) {
			declSpec.setConst(true);
		}

		declarator.setNestedDeclarator(sourceDeclarator.getNestedDeclarator());
		return nodeFactory.newParameterDeclaration(declSpec, declarator);
	}

	public Indirection getIndirection() {
		if (indirection == null) {
			indirection = Indirection.NONE;
			boolean isCpp = declarationName.getTranslationUnit() instanceof ICPPASTTranslationUnit;
			if (isOutputParameter()) {
				if (isCpp && !passOutputByPointer) {
					indirection = Indirection.REFERENCE;
				} else {
					indirection = Indirection.POINTER;
				}
			} else {
				IType type = TypeHelper.createType(getDeclarator());
				if (TypeHelper.shouldBePassedByReference(type, declarationName.getTranslationUnit())) {
					if (isCpp) {
						if (!isWriteAccess) {
							indirection = Indirection.REFERENCE;
						}
					} else {
						indirection = Indirection.POINTER;
					}
				}
			}
		}
		return indirection;
	}

	private IASTDeclarator createDeclarator(INodeFactory nodeFactory, IASTDeclarator sourceDeclarator,
			String name) {
		IASTName astName = name != null ?
				nodeFactory.newName(name.toCharArray()) : nodeFactory.newName();
		IASTDeclarator declarator;
		if (sourceDeclarator instanceof IASTArrayDeclarator) {
			IASTArrayDeclarator arrDeclarator = (IASTArrayDeclarator) sourceDeclarator;
			IASTArrayDeclarator arrayDeclarator = nodeFactory.newArrayDeclarator(astName);
			IASTArrayModifier[] arrayModifiers = arrDeclarator.getArrayModifiers();
			for (IASTArrayModifier arrayModifier : arrayModifiers) {
				arrayDeclarator.addArrayModifier(arrayModifier.copy(CopyStyle.withLocations));
			}
			declarator= arrayDeclarator;
		} else {
			declarator = nodeFactory.newDeclarator(astName);
		}
		for (IASTPointerOperator pointerOp : sourceDeclarator.getPointerOperators()) {
			declarator.addPointerOperator(pointerOp.copy(CopyStyle.withLocations));
		}
		return declarator;
	}

	@SuppressWarnings("unchecked")
	private static <T extends IASTNode> T safeCopy(T node) {
		return node == null ? null : (T) node.copy(CopyStyle.withLocations);
	}

	public ITranslationUnit getTranslationUnit() {
		return name != null ? name.getTranslationUnit().getOriginatingTranslationUnit() : null;
	}

	public boolean isPassOutputByPointer() {
		return passOutputByPointer;
	}

	public void setPassOutputByPointer(boolean passOutputByPointer) {
		this.passOutputByPointer = passOutputByPointer;
		indirection = null;
	}
}

Back to the top