-
Notifications
You must be signed in to change notification settings - Fork 187
/
CPPEvaluation.java
292 lines (264 loc) · 8.79 KB
/
CPPEvaluation.java
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
/*******************************************************************************
* Copyright (c) 2012, 2014, 2023 Google, 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:
* Sergey Prigogin (Google) - initial API and implementation
* Nathan Ridge
* Igor V. Kovalenko
*******************************************************************************/
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.cdt.internal.core.dom.parser.cpp.semantics.Cost.Rank;
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
* @param onlyConstexprConversion allow only constexpr conversion operators
*/
protected static ICPPEvaluation maybeApplyConversion(ICPPEvaluation argument, IType targetType,
boolean allowContextualConversion, boolean onlyConstexprConversion) {
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 (onlyConstexprConversion && !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.
Cost cost = Conversions.checkImplicitConversionSequence(targetType, type, valueCategory, UDCMode.FORBIDDEN,
Context.ORDINARY);
if (!cost.converts()) {
return EvalFixed.INCOMPLETE;
} else if (cost.getRank() == Rank.CONVERSION) {
return new EvalTypeId(targetType, argument.getTemplateDefinition(), false, false, argument);
}
} catch (DOMException e) {
CCorePlugin.log(e);
}
return argument;
}
protected static boolean areEquivalentOrNull(ICPPEvaluation a, ICPPEvaluation b) {
if (a == null) {
return (b == null);
}
return a.isEquivalentTo(b);
}
protected static boolean areEquivalentEvaluations(ICPPEvaluation[] a, ICPPEvaluation[] b) {
if (a == null) {
return (b == null);
}
if (a.length != b.length) {
return false;
}
for (int i = 0; i < a.length; i++) {
if (!a[i].isEquivalentTo(b[i])) {
return false;
}
}
return true;
}
protected static boolean areEquivalentArguments(ICPPTemplateArgument[] a, ICPPTemplateArgument[] b) {
if (a == null) {
return (b == null);
}
if (a.length != b.length) {
return false;
}
for (int i = 0; i < a.length; i++) {
if (!a[i].isSameValue(b[i])) {
return false;
}
}
return true;
}
protected static boolean areEquivalentTypes(IType[] a, IType[] b) {
if (a == null) {
return (b == null);
}
if (a.length != b.length) {
return false;
}
for (int i = 0; i < a.length; i++) {
if (!a[i].isSameType(b[i])) {
return false;
}
}
return true;
}
protected static <T extends IBinding> boolean areEquivalentBindings(T[] a, T[] b) {
if (a == null) {
return (b == null);
}
if (a.length != b.length) {
return false;
}
for (int i = 0; i < a.length; i++) {
if (!a[i].equals(b[i])) {
return false;
}
}
return true;
}
}