From cd6f2349471dc8be9c9baec1733a70439785d200 Mon Sep 17 00:00:00 2001 From: Andrew Niefer Date: Mon, 28 Feb 2005 18:35:47 +0000 Subject: [PATCH] CPPParameter.getScope() for bug 86121 --- .../core/parser/tests/ast2/AST2CPPTests.java | 5 +++++ .../core/dom/parser/cpp/CPPFunction.java | 22 ++++++++++++++----- .../core/dom/parser/cpp/CPPParameter.java | 3 +-- .../core/dom/parser/cpp/CPPVisitor.java | 17 +++++++------- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java index d6bc508e143..13b44efa08c 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java @@ -46,6 +46,7 @@ import org.eclipse.cdt.core.dom.ast.IParameter; import org.eclipse.cdt.core.dom.ast.IPointerType; import org.eclipse.cdt.core.dom.ast.IProblemBinding; import org.eclipse.cdt.core.dom.ast.IQualifierType; +import org.eclipse.cdt.core.dom.ast.IScope; import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.ITypedef; import org.eclipse.cdt.core.dom.ast.IVariable; @@ -611,6 +612,10 @@ public class AST2CPPTests extends AST2BaseTest { assertInstances(collector, f, 2); assertInstances(collector, a, 3); + + IScope scope = a.getScope(); + assertNotNull( scope ); + assertSame( scope.getParent(), f.getScope() ); } public void testSimpleFunctionCall() throws Exception { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunction.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunction.java index 584e8d606d7..8146e8c9af5 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunction.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPFunction.java @@ -178,11 +178,12 @@ public class CPPFunction implements IFunction, ICPPBinding { * @see org.eclipse.cdt.core.dom.ast.IFunction#getFunctionScope() */ public IScope getFunctionScope() { + resolveAllDeclarations(); if( definition != null ){ - IASTFunctionDefinition def = (IASTFunctionDefinition) definition.getParent(); - return def.getScope(); - } - return null; + return definition.getFunctionScope(); + } + + return declarations[0].getFunctionScope(); } /* (non-Javadoc) @@ -268,12 +269,21 @@ public class CPPFunction implements IFunction, ICPPBinding { IASTParameterDeclaration temp = null; if( definition != null ){ temp = definition.getParameters()[i]; - ((CPPASTName)temp.getDeclarator().getName()).setBinding( binding ); + CPPASTName n = (CPPASTName)temp.getDeclarator().getName(); + if( n != name ) { + n.setBinding( binding ); + ((CPPParameter)binding).addDeclaration( n ); + } } if( declarations != null ){ for( int j = 0; j < declarations.length && declarations[j] != null; j++ ){ temp = declarations[j].getParameters()[i]; - ((CPPASTName)temp.getDeclarator().getName()).setBinding( binding ); + CPPASTName n = (CPPASTName)temp.getDeclarator().getName(); + if( n != name ) { + n.setBinding( binding ); + ((CPPParameter)binding).addDeclaration( n ); + } + } } return binding; diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPParameter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPParameter.java index 861c4298092..f67a0202361 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPParameter.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPParameter.java @@ -107,8 +107,7 @@ public class CPPParameter implements IParameter, ICPPBinding { * @see org.eclipse.cdt.core.dom.ast.IBinding#getScope() */ public IScope getScope() { - // TODO Auto-generated method stub - return null; + return CPPVisitor.getContainingScope( getPrimaryDeclaration() ); } /* (non-Javadoc) diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java index 632baa05c91..97413da7d6c 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java @@ -543,8 +543,10 @@ public class CPPVisitor implements ICPPASTVisitor { } else if( node instanceof IASTParameterDeclaration ){ ICPPASTFunctionDeclarator dtor = (ICPPASTFunctionDeclarator) node.getParent(); ASTNodeProperty prop = dtor.getPropertyInParent(); - if( prop == IASTSimpleDeclaration.DECLARATOR || prop == IASTFunctionDefinition.DECLARATOR ) + if( prop == IASTSimpleDeclaration.DECLARATOR ) return dtor.getFunctionScope(); + else if( prop == IASTFunctionDefinition.DECLARATOR ) + return ((IASTCompoundStatement)((IASTFunctionDefinition)dtor.getParent()).getBody()).getScope(); } else if( node instanceof IASTExpression ){ IASTNode parent = node.getParent(); if( parent instanceof IASTForStatement ){ @@ -610,13 +612,12 @@ public class CPPVisitor implements ICPPASTVisitor { scope = getContainingScope( (IASTStatement)parent ); } else if( parent instanceof IASTFunctionDefinition ){ IASTFunctionDeclarator fnDeclarator = ((IASTFunctionDefinition) parent ).getDeclarator(); - IFunction function = (IFunction) fnDeclarator.getName().resolveBinding(); - if( function != null ){ - try { - scope = function.getScope(); - } catch ( DOMException e ) { - } - } + IASTName name = fnDeclarator.getName(); + if( name instanceof ICPPASTQualifiedName ){ + IASTName [] ns = ((ICPPASTQualifiedName)name).getNames(); + name = ns [ ns.length -1 ]; + } + return getContainingScope( name ); } if( statement instanceof IASTGotoStatement || statement instanceof IASTLabelStatement ){