Repository navigation
New validators #257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Nemoumbra
wants to merge
3
commits into
WerWolv:master
Choose a base branch
from
Nemoumbra:new-validators
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
New validators #257
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
18 changes: 18 additions & 0 deletions
18
lib/include/pl/core/ast/visitors/control_flow_validator.hpp
This file contains hidden or 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,18 @@ | ||
| #pragma once | ||
|
|
||
| #include <pl/core/ast/visitors/ast_validator.hpp> | ||
|
|
||
| namespace pl::core { | ||
| class ControlFlowValidator final : public ASTValidator { | ||
| uint32_t m_loopsCount = 0; | ||
|
|
||
| void reset() override; | ||
|
|
||
| void visit(ast::ASTNodeFunctionDefinition& node) override; | ||
| void visit(ast::ASTNodeWhileStatement& node) override; | ||
| void visit(ast::ASTNodeMatchStatement& node) override; | ||
| void visit(ast::ASTNodeTryCatchStatement& node) override; | ||
| void visit(ast::ASTNodeConditionalStatement& node) override; | ||
| void visit(ast::ASTNodeControlFlowStatement& node) override; | ||
| }; | ||
| } |
This file contains hidden or 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,55 @@ | ||
| #pragma once | ||
|
|
||
| #include <pl/core/ast/visitors/ast_validator.hpp> | ||
| #include <stack> | ||
|
|
||
| namespace pl::core { | ||
| class ScopeValidator final : public ASTValidator { | ||
|
|
||
| struct Scope { | ||
| std::unordered_set<std::string> m_names; | ||
|
|
||
| [[nodiscard]] bool newIdentifier(const std::string& name); | ||
|
|
||
| void mergeWith(Scope& from); | ||
| }; | ||
|
|
||
| std::stack<Scope> m_scopes; | ||
|
|
||
| u32 m_curNonFuncScopes = 0; | ||
|
|
||
| public: | ||
| ScopeValidator(); | ||
|
|
||
| private: | ||
| void reset() override; | ||
|
|
||
| void pushScope(); | ||
| void pushScope(Scope&& scope); | ||
| void popScope(); | ||
| Scope ¤tScope(); | ||
|
|
||
| [[nodiscard]] bool isFunctionScope() const; | ||
|
|
||
| [[nodiscard]] bool newIdentifier(const std::string& name); | ||
|
|
||
|
|
||
| void visit(ast::ASTNodeVariableDecl& node) override; | ||
| void visit(ast::ASTNodeArrayVariableDecl& node) override; | ||
| void visit(ast::ASTNodePointerVariableDecl& node) override; | ||
| void visit(ast::ASTNodeBitfieldField& node) override; | ||
| void visit(ast::ASTNodeMultiVariableDecl& node) override; | ||
| void visit(ast::ASTNodeTypeDecl& node) override; | ||
| void visit(ast::ASTNodeStruct& node) override; | ||
| void visit(ast::ASTNodeUnion& node) override; | ||
| void visit(ast::ASTNodeBitfield& node) override; | ||
| void visit(ast::ASTNodeBitfieldArrayVariableDecl& node) override; | ||
| void visit(ast::ASTNodeEnum& node) override; | ||
| void visit(ast::ASTNodeConditionalStatement& node) override; | ||
| void visit(ast::ASTNodeFunctionDefinition& node) override; | ||
| void visit(ast::ASTNodeCompoundStatement& node) override; | ||
| void visit(ast::ASTNodeWhileStatement& node) override; | ||
| void visit(ast::ASTNodeMatchStatement& node) override; | ||
| void visit(ast::ASTNodeTryCatchStatement& node) override; | ||
| }; | ||
| } |
This file contains hidden or 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 hidden or 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
69 changes: 69 additions & 0 deletions
69
lib/source/pl/core/ast/visitors/control_flow_validator.cpp
This file contains hidden or 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,69 @@ | ||
|
|
||
| #include <pl/core/ast/visitors/control_flow_validator.hpp> | ||
|
|
||
| #include <pl/core/ast/ast_node_function_definition.hpp> | ||
| #include <pl/core/ast/ast_node_control_flow_statement.hpp> | ||
| #include <pl/core/ast/ast_node_try_catch_statement.hpp> | ||
| #include <pl/core/ast/ast_node_while_statement.hpp> | ||
| #include <pl/core/ast/ast_node_match_statement.hpp> | ||
| #include <pl/core/ast/ast_node_conditional_statement.hpp> | ||
|
|
||
| namespace pl::core { | ||
| void ControlFlowValidator::visit(ast::ASTNodeFunctionDefinition &node) { | ||
| visitTypeErasedNodes(node.getBody()); | ||
| } | ||
|
|
||
| void ControlFlowValidator::visit(ast::ASTNodeWhileStatement &node) { | ||
| ++m_loopsCount; | ||
| visitTypeErasedNodes(node.getBody()); | ||
| --m_loopsCount; | ||
| } | ||
|
|
||
| void ControlFlowValidator::visit(ast::ASTNodeMatchStatement &node) { | ||
| if (const auto& default_case = node.getDefaultCase(); default_case) { | ||
| const auto& body = (*default_case).body; | ||
| visitTypeErasedNodes(body); | ||
| } | ||
| for (const auto& match_case: node.getNonDefaultCases()) { | ||
| visitTypeErasedNodes(match_case.body); | ||
| } | ||
| } | ||
|
|
||
| void ControlFlowValidator::visit(ast::ASTNodeTryCatchStatement &node) { | ||
| visitTypeErasedNodes(node.getTryBody()); | ||
| visitTypeErasedNodes(node.getCatchBody()); | ||
| } | ||
|
|
||
| void ControlFlowValidator::visit(ast::ASTNodeConditionalStatement &node) { | ||
| visitTypeErasedNodes(node.getTrueBody()); | ||
| visitTypeErasedNodes(node.getFalseBody()); | ||
| } | ||
|
|
||
| void ControlFlowValidator::visit(ast::ASTNodeControlFlowStatement &node) { | ||
| if (m_loopsCount > 0) { | ||
| // Nothing bad. | ||
| return; | ||
| } | ||
| // Could be bad, let's check | ||
|
|
||
| auto type = node.getType(); | ||
| switch (type) { | ||
| case ControlFlowStatement::Break: { | ||
| error("Break statements can only be used within a loop."); | ||
| return; | ||
| } | ||
| case ControlFlowStatement::Continue: { | ||
| error("Continue statements can only be used within a loop."); | ||
| return; | ||
| } | ||
| default: { | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void ControlFlowValidator::reset() { | ||
| // This can't become anything else, but why not make it explicit... | ||
| m_loopsCount = 0; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From what I can tell, this only handles control flow statements in functions but not inside types, correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.