From b9d472dbe34108b633e1ad489a88abbf96da5733 Mon Sep 17 00:00:00 2001 From: Andrew Niefer Date: Mon, 13 Dec 2004 18:17:23 +0000 Subject: [PATCH] Start of types for C. By Devin Steffler --- .../core/parser/tests/ast2/AST2BaseTest.java | 3 + .../cdt/core/parser/tests/ast2/AST2Tests.java | 72 ++++++++++++++-- .../core/dom/parser/c/CBasicType.java | 86 +++++++++++++++++++ .../core/dom/parser/c/CPointerType.java | 66 ++++++++++++++ .../core/dom/parser/c/CQualifierType.java | 65 ++++++++++++++ .../internal/core/dom/parser/c/CTypeDef.java | 81 +++++++++++++++-- .../internal/core/dom/parser/c/CVariable.java | 71 ++++++++++++++- .../internal/core/dom/parser/c/CVisitor.java | 10 ++- 8 files changed, 437 insertions(+), 17 deletions(-) create mode 100644 core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CBasicType.java create mode 100644 core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CPointerType.java create mode 100644 core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CQualifierType.java diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2BaseTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2BaseTest.java index 4142d7495cd..9ac627c41b1 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2BaseTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2BaseTest.java @@ -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++; 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 d4728444445..c5a176dee0f 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 @@ -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$ diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CBasicType.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CBasicType.java new file mode 100644 index 00000000000..4edfd9fa04b --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CBasicType.java @@ -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()); + } +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CPointerType.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CPointerType.java new file mode 100644 index 00000000000..7bb6ac59a09 --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CPointerType.java @@ -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; + } +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CQualifierType.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CQualifierType.java new file mode 100644 index 00000000000..98e7a21a4b9 --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CQualifierType.java @@ -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); + } +} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CTypeDef.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CTypeDef.java index 99dd443a72b..78cfceda7bf 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CTypeDef.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CTypeDef.java @@ -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 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