Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 00c58c13d5755a898f306965644dd1d216e855a0 (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
309
310
311
312
313
314
315
316
/*******************************************************************************
 * Copyright (c) 2005, 2015 QNX Software Systems 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:
 *     Doug Schaefer (QNX) - Initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *     Andrew Ferguson (Symbian)
 *     Bryan Wilkinson (QNX)
 *     Sergey Prigogin (Google)
 *     Jens Elmenthaler - http://bugs.eclipse.org/173458 (camel case completion)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.pdom.dom.cpp;

import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ILinkage;
import org.eclipse.cdt.core.dom.IPDOMNode;
import org.eclipse.cdt.core.dom.IPDOMVisitor;
import org.eclipse.cdt.core.dom.ast.EScopeKind;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConversionName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope;
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.ICPPMethod;
import org.eclipse.cdt.core.index.IIndexBinding;
import org.eclipse.cdt.core.index.IIndexFileSet;
import org.eclipse.cdt.core.index.IIndexName;
import org.eclipse.cdt.core.index.IndexFilter;
import org.eclipse.cdt.core.parser.Keywords;
import org.eclipse.cdt.core.parser.util.CharArrayObjectMap;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPClassScope;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPSemantics;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil;
import org.eclipse.cdt.internal.core.index.DeclaredBindingsFilter;
import org.eclipse.cdt.internal.core.index.IIndexScope;
import org.eclipse.cdt.internal.core.parser.util.ContentAssistMatcherFactory;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.cdt.internal.core.pdom.dom.BindingCollector;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMNode;
import org.eclipse.core.runtime.CoreException;

/**
 * Represents the class scope for a class stored in the index.
 * For safe use, all fields need to be final.
 */
class PDOMCPPClassScope implements ICPPClassScope, IIndexScope {
	private static final class PopulateMap implements IPDOMVisitor {
		private final CharArrayObjectMap<List<PDOMBinding>> fResult;

		private PopulateMap(CharArrayObjectMap<List<PDOMBinding>> result) {
			fResult = result;
		}

		@Override
		public boolean visit(IPDOMNode node) throws CoreException {
			if (node instanceof PDOMBinding) {
				final PDOMBinding binding= (PDOMBinding) node;
				final char[] nchars = binding.getNameCharArray();
				List<PDOMBinding> list= fResult.get(nchars);
				if (list == null) {
					list= new ArrayList<>();
					fResult.put(nchars, list);
				}
				list.add(binding);
				if (binding instanceof ICompositeType && ((ICompositeType) binding).isAnonymous()) {
					return true; // visit children
				}
			}
			return false;
		}

		@Override
		public void leave(IPDOMNode node){}
	}

	private static final IndexFilter CONVERSION_FILTER =
			new DeclaredBindingsFilter(ILinkage.CPP_LINKAGE_ID, true, false) {
		@Override
		public boolean acceptBinding(IBinding binding) throws CoreException {
			return binding instanceof ICPPMethod && 
				SemanticUtil.isConversionOperator(((ICPPMethod) binding)) &&
				super.acceptBinding(binding);
		}
	};
	
	private final IPDOMCPPClassType fBinding;

	public PDOMCPPClassScope(IPDOMCPPClassType binding) {
		fBinding= binding;
	}
	
	@Override
	public EScopeKind getKind() {
		return EScopeKind.eClassType;
	}

	@Override
	public ICPPClassType getClassType() {
		return fBinding;
	}

	@Override
	public IBinding getBinding(IASTName name, boolean resolve) {
		return getBinding(name, resolve, null);
	}

	@Override
	public IBinding[] getBindings(IASTName name, boolean resolve, boolean prefixLookup) {
		return getBindings(new ScopeLookupData(name, resolve, prefixLookup));
	}

	@Override
	public IBinding getBinding(IASTName name, boolean resolve, IIndexFileSet fileSet) {
		try {
		    final char[] nameChars = name.getSimpleID();
			if (CharArrayUtils.equals(fBinding.getNameCharArray(), nameChars)) {
	            // 9.2 ... The class-name is also inserted into the scope of the class itself
		        return getClassNameBinding();
		    }
			
			final IBinding[] candidates = getBindingsViaCache(fBinding, nameChars, IndexFilter.CPP_DECLARED_OR_IMPLICIT_NO_INSTANCE);
			return CPPSemantics.resolveAmbiguities(name, candidates);
		} catch (CoreException e) {
			CCorePlugin.log(e);
		}
		return null;
	}

	private IBinding getClassNameBinding() {
		return fBinding;
	}
	
	@Deprecated	@Override
	public IBinding[] getBindings(IASTName name, boolean resolve, boolean prefixLookup, IIndexFileSet fileSet) {
		return getBindings(new ScopeLookupData(name, resolve, prefixLookup));
	}

	@Override
	public IBinding[] getBindings(ScopeLookupData lookup) {
		try {
			if (lookup.getLookupName() instanceof ICPPASTConversionName) {
				BindingCollector visitor = new BindingCollector(fBinding.getLinkage(),
						Keywords.cOPERATOR, CONVERSION_FILTER, true, false, true);
				acceptViaCache(fBinding, visitor, true);
				return visitor.getBindings();
			} 

			final char[] nameChars = lookup.getLookupKey();
			if (!lookup.isPrefixLookup()) {
				if (CharArrayUtils.equals(fBinding.getNameCharArray(), nameChars)) {
			        if (CPPClassScope.shallReturnConstructors(lookup.getLookupName(), false)){
			        	return fBinding.getConstructors();
			        }
			        return new IBinding[] { getClassNameBinding() };
				}
			    return getBindingsViaCache(fBinding, nameChars, IndexFilter.CPP_DECLARED_OR_IMPLICIT_NO_INSTANCE);
			}
			
			// Prefix lookup.
			BindingCollector visitor = new BindingCollector(fBinding.getLinkage(), nameChars,
					IndexFilter.CPP_DECLARED_OR_IMPLICIT_NO_INSTANCE, true, true, false);
			if (ContentAssistMatcherFactory.getInstance().match(nameChars, fBinding.getNameCharArray())) {
				// Add the class itself, constructors will be found during the visit.
		        visitor.visit((IPDOMNode) getClassNameBinding());
			}
			acceptViaCache(fBinding, visitor, true);
			return visitor.getBindings();
		} catch (CoreException e) {
			CCorePlugin.log(e);
		}
		return null;
	}

	public static IBinding[] getBindingsViaCache(IPDOMCPPClassType ct, final char[] name, IndexFilter filter) throws CoreException {
		CharArrayObjectMap<List<PDOMBinding>> map = getBindingMap(ct);		
		List<PDOMBinding> cached= map.get(name);
		if (cached == null)
			return IBinding.EMPTY_BINDING_ARRAY;
		
		int i= 0;
		IBinding[] result= new IBinding[cached.size()];
		for (IBinding binding : cached) {
			if (filter.acceptBinding(binding)) {
				result[i++]= binding;
			}
		}
		if (i == result.length)
			return result;
		
		final IBinding[] bresult= new IBinding[i];
		System.arraycopy(result, 0, bresult, 0, i);
		return bresult;
	}

	/**
	 * Visit bindings via the cache.
	 */
	public static void acceptViaCache(IPDOMCPPClassType ct, IPDOMVisitor visitor,
			boolean includeNestedInAnonymous) throws CoreException {
		final long record= ct.getRecord();
		CharArrayObjectMap<List<PDOMBinding>> map= getBindingMap(ct);
		for (List<PDOMBinding> list : map.values()) {
			for (PDOMBinding node : list) {
				if (includeNestedInAnonymous || node.getParentNodeRec() == record) {
					if (visitor.visit(node)) {
						node.accept(visitor);
					}
					visitor.leave(node);
				}
			}
		}
	}

	public static void updateCache(IPDOMCPPClassType ct, PDOMNode member) throws CoreException {
		if (member instanceof PDOMBinding) {
			final Long key= ct.getRecord() + PDOMCPPLinkage.CACHE_MEMBERS;
			final PDOM pdom = ct.getPDOM();
			@SuppressWarnings("unchecked")
			Reference<CharArrayObjectMap<List<PDOMBinding>>> cached=
					(Reference<CharArrayObjectMap<List<PDOMBinding>>>) pdom.getCachedResult(key);
			CharArrayObjectMap<List<PDOMBinding>> map= cached == null ? null : cached.get();
			if (map != null) {
				new PopulateMap(map).visit(member);
			}
		}
	}

	public static CharArrayObjectMap<List<PDOMBinding>> getBindingMap(IPDOMCPPClassType ct) throws CoreException {
		final Long key= ct.getRecord() + PDOMCPPLinkage.CACHE_MEMBERS;
		final PDOM pdom = ct.getPDOM();
		@SuppressWarnings("unchecked")
		Reference<CharArrayObjectMap<List<PDOMBinding>>> cached=
				(Reference<CharArrayObjectMap<List<PDOMBinding>>>) pdom.getCachedResult(key);
		CharArrayObjectMap<List<PDOMBinding>> map= cached == null ? null : cached.get();
		
		if (map == null) {
			// There is no cache, build it:
			map= new CharArrayObjectMap<>(8);
			IPDOMVisitor visitor= new PopulateMap(map);
			visitor.visit(ct);
			ct.acceptUncached(visitor);
			pdom.putCachedResult(key, new SoftReference<>(map));
		}
		return map;
	}

	@Override
	public IBinding[] find(String name, IASTTranslationUnit tu) {
	    return CPPSemantics.findBindingsInScope(this, name, tu);
	}

	@Override @Deprecated
	public IBinding[] find(String name) {
		return CPPSemantics.findBindings(this, name, false);
	}
	
	@Override
	public IIndexBinding getScopeBinding() {
		return fBinding;
	}

	@Override
	public ICPPMethod[] getImplicitMethods() {
		try {
			PDOMClassUtil.MethodCollector methods = new PDOMClassUtil.MethodCollector(true, false);
			acceptViaCache(fBinding, methods, false);
			return methods.getMethods();
		} catch (CoreException e) {
			return ICPPMethod.EMPTY_CPPMETHOD_ARRAY;
		}
	}

	@Override
	public ICPPConstructor[] getConstructors() {
		return fBinding.getConstructors();
	}

	@Override
	public IIndexScope getParent() {
		return fBinding.getScope();
	}

	@Override
	public IIndexName getScopeName() {
		return fBinding.getScopeName();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof PDOMCPPClassScope)
			return fBinding.equals(((PDOMCPPClassScope) obj).fBinding);
		return false;
	}

	@Override
	public int hashCode() {
		return fBinding.hashCode();
	}
}

Back to the top