[cfe-dev] Transform Enum constants in the source to their integral values

Nikola Smiljanic popizdeh at gmail.com
Thu Aug 7 00:08:21 PDT 2014


My guess is get the initializer for the variable declaration, cast it to
appropriate expression class, get the value from that.

// Enum e = One;

VarDecl *Var = // declaration for 'e'
DeclRefExpr *Expr = dyn_cast<DeclRefExpr>(Var->getInit());
EnumConstantDecl *ED = dyn_cast<EnumConstantDecl>(Expr->getFoundDecl());
int N = ED->getInitVal();


On Thu, Aug 7, 2014 at 4:47 PM, Manasij Mukherjee <manasij7479 at gmail.com>
wrote:

> That makes sense.
> How do I get the value of 'One' from the VarDecl.
> I ultimately want to do:
> Enum e = 0; // if the enum constant One is defaulted to 0
>
>
>
> On Thu, Aug 7, 2014 at 5:14 AM, Nikola Smiljanic <popizdeh at gmail.com>
> wrote:
>
>> enum Enum { One, Two };
>> Enum e = One;
>>
>> 'One' and 'Two' are EnumConstantDecls but 'e' is a VarDecl whose type is
>> Enum (EnumType).
>>
>>
>>
>> On Thu, Aug 7, 2014 at 5:37 AM, Manasij Mukherjee <manasij7479 at gmail.com>
>> wrote:
>>
>>> Hi,
>>> I want to transform a source file so that Enum constants are replaced by
>>> their respective integral values.
>>> More specifically, this should work on variable initializations and
>>> function or template default argument values.
>>>
>>> I can obtain a VarDecl for functions and variables and a
>>> NonTypeTemplateParmDecl for the templates.
>>> (By using DeclVisitor)
>>>
>>> How can I detect if these represent Enum Constants ?
>>> And then get their value, possibly.
>>>
>>> I tried implementing VisitEnumConstantDecl, but that doesn't seem to get
>>> called for these cases.
>>>
>>> Thanks,
>>> Manasij Mukherjee
>>>
>>>
>>> _______________________________________________
>>> cfe-dev mailing list
>>> cfe-dev at cs.uiuc.edu
>>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140807/c0af821b/attachment.html>


More information about the cfe-dev mailing list