|
67 | 67 | using namespace mlir; |
68 | 68 | using namespace mlir::detail; |
69 | 69 |
|
70 | | -/// Parse a floating point value from an integer literal token. |
71 | | -FailureOr<APFloat> detail::parseFloatFromIntegerLiteral( |
72 | | - function_ref<InFlightDiagnostic()> emitError, const Token &tok, |
73 | | - bool isNegative, const llvm::fltSemantics &semantics) { |
74 | | - StringRef spelling = tok.getSpelling(); |
75 | | - bool isHex = spelling.size() > 1 && spelling[1] == 'x'; |
76 | | - if (!isHex) { |
77 | | - auto error = emitError(); |
78 | | - error << "unexpected decimal integer literal for a " |
79 | | - "floating point value"; |
80 | | - error.attachNote() << "add a trailing dot to make the literal a float"; |
81 | | - return failure(); |
82 | | - } |
83 | | - if (isNegative) { |
84 | | - emitError() << "hexadecimal float literal should not have a " |
85 | | - "leading minus"; |
86 | | - return failure(); |
87 | | - } |
88 | | - |
89 | | - APInt intValue; |
90 | | - tok.getSpelling().getAsInteger(isHex ? 0 : 10, intValue); |
91 | | - auto typeSizeInBits = APFloat::semanticsSizeInBits(semantics); |
92 | | - if (intValue.getActiveBits() > typeSizeInBits) { |
93 | | - return emitError() << "hexadecimal float constant out of range for type"; |
94 | | - return failure(); |
95 | | - } |
96 | | - |
97 | | - APInt truncatedValue(typeSizeInBits, intValue.getNumWords(), |
98 | | - intValue.getRawData()); |
99 | | - return APFloat(semantics, truncatedValue); |
100 | | -} |
101 | | - |
102 | | -FailureOr<APFloat> |
103 | | -detail::parseFloatFromLiteral(function_ref<InFlightDiagnostic()> emitError, |
104 | | - const Token &tok, bool isNegative, |
105 | | - const llvm::fltSemantics &semantics) { |
106 | | - // Check for a floating point value. |
107 | | - if (tok.is(Token::floatliteral)) { |
108 | | - auto val = tok.getFloatingPointValue(); |
109 | | - if (!val) |
110 | | - return emitError() << "floating point value too large"; |
111 | | - |
112 | | - APFloat result(isNegative ? -*val : *val); |
113 | | - bool unused; |
114 | | - result.convert(semantics, APFloat::rmNearestTiesToEven, &unused); |
115 | | - return result; |
116 | | - } |
117 | | - |
118 | | - // Check for a hexadecimal float value. |
119 | | - if (tok.is(Token::integer)) |
120 | | - return parseFloatFromIntegerLiteral(emitError, tok, isNegative, semantics); |
121 | | - |
122 | | - return emitError() << "expected floating point literal"; |
123 | | -} |
124 | | - |
125 | 70 | //===----------------------------------------------------------------------===// |
126 | 71 | // CodeComplete |
127 | 72 | //===----------------------------------------------------------------------===// |
@@ -402,6 +347,61 @@ OptionalParseResult Parser::parseOptionalDecimalInteger(APInt &result) { |
402 | 347 | return success(); |
403 | 348 | } |
404 | 349 |
|
| 350 | +FailureOr<APFloat> |
| 351 | +Parser::parseFloatFromLiteral(const Token &tok, bool isNegative, |
| 352 | + const llvm::fltSemantics &semantics) { |
| 353 | + // Check for a floating point value. |
| 354 | + if (tok.is(Token::floatliteral)) { |
| 355 | + auto val = tok.getFloatingPointValue(); |
| 356 | + if (!val) |
| 357 | + return emitError(tok.getLoc()) << "floating point value too large"; |
| 358 | + |
| 359 | + APFloat result(isNegative ? -*val : *val); |
| 360 | + bool unused; |
| 361 | + result.convert(semantics, APFloat::rmNearestTiesToEven, &unused); |
| 362 | + return result; |
| 363 | + } |
| 364 | + |
| 365 | + // Check for a hexadecimal float value. |
| 366 | + if (tok.is(Token::integer)) |
| 367 | + return parseFloatFromIntegerLiteral(tok, isNegative, semantics); |
| 368 | + |
| 369 | + return emitError(tok.getLoc()) << "expected floating point literal"; |
| 370 | +} |
| 371 | + |
| 372 | +/// Parse a floating point value from an integer literal token. |
| 373 | +FailureOr<APFloat> |
| 374 | +Parser::parseFloatFromIntegerLiteral(const Token &tok, bool isNegative, |
| 375 | + const llvm::fltSemantics &semantics) { |
| 376 | + StringRef spelling = tok.getSpelling(); |
| 377 | + bool isHex = spelling.size() > 1 && spelling[1] == 'x'; |
| 378 | + if (!isHex) { |
| 379 | + auto error = emitError(tok.getLoc()); |
| 380 | + error << "unexpected decimal integer literal for a " |
| 381 | + "floating point value"; |
| 382 | + error.attachNote() << "add a trailing dot to make the literal a float"; |
| 383 | + return failure(); |
| 384 | + } |
| 385 | + if (isNegative) { |
| 386 | + emitError(tok.getLoc()) << "hexadecimal float literal should not have a " |
| 387 | + "leading minus"; |
| 388 | + return failure(); |
| 389 | + } |
| 390 | + |
| 391 | + APInt intValue; |
| 392 | + tok.getSpelling().getAsInteger(isHex ? 0 : 10, intValue); |
| 393 | + auto typeSizeInBits = APFloat::semanticsSizeInBits(semantics); |
| 394 | + if (intValue.getActiveBits() > typeSizeInBits) { |
| 395 | + return emitError(tok.getLoc()) |
| 396 | + << "hexadecimal float constant out of range for type"; |
| 397 | + return failure(); |
| 398 | + } |
| 399 | + |
| 400 | + APInt truncatedValue(typeSizeInBits, intValue.getNumWords(), |
| 401 | + intValue.getRawData()); |
| 402 | + return APFloat(semantics, truncatedValue); |
| 403 | +} |
| 404 | + |
405 | 405 | ParseResult Parser::parseOptionalKeyword(StringRef *keyword) { |
406 | 406 | // Check that the current token is a keyword. |
407 | 407 | if (!isCurrentTokenAKeyword()) |
|
0 commit comments