1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-13 11:15:38 +02:00

Moved implementation of CVisitor.getExpressionType to the various expression nodes.

This commit is contained in:
Markus Schorn 2009-01-28 16:40:05 +00:00
parent bc804b6935
commit e1053209d2
19 changed files with 312 additions and 287 deletions

View file

@ -20,7 +20,6 @@ import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor;
import org.eclipse.core.runtime.Assert;
/**
@ -52,7 +51,7 @@ public abstract class ASTAmbiguousBinaryVsCastExpression extends ASTAmbiguousNod
}
public IType getExpressionType() {
return CVisitor.getExpressionType(getExpressions()[0]);
return null;
}
@Override

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2005, 2008 IBM Corporation and others.
* Copyright (c) 2005, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@ -16,13 +16,11 @@ import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IASTTypeIdInitializerExpression;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor;
/**
* Compound literals for c and c++.
*/
public class ASTTypeIdInitializerExpression extends ASTNode implements IASTTypeIdInitializerExpression {
public abstract class ASTTypeIdInitializerExpression extends ASTNode implements IASTTypeIdInitializerExpression {
private IASTTypeId typeId;
private IASTInitializer initializer;
@ -34,12 +32,6 @@ public class ASTTypeIdInitializerExpression extends ASTNode implements IASTTypeI
setInitializer(i);
}
public ASTTypeIdInitializerExpression copy() {
ASTTypeIdInitializerExpression copy = new ASTTypeIdInitializerExpression();
initializeCopy(copy);
return copy;
}
protected void initializeCopy(ASTTypeIdInitializerExpression copy) {
copy.setTypeId(typeId == null ? null : typeId.copy());
copy.setInitializer(initializer == null ? null : initializer.copy());
@ -94,8 +86,4 @@ public class ASTTypeIdInitializerExpression extends ASTNode implements IASTTypeI
}
return true;
}
public IType getExpressionType() {
return CVisitor.getExpressionType(this);
}
}

View file

@ -49,7 +49,7 @@ public class CASTAmbiguousExpression extends ASTAmbiguousNode implements IASTAmb
}
public IType getExpressionType() {
return CVisitor.getExpressionType(this);
return null;
}
public IASTExpression copy() {

View file

@ -1,26 +1,30 @@
/*******************************************************************************
* Copyright (c) 2005, 2008 IBM Corporation and others.
* Copyright (c) 2005, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
* Yuan Zhang / Beth Tibbitts (IBM Research)
* John Camelon (IBM Rational Software) - Initial API and implementation
* Yuan Zhang / Beth Tibbitts (IBM Research)
* Markus Schorn (Wind River Systems)
*******************************************************************************/
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.IASTArraySubscriptExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IArrayType;
import org.eclipse.cdt.core.dom.ast.IPointerType;
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;
/**
* @author jcamelon
* Array subscript expression for c
*/
public class CASTArraySubscriptExpression extends ASTNode implements
IASTArraySubscriptExpression, IASTAmbiguityParent {
@ -109,7 +113,15 @@ public class CASTArraySubscriptExpression extends ASTNode implements
}
public IType getExpressionType() {
return CVisitor.getExpressionType(this);
IType t = getArrayExpression().getExpressionType();
try {
if (t instanceof IPointerType)
return ((IPointerType)t).getType();
else if (t instanceof IArrayType)
return ((IArrayType)t).getType();
} catch (DOMException e) {
return e.getProblem();
}
return t;
}
}

View file

@ -1,26 +1,30 @@
/*******************************************************************************
* Copyright (c) 2005, 2008 IBM Corporation and others.
* Copyright (c) 2005, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
* Yuan Zhang / Beth Tibbitts (IBM Research)
* John Camelon (IBM Rational Software) - Initial API and implementation
* Yuan Zhang / Beth Tibbitts (IBM Research)
* Markus Schorn (Wind River Systems)
*******************************************************************************/
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;
import org.eclipse.cdt.core.dom.ast.IBasicType;
import org.eclipse.cdt.core.dom.ast.IPointerType;
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;
/**
* @author jcamelon
* Binary expression for c
*/
public class CASTBinaryExpression extends ASTNode implements
IASTBinaryExpression, IASTAmbiguityParent {
@ -124,7 +128,42 @@ public class CASTBinaryExpression extends ASTNode implements
}
public IType getExpressionType() {
return CVisitor.getExpressionType(this);
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;
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;
}
break;
}
} catch (DOMException e) {
return e.getProblem();
}
return getOperand1().getExpressionType();
}
}

