1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-01 13:25:45 +02:00

Decouple cast-expressions from unary expressions by Richard Miskin, bug 270252.

This commit is contained in:
Markus Schorn 2009-03-30 13:17:26 +00:00
parent 8d8462bc93
commit dae94a218c
3 changed files with 85 additions and 35 deletions

View file

@ -14,27 +14,31 @@ package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTCastExpression; import org.eclipse.cdt.core.dom.ast.IASTCastExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpression; import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTypeId; import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent;
/** /**
* Cast expressions for c * Cast expressions for c
*/ */
public class CASTCastExpression extends CASTUnaryExpression implements IASTCastExpression { public class CASTCastExpression extends ASTNode implements IASTCastExpression, IASTAmbiguityParent {
private int operator;
private IASTExpression operand;
private IASTTypeId typeId; private IASTTypeId typeId;
public CASTCastExpression() { public CASTCastExpression() {
super(op_cast, null); this.operator = op_cast;
} }
public CASTCastExpression(IASTTypeId typeId, IASTExpression operand) { public CASTCastExpression(IASTTypeId typeId, IASTExpression operand) {
super(op_cast, operand); this();
setOperand(operand);
setTypeId(typeId); setTypeId(typeId);
} }
@Override
public CASTCastExpression copy() { public CASTCastExpression copy() {
CASTCastExpression copy = new CASTCastExpression(); CASTCastExpression copy = new CASTCastExpression();
copy.setTypeId(typeId == null ? null : typeId.copy()); copy.setTypeId(typeId == null ? null : typeId.copy());
@ -44,6 +48,28 @@ public class CASTCastExpression extends CASTUnaryExpression implements IASTCastE
return copy; return copy;
} }
public int getOperator() {
return operator;
}
public void setOperator(int value) {
assertNotFrozen();
this.operator = value;
}
public IASTExpression getOperand() {
return operand;
}
public void setOperand(IASTExpression expression) {
assertNotFrozen();
operand = expression;
if (expression != null) {
expression.setParent(this);
expression.setPropertyInParent(OPERAND);
}
}
public void setTypeId(IASTTypeId typeId) { public void setTypeId(IASTTypeId typeId) {
assertNotFrozen(); assertNotFrozen();
this.typeId = typeId; this.typeId = typeId;
@ -83,7 +109,14 @@ public class CASTCastExpression extends CASTUnaryExpression implements IASTCastE
return true; return true;
} }
@Override public void replace(IASTNode child, IASTNode other) {
if (child == operand) {
other.setPropertyInParent(child.getPropertyInParent());
other.setParent(child.getParent());
operand = (IASTExpression) other;
}
}
public IType getExpressionType() { public IType getExpressionType() {
IASTTypeId id= getTypeId(); IASTTypeId id= getTypeId();
return CVisitor.createType(id.getAbstractDeclarator()); return CVisitor.createType(id.getAbstractDeclarator());

View file

@ -12,29 +12,32 @@
package org.eclipse.cdt.internal.core.dom.parser.cpp; package org.eclipse.cdt.internal.core.dom.parser.cpp;
import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTCastExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpression; import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTypeId; import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCastExpression; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCastExpression;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
/** /**
* Cast expression for c++ * Cast expression for c++
*/ */
public class CPPASTCastExpression extends CPPASTUnaryExpression implements ICPPASTCastExpression { public class CPPASTCastExpression extends ASTNode implements ICPPASTCastExpression, IASTAmbiguityParent {
private int op;
private IASTExpression operand;
private IASTTypeId typeId; private IASTTypeId typeId;
public CPPASTCastExpression() { public CPPASTCastExpression() {
} }
public CPPASTCastExpression(int operator, IASTTypeId typeId, IASTExpression operand) { public CPPASTCastExpression(int operator, IASTTypeId typeId, IASTExpression operand) {
super(operator, operand); op = operator;
setOperand(operand);
setTypeId(typeId); setTypeId(typeId);
} }
@Override
public CPPASTCastExpression copy() { public CPPASTCastExpression copy() {
CPPASTCastExpression copy = new CPPASTCastExpression(); CPPASTCastExpression copy = new CPPASTCastExpression();
copy.setOperator(getOperator()); copy.setOperator(getOperator());
@ -59,16 +62,25 @@ public class CPPASTCastExpression extends CPPASTUnaryExpression implements ICPPA
return typeId; return typeId;
} }
@Override public int getOperator() {
return op;
}
public void setOperator(int operator) {
assertNotFrozen();
op = operator;
}
public IASTExpression getOperand() {
return operand;
}
public void setOperand(IASTExpression expression) { public void setOperand(IASTExpression expression) {
assertNotFrozen(); assertNotFrozen();
super.setOperand(expression); operand = expression;
// this needs to be overridden because CPPASTUnaryExpression sets
// propertyInParent to ICPPASTUnaryExpression.OPERAND, we want
// ICPPASTCastExpression.OPERAND
if (expression != null) { if (expression != null) {
expression.setParent(this); expression.setParent(this);
expression.setPropertyInParent(IASTCastExpression.OPERAND); expression.setPropertyInParent(OPERAND);
} }
} }
@ -96,7 +108,14 @@ public class CPPASTCastExpression extends CPPASTUnaryExpression implements ICPPA
return true; return true;
} }
@Override public void replace(IASTNode child, IASTNode other) {
if (child == operand) {
other.setPropertyInParent(child.getPropertyInParent());
other.setParent(child.getParent());
operand = (IASTExpression) other;
}
}
public IType getExpressionType() { public IType getExpressionType() {
return CPPVisitor.createType(typeId.getAbstractDeclarator()); return CPPVisitor.createType(typeId.getAbstractDeclarator());
} }

View file

@ -125,8 +125,10 @@ public class ExpressionWriter extends NodeWriter{
((IASTIdExpression) expression).getName().accept(visitor); ((IASTIdExpression) expression).getName().accept(visitor);
} else if (expression instanceof IASTLiteralExpression) { } else if (expression instanceof IASTLiteralExpression) {
writeLiteralExpression((IASTLiteralExpression) expression); writeLiteralExpression((IASTLiteralExpression) expression);
} else if (expression instanceof IASTUnaryExpression) {//UnaryExpressions including Cast Expressions } else if (expression instanceof IASTUnaryExpression) {
writeUnaryExpression((IASTUnaryExpression) expression); writeUnaryExpression((IASTUnaryExpression) expression);
} else if (expression instanceof IASTCastExpression) {
writeCastExpression((IASTCastExpression) expression);
} else if (expression instanceof ICPPASTNewExpression) { } else if (expression instanceof ICPPASTNewExpression) {
writeCPPNewExpression((ICPPASTNewExpression) expression); writeCPPNewExpression((ICPPASTNewExpression) expression);
}else if (expression instanceof IASTConditionalExpression) { }else if (expression instanceof IASTConditionalExpression) {
@ -372,9 +374,6 @@ public class ExpressionWriter extends NodeWriter{
} }
private void writeUnaryExpression(IASTUnaryExpression unExp) { private void writeUnaryExpression(IASTUnaryExpression unExp) {
if (unExp instanceof IASTCastExpression) {//Castoperatoren sind auch Unreoperatoren
writeCastExpression((IASTCastExpression) unExp);
}else{
if(isPrefixExpression(unExp )) { if(isPrefixExpression(unExp )) {
scribe.print(getPrefixOperator(unExp)); scribe.print(getPrefixOperator(unExp));
} }
@ -383,7 +382,6 @@ public class ExpressionWriter extends NodeWriter{
scribe.print(getPostfixOperator(unExp)); scribe.print(getPostfixOperator(unExp));
} }
} }
}
private void writeConditionalExpression(IASTConditionalExpression condExp) { private void writeConditionalExpression(IASTConditionalExpression condExp) {
condExp.getLogicalConditionExpression().accept(visitor); condExp.getLogicalConditionExpression().accept(visitor);