1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-09 02:36:01 +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
*/
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$
@ -75,7 +74,8 @@ public class ASTTypeUtil {
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);
if (i < parms.length - 1)
result.append(COMMA_SPACE);
}
}
result.append(Keywords.cpRPAREN);
@ -93,7 +93,8 @@ public class ASTTypeUtil {
for (int i = 0; i < types.length; i++) {
if (types[i] != null) {
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();
@ -109,7 +110,9 @@ public class ASTTypeUtil {
IType[] parms = null;
try {
parms = type.getParameterTypes();
} catch (DOMException e) { return EMPTY_STRING_ARRAY; }
} catch (DOMException e) {
return EMPTY_STRING_ARRAY;
}
String[] result = new String[parms.length];
@ -130,33 +133,83 @@ public class ASTTypeUtil {
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) {}
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 (((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; }
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; }
if (((IGPPBasicType) type).isLongLong()) {
if (needSpace) {
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()) {
case IGPPBasicType.t_typeof:
result.append(GCCKeywords.TYPEOF);
break;
}
} catch (DOMException e) {}
} catch (DOMException e) {
}
} else if (type instanceof ICPPBasicType) {
try {
switch (((ICPPBasicType) type).getType()) {
@ -167,18 +220,30 @@ public class ASTTypeUtil {
result.append(Keywords.WCHAR_T);
break;
}
} catch (DOMException e) {}
} catch (DOMException e) {
}
} else if (type instanceof ICBasicType) {
try {
if (((ICBasicType)type).isComplex()) { 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; }
if (((ICBasicType) type).isComplex()) {
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()) {
case ICBasicType.t_Bool:
result.append(Keywords.c_BOOL);
break;
}
} catch (DOMException e) {}
} catch (DOMException e) {
}
}
try {
@ -204,8 +269,8 @@ public class ASTTypeUtil {
result.append(Keywords.VOID);
break;
}
} catch (DOMException e) {}
} catch (DOMException e) {
}
} else if (type instanceof ICompositeType) {
// 101114 fix, do not display class, and for consistency don't display struct/union as well
// if (type instanceof ICPPClassType) {
@ -233,7 +298,7 @@ public class ASTTypeUtil {
try {
String qn = CPPVisitor.renderQualifiedName(getQualifiedNameForAnonymous((ICPPClassType) type));
result.append(qn);
} catch(DOMException de) {
} catch (DOMException e) {
result.append(getNameForAnonymous((ICompositeType) type));
}
} else {
@ -252,40 +317,79 @@ public class ASTTypeUtil {
} 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; }
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) {}
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; }
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; }
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) {}
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; }
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; }
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) {}
if (((IQualifierType) type).isConst()) {
if (needSpace) {
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());
}
@ -321,32 +425,26 @@ public class ASTTypeUtil {
}
if (!resolveTypedefs && isTypedef) {
type= null; // stop here
}
else if (type instanceof ITypeContainer) {
} else if (type instanceof ITypeContainer) {
try {
type = ((ITypeContainer) type).getType();
} catch (DOMException e) {
type= null;
}
}
else {
} else {
type= null;
}
}
// 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.length() > 0) result.append(SPACE); // only add a space if this is not the first type being added
if (types[j] != null) {
if (j > 0 && types[j - 1] instanceof IQualifierType) {
result.append(getTypeString(types[j-1]));
result.append(SPACE);
result.append(getTypeString(types[j]));
smartAppend(result, getTypeString(types[j - 1]));
smartAppend(result, getTypeString(types[j]));
--j;
}
else {
result.append(getTypeString(types[j]));
} else {
smartAppend(result, getTypeString(types[j]));
}
}
}
@ -354,6 +452,13 @@ public class ASTTypeUtil {
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.
*
@ -522,8 +627,7 @@ public class ASTTypeUtil {
}
result.addFirst(new String(name));
}
}
else {
} else {
result.addFirst(new String(name));
}
}

View file

@ -14,6 +14,7 @@
*/
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.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef;
@ -72,4 +73,9 @@ public class CPPReferenceType implements ICPPReferenceType, ITypeContainer {
}
return t;
}
@Override
public String toString() {
return ASTTypeUtil.getType(this);
}
}