View file

@ -1,12 +1,13 @@
/*******************************************************************************
* Copyright (c) 2005, 2008 IBM Corporation and others.
* Copyright (c) 2005, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
* John Camelon (IBM Rational Software) - Initial API and implementation
* Markus Schorn (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c;
@ -14,9 +15,10 @@ 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.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IType;
/**
* @author jcamelon
* Cast expressions for c
*/
public class CASTCastExpression extends CASTUnaryExpression implements IASTCastExpression {
@ -80,4 +82,10 @@ public class CASTCastExpression extends CASTUnaryExpression implements IASTCastE
return true;
}
@Override
public IType getExpressionType() {
IASTTypeId id= getTypeId();
return CVisitor.createType(id.getAbstractDeclarator());
}
}

View file

@ -1,27 +1,29 @@
/*******************************************************************************
* Copyright (c) 2005, 2008 IBM Corporation and others.
* Copyright (c) 2005, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
* Yuan Zhang / Beth Tibbitts (IBM Research)
* John Camelon (IBM Rational Software) - Initial API and implementation
* Yuan Zhang / Beth Tibbitts (IBM Research)
* Markus Schorn (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTCompoundStatementExpression;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
/**
* @author jcamelon
* Compound statement for c
*/
public class CASTCompoundStatementExpression extends ASTNode implements
IGNUASTCompoundStatementExpression {
public class CASTCompoundStatementExpression extends ASTNode implements IGNUASTCompoundStatementExpression {
private IASTCompoundStatement statement;
@ -74,7 +76,14 @@ public class CASTCompoundStatementExpression extends ASTNode implements
}
public IType getExpressionType() {
return CVisitor.getExpressionType(this);
}
IASTCompoundStatement compound = getCompoundStatement();
IASTStatement[] statements = compound.getStatements();
if (statements.length > 0) {
IASTStatement st = statements[statements.length - 1];
if (st instanceof IASTExpressionStatement)
return ((IASTExpressionStatement)st).getExpression().getExpressionType();
}
return null;
}
}

View file

@ -1,13 +1,14 @@
/*******************************************************************************
* Copyright (c) 2005, 2008 IBM Corporation and others.
* Copyright (c) 2005, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
* Yuan Zhang / Beth Tibbitts (IBM Research)
* John Camelon (IBM Rational Software) - Initial API and implementation
* Yuan Zhang / Beth Tibbitts (IBM Research)
* Markus Schorn (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c;
@ -15,12 +16,13 @@ import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTConditionalExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IPointerType;
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;
/**
* @author jcamelon
* Conditional expression in C
*/
public class CASTConditionalExpression extends ASTNode implements
IASTConditionalExpression, IASTAmbiguityParent {
@ -132,7 +134,14 @@ public class CASTConditionalExpression extends ASTNode implements
}
public IType getExpressionType() {
return CVisitor.getExpressionType(this);
}
IASTExpression positiveExpression = getPositiveResultExpression();
if (positiveExpression == null) {
positiveExpression= getLogicalConditionExpression();
}
IType t2 = positiveExpression.getExpressionType();
IType t3 = getNegativeResultExpression().getExpressionType();
if (t3 instanceof IPointerType || t2 == null)
return t3;
return t2;
}
}

View file

@ -1,14 +1,14 @@
/*******************************************************************************
* Copyright (c) 2005, 2008 IBM Corporation and others.
* Copyright (c) 2005, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
* Markus Schorn (Wind River Systems)
* Yuan Zhang / Beth Tibbitts (IBM Research)
* John Camelon (IBM Rational Software) - Initial API and implementation
* Markus Schorn (Wind River Systems)
* Yuan Zhang / Beth Tibbitts (IBM Research)
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c;
@ -22,7 +22,7 @@ import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent;
/**
* @author jcamelon
* Expression list in C
*/
public class CASTExpressionList extends ASTNode implements IASTExpressionList,
IASTAmbiguityParent {
@ -95,7 +95,11 @@ public class CASTExpressionList extends ASTNode implements IASTExpressionList,
}
public IType getExpressionType() {
return CVisitor.getExpressionType(this);
for (int i = expressions.length-1; i >= 0; i--) {
IASTExpression expr= expressions[i];
if (expr != null)
return expr.getExpressionType();
}
return null;
}
}

View file

