1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Start of types for C. By Devin Steffler

This commit is contained in:
Andrew Niefer 2004-12-13 18:17:23 +00:00
parent 3abb569374
commit b9d472dbe3
8 changed files with 437 additions and 17 deletions

View file

@ -210,6 +210,9 @@ public class AST2BaseTest extends TestCase {
protected void assertInstances( CNameCollector collector, IBinding binding, int num ) throws Exception {
int count = 0;
if (binding == null) assertTrue(false);
for( int i = 0; i < collector.size(); i++ )
if( collector.getName( i ).resolveBinding() == binding )
count++;

View file

@ -340,7 +340,9 @@ public class AST2Tests extends AST2BaseTest {
ICompositeType str1 = (ICompositeType) nameA1.resolveBinding();
ICompositeType str2 = (ICompositeType) nameA2.resolveBinding();
IVariable var = (IVariable) namea.resolveBinding();
ICompositeType str3 = (ICompositeType) var.getType();
IType str3pointer = var.getType();
assertTrue(str3pointer instanceof IPointerType);
ICompositeType str3 = (ICompositeType) ((IPointerType)str3pointer).getType();
ICompositeType str4 = (ICompositeType) nameA3.resolveBinding();
assertNotNull( str1 );
assertNotNull( str2 );
@ -383,7 +385,8 @@ public class AST2Tests extends AST2BaseTest {
ICompositeType str1 = (ICompositeType) nameA1.resolveBinding();
ICompositeType str2 = (ICompositeType) nameA2.resolveBinding();
IVariable var = (IVariable) namea.resolveBinding();
ICompositeType str3 = (ICompositeType) var.getType();
IPointerType str3pointer = (IPointerType) var.getType();
ICompositeType str3 = (ICompositeType) str3pointer.getType();
assertNotNull( str1 );
assertSame( str1, str2 );
assertSame( str2, str3 );
@ -440,7 +443,8 @@ public class AST2Tests extends AST2BaseTest {
//bindings
IVariable var_a1 = (IVariable) name_aref.resolveBinding();
IVariable var_i1 = (IVariable) name_iref.resolveBinding();
ICompositeType structA_1 = (ICompositeType) var_a1.getType();
IPointerType structA_1pointer = (IPointerType) var_a1.getType();
ICompositeType structA_1 = (ICompositeType) structA_1pointer.getType();
ICompositeType structA_2 = (ICompositeType) name_A1.resolveBinding();
ICompositeType structA_3 = (ICompositeType) name_A2.resolveBinding();
ICompositeType structA_4 = (ICompositeType) name_Adef.resolveBinding();
@ -876,11 +880,14 @@ public class AST2Tests extends AST2BaseTest {
}
public void _testBasicTypes() throws Exception {
public void testBasicTypes() throws Exception {
StringBuffer buffer = new StringBuffer();
buffer.append( "int a; \n" ); //$NON-NLS-1$
buffer.append( "char * b; \n" ); //$NON-NLS-1$
buffer.append( "const int c; \n" ); //$NON-NLS-1$
buffer.append( "const char * const d; \n" ); //$NON-NLS-1$
buffer.append( "const char ** e; \n" ); //$NON-NLS-1$
buffer.append( "const char ***** f; \n" ); //$NON-NLS-1$
IASTTranslationUnit tu = parse( buffer.toString(), ParserLanguage.C );
@ -890,26 +897,77 @@ public class AST2Tests extends AST2BaseTest {
IVariable b = (IVariable) decl.getDeclarators()[0].getName().resolveBinding();
decl = (IASTSimpleDeclaration) tu.getDeclarations()[2];
IVariable c = (IVariable) decl.getDeclarators()[0].getName().resolveBinding();
decl = (IASTSimpleDeclaration) tu.getDeclarations()[3];
IVariable d = (IVariable) decl.getDeclarators()[0].getName().resolveBinding();
decl = (IASTSimpleDeclaration) tu.getDeclarations()[4];
IVariable e = (IVariable) decl.getDeclarators()[0].getName().resolveBinding();
decl = (IASTSimpleDeclaration) tu.getDeclarations()[5];
IVariable f = (IVariable) decl.getDeclarators()[0].getName().resolveBinding();
IType t_a_1 = a.getType();
assertTrue( t_a_1 instanceof IBasicType );
assertFalse( ((IBasicType)t_a_1).isLong() );
assertFalse( ((IBasicType)t_a_1).isShort() );
assertFalse( ((IBasicType)t_a_1).isSigned() );
assertFalse( ((IBasicType)t_a_1).isUnsigned() );
assertEquals( ((IBasicType)t_a_1).getType(), IBasicType.t_int );
IType t_b_1 = b.getType();
assertTrue( t_b_1 instanceof IPointerType );
IType t_b_2 = ((IPointerType) t_b_1).getType();
assertTrue( t_b_2 instanceof IBasicType );
assertEquals( ((IBasicType)t_b_2).getType(), IBasicType.t_char );
IType t_c_1 = c.getType();
assertTrue( t_c_1 instanceof IQualifierType );
assertTrue( ((IQualifierType)t_c_1).isConst());
assertTrue( ((IQualifierType)t_c_1).isConst() );
IType t_c_2 = ((IQualifierType)t_c_1).getType();
assertTrue( t_c_2 instanceof IBasicType );
assertEquals( ((IBasicType)t_c_2).getType(), IBasicType.t_int );
IType t_d_1 = d.getType();
assertTrue( t_d_1 instanceof IPointerType );
assertTrue( ((IPointerType)t_d_1).isConst() );
IType t_d_2 = ((IPointerType)t_d_1).getType();
assertTrue( t_d_2 instanceof IQualifierType );
assertTrue( ((IQualifierType)t_d_2).isConst() );
IType t_d_3 = ((IQualifierType)t_d_2).getType();
assertTrue( t_d_3 instanceof IBasicType );
assertEquals( ((IBasicType)t_d_3).getType(), IBasicType.t_char );
IType t_e_1 = e.getType();
assertTrue( t_e_1 instanceof IPointerType );
IType t_e_2 = ((IPointerType)t_e_1).getType();
assertTrue( t_e_2 instanceof IPointerType );
IType t_e_3 = ((IPointerType)t_e_2).getType();
assertTrue( t_e_3 instanceof IQualifierType );
assertTrue( ((IQualifierType)t_e_3).isConst() );
IType t_e_4 = ((IQualifierType)t_e_3).getType();
assertTrue( t_e_4 instanceof IBasicType );
assertEquals( ((IBasicType)t_e_4).getType(), IBasicType.t_char );
IType t_f_1 = f.getType();
assertTrue( t_f_1 instanceof IPointerType );
IType t_f_2 = ((IPointerType)t_f_1).getType();
assertTrue( t_f_2 instanceof IPointerType );
IType t_f_3 = ((IPointerType)t_f_2).getType();
assertTrue( t_f_2 instanceof IPointerType );
IType t_f_4 = ((IPointerType)t_f_3).getType();
assertTrue( t_f_2 instanceof IPointerType );
IType t_f_5 = ((IPointerType)t_f_4).getType();
assertTrue( t_f_2 instanceof IPointerType );
IType t_f_6 = ((IPointerType)t_f_5).getType();
assertTrue( t_f_6 instanceof IQualifierType );
assertTrue( ((IQualifierType)t_f_6).isConst() );
IType t_f_7 = ((IQualifierType)t_f_6).getType();
assertTrue( t_f_7 instanceof IBasicType );
assertEquals( ((IBasicType)t_f_7).getType(), IBasicType.t_char );
}
public void _testCompositeTypes() throws Exception{
public void testCompositeTypes() throws Exception{
StringBuffer buffer = new StringBuffer();
buffer.append( "struct A {} a1; \n"); //$NON-NLS-1$
buffer.append( "typedef A * AP; \n"); //$NON-NLS-1$
buffer.append( "typedef struct A * AP; \n"); //$NON-NLS-1$
buffer.append( "struct A * const a2; \n"); //$NON-NLS-1$
buffer.append( "AP a3; \n"); //$NON-NLS-1$

View file

@ -0,0 +1,86 @@
/**********************************************************************
* Copyright (c) 2002-2004 IBM Canada and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
**********************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.c.ICASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICBasicType;
/**
* @author dsteffle
*/
public class CBasicType implements ICBasicType {
private ICASTSimpleDeclSpecifier sds = null;
/**
* keep a reference to the declaration specifier so that duplicate information isn't generated.
*
* @param sds the simple declaration specifier
*/
public CBasicType(ICASTSimpleDeclSpecifier sds) {
this.sds = sds;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IBasicType#getType()
*/
public int getType() {
return sds.getType();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IBasicType#isSigned()
*/
public boolean isSigned() {
return sds.isSigned();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IBasicType#isUnsigned()
*/
public boolean isUnsigned() {
return sds.isUnsigned();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IBasicType#isShort()
*/
public boolean isShort() {
return sds.isShort();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IBasicType#isLong()
*/
public boolean isLong() {
return sds.isLong();
}
public boolean isLongLong() {
return sds.isLongLong();
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
if (!(obj instanceof CBasicType)) return false;
CBasicType cObj = (CBasicType)obj;
return (cObj.getType() == this.getType()
&& cObj.isLong() == this.isLong()
&& cObj.isShort() == this.isShort()
&& cObj.isSigned() == this.isSigned()
&& cObj.isUnsigned() == this.isUnsigned()
&& cObj.isLongLong() == this.isLongLong());
}
}

View file

@ -0,0 +1,66 @@
/**********************************************************************
* Copyright (c) 2002-2004 IBM Canada and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
**********************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.c.ICASTPointer;
import org.eclipse.cdt.core.dom.ast.c.ICPointerType;
/**
* @author dsteffle
*/
public class CPointerType implements ICPointerType {
IType nextType = null;
ICASTPointer pointer = null;
public CPointerType() {}
public CPointerType(IType next) {
this.nextType = next;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.c.ICPointerType#isRestrict()
*/
public boolean isRestrict() {
return pointer.isRestrict();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IPointerType#getType()
*/
public IType getType() {
return nextType;
}
public void setType(IType type) {
nextType = type;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IPointerType#isConst()
*/
public boolean isConst() {
return pointer.isConst();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IPointerType#isVolatile()
*/
public boolean isVolatile() {
return pointer.isVolatile();
}
public void setPointer(ICASTPointer pointer) {
this.pointer = pointer;
}
}

View file

@ -0,0 +1,65 @@
/**********************************************************************
* Copyright (c) 2002-2004 IBM Canada and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* IBM Rational Software - Initial API and implementation
**********************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.c.ICASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICQualifierType;
/**
* @author dsteffle
*/
public class CQualifierType implements ICQualifierType {
ICASTDeclSpecifier declSpec = null;
/**
* CQualifierType has an IBasicType to keep track of the basic type information.
*
* @param type the CQualifierType's IBasicType
*/
public CQualifierType(ICASTDeclSpecifier declSpec) {
this.declSpec = declSpec;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IQualifierType#isConst()
*/
public boolean isConst() {
if (declSpec == null) return false;
return declSpec.isConst();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IQualifierType#isVolatile()
*/
public boolean isVolatile() {
if (declSpec == null) return false;
return declSpec.isVolatile();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.c.ICQualifierType#isRestrict()
*/
public boolean isRestrict() {
if (declSpec == null) return false;
return declSpec.isRestrict();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IQualifierType#getType()
*/
public IType getType() {
return new CBasicType((ICASTSimpleDeclSpecifier)declSpec);
}
}

View file

@ -11,16 +11,22 @@
**********************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.c.ICASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTPointer;
import org.eclipse.cdt.core.dom.ast.c.ICASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTTypedefNameSpecifier;
/**
* Created on Nov 8, 2004
@ -44,11 +50,76 @@ public class CTypeDef implements ITypedef {
IASTSimpleDeclaration declaration = (IASTSimpleDeclaration) declarator.getParent();
IASTDeclSpecifier declSpec = declaration.getDeclSpecifier();
if( declSpec instanceof ICASTCompositeTypeSpecifier ){
ICASTCompositeTypeSpecifier compTypeSpec = (ICASTCompositeTypeSpecifier) declSpec;
return (IType) compTypeSpec.getName().resolveBinding();
}
if( declSpec instanceof ICASTTypedefNameSpecifier ){
IType lastType = null;
ICASTTypedefNameSpecifier nameSpec = (ICASTTypedefNameSpecifier) declSpec;
lastType = (IType) nameSpec.getName().resolveBinding();
IType pointerChain = setupPointerChain(declarator.getPointerOperators(), lastType);
if (pointerChain != null) return pointerChain;
return lastType;
} else if( declSpec instanceof IASTElaboratedTypeSpecifier ){
IType lastType = null;
IASTElaboratedTypeSpecifier elabTypeSpec = (IASTElaboratedTypeSpecifier) declSpec;
lastType = (IType) elabTypeSpec.getName().resolveBinding();
IType pointerChain = setupPointerChain(declarator.getPointerOperators(), lastType);
if (pointerChain != null) return pointerChain;
return lastType;
} else if( declSpec instanceof IASTCompositeTypeSpecifier ){
IType lastType = null;
IASTCompositeTypeSpecifier compTypeSpec = (IASTCompositeTypeSpecifier) declSpec;
lastType = (IType) compTypeSpec.getName().resolveBinding();
IType pointerChain = setupPointerChain(declarator.getPointerOperators(), lastType);
if (pointerChain != null) return pointerChain;
return lastType;
} else if (declSpec instanceof ICASTSimpleDeclSpecifier) {
IType lastType = null;
if (declSpec.isConst() || declSpec.isVolatile() || ((ICASTSimpleDeclSpecifier)declSpec).isRestrict())
lastType = new CQualifierType((ICASTDeclSpecifier)declSpec);
else
lastType = new CBasicType((ICASTSimpleDeclSpecifier)declSpec);
IType pointerChain = setupPointerChain(declarator.getPointerOperators(), lastType);
if (pointerChain != null) return pointerChain;
return lastType;
}
return null;
}
private IType setupPointerChain(IASTPointerOperator[] ptrs, IType lastType) {
CPointerType pointerType = null;
if ( ptrs != null && ptrs.length > 0 ) {
pointerType = new CPointerType();
if (ptrs.length == 1) {
pointerType.setType(lastType);
pointerType.setPointer((ICASTPointer)ptrs[0]);
} else {
CPointerType tempType = new CPointerType();
pointerType.setType(tempType);
pointerType.setPointer((ICASTPointer)ptrs[0]);
for (int i=1; i<ptrs.length - 1; i++) {
tempType.setType(new CPointerType());
tempType.setPointer((ICASTPointer)ptrs[i]);
tempType = (CPointerType)tempType.getType();
}
tempType.setType(lastType);
}
return pointerType;
}
return null;
}

View file

@ -18,11 +18,16 @@ import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.dom.ast.c.ICASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTPointer;
import org.eclipse.cdt.core.dom.ast.c.ICASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTTypedefNameSpecifier;
import org.eclipse.cdt.internal.core.dom.parser.c.CBasicType;
/**
* Created on Nov 5, 2004
@ -66,17 +71,77 @@ public class CVariable implements IVariable {
IASTSimpleDeclaration declaration = (IASTSimpleDeclaration) declarator.getParent();
IASTDeclSpecifier declSpec = declaration.getDeclSpecifier();
if( declSpec instanceof ICASTTypedefNameSpecifier ){
IType lastType = null;
ICASTTypedefNameSpecifier nameSpec = (ICASTTypedefNameSpecifier) declSpec;
return (IType) nameSpec.getName().resolveBinding();
lastType = (IType) nameSpec.getName().resolveBinding();
IType pointerChain = setupPointerChain(declarator.getPointerOperators(), lastType);
if (pointerChain != null) return pointerChain;
return lastType;
} else if( declSpec instanceof IASTElaboratedTypeSpecifier ){
IType lastType = null;
IASTElaboratedTypeSpecifier elabTypeSpec = (IASTElaboratedTypeSpecifier) declSpec;
return (IType) elabTypeSpec.getName().resolveBinding();
lastType = (IType) elabTypeSpec.getName().resolveBinding();
IType pointerChain = setupPointerChain(declarator.getPointerOperators(), lastType);
if (pointerChain != null) return pointerChain;
return lastType;
} else if( declSpec instanceof IASTCompositeTypeSpecifier ){
IType lastType = null;
IASTCompositeTypeSpecifier compTypeSpec = (IASTCompositeTypeSpecifier) declSpec;
return (IType) compTypeSpec.getName().resolveBinding();
lastType = (IType) compTypeSpec.getName().resolveBinding();
IType pointerChain = setupPointerChain(declarator.getPointerOperators(), lastType);
if (pointerChain != null) return pointerChain;
return lastType;
} else if (declSpec instanceof ICASTSimpleDeclSpecifier) {
IType lastType = null;
if (declSpec.isConst() || declSpec.isVolatile() || ((ICASTSimpleDeclSpecifier)declSpec).isRestrict())
lastType = new CQualifierType((ICASTDeclSpecifier)declSpec);
else
lastType = new CBasicType((ICASTSimpleDeclSpecifier)declSpec);
IType pointerChain = setupPointerChain(declarator.getPointerOperators(), lastType);
if (pointerChain != null) return pointerChain;
return lastType;
}
return null;
}
private IType setupPointerChain(IASTPointerOperator[] ptrs, IType lastType) {
CPointerType pointerType = null;
if ( ptrs != null && ptrs.length > 0 ) {
pointerType = new CPointerType();
if (ptrs.length == 1) {
pointerType.setType(lastType);
pointerType.setPointer((ICASTPointer)ptrs[0]);
} else {
CPointerType tempType = new CPointerType();
pointerType.setType(tempType);
pointerType.setPointer((ICASTPointer)ptrs[0]);
for (int i=1; i<ptrs.length - 1; i++) {
tempType.setType(new CPointerType());
tempType.setPointer((ICASTPointer)ptrs[i]);
tempType = (CPointerType)tempType.getType();
}
tempType.setType(lastType);
}
return pointerType;
}
return null;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IBinding#getName()

View file

@ -70,6 +70,7 @@ import org.eclipse.cdt.core.dom.ast.c.ICASTEnumerationSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTTypeIdInitializerExpression;
import org.eclipse.cdt.core.dom.ast.c.ICASTTypedefNameSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICFunctionScope;
import org.eclipse.cdt.core.dom.ast.c.ICPointerType;
import org.eclipse.cdt.core.dom.ast.c.ICScope;
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTCompoundStatementExpression;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
@ -209,8 +210,13 @@ public class CVisitor {
} else {
type = getExpressionType( fieldOwner );
}
while( type != null && type instanceof ITypedef )
type = ((ITypedef)type).getType();
while( type != null && (type instanceof ITypedef || type instanceof ICPointerType)) {
if (type instanceof ITypedef) {
type = ((ITypedef)type).getType();
} else if (type instanceof ICPointerType) {
type = ((ICPointerType)type).getType();
}
}
if( type != null && type instanceof ICompositeType ){
return ((ICompositeType) type).findField( fieldReference.getFieldName().toString() );