1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 00:45:28 +02:00

IFunction.isStatic, IScope.getPhysicalNode

This commit is contained in:
Andrew Niefer 2005-02-22 21:45:44 +00:00
parent a4475cf6e3
commit cdf3cc5492
13 changed files with 217 additions and 50 deletions

View file

@ -1923,6 +1923,20 @@ public class AST2CPPTests extends AST2BaseTest {
assertEquals( 4, bs.length );
}
public void testIsStatic() throws Exception {
StringBuffer buffer = new StringBuffer();
buffer.append("static void f(); \n" ); //$NON-NLS-1$
buffer.append("void f() {} \n" ); //$NON-NLS-1$
IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP);
CPPNameCollector col = new CPPNameCollector();
CPPVisitor.visitTranslationUnit(tu, col);
IFunction f = (IFunction) col.getName(1).resolveBinding();
assertTrue( f.isStatic() );
assertInstances( col, f, 2 );
}
// public void testBug85310() throws Exception
// {
// StringBuffer buffer = new StringBuffer( "void f() {" ); //$NON-NLS-1$

View file

@ -42,4 +42,5 @@ public interface IFunction extends IBinding {
*/
public IFunctionType getType() throws DOMException;
public boolean isStatic() throws DOMException;
}

View file

@ -33,4 +33,9 @@ public interface IScope {
* @return List of IBinding
*/
public IBinding[] find(String name) throws DOMException;
/**
* @return
*/
public IASTNode getPhysicalNode() throws DOMException;
}

View file

@ -26,8 +26,6 @@ public interface ICPPMember {
*/
public int getVisibility() throws DOMException;
public boolean isStatic() throws DOMException;
public static final int v_private = ICPPASTVisiblityLabel.v_private;
public static final int v_protected = ICPPASTVisiblityLabel.v_protected;
public static final int v_public = ICPPASTVisiblityLabel.v_public;

View file

@ -15,7 +15,6 @@ package org.eclipse.cdt.core.dom.ast.cpp;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IScope;
@ -23,7 +22,7 @@ import org.eclipse.cdt.core.dom.ast.IScope;
* @author aniefer
*/
public interface ICPPScope extends IScope {
public IASTNode getPhysicalNode() throws DOMException;
public void addBinding( IBinding binding ) throws DOMException;
public IBinding getBinding( IASTName name ) throws DOMException;
}

View file

@ -14,6 +14,7 @@
*/
package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.c.ICASTCompositeTypeSpecifier;
@ -79,4 +80,10 @@ public class CCompositeTypeScope implements ICCompositeTypeScope {
bindings.remove( binding.getNameCharArray(), 0, binding.getNameCharArray().length);
}
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IScope#getPhysicalNode()
*/
public IASTNode getPhysicalNode() {
return compositeTypeSpec;
}
}

View file

