Skip to main content
summaryrefslogtreecommitdiffstats
blob: be9b4d90b209a9d0ef0e93db68b97da4cea3db71 (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
/*******************************************************************************
 * Copyright (c) 2012, 2014 Google, 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:
 * 	   Sergey Prigogin (Google) - initial API and implementation
 *     Nathan Ridge
 *******************************************************************************/
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 org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory;
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.ICPPASTExpression;
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.ICPPTemplateArgument;
import org.eclipse.cdt.internal.core.dom.parser.IntegralValue;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluation;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.InstantiationContext;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.Conversions.Context;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.Conversions.UDCMode;
import org.eclipse.core.runtime.CoreException;

public abstract class CPPEvaluation implements ICPPEvaluation {

	CPPEvaluation() {
	}

	@Override
	public IBinding getTemplateDefinition() {
		return null;
	}

	@Override
	public char[] getSignature() {
		SignatureBuilder buf = new SignatureBuilder();
		try {
			marshal(buf, true);
		} catch (CoreException e) {
			CCorePlugin.log(e);
			return new char[] { '?' };
		}
		return buf.getSignature();
	}
	
	public static IType getType(ICPPASTExpression expr) {
		CPPSemantics.pushLookupPoint(expr);
		try {
			return expr.getEvaluation().getType();
		} finally {
			CPPSemantics.popLookupPoint();
		}
	}
	
	public static ValueCategory getValueCategory(ICPPASTExpression expr) {
		CPPSemantics.pushLookupPoint(expr);
		try {
			return expr.getEvaluation().getValueCategory();
		} finally {
			CPPSemantics.popLookupPoint();
		}
	}

	protected static IBinding resolveUnknown(ICPPUnknownBinding unknown, InstantiationContext context) {
		try {
			return CPPTemplates.resolveUnknown(unknown, context);
		} catch (DOMException e) {
			CCorePlugin.log(e);
		}
		return unknown;
	}

	protected static ICPPTemplateArgument[] instantiateArguments(ICPPTemplateArgument[] args,
			InstantiationContext context, boolean strict) {
		try {
			return CPPTemplates.instantiateArguments(args, context, strict);
		} catch (DOMException e) {
			CCorePlugin.log(e);
		}
		return args;
	}

	protected static IBinding instantiateBinding(IBinding binding, InstantiationContext context, int maxDepth) {
		try {
			return CPPTemplates.instantiateBinding(binding, context, maxDepth);
		} catch (DOMException e) {
			CCorePlugin.log(e);
		}
		return binding;
	}

	protected static boolean containsDependentType(ICPPEvaluation[] evaluations) {
		for (ICPPEvaluation eval : evaluations) {
			if (eval.isTypeDependent())
				return true;
		}
		return false;
	}

	protected static boolean containsDependentValue(ICPPEvaluation[] evaluations) {
		for (ICPPEvaluation eval : evaluations) {
			if (eval.isValueDependent())
				return true;
		}
		return false;
	}

	/**
	 * Checks if all evaluations contained in the given array are constant expressions.
	 *
	 * @param evaluations the evaluations to check
     */
	protected static boolean areAllConstantExpressions(ICPPEvaluation[] evaluations) {
		return areAllConstantExpressions(evaluations, 0, evaluations.length);
	}

	/**
	 * Checks if all evaluations contained in a range of the given array are constant expressions.
	 *
	 * @param evaluations the evaluations to check
     * @param from the initial index of the range to be checked, inclusive
     * @param to the final index of the range to be checked, exclusive
     */
	protected static boolean areAllConstantExpressions(ICPPEvaluation[] evaluations, int from, int to) {
		for (int i = from; i < to; i++) {
			if (!evaluations[i].isConstantExpression()) {
				return false;
			}
		}
		return true;
	}

	protected static boolean isConstexprValue(IValue value) {
		if (value == null) {
			return false;
		}
		ICPPEvaluation innerEval = value.getEvaluation();
		if (innerEval == null) {
			if (value instanceof IntegralValue) {
				return value.numberValue() != null;
			} else {
				return true;
			}
		}
		return innerEval.isConstantExpression();
	}

	protected static boolean isNullOrConstexprFunc(ICPPFunction function) {
		return function == null || function.isConstexpr();
	}

	/**
	 * If a user-defined conversion is required to convert 'argument' to type 'targetType',
	 * returns 'argument' wrapped in an evaluation representing the conversion.
	 * Otherwise, returns 'argument' unmodified.
	 *
	 * @param argument the evaluation to convert
	 * @param targetType the type to convert to
	 * @param allowContextualConversion enable/disable explicit contextual conversion
	 */
	protected static ICPPEvaluation maybeApplyConversion(ICPPEvaluation argument, IType targetType,
			boolean allowContextualConversion) {
		if (targetType == null) {
			return argument;
		}
		
		IType type = argument.getType();
		
		// Types match - don't bother to check for conversions.
		if (targetType.isSameType(type)) {
			return argument;
		}
		
		try {
			// Source type is class type - check for conversion operator.
			IType uqType= SemanticUtil.getNestedType(type, TDEF | REF | CVTYPE);
			ValueCategory valueCategory = argument.getValueCategory();
			if (uqType instanceof ICPPClassType) {
				Cost cost = Conversions.initializationByConversion(valueCategory, type, (ICPPClassType) uqType,
						targetType, false, allowContextualConversion);
				ICPPFunction conversion = cost.getUserDefinedConversion();
				if (conversion != null) {
					if (!conversion.isConstexpr()) {
						return EvalFixed.INCOMPLETE;
					}
					ICPPEvaluation eval = new EvalMemberAccess(uqType, valueCategory, conversion, argument, 
							false, CPPSemantics.getCurrentLookupPoint());
					return new EvalFunctionCall(new ICPPEvaluation[] { eval }, null, (IBinding) null);
				}
			}
			
			// Source type is not a class type, or is but a conversion operator wasn't used.
			// Check for standard conversions.
			if (!Conversions.checkImplicitConversionSequence(targetType, type, valueCategory, UDCMode.FORBIDDEN, 
					Context.ORDINARY).converts()) {
				return EvalFixed.INCOMPLETE;
			}
		} catch (DOMException e) {
			CCorePlugin.log(e);
		}
		return argument;
	}
}

Back to the top