Reputation: 1
I'm writing a compiler as an assignment and I'm having some critical problems with dynamic_cast. I will try to share the key pieces of code that fail to work, along with the essential code one might need to understand what's going on here.
Relevant classes:
#define YYSTYPE Node*
class Node {
public:
Node(){}
virtual ~Node() {}
};
class Type : public Node {
private:
// some vars
public:
Type(/* some vars */) {}
// more methods
}
And some of the rules in my .ypp file:
M: /* epsilon */ { // Marker, generates new llvm label
genLabel();
$$ = new Type(/*...*/);
}
S: RETURN SC {/* unrelated code */}
| WHILE LPAREN M E {/* unrelated code */} RPAREN M S {
Type* m1 = dynamic_cast<Type*>($3);
if(!m1){
cout << "m1 bad" << endl;
}
Type* m2 = dynamic_cast<Type*>($6);
if(!m2){
cout << "m2 bad" << endl;
}
// more unrelated code
}
Note: There are no conflicts with other rules. Bison made sure of that.
The output while parsing some input for example:
Input:
while (true) return;
Output:
m2 bad
I can't figure it out. How does one dynamic cast work fine (based on extra code, and return the right pointer), yet another one that is supposed to do exactly the same fails.
Upvotes: 0
Views: 54