@@ -1811,23 +1811,56 @@ R"(#include <stdio.h>
1811
1811
1812
1812
void visit_Allocate (const ASR::Allocate_t &x) {
1813
1813
std::string indent (indentation_level*indentation_spaces, ' ' );
1814
- std::string out = indent + " // FIXME: allocate( " ;
1814
+ std::string out = " " ;
1815
1815
for (size_t i=0 ; i<x.n_args ; i++) {
1816
1816
ASR::symbol_t * tmp_sym = nullptr ;
1817
+ ASR::ttype_t * type = nullptr ;
1817
1818
ASR::expr_t * tmp_expr = x.m_args [i].m_a ;
1818
1819
if ( ASR::is_a<ASR::Var_t>(*tmp_expr) ) {
1819
1820
const ASR::Var_t* tmp_var = ASR::down_cast<ASR::Var_t>(tmp_expr);
1820
1821
tmp_sym = tmp_var->m_v ;
1822
+ type = ASRUtils::expr_type (tmp_expr);
1821
1823
} else {
1822
1824
throw CodeGenError (" Cannot deallocate variables in expression " +
1823
1825
std::to_string (tmp_expr->type ),
1824
1826
tmp_expr->base .loc );
1825
1827
}
1826
- // ASR::dimension_t* dims = x.m_args[i].m_dims;
1827
- // size_t n_dims = x.m_args[i].n_dims;
1828
- out += std::string (ASRUtils::symbol_name (tmp_sym)) + " , " ;
1828
+ std::string sym = ASRUtils::symbol_name (tmp_sym);
1829
+ if (ASRUtils::is_array (type)) {
1830
+ std::string size_str = " 1" ;
1831
+ out += indent + sym + " ->n_dims = " + std::to_string (x.m_args [i].n_dims ) + " ;\n " ;
1832
+ for (size_t j=0 ; j<x.m_args [i].n_dims ; j++) {
1833
+ std::string st, l;
1834
+ if (x.m_args [i].m_dims [j].m_start ) {
1835
+ self ().visit_expr (*x.m_args [i].m_dims [j].m_start );
1836
+ st = src;
1837
+ } else {
1838
+ st = " 0" ;
1839
+ }
1840
+ if (x.m_args [i].m_dims [j].m_length ) {
1841
+ self ().visit_expr (*x.m_args [i].m_dims [j].m_length );
1842
+ l = src;
1843
+ } else {
1844
+ l = " 1" ;
1845
+ }
1846
+ size_str += " *" + l;
1847
+ out += indent + sym + " ->dims[" + std::to_string (j) + " ].lower_bound = " ;
1848
+ out += st + " ;\n " ;
1849
+ out += indent + sym + " ->dims[" + std::to_string (j) + " ].length = " ;
1850
+ out += l + " ;\n " ;
1851
+ }
1852
+ std::string ty = CUtils::get_c_type_from_ttype_t (type);
1853
+ size_str += " *sizeof(" + ty + " )" ;
1854
+ out += indent + sym + " ->data = (" + ty + " *) _lfortran_malloc(" + size_str + " )" ;
1855
+ out += " ;\n " ;
1856
+ out += indent + sym + " ->is_allocated = true;\n " ;
1857
+ } else {
1858
+ std::string ty = CUtils::get_c_type_from_ttype_t (type), size_str;
1859
+ size_str = " sizeof(" + ty + " )" ;
1860
+ out += indent + sym + " = (" + ty + " *) _lfortran_malloc(" + size_str + " )" ;
1861
+ out += " ;\n " ;
1862
+ }
1829
1863
}
1830
- out += " );\n " ;
1831
1864
src = out;
1832
1865
}
1833
1866
0 commit comments