Skip to content

Commit 1ab13c8

Browse files
Smit-createczgdp1807
authored andcommitted
Add is_c argument in c_ds_api
1 parent c73a309 commit 1ab13c8

File tree

2 files changed

+31
-34
lines changed

2 files changed

+31
-34
lines changed

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ 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} {
9797
}

src/libasr/codegen/c_utils.h

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -116,27 +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-
140122
class CCPPDSUtils {
141123
private:
142124

@@ -150,10 +132,11 @@ class CCPPDSUtils {
150132
std::string func_decls;
151133

152134
SymbolTable* global_scope;
135+
bool is_c;
153136

154137
public:
155138

156-
CCPPDSUtils() {
139+
CCPPDSUtils(bool is_c): is_c{is_c} {
157140
generated_code.clear();
158141
func_decls.clear();
159142
}
@@ -168,21 +151,35 @@ class CCPPDSUtils {
168151
}
169152

170153
std::string get_deepcopy(ASR::ttype_t *t, std::string value, std::string target) {
171-
if (ASR::is_a<ASR::List_t>(*t)) {
172-
ASR::List_t* list_type = ASR::down_cast<ASR::List_t>(t);
173-
std::string list_type_code = ASRUtils::get_type_code(list_type->m_type, true);
174-
std::string func = typecodeToDSfuncs[list_type_code]["list_deepcopy"];
175-
return func + "(&" + value + ", &" + target + ");";
176-
} else if (ASR::is_a<ASR::Tuple_t>(*t)) {
177-
ASR::Tuple_t* tup_type = ASR::down_cast<ASR::Tuple_t>(t);
178-
std::string tup_type_code = CUtils::get_tuple_type_code(tup_type);
179-
std::string func = typecodeToDSfuncs[tup_type_code]["tuple_deepcopy"];
180-
return func + "(" + value + ", &" + target + ");";
181-
} else if (ASR::is_a<ASR::Character_t>(*t)) {
182-
return "strcpy(" + target + ", " + value + ");";
183-
} else {
184-
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+
}
185181
}
182+
return result;
186183
}
187184

188185
bool is_non_primitive_DT(ASR::ttype_t *t) {

0 commit comments

Comments
 (0)