|
Lines 7-39
Link Here
|
| 7 |
* |
7 |
* |
| 8 |
* Contributors: |
8 |
* Contributors: |
| 9 |
* QNX Software Systems - initial API and implementation |
9 |
* QNX Software Systems - initial API and implementation |
|
|
10 |
* Andrew Ferguson (Symbian) |
| 10 |
*******************************************************************************/ |
11 |
*******************************************************************************/ |
| 11 |
package org.eclipse.cdt.core.browser; |
12 |
package org.eclipse.cdt.core.browser; |
| 12 |
|
13 |
|
| 13 |
import java.util.ArrayList; |
14 |
import java.util.regex.Pattern; |
| 14 |
import java.util.List; |
|
|
| 15 |
|
15 |
|
| 16 |
import org.eclipse.cdt.core.CCorePlugin; |
16 |
import org.eclipse.cdt.core.CCorePlugin; |
| 17 |
import org.eclipse.cdt.core.dom.ILinkage; |
|
|
| 18 |
import org.eclipse.cdt.core.dom.IPDOMNode; |
| 19 |
import org.eclipse.cdt.core.dom.IPDOMVisitor; |
| 20 |
import org.eclipse.cdt.core.dom.ast.DOMException; |
| 21 |
import org.eclipse.cdt.core.dom.ast.IBinding; |
17 |
import org.eclipse.cdt.core.dom.ast.IBinding; |
| 22 |
import org.eclipse.cdt.core.dom.ast.ICompositeType; |
18 |
import org.eclipse.cdt.core.index.IIndex; |
| 23 |
import org.eclipse.cdt.core.dom.ast.IEnumeration; |
19 |
import org.eclipse.cdt.core.index.IIndexBinding; |
| 24 |
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType; |
20 |
import org.eclipse.cdt.core.index.IndexFilter; |
| 25 |
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace; |
|
|
| 26 |
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespaceAlias; |
| 27 |
import org.eclipse.cdt.core.model.CoreModel; |
21 |
import org.eclipse.cdt.core.model.CoreModel; |
| 28 |
import org.eclipse.cdt.core.model.ICElement; |
22 |
import org.eclipse.cdt.core.model.ICElement; |
| 29 |
import org.eclipse.cdt.core.model.ICProject; |
23 |
import org.eclipse.cdt.core.model.ICProject; |
| 30 |
import org.eclipse.cdt.internal.core.CCoreInternals; |
24 |
import org.eclipse.cdt.internal.core.browser.util.IndexModelUtil; |
| 31 |
import org.eclipse.cdt.internal.core.pdom.PDOM; |
|
|
| 32 |
import org.eclipse.cdt.internal.core.pdom.PDOMManager; |
| 33 |
import org.eclipse.cdt.internal.core.pdom.dom.PDOMBinding; |
| 34 |
import org.eclipse.cdt.internal.core.pdom.dom.PDOMLinkage; |
| 35 |
import org.eclipse.cdt.internal.core.pdom.dom.c.PDOMCStructure; |
| 36 |
import org.eclipse.core.runtime.CoreException; |
25 |
import org.eclipse.core.runtime.CoreException; |
|
|
26 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 27 |
import org.eclipse.core.runtime.NullProgressMonitor; |
| 37 |
|
28 |
|
| 38 |
/** |
29 |
/** |
| 39 |
* Manages a search cache for types in the workspace. Instead of returning |
30 |
* Manages a search cache for types in the workspace. Instead of returning |
|
Lines 49-185
Link Here
|
| 49 |
* waits for the completion of the background job. |
40 |
* waits for the completion of the background job. |
| 50 |
*/ |
41 |
*/ |
| 51 |
public class AllTypesCache { |
42 |
public class AllTypesCache { |
| 52 |
|
43 |
private static final boolean DEBUG = false; |
| 53 |
private abstract static class TypesCollector implements IPDOMVisitor { |
|
|
| 54 |
private final int[] kinds; |
| 55 |
protected final List types; |
| 56 |
protected final ICProject project; |
| 57 |
|
| 58 |
protected TypesCollector(int[] kinds, List types, ICProject project) { |
| 59 |
this.kinds = kinds; |
| 60 |
this.types = types; |
| 61 |
this.project = project; |
| 62 |
} |
| 63 |
|
| 64 |
protected abstract void visitKind(IPDOMNode node, int kind); |
| 65 |
|
| 66 |
public boolean visit(IPDOMNode node) throws CoreException { |
| 67 |
for (int i = 0; i < kinds.length; ++i) |
| 68 |
visitKind(node, kinds[i]); |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
public void leave(IPDOMNode node) throws CoreException { |
| 73 |
} |
| 74 |
|
| 75 |
public List getTypes() { |
| 76 |
return types; |
| 77 |
} |
| 78 |
} |
| 79 |
|
44 |
|
| 80 |
private static class CTypesCollector extends TypesCollector { |
45 |
private static ITypeInfo[] getTypes(ICProject[] projects, final int[] kinds, IProgressMonitor monitor) throws CoreException { |
| 81 |
public CTypesCollector(int[] kinds, List types, ICProject project) { |
46 |
IIndex index = CCorePlugin.getIndexManager().getIndex(projects); |
| 82 |
super(kinds, types, project); |
|
|
| 83 |
} |
| 84 |
|
47 |
|
| 85 |
protected void visitKind(IPDOMNode node, int kind) { |
48 |
try { |
| 86 |
switch (kind) { |
49 |
index.acquireReadLock(); |
| 87 |
case ICElement.C_NAMESPACE: |
50 |
|
| 88 |
return; |
51 |
long start = System.currentTimeMillis(); |
| 89 |
case ICElement.C_CLASS: |
52 |
|
| 90 |
return; |
53 |
IIndexBinding[] all = |
| 91 |
case ICElement.C_STRUCT: |
54 |
index.findBindings(Pattern.compile(".*"), false, new IndexFilter() { //$NON-NLS-1$ |
| 92 |
if (node instanceof PDOMCStructure) |
55 |
public boolean acceptBinding(IBinding binding) { |
| 93 |
types.add(new PDOMTypeInfo((IBinding)node, kind, project)); |
56 |
return IndexModelUtil.bindingHasCElementType(binding, kinds); |
| 94 |
return; |
57 |
}}, |
| 95 |
case ICElement.C_UNION: |
58 |
monitor |
| 96 |
return; |
59 |
); |
| 97 |
case ICElement.C_ENUMERATION: |
60 |
|
| 98 |
return; |
61 |
if(DEBUG) { |
| 99 |
case ICElement.C_TYPEDEF: |
62 |
System.out.println("Index search took "+(System.currentTimeMillis() - start)); //$NON-NLS-1$ |
| 100 |
return; |
63 |
start = System.currentTimeMillis(); |
| 101 |
} |
64 |
} |
| 102 |
} |
65 |
|
| 103 |
} |
66 |
ITypeInfo[] result = new ITypeInfo[all.length]; |
| 104 |
|
67 |
for(int i=0; i<all.length; i++) { |
| 105 |
private static class CPPTypesCollector extends TypesCollector { |
68 |
IIndexBinding ib = (IIndexBinding) all[i]; |
| 106 |
public CPPTypesCollector(int[] kinds, List types, ICProject project) { |
69 |
result[i] = new IndexTypeInfo(ib.getQualifiedName(), IndexModelUtil.getElementType(ib), index); |
| 107 |
super(kinds, types, project); |
|
|
| 108 |
} |
| 109 |
|
| 110 |
protected void visitKind(IPDOMNode node, int kind) { |
| 111 |
try { |
| 112 |
switch (kind) { |
| 113 |
case ICElement.C_NAMESPACE: |
| 114 |
if (node instanceof ICPPNamespace || node instanceof ICPPNamespaceAlias) |
| 115 |
types.add(new PDOMTypeInfo((PDOMBinding)node, kind, project)); |
| 116 |
return; |
| 117 |
case ICElement.C_CLASS: |
| 118 |
if (node instanceof ICPPClassType |
| 119 |
&& ((ICPPClassType)node).getKey() == ICPPClassType.k_class) |
| 120 |
types.add(new PDOMTypeInfo((PDOMBinding)node, kind, project)); |
| 121 |
return; |
| 122 |
case ICElement.C_STRUCT: |
| 123 |
if (node instanceof ICPPClassType |
| 124 |
&& ((ICPPClassType)node).getKey() == ICompositeType.k_struct) |
| 125 |
types.add(new PDOMTypeInfo((PDOMBinding)node, kind, project)); |
| 126 |
return; |
| 127 |
case ICElement.C_UNION: |
| 128 |
if (node instanceof ICPPClassType |
| 129 |
&& ((ICPPClassType)node).getKey() == ICompositeType.k_union) |
| 130 |
types.add(new PDOMTypeInfo((PDOMBinding)node, kind, project)); |
| 131 |
return; |
| 132 |
case ICElement.C_ENUMERATION: |
| 133 |
if (node instanceof IEnumeration |
| 134 |
/*&& node instanceof ICPPBinding*/) |
| 135 |
types.add(new PDOMTypeInfo((IEnumeration)node, kind, project)); |
| 136 |
return; |
| 137 |
case ICElement.C_TYPEDEF: |
| 138 |
return; |
| 139 |
} |
| 140 |
} catch (DOMException e) { |
| 141 |
CCorePlugin.log(e); |
| 142 |
} |
70 |
} |
| 143 |
} |
71 |
|
| 144 |
} |
72 |
if(DEBUG) { |
| 145 |
|
73 |
System.out.println("Wrapping as ITypeInfo took "+(System.currentTimeMillis() - start)); //$NON-NLS-1$ |
| 146 |
private static ITypeInfo[] getTypes(ICProject[] projects, int[] kinds) throws CoreException { |
74 |
start = System.currentTimeMillis(); |
| 147 |
List types = new ArrayList(); |
|
|
| 148 |
PDOMManager pdomManager = CCoreInternals.getPDOMManager(); |
| 149 |
|
| 150 |
for (int i = 0; i < projects.length; ++i) { |
| 151 |
ICProject project = projects[i]; |
| 152 |
CTypesCollector cCollector = new CTypesCollector(kinds, types, project); |
| 153 |
CPPTypesCollector cppCollector = new CPPTypesCollector(kinds, types, project); |
| 154 |
|
| 155 |
PDOM pdom = (PDOM)pdomManager.getPDOM(project); |
| 156 |
PDOMLinkage linkage= pdom.getLinkage(ILinkage.C_LINKAGE_ID); |
| 157 |
if (linkage != null) { |
| 158 |
linkage.accept(cCollector); |
| 159 |
} |
75 |
} |
| 160 |
|
76 |
|
| 161 |
linkage= pdom.getLinkage(ILinkage.CPP_LINKAGE_ID); |
77 |
return result; |
| 162 |
if (linkage != null) { |
78 |
} catch(InterruptedException ie) { |
| 163 |
linkage.accept(cppCollector); |
79 |
ie.printStackTrace(); |
| 164 |
} |
80 |
} finally { |
|
|
81 |
index.releaseReadLock(); |
| 165 |
} |
82 |
} |
| 166 |
|
83 |
return new ITypeInfo[0]; |
| 167 |
return (ITypeInfo[])types.toArray(new ITypeInfo[types.size()]); |
|
|
| 168 |
} |
84 |
} |
| 169 |
|
85 |
|
| 170 |
/** |
86 |
/** |
| 171 |
* Returns all types in the workspace. |
87 |
* Returns all types in the workspace. |
| 172 |
*/ |
88 |
*/ |
| 173 |
public static ITypeInfo[] getAllTypes() { |
89 |
public static ITypeInfo[] getAllTypes() { |
|
|
90 |
return getAllTypes(new NullProgressMonitor()); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Returns all types in the workspace. |
| 95 |
*/ |
| 96 |
public static ITypeInfo[] getAllTypes(IProgressMonitor monitor) { |
| 174 |
try { |
97 |
try { |
| 175 |
ICProject[] projects = CoreModel.getDefault().getCModel().getCProjects(); |
98 |
ICProject[] projects = CoreModel.getDefault().getCModel().getCProjects(); |
| 176 |
return getTypes(projects, ITypeInfo.KNOWN_TYPES); |
99 |
return getTypes(projects, ITypeInfo.KNOWN_TYPES, monitor); |
| 177 |
} catch (CoreException e) { |
100 |
} catch (CoreException e) { |
| 178 |
CCorePlugin.log(e); |
101 |
CCorePlugin.log(e); |
| 179 |
return new ITypeInfo[0]; |
102 |
return new ITypeInfo[0]; |
| 180 |
} |
103 |
} |
| 181 |
} |
104 |
} |
| 182 |
|
105 |
|
| 183 |
/** |
106 |
/** |
| 184 |
* Returns all types in the given scope. |
107 |
* Returns all types in the given scope. |
| 185 |
* |
108 |
* |
|
Lines 189-201
Link Here
|
| 189 |
*/ |
112 |
*/ |
| 190 |
public static ITypeInfo[] getTypes(ITypeSearchScope scope, int[] kinds) { |
113 |
public static ITypeInfo[] getTypes(ITypeSearchScope scope, int[] kinds) { |
| 191 |
try { |
114 |
try { |
| 192 |
return getTypes(scope.getEnclosingProjects(), kinds); |
115 |
return getTypes(scope.getEnclosingProjects(), kinds, new NullProgressMonitor()); |
| 193 |
} catch (CoreException e) { |
116 |
} catch (CoreException e) { |
| 194 |
CCorePlugin.log(e); |
117 |
CCorePlugin.log(e); |
| 195 |
return new ITypeInfo[0]; |
118 |
return new ITypeInfo[0]; |
| 196 |
} |
119 |
} |
| 197 |
} |
120 |
} |
| 198 |
|
121 |
|
| 199 |
/** |
122 |
/** |
| 200 |
* Returns all namespaces in the given scope. |
123 |
* Returns all namespaces in the given scope. |
| 201 |
* |
124 |
* |
|
Lines 204-216
Link Here
|
| 204 |
*/ |
127 |
*/ |
| 205 |
public static ITypeInfo[] getNamespaces(ITypeSearchScope scope, boolean includeGlobalNamespace) { |
128 |
public static ITypeInfo[] getNamespaces(ITypeSearchScope scope, boolean includeGlobalNamespace) { |
| 206 |
try { |
129 |
try { |
| 207 |
return getTypes(scope.getEnclosingProjects(), new int[] {ICElement.C_NAMESPACE}); |
130 |
return getTypes(scope.getEnclosingProjects(), new int[] {ICElement.C_NAMESPACE}, new NullProgressMonitor()); |
| 208 |
} catch (CoreException e) { |
131 |
} catch (CoreException e) { |
| 209 |
CCorePlugin.log(e); |
132 |
CCorePlugin.log(e); |
| 210 |
return new ITypeInfo[0]; |
133 |
return new ITypeInfo[0]; |
| 211 |
} |
134 |
} |
| 212 |
} |
135 |
} |
| 213 |
|
136 |
|
| 214 |
/** Returns first type in the cache which matches the given |
137 |
/** Returns first type in the cache which matches the given |
| 215 |
* type and name. If no type is found, <code>null</code> |
138 |
* type and name. If no type is found, <code>null</code> |
| 216 |
* is returned. |
139 |
* is returned. |