Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5edc83d29237903075211bf44b1a816db25b7db1 (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
/*******************************************************************************
 * Copyright (c) 2006, 2015 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.callhierarchy;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.eclipse.core.runtime.IAdaptable;

import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.IEnumerator;
import org.eclipse.cdt.core.model.IMacro;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.IVariableDeclaration;

/**
 * Represents a node in the include browser
 */
public class CHNode implements IAdaptable {
	private CHNode fParent;
	private ICElement fRepresentedDecl;
	private ITranslationUnit fFileOfReferences;
    private List<CHReferenceInfo> fReferences;

    protected int fHashCode;
    private long fTimestamp;
    private boolean fIsRecursive;
	private boolean fIsInitializer;
	private boolean fIsReadAccess;
	private boolean fIsWriteAccess;
	private final int fLinkageID;

    /**
     * Creates a new node for the include browser
     */
    public CHNode(CHNode parent, ITranslationUnit fileOfReferences, long timestamp, ICElement decl,
    		int linkageID) {
        fParent= parent;
        fFileOfReferences= fileOfReferences;
        fReferences= Collections.emptyList();
        fRepresentedDecl= decl;
        fIsRecursive= computeIsRecursive(fParent, decl);
        fHashCode= computeHashCode();
        fTimestamp= timestamp;
        fLinkageID= linkageID;
    }

	private int computeHashCode() {
        int hashCode= 1;
        if (fParent != null) {
            hashCode= fParent.hashCode() * 31;
        }
        if (fRepresentedDecl != null) {
        	hashCode+= fRepresentedDecl.hashCode();
        }
        return hashCode;
    }

    @Override
	public int hashCode() {
        return fHashCode;
    }

    @Override
	public boolean equals(Object o) {
		if (!(o instanceof CHNode)) {
			return false;
		}

		CHNode rhs = (CHNode) o;
		if (fHashCode != rhs.fHashCode) {
			return false;
		}

		return Objects.equals(fRepresentedDecl, rhs.fRepresentedDecl);
    }

    private boolean computeIsRecursive(CHNode parent, ICElement decl) {
        if (parent == null || decl == null) {
            return false;
        }
        if (decl.equals(parent.fRepresentedDecl)) {
            return true;
        }
        return computeIsRecursive(parent.fParent, decl);
    }

	/**
     * Returns the parent node or <code>null</code> for the root node.
     */
    public CHNode getParent() {
        return fParent;
    }

    public int getLinkageID() {
    	return fLinkageID;
    }

	public boolean isRecursive() {
        return fIsRecursive;
    }

	public int getReferenceCount() {
		return fReferences.size();
	}

	public CHReferenceInfo getReference(int idx) {
		return fReferences.get(idx);
	}

	public ICElement getRepresentedDeclaration() {
		return fRepresentedDecl;
	}

	public long getTimestamp() {
		return fTimestamp;
	}

	public boolean isMacro() {
		return fRepresentedDecl instanceof IMacro;
	}

	public boolean isVariableOrEnumerator() {
		return fRepresentedDecl instanceof IVariableDeclaration ||
			fRepresentedDecl instanceof IEnumerator;
	}

	public int getFirstReferenceOffset() {
		return fReferences.isEmpty() ? -1 : getReference(0).getOffset();
	}

	public void addReference(CHReferenceInfo info) {
		switch (fReferences.size()) {
		case 0:
			fReferences= Collections.singletonList(info);
			return;
		case 1:
			fReferences= new ArrayList<CHReferenceInfo>(fReferences);
			break;
		}
		fReferences.add(info);
	}

	public ITranslationUnit getFileOfReferences() {
		return fFileOfReferences;
	}

	@Override
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Object getAdapter(Class adapter) {
		if (adapter.isAssignableFrom(ICElement.class)) {
			return getRepresentedDeclaration();
		}
		return null;
	}

	public boolean isMultiDef() {
		return false;
	}

	public ICElement getOneRepresentedDeclaration() {
		return getRepresentedDeclaration();
	}

	public boolean isInitializer() {
		return fIsInitializer;
	}

	public void setInitializer(boolean isInitializer) {
		fIsInitializer = isInitializer;
	}

	public void sortReferencesByOffset() {
		if (fReferences.size() > 1) {
			Collections.sort(fReferences, CHReferenceInfo.COMPARE_OFFSET);
		}
	}

	public void setRWAccess(boolean readAccess, boolean writeAccess) {
		fIsReadAccess= readAccess;
		fIsWriteAccess= writeAccess;
	}

	public boolean isReadAccess() {
		return fIsReadAccess;
	}

	public boolean isWriteAccess() {
		return fIsWriteAccess;
	}
}

Back to the top