Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3bb2101f05f2944edec485b8fbe5239bf112892e (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
/*******************************************************************************
* Copyright (c) 2016 Institute for Software, HSR Hochschule fuer Technik
* Rapperswil, University of applied sciences 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
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IArrayType;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IField;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.IValue;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBase;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPField;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFieldReference;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ClassTypeHelper;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluation;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.ActivationRecord;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.EvalFixed;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.EvalInitList;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.EvalUtil;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;

public final class CompositeValue implements IValue {
	private final ICPPEvaluation evaluation;
	private final ICPPEvaluation[] values;

	public CompositeValue(ICPPEvaluation evaluation, ICPPEvaluation[] values) {
		this.evaluation = evaluation;
		for (int i = 0; i < values.length; i++) {
			if (values[i] == null)
				values[i] = EvalFixed.INCOMPLETE;
		}
		this.values = values;
	}

	@Override
	public Long numericalValue() {
		return null;
	}

	@Override
	public Number numberValue() {
		return null;
	}

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

	@Override
	public char[] getSignature() {
		if (evaluation != null) {
			return evaluation.getSignature();
		}
		return new char[]{};
	}

	@Deprecated
	@Override
	public char[] getInternalExpression() {
		return CharArrayUtils.EMPTY_CHAR_ARRAY;
	}

	@Deprecated
	@Override
	public IBinding[] getUnknownBindings() {
		return IBinding.EMPTY_BINDING_ARRAY;
	}

	@Override
	public int numberOfSubValues() {
		return values.length;
	}

	@Override
	public ICPPEvaluation getSubValue(final int index) {
		return rangeIsValid(index) ? values[index] : EvalFixed.INCOMPLETE;
	}

	private boolean rangeIsValid(int index) {
		return 0 <= index && index < values.length;
	}

	public static IValue create(EvalInitList initList) {
		ICPPEvaluation[] clauses = initList.getClauses();
		ICPPEvaluation[] values = new ICPPEvaluation[clauses.length];
		for (int i = 0; i < clauses.length; i++) {
			ICPPEvaluation eval = clauses[i];
			values[i] = new EvalFixed(eval.getType(null), eval.getValueCategory(null), eval.getValue(null));
		}
		return new CompositeValue(initList, values);
	}

	/**
	 * Creates a value representing an instance of the given array type initialized with
	 * the elements of the given initializer list.
	 */
	public static IValue create(EvalInitList initList, IArrayType type, IASTNode point) {
		Number arraySize = type.getSize().numberValue();
		if (arraySize == null) {
			// Array size is dependent. TODO: Handle this?
			return IntegralValue.UNKNOWN;
		}
		// More initializers than array members
		if (arraySize.intValue() < initList.getClauses().length) {
			return IntegralValue.ERROR;
		}
		IType elementType = type.getType();
		ICPPEvaluation[] values = new ICPPEvaluation[arraySize.intValue()];
		for (int i = 0; i < initList.getClauses().length; i++) {
			ICPPEvaluation eval = initList.getClauses()[i];
			IValue value = getValue(elementType, eval, point);
			values[i] = new EvalFixed(elementType, eval.getValueCategory(point), value);
		}
		return new CompositeValue(initList, values);
	}

	/**
	 * Gets the value of an evaluation, interpreted as a value of the given type.
	 */
	private static IValue getValue(IType type, ICPPEvaluation eval, IASTNode point) {
		IValue value;
		if (type instanceof IArrayType && eval instanceof EvalInitList) {
			value = CompositeValue.create((EvalInitList) eval, (IArrayType) type, point);
		} else if (type instanceof ICompositeType && eval instanceof EvalInitList) {
			value = CompositeValue.create((EvalInitList) eval, (ICompositeType) type, point);
		} else if (eval instanceof EvalInitList) {
			value = IntegralValue.UNKNOWN;
		} else {
			value = eval.getValue(null);
		}
		return value;
	}

	/**
	 * Creates a value representing an instance of the given composite type initialized with
	 * the elements of the given initializer list.
	 */
	public static IValue create(EvalInitList initList, ICompositeType type, IASTNode point) {
		IField[] fields;
		if (type instanceof ICPPClassType) {
			fields = ClassTypeHelper.getFields((ICPPClassType) type, point);
		} else {
			fields = type.getFields();
		}
		ICPPEvaluation[] values = new ICPPEvaluation[fields.length];
		ICPPEvaluation[] clauses = initList.getClauses();
		for (int i = 0; i < fields.length; i++) {
			if (i == clauses.length)
				break;
			IField field = fields[i];
			ICPPEvaluation eval = clauses[i];
			IType fieldType = field.getType();
			IValue value = getValue(fieldType, eval, point);
			values[i] = new EvalFixed(fieldType, eval.getValueCategory(null), value);
		}
		return new CompositeValue(initList, values);
	}

	// The set of class types for which composite value creation is in progress on each thread.
	// Used to guard against infinite recursion due to a class (invalidly) aggregating itself.
	private static final ThreadLocal<Set<ICPPClassType>> fCreateInProgress =
			new ThreadLocal<Set<ICPPClassType>>() {
		@Override
		protected Set<ICPPClassType> initialValue() {
			return new HashSet<>();
		}
	};

	/**
	 * Creates a value representing an instance of a class type, with the values of the fields
	 * determined by the default member initializers only. Constructors are not considered
	 * when determining the values of the fields.
	 */
	public static CompositeValue create(ICPPClassType classType) {
		Set<ICPPClassType> recursionProtectionSet = fCreateInProgress.get();
		if (!recursionProtectionSet.add(classType)) {
			return new CompositeValue(null, ICPPEvaluation.EMPTY_ARRAY);
		}
		try {
			ActivationRecord record = new ActivationRecord();
			ICPPEvaluation[] values = new ICPPEvaluation[ClassTypeHelper.getFields(classType, null).length];

			// Recursively create all the base class member variables.
			ICPPBase[] bases = ClassTypeHelper.getBases(classType, null);
			for (ICPPBase base : bases) {
				IBinding baseClass = base.getBaseClass();
				if (baseClass instanceof ICPPClassType) {
					ICPPClassType baseClassType = (ICPPClassType) baseClass;
					ICPPField[] baseFields = ClassTypeHelper.getDeclaredFields(baseClassType, null);
					IValue compValue = CompositeValue.create(baseClassType);
					for (ICPPField baseField : baseFields) {
						int fieldPos = CPPASTFieldReference.getFieldPosition(baseField);
						record.update(baseField, compValue.getSubValue(fieldPos));
						// TODO(nathanridge): This won't work with multiple inheritance, since 'fieldPos'
						// is a field position in the base class' hierarchy, while values[] expects
						// as index a field position in classType's hierarchy.
						values[fieldPos] = compValue.getSubValue(fieldPos);
					}
				}
			}

			ICPPField[] fields = ClassTypeHelper.getDeclaredFields(classType, null);
			for (ICPPField field : fields) {
				if (field.isStatic())
					continue;
				final ICPPEvaluation value = EvalUtil.getVariableValue(field, record);
				int fieldPos = CPPASTFieldReference.getFieldPosition(field);
				record.update(field, value);
				values[fieldPos] = value;
			}
			return new CompositeValue(null, values);
		} finally {
			recursionProtectionSet.remove(classType);
		}
	}

	@Override
	public ICPPEvaluation[] getAllSubValues() {
		return values;
	}

	@Override
	public void setSubValue(int position, ICPPEvaluation newValue) {
		if (position >= 0 && position < values.length) {
			values[position] = newValue == null ? EvalFixed.INCOMPLETE : newValue;
		} else {
			CCorePlugin.log(IStatus.WARNING, "Out-of-bounds access to composite value: " + position + //$NON-NLS-1$ 
					" (length is " + values.length + ")");  //$NON-NLS-1$//$NON-NLS-2$
		}
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append('[');
		for (int i = 0; i < values.length; i++) {
			if (i != 0) {
				builder.append(',').append(' ');
			}
			builder.append(values[i].toString());
		}
		builder.append(']');
		return builder.toString();
	}

	@Override
	public IValue clone() {
		ICPPEvaluation[] newValues = new ICPPEvaluation[values.length];
		for (int i = 0; i < newValues.length; i++) {
			ICPPEvaluation eval = values[i];
			if (eval == EvalFixed.INCOMPLETE) {
				newValues[i] = eval;
			} else {
				IValue newValue = eval.getValue(null).clone();
				newValues[i] = new EvalFixed(eval.getType(null), eval.getValueCategory(null), newValue);
			}
		}
		return new CompositeValue(evaluation, newValues);
	}

	@Override
	public void marshal(ITypeMarshalBuffer buf) throws CoreException {
		buf.putShort(ITypeMarshalBuffer.COMPOSITE_VALUE);
		buf.marshalEvaluation(evaluation, true);
		buf.putInt(values.length);
		for (ICPPEvaluation value : values) {
			buf.marshalEvaluation(value, true);
		}
	}

	public static IValue unmarshal(short firstBytes, ITypeMarshalBuffer buf) throws CoreException {
		ICPPEvaluation evaluation = (ICPPEvaluation) buf.unmarshalEvaluation();
		int len = buf.getInt();
		ICPPEvaluation values[] = new ICPPEvaluation[len];
		for (int i = 0; i < len; i++) {
			values[i] = (ICPPEvaluation) buf.unmarshalEvaluation();
		}
		return new CompositeValue(evaluation, values);
	}
}

Back to the top