Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d222425d19c584d7d90a69b39acf814c33ec6f3a (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*******************************************************************************
 * Copyright (c) 2005, 2010 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)
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.pdom.dom;

import java.util.ArrayList;

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.core.dom.ast.IASTPreprocessorIncludeStatement;
import org.eclipse.cdt.core.index.IIndexFile;
import org.eclipse.cdt.core.index.IIndexName;
import org.eclipse.cdt.core.index.IndexLocationFactory;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.index.IIndexFragment;
import org.eclipse.cdt.internal.core.index.IIndexFragmentName;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.cdt.internal.core.pdom.db.Database;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;


public final class PDOMName implements IIndexFragmentName, IASTFileLocation {
	private final PDOMLinkage linkage;
	private final long record;
	
	private static final int FILE_REC_OFFSET     = 0;
	private static final int FILE_NEXT_OFFSET	 = 4;
	private static final int CALLER_REC_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; // 3-byte unsigned int (sufficient for files <= 16mb)
	private static final int NODE_LENGTH_OFFSET  = 27; // short (sufficient for names <= 32k)
	private static final int FLAGS 				 = 29; 

	private static final int RECORD_SIZE = 30;	// 30 yields a 32-byte block. (31 would trigger a 40-byte block)

	public static final int IS_DECLARATION 						= 0x01;
	public static final int IS_DEFINITION 						= 0x02;
	public static final int IS_REFERENCE 						= IS_DECLARATION | IS_DEFINITION;
	public static final int DECL_DEF_REF_MASK					= IS_DECLARATION | IS_DEFINITION | IS_REFERENCE;
	public static final int INHERIT_FRIEND_INLINE_MASK	    	= 0x0C;
	public static final int IS_INHERITANCE_SPEC 				= 0x04;
	public static final int IS_FRIEND_SPEC						= 0x08;
	public static final int IS_INLINE_NAMESPACE					= 0x0C;
	public static final int COULD_BE_POLYMORPHIC_METHOD_CALL	= 0x10;
	public static final int READ_ACCESS 						= 0x20;
	public static final int WRITE_ACCESS 						= 0x40;

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

		// What kind of name are we
		int flags= getRoleOfName(name);
		
		flags |= binding.getAdditionalNameFlags(flags, name);
		db.putByte(record + FLAGS, (byte) flags);

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

		db.putRecPtr(record + BINDING_REC_OFFSET, binding.getRecord());
		
		db.putRecPtr(record + FILE_REC_OFFSET, file.getRecord());
		if (caller != null) {
			db.putRecPtr(record + CALLER_REC_OFFSET, caller.getRecord());
		}

