Skip to main content
summaryrefslogtreecommitdiffstats
blob: 963ac988f12a9251f367daf31feeaad598474fc1 (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
/*******************************************************************************
 * Copyright (c) 2004, 2014 IBM Corporation 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:
 *     Andrew Niefer (IBM Corporation) - initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *     Nathan Ridge
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp;

import org.eclipse.cdt.core.dom.ILinkage;
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.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.IValue;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPParameterPackType;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.Linkage;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.ASTQueries;
import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding;
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.core.runtime.PlatformObject;

/**
 * Binding for a c++ function parameter.
 */
public class CPPParameter extends PlatformObject implements ICPPParameter, ICPPInternalBinding, ICPPTwoPhaseBinding {

    public static class CPPParameterProblem extends ProblemBinding implements ICPPParameter {
        public CPPParameterProblem(IASTNode node, int id, char[] arg) {
            super(node, id, arg);
        }
    }

	private IType fType;
	private IASTName[] fDeclarations;
	private int fPosition;

	public CPPParameter(IASTName name, int pos) {
		this.fDeclarations = new IASTName[] { name };
		fPosition= pos;
	}
	
	public CPPParameter(IType type, int pos) {
	    this.fType = type;
	    fPosition= pos;
	}

    @Override
	public boolean isParameterPack() {
		return getType() instanceof ICPPParameterPackType;
	}

    @Override
	public IASTNode[] getDeclarations() {
        return fDeclarations;
    }

    @Override
	public IASTNode getDefinition() {
        return null;
    }

	@Override
	public void addDeclaration(IASTNode node) {
		if (!(node instanceof IASTName))
			return;
		IASTName name = (IASTName) node;
		if (fDeclarations == null || fDeclarations.length == 0) {
	        fDeclarations = new IASTName[] { name };
		} else {
	        if (isDeclaredBefore((ASTNode) node, (ASTNode) fDeclarations[0])) {
				fDeclarations = ArrayUtil.prepend(IASTName.class, fDeclarations, name);
			} else {
				fDeclarations = ArrayUtil.append(IASTName.class, fDeclarations, name);
			}
	    }
	}
	
	private boolean isDeclaredBefore(ASTNode n1, ASTNode n2) {
		if (n1.getLength() == 0)
			return false;
		if (n2.getLength() == 0)
			return true;
		return n1.getOffset() < n2.getOffset();
	}

	private IASTName getPrimaryDeclaration() {
	    if (fDeclarations != null) {
	        for (int i = 0; i < fDeclarations.length && fDeclarations[i] != null; i++) {
	            IASTNode node = fDeclarations[i].getParent();
	            while (!(node instanceof IASTDeclaration))
	                node = node.getParent();
	            
	            if (node instanceof IASTFunctionDefinition)
	                return fDeclarations[i];
	        }
	        return fDeclarations[0];
	    }
	    return null;
	}

	@Override
	public String getName() {
		return new String(getNameCharArray());
	}

	@Override
	public char[] getNameCharArray() {
	    IASTName name = getPrimaryDeclaration();
	    if (name != null)
	        return name.getSimpleID();
	    return CharArrayUtils.EMPTY;
	}

	@Override
	public IScope getScope() {
		return CPPVisitor.getContainingScope(getPrimaryDeclaration());
	}

	public IASTNode getPhysicalNode() {
	    if (fDeclarations != null)
	        return fDeclarations[0];
		return null;
	}

	@Override
	public IType getType() {
		if (fType == null && fDeclarations != null) {
			IASTNode parent= fDeclarations[0].getParent();
			while (parent != null) {
				if (parent instanceof ICPPASTParameterDeclaration) {
					fType= CPPVisitor.createType((ICPPASTParameterDeclaration) parent, false);
					break;
				}
				parent= parent.getParent();
			}
		}
		return fType;
	}

    @Override
	public boolean isStatic() {
        return false;
    }

    @Override
	public String[] getQualifiedName() {
        return new String[] { getName() };
    }

    @Override
	public char[][] getQualifiedNameCharArray() {
        return new char[][] { getNameCharArray() };
    }

    @Override
	public boolean isGloballyQualified() {
        return false;
    }

	@Override
	public void addDefinition(IASTNode node) {
		addDeclaration(node);
	}

    @Override
	public boolean isExtern() {
        // 7.1.1-5 extern can not be used in the declaration of a parameter
        return false;
    }

    @Override
	public boolean isMutable() {
        // 7.1.1-8 mutable can only apply to class members
        return false;
    }

    @Override
	public boolean isAuto() {
        return hasStorageClass(IASTDeclSpecifier.sc_auto);
    }

    @Override
	public boolean isRegister() {
        return hasStorageClass(IASTDeclSpecifier.sc_register);
    }
    
    public boolean hasStorageClass(int storage) {
		IASTNode[] ns = getDeclarations();
		if (ns == null)
			return false;

		for (int i = 0; i < ns.length && ns[i] != null; i++) {
			IASTNode parent = ns[i].getParent();
			while (parent != null && !(parent instanceof IASTParameterDeclaration))
				parent = parent.getParent();
			if (parent != null) {
				IASTDeclSpecifier declSpec = ((IASTParameterDeclaration) parent).getDeclSpecifier();
				if (declSpec.getStorageClass() == storage)
					return true;
			}
		}
		return false;
	}

	public IASTInitializer getInitializer() {
		if (fDeclarations == null)
			return null;
		for (int i = 0; i < fDeclarations.length && fDeclarations[i] != null; i++) {
			IASTNode parent = fDeclarations[i].getParent();
			while (parent.getPropertyInParent() == IASTDeclarator.NESTED_DECLARATOR)
				parent = parent.getParent();
			IASTInitializer init = ((IASTDeclarator) parent).getInitializer();
			if (init != null)
				return init;
		}
		return null;
	}
	
	@Override
	public boolean hasDefaultValue() {
		return getInitializer() != null;
	}
	
	@Override
	public IValue getDefaultValue() {
		IASTInitializer init = getInitializer();
		if (init != null) {
			return SemanticUtil.getValueOfInitializer(init, getType());
		}
		return null;
	}
	
	@Override
	public ILinkage getLinkage() {
		return Linkage.CPP_LINKAGE;
	}

	@Override
	public boolean isExternC() {
		return false;
	}
	
	@Override
	public String toString() {
		String name = getName();
		return name.length() != 0 ? name : "<unnamed>"; //$NON-NLS-1$
	}
	
	@Override
	public IBinding getOwner() {
		IASTFunctionDeclarator decl =
				CPPVisitor.findAncestorWithType(fDeclarations[0], IASTFunctionDeclarator.class);
		if (decl == null)
			return null;
		IASTName name= decl.getName();
		return name != null ? name.resolveBinding() : null;
	}

	@Override
	public IValue getInitialValue() {
		return null;
	}

	@Override
	public IBinding resolveFinalBinding(CPPASTNameBase name) {
		// Check if the binding has been updated.
		IBinding current= name.getPreBinding();
		if (current != this)
			return current;
		
		IASTNode node= getPrimaryDeclaration();
		while (node != null && !(node instanceof IASTFunctionDeclarator)) {
			node= node.getParent();
		}
		if (node instanceof IASTFunctionDeclarator) {
			IASTName funcName= ASTQueries.findInnermostDeclarator((IASTFunctionDeclarator) node).getName();
			IBinding b= funcName.resolvePreBinding();
			if (b instanceof ICPPInternalFunction) {
				return ((ICPPInternalFunction) b).resolveParameter(this);
			}
		}
		return this;
	}

	public int getParameterPosition() {
		return fPosition;
	}
}

Back to the top