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

Cosmetics.

This commit is contained in:
Sergey Prigogin 2012-01-07 17:37:17 -08:00
parent 986bf0206b
commit 4071a34762

View file

@ -17,20 +17,21 @@ import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorStatement; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode; import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
/** /**
* @author dsteffle * @author dsteffle
*/ */
public class DOMASTNodeParent extends DOMASTNodeLeaf { public class DOMASTNodeParent extends DOMASTNodeLeaf {
private static final int NO_PREPROCESSOR_STATMENT = -1; private static final int NO_PREPROCESSOR_STATEMENT = -1;
private static final DOMASTNodeLeaf[] EMPTY_CHILDREN_ARRAY = new DOMASTNodeLeaf[0]; private static final DOMASTNodeLeaf[] EMPTY_CHILDREN_ARRAY = {};
private static final int DEFAULT_NODE_CHAIN_SIZE = 4; private static final int DEFAULT_NODE_CHAIN_SIZE = 4;
private static final int DEFAULT_CHILDREN_SIZE = 4; private static final int DEFAULT_CHILDREN_SIZE = 4;
int index=0; int index;
private DOMASTNodeLeaf[] children; private DOMASTNodeLeaf[] children;
boolean cleanupedElements = false; boolean cleanupedElements;
private int indexFirstPreproStmnt=NO_PREPROCESSOR_STATMENT; private int indexFirstPreproStmnt= NO_PREPROCESSOR_STATEMENT;
public int getStartSearch() { public int getStartSearch() {
return index; return index;
@ -46,7 +47,7 @@ public class DOMASTNodeParent extends DOMASTNodeLeaf {
children = new DOMASTNodeLeaf[DEFAULT_CHILDREN_SIZE]; children = new DOMASTNodeLeaf[DEFAULT_CHILDREN_SIZE];
} }
public void addChild(DOMASTNodeLeaf child) { public void addChild(DOMASTNodeLeaf child) {
if (child.getNode() instanceof IASTPreprocessorStatement && indexFirstPreproStmnt == NO_PREPROCESSOR_STATMENT) { if (child.getNode() instanceof IASTPreprocessorStatement && indexFirstPreproStmnt == NO_PREPROCESSOR_STATEMENT) {
indexFirstPreproStmnt=index; indexFirstPreproStmnt=index;
} }
@ -60,7 +61,7 @@ public class DOMASTNodeParent extends DOMASTNodeLeaf {
child.setParent(this); child.setParent(this);
} }
public void removeChild(DOMASTNodeLeaf child) { public void removeChild(DOMASTNodeLeaf child) {
for(int i=0; i<children.length; i++) { for (int i=0; i<children.length; i++) {
if (children[i] == child) { if (children[i] == child) {
children[i] = null; children[i] = null;
break; break;
@ -96,19 +97,19 @@ public class DOMASTNodeParent extends DOMASTNodeLeaf {
* @param pos * @param pos
* @return * @return
*/ */
public Object[] insert(Class c, Object[] array, Object obj, int pos) { public <T> T[] insert(Class<T> c, T[] array, T obj, int pos) {
if (pos < 0 || pos >= array.length) { if (pos < 0 || pos >= array.length) {
return ArrayUtil.append(c, array, obj); return ArrayUtil.append(c, array, obj);
} }
Object[] temp = (Object[]) Array.newInstance( c, array.length + 1 ); T[] temp = (T[]) Array.newInstance(c, array.length + 1);
if (pos > 0) { if (pos > 0) {
System.arraycopy( array, 0, temp, 0, pos ); System.arraycopy(array, 0, temp, 0, pos);
temp[pos] = obj; temp[pos] = obj;
System.arraycopy( array, pos, temp, pos + 1, array.length - pos ); System.arraycopy(array, pos, temp, pos + 1, array.length - pos);
} else { } else {
temp[0] = obj; temp[0] = obj;
System.arraycopy( array, 0, temp, 1, array.length ); System.arraycopy(array, 0, temp, 1, array.length);
} }
return temp; return temp;
@ -126,7 +127,7 @@ public class DOMASTNodeParent extends DOMASTNodeLeaf {
int checkLength=0; int checkLength=0;
boolean moved=false; boolean moved=false;
for (int j=0, i=0; j < children.length && children[j] != null; j++) { for (int j=0, i=0; j < children.length && children[j] != null; j++) {
if( !(children[j].getNode() instanceof IASTPreprocessorStatement) ) if (!(children[j].getNode() instanceof IASTPreprocessorStatement))
continue; continue;
while(true) { while(true) {
if (i==j) break; // don't need to check itself or anything after it if (i==j) break; // don't need to check itself or anything after it
@ -139,7 +140,7 @@ public class DOMASTNodeParent extends DOMASTNodeLeaf {
// if the checking element comes before the first element then move the checking element before the first element // if the checking element comes before the first element then move the checking element before the first element
if (checkOffset < firstOffset && checkOffset + checkLength < firstOffset + firstLength) { if (checkOffset < firstOffset && checkOffset + checkLength < firstOffset + firstLength) {
DOMASTNodeLeaf temp = children[j]; DOMASTNodeLeaf temp = children[j];
System.arraycopy( children, i, children, i + 1, j - i ); System.arraycopy(children, i, children, i + 1, j - i);
children[i] = temp; children[i] = temp;
break; break;
} }
@ -147,9 +148,9 @@ public class DOMASTNodeParent extends DOMASTNodeLeaf {
// if the checking element is within the bounds of the first element then it must be a child of that element // if the checking element is within the bounds of the first element then it must be a child of that element
if (checkOffset > firstOffset && checkOffset + checkLength < firstOffset + firstLength) { if (checkOffset > firstOffset && checkOffset + checkLength < firstOffset + firstLength) {
DOMASTNodeLeaf temp = children[j]; DOMASTNodeLeaf temp = children[j];
if( j + 1 < children.length ) if (j + 1 < children.length)
System.arraycopy( children, j + 1, children, j, children.length - j - 1 ); System.arraycopy(children, j + 1, children, j, children.length - j - 1);
children[ children.length - 1 ] = null; children[children.length - 1] = null;
((DOMASTNodeParent)children[i]).addChild(temp); ((DOMASTNodeParent)children[i]).addChild(temp);
j--; j--;
break; break;
@ -162,7 +163,7 @@ public class DOMASTNodeParent extends DOMASTNodeLeaf {
children = ArrayUtil.removeNulls(DOMASTNodeLeaf.class, children); children = ArrayUtil.removeNulls(DOMASTNodeLeaf.class, children);
// need to also clean up the children's children, to make sure all nulls are removed (prevent expansion sign when there isn't one) // need to also clean up the children's children, to make sure all nulls are removed (prevent expansion sign when there isn't one)
for(int i=0; i<children.length; i++) { for (int i=0; i<children.length; i++) {
if (children[i] instanceof DOMASTNodeParent) { if (children[i] instanceof DOMASTNodeParent) {
DOMASTNodeLeaf[] kids = ((DOMASTNodeParent)children[i]).children; DOMASTNodeLeaf[] kids = ((DOMASTNodeParent)children[i]).children;
// remove null elements // remove null elements
@ -209,13 +210,13 @@ public class DOMASTNodeParent extends DOMASTNodeLeaf {
// loop through the chain of nodes and use it to only search the necessary children required to find the node // loop through the chain of nodes and use it to only search the necessary children required to find the node
DOMASTNodeLeaf[] childrenToSearch = children; DOMASTNodeLeaf[] childrenToSearch = children;
int j=childrenToSearch.length-1; int j=childrenToSearch.length-1;
outerLoop: for(int i=nodeChain.length-1; i>=0; i--) { outerLoop: for (int i=nodeChain.length-1; i>=0; i--) {
if (nodeChain[i] != null) { if (nodeChain[i] != null) {
parentToFind = nodeChain[i]; parentToFind = nodeChain[i];
for(; j>=0; j--) { for (; j>=0; j--) {
if (childrenToSearch[j] instanceof DOMASTNodeParent) { if (childrenToSearch[j] instanceof DOMASTNodeParent) {
if ( childrenToSearch[j].getNode() == node.getParent() ) { if (childrenToSearch[j].getNode() == node.getParent()) {
return (DOMASTNodeParent)childrenToSearch[j]; return (DOMASTNodeParent)childrenToSearch[j];
} }
@ -262,13 +263,13 @@ public class DOMASTNodeParent extends DOMASTNodeLeaf {
// loop through the chain of nodes and use it to only search the necessary children required to find the node // loop through the chain of nodes and use it to only search the necessary children required to find the node
DOMASTNodeLeaf[] childrenToSearch = children; DOMASTNodeLeaf[] childrenToSearch = children;
int j=getStartSearch(); int j=getStartSearch();
outerLoop: for(int i=nodeChain.length-1; i>=0; i--) { outerLoop: for (int i=nodeChain.length-1; i>=0; i--) {
if (nodeChain[i] != null) { if (nodeChain[i] != null) {
parentToFind = nodeChain[i]; parentToFind = nodeChain[i];
for(; j>=0; j--) { // use the DOMASTNodeParent's index to start searching at the end of it's children (performance optimization) for (; j>=0; j--) { // use the DOMASTNodeParent's index to start searching at the end of it's children (performance optimization)
if (j<childrenToSearch.length && childrenToSearch[j] instanceof DOMASTNodeParent) { if (j<childrenToSearch.length && childrenToSearch[j] instanceof DOMASTNodeParent) {
if ( childrenToSearch[j].getNode() == node.getParent() ) { if (childrenToSearch[j].getNode() == node.getParent()) {
return (DOMASTNodeParent)childrenToSearch[j]; return (DOMASTNodeParent)childrenToSearch[j];
} }
@ -297,21 +298,21 @@ public class DOMASTNodeParent extends DOMASTNodeLeaf {
} }
@Override @Override
public int relativeNodePosition( IASTNode n ){ public int relativeNodePosition(IASTNode n){
ASTNode astNode = (ASTNode) n; ASTNode astNode = (ASTNode) n;
if( !cleanupedElements ){ if (!cleanupedElements){
cleanChildren(); cleanChildren();
} }
if( children.length > 0 ){ if (children.length > 0){
ASTNode first = (ASTNode) children[0].getNode(); ASTNode first = (ASTNode) children[0].getNode();
if( first.getOffset() > astNode.getOffset() ) if (first.getOffset() > astNode.getOffset())
return -1; return -1;
ASTNode last = (ASTNode) children[ children.length - 1 ].getNode(); ASTNode last = (ASTNode) children[children.length - 1].getNode();
if( (last.getOffset() + last.getLength()) < (astNode.getOffset() + astNode.getLength()) ) if ((last.getOffset() + last.getLength()) < (astNode.getOffset() + astNode.getLength()))
return 1; return 1;
return 0; return 0;
} }
return super.relativeNodePosition( n ); return super.relativeNodePosition(n);
} }
/** /**
* Returns the DOMASTNodeParent that corresponds to the IASTNode. This is the DOMASTNodeParent * Returns the DOMASTNodeParent that corresponds to the IASTNode. This is the DOMASTNodeParent
@ -327,34 +328,34 @@ public class DOMASTNodeParent extends DOMASTNodeLeaf {
if (equalNodes(node, this.getNode(), useOffset)) { if (equalNodes(node, this.getNode(), useOffset)) {
return this; return this;
} }
if( children.length == 0 ) if (children.length == 0)
return null; return null;
if( !cleanupedElements ){ if (!cleanupedElements){
cleanChildren(); cleanChildren();
} }
int a = 0, z = children.length - 1; int a = 0, z = children.length - 1;
int idx = (z - a) / 2 ; int idx = (z - a) / 2 ;
while( true ){ while(true){
int compare = children[ idx ].relativeNodePosition( node ); int compare = children[idx].relativeNodePosition(node);
if( compare == 0 ){ if (compare == 0){
if( children[idx] instanceof DOMASTNodeParent ){ if (children[idx] instanceof DOMASTNodeParent){
return ((DOMASTNodeParent)children[idx]).findTreeObject( node, useOffset ); return ((DOMASTNodeParent)children[idx]).findTreeObject(node, useOffset);
} }
return null; //?? return null; //??
} else if( compare == -1 ) } else if (compare == -1)
z = idx; z = idx;
else else
a = idx; a = idx;
int diff = z - a; int diff = z - a;
if( diff == 0 ) if (diff == 0)
return null; return null;
else if( diff == 1 ) else if (diff == 1)
idx = ( idx == z ) ? a : z; idx = (idx == z) ? a : z;
else else
idx = a + ( z - a ) / 2; idx = a + (z - a) / 2;
if( z == a ) if (z == a)
return null; return null;
if( z - a == 1 && children[ a ].relativeNodePosition( node ) == 1 && children[ z ].relativeNodePosition( node ) == -1 ) if (z - a == 1 && children[a].relativeNodePosition(node) == 1 && children[z].relativeNodePosition(node) == -1)
return null; return null;
} }
} }
@ -372,7 +373,7 @@ public class DOMASTNodeParent extends DOMASTNodeLeaf {
} else { } else {
IASTNodeLocation[] locs1 = node1.getNodeLocations(); IASTNodeLocation[] locs1 = node1.getNodeLocations();
IASTNodeLocation[] locs2 = node2.getNodeLocations(); IASTNodeLocation[] locs2 = node2.getNodeLocations();
for(int i=0; i<locs1.length && i<locs2.length; i++) { for (int i= 0; i < locs1.length && i<locs2.length; i++) {
if (locs1[i].getNodeOffset() != locs2[i].getNodeOffset() || if (locs1[i].getNodeOffset() != locs2[i].getNodeOffset() ||
locs1[i].getNodeLength() != locs2[i].getNodeLength()) locs1[i].getNodeLength() != locs2[i].getNodeLength())
return false; return false;
@ -384,7 +385,7 @@ public class DOMASTNodeParent extends DOMASTNodeLeaf {
return false; return false;
} }
} else { } else {
if ( node1 == node2 ) if (node1 == node2)
return true; return true;
} }
@ -394,5 +395,4 @@ public class DOMASTNodeParent extends DOMASTNodeLeaf {
public void setChildren(DOMASTNodeLeaf[] children) { public void setChildren(DOMASTNodeLeaf[] children) {
this.children = children; this.children = children;
} }
} }