Skip to main content
summaryrefslogtreecommitdiffstats
blob: cc3970425b11c1ba799c83f41e5fe9c18df62a55 (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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*******************************************************************************
 * Copyright (c) 2012, 2016 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
 *     Sergey Prigogin (Google)
 *     Nathan Ridge
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp.semantics;

import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.ExpressionTypes.valueCategoryFromFunctionCall;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.ExpressionTypes.valueCategoryFromReturnType;
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.Arrays;

import org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IFunctionType;
import org.eclipse.cdt.core.dom.ast.IPointerType;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.IValue;
import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPReferenceType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPSpecialization;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateInstance;
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.DependentValue;
import org.eclipse.cdt.internal.core.dom.parser.ITypeMarshalBuffer;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPFunction;
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.cdt.internal.core.dom.parser.cpp.OverloadableOperator;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPSemantics.LookupMode;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;

public final class EvalFunctionCall extends CPPDependentEvaluation {
	private final ICPPEvaluation[] fArguments;
	private ICPPFunction fOverload = CPPFunction.UNINITIALIZED_FUNCTION;
	private IType fType;
	private boolean fCheckedIsConstantExpression;
	private boolean fIsConstantExpression;

	private final ICPPEvaluation fImplicitThis;

	public EvalFunctionCall(ICPPEvaluation[] args, ICPPEvaluation owner, IBinding templateDefinition) {
		super(templateDefinition);
		for (int i = 0; i < args.length; i++) {
			Assert.isNotNull(args[i]);
		}
		fArguments = args;
		fImplicitThis = getImplicitThis() == owner ? null : owner;
	}

	public EvalFunctionCall(ICPPEvaluation[] args, ICPPEvaluation owner, IASTNode pointOfDefinition) {
		this(args, owner, findEnclosingTemplate(pointOfDefinition));
	}

	/**
	 * Returns arguments of the function call. The first argument is the function name, the rest are arguments
	 * passed to the function.
	 */
	public ICPPEvaluation[] getArguments() {
		return fArguments;
	}
	
	private ICPPEvaluation getImplicitThis() {
		if (fImplicitThis != null)
			return fImplicitThis;
		
		if (fArguments.length > 0) {
			if (fArguments[0] instanceof EvalMemberAccess) {
				return ((EvalMemberAccess) fArguments[0]).getOwnerEval();
			} else if (fArguments[0] instanceof EvalID) {
				return ((EvalID) fArguments[0]).getFieldOwner();
			}
		}
		
		return null;
	}

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

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

	@Override
	public boolean isTypeDependent() {
		return containsDependentType(fArguments);
	}

	@Override
	public boolean isValueDependent() {
		return containsDependentValue(fArguments) || 
			   !CPPTemplates.isFullyInstantiated(resolveFunctionBinding());
	}
	
	@Override
	public boolean isConstantExpression() {
		if (!fCheckedIsConstantExpression) {
			fCheckedIsConstantExpression = true;
			fIsConstantExpression = computeIsConstantExpression();
		}
		return fIsConstantExpression;
	}

	private boolean computeIsConstantExpression() {
		return areAllConstantExpressions(fArguments) && isNullOrConstexprFunc(getOverload());
	}

	public ICPPFunction getOverload() {
		if (fOverload == CPPFunction.UNINITIALIZED_FUNCTION) {
			fOverload = computeOverload();
		}
		return fOverload;
	}

	private ICPPFunction computeOverload() {
		if (isTypeDependent())
			return null;

		IType t= SemanticUtil.getNestedType(fArguments[0].getType(), TDEF | REF | CVTYPE);
		if (t instanceof ICPPClassType) {
	    	return CPPSemantics.findOverloadedOperator(getTemplateDefinitionScope(), fArguments, t,
	    			OverloadableOperator.PAREN, LookupMode.NO_GLOBALS);
		}
		return null;
	}

	@Override
	public IType getType() {
		if (fType == null)
			fType = computeType();
		return fType;
	}

	private IType computeType() {
		if (isTypeDependent())
			return new TypeOfDependentExpression(this);

		ICPPFunction overload = getOverload();
		if (overload != null)
			return ExpressionTypes.typeFromFunctionCall(overload);

		ICPPEvaluation function = fArguments[0];
		IType result = ExpressionTypes.typeFromFunctionCall(function.getType());
		if (function instanceof EvalMemberAccess) {
			result = ExpressionTypes.restoreTypedefs(result, ((EvalMemberAccess) function).getOwnerType());
		}
		return result;
	}

	@Override
	public IValue getValue() {
		ICPPEvaluation eval = evaluateFunctionBody(new ConstexprEvaluationContext());
		if (eval == this) {
			return DependentValue.create(eval);
		}
		return eval.getValue();
	}

	@Override
	public ValueCategory getValueCategory() {
		ICPPFunction overload = getOverload();
		if (overload != null)
			return valueCategoryFromFunctionCall(overload);

		IType t= fArguments[0].getType();
		if (t instanceof IPointerType) {
			t = SemanticUtil.getNestedType(((IPointerType) t).getType(), TDEF | REF | CVTYPE);
		}
		if (t instanceof IFunctionType) {
			return valueCategoryFromReturnType(((IFunctionType) t).getReturnType());
		}
		return ValueCategory.PRVALUE;
	}

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

	public static ICPPEvaluation unmarshal(short firstBytes, ITypeMarshalBuffer buffer)
			throws CoreException {
		int len = buffer.getInt();
		ICPPEvaluation[] args = new ICPPEvaluation[len];
		for (int i = 0; i < args.length; i++) {
			args[i] = buffer.unmarshalEvaluation();
		}
		ICPPEvaluation implicitThis = buffer.unmarshalEvaluation();
		IBinding templateDefinition = buffer.unmarshalBinding();
		return new EvalFunctionCall(args, implicitThis, templateDefinition);
	}

	@Override
	public ICPPEvaluation instantiate(InstantiationContext context, int maxDepth) {
		ICPPEvaluation[] args = instantiateCommaSeparatedSubexpressions(fArguments, context, maxDepth);
		if (args == fArguments)
			return this;

		ICPPEvaluation implicitThis = fImplicitThis;
		
		if (args[0] instanceof EvalFunctionSet && getOverload() == null) {
			// Resolve the function using the parameters of the function call.
			EvalFunctionSet functionSet = (EvalFunctionSet) args[0];
			args[0] = functionSet.resolveFunction(Arrays.copyOfRange(args, 1, args.length));

			// Propagate instantiation errors for SFINAE purposes.
			if (args[0] == EvalFixed.INCOMPLETE) {
				return args[0];
			}
			
			// For functions sets of member functions, EvalFunctionSet does not store 
			// the value of the object on which the member function is called.
			// If this value was previously elided (not stored explicitly in
			// fImplicitThis to avoid duplication with the copy stored in fArguments[0]),
			// starts storing it in fImplicitThis because *someone* needs to store
			// the value for correct constexpr evaluation.
			if (implicitThis == null) {
				implicitThis = getImplicitThis();
			}
		}

		ICPPEvaluation newImplicitThis = implicitThis != null ? implicitThis.instantiate(context, maxDepth) : null;
		return new EvalFunctionCall(args, newImplicitThis, getTemplateDefinition());
	}

	@Override
	public ICPPEvaluation computeForFunctionCall(ActivationRecord record, ConstexprEvaluationContext context) {
		if (context.getStepsPerformed() >= ConstexprEvaluationContext.MAX_CONSTEXPR_EVALUATION_STEPS) {
			return EvalFixed.INCOMPLETE;
		}

		ICPPFunction functionBinding = resolveFunctionBinding();
		if (functionBinding == null)
			return EvalFixed.INCOMPLETE;

		ICPPEvaluation[] args = new ICPPEvaluation[fArguments.length];
		System.arraycopy(fArguments, 0, args, 0, fArguments.length);

		ICPPParameter[] parameters = functionBinding.getParameters();
		for (int i = 0; i < fArguments.length; i++) {
			ICPPEvaluation arg = fArguments[i].computeForFunctionCall(record, context.recordStep());
            if (0 < i && i <= parameters.length && isReference(parameters[i - 1]) && fArguments[i] instanceof EvalBinding) {
            	final EvalBinding evalBinding = (EvalBinding) fArguments[i];
            	IBinding binding = evalBinding.getBinding();
            	// If the binding being referenced isn't present in the activation record,
            	// we won't be able to evaluate the function call.
            	if (record.getVariable(binding) == null)
            		return EvalFixed.INCOMPLETE;
            	arg = new EvalReference(record, binding, evalBinding.getTemplateDefinition());
            } else if (0 < i && i <= parameters.length && !isReference(parameters[i - 1])) {
            	IValue copiedValue = arg.getValue().clone();
            	arg = new EvalFixed(arg.getType(), arg.getValueCategory(), copiedValue);
            }
            if (arg == EvalFixed.INCOMPLETE)
        		return EvalFixed.INCOMPLETE;
			args[i] = arg;
		}

		ICPPEvaluation implicitThis = getImplicitThis();
		ICPPEvaluation owner = null;
		if (functionBinding instanceof ICPPMethod) {
			if (implicitThis instanceof EvalBinding) {
				IBinding ownerBinding = ((EvalBinding) implicitThis).getBinding();
				if (record.getVariable(ownerBinding) != null) {
					owner = new EvalReference(record, ownerBinding, implicitThis.getTemplateDefinition());
				} else {
					owner = implicitThis;
				}
			} else if (implicitThis != null) {
				owner = implicitThis.computeForFunctionCall(record, context);
			} else {
				owner = record.getImplicitThis();
			}
		}

		return new EvalFunctionCall(args, owner, getTemplateDefinition()).evaluateFunctionBody(context.recordStep());
	}

	private ICPPEvaluation evaluateFunctionBody(ConstexprEvaluationContext context) {
		if (isValueDependent()) {
			return this;
		}

		// If the arguments are not all constant expressions, there is
		// no point trying to substitute them into the return expression.
		if (!areAllConstantExpressions(fArguments, 1, fArguments.length))
			return EvalFixed.INCOMPLETE;

		ICPPFunction function = resolveFunctionBinding();
		if (function == null)
			return this;
		
		if (!function.isConstexpr())
			return EvalFixed.INCOMPLETE;

		ActivationRecord record = createActivationRecord(function.getParameters(), fArguments, 
				getImplicitThis());
		ICPPExecution bodyExec = CPPFunction.getFunctionBodyExecution(function);
		if (bodyExec == null) {
			if (!(function instanceof ICPPTemplateInstance)
					|| ((ICPPTemplateInstance) function).isExplicitSpecialization()) {
				return EvalFixed.INCOMPLETE;
			}
			ICPPTemplateInstance functionInstance = (ICPPTemplateInstance) function;
			IBinding specialized = functionInstance.getSpecializedBinding();
			if (!(specialized instanceof ICPPFunction))
				return this;
			bodyExec = CPPFunction.getFunctionBodyExecution((ICPPFunction) specialized);
		}
		if (bodyExec != null) {
			bodyExec = bodyExec.executeForFunctionCall(record, context.recordStep());

			// If the function exited via a return statement, bodyExec.executeForFunctionCall() will have
			// just returned the ExecReturn, which needs to be executed separately.
			if (bodyExec != null) {
				bodyExec = bodyExec.executeForFunctionCall(record, context.recordStep());
				if (bodyExec instanceof ExecReturn) {
					ExecReturn execReturn = (ExecReturn) bodyExec;

					ICPPEvaluation returnValueEval = execReturn.getReturnValueEvaluation();
					// TODO(nathanridge): ExecReturn.executeForFunctionCall() already calls
					// computeForFunctionCall() on the return value evaluation. Why do we
					// need to do it again, and only if it's an EvalBinding?
					if (returnValueEval instanceof EvalBinding) {
						returnValueEval = returnValueEval.computeForFunctionCall(record, context.recordStep());
					}
					return returnValueEval;
				} else if (bodyExec == ExecIncomplete.INSTANCE) {
					return EvalFixed.INCOMPLETE;
				}
			}
		}
		return EvalFixed.INCOMPLETE;
	}

	private ICPPFunction resolveFunctionBinding() {
		ICPPFunction function = getOverload();
		if (function == null) {
			ICPPEvaluation funcEval = fArguments[0];
			if (funcEval instanceof EvalFunctionSet) {
				EvalFunctionSet funcEvalFunctionSet = (EvalFunctionSet) funcEval;
				funcEval = funcEvalFunctionSet.resolveFunction(Arrays.copyOfRange(fArguments, 1, fArguments.length));
			}

			IBinding binding = null;
			if (funcEval instanceof EvalBinding) {
				EvalBinding funcEvalBinding = (EvalBinding) funcEval;
				binding = funcEvalBinding.getBinding();
			} else if (funcEval instanceof EvalMemberAccess) {
				EvalMemberAccess funcEvalMemberAccess = (EvalMemberAccess) funcEval;
				binding = funcEvalMemberAccess.getMember();
			}

			if (binding instanceof ICPPFunction) {
				function = (ICPPFunction) binding;
			}
		}
		return function;
	}

	private boolean isReference(IBinding binding) {
		return binding instanceof IVariable
				&& (((IVariable) binding).getType() instanceof ICPPReferenceType || ((IVariable) binding)
						.getType() instanceof IPointerType);
	}

	public static ActivationRecord createActivationRecord(ICPPParameter[] parameters, ICPPEvaluation[] arguments, ICPPEvaluation implicitThis) {
		ActivationRecord record = new ActivationRecord(parameters, implicitThis);

		// We start at arguments[1] because arguments[0] is the function's evaluation.
		int j = 1;
		for (ICPPParameter param : parameters) {
			if (param.isParameterPack() || isSpecializedParameterPack(param)) {
				// The parameter pack consumes all remaining arguments.
				int paramPackLen = arguments.length - j;
				ICPPEvaluation[] values = new ICPPEvaluation[paramPackLen];
				IType[] types = new IType[paramPackLen];
				for (int i = 0; i < paramPackLen; i++) {
					ICPPEvaluation arg = arguments[j+i];
					values[i] = arg;
					types[i] = arg.getType();
				}

				IValue paramPackValue = new CompositeValue(null, values);
				IType paramPackType = new ParameterPackType(types);
				EvalFixed paramPack = new EvalFixed(paramPackType, ValueCategory.PRVALUE, paramPackValue);
				record.update(param, paramPack);
				break;
			} else {
				if (j < arguments.length) {
					ICPPEvaluation argument = maybeApplyConversion(arguments[j++], param.getType(), false);
					record.update(param, argument);
				} else if (param.hasDefaultValue()) {
					IValue value = param.getDefaultValue();
					ICPPEvaluation eval = value.getEvaluation();
					if (eval == null) {
						eval = new EvalFixed(param.getType(), ValueCategory.PRVALUE, value);
					}
					record.update(param, eval);
				}
			}
		}
		return record;
	}

	private static boolean isSpecializedParameterPack(ICPPParameter param) {
		if (param instanceof ICPPSpecialization) {
			ICPPSpecialization paramSpecialization = (ICPPSpecialization) param;
			IBinding specializedBinding = paramSpecialization.getSpecializedBinding();
			if (specializedBinding instanceof ICPPParameter) {
				ICPPParameter specializedParam = (ICPPParameter) specializedBinding;
				return specializedParam.isParameterPack();
			}
		}
		return false;
	}

	@Override
	public int determinePackSize(ICPPTemplateParameterMap tpMap) {
		int r = CPPTemplates.PACK_SIZE_NOT_FOUND;
		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;
	}

	public static class ParameterPackType implements IType {
		private final IType[] types;

		public ParameterPackType(IType[] types) {
			this.types = types;
		}

		public IType[] getTypes() {
			return types;
		}

		@Override
		public boolean isSameType(IType type) {
			return false;
		}

		@Override
		public Object clone() {
			try {
				return super.clone();
			} catch (CloneNotSupportedException e) {
			}
			return null;
		}
	}
}

Back to the top