-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
104 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/// \file | ||
/// \brief ParentMap analysis. | ||
/// | ||
/// This is used to construct upward edges. For each node, record it's direct | ||
/// parent. (Abstract Syntax TREE only have one parent for each node). | ||
|
||
#pragma once | ||
|
||
#include "nixf/Basic/Nodes/Basic.h" | ||
|
||
#include <map> | ||
|
||
namespace nixf { | ||
|
||
class ParentMapAnalysis { | ||
std::map<const Node *, const Node *> ParentMap; | ||
|
||
void dfs(const Node *N, const Node *Parent); | ||
|
||
public: | ||
void runOnAST(const Node &Root); | ||
|
||
const Node *query(const Node &N); | ||
|
||
static bool isRoot(const Node *Up, const Node &N); | ||
|
||
/// \brief Search up until the node becomes a concrete expression. | ||
/// a | ||
/// ^<----- ID -> ExprVar | ||
const Node *upExpr(const Node &N); | ||
|
||
bool isRoot(const Node &N); | ||
}; | ||
|
||
} // namespace nixf |
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,38 @@ | ||
#include "nixf/Sema/ParentMap.h" | ||
|
||
using namespace nixf; | ||
|
||
void ParentMapAnalysis::dfs(const Node *N, const Node *Parent) { | ||
if (!N) | ||
return; | ||
ParentMap.insert({N, Parent}); | ||
for (const Node *Ch : N->children()) | ||
dfs(Ch, N); | ||
} | ||
|
||
const Node *ParentMapAnalysis::query(const Node &N) { | ||
return ParentMap.contains(&N) ? ParentMap.at(&N) : nullptr; | ||
} | ||
|
||
const Node *ParentMapAnalysis::upExpr(const Node &N) { | ||
|
||
if (Expr::isExpr(N.kind())) | ||
return &N; | ||
const Node *Up = query(N); | ||
if (isRoot(Up, N) || !Up) | ||
return nullptr; | ||
return upExpr(*Up); | ||
} | ||
|
||
void ParentMapAnalysis::runOnAST(const Node &Root) { | ||
// Special case. Root node has itself as "parent". | ||
dfs(&Root, &Root); | ||
} | ||
|
||
bool nixf::ParentMapAnalysis::isRoot(const Node *Up, const Node &N) { | ||
return Up == &N; | ||
} | ||
|
||
bool nixf::ParentMapAnalysis::isRoot(const Node &N) { | ||
return isRoot(query(N), N); | ||
} |
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,29 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include "nixf/Basic/Diagnostic.h" | ||
#include "nixf/Parse/Parser.h" | ||
#include "nixf/Sema/ParentMap.h" | ||
|
||
using namespace nixf; | ||
|
||
namespace { | ||
|
||
struct ParentMapTest : testing::Test { | ||
std::vector<Diagnostic> Diags; | ||
|
||
ParentMapAnalysis PMA; | ||
}; | ||
|
||
TEST_F(ParentMapTest, Basic) { | ||
auto AST = nixf::parse(R"(a)", Diags); | ||
PMA.runOnAST(*AST); | ||
|
||
const Node *ID = AST->descend({{0, 0}, {0, 1}}); | ||
ASSERT_EQ(ID->kind(), Node::NK_Identifer); | ||
|
||
const Node *Var = PMA.upExpr(*ID); | ||
|
||
ASSERT_EQ(Var->kind(), Node::NK_ExprVar); | ||
} | ||
|
||
} // namespace |