Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7f50ad5f082d5a4fbde098e2739210087ee2aacd (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
/*******************************************************************************
 * Copyright (c) 2005, 2011 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:
 *     Doug Schaefer (QNX) - Initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *     Andrew Ferguson (Symbian)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.pdom.dom;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IPDOMVisitor;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.cdt.internal.core.pdom.db.Database;
import org.eclipse.core.runtime.CoreException;

/**
 * This is a basic node in the PDOM database.
 * PDOM nodes form a multi-root tree with linkages being the roots.
 * This class managed the parent pointer.
 */
public abstract class PDOMNode implements IInternalPDOMNode {
	private static final int TYPE = 0;
	private static final int PARENT = 4;
	
	protected static final int RECORD_SIZE = 8;
	
	private final PDOMLinkage fLinkage;
	protected final long record;
	
	private volatile long cachedParentRecord;
	
	protected PDOMNode(PDOMLinkage linkage, long record) {
		fLinkage = linkage;
		this.record = record;
	}

	protected PDOMNode(PDOMLinkage linkage, PDOMNode parent) throws CoreException {
		this(linkage.getDB(), linkage, parent == null ? 0 : parent.getRecord());
	}

	/**
	 * For linkages, only.
	 */
	protected PDOMNode(Database db) throws CoreException {
		this(db, null, 0);
	}
	
	protected PDOMNode(Database db, PDOMLinkage linkage, long parentRec) throws CoreException {
		this.fLinkage = linkage;

		record = db.malloc(getRecordSize());
		db.putInt(record + TYPE, getNodeType());
		
		cachedParentRecord = parentRec;
		db.putRecPtr(record + PARENT, parentRec);
	}

	protected Database getDB() {
		return fLinkage.getDB();
	}

	public PDOM getPDOM() {
		return fLinkage.getPDOM();
	}
	
	public PDOMLinkage getLinkage() {
		return fLinkage;
	}

	protected abstract int getRecordSize();

	public abstract int getNodeType();

	@Override
	public final long getRecord() {
		return record;
	}
	
	public final long getBindingID() {
		return record;
	}

	/**
	 * Checks if <code>other</code> node is the immediate parent of this one.
	 * @param other paternity test subject.
	 * @return <code>true</code> if <code>other</code> node in the parent of this one.
	 */
	public boolean isChildOf(PDOMNode other) {
		try {
			return other.fLinkage == fLinkage && other.record == getParentNodeRec();
		} catch (CoreException e) {
			return false;
		}
	}

	@Override
	public final boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (obj instanceof PDOMNode) {
			PDOMNode other = (PDOMNode) obj;
			return getPDOM() == other.getPDOM() && record == other.record;
		}
		
		return super.equals(obj);
	}

	@Override
	public final int hashCode() {
		return System.identityHashCode(getPDOM()) + (int) (41 * record);
	}

	@Override
	public void accept(IPDOMVisitor visitor) throws CoreException {
		// No children here.
	}
	
	public static int getNodeType(Database db, long record) throws CoreException {
		return db.getInt(record + TYPE);
	}
	
	public long getParentNodeRec() throws CoreException {
		if (cachedParentRecord != 0) {
			return cachedParentRecord;
		}
		return cachedParentRecord= getDB().getRecPtr(record + PARENT);
	}
	
	public PDOMNode getParentNode() throws CoreException {
		long parentrec = getParentNodeRec();
		return parentrec != 0 ? getLinkage().getNode(parentrec) : null;
	}
	
	public void addChild(PDOMNode child) throws CoreException {
		// nothing here
	}
		
	/**
	 * Convenience method for fetching a byte from the database.
	 * @param offset Location of the byte.
	 * @return a byte from the database.
	 */
	protected byte getByte(long offset) {
		try {
			return getDB().getByte(offset);
		} catch (CoreException e) {
			CCorePlugin.log(e);
			return 0;
		}
	}

	/**
	 * Returns the bit at the specified offset in a bit vector.
	 * @param bitVector Bits.
	 * @param offset The position of the desired bit.
	 * @return the bit at the specified offset.
	 */
	protected static boolean getBit(int bitVector, int offset) {
		int mask = 1 << offset;
		return (bitVector & mask) != 0;
	}

	/**
	 * Delete this PDOMNode, make sure you are actually the owner of this record!
	 * @param linkage 
	 * @throws CoreException 
	 */
	@Override
	public void delete(PDOMLinkage linkage) throws CoreException {
		getDB().free(record);
	}
}

Back to the top