Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f36f2320c402631ef0980ded7fa74a5213a11520 (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
/*******************************************************************************
 * Copyright (c) 2005, 2008 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:
 * IBM - Initial API and implementation
 * Bryan Wilkinson (QNX)
 * Andrew Ferguson (Symbian)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IField;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBase;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPDeferredTemplateInstance;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPField;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPSpecialization;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateInstance;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.parser.util.ObjectMap;
import org.eclipse.cdt.internal.core.dom.parser.ASTInternal;
import org.eclipse.cdt.internal.core.index.IIndexType;

/**
 * @author aniefer
 */
public class CPPClassInstance extends CPPInstance implements ICPPClassType, ICPPInternalClassType {
	private CPPClassSpecializationScope instanceScope;

	/**
	 * @param decl
	 * @param args
	 * @param arguments
	 */
	public CPPClassInstance(ICPPScope scope, IBinding decl, ObjectMap argMap, IType[] args) {
		super(scope, decl, argMap, args);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType#getBases()
	 */
	public ICPPBase[] getBases() throws DOMException {
		ICPPClassType cls = (ICPPClassType) getSpecializedBinding();
		if (cls != null) {
			ICPPBase[] result = null;
			ICPPBase[] bindings = cls.getBases();
			for (int i = 0; i < bindings.length; i++) {
				ICPPBase specBinding = (ICPPBase) ((ICPPInternalBase) bindings[i]).clone();
				IBinding base = bindings[i].getBaseClass();
				if (base instanceof IType) {
					IType specBase = CPPTemplates.instantiateType((IType) base, argumentMap);
					specBase = CPPSemantics.getUltimateType(specBase, false);
					if (specBase instanceof IBinding) {
						((ICPPInternalBase) specBinding).setBaseClass((IBinding) specBase);
					}
					result = (ICPPBase[]) ArrayUtil.append(ICPPBase.class, result, specBinding);
				}
			}
			return (ICPPBase[]) ArrayUtil.trim(ICPPBase.class, result);
		}
		return ICPPBase.EMPTY_BASE_ARRAY;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.ICompositeType#getFields()
	 */
	public IField[] getFields() throws DOMException {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.ICompositeType#findField(java.lang.String)
	 */
	public IField findField(String name) throws DOMException {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType#getDeclaredFields()
	 */
	public ICPPField[] getDeclaredFields() throws DOMException {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType#getMethods()
	 */
	public ICPPMethod[] getMethods() throws DOMException {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType#getAllDeclaredMethods()
	 */
	public ICPPMethod[] getAllDeclaredMethods() throws DOMException {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType#getDeclaredMethods()
	 */
	public ICPPMethod[] getDeclaredMethods() throws DOMException {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType#getConstructors()
	 */
	public ICPPConstructor[] getConstructors() throws DOMException {
		CPPClassSpecializationScope scope = (CPPClassSpecializationScope) getCompositeScope();
		if (scope.isFullyCached())
			return scope.getConstructors();
		return ICPPConstructor.EMPTY_CONSTRUCTOR_ARRAY;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType#getFriends()
	 */
	public IBinding[] getFriends() throws DOMException {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.ICompositeType#getKey()
	 */
	public int getKey() throws DOMException {
		return ((ICPPClassType) getSpecializedBinding()).getKey();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.ICompositeType#getCompositeScope()
	 */
	public IScope getCompositeScope() {
		if (instanceScope == null) {
			instanceScope = new CPPClassSpecializationScope(this);
		}
		return instanceScope;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#clone()
	 */
	@Override
	public Object clone() {
		return this;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.dom.ast.IType#isSameType(org.eclipse.cdt.core.dom.ast.IType)
	 */
	public boolean isSameType(IType type) {
		if (type == this)
			return true;
		if (type instanceof ITypedef || type instanceof IIndexType)
			return type.isSameType(this);
		if (type instanceof ICPPDeferredTemplateInstance && type instanceof ICPPClassType)
			return type.isSameType(this);  // the CPPDeferredClassInstance has some fuzziness

		if (type instanceof ICPPTemplateInstance) {
			ICPPClassType ct1= (ICPPClassType) getSpecializedBinding();
			ICPPClassType ct2= (ICPPClassType) ((ICPPTemplateInstance) type).getTemplateDefinition();
			if (!ct1.isSameType(ct2))
				return false;

			ObjectMap m1 = getArgumentMap();
			ObjectMap m2 = ((ICPPTemplateInstance) type).getArgumentMap();
			if (m1 == null || m2 == null || m1.size() != m2.size())
				return false;
			for (int i = 0; i < m1.size(); i++) {
				IType t1 = (IType) m1.getAt(i);
				IType t2 = (IType) m2.getAt(i);
				if (t1 == null || !t1.isSameType(t2))
					return false;
			}
			return true;
		}

		return false;
	}

	public ICPPClassType[] getNestedClasses() throws DOMException {
		return ICPPClassType.EMPTY_CLASS_ARRAY;
	}

	public ICPPMethod[] getConversionOperators() throws DOMException {
		IScope scope = getCompositeScope();
		if (scope instanceof CPPClassSpecializationScope) {
			if (ASTInternal.isFullyCached(scope))
				return ((CPPClassSpecializationScope)scope).getConversionOperators();
		}
		return ICPPMethod.EMPTY_CPPMETHOD_ARRAY;
	}

	/**
	 * Returns a combined argument map of this class and all its base template classes.
	 * This combined map helps with instantiation of members of template classes that subclass
	 * other template classes (see AST2TemplateTests#testRebindPattern_214017_2()).
	 */
	@Override
	public ObjectMap getArgumentMap() {
		ObjectMap argMap = argumentMap;
		List<ICPPSpecialization> bases = null;
		try {
			for (ICPPBase base : getBases()) {
				IBinding baseClass = base.getBaseClass();
				if (baseClass instanceof ICPPSpecialization) {
					if (bases == null) {
						bases = new ArrayList<ICPPSpecialization>();
					}
					bases.add((ICPPSpecialization) baseClass);
				}
			}
			if (bases != null) {
				for (int i = 0; i < bases.size(); i++) {
					for (ICPPBase base : ((ICPPClassType) bases.get(i)).getBases()) {
						IBinding baseClass = base.getBaseClass();
						if (baseClass instanceof ICPPSpecialization) {
							bases.add((ICPPSpecialization) baseClass);
						}
					}
					if (bases.size() > 20) {  // Protect against cyclic inheritance.
						break;
					}
				}
			}
		} catch (DOMException e) {
			// Ignore 
		}

		if (bases != null) {
			for (ICPPSpecialization base : bases) {
				// Protect against infinite recursion.
				ObjectMap baseArgMap = base instanceof CPPClassInstance ?
						((CPPClassInstance) base).argumentMap : base.getArgumentMap();
				if (!baseArgMap.isEmpty()) {
					if (argMap == argumentMap) {
						argMap = (ObjectMap) argumentMap.clone();
					}
					for (int i = 0; i < baseArgMap.size(); i++) {
						Object key = baseArgMap.keyAt(i);
						if (!argMap.containsKey(key)) {
							argMap.put(key, baseArgMap.getAt(i));
						}
					}
				}
			}
		}
		return argMap;
	}

	@Override
	public boolean equals(Object obj) {
		return obj instanceof ICPPClassType && isSameType((ICPPClassType) obj);
	}
}

Back to the top