1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 17:05:26 +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.IASTCastExpression;
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.IType;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent;
/**
* 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;
public CASTCastExpression() {
super(op_cast, null);
this.operator = op_cast;
}
public CASTCastExpression(IASTTypeId typeId, IASTExpression operand) {
super(op_cast, operand);
this();
setOperand(operand);
setTypeId(typeId);
}
@Override
public CASTCastExpression copy() {
CASTCastExpression copy = new CASTCastExpression();
copy.setTypeId(typeId == null ? null : typeId.copy());
@ -44,6 +48,28 @@ public class CASTCastExpression extends CASTUnaryExpression implements IASTCastE
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) {
assertNotFrozen();
this.typeId = typeId;
@ -83,7 +109,14 @@ public class CASTCastExpression extends CASTUnaryExpression implements IASTCastE
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() {
IASTTypeId id= getTypeId();
return CVisitor.createType(id.getAbstractDeclarator());

View file

@ -12,29 +12,32 @@
package org.eclipse.cdt.internal.core.dom.parser.cpp;
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.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IType;
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;
/**
* 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;
public CPPASTCastExpression() {
}
public CPPASTCastExpression(int operator, IASTTypeId typeId, IASTExpression operand) {
super(operator, operand);
op = operator;
setOperand(operand);
setTypeId(typeId);
}
@Override
public CPPASTCastExpression copy() {
CPPASTCastExpression copy = new CPPASTCastExpression();
copy.setOperator(getOperator());
@ -59,19 +62,28 @@ public class CPPASTCastExpression extends CPPASTUnaryExpression implements ICPPA
return typeId;
}
@Override
public void setOperand(IASTExpression expression) {
assertNotFrozen();
super.setOperand(expression);
// this needs to be overridden because CPPASTUnaryExpression sets
// propertyInParent to ICPPASTUnaryExpression.OPERAND, we want
// ICPPASTCastExpression.OPERAND
if (expression != null) {
expression.setParent(this);
expression.setPropertyInParent(IASTCastExpression.OPERAND);
}
public int getOperator() {
return op;
}
public void setOperator(int operator) {
assertNotFrozen();
op = operator;
}
public IASTExpression getOperand() {
return operand;
}
public void setOperand(IASTExpression expression) {
assertNotFrozen();
operand = expression;
if (expression != null) {
expression.setParent(this);
expression.setPropertyInParent(OPERAND);
}
}
@Override
public boolean accept( ASTVisitor action ){
if( action.shouldVisitExpressions ){
@ -96,7 +108,14 @@ public class CPPASTCastExpression extends CPPASTUnaryExpression implements ICPPA
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() {
return CPPVisitor.createType(typeId.getAbstractDeclarator());
}

View file

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