|
From: <jas...@us...> - 2006-07-11 06:11:36
|
Revision: 1511 Author: jasonpmorrison Date: 2006-07-10 23:11:31 -0700 (Mon, 10 Jul 2006) ViewCVS: http://svn.sourceforge.net/rubyeclipse/?rev=1511&view=rev Log Message: ----------- Bit of refactoring Modified Paths: -------------- branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultOccurrencesFinder.java Modified: branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultOccurrencesFinder.java =================================================================== --- branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultOccurrencesFinder.java 2006-07-11 04:46:35 UTC (rev 1510) +++ branches/type_inferrence/trunk/org.rubypeople.rdt.core/src/org/rubypeople/rdt/internal/ti/DefaultOccurrencesFinder.java 2006-07-11 06:11:31 UTC (rev 1511) @@ -52,182 +52,94 @@ return null; } + /** + * Determines the kind of originating node, and collects occurrences accordingly + */ public List<ISourcePosition> perform() { - // References to return - List<ISourcePosition> references = new LinkedList<ISourcePosition>(); + // occurrences to return + List<ISourcePosition> occurrences = new LinkedList<ISourcePosition>(); if ( isLocalVarRef(orig) ) { - pushLocalVarRefs( root, orig, references ); + pushLocalVarRefs( root, orig, occurrences ); } if ( isInstanceVarRef(orig) ) { - pushInstVarRefs( root, orig, references ); + pushInstVarRefs( root, orig, occurrences ); } if ( isGlobalVarRef(orig) ) { - pushGlobalVarRefs( root, orig, references ); + pushGlobalVarRefs( root, orig, occurrences ); } // if ( isMethodRefNode(orig)) { - // pushMethodRefs( root, orig, references ); + // pushMethodRefs( root, orig, occurrences ); // } if ( orig instanceof ConstNode ) { - pushConstRefs( root, orig, references ); + pushConstRefs( root, orig, occurrences ); } - return references; + return occurrences; } + + // **************************************************************************** + // * + // * Reference kind definitions + // * + // **************************************************************************** - private ISourcePosition getPositionOfName(Node node, Node scope) - { - ISourcePosition pos = node.getPosition(); - - //todo: refactor the getting-of-name - String name = null; - if ( isLocalVarRef(node) ) { name = getLocalVarRefName(node, scope); } - if ( isInstanceVarRef(node) ) { name = getInstVarRefName(node, scope); } - if ( isGlobalVarRef(node) ) { name = getGlobalVarRefName(node); } - if ( node instanceof ConstNode ) { name = ((ConstNode)node).getName(); } - - if ( name == null ) - { - System.err.println("Couldn't get the name for: " + node.toString() + " in " + scope.toString() ); - } - return new SourcePosition(pos.getFile(), pos.getStartLine(), pos.getEndLine(), pos.getStartOffset(), pos.getStartOffset() + name.length() ); - } - + /** - * Returns the name of a local var ref (LocalAsgnNode, ArgumentNode, LocalVarNode) - * @param node Node to get the name of - * @param scope Enclosing scope (to scrape args, etc.) + * Determines whether a given node is a local variable reference + * @param node * @return */ - private String getLocalVarRefName( Node node, Node scope ) { - if (node instanceof LocalAsgnNode) { - return ((LocalAsgnNode)node).getName(); - } - - if ( node instanceof ArgumentNode ) { - return ((ArgumentNode)node).getName(); - } - - if ( node instanceof LocalVarNode ) { - if ( scope instanceof DefnNode ) { - return ((DefnNode)scope).getBodyNode().getLocalNames()[((LocalVarNode)node).getCount()]; - } - if ( scope instanceof DefsNode ) { - return ((DefsNode)scope).getBodyNode().getLocalNames()[((LocalVarNode)node).getCount()]; - } - - // No enclosing ScopeNode found, try searching backwards for an AsgnNode - final int localVarCount = ((LocalVarNode)node).getCount(); - Node previousAssign = FirstPrecursorNodeLocator.Instance().findFirstPrecursor(scope, node.getPosition().getStartOffset(), new INodeAcceptor() { - public boolean doesAccept(Node node) { - if ( node instanceof LocalAsgnNode ) - { - return ((LocalAsgnNode)node).getCount() == localVarCount; - } - return false; - } - }); - if ( previousAssign != null ) - { - return ((LocalAsgnNode)previousAssign).getName(); - } - - System.err.println("Unhandled scope for local var ref node found: " + scope.toString() ); - //TODO: if scope instanceof Block Body? what type is this.. - } - - if ( node instanceof DVarNode ) { - return ((DVarNode)node).getName(); - } - -// System.err.println("Encountered unhandled node type in getLocalVarRefName: " + node.toString() + " in " + scope.toString()); - return null; - } - - private String getClassNodeName( ClassNode classNode ) { - if (classNode.getCPath() instanceof Colon2Node) { - Colon2Node c2node = (Colon2Node) classNode.getCPath(); - return c2node.getName(); - } - System.err.println("ClassNode.getCPath() returned other than Colon2Node: " + classNode.toString() ); - return null; - } - - - private String getInstVarRefName( Node node, Node scope ) { - if ( node instanceof InstAsgnNode ) { - return ((InstAsgnNode)node).getName(); - } - - if ( node instanceof ArgumentNode ) { - return ((InstAsgnNode)node).getName(); - } - - if ( node instanceof InstVarNode ) { - return ((InstVarNode)node).getName(); - } - - if ( node instanceof DVarNode ) { - return ((DVarNode)node).getName(); - } - -// System.err.println("Encountered unhandled node type for getInstVarRefName: " + node.toString() + " in " + scope.toString()); - return null; - } - - private String getGlobalVarRefName( Node node ) { - if ( node instanceof GlobalVarNode ) - { - return ((GlobalVarNode)node).getName(); - } - if ( node instanceof GlobalAsgnNode ) { - return ((GlobalAsgnNode)node).getName(); - } - return null; - } - - private String getMethodRefName( Node node ) { - if ( node instanceof DefnNode ) { - return ((DefnNode)node).getName(); - } - if ( node instanceof DefsNode ) { - return ((DefsNode)node).getName(); - } - if ( node instanceof CallNode ) { - return ((CallNode)node).getName(); - } - if ( node instanceof VCallNode ) { - return ((VCallNode)node).getMethodName(); - } - return null; - } - - - private boolean isLocalVarRef( Node node ) { return ( ( node instanceof LocalAsgnNode ) || ( node instanceof ArgumentNode ) || ( node instanceof LocalVarNode ) ); } + /** + * Determines whether a given node is an instance variable reference + * @param node + * @return + */ private boolean isInstanceVarRef( Node node ) { return ( ( node instanceof InstAsgnNode ) || ( node instanceof InstVarNode ) ) ; } - + + /** + * Determines whether a given node is a global variable reference + * @param node + * @return + */ private boolean isGlobalVarRef( Node node ) { return ( ( node instanceof GlobalAsgnNode ) || ( node instanceof GlobalVarNode ) ); } + /** + * Determines whether a given node is method reference (either definition or invocation) + * @param node + * @return + */ private boolean isMethodRefNode( Node node ) { return ( ( node instanceof DefnNode ) || ( node instanceof DefsNode ) || ( node instanceof CallNode ) || ( node instanceof VCallNode ) ); } - - private void pushLocalVarRefs( Node root, Node orig, List<ISourcePosition> references ) { - System.out.println("Finding references for a local variable " + orig.toString()); + // **************************************************************************** + // * + // * Worker methods - handles delegation of occurrence searches + // * + // **************************************************************************** + + /** + * Collects all corresponding local variable occurrences + * @param root Root node to search + * @param orig Originating node + * @param occurrences + */ + private void pushLocalVarRefs( Node root, Node orig, List<ISourcePosition> occurrences ) { + System.out.println("Finding occurrences for a local variable " + orig.toString()); // Find the search space Node searchSpace = FirstPrecursorNodeLocator.Instance().findFirstPrecursor(root, orig.getPosition().getStartOffset(), new INodeAcceptor() { @@ -251,21 +163,24 @@ List<Node> searchResults = ScopedNodeLocator.Instance().findNodesInScope(searchSpace, new INodeAcceptor() { public boolean doesAccept(Node node) { String name = getLocalVarRefName(node, finalSearchSpace); -// System.out.println("Matching name" + name); return ( name != null && name.equals(origName)); } }); // Scrape position from pertinent nodes for ( Node searchResult : searchResults ) { - references.add(getPositionOfName(searchResult, searchSpace)); + occurrences.add(getPositionOfName(searchResult, searchSpace)); } - -// System.out.println("Searching search space " + searchSpace.toString() + searchSpace.getPosition().toString() ); } - private void pushInstVarRefs( Node root, Node orig, List<ISourcePosition> references ) { - System.out.println("Finding references for an instance variable " + orig.toString() ); + /** + * Collects all instance variable occurrences + * @param root + * @param orig + * @param occurrences + */ + private void pushInstVarRefs( Node root, Node orig, List<ISourcePosition> occurrences ) { + System.out.println("Finding occurrences for an instance variable " + orig.toString() ); Node searchSpace; @@ -301,17 +216,18 @@ } // Finalize searchSpace because Java's scoping rules are the awesome - final Node finalSearchSpace = searchSpace; + //todo: not needed? + //final Node finalSearchSpace = searchSpace; // Get name of local variable reference - final String origName = getInstVarRefName(orig,searchSpace); + final String origName = getInstVarRefName(orig); // Find all pertinent nodes List<Node> searchResults = ScopedNodeLocator.Instance().findNodesInScope(searchSpace, new INodeAcceptor() { public boolean doesAccept(Node node) { if ( isInstanceVarRef(node) ) { - String name = getInstVarRefName(node, finalSearchSpace); + String name = getInstVarRefName(node); return ( name != null && name.equals(origName)); } return false; @@ -320,12 +236,18 @@ // Scrape position from pertinent nodes for ( Node searchResult : searchResults ) { - references.add(getPositionOfName(searchResult, searchSpace)); + occurrences.add(getPositionOfName(searchResult, searchSpace)); } } - private void pushGlobalVarRefs( Node root, Node orig, List<ISourcePosition> references ) { + /** + * Collects all global variable occurrences + * @param root + * @param orig + * @param occurrences + */ + private void pushGlobalVarRefs( Node root, Node orig, List<ISourcePosition> occurrences ) { final Node searchSpace = root; final String origName = getGlobalVarRefName(orig); @@ -338,21 +260,21 @@ // Scrape position from pertinent nodes for ( Node searchResult : searchResults ) { - references.add(getPositionOfName(searchResult, searchSpace)); + occurrences.add(getPositionOfName(searchResult, searchSpace)); } } //todo: complete -// private void pushMethodRefs( Node root, Node orig, List<ISourcePosition> references) { +// private void pushMethodRefs( Node root, Node orig, List<ISourcePosition> occurrences) { // // // DefnNode DefsNode CallNode VCallNode // -// System.out.println("Finding references for method reference node " + orig.toString() ); +// System.out.println("Finding occurrences for method reference node " + orig.toString() ); // // final Node searchSpace = root; // String origName = getMethodRefName(orig); // -// // If orig is a method definition, find all references to that selector for the orig's enclosing type +// // If orig is a method definition, find all occurrences to that selector for the orig's enclosing type // if ( orig instanceof DefnNode || orig instanceof DefsNode ) // { // ((DefnNode)orig).g @@ -361,7 +283,10 @@ // Node receiver = getMethodReceiver(orig); // } - private void pushConstRefs( Node root, Node orig, List<ISourcePosition> references) { + /** + * Collects all pertinent ConstNode occurrences + */ + private void pushConstRefs( Node root, Node orig, List<ISourcePosition> occurrences) { if ( !( orig instanceof ConstNode) ) { return; @@ -379,7 +304,160 @@ }); for ( Node searchResult : searchResults ) { - references.add(getPositionOfName(searchResult, root ) ); + occurrences.add(getPositionOfName(searchResult, root ) ); } } + + // **************************************************************************** + // * + // * Utility methods + // * + // **************************************************************************** + + /** + * Gets the position of the name for the specified node. + * @param node Node that responds to getName() or some variant + * @param scope Scope that holds the node (pertinent for locals and args) + * @return ISourcePosition that holds the name of the node + */ + private ISourcePosition getPositionOfName(Node node, Node scope) + { + ISourcePosition pos = node.getPosition(); + + //todo: refactor the getting-of-name + String name = null; + if ( isLocalVarRef(node) ) { name = getLocalVarRefName(node, scope); } + if ( isInstanceVarRef(node) ) { name = getInstVarRefName(node ); } + if ( isGlobalVarRef(node) ) { name = getGlobalVarRefName(node); } + if ( node instanceof ConstNode ) { name = ((ConstNode)node).getName(); } + + if ( name == null ) + { + System.err.println("Couldn't get the name for: " + node.toString() + " in " + scope.toString() ); + } + return new SourcePosition(pos.getFile(), pos.getStartLine(), pos.getEndLine(), pos.getStartOffset(), pos.getStartOffset() + name.length() ); + } + + /** + * Returns the name of a local var ref (LocalAsgnNode, ArgumentNode, LocalVarNode) + * @param node Node to get the name of + * @param scope Enclosing scope (to scrape args, etc.) + * @return + */ + private String getLocalVarRefName( Node node, Node scope ) { + if (node instanceof LocalAsgnNode) { + return ((LocalAsgnNode)node).getName(); + } + + if ( node instanceof ArgumentNode ) { + return ((ArgumentNode)node).getName(); + } + + if ( node instanceof LocalVarNode ) { + if ( scope instanceof DefnNode ) { + return ((DefnNode)scope).getBodyNode().getLocalNames()[((LocalVarNode)node).getCount()]; + } + if ( scope instanceof DefsNode ) { + return ((DefsNode)scope).getBodyNode().getLocalNames()[((LocalVarNode)node).getCount()]; + } + + // No enclosing ScopeNode found, try searching backwards for an AsgnNode + final int localVarCount = ((LocalVarNode)node).getCount(); + Node previousAssign = FirstPrecursorNodeLocator.Instance().findFirstPrecursor(scope, node.getPosition().getStartOffset(), new INodeAcceptor() { + public boolean doesAccept(Node node) { + if ( node instanceof LocalAsgnNode ) + { + return ((LocalAsgnNode)node).getCount() == localVarCount; + } + return false; + } + }); + if ( previousAssign != null ) + { + return ((LocalAsgnNode)previousAssign).getName(); + } + + System.err.println("Unhandled scope for local var ref node found: " + scope.toString() ); + //TODO: if scope instanceof Block Body? what type is this.. + } + + if ( node instanceof DVarNode ) { + return ((DVarNode)node).getName(); + } + + return null; + } + + /** + * Gets the name of an instance variable reference + * @param node Instanve variable reference + * @return + */ + private String getInstVarRefName( Node node ) { + if ( node instanceof InstAsgnNode ) { + return ((InstAsgnNode)node).getName(); + } + + if ( node instanceof InstVarNode ) { + return ((InstVarNode)node).getName(); + } + + if ( node instanceof DVarNode ) { + return ((DVarNode)node).getName(); + } + + return null; + } + + /** + * Gets the name of a global variable reference + * @param node + * @return + */ + private String getGlobalVarRefName( Node node ) { + if ( node instanceof GlobalVarNode ) + { + return ((GlobalVarNode)node).getName(); + } + if ( node instanceof GlobalAsgnNode ) { + return ((GlobalAsgnNode)node).getName(); + } + return null; + } + + /** + * Gets the name of a method reference (either definition or invocation) + * @param node + * @return + */ + private String getMethodRefName( Node node ) { + if ( node instanceof DefnNode ) { + return ((DefnNode)node).getName(); + } + if ( node instanceof DefsNode ) { + return ((DefsNode)node).getName(); + } + if ( node instanceof CallNode ) { + return ((CallNode)node).getName(); + } + if ( node instanceof VCallNode ) { + return ((VCallNode)node).getMethodName(); + } + return null; + } + + /** + * Helper method to get the class name froma ClassNode + * @param classNode + * @return + */ + private String getClassNodeName( ClassNode classNode ) { + if (classNode.getCPath() instanceof Colon2Node) { + Colon2Node c2node = (Colon2Node) classNode.getCPath(); + return c2node.getName(); + } + System.err.println("ClassNode.getCPath() returned other than Colon2Node: " + classNode.toString() ); + return null; + } + } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |