Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1cf9d4f822b0a3c5f5b3f28c069805598e0de0c5 (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
/*******************************************************************************
 * Copyright (c) 2008, 2014 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
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser;

import java.util.Arrays;

import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory;
import org.eclipse.cdt.core.dom.ast.IValue;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPBasicType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluation;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.EvalBinary;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.EvalBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.EvalFixed;
import org.eclipse.cdt.internal.core.pdom.dom.TypeMarshalBuffer;
import org.eclipse.core.runtime.CoreException;

/**
 * Represents values of variables, enumerators or expressions. The primary purpose of
 * the representation is to support instantiation of templates with non-type template parameters.
 */
public class IntegralValue implements IValue {
	public static final int MAX_RECURSION_DEPTH = 25;

	// IntegralValue.THIS represents the this pointer inside a member function / constructor.
	public static final IntegralValue THIS = new IntegralValue("this".toCharArray()); //$NON-NLS-1$

	// IntegralValue.UNKNOWN indicates general inability to determine a value. It doesn't have to be an error,
	// it could be that evaluation ran into a performance limit, or that we can't model this kind of
	// value (such as a pointer to a function).
	public static final IntegralValue UNKNOWN = new IntegralValue("<unknown>".toCharArray()) { //$NON-NLS-1$
		@Override
		public void setSubValue(int position, ICPPEvaluation newValue) {
			throw new UnsupportedOperationException();
		}
	};

	// IntegralValue.ERROR indicates that an error, such as a substitution failure, occurred during evaluation.
	public static final IntegralValue ERROR = new IntegralValue("<error>".toCharArray()); //$NON-NLS-1$

	public static final IntegralValue NOT_INITIALIZED = new IntegralValue("<__>".toCharArray()); //$NON-NLS-1$

	private static final char UNIQUE_CHAR = '_';

	private final static IntegralValue[] TYPICAL = { new IntegralValue(new char[] { '-', '1' }),
			new IntegralValue(new char[] { '0' }), new IntegralValue(new char[] { '1' }),
			new IntegralValue(new char[] { '2' }), new IntegralValue(new char[] { '3' }),
			new IntegralValue(new char[] { '4' }), new IntegralValue(new char[] { '5' }),
			new IntegralValue(new char[] { '6' }), new IntegralValue(new char[] { '7' }) };

	private static int sUnique = 0;

	private final char[] fFixedValue;

	private IntegralValue(char[] fixedValue) {
		fFixedValue = fixedValue;
	}

	@Override
	public final Number numberValue() {
		return parseLong(fFixedValue);
	}

	@Override
	public ICPPEvaluation getEvaluation() {
		return null;
	}

	@Override
	public final char[] getSignature() {
		return fFixedValue;
	}

	@Override
	public void marshal(ITypeMarshalBuffer buf) throws CoreException {
		if (UNKNOWN == this) {
			buf.putShort((short) (ITypeMarshalBuffer.INTEGRAL_VALUE | ITypeMarshalBuffer.FLAG1));
		} else if (ERROR == this) {
			buf.putShort(
					(short) (ITypeMarshalBuffer.INTEGRAL_VALUE | ITypeMarshalBuffer.FLAG1 | ITypeMarshalBuffer.FLAG2));
		} else if (THIS == this) {
			buf.putShort((short) (ITypeMarshalBuffer.INTEGRAL_VALUE | ITypeMarshalBuffer.FLAG5));
		} else {
			Number num = numberValue();
			if (num != null) {
				long lv = num.longValue();
				if (lv >= 0) {
					buf.putShort((short) (ITypeMarshalBuffer.INTEGRAL_VALUE | ITypeMarshalBuffer.FLAG2));
					buf.putLong(lv);
				} else {
					buf.putShort((short) (ITypeMarshalBuffer.INTEGRAL_VALUE | ITypeMarshalBuffer.FLAG3));
					buf.putLong(-lv);
				}
			} else {
				buf.putShort((short) (ITypeMarshalBuffer.INTEGRAL_VALUE | ITypeMarshalBuffer.FLAG4));
				buf.putCharArray(fFixedValue);
			}
		}
	}

	public static IValue unmarshal(short firstBytes, ITypeMarshalBuffer buf) throws CoreException {
		if (firstBytes == TypeMarshalBuffer.NULL_TYPE)
			return UNKNOWN;
		if ((firstBytes & ITypeMarshalBuffer.FLAG1) != 0) {
			if ((firstBytes & ITypeMarshalBuffer.FLAG2) != 0)
				return ERROR;
			return UNKNOWN;
		}
		if ((firstBytes & ITypeMarshalBuffer.FLAG2) != 0)
			return create(buf.getLong());
		if ((firstBytes & ITypeMarshalBuffer.FLAG3) != 0)
			return create(-buf.getLong());
		if ((firstBytes & ITypeMarshalBuffer.FLAG4) != 0)
			return new IntegralValue(buf.getCharArray());
		if ((firstBytes & ITypeMarshalBuffer.FLAG5) != 0)
			return THIS;

		return UNKNOWN;
	}

	@Override
	public int hashCode() {
		return CharArrayUtils.hash(getSignature());
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof IntegralValue)) {
			return false;
		}
		final IntegralValue rhs = (IntegralValue) obj;
		return CharArrayUtils.equals(getSignature(), rhs.getSignature());
	}

	/**
	 * For debugging only.
	 */
	@Override
	public String toString() {
		return new String(getSignature());
	}

	/**
	 * Creates a value representing the given number.
	 */
	public static IntegralValue create(long value) {
		if (value >= -1 && value < TYPICAL.length - 1)
			return TYPICAL[(int) value + 1];
		return new IntegralValue(toCharArray(value));
	}

	/**
	 * Creates a value object representing the given boolean value.
	 */
	public static IntegralValue create(boolean value) {
		return create(value ? 1 : 0);
	}

	public static IValue incrementedValue(IValue value, int increment) {
		if (value == UNKNOWN)
			return UNKNOWN;
		if (value == ERROR)
			return ERROR;
		Number val = value.numberValue();
		if (val != null) {
			return create(val.longValue() + increment);
		}
		ICPPEvaluation arg1 = value.getEvaluation();
		EvalFixed arg2 = new EvalFixed(CPPBasicType.INT, ValueCategory.PRVALUE, create(increment));
		return DependentValue
				.create(new EvalBinary(IASTBinaryExpression.op_plus, arg1, arg2, arg1.getTemplateDefinition()));
	}

	/**
	 * Tests whether the value is a template parameter (or a parameter pack).
	 *
	 * @return the parameter id of the parameter, or <code>-1</code> if it is not a template
	 *         parameter.
	 */
	public static int isTemplateParameter(IValue tval) {
		ICPPEvaluation eval = tval.getEvaluation();
		if (eval instanceof EvalBinding) {
			return ((EvalBinding) eval).getTemplateParameterID();
		}
		return -1;
	}

	/**
	 * Tests whether the value references some template parameter.
	 */
	public static boolean referencesTemplateParameter(IValue tval) {
		ICPPEvaluation eval = tval.getEvaluation();
		if (eval == null)
			return false;
		return eval.referencesTemplateParameter();
	}

	/**
	 * Tests whether the value depends on a template parameter.
	 */
	public static boolean isDependentValue(IValue nonTypeValue) {
		if (nonTypeValue == null)
			return false;
		ICPPEvaluation eval = nonTypeValue.getEvaluation();
		return eval != null && eval.isValueDependent();
	}

	/**
	 * Creates a unique value needed during template instantiation.
	 */
	public static IValue unique() {
		StringBuilder buf = new StringBuilder(10);
		buf.append(UNIQUE_CHAR);
		buf.append(++sUnique);
		return new IntegralValue(CharArrayUtils.extractChars(buf));
	}

	/**
	 * Parses a long, returns <code>null</code> if not possible
	 */
	private static Long parseLong(char[] value) {
		final long maxvalue = Long.MAX_VALUE / 10;
		final int len = value.length;
		boolean negative = false;
		long result = 0;
		int i = 0;

		if (len > 0 && value[0] == '-') {
			negative = true;
			i++;
		}
		if (i == len)
			return null;

		for (; i < len; i++) {
			if (result > maxvalue)
				return null;

			final int digit = (value[i] - '0');
			if (digit < 0 || digit > 9)
				return null;
			result = result * 10 + digit;
		}
		return negative ? -result : result;
	}

	/**
	 * Converts long to a char array
	 */
	private static char[] toCharArray(long value) {
		StringBuilder buf = new StringBuilder();
		buf.append(value);
		return CharArrayUtils.extractChars(buf);
	}

	@Override
	public final int numberOfSubValues() {
		return 1;
	}

	@Override
	public final ICPPEvaluation getSubValue(int index) {
		return EvalFixed.INCOMPLETE;
	}

	@Override
	public final ICPPEvaluation[] getAllSubValues() {
		return new ICPPEvaluation[] { getEvaluation() };
	}

	@Override
	public void setSubValue(int position, ICPPEvaluation newValue) {
		throw new IllegalStateException("Trying to set incomplete value"); //$NON-NLS-1$
	}

	@Override
	public IValue clone() {
		return new IntegralValue(Arrays.copyOf(fFixedValue, fFixedValue.length));
	}

	@Override
	public boolean isEquivalentTo(IValue other) {
		if (!(other instanceof IntegralValue)) {
			return false;
		}
		IntegralValue o = (IntegralValue) other;
		return fFixedValue.equals(o.fFixedValue);
	}
}

Back to the top