mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 14:42:11 +02:00
Code streamlining.
Change-Id: If94003c41bac154601ddd8ce87a0418fbf4f1b0c
This commit is contained in:
parent
66c965df2e
commit
dd92d1c9d5
1 changed files with 52 additions and 64 deletions
|
@ -16,6 +16,7 @@ import org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||||
import org.eclipse.cdt.core.dom.ast.IArrayType;
|
import org.eclipse.cdt.core.dom.ast.IArrayType;
|
||||||
import org.eclipse.cdt.core.dom.ast.IPointerType;
|
import org.eclipse.cdt.core.dom.ast.IPointerType;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IQualifierType;
|
||||||
import org.eclipse.cdt.core.dom.ast.IType;
|
import org.eclipse.cdt.core.dom.ast.IType;
|
||||||
import org.eclipse.cdt.core.dom.ast.IValue;
|
import org.eclipse.cdt.core.dom.ast.IValue;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding;
|
||||||
|
@ -25,7 +26,6 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.CompositeValue;
|
import org.eclipse.cdt.internal.core.dom.parser.CompositeValue;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.ITypeMarshalBuffer;
|
import org.eclipse.cdt.internal.core.dom.parser.ITypeMarshalBuffer;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPBasicType;
|
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPBasicType;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPQualifierType;
|
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluation;
|
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluation;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluation.ConstexprEvaluationContext;
|
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluation.ConstexprEvaluationContext;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPExecution;
|
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPExecution;
|
||||||
|
@ -43,16 +43,14 @@ public final class ExecDeclarator implements ICPPExecution {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ICPPExecution executeForFunctionCall(ActivationRecord record, ConstexprEvaluationContext context) {
|
public ICPPExecution executeForFunctionCall(ActivationRecord record, ConstexprEvaluationContext context) {
|
||||||
if (!(declaredBinding instanceof ICPPVariable)) {
|
if (!(declaredBinding instanceof ICPPVariable))
|
||||||
return this;
|
return this;
|
||||||
}
|
|
||||||
|
|
||||||
ICPPVariable declaredVariable = (ICPPVariable) declaredBinding;
|
ICPPVariable declaredVariable = (ICPPVariable) declaredBinding;
|
||||||
IType type = declaredVariable.getType();
|
IType type = declaredVariable.getType();
|
||||||
ICPPEvaluation initialValue = createInitialValue(type, record, context);
|
ICPPEvaluation initialValue = createInitialValue(type, record, context);
|
||||||
if (initialValue == null || initialValue == EvalFixed.INCOMPLETE) {
|
if (initialValue == null || initialValue == EvalFixed.INCOMPLETE)
|
||||||
return ExecIncomplete.INSTANCE;
|
return ExecIncomplete.INSTANCE;
|
||||||
}
|
|
||||||
|
|
||||||
record.update(declaredBinding, initialValue);
|
record.update(declaredBinding, initialValue);
|
||||||
return this;
|
return this;
|
||||||
|
@ -64,68 +62,71 @@ public final class ExecDeclarator implements ICPPExecution {
|
||||||
|
|
||||||
private static ICPPEvaluation maybeUnwrapInitList(ICPPEvaluation eval, IType targetType, IASTNode point) {
|
private static ICPPEvaluation maybeUnwrapInitList(ICPPEvaluation eval, IType targetType, IASTNode point) {
|
||||||
// Only 1-element initializer lists are eligible for unwrapping.
|
// Only 1-element initializer lists are eligible for unwrapping.
|
||||||
if (!(eval instanceof EvalInitList)) {
|
if (!(eval instanceof EvalInitList))
|
||||||
return eval;
|
return eval;
|
||||||
}
|
|
||||||
EvalInitList initList = (EvalInitList) eval;
|
EvalInitList initList = (EvalInitList) eval;
|
||||||
if (initList.getClauses().length != 1) {
|
ICPPEvaluation[] clauses = initList.getClauses();
|
||||||
|
if (clauses.length != 1)
|
||||||
return eval;
|
return eval;
|
||||||
}
|
|
||||||
|
|
||||||
// Never unwrap initializers for array types.
|
// Never unwrap initializers for array types.
|
||||||
if (isArrayType(targetType)) {
|
if (targetType instanceof IArrayType)
|
||||||
return eval;
|
return eval;
|
||||||
}
|
|
||||||
|
|
||||||
// Only unwrap initializers for class types if the type of the initializer
|
// Only unwrap initializers for class types if the type of the initializer
|
||||||
// element matches the class type, indicating that we're calling the
|
// element matches the class type, indicating that we're calling the
|
||||||
// implicit copy constructor (as opposed to doing memberwise initialization).
|
// implicit copy constructor (as opposed to doing memberwise initialization).
|
||||||
if (isClassType(targetType)) {
|
ICPPEvaluation clause = clauses[0];
|
||||||
if (!initList.getClauses()[0].getType(point).isSameType(targetType)) {
|
if (targetType instanceof ICPPClassType && !clause.getType(point).isSameType(targetType))
|
||||||
return eval;
|
return eval;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise unwrap.
|
// Otherwise unwrap.
|
||||||
return initList.getClauses()[0];
|
return clause;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ICPPEvaluation createInitialValue(IType type, ActivationRecord record, ConstexprEvaluationContext context) {
|
private ICPPEvaluation createInitialValue(IType type, ActivationRecord record,
|
||||||
if (initializerEval == null) {
|
ConstexprEvaluationContext context) {
|
||||||
|
if (initializerEval == null)
|
||||||
return createDefaultInitializedCompositeValue(type);
|
return createDefaultInitializedCompositeValue(type);
|
||||||
}
|
|
||||||
|
|
||||||
IType nestedType = SemanticUtil.getNestedType(type, TDEF | REF | CVTYPE);
|
IType nestedType = SemanticUtil.getNestedType(type, TDEF | REF | CVTYPE);
|
||||||
|
|
||||||
ICPPEvaluation computedInitializerEval = initializerEval.computeForFunctionCall(record, context.recordStep());
|
ICPPEvaluation computedInitializerEval =
|
||||||
|
initializerEval.computeForFunctionCall(record, context.recordStep());
|
||||||
|
|
||||||
// In some contexts, unwrap 1-element initializer lists.
|
// In some contexts, unwrap 1-element initializer lists.
|
||||||
computedInitializerEval = maybeUnwrapInitList(computedInitializerEval, nestedType, context.getPoint());
|
computedInitializerEval = maybeUnwrapInitList(computedInitializerEval, nestedType, context.getPoint());
|
||||||
|
|
||||||
if (isReferenceType(type)) {
|
if (type instanceof ICPPReferenceType)
|
||||||
return createReferenceValue(record, context, computedInitializerEval);
|
return createReferenceValue(record, context, computedInitializerEval);
|
||||||
} else if (isPointerType(nestedType) && !isCStringType(nestedType)) {
|
|
||||||
|
if (nestedType instanceof IPointerType && !isCStringType(nestedType))
|
||||||
return createPointerValue(record, context, computedInitializerEval);
|
return createPointerValue(record, context, computedInitializerEval);
|
||||||
} else if (isArrayType(nestedType) && !isCStringType(nestedType)) {
|
|
||||||
|
if (nestedType instanceof IArrayType && !isCStringType(nestedType)) {
|
||||||
if (computedInitializerEval instanceof EvalInitList) {
|
if (computedInitializerEval instanceof EvalInitList) {
|
||||||
IValue value = CompositeValue.create((EvalInitList) computedInitializerEval,
|
IValue value = CompositeValue.create((EvalInitList) computedInitializerEval,
|
||||||
(IArrayType) nestedType, context.getPoint());
|
(IArrayType) nestedType, context.getPoint());
|
||||||
return new EvalFixed(type, computedInitializerEval.getValueCategory(context.getPoint()), value);
|
return new EvalFixed(type, computedInitializerEval.getValueCategory(context.getPoint()), value);
|
||||||
} else {
|
|
||||||
// TODO(sprigogin): Should something else be done here?
|
|
||||||
return EvalFixed.INCOMPLETE;
|
|
||||||
}
|
}
|
||||||
} else if (isValueInitialization(computedInitializerEval)) {
|
// TODO(sprigogin): Should something else be done here?
|
||||||
ICPPEvaluation defaultValue = new EvalTypeId(type, context.getPoint(), false, new ICPPEvaluation[]{});
|
return EvalFixed.INCOMPLETE;
|
||||||
return new EvalFixed(type, defaultValue.getValueCategory(context.getPoint()), defaultValue.getValue(context.getPoint()));
|
|
||||||
} else {
|
|
||||||
return new EvalFixed(type, computedInitializerEval.getValueCategory(context.getPoint()),
|
|
||||||
computedInitializerEval.getValue(context.getPoint()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isValueInitialization(computedInitializerEval)) {
|
||||||
|
ICPPEvaluation defaultValue =
|
||||||
|
new EvalTypeId(type, context.getPoint(), false, ICPPEvaluation.EMPTY_ARRAY);
|
||||||
|
return new EvalFixed(type, defaultValue.getValueCategory(context.getPoint()),
|
||||||
|
defaultValue.getValue(context.getPoint()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new EvalFixed(type, computedInitializerEval.getValueCategory(context.getPoint()),
|
||||||
|
computedInitializerEval.getValue(context.getPoint()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ICPPEvaluation createDefaultInitializedCompositeValue(IType type) {
|
private static ICPPEvaluation createDefaultInitializedCompositeValue(IType type) {
|
||||||
if (!isClassType(type)) {
|
if (!(type instanceof ICPPClassType)) {
|
||||||
return EvalFixed.INCOMPLETE;
|
return EvalFixed.INCOMPLETE;
|
||||||
}
|
}
|
||||||
ICPPClassType classType = (ICPPClassType) type;
|
ICPPClassType classType = (ICPPClassType) type;
|
||||||
|
@ -141,18 +142,17 @@ public final class ExecDeclarator implements ICPPExecution {
|
||||||
ICPPEvaluation initValue = initializerEval;
|
ICPPEvaluation initValue = initializerEval;
|
||||||
if (initValue instanceof EvalInitList) {
|
if (initValue instanceof EvalInitList) {
|
||||||
initValue = ((EvalInitList) initValue).getClauses()[0];
|
initValue = ((EvalInitList) initValue).getClauses()[0];
|
||||||
}
|
} else if (!(initValue instanceof EvalBinding)) {
|
||||||
else if (!(initValue instanceof EvalBinding)) {
|
|
||||||
initValue = initializerEval.getValue(context.getPoint()).getSubValue(0);
|
initValue = initializerEval.getValue(context.getPoint()).getSubValue(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (initValue instanceof EvalBinding) {
|
if (initValue instanceof EvalBinding)
|
||||||
return createReferenceFromBinding(record, context, (EvalBinding) initValue);
|
return createReferenceFromBinding(record, context, (EvalBinding) initValue);
|
||||||
} else if (initValue instanceof EvalBinary && computedInitializerEval instanceof EvalCompositeAccess) {
|
|
||||||
|
if (initValue instanceof EvalBinary && computedInitializerEval instanceof EvalCompositeAccess)
|
||||||
return createReferenceFromCompositeAccess(record, context, (EvalCompositeAccess) computedInitializerEval);
|
return createReferenceFromCompositeAccess(record, context, (EvalCompositeAccess) computedInitializerEval);
|
||||||
} else {
|
|
||||||
return EvalFixed.INCOMPLETE;
|
return EvalFixed.INCOMPLETE;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private ICPPEvaluation createPointerValue(ActivationRecord record, ConstexprEvaluationContext context,
|
private ICPPEvaluation createPointerValue(ActivationRecord record, ConstexprEvaluationContext context,
|
||||||
|
@ -161,10 +161,11 @@ public final class ExecDeclarator implements ICPPExecution {
|
||||||
if (isPointerToArray(initValue, context)) {
|
if (isPointerToArray(initValue, context)) {
|
||||||
EvalCompositeAccess arrayPointer = new EvalCompositeAccess(computedInitializerEval, 0);
|
EvalCompositeAccess arrayPointer = new EvalCompositeAccess(computedInitializerEval, 0);
|
||||||
return createPointerFromCompositeAccess(record, context, arrayPointer);
|
return createPointerFromCompositeAccess(record, context, arrayPointer);
|
||||||
} else if (computedInitializerEval instanceof EvalPointer) {
|
|
||||||
EvalPointer pointer = (EvalPointer) computedInitializerEval;
|
|
||||||
return pointer.copy();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (computedInitializerEval instanceof EvalPointer)
|
||||||
|
return ((EvalPointer) computedInitializerEval).copy();
|
||||||
|
|
||||||
return EvalFixed.INCOMPLETE;
|
return EvalFixed.INCOMPLETE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,6 +174,7 @@ public final class ExecDeclarator implements ICPPExecution {
|
||||||
EvalInitList evalInitList = (EvalInitList) eval;
|
EvalInitList evalInitList = (EvalInitList) eval;
|
||||||
return evalInitList.getClauses().length == 0;
|
return evalInitList.getClauses().length == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,18 +197,6 @@ public final class ExecDeclarator implements ICPPExecution {
|
||||||
return new EvalPointer(record, evalCompAccess, context.getPoint());
|
return new EvalPointer(record, evalCompAccess, context.getPoint());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isReferenceType(IType type) {
|
|
||||||
return type instanceof ICPPReferenceType;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean isPointerType(IType type) {
|
|
||||||
return type instanceof IPointerType;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean isArrayType(IType type) {
|
|
||||||
return type instanceof IArrayType;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean isCStringType(IType type) {
|
private static boolean isCStringType(IType type) {
|
||||||
IType nestedType = null;
|
IType nestedType = null;
|
||||||
if (type instanceof IArrayType) {
|
if (type instanceof IArrayType) {
|
||||||
|
@ -215,16 +205,14 @@ public final class ExecDeclarator implements ICPPExecution {
|
||||||
nestedType = ((IPointerType) type).getType();
|
nestedType = ((IPointerType) type).getType();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nestedType != null) {
|
if (nestedType instanceof IQualifierType) {
|
||||||
return nestedType.isSameType(new CPPQualifierType(CPPBasicType.CHAR, true, false));
|
IQualifierType qualifierType = (IQualifierType) nestedType;
|
||||||
|
if (qualifierType.isConst() && !qualifierType.isVolatile())
|
||||||
|
return qualifierType.getType().isSameType(CPPBasicType.CHAR);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isClassType(IType type) {
|
|
||||||
return type instanceof ICPPClassType;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ICPPExecution instantiate(InstantiationContext context, int maxDepth) {
|
public ICPPExecution instantiate(InstantiationContext context, int maxDepth) {
|
||||||
ICPPBinding newDeclaredBinding;
|
ICPPBinding newDeclaredBinding;
|
||||||
|
@ -232,8 +220,8 @@ public final class ExecDeclarator implements ICPPExecution {
|
||||||
ICPPVariable declaredVariable = (ICPPVariable) declaredBinding;
|
ICPPVariable declaredVariable = (ICPPVariable) declaredBinding;
|
||||||
newDeclaredBinding = CPPTemplates.createVariableSpecialization(context, declaredVariable);
|
newDeclaredBinding = CPPTemplates.createVariableSpecialization(context, declaredVariable);
|
||||||
} else {
|
} else {
|
||||||
newDeclaredBinding = (ICPPBinding)CPPTemplates.createSpecialization(context.getContextSpecialization(),
|
newDeclaredBinding = (ICPPBinding) CPPTemplates.createSpecialization(
|
||||||
declaredBinding, context.getPoint());
|
context.getContextSpecialization(), declaredBinding, context.getPoint());
|
||||||
}
|
}
|
||||||
|
|
||||||
ICPPEvaluation newInitializerEval =
|
ICPPEvaluation newInitializerEval =
|
||||||
|
|
Loading…
Add table
Reference in a new issue