@ -80,4 +80,11 @@ public class CExternalFunction implements IFunction, ICExternalBinding {
public IScope getScope() {
return tu.getScope();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IFunction#isStatic()
*/
public boolean isStatic() {
return false;
}
}

View file

@ -10,6 +10,7 @@
**********************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
@ -18,6 +19,7 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.IFunctionType;
@ -25,8 +27,11 @@ import org.eclipse.cdt.core.dom.ast.IParameter;
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVisitor;
/**
* Created on Nov 5, 2004
@ -35,6 +40,12 @@ import org.eclipse.cdt.core.parser.util.CharArrayUtils;
public class CFunction implements IFunction, ICBinding {
private IASTStandardFunctionDeclarator [] declarators = null;
private IASTFunctionDeclarator definition;
private static final int FULLY_RESOLVED = 1;
private static final int RESOLUTION_IN_PROGRESS = 1 << 1;
private static final int IS_STATIC = 3 << 2;
private int bits = 0;
IFunctionType type = null;
public CFunction( IASTFunctionDeclarator declarator ){
@ -70,6 +81,24 @@ public class CFunction implements IFunction, ICBinding {
}
}
private void resolveAllDeclarations(){
if( (bits & (FULLY_RESOLVED | RESOLUTION_IN_PROGRESS)) == 0 ){
bits |= RESOLUTION_IN_PROGRESS;
IASTTranslationUnit tu = null;
if( definition != null )
tu = definition.getTranslationUnit();
else if( declarators != null )
tu = declarators[0].getTranslationUnit();
if( tu != null ){
CPPVisitor.getDeclarations( tu, this );
}
declarators = (ICPPASTFunctionDeclarator[]) ArrayUtil.trim( ICPPASTFunctionDeclarator.class, declarators );
bits |= FULLY_RESOLVED;
bits &= ~RESOLUTION_IN_PROGRESS;
}
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IFunction#getParameters()
*/
@ -267,4 +296,37 @@ public class CFunction implements IFunction, ICBinding {
}
}
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IFunction#isStatic()
*/
public boolean isStatic() {
if( (bits & FULLY_RESOLVED) == 0 ){
resolveAllDeclarations();
}
//2 state bits, most significant = whether or not we've figure this out yet
//least significant = whether or not we are static
int state = ( bits & IS_STATIC ) >> 2;
if( state > 1 ) return (state % 2 != 0);
IASTFunctionDeclarator dtor = definition;
IASTDeclSpecifier declSpec = ((IASTFunctionDefinition)dtor.getParent()).getDeclSpecifier();
if( declSpec.getStorageClass() == IASTDeclSpecifier.sc_static ){
bits |= 3 << 2;
return true;
}
for( int i = 0; i < declarators.length; i++ ){
IASTNode parent = declarators[i].getParent();
declSpec = ((IASTSimpleDeclaration)parent).getDeclSpecifier();
if( declSpec.getStorageClass() == IASTDeclSpecifier.sc_static ){
bits |= 3 << 2;
return true;
}
}
bits |= 2 << 2;
return false;
}
}

View file

@ -14,6 +14,7 @@ package org.eclipse.cdt.internal.core.dom.parser.c;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTLabelStatement;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ILabel;
@ -121,4 +122,11 @@ public class CFunctionScope implements ICFunctionScope {
bindings.remove( binding.getNameCharArray(), 0, binding.getNameCharArray().length);
}
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IScope#getPhysicalNode()
*/
public IASTNode getPhysicalNode() {
return function;
}
}

View file

@ -73,4 +73,11 @@ public class CScope implements ICScope {
bindings[type].remove( binding.getNameCharArray(), 0, binding.getNameCharArray().length);
}
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IScope#getPhysicalNode()
*/
public IASTNode getPhysicalNode() {
return physicalNode;
}
}

View file

