IntegerType() = default;
/* implicit */ IntegerType(Type::ImplType *ptr);
+ /// Get or create a new IntegerType of the given width within the context.
+ /// Assume the width is within the allowed range and assert on failures.
+ /// Use getChecked to handle failures gracefully.
static IntegerType get(unsigned width, MLIRContext *context);
+ /// Get or create a new IntegerType of the given width within the context,
+ /// defined at the given, potentially unknown, location. If the width is
+ /// outside the allowed range, emit errors and return a null type.
+ static IntegerType getChecked(unsigned width, MLIRContext *context,
+ Location location);
+
/// Return the bitwidth of this integer type.
unsigned getWidth() const;
return impl.indexType;
}
-IntegerType IntegerType::get(unsigned width, MLIRContext *context) {
- assert(width <= kMaxWidth && "admissible integer bitwidth exceeded");
+static IntegerType getIntegerType(unsigned width, MLIRContext *context,
+ llvm::Optional<Location> location) {
+ if (width > IntegerType::kMaxWidth) {
+ if (location)
+ context->emitError(*location, "integer bitwidth is limited to " +
+ Twine(IntegerType::kMaxWidth) +
+ " bits");
+ return {};
+ }
+
auto &impl = context->getImpl();
auto *&result = impl.integers[width];
if (!result) {
result = impl.allocator.Allocate<IntegerTypeStorage>();
- new (result) IntegerTypeStorage{{Kind::Integer, context}, width};
+ new (result) IntegerTypeStorage{{Type::Kind::Integer, context}, width};
}
return result;
}
+IntegerType IntegerType::getChecked(unsigned width, MLIRContext *context,
+ Location location) {
+ return getIntegerType(width, context, location);
+}
+
+IntegerType IntegerType::get(unsigned width, MLIRContext *context) {
+ auto type = getIntegerType(width, context, None);
+ assert(type && "failed to construct IntegerType");
+ return type;
+}
+
FloatType FloatType::get(Kind kind, MLIRContext *context) {
assert(kind >= Kind::FIRST_FLOATING_POINT_TYPE &&
kind <= Kind::LAST_FLOATING_POINT_TYPE && "Not an FP type kind");
auto width = getToken().getIntTypeBitwidth();
if (!width.hasValue())
return (emitError("invalid integer width"), nullptr);
- if (width > IntegerType::kMaxWidth)
- return (emitError("integer bitwidth is limited to " +
- Twine(IntegerType::kMaxWidth) + " bits"),
- nullptr);
+ auto loc = getEncodedSourceLocation(getToken().getLoc());
consumeToken(Token::inttype);
- return builder.getIntegerType(width.getValue());
+ return IntegerType::getChecked(width.getValue(), builder.getContext(), loc);
}
// float-type