Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c810cc7d8cd25828893fb2bc4a3a6043c4d809ef (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
/*******************************************************************************
 * Copyright (c) 2008, 2012 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
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.extractfunction;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
import org.eclipse.cdt.internal.ui.refactoring.MethodContext;
import org.eclipse.cdt.internal.ui.refactoring.NameInformation;
import org.eclipse.cdt.internal.ui.refactoring.utils.VisibilityEnum;

public class ExtractFunctionInformation {
	private VisibilityEnum visibility = VisibilityEnum.v_private;
	private String methodName;
	private boolean replaceDuplicates;
	private List<NameInformation> parameters;
	private NameInformation mandatoryReturnVariable;
	private ICPPASTFunctionDeclarator declarator;
	private MethodContext context;
	private boolean isExtractExpression;
	private boolean virtual;

	/**
	 * Returns the function declarator of the method or function from were the statements
	 * are extracted from.
	 * @return the function declarator or null
	 */
	public ICPPASTFunctionDeclarator getDeclarator() {
		return declarator;
	}

	public void setDeclarator(ICPPASTFunctionDeclarator declarator) {
		this.declarator = declarator;
	}

	public String getMethodName() {
		return methodName;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}

	public boolean isReplaceDuplicates() {
		return replaceDuplicates;
	}

	public void setReplaceDuplicates(boolean replaceDuplicates) {
		this.replaceDuplicates = replaceDuplicates;
	}

	public NameInformation getReturnVariable() {
		if (mandatoryReturnVariable != null)
			return mandatoryReturnVariable;
		for (NameInformation param : parameters) {
			if (param.isReturnValue())
				return param;
		}
		return null;
	}

	public NameInformation getMandatoryReturnVariable() {
		return mandatoryReturnVariable;
	}

	public void setMandatoryReturnVariable(NameInformation variable) {
		this.mandatoryReturnVariable = variable;
	}

	public List<NameInformation> getParameters() {
		return parameters;
	}

	public void setParameters(List<NameInformation> parameters) {
		this.parameters = new ArrayList<NameInformation>(parameters);
	}

	public VisibilityEnum getVisibility() {
		return visibility;
	}

	public void setVisibility(VisibilityEnum visibility) {
		this.visibility = visibility;
	}

	public MethodContext getMethodContext() {
		return context;
	}

	public void setMethodContext(MethodContext context) {
		this.context = context;
	}

	public boolean isExtractExpression() {
		return isExtractExpression;
	}

	public void setExtractExpression(boolean isExtractExpression) {
		this.isExtractExpression = isExtractExpression;
	}

	public boolean isVirtual() {
		return virtual;
	}

	public void setVirtual(boolean isVirtual) {
		this.virtual = isVirtual;
	}

	public void sortParameters(final boolean outFirst) {
		Collections.sort(parameters, new Comparator<NameInformation>() {
			@Override
			public int compare(NameInformation p1, NameInformation p2) {
				boolean out1 = p1.isOutputParameter() || hasNonConstPointerOrReference(p1);
				boolean out2 = p2.isOutputParameter() || hasNonConstPointerOrReference(p2);
				return out1 == out2 ? 0 : out1 == outFirst ? -1 : 1;
			}
		});
	}

	public static boolean hasNonConstPointerOrReference(NameInformation param) {
		IASTDeclarator declarator = param.getDeclarator();
		IASTPointerOperator[] operators = declarator.getPointerOperators();
		if (operators.length != 0) {
			IASTDeclSpecifier declSpecifier = param.getDeclSpecifier();
			return declSpecifier == null || !declSpecifier.isConst();
		}
		return false;
	}
}

Back to the top