@ -14,13 +14,16 @@
package org.eclipse.cdt.internal.core.dom.parser.cpp;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.IFunctionType;
@ -29,6 +32,7 @@ import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding;
/**
@ -37,11 +41,6 @@ import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding;
public class CPPFunction implements IFunction, ICPPBinding {
public static class CPPFunctionProblem extends ProblemBinding implements IFunction {
/**
* @param id
* @param arg
*/
public CPPFunctionProblem( int id, char[] arg ) {
super( id, arg );
}
@ -57,12 +56,21 @@ public class CPPFunction implements IFunction, ICPPBinding {
public IFunctionType getType() throws DOMException {
throw new DOMException( this );
}
public boolean isStatic() throws DOMException {
throw new DOMException( this );
}
}
protected ICPPASTFunctionDeclarator [] declarations;
protected ICPPASTFunctionDeclarator definition;
protected IFunctionType type = null;
private static final int FULLY_RESOLVED = 1;
private static final int RESOLUTION_IN_PROGRESS = 1 << 1;
private static final int IS_STATIC = 3 << 2;
private int bits = 0;
public CPPFunction( ICPPASTFunctionDeclarator declarator ){
if( declarator != null ) {
IASTNode parent = declarator.getParent();
@ -73,6 +81,34 @@ public class CPPFunction implements IFunction, ICPPBinding {
}
}
private void resolveAllDeclarations(){
if( (bits & (FULLY_RESOLVED | RESOLUTION_IN_PROGRESS)) == 0 ){
bits |= RESOLUTION_IN_PROGRESS;
IASTTranslationUnit tu = null;
if( definition != null )
tu = definition.getTranslationUnit();
else if( declarations != null )
tu = declarations[0].getTranslationUnit();
else {
//implicit binding
IScope scope = getScope();
try {
IASTNode node = scope.getPhysicalNode();
while( !( node instanceof IASTTranslationUnit) )
node = node.getParent();
tu = (IASTTranslationUnit) node;
} catch ( DOMException e ) {
}
}
if( tu != null ){
CPPVisitor.getDeclarations( tu, this );
}
declarations = (ICPPASTFunctionDeclarator[]) ArrayUtil.trim( ICPPASTFunctionDeclarator.class, declarations );
bits |= FULLY_RESOLVED;
bits &= ~RESOLUTION_IN_PROGRESS;
}
}
/* (non-Javadoc)
* @see org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPBinding#getDeclarations()
*/
@ -98,7 +134,10 @@ public class CPPFunction implements IFunction, ICPPBinding {
return;
}
for( int i = 0; i < declarations.length; i++ ){
if( declarations[i] == null ){
if( declarations[i] == dtor ){
//already in
return;
} else if( declarations[i] == null ){
declarations[i] = dtor;
updateParameterBindings( dtor );
return;
@ -183,17 +222,6 @@ public class CPPFunction implements IFunction, ICPPBinding {
return scope;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IBinding#getPhysicalNode()
*/
public IASTNode getPhysicalNode() {
if( definition != null )
return definition;
else if( declarations != null && declarations.length > 0 )
return declarations[0];
return null;
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IFunction#getType()
*/
@ -234,7 +262,7 @@ public class CPPFunction implements IFunction, ICPPBinding {
}
protected void updateParameterBindings( ICPPASTFunctionDeclarator fdtor ){
ICPPASTFunctionDeclarator orig = (ICPPASTFunctionDeclarator) getPhysicalNode();
ICPPASTFunctionDeclarator orig = definition != null ? definition : declarations[0];
IASTParameterDeclaration [] ops = orig.getParameters();
IASTParameterDeclaration [] nps = fdtor.getParameters();
CPPParameter temp = null;
@ -247,4 +275,38 @@ public class CPPFunction implements IFunction, ICPPBinding {
}
}
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IFunction#isStatic()
*/
public boolean isStatic() {
if( (bits & FULLY_RESOLVED) == 0 ){
resolveAllDeclarations();
}
//2 state bits, most significant = whether or not we've figure this out yet
//least significant = whether or not we are static
int state = ( bits & IS_STATIC ) >> 2;
if( state > 1 ) return (state % 2 != 0);
IASTFunctionDeclarator dtor = (IASTFunctionDeclarator) getDefinition();
IASTDeclSpecifier declSpec = ((IASTFunctionDefinition)dtor.getParent()).getDeclSpecifier();
if( declSpec.getStorageClass() == IASTDeclSpecifier.sc_static ){
bits |= 3 << 2;
return true;
}
IASTFunctionDeclarator[] dtors = (IASTFunctionDeclarator[]) getDeclarations();
for( int i = 0; i < dtors.length; i++ ){
IASTNode parent = dtors[i].getParent();
declSpec = ((IASTSimpleDeclaration)parent).getDeclSpecifier();
if( declSpec.getStorageClass() == IASTDeclSpecifier.sc_static ){
bits |= 3 << 2;
return true;
}
}
bits |= 2 << 2;
return false;
}
}

View file

@ -15,16 +15,13 @@ package org.eclipse.cdt.internal.core.dom.parser.cpp;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTVisiblityLabel;
@ -68,7 +65,7 @@ public class CPPMethod extends CPPFunction implements ICPPMethod {
}
}
char [] myName = ((IASTDeclarator)getPhysicalNode()).getName().toCharArray();
char [] myName = getNameCharArray();
ICPPClassScope scope = (ICPPClassScope) getScope();
ICPPASTCompositeTypeSpecifier compSpec = (ICPPASTCompositeTypeSpecifier) scope.getPhysicalNode();
@ -148,24 +145,24 @@ public class CPPMethod extends CPPFunction implements ICPPMethod {
return declarations[0].getName().toCharArray();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.cpp.ICPPMember#isStatic()
*/
public boolean isStatic() throws DOMException {
IASTDeclarator dtor = (IASTDeclarator) getPrimaryDeclaration();
if( dtor == null ) return false;
while( dtor.getPropertyInParent() == IASTDeclarator.NESTED_DECLARATOR )
dtor = (IASTDeclarator) dtor.getParent();
IASTNode node = dtor.getParent();
if( node instanceof IASTSimpleDeclaration ){
ICPPASTDeclSpecifier declSpec = (ICPPASTDeclSpecifier) ((IASTSimpleDeclaration)node).getDeclSpecifier();
return (declSpec.getStorageClass() == IASTDeclSpecifier.sc_static );
} else if( node instanceof IASTFunctionDefinition ){
ICPPASTDeclSpecifier declSpec = (ICPPASTDeclSpecifier) ((IASTFunctionDefinition)node).getDeclSpecifier();
return (declSpec.getStorageClass() == IASTDeclSpecifier.sc_static );
}
return false;
}
// /* (non-Javadoc)
// * @see org.eclipse.cdt.core.dom.ast.cpp.ICPPMember#isStatic()
// */
// public boolean isStatic() throws DOMException {
// IASTDeclarator dtor = (IASTDeclarator) getPrimaryDeclaration();
// if( dtor == null ) return false;
//
// while( dtor.getPropertyInParent() == IASTDeclarator.NESTED_DECLARATOR )
// dtor = (IASTDeclarator) dtor.getParent();
//
// IASTNode node = dtor.getParent();
// if( node instanceof IASTSimpleDeclaration ){
// ICPPASTDeclSpecifier declSpec = (ICPPASTDeclSpecifier) ((IASTSimpleDeclaration)node).getDeclSpecifier();
// return (declSpec.getStorageClass() == IASTDeclSpecifier.sc_static );
// } else if( node instanceof IASTFunctionDefinition ){
// ICPPASTDeclSpecifier declSpec = (ICPPASTDeclSpecifier) ((IASTFunctionDefinition)node).getDeclSpecifier();
// return (declSpec.getStorageClass() == IASTDeclSpecifier.sc_static );
// }
// return false;
// }
}

View file

@ -79,7 +79,6 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPCompositeBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMember;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespaceScope;
@ -758,7 +757,8 @@ public class CPPSemantics {
return true;
if( binding instanceof IEnumerator )
continue;
else if( binding instanceof ICPPMember && ((ICPPMember)binding).isStatic() )
else if( (binding instanceof IFunction && ((IFunction)binding).isStatic()) ||
(binding instanceof IVariable && ((IVariable)binding).isStatic()) )
continue;
return true;
@ -1114,9 +1114,9 @@ public class CPPSemantics {
ICPPBinding cpp = (ICPPBinding) binding;
IASTNode[] n = cpp.getDeclarations();
if( n != null && n.length > 0 )
return (((ASTNode) n[0]).getOffset() < ((ASTNode)node).getOffset() );
return (((ASTNode) n[0]).getOffset() <= ((ASTNode)node).getOffset() );
else if( cpp.getDefinition() != null )
return (((ASTNode) cpp.getDefinition()).getOffset() < ((ASTNode)node).getOffset() );
return (((ASTNode) cpp.getDefinition()).getOffset() <= ((ASTNode)node).getOffset() );
else
return true;
}