I really tried searching for answer in this forum for such a question but none seems to work so far.
I want to type check a method declaration such as:
public int stackOverFlow() {int a; a = a + 1; return 0;}
The type of the return expression must match the return type of the method(which in this example is true).
I use Java Tree Builder which generates syntax trees for all my non-terminals in my grammar(in the form of nodes) and a default depth-first visitor.
I have a MethodDeclaration class that implements a Node interface. The node interface has an accept method of the form:
public Node accept(TypeVisitor v){ return v.visit(v));
This accept method makes it possible for a TypeVisitor to visit a MethodDeclaration.
Now to visit a method declaration, I do one simple type checking
public Node visit(MethodDeclaration n){
// this visits the f10 Node, which is the return expression,
// and returns a specific Node object
Node rtype = n.f10.accept(this);
// this also does a similar thing by visitng the f1 Node,
// the method's return type, and returns a specific Node Object
Node acType = n.f1.accept(this);
// Now if I compare the two specific Node objects, it always fails.
if(rtype == acType){
//enter here
}
}
Why does it not enter the if-body? I also tried rtype.equals(acType)
and it returns false.
I tried rtype.toString.equals(acType.toString())
which also returns false.
I tried stepping into the code using the eclipse debugger and here is the output:
rtype IntegerType (id=67)
acType IntegerType (id=69)
As can be seen from the debugger output both rtype and acType are IntegerType objects.
Any idea why the comparision is failing?
If i use if(rtype instanceof IntegerType) this returns true and
If i use if(acType instanceof IntegerType) this also returns true.
But the object comparision always fails?
I am using JavaCC(for parser generation), JTB(AST and Visitors creator), eclipse and java 1.7