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

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.internal.core.index.IIndexFragment;
import org.eclipse.cdt.internal.core.index.IIndexFragmentName;
import org.eclipse.cdt.internal.core.index.IIndexProxyBinding;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.cdt.internal.core.pdom.db.Database;
import org.eclipse.core.runtime.CoreException;

/**
 * @author Doug Schaefer
 *
 */
public class PDOMName implements IIndexFragmentName, IASTFileLocation {

	private final PDOM pdom;
	private final int record;
	
	private static final int FILE_REC_OFFSET     = 0;
	private static final int FILE_PREV_OFFSET 	 = 4;
	private static final int FILE_NEXT_OFFSET	 = 8;
	private static final int BINDING_REC_OFFSET  = 12;
	private static final int BINDING_PREV_OFFSET = 16;
	private static final int BINDING_NEXT_OFFSET = 20;
	private static final int NODE_OFFSET_OFFSET  = 24;
	private static final int NODE_LENGTH_OFFSET  = 28;
	private static final int FLAGS 				 = 32;

	private static final int RECORD_SIZE = 33;

	private static final int IS_DECLARATION = 1;
	private static final int IS_DEFINITION = 2;
	private static final int IS_REFERENCE = 3;
	

	public PDOMName(PDOM pdom, IASTName name, PDOMFile file, PDOMBinding binding) throws CoreException {
		this.pdom = pdom;
		Database db = pdom.getDB();
		record = db.malloc(RECORD_SIZE);

		// What kind of name are we
		byte flags = 0;
		if (name.isDefinition())
			flags = IS_DEFINITION;
		else if (name.isDeclaration())
			flags = IS_DECLARATION;
		else 
			flags = IS_REFERENCE;
		db.putByte(record + FLAGS, flags);

		// Hook us up to the binding
		if (binding != null) {
			switch (flags) {
			case IS_DEFINITION:
				binding.addDefinition(this);
				break;
			case IS_DECLARATION:
				binding.addDeclaration(this);
				break;
			case IS_REFERENCE:
				binding.addReference(this);
				break;
			}

			db.putInt(record + BINDING_REC_OFFSET, binding.getRecord());
		}
		
		// Hook us up the the liked name list from file
		db.putInt(record + FILE_REC_OFFSET, file.getRecord());
		file.addName(this);

		// Record our location in the file
		IASTFileLocation fileloc = name.getFileLocation();
		db.putInt(record + NODE_OFFSET_OFFSET, fileloc.getNodeOffset());
		db.putInt(record + NODE_LENGTH_OFFSET, fileloc.getNodeLength());
	}
	
	public PDOMName(PDOM pdom, int nameRecord) {
		this.pdom = pdom;
		this.record = nameRecord;
	}
	
	public int getRecord() {
		return record;
	}

	private int getRecField(int offset) throws CoreException {
		return pdom.getDB().getInt(record + offset);
	}
	
	private void setRecField(int offset, int fieldrec) throws CoreException {
		pdom.getDB().putInt(record + offset, fieldrec);
	}
	
	public PDOMBinding getPDOMBinding() throws CoreException {
		int bindingrec = getRecField(BINDING_REC_OFFSET);
		return pdom.getBinding(bindingrec);
	}
	
	public void setBinding(PDOMBinding binding) throws CoreException {
		int bindingrec = binding != null ? binding.getRecord() : 0;
		setRecField(BINDING_REC_OFFSET, bindingrec);
	}

	private PDOMName getNameField(int offset) throws CoreException {
		int namerec = getRecField(offset);
		return namerec != 0 ? new PDOMName(pdom, namerec) : null;
	}
	
	private void setNameField(int offset, PDOMName name) throws CoreException {
		int namerec = name != null ? name.getRecord() : 0;
		setRecField(offset, namerec);
	}
	
	public PDOMName getPrevInBinding() throws CoreException {
		return getNameField(BINDING_PREV_OFFSET);
	}
	
	public void setPrevInBinding(PDOMName name) throws CoreException {
		setNameField(BINDING_PREV_OFFSET, name);
	}

	public PDOMName getNextInBinding() throws CoreException {
		return getNameField(BINDING_NEXT_OFFSET);
	}
	
