Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e0c78657384508664dcc32f43cb0676dcfb466b4 (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
/*******************************************************************************
 * Copyright (c) 2012, 2014 Google, 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:
 * 	   Sergey Prigogin (Google) - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.includes;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IMacroBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding;
import org.eclipse.cdt.core.index.IIndexFile;
import org.eclipse.cdt.core.parser.util.StringUtil;
import org.eclipse.core.runtime.IPath;

class InclusionRequest {
	private static final String UNINITIALIZED = "uninitialized"; //$NON-NLS-1$

	private final IBinding fBinding;
	private final Map<IIndexFile, IPath> fDeclaringFiles;
	private final boolean fReachable;
	private List<IPath> fCandidatePaths;
	private IPath fResolvedPath;
	private String fQualifiedName = UNINITIALIZED;

	/**
	 * @param binding the binding that requires inclusion
	 * @param declaringHeaders headers that can be included to declare the binding and paths
	 *     that can be used to include them
	 * @param reachable indicates whether the headers were previously included or not
	 */
	public InclusionRequest(IBinding binding, Map<IIndexFile, IPath> declaringHeaders, boolean reachable) {
		fBinding = binding;
		fDeclaringFiles = Collections.unmodifiableMap(declaringHeaders);
		fReachable = reachable;
		fCandidatePaths = new ArrayList<>(new HashSet<>(fDeclaringFiles.values()));
	}

	public IBinding getBinding() {
		return fBinding;
	}

	/**
	 * Returns the qualified name of the binding, or {@code null} if the binding doesn't have
	 * a qualified name.
	 */
	public String getBindingQualifiedName() {
		if (fQualifiedName == UNINITIALIZED) {
			fQualifiedName = null;
			if (fBinding instanceof ICPPBinding) {
				ICPPBinding cppBinding = (ICPPBinding) fBinding;
				try {
					if (cppBinding.isGloballyQualified()) {
						fQualifiedName = StringUtil.join(cppBinding.getQualifiedName(), "::"); //$NON-NLS-1$
					}
				} catch (DOMException e) {
					// Leave null;
				}
			} else if (fBinding instanceof IMacroBinding || fBinding.getOwner() == null) {
				fQualifiedName = fBinding.getName();
			}
		}
		return fQualifiedName;
	}

	public Map<IIndexFile, IPath> getDeclaringFiles() {
		return fDeclaringFiles;
	}

	public List<IPath> getCandidatePaths() {
		return fCandidatePaths;
	}

	public void setCandidatePaths(List<IPath> paths) {
		fCandidatePaths = paths;
	}

	public boolean isReachable() {
		return fReachable;
	}

	public void resolve(IPath path) {
		if (fResolvedPath != null)
			throw new IllegalStateException();
		fResolvedPath = path;
	}

	public IPath getResolvedPath() {
		return fResolvedPath;
	}

	public boolean isResolved() {
		return fResolvedPath != null;
	}

	/** For debugging only */
	@Override
	public String toString() {
		StringBuilder buf = new StringBuilder();
		buf.append(fBinding.getName());
		buf.append(" defined in "); //$NON-NLS-1$
		for (int i = 0; i < fCandidatePaths.size(); i++) {
			if (i != 0)
				buf.append(", "); //$NON-NLS-1$
			buf.append(fCandidatePaths.get(i).toOSString());
		}
		if (fResolvedPath != null) {
			buf.append(" represented by "); //$NON-NLS-1$
			buf.append(fResolvedPath.toOSString());
		}
		return buf.toString();
	}
}

Back to the top