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
|
/*******************************************************************************
* Copyright (c) 2012, 2013 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.glvalueType;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.ExpressionTypes.prvalueType;
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.IEnumerator;
import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.IFunctionType;
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.ICPPClassSpecialization;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPParameterPackType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPSpecialization;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateArgument;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateNonTypeParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameterMap;
import org.eclipse.cdt.internal.core.dom.parser.IInternalVariable;
import org.eclipse.cdt.internal.core.dom.parser.ISerializableEvaluation;
import org.eclipse.cdt.internal.core.dom.parser.ITypeMarshalBuffer;
import org.eclipse.cdt.internal.core.dom.parser.ProblemType;
import org.eclipse.cdt.internal.core.dom.parser.Value;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPParameter;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluation;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownBinding;
import org.eclipse.core.runtime.CoreException;
public class EvalBinding extends CPPDependentEvaluation {
/**
* The function owning the parameter if the binding is a function parameter, otherwise
* {@code null}. May be computed lazily and remains {@code null} until computed.
*/
private ICPPFunction fParameterOwner;
/**
* The position of the parameter in the parameter list if the binding is a function parameter,
* otherwise -1.
*/
private int fParameterPosition;
/**
* The binding represented by this evaluation. For a function parameter binding may be computed
* lazily to avoid infinite recursion during unmarshalling of the evaluation. If #fBinding is
* {@code null}, {@link #fParameterOwner} is guaranteed to be not {@code null} and vice versa.
*/
private IBinding fBinding;
private final boolean fFixedType;
private IType fType;
private boolean fCheckedIsValueDependent;
private boolean fIsValueDependent;
private boolean fIsTypeDependent;
private boolean fCheckedIsTypeDependent;
public EvalBinding(IBinding binding, IType type, IASTNode pointOfDefinition) {
this(binding, type, findEnclosingTemplate(pointOfDefinition));
}
public EvalBinding(IBinding binding, IType type, IBinding templateDefinition) {
super(templateDefinition);
fParameterPosition = -1;
fBinding= binding;
fType= type;
fFixedType= type != null;
}
public EvalBinding(ICPPFunction parameterOwner, int parameterPosition, IType type, IASTNode pointOfDefinition) {
this(parameterOwner, parameterPosition, type, findEnclosingTemplate(pointOfDefinition));
}
public EvalBinding(ICPPFunction parameterOwner, int parameterPosition, IType type, IBinding templateDefinition) {
super(templateDefinition);
fParameterOwner = parameterOwner;
fParameterPosition = parameterPosition;
fType= type;
fFixedType= type != null;
}
public IBinding getBinding() {
if (fBinding == null) {
// fParameterOwner is guaranteed to be not null.
ICPPParameter[] parameters = fParameterOwner.getParameters();
fBinding = parameters[fParameterPosition];
}
return fBinding;
}
/**
* @return if the binding is a function parameter, returns its position in the parameter list,
* otherwise returns -1
*/
public int getFunctionParameterPosition() {
if (fParameterPosition < 0) {
if (fBinding instanceof CPPParameter) {
fParameterPosition = ((CPPParameter) fBinding).getParameterPosition();
} else {
ICPPFunction parameterOwner = getParameterOwner();
if (parameterOwner != null) {
ICPPParameter[] parameters = fParameterOwner.getParameters();
fParameterPosition = findInArray(parameters, fBinding);
}
}
}
return fParameterPosition;
}
/**
* Finds a given object in an array.
*
* @param array the array to find the object in
* @param obj the object to find
* @return the index of the object in the array, or -1 if the object is not in the array
*/
private static int findInArray(Object[] array, Object obj) {
for (int i = 0; i < array.length; i++) {
if (obj == array[i])
return i;
}
return -1;
}
/**
* @return the function owning the parameter if the binding is a function parameter,
* otherwise {@code null}.
*/
public ICPPFunction getParameterOwner() {
if (fParameterOwner == null && fBinding instanceof ICPPParameter) {
IBinding owner = fBinding.getOwner();
if (owner instanceof ICPPFunction)
fParameterOwner = (ICPPFunction) owner;
}
return fParameterOwner;
}
/**
* @return if the binding is a template parameter, returns its ID, otherwise returns -1
*/
public int getTemplateParameterID() {
// No need to call getBinding method since fBinding cannot be null if the evaluation
// represents a template parameter.
return fBinding instanceof ICPPTemplateParameter ?
((ICPPTemplateParameter) fBinding).getParameterID() : -1;
}
public IType getFixedType() {
return fFixedType ? fType : null;
}
@Override
public boolean isInitializerList() {
return false;
}
@Override
public boolean isFunctionSet() {
return false;
}
@Override
public boolean isTypeDependent() {
if (!fCheckedIsTypeDependent) {
fCheckedIsTypeDependent= true;
fIsTypeDependent= computeIsTypeDependent();
}
return fIsTypeDependent;
}
private boolean computeIsTypeDependent() {
IType t= null;
if (fFixedType) {
t = fType;
} else {
IBinding binding = getBinding();
if (binding instanceof IEnumerator) {
t= ((IEnumerator) binding).getType();
} else if (binding instanceof ICPPTemplateNonTypeParameter) {
t= ((ICPPTemplateNonTypeParameter) binding).getType();
} else if (binding instanceof IVariable) {
t = ((IVariable) binding).getType();
} else if (binding instanceof ICPPUnknownBinding) {
return true;
} else if (binding instanceof IFunction) {
t= ((IFunction) binding).getType();
} else {
return false;
}
}
return CPPTemplates.isDependentType(t);
}
@Override
public boolean isValueDependent() {
if (!fCheckedIsValueDependent) {
fCheckedIsValueDependent= true;
fIsValueDependent= computeIsValueDependent();
}
return fIsValueDependent;
}
private boolean computeIsValueDependent() {
// No need to call getBinding() since a function parameter never has an initial value.
if (fBinding instanceof IEnumerator) {
return Value.isDependentValue(((IEnumerator) fBinding).getValue());
}
if (fBinding instanceof ICPPTemplateNonTypeParameter) {
return true;
}
if (fBinding instanceof IVariable) {
return Value.isDependentValue(((IVariable) fBinding).getInitialValue());
}
if (fBinding instanceof ICPPUnknownBinding) {
return true;
}
if (fBinding instanceof IFunction) {
return false;
}
return false;
}
@Override
public IType getTypeOrFunctionSet(IASTNode point) {
if (fType == null) {
fType= computeType(point);
}
return fType;
}
private IType computeType(IASTNode point) {
IBinding binding = getBinding();
if (binding instanceof IEnumerator) {
return ((IEnumerator) binding).getType();
}
if (binding instanceof ICPPTemplateNonTypeParameter) {
IType type= ((ICPPTemplateNonTypeParameter) binding).getType();
// If the binding is a non-type parameter pack, it must have been
// referenced from inside the expansion pattern of a pack expansion.
// In such a context, the type of the binding is the type of each
// parameter in the parameter pack, not the type of the pack itself.
if (type instanceof ICPPParameterPackType)
type = ((ICPPParameterPackType) type).getType();
return prvalueType(type);
}
if (binding instanceof IVariable) {
final IType type = ((IVariable) binding).getType();
return SemanticUtil.mapToAST(glvalueType(type), point);
}
if (binding instanceof IFunction) {
final IFunctionType type = ((IFunction) binding).getType();
return SemanticUtil.mapToAST(type, point);
}
return ProblemType.UNKNOWN_FOR_EXPRESSION;
}
@Override
public IValue getValue(IASTNode point) {
if (isValueDependent())
return Value.create(this);
IValue value= null;
// No need to call getBinding() since a function parameter never has an initial value.
if (fBinding instanceof IInternalVariable) {
value= ((IInternalVariable) fBinding).getInitialValue(Value.MAX_RECURSION_DEPTH);
} else if (fBinding instanceof IVariable) {
value= ((IVariable) fBinding).getInitialValue();
} else if (fBinding instanceof IEnumerator) {
value= ((IEnumerator) fBinding).getValue();
}
if (value == null)
value = Value.UNKNOWN;
return value;
}
@Override
public ValueCategory getValueCategory(IASTNode point) {
if (fBinding instanceof ICPPTemplateNonTypeParameter)
return ValueCategory.PRVALUE;
// fBinding can be null only when the evaluation represents a function parameter.
if (fBinding instanceof IFunction || fBinding instanceof IVariable || fBinding == null) {
return ValueCategory.LVALUE;
}
return ValueCategory.PRVALUE;
}
@Override
public void marshal(ITypeMarshalBuffer buffer, boolean includeValue) throws CoreException {
byte firstByte = ITypeMarshalBuffer.EVAL_BINDING;
ICPPFunction parameterOwner = getParameterOwner();
if (parameterOwner != null) {
// A function parameter cannot be marshalled directly. We are storing the owning
// function and the parameter position instead.
buffer.putByte((byte) (ITypeMarshalBuffer.EVAL_BINDING | ITypeMarshalBuffer.FLAG1));
buffer.marshalBinding(parameterOwner);
buffer.putInt(getFunctionParameterPosition());
} else {
buffer.putByte(firstByte);
buffer.marshalBinding(fBinding);
}
buffer.marshalType(fFixedType ? fType : null);
marshalTemplateDefinition(buffer);
}
public static ISerializableEvaluation unmarshal(int firstByte, ITypeMarshalBuffer buffer) throws CoreException {
if ((firstByte & ITypeMarshalBuffer.FLAG1) != 0) {
ICPPFunction parameterOwner= (ICPPFunction) buffer.unmarshalBinding();
int parameterPosition= buffer.getInt();
IType type= buffer.unmarshalType();
IBinding templateDefinition= buffer.unmarshalBinding();
return new EvalBinding(parameterOwner, parameterPosition, type, templateDefinition);
} else {
IBinding binding= buffer.unmarshalBinding();
IType type= buffer.unmarshalType();
IBinding templateDefinition= buffer.unmarshalBinding();
return new EvalBinding(binding, type, templateDefinition);
}
}
@Override
public ICPPEvaluation instantiate(ICPPTemplateParameterMap tpMap, int packOffset,
ICPPClassSpecialization within, int maxdepth, IASTNode point) {
IBinding origBinding = getBinding();
if (origBinding instanceof ICPPTemplateNonTypeParameter) {
ICPPTemplateArgument argument = tpMap.getArgument((ICPPTemplateNonTypeParameter) origBinding, packOffset);
if (argument != null && argument.isNonTypeValue()) {
return argument.getNonTypeEvaluation();
}
// TODO(sprigogin): Do we need something similar for pack expansion?
} else if (origBinding instanceof ICPPParameter) {
ICPPParameter parameter = (ICPPParameter) origBinding;
IType origType = parameter.getType();
IType instantiatedType = CPPTemplates.instantiateType(origType, tpMap, packOffset, within, point);
if (origType != instantiatedType) {
return new EvalFixed(instantiatedType, ValueCategory.LVALUE, Value.create(this));
}
} else {
IBinding instantiatedBinding = instantiateBinding(origBinding, tpMap, packOffset, within, maxdepth, point);
if (instantiatedBinding != origBinding)
return new EvalBinding(instantiatedBinding, null, getTemplateDefinition());
}
return this;
}
@Override
public ICPPEvaluation computeForFunctionCall(CPPFunctionParameterMap parameterMap,
int maxdepth, IASTNode point) {
int pos = getFunctionParameterPosition();
if (pos >= 0) {
ICPPEvaluation eval = parameterMap.getArgument(pos);
if (eval != null)
return eval;
}
return this;
}
@Override
public int determinePackSize(ICPPTemplateParameterMap tpMap) {
IBinding binding = getBinding();
if (binding instanceof IEnumerator) {
return CPPTemplates.determinePackSize(((IEnumerator) binding).getValue(), tpMap);
}
if (binding instanceof ICPPTemplateNonTypeParameter) {
return CPPTemplates.determinePackSize((ICPPTemplateNonTypeParameter) binding, tpMap);
}
if (binding instanceof ICPPUnknownBinding) {
return CPPTemplates.determinePackSize((ICPPUnknownBinding) binding, tpMap);
}
if (binding instanceof ICPPSpecialization) {
binding = ((ICPPSpecialization) binding).getSpecializedBinding();
}
int r = CPPTemplates.PACK_SIZE_NOT_FOUND;
if (binding instanceof ICPPTemplateDefinition) {
ICPPTemplateParameter[] parameters = ((ICPPTemplateDefinition) binding).getTemplateParameters();
for (ICPPTemplateParameter param : parameters) {
r = CPPTemplates.combinePackSize(r, CPPTemplates.determinePackSize(param, tpMap));
}
}
return r;
}
@Override
public boolean referencesTemplateParameter() {
// No need to call getBinding method since fBinding cannot be null if the evaluation
// represents a template parameter.
return fBinding instanceof ICPPTemplateParameter;
}
}
|