		// Record our location in the file
		IASTFileLocation fileloc = name.getFileLocation();
		db.put3ByteUnsignedInt(record + NODE_OFFSET_OFFSET, fileloc.getNodeOffset());
		db.putShort(record + NODE_LENGTH_OFFSET, (short) fileloc.getNodeLength());
	}

	private int getRoleOfName(IASTName name) {
		if (name.isDefinition()) {
			return IS_DEFINITION;
		}  
		if (name.isDeclaration()) {
			return IS_DECLARATION;
		} 
		return IS_REFERENCE;
	}
	
	public PDOMName(PDOMLinkage linkage, long nameRecord) {
		this.linkage = linkage;
		this.record = nameRecord;
	}
	
	public long getRecord() {
		return record;
	}

	private long getRecField(int offset) throws CoreException {
		return linkage.getDB().getRecPtr(record + offset);
	}

	private void setRecField(int offset, long fieldrec) throws CoreException {
		linkage.getDB().putRecPtr(record + offset, fieldrec);
	}

	public PDOM getPDOM() {
		return linkage.getPDOM();
	}
	
	@Override
	public PDOMBinding getBinding() throws CoreException {
		long bindingrec = getRecField(BINDING_REC_OFFSET);
		return linkage.getBinding(bindingrec);
	}

	public void setBinding(PDOMBinding binding) throws CoreException {
		long bindingrec = binding != null ? binding.getRecord() : 0;
		setRecField(BINDING_REC_OFFSET, bindingrec);
	}

	private PDOMName getNameField(int offset) throws CoreException {
		long namerec = getRecField(offset);
		return namerec != 0 ? new PDOMName(linkage, namerec) : null;
	}

	private void setNameField(int offset, PDOMName name) throws CoreException {
		long 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);
	}
	
	@Override
	public PDOMFile getFile() throws CoreException {
		long filerec = linkage.getDB().getRecPtr(record + FILE_REC_OFFSET);
		return filerec != 0 ? new PDOMFile(linkage, filerec) : null;
	}

	public long getFileRecord() throws CoreException {
		return linkage.getDB().getRecPtr(record + FILE_REC_OFFSET);
	}

	void setFile(PDOMFile file) throws CoreException {
		linkage.getDB().putRecPtr(record + FILE_REC_OFFSET, file != null ? file.getRecord() : 0);
	}

	@Override
	public IIndexName getEnclosingDefinition() throws CoreException {
		long namerec = getEnclosingDefinitionRecord();
		return namerec != 0 ? new PDOMName(linkage, namerec) : null;
	}

	long getEnclosingDefinitionRecord() throws CoreException {
		return linkage.getDB().getRecPtr(record + CALLER_REC_OFFSET);
	}
	
	public PDOMName getNextInFile() throws CoreException {
		return getNameField(FILE_NEXT_OFFSET);
	}
	
	public void setNextInFile(PDOMName name) throws CoreException {
		setNameField(FILE_NEXT_OFFSET, name);
	}

	/**
	 * @deprecated use {@link #getSimpleID()}, instead.
	 */
	@Override
	@Deprecated
	public char[] toCharArray() {
		return getSimpleID();
	}

	@Override
	public char[] getSimpleID() {
		try {
			Database db = linkage.getDB();
			long bindingRec = db.getRecPtr(record + BINDING_REC_OFFSET);
			IPDOMBinding binding = linkage.getBinding(bindingRec);
			return binding != null ? binding.getNameCharArray() : null;
		} catch (CoreException e) {
			CCorePlugin.log(e);
			return CharArrayUtils.EMPTY;
		}
	}

	@Override
	public String toString() {
		return new String(getSimpleID());
	}
	
	private int getFlags(int mask) throws CoreException {
		return linkage.getDB().getByte(record + FLAGS) & mask;
	}

	public void setIsFriendSpecifier() throws CoreException {
		int flags= linkage.getDB().getByte(record + FLAGS) & 0xff;
		flags |= IS_FRIEND_SPEC;
		linkage.getDB().putByte(record + FLAGS, (byte) flags);
	}

	public void setIsBaseSpecifier() throws CoreException {
		int flags= linkage.getDB().getByte(record + FLAGS) & 0xff;
		flags |= IS_INHERITANCE_SPEC;
		linkage.getDB().putByte(record + FLAGS, (byte) flags);
	}

	public void setIsInlineNamespace() throws CoreException {
		int flags= linkage.getDB().getByte(record + FLAGS) & 0xff;
		flags |= IS_INLINE_NAMESPACE;
		linkage.getDB().putByte(record + FLAGS, (byte) flags);
	}

	public boolean isFriendSpecifier() throws CoreException {
		return getFlags(INHERIT_FRIEND_INLINE_MASK) == IS_FRIEND_SPEC;
	}

	@Override
	public boolean isBaseSpecifier() throws CoreException {
		return getFlags(INHERIT_FRIEND_INLINE_MASK) == IS_INHERITANCE_SPEC;
	}
	
	@Override
	public boolean isInlineNamespaceDefinition() throws CoreException {
		return getFlags(INHERIT_FRIEND_INLINE_MASK) == IS_INLINE_NAMESPACE;
	}

	@Override
	public boolean couldBePolymorphicMethodCall() throws CoreException {
		return getFlags(COULD_BE_POLYMORPHIC_METHOD_CALL) == COULD_BE_POLYMORPHIC_METHOD_CALL;
	}

	@Override
	public boolean isReadAccess() throws CoreException {
		return getFlags(READ_ACCESS) == READ_ACCESS;
	}

	@Override
	public boolean isWriteAccess() throws CoreException {
		return getFlags(WRITE_ACCESS) == WRITE_ACCESS;
	}

	@Override
	public boolean isDeclaration() {
		try {
			int flags = getFlags(DECL_DEF_REF_MASK);
			return flags == IS_DECLARATION || flags == IS_DEFINITION;
		} catch (CoreException e) {
			CCorePlugin.log(e);
			return false;
		}
	}

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

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

	@Override
	public IASTFileLocation getFileLocation() {
		return this;
	}

	@Override
	public String getFileName() {
		try {
			IIndexFile file = getFile();
			if (file == null) {
				return null;
			}
			// We need to specify what this method can return to know
			// how to implement this. Existing implementations return
			// the absolute path, so here we attempt to do the same.
			IPath location = IndexLocationFactory.getAbsolutePath(file.getLocation());
			return location != null ? location.toOSString() : null;
		} catch (CoreException e) {
			CCorePlugin.log(e);
		}
		return null;
	}

	@Override
	public int getStartingLineNumber() {
		return 0;
	}

	@Override
	public int getEndingLineNumber() {
		return 0;
	}

	@Override
	public IASTPreprocessorIncludeStatement getContextInclusionStatement() {
		return null;
	}

	@Override
	public IASTFileLocation asFileLocation() {
		return this;
	}

	@Override
	public int getNodeLength() {
		try {
			return (linkage.getDB().getShort(record + NODE_LENGTH_OFFSET)) & 0xffff;
		} catch (CoreException e) {
			CCorePlugin.log(e);
			return 0;
		}
	}

	@Override
	public int getNodeOffset() {
		try {
			return linkage.getDB().get3ByteUnsignedInt(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(DECL_DEF_REF_MASK)) {
			case IS_DECLARATION:
				getBinding().setFirstDeclaration(nextName);
				break;
			case IS_DEFINITION:
				getBinding().setFirstDefinition(nextName);
				break;
			case IS_REFERENCE:
				getBinding().setFirstReference(nextName);
				break;
			}
		}

		if (nextName != null)
			nextName.setPrevInBinding(prevName);

		// Delete our record
		linkage.getDB().free(record);
	}

	@Override
	public IIndexFragment getIndexFragment() {
		return linkage.getPDOM();
	}

	@Override
	public IIndexName[] getEnclosedNames() throws CoreException {
		ArrayList<PDOMName> result= new ArrayList<PDOMName>();
		PDOMName name= getNextInFile();
		while (name != null) {
			if (name.getEnclosingDefinitionRecord() == record) {
				result.add(name);
			}
			name= name.getNextInFile();
		}
		return result.toArray(new PDOMName[result.size()]);
	}
}

Back to the top