This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Getting type used in THROW_EXPR


Richard Guenther wrote:
> On 10/14/06, Brendon Costa <brendon@christian.net> wrote:
>> Hi all,
>>
>> I have yet another question that has arisen as i have started testing my
>> code. Basically I am trying to get the type that is being used in
>> throwing an exception.
>>
>>
>> Is there a simple macro i can use to get the type of an exception from a
>> THROW_EXPR? I think this is a matter of getting the TREE_TYPE for the
>> value passed into the function: except.c: build_throw(tree exp)
>
> If you look at cp/cp-tree.def you will see
>
> /* A throw expression.  operand 0 is the expression, if there was one,
>   else it is NULL_TREE.  */
> DEFTREECODE (THROW_EXPR, "throw_expr", tcc_expression, 1)
>
> which means that TREE_OPERAND (t, 0) is the expression thrown.  Based
> on whether that is a reference already or not, you need to create a
> reference by your own using build1 (ADDR_EXPR, ...) with a properly
> constructed reference type (I guess there's some helper for that in the
> C++ frontend).

Thanks for the fast reply. I have read that documentation before. The
problem is that the expression that you get from TREE_OPERAND(t, 0) is
not the same as the one that is passed into the except.c:
build_throw(tree exp) function which is the actual expression used to
determine the exception type.

Looking through the code for the function build_throw(), it adds
NOP_EXPR nodes, numerous compound expressions + cleanup nodes etc to the
original expression and this jumble of additional expressions is what is
considered the "expression" in the above comment (it somewhere contains
a link to the original expression somewhere). So getting the tree type
of that expression will not give me the type for the exception being
thrown, unlike getting the type of the expression that is initially
passed into that function.


For example getting the TREE_TYPE for the code below of the "exp" node
passed into build_throw() gives a node type of:
RECORD_TYPE

where as doing the same on TREE_OPERAND(t, 0) gives:
VOID_TYPE


class C
{
};

int main()
{
	throw C();
	return 0;
}


Also my terminology is not quite correct, when i say "reference" i meant
i need to get a pointer to the tree node that has the information, i.e.
a reference to the tree node with the information. Not a ADDR_EXPR.
Sorry for the confusing use of terminology.


Thanks,
Brendon.



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]