From 081526f6563de95ddbef7ef853207d66de3ed042 Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Wed, 7 Oct 2009 14:01:24 +0000 Subject: [PATCH] Type of subscript expression, bug 291595. --- .../cdt/core/parser/tests/ast2/AST2Tests.java | 46 +++++++++++++++ .../c/CASTArraySubscriptExpression.java | 1 + .../dom/parser/c/CASTBinaryExpression.java | 59 +++++++++---------- .../internal/core/dom/parser/c/CVisitor.java | 2 +- 4 files changed, 75 insertions(+), 33 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java index e0446f12fb1..9490f86d4ee 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java @@ -6408,6 +6408,52 @@ public class AST2Tests extends AST2BaseTest { } } + + // /* + // * Check that the type returned by CASTArraySubscriptExpression + // * handles typedefs correctly. + // * + // */ + // struct s { + // int a; + // }; + // typedef struct s* ptr; + // typedef struct s array[10]; + // typedef array newArray; + // ptr var1; + // struct s* var2; + // array var3; + // struct s var4[10]; + // newArray var5; + // + // void foo() { + // /* The type of the arraysubscript expression should be struct s + // * each of the following statements + // */ + // var1[1].a = 1; + // var2[1].a = 1; + // var3[1].a = 1; + // var4[1].a = 1; + // var5[1].a = 1; + // } + public void testArraySubscriptExpressionGetExpressionType() throws Exception { + for(ParserLanguage lang : ParserLanguage.values()) { + IASTTranslationUnit tu = parseAndCheckBindings(getAboveComment(), lang); + assertTrue(tu.isFrozen()); + for (IASTDeclaration d : tu.getDeclarations()) { + if (d instanceof IASTFunctionDefinition) { + for (IASTStatement s : ((IASTCompoundStatement)((IASTFunctionDefinition)d).getBody()).getStatements()) { + IASTExpression op1 = ((IASTBinaryExpression)((IASTExpressionStatement)s).getExpression()).getOperand1(); + IASTExpression owner = ((IASTFieldReference)op1).getFieldOwner(); + IType t = owner.getExpressionType(); + assertTrue( t instanceof ICompositeType ); + assertEquals( "s",((ICompositeType)t).getName()); + } + } + } + } + } + // extern int a[]; // int a[1]; public void testIncompleteArrays_269926() throws Exception { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTArraySubscriptExpression.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTArraySubscriptExpression.java index 3b90f56212f..83bdceacf17 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTArraySubscriptExpression.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTArraySubscriptExpression.java @@ -113,6 +113,7 @@ public class CASTArraySubscriptExpression extends ASTNode implements public IType getExpressionType() { IType t = getArrayExpression().getExpressionType(); + t = CVisitor.unwrapTypedefs(t); if (t instanceof IPointerType) return ((IPointerType)t).getType(); else if (t instanceof IArrayType) diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTBinaryExpression.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTBinaryExpression.java index d6a023d38f9..4949470a256 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTBinaryExpression.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTBinaryExpression.java @@ -13,7 +13,6 @@ package org.eclipse.cdt.internal.core.dom.parser.c; import org.eclipse.cdt.core.dom.ast.ASTVisitor; -import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression; import org.eclipse.cdt.core.dom.ast.IASTExpression; import org.eclipse.cdt.core.dom.ast.IASTNode; @@ -129,39 +128,35 @@ public class CASTBinaryExpression extends ASTNode implements public IType getExpressionType() { int op = getOperator(); - try { - switch(op) { - case op_lessEqual: - case op_lessThan: - case op_greaterEqual: - case op_greaterThan: - case op_logicalAnd: - case op_logicalOr: - case op_equals: - case op_notequals: - CBasicType basicType = new CBasicType(IBasicType.t_int, 0); - basicType.setValue(this); - return basicType; - case IASTBinaryExpression.op_plus: - IType t2 = getOperand2().getExpressionType(); - if (CVisitor.unwrapTypedefs(t2) instanceof IPointerType) { - return t2; - } - break; + switch(op) { + case op_lessEqual: + case op_lessThan: + case op_greaterEqual: + case op_greaterThan: + case op_logicalAnd: + case op_logicalOr: + case op_equals: + case op_notequals: + CBasicType basicType = new CBasicType(IBasicType.t_int, 0); + basicType.setValue(this); + return basicType; + case IASTBinaryExpression.op_plus: + IType t2 = getOperand2().getExpressionType(); + if (CVisitor.unwrapTypedefs(t2) instanceof IPointerType) { + return t2; + } + break; - case IASTBinaryExpression.op_minus: - t2= getOperand2().getExpressionType(); - if (CVisitor.unwrapTypedefs(t2) instanceof IPointerType) { - IType t1 = getOperand1().getExpressionType(); - if (CVisitor.unwrapTypedefs(t1) instanceof IPointerType) { - return CVisitor.getPtrDiffType(this); - } - return t1; + case IASTBinaryExpression.op_minus: + t2= getOperand2().getExpressionType(); + if (CVisitor.unwrapTypedefs(t2) instanceof IPointerType) { + IType t1 = getOperand1().getExpressionType(); + if (CVisitor.unwrapTypedefs(t1) instanceof IPointerType) { + return CVisitor.getPtrDiffType(this); } - break; - } - } catch (DOMException e) { - return e.getProblem(); + return t1; + } + break; } return getOperand1().getExpressionType(); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java index 17b8e071665..c8d94049f53 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java @@ -668,7 +668,7 @@ public class CVisitor extends ASTQueries { return new CBasicType(IBasicType.t_int, CBasicType.IS_LONG | CBasicType.IS_UNSIGNED); } - static IType unwrapTypedefs(IType type) throws DOMException { + static IType unwrapTypedefs(IType type) { while (type instanceof ITypedef) { type= ((ITypedef) type).getType(); }