-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added implementation and test cases for MIRMetadata
- Loading branch information
1 parent
08c3ae8
commit 8a2e1e0
Showing
3 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#ifndef MIR_METADATA_H | ||
#define MIR_METADATA_H | ||
|
||
#include <memory> | ||
#include <cassert> | ||
namespace graphit { | ||
namespace mir { | ||
|
||
template<typename T> | ||
class MIRMetadataImpl; | ||
|
||
// The abstract class for the mir metadata | ||
// Different templated metadata types inherit from this type | ||
class MIRMetadata: public std::enable_shared_from_this<MIRMetadata> { | ||
public: | ||
typedef std::shared_ptr<MIRMetadata> Ptr; | ||
virtual ~MIRMetadata() = default; | ||
|
||
|
||
template <typename T> | ||
bool isa (void) { | ||
if(std::dynamic_pointer_cast<MIRMetadataImpl<T>>(shared_from_this())) | ||
return true; | ||
return false; | ||
} | ||
template <typename T> | ||
std::shared_ptr<MIRMetadataImpl<T>> to(void) { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
std::shared_ptr<MIRMetadataImpl<T>> ret = std::dynamic_pointer_cast<MIRMetadataImpl<T>>(shared_from_this()); | ||
assert(ret != nullptr); | ||
return ret; | ||
} | ||
}; | ||
|
||
// Templated metadata class for each type | ||
template<typename T> | ||
class MIRMetadataImpl: public MIRMetadata { | ||
public: | ||
typedef std::shared_ptr<MIRMetadataImpl<T>> Ptr; | ||
T val; | ||
MIRMetadataImpl(T _val): val(_val) { | ||
} | ||
}; | ||
|
||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,4 +110,76 @@ TEST_F(MidendTest, SimpleVertexSetDeclAllocWithMain) { | |
"const vertices : vertexset{Vertex} = new vertexset{Vertex}(5);\n" | ||
"func main() print 4; end"); | ||
EXPECT_EQ (0, basicTest(is)); | ||
} | ||
} | ||
|
||
// Test cases for the MIRMetadata API | ||
TEST_F(MidendTest, SimpleMetadataTest) { | ||
istringstream is("func main() print 4; end"); | ||
EXPECT_EQ(0, basicTest(is)); | ||
EXPECT_EQ(true, mir_context_->isFunction("main")); | ||
|
||
mir::FuncDecl::Ptr main_func = mir_context_->getFunction("main"); | ||
|
||
main_func->setMetadata<bool>("basic_boolean_md", true); | ||
main_func->setMetadata<int>("basic_int_md", 42); | ||
EXPECT_EQ(true, main_func->hasMetadata<bool>("basic_boolean_md")); | ||
EXPECT_EQ(true, main_func->getMetadata<bool>("basic_boolean_md")); | ||
|
||
EXPECT_EQ(true, main_func->hasMetadata<int>("basic_int_md")); | ||
EXPECT_EQ(42, main_func->getMetadata<int>("basic_int_md")); | ||
|
||
} | ||
TEST_F(MidendTest, SimpleMetadataTestNoExist) { | ||
istringstream is("func main() print 4; end"); | ||
EXPECT_EQ(0, basicTest(is)); | ||
EXPECT_EQ(true, mir_context_->isFunction("main")); | ||
|
||
mir::FuncDecl::Ptr main_func = mir_context_->getFunction("main"); | ||
|
||
main_func->setMetadata<int>("basic_int_md", 42); | ||
EXPECT_EQ(false, main_func->hasMetadata<int>("other_int_md")); | ||
EXPECT_EQ(false, main_func->hasMetadata<bool>("basic_int_md")); | ||
} | ||
|
||
TEST_F(MidendTest, SimpleMetadataTestString) { | ||
istringstream is("func main() print 4; end"); | ||
EXPECT_EQ(0, basicTest(is)); | ||
EXPECT_EQ(true, mir_context_->isFunction("main")); | ||
|
||
mir::FuncDecl::Ptr main_func = mir_context_->getFunction("main"); | ||
|
||
main_func->setMetadata<std::string>("basic_str_md", "md value"); | ||
EXPECT_EQ(true, main_func->hasMetadata<std::string>("basic_str_md")); | ||
EXPECT_EQ("md value", main_func->getMetadata<std::string>("basic_str_md")); | ||
} | ||
|
||
TEST_F(MidendTest, SimpleMetadataTestMIRNodeAsMD) { | ||
istringstream is("const val:int = 42;\nfunc main() print val; end"); | ||
EXPECT_EQ(0, basicTest(is)); | ||
EXPECT_EQ(true, mir_context_->isFunction("main")); | ||
EXPECT_EQ(1, mir_context_->getConstants().size()); | ||
|
||
mir::FuncDecl::Ptr main_func = mir_context_->getFunction("main"); | ||
mir::VarDecl::Ptr decl = mir_context_->getConstants()[0]; | ||
|
||
main_func->setMetadata<mir::MIRNode::Ptr>("used_var_md", decl); | ||
|
||
EXPECT_EQ(true, main_func->hasMetadata<mir::MIRNode::Ptr>("used_var_md")); | ||
mir::MIRNode::Ptr mdnode = main_func->getMetadata<mir::MIRNode::Ptr>("used_var_md"); | ||
EXPECT_EQ(true, mir::isa<mir::VarDecl>(mdnode)); | ||
} | ||
|
||
TEST_F(MidendTest, SimpleMetadataTestMIRNodeVectorAsMD) { | ||
istringstream is("const val:int = 42;\nconst val2: int = 55;\nfunc main() print val + val2; end"); | ||
EXPECT_EQ(0, basicTest(is)); | ||
EXPECT_EQ(true, mir_context_->isFunction("main")); | ||
EXPECT_EQ(2, mir_context_->getConstants().size()); | ||
|
||
mir::FuncDecl::Ptr main_func = mir_context_->getFunction("main"); | ||
std::vector<mir::VarDecl::Ptr> decls = mir_context_->getConstants(); | ||
|
||
main_func->setMetadata<std::vector<mir::VarDecl::Ptr>>("used_vars_md", decls); | ||
|
||
EXPECT_EQ(true, main_func->hasMetadata<std::vector<mir::VarDecl::Ptr>>("used_vars_md")); | ||
EXPECT_EQ(2, main_func->getMetadata<std::vector<mir::VarDecl::Ptr>>("used_vars_md").size()); | ||
} | ||
This comment has been minimized.
Sorry, something went wrong.
yunmingzhang17
Collaborator
|
Could we use " typename MIRMetadataImp::Ptr" instead of "std::shared_ptr<MIRMetadataImpl>"? I think " typename MIRMetadataImp::Ptr" is more consistent with our other type declarations?