getting int value from IntegerLiteral, float value from FloatingLiteral

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). 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

Any suggestions on this please?

-Rajendra

You can get the value with the llvm::APInt::getLimitedValue method.

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.