Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ce76be4612e796973946ba82cac56b1b78cec1fc (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
/*******************************************************************************
 * Copyright (c) 2007, 2008 Wind River Systems, Inc. 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:
 *    Markus Schorn - initial API and implementation
 *******************************************************************************/ 
package org.eclipse.cdt.internal.core.parser.scanner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit.IDependencyTree.IASTInclusionNode;

/**
 * Base class for all location contexts that can contain children. 
 * <p>
 * @since 5.0
 */
class LocationCtxContainer extends LocationCtx {
	/**
	 * The total length of all children in terms of sequence numbers.
	 */
	private int fChildSequenceLength;

	private ArrayList<LocationCtx> fChildren;
	private char[] fSource;
	private int[] fLineOffsets;
	
	public LocationCtxContainer(LocationCtxContainer parent, char[] source, int parentOffset, int parentEndOffset, int sequenceNumber) {
		super(parent, parentOffset, parentEndOffset, sequenceNumber);
		fSource= source;
	}
	
	@Override
	public Collection<LocationCtx> getChildren() {
		if (fChildren == null) {
			return Collections.emptyList();
		}
		return fChildren;
	}

	public void addChild(LocationCtx locationCtx) {
		if (fChildren == null) {
			fChildren= new ArrayList<LocationCtx>();
		}
		fChildren.add(locationCtx);
	}

	public char[] getSource(int offset, int length) {
		offset= Math.max(0, Math.min(offset, fSource.length));
		length= Math.max(0, Math.min(length, fSource.length-offset));
		char[] result= new char[length];
		System.arraycopy(fSource, offset, result, 0, length);
		return result;
	}

	@Override
	public final int getSequenceLength() {
		return fSource.length + fChildSequenceLength;
	}
	
	@Override
	public final int getSequenceNumberForOffset(int offset, boolean checkChildren) {
		int result= fSequenceNumber + fChildSequenceLength + offset;
		if (checkChildren && fChildren != null) {
			for (int i= fChildren.size()-1; i >= 0; i--) {
				final LocationCtx child= fChildren.get(i);
				if (child.fEndOffsetInParent > offset) {	// child was inserted behind the offset, adjust sequence number
					result-= child.getSequenceLength();
				}
				else {
					return result;
				}
			}
		}
		return result;
	}
	
	@Override
	public void addChildSequenceLength(int childLength) {
		fChildSequenceLength+= childLength;
	}

	@Override
	public final LocationCtx findSurroundingContext(int sequenceNumber, int length) {
		int testEnd= length > 1 ? sequenceNumber+length-1 : sequenceNumber;
		final LocationCtx child= findChildLessOrEqualThan(sequenceNumber, false);
		if (child != null && child.fSequenceNumber+child.getSequenceLength() > testEnd) {
			return child.findSurroundingContext(sequenceNumber, length);
		}
		return this;
	}

	@Override
	public final LocationCtxMacroExpansion findEnclosingMacroExpansion(int sequenceNumber, int length) {
		int testEnd= length > 1 ? sequenceNumber+length-1 : sequenceNumber;
		final LocationCtx child= findChildLessOrEqualThan(sequenceNumber, true);
		if (child != null && child.fSequenceNumber+child.getSequenceLength() > testEnd) {
			return child.findEnclosingMacroExpansion(sequenceNumber, length);
		}
		return null;
	}

	@Override
	public ASTFileLocation findMappedFileLocation(int sequenceNumber, int length) {
		// try to delegate to a child.
		int testEnd= length > 1 ? sequenceNumber+length-1 : sequenceNumber;
		final LocationCtx child= findChildLessOrEqualThan(sequenceNumber, false);
		if (child != null && child.fSequenceNumber+child.getSequenceLength() > testEnd) {
			return child.findMappedFileLocation(sequenceNumber, length);
		}
		return super.findMappedFileLocation(sequenceNumber, length);
	}

	@Override
	public boolean collectLocations(int sequenceNumber, final int length, ArrayList<IASTNodeLocation> locations) {
		final int endSequenceNumber= sequenceNumber+length;
		if (fChildren != null) {
			int childIdx= Math.max(0, findChildIdxLessOrEqualThan(sequenceNumber, false));
			for (; childIdx < fChildren.size(); childIdx++) {
				final LocationCtx child= fChildren.get(childIdx);

				// create the location between start and the child
				if (sequenceNumber < child.fSequenceNumber) {
					// compute offset backwards from the child's offset
					final int offset= child.fEndOffsetInParent - (child.fSequenceNumber - sequenceNumber);
					// it the child is not affected, we are done.
					if (endSequenceNumber <= child.fSequenceNumber) {
						addFileLocation(offset, endSequenceNumber-sequenceNumber, locations);
						return true;
					}
					addFileLocation(offset, child.fOffsetInParent-offset, locations);
					sequenceNumber= child.fSequenceNumber;
				}

				// let the child create locations
				final int childEndSequenceNumber= child.fSequenceNumber + child.getSequenceLength();
				if (sequenceNumber < childEndSequenceNumber) {
					if (child.collectLocations(sequenceNumber, endSequenceNumber-sequenceNumber, locations)) {
						return true;
					}
					sequenceNumber= childEndSequenceNumber;
				}
			}
		}

		// create the location after the last child.
		final int myEndNumber = fSequenceNumber + getSequenceLength();
		final int offset= fSource.length - (myEndNumber - sequenceNumber);
		if (endSequenceNumber <= myEndNumber) {
			addFileLocation(offset, endSequenceNumber-sequenceNumber, locations);
			return true;
		}
		addFileLocation(offset, fSource.length-offset, locations);
		return false;
	}
	
	private ArrayList<IASTNodeLocation> addFileLocation(int offset, int length, ArrayList<IASTNodeLocation> sofar) {
		IASTFileLocation loc= createFileLocation(offset, length);
		if (loc != null) {
			sofar.add(loc);
		}
		return sofar;
	}

	ASTFileLocation createFileLocation(int start, int length) {
		return null;
	}

	final int findChildIdxLessOrEqualThan(int sequenceNumber, boolean beforeReplacedChars) {
		if (fChildren == null) {
			return -1;
		}
		int upper= fChildren.size();
		int lower= 0;
		while (upper > lower) {
			int middle= (upper+lower)/2;
			LocationCtx child= fChildren.get(middle);
			int childSequenceNumber= child.fSequenceNumber;
			if (beforeReplacedChars) {
				childSequenceNumber-= child.fEndOffsetInParent-child.fOffsetInParent; 
			}
			if (childSequenceNumber <= sequenceNumber) {
				lower= middle+1;
			}
			else {
				upper= middle;
			}
		}
		return lower-1;
	}

	final LocationCtx findChildLessOrEqualThan(final int sequenceNumber, boolean beforeReplacedChars) {
		final int idx= findChildIdxLessOrEqualThan(sequenceNumber, beforeReplacedChars);
		return idx >= 0 ? fChildren.get(idx) : null;
	}

	@Override
	public void getInclusions(ArrayList<IASTInclusionNode> result) {
		if (fChildren != null) {
			for (LocationCtx ctx : fChildren) {
				if (ctx.getInclusionStatement() != null) {
					result.add(new ASTInclusionNode(ctx));
				}
				else {
					ctx.getInclusions(result);
				}
			}
		}
	}
	
	@Override
	public int getLineNumber(int offset) {
		if (fLineOffsets == null) {
			fLineOffsets= computeLineOffsets();
		}
		int idx= Arrays.binarySearch(fLineOffsets, offset);
		if (idx < 0) {
			return -idx;
		}
		return idx+1;
	}

	private int[] computeLineOffsets() {
		ArrayList<Integer> offsets= new ArrayList<Integer>();
		for (int i = 0; i < fSource.length; i++) {
			if (fSource[i] == '\n') {
				offsets.add(new Integer(i));
			}
		}
		int[] result= new int[offsets.size()];
		for (int i = 0; i < result.length; i++) {
			result[i]= offsets.get(i).intValue();
			
		}
		return result;
	}
}

Back to the top