1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-09 10:46:02 +02:00

toString method.

This commit is contained in:
Sergey Prigogin 2008-03-17 00:12:41 +00:00
parent ce309040bd
commit e1c98f2c02
2 changed files with 229 additions and 119 deletions

View file

@ -51,7 +51,6 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding;
* @author dsteffle * @author dsteffle
*/ */
public class ASTTypeUtil { public class ASTTypeUtil {
private static final String COMMA_SPACE = ", "; //$NON-NLS-1$ private static final String COMMA_SPACE = ", "; //$NON-NLS-1$
private static final String EMPTY_STRING = ""; //$NON-NLS-1$ private static final String EMPTY_STRING = ""; //$NON-NLS-1$
private static final String SPACE = " "; //$NON-NLS-1$ private static final String SPACE = " "; //$NON-NLS-1$
@ -75,7 +74,8 @@ public class ASTTypeUtil {
for (int i = 0; i < parms.length; i++) { for (int i = 0; i < parms.length; i++) {
if (parms[i] != null) { if (parms[i] != null) {
result.append(parms[i]); result.append(parms[i]);
if (i<parms.length-1) result.append(COMMA_SPACE); if (i < parms.length - 1)
result.append(COMMA_SPACE);
} }
} }
result.append(Keywords.cpRPAREN); result.append(Keywords.cpRPAREN);
@ -93,7 +93,8 @@ public class ASTTypeUtil {
for (int i = 0; i < types.length; i++) { for (int i = 0; i < types.length; i++) {
if (types[i] != null) { if (types[i] != null) {
result.append(getTypeString(types[i])); result.append(getTypeString(types[i]));
if (i<types.length-1) result.append(COMMA_SPACE); if (i < types.length - 1)
result.append(COMMA_SPACE);
} }
} }
return result.toString(); return result.toString();
@ -109,7 +110,9 @@ public class ASTTypeUtil {
IType[] parms = null; IType[] parms = null;
try { try {
parms = type.getParameterTypes(); parms = type.getParameterTypes();
} catch (DOMException e) { return EMPTY_STRING_ARRAY; } } catch (DOMException e) {
return EMPTY_STRING_ARRAY;
}
String[] result = new String[parms.length]; String[] result = new String[parms.length];
@ -130,33 +133,83 @@ public class ASTTypeUtil {
result.append(Keywords.cpLBRACKET); result.append(Keywords.cpLBRACKET);
if (type instanceof ICArrayType) { if (type instanceof ICArrayType) {
try { try {
if (((ICArrayType)type).isConst()) { result.append(Keywords.CONST); needSpace=true; } if (((ICArrayType) type).isConst()) {
if (((ICArrayType)type).isRestrict()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.RESTRICT); needSpace=true; } result.append(Keywords.CONST); 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); } if (((ICArrayType) type).isRestrict()) {
} catch (DOMException e) {} 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); result.append(Keywords.cpRBRACKET);
} else if (type instanceof IBasicType) { } else if (type instanceof IBasicType) {
try { try {
if (((IBasicType)type).isSigned()) { result.append(Keywords.SIGNED); needSpace = true; } if (((IBasicType) type).isSigned()) {
else if (((IBasicType)type).isUnsigned()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.UNSIGNED); needSpace=true; } result.append(Keywords.SIGNED); needSpace = true;
if (((IBasicType)type).isLong()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.LONG); needSpace = true; } } else if (((IBasicType) type).isUnsigned()) {
else if (((IBasicType)type).isShort()) { if (needSpace) { result.append(SPACE); needSpace=false; }result.append(Keywords.SHORT); needSpace = true; } if (needSpace) {
} catch (DOMException e) {} 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) { if (type instanceof IGPPBasicType) {
try { try {
if (((IGPPBasicType)type).isLongLong()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.LONG_LONG); needSpace=true; } if (((IGPPBasicType) type).isLongLong()) {
if (((IGPPBasicType)type).isComplex()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.c_COMPLEX); needSpace=true; } if (needSpace) {
if (((IGPPBasicType)type).isImaginary()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.c_IMAGINARY); needSpace=true; } result.append(SPACE); needSpace = false;
}
result.append(Keywords.LONG_LONG); needSpace = true;
}
if (((IGPPBasicType) type).isComplex()) {
if (needSpace) {
result.append(SPACE); needSpace = false;
}
result.append(Keywords.c_COMPLEX); needSpace = true;
}
if (((IGPPBasicType) type).isImaginary()) {
if (needSpace) {
result.append(SPACE); needSpace = false;
}
result.append(Keywords.c_IMAGINARY); needSpace = true;
}
switch (((IGPPBasicType) type).getType()) { switch (((IGPPBasicType) type).getType()) {
case IGPPBasicType.t_typeof: case IGPPBasicType.t_typeof:
result.append(GCCKeywords.TYPEOF); result.append(GCCKeywords.TYPEOF);
break; break;
} }
} catch (DOMException e) {} } catch (DOMException e) {
}
} else if (type instanceof ICPPBasicType) { } else if (type instanceof ICPPBasicType) {
try { try {
switch (((ICPPBasicType) type).getType()) { switch (((ICPPBasicType) type).getType()) {
@ -167,18 +220,30 @@ public class ASTTypeUtil {
result.append(Keywords.WCHAR_T); result.append(Keywords.WCHAR_T);
break; break;
} }
} catch (DOMException e) {} } catch (DOMException e) {
}
} else if (type instanceof ICBasicType) { } else if (type instanceof ICBasicType) {
try { try {
if (((ICBasicType)type).isComplex()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.c_COMPLEX); needSpace=true; } if (((ICBasicType) type).isComplex()) {
if (((ICBasicType)type).isImaginary()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.c_IMAGINARY); needSpace=true; } if (needSpace) {
result.append(SPACE); needSpace = false;
}
result.append(Keywords.c_COMPLEX); needSpace = true;
}
if (((ICBasicType) type).isImaginary()) {
if (needSpace) {
result.append(SPACE); needSpace = false;
}
result.append(Keywords.c_IMAGINARY); needSpace = true;
}
switch (((ICBasicType) type).getType()) { switch (((ICBasicType) type).getType()) {
case ICBasicType.t_Bool: case ICBasicType.t_Bool:
result.append(Keywords.c_BOOL); result.append(Keywords.c_BOOL);
break; break;
} }
} catch (DOMException e) {} } catch (DOMException e) {
}
} }
try { try {
@ -204,8 +269,8 @@ public class ASTTypeUtil {
result.append(Keywords.VOID); result.append(Keywords.VOID);
break; break;
} }
} catch (DOMException e) {} } catch (DOMException e) {
}
} else if (type instanceof ICompositeType) { } else if (type instanceof ICompositeType) {
// 101114 fix, do not display class, and for consistency don't display struct/union as well // 101114 fix, do not display class, and for consistency don't display struct/union as well
// if (type instanceof ICPPClassType) { // if (type instanceof ICPPClassType) {
@ -233,7 +298,7 @@ public class ASTTypeUtil {
try { try {
String qn = CPPVisitor.renderQualifiedName(getQualifiedNameForAnonymous((ICPPClassType) type)); String qn = CPPVisitor.renderQualifiedName(getQualifiedNameForAnonymous((ICPPClassType) type));
result.append(qn); result.append(qn);
} catch(DOMException de) { } catch (DOMException e) {
result.append(getNameForAnonymous((ICompositeType) type)); result.append(getNameForAnonymous((ICompositeType) type));
} }
} else { } else {
@ -252,40 +317,79 @@ public class ASTTypeUtil {
} else if (type instanceof IFunctionType) { } else if (type instanceof IFunctionType) {
try { try {
String temp = getType(((IFunctionType) type).getReturnType()); String temp = getType(((IFunctionType) type).getReturnType());
if (temp != null && !temp.equals(EMPTY_STRING)) { result.append(temp); needSpace=true; } if (temp != null && !temp.equals(EMPTY_STRING)) {
if (needSpace) { result.append(SPACE); needSpace=false; } result.append(temp); needSpace = true;
}
if (needSpace) {
result.append(SPACE); needSpace = false;
}
temp = getParameterTypeString((IFunctionType) type); temp = getParameterTypeString((IFunctionType) type);
if (temp != null && !temp.equals(EMPTY_STRING)) { result.append(temp); needSpace=false; } if (temp != null && !temp.equals(EMPTY_STRING)) {
} catch (DOMException e) {} result.append(temp); needSpace = false;
}
} catch (DOMException e) {
}
} else if (type instanceof IPointerType) { } else if (type instanceof IPointerType) {
result.append(Keywords.cpSTAR); needSpace = true; result.append(Keywords.cpSTAR); needSpace = true;
if (type instanceof IGPPPointerType) { if (type instanceof IGPPPointerType) {
if (((IGPPPointerType)type).isRestrict()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.RESTRICT); needSpace=true; } if (((IGPPPointerType) type).isRestrict()) {
if (needSpace) {
result.append(SPACE); needSpace = false;
}
result.append(Keywords.RESTRICT); needSpace = true;
}
} else if (type instanceof ICPointerType) { } else if (type instanceof ICPointerType) {
if (((ICPointerType)type).isRestrict()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.RESTRICT); needSpace=true; } if (((ICPointerType) type).isRestrict()) {
if (needSpace) {
result.append(SPACE); needSpace = false;
}
result.append(Keywords.RESTRICT); needSpace = true;
}
} }
try { try {
if (((IPointerType)type).isConst()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.CONST); needSpace=true; } if (((IPointerType) type).isConst()) {
if (((IPointerType)type).isVolatile()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.VOLATILE); needSpace=true; } if (needSpace) {
} catch (DOMException e) {} 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) { } else if (type instanceof IQualifierType) {
if (type instanceof ICQualifierType) { if (type instanceof ICQualifierType) {
if (((ICQualifierType)type).isRestrict()) { result.append(Keywords.RESTRICT); needSpace=true; } if (((ICQualifierType) type).isRestrict()) {
result.append(Keywords.RESTRICT); needSpace = true;
}
} else if (type instanceof IGPPQualifierType) { } else if (type instanceof IGPPQualifierType) {
if (((IGPPQualifierType)type).isRestrict()) { result.append(Keywords.RESTRICT); needSpace=true; } if (((IGPPQualifierType) type).isRestrict()) {
result.append(Keywords.RESTRICT); needSpace = true;
}
} }
try { try {
if (((IQualifierType)type).isConst()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.CONST); needSpace=true; } if (((IQualifierType) type).isConst()) {
if (((IQualifierType)type).isVolatile()) { if (needSpace) { result.append(SPACE); needSpace=false; } result.append(Keywords.VOLATILE); needSpace=true; } if (needSpace) {
} catch (DOMException e) {} result.append(SPACE); needSpace = false;
} }
else if (type instanceof ITypedef) { 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) {
}
} else if (type instanceof ITypedef) {
result.append(((ITypedef) type).getNameCharArray()); result.append(((ITypedef) type).getNameCharArray());
} }
@ -321,32 +425,26 @@ public class ASTTypeUtil {
} }
if (!resolveTypedefs && isTypedef) { if (!resolveTypedefs && isTypedef) {
type= null; // stop here type= null; // stop here
} } else if (type instanceof ITypeContainer) {
else if (type instanceof ITypeContainer) {
try { try {
type = ((ITypeContainer) type).getType(); type = ((ITypeContainer) type).getType();
} catch (DOMException e) { } catch (DOMException e) {
type= null; type= null;
} }
} } else {
else {
type= null; type= null;
} }
} }
// pop all of the types off of the stack, and build the string representation while doing so // 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--) { for (int j = types.length - 1; j >= 0; j--) {
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) { if (types[j] != null) {
if (j > 0 && types[j - 1] instanceof IQualifierType) { if (j > 0 && types[j - 1] instanceof IQualifierType) {
result.append(getTypeString(types[j-1])); smartAppend(result, getTypeString(types[j - 1]));
result.append(SPACE); smartAppend(result, getTypeString(types[j]));
result.append(getTypeString(types[j]));
--j; --j;
} } else {
else { smartAppend(result, getTypeString(types[j]));
result.append(getTypeString(types[j]));
} }
} }
} }
@ -354,6 +452,13 @@ public class ASTTypeUtil {
return result.toString(); return result.toString();
} }
private static void smartAppend(StringBuilder buf, String str) {
if (buf.length() > 0 && str.length() > 0 && "&*".indexOf(str.charAt(0)) < 0) { //$NON-NLS-1$
buf.append(SPACE);
}
buf.append(str);
}
/** /**
* Returns the type representation of the declarator (including parameters) as a String. * Returns the type representation of the declarator (including parameters) as a String.
* *
@ -522,8 +627,7 @@ public class ASTTypeUtil {
} }
result.addFirst(new String(name)); result.addFirst(new String(name));
} }
} } else {
else {
result.addFirst(new String(name)); result.addFirst(new String(name));
} }
} }

View file

@ -14,6 +14,7 @@
*/ */
package org.eclipse.cdt.internal.core.dom.parser.cpp; package org.eclipse.cdt.internal.core.dom.parser.cpp;
import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef; import org.eclipse.cdt.core.dom.ast.ITypedef;
@ -72,4 +73,9 @@ public class CPPReferenceType implements ICPPReferenceType, ITypeContainer {
} }
return t; return t;
} }
@Override
public String toString() {
return ASTTypeUtil.getType(this);
}
} }