Skip to main content
summaryrefslogtreecommitdiffstats
blob: 28ac86455ce1414c56957efd84d155bfc007bd63 (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
/*******************************************************************************
 * Copyright (c) 2011, 2012 Wind River Systems, Inc. and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Markus Schorn - initial API and implementation
 *******************************************************************************/

package org.eclipse.cdt.internal.core.dom.parser.cpp;

import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.internal.core.dom.parser.AbstractGNUSourceCodeParser.BinaryOperator;

/**
 * Tracks variants of expressions due to the ambiguity between template-id and '<' operator.
 */
public class NameOrTemplateIDVariants {
	/**
	 * A point where a '<' can be interpreted as less-than or as the angle-bracket of a template-id.
	 */
	static class BranchPoint {
		private BranchPoint fNext;
		private Variant fFirstVariant;
		private final boolean fAllowAssignment;
		private final int fConditionCount;
		private final BinaryOperator fLeftOperator;

		BranchPoint(BranchPoint next, Variant variant, BinaryOperator left, boolean allowAssignment,
				int conditionCount) {
			fNext = next;
			fFirstVariant = variant;
			fAllowAssignment = allowAssignment;
			fConditionCount = conditionCount;
			fLeftOperator = left;
			// Set owner
			while (variant != null) {
				variant.fOwner = this;
				variant = variant.getNext();
			}
		}

		public boolean isAllowAssignment() {
			return fAllowAssignment;
		}

		public int getConditionCount() {
			return fConditionCount;
		}

		public BinaryOperator getLeftOperator() {
			return fLeftOperator;
		}

		public Variant getFirstVariant() {
			return fFirstVariant;
		}

		public BranchPoint getNext() {
			return fNext;
		}

		public void reverseVariants() {
			Variant prev = null;
			Variant curr = fFirstVariant;
			while (curr != null) {
				Variant next = curr.getNext();
				curr.fNext = prev;
				prev = curr;
				curr = next;
			}
			fFirstVariant = prev;
		}
	}

	/**
	 * A variant for a branch-point is a cast-expression that can be used within a binary expression.
	 */
	static class Variant {
		private BranchPoint fOwner;
		private Variant fNext;
		private final IASTExpression fExpression;
		private BinaryOperator fTargetOperator;
		private final int fRightOffset;
		private final IASTName[] fTemplateNames;

		public Variant(Variant next, IASTExpression expr, IASTName[] templateNames, int rightOffset) {
			fNext = next;
			fExpression = expr;
			fRightOffset = rightOffset;
			fTemplateNames = templateNames;
		}

		public BranchPoint getOwner() {
			return fOwner;
		}

		public int getRightOffset() {
			return fRightOffset;
		}

		public IASTName[] getTemplateNames() {
			return fTemplateNames;
		}

		public Variant getNext() {
			return fNext;
		}

		public IASTExpression getExpression() {
			return fExpression;
		}

		public BinaryOperator getTargetOperator() {
			return fTargetOperator;
		}

		public void setTargetOperator(BinaryOperator lastOperator) {
			fTargetOperator = lastOperator;
		}
	}

	private BranchPoint fFirst;

	public boolean isEmpty() {
		return fFirst == null;
	}

	public void addBranchPoint(Variant variants, BinaryOperator left, boolean allowAssignment, int conditionCount) {
		fFirst = new BranchPoint(fFirst, variants, left, allowAssignment, conditionCount);
	}

	public void closeVariants(int offset, BinaryOperator lastOperator) {
		for (BranchPoint p = fFirst; p != null; p = p.getNext()) {
			for (Variant v = p.getFirstVariant(); v != null; v = v.getNext()) {
				if (v.getTargetOperator() == null) {
					if (offset == v.getRightOffset()) {
						v.setTargetOperator(lastOperator);
					}
				}
			}
		}
	}

	public Variant selectFallback() {
		// Search for an open variant, with a small right offset and a large left offset
		for (BranchPoint p = fFirst; p != null; p = p.getNext()) {
			Variant best = null;
			for (Variant v = p.getFirstVariant(); v != null; v = v.getNext()) {
				if (v.getTargetOperator() == null) {
					if (best == null || v.fRightOffset < best.fRightOffset) {
						best = v;
					}
				}
			}
			if (best != null) {
				remove(best);
				return best;
			}
		}
		return null;
	}

	private void remove(Variant remove) {
		final BranchPoint owner = remove.fOwner;
		final Variant next = remove.getNext();
		Variant prev = owner.getFirstVariant();
		if (remove == prev) {
			owner.fFirstVariant = next;
			if (next == null) {
				remove(owner);
			}
		} else {
			while (prev != null) {
				Variant n = prev.getNext();
				if (n == remove) {
					prev.fNext = next;
					break;
				}
				prev = n;
			}
		}
	}

	private void remove(BranchPoint remove) {
		final BranchPoint next = remove.getNext();
		if (remove == fFirst) {
			fFirst = next;
		} else {
			BranchPoint prev = fFirst;
			while (prev != null) {
				BranchPoint n = prev.getNext();
				if (n == remove) {
					prev.fNext = next;
					break;
				}
				prev = n;
			}
		}
	}

	public BranchPoint getOrderedBranchPoints() {
		BranchPoint prev = null;
		BranchPoint curr = fFirst;
		while (curr != null) {
			curr.reverseVariants();
			BranchPoint next = curr.getNext();
			curr.fNext = prev;
			prev = curr;
			curr = next;
		}
		fFirst = null;
		return prev;
	}

	public boolean hasRightBound(int opOffset) {
		// Search for an open variant, with a small right offset and a large left offset
		for (BranchPoint p = fFirst; p != null; p = p.getNext()) {
			for (Variant v = p.getFirstVariant(); v != null; v = v.getNext()) {
				if (v.fRightOffset > opOffset)
					return false;
			}
		}
		return true;
	}

	public void removeInvalid(BinaryOperator lastOperator) {
		for (BranchPoint p = fFirst; p != null; p = p.getNext()) {
			if (!isReachable(p, lastOperator)) {
				remove(p);
			} else {
				for (Variant v = p.getFirstVariant(); v != null; v = v.getNext()) {
					if (v.getTargetOperator() == null) {
						remove(v);
					}
				}
			}
		}
	}

	private boolean isReachable(BranchPoint bp, BinaryOperator endOperator) {
		BinaryOperator op = bp.getLeftOperator();
		if (op == null)
			return true;

		for (; endOperator != null; endOperator = endOperator.getNext()) {
			if (endOperator == op)
				return true;
		}
		return false;
	}
}

Back to the top