Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fb219474e54dd2f1caef2228507dee644870c3cd (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
/*******************************************************************************
 * Copyright (c) 2004, 2006 QNX Software Systems 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:
 *     QNX Software Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.core.browser;

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.IPDOMManager;
import org.eclipse.cdt.core.dom.IPDOMNode;
import org.eclipse.cdt.core.dom.IPDOMVisitor;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IEnumeration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespaceAlias;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMLinkage;
import org.eclipse.cdt.internal.core.pdom.dom.c.PDOMCStructure;
import org.eclipse.core.runtime.CoreException;

/**
 * Manages a search cache for types in the workspace. Instead of returning
 * objects of type <code>ICElement</code> the methods of this class returns a
 * list of the lightweight objects <code>TypeInfo</code>.
 * <P>
 * AllTypesCache runs asynchronously using a background job to rebuild the cache
 * as needed. If the cache becomes dirty again while the background job is
 * running, the job is restarted.
 * <P>
 * If <code>getTypes</code> is called in response to a user action, a progress
 * dialog is shown. If called before the background job has finished, getTypes
 * waits for the completion of the background job.
 */
public class AllTypesCache {

	private abstract static class TypesCollector implements IPDOMVisitor {
		private final int[] kinds;
		protected final List types;
		protected final ICProject project;
		
		protected TypesCollector(int[] kinds, List types, ICProject project) {
			this.kinds = kinds;
			this.types = types;
			this.project = project;
		}
		
		protected abstract void visitKind(IPDOMNode node, int kind);
		
		public boolean visit(IPDOMNode node) throws CoreException {
			for (int i = 0; i < kinds.length; ++i)
				visitKind(node, kinds[i]);
			return true;
		}
		
		public void leave(IPDOMNode node) throws CoreException {
		}
		
		public List getTypes() {
			return types;
		}
	}
	
	private static class CTypesCollector extends TypesCollector {
		public CTypesCollector(int[] kinds, List types, ICProject project) {
			super(kinds, types, project);
		}
		
		protected void visitKind(IPDOMNode node, int kind) {
			switch (kind) {
			case ICElement.C_NAMESPACE:
				return;
			case ICElement.C_CLASS:
				return;
			case ICElement.C_STRUCT:
				if (node instanceof PDOMCStructure)
					types.add(new PDOMTypeInfo((IBinding)node, kind, project));
				return;
			case ICElement.C_UNION:
				return;
			case ICElement.C_ENUMERATION:
				return;
			case ICElement.C_TYPEDEF:
				return;
			}
		}
	}
	
	private static class CPPTypesCollector extends TypesCollector {
		public CPPTypesCollector(int[] kinds, List types, ICProject project) {
			super(kinds, types, project);
		}
		
		protected void visitKind(IPDOMNode node, int kind) {
			try {
				switch (kind) {
				case ICElement.C_NAMESPACE:
					if (node instanceof ICPPNamespace || node instanceof ICPPNamespaceAlias)
						types.add(new PDOMTypeInfo((PDOMBinding)node, kind, project));
					return;
				case ICElement.C_CLASS:
					if (node instanceof ICPPClassType
							&& ((ICPPClassType)node).getKey() == ICPPClassType.k_class)
						types.add(new PDOMTypeInfo((PDOMBinding)node, kind, project));
					return;
				case ICElement.C_STRUCT:
					if (node instanceof ICPPClassType
							&& ((ICPPClassType)node).getKey() == ICompositeType.k_struct)
						types.add(new PDOMTypeInfo((PDOMBinding)node, kind, project));
					return;
				case ICElement.C_UNION:
					if (node instanceof ICPPClassType
							&& ((ICPPClassType)node).getKey() == ICompositeType.k_union)
						types.add(new PDOMTypeInfo((PDOMBinding)node, kind, project));
					return;
				case ICElement.C_ENUMERATION:
					if (node instanceof IEnumeration
							/*&& node instanceof ICPPBinding*/)
							types.add(new PDOMTypeInfo((IEnumeration)node, kind, project));
					return;
				case ICElement.C_TYPEDEF:
					return;
				}
			} catch (DOMException e) {
				CCorePlugin.log(e);
			}
		}
	}
	
	private static ITypeInfo[] getTypes(ICProject[] projects, int[] kinds) throws CoreException {
		List types = new ArrayList();
		IPDOMManager pdomManager = CCorePlugin.getPDOMManager();
		
		for (int i = 0; i < projects.length; ++i) {
			ICProject project = projects[i];
			CTypesCollector cCollector = new CTypesCollector(kinds, types, project);
			CPPTypesCollector cppCollector = new CPPTypesCollector(kinds, types, project);
				
			PDOM pdom = (PDOM)pdomManager.getPDOM(project);
			PDOMLinkage linkage= pdom.getLinkage(ILinkage.C_LINKAGE_ID);
			if (linkage != null) {
				linkage.accept(cCollector);
			}
			
			linkage= pdom.getLinkage(ILinkage.CPP_LINKAGE_ID);
			if (linkage != null) {
				linkage.accept(cppCollector);
			}
		}
			
		return (ITypeInfo[])types.toArray(new ITypeInfo[types.size()]);
	}
	
	/**
	 * Returns all types in the workspace.
	 */
	public static ITypeInfo[] getAllTypes() {
		try {
			ICProject[] projects = CoreModel.getDefault().getCModel().getCProjects();
			return getTypes(projects, ITypeInfo.KNOWN_TYPES);
		} catch (CoreException e) {
			CCorePlugin.log(e);
			return new ITypeInfo[0];
		}
	}
	
	/**
	 * Returns all types in the given scope.
	 * 
	 * @param scope The search scope
	 * @param kinds Array containing CElement types: C_NAMESPACE, C_CLASS,
	 *              C_UNION, C_ENUMERATION, C_TYPEDEF
	 */
	public static ITypeInfo[] getTypes(ITypeSearchScope scope, int[] kinds) {
		try {
			return getTypes(scope.getEnclosingProjects(), kinds);
		} catch (CoreException e) {
			CCorePlugin.log(e);
			return new ITypeInfo[0];
		}
	}
	
	/**
	 * Returns all namespaces in the given scope.
	 * 
	 * @param scope The search scope
	 * @param includeGlobalNamespace <code>true</code> if the global (default) namespace should be returned
	 */
	public static ITypeInfo[] getNamespaces(ITypeSearchScope scope, boolean includeGlobalNamespace) {
		try {
			return getTypes(scope.getEnclosingProjects(), new int[] {ICElement.C_NAMESPACE});
		} catch (CoreException e) {
			CCorePlugin.log(e);
			return new ITypeInfo[0];
		}
	}
	
	/** Returns first type in the cache which matches the given
	 *  type and name.  If no type is found, <code>null</code>
	 *  is returned.
	 *
	 * @param project the enclosing project
	 * @param type the ICElement type
	 * @param qualifiedName the qualified type name to match
	 * @return the matching type
	 */
	public static ITypeInfo getType(ICProject project, int type, IQualifiedTypeName qualifiedName) {
		// TODO - seems to be only used when a namespace name is changed
		// which would be pretty slow against the PDOM.
		return null;
	}

	/**
	 * Returns all types matching name in the given project.
	 * 
	 * @param project the enclosing project
	 * @param qualifiedName The qualified type name
	 * @param matchEnclosed <code>true</code> if enclosed types count as matches (foo::bar == bar)
	 * @param ignoreCase <code>true</code> if case-insensitive
	 * @return Array of types
	 */
	public static ITypeInfo[] getTypes(ICProject project, IQualifiedTypeName qualifiedName, boolean matchEnclosed, boolean ignoreCase) {
		// TODO - seems to be only used when a class or namespace name is changed
		// which would be pretty slow against the PDOM.
		return new ITypeInfo[0];
	}

}

Back to the top