	public void setNextInBinding(PDOMName name) throws CoreException {
		setNameField(BINDING_NEXT_OFFSET, name);
	}
	
	public PDOMFile getFile() throws CoreException {
		int filerec = pdom.getDB().getInt(record + FILE_REC_OFFSET);
		return filerec != 0 ? new PDOMFile(pdom, filerec) : null;
	}
	
	public PDOMName getNextInFile() throws CoreException {
		return getNameField(FILE_NEXT_OFFSET);
	}
	
	public void setNextInFile(PDOMName name) throws CoreException {
		setNameField(FILE_NEXT_OFFSET, name);
	}
	
	public PDOMName getPrevInFile() throws CoreException {
		return getNameField(FILE_PREV_OFFSET);
	}

	public void setPrevInFile(PDOMName name) throws CoreException {
		setNameField(FILE_PREV_OFFSET, name);
	}
	
	public PDOMBinding resolveBinding() {
		try {
			int bindingRecord = pdom.getDB().getInt(record + BINDING_REC_OFFSET);
			return pdom.getBinding(bindingRecord);
		} catch (CoreException e) {
			CCorePlugin.log(e);
			return null;
		}
	}

	public char[] toCharArray() {
		try {
			Database db = pdom.getDB();
			int bindingRec = db.getInt(record + BINDING_REC_OFFSET);
			PDOMBinding binding = pdom.getBinding(bindingRec);
			return binding != null ? binding.getNameCharArray() : null;
		} catch (CoreException e) {
			CCorePlugin.log(e);
			return null;
		}
	}

	private byte getFlags() throws CoreException {
		return pdom.getDB().getByte(record + FLAGS);
	}
	
	public boolean isDeclaration() {
		try {
			byte flags = getFlags();
			return flags == IS_DECLARATION || flags == IS_DEFINITION;
		} catch (CoreException e) {
			CCorePlugin.log(e);
			return false;
		}
	}

	public boolean isReference() {
		try {
			byte flags = getFlags();
			return flags == IS_REFERENCE;
		} catch (CoreException e) {
			CCorePlugin.log(e);
			return false;
		}
	}

	public boolean isDefinition() {
		try {
			byte flags = getFlags();
			return flags == IS_DEFINITION;
		} catch (CoreException e) {
			CCorePlugin.log(e);
			return false;
		}
	}

	public IASTFileLocation getFileLocation() {
		return this;
	}

	public String getFileName() {
		try {
			PDOMFile file = getFile();
			return file != null ? file.getFileName().getString() : null;
		} catch (CoreException e) {
			CCorePlugin.log(e);
			return null;
		}
	}

	public int getStartingLineNumber() {
		return 0;
	}

	public int getEndingLineNumber() {
		return 0;
	}

	public IASTFileLocation asFileLocation() {
		return this;
	}

	public int getNodeLength() {
		try {
			return pdom.getDB().getInt(record + NODE_LENGTH_OFFSET);
		} catch (CoreException e) {
			CCorePlugin.log(e);
			return 0;
		}
	}

	public int getNodeOffset() {
		try {
			return pdom.getDB().getInt(record + NODE_OFFSET_OFFSET);
		} catch (CoreException e) {
			CCorePlugin.log(e);
			return 0;
		}
	}

	public void delete() throws CoreException {
		// Delete from the binding chain
		PDOMName prevName = getPrevInBinding();
		PDOMName nextName = getNextInBinding();
		if (prevName != null)
			prevName.setNextInBinding(nextName);
		else {
			switch (getFlags()) {
			case IS_DECLARATION:
				getPDOMBinding().setFirstDeclaration(nextName);
				break;
			case IS_DEFINITION:
				getPDOMBinding().setFirstDefinition(nextName);
				break;
			case IS_REFERENCE:
				getPDOMBinding().setFirstReference(nextName);
				break;
			}
		}
		
		if (nextName != null)
			nextName.setPrevInBinding(prevName);
		
		// Delete our record
		pdom.getDB().free(record);
	}

	public IIndexFragment getIndexFragment() {
		return pdom;
	}
	
	public IIndexProxyBinding getBinding() throws CoreException {
		return getPDOMBinding();
	}
}

Back to the top