@ -1,18 +1,20 @@
/*******************************************************************************
* Copyright (c) 2005, 2008 IBM Corporation and others.
* Copyright (c) 2005, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
* Yuan Zhang / Beth Tibbitts (IBM Research)
* Bryan Wilkinson (QNX)
* John Camelon (IBM Rational Software) - Initial API and implementation
* Yuan Zhang / Beth Tibbitts (IBM Research)
* Bryan Wilkinson (QNX)
* Markus Schorn (Wind River Systems)
*******************************************************************************/
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.IASTCompletionContext;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
@ -20,11 +22,12 @@ import org.eclipse.cdt.core.dom.ast.IASTName;
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.IVariable;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent;
/**
* @author jcamelon
* Field reference in C.
*/
public class CASTFieldReference extends ASTNode implements IASTFieldReference, IASTAmbiguityParent, IASTCompletionContext {
@ -130,7 +133,15 @@ public class CASTFieldReference extends ASTNode implements IASTFieldReference, I
}
public IType getExpressionType() {
return CVisitor.getExpressionType(this);
IBinding binding = getFieldName().resolveBinding();
if (binding instanceof IVariable) {
try {
return ((IVariable)binding).getType();
} catch (DOMException e) {
return e.getProblem();
}
}
return null;
}
public IBinding[] findBindings(IASTName n, boolean isPrefix) {

View file

@ -1,26 +1,30 @@
/*******************************************************************************
* Copyright (c) 2005, 2008 IBM Corporation and others.
* Copyright (c) 2005, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
* Yuan Zhang / Beth Tibbitts (IBM Research)
* John Camelon (IBM Rational Software) - Initial API and implementation
* Yuan Zhang / Beth Tibbitts (IBM Research)
* Markus Schorn (Wind River Systems)
*******************************************************************************/
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.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IFunctionType;
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;
import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer;
/**
* @author jcamelon
* Function call expression in C.
*/
public class CASTFunctionCallExpression extends ASTNode implements
IASTFunctionCallExpression, IASTAmbiguityParent {
@ -109,8 +113,16 @@ public class CASTFunctionCallExpression extends ASTNode implements
}
}
public IType getExpressionType() {
return CVisitor.getExpressionType(this);
}
public IType getExpressionType() {
IType type = getFunctionNameExpression().getExpressionType();
try {
while (type instanceof ITypeContainer)
type = ((ITypeContainer) type).getType();
if (type instanceof IFunctionType)
return ((IFunctionType) type).getReturnType();
} catch (DOMException e) {
return e.getProblem();
}
return null;
}
}

View file

