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

Added toString methods.

This commit is contained in:
Sergey Prigogin 2008-03-08 23:00:47 +00:00
parent 934ea2e952
commit 7cdbcfc0f9
3 changed files with 263 additions and 185 deletions

View file

@ -12,6 +12,9 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp; package org.eclipse.cdt.internal.core.dom.parser.cpp;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.cdt.core.dom.ast.DOMException; import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IField; import org.eclipse.cdt.core.dom.ast.IField;
@ -25,6 +28,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPDeferredTemplateInstance;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPField; import org.eclipse.cdt.core.dom.ast.cpp.ICPPField;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPScope; import org.eclipse.cdt.core.dom.ast.cpp.ICPPScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPSpecialization;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateInstance; import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateInstance;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.parser.util.ObjectMap; import org.eclipse.cdt.core.parser.util.ObjectMap;
@ -201,6 +205,63 @@ public class CPPClassInstance extends CPPInstance implements ICPPClassType, ICPP
return ICPPMethod.EMPTY_CPPMETHOD_ARRAY; return ICPPMethod.EMPTY_CPPMETHOD_ARRAY;
} }
/**
* Returns a combined argument map of this class and all its base template classes.
* This combined map helps with instantiation of members of template classes that subclass
* other template classes (see AST2TemplateTests#testRebindPattern_214017_2()).
*/
@Override
public ObjectMap getArgumentMap() {
ObjectMap argMap = argumentMap;
List<ICPPSpecialization> bases = null;
try {
for (ICPPBase base : getBases()) {
IBinding baseClass = base.getBaseClass();
if (baseClass instanceof ICPPSpecialization) {
if (bases == null) {
bases = new ArrayList<ICPPSpecialization>();
}
bases.add((ICPPSpecialization) baseClass);
}
}
if (bases != null) {
for (int i = 0; i < bases.size(); i++) {
for (ICPPBase base : ((ICPPClassType) bases.get(i)).getBases()) {
IBinding baseClass = base.getBaseClass();
if (baseClass instanceof ICPPSpecialization) {
bases.add((ICPPSpecialization) baseClass);
}
}
if (bases.size() > 20) { // Protect against cyclic inheritance.
break;
}
}
}
} catch (DOMException e) {
// Ignore
}
if (bases != null) {
for (ICPPSpecialization base : bases) {
// Protect against infinite recursion.
ObjectMap baseArgMap = base instanceof CPPClassInstance ?
((CPPClassInstance) base).argumentMap : base.getArgumentMap();
if (!baseArgMap.isEmpty()) {
if (argMap == argumentMap) {
argMap = (ObjectMap) argumentMap.clone();
}
for (int i = 0; i < baseArgMap.size(); i++) {
Object key = baseArgMap.keyAt(i);
if (!argMap.containsKey(key)) {
argMap.put(key, baseArgMap.getAt(i));
}
}
}
}
}
return argMap;
}
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
return obj instanceof ICPPClassType && isSameType((ICPPClassType) obj); return obj instanceof ICPPClassType && isSameType((ICPPClassType) obj);

View file

