Skip to content

Commit

Permalink
Merge pull request #1293 from spcl/fortran_allocate
Browse files Browse the repository at this point in the history
basic allocatable functionality
  • Loading branch information
acalotoiu committed Jul 5, 2023
2 parents 36cb24e + 03f5770 commit 7e03954
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 36 deletions.
33 changes: 33 additions & 0 deletions dace/frontend/fortran/ast_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ def __init__(self, ast: f03.Program, tables: symbol_table.SymbolTables):
"Structure_Constructor": self.structure_constructor,
"Component_Spec_List": self.component_spec_list,
"Write_Stmt": self.write_stmt,
"Assumed_Shape_Spec_List": self.assumed_shape_spec_list,
"Allocate_Stmt": self.allocate_stmt,
"Allocation_List": self.allocation_list,
"Allocation": self.allocation,
"Allocate_Shape_Spec": self.allocate_shape_spec,
"Allocate_Shape_Spec_List": self.allocate_shape_spec_list,
}

def list_tables(self):
Expand Down Expand Up @@ -359,6 +365,30 @@ def array_constructor(self, node: FASTNode):
value_list = get_child(children, ast_internal_classes.Ac_Value_List_Node)
return ast_internal_classes.Array_Constructor_Node(value_list=value_list.value_list)

def allocate_stmt(self, node: FASTNode):
children = self.create_children(node)
return ast_internal_classes.Allocate_Stmt_Node(allocation_list=children[1])

def allocation_list(self, node: FASTNode):
children = self.create_children(node)
return children

def allocation(self, node: FASTNode):
children = self.create_children(node)
name = get_child(children, ast_internal_classes.Name_Node)
shape = get_child(children, ast_internal_classes.Allocate_Shape_Spec_List)
return ast_internal_classes.Allocation_Node(name=name, shape=shape)

def allocate_shape_spec_list(self, node: FASTNode):
children = self.create_children(node)
return ast_internal_classes.Allocate_Shape_Spec_List(shape_list=children)

def allocate_shape_spec(self, node: FASTNode):
children = self.create_children(node)
if len(children) != 2:
raise NotImplementedError("Only simple allocate shape specs are supported")
return children[1]

def structure_constructor(self, node: FASTNode):
children = self.create_children(node)
name = get_child(children, ast_internal_classes.Type_Name_Node)
Expand Down Expand Up @@ -490,6 +520,9 @@ def declaration_type_spec(self, node: FASTNode):
raise NotImplementedError("Declaration type spec is not supported yet")
return node

def assumed_shape_spec_list(self, node: FASTNode):
return node

def type_declaration_stmt(self, node: FASTNode):

#decide if its a intrinsic variable type or a derived type
Expand Down
20 changes: 20 additions & 0 deletions dace/frontend/fortran/ast_internal_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,26 @@ class Type_Decl_Node(Statement_Node):
_fields = ()


class Allocate_Shape_Spec_Node(FNode):
_attributes = ()
_fields = ('sizes', )


class Allocate_Shape_Spec_List(FNode):
_attributes = ()
_fields = ('shape_list', )


class Allocation_Node(FNode):
_attributes = ('name', )
_fields = ('shape', )


class Allocate_Stmt_Node(FNode):
_attributes = ()
_fields = ('allocation_list', )


class Symbol_Decl_Node(Statement_Node):
_attributes = (
'name',
Expand Down
11 changes: 4 additions & 7 deletions dace/frontend/fortran/ast_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,7 @@ def visit_Array_Subscript_Node(self, node: ast_internal_classes.Array_Subscript_
new_indices.append(ast_internal_classes.Name_Node(name="tmp_index_" + str(tmp)))
tmp = tmp + 1
self.count = tmp
return ast_internal_classes.Array_Subscript_Node(
name=node.name,
indices=new_indices,
)
return ast_internal_classes.Array_Subscript_Node(name=node.name, indices=new_indices)

def visit_Execution_Part_Node(self, node: ast_internal_classes.Execution_Part_Node):
newbody = []
Expand Down Expand Up @@ -543,8 +540,8 @@ def localFunctionStatementEliminator(node: ast_internal_classes.FNode):
i.lval, ast_internal_classes.Structure_Constructor_Node):
function_statement_name = i.lval.name
is_actually_function_statement = False
# In Fortran, function statement are defined as scalar values,
# but called as arrays, so by identifiying that it is called as
# In Fortran, function statement are defined as scalar values,
# but called as arrays, so by identifiying that it is called as
# a call_expr or structure_constructor, we also need to match
# the specification part and see that it is scalar rather than an array.
found = False
Expand All @@ -562,7 +559,7 @@ def localFunctionStatementEliminator(node: ast_internal_classes.FNode):
if is_actually_function_statement:
to_change.append([i.lval, i.rval])
new_exec.remove(i)

else:
#There are no function statements after the first one that isn't a function statement
break
Expand Down

0 comments on commit 7e03954

Please sign in to comment.