Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7b1b8a0e8e97c9f814eecbca4ea6d0b06b92f57a (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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
/*******************************************************************************
 * 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.extractfunction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.preferences.IPreferencesService;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
import org.eclipse.osgi.util.NLS;
import org.eclipse.text.edits.TextEditGroup;

import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTComment;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
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.IASTEqualsInitializer;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTInitializerClause;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier;
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.IASTReturnStatement;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IBasicType;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IField;
import org.eclipse.cdt.core.dom.ast.INodeFactory;
import org.eclipse.cdt.core.dom.ast.IParameter;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConversionName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTOperatorName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateId;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.cdt.core.dom.rewrite.TypeHelper;
import org.eclipse.cdt.core.formatter.DefaultCodeFormatterOptions;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.PreferenceConstants;

import org.eclipse.cdt.internal.core.dom.parser.c.CASTBinaryExpression;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTBinaryExpression;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTCompoundStatement;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTDeclarationStatement;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTDeclarator;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTEqualsInitializer;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTExpressionStatement;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionCallExpression;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDefinition;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTIdExpression;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTQualifiedName;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTReturnStatement;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclaration;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTemplateDeclaration;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil;
import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriterVisitor;

import org.eclipse.cdt.internal.ui.refactoring.CRefactoring;
import org.eclipse.cdt.internal.ui.refactoring.CRefactoringDescriptor;
import org.eclipse.cdt.internal.ui.refactoring.ClassMemberInserter;
import org.eclipse.cdt.internal.ui.refactoring.ClassMemberInserter.InsertionInfo;
import org.eclipse.cdt.internal.ui.refactoring.Container;
import org.eclipse.cdt.internal.ui.refactoring.MethodContext;
import org.eclipse.cdt.internal.ui.refactoring.MethodContext.ContextType;
import org.eclipse.cdt.internal.ui.refactoring.ModificationCollector;
import org.eclipse.cdt.internal.ui.refactoring.NameInformation;
import org.eclipse.cdt.internal.ui.refactoring.NameInformation.Indirection;
import org.eclipse.cdt.internal.ui.refactoring.NodeContainer;
import org.eclipse.cdt.internal.ui.refactoring.utils.ASTHelper;
import org.eclipse.cdt.internal.ui.refactoring.utils.CPPASTAllVisitor;
import org.eclipse.cdt.internal.ui.refactoring.utils.Checks;
import org.eclipse.cdt.internal.ui.refactoring.utils.NodeHelper;
import org.eclipse.cdt.internal.ui.refactoring.utils.SelectionHelper;
import org.eclipse.cdt.internal.ui.viewsupport.BasicElementLabels;

public class ExtractFunctionRefactoring extends CRefactoring {
	public static final String ID =
			"org.eclipse.cdt.internal.ui.refactoring.extractfunction.ExtractFunctionRefactoring"; //$NON-NLS-1$

	static final Integer NULL_INTEGER = Integer.valueOf(0);
	static final char[] ZERO= "0".toCharArray(); //$NON-NLS-1$

	private NodeContainer container;
	final ExtractFunctionInformation info;

	final Map<String, Integer> names;
	final Container<Integer> namesCounter;
	final Container<Integer> trailPos;
	private final Container<Integer> returnNumber;

	HashMap<String, Integer> nameTrail;

	private FunctionExtractor extractor;
	private INodeFactory nodeFactory;
	DefaultCodeFormatterOptions formattingOptions;

	private IIndex index;
	private IASTTranslationUnit ast;

	public ExtractFunctionRefactoring(ICElement element, ISelection selection, ICProject project) {
		super(element, selection, project);
		info = new ExtractFunctionInformation();
		name = Messages.ExtractFunctionRefactoring_ExtractFunction;
		names = new HashMap<String, Integer>();
		namesCounter = new Container<Integer>(NULL_INTEGER);
		trailPos = new Container<Integer>(NULL_INTEGER);
		returnNumber = new Container<Integer>(NULL_INTEGER);
		formattingOptions = new DefaultCodeFormatterOptions(project.getOptions(true));
	}

	@Override
	public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
			throws CoreException, OperationCanceledException {
		SubMonitor sm = SubMonitor.convert(pm, 10);

		try {
			RefactoringStatus status = super.checkInitialConditions(sm.newChild(8));
			if (status.hasError()) {
				return status;
			}

			index = getIndex();
			ast = getAST(tu, sm.newChild(1));
			nodeFactory = ast.getASTNodeFactory();
			container = findExtractableNodes();

			if (isProgressMonitorCanceled(sm, initStatus))
				return initStatus;

			if (container.isEmpty()) {
				initStatus.addFatalError(Messages.ExtractFunctionRefactoring_NoStmtSelected);
				return initStatus;
			}

			checkForNonExtractableStatements(container, initStatus);

			List<NameInformation> returnValueCandidates = container.getReturnValueCandidates();
			if (returnValueCandidates.size() > 1) {
				initStatus.addFatalError(Messages.ExtractFunctionRefactoring_TooManySelected);
				return initStatus;
			} else if (returnValueCandidates.size() == 1) {
				info.setMandatoryReturnVariable(returnValueCandidates.get(0));
			}

			info.setParameters(container.getParameterCandidates());
			initStatus.merge(checkParameterAndReturnTypes());
			if (initStatus.hasFatalError())
				return initStatus;

			extractor =	FunctionExtractor.createFor(container.getNodesToWrite());

			if (extractor.canChooseReturnValue() && info.getMandatoryReturnVariable() == null) {
				chooseReturnVariable();
			}

			IPreferencesService preferences = Platform.getPreferencesService();
			final boolean outFirst = preferences.getBoolean(CUIPlugin.PLUGIN_ID,
					PreferenceConstants.FUNCTION_OUTPUT_PARAMETERS_BEFORE_INPUT, false,
					PreferenceConstants.getPreferenceScopes(project.getProject()));
			info.sortParameters(outFirst);

			boolean isExtractExpression = container.getNodesToWrite().get(0) instanceof IASTExpression;
			info.setExtractExpression(isExtractExpression);

			info.setDeclarator(getDeclaration(container.getNodesToWrite().get(0)));
			MethodContext context =	NodeHelper.findMethodContext(container.getNodesToWrite().get(0),
					refactoringContext, sm.newChild(1));
			info.setMethodContext(context);
			return initStatus;
		} finally {
			sm.done();
		}
	}

	private void chooseReturnVariable() {
		NameInformation candidate = null;
		for (NameInformation param : info.getParameters()) {
			if (param.isOutput()) {
				IASTDeclarator declarator = param.getDeclarator();
				IType type = TypeHelper.createType(declarator);
				type = SemanticUtil.getNestedType(type, SemanticUtil.CVTYPE | SemanticUtil.TDEF);
				if (type instanceof IBasicType) {
					if (((IBasicType) type).getKind() == IBasicType.Kind.eBoolean) {
						param.setReturnValue(true);
						return;
					}
				}
				if (candidate == null &&
						!TypeHelper.shouldBePassedByReference(type, declarator.getTranslationUnit())) {
					candidate = param;
				}
			}
		}
		if (candidate != null)
			candidate.setReturnValue(true);
	}

	private void checkForNonExtractableStatements(NodeContainer container, RefactoringStatus status) {
		NonExtractableStatementFinder finder = new NonExtractableStatementFinder();
		for (IASTNode node : container.getNodesToWrite()) {
			node.accept(finder);
			if (finder.containsContinue()) {
				initStatus.addFatalError(Messages.ExtractFunctionRefactoring_Error_Continue);
				break;
			} else if (finder.containsBreak()) {
				initStatus.addFatalError(Messages.ExtractFunctionRefactoring_Error_Break);
				break;
			}
		}

		ReturnStatementFinder returnFinder = new ReturnStatementFinder();
		for (IASTNode node : container.getNodesToWrite()) {
			node.accept(returnFinder);
			if (returnFinder.containsReturn()) {
				initStatus.addFatalError(Messages.ExtractFunctionRefactoring_Error_Return);
				break;
			}
		}
	}

	private ICPPASTFunctionDeclarator getDeclaration(IASTNode node) {
		while (node != null && !(node instanceof IASTFunctionDefinition)) {
			node = node.getParent();
		}
		if (node != null) {
			IASTFunctionDeclarator declarator = ((IASTFunctionDefinition) node).getDeclarator();
			if (declarator instanceof ICPPASTFunctionDeclarator) {
				return (ICPPASTFunctionDeclarator) declarator;
			}
		}
		return null;
	}

	@Override
	public RefactoringStatus checkFinalConditions(IProgressMonitor pm, CheckConditionsContext checkContext)
			throws CoreException, OperationCanceledException {
		RefactoringStatus status = new RefactoringStatus();

		final IASTName methodName = new CPPASTName(info.getMethodName().toCharArray());
		MethodContext context = info.getMethodContext();

		if (context.getType() == ContextType.METHOD && !context.isInline()) {
			ICPPASTCompositeTypeSpecifier classDeclaration =
					(ICPPASTCompositeTypeSpecifier) context.getMethodDeclaration().getParent();
			IASTSimpleDeclaration methodDeclaration = getDeclaration(methodName);

			if (isMethodAllreadyDefined(methodDeclaration, classDeclaration, getIndex())) {
				status.addError(Messages.ExtractFunctionRefactoring_name_in_use);
			}
		}
		return status;
	}

	@Override
	protected void collectModifications(IProgressMonitor pm, ModificationCollector collector)
			throws CoreException, OperationCanceledException {
		final IASTName methodName = new CPPASTName(info.getMethodName().toCharArray());
		MethodContext context = info.getMethodContext();

		// Create declaration in class
		if (context.getType() == ContextType.METHOD && !context.isInline()) {
			createMethodDeclaration(methodName, context, collector);
		}
		// Create method definition
		IASTNode firstNode = container.getNodesToWrite().get(0);
		createMethodDefinition(methodName, context, firstNode, collector);

		createMethodCalls(methodName, context, collector);
	}

	private void createMethodCalls(IASTName methodName, MethodContext context,
			ModificationCollector collector) throws CoreException {
		String title;
		if (context.getType() == MethodContext.ContextType.METHOD) {
			title = Messages.ExtractFunctionRefactoring_CreateMethodCall;
		} else {
			title = Messages.ExtractFunctionRefactoring_CreateFunctionCall;
		}

		IASTNode methodCall = getMethodCall(methodName);

		IASTNode firstNodeToWrite = container.getNodesToWrite().get(0);
		ASTRewrite rewriter = collector.rewriterForTranslationUnit(
				firstNodeToWrite.getTranslationUnit());
		TextEditGroup editGroup = new TextEditGroup(title);
		if (methodCall instanceof IASTDeclaration) {
			CPPASTDeclarationStatement declarationStatement =
					new CPPASTDeclarationStatement((IASTDeclaration) methodCall);
			methodCall = declarationStatement;
		}
		insertCallIntoTree(methodCall, container.getNodesToWrite(), rewriter, editGroup);

		if (info.isReplaceDuplicates()) {
			replaceSimilar(collector, methodName);
		}

		for (IASTNode node : container.getNodesToWrite()) {
			if (node != firstNodeToWrite) {
				rewriter.remove(node, editGroup);
			}
		}
	}

	private void insertCallIntoTree(IASTNode methodCall, List<IASTNode> list, ASTRewrite rewriter,
			TextEditGroup editGroup) {
		IASTNode firstNode = list.get(0);
		if (list.size() > 1 && firstNode.getParent() instanceof IASTBinaryExpression &&
				firstNode.getParent().getParent() instanceof IASTBinaryExpression) {
			IASTBinaryExpression parent = (IASTBinaryExpression) firstNode.getParent();
			IASTExpression leftSubTree = parent.getOperand1();
			int op = parent.getOperator();
			IASTBinaryExpression newParentNode = new CPPASTBinaryExpression();
			IASTBinaryExpression rootBinExp = getRootBinExp(parent, list);
			newParentNode.setParent(rootBinExp.getParent());
			newParentNode.setOperand1(leftSubTree.copy(CopyStyle.withLocations));
			newParentNode.setOperator(op);
			newParentNode.setOperand2((IASTExpression) methodCall);
			rewriter.replace(rootBinExp, newParentNode, editGroup);
		} else {
			rewriter.replace(firstNode, methodCall, editGroup);
		}
	}

	private IASTBinaryExpression getRootBinExp(IASTBinaryExpression binExp, List<IASTNode> nodeList) {
		while (binExp.getParent() instanceof IASTBinaryExpression &&
				nodeList.contains(((IASTBinaryExpression) binExp.getParent()).getOperand2())) {
			binExp = (IASTBinaryExpression) binExp.getParent();
		}
		return binExp;
	}

	private void createMethodDefinition(final IASTName methodName, MethodContext context,
			IASTNode firstExtractedNode, ModificationCollector collector) {
		IASTFunctionDefinition functionToExtractFrom =
				CPPVisitor.findAncestorWithType(firstExtractedNode, IASTFunctionDefinition.class);
		if (functionToExtractFrom != null) {
			String title;
			if (context.getType() == MethodContext.ContextType.METHOD) {
				title = Messages.ExtractFunctionRefactoring_CreateMethodDef;
			} else {
				title = Messages.ExtractFunctionRefactoring_CreateFunctionDef;
			}

			ASTRewrite rewriter = collector.rewriterForTranslationUnit(functionToExtractFrom.getTranslationUnit());
			addMethod(methodName, context, rewriter, functionToExtractFrom, new TextEditGroup(title));
		}
	}

	private void createMethodDeclaration(final IASTName astMethodName, MethodContext context,
			ModificationCollector collector) {
		ICPPASTCompositeTypeSpecifier classDeclaration =
				(ICPPASTCompositeTypeSpecifier) context.getMethodDeclaration().getParent();

		IASTSimpleDeclaration methodDeclaration = getDeclaration(collector, astMethodName);

		ClassMemberInserter.createChange(classDeclaration, info.getVisibility(),
				methodDeclaration, false, collector);
	}

	private void replaceSimilar(ModificationCollector collector, IASTName methodName) {
		// Find similar code
		final List<IASTNode> nodesToRewriteWithoutComments = getNodesWithoutComments(container.getNodesToWrite());
		final List<IASTNode> initTrail = getTrail(nodesToRewriteWithoutComments);
		IASTTranslationUnit ast = nodesToRewriteWithoutComments.get(0).getTranslationUnit();
		ast.accept(new SimilarReplacerVisitor(this, container, collector, initTrail, methodName,
				nodesToRewriteWithoutComments));
	}

	public int getNumberOfDuplicates() {
		final List<IASTNode> nodesToRewriteWithoutComments = getNodesWithoutComments(container.getNodesToWrite());
		final List<IASTNode> initTrail = getTrail(nodesToRewriteWithoutComments);
		final int[] count = new int[1];
		IASTTranslationUnit ast = nodesToRewriteWithoutComments.get(0).getTranslationUnit();
		ast.accept(new SimilarFinderVisitor(this, container, initTrail, nodesToRewriteWithoutComments) {
			@Override
			protected void foundSimilar() {
				++count[0];
			}
		});
		return count[0];
	}

	private List<IASTNode> getNodesWithoutComments(List<IASTNode> nodes) {
		final List<IASTNode> nodesWithoutComments = new ArrayList<IASTNode>(nodes.size());

		for (IASTNode node : nodes) {
			if (!(node instanceof IASTComment)) {
				nodesWithoutComments.add(node);
			}
		}
		return nodesWithoutComments;
	}

	private List<IASTNode> getTrail(List<IASTNode> stmts) {
		final List<IASTNode> trail = new ArrayList<IASTNode>();
		nameTrail = new HashMap<String, Integer>();
		final Container<Integer> trailCounter = new Container<Integer>(NULL_INTEGER);

		for (IASTNode node : stmts) {
			node.accept(new CPPASTAllVisitor() {
				@Override
				public int visitAll(IASTNode node) {
					if (node instanceof IASTComment) {
						// Visit comments, but don't add them to the trail
						return super.visitAll(node);
					} else if (node instanceof IASTNamedTypeSpecifier) {
						// Skip if somewhere is a named type specifier
						trail.add(node);
						return PROCESS_SKIP;
					} else if (node instanceof IASTName) {
						if (node instanceof ICPPASTConversionName &&
								node instanceof ICPPASTOperatorName	&&
								node instanceof ICPPASTTemplateId) {
							trail.add(node);
							return super.visitAll(node);
						} else {
							// Save name sequence number
							IASTName name = (IASTName) node;
							TrailName trailName = new TrailName(name);
							int actCount = trailCounter.getObject().intValue();
							if (nameTrail.containsKey(name.getRawSignature())) {
								Integer value = nameTrail.get(name.getRawSignature());
								actCount = value.intValue();
							} else {
								trailCounter.setObject(Integer.valueOf(++actCount));
								nameTrail.put(name.getRawSignature(), trailCounter.getObject());
							}
							trailName.setNameNumber(actCount);

							if (info.getReturnVariable() != null &&
									info.getReturnVariable().getName().getRawSignature().equals(
											name.getRawSignature())) {
								returnNumber.setObject(Integer.valueOf(actCount));
							}

							trail.add(trailName);
							return PROCESS_SKIP;
						}
					} else {
						trail.add(node);
						return super.visitAll(node);
					}
				}
			});
		}

		return trail;
	}

	boolean isStatementInTrail(IASTStatement stmt, final List<IASTNode> trail) {
		final Container<Boolean> same = new Container<Boolean>(Boolean.TRUE);
		final TrailNodeEqualityChecker equalityChecker =
				new TrailNodeEqualityChecker(names, namesCounter, index);

		stmt.accept(new CPPASTAllVisitor() {
			@Override
			public int visitAll(IASTNode node) {
				int pos = trailPos.getObject().intValue();

				if (trail.size() <= 0 || pos >= trail.size()) {
					same.setObject(Boolean.FALSE);
					return PROCESS_ABORT;
				}

				if (node instanceof IASTComment) {
					// Visit comments, but they are not in the trail
					return super.visitAll(node);
				}

				IASTNode trailNode = trail.get(pos);
				trailPos.setObject(Integer.valueOf(pos + 1));

				if (equalityChecker.isEqual(trailNode, node)) {
					if (node instanceof ICPPASTQualifiedName || node instanceof IASTNamedTypeSpecifier) {
						return PROCESS_SKIP;
					} else {
						return super.visitAll(node);
					}
				} else {
					same.setObject(new Boolean(false));
					return PROCESS_ABORT;
				}
			}
		});

		return same.getObject().booleanValue();
	}

	private boolean isMethodAllreadyDefined(IASTSimpleDeclaration methodDeclaration,
			ICPPASTCompositeTypeSpecifier classDeclaration, IIndex index) {
		TrailNodeEqualityChecker equalityChecker =
				new TrailNodeEqualityChecker(names, namesCounter, index);

		IBinding bind = classDeclaration.getName().resolveBinding();
		IASTStandardFunctionDeclarator declarator =
				(IASTStandardFunctionDeclarator) methodDeclaration.getDeclarators()[0];
		String name = new String(declarator.getName().toCharArray());
		if (bind instanceof ICPPClassType) {
			ICPPClassType classBind = (ICPPClassType) bind;
			IField[] fields = classBind.getFields();
			for (IField field : fields) {
				if (field.getName().equals(name)) {
					return true;
				}
			}
			ICPPMethod[] methods = classBind.getAllDeclaredMethods();
			for (ICPPMethod method : methods) {
				if (!method.takesVarArgs() && name.equals(method.getName())) {
					IParameter[] parameters = method.getParameters();
					if (parameters.length == declarator.getParameters().length) {
						for (int i = 0; i < parameters.length; i++) {
							IASTName[] origParameterName = ast.getDeclarationsInAST(parameters[i]);
							IASTParameterDeclaration origParameter =
									(IASTParameterDeclaration) origParameterName[0].getParent().getParent();
							IASTParameterDeclaration newParameter = declarator.getParameters()[i];

							// If not the same break;
							if (!(equalityChecker.isEqual(origParameter.getDeclSpecifier(),
									newParameter.getDeclSpecifier()) &&
									ASTHelper.samePointers(origParameter.getDeclarator().getPointerOperators(),
											newParameter.getDeclarator().getPointerOperators(),
											equalityChecker))) {
								break;
							}

							if (!(i < (parameters.length - 1))) {
								return true;
							}
						}
					}
				}
			}
			return false;
		}
		return true;
	}

	private void addMethod(IASTName methodName, MethodContext context, ASTRewrite rewrite,
			IASTNode functionToExtractFrom, TextEditGroup group) {
		ICPPASTQualifiedName qname = new CPPASTQualifiedName();
		if (context.getType() == ContextType.METHOD) {
			if (context.getMethodQName() != null) {
				for (int i = 0; i < context.getMethodQName().getNames().length - 1; i++) {
					qname.addName(new CPPASTName(context.getMethodQName().getNames()[i].toCharArray()));
				}
			}
		}
		qname.addName(methodName);

		IASTFunctionDefinition func = new CPPASTFunctionDefinition();
		func.setParent(ast);

		IASTDeclSpecifier returnType = getReturnType();
		func.setDeclSpecifier(returnType);

		IASTStandardFunctionDeclarator createdFunctionDeclarator =
				extractor.createFunctionDeclarator(qname,
						info.getDeclarator(), info.getReturnVariable(), container.getNodesToWrite(),
						info.getParameters(), nodeFactory);
		func.setDeclarator(createdFunctionDeclarator);

		IASTCompoundStatement compound = new CPPASTCompoundStatement();
		func.setBody(compound);

		ASTRewrite subRewrite;
		IASTNode parent = functionToExtractFrom.getParent();
		IASTNode nodeToInsert = func;
		if (parent instanceof ICPPASTTemplateDeclaration) {
			ICPPASTTemplateDeclaration parentTemplate = (ICPPASTTemplateDeclaration) parent;
			CPPASTTemplateDeclaration templateDeclaration = new CPPASTTemplateDeclaration();
			templateDeclaration.setParent(ast);

			for (ICPPASTTemplateParameter param : parentTemplate.getTemplateParameters()) {
				templateDeclaration.addTemplateParameter(param.copy(CopyStyle.withLocations));
			}

			functionToExtractFrom = parentTemplate;
			templateDeclaration.setDeclaration(func);
			nodeToInsert = templateDeclaration;
			parent = parent.getParent();
		}

		InsertionInfo insertion;
		if (parent instanceof ICPPASTCompositeTypeSpecifier) {
			// Inserting into a class declaration
			insertion = ClassMemberInserter.findInsertionPoint((ICPPASTCompositeTypeSpecifier) parent,
					info.getVisibility(), false);
		} else {
			// Inserting into a translation unit or a namespace.
			// TODO(sprigogin): Use insertBeforeNode instead of functionToExtractFrom when creating InsertionInfo
//			IASTNode insertBeforeNode = info.getMethodContext().getType() == ContextType.METHOD ?
//					null : functionToExtractFrom;
			insertion = new InsertionInfo(parent, functionToExtractFrom);
		}
		if (insertion.getPrologue() != null) {
			rewrite.insertBefore(insertion.getParentNode(),	insertion.getInsertBeforeNode(),
					insertion.getPrologue(), group);
		}
		subRewrite = rewrite.insertBefore(insertion.getParentNode(),
				insertion.getInsertBeforeNode(), nodeToInsert, group);
		if (insertion.getEpilogue() != null) {
			rewrite.insertBefore(insertion.getParentNode(),
					insertion.getInsertBeforeNode(), insertion.getEpilogue(), group);
		}

		extractor.constructMethodBody(compound, container.getNodesToWrite(),
				info.getParameters(), subRewrite, group);

		// Set return value
		NameInformation returnVariable = info.getReturnVariable();
		if (returnVariable != null) {
			IASTReturnStatement returnStmt = new CPPASTReturnStatement();
			IASTIdExpression expr = new CPPASTIdExpression();
			if (returnVariable.getNewName() == null) {
				expr.setName(newName(returnVariable.getName()));
			} else {
				expr.setName(new CPPASTName(returnVariable.getNewName().toCharArray()));
			}
			returnStmt.setReturnValue(expr);
			subRewrite.insertBefore(compound, null, returnStmt, group);
		}
	}

	private IASTName newName(IASTName declaration) {
		return new CPPASTName(declaration.toCharArray());
	}

	private IASTDeclSpecifier getReturnType() {
		IASTNode firstNodeToWrite = container.getNodesToWrite().get(0);
		NameInformation returnVariable = info.getReturnVariable();
		return extractor.determineReturnType(firstNodeToWrite,
				returnVariable);
	}

	protected IASTNode getMethodCall(IASTName astMethodName, Map<String, Integer> trailNameTable,
			Map<String, Integer> similarNameTable, NodeContainer myContainer,
			NodeContainer mySimilarContainer) {
		IASTExpressionStatement stmt = new CPPASTExpressionStatement();
		IASTFunctionCallExpression callExpression = new CPPASTFunctionCallExpression();
		IASTIdExpression idExpression = new CPPASTIdExpression();
		idExpression.setName(astMethodName);
		List<IASTInitializerClause> args = new ArrayList<IASTInitializerClause>();

		Set<IASTName> declarations = new HashSet<IASTName>();
		IASTName retName = null;
		boolean theRetName = false;

		for (NameInformation nameInfo : info.getParameters()) {
			Integer trailSeqNumber = trailNameTable.get(nameInfo.getDeclarationName().getRawSignature());
			String origName = null;
			for (Entry<String, Integer> entry : similarNameTable.entrySet()) {
				if (entry.getValue().equals(trailSeqNumber)) {
					origName = entry.getKey();
					if (info.getReturnVariable() != null &&
							trailSeqNumber.equals(returnNumber.getObject())) {
						theRetName = true;
					}
				}
			}

			if (origName != null) {
				boolean found = false;
				for (NameInformation simNameInfo : mySimilarContainer.getNames()) {
					if (origName.equals(simNameInfo.getDeclarationName().getRawSignature())) {
						addParameterIfPossible(args, declarations, simNameInfo);
						found = true;

						if (theRetName) {
							theRetName = false;
							retName = new CPPASTName(
									simNameInfo.getDeclarationName().getRawSignature().toCharArray());
						}
					}
				}

				if (!found) {
					// should be a field, use the old name
					IASTIdExpression expression = new CPPASTIdExpression();
					CPPASTName fieldName = new CPPASTName(origName.toCharArray());
					expression.setName(fieldName);
					args.add(expression);

					if (theRetName) {
						theRetName = false;
						retName = fieldName;
					}
				}
			}
		}

		callExpression.setArguments(args.toArray(new IASTInitializerClause[args.size()]));
		callExpression.setFunctionNameExpression(idExpression);

		if (info.getReturnVariable() == null) {
			return getReturnAssignment(stmt, callExpression);
		}
		return getReturnAssignment(stmt, callExpression, retName);
	}

	private IASTNode getMethodCall(IASTName methodName) {
		IASTExpressionStatement statement = new CPPASTExpressionStatement();

		IASTFunctionCallExpression callExpression = new CPPASTFunctionCallExpression();
		IASTIdExpression idExpression = new CPPASTIdExpression();
		idExpression.setName(new CPPASTName(methodName.toCharArray()));
		List<IASTInitializerClause> args = getCallParameters();
		callExpression.setArguments(args.toArray(new IASTInitializerClause[args.size()]));
		callExpression.setFunctionNameExpression(idExpression);

		if (info.getReturnVariable() == null) {
			return getReturnAssignment(statement, callExpression);
		}
		IASTName retName = newName(info.getReturnVariable().getName());
		return getReturnAssignment(statement, callExpression, retName);
	}

	private IASTNode getReturnAssignment(IASTExpressionStatement stmt,
			IASTFunctionCallExpression callExpression, IASTName retname) {
		if (info.getReturnVariable().equals(info.getMandatoryReturnVariable())) {
			IASTSimpleDeclaration orgDecl = CPPVisitor.findAncestorWithType(info.getReturnVariable().getDeclarationName(), IASTSimpleDeclaration.class);
			IASTSimpleDeclaration decl = new CPPASTSimpleDeclaration();

			decl.setDeclSpecifier(orgDecl.getDeclSpecifier().copy(CopyStyle.withLocations));

			IASTDeclarator declarator = new CPPASTDeclarator();
			declarator.setName(retname);

			for (IASTPointerOperator pointer : orgDecl.getDeclarators()[0].getPointerOperators()) {
				declarator.addPointerOperator(pointer.copy(CopyStyle.withLocations));
			}

			IASTEqualsInitializer initializer = new CPPASTEqualsInitializer();
			initializer.setInitializerClause(callExpression);
			declarator.setInitializer(initializer);

			decl.addDeclarator(declarator);

			return decl;
		}
		IASTBinaryExpression binaryExpression = new CASTBinaryExpression();
		binaryExpression.setOperator(IASTBinaryExpression.op_assign);
		IASTIdExpression nameExpression = new CPPASTIdExpression();

		nameExpression.setName(retname);
		binaryExpression.setOperand1(nameExpression);
		binaryExpression.setOperand2(callExpression);

		return getReturnAssignment(stmt, binaryExpression);
	}

	private IASTNode getReturnAssignment(IASTExpressionStatement stmt,
			IASTExpression callExpression) {
		IASTNode node = container.getNodesToWrite().get(0);
		return extractor.createReturnAssignment(node, stmt, callExpression);
	}

	private IASTSimpleDeclaration getDeclaration(IASTName name) {
		IASTSimpleDeclaration simpleDecl = new CPPASTSimpleDeclaration();
		IASTStandardFunctionDeclarator declarator =
				extractor.createFunctionDeclarator(name,
						info.getDeclarator(), info.getReturnVariable(), container.getNodesToWrite(),
						info.getParameters(), nodeFactory);
		simpleDecl.addDeclarator(declarator);
		return simpleDecl;
	}

	private IASTSimpleDeclaration getDeclaration(ModificationCollector collector, IASTName name) {
		IASTDeclSpecifier declSpec = getReturnType();
		IASTSimpleDeclaration simpleDecl = nodeFactory.newSimpleDeclaration(declSpec);
		if (info.isVirtual() && declSpec instanceof ICPPASTDeclSpecifier) {
			((ICPPASTDeclSpecifier) declSpec).setVirtual(true);
		}
		simpleDecl.setParent(ast);
		IASTStandardFunctionDeclarator declarator =
				extractor.createFunctionDeclarator(name,
						info.getDeclarator(), info.getReturnVariable(), container.getNodesToWrite(),
						info.getParameters(), nodeFactory);
		simpleDecl.addDeclarator(declarator);
		return simpleDecl;
	}

	private NodeContainer findExtractableNodes() {
		final NodeContainer container = new NodeContainer();
		ast.accept(new ASTVisitor() {
			{
				shouldVisitStatements = true;
				shouldVisitExpressions = true;
			}

			@Override
			public int visit(IASTStatement stmt) {
				if (isNodeInsideSelection(stmt)) {
					container.add(stmt);
					return PROCESS_SKIP;
				}
				return super.visit(stmt);
			}

			@Override
			public int visit(IASTExpression expression) {
				if (isNodeInsideSelection(expression)) {
					container.add(expression);
					return PROCESS_SKIP;
				}
				return super.visit(expression);
			}
		});
		return container;
	}

	private boolean isNodeInsideSelection(IASTNode node) {
		return node.isPartOfTranslationUnitFile() && SelectionHelper.isNodeInsideRegion(node, selectedRegion);
	}

	public List<IASTInitializerClause> getCallParameters() {
		List<IASTInitializerClause> args = new ArrayList<IASTInitializerClause>();
		Set<IASTName> declarations = new HashSet<IASTName>();
		for (NameInformation nameInfo : info.getParameters()) {
			addParameterIfPossible(args, declarations, nameInfo);
		}
		return args;
	}

	private void addParameterIfPossible(List<IASTInitializerClause> args, Set<IASTName> declarations,
			NameInformation nameInfo) {
		if (!container.isDeclaredInSelection(nameInfo)) {
			IASTName declaration = nameInfo.getDeclarationName();
			if (declarations.add(declaration)) {
				IASTExpression expression = nodeFactory.newIdExpression(newName(declaration));
				if (nameInfo.getIndirection() == Indirection.POINTER) {
					expression = nodeFactory.newUnaryExpression(IASTUnaryExpression.op_amper, expression);
				}
				args.add(expression);
			}
		}
	}

	@Override
	protected RefactoringDescriptor getRefactoringDescriptor() {
		Map<String, String> arguments = getArgumentMap();
		RefactoringDescriptor desc =
				new ExtractFunctionRefactoringDescriptor(project.getProject().getName(),
						"Extract Method Refactoring", "Create method " + info.getMethodName(), arguments);  //$NON-NLS-1$//$NON-NLS-2$
		return desc;
	}

	private Map<String, String> getArgumentMap() {
		Map<String, String> arguments = new HashMap<String, String>();
		arguments.put(CRefactoringDescriptor.FILE_NAME, tu.getLocationURI().toString());
		arguments.put(CRefactoringDescriptor.SELECTION,
				selectedRegion.getOffset() + "," + selectedRegion.getLength()); //$NON-NLS-1$
		arguments.put(ExtractFunctionRefactoringDescriptor.NAME, info.getMethodName());
		arguments.put(ExtractFunctionRefactoringDescriptor.VISIBILITY,
				info.getVisibility().toString());
		arguments.put(ExtractFunctionRefactoringDescriptor.REPLACE_DUPLICATES,
				Boolean.toString(info.isReplaceDuplicates()));
		return arguments;
	}

	public ExtractFunctionInformation getRefactoringInfo() {
		return info;
	}

	/**
	 * Checks if the new method name is a valid method name. This method doesn't
	 * check if a method with the same name already exists in the hierarchy.
	 * @return validation status
	 */
	public RefactoringStatus checkMethodName() {
		return Checks.checkIdentifier(info.getMethodName(), tu);
	}

	/**
	 * Checks if the parameter names are valid.
	 * @return validation status
	 */
	public RefactoringStatus checkParameterNames() {
		RefactoringStatus result= new RefactoringStatus();
		List<NameInformation> parameters = info.getParameters();
		Set<String> usedNames = new HashSet<String>();
		Set<IASTName> declarations = new HashSet<IASTName>();
		for (NameInformation nameInfo : container.getNames()) {
			IASTName declaration = nameInfo.getDeclarationName();
			if (declarations.add(declaration) && !parameters.contains(nameInfo)) {
				usedNames.add(String.valueOf(nameInfo.getName().getSimpleID()));
			}
		}
		for (NameInformation parameter : parameters) {
			result.merge(Checks.checkIdentifier(parameter.getNewName(), tu));
			for (NameInformation other : parameters) {
				if (parameter != other && other.getNewName().equals(parameter.getNewName())) {
					result.addError(NLS.bind(Messages.ExtractFunctionRefactoring_duplicate_parameter,
							BasicElementLabels.getCElementName(other.getNewName())));
					return result;
				}
			}
			if (parameter.isRenamed() && usedNames.contains(parameter.getNewName())) {
				result.addError(NLS.bind(Messages.ExtractFunctionRefactoring_parameter_name_in_use,
						BasicElementLabels.getCElementName(parameter.getNewName())));
				return result;
			}
		}
		return result;
	}

	/**
	 * Checks if the parameter names are valid.
	 * @return validation status
	 */
	public RefactoringStatus checkParameterAndReturnTypes() {
		RefactoringStatus result= new RefactoringStatus();
		for (NameInformation parameter : info.getParameters()) {
			String typeName = parameter.getTypeName();
			if (typeName == null || typeName.isEmpty()) {
				result.addError(NLS.bind(Messages.ExtractFunctionRefactoring_invalid_type,
						BasicElementLabels.getCElementName(parameter.getNewName())));
				return result;
			}
		}
		return result;
	}

	/**
	 * Returns the signature of the new method.
	 *
	 * @param methodName the method name used for the new method
	 * @return the signature of the extracted method
	 */
	public String getSignature(String methodName) {
		StringBuilder buf = new StringBuilder();
		NameInformation returnVariable = info.getReturnVariable();
		if (returnVariable != null) {
			String type = returnVariable.getReturnType();
			if (type != null) {
				buf.append(type);
			} else {
				buf.append("<unknown type>"); //$NON-NLS-1$
			}
		} else {
			buf.append("void"); //$NON-NLS-1$
		}
		buf.append(' ');
		buf.append(methodName);
		if (formattingOptions.insert_space_before_opening_paren_in_method_declaration)
			buf.append(' ');
		buf.append('(');
		List<NameInformation> parameters = info.getParameters();
		if (!parameters.isEmpty()) {
			if (formattingOptions.insert_space_after_opening_paren_in_method_declaration)
				buf.append(' ');
			boolean first = true;
			for (NameInformation parameter : parameters) {
				if (!first) {
					if (formattingOptions.insert_space_before_comma_in_method_declaration_parameters)
						buf.append(' ');
					buf.append(',');
					if (formattingOptions.insert_space_after_comma_in_method_declaration_parameters)
						buf.append(' ');
				}
				IASTParameterDeclaration declaration = parameter.getParameterDeclaration(nodeFactory);
				ASTWriterVisitor writer = new ASTWriterVisitor();
				declaration.accept(writer);
				buf.append(writer.toString());
				first = false;
			}
			if (formattingOptions.insert_space_before_closing_paren_in_method_declaration)
				buf.append(' ');
		} else if (formattingOptions.insert_space_between_empty_parens_in_method_declaration) {
			buf.append(' ');
		}
		buf.append(')');
		return buf.toString();
	}
}

Back to the top