@ -12,6 +12,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.ILinkage; import org.eclipse.cdt.core.dom.ILinkage;
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.IASTDeclSpecifier; import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
@ -122,11 +123,11 @@ public class CPPFunction extends PlatformObject implements ICPPFunction, ICPPInt
if ((bits & (FULLY_RESOLVED | RESOLUTION_IN_PROGRESS)) == 0) { if ((bits & (FULLY_RESOLVED | RESOLUTION_IN_PROGRESS)) == 0) {
bits |= RESOLUTION_IN_PROGRESS; bits |= RESOLUTION_IN_PROGRESS;
IASTTranslationUnit tu = null; IASTTranslationUnit tu = null;
if( definition != null ) if (definition != null) {
tu = definition.getTranslationUnit(); tu = definition.getTranslationUnit();
else if( declarations != null ) } else if (declarations != null) {
tu = declarations[0].getTranslationUnit(); tu = declarations[0].getTranslationUnit();
else { } else {
//implicit binding //implicit binding
IScope scope = getScope(); IScope scope = getScope();
try { try {
@ -140,7 +141,8 @@ public class CPPFunction extends PlatformObject implements ICPPFunction, ICPPInt
if (tu != null) { if (tu != null) {
CPPVisitor.getDeclarations(tu, this); CPPVisitor.getDeclarations(tu, this);
} }
declarations = (ICPPASTFunctionDeclarator[]) ArrayUtil.trim( ICPPASTFunctionDeclarator.class, declarations ); declarations = (ICPPASTFunctionDeclarator[]) ArrayUtil.trim(ICPPASTFunctionDeclarator.class,
declarations);
bits |= FULLY_RESOLVED; bits |= FULLY_RESOLVED;
bits &= ~RESOLUTION_IN_PROGRESS; bits &= ~RESOLUTION_IN_PROGRESS;
} }
@ -183,11 +185,13 @@ public class CPPFunction extends PlatformObject implements ICPPFunction, ICPPInt
return; return;
} }
//keep the lowest offset declaration in [0] // Keep the lowest offset declaration in [0]
if (declarations.length > 0 && ((ASTNode)node).getOffset() < ((ASTNode)declarations[0]).getOffset()) { if (declarations.length > 0 && ((ASTNode)node).getOffset() < ((ASTNode)declarations[0]).getOffset()) {
declarations = (ICPPASTFunctionDeclarator[]) ArrayUtil.prepend( ICPPASTFunctionDeclarator.class, declarations, dtor ); declarations = (ICPPASTFunctionDeclarator[]) ArrayUtil.prepend(ICPPASTFunctionDeclarator.class,
declarations, dtor);
} else { } else {
declarations = (ICPPASTFunctionDeclarator[]) ArrayUtil.append( ICPPASTFunctionDeclarator.class, declarations, dtor ); declarations = (ICPPASTFunctionDeclarator[]) ArrayUtil.append(ICPPASTFunctionDeclarator.class,
declarations, dtor);
} }
} }
@ -221,7 +225,8 @@ public class CPPFunction extends PlatformObject implements ICPPFunction, ICPPInt
result[i]= (IParameter) binding; result[i]= (IParameter) binding;
} }
else { else {
result[i] = new CPPParameter.CPPParameterProblem(p, IProblemBinding.SEMANTIC_INVALID_TYPE, name.toCharArray()); result[i] = new CPPParameter.CPPParameterProblem(p, IProblemBinding.SEMANTIC_INVALID_TYPE,
name.toCharArray());
} }
} }
} }
@ -392,7 +397,7 @@ public class CPPFunction extends PlatformObject implements ICPPFunction, ICPPInt
} }
return hasStorageClass(this, IASTDeclSpecifier.sc_static); return hasStorageClass(this, IASTDeclSpecifier.sc_static);
} }
// }
// static public boolean isStatic // static public boolean isStatic
// //2 state bits, most significant = whether or not we've figure this out yet // //2 state bits, most significant = whether or not we've figure this out yet
// //least significant = whether or not we are static // //least significant = whether or not we are static
@ -463,19 +468,20 @@ public class CPPFunction extends PlatformObject implements ICPPFunction, ICPPInt
parent = parent.getParent(); parent = parent.getParent();
IASTDeclSpecifier declSpec = null; IASTDeclSpecifier declSpec = null;
if( parent instanceof IASTSimpleDeclaration ) if (parent instanceof IASTSimpleDeclaration) {
declSpec = ((IASTSimpleDeclaration)parent).getDeclSpecifier(); declSpec = ((IASTSimpleDeclaration)parent).getDeclSpecifier();
else if( parent instanceof IASTFunctionDefinition ) } else if (parent instanceof IASTFunctionDefinition) {
declSpec = ((IASTFunctionDefinition)parent).getDeclSpecifier(); declSpec = ((IASTFunctionDefinition)parent).getDeclSpecifier();
}
if (declSpec.getStorageClass() == storage) { if (declSpec.getStorageClass() == storage) {
return true; return true;
} }
} }
if (ds != null && ++i < ds.length) { if (ds != null && ++i < ds.length) {
dtor = (ICPPASTFunctionDeclarator) ds[i]; dtor = (ICPPASTFunctionDeclarator) ds[i];
} } else {
else
break; break;
}
} while (dtor != null); } while (dtor != null);
return false; return false;
} }
@ -574,4 +580,9 @@ public class CPPFunction extends PlatformObject implements ICPPFunction, ICPPInt
public ILinkage getLinkage() { public ILinkage getLinkage() {
return Linkage.CPP_LINKAGE; return Linkage.CPP_LINKAGE;
} }
@Override
public String toString() {
return getName() + ASTTypeUtil.getParameterTypeString(getType()); //$NON-NLS-1$
}
} }

View file

@ -103,9 +103,9 @@ public class CPPParameter extends PlatformObject implements ICPPParameter, ICPPI
if (!(node instanceof IASTName)) if (!(node instanceof IASTName))
return; return;
IASTName name = (IASTName) node; IASTName name = (IASTName) node;
if( declarations == null ) if (declarations == null) {
declarations = new IASTName[] { name }; declarations = new IASTName[] { name };
else { } else {
//keep the lowest offset declaration in[0] //keep the lowest offset declaration in[0]
if (declarations.length > 0 && ((ASTNode)node).getOffset() < ((ASTNode)declarations[0]).getOffset()) { if (declarations.length > 0 && ((ASTNode)node).getOffset() < ((ASTNode)declarations[0]).getOffset()) {
declarations = (IASTName[]) ArrayUtil.prepend(IASTName.class, declarations, name); declarations = (IASTName[]) ArrayUtil.prepend(IASTName.class, declarations, name);
@ -288,4 +288,10 @@ public class CPPParameter extends PlatformObject implements ICPPParameter, ICPPI
public boolean isExternC() { public boolean isExternC() {
return false; return false;
} }
@Override
public String toString() {
String name = getName();
return name.length() != 0 ? name : "<unnamed>"; //$NON-NLS-1$
}
} }