Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e53e0ff548a93d4c234705becd1d5b7b7584f06e (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
/*******************************************************************************
 * Copyright (c) 2005, 2010 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) - Initial API and implementation
 *    Markus Schorn (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp;

import org.eclipse.cdt.core.dom.ILinkage;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplatedTypeTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter;
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.ProblemBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPTemplates;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.core.runtime.PlatformObject;

/**
 * Base implementation for template parameter bindings in the AST.
 */
public abstract class CPPTemplateParameter extends PlatformObject 
		implements ICPPTemplateParameter, ICPPInternalBinding, ICPPTwoPhaseBinding {
	private IASTName[] declarations;
	private final int fParameterID;
	
	public CPPTemplateParameter(IASTName name) {
		declarations = new IASTName[] {name};
		fParameterID= computeParameterID(name);
	}

	private int computeParameterID(IASTName name) {
		int nesting= 0;
		ICPPASTTemplateParameter tp= null;
		ICPPASTTemplateParameter[] tps= null;
		for (IASTNode node= name.getParent(); node != null; node= node.getParent()) {
			if (tp == null && node instanceof ICPPASTTemplateParameter) {
				tp= (ICPPASTTemplateParameter) node;
			} else if (node instanceof ICPPASTInternalTemplateDeclaration) {
				final ICPPASTInternalTemplateDeclaration tdecl= (ICPPASTInternalTemplateDeclaration) node;
				nesting+= tdecl.getNestingLevel();
				if (tps == null) {
					tps= tdecl.getTemplateParameters();
				}
				break;
			} else if (node instanceof ICPPASTTemplatedTypeTemplateParameter) {
				nesting++;
				if (tps == null) {
					tps= ((ICPPASTTemplatedTypeTemplateParameter) node).getTemplateParameters();
				}
			}
		}
		int pos= 0;
		if (tps != null && tp != null) {
			for (int i = 0; i < tps.length; i++) {
				if (tps[i] == tp) {
					pos= i;
					break;
				}
			}
		}

		return (nesting << 16) + (pos & 0xffff);
	}

	@Override
	public Object clone() {
        IType t = null;
   		try {
            t = (IType) super.clone();
        } catch (CloneNotSupportedException e) {
            //not going to happen
        }
        return t;
    }
	
	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.IBinding#getName()
	 */
	public final String getName() {
		return new String(getNameCharArray());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.IBinding#getNameCharArray()
	 */
	public final char[] getNameCharArray() {
		// Search for the first declaration that has a name.
		for (IASTName decl : declarations) {
			if (decl == null)
				break;
			
			final char[] result= decl.getSimpleID();
			if (result.length > 0)
				return result;
		}
		return CharArrayUtils.EMPTY;
	}

	public int getParameterID() {
		return fParameterID;
	}

	public short getParameterPosition() {
		return (short) fParameterID;
	}

	public short getTemplateNestingLevel() {
		return (short) (fParameterID >> 16);
	}

	public IASTName getPrimaryDeclaration () {
		return declarations[0];
	}
	
	private ICPPASTTemplateParameter getASTTemplateParameter() {
		IASTNode node= declarations[0];
		while (node != null && !(node instanceof ICPPASTTemplateParameter))
			node= node.getParent();
		assert node != null;
		return (ICPPASTTemplateParameter) node;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.IBinding#getScope()
	 */
	public IScope getScope() {
		return CPPVisitor.getContainingScope(getPrimaryDeclaration());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding#getQualifiedName()
	 */
	public String[] getQualifiedName() {
		return new String[] { getName() };
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding#getQualifiedNameCharArray()
	 */
	public char[][] getQualifiedNameCharArray() {
		return new char[][] {getNameCharArray() };
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding#isGloballyQualified()
	 */
	public boolean isGloballyQualified() {
		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#getDeclarations()
	 */
	public IASTName[] getDeclarations() {
		return declarations;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#getDefinition()
	 */
	public IASTNode getDefinition() {
		if (declarations != null && declarations.length > 0)
			return declarations[0];
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#addDefinition(org.eclipse.cdt.core.dom.ast.IASTNode)
	 */
	public void addDefinition(IASTNode node) {
		addDeclaration(node);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding#addDeclaration(org.eclipse.cdt.core.dom.ast.IASTNode)
	 */
	public void addDeclaration(IASTNode node) {
		if (!(node instanceof IASTName))
			return;
		IASTName name = (IASTName) node;
		if (declarations == null) {
	        declarations = new IASTName[] { name };
		} else {
	        if (declarations.length > 0 && declarations[0] == node)
	            return;
			// keep the lowest offset declaration in [0]
			if (declarations.length > 0 && ((ASTNode)node).getOffset() < ((ASTNode)declarations[0]).getOffset()) {
				declarations = (IASTName[]) ArrayUtil.prepend(IASTName.class, declarations, name);
			} else {
				declarations = (IASTName[]) ArrayUtil.append(IASTName.class, declarations, name);
			}
	    }
	}
	
	public ILinkage getLinkage() {
		return Linkage.CPP_LINKAGE;
	}
	
	@Override
	public String toString() {
		return getName();
	}	
	
	public IBinding getOwner() {
		if (declarations == null || declarations.length == 0)
			return null;
		
		IASTNode node= declarations[0];
		while (!(node instanceof ICPPASTTemplateParameter)) {
			if (node == null)
				return null;
			
			node= node.getParent();
		}
		
		return CPPTemplates.getContainingTemplate((ICPPASTTemplateParameter) node);
	}
	
	public IBinding resolveFinalBinding(CPPASTNameBase name) {
		// check if the binding has been updated.
		IBinding current= name.getPreBinding();
		if (current != this)
			return current;
		
		ICPPTemplateDefinition template= CPPTemplates.getContainingTemplate(getASTTemplateParameter());
		if (template instanceof ICPPInternalTemplate) {
			return ((ICPPInternalTemplate) template).resolveTemplateParameter(this);
		}

		// problem finding the containing template
		if (template == null) {
			return this;
		}
		
		// use parameter from the index
		try {
			ICPPTemplateParameter[] params = template.getTemplateParameters();
			final int pos= getParameterPosition();
			if (pos < params.length) 
				return params[pos];
		} catch (DOMException e) {
			return e.getProblem();
		}
		return new ProblemBinding(getPrimaryDeclaration(), IProblemBinding.SEMANTIC_DEFINITION_NOT_FOUND);
	}
}

Back to the top