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

changes to help reduce the number of arrays used in resolving bindings

This commit is contained in:
Andrew Niefer 2005-06-24 21:05:28 +00:00
parent 2b8dec03b8
commit a0dd64276b
6 changed files with 53 additions and 46 deletions

View file

@ -15,6 +15,7 @@
*/ */
package org.eclipse.cdt.core.parser.util; package org.eclipse.cdt.core.parser.util;
import java.lang.reflect.Array;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -141,4 +142,10 @@ public abstract class ObjectTable extends HashTable implements Cloneable{
System.arraycopy( keyTable, 0, keys, 0, keys.length ); System.arraycopy( keyTable, 0, keys, 0, keys.length );
return keys; return keys;
} }
public Object [] keyArray( Class c ){
Object [] keys = (Object[]) Array.newInstance( c, size() );
System.arraycopy( keyTable, 0, keys, 0, keys.length );
return keys;
}
} }

View file

@ -57,7 +57,7 @@ public class CPPClassInstanceScope implements ICPPClassScope {
} }
public boolean isFullyCached(){ public boolean isFullyCached(){
if( !isFullyCached ){ if( !isFullyCached ){
CPPSemantics.LookupData data = new CPPSemantics.LookupData( CPPSemantics.EMPTY_NAME_ARRAY ); CPPSemantics.LookupData data = new CPPSemantics.LookupData();
try { try {
CPPSemantics.lookupInScope( data, this, null ); CPPSemantics.lookupInScope( data, this, null );
} catch (DOMException e) { } catch (DOMException e) {

View file

@ -564,7 +564,7 @@ public class CPPClassType implements ICPPClassType, ICPPInternalClassType {
* @see org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType#getMethods() * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType#getMethods()
*/ */
public ICPPMethod[] getMethods() throws DOMException { public ICPPMethod[] getMethods() throws DOMException {
ObjectSet set = new ObjectSet(2); ObjectSet set = new ObjectSet(4);
ICPPMethod [] ms = getDeclaredMethods(); ICPPMethod [] ms = getDeclaredMethods();
set.addAll( ms ); set.addAll( ms );
ICPPClassScope scope = (ICPPClassScope) getCompositeScope(); ICPPClassScope scope = (ICPPClassScope) getCompositeScope();
@ -573,7 +573,7 @@ public class CPPClassType implements ICPPClassType, ICPPInternalClassType {
for ( int i = 0; i < bases.length; i++ ) { for ( int i = 0; i < bases.length; i++ ) {
set.addAll( bases[i].getBaseClass().getMethods() ); set.addAll( bases[i].getBaseClass().getMethods() );
} }
return (ICPPMethod[]) ArrayUtil.trim( ICPPMethod.class, set.keyArray(), true ); return (ICPPMethod[]) set.keyArray( ICPPMethod.class );
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -733,7 +733,7 @@ public class CPPClassType implements ICPPClassType, ICPPInternalClassType {
} }
} }
return (IBinding[]) ArrayUtil.trim( IBinding.class, resultSet.keyArray(), true ); return (IBinding[]) resultSet.keyArray( IBinding.class );
} }
/* (non-Javadoc) /* (non-Javadoc)

View file

@ -333,7 +333,7 @@ public class CPPNamespace implements ICPPNamespace, ICPPInternalBinding {
} }
} }
} }
return (IBinding[]) ArrayUtil.trim( IBinding.class, collector.members.keyArray(), true ); return (IBinding[]) collector.members.keyArray( IBinding.class );
} }
return IBinding.EMPTY_BINDING_ARRAY; return IBinding.EMPTY_BINDING_ARRAY;
} }

View file

@ -90,20 +90,21 @@ abstract public class CPPScope implements ICPPScope{
Object obj = bindings.get( c ); Object obj = bindings.get( c );
if( obj != null ){ if( obj != null ){
if( obj instanceof ObjectSet ) { if( obj instanceof ObjectSet ) {
ObjectSet os = (ObjectSet) obj;
if( forceResolve ) if( forceResolve )
return CPPSemantics.resolveAmbiguities( name, ((ObjectSet) obj).keyArray() ); return CPPSemantics.resolveAmbiguities( name, os.keyArray() );
IBinding [] bs = null; IBinding [] bs = null;
Object [] os = ((ObjectSet) obj).keyArray(); for( int i = 0; i < os.size(); i++ ){
for( int i = 0; i < os.length; i++ ){ Object o = os.keyAt( i );
if( os[i] instanceof IASTName ){ if( o instanceof IASTName ){
IASTName n = (IASTName) os[i]; IASTName n = (IASTName) o;
if( n instanceof ICPPASTQualifiedName ){ if( n instanceof ICPPASTQualifiedName ){
IASTName [] ns = ((ICPPASTQualifiedName)n).getNames(); IASTName [] ns = ((ICPPASTQualifiedName)n).getNames();
n = ns[ ns.length - 1 ]; n = ns[ ns.length - 1 ];
} }
bs = (IBinding[]) ArrayUtil.append( IBinding.class, bs, n.getBinding() ); bs = (IBinding[]) ArrayUtil.append( IBinding.class, bs, n.getBinding() );
} else } else
bs = (IBinding[]) ArrayUtil.append( IBinding.class, bs, os[i] ); bs = (IBinding[]) ArrayUtil.append( IBinding.class, bs, o );
} }
return CPPSemantics.resolveAmbiguities( name, bs ); return CPPSemantics.resolveAmbiguities( name, bs );
} else if( obj instanceof IASTName ){ } else if( obj instanceof IASTName ){

View file

@ -138,7 +138,6 @@ public class CPPSemantics {
static protected class LookupData static protected class LookupData
{ {
protected IASTName astName; protected IASTName astName;
public char[] name;
public ObjectMap usingDirectives = ObjectMap.EMPTY_MAP; public ObjectMap usingDirectives = ObjectMap.EMPTY_MAP;
public ObjectSet visited = ObjectSet.EMPTY_SET; //used to ensure we don't visit things more than once public ObjectSet visited = ObjectSet.EMPTY_SET; //used to ensure we don't visit things more than once
public ObjectSet inheritanceChain; //used to detect circular inheritance public ObjectSet inheritanceChain; //used to detect circular inheritance
@ -161,14 +160,17 @@ public class CPPSemantics {
public LookupData( IASTName n ){ public LookupData( IASTName n ){
astName = n; astName = n;
this.name = n.toCharArray();
typesOnly = typesOnly(); typesOnly = typesOnly();
considerConstructors = considerConstructors(); considerConstructors = considerConstructors();
checkWholeClassScope = checkWholeClassScope(); checkWholeClassScope = checkWholeClassScope();
} }
public LookupData( char [] n ){ public LookupData(){
astName = null; astName = null;
this.name = n; }
public final char [] name () {
if( astName != null )
return astName.toCharArray();
return EMPTY_NAME_ARRAY;
} }
public boolean includeBlockItem( IASTNode item ){ public boolean includeBlockItem( IASTNode item ){
if( astName.getPropertyInParent() == STRING_LOOKUP_PROPERTY ) return true; if( astName.getPropertyInParent() == STRING_LOOKUP_PROPERTY ) return true;
@ -590,15 +592,11 @@ public class CPPSemantics {
try { try {
IScope scope = (binding != null ) ? binding.getScope() : null; IScope scope = (binding != null ) ? binding.getScope() : null;
if( data.associated.size() > 0 && ( scope == null|| !(scope instanceof ICPPClassScope) ) ){ if( data.associated.size() > 0 && ( scope == null|| !(scope instanceof ICPPClassScope) ) ){
Object [] assoc = new Object[ data.associated.size() ];
System.arraycopy( data.associated.keyArray(), 0, assoc, 0, assoc.length );
data.ignoreUsingDirectives = true; data.ignoreUsingDirectives = true;
data.forceQualified = true; data.forceQualified = true;
for( int i = 0; i < assoc.length; i++ ){ for( int i = 0; i < data.associated.size(); i++ ){
if( data.associated.containsKey( assoc[i] ) ) lookup( data, data.associated.keyAt(i) );
lookup( data, assoc[i] );
} }
binding = resolveAmbiguities( data, data.astName ); binding = resolveAmbiguities( data, data.astName );
} }
} catch ( DOMException e ) { } catch ( DOMException e ) {
@ -663,7 +661,7 @@ public class CPPSemantics {
if( parent instanceof IASTTypeId && parent.getPropertyInParent() == ICPPASTTemplateId.TEMPLATE_ID_ARGUMENT ){ if( parent instanceof IASTTypeId && parent.getPropertyInParent() == ICPPASTTemplateId.TEMPLATE_ID_ARGUMENT ){
//don't do a problem here //don't do a problem here
} else { } else {
binding = new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_INVALID_TYPE, data.name ); binding = new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_INVALID_TYPE, data.name() );
} }
} }
} }
@ -675,9 +673,9 @@ public class CPPSemantics {
} }
if( binding == null ){ if( binding == null ){
if( name instanceof ICPPASTQualifiedName && data.forDefinition() ) if( name instanceof ICPPASTQualifiedName && data.forDefinition() )
binding = new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_MEMBER_DECLARATION_NOT_FOUND, data.name ); binding = new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_MEMBER_DECLARATION_NOT_FOUND, data.name() );
else else
binding = new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_NAME_NOT_FOUND, data.name ); binding = new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_NAME_NOT_FOUND, data.name() );
} }
return binding; return binding;
} }
@ -1094,13 +1092,13 @@ public class CPPSemantics {
Object [] r = (Object[]) result; Object [] r = (Object[]) result;
for( int j = 0; j < r.length && r[j] != null; j++ ) { for( int j = 0; j < r.length && r[j] != null; j++ ) {
if( checkForAmbiguity( data, r[j], inherited ) ){ if( checkForAmbiguity( data, r[j], inherited ) ){
data.problem = new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, data.name ); data.problem = new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, data.name() );
return null; return null;
} }
} }
} else { } else {
if( checkForAmbiguity( data, result, inherited ) ){ if( checkForAmbiguity( data, result, inherited ) ){
data.problem = new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, data.name ); data.problem = new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, data.name() );
return null; return null;
} }
} }
@ -1655,8 +1653,9 @@ public class CPPSemantics {
return false; return false;
} }
char[] c = potential.toCharArray(); char[] c = potential.toCharArray();
return ( (data.prefixLookup && CharArrayUtils.equals( c, 0, data.name.length, data.name )) || char [] n = data.name();
(!data.prefixLookup && CharArrayUtils.equals( c, data.name )) ); return ( (data.prefixLookup && CharArrayUtils.equals( c, 0, n.length, n )) ||
(!data.prefixLookup && CharArrayUtils.equals( c, n )) );
} }
private static void addDefinition( IBinding binding, IASTName name ){ private static void addDefinition( IBinding binding, IASTName name ){
@ -1817,7 +1816,7 @@ public class CPPSemantics {
{ {
//ok, stay with the template, the specialization, if applicable, will come out during instantiation //ok, stay with the template, the specialization, if applicable, will come out during instantiation
} else if( type != temp && !((IType)type).isSameType( (IType) temp )) { } else if( type != temp && !((IType)type).isSameType( (IType) temp )) {
return new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, data.name ); return new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, data.name() );
} }
} else if( temp instanceof IFunction ){ } else if( temp instanceof IFunction ){
if( temp instanceof ICPPTemplateDefinition ){ if( temp instanceof ICPPTemplateDefinition ){
@ -1837,14 +1836,14 @@ public class CPPSemantics {
{ {
//ok, delegates are synonyms //ok, delegates are synonyms
} else if( obj != temp ){ } else if( obj != temp ){
return new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, data.name ); return new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, data.name() );
} }
} }
} }
if( data.forUsingDeclaration() ){ if( data.forUsingDeclaration() ){
IBinding [] bindings = null; IBinding [] bindings = null;
if( obj != null ){ if( obj != null ){
if( fns.size() > 0 ) return new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, data.name ); if( fns.size() > 0 ) return new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, data.name() );
// if( type == null ) return obj; // if( type == null ) return obj;
bindings = (IBinding[]) ArrayUtil.append( IBinding.class, bindings, obj ); bindings = (IBinding[]) ArrayUtil.append( IBinding.class, bindings, obj );
bindings = (IBinding[]) ArrayUtil.append( IBinding.class, bindings, type ); bindings = (IBinding[]) ArrayUtil.append( IBinding.class, bindings, type );
@ -1869,8 +1868,9 @@ public class CPPSemantics {
} }
} else { } else {
if( fns == ObjectSet.EMPTY_SET ) if( fns == ObjectSet.EMPTY_SET )
fns = new ObjectSet( templateFns.size() ); fns = templateFns;
fns.addAll( templateFns ); else
fns.addAll( templateFns );
} }
} }
int numFns = fns.size(); int numFns = fns.size();
@ -1881,8 +1881,8 @@ public class CPPSemantics {
if( numFns > 0 ){ if( numFns > 0 ){
if( obj != null ) if( obj != null )
return new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, data.name ); return new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, data.name() );
return resolveFunction( data, (IBinding[]) ArrayUtil.trim( IBinding.class, fns.keyArray(), true ) ); return resolveFunction( data, (IBinding[]) fns.keyArray( IBinding.class ) );
} }
return obj; return obj;
@ -2047,7 +2047,7 @@ public class CPPSemantics {
} }
static private IBinding resolveFunction( CPPSemantics.LookupData data, IBinding[] fns ) throws DOMException{ static private IBinding resolveFunction( CPPSemantics.LookupData data, IBinding[] fns ) throws DOMException{
fns = (IBinding[]) ArrayUtil.trim( IBinding.class, fns, true ); fns = (IBinding[]) ArrayUtil.trim( IBinding.class, fns );
if( fns == null || fns.length == 0 ) if( fns == null || fns.length == 0 )
return null; return null;
@ -2246,7 +2246,7 @@ public class CPPSemantics {
if( ambiguous || bestHasAmbiguousParam ){ if( ambiguous || bestHasAmbiguousParam ){
return new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, data.name ); return new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, data.name() );
} }
return bestFn; return bestFn;
@ -2280,7 +2280,7 @@ public class CPPSemantics {
while( type != null ){ while( type != null ){
type = (type != null) ? getUltimateType( type, false ) : null; type = (type != null) ? getUltimateType( type, false ) : null;
if( type == null || !( type instanceof IFunctionType ) ) if( type == null || !( type instanceof IFunctionType ) )
return new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, data.name ); return new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, data.name() );
for( int i = 0; i < fns.length; i++ ){ for( int i = 0; i < fns.length; i++ ){
IFunction fn = (IFunction) fns[i]; IFunction fn = (IFunction) fns[i];
@ -2294,7 +2294,7 @@ public class CPPSemantics {
if( result == null ) if( result == null )
result = fn; result = fn;
else else
return new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, data.name ); return new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, data.name() );
} }
} }
@ -2305,7 +2305,7 @@ public class CPPSemantics {
} }
} }
return ( result != null ) ? result : new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, data.name ); return ( result != null ) ? result : new ProblemBinding( data.astName, IProblemBinding.SEMANTIC_AMBIGUOUS_LOOKUP, data.name() );
} }
private static Object getTargetType( LookupData data ){ private static Object getTargetType( LookupData data ){
@ -2550,15 +2550,14 @@ public class CPPSemantics {
//constructors //constructors
if( t instanceof ICPPClassType ){ if( t instanceof ICPPClassType ){
LookupData data = new LookupData( EMPTY_NAME_ARRAY );
data.forUserDefinedConversion = true;
data.functionParameters = new IType [] { source };
ICPPConstructor [] constructors = ((ICPPClassType)t).getConstructors(); ICPPConstructor [] constructors = ((ICPPClassType)t).getConstructors();
if( constructors.length > 0 ){ if( constructors.length > 0 ){
if( constructors.length == 1 && constructors[0] instanceof IProblemBinding ) if( constructors.length == 1 && constructors[0] instanceof IProblemBinding )
constructor = null; constructor = null;
else { else {
LookupData data = new LookupData();
data.forUserDefinedConversion = true;
data.functionParameters = new IType [] { source };
IBinding binding = resolveFunction( data, constructors ); IBinding binding = resolveFunction( data, constructors );
if( binding instanceof ICPPConstructor ) if( binding instanceof ICPPConstructor )
constructor = (ICPPConstructor) binding; constructor = (ICPPConstructor) binding;
@ -3104,7 +3103,7 @@ public class CPPSemantics {
} }
} }
return (IBinding[]) ArrayUtil.trim( IBinding.class, set.keyArray(), true ); return (IBinding[]) set.keyArray( IBinding.class );
} }
public static IBinding [] prefixLookup( IASTName name ){ public static IBinding [] prefixLookup( IASTName name ){
LookupData data = createLookupData( name, true ); LookupData data = createLookupData( name, true );