[cfe-dev] getting int value from IntegerLiteral, float value from FloatingLiteral

Chandler Carruth chandlerc at google.com
Mon Dec 17 03:59:55 PST 2012


On Sat, Dec 15, 2012 at 10:32 PM, Rajendra <rks at cse.iitb.ac.in> wrote:

> Hi,
>
> I want to get precise int value for a variable from IntegerLiteral type
> and float value for a variable from FloatingLiteral type (do not want to
> loose precision).


If you do not want to lose precision, you do not want an 'int' or 'float'.
The literal AST nodes expose an APInt and APFloat (resp.) which encode the
exact value with full precision. See the documentation for their APIs,
including methods to query how much precision is required to represent them
exactly, and other basic properties.


> I don't see a function to call to get int or unsigned int directly.
>
> Is following code only correct way or is there any better way to get these?
>
>     const Expr *e = varDecl->getInit();
>     const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(e);  // e is of
> type Expr*
>     double val = IL->getValue().**signedRoundToDouble();
>     int ival = (int) val;
>     std::cerr << "\tinitial value: " << val << " ival " << ival << "\n";
>
>     const Expr *e = varDecl->getInit();
>     const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(e);  // e is of
> type Expr*
>     double val = FL->getValue().**convertToDouble();
>     float fval = (float) val;
>     std::cerr << "\tinitial value: " << val << " fval " << fval << "\n";
>
> Please advise.
>
> - Rajendra
> ______________________________**_________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/**mailman/listinfo/cfe-dev<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/20121217/b6bb06b2/attachment.html>


More information about the cfe-dev mailing list