Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5d8348e5ab85121698c10cd3274271d32cb07a5a (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
/*******************************************************************************
 * Copyright (c) 2012, 2016 Wind River Systems, 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:
 *     Markus Schorn - initial API and implementation
 *     Sergey Prigogin (Google)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp.semantics;

import static org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory.LVALUE;
import static org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory.PRVALUE;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_alignof;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_nothrow_constructor;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_nothrow_copy;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_trivial_assign;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_trivial_constructor;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_trivial_copy;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_trivial_destructor;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_has_virtual_destructor;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_abstract;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_class;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_empty;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_enum;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_final;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_literal_type;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_pod;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_polymorphic;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_standard_layout;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_trivial;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_trivially_copyable;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_is_union;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_sizeof;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_sizeofParameterPack;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_typeid;
import static org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression.op_typeof;

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.IType;
import org.eclipse.cdt.core.dom.ast.IValue;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateArgument;
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.DependentValue;
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.ProblemType;
import org.eclipse.cdt.internal.core.dom.parser.ValueFactory;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPBasicType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluation;
import org.eclipse.cdt.internal.core.dom.parser.cpp.InstantiationContext;
import org.eclipse.core.runtime.CoreException;

public class EvalUnaryTypeID extends CPPDependentEvaluation {
	private final int fOperator;
	private final IType fOrigType;
	private IType fType;

	public EvalUnaryTypeID(int operator, IType type, IASTNode pointOfDefinition) {
		this(operator, type, findEnclosingTemplate(pointOfDefinition));
	}

	public EvalUnaryTypeID(int operator, IType type, IBinding templateDefinition) {
		super(templateDefinition);
		fOperator = operator;
		fOrigType = type;
	}

	public int getOperator() {
		return fOperator;
	}

	public IType getArgument() {
		return fOrigType;
	}

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

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

	@Override
	public boolean isTypeDependent() {
		if (fOperator == op_typeof)
			return CPPTemplates.isDependentType(fOrigType);
		return false;
	}

	@Override
	public boolean isValueDependent() {
		switch (fOperator) {
		case op_sizeofParameterPack:
			return true;
		case op_sizeof:
		case op_alignof:
		case op_has_nothrow_copy:
		case op_has_nothrow_constructor:
		case op_has_trivial_assign:
		case op_has_trivial_constructor:
		case op_has_trivial_copy:
		case op_has_trivial_destructor:
		case op_has_virtual_destructor:
		case op_is_abstract:
		case op_is_class:
		case op_is_empty:
		case op_is_enum:
		case op_is_final:
		case op_is_literal_type:
		case op_is_pod:
		case op_is_polymorphic:
		case op_is_standard_layout:
		case op_is_trivial:
		case op_is_trivially_copyable:
		case op_is_union:
			return CPPTemplates.isDependentType(fOrigType);

		case op_typeid:
		case op_typeof:
			return false;
		}
		return false;
	}

	@Override
	public boolean isConstantExpression() {
		return true;
	}

	@Override
	public boolean isEquivalentTo(ICPPEvaluation other) {
		if (!(other instanceof EvalUnaryTypeID)) {
			return false;
		}
		EvalUnaryTypeID o = (EvalUnaryTypeID) other;
		return fOperator == o.fOperator && fOrigType.isSameType(o.fOrigType);
	}

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

	private IType computeType() {
		switch (fOperator) {
		case op_sizeof:
		case op_sizeofParameterPack:
		case op_alignof:
			IType result = (IType) CPPVisitor.get_SIZE_T().clone();
			if (SemanticUtil.getSimplifiedType(result) instanceof CPPBasicType) {
				result = CPPVisitor.associateTypeWithValue(result, getValue());
			}
			return result;
		case op_typeid:
			return CPPVisitor.get_type_info();
		case op_has_nothrow_copy:
		case op_has_nothrow_constructor:
		case op_has_trivial_assign:
		case op_has_trivial_constructor:
		case op_has_trivial_copy:
		case op_has_trivial_destructor:
		case op_has_virtual_destructor:
		case op_is_abstract:
		case op_is_class:
		case op_is_empty:
		case op_is_enum:
		case op_is_final:
		case op_is_literal_type:
		case op_is_pod:
		case op_is_polymorphic:
		case op_is_standard_layout:
		case op_is_trivial:
		case op_is_trivially_copyable:
		case op_is_union:
			return CPPBasicType.BOOLEAN;
		case op_typeof:
			if (isTypeDependent())
				return new TypeOfDependentExpression(this);
			return fOrigType;
		}
		return ProblemType.UNKNOWN_FOR_EXPRESSION;
	}

	@Override
	public IValue getValue() {
		if (isValueDependent())
			return DependentValue.create(this);

		return ValueFactory.evaluateUnaryTypeIdExpression(fOperator, fOrigType);
	}

	@Override
	public ValueCategory getValueCategory() {
		return fOperator == op_typeid ? LVALUE : PRVALUE;
	}

	@Override
	public void marshal(ITypeMarshalBuffer buffer, boolean includeValue) throws CoreException {
		buffer.putShort(ITypeMarshalBuffer.EVAL_UNARY_TYPE_ID);
		buffer.putByte((byte) fOperator);
		buffer.marshalType(fOrigType);
		marshalTemplateDefinition(buffer);
	}

	public static ICPPEvaluation unmarshal(short firstBytes, ITypeMarshalBuffer buffer) throws CoreException {
		int op = buffer.getByte();
		IType arg = buffer.unmarshalType();
		IBinding templateDefinition = buffer.unmarshalBinding();
		return new EvalUnaryTypeID(op, arg, templateDefinition);
	}

	@Override
	public ICPPEvaluation instantiate(InstantiationContext context, int maxDepth) {
		if (fOperator == op_sizeofParameterPack) {
			return instantiateSizeofParameterPack(context);
		}
		return instantiateBySubstitution(context);
	}

	@Override
	public ICPPEvaluation computeForFunctionCall(ActivationRecord record, ConstexprEvaluationContext context) {
		return this;
	}

	@Override
	public int determinePackSize(ICPPTemplateParameterMap tpMap) {
		return CPPTemplates.determinePackSize(fOrigType, tpMap);
	}

	@Override
	public boolean referencesTemplateParameter() {
		return CPPTemplates.isDependentType(fOrigType);
	}

	private ICPPEvaluation instantiateBySubstitution(InstantiationContext context) {
		IType type = CPPTemplates.instantiateType(fOrigType, context);
		if (type == fOrigType)
			return this;
		return new EvalUnaryTypeID(fOperator, type, getTemplateDefinition());
	}

	private ICPPEvaluation instantiateSizeofParameterPack(InstantiationContext context) {
		if (fOrigType instanceof ICPPTemplateParameter) {
			ICPPTemplateParameter pack = (ICPPTemplateParameter) fOrigType;
			if (pack.isParameterPack()) {
				ICPPTemplateArgument[] args = context.getPackExpansion(pack);
				if (args == null) {
					return this;
				}
				int concreteArgCount = 0;
				boolean havePackExpansion = false;
				for (ICPPTemplateArgument arg : args) {
					if (arg.isPackExpansion()) {
						havePackExpansion = true;
					} else {
						concreteArgCount++;
					}
				}
				if (havePackExpansion) {
					// TODO(bug 530103):
					//   This will only handle correctly the case where there is a single argument
					//   which is a pack expansion, and no concrete arguments.
					//   To correctly handle cases with multiple pack expansions, or a mixture
					//   of concrete arguments and pack expansions, we need to do the following:
					//     - For each pack expansion, find the parameter pack P which it's
					//       expanding (if it's expanding multiple parameter packs, any one
					//       should be sufficient), and construct an EvalUnaryTypeId representing
					//       sizeof...(P).
					//     - Construct an EvalBinary tree representing the sum of |concreteArgCount|
					//       and the EvalUnaryTypeIds from the previous step.
					return instantiateBySubstitution(context);
				} else {
					return new EvalFixed(getType(), getValueCategory(), IntegralValue.create(concreteArgCount));
				}
			}
		}
		return EvalFixed.INCOMPLETE;
	}

	@Override
	public boolean isNoexcept() {
		return true;
	}
}

Back to the top