-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add minimizer that would replace references to complex, user-defined types with simple "int"
- Loading branch information
Showing
4 changed files
with
66 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
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,53 @@ | ||
#include "OneTimeRewriter.hpp" | ||
|
||
class IsPrimitive : public SyntaxVisitor<IsPrimitive> { | ||
public: | ||
// We don't visit the children, so it might not be obvious why we're using a Visitor. | ||
// We do it so we can use SyntaxNode::visit for dynamic type dispatch. | ||
bool isPrimitive; | ||
|
||
template <typename T> | ||
void handle(const T& t) { | ||
isPrimitive = false; | ||
} | ||
|
||
void handle(const IntegerTypeSyntax& t) { | ||
isPrimitive = true; | ||
} | ||
|
||
void handle(const KeywordTypeSyntax& t) { | ||
isPrimitive = true; | ||
} | ||
|
||
void handle(const ImplicitTypeSyntax& t) { | ||
isPrimitive = true; | ||
} | ||
}; | ||
|
||
|
||
class TypeSimplifier : public OneTimeRewriter<TypeSimplifier> { | ||
// replace references to user-defined types with int | ||
public: | ||
bool canSimplify(not_null<const DataTypeSyntax*> node) { | ||
IsPrimitive checker; | ||
node->visit(checker); | ||
return !checker.isPrimitive; | ||
} | ||
|
||
IntegerTypeSyntax* makeIntNode(SourceLocation location) { | ||
auto token = Token(alloc, parsing::TokenKind::IntKeyword, {&SingleSpace, 1}, "int", location); | ||
auto node = IntegerTypeSyntax(SyntaxKind::IntType, token, {}, SyntaxList<VariableDimensionSyntax>({})); | ||
return alloc.emplace<IntegerTypeSyntax>(node); | ||
} | ||
|
||
ShouldVisitChildren handle(const DataTypeSyntax& node, bool isNodeRemovable) { | ||
if (state == REMOVAL_ALLOWED && canSimplify(&node)) { | ||
replaceNode(node, *makeIntNode(node.sourceRange().start())); | ||
} | ||
return VISIT_CHILDREN; | ||
} | ||
}; | ||
|
||
template bool rewriteLoop<TypeSimplifier>(std::shared_ptr<SyntaxTree>& tree, | ||
std::string stageName, | ||
std::string passIdx); |