[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[cdt-patch] ASTTypeUtil and ASTSignatureUtil
|
I've added ASTTypeUtil.java and ASTSignatureUtil.java.
This way it is more clear what the similarly named functions are
to be used for. Having separate Util classes also makes it easier
to read/develop.
ASTTypeUtil.java:
- for checking type equality
- to be used when comparing types, the
IType interfaces are used to properly convert types
ex: "int d(int a[]);"
has a type "int (int *)" after type conversion (see C99: 6.7.5.3-7)
ASTSignatureUtil.java :
- for checking signature equality
- to be used when comparing signatures
ex: "int d(int a[]);" has
a signature "int (int [])"
This patch also includes some simple
tests in AST2UtilTests.java. I am now working on testing old tests
to make sure that the new Util classes give the same result as the old
ASTUtil.java (for all previously tested scenarios that I can find). I
hope to have a patch with these new tests by the end of today.
Note: The functionality of the
new Util classes can be easily tested via the DOM AST.
Devin Steffler
IBM's Eclipse CDT
Ottawa (Palladium), Ontario, Canada
Index: parser/org/eclipse/cdt/core/dom/ast/ASTUtil.java
===================================================================
RCS file: parser/org/eclipse/cdt/core/dom/ast/ASTUtil.java
diff -N parser/org/eclipse/cdt/core/dom/ast/ASTUtil.java
--- parser/org/eclipse/cdt/core/dom/ast/ASTUtil.java 9 Mar 2005 19:50:55 -0000 1.1
+++ /dev/null 1 Jan 1970 00:00:00 -0000
@@ -1,342 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2005 Rational Software Corp. 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:
- * Rational Software - initial implementation
- ******************************************************************************/
-
-package org.eclipse.cdt.core.dom.ast;
-
-import org.eclipse.cdt.core.dom.ast.c.ICArrayType;
-import org.eclipse.cdt.core.dom.ast.c.ICBasicType;
-import org.eclipse.cdt.core.dom.ast.c.ICPointerType;
-import org.eclipse.cdt.core.dom.ast.c.ICQualifierType;
-import org.eclipse.cdt.core.dom.ast.cpp.ICPPBasicType;
-import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
-import org.eclipse.cdt.core.dom.ast.cpp.ICPPReferenceType;
-import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateTypeParameter;
-import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPBasicType;
-import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPPointerType;
-import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPQualifierType;
-import org.eclipse.cdt.core.parser.GCCKeywords;
-import org.eclipse.cdt.core.parser.Keywords;
-import org.eclipse.cdt.core.parser.util.ArrayUtil;
-import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer;
-import org.eclipse.cdt.internal.core.dom.parser.c.CASTTypeId;
-import org.eclipse.cdt.internal.core.dom.parser.c.CExternalFunction;
-import org.eclipse.cdt.internal.core.dom.parser.c.CExternalVariable;
-import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor;
-import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTypeId;
-import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor;
-
-/**
- * This is a utility class to help convert AST elements to Strings.
- */
-
-public class ASTUtil {
-
- private static final String COMMA_SPACE = ", "; //$NON-NLS-1$
- private static final String EMPTY_STRING = ""; //$NON-NLS-1$
- private static final String SPACE = " "; //$NON-NLS-1$
- private static final String[] EMPTY_STRING_ARRAY = new String[0];
- private static final int DEAULT_ITYPE_SIZE = 2;
-
- public static String getParameterTypeString(IFunctionType type) {
- StringBuffer result = new StringBuffer();
- String[] parms = getParameterTypeStringArray(type);
-
- result.append(Keywords.cpLPAREN);
- for(int i=0; i<parms.length; i++) {
- if (parms[i] != null) {
- result.append(parms[i]);
- if (i<parms.length-1) result.append(COMMA_SPACE);
- }
- }
- result.append(Keywords.cpRPAREN);
- return result.toString();
- }
-
- public static String[] getParameterTypeStringArray(IFunctionType type) {
- IType[] parms = null;
- try {
- parms = type.getParameterTypes();
- } catch (DOMException e) { return EMPTY_STRING_ARRAY; }
-
- String[] result = new String[parms.length];
-
- for(int i=0; i<parms.length; i++) {
- if (parms[i] != null) {
- result[i] = getType(parms[i]);
- }
- }
-
- return result;
- }
-
- private static String getTypeString(IType type) {
- StringBuffer result = new StringBuffer();
- boolean needSpace = false;
-
- if (type instanceof IArrayType) {
- result.append(Keywords.cpLBRACKET);
- if (type instanceof ICArrayType) {
- try {
- if (((ICArrayType)type).isConst()) { result.append(Keywords.CONST); needSpace=true; }
- if (((ICArrayType)type).isRestrict()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.RESTRICT); needSpace=true; }
- if (((ICArrayType)type).isStatic()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.STATIC); needSpace=true; }
- if (((ICArrayType)type).isVolatile()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.VOLATILE); }
- } catch (DOMException e) {}
- }
- result.append(Keywords.cpRBRACKET);
- } else if (type instanceof IBasicType) {
- try {
- if (((IBasicType)type).isSigned()) { result.append(Keywords.SIGNED); needSpace = true; }
- else if (((IBasicType)type).isUnsigned()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.UNSIGNED); needSpace=true; }
- if (((IBasicType)type).isLong()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.LONG); needSpace = true; }
- else if (((IBasicType)type).isShort()) { if (needSpace) { result.append(SPACE); needSpace=false; }result.append(Keywords.SHORT); needSpace = true; }
- } catch (DOMException e) {}
-
- if (type instanceof IGPPBasicType) {
- try {
- if (((IGPPBasicType)type).isLongLong()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.LONG_LONG); needSpace=true; }
-
- switch (((IGPPBasicType)type).getType()) {
- case IGPPBasicType.t_Complex:
- result.append(Keywords.c_COMPLEX);
- break;
- case IGPPBasicType.t_Imaginary:
- result.append(Keywords.c_IMAGINARY);
- break;
- case IGPPBasicType.t_typeof:
- result.append(GCCKeywords.TYPEOF);
- break;
- }
- } catch (DOMException e) {}
- } else if (type instanceof ICPPBasicType) {
- try {
- switch (((ICPPBasicType)type).getType()) {
- case ICPPBasicType.t_bool:
- result.append(Keywords.BOOL);
- break;
- case ICPPBasicType.t_wchar_t:
- result.append(Keywords.WCHAR_T);
- break;
- }
- } catch (DOMException e) {}
- } else if (type instanceof ICBasicType) {
- try {
- switch (((ICBasicType)type).getType()) {
- case ICBasicType.t_Bool:
- result.append(Keywords.c_BOOL);
- break;
- case ICBasicType.t_Complex:
- result.append(Keywords.c_COMPLEX);
- break;
- case ICBasicType.t_Imaginary:
- result.append(Keywords.c_IMAGINARY);
- break;
- }
- } catch (DOMException e) {}
- }
-
- try {
- switch (((IBasicType)type).getType()) {
- case IBasicType.t_char:
- result.append(Keywords.CHAR);
- break;
- case IBasicType.t_double:
- result.append(Keywords.DOUBLE);
- break;
- case IBasicType.t_float:
- result.append(Keywords.FLOAT);
- break;
- case IBasicType.t_int:
- result.append(Keywords.INT);
- break;
- case IBasicType.t_void:
- result.append(Keywords.VOID);
- break;
- }
- } catch (DOMException e) {}
-
- } else if (type instanceof ICompositeType) {
- if (type instanceof ICPPClassType) {
- try {
- switch(((ICPPClassType)type).getKey()) {
- case ICPPClassType.k_class:
- result.append(Keywords.CLASS);
- break;
- }
- } catch (DOMException e) {}
- }
-
- try {
- switch(((ICompositeType)type).getKey()) {
- case ICompositeType.k_struct:
- result.append(Keywords.STRUCT);
- break;
- case ICompositeType.k_union:
- result.append(Keywords.UNION);
- break;
- }
- } catch (DOMException e) {}
-
- } else if (type instanceof ICPPReferenceType) {
- result.append(Keywords.cpAMPER);
- } else if (type instanceof ICPPTemplateTypeParameter) {
- try {
- result.append(getType(((ICPPTemplateTypeParameter)type).getDefault()));
- } catch (DOMException e) {}
- } else if (type instanceof IEnumeration) {
- result.append(Keywords.ENUM);
- } else if (type instanceof IFunctionType) {
- try {
- String temp = getType(((IFunctionType)type).getReturnType());
- if (temp != null && !temp.equals(EMPTY_STRING)) { result.append(temp); needSpace=true; }
- if (needSpace) { result.append(SPACE); needSpace=false; }
- temp = getParameterTypeString((IFunctionType)type);
- if (temp != null && !temp.equals(EMPTY_STRING)) { result.append(temp); needSpace=false; }
- } catch (DOMException e) {}
- } else if (type instanceof IPointerType) {
- result.append(Keywords.cpSTAR); needSpace=true;
-
- if (type instanceof IGPPPointerType) {
- if (((IGPPPointerType)type).isRestrict()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.RESTRICT); needSpace=true; }
- } else if (type instanceof ICPointerType) {
- if (((ICPointerType)type).isRestrict()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.RESTRICT); needSpace=true; }
- }
-
- try {
- if (((IPointerType)type).isConst()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.CONST); needSpace=true; }
- if (((IPointerType)type).isVolatile()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.VOLATILE); needSpace=true; }
- } catch (DOMException e) {}
-
- } else if (type instanceof IQualifierType) {
-
- if (type instanceof ICQualifierType) {
- if (((ICQualifierType)type).isRestrict()) { result.append(Keywords.RESTRICT); needSpace=true; }
- } else if (type instanceof IGPPQualifierType) {
- if (((IGPPQualifierType)type).isRestrict()) { result.append(Keywords.RESTRICT); needSpace=true; }
- }
-
- try {
- if (((IQualifierType)type).isConst()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.CONST); needSpace=true; }
- if (((IQualifierType)type).isVolatile()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.VOLATILE); needSpace=true; }
- } catch (DOMException e) {}
-
- }
-
- return result.toString();
- }
-
- public static String getType(IType type) {
- StringBuffer result = new StringBuffer();
- IType[] types = new IType[DEAULT_ITYPE_SIZE];
-
- // push all of the types onto the stack
- while(type != null && type instanceof ITypeContainer) {
- types = (IType[]) ArrayUtil.append( IType.class, types, type );
-
- try {
- type = ((ITypeContainer)type).getType();
- } catch (DOMException e) {}
- }
-
- if (type != null && !(type instanceof ITypeContainer)) {
- types = (IType[]) ArrayUtil.append( IType.class, types, type );
- }
-
- // pop all of the types off of the stack, and build the string representation while doing so
- for(int j=types.length-1; j>=0; j--) {
- if (types[j] != null)
- result.append(getTypeString(types[j]));
-
- if (types[j] != null && j>0) result.append(SPACE);
- }
-
- return result.toString();
- }
-
- public static String getDeclaratorType(IASTDeclarator decltor) {
- // get the most nested declarator
- while(decltor.getNestedDeclarator() != null)
- decltor = decltor.getNestedDeclarator();
-
- IBinding binding = decltor.getName().resolveBinding();
- IType type = null;
-
- try {
- if (binding instanceof CExternalFunction) {
- type = ((CExternalFunction)binding).getType();
- } else if (binding instanceof CExternalVariable) {
- type = ((CExternalVariable)binding).getType();
- } else if (binding instanceof IEnumerator) {
- type = ((IEnumerator)binding).getType();
- } else if (binding instanceof IFunction) {
- type = ((IFunction)binding).getType();
- } else if (binding instanceof ITypedef) {
- type = ((ITypedef)binding).getType();
- } else if (binding instanceof IVariable) {
- type = ((IVariable)binding).getType();
- }
- } catch (DOMException e) {
- return EMPTY_STRING;
- }
-
- if (type != null) {
- return getType(type);
- }
-
- return EMPTY_STRING;
- }
-
- /**
- * Return's the String representation of a node's type (if available). This is
- * currently only being used for testing.
- *
- * TODO Remove this function when done testing if it is no longer needed
- *
- * @param node
- * @return
- */
- public static String getNodeType(IASTNode node) {
- try {
- if (node instanceof IASTDeclarator)
- return getDeclaratorType((IASTDeclarator)node);
- if (node instanceof IASTName && ((IASTName)node).resolveBinding() instanceof IVariable)
- return getType(((IVariable)((IASTName)node).resolveBinding()).getType());
- if (node instanceof IASTName && ((IASTName)node).resolveBinding() instanceof IFunction)
- return getType(((IFunction)((IASTName)node).resolveBinding()).getType());
- if (node instanceof IASTName && ((IASTName)node).resolveBinding() instanceof IType)
- return getType((IType)((IASTName)node).resolveBinding());
- if (node instanceof IASTTypeId)
- return getType((IASTTypeId)node);
- } catch (DOMException e) { return EMPTY_STRING; }
-
- return EMPTY_STRING;
- }
-
- public static String getType(IASTTypeId typeId) {
- if (typeId instanceof CASTTypeId)
- return createCType(typeId.getAbstractDeclarator());
- else if (typeId instanceof CPPASTTypeId)
- return createCPPType(typeId.getAbstractDeclarator());
-
- return EMPTY_STRING;
- }
-
- private static String createCType(IASTDeclarator declarator) {
- IType type = CVisitor.createType(declarator);
- return getType(type);
- }
-
- private static String createCPPType(IASTDeclarator declarator) {
- IType type = CPPVisitor.createType(declarator);
- return getType(type);
- }
-
-}
\ No newline at end of file
Index: parser/org/eclipse/cdt/core/dom/ast/gnu/c/ICASTKnRFunctionDeclarator.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/gnu/c/ICASTKnRFunctionDeclarator.java,v
retrieving revision 1.1
diff -u -r1.1 ICASTKnRFunctionDeclarator.java
--- parser/org/eclipse/cdt/core/dom/ast/gnu/c/ICASTKnRFunctionDeclarator.java 19 Jan 2005 20:24:01 -0000 1.1
+++ parser/org/eclipse/cdt/core/dom/ast/gnu/c/ICASTKnRFunctionDeclarator.java 11 Mar 2005 18:06:12 -0000
@@ -12,6 +12,7 @@
import org.eclipse.cdt.core.dom.ast.ASTNodeProperty;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
+import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTName;
@@ -29,4 +30,5 @@
public static final ASTNodeProperty FUNCTION_PARAMETER = new ASTNodeProperty( "Parameter"); //$NON-NLS-1$
public void setParameterDeclarations(IASTDeclaration[] decls);
public IASTDeclaration[] getParameterDeclarations();
+ public IASTDeclarator getDeclaratorForParameterName(IASTName name);
}
Index: parser/org/eclipse/cdt/core/parser/Keywords.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/Keywords.java,v
retrieving revision 1.7
diff -u -r1.7 Keywords.java
--- parser/org/eclipse/cdt/core/parser/Keywords.java 9 Mar 2005 19:50:55 -0000 1.7
+++ parser/org/eclipse/cdt/core/parser/Keywords.java 11 Mar 2005 18:06:12 -0000
@@ -15,6 +15,12 @@
*/
public class Keywords {
+ public static final String CAST = "cast"; //$NON-NLS-1$
+ public static final String ALIGNOF = "alignof"; //$NON-NLS-1$
+ public static final String TYPEOF = "typeof"; //$NON-NLS-1$
+ public static final String cpMIN = "<?"; //$NON-NLS-1$
+ public static final String cpMAX = ">?"; //$NON-NLS-1$
+
public static final String _BOOL = "_Bool"; //$NON-NLS-1$
public static final String _COMPLEX = "_Complex"; //$NON-NLS-1$
public static final String _IMAGINARY = "_Imaginary"; //$NON-NLS-1$
Index: parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTKnRFunctionDeclarator.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTKnRFunctionDeclarator.java,v
retrieving revision 1.2
diff -u -r1.2 CASTKnRFunctionDeclarator.java
--- parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTKnRFunctionDeclarator.java 8 Mar 2005 19:32:21 -0000 1.2
+++ parser/org/eclipse/cdt/internal/core/dom/parser/c/CASTKnRFunctionDeclarator.java 11 Mar 2005 18:06:13 -0000
@@ -12,7 +12,9 @@
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
+import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTName;
+import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator;
/**
@@ -66,4 +68,27 @@
return true;
}
+
+ /* (non-Javadoc)
+ * @see org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator#getDeclaratorForParameterName()
+ */
+ public IASTDeclarator getDeclaratorForParameterName(IASTName name) {
+ boolean found=false;
+ for(int i=0; i<parameterNames.length; i++) {
+ if (parameterNames[i] == name) found = true;
+ }
+ if(!found) return null;
+
+ for(int i=0; i<parameterDeclarations.length; i++) {
+ if (parameterDeclarations[i] instanceof IASTSimpleDeclaration) {
+ IASTDeclarator[] decltors = ((IASTSimpleDeclaration)parameterDeclarations[i]).getDeclarators();
+ for(int j=0; j<decltors.length; j++) {
+ if(decltors[j].getName().toString().equals(name.toString()))
+ return decltors[j];
+ }
+ }
+ }
+
+ return null;
+ }
}
Index: parser/org/eclipse/cdt/core/dom/ast/ASTSignatureUtil.java
===================================================================
RCS file: parser/org/eclipse/cdt/core/dom/ast/ASTSignatureUtil.java
diff -N parser/org/eclipse/cdt/core/dom/ast/ASTSignatureUtil.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ parser/org/eclipse/cdt/core/dom/ast/ASTSignatureUtil.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,943 @@
+/*******************************************************************************
+ * Copyright (c) 2005 Rational Software Corp. 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:
+ * Rational Software - initial implementation
+ ******************************************************************************/
+
+package org.eclipse.cdt.core.dom.ast;
+
+import org.eclipse.cdt.core.dom.ast.c.ICASTArrayDesignator;
+import org.eclipse.cdt.core.dom.ast.c.ICASTArrayModifier;
+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.ICASTDesignatedInitializer;
+import org.eclipse.cdt.core.dom.ast.c.ICASTDesignator;
+import org.eclipse.cdt.core.dom.ast.c.ICASTFieldDesignator;
+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.ICASTTypeIdInitializerExpression;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTBinaryExpression;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCastExpression;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorInitializer;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclSpecifier;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeleteExpression;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTElaboratedTypeSpecifier;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTReferenceOperator;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeConstructorExpression;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTypeIdExpression;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTypenameExpression;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUnaryExpression;
+import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTCompoundStatementExpression;
+import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTTypeIdExpression;
+import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTUnaryExpression;
+import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator;
+import org.eclipse.cdt.core.dom.ast.gnu.c.IGCCASTArrayRangeDesignator;
+import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTBinaryExpression;
+import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTDeclSpecifier;
+import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTPointer;
+import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTSimpleDeclSpecifier;
+import org.eclipse.cdt.core.parser.GCCKeywords;
+import org.eclipse.cdt.core.parser.Keywords;
+
+/**
+ * This is a utility class to help convert AST elements to Strings.
+ * @author dsteffle
+ */
+
+public class ASTSignatureUtil {
+
+ private static final String COMMA_SPACE = ", "; //$NON-NLS-1$
+ private static final String EMPTY_STRING = ""; //$NON-NLS-1$
+ private static final String SPACE = " "; //$NON-NLS-1$
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+
+ /**
+ * Return's the String representation of a node's type (if available). This is
+ * currently only being used for testing.
+ *
+ * TODO Remove this function when done testing if it is no longer needed
+ *
+ * @param node
+ * @return
+ */
+ public static String getNodeSignature(IASTNode node) {
+ if (node instanceof IASTDeclarator)
+ return getSignature((IASTDeclarator)node);
+ if (node instanceof IASTDeclSpecifier)
+ return getSignature((IASTDeclSpecifier)node);
+ if (node instanceof IASTTypeId)
+ return getSignature((IASTTypeId)node);
+
+ return EMPTY_STRING;
+ }
+
+ // if only function declarator's have parameters then change this to function declarator... should check before starting.. make my life easier!
+ public static String getParameterSignature(IASTDeclarator decltor) {
+ // should only be working with decltor that has parms
+ if (!(decltor instanceof IASTStandardFunctionDeclarator || decltor instanceof ICASTKnRFunctionDeclarator)) return EMPTY_STRING;
+
+ StringBuffer result = new StringBuffer();
+
+ String[] parms = getParameterSignatureArray(decltor);
+
+ result.append(Keywords.cpLPAREN);
+ for(int i=0; i<parms.length; i++) {
+ if (parms[i] != null) {
+ result.append(parms[i]);
+ if (i<parms.length-1) result.append(COMMA_SPACE);
+ }
+ }
+ result.append(Keywords.cpRPAREN);
+
+ return result.toString();
+ }
+
+ public static String[] getParameterSignatureArray(IASTDeclarator decltor) {
+// should only be working with decltor that has parms
+ if (!(decltor instanceof IASTStandardFunctionDeclarator || decltor instanceof ICASTKnRFunctionDeclarator)) return EMPTY_STRING_ARRAY;
+
+ String[] result = EMPTY_STRING_ARRAY;
+
+ if (decltor instanceof IASTStandardFunctionDeclarator) {
+ IASTParameterDeclaration[] parms = null;
+ parms = ((IASTStandardFunctionDeclarator)decltor).getParameters();
+
+ result = new String[parms.length];
+
+ for(int i=0; i<parms.length; i++) {
+ if (parms[i] != null) {
+ result[i] = getSignature(parms[i].getDeclarator());
+ }
+ }
+ } else if (decltor instanceof ICASTKnRFunctionDeclarator) {
+ IASTName[] names = null;
+ names = ((ICASTKnRFunctionDeclarator)decltor).getParameterNames(); // required to get the order the parameters are used
+
+ result = new String[names.length];
+
+ for(int i=0; i<names.length; i++) {
+ if (names[i] != null) {
+ result[i] = getSignature(((ICASTKnRFunctionDeclarator)decltor).getDeclaratorForParameterName(names[i]));
+ }
+ }
+ }
+
+ return result;
+ }
+
+ private static String getDeclaratorSpecificSignature(IASTDeclarator declarator) {
+ StringBuffer result = new StringBuffer();
+ IASTPointerOperator[] ops = declarator.getPointerOperators();
+ boolean needSpace=false;
+
+ if (ops != null && ops.length > 0) {
+ for(int i=0; i<ops.length; i++) {
+ if (ops[i] != null) {
+ if (ops[i] instanceof IASTPointer) {
+ result.append(Keywords.cpSTAR); // want to have this before keywords on the pointer
+ needSpace=true;
+ }
+
+ if (ops[i] instanceof IGPPASTPointer) {
+ if (((IGPPASTPointer)ops[i]).isRestrict()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.RESTRICT); needSpace=true; }
+ }
+
+ if (ops[i] instanceof ICASTPointer) {
+ if (((ICASTPointer)ops[i]).isRestrict()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.RESTRICT); needSpace=true; }
+ }
+
+ if (ops[i] instanceof IASTPointer) {
+ if (((IASTPointer)ops[i]).isConst()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.CONST); needSpace=true; }
+ if (((IASTPointer)ops[i]).isVolatile()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.VOLATILE); needSpace=true; }
+ }
+
+ if (ops[i] instanceof ICPPASTReferenceOperator) {
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.cpAMPER); needSpace=true;
+ }
+ }
+ }
+ }
+
+ if (declarator instanceof IASTArrayDeclarator) {
+ IASTArrayModifier[] mods = ((IASTArrayDeclarator)declarator).getArrayModifiers();
+
+ for(int i=0; i<mods.length; i++) {
+ if (mods[i] != null) {
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.cpLBRACKET);
+ if (mods[i] instanceof ICASTArrayModifier) {
+ if (((ICASTArrayModifier)mods[i]).isConst()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.CONST); needSpace=true; }
+ if (((ICASTArrayModifier)mods[i]).isRestrict()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.RESTRICT); needSpace=true; }
+ if (((ICASTArrayModifier)mods[i]).isStatic()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.STATIC); needSpace=true; }
+ if (((ICASTArrayModifier)mods[i]).isVolatile()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.VOLATILE); }
+ }
+ result.append(Keywords.cpRBRACKET);
+ }
+ }
+ }
+
+ return result.toString();
+ }
+
+ private static String getDeclaratorSignature(IASTDeclarator declarator) {
+ if (declarator == null) return EMPTY_STRING;
+
+ StringBuffer result = new StringBuffer();
+
+ result.append(getDeclaratorSpecificSignature(declarator));
+
+ if (declarator.getNestedDeclarator() != null) {
+ result.append(SPACE);
+ result.append(Keywords.cpLPAREN);
+ result.append(getDeclaratorSignature(declarator.getNestedDeclarator()));
+ result.append(Keywords.cpRPAREN);
+ }
+
+ // append the parameter's signatures
+ result.append(getParameterSignature(declarator));
+
+ return result.toString();
+ }
+
+ public static String getInitializerString(IASTInitializer init) {
+ StringBuffer result = new StringBuffer();
+
+ if (init instanceof IASTInitializerExpression){
+ result.append(getExpressionString(((IASTInitializerExpression)init).getExpression()));
+ } else if (init instanceof IASTInitializerList) {
+ result.append(Keywords.cpLBRACE);
+ IASTInitializer[] inits = ((IASTInitializerList)init).getInitializers();
+ for(int i=0; i<inits.length; i++) {
+ result.append(getInitializerString(inits[i]));
+ if (i<inits.length-1) result.append(COMMA_SPACE);
+ }
+ result.append(Keywords.cpRBRACE);
+ } else if (init instanceof ICASTDesignatedInitializer) {
+ ICASTDesignator[] designators = ((ICASTDesignatedInitializer)init).getDesignators();
+ for(int i=0; i<designators.length; i++) {
+ result.append(getDesignatorSignature(designators[i]));
+ if (i<designators.length-1) result.append(COMMA_SPACE);
+ }
+ result.append(Keywords.cpASSIGN);
+ result.append(getInitializerString(((ICASTDesignatedInitializer)init).getOperandInitializer()));
+ } else if (init instanceof ICPPASTConstructorInitializer) {
+
+ }
+
+ return result.toString();
+ }
+
+ private static String getDesignatorSignature(ICASTDesignator designator) {
+ StringBuffer result = new StringBuffer();
+
+ if (designator instanceof ICASTArrayDesignator) {
+ result.append(Keywords.cpLBRACKET);
+ result.append(getExpressionString(((ICASTArrayDesignator)designator).getSubscriptExpression()));
+ result.append(Keywords.cpRBRACKET);
+ } else if (designator instanceof ICASTFieldDesignator) {
+ result.append(Keywords.cpDOT);
+ result.append(((ICASTFieldDesignator)designator).getName().toString());
+ } else if (designator instanceof IGCCASTArrayRangeDesignator) {
+ result.append(Keywords.cpLBRACKET);
+ result.append(getExpressionString(((IGCCASTArrayRangeDesignator)designator).getRangeFloor()));
+ result.append(SPACE);
+ result.append(Keywords.cpELLIPSIS);
+ result.append(SPACE);
+ result.append(getExpressionString(((IGCCASTArrayRangeDesignator)designator).getRangeCeiling()));
+ result.append(Keywords.cpRBRACKET);
+ }
+
+ return result.toString();
+ }
+
+ public static String getSignature(IASTDeclarator declarator) {
+ StringBuffer result = new StringBuffer();
+
+ // get the declSpec
+ IASTDeclSpecifier declSpec = null;
+
+ IASTNode node = declarator.getParent();
+ while( node instanceof IASTDeclarator ){
+ declarator = (IASTDeclarator) node;
+ node = node.getParent();
+ }
+
+ if( node instanceof IASTParameterDeclaration )
+ declSpec = ((IASTParameterDeclaration) node).getDeclSpecifier();
+ else if( node instanceof IASTSimpleDeclaration )
+ declSpec = ((IASTSimpleDeclaration)node).getDeclSpecifier();
+ else if( node instanceof IASTFunctionDefinition )
+ declSpec = ((IASTFunctionDefinition)node).getDeclSpecifier();
+ else if( node instanceof IASTTypeId )
+ declSpec = ((IASTTypeId)node).getDeclSpecifier();
+
+ // append the declSpec's signature to the signature
+ String specString = getSignature(declSpec);
+ if (specString != null && !specString.equals(EMPTY_STRING))
+ result.append(specString);
+
+ // append the declarator's signature (without specifier)
+ String decltorString = getDeclaratorSignature(declarator);
+ if (specString != null && specString.length() > 0 &&
+ decltorString != null && decltorString.length() > 0) {
+ result.append(SPACE);
+ }
+ result.append(decltorString);
+
+ return result.toString();
+ }
+
+ public static String getSignature(IASTDeclSpecifier declSpec) {
+ if (declSpec == null) return EMPTY_STRING;
+ boolean needSpace=false;
+
+ StringBuffer result = new StringBuffer();
+
+ if (declSpec.getStorageClass() == ICPPASTDeclSpecifier.sc_mutable) { result.append(Keywords.MUTABLE); needSpace=true; }
+ if (declSpec.getStorageClass() == IASTDeclSpecifier.sc_auto) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.AUTO); needSpace=true; }
+ if (declSpec.getStorageClass() == IASTDeclSpecifier.sc_extern) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.EXTERN); needSpace=true; }
+ if (declSpec.getStorageClass() == IASTDeclSpecifier.sc_register) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.REGISTER); needSpace=true; }
+ if (declSpec.getStorageClass() == IASTDeclSpecifier.sc_static) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.STATIC); needSpace=true; }
+ if (declSpec.getStorageClass() == IASTDeclSpecifier.sc_typedef) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.TYPEDEF); needSpace=true; }
+
+ if (declSpec.isConst()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.CONST); needSpace=true; }
+ if (declSpec.isInline()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.INLINE); needSpace=true; }
+ if (declSpec.isVolatile()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.VOLATILE); needSpace=true; }
+
+ if (declSpec instanceof ICASTDeclSpecifier ) {
+ if (((ICASTDeclSpecifier)declSpec).isRestrict()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.RESTRICT); needSpace=true; }
+ } else if (declSpec instanceof ICPPASTDeclSpecifier) {
+ if (declSpec.getStorageClass() == ICPPASTDeclSpecifier.sc_mutable) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.MUTABLE); needSpace=true; }
+ if (((ICPPASTDeclSpecifier)declSpec).isExplicit()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.EXPLICIT); needSpace=true; }
+ if (((ICPPASTDeclSpecifier)declSpec).isFriend()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.FRIEND); needSpace=true; }
+ if (((ICPPASTDeclSpecifier)declSpec).isVirtual()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.VIRTUAL); needSpace=true; }
+ } else if (declSpec instanceof IGPPASTDeclSpecifier) {
+ if (((IGPPASTDeclSpecifier)declSpec).isRestrict()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.RESTRICT); needSpace=true; }
+ }
+
+ // handle complex cases
+ if (declSpec instanceof IASTCompositeTypeSpecifier) {
+ if (declSpec instanceof ICPPASTCompositeTypeSpecifier) {
+ switch(((ICPPASTCompositeTypeSpecifier)declSpec).getKey()) {
+ case ICPPASTCompositeTypeSpecifier.k_class:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.CLASS); needSpace=true;
+ break;
+ case ICPPASTCompositeTypeSpecifier.k_struct:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.STRUCT); needSpace=true;
+ break;
+ case ICPPASTCompositeTypeSpecifier.k_union:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.UNION); needSpace=true;
+ break;
+ }
+ } else if (declSpec instanceof ICASTCompositeTypeSpecifier) {
+ switch(((ICASTCompositeTypeSpecifier)declSpec).getKey()) {
+ case ICASTCompositeTypeSpecifier.k_struct:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.STRUCT); needSpace=true;
+ break;
+ case ICASTCompositeTypeSpecifier.k_union:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.UNION); needSpace=true;
+ break;
+ }
+ }
+ } else if (declSpec instanceof IASTElaboratedTypeSpecifier) {
+ switch(((IASTElaboratedTypeSpecifier)declSpec).getKind()) {
+ case ICPPASTElaboratedTypeSpecifier.k_class:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.CLASS); needSpace=true;
+ break;
+ case IASTElaboratedTypeSpecifier.k_enum:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.ENUM); needSpace=true;
+ break;
+ case IASTElaboratedTypeSpecifier.k_struct:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.STRUCT); needSpace=true;
+ break;
+ case IASTElaboratedTypeSpecifier.k_union:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.UNION); needSpace=true;
+ break;
+ }
+ } else if (declSpec instanceof IASTEnumerationSpecifier) {
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.ENUM); needSpace=true;
+ } else if (declSpec instanceof IASTNamedTypeSpecifier) {
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(((IASTNamedTypeSpecifier)declSpec).getName().toString()); needSpace=true;
+ } else if (declSpec instanceof IASTSimpleDeclSpecifier) {
+ // handle complex cases
+ if (declSpec instanceof IGPPASTSimpleDeclSpecifier) {
+ if (((IGPPASTSimpleDeclSpecifier)declSpec).isLongLong()) result.append(Keywords.LONG_LONG);
+
+ switch(((IGPPASTSimpleDeclSpecifier)declSpec).getType()) {
+ case IGPPASTSimpleDeclSpecifier.t_typeof:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(GCCKeywords.TYPEOF); needSpace=true;
+ break;
+ case IGPPASTSimpleDeclSpecifier.t_Complex:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.c_COMPLEX); needSpace=true;
+ break;
+ case IGPPASTSimpleDeclSpecifier.t_Imaginary:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.c_IMAGINARY); needSpace=true;
+ break;
+ }
+ }
+
+ if (declSpec instanceof ICPPASTSimpleDeclSpecifier) {
+ switch(((ICPPASTSimpleDeclSpecifier)declSpec).getType()) {
+ case ICPPASTSimpleDeclSpecifier.t_bool:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.BOOL); needSpace=true;
+ break;
+ case ICPPASTSimpleDeclSpecifier.t_wchar_t:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.WCHAR_T); needSpace=true;
+ break;
+ }
+ }
+
+ if (declSpec instanceof ICASTSimpleDeclSpecifier) {
+ if (((ICASTSimpleDeclSpecifier)declSpec).isLongLong()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.LONG_LONG); needSpace=true; }
+
+ switch(((ICASTSimpleDeclSpecifier)declSpec).getType()) {
+ case ICASTSimpleDeclSpecifier.t_Bool:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.c_BOOL); needSpace=true;
+ break;
+ case ICASTSimpleDeclSpecifier.t_Complex:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.c_COMPLEX); needSpace=true;
+ break;
+ case ICASTSimpleDeclSpecifier.t_Imaginary:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.c_IMAGINARY); needSpace=true;
+ break;
+ }
+ }
+
+
+ // handle simple cases
+ if (((IASTSimpleDeclSpecifier)declSpec).isLong()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.LONG); needSpace=true; }
+ if (((IASTSimpleDeclSpecifier)declSpec).isShort()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.SHORT); needSpace=true; }
+ if (((IASTSimpleDeclSpecifier)declSpec).isSigned()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.SIGNED); needSpace=true; }
+ if (((IASTSimpleDeclSpecifier)declSpec).isUnsigned()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.UNSIGNED); needSpace=true; }
+
+ switch(((IASTSimpleDeclSpecifier)declSpec).getType()) {
+ case IASTSimpleDeclSpecifier.t_char:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.CHAR); needSpace=true;
+ break;
+ case IASTSimpleDeclSpecifier.t_double:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.DOUBLE); needSpace=true;
+ break;
+ case IASTSimpleDeclSpecifier.t_float:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.FLOAT); needSpace=true;
+ break;
+ case IASTSimpleDeclSpecifier.t_int:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.INT); needSpace=true;
+ break;
+ case IASTSimpleDeclSpecifier.t_void:
+ if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.VOID); needSpace=true;
+ break;
+ }
+ }
+
+ return result.toString();
+ }
+
+ public static String getSignature(IASTTypeId typeId) {
+ return getSignature(typeId.getAbstractDeclarator());
+ }
+
+ /**
+ * Return a string for the given expression. Expressions having an extension kind should
+ * provide their own toString method which will be called by this.
+ * @param expression
+ * @return
+ */
+ public static String getExpressionString( IASTExpression expression ){
+ if (expression instanceof IASTArraySubscriptExpression)
+ return getArraySubscriptExpression((IASTArraySubscriptExpression)expression);
+ else if (expression instanceof IASTBinaryExpression)
+ return getBinaryExpression( (IASTBinaryExpression)expression );
+ else if (expression instanceof IASTCastExpression)
+ return getCastExpression((IASTCastExpression)expression);
+ else if (expression instanceof IASTConditionalExpression)
+ return getConditionalExpression((IASTConditionalExpression)expression);
+ else if (expression instanceof IASTExpressionList)
+ return getExpressionList((IASTExpressionList)expression);
+ else if (expression instanceof IASTFieldReference)
+ return getFieldReference((IASTFieldReference)expression);
+ else if (expression instanceof IASTFunctionCallExpression)
+ return getFunctionCallExpression((IASTFunctionCallExpression)expression);
+ else if (expression instanceof IASTIdExpression)
+ return getIdExpression((IASTIdExpression)expression);
+ else if (expression instanceof IASTLiteralExpression)
+ return getLiteralExpression((IASTLiteralExpression)expression);
+ else if (expression instanceof IASTTypeIdExpression)
+ return getTypeIdExpression( (IASTTypeIdExpression)expression );
+ else if (expression instanceof IASTUnaryExpression)
+ return getUnaryExpression( (IASTUnaryExpression)expression );
+ else if (expression instanceof ICASTTypeIdInitializerExpression)
+ return getTypeIdInitializerExpression((ICASTTypeIdInitializerExpression)expression);
+ else if (expression instanceof ICPPASTDeleteExpression)
+ return getDeleteExpression((ICPPASTDeleteExpression)expression);
+ else if (expression instanceof ICPPASTNewExpression)
+ return getNewExpression((ICPPASTNewExpression)expression);
+ else if (expression instanceof ICPPASTSimpleTypeConstructorExpression)
+ return getSimpleTypeConstructorExpression((ICPPASTSimpleTypeConstructorExpression)expression);
+ else if (expression instanceof ICPPASTTypenameExpression)
+ return getTypenameExpression((ICPPASTTypenameExpression)expression);
+ else if (expression instanceof IGNUASTCompoundStatementExpression)
+ return getCompoundStatementExpression((IGNUASTCompoundStatementExpression)expression);
+
+ return getEmptyExpression( expression );
+ }
+
+ private static String getArraySubscriptExpression(IASTArraySubscriptExpression expression) {
+ StringBuffer result = new StringBuffer();
+ result.append(getExpressionString(expression.getArrayExpression()));
+ result.append(Keywords.cpLBRACKET);
+ result.append(getExpressionString(expression.getSubscriptExpression()));
+ result.append(Keywords.cpRBRACKET);
+ return result.toString();
+ }
+
+ private static String getCastExpression(IASTCastExpression expression) {
+ StringBuffer result = new StringBuffer();
+ boolean normalCast = false;
+
+ if (expression.getOperator() == IASTCastExpression.op_cast)
+ normalCast = true;
+
+ if (normalCast) {
+ result.append(Keywords.cpLPAREN);
+ result.append(getSignature(expression.getTypeId()));
+ result.append(Keywords.cpRPAREN);
+ result.append(getExpressionString(expression.getOperand()));
+ } else {
+ result.append(getCastOperatorString(expression));
+ result.append(Keywords.cpLT);
+ result.append(getSignature(expression.getTypeId()));
+ result.append(Keywords.cpGT);
+ result.append(Keywords.cpLPAREN);
+ result.append(getExpressionString(expression.getOperand()));
+ result.append(Keywords.cpRPAREN);
+ }
+
+ return result.toString();
+ }
+
+ private static String getFieldReference(IASTFieldReference expression) {
+ StringBuffer result = new StringBuffer();
+ result.append(getExpressionString(expression.getFieldOwner()));
+ result.append(Keywords.cpDOT);
+ result.append(expression.getFieldName().toString());
+ return result.toString();
+ }
+
+ private static String getFunctionCallExpression(IASTFunctionCallExpression expression) {
+ StringBuffer result = new StringBuffer();
+ result.append(getExpressionString(expression.getFunctionNameExpression()));
+ result.append(Keywords.cpLPAREN);
+ result.append(getExpressionString(expression.getParameterExpression()));
+ result.append(Keywords.cpRPAREN);
+ return result.toString();
+ }
+
+ private static String getTypeIdInitializerExpression(ICASTTypeIdInitializerExpression expression) {
+ StringBuffer result = new StringBuffer();
+ result.append(Keywords.cpLPAREN);
+ result.append(getSignature(expression.getTypeId()));
+ result.append(Keywords.cpRPAREN);
+ result.append(getInitializerString(expression.getInitializer()));
+ return result.toString();
+ }
+
+ private static String getDeleteExpression(ICPPASTDeleteExpression expression) {
+ StringBuffer result = new StringBuffer();
+ result.append(Keywords.DELETE);
+ result.append(SPACE);
+ if (expression.getOperand() != null)
+ result.append(getExpressionString(expression.getOperand()));
+ return result.toString();
+ }
+
+ private static String getSimpleTypeConstructorExpression(ICPPASTSimpleTypeConstructorExpression expression) {
+ StringBuffer result = new StringBuffer();
+ result.append(Keywords.cpLPAREN);
+ result.append(expression.getInitialValue());
+ result.append(Keywords.cpRPAREN);
+ return result.toString();
+ }
+
+ private static String getTypenameExpression(ICPPASTTypenameExpression expression) {
+ StringBuffer result = new StringBuffer();
+ result.append(Keywords.TYPENAME);
+ result.append(SPACE);
+ result.append(expression.getName().toString());
+ IASTExpression initValue = expression.getInitialValue();
+ result.append(Keywords.cpLPAREN);
+ if (initValue != null) {
+ result.append(getExpressionString(initValue));
+ }
+ result.append(Keywords.cpRPAREN);
+ return result.toString();
+ }
+
+ private static String getCompoundStatementExpression(IGNUASTCompoundStatementExpression expression) {
+ return String.valueOf(Keywords.cpELLIPSIS); // TODO might need to getSignature(IASTStatement) in the future
+ }
+
+ private static String getTypeIdExpression(IASTTypeIdExpression expression) {
+ StringBuffer result = new StringBuffer();
+ String operator = getTypeIdExpressionOperator(expression);
+ if (operator != null && !operator.equals(EMPTY_STRING)) result.append(operator);
+
+ if (operator != null && !operator.equals(EMPTY_STRING)) result.append(Keywords.cpLPAREN);
+ result.append(getSignature(expression.getTypeId()));
+ if (operator != null && !operator.equals(EMPTY_STRING)) result.append(Keywords.cpRPAREN);
+ return result.toString();
+ }
+
+ private static String getExpressionList(IASTExpressionList expression) {
+ StringBuffer result = new StringBuffer();
+ IASTExpression[] exps = expression.getExpressions();
+ if (exps != null && exps.length>0) {
+ result.append(Keywords.cpLPAREN);
+ for(int i=0; i<exps.length; i++) {
+ result.append(getExpressionString(exps[i]));
+ if (i < exps.length-1) {
+ result.append(COMMA_SPACE);
+ }
+ }
+ result.append(Keywords.cpRPAREN);
+ }
+ return result.toString();
+ }
+
+ private static String getEmptyExpression( IASTExpression expression ){
+ return EMPTY_STRING;
+ }
+
+ private static String getLiteralExpression( IASTLiteralExpression expression ){
+ return expression.toString();
+ }
+
+ private static String getIdExpression( IASTIdExpression expression ){
+ return expression.getName().toString();
+ }
+ private static String getConditionalExpression( IASTConditionalExpression expression ){
+ StringBuffer result = new StringBuffer();
+ result.append(getExpressionString(expression.getLogicalConditionExpression()));
+ result.append(SPACE);
+ result.append(Keywords.cpQUESTION);
+ result.append(SPACE);
+ result.append(getExpressionString(expression.getPositiveResultExpression()));
+ result.append(SPACE);
+ result.append(Keywords.cpCOLON);
+ result.append(SPACE);
+ result.append(getExpressionString(expression.getNegativeResultExpression()));
+ return result.toString();
+ }
+ private static String getNewExpression( ICPPASTNewExpression expression ){
+ StringBuffer result = new StringBuffer();
+ result.append(Keywords.NEW);
+ result.append(SPACE);
+ if (expression.getNewPlacement() != null) {
+ result.append(getExpressionString(expression.getNewPlacement()));
+ }
+ result.append(getSignature(expression.getTypeId()));
+ return result.toString();
+ }
+ private static String getBinaryExpression( IASTBinaryExpression expression ){
+ StringBuffer buffer = new StringBuffer();
+ buffer.append( getExpressionString( expression.getOperand1() ) );
+ buffer.append(SPACE);
+ buffer.append( getBinaryOperatorString( expression ));
+ buffer.append(SPACE);
+ buffer.append( getExpressionString( expression.getOperand2() ) );
+ return buffer.toString();
+ }
+
+ public static String getUnaryExpression( IASTUnaryExpression expression ){
+ StringBuffer buffer = new StringBuffer();
+ boolean postOperator=false;
+ boolean primaryBracketed=false;
+
+ switch(expression.getOperator()) {
+ case IASTUnaryExpression.op_postFixDecr:
+ case IASTUnaryExpression.op_postFixIncr:
+ postOperator=true;
+ break;
+ case IASTUnaryExpression.op_bracketedPrimary:
+ primaryBracketed=true;
+ break;
+ default:
+ postOperator=false;
+ break;
+ }
+
+ if (!postOperator && !primaryBracketed) buffer.append(getUnaryOperatorString(expression));
+
+ // need to add a space to the unary expression if it is a specific operator
+ switch(expression.getOperator()) {
+ case IASTUnaryExpression.op_sizeof:
+ case ICPPASTUnaryExpression.op_throw:
+ case ICPPASTUnaryExpression.op_typeid:
+ buffer.append(SPACE);
+ break;
+ }
+
+ if (primaryBracketed) buffer.append(Keywords.cpLPAREN);
+ buffer.append(getExpressionString(expression.getOperand()));
+ if (primaryBracketed) buffer.append(Keywords.cpRPAREN);
+ if (postOperator && !primaryBracketed) buffer.append(getUnaryOperatorString(expression));
+
+ return buffer.toString();
+ }
+
+ /**
+ * @param expression
+ * @return
+ */
+ public static String getCastOperatorString(IASTCastExpression expression) {
+ int op = expression.getOperator();
+ String opString = EMPTY_STRING;
+
+ if (expression instanceof ICPPASTCastExpression) {
+ switch (op) {
+ case ICPPASTCastExpression.op_const_cast:
+ opString = Keywords.CONST_CAST;
+ break;
+ case ICPPASTCastExpression.op_dynamic_cast:
+ opString = Keywords.DYNAMIC_CAST;
+ break;
+ case ICPPASTCastExpression.op_reinterpret_cast:
+ opString = Keywords.REINTERPRET_CAST;
+ break;
+ case ICPPASTCastExpression.op_static_cast:
+ opString = Keywords.STATIC_CAST;
+ break;
+ default:
+ break;
+ }
+ }
+
+ if (!opString.equals(EMPTY_STRING)) return opString;
+
+ switch(op) {
+ case IASTCastExpression.op_cast:
+ opString = Keywords.CAST;
+ break;
+ }
+
+ return opString;
+ }
+ public static String getUnaryOperatorString(IASTUnaryExpression be) {
+ int op = be.getOperator();
+ String opString = EMPTY_STRING;
+
+ if (be instanceof ICPPASTUnaryExpression) {
+ switch(op) {
+ case ICPPASTUnaryExpression.op_throw:
+ opString = Keywords.THROW;
+ break;
+ case ICPPASTUnaryExpression.op_typeid:
+ opString = Keywords.TYPEID;
+ break;
+ }
+ } else if (be instanceof IGNUASTUnaryExpression) {
+ switch(op) {
+ case IGNUASTUnaryExpression.op_alignOf:
+ opString = Keywords.ALIGNOF;
+ break;
+ case IGNUASTUnaryExpression.op_typeof:
+ opString = Keywords.TYPEOF;
+ break;
+ }
+ }
+
+ if (!opString.equals(EMPTY_STRING)) return opString;
+
+ switch(op) {
+ case IASTUnaryExpression.op_amper:
+ opString = String.valueOf(Keywords.cpAMPER);
+ break;
+ case IASTUnaryExpression.op_minus:
+ opString = String.valueOf(Keywords.cpMINUS);
+ break;
+ case IASTUnaryExpression.op_not:
+ opString = String.valueOf(Keywords.cpNOT);
+ break;
+ case IASTUnaryExpression.op_plus:
+ opString = String.valueOf(Keywords.cpPLUS);
+ break;
+ case IASTUnaryExpression.op_postFixDecr:
+ opString = String.valueOf(Keywords.cpDECR);
+ break;
+ case IASTUnaryExpression.op_postFixIncr:
+ opString = String.valueOf(Keywords.cpINCR);
+ break;
+ case IASTUnaryExpression.op_prefixDecr:
+ opString = String.valueOf(Keywords.cpDECR);
+ break;
+ case IASTUnaryExpression.op_prefixIncr:
+ opString = String.valueOf(Keywords.cpINCR);
+ break;
+ case IASTUnaryExpression.op_sizeof:
+ opString = Keywords.SIZEOF;
+ break;
+ case IASTUnaryExpression.op_star:
+ opString = String.valueOf(Keywords.cpSTAR);
+ break;
+ case IASTUnaryExpression.op_tilde:
+ opString = String.valueOf(Keywords.cpCOMPL);
+ break;
+ }
+
+ return opString;
+ }
+
+ public static String getBinaryOperatorString(IASTBinaryExpression be) {
+ int op = be.getOperator();
+ String opString = EMPTY_STRING;
+
+ if (be instanceof ICPPASTBinaryExpression) {
+ switch(op) {
+ case ICPPASTBinaryExpression.op_pmarrow:
+ opString = String.valueOf(Keywords.cpARROW);
+ break;
+ case ICPPASTBinaryExpression.op_pmdot:
+ opString = String.valueOf(Keywords.cpDOT);
+ break;
+ }
+ } else if (be instanceof IGPPASTBinaryExpression) {
+ switch(op) {
+ case IGPPASTBinaryExpression.op_max:
+ opString = String.valueOf(Keywords.cpMAX);
+ break;
+ case IGPPASTBinaryExpression.op_min:
+ opString = String.valueOf(Keywords.cpMIN);
+ break;
+ }
+ }
+
+ if (!opString.equals(EMPTY_STRING)) return opString;
+
+ switch(op) {
+ case IASTBinaryExpression.op_multiply:
+ opString = String.valueOf(Keywords.cpSTAR);
+ break;
+ case IASTBinaryExpression.op_divide:
+ opString = String.valueOf(Keywords.cpDIV);
+ break;
+ case IASTBinaryExpression.op_modulo:
+ opString = String.valueOf(Keywords.cpMOD);
+ break;
+ case IASTBinaryExpression.op_plus:
+ opString = String.valueOf(Keywords.cpPLUS);
+ break;
+ case IASTBinaryExpression.op_minus:
+ opString = String.valueOf(Keywords.cpMINUS);
+ break;
+ case IASTBinaryExpression.op_shiftLeft:
+ opString = String.valueOf(Keywords.cpSHIFTL);
+ break;
+ case IASTBinaryExpression.op_shiftRight:
+ opString = String.valueOf(Keywords.cpSHIFTR);
+ break;
+ case IASTBinaryExpression.op_lessThan:
+ opString = String.valueOf(Keywords.cpLT);
+ break;
+ case IASTBinaryExpression.op_greaterThan:
+ opString = String.valueOf(Keywords.cpGT);
+ break;
+ case IASTBinaryExpression.op_lessEqual:
+ opString = String.valueOf(Keywords.cpLTEQUAL);
+ break;
+ case IASTBinaryExpression.op_greaterEqual:
+ opString = String.valueOf(Keywords.cpGTEQUAL);
+ break;
+ case IASTBinaryExpression.op_binaryAnd:
+ opString = String.valueOf(Keywords.cpAMPER);
+ break;
+ case IASTBinaryExpression.op_binaryXor:
+ opString = String.valueOf(Keywords.cpXOR);
+ break;
+ case IASTBinaryExpression.op_binaryOr:
+ opString = String.valueOf(Keywords.cpBITOR);
+ break;
+ case IASTBinaryExpression.op_logicalAnd:
+ opString = String.valueOf(Keywords.cpAND);
+ break;
+ case IASTBinaryExpression.op_logicalOr:
+ opString = String.valueOf(Keywords.cpOR);
+ break;
+ case IASTBinaryExpression.op_assign:
+ opString = String.valueOf(Keywords.cpASSIGN);
+ break;
+ case IASTBinaryExpression.op_multiplyAssign:
+ opString = String.valueOf(Keywords.cpSTARASSIGN);
+ break;
+ case IASTBinaryExpression.op_divideAssign:
+ opString = String.valueOf(Keywords.cpDIVASSIGN);
+ break;
+ case IASTBinaryExpression.op_moduloAssign:
+ opString = String.valueOf(Keywords.cpMODASSIGN);
+ break;
+ case IASTBinaryExpression.op_plusAssign:
+ opString = String.valueOf(Keywords.cpPLUSASSIGN);
+ break;
+ case IASTBinaryExpression.op_minusAssign:
+ opString = String.valueOf(Keywords.cpMINUSASSIGN);
+ break;
+ case IASTBinaryExpression.op_shiftLeftAssign:
+ opString = String.valueOf(Keywords.cpSHIFTLASSIGN);
+ break;
+ case IASTBinaryExpression.op_shiftRightAssign:
+ opString = String.valueOf(Keywords.cpSHIFTRASSIGN);
+ break;
+ case IASTBinaryExpression.op_binaryAndAssign:
+ opString = String.valueOf(Keywords.cpAMPERASSIGN);
+ break;
+ case IASTBinaryExpression.op_binaryXorAssign:
+ opString = String.valueOf(Keywords.cpXORASSIGN);
+ break;
+ case IASTBinaryExpression.op_binaryOrAssign:
+ opString = String.valueOf(Keywords.cpBITORASSIGN);
+ break;
+ case IASTBinaryExpression.op_equals:
+ opString = String.valueOf(Keywords.cpEQUAL);
+ break;
+ case IASTBinaryExpression.op_notequals:
+ opString = String.valueOf(Keywords.cpNOTEQUAL);
+ break;
+ }
+
+ return opString;
+ }
+
+ private static String getTypeIdExpressionOperator(IASTTypeIdExpression expression) {
+ String result = EMPTY_STRING;
+
+ if (expression instanceof IGNUASTTypeIdExpression) {
+ switch (expression.getOperator()) {
+ case IGNUASTTypeIdExpression.op_alignof:
+ result = Keywords.ALIGNOF;
+ break;
+ case IGNUASTTypeIdExpression.op_typeof:
+ result = Keywords.TYPEOF;
+ break;
+ }
+ }
+
+ if (expression instanceof ICPPASTTypeIdExpression) {
+ switch(expression.getOperator()) {
+ case ICPPASTTypeIdExpression.op_typeid:
+ result = Keywords.TYPEID;
+ break;
+ }
+ }
+
+ if (expression.getOperator() == IASTTypeIdExpression.op_sizeof)
+ result = Keywords.SIZEOF;
+
+ return result;
+ }
+
+}
Index: parser/org/eclipse/cdt/core/dom/ast/ASTTypeUtil.java
===================================================================
RCS file: parser/org/eclipse/cdt/core/dom/ast/ASTTypeUtil.java
diff -N parser/org/eclipse/cdt/core/dom/ast/ASTTypeUtil.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ parser/org/eclipse/cdt/core/dom/ast/ASTTypeUtil.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,345 @@
+/*******************************************************************************
+ * Copyright (c) 2005 Rational Software Corp. 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:
+ * Rational Software - initial implementation
+ ******************************************************************************/
+
+package org.eclipse.cdt.core.dom.ast;
+
+import org.eclipse.cdt.core.dom.ast.c.ICArrayType;
+import org.eclipse.cdt.core.dom.ast.c.ICBasicType;
+import org.eclipse.cdt.core.dom.ast.c.ICPointerType;
+import org.eclipse.cdt.core.dom.ast.c.ICQualifierType;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPBasicType;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPReferenceType;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateTypeParameter;
+import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPBasicType;
+import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPPointerType;
+import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPQualifierType;
+import org.eclipse.cdt.core.parser.GCCKeywords;
+import org.eclipse.cdt.core.parser.Keywords;
+import org.eclipse.cdt.core.parser.util.ArrayUtil;
+import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer;
+import org.eclipse.cdt.internal.core.dom.parser.c.CASTTypeId;
+import org.eclipse.cdt.internal.core.dom.parser.c.CExternalFunction;
+import org.eclipse.cdt.internal.core.dom.parser.c.CExternalVariable;
+import org.eclipse.cdt.internal.core.dom.parser.c.CVisitor;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTypeId;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor;
+
+/**
+ * This is a utility class to help convert AST elements to Strings.
+ * @author dsteffle
+ */
+public class ASTTypeUtil {
+
+ private static final String COMMA_SPACE = ", "; //$NON-NLS-1$
+ private static final String EMPTY_STRING = ""; //$NON-NLS-1$
+ private static final String SPACE = " "; //$NON-NLS-1$
+ private static final String[] EMPTY_STRING_ARRAY = new String[0];
+ private static final int DEAULT_ITYPE_SIZE = 2;
+
+ public static String getParameterTypeString(IFunctionType type) {
+ StringBuffer result = new StringBuffer();
+ String[] parms = getParameterTypeStringArray(type);
+
+ result.append(Keywords.cpLPAREN);
+ for(int i=0; i<parms.length; i++) {
+ if (parms[i] != null) {
+ result.append(parms[i]);
+ if (i<parms.length-1) result.append(COMMA_SPACE);
+ }
+ }
+ result.append(Keywords.cpRPAREN);
+ return result.toString();
+ }
+
+ public static String[] getParameterTypeStringArray(IFunctionType type) {
+ IType[] parms = null;
+ try {
+ parms = type.getParameterTypes();
+ } catch (DOMException e) { return EMPTY_STRING_ARRAY; }
+
+ String[] result = new String[parms.length];
+
+ for(int i=0; i<parms.length; i++) {
+ if (parms[i] != null) {
+ result[i] = getType(parms[i]);
+ }
+ }
+
+ return result;
+ }
+
+ private static String getTypeString(IType type) {
+ StringBuffer result = new StringBuffer();
+ boolean needSpace = false;
+
+ if (type instanceof IArrayType) {
+ result.append(Keywords.cpLBRACKET);
+ if (type instanceof ICArrayType) {
+ try {
+ if (((ICArrayType)type).isConst()) { result.append(Keywords.CONST); needSpace=true; }
+ if (((ICArrayType)type).isRestrict()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.RESTRICT); needSpace=true; }
+ if (((ICArrayType)type).isStatic()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.STATIC); needSpace=true; }
+ if (((ICArrayType)type).isVolatile()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.VOLATILE); }
+ } catch (DOMException e) {}
+ }
+ result.append(Keywords.cpRBRACKET);
+ } else if (type instanceof IBasicType) {
+ try {
+ if (((IBasicType)type).isSigned()) { result.append(Keywords.SIGNED); needSpace = true; }
+ else if (((IBasicType)type).isUnsigned()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.UNSIGNED); needSpace=true; }
+ if (((IBasicType)type).isLong()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.LONG); needSpace = true; }
+ else if (((IBasicType)type).isShort()) { if (needSpace) { result.append(SPACE); needSpace=false; }result.append(Keywords.SHORT); needSpace = true; }
+ } catch (DOMException e) {}
+
+ if (type instanceof IGPPBasicType) {
+ try {
+ if (((IGPPBasicType)type).isLongLong()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.LONG_LONG); needSpace=true; }
+
+ switch (((IGPPBasicType)type).getType()) {
+ case IGPPBasicType.t_Complex:
+ result.append(Keywords.c_COMPLEX);
+ break;
+ case IGPPBasicType.t_Imaginary:
+ result.append(Keywords.c_IMAGINARY);
+ break;
+ case IGPPBasicType.t_typeof:
+ result.append(GCCKeywords.TYPEOF);
+ break;
+ }
+ } catch (DOMException e) {}
+ } else if (type instanceof ICPPBasicType) {
+ try {
+ switch (((ICPPBasicType)type).getType()) {
+ case ICPPBasicType.t_bool:
+ result.append(Keywords.BOOL);
+ break;
+ case ICPPBasicType.t_wchar_t:
+ result.append(Keywords.WCHAR_T);
+ break;
+ }
+ } catch (DOMException e) {}
+ } else if (type instanceof ICBasicType) {
+ try {
+ switch (((ICBasicType)type).getType()) {
+ case ICBasicType.t_Bool:
+ result.append(Keywords.c_BOOL);
+ break;
+ case ICBasicType.t_Complex:
+ result.append(Keywords.c_COMPLEX);
+ break;
+ case ICBasicType.t_Imaginary:
+ result.append(Keywords.c_IMAGINARY);
+ break;
+ }
+ } catch (DOMException e) {}
+ }
+
+ try {
+ switch (((IBasicType)type).getType()) {
+ case IBasicType.t_char:
+ result.append(Keywords.CHAR);
+ break;
+ case IBasicType.t_double:
+ result.append(Keywords.DOUBLE);
+ break;
+ case IBasicType.t_float:
+ result.append(Keywords.FLOAT);
+ break;
+ case IBasicType.t_int:
+ result.append(Keywords.INT);
+ break;
+ case IBasicType.t_void:
+ result.append(Keywords.VOID);
+ break;
+ }
+ } catch (DOMException e) {}
+
+ } else if (type instanceof ICompositeType) {
+ if (type instanceof ICPPClassType) {
+ try {
+ switch(((ICPPClassType)type).getKey()) {
+ case ICPPClassType.k_class:
+ result.append(Keywords.CLASS);
+ break;
+ }
+ } catch (DOMException e) {}
+ }
+
+ try {
+ switch(((ICompositeType)type).getKey()) {
+ case ICompositeType.k_struct:
+ result.append(Keywords.STRUCT);
+ break;
+ case ICompositeType.k_union:
+ result.append(Keywords.UNION);
+ break;
+ }
+ } catch (DOMException e) {}
+
+ } else if (type instanceof ICPPReferenceType) {
+ result.append(Keywords.cpAMPER);
+ } else if (type instanceof ICPPTemplateTypeParameter) {
+ try {
+ result.append(getType(((ICPPTemplateTypeParameter)type).getDefault()));
+ } catch (DOMException e) {}
+ } else if (type instanceof IEnumeration) {
+ result.append(Keywords.ENUM);
+ } else if (type instanceof IFunctionType) {
+ try {
+ String temp = getType(((IFunctionType)type).getReturnType());
+ if (temp != null && !temp.equals(EMPTY_STRING)) { result.append(temp); needSpace=true; }
+ if (needSpace) { result.append(SPACE); needSpace=false; }
+ temp = getParameterTypeString((IFunctionType)type);
+ if (temp != null && !temp.equals(EMPTY_STRING)) { result.append(temp); needSpace=false; }
+ } catch (DOMException e) {}
+ } else if (type instanceof IPointerType) {
+ result.append(Keywords.cpSTAR); needSpace=true;
+
+ if (type instanceof IGPPPointerType) {
+ if (((IGPPPointerType)type).isRestrict()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.RESTRICT); needSpace=true; }
+ } else if (type instanceof ICPointerType) {
+ if (((ICPointerType)type).isRestrict()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.RESTRICT); needSpace=true; }
+ }
+
+ try {
+ if (((IPointerType)type).isConst()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.CONST); needSpace=true; }
+ if (((IPointerType)type).isVolatile()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.VOLATILE); needSpace=true; }
+ } catch (DOMException e) {}
+
+ } else if (type instanceof IQualifierType) {
+
+ if (type instanceof ICQualifierType) {
+ if (((ICQualifierType)type).isRestrict()) { result.append(Keywords.RESTRICT); needSpace=true; }
+ } else if (type instanceof IGPPQualifierType) {
+ if (((IGPPQualifierType)type).isRestrict()) { result.append(Keywords.RESTRICT); needSpace=true; }
+ }
+
+ try {
+ if (((IQualifierType)type).isConst()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.CONST); needSpace=true; }
+ if (((IQualifierType)type).isVolatile()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.VOLATILE); needSpace=true; }
+ } catch (DOMException e) {}
+
+ }
+
+ return result.toString();
+ }
+
+ public static String getType(IType type) {
+ StringBuffer result = new StringBuffer();
+ IType[] types = new IType[DEAULT_ITYPE_SIZE];
+
+ // push all of the types onto the stack
+ while(type != null && type instanceof ITypeContainer) {
+ types = (IType[]) ArrayUtil.append( IType.class, types, type );
+
+ try {
+ type = ((ITypeContainer)type).getType();
+ } catch (DOMException e) {}
+ }
+
+ if (type != null && !(type instanceof ITypeContainer)) {
+ types = (IType[]) ArrayUtil.append( IType.class, types, type );
+ }
+
+ // pop all of the types off of the stack, and build the string representation while doing so
+ for(int j=types.length-1; j>=0; j--) {
+ if (types[j] instanceof ITypedef)
+ continue;
+
+ if (types[j] != null && result.length() > 0) result.append(SPACE); // only add a space if this is not the first type being added
+
+ if (types[j] != null)
+ result.append(getTypeString(types[j]));
+ }
+
+ return result.toString();
+ }
+
+ public static String getType(IASTDeclarator decltor) {
+ // get the most nested declarator
+ while(decltor.getNestedDeclarator() != null)
+ decltor = decltor.getNestedDeclarator();
+
+ IBinding binding = decltor.getName().resolveBinding();
+ IType type = null;
+
+ try {
+ if (binding instanceof CExternalFunction) {
+ type = ((CExternalFunction)binding).getType();
+ } else if (binding instanceof CExternalVariable) {
+ type = ((CExternalVariable)binding).getType();
+ } else if (binding instanceof IEnumerator) {
+ type = ((IEnumerator)binding).getType();
+ } else if (binding instanceof IFunction) {
+ type = ((IFunction)binding).getType();
+ } else if (binding instanceof ITypedef) {
+ type = ((ITypedef)binding).getType();
+ } else if (binding instanceof IVariable) {
+ type = ((IVariable)binding).getType();
+ }
+ } catch (DOMException e) {
+ return EMPTY_STRING;
+ }
+
+ if (type != null) {
+ return getType(type);
+ }
+
+ return EMPTY_STRING;
+ }
+
+ /**
+ * Return's the String representation of a node's type (if available). This is
+ * currently only being used for testing.
+ *
+ * TODO Remove this function when done testing if it is no longer needed
+ *
+ * @param node
+ * @return
+ */
+ public static String getNodeType(IASTNode node) {
+ try {
+ if (node instanceof IASTDeclarator)
+ return getType((IASTDeclarator)node);
+ if (node instanceof IASTName && ((IASTName)node).resolveBinding() instanceof IVariable)
+ return getType(((IVariable)((IASTName)node).resolveBinding()).getType());
+ if (node instanceof IASTName && ((IASTName)node).resolveBinding() instanceof IFunction)
+ return getType(((IFunction)((IASTName)node).resolveBinding()).getType());
+ if (node instanceof IASTName && ((IASTName)node).resolveBinding() instanceof IType)
+ return getType((IType)((IASTName)node).resolveBinding());
+ if (node instanceof IASTTypeId)
+ return getType((IASTTypeId)node);
+ } catch (DOMException e) { return EMPTY_STRING; }
+
+ return EMPTY_STRING;
+ }
+
+ public static String getType(IASTTypeId typeId) {
+ if (typeId instanceof CASTTypeId)
+ return createCType(typeId.getAbstractDeclarator());
+ else if (typeId instanceof CPPASTTypeId)
+ return createCPPType(typeId.getAbstractDeclarator());
+
+ return EMPTY_STRING;
+ }
+
+ private static String createCType(IASTDeclarator declarator) {
+ IType type = CVisitor.createType(declarator);
+ return getType(type);
+ }
+
+ private static String createCPPType(IASTDeclarator declarator) {
+ IType type = CPPVisitor.createType(declarator);
+ return getType(type);
+ }
+
+}
Index: src/org/eclipse/cdt/ui/tests/DOMAST/DOMAST.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/DOMAST.java,v
retrieving revision 1.27
diff -u -r1.27 DOMAST.java
--- src/org/eclipse/cdt/ui/tests/DOMAST/DOMAST.java 9 Mar 2005 19:50:50 -0000 1.27
+++ src/org/eclipse/cdt/ui/tests/DOMAST/DOMAST.java 11 Mar 2005 18:08:00 -0000
@@ -13,7 +13,8 @@
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.CDOM;
import org.eclipse.cdt.core.dom.IASTServiceProvider;
-import org.eclipse.cdt.core.dom.ast.ASTUtil;
+import org.eclipse.cdt.core.dom.ast.ASTSignatureUtil;
+import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
import org.eclipse.cdt.core.dom.ast.IASTArrayModifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
@@ -33,6 +34,9 @@
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.IFunction;
+import org.eclipse.cdt.core.dom.ast.IType;
+import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.dom.ast.c.CASTVisitor;
import org.eclipse.cdt.core.dom.ast.c.ICASTDesignator;
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
@@ -107,6 +111,9 @@
public class DOMAST extends ViewPart {
private static final String ASTUTIL_MENU_LABEL = "ASTUtil#"; //$NON-NLS-1$
private static final String DISPLAY_TYPE = "getNodeType(IASTNode)"; //$NON-NLS-1$
+ private static final String DISPLAY_SIGNATURE = "getNodeSignature(IASTNode)"; //$NON-NLS-1$
+ private static final String DISPLAY_EXPRESSION = "getExpressionString(IASTExpression)"; //$NON-NLS-1$
+ private static final String DISPLAY_INITIALIZER = "getInitializerString(IASTInitializer)"; //$NON-NLS-1$
private static final String NOT_VALID_COMPILATION_UNIT = "The active editor does not contain a valid compilation unit."; //$NON-NLS-1$
private static final String EXTENSION_CXX = "CXX"; //$NON-NLS-1$
private static final String EXTENSION_CPP = "CPP"; //$NON-NLS-1$
@@ -129,6 +136,9 @@
private Action openDeclarationsAction;
private Action openReferencesAction;
private Action displayNodeTypeAction;
+ private Action displayNodeSignatureAction;
+ private Action displayExpressionAction;
+ private Action displayInitializerAction;
private Action singleClickAction;
private Action loadActiveEditorAction;
private Action refreshAction;
@@ -638,25 +648,71 @@
MenuManager menuMgr = new MenuManager(POPUPMENU);
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
+ private void hideMenuItems(IMenuManager manager) {
+ IContributionItem[] items = manager.getItems();
+
+ for (int i = 0; i < items.length; i++) {
+ if (items[i] instanceof IMenuManager) {
+ hideMenuItems((IMenuManager)items[i]);
+ }
+
+ if (items[i] instanceof ActionContributionItem) {
+ String text = ((ActionContributionItem) items[i]).getAction().getText();
+ IASTNode selectedNode = null;
+ if (viewer.getSelection() instanceof StructuredSelection
+ && ((StructuredSelection) viewer.getSelection())
+ .getFirstElement() instanceof TreeObject) {
+ selectedNode = ((TreeObject) ((StructuredSelection) viewer
+ .getSelection()).getFirstElement()).getNode();
+ }
+
+ if (text.equals(OPEN_REFERENCES) || text.equals(OPEN_DECLARATIONS)) {
+ if (selectedNode instanceof IASTName) {
+ items[i].setVisible(true);
+ } else {
+ items[i].setVisible(false);
+ }
+ }
+
+ if (text.equals(DISPLAY_SIGNATURE)) {
+ if (selectedNode instanceof IASTDeclarator ||
+ selectedNode instanceof IASTDeclSpecifier ||
+ selectedNode instanceof IASTTypeId) {
+ items[i].setVisible(true);
+ } else {
+ items[i].setVisible(false);
+ }
+ } else if (text.equals(DISPLAY_TYPE)) {
+ if (selectedNode instanceof IASTDeclarator ||
+ selectedNode instanceof IASTTypeId ||
+ (selectedNode instanceof IASTName && (
+ ((IASTName)selectedNode).resolveBinding() instanceof IVariable ||
+ ((IASTName)selectedNode).resolveBinding() instanceof IFunction ||
+ ((IASTName)selectedNode).resolveBinding() instanceof IType))) {
+ items[i].setVisible(true);
+ } else {
+ items[i].setVisible(false);
+ }
+ } else if (text.equals(DISPLAY_EXPRESSION)) {
+ if (selectedNode instanceof IASTExpression) {
+ items[i].setVisible(true);
+ } else {
+ items[i].setVisible(false);
+ }
+ } else if (text.equals(DISPLAY_INITIALIZER)) {
+ if (selectedNode instanceof IASTInitializer) {
+ items[i].setVisible(true);
+ } else {
+ items[i].setVisible(false);
+ }
+ }
+ }
+ }
+ }
+
public void menuAboutToShow(IMenuManager manager) {
DOMAST.this.fillContextMenu(manager);
- IContributionItem[] items = manager.getItems();
- for (int i = 0; i < items.length; i++) {
- if (items[i] instanceof ActionContributionItem
- && (((ActionContributionItem) items[i]).getAction()
- .getText().equals(OPEN_REFERENCES) || ((ActionContributionItem) items[i])
- .getAction().getText().equals(OPEN_DECLARATIONS))) {
- if (viewer.getSelection() instanceof StructuredSelection
- && ((StructuredSelection) viewer.getSelection())
- .getFirstElement() instanceof TreeObject
- && ((TreeObject) ((StructuredSelection) viewer
- .getSelection()).getFirstElement()).getNode() instanceof IASTName) {
- items[i].setVisible(true);
- } else {
- items[i].setVisible(false);
- }
- }
- }
+ hideMenuItems(manager);
}
});
Menu menu = menuMgr.createContextMenu(viewer.getControl());
@@ -681,6 +737,9 @@
// ASTUtil#... menu
MenuManager astMenu = new MenuManager(ASTUTIL_MENU_LABEL);
astMenu.add(displayNodeTypeAction);
+ astMenu.add(displayNodeSignatureAction);
+ astMenu.add(displayExpressionAction);
+ astMenu.add(displayInitializerAction);
manager.add(astMenu);
manager.add(new Separator());
drillDownAdapter.addNavigationActions(manager);
@@ -787,13 +846,52 @@
if (selection instanceof IStructuredSelection &&
((IStructuredSelection)selection).getFirstElement() instanceof TreeObject &&
((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode() != null) {
- showMessage("ASTUtil#getNodeType(IASTNode): \"" + ASTUtil.getNodeType(((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
+ showMessage("ASTUtil#getNodeType(IASTNode): \"" + ASTTypeUtil.getNodeType(((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}
} };
displayNodeTypeAction.setText(DISPLAY_TYPE);
displayNodeTypeAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
.getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
+ displayNodeSignatureAction = new Action() {
+ public void run() {
+ ISelection selection = viewer.getSelection();
+ if (selection instanceof IStructuredSelection &&
+ ((IStructuredSelection)selection).getFirstElement() instanceof TreeObject &&
+ ((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode() != null) {
+ showMessage("ASTSignatureUtil#getNodeSignature(IASTNode): \"" + ASTSignatureUtil.getNodeSignature(((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+ } };
+ displayNodeSignatureAction.setText(DISPLAY_SIGNATURE);
+ displayNodeSignatureAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
+ .getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
+
+ displayExpressionAction = new Action() {
+ public void run() {
+ ISelection selection = viewer.getSelection();
+ if (selection instanceof IStructuredSelection &&
+ ((IStructuredSelection)selection).getFirstElement() instanceof TreeObject &&
+ ((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode() instanceof IASTExpression) {
+ showMessage("ASTSignatureUtil#getExpressionString(IASTExpression): \"" + ASTSignatureUtil.getExpressionString((IASTExpression)((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+ } };
+ displayExpressionAction.setText(DISPLAY_EXPRESSION);
+ displayExpressionAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
+ .getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
+
+ displayInitializerAction = new Action() {
+ public void run() {
+ ISelection selection = viewer.getSelection();
+ if (selection instanceof IStructuredSelection &&
+ ((IStructuredSelection)selection).getFirstElement() instanceof TreeObject &&
+ ((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode() instanceof IASTInitializer) {
+ showMessage("ASTSignatureUtil#getInitializerString(IASTInitializer): \"" + ASTSignatureUtil.getInitializerString((IASTInitializer)((TreeObject)((IStructuredSelection)selection).getFirstElement()).getNode()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$
+ }
+ } };
+ displayInitializerAction.setText(DISPLAY_INITIALIZER);
+ displayInitializerAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
+ .getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
+
singleClickAction = new ASTHighlighterAction(part);
}
@@ -939,7 +1037,7 @@
void showMessage(String message) {
MessageDialog.openInformation(viewer.getControl().getShell(), VIEW_NAME,
- message);
+ message.replaceAll("&", "&&")); //$NON-NLS-1$ //$NON-NLS-2$
}
/**
Index: src/org/eclipse/cdt/ui/tests/DOMAST/TreeObject.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.ui.tests/src/org/eclipse/cdt/ui/tests/DOMAST/TreeObject.java,v
retrieving revision 1.15
diff -u -r1.15 TreeObject.java
--- src/org/eclipse/cdt/ui/tests/DOMAST/TreeObject.java 9 Feb 2005 21:01:43 -0000 1.15
+++ src/org/eclipse/cdt/ui/tests/DOMAST/TreeObject.java 11 Mar 2005 18:08:00 -0000
@@ -10,6 +10,7 @@
**********************************************************************/
package org.eclipse.cdt.ui.tests.DOMAST;
+import org.eclipse.cdt.core.dom.ast.ASTSignatureUtil;
import org.eclipse.cdt.core.dom.ast.IASTArrayModifier;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTCastExpression;
@@ -35,12 +36,7 @@
import org.eclipse.cdt.core.dom.ast.c.ICASTDesignator;
import org.eclipse.cdt.core.dom.ast.c.ICASTFieldDesignator;
import org.eclipse.cdt.core.dom.ast.c.ICASTPointer;
-import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTBinaryExpression;
-import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCastExpression;
-import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUnaryExpression;
-import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.gnu.c.IGCCASTArrayRangeDesignator;
-import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTPointer;
import org.eclipse.core.runtime.IAdaptable;
@@ -48,61 +44,12 @@
* @author dsteffle
*/
public class TreeObject implements IAdaptable {
- private static final String CAST = "cast"; //$NON-NLS-1$
- private static final String STATIC_CAST = "static_cast"; //$NON-NLS-1$
- private static final String REINTERPRET_CAST = "reinterpret_cast"; //$NON-NLS-1$
- private static final String DYNAMIC_CAST = "dynamic_cast"; //$NON-NLS-1$
- private static final String CONST_CAST = "const_cast"; //$NON-NLS-1$
- private static final String OP_BRACKETEDPRIMARY = "( )"; //$NON-NLS-1$
- private static final String OP_TILDE = "~"; //$NON-NLS-1$
- private static final String OP_SIZEOF = "sizeof"; //$NON-NLS-1$
- private static final String OP_INCR = "++"; //$NON-NLS-1$
- private static final String OP_DECR = "--"; //$NON-NLS-1$
- private static final String OP_NOT = "!"; //$NON-NLS-1$
- private static final String OP_AMPER = "&"; //$NON-NLS-1$
- private static final String OP_TYPEOF = "typeof"; //$NON-NLS-1$
- private static final String OP_ALIGNOF = "alignof"; //$NON-NLS-1$
- private static final String OP_TYPEID = "typeid"; //$NON-NLS-1$
- private static final String OP_THROW = "throw"; //$NON-NLS-1$
private static final String VARIABLE_SIZED_ = "* "; //$NON-NLS-1$
private static final String VOLATILE_ = "volatile "; //$NON-NLS-1$
private static final String STATIC_ = "static "; //$NON-NLS-1$
private static final String RESTRICT_ = "restrict "; //$NON-NLS-1$
private static final String CONST_ = "const "; //$NON-NLS-1$
private static final String DASH = "-"; //$NON-NLS-1$
- private static final String OP_NOTEQUALS = "!="; //$NON-NLS-1$
- private static final String OP_EQUALS = "=="; //$NON-NLS-1$
- private static final String OP_BINARYXORASSIGN = "^="; //$NON-NLS-1$
- private static final String OP_BINARYANDASSIGN = "&="; //$NON-NLS-1$
- private static final String OP_BINARYORASSIGN = "|="; //$NON-NLS-1$
- private static final String OP_SHIFTRIGHTASSIGN = ">>="; //$NON-NLS-1$
- private static final String OP_SHIFTLEFTASSIGN = "<<="; //$NON-NLS-1$
- private static final String OP_MINUSASSIGN = "-="; //$NON-NLS-1$
- private static final String OP_PLUSASSIGN = "+="; //$NON-NLS-1$
- private static final String OP_MODULOASSIGN = "%="; //$NON-NLS-1$
- private static final String OP_DIVIDEASSIGN = "/="; //$NON-NLS-1$
- private static final String OP_MULTIPLYASSIGN = "*="; //$NON-NLS-1$
- private static final String OP_ASSIGN = "="; //$NON-NLS-1$
- private static final String OP_LOGICALOR = "||"; //$NON-NLS-1$
- private static final String OP_LOGICALAND = "&&"; //$NON-NLS-1$
- private static final String OP_BINARYOR = "|"; //$NON-NLS-1$
- private static final String OP_BINARYXOR = "^"; //$NON-NLS-1$
- private static final String OP_BINARYAND = "&"; //$NON-NLS-1$
- private static final String OP_GREATEREQUAL = ">="; //$NON-NLS-1$
- private static final String OP_LESSEQUAL = "<="; //$NON-NLS-1$
- private static final String OP_GREATERTHAN = ">"; //$NON-NLS-1$
- private static final String OP_LESSTHAN = "<"; //$NON-NLS-1$
- private static final String OP_SHIFTRIGHT = ">>"; //$NON-NLS-1$
- private static final String OP_SHIFTLEFT = "<<"; //$NON-NLS-1$
- private static final String OP_MINUS = DASH; //$NON-NLS-1$
- private static final String OP_PLUS = "+"; //$NON-NLS-1$
- private static final String OP_MODULO = "%"; //$NON-NLS-1$
- private static final String OP_DIVIDE = "/"; //$NON-NLS-1$
- private static final String OP_STAR = "*"; //$NON-NLS-1$
- private static final String OP_MIN = "<?"; //$NON-NLS-1$
- private static final String OP_MAX = ">?"; //$NON-NLS-1$
- private static final String OP_PMDOT = "."; //$NON-NLS-1$
- private static final String OP_PMARROW = "->"; //$NON-NLS-1$
private static final String FILE_SEPARATOR = "\\"; //$NON-NLS-1$
public static final String BLANK_STRING = ""; //$NON-NLS-1$
private static final String IGCCAST_PREFIX = "IGCCAST"; //$NON-NLS-1$
@@ -113,7 +60,7 @@
private static final String IAST_PREFIX = "IAST"; //$NON-NLS-1$
private static final String START_OF_LIST = ": "; //$NON-NLS-1$
private static final String LIST_SEPARATOR = ", "; //$NON-NLS-1$
- private static final String FILENAME_SEPARATOR = OP_PMDOT; //$NON-NLS-1$
+ private static final String FILENAME_SEPARATOR = "."; //$NON-NLS-1$
private IASTNode node = null;
private TreeParent parent;
@@ -223,13 +170,13 @@
buffer.append(node.toString());
} else if ( node instanceof IASTCastExpression ) {
buffer.append(START_OF_LIST);
- buffer.append( getCastOperatorString( (IASTCastExpression)node ) );
+ buffer.append( ASTSignatureUtil.getCastOperatorString( (IASTCastExpression)node ) );
} else if ( node instanceof IASTUnaryExpression ) {
buffer.append(START_OF_LIST);
- buffer.append( getUnaryOperatorString( (IASTUnaryExpression)node ) );
+ buffer.append( ASTSignatureUtil.getUnaryOperatorString( (IASTUnaryExpression)node ) );
} else if ( node instanceof IASTBinaryExpression ) {
buffer.append(START_OF_LIST);
- buffer.append( getBinaryOperatorString( (IASTBinaryExpression)node ) );
+ buffer.append( ASTSignatureUtil.getBinaryOperatorString( (IASTBinaryExpression)node ) );
} else if ( node instanceof ICASTDesignator ) {
if ( node instanceof ICASTArrayDesignator && ((ICASTArrayDesignator)node).getSubscriptExpression() != null ) {
buffer.append(START_OF_LIST);
@@ -295,230 +242,6 @@
}
return buffer.toString();
- }
-
- /**
- * @param expression
- * @return
- */
- private String getCastOperatorString(IASTCastExpression expression) {
- int op = expression.getOperator();
- String opString = BLANK_STRING;
-
- if (expression instanceof ICPPASTCastExpression) {
- switch (op) {
- case ICPPASTCastExpression.op_const_cast:
- opString = CONST_CAST;
- break;
- case ICPPASTCastExpression.op_dynamic_cast:
- opString = DYNAMIC_CAST;
- break;
- case ICPPASTCastExpression.op_reinterpret_cast:
- opString = REINTERPRET_CAST;
- break;
- case ICPPASTCastExpression.op_static_cast:
- opString = STATIC_CAST;
- break;
- default:
- break;
- }
- }
-
- if (!opString.equals(BLANK_STRING)) return opString;
-
- switch(op) {
- case IASTCastExpression.op_cast:
- opString = CAST;
- break;
- }
-
- return opString;
- }
- private String getUnaryOperatorString(IASTUnaryExpression be) {
- int op = be.getOperator();
- String opString = BLANK_STRING;
-
- if (be instanceof ICPPASTUnaryExpression) {
- switch(op) {
- case ICPPASTUnaryExpression.op_throw:
- opString = OP_THROW;
- break;
- case ICPPASTUnaryExpression.op_typeid:
- opString = OP_TYPEID;
- break;
- }
- } else if (be instanceof IGNUASTUnaryExpression) {
- switch(op) {
- case IGNUASTUnaryExpression.op_alignOf:
- opString = OP_ALIGNOF;
- break;
- case IGNUASTUnaryExpression.op_typeof:
- opString = OP_TYPEOF;
- break;
- }
- }
-
- if (!opString.equals(BLANK_STRING)) return opString;
-
- switch(op) {
- case IASTUnaryExpression.op_amper:
- opString = OP_AMPER;
- break;
- case IASTUnaryExpression.op_bracketedPrimary:
- opString = OP_BRACKETEDPRIMARY;
- break;
- case IASTUnaryExpression.op_minus:
- opString = OP_MINUS;
- break;
- case IASTUnaryExpression.op_not:
- opString = OP_NOT;
- break;
- case IASTUnaryExpression.op_plus:
- opString = OP_PLUS;
- break;
- case IASTUnaryExpression.op_postFixDecr:
- opString = OP_DECR;
- break;
- case IASTUnaryExpression.op_postFixIncr:
- opString = OP_INCR;
- break;
- case IASTUnaryExpression.op_prefixDecr:
- opString = OP_DECR;
- break;
- case IASTUnaryExpression.op_prefixIncr:
- opString = OP_INCR;
- break;
- case IASTUnaryExpression.op_sizeof:
- opString = OP_SIZEOF;
- break;
- case IASTUnaryExpression.op_star:
- opString = OP_STAR;
- break;
- case IASTUnaryExpression.op_tilde:
- opString = OP_TILDE;
- break;
- }
-
- return opString;
- }
-
- private String getBinaryOperatorString(IASTBinaryExpression be) {
- int op = be.getOperator();
- String opString = BLANK_STRING;
-
- if (be instanceof ICPPASTBinaryExpression) {
- switch(op) {
- case ICPPASTBinaryExpression.op_pmarrow:
- opString = OP_PMARROW;
- break;
- case ICPPASTBinaryExpression.op_pmdot:
- opString = OP_PMDOT;
- break;
- }
- } else if (be instanceof IGPPASTBinaryExpression) {
- switch(op) {
- case IGPPASTBinaryExpression.op_max:
- opString = OP_MAX;
- break;
- case IGPPASTBinaryExpression.op_min:
- opString = OP_MIN;
- break;
- }
- }
-
- if (!opString.equals(BLANK_STRING)) return opString;
-
- switch(op) {
- case IASTBinaryExpression.op_multiply:
- opString = OP_STAR;
- break;
- case IASTBinaryExpression.op_divide:
- opString = OP_DIVIDE;
- break;
- case IASTBinaryExpression.op_modulo:
- opString = OP_MODULO;
- break;
- case IASTBinaryExpression.op_plus:
- opString = OP_PLUS;
- break;
- case IASTBinaryExpression.op_minus:
- opString = OP_MINUS;
- break;
- case IASTBinaryExpression.op_shiftLeft:
- opString = OP_SHIFTLEFT;
- break;
- case IASTBinaryExpression.op_shiftRight:
- opString = OP_SHIFTRIGHT;
- break;
- case IASTBinaryExpression.op_lessThan:
- opString = OP_LESSTHAN;
- break;
- case IASTBinaryExpression.op_greaterThan:
- opString = OP_GREATERTHAN;
- break;
- case IASTBinaryExpression.op_lessEqual:
- opString = OP_LESSEQUAL;
- break;
- case IASTBinaryExpression.op_greaterEqual:
- opString = OP_GREATEREQUAL;
- break;
- case IASTBinaryExpression.op_binaryAnd:
- opString = OP_BINARYAND;
- break;
- case IASTBinaryExpression.op_binaryXor:
- opString = OP_BINARYXOR;
- break;
- case IASTBinaryExpression.op_binaryOr:
- opString = OP_BINARYOR;
- break;
- case IASTBinaryExpression.op_logicalAnd:
- opString = OP_LOGICALAND;
- break;
- case IASTBinaryExpression.op_logicalOr:
- opString = OP_LOGICALOR;
- break;
- case IASTBinaryExpression.op_assign:
- opString = OP_ASSIGN;
- break;
- case IASTBinaryExpression.op_multiplyAssign:
- opString = OP_MULTIPLYASSIGN;
- break;
- case IASTBinaryExpression.op_divideAssign:
- opString = OP_DIVIDEASSIGN;
- break;
- case IASTBinaryExpression.op_moduloAssign:
- opString = OP_MODULOASSIGN;
- break;
- case IASTBinaryExpression.op_plusAssign:
- opString = OP_PLUSASSIGN;
- break;
- case IASTBinaryExpression.op_minusAssign:
- opString = OP_MINUSASSIGN;
- break;
- case IASTBinaryExpression.op_shiftLeftAssign:
- opString = OP_SHIFTLEFTASSIGN;
- break;
- case IASTBinaryExpression.op_shiftRightAssign:
- opString = OP_SHIFTRIGHTASSIGN;
- break;
- case IASTBinaryExpression.op_binaryAndAssign:
- opString = OP_BINARYANDASSIGN;
- break;
- case IASTBinaryExpression.op_binaryXorAssign:
- opString = OP_BINARYXORASSIGN;
- break;
- case IASTBinaryExpression.op_binaryOrAssign:
- opString = OP_BINARYORASSIGN;
- break;
- case IASTBinaryExpression.op_equals:
- opString = OP_EQUALS;
- break;
- case IASTBinaryExpression.op_notequals:
- opString = OP_NOTEQUALS;
- break;
- }
-
- return opString;
}
private String getDeclaratorName(IASTDeclarator decltor) {
Index: parser/org/eclipse/cdt/core/parser/tests/ast2/DOMParserTestSuite.java
===================================================================
RCS file: /home/tools/org.eclipse.cdt-core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMParserTestSuite.java,v
retrieving revision 1.1
diff -u -r1.1 DOMParserTestSuite.java
--- parser/org/eclipse/cdt/core/parser/tests/ast2/DOMParserTestSuite.java 21 Jan 2005 21:12:14 -0000 1.1
+++ parser/org/eclipse/cdt/core/parser/tests/ast2/DOMParserTestSuite.java 11 Mar 2005 18:07:47 -0000
@@ -32,6 +32,7 @@
suite.addTestSuite( DOMLocationTests.class );
suite.addTest( DOMLocationInclusionTests.suite() );
suite.addTestSuite( AST2KnRTests.class );
+ suite.addTestSuite( AST2UtilTests.class );
return suite;
}
Index: parser/org/eclipse/cdt/core/parser/tests/ast2/AST2UtilTests.java
===================================================================
RCS file: parser/org/eclipse/cdt/core/parser/tests/ast2/AST2UtilTests.java
diff -N parser/org/eclipse/cdt/core/parser/tests/ast2/AST2UtilTests.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ parser/org/eclipse/cdt/core/parser/tests/ast2/AST2UtilTests.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,183 @@
+/*******************************************************************************
+ * Copyright (c) 2005 Rational Software Corp. 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:
+ * Rational Software - initial implementation
+ ******************************************************************************/
+package org.eclipse.cdt.core.parser.tests.ast2;
+
+import org.eclipse.cdt.core.dom.ast.ASTSignatureUtil;
+import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
+import org.eclipse.cdt.core.dom.ast.IASTCastExpression;
+import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
+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.IASTExpression;
+import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
+import org.eclipse.cdt.core.dom.ast.IASTInitializerExpression;
+import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
+import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
+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.IFunction;
+import org.eclipse.cdt.core.dom.ast.IFunctionType;
+import org.eclipse.cdt.core.dom.ast.IType;
+import org.eclipse.cdt.core.parser.ParserLanguage;
+
+/**
+ * @author dsteffle
+ */
+public class AST2UtilTests extends AST2BaseTest {
+
+ protected void isExpressionStringEqual(IASTExpression exp, String str) {
+ assertEquals(str, ASTSignatureUtil.getExpressionString(exp));
+ }
+
+ protected void isParameterSignatureEqual(IASTDeclarator decltor, String str) {
+ assertEquals(str, ASTSignatureUtil.getParameterSignature(decltor));
+ }
+
+ protected void isSignatureEqual(IASTDeclarator decltor, String str) {
+ assertEquals(str, ASTSignatureUtil.getSignature(decltor));
+ }
+
+ protected void isSignatureEqual(IASTDeclSpecifier declSpec, String str) {
+ assertEquals(str, ASTSignatureUtil.getSignature(declSpec));
+ }
+
+ protected void isSignatureEqual(IASTTypeId typeId, String str) {
+ assertEquals(str, ASTSignatureUtil.getSignature(typeId));
+ }
+
+ protected void isTypeEqual(IASTDeclarator decltor, String str) {
+ assertEquals(str, ASTTypeUtil.getType(decltor));
+ }
+
+ protected void isTypeEqual(IASTTypeId typeId, String str) {
+ assertEquals(str, ASTTypeUtil.getType(typeId));
+ }
+
+ protected void isTypeEqual(IType type, String str) {
+ assertEquals(str, ASTTypeUtil.getType(type));
+ }
+
+ protected void isParameterTypeEqual(IFunctionType fType, String str) {
+ assertEquals(str, ASTTypeUtil.getParameterTypeString(fType));
+ }
+
+ public void testSimpleSignature() throws Exception {
+ StringBuffer buff = new StringBuffer();
+ buff.append("int l, m, n=0;\n"); //$NON-NLS-1$
+ buff.append("int j = l ? m : n;\n"); //$NON-NLS-1$
+ buff.append("int i = l^m;\n"); //$NON-NLS-1$
+ buff.append("int g = i<<=j;\n"); //$NON-NLS-1$
+ buff.append("int f = sizeof( int );\n"); //$NON-NLS-1$
+ buff.append("int e = ~f;\n"); //$NON-NLS-1$
+ buff.append("int d = ++e;\n"); //$NON-NLS-1$
+ buff.append("int b = d++;\n"); //$NON-NLS-1$
+ buff.append("int c = sizeof b;\n"); //$NON-NLS-1$
+ buff.append("int a = b + c;\n"); //$NON-NLS-1$
+
+ IASTTranslationUnit tu = parse(buff.toString(), ParserLanguage.C);
+ IASTDeclaration[] d = tu.getDeclarations();
+
+ isExpressionStringEqual(((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[2].getInitializer()).getExpression(), "0"); //$NON-NLS-1$
+ isExpressionStringEqual(((IASTInitializerExpression)((IASTSimpleDeclaration)d[1]).getDeclarators()[0].getInitializer()).getExpression(), "l ? m : n"); //$NON-NLS-1$
+ isExpressionStringEqual(((IASTInitializerExpression)((IASTSimpleDeclaration)d[2]).getDeclarators()[0].getInitializer()).getExpression(), "l ^ m"); //$NON-NLS-1$
+ isExpressionStringEqual(((IASTInitializerExpression)((IASTSimpleDeclaration)d[3]).getDeclarators()[0].getInitializer()).getExpression(), "i <<= j"); //$NON-NLS-1$
+ isExpressionStringEqual(((IASTInitializerExpression)((IASTSimpleDeclaration)d[4]).getDeclarators()[0].getInitializer()).getExpression(), "sizeof(int)"); //$NON-NLS-1$
+ isExpressionStringEqual(((IASTInitializerExpression)((IASTSimpleDeclaration)d[5]).getDeclarators()[0].getInitializer()).getExpression(), "~f"); //$NON-NLS-1$
+ isExpressionStringEqual(((IASTInitializerExpression)((IASTSimpleDeclaration)d[6]).getDeclarators()[0].getInitializer()).getExpression(), "++e"); //$NON-NLS-1$
+ isExpressionStringEqual(((IASTInitializerExpression)((IASTSimpleDeclaration)d[7]).getDeclarators()[0].getInitializer()).getExpression(), "d++"); //$NON-NLS-1$
+ isExpressionStringEqual(((IASTInitializerExpression)((IASTSimpleDeclaration)d[8]).getDeclarators()[0].getInitializer()).getExpression(), "sizeof b"); //$NON-NLS-1$
+ isExpressionStringEqual(((IASTInitializerExpression)((IASTSimpleDeclaration)d[9]).getDeclarators()[0].getInitializer()).getExpression(), "b + c"); //$NON-NLS-1$
+ }
+
+ public void testSimpleParameter() throws Exception {
+ StringBuffer buff = new StringBuffer();
+ buff.append("int a(int x);\n"); //$NON-NLS-1$
+ buff.append("int * b(char y, int x);\n"); //$NON-NLS-1$
+ buff.append("void c(int * z, float **b);\n"); //$NON-NLS-1$
+ buff.append("static int d(int a[restrict]);\n"); //$NON-NLS-1$
+
+ IASTTranslationUnit tu = parse(buff.toString(), ParserLanguage.C);
+ IASTDeclaration[] d = tu.getDeclarations();
+
+ isParameterSignatureEqual(((IASTSimpleDeclaration)d[0]).getDeclarators()[0], "(int)"); //$NON-NLS-1$
+ isParameterSignatureEqual(((IASTSimpleDeclaration)d[1]).getDeclarators()[0], "(char, int)"); //$NON-NLS-1$
+ isParameterSignatureEqual(((IASTSimpleDeclaration)d[2]).getDeclarators()[0], "(int *, float **)"); //$NON-NLS-1$
+ isParameterSignatureEqual(((IASTSimpleDeclaration)d[3]).getDeclarators()[0], "(int [restrict])"); //$NON-NLS-1$
+
+ isSignatureEqual(((IASTSimpleDeclaration)d[0]).getDeclarators()[0], "int (int)"); //$NON-NLS-1$
+ isSignatureEqual(((IASTSimpleDeclaration)d[1]).getDeclarators()[0], "int *(char, int)"); //$NON-NLS-1$
+ isSignatureEqual(((IASTSimpleDeclaration)d[2]).getDeclarators()[0], "void (int *, float **)"); //$NON-NLS-1$
+ isSignatureEqual(((IASTSimpleDeclaration)d[3]).getDeclarators()[0], "static int (int [restrict])"); //$NON-NLS-1$
+
+ isSignatureEqual(((IASTSimpleDeclaration)d[0]).getDeclSpecifier(), "int"); //$NON-NLS-1$
+ isSignatureEqual(((IASTSimpleDeclaration)d[1]).getDeclSpecifier(), "int"); //$NON-NLS-1$
+ isSignatureEqual(((IASTSimpleDeclaration)d[2]).getDeclSpecifier(), "void"); //$NON-NLS-1$
+ isSignatureEqual(((IASTSimpleDeclaration)d[3]).getDeclSpecifier(), "static int"); //$NON-NLS-1$
+
+ isTypeEqual(((IASTSimpleDeclaration)d[0]).getDeclarators()[0], "int (int)"); //$NON-NLS-1$
+ isTypeEqual(((IASTSimpleDeclaration)d[1]).getDeclarators()[0], "int * (char, int)"); //$NON-NLS-1$
+ isTypeEqual(((IASTSimpleDeclaration)d[2]).getDeclarators()[0], "void (int *, float * *)"); //$NON-NLS-1$
+ isTypeEqual(((IASTSimpleDeclaration)d[3]).getDeclarators()[0], "int (int * restrict)"); //$NON-NLS-1$
+
+ isTypeEqual(((IFunction)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getName().resolveBinding()).getType(), "int (int)"); //$NON-NLS-1$
+ isTypeEqual(((IFunction)((IASTSimpleDeclaration)d[1]).getDeclarators()[0].getName().resolveBinding()).getType(), "int * (char, int)"); //$NON-NLS-1$
+ isTypeEqual(((IFunction)((IASTSimpleDeclaration)d[2]).getDeclarators()[0].getName().resolveBinding()).getType(), "void (int *, float * *)"); //$NON-NLS-1$
+ isTypeEqual(((IFunction)((IASTSimpleDeclaration)d[3]).getDeclarators()[0].getName().resolveBinding()).getType(), "int (int * restrict)"); //$NON-NLS-1$
+
+ isParameterTypeEqual(((IFunction)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getName().resolveBinding()).getType(), "(int)"); //$NON-NLS-1$
+ isParameterTypeEqual(((IFunction)((IASTSimpleDeclaration)d[1]).getDeclarators()[0].getName().resolveBinding()).getType(), "(char, int)"); //$NON-NLS-1$
+ isParameterTypeEqual(((IFunction)((IASTSimpleDeclaration)d[2]).getDeclarators()[0].getName().resolveBinding()).getType(), "(int *, float * *)"); //$NON-NLS-1$
+ isParameterTypeEqual(((IFunction)((IASTSimpleDeclaration)d[3]).getDeclarators()[0].getName().resolveBinding()).getType(), "(int * restrict)"); //$NON-NLS-1$
+ }
+
+ public void testSimpleCParameterSignature() throws Exception {
+ StringBuffer buff = new StringBuffer();
+ buff.append("int a(int x);\n"); //$NON-NLS-1$
+ buff.append("int * b(char y, int x);\n"); //$NON-NLS-1$
+ buff.append("void c(int * z, float **b);\n"); //$NON-NLS-1$
+ buff.append("static int d(int a[restrict]);\n"); //$NON-NLS-1$
+
+ IASTTranslationUnit tu = parse(buff.toString(), ParserLanguage.C);
+ IASTDeclaration[] d = tu.getDeclarations();
+
+ isParameterSignatureEqual(((IASTSimpleDeclaration)d[0]).getDeclarators()[0], "(int)"); //$NON-NLS-1$
+ isParameterSignatureEqual(((IASTSimpleDeclaration)d[1]).getDeclarators()[0], "(char, int)"); //$NON-NLS-1$
+ isParameterSignatureEqual(((IASTSimpleDeclaration)d[2]).getDeclarators()[0], "(int *, float **)"); //$NON-NLS-1$
+ isParameterSignatureEqual(((IASTSimpleDeclaration)d[3]).getDeclarators()[0], "(int [restrict])"); //$NON-NLS-1$
+ }
+
+ public void testSimpleTypeId() throws Exception {
+ StringBuffer buff = new StringBuffer();
+ buff.append("int x = sizeof( int );\n"); //$NON-NLS-1$
+ buff.append("union Squaw { int x; double u; };\n"); //$NON-NLS-1$
+ buff.append("int main(int argc, char **argv) {\n"); //$NON-NLS-1$
+ buff.append("return sizeof( union Squaw );\n}\n"); //$NON-NLS-1$
+ buff.append("typedef short Z; typedef Z jc;\n"); //$NON-NLS-1$
+ buff.append("int y = 4;\n"); //$NON-NLS-1$
+ buff.append("jc myJc = (jc)y;\n"); //$NON-NLS-1$
+
+ IASTTranslationUnit tu = parse(buff.toString(), ParserLanguage.C);
+ IASTDeclaration[] d = tu.getDeclarations();
+
+ // verify signatures
+ isSignatureEqual( ((IASTTypeIdExpression)((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression()).getTypeId(), "int"); //$NON-NLS-1$
+ isSignatureEqual( ((IASTTypeIdExpression)((IASTReturnStatement)((IASTCompoundStatement)((IASTFunctionDefinition)d[2]).getBody()).getStatements()[0]).getReturnValue()).getTypeId(), "union"); //$NON-NLS-1$
+ isSignatureEqual( ((IASTCastExpression)((IASTInitializerExpression)((IASTSimpleDeclaration)d[6]).getDeclarators()[0].getInitializer()).getExpression()).getTypeId() , "jc"); //$NON-NLS-1$
+
+ // verify types
+ isTypeEqual( ((IASTTypeIdExpression)((IASTInitializerExpression)((IASTSimpleDeclaration)d[0]).getDeclarators()[0].getInitializer()).getExpression()).getTypeId(), "int"); //$NON-NLS-1$
+ isTypeEqual( ((IASTTypeIdExpression)((IASTReturnStatement)((IASTCompoundStatement)((IASTFunctionDefinition)d[2]).getBody()).getStatements()[0]).getReturnValue()).getTypeId(), "union"); //$NON-NLS-1$
+ isTypeEqual( ((IASTCastExpression)((IASTInitializerExpression)((IASTSimpleDeclaration)d[6]).getDeclarators()[0].getInitializer()).getExpression()).getTypeId() , "short"); //$NON-NLS-1$
+ }
+
+}