Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def throw_err() -> None:
raise Exception("This is an error")


print("Before error")

throw_err()

print("After error")
8 changes: 7 additions & 1 deletion src/lython/dialects/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_library(PyDialect STATIC
cpp/PyDialect.cpp
cpp/PyDialectTypes.cpp
cpp/PyVerifier.cpp
cpp/PyVerifier/Common.cpp
cpp/PyVerifier/ClassFunc.cpp
cpp/PyVerifier/Call.cpp
cpp/PyVerifier/Casts.cpp
cpp/PyVerifier/ConstTupleDict.cpp
cpp/PyVerifier/EH.cpp
cpp/PyVerifier/Numeric.cpp
)

add_dependencies(PyDialect PyDialectIncGen)
Expand Down
13 changes: 11 additions & 2 deletions src/lython/dialects/cpp/PyDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ namespace py {

void PyDialect::initialize() {
addTypes<IntType, FloatType, BoolType, StrType, ObjectType, NoneType,
TupleType, DictType, ClassType, FuncSignatureType, FuncType,
PrimFuncType>();
TupleType, DictType, ClassType, ExceptionType, TracebackType,
LocationType, FuncSignatureType, FuncType, PrimFuncType>();

addOperations<
#define GET_OP_LIST
Expand Down Expand Up @@ -99,6 +99,12 @@ Type PyDialect::parseType(DialectAsmParser &parser) const {
return Type();
return ClassType::get(ctx, classNameAttr.getValue());
}
if (keyword == "exception")
return ExceptionType::get(ctx);
if (keyword == "traceback")
return TracebackType::get(ctx);
if (keyword == "location")
return LocationType::get(ctx);
if (keyword == "func") {
if (parser.parseLess())
return Type();
Expand Down Expand Up @@ -214,6 +220,9 @@ void PyDialect::printType(Type type, DialectAsmPrinter &printer) const {
.Case<ClassType>([&](ClassType classTy) {
printer << "class<\"" << classTy.getClassName() << "\">";
})
.Case<ExceptionType>([&](ExceptionType) { printer << "exception"; })
.Case<TracebackType>([&](TracebackType) { printer << "traceback"; })
.Case<LocationType>([&](LocationType) { printer << "location"; })
.Case<FuncSignatureType>([&](FuncSignatureType sigTy) {
printer << "funcsig<";
printTypeList(sigTy.getPositionalTypes());
Expand Down
21 changes: 20 additions & 1 deletion src/lython/dialects/cpp/PyDialectTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ ::llvm::StringRef ClassType::getClassName() const {
return getImpl()->className;
}

ExceptionType ExceptionType::get(MLIRContext *ctx) {
return Base::get(ctx, TypeKind::Exception);
}

TracebackType TracebackType::get(MLIRContext *ctx) {
return Base::get(ctx, TypeKind::Traceback);
}

LocationType LocationType::get(MLIRContext *ctx) {
return Base::get(ctx, TypeKind::Location);
}

FuncSignatureType FuncSignatureType::get(MLIRContext *ctx,
ArrayRef<Type> positional,
ArrayRef<Type> kwonly, Type varargType,
Expand Down Expand Up @@ -183,6 +195,12 @@ bool isPyDictType(Type type) { return mlir::isa<DictType>(type); }

bool isPyClassType(Type type) { return mlir::isa<ClassType>(type); }

bool isPyExceptionType(Type type) { return mlir::isa<ExceptionType>(type); }

bool isPyTracebackType(Type type) { return mlir::isa<TracebackType>(type); }

bool isPyLocationType(Type type) { return mlir::isa<LocationType>(type); }

bool isPyFuncSigType(Type type) { return mlir::isa<FuncSignatureType>(type); }

bool isPyFuncType(Type type) { return mlir::isa<FuncType>(type); }
Expand All @@ -196,7 +214,8 @@ bool isCallableType(Type type) {
bool isPyType(Type type) {
return llvm::TypeSwitch<Type, bool>(type)
.Case<IntType, FloatType, BoolType, StrType, ObjectType, NoneType,
TupleType, DictType, ClassType, FuncType>([](auto) { return true; })
TupleType, DictType, ClassType, ExceptionType, TracebackType,
LocationType, FuncType>([](auto) { return true; })
.Default([](Type) { return false; });
}

Expand Down
45 changes: 45 additions & 0 deletions src/lython/dialects/cpp/PyDialectTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ enum class TypeKind : unsigned {
Tuple,
Dict,
Class, // User-defined class type
Exception,
Traceback,
Location,
FuncSig,
Func,
PrimFunc
Expand Down Expand Up @@ -261,6 +264,45 @@ class ClassType : public mlir::Type::TypeBase<ClassType, mlir::Type,
::llvm::StringRef getClassName() const;
};

class ExceptionType
: public mlir::Type::TypeBase<ExceptionType, mlir::Type,
detail::SimpleTypeStorage> {
public:
using Base::Base;
static constexpr ::llvm::StringLiteral name{"py.exception"};

static ExceptionType get(mlir::MLIRContext *ctx);
static bool kindof(unsigned kind) {
return kind == static_cast<unsigned>(TypeKind::Exception);
}
};

class TracebackType
: public mlir::Type::TypeBase<TracebackType, mlir::Type,
detail::SimpleTypeStorage> {
public:
using Base::Base;
static constexpr ::llvm::StringLiteral name{"py.traceback"};

static TracebackType get(mlir::MLIRContext *ctx);
static bool kindof(unsigned kind) {
return kind == static_cast<unsigned>(TypeKind::Traceback);
}
};

class LocationType
: public mlir::Type::TypeBase<LocationType, mlir::Type,
detail::SimpleTypeStorage> {
public:
using Base::Base;
static constexpr ::llvm::StringLiteral name{"py.location"};

static LocationType get(mlir::MLIRContext *ctx);
static bool kindof(unsigned kind) {
return kind == static_cast<unsigned>(TypeKind::Location);
}
};

class FuncSignatureType
: public mlir::Type::TypeBase<FuncSignatureType, mlir::Type,
detail::FuncSignatureStorage> {
Expand Down Expand Up @@ -323,6 +365,9 @@ bool isPyNoneType(mlir::Type type);
bool isPyTupleType(mlir::Type type);
bool isPyDictType(mlir::Type type);
bool isPyClassType(mlir::Type type);
bool isPyExceptionType(mlir::Type type);
bool isPyTracebackType(mlir::Type type);
bool isPyLocationType(mlir::Type type);
bool isPyFuncSigType(mlir::Type type);
bool isPyFuncType(mlir::Type type);
bool isPyPrimFuncType(mlir::Type type);
Expand Down
Loading