Skip to content

Commit

Permalink
Merge pull request #422 from daphne-eu/dev-issue-420
Browse files Browse the repository at this point in the history
[DAPHNE-#420]: ANTLR Any Conversion.
  • Loading branch information
corepointer authored Aug 27, 2022
2 parents 31a0852 + 3a7f132 commit 3e3927d
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 116 deletions.
56 changes: 31 additions & 25 deletions src/parser/ParserUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,74 +30,74 @@

/**
* @brief General utilities for parsing to DaphneIR.
*
*
*
*
*/
class ParserUtils {
/**
* The OpBuilder used to generate DaphneIR operations.
*/
mlir::OpBuilder & builder;

public:

// ************************************************************************
// `mlir::Type`s corresponsing to the types in `DaphneTypes.td`
// ************************************************************************

/**
* @brief The `mlir::Type` denoted by `Size` in DaphneIR.
*/
const mlir::Type sizeType;

/**
* @brief The `mlir::Type` denoted by `BoolScalar` in DaphneIR.
*/
const mlir::Type boolType;

/**
* @brief The `mlir::Type` denoted by `Seed` in DaphneIR.
*/
const mlir::Type seedType;

/**
* @brief The `mlir::Type` denoted by `StrScalar` in DaphneIR.
*/
const mlir::Type strType;

/**
* @brief A DaphneIR `Matrix` with value type DaphneIR `Size`.
*/
mlir::Type matrixOfSizeType;

/**
* @brief The placeholder for an unknown type.
*/
mlir::Type unknownType;

/**
* @brief Get a `daphne::MatrixType` with the given value type.
* @param vt
* @return
* @return
*/
mlir::daphne::MatrixType matrixOf(mlir::Type vt) {
return mlir::daphne::MatrixType::get(builder.getContext(), vt);
}

/**
* @brief Get a `daphne::MatrixType` with the type of the given value as
* the value type.
* @param vt
* @return
* @return
*/
mlir::daphne::MatrixType matrixOf(mlir::Value v) {
return mlir::daphne::MatrixType::get(builder.getContext(), v.getType());
}

// ************************************************************************
// Constructor
// ************************************************************************

ParserUtils(mlir::OpBuilder & builder)
:
builder(builder),
Expand All @@ -110,15 +110,15 @@ class ParserUtils {
{
// nothing to do
}

// ************************************************************************
// Casting if necessary
// ************************************************************************

/**
* @brief Wraps the given `Value` in a `CastOp` if it does not have the
* given `Type`.
*
*
* @param loc A location.
* @param t The expected type.
* @param v The value.
Expand All @@ -129,19 +129,19 @@ class ParserUtils {
return v;
return builder.create<mlir::daphne::CastOp>(v.getLoc(), t, v);
}

mlir::Value castSizeIf(mlir::Value v) {
return castIf(sizeType, v);
}

mlir::Value castBoolIf(mlir::Value v) {
return castIf(boolType, v);
}

mlir::Value castSeedIf(mlir::Value v) {
return castIf(seedType, v);
}

mlir::Value castStrIf(mlir::Value v) {
return castIf(strType, v);
}
Expand Down Expand Up @@ -204,13 +204,19 @@ class ParserUtils {
// ************************************************************************
// Misc
// ************************************************************************

mlir::Value valueOrError(antlrcpp::Any a) {
if(a.is<mlir::Value>())
return a.as<mlir::Value>();
throw std::runtime_error("something was expected to be an mlir::Value, but it was none");
}

mlir::Type typeOrError(antlrcpp::Any a) {
if(a.is<mlir::Type>())
return a.as<mlir::Type>();
throw std::runtime_error("something was expected to be an mlir::Type, but it was none");
}

/**
* @brief Utility function for getting the file location of the token
* @param start Start token of this rule (usually you want to use `ctx->start`)
Expand All @@ -233,4 +239,4 @@ class ParserUtils {
}
};

#endif //SRC_PARSER_PARSERUTILS_H
#endif //SRC_PARSER_PARSERUTILS_H
Loading

0 comments on commit 3e3927d

Please sign in to comment.