-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setting
use_range_ptr_var
NmodlType in SymTab (#1139)
Created FunctionCallpathVisitor that traverses the function calls and sets `use_range_ptr_var` property
- Loading branch information
1 parent
d70c51b
commit d1290e0
Showing
7 changed files
with
224 additions
and
3 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
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,95 @@ | ||
|
||
/* | ||
* Copyright 2024 Blue Brain Project, EPFL. | ||
* See the top-level LICENSE file for details. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "visitors/function_callpath_visitor.hpp" | ||
|
||
namespace nmodl { | ||
namespace visitor { | ||
|
||
using symtab::Symbol; | ||
using symtab::syminfo::NmodlType; | ||
|
||
void FunctionCallpathVisitor::visit_var_name(const ast::VarName& node) { | ||
if (visited_functions_or_procedures.empty()) { | ||
return; | ||
} | ||
/// If node is either a RANGE var, a POINTER or a BBCOREPOINTER then | ||
/// the FUNCTION or PROCEDURE it's used in should have the `use_range_ptr_var` | ||
/// property | ||
auto sym = psymtab->lookup(node.get_node_name()); | ||
const auto properties = NmodlType::range_var | NmodlType::pointer_var | | ||
NmodlType::bbcore_pointer_var; | ||
if (sym && sym->has_any_property(properties)) { | ||
const auto top = visited_functions_or_procedures.back(); | ||
const auto caller_func_name = | ||
top->is_function_block() | ||
? dynamic_cast<const ast::FunctionBlock*>(top)->get_node_name() | ||
: dynamic_cast<const ast::ProcedureBlock*>(top)->get_node_name(); | ||
auto caller_func_proc_sym = psymtab->lookup(caller_func_name); | ||
caller_func_proc_sym->add_properties(NmodlType::use_range_ptr_var); | ||
} | ||
} | ||
|
||
void FunctionCallpathVisitor::visit_function_call(const ast::FunctionCall& node) { | ||
if (visited_functions_or_procedures.empty()) { | ||
return; | ||
} | ||
const auto name = node.get_node_name(); | ||
const auto func_symbol = psymtab->lookup(name); | ||
if (!func_symbol || | ||
!func_symbol->has_any_property(NmodlType::function_block | NmodlType::procedure_block) || | ||
func_symbol->get_nodes().empty()) { | ||
return; | ||
} | ||
/// Visit the called FUNCTION/PROCEDURE AST node to check whether | ||
/// it has `use_range_ptr_var` property. If it does the currently called | ||
/// function needs to have it too. | ||
const auto func_block = func_symbol->get_nodes()[0]; | ||
func_block->accept(*this); | ||
if (func_symbol->has_any_property(NmodlType::use_range_ptr_var)) { | ||
const auto top = visited_functions_or_procedures.back(); | ||
auto caller_func_name = | ||
top->is_function_block() | ||
? dynamic_cast<const ast::FunctionBlock*>(top)->get_node_name() | ||
: dynamic_cast<const ast::ProcedureBlock*>(top)->get_node_name(); | ||
auto caller_func_proc_sym = psymtab->lookup(caller_func_name); | ||
caller_func_proc_sym->add_properties(NmodlType::use_range_ptr_var); | ||
} | ||
} | ||
|
||
void FunctionCallpathVisitor::visit_procedure_block(const ast::ProcedureBlock& node) { | ||
/// Avoid recursive calls | ||
if (std::find(visited_functions_or_procedures.begin(), | ||
visited_functions_or_procedures.end(), | ||
&node) != visited_functions_or_procedures.end()) { | ||
return; | ||
} | ||
visited_functions_or_procedures.push_back(&node); | ||
node.visit_children(*this); | ||
visited_functions_or_procedures.pop_back(); | ||
} | ||
|
||
void FunctionCallpathVisitor::visit_function_block(const ast::FunctionBlock& node) { | ||
// Avoid recursive calls | ||
if (std::find(visited_functions_or_procedures.begin(), | ||
visited_functions_or_procedures.end(), | ||
&node) != visited_functions_or_procedures.end()) { | ||
return; | ||
} | ||
visited_functions_or_procedures.push_back(&node); | ||
node.visit_children(*this); | ||
visited_functions_or_procedures.pop_back(); | ||
} | ||
|
||
void FunctionCallpathVisitor::visit_program(const ast::Program& node) { | ||
psymtab = node.get_symbol_table(); | ||
node.visit_children(*this); | ||
} | ||
|
||
} // namespace visitor | ||
} // namespace nmodl |
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,65 @@ | ||
/* | ||
* Copyright 2024 Blue Brain Project, EPFL. | ||
* See the top-level LICENSE file for details. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#pragma once | ||
|
||
/** | ||
* \file | ||
* \brief \copybrief nmodl::visitor::FunctionCallpathVisitor | ||
*/ | ||
|
||
#include "ast/all.hpp" | ||
#include "symtab/decl.hpp" | ||
#include "visitors/ast_visitor.hpp" | ||
|
||
namespace nmodl { | ||
namespace visitor { | ||
|
||
/** | ||
* \addtogroup visitor_classes | ||
* \{ | ||
*/ | ||
|
||
/** | ||
* \class FunctionCallpathVisitor | ||
* \brief %Visitor for traversing \c FunctionBlock s and \c ProcedureBlocks through | ||
* their \c FunctionCall s | ||
* | ||
* This visitor is used to traverse the \c FUNCTION s and \c PROCEDURE s in the NMODL files. | ||
* It visits the \c FunctionBlock s and \c ProcedureBlock s and if there is a \c FunctionCall | ||
* in those, it visits the \c FunctionBlock or \c ProcedureBlock of the \c FunctionCall. | ||
* Currently it only checks whether in this path of function calls there is any use of \c RANGE , | ||
* \c POINTER or \c BBCOREPOINTER variable. In case there is it adds the \c use_range_ptr_var | ||
* property in the \c Symbol of the function or procedure in the program \c SymbolTable and does the | ||
* same recursively for all the caller functions. The \c use_range_ptr_var property is used later in | ||
* the \c CodegenNeuronCppVisitor . | ||
* | ||
*/ | ||
class FunctionCallpathVisitor: public ConstAstVisitor { | ||
private: | ||
/// Vector of currently visited functions or procedures (used as a searchable stack) | ||
std::vector<const ast::Block*> visited_functions_or_procedures; | ||
|
||
/// symbol table for the program | ||
symtab::SymbolTable* psymtab = nullptr; | ||
|
||
public: | ||
void visit_var_name(const ast::VarName& node) override; | ||
|
||
void visit_function_call(const ast::FunctionCall& node) override; | ||
|
||
void visit_function_block(const ast::FunctionBlock& node) override; | ||
|
||
void visit_procedure_block(const ast::ProcedureBlock& node) override; | ||
|
||
void visit_program(const ast::Program& node) override; | ||
}; | ||
|
||
/** \} */ // end of visitor_classes | ||
|
||
} // namespace visitor | ||
} // namespace nmodl |
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