1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-14 11:45:38 +02:00

Pretty printing qualified names.

This commit is contained in:
John Camelon 2005-02-08 14:59:35 +00:00
parent cc8d114f90
commit e20bdb8813
2 changed files with 128 additions and 119 deletions

View file

@ -17,7 +17,8 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
/** /**
* @author jcamelon * @author jcamelon
*/ */
public class CPPASTQualifiedName extends CPPASTNode implements ICPPASTQualifiedName { public class CPPASTQualifiedName extends CPPASTNode implements
ICPPASTQualifiedName {
/** /**
* @param duple * @param duple
@ -25,54 +26,43 @@ public class CPPASTQualifiedName extends CPPASTNode implements ICPPASTQualifiedN
public CPPASTQualifiedName() { public CPPASTQualifiedName() {
} }
/* (non-Javadoc) /*
* (non-Javadoc)
*
* @see org.eclipse.cdt.core.dom.ast.IASTName#resolveBinding() * @see org.eclipse.cdt.core.dom.ast.IASTName#resolveBinding()
*/ */
public IBinding resolveBinding() { public IBinding resolveBinding() {
//The full qualified name resolves to the same thing as the last name //The full qualified name resolves to the same thing as the last name
removeNullNames(); removeNullNames();
return names[ names.length - 1 ].resolveBinding(); return names[names.length - 1].resolveBinding();
} }
/*
* (non-Javadoc)
/* (non-Javadoc) *
* @see java.lang.Object#toString() * @see java.lang.Object#toString()
*/ */
public String toString() { public String toString() {
if( names == null ) return null; return signature;
removeNullNames();
StringBuffer buffer = new StringBuffer();
for( int i = 0; i < names.length; ++i )
{
String n = names[i].toString();
if( n == null )
return null;
buffer.append( n );
if( i != names.length - 1 )
buffer.append( "::"); //$NON-NLS-1$
}
return buffer.toString();
} }
/*
/* (non-Javadoc) * (non-Javadoc)
*
* @see org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName#addName(org.eclipse.cdt.core.dom.ast.IASTName) * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName#addName(org.eclipse.cdt.core.dom.ast.IASTName)
*/ */
public void addName(IASTName name) { public void addName(IASTName name) {
if( names == null ) if (names == null) {
{ names = new IASTName[DEFAULT_NAMES_LIST_SIZE];
names = new IASTName[ DEFAULT_NAMES_LIST_SIZE ];
currentIndex = 0; currentIndex = 0;
} }
if( names.length == currentIndex ) if (names.length == currentIndex) {
{ IASTName[] old = names;
IASTName [] old = names; names = new IASTName[old.length * 2];
names = new IASTName[ old.length * 2 ]; for (int i = 0; i < old.length; ++i)
for( int i = 0; i < old.length; ++i )
names[i] = old[i]; names[i] = old[i];
} }
names[ currentIndex++ ] = name; names[currentIndex++] = name;
} }
/** /**
@ -80,58 +70,65 @@ public class CPPASTQualifiedName extends CPPASTNode implements ICPPASTQualifiedN
*/ */
private void removeNullNames() { private void removeNullNames() {
int nullCount = 0; int nullCount = 0;
for( int i = 0; i < names.length; ++i ) for (int i = 0; i < names.length; ++i)
if( names[i] == null ) if (names[i] == null)
++nullCount; ++nullCount;
if( nullCount == 0 ) return; if (nullCount == 0)
IASTName [] old = names; return;
IASTName[] old = names;
int newSize = old.length - nullCount; int newSize = old.length - nullCount;
names = new IASTName[ newSize ]; names = new IASTName[newSize];
for( int i = 0; i < newSize; ++i ) for (int i = 0; i < newSize; ++i)
names[i] = old[i]; names[i] = old[i];
currentIndex = newSize; currentIndex = newSize;
} }
private int currentIndex = 0; private int currentIndex = 0;
private IASTName [] names = null; private IASTName[] names = null;
private static final int DEFAULT_NAMES_LIST_SIZE = 4; private static final int DEFAULT_NAMES_LIST_SIZE = 4;
private boolean value; private boolean value;
private String signature;
/* (non-Javadoc) /*
* (non-Javadoc)
*
* @see org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName#getNames() * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName#getNames()
*/ */
public IASTName[] getNames() { public IASTName[] getNames() {
if( names == null ) return IASTName.EMPTY_NAME_ARRAY; if (names == null)
return IASTName.EMPTY_NAME_ARRAY;
removeNullNames(); removeNullNames();
return names; return names;
} }
/* (non-Javadoc) /*
* (non-Javadoc)
*
* @see org.eclipse.cdt.core.dom.ast.IASTName#toCharArray() * @see org.eclipse.cdt.core.dom.ast.IASTName#toCharArray()
*/ */
public char[] toCharArray() { public char[] toCharArray() {
if( names == null ) return null; if (names == null)
return null;
removeNullNames(); removeNullNames();
//count first //count first
int len = 0; int len = 0;
for( int i = 0; i < names.length; ++i ) for (int i = 0; i < names.length; ++i) {
{ char[] n = names[i].toCharArray();
char [] n = names[i].toCharArray(); if (n == null)
if( n == null )
return null; return null;
len += n.length; len += n.length;
if( i != names.length - 1 ) if (i != names.length - 1)
len += 2; len += 2;
} }
char [] nameArray = new char[ len ]; char[] nameArray = new char[len];
int pos = 0; int pos = 0;
for( int i = 0; i < names.length; i++ ){ for (int i = 0; i < names.length; i++) {
char [] n = names[i].toCharArray(); char[] n = names[i].toCharArray();
System.arraycopy( n, 0, nameArray, pos, n.length ); System.arraycopy(n, 0, nameArray, pos, n.length);
pos += n.length; pos += n.length;
if( i != names.length - 1 ){ if (i != names.length - 1) {
nameArray[pos++] = ':'; nameArray[pos++] = ':';
nameArray[pos++] = ':'; nameArray[pos++] = ':';
} }
@ -139,19 +136,30 @@ public class CPPASTQualifiedName extends CPPASTNode implements ICPPASTQualifiedN
return nameArray; return nameArray;
} }
/* (non-Javadoc) /*
* (non-Javadoc)
*
* @see org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName#isFullyQualified() * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName#isFullyQualified()
*/ */
public boolean isFullyQualified() { public boolean isFullyQualified() {
return value; return value;
} }
/* (non-Javadoc) /*
* (non-Javadoc)
*
* @see org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName#setFullyQualified(boolean) * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName#setFullyQualified(boolean)
*/ */
public void setFullyQualified(boolean value) { public void setFullyQualified(boolean value) {
this.value = value; this.value = value;
} }
/**
* @param string
*/
public void setValue(String string) {
this.signature = string;
}
} }

View file

@ -2486,6 +2486,7 @@ public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
CPPASTQualifiedName result = new CPPASTQualifiedName(); CPPASTQualifiedName result = new CPPASTQualifiedName();
result.setOffsetAndLength(duple.getStartOffset(), duple.getEndOffset() result.setOffsetAndLength(duple.getStartOffset(), duple.getEndOffset()
- duple.getStartOffset()); - duple.getStartOffset());
result.setValue( duple.toString() );
ITokenDuple[] segments = duple.getSegments(); ITokenDuple[] segments = duple.getSegments();
int startingValue = 0; int startingValue = 0;
if( segments.length > 0 && if( segments.length > 0 &&