Skip to main content
summaryrefslogtreecommitdiffstats
blob: 52fc6379f339afb581d1105fbae179f742aa41f4 (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
/*******************************************************************************
 * Copyright (c) 2006, 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:
 * 	   QNX - Initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *     Sergey Prigogin (Google)
 *******************************************************************************/

package org.eclipse.cdt.internal.core.pdom.dom;

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.IIndexFileLocation;
import org.eclipse.cdt.internal.core.index.IIndexFragment;
import org.eclipse.cdt.internal.core.index.IIndexFragmentFile;
import org.eclipse.cdt.internal.core.index.IIndexFragmentInclude;
import org.eclipse.cdt.internal.core.pdom.db.Database;
import org.eclipse.core.runtime.CoreException;

/**
 * @author Doug Schaefer
 */
public class PDOMInclude implements IIndexFragmentInclude {

	private static final int INCLUDED_FILE		 	=  0;
	private static final int INCLUDED_BY 			=  4;
	private static final int INCLUDES_NEXT 			=  8;
	private static final int INCLUDED_BY_NEXT 		= 12;
	private static final int INCLUDED_BY_PREV 		= 16;
	// If the include name is the same as the end part of the path of the included file,
	// we store the length of the name instead of the name itself, and indicate that
	// by turning on FLAG_DEDUCIBLE_NAME flag. Notice that the length of include name
	// can be different from the node length, if the name is defined by a macro. 
	private static final int INCLUDE_NAME_OR_LENGTH = 20; // TODO: assumes that int and stored pointers are the same size
	private static final int NODE_OFFSET  			= 24; // 3-byte unsigned int (sufficient for files <= 16mb)
	private static final int NODE_LENGTH  			= 27; // short (sufficient for names <= 32k)
	private static final int FLAGS		 			= 29;
	private static final int RECORD_SIZE 			= 30;

	private static final int FLAG_SYSTEM_INCLUDE 		= 0x01;
	private static final int FLAG_INACTIVE_INCLUDE 		= 0x02;
	private static final int FLAG_RESOLVED_BY_HEURISTICS= 0x04;
	private static final int FLAG_DEDUCIBLE_NAME		= 0x08;
	
	private final PDOMLinkage linkage;
	private final long record;

	// Cached fields
	private String fName;

	public PDOMInclude(PDOMLinkage pdom, long record) {
		this.linkage = pdom;
		this.record = record;
	}

	public PDOMInclude(PDOMLinkage linkage, IASTPreprocessorIncludeStatement include, PDOMFile containerFile,
			PDOMFile targetFile) throws CoreException {
		this.linkage = linkage;
		this.record = linkage.getDB().malloc(RECORD_SIZE);
		IASTName name = include.getName();
		char[] nameChars = name.getSimpleID();
		IASTFileLocation loc = name.getFileLocation();
		// Includes generated by -include or -macro don't have a location
		if (loc != null) {
			linkage.getDB().put3ByteUnsignedInt(record + NODE_OFFSET, loc.getNodeOffset());
			linkage.getDB().putShort(record + NODE_LENGTH, (short) loc.getNodeLength());
		}

		final Database db = linkage.getDB();
		if (targetFile != null) {
			db.putRecPtr(record + INCLUDED_FILE, targetFile.getRecord());
		}
		boolean deducible_name = isDeducibleName(targetFile, nameChars);
		// If the name is the same as an end part of the path of the included file,
		// store the length of the name instead of the name itself.
		if (deducible_name) {
			db.putInt(record + INCLUDE_NAME_OR_LENGTH, nameChars.length);
		} else {
			db.putRecPtr(record + INCLUDE_NAME_OR_LENGTH, db.newString(nameChars).getRecord());
		}
		setFlag(encodeFlags(include, deducible_name));
		setIncludedBy(containerFile);
	}

	private byte encodeFlags(IASTPreprocessorIncludeStatement include, boolean deducible_name) {
		byte flags= 0;
		if (include.isSystemInclude()) {
			flags |= FLAG_SYSTEM_INCLUDE;
		}
		if (!include.isActive()) {
			flags |= FLAG_INACTIVE_INCLUDE;
		} else if (include.isResolvedByHeuristics()) {
			flags |= FLAG_RESOLVED_BY_HEURISTICS;
		}
		if (deducible_name) {
			flags |= FLAG_DEDUCIBLE_NAME;
		}
		return flags;
	}

	public long getRecord() {
		return record;
	}

	public void delete() throws CoreException {
		if (isResolved()) {
			// Remove us from the includedBy chain
			removeThisFromIncludedByChain();
		}
		final Database db = linkage.getDB();
		if ((getFlag() & FLAG_DEDUCIBLE_NAME) == 0) {
			long rec = db.getRecPtr(record + INCLUDE_NAME_OR_LENGTH);
			db.getString(rec).delete();
		}
		// Delete our record
		db.free(record);
	}

	private void removeThisFromIncludedByChain() throws CoreException {
		PDOMInclude prevInclude = getPrevInIncludedBy();
		PDOMInclude nextInclude = getNextInIncludedBy();
		if (prevInclude != null) {
			prevInclude.setNextInIncludedBy(nextInclude);
		} else {
			((PDOMFile) getIncludes()).setFirstIncludedBy(nextInclude);
		}

		if (nextInclude != null)
			nextInclude.setPrevInIncludedBy(prevInclude);
	}

	public IIndexFragmentFile getIncludes() throws CoreException {
		long rec = linkage.getDB().getRecPtr(record + INCLUDED_FILE);
		return rec != 0 ? new PDOMFile(linkage, rec) : null;
	}

	void setIncludes(PDOMFile includedFile) throws CoreException {
		long rec = includedFile != null ? includedFile.getRecord() : 0;
		linkage.getDB().putRecPtr(record + INCLUDED_FILE, rec);
	}

	/**
	 * Checks if the name is the same as the end part of the path of the included file.
	 */
	private static boolean isDeducibleName(PDOMFile includedFile, char[] name) throws CoreException {
		if (includedFile == null) {
			return false;
		}
		String s = includedFile.getLocation().getURI().getPath();
		int pos = s.length() - name.length;
		if (pos < 0) {
			return false;
		}
		for (int i = 0; i < name.length; i++, pos++) {
			if (s.charAt(pos) != name[i]) {
				return false;
			}
		}
		return true;
	}

	public IIndexFile getIncludedBy() throws CoreException {
		long rec = linkage.getDB().getRecPtr(record + INCLUDED_BY);
		return rec != 0 ? new PDOMFile(linkage, rec) : null;
	}

	void setIncludedBy(PDOMFile includedBy) throws CoreException {
		long rec = includedBy != null ? includedBy.getRecord() : 0;
		linkage.getDB().putRecPtr(record + INCLUDED_BY, rec);
	}

	public PDOMInclude getNextInIncludes() throws CoreException {
		long rec = linkage.getDB().getRecPtr(record + INCLUDES_NEXT);
		return rec != 0 ? new PDOMInclude(linkage, rec) : null;
	}

	public void setNextInIncludes(PDOMInclude include) throws CoreException {
		long rec = include != null ? include.getRecord() : 0;
		linkage.getDB().putRecPtr(record + INCLUDES_NEXT, rec);
	}

	public PDOMInclude getNextInIncludedBy() throws CoreException {
		long rec = linkage.getDB().getRecPtr(record + INCLUDED_BY_NEXT);
		return rec != 0 ? new PDOMInclude(linkage, rec) : null;
	}

	public void setNextInIncludedBy(PDOMInclude include) throws CoreException {
		long rec = include != null ? include.getRecord() : 0;
		linkage.getDB().putRecPtr(record + INCLUDED_BY_NEXT, rec);
	}

	public PDOMInclude getPrevInIncludedBy() throws CoreException {
		long rec = getPrevInIncludedByRecord();
		return rec != 0 ? new PDOMInclude(linkage, rec) : null;
	}

	long getPrevInIncludedByRecord() throws CoreException {
		return linkage.getDB().getRecPtr(record + INCLUDED_BY_PREV);
	}

	public void setPrevInIncludedBy(PDOMInclude include) throws CoreException {
		long rec = include != null ? include.getRecord() : 0;
		linkage.getDB().putRecPtr(record + INCLUDED_BY_PREV, rec);
	}

	public IIndexFileLocation getIncludedByLocation() throws CoreException {
		return getIncludedBy().getLocation();
	}

	public IIndexFileLocation getIncludesLocation() throws CoreException {
		IIndexFragmentFile includes = getIncludes();
		return includes != null ? includes.getLocation() : null;
	}

	public IIndexFragment getFragment() {
		return linkage.getPDOM();
	}

	private void setFlag(byte flag) throws CoreException {
		linkage.getDB().putByte(record + FLAGS, flag);
	}

	private int getFlag() throws CoreException {
		return linkage.getDB().getByte(record + FLAGS);
	}

	public boolean isSystemInclude() throws CoreException {
		return (getFlag() & FLAG_SYSTEM_INCLUDE) != 0;
	}

	public boolean isActive() throws CoreException {
		return (getFlag() & FLAG_INACTIVE_INCLUDE) == 0;
	}

	public boolean isResolved() throws CoreException {
		return linkage.getDB().getRecPtr(record + INCLUDED_FILE) != 0;
	}

	public boolean isResolvedByHeuristics() throws CoreException {
		return (getFlag() & FLAG_RESOLVED_BY_HEURISTICS) != 0;
	}

	public int getNameOffset() throws CoreException {
		return linkage.getDB().get3ByteUnsignedInt(record + NODE_OFFSET);
	}

	public int getNameLength() throws CoreException {
		return linkage.getDB().getShort(record + NODE_LENGTH) & 0xffff;
	}	

	public String getFullName() throws CoreException {
		if (fName == null) {
			final Database db = linkage.getDB();
			// The include name is either stored explicitly, or can be deduced from the path
			// of the included file.
			if ((getFlag() & FLAG_DEDUCIBLE_NAME) == 0) {
				long rec = db.getRecPtr(record + INCLUDE_NAME_OR_LENGTH);
				fName = db.getString(rec).getString();
			} else {
				String path = getIncludes().getLocation().getURI().getPath();
				int nameLength = db.getInt(record + INCLUDE_NAME_OR_LENGTH);
				fName = path.substring(Math.max(path.length() - nameLength, 0));
			}
		}
		return fName;
	}

	public String getName() throws CoreException {
		final String fullName= getFullName();
		final int idx= Math.max(fullName.lastIndexOf('/'), fullName.lastIndexOf('\\'));
		return fullName.substring(idx + 1);
	}

	public void convertToUnresolved() throws CoreException {
		if (isResolved()) {
			final Database db = linkage.getDB();
			int flag = getFlag();
			// Since included file is going away, the name is no longer deducible.
			if ((flag & FLAG_DEDUCIBLE_NAME) != 0) {
				long rec= db.newString(getFullName()).getRecord();
				db.putRecPtr(record + INCLUDE_NAME_OR_LENGTH, rec);
				setFlag((byte) (flag & ~FLAG_DEDUCIBLE_NAME));
			}
			db.putRecPtr(record + INCLUDED_FILE, 0);
		}
	}
}

Back to the top