@ -1,12 +1,12 @@
/*******************************************************************************
* Copyright (c) 2005, 2008 IBM Corporation and others.
* Copyright (c) 2005, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
* John Camelon (IBM Rational Software) - Initial API and implementation
* Yuan Zhang / Beth Tibbitts (IBM Research)
* Bryan Wilkinson (QNX)
* Anton Leherbauer (Wind River Systems)
@ -14,18 +14,21 @@
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.IASTCompletionContext;
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IEnumeration;
import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
/**
* @author jcamelon
* ID Expression in C.
*/
public class CASTIdExpression extends ASTNode implements IASTIdExpression, IASTCompletionContext {
@ -86,7 +89,18 @@ public class CASTIdExpression extends ASTNode implements IASTIdExpression, IASTC
}
public IType getExpressionType() {
return CVisitor.getExpressionType(this);
IBinding binding = getName().resolveBinding();
try {
if (binding instanceof IVariable) {
return ((IVariable)binding).getType();
}
if (binding instanceof IFunction) {
return ((IFunction)binding).getType();
}
} catch (DOMException e) {
return e.getProblem();
}
return null;
}
public IBinding[] findBindings(IASTName n, boolean isPrefix) {

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2005, 2008 IBM Corporation and others.
* Copyright (c) 2005, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@ -14,6 +14,7 @@ package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.IBasicType;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
@ -82,9 +83,21 @@ public class CASTLiteralExpression extends ASTNode implements IASTLiteralExpress
return true;
}
public IType getExpressionType() {
return CVisitor.getExpressionType(this);
}
public IType getExpressionType() {
switch (getKind()) {
case IASTLiteralExpression.lk_char_constant:
return new CBasicType(IBasicType.t_char, 0, this);
case IASTLiteralExpression.lk_float_constant:
return new CBasicType(IBasicType.t_float, 0, this);
case IASTLiteralExpression.lk_integer_constant:
return new CBasicType(IBasicType.t_int, 0, this);
case IASTLiteralExpression.lk_string_literal:
IType type = new CBasicType(IBasicType.t_char, 0, this);
type = new CQualifierType(type, true, false, false);
return new CPointerType(type, 0);
}
return null;
}
/**
* @deprecated, use {@link #setValue(char[])}, instead.

View file

@ -1,13 +1,14 @@
/*******************************************************************************
* Copyright (c) 2005, 2008 IBM Corporation and others.
* Copyright (c) 2005, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
* Yuan Zhang / Beth Tibbitts (IBM Research)
* John Camelon (IBM Rational Software) - Initial API and implementation
* Yuan Zhang / Beth Tibbitts (IBM Research)
* Markus Schorn (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c;
@ -18,7 +19,7 @@ import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
/**
* @author jcamelon
* Type-id or unary operation on a type-id.
*/
public class CASTTypeIdExpression extends ASTNode implements IASTTypeIdExpression {
@ -84,6 +85,9 @@ public class CASTTypeIdExpression extends ASTNode implements IASTTypeIdExpressio
}
public IType getExpressionType() {
return CVisitor.getExpressionType(this);
if (getOperator() == op_sizeof) {
return CVisitor.getSize_T(this);
}
return CVisitor.createType(typeId.getAbstractDeclarator());
}
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2005, 2008 IBM Corporation and others.
* Copyright (c) 2005, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@ -14,6 +14,7 @@ package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.c.ICASTTypeIdInitializerExpression;
import org.eclipse.cdt.internal.core.dom.parser.ASTTypeIdInitializerExpression;
@ -23,7 +24,7 @@ import org.eclipse.cdt.internal.core.dom.parser.ASTTypeIdInitializerExpression;
public class CASTTypeIdInitializerExpression extends ASTTypeIdInitializerExpression implements
ICASTTypeIdInitializerExpression {
public CASTTypeIdInitializerExpression() {
private CASTTypeIdInitializerExpression() {
super();
}
@ -31,10 +32,13 @@ public class CASTTypeIdInitializerExpression extends ASTTypeIdInitializerExpress
super(typeId, initializer);
}
@Override
public CASTTypeIdInitializerExpression copy() {
CASTTypeIdInitializerExpression copy = new CASTTypeIdInitializerExpression();
initializeCopy(copy);
return copy;
}
public IType getExpressionType() {
return CVisitor.createType(getTypeId().getAbstractDeclarator());
}
}

View file

@ -1,30 +1,33 @@
/*******************************************************************************
* Copyright (c) 2005, 2008 IBM Corporation and others.
* Copyright (c) 2005, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
* Yuan Zhang / Beth Tibbitts (IBM Research)
* John Camelon (IBM Rational Software) - Initial API and implementation
* Yuan Zhang / Beth Tibbitts (IBM Research)
* Markus Schorn (Wind River Systems)
*******************************************************************************/
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.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IArrayType;
import org.eclipse.cdt.core.dom.ast.IPointerType;
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;
import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer;
/**
* @author jcamelon
* Unary expression in C.
*/
public class CASTUnaryExpression extends ASTNode implements
IASTUnaryExpression, IASTAmbiguityParent {
public class CASTUnaryExpression extends ASTNode implements IASTUnaryExpression, IASTAmbiguityParent {
private int operator;
private IASTExpression operand;
@ -96,8 +99,19 @@ public class CASTUnaryExpression extends ASTNode implements
}
}
public IType getExpressionType() {
return CVisitor.getExpressionType(this);
}
public IType getExpressionType() {
IType type = getOperand().getExpressionType();
int op = getOperator();
try {
if (op == IASTUnaryExpression.op_star && (type instanceof IPointerType || type instanceof IArrayType)) {
return ((ITypeContainer) type).getType();
} else if (op == IASTUnaryExpression.op_amper) {
return new CPointerType(type, 0);
}
} catch (DOMException e) {
return e.getProblem();
}
return type;
}
}

View file

