[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

%union and shared pointers to AST nodes

From: Frans Englich
Subject: %union and shared pointers to AST nodes
Date: Fri, 16 Sep 2005 17:14:40 +0000
User-agent: KMail/1.8.50


I have a problem involving bison although a solution may not directly mean 
modifying the bison grammar. Nevertheless, someone might be able to spot a 

From my actions I call factory functions which returns instances I assign to 
the actions($$), in order to build an AST tree. I write in C++, and recently 
converted all code to return/take shared pointers to objects, instead of raw 

When it came to converting the Bison grammar, I of course ran into the problem 
that the union can't house class objects(such as shared_ptr instances), but 
only primitive types. The problem is that the shared pointer goes out of 
scope when the action terminates and hence deletes its object, and that's 
before it have been re-attached to a tree, leading to a dangling pointer 
ending up in the union.

What is the best way to solve this? Here's the possibilities I can find:

* Make the factory functions not return SharedPtr objects, but pointers to 
'new' allocated objects. That would make my API inconsistent, and other 
situations cumbersome.

* After having received the shared pointer object from the function, allocate 
on the heap, assign it to the heap, and then return a pointer to the shared 
pointer(should work, afaict). That sounds ugly and feels slow, to me.

* Do hackish manual reference counting. Manually add the reference count, and 
after it have been assigned to the tree, count down, such that the reference 
count is correct. (This avoids the reference count to reach zero when the 
shared pointer goes out of scope). Hackish, and error prone, IMHO.

What do you suggest? If someone have experience from a similar situation, how 
did you solve it?

Any suggestions are of interest. Currently I'm leaning towards the latter 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]