def get_declarations_from_block(cls, var_name: str, block: ASTBlock) -> List[ASTDeclaration]: """ Get declarations from the given block containing the given variable. :param var_name: variable name :param block: block to collect the variable declarations :return: a list of declarations """ if block is None: return [] if not type(var_name) is str: var_name = str(var_name) decls = [] for decl in block.get_declarations(): if isinstance(decl, ASTInlineExpression): var_names = [decl.get_variable_name()] elif isinstance(decl, ASTOdeEquation): var_names = [decl.get_lhs().get_name()] else: var_names = [var.get_name() for var in decl.get_variables()] for _var_name in var_names: if _var_name == var_name: decls.append(decl) break return decls
def all_variables_defined_in_block(cls, block: ASTBlock) -> List[ASTVariable]: if block is None: return [] vars = [] for decl in block.get_declarations(): for var in decl.get_variables(): vars.append(var) return vars
def get_declaration_by_name(cls, block: ASTBlock, var_name: str) -> Optional[ASTDeclaration]: """ Get a declaration by variable name. :param block: the block to look for the variable in :param var_name: name of the variable to look for (including single quotes indicating differential order) """ decls = block.get_declarations() for decl in decls: for var in decl.get_variables(): if var.get_complete_name() == var_name: return decl return None
def create_ast_block(cls, stmts, source_position): # type: (list(ASTSmallStmt|ASTCompoundStmt),ASTSourceLocation) -> ASTBlock return ASTBlock(stmts, source_position=source_position)