Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5dbf28390fa8b2faeacf4e4d0fc89efc1079d11f (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*******************************************************************************
* 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.cpp.semantics;

import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.CVTYPE;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.REF;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.TDEF;

import java.util.Map;

import org.eclipse.cdt.core.dom.ast.ASTNodeFactoryFactory;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTEqualsInitializer;
import org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTInitializerClause;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.IValue;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorInitializer;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTInitializerClause;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTInitializerList;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPField;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateArgument;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameterMap;
import org.eclipse.cdt.internal.core.dom.parser.CompositeValue;
import org.eclipse.cdt.internal.core.dom.parser.ITypeMarshalBuffer;
import org.eclipse.cdt.internal.core.dom.parser.IntegralValue;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFieldReference;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPDeferredFunction;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVariable;
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.ICPPExecution;
import org.eclipse.cdt.internal.core.dom.parser.cpp.InstantiationContext;
import org.eclipse.core.runtime.CoreException;

public final class EvalConstructor extends CPPDependentEvaluation {
	private final IType fType;
	private final ICPPConstructor fConstructor;
	private final ICPPEvaluation[] fArguments;
	private boolean fCheckedIsTypeDependent;
	private boolean fIsTypeDependent;
	private boolean fCheckedIsConstantExpression;
	private boolean fIsConstantExpression;
	private static final IASTName TEMP_NAME = ASTNodeFactoryFactory.getDefaultCPPNodeFactory().newName();

	public EvalConstructor(IType type, ICPPConstructor constructor, ICPPEvaluation[] arguments,
			IBinding templateDefinition) {
		super(templateDefinition);
		fType = type;
		fConstructor = constructor;
		fArguments = arguments != null ? arguments : ICPPEvaluation.EMPTY_ARRAY;
	}

	public EvalConstructor(IType type, ICPPConstructor constructor, ICPPEvaluation[] arguments,
			IASTNode pointOfDefinition) {
		this(type, constructor, arguments, findEnclosingTemplate(pointOfDefinition));
	}

	@Override
	public boolean isInitializerList() {
		return false;
	}

	@Override
	public boolean isFunctionSet() {
		return false;
	}

	@Override
	public boolean isTypeDependent() {
		if (!fCheckedIsTypeDependent) {
			fCheckedIsTypeDependent = true;
			fIsTypeDependent = CPPTemplates.isDependentType(fType) || containsDependentType(fArguments);
		}
		return fIsTypeDependent;
	}

	@Override
	public boolean isValueDependent() {
		if (CPPTemplates.isDependentType(fType))
			return true;
		for (ICPPEvaluation arg : fArguments) {
			if (arg.isValueDependent())
				return true;
		}
		return false;
	}

	@Override
	public boolean isConstantExpression() {
		if (!fCheckedIsConstantExpression) {
			fCheckedIsConstantExpression = true;
			fIsConstantExpression = computeIsConstantExpression();
		}
		return fIsConstantExpression;
	}

	private boolean computeIsConstantExpression() {
		return fConstructor.isConstexpr() && areAllConstantExpressions(fArguments);
	}

	@Override
	public IType getType() {
		return fType;
	}

	@Override
	public IValue getValue() {
		ICPPEvaluation computed =
				computeForFunctionCall(new ActivationRecord(), new ConstexprEvaluationContext());
		if (computed == this)
			return IntegralValue.ERROR;

		return computed.getValue();
	}

	@Override
	public ValueCategory getValueCategory() {
		return null;
	}

	@Override
	public ICPPEvaluation computeForFunctionCall(ActivationRecord callSiteRecord,
			ConstexprEvaluationContext context) {
		final IType unwrappedType = SemanticUtil.getNestedType(fType, TDEF | REF | CVTYPE);
		if (!(unwrappedType instanceof ICPPClassType)) {
			return this;
		}
		final ICPPClassType classType = (ICPPClassType) unwrappedType; 
		final CompositeValue compositeValue = CompositeValue.create(classType);
		ICPPEvaluation[] argList = evaluateArguments(fArguments, callSiteRecord, context);
		EvalFixed constructedObject = new EvalFixed(fType, ValueCategory.PRVALUE, compositeValue);
		CPPVariable binding = new CPPVariable(TEMP_NAME);

		ActivationRecord localRecord = EvalFunctionCall.createActivationRecord(fConstructor.getParameters(), 
				argList, constructedObject);
		localRecord.update(binding, constructedObject);

		ICPPExecution exec = fConstructor.getConstructorChainExecution();
		if (exec instanceof ExecConstructorChain) {
			ExecConstructorChain memberInitList = (ExecConstructorChain) exec;
			Map<IBinding, ICPPEvaluation> ccInitializers = memberInitList.getConstructorChainInitializers();
			for (Map.Entry<IBinding, ICPPEvaluation> ccInitializer : ccInitializers.entrySet()) {
				if (ccInitializer.getKey() instanceof ICPPConstructor) {
					ICPPClassType baseClassType = (ICPPClassType) ccInitializer.getKey().getOwner();
					final ICPPEvaluation memberEval = ccInitializer.getValue();
					ICPPEvaluation memberValue =
							memberEval.computeForFunctionCall(localRecord, context.recordStep());
					ICPPEvaluation[] baseClassValues = memberValue.getValue().getAllSubValues();

					ICPPField[] baseFields = ClassTypeHelper.getFields(baseClassType);
					for (ICPPField baseField : baseFields) {
						// TODO: This has the same problem with multiple inheritance as
						//       CompositeValue.create(ICPPClassType).
						int fieldPos = CPPASTFieldReference.getFieldPosition(baseField);
						constructedObject.getValue().setSubValue(fieldPos, baseClassValues[fieldPos]);
					}
				}
			}
		}


		ICPPField[] fields = classType.getDeclaredFields();
		for (ICPPField field : fields) {
			if (field.isStatic()) {
				continue;
			}
			final Map.Entry<IBinding, ICPPEvaluation> initializer =
					getInitializerFromMemberInitializerList(field, exec);

			ICPPEvaluation value = null;
			if (initializer != null) {
				ExecDeclarator declaratorExec = getDeclaratorExecutionFromMemberInitializerList(initializer);
				value = getFieldValue(declaratorExec, classType, localRecord, context);
			} else {
				value = EvalUtil.getVariableValue(field, localRecord);
			}
			final int fieldPos = CPPASTFieldReference.getFieldPosition(field);
			compositeValue.setSubValue(fieldPos, value);
		}

		// TODO(nathanridge): EvalFunctionCall.computeForFunctionCall() will:
		//    - evaluate the arguments again
		//    - create another ActivationRecord (inside evaluateFunctionBody())
		// Are these necessary?
		new EvalFunctionCall(argList, constructedObject, CPPSemantics.getCurrentLookupPoint())
				.computeForFunctionCall(localRecord, context.recordStep());
		return localRecord.getVariable(binding);
	}

	private Map.Entry<IBinding, ICPPEvaluation> getInitializerFromMemberInitializerList(ICPPField field,
			ICPPExecution exec) {
		if (!(exec instanceof ExecConstructorChain)) {
			return null;
		}

		final ExecConstructorChain initList = (ExecConstructorChain) exec;
		for (Map.Entry<IBinding, ICPPEvaluation> init : initList.getConstructorChainInitializers().entrySet()) {
			final IBinding member = init.getKey();
			if (member instanceof ICPPField && member.getName().equals(field.getName())) {
				return init;
			}
		}
		return null;
	}

	private ExecDeclarator getDeclaratorExecutionFromMemberInitializerList(
			Map.Entry<IBinding, ICPPEvaluation> ccInitializer) {
		final ICPPBinding member = (ICPPBinding) ccInitializer.getKey();
		final ICPPEvaluation memberEval = ccInitializer.getValue();
		return new ExecDeclarator(member, memberEval);
	}

	private ICPPEvaluation getFieldValue(ExecDeclarator declaratorExec, ICPPClassType classType,
			ActivationRecord record, ConstexprEvaluationContext context) {
		if (declaratorExec == null) {
			return null;
		}

		if (declaratorExec.executeForFunctionCall(record, context) != ExecIncomplete.INSTANCE) {
			final ICPPEvaluation value = record.getVariable(declaratorExec.getDeclaredBinding());
			return value;
		}
		return null;
	}

	public static ICPPEvaluation[] extractArguments(IASTInitializer initializer, ICPPConstructor constructor) {
		ICPPEvaluation[] args = extractArguments(initializer);
		if (args.length == 1 && constructor.getParameters().length > 1 && args[0] instanceof EvalInitList) {
			EvalInitList evalInitList = (EvalInitList) args[0];
			args = evalInitList.getClauses();
		}
		return args;
	}

	public static ICPPEvaluation[] extractArguments(IASTInitializer initializer) {
		if (initializer == null) {
			return ICPPEvaluation.EMPTY_ARRAY;
		} else if (initializer instanceof ICPPASTConstructorInitializer) {
			ICPPASTConstructorInitializer ctorInitializer = (ICPPASTConstructorInitializer) initializer;
			return evaluateArguments(ctorInitializer.getArguments());
		} else if (initializer instanceof ICPPASTInitializerList) {
			ICPPASTInitializerList initList = (ICPPASTInitializerList) initializer;
			return evaluateArguments(initList.getClauses());
		} else if (initializer instanceof IASTEqualsInitializer) {
			IASTEqualsInitializer equalsInitalizer = (IASTEqualsInitializer) initializer;
			IASTInitializerClause initClause = equalsInitalizer.getInitializerClause();
			return evaluateArguments(initClause);
		} else {
			throw new IllegalArgumentException(initializer.getClass().getSimpleName()
					+ " type of initializer is not supported"); //$NON-NLS-1$
		}
	}

	private static ICPPEvaluation[] evaluateArguments(IASTInitializerClause... clauses) {
		ICPPEvaluation[] args = new ICPPEvaluation[clauses.length];
		for (int i = 0; i < clauses.length; i++) {
			ICPPASTInitializerClause clause = (ICPPASTInitializerClause) clauses[i];
			args[i] = clause.getEvaluation();
		}
		return args;
	}

	private ICPPEvaluation[] evaluateArguments(ICPPEvaluation[] arguments, ActivationRecord record,
			ConstexprEvaluationContext context) {
		ICPPEvaluation[] argList = new ICPPEvaluation[arguments.length + 1];
		EvalBinding constructorBinding =
				new EvalBinding(fConstructor, fConstructor.getType(), getTemplateDefinition());
		argList[0] = constructorBinding;
		for (int i = 0; i < arguments.length; i++) {
			ICPPEvaluation evaluatedClause = arguments[i].computeForFunctionCall(record, context.recordStep());
			argList[i+1] = evaluatedClause;
		}
		return argList;
	}


	@Override
	public int determinePackSize(ICPPTemplateParameterMap tpMap) {
		int r = CPPTemplates.determinePackSize(fType, tpMap);
		for (ICPPEvaluation arg : fArguments) {
			r = CPPTemplates.combinePackSize(r, arg.determinePackSize(tpMap));
		}
		return r;
	}

	@Override
	public boolean referencesTemplateParameter() {
		for (ICPPEvaluation arg : fArguments) {
			if (arg.referencesTemplateParameter())
				return true;
		}
		return false;
	}

	@Override
	public void marshal(ITypeMarshalBuffer buffer, boolean includeValue) throws CoreException {
		buffer.putShort(ITypeMarshalBuffer.EVAL_CONSTRUCTOR);
		buffer.marshalType(fType);
		buffer.marshalBinding(fConstructor);
		buffer.putInt(fArguments.length);
		for (ICPPEvaluation arg : fArguments) {
			buffer.marshalEvaluation(arg, includeValue);
		}
		marshalTemplateDefinition(buffer);
	}

	public static ICPPEvaluation unmarshal(short firstBytes, ITypeMarshalBuffer buffer) throws CoreException {
		IType type = buffer.unmarshalType();
		ICPPConstructor constructor = (ICPPConstructor) buffer.unmarshalBinding();
		int len = buffer.getInt();
		ICPPEvaluation[] arguments = new ICPPEvaluation[len];
		for (int i = 0; i < arguments.length; i++) {
			arguments[i] = buffer.unmarshalEvaluation();
		}
		IBinding templateDefinition = buffer.unmarshalBinding();
		return new EvalConstructor(type, constructor, arguments, templateDefinition);
	}

	@Override
	public ICPPEvaluation instantiate(InstantiationContext context, int maxDepth) {
		ICPPEvaluation[] newArguments = new ICPPEvaluation[fArguments.length];
		for (int i = 0; i < fArguments.length; i++) {
			newArguments[i] = fArguments[i].instantiate(context, maxDepth);
		}

		IType newType = null;
		ICPPConstructor newConstructor;
		try {
			newConstructor = (ICPPConstructor) CPPTemplates.instantiateBinding(fConstructor, context, maxDepth);
			if (newConstructor != null) {
				newType = newConstructor.getClassOwner();
			}
			if (newConstructor instanceof CPPDeferredFunction) {
				ICPPFunction[] candidates = ((CPPDeferredFunction) newConstructor).getCandidates();
				if (candidates != null) {
					CPPFunctionSet functionSet =
							new CPPFunctionSet(candidates, new ICPPTemplateArgument[]{}, null);
					EvalFunctionSet evalFunctionSet =
							new EvalFunctionSet(functionSet, false, false, newType, 
									CPPSemantics.getCurrentLookupPoint());
					ICPPEvaluation resolved = evalFunctionSet.resolveFunction(newArguments);
					if (resolved instanceof EvalBinding) {
						EvalBinding evalBinding = (EvalBinding) resolved;
						newConstructor = (ICPPConstructor) evalBinding.getBinding();
						if (newConstructor != null) {
							newType = newConstructor.getClassOwner();
						}
					}
				}
			}
		} catch (DOMException e) {
			newConstructor = fConstructor;
		}

		// Only instantiate fType separately if we couldn't get the instantiated type
		// via newConstructor.getClassOwner() for some reason. This is not just for
		// efficiency; instantiating fType directly will not work if fType is a
		// CPPClassTemplate because CPPTemplates.instantiateType() does not instantiate
		// CPPClassTemplates, only CPPDeferredClassInstances (TODO: why?).
		if (newType == null) {
			newType = CPPTemplates.instantiateType(fType, context);
		}

		return new EvalConstructor(newType, newConstructor, newArguments, getTemplateDefinition());
	}
}

Back to the top