Add parsing for floating point attributes.
This is doing it in a suboptimal manner by recombining [integer period literal] into a string literal and parsing that via to_float.
PiperOrigin-RevId: 206855106
diff --git a/lib/Parser/Parser.cpp b/lib/Parser/Parser.cpp
index b20a40a..aa08cb6 100644
--- a/lib/Parser/Parser.cpp
+++ b/lib/Parser/Parser.cpp
@@ -21,6 +21,8 @@
#include "mlir/Parser.h"
#include "Lexer.h"
+#include "llvm/ADT/DenseMap.h"
+#include "llvm/Support/SourceMgr.h"
#include "mlir/IR/AffineExpr.h"
#include "mlir/IR/AffineMap.h"
#include "mlir/IR/Attributes.h"
@@ -31,8 +33,6 @@
#include "mlir/IR/OperationSet.h"
#include "mlir/IR/Statements.h"
#include "mlir/IR/Types.h"
-#include "llvm/ADT/DenseMap.h"
-#include "llvm/Support/SourceMgr.h"
using namespace mlir;
using llvm::SMLoc;
using llvm::SourceMgr;
@@ -582,6 +582,14 @@
consumeToken(Token::kw_false);
return builder.getBoolAttr(false);
+ case Token::floatliteral: {
+ auto val = getToken().getFloatingPointValue();
+ if (!val.hasValue())
+ return (emitError("floating point value too large for attribute"),
+ nullptr);
+ consumeToken(Token::floatliteral);
+ return builder.getFloatAttr(val.getValue());
+ }
case Token::integer: {
auto val = getToken().getUInt64IntegerValue();
if (!val.hasValue() || (int64_t)val.getValue() < 0)
@@ -599,6 +607,14 @@
consumeToken(Token::integer);
return builder.getIntegerAttr((int64_t)-val.getValue());
}
+ if (getToken().is(Token::floatliteral)) {
+ auto val = getToken().getFloatingPointValue();
+ if (!val.hasValue())
+ return (emitError("floating point value too large for attribute"),
+ nullptr);
+ consumeToken(Token::floatliteral);
+ return builder.getFloatAttr(-val.getValue());
+ }
return (emitError("expected constant integer or floating point value"),
nullptr);
@@ -629,7 +645,6 @@
if (affineMap != nullptr)
return builder.getAffineMapAttr(affineMap);
- // TODO: Handle floating point.
return (emitError("expected constant attribute value"), nullptr);
}
}