Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bd450f3b2442122b1761ca611815a7b804df8303 (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
/*******************************************************************************
 * Copyright (c) 2008, 2013 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.dom.parser;

import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTImageLocation;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroExpansion;

/**
 * For searching ast-nodes by offset and length, instances of this class can be used to determine
 * whether a node matches or not.
 *
 * @since 5.0
 */
public class ASTNodeSpecification<T extends IASTNode> {
	public enum Relation { FIRST_CONTAINED, EXACT_MATCH, ENCLOSING, STRICTLY_ENCLOSING }

	private final Class<T> fClass;
	private final Relation fRelation;
	private final int fFileOffset;
	private final int fFileEndOffset;
	private int fSeqNumber;
	private int fSeqEndNumber;
	private int fBestOffset;
	private int fBestEndOffset;
	private T fBestNode;
	private boolean fSearchInExpansion;
	private boolean fZeroToLeft;

	public ASTNodeSpecification(Relation relation, Class<T> clazz, int fileOffset, int fileLength) {
		fRelation= relation;
		fClass= clazz;
		fFileOffset= fileOffset;
		fFileEndOffset= fileOffset + fileLength;
	}

	public void setRangeInSequence(int offsetInSeq, int lengthInSeq) {
		fSeqNumber= offsetInSeq;
		fSeqEndNumber= offsetInSeq + lengthInSeq;
	}

	public void setRangeInSequence(int offsetInSeq, int lengthInSeq, boolean zeroRangeToLeft) {
		setRangeInSequence(offsetInSeq, lengthInSeq);
		fZeroToLeft= zeroRangeToLeft;
	}

	public void setSearchInExpansion(boolean searchInExpansion) {
		fSearchInExpansion= searchInExpansion;
	}

	public Relation getRelationToSelection() {
		return fRelation;
	}

	public int getSequenceStart() {
		return fSeqNumber;
	}

	public int getSequenceEnd() {
		return fSeqEndNumber;
	}

	public T getBestNode() {
		return fBestNode;
	}

	public boolean requiresClass(Class<? extends IASTNode> clazz) {
		return clazz.isAssignableFrom(fClass);
	}

	@SuppressWarnings("unchecked")
	public void visit(ASTNode astNode) {
		if (isAcceptableNode(astNode) &&
				isMatchingRange(astNode.getOffset(), astNode.getLength(), fSeqNumber, fSeqEndNumber)) {
			IASTFileLocation loc= astNode.getFileLocation();
			if (loc != null) {
				storeIfBest(loc, (T) astNode);
			}
		}
	}

	@SuppressWarnings("unchecked")
	public void visit(ASTNode astNode, IASTImageLocation imageLocation) {
		if (isAcceptableNode(astNode) && imageLocation != null) {
			if (isMatchingRange(imageLocation.getNodeOffset(), imageLocation.getNodeLength(), fFileOffset, fFileEndOffset)) {
				storeIfBest(imageLocation, (T) astNode);
			}
		}
	}

	private boolean isMatchingRange(int offset, int length, int selOffset, int selEndOffset) {
		final int endOffset= offset + length;
		switch (fRelation) {
		case EXACT_MATCH:
			return selOffset == offset && selEndOffset == endOffset;
		case FIRST_CONTAINED:
			return selOffset <= offset && endOffset <= selEndOffset;
		case ENCLOSING:
			return offset <= selOffset && selEndOffset <= endOffset;
		case STRICTLY_ENCLOSING:
			if (offset <= selOffset && selEndOffset <= endOffset) {
				return offset != selOffset || selEndOffset != endOffset;
			}
			return false;
		}
		assert false;
		return false;
	}

	public boolean isAcceptableNode(IASTNode astNode) {
		if (astNode == null || !fClass.isAssignableFrom(astNode.getClass()))
			return false;

		if (fSearchInExpansion) {
			IASTNode check= astNode instanceof IASTName ? astNode.getParent() : astNode;
			if (check instanceof IASTPreprocessorMacroExpansion) {
				return false;
			}
		}
		return true;
	}

	/**
	 * Returns whether the node can contain matches in its range.
	 */
	public boolean canContainMatches(ASTNode node) {
		final int offset= node.getOffset();
		final int endOffset= offset + node.getLength();
		switch (fRelation) {
		case EXACT_MATCH:
		case ENCLOSING:
			return offset <= fSeqNumber && fSeqEndNumber <= endOffset;
		case STRICTLY_ENCLOSING:
			if (offset <= fSeqNumber && fSeqEndNumber <= endOffset) {
				return offset != fSeqNumber || fSeqEndNumber != endOffset;
			}
			return false;
		case FIRST_CONTAINED:
			return offset <= fSeqEndNumber && fSeqNumber <= endOffset;
		}
		assert false;
		return false;
	}

	private void storeIfBest(IASTFileLocation loc, T astNode) {
		if (loc != null) {
			final int offset = loc.getNodeOffset();
			final int length = loc.getNodeLength();
			if (isBetterMatch(offset, length, astNode)) {
				fBestNode= astNode;
				fBestOffset= offset;
				fBestEndOffset= offset + length;
			}
		}
	}

	/**
	 * Assuming that the given range matches, this method returns whether the match is better
	 * than the best match stored.
	 */
	private boolean isBetterMatch(int offset, int length, IASTNode cand) {
		if (fBestNode == null) {
			return true;
		}

		final int endOffset= offset + length;
		switch(fRelation) {
		case EXACT_MATCH:
			return isParent(fBestNode, cand);
		case FIRST_CONTAINED:
			if (offset < fBestOffset) {
				return true;
			}
			if (offset == fBestOffset) {
				if (endOffset < fBestEndOffset) {
					return true;
				}
				return endOffset == fBestEndOffset && isParent(fBestNode, cand);
			}
			return false;
		case ENCLOSING:
		case STRICTLY_ENCLOSING:
			final int bestLength= fBestEndOffset-fBestOffset;
			if (length < bestLength) {
				return true;
			}
			return length == bestLength && isParent(fBestNode, cand);
		default:
			assert false;
			return false;
		}
	}

	private boolean isParent(IASTNode cand1, IASTNode cand2) {
		while(cand2 != null) {
			if (cand2 == cand1) {
				return true;
			}
			cand2= cand2.getParent();
		}
		return false;
	}

	public IASTPreprocessorMacroExpansion findLeadingMacroExpansion(ASTNodeSelector nodeSelector) {
		IASTPreprocessorMacroExpansion exp=
				nodeSelector.findEnclosingMacroExpansion(fZeroToLeft ? fFileOffset - 1 : fFileOffset, 1);
		if (fRelation == Relation.ENCLOSING || fRelation == Relation.STRICTLY_ENCLOSING)
			return exp;

		if (exp != null) {
			IASTFileLocation loc= exp.getFileLocation();
			if (loc != null) {
				final int offset= loc.getNodeOffset();
				final int endOffset= offset + loc.getNodeLength();
				if (offset == fFileOffset && endOffset <= fFileEndOffset)
					return exp;
			}
		}
		return null;
	}

	public IASTPreprocessorMacroExpansion findTrailingMacroExpansion(ASTNodeSelector nodeSelector) {
		IASTPreprocessorMacroExpansion exp=
				nodeSelector.findEnclosingMacroExpansion(fFileEndOffset == fFileOffset && !fZeroToLeft ? fFileEndOffset : fFileEndOffset - 1, 1);
		if (fRelation == Relation.ENCLOSING || fRelation == Relation.STRICTLY_ENCLOSING)
			return exp;

		if (exp != null) {
			IASTFileLocation loc= exp.getFileLocation();
			if (loc != null) {
				final int offset= loc.getNodeOffset();
				final int endOffset= offset + loc.getNodeLength();
				if (endOffset == fFileEndOffset && offset >= fFileOffset)
					return exp;
			}
		}
		return null;
	}
}

Back to the top