Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4e7ed0e8d23eb7098099d6216e91c47b0ec434ab (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
317
318
319
320
/*******************************************************************************
 * Copyright (c) 2006, 2012 Wind River Systems, Inc. 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:
 *    Markus Schorn - initial API and implementation
 *******************************************************************************/

package org.eclipse.cdt.internal.ui.typehierarchy;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.IASTName;
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.IEnumerator;
import org.eclipse.cdt.core.dom.ast.IField;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMember;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexManager;
import org.eclipse.cdt.core.index.IIndexName;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.IFunctionDeclaration;
import org.eclipse.cdt.internal.ui.util.ExceptionHandler;
import org.eclipse.cdt.internal.ui.util.StatusLineHandler;
import org.eclipse.cdt.internal.ui.viewsupport.IndexUI;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.Region;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.texteditor.ITextEditor;

public class TypeHierarchyUI {
	private static final int INDEX_SEARCH_OPTION = IIndexManager.ADD_DEPENDENCIES | IIndexManager.ADD_DEPENDENT
			| IIndexManager.ADD_EXTENSION_FRAGMENTS_TYPE_HIERARCHY;

	public static THViewPart open(ICElement input, IWorkbenchWindow window) {
		if (!isValidInput(input)) {
			return null;
		}
		ICElement memberInput = null;
		if (!isValidTypeInput(input)) {
			memberInput = input;
			input = memberInput.getParent();
			if (!isValidTypeInput(input)) {
				ICElement[] inputs = findInput(memberInput);
				if (inputs != null) {
					input = inputs[0];
					memberInput = inputs[1];
				}
			}
		}

		if (isValidTypeInput(input)) {
			return openInViewPart(window, input, memberInput);
		}
		return null;
	}

	private static THViewPart openInViewPart(IWorkbenchWindow window, ICElement input, ICElement member) {
		IWorkbenchPage page = window.getActivePage();
		try {
			THViewPart result = (THViewPart) page.showView(CUIPlugin.ID_TYPE_HIERARCHY);
			result.setInput(input, member);
			return result;
		} catch (CoreException e) {
			ExceptionHandler.handle(e, window.getShell(), Messages.TypeHierarchyUI_OpenTypeHierarchy, null);
		}
		return null;
	}

	public static ICElement[] getInput(final ITextEditor editor, IRegion region) {
		if (editor != null) {
			final IEditorInput editorInput = editor.getEditorInput();
			ICElement inputCElement = CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editorInput);
			if (inputCElement != null) {
				final ICProject project = inputCElement.getCProject();
				try {
					return findInput(project, editorInput, region);
				} catch (CoreException e) {
					CUIPlugin.log(e);
				}
			}
		}
		return null;
	}

	public static void open(final ITextEditor editor, final ITextSelection sel) {
		if (editor != null) {
			ICElement inputCElement = CUIPlugin.getDefault().getWorkingCopyManager()
					.getWorkingCopy(editor.getEditorInput());
			if (inputCElement != null) {
				final ICProject project = inputCElement.getCProject();
				final IEditorInput editorInput = editor.getEditorInput();
				final Display display = Display.getCurrent();

				Job job = new Job(Messages.TypeHierarchyUI_OpenTypeHierarchy) {
					@Override
					protected IStatus run(IProgressMonitor monitor) {
						try {
							StatusLineHandler.clearStatusLine(editor.getSite());
							IRegion reg = new Region(sel.getOffset(), sel.getLength());
							final ICElement[] elems = findInput(project, editorInput, reg);
							if (elems != null && elems.length == 2) {
								display.asyncExec(() -> openInViewPart(editor.getSite().getWorkbenchWindow(), elems[0],
										elems[1]));
							} else {
								StatusLineHandler.showStatusLineMessage(editor.getSite(),
										Messages.TypeHierarchyUI_OpenFailure_message);
							}
							return Status.OK_STATUS;
						} catch (CoreException e) {
							return e.getStatus();
						}
					}
				};
				job.setUser(true);
				job.schedule();
			}
		}
	}

	private static ICElement[] findInput(ICProject project, IEditorInput editorInput, IRegion sel)
			throws CoreException {
		try {
			IIndex index = CCorePlugin.getIndexManager().getIndex(project, INDEX_SEARCH_OPTION);

			index.acquireReadLock();
			try {
				IASTName name = IndexUI.getSelectedName(editorInput, sel);
				if (name != null) {
					IBinding binding = name.resolveBinding();
					if (!isValidInput(binding)) {
						return null;
					}
					ICElement member = null;
					if (!isValidTypeInput(binding)) {
						member = findDeclaration(project, index, name, binding);
						name = null;
						binding = findTypeBinding(binding);
					}
					if (isValidTypeInput(binding)) {
						ICElement input = findDefinition(project, index, name, binding);
						if (input != null) {
							return new ICElement[] { input, member };
						}
					}
				}
			} finally {
				index.releaseReadLock();
			}
		} catch (CoreException e) {
			CUIPlugin.log(e);
		} catch (InterruptedException e) {
		}
		return null;
	}

	private static ICElement[] findInput(ICElement member) {
		ICProject project = member.getCProject();
		try {
			IIndex index = CCorePlugin.getIndexManager().getIndex(project, INDEX_SEARCH_OPTION);
			index.acquireReadLock();
			try {
				IIndexName name = IndexUI.elementToName(index, member);
				if (name != null) {
					member = IndexUI.getCElementForName(project, index, name);
					IBinding binding = index.findBinding(name);
					binding = findTypeBinding(binding);
					if (isValidTypeInput(binding)) {
						ICElement input = findDefinition(project, index, null, binding);
						if (input != null) {
							return new ICElement[] { input, member };
						}
					}
				}
			} finally {
				index.releaseReadLock();
			}
		} catch (CoreException e) {
			CUIPlugin.log(e);
		} catch (InterruptedException e) {
		}
		return null;
	}

	private static IBinding findTypeBinding(IBinding memberBinding) {
		if (memberBinding instanceof IEnumerator) {
			IType type = ((IEnumerator) memberBinding).getType();
			if (type instanceof IBinding) {
				return (IBinding) type;
			}
		} else if (memberBinding instanceof ICPPMember) {
			return ((ICPPMember) memberBinding).getClassOwner();
		} else if (memberBinding instanceof IField) {
			return ((IField) memberBinding).getCompositeTypeOwner();
		}
		return null;
	}

	private static ICElement findDefinition(ICProject project, IIndex index, IASTName name, IBinding binding)
			throws CoreException {
		if (name != null && name.isDefinition()) {
			return IndexUI.getCElementForName(project, index, name);
		}

		ICElement[] elems = IndexUI.findAllDefinitions(index, binding);
		if (elems.length > 0) {
			return elems[0];
		}
		return IndexUI.findAnyDeclaration(index, project, binding);
	}

	private static ICElement findDeclaration(ICProject project, IIndex index, IASTName name, IBinding binding)
			throws CoreException {
		if (name != null && name.isDefinition()) {
			return IndexUI.getCElementForName(project, index, name);
		}

		ICElement[] elems = IndexUI.findAllDefinitions(index, binding);
		if (elems.length > 0) {
			return elems[0];
		}
		return IndexUI.findAnyDeclaration(index, project, binding);
	}

	public static boolean isValidInput(IBinding binding) {
		if (isValidTypeInput(binding) || binding instanceof ICPPMember || binding instanceof IEnumerator
				|| binding instanceof IField) {
			return true;
		}
		return false;
	}

	public static boolean isValidTypeInput(IBinding binding) {
		if (binding instanceof ICompositeType || binding instanceof IEnumeration || binding instanceof ITypedef) {
			return true;
		}
		return false;
	}

	public static boolean isValidInput(ICElement elem) {
		if (elem == null) {
			return false;
		}
		if (isValidTypeInput(elem)) {
			return true;
		}
		switch (elem.getElementType()) {
		case ICElement.C_FIELD:
		case ICElement.C_METHOD:
		case ICElement.C_METHOD_DECLARATION:
		case ICElement.C_TEMPLATE_METHOD:
		case ICElement.C_TEMPLATE_METHOD_DECLARATION:
		case ICElement.C_ENUMERATOR:
			return true;
		}
		return false;
	}

	public static boolean isValidTypeInput(ICElement elem) {
		if (elem == null) {
			return false;
		}
		switch (elem.getElementType()) {
		case ICElement.C_CLASS:
		case ICElement.C_STRUCT:
		case ICElement.C_UNION:
		case ICElement.C_CLASS_DECLARATION:
		case ICElement.C_STRUCT_DECLARATION:
		case ICElement.C_UNION_DECLARATION:
		case ICElement.C_ENUMERATION:
		case ICElement.C_TYPEDEF:
			//		case ICElement.C_TEMPLATE_CLASS:
			//		case ICElement.C_TEMPLATE_CLASS_DECLARATION:
			//		case ICElement.C_TEMPLATE_STRUCT:
			//		case ICElement.C_TEMPLATE_STRUCT_DECLARATION:
			//		case ICElement.C_TEMPLATE_UNION:
			//		case ICElement.C_TEMPLATE_UNION_DECLARATION:
			return true;
		}
		return false;
	}

	static String getLocalElementSignature(ICElement element) {
		if (element != null) {
			try {
				switch (element.getElementType()) {
				case ICElement.C_METHOD:
				case ICElement.C_METHOD_DECLARATION:
					return ((IFunctionDeclaration) element).getSignature();
				case ICElement.C_FIELD:
					return element.getElementName();
				}
			} catch (CModelException e) {
				CUIPlugin.log(e);
			}
		}
		return null;
	}
}

Back to the top