Skip to main content
summaryrefslogtreecommitdiffstats
blob: e0034f70ce9d0aa4d0489c4a35cf7606d5239ee4 (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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*******************************************************************************
 * 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
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring;

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

import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

import org.eclipse.cdt.core.dom.ast.DOMException;
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.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTMacroExpansionLocation;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
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.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTReferenceOperator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateTypeParameter;
import org.eclipse.cdt.ui.CUIPlugin;

import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTArrayDeclarator;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTDeclarator;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTParameterDeclaration;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTReferenceOperator;
import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriter;

public class NodeContainer {
	
	public final NameInformation NULL_NAME_INFORMATION = new NameInformation(
			new CPPASTName());

	private final ArrayList<IASTNode> vec;
	private final ArrayList<NameInformation> names;

	public class NameInformation {

		private IASTName name;
		private IASTName declaration;
		private final ArrayList<IASTName> references;
		private ArrayList<IASTName> referencesAfterCached;
		private int lastCachedReferencesHash;
		private boolean isReference;
		private boolean isReturnValue;
		private boolean isConst;
		private boolean isWriteAccess;

		private boolean userSetIsReference;
		private boolean userSetIsReturnValue;
		private String userSetName;
		private int userOrder;

		public int getUserOrder() {
			return userOrder;
		}

		public void setUserOrder(int userOrder) {
			this.userOrder = userOrder;
		}

		public NameInformation(IASTName name) {
			super();
			this.name = name;
			references = new ArrayList<IASTName>();
		}

		public IASTName getDeclaration() {
			return declaration;
		}

		public void setDeclaration(IASTName declaration) {
			this.declaration = declaration;
		}

		public IASTName getName() {
			return name;
		}

		public void setName(IASTName name) {
			this.name = name;
		}

		public void addReference(IASTName name) {
			references.add(name);
		}

		public ArrayList<IASTName> getReferencesAfterSelection() {
			if (referencesAfterCached == null
					|| lastCachedReferencesHash != references.hashCode()) {

				lastCachedReferencesHash = references.hashCode();
				referencesAfterCached = new ArrayList<IASTName>();
				for (IASTName ref : references) {
					IASTFileLocation loc = ref.getFileLocation();
					if (loc.getNodeOffset() >= getEndOffset()) {
						referencesAfterCached.add(ref);
					}
				}
			}
			return referencesAfterCached;
		}

		public boolean isUsedAfterReferences() {
			return getReferencesAfterSelection().size() > 0;
		}

		public ICPPASTParameterDeclaration getICPPASTParameterDeclaration(
				boolean isReference) {
			ICPPASTParameterDeclaration para = new CPPASTParameterDeclaration();
			IASTDeclarator sourceDeclarator = (IASTDeclarator) getDeclaration()
					.getParent();

			if (sourceDeclarator.getParent() instanceof IASTSimpleDeclaration) {
				IASTSimpleDeclaration decl = (IASTSimpleDeclaration) sourceDeclarator
						.getParent();
				para.setDeclSpecifier(decl.getDeclSpecifier().copy());
			} else if (sourceDeclarator.getParent() instanceof IASTParameterDeclaration) {
				IASTParameterDeclaration decl = (IASTParameterDeclaration) sourceDeclarator
						.getParent();
				para.setDeclSpecifier(decl.getDeclSpecifier().copy());
			}

			IASTDeclarator declarator;
			if (sourceDeclarator instanceof IASTArrayDeclarator) {
				IASTArrayDeclarator arrDeclarator = (IASTArrayDeclarator) sourceDeclarator;
				declarator = new CPPASTArrayDeclarator();
				IASTArrayModifier[] arrayModifiers = arrDeclarator
						.getArrayModifiers();
				for (IASTArrayModifier arrayModifier : arrayModifiers) {
					((IASTArrayDeclarator) declarator)
							.addArrayModifier(arrayModifier.copy());
				}

			} else {
				declarator = new CPPASTDeclarator();
			}
			declarator.setName(new CPPASTName(getDeclaration().toCharArray()));
			for (IASTPointerOperator pointerOp : sourceDeclarator
					.getPointerOperators()) {
				declarator.addPointerOperator(pointerOp.copy());
			}

			if (isReference && !hasReferenceOperartor(declarator)) {
				declarator.addPointerOperator(new CPPASTReferenceOperator());
			}

			declarator.setNestedDeclarator(sourceDeclarator
					.getNestedDeclarator());
			para.setDeclarator(declarator);

			return para;
		}

		public boolean hasReferenceOperartor(IASTDeclarator declarator) {
			for (IASTPointerOperator pOp : declarator.getPointerOperators()) {
				if (pOp instanceof ICPPASTReferenceOperator) {
					return true;
				}
			}
			return false;
		}

		public String getType() {
			IASTDeclSpecifier declSpec = null;

			IASTNode node = getDeclaration().getParent();
			if (node instanceof ICPPASTSimpleTypeTemplateParameter) {
				ICPPASTSimpleTypeTemplateParameter parameter = (ICPPASTSimpleTypeTemplateParameter) node;
				return parameter.getName().toString();
			}
			IASTDeclarator sourceDeclarator = (IASTDeclarator) node;
			if (sourceDeclarator.getParent() instanceof IASTSimpleDeclaration) {
				IASTSimpleDeclaration decl = (IASTSimpleDeclaration) sourceDeclarator
						.getParent();
				declSpec = decl.getDeclSpecifier();
			} else if (sourceDeclarator.getParent() instanceof IASTParameterDeclaration) {
				IASTParameterDeclaration decl = (IASTParameterDeclaration) sourceDeclarator
						.getParent();
				declSpec = decl.getDeclSpecifier();
			}

			ASTWriter writer = new ASTWriter();
			return writer.write(declSpec);
		}

		public boolean isDeclarationInScope() {
			if(declaration.toCharArray().length > 0) {
				int declOffset = declaration.getFileLocation().getNodeOffset();
				return declOffset >= getStartOffset()
				&& declOffset <= getEndOffset();
			}
			return true;
		}

		@Override
		public String toString() {
			return Messages.NodeContainer_Name + name + ' '
					+ isDeclarationInScope();
		}

		public boolean isReference() {
			return isReference;
		}

		public void setReference(boolean isReference) {
			this.isReference = isReference;
		}

		public boolean isReturnValue() {
			return isReturnValue;
		}

		public void setReturnValue(boolean isReturnValue) {
			this.isReturnValue = isReturnValue;
		}

		public boolean isUserSetIsReference() {
			return userSetIsReference;
		}

		public void setUserSetIsReference(boolean userSetIsReference) {
			this.userSetIsReference = userSetIsReference;
		}

		public boolean isUserSetIsReturnValue() {
			return userSetIsReturnValue;
		}

		public void setUserSetIsReturnValue(boolean userSetIsReturnValue) {
			this.userSetIsReturnValue = userSetIsReturnValue;
		}

		public String getUserSetName() {
			return userSetName;
		}

		public void setUserSetName(String userSetName) {
			this.userSetName = userSetName;
		}

		public boolean isConst() {
			return isConst;
		}

		public void setConst(boolean isConst) {
			this.isConst = isConst;
		}

		public boolean isWriteAccess() {
			return isWriteAccess;
		}

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

	}

	public NodeContainer() {
		super();
		vec = new ArrayList<IASTNode>();
		names = new ArrayList<NameInformation>();
	}

	public int size() {
		return vec.size();
	}

	public void add(IASTNode node) {
		vec.add(node);
	}

	public void findAllNames() {
		for (IASTNode node : vec) {
			node.accept(new CPPASTVisitor() {
				{
					shouldVisitNames = true;
				}

				@Override
				public int visit(IASTName name) {
					IBinding bind = name.resolveBinding();

					if (bind instanceof ICPPBinding
							&& !(bind instanceof ICPPTemplateTypeParameter)) {
						ICPPBinding cppBind = (ICPPBinding) bind;
						try {
							if (!cppBind.isGloballyQualified()) {
								NameInformation nameInformation = new NameInformation(
										name);

								IASTName[] refs = name.getTranslationUnit()
										.getReferences(bind);
								for (IASTName ref : refs) {
									nameInformation.addReference(ref);
								}
								names.add(nameInformation);
							}
						} catch (DOMException e) {
							ILog logger = CUIPlugin.getDefault().getLog();
							IStatus status = new Status(IStatus.WARNING,
									CUIPlugin.PLUGIN_ID, IStatus.OK, e
											.getMessage(), e);
							logger.log(status);
						}
					}else if(bind instanceof IVariable) {
						NameInformation nameInformation = new NameInformation(
								name);

						IASTName[] refs = name.getTranslationUnit()
								.getReferences(bind);
						for (IASTName ref : refs) {
							nameInformation.addReference(ref);
						}
						names.add(nameInformation);
					}
					return super.visit(name);
				}
			});
		}

		for (NameInformation nameInf : names) {
			IASTName name = nameInf.getName();

			IASTTranslationUnit unit = name.getTranslationUnit();
			IASTName[] decls = unit.getDeclarationsInAST(name.resolveBinding());
			for (IASTName declaration : decls) {
				nameInf.setDeclaration(declaration);
			}
		}
	}

	/*
	 * Returns all local names in the selection which will be used after the
	 * selection expected the ones which are pointers
	 */
	public ArrayList<NameInformation> getAllAfterUsedNames() {
		ArrayList<IASTName> declarations = new ArrayList<IASTName>();
		ArrayList<NameInformation> usedAfter = new ArrayList<NameInformation>();

		if (names.size() <= 0) {
			findAllNames();
		}

		for (NameInformation nameInf : names) {
			if (!declarations.contains(nameInf.getDeclaration())) {

				declarations.add(nameInf.getDeclaration());
				if (nameInf.isUsedAfterReferences()) {
					usedAfter.add(nameInf);
					nameInf.setReference(true);
				}
			}
		}

		return usedAfter;
	}

	public ArrayList<NameInformation> getAllAfterUsedNamesChoosenByUser() {
		ArrayList<IASTName> declarations = new ArrayList<IASTName>();
		ArrayList<NameInformation> usedAfter = new ArrayList<NameInformation>();

		for (NameInformation nameInf : names) {
			if (!declarations.contains(nameInf.getDeclaration())) {

				declarations.add(nameInf.getDeclaration());
				if (nameInf.isUserSetIsReference()
						|| nameInf.isUserSetIsReturnValue()) {
					usedAfter.add(nameInf);
				}
			}
		}

		return usedAfter;
	}

	public ArrayList<NameInformation> getUsedNamesUnique() {
		ArrayList<IASTName> declarations = new ArrayList<IASTName>();
		ArrayList<NameInformation> usedAfter = new ArrayList<NameInformation>();

		if (names.size() <= 0) {
			findAllNames();
		}

		for (NameInformation nameInf : names) {
			if (!declarations.contains(nameInf.getDeclaration())) {

				declarations.add(nameInf.getDeclaration());
				usedAfter.add(nameInf);
			} else {
				for (NameInformation nameInformation : usedAfter) {
					if (nameInf.isWriteAccess()
							&& nameInf.getDeclaration() == nameInformation
									.getDeclaration()) {
						nameInformation.setWriteAccess(true);
					}
				}
			}
		}

		return usedAfter;
	}

	/*
	 * Returns all local names in the selection which will be used after the
	 * selection expected the ones which are pointers
	 * XXX Was soll dieser Kommentar aussagen? --Mirko
	 */
	public ArrayList<NameInformation> getAllDeclaredInScope() {
		ArrayList<IASTName> declarations = new ArrayList<IASTName>();
		ArrayList<NameInformation> usedAfter = new ArrayList<NameInformation>();

		for (NameInformation nameInf : names) {
			if (nameInf.isDeclarationInScope()
					&& !declarations.contains(nameInf.getDeclaration()) && nameInf.isUsedAfterReferences()) {

				declarations.add(nameInf.getDeclaration());
				usedAfter.add(nameInf);
				// is return value candidate, set returnvalue to true and
				// reference to false
				nameInf.setReturnValue(true);
				nameInf.setReference(false);
			}
		}

		return usedAfter;
	}

	public List<IASTNode> getNodesToWrite() {
		return vec;
	}

	public int getStartOffset() {
		return getOffset(false);
	}

	public int getStartOffsetIncludingComments() {
		return getOffset(true);
	}

	private int getOffset(boolean includeComments) {
		int start = Integer.MAX_VALUE;

		for (IASTNode node : vec) {
			int nodeStart = Integer.MAX_VALUE;

			IASTNodeLocation[] nodeLocations = node.getNodeLocations();
			if (nodeLocations.length != 1) {
				for (IASTNodeLocation location : nodeLocations) {
					int nodeOffset;
					if (location instanceof IASTMacroExpansionLocation) {
						IASTMacroExpansionLocation macroLoc = (IASTMacroExpansionLocation) location;
						nodeOffset = macroLoc.asFileLocation().getNodeOffset();
					}else {
						nodeOffset = node.getFileLocation().getNodeOffset();
					}
					if(nodeOffset <  nodeStart) {
						nodeStart = nodeOffset;
					}
				}
			} else {
				nodeStart = node.getFileLocation().getNodeOffset();
			}
			if (nodeStart < start) {
				start = nodeStart;
			}
		}

		return start;
	}

	public int getEndOffset() {
		return getEndOffset(false);
	}
	
	public int getEndOffsetIncludingComments() {
		return getEndOffset(true);
	}

	private int getEndOffset(boolean includeComments) {
		int end = 0;

		for (IASTNode node : vec) {
			int fileOffset = 0;
			int length = 0;
			
			IASTNodeLocation[] nodeLocations = node.getNodeLocations();
			for (IASTNodeLocation location : nodeLocations) {
				int nodeOffset, nodeLength;
				if (location instanceof IASTMacroExpansionLocation) {
					IASTMacroExpansionLocation macroLoc = (IASTMacroExpansionLocation) location;
					nodeOffset = macroLoc.asFileLocation().getNodeOffset();
					nodeLength = macroLoc.asFileLocation().getNodeLength();
				}else {
					nodeOffset = location.getNodeOffset();
					nodeLength = location.getNodeLength();
				}
				if(fileOffset < nodeOffset) {
					fileOffset = nodeOffset;
					length = nodeLength;
				}
			}
		int endNode = fileOffset + length;
		if (endNode > end) {
			end = endNode;
		}
	}

	return end;
	}

	@Override
	public String toString() {
		return vec.toString();
	}

	public ArrayList<NameInformation> getNames() {
		return names;
	}
	
}

Back to the top