1
/*******************************************************************************
2
* Copyright (c) 2004, 2008 Wind River Systems, Inc. and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Markus Schorn - initial API and implementation
10
******************************************************************************/
11
package org.eclipse.cdt.internal.ui.refactoring.rename;
13
import org.eclipse.core.resources.IFile;
15
import org.eclipse.cdt.core.dom.ast.DOMException;
16
import org.eclipse.cdt.core.dom.ast.IASTName;
17
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
18
import org.eclipse.cdt.core.dom.ast.IBinding;
19
import org.eclipse.cdt.core.dom.ast.ICompositeType;
20
import org.eclipse.cdt.core.dom.ast.IEnumeration;
21
import org.eclipse.cdt.core.dom.ast.IEnumerator;
22
import org.eclipse.cdt.core.dom.ast.IField;
23
import org.eclipse.cdt.core.dom.ast.IFunction;
24
import org.eclipse.cdt.core.dom.ast.IMacroBinding;
25
import org.eclipse.cdt.core.dom.ast.IParameter;
26
import org.eclipse.cdt.core.dom.ast.IScope;
27
import org.eclipse.cdt.core.dom.ast.ITypedef;
28
import org.eclipse.cdt.core.dom.ast.IVariable;
29
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
30
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace;
31
import org.eclipse.cdt.core.model.CModelException;
32
import org.eclipse.cdt.core.model.ICElement;
33
import org.eclipse.cdt.core.model.ISourceRange;
34
import org.eclipse.cdt.core.model.ISourceReference;
35
import org.eclipse.cdt.ui.CUIPlugin;
39
* Represents the input to a refactoring. Important is file and offset the rest
40
* can be calculated from the AST.
42
public class CRefactoringArgument {
43
private int fOffset=0;
44
private int fLength= 0;
45
private String fText= ""; //$NON-NLS-1$
46
private int fKind= CRefactory.ARGUMENT_UNKNOWN;
49
private IBinding fBinding;
50
private IScope fScope;
51
private IASTTranslationUnit fTranslationUnit;
53
public CRefactoringArgument(IFile file, int offset, int length) {
54
fKind= CRefactory.ARGUMENT_UNKNOWN;
60
public CRefactoringArgument(ICElement elem) {
61
fKind= CRefactory.ARGUMENT_UNKNOWN;
62
if (elem instanceof ISourceReference) {
63
ISourceReference sref= (ISourceReference) elem;
66
sr = sref.getSourceRange();
67
fFile= (IFile) sref.getTranslationUnit().getResource();
68
fOffset= sr.getIdStartPos();
69
fLength= sr.getIdLength();
70
} catch (CModelException e) {
77
public String getName() {
82
public IFile getSourceFile() {
87
public int getArgumentKind() {
92
public int getOffset() {
96
public int getLength() {
100
public void setName(IASTName name) {
101
fText= name.toString();
104
public void setBinding(IASTTranslationUnit tu, IBinding binding, IScope scope) {
105
fTranslationUnit= tu;
108
if (binding instanceof IVariable) {
109
IVariable var= (IVariable) binding;
110
if (binding instanceof IField) {
111
fKind= CRefactory.ARGUMENT_FIELD;
113
else if (binding instanceof IParameter) {
114
fKind= CRefactory.ARGUMENT_PARAMETER;
117
if (ASTManager.isLocalVariable(var, scope)) {
118
fKind= CRefactory.ARGUMENT_LOCAL_VAR;
121
boolean isStatic= false;
123
isStatic= var.isStatic();
124
} catch (DOMException e) {
127
fKind= CRefactory.ARGUMENT_FILE_LOCAL_VAR;
130
fKind= CRefactory.ARGUMENT_GLOBAL_VAR;
135
else if (binding instanceof IEnumerator) {
136
fKind= CRefactory.ARGUMENT_ENUMERATOR;
138
else if (binding instanceof IFunction) {
139
fKind= CRefactory.ARGUMENT_NON_VIRTUAL_METHOD;
140
IFunction func= (IFunction) binding;
141
if (binding instanceof ICPPMethod) {
142
ICPPMethod method= (ICPPMethod) binding;
143
int isVirtual= ASTManager.UNKNOWN;
145
isVirtual = ASTManager.isVirtualMethod(method);
146
} catch (DOMException e) {
148
if (isVirtual == ASTManager.TRUE) {
149
fKind= CRefactory.ARGUMENT_VIRTUAL_METHOD;
153
boolean isStatic= false;
155
isStatic= func.isStatic();
156
} catch (DOMException e) {
159
fKind= CRefactory.ARGUMENT_FILE_LOCAL_FUNCTION;
162
fKind= CRefactory.ARGUMENT_GLOBAL_FUNCTION;
166
else if (binding instanceof ICompositeType) {
167
fKind= CRefactory.ARGUMENT_CLASS_TYPE;
169
else if (binding instanceof IEnumeration || binding instanceof ITypedef) {
170
fKind= CRefactory.ARGUMENT_TYPE;
172
else if (binding instanceof ICPPNamespace) {
173
fKind= CRefactory.ARGUMENT_NAMESPACE;
175
else if (binding instanceof IMacroBinding) {
176
fKind= CRefactory.ARGUMENT_MACRO;
180
public IScope getScope() {
184
public IBinding getBinding() {
188
public IASTTranslationUnit getTranslationUnit() {
189
return fTranslationUnit;