@ -20,12 +20,9 @@ import org.eclipse.cdt.core.dom.ast.ASTNodeProperty;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTArrayModifier;
import org.eclipse.cdt.core.dom.ast.IASTArraySubscriptExpression;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTCastExpression;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTConditionalExpression;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement;
@ -33,8 +30,6 @@ import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpressionList;
import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
import org.eclipse.cdt.core.dom.ast.IASTForStatement;
import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression;
@ -44,7 +39,6 @@ import org.eclipse.cdt.core.dom.ast.IASTGotoStatement;
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTLabelStatement;
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTNode;
@ -57,8 +51,6 @@ import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IArrayType;
import org.eclipse.cdt.core.dom.ast.IBasicType;
import org.eclipse.cdt.core.dom.ast.IBinding;
@ -70,7 +62,6 @@ import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.IFunctionType;
import org.eclipse.cdt.core.dom.ast.ILabel;
import org.eclipse.cdt.core.dom.ast.IParameter;
import org.eclipse.cdt.core.dom.ast.IPointerType;
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.IType;
@ -89,7 +80,6 @@ import org.eclipse.cdt.core.dom.ast.c.ICASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTTypedefNameSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICCompositeTypeScope;
import org.eclipse.cdt.core.dom.ast.c.ICFunctionScope;
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTCompoundStatementExpression;
import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.gnu.c.IGCCASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.index.IIndexBinding;
@ -607,12 +597,10 @@ public class CVisitor extends ASTQueries {
*/
private static Object findBinding(IASTFieldReference fieldReference, boolean prefix) {
IASTExpression fieldOwner = fieldReference.getFieldOwner();
IType type = null;
if (fieldOwner instanceof IASTArraySubscriptExpression) {
type = getExpressionType(((IASTArraySubscriptExpression) fieldOwner).getArrayExpression());
} else {
type = getExpressionType(fieldOwner);
}
if (fieldOwner == null)
return null;
IType type = fieldOwner.getExpressionType();
while (type != null && type instanceof ITypeContainer) {
try {
type = ((ITypeContainer)type).getType();
@ -646,185 +634,41 @@ public class CVisitor extends ASTQueries {
return null;
}
public static IType getExpressionType(IASTExpression expression) {
try{
if (expression instanceof IASTIdExpression) {
IBinding binding = ((IASTIdExpression)expression).getName().resolveBinding();
if (binding instanceof IVariable) {
return ((IVariable)binding).getType();
}
else if (binding instanceof IFunction) {
return ((IFunction)binding).getType();
}
} else if (expression instanceof IASTCastExpression) {
IASTTypeId id = ((IASTCastExpression)expression).getTypeId();
return createType(id.getAbstractDeclarator());
} else if (expression instanceof IASTFieldReference) {
IBinding binding = ((IASTFieldReference)expression).getFieldName().resolveBinding();
if (binding instanceof IVariable) {
return ((IVariable)binding).getType();
}
} else if (expression instanceof IASTFunctionCallExpression) {
IType type = getExpressionType(((IASTFunctionCallExpression)expression).getFunctionNameExpression());
while (type instanceof ITypeContainer)
type = ((ITypeContainer)type).getType();
if (type instanceof IFunctionType)
return ((IFunctionType)type).getReturnType();
} else if (expression instanceof IASTUnaryExpression) {
IType type = getExpressionType(((IASTUnaryExpression)expression).getOperand());
int op = ((IASTUnaryExpression)expression).getOperator();
if (op == IASTUnaryExpression.op_star && (type instanceof IPointerType || type instanceof IArrayType)) {
return ((ITypeContainer)type).getType();
} else if (op == IASTUnaryExpression.op_amper) {
return new CPointerType(type, 0);
}
return type;
} else if (expression instanceof IASTLiteralExpression) {
switch(((IASTLiteralExpression) expression).getKind()) {
case IASTLiteralExpression.lk_char_constant:
return new CBasicType(IBasicType.t_char, 0, expression);
case IASTLiteralExpression.lk_float_constant:
return new CBasicType(IBasicType.t_float, 0, expression);
case IASTLiteralExpression.lk_integer_constant:
return new CBasicType(IBasicType.t_int, 0, expression);
case IASTLiteralExpression.lk_string_literal:
IType type = new CBasicType(IBasicType.t_char, 0, expression);
type = new CQualifierType(type, true, false, false);
return new CPointerType(type, 0);
}
} else if (expression instanceof IASTBinaryExpression) {
IASTBinaryExpression binary = (IASTBinaryExpression) expression;
int op = binary.getOperator();
switch(op) {
case IASTBinaryExpression.op_lessEqual:
case IASTBinaryExpression.op_lessThan:
case IASTBinaryExpression.op_greaterEqual:
case IASTBinaryExpression.op_greaterThan:
case IASTBinaryExpression.op_logicalAnd:
case IASTBinaryExpression.op_logicalOr:
case IASTBinaryExpression.op_equals:
case IASTBinaryExpression.op_notequals:
CBasicType basicType = new CBasicType(IBasicType.t_int, 0);
basicType.setValue(expression);
return basicType;
case IASTBinaryExpression.op_plus:
IType t2 = getExpressionType(binary.getOperand2());
if (unwrapTypedefs(t2) instanceof IPointerType) {
return t2;
}
break;
case IASTBinaryExpression.op_minus:
t2= getExpressionType(binary.getOperand2());
if (unwrapTypedefs(t2) instanceof IPointerType) {
IType t1 = getExpressionType(binary.getOperand1());
if (unwrapTypedefs(t1) instanceof IPointerType) {
IScope scope = getContainingScope(expression);
try {
IBinding[] bs = scope.find(PTRDIFF_T);
if (bs.length > 0) {
for (IBinding b : bs) {
if (b instanceof IType) {
if (b instanceof ICInternalBinding == false ||
CVisitor.declaredBefore(((ICInternalBinding) b).getPhysicalNode(), binary)) {
return (IType) b;
}
}
}
}
} catch (DOMException e) {
}
basicType = new CBasicType(IBasicType.t_int, CBasicType.IS_UNSIGNED | CBasicType.IS_LONG);
basicType.setValue(expression);
return basicType;
}
return t1;
}
break;
}
return getExpressionType(binary.getOperand1());
} else if (expression instanceof IASTUnaryExpression) {
int op = ((IASTUnaryExpression)expression).getOperator();
if (op == IASTUnaryExpression.op_sizeof) {
IScope scope = getContainingScope(expression);
if (scope != null) {
IBinding[] bs = scope.find(SIZE_T);
if (bs.length > 0 && bs[0] instanceof IType) {
return (IType) bs[0];
static IType getPtrDiffType(IASTBinaryExpression expr) {
IScope scope = getContainingScope(expr);
try {
IBinding[] bs = scope.find(PTRDIFF_T);
if (bs.length > 0) {
for (IBinding b : bs) {
if (b instanceof IType) {
if (b instanceof ICInternalBinding == false ||
CVisitor.declaredBefore(((ICInternalBinding) b).getPhysicalNode(), expr)) {
return (IType) b;
}
}
return new CBasicType(IBasicType.t_int, CBasicType.IS_LONG | CBasicType.IS_UNSIGNED, expression);
}
IType type = getExpressionType(((IASTUnaryExpression)expression).getOperand());
if (op == IASTUnaryExpression.op_star && (type instanceof IPointerType || type instanceof IArrayType)) {
try {
return ((ITypeContainer)type).getType();
} catch (DOMException e) {
return e.getProblem();
}
} else if (op == IASTUnaryExpression.op_amper) {
return new CPointerType(type, 0);
} else if (type instanceof CBasicType) {
((CBasicType)type).setValue(expression);
}
return type;
} else if (expression instanceof IASTFieldReference) {
IBinding binding = (IBinding) findBinding((IASTFieldReference) expression, false);
if (binding instanceof IVariable)
return ((IVariable)binding).getType();
else if (binding instanceof IFunction)
return ((IFunction)binding).getType();
else if (binding instanceof IEnumerator)
return ((IEnumerator)binding).getType();
} else if (expression instanceof IASTExpressionList) {
IASTExpression[] exps = ((IASTExpressionList)expression).getExpressions();
return getExpressionType(exps[exps.length - 1]);
} else if (expression instanceof IASTTypeIdExpression) {
IASTTypeIdExpression typeidExp = (IASTTypeIdExpression) expression;
if (typeidExp.getOperator() == IASTTypeIdExpression.op_sizeof) {
IScope scope = getContainingScope(typeidExp);
IBinding[] bs = scope.find(SIZE_T);
if (bs.length > 0 && bs[0] instanceof IType) {
return (IType) bs[0];
}
return new CBasicType(IBasicType.t_int, CBasicType.IS_LONG | CBasicType.IS_UNSIGNED);
}
return createType(typeidExp.getTypeId().getAbstractDeclarator());
} else if (expression instanceof IASTArraySubscriptExpression) {
IType t = getExpressionType(((IASTArraySubscriptExpression) expression).getArrayExpression());
if (t instanceof IPointerType)
return ((IPointerType)t).getType();
else if (t instanceof IArrayType)
return ((IArrayType)t).getType();
} else if (expression instanceof IGNUASTCompoundStatementExpression) {
IASTCompoundStatement compound = ((IGNUASTCompoundStatementExpression)expression).getCompoundStatement();
IASTStatement[] statements = compound.getStatements();
if (statements.length > 0) {
IASTStatement st = statements[statements.length - 1];
if (st instanceof IASTExpressionStatement)
return getExpressionType(((IASTExpressionStatement)st).getExpression());
}
} else if (expression instanceof IASTConditionalExpression) {
final IASTConditionalExpression conditional = (IASTConditionalExpression) expression;
IASTExpression positiveExpression = conditional.getPositiveResultExpression();
if (positiveExpression == null) {
positiveExpression= conditional.getLogicalConditionExpression();
}
IType t2 = getExpressionType(positiveExpression);
IType t3 = getExpressionType(conditional.getNegativeResultExpression());
if (t3 instanceof IPointerType || t2 == null)
return t3;
return t2;
}
} catch(DOMException e) {
return e.getProblem();
}
return null;
} catch (DOMException e) {
}
CBasicType basicType = new CBasicType(IBasicType.t_int, CBasicType.IS_UNSIGNED | CBasicType.IS_LONG);
basicType.setValue(expr);
return basicType;
}
static IType getSize_T(IASTExpression expr) {
IScope scope = getContainingScope(expr);
try {
IBinding[] bs = scope.find(SIZE_T);
if (bs.length > 0 && bs[0] instanceof IType) {
return (IType) bs[0];
}
} catch (DOMException e) {
}
return new CBasicType(IBasicType.t_int, CBasicType.IS_LONG | CBasicType.IS_UNSIGNED);
}
private static IType unwrapTypedefs(IType type) throws DOMException {
static IType unwrapTypedefs(IType type) throws DOMException {
while (type instanceof ITypedef) {
type= ((ITypedef) type).getType();
}
@ -1483,7 +1327,7 @@ public class CVisitor extends ASTQueries {
if (declSpec instanceof IGCCASTSimpleDeclSpecifier) {
IASTExpression exp = ((IGCCASTSimpleDeclSpecifier)declSpec).getTypeofExpression();
if (exp != null)
return getExpressionType(exp);
return exp.getExpressionType();
return new CBasicType((ICASTSimpleDeclSpecifier) declSpec);
} else if (declSpec instanceof ICASTSimpleDeclSpecifier) {
return new CBasicType((ICASTSimpleDeclSpecifier)declSpec);

View file

@ -0,0 +1,42 @@
/*******************************************************************************
* Copyright (c) 2009 Wind River Systems, Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Markus Schorn - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IASTTypeIdInitializerExpression;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.internal.core.dom.parser.ASTTypeIdInitializerExpression;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
/**
* C++ variant of type id initializer expression.
*/
public class CPPASTTypeIdInitializerExpression extends ASTTypeIdInitializerExpression {
private CPPASTTypeIdInitializerExpression() {
}
public CPPASTTypeIdInitializerExpression(IASTTypeId typeId, IASTInitializer initializer) {
super(typeId, initializer);
}
public IASTTypeIdInitializerExpression copy() {
CPPASTTypeIdInitializerExpression expr= new CPPASTTypeIdInitializerExpression();
initializeCopy(expr);
return expr;
}
public IType getExpressionType() {
final IASTTypeId typeId = getTypeId();
return CPPVisitor.createType(typeId.getDeclSpecifier(), typeId.getAbstractDeclarator());
}
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2006, 2008 IBM Corporation and others.
* Copyright (c) 2006, 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@ -103,7 +103,6 @@ import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTExplicitTemplateInstantiation
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTPointer;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTPointerToMember;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTSimpleDeclSpecifier;
import org.eclipse.cdt.internal.core.dom.parser.ASTTypeIdInitializerExpression;
/**
@ -195,7 +194,7 @@ public class CPPNodeFactory implements ICPPNodeFactory {
}
public IASTTypeIdInitializerExpression newTypeIdInitializerExpression(IASTTypeId typeId, IASTInitializer initializer) {
return new ASTTypeIdInitializerExpression(typeId, initializer);
return new CPPASTTypeIdInitializerExpression(typeId, initializer);
}
public ICPPASTFieldReference newFieldReference(IASTName name, IASTExpression owner) {