Skip to content

Commit 7feb501

Browse files
authored
Merge pull request #1300 from Smit-create/c6
C/CPP: Unify deepcopy API
2 parents ebbf0a1 + 1ab13c8 commit 7feb501

File tree

2 files changed

+37
-58
lines changed

2 files changed

+37
-58
lines changed

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,9 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
9191
platform{platform},
9292
gen_stdstring{gen_stdstring}, gen_stdcomplex{gen_stdcomplex},
9393
is_c{is_c}, global_scope{nullptr}, lower_bound{default_lower_bound},
94-
template_number{0}, c_ds_api{std::make_unique<CCPPDSUtils>()},
94+
template_number{0}, c_ds_api{std::make_unique<CCPPDSUtils>(is_c)},
9595
const_name{"constname"},
9696
const_list_count{0}, is_string_concat_present{false} {
97-
if( is_c ) {
98-
c_ds_api->set_deepcopy_function(&CUtils::deepcopy);
99-
} else {
100-
c_ds_api->set_deepcopy_function(&CPPUtils::deepcopy);
101-
}
10297
}
10398

10499
void visit_TranslationUnit(const ASR::TranslationUnit_t &x) {
@@ -706,9 +701,9 @@ R"(#include <stdio.h>
706701
alloc = indent + target + " = " + "(char *) malloc((strlen(" +
707702
value + ") + 1 ) * sizeof(char));\n";
708703
}
709-
src += alloc + indent + CUtils::deepcopy(target, value, m_target_type) + "\n";
704+
src += alloc + indent + c_ds_api->get_deepcopy(m_target_type, value, target) + "\n";
710705
} else {
711-
src += indent + CPPUtils::deepcopy(target, value, m_target_type) + "\n";
706+
src += indent + c_ds_api->get_deepcopy(m_target_type, value, target) + "\n";
712707
}
713708
}
714709
from_std_vector_helper.clear();
@@ -779,11 +774,8 @@ R"(#include <stdio.h>
779774
if( ASR::is_a<ASR::Character_t>(*t->m_type) ) {
780775
src_tmp += const_name + ".data[" + std::to_string(i) +"] = (char*) malloc(40 * sizeof(char));\n";
781776
}
782-
if( is_c ) {
783-
src_tmp += indent + CUtils::deepcopy(const_name + ".data[" + std::to_string(i) +"]", src, t->m_type) + "\n";
784-
} else {
785-
src_tmp += indent + CPPUtils::deepcopy(const_name + ".data[" + std::to_string(i) +"]", src, t->m_type) + "\n";
786-
}
777+
src_tmp += indent + c_ds_api->get_deepcopy(t->m_type, src,
778+
const_name + ".data[" + std::to_string(i) +"]") + "\n";
787779
}
788780
src_tmp += indent + const_name + ".current_end_point = " + std::to_string(x.n_args) + ";\n";
789781
src = src_tmp;
@@ -805,11 +797,7 @@ R"(#include <stdio.h>
805797
if (ASR::is_a<ASR::Character_t>(*t->m_type[i])) {
806798
src_tmp += indent + const_name + ele + " = (char*) malloc(40 * sizeof(char));\n";
807799
}
808-
if (is_c) {
809-
src_tmp += indent + CUtils::deepcopy(const_name + ele , src, t->m_type[i]) + "\n";
810-
} else {
811-
src_tmp += indent + CPPUtils::deepcopy(const_name + ele, src, t->m_type[i]) + "\n";
812-
}
800+
src_tmp += indent + c_ds_api->get_deepcopy(t->m_type[i], src, const_name + ele) + "\n";
813801
}
814802
src_tmp += indent + const_name + ".length" + " = " + std::to_string(x.n_elements) + ";\n";
815803
src = src_tmp;

src/libasr/codegen/c_utils.h

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -116,28 +116,9 @@ namespace CUtils {
116116
return type_src;
117117
}
118118

119-
static inline std::string deepcopy(std::string target, std::string value, ASR::ttype_t* m_type) {
120-
switch (m_type->type) {
121-
case ASR::ttypeType::Character: {
122-
return "strcpy(" + target + ", " + value + ");";
123-
}
124-
default: {
125-
return target + " = " + value + ";";
126-
}
127-
}
128-
}
129-
130119
} // namespace CUtils
131120

132121

133-
namespace CPPUtils {
134-
static inline std::string deepcopy(std::string target, std::string value, ASR::ttype_t* /*m_type*/) {
135-
return target + " = " + value + ";";
136-
}
137-
} // namespace CPPUtils
138-
139-
typedef std::string (*DeepCopyFunction)(std::string, std::string, ASR::ttype_t*);
140-
141122
class CCPPDSUtils {
142123
private:
143124

@@ -151,19 +132,15 @@ class CCPPDSUtils {
151132
std::string func_decls;
152133

153134
SymbolTable* global_scope;
154-
DeepCopyFunction deepcopy_function;
135+
bool is_c;
155136

156137
public:
157138

158-
CCPPDSUtils() {
139+
CCPPDSUtils(bool is_c): is_c{is_c} {
159140
generated_code.clear();
160141
func_decls.clear();
161142
}
162143

163-
void set_deepcopy_function(DeepCopyFunction func) {
164-
deepcopy_function = func;
165-
}
166-
167144
void set_indentation(int indendation_level_, int indendation_space_) {
168145
indentation_level = indendation_level_;
169146
indentation_spaces = indendation_space_;
@@ -174,21 +151,35 @@ class CCPPDSUtils {
174151
}
175152

176153
std::string get_deepcopy(ASR::ttype_t *t, std::string value, std::string target) {
177-
if (ASR::is_a<ASR::List_t>(*t)) {
178-
ASR::List_t* list_type = ASR::down_cast<ASR::List_t>(t);
179-
std::string list_type_code = ASRUtils::get_type_code(list_type->m_type, true);
180-
std::string func = typecodeToDSfuncs[list_type_code]["list_deepcopy"];
181-
return func + "(&" + value + ", &" + target + ");";
182-
} else if (ASR::is_a<ASR::Tuple_t>(*t)) {
183-
ASR::Tuple_t* tup_type = ASR::down_cast<ASR::Tuple_t>(t);
184-
std::string tup_type_code = CUtils::get_tuple_type_code(tup_type);
185-
std::string func = typecodeToDSfuncs[tup_type_code]["tuple_deepcopy"];
186-
return func + "(" + value + ", &" + target + ");";
187-
} else if (ASR::is_a<ASR::Character_t>(*t)) {
188-
return "strcpy(" + target + ", " + value + ");";
189-
} else {
190-
return target + " = " + value + ";";
154+
std::string result;
155+
switch (t->type) {
156+
case ASR::ttypeType::List : {
157+
ASR::List_t* list_type = ASR::down_cast<ASR::List_t>(t);
158+
std::string list_type_code = ASRUtils::get_type_code(list_type->m_type, true);
159+
std::string func = typecodeToDSfuncs[list_type_code]["list_deepcopy"];
160+
result = func + "(&" + value + ", &" + target + ");";
161+
break;
162+
}
163+
case ASR::ttypeType::Tuple : {
164+
ASR::Tuple_t* tup_type = ASR::down_cast<ASR::Tuple_t>(t);
165+
std::string tup_type_code = CUtils::get_tuple_type_code(tup_type);
166+
std::string func = typecodeToDSfuncs[tup_type_code]["tuple_deepcopy"];
167+
result = func + "(" + value + ", &" + target + ");";
168+
break;
169+
}
170+
case ASR::ttypeType::Character : {
171+
if (is_c) {
172+
result = "strcpy(" + target + ", " + value + ");";
173+
} else {
174+
result = target + " = " + value + ";";
175+
}
176+
break;
177+
}
178+
default: {
179+
result = target + " = " + value + ";";
180+
}
191181
}
182+
return result;
192183
}
193184

194185
bool is_non_primitive_DT(ASR::ttype_t *t) {
@@ -522,7 +513,7 @@ class CCPPDSUtils {
522513
if( ASR::is_a<ASR::Character_t>(*m_type) ) {
523514
generated_code += indent + tab + "x->data[pos] = (char*) malloc(40 * sizeof(char));\n";
524515
}
525-
generated_code += indent + tab + deepcopy_function("x->data[pos]", "element", m_type) + "\n";
516+
generated_code += indent + tab + get_deepcopy(m_type, "element", "x->data[pos]") + "\n";
526517
generated_code += indent + tab + "x->current_end_point += 1;\n";
527518
generated_code += indent + "}\n\n";
528519
}

0 commit comments

Comments
 (0)