Skip to content

Commit c52fd5c

Browse files
committed
cleaups: use function from ASRUtils namespace
1 parent 2422cf7 commit c52fd5c

File tree

1 file changed

+3
-68
lines changed

1 file changed

+3
-68
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 3 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -139,53 +139,8 @@ ASR::Module_t* load_module(Allocator &al, SymbolTable *symtab,
139139
return mod2;
140140
}
141141

142-
template <typename T>
143-
bool argument_types_match(const Vec<ASR::expr_t*> &args,
144-
const T &sub) {
145-
if (args.size() <= sub.n_args) {
146-
size_t i;
147-
for (i = 0; i < args.size(); i++) {
148-
ASR::Variable_t *v = LFortran::ASRUtils::EXPR2VAR(sub.m_args[i]);
149-
ASR::ttype_t *arg1 = ASRUtils::expr_type(args[i]);
150-
ASR::ttype_t *arg2 = v->m_type;
151-
if (!ASRUtils::check_equal_type(arg1, arg2)) {
152-
return false;
153-
}
154-
}
155-
for( ; i < sub.n_args; i++ ) {
156-
ASR::Variable_t *v = LFortran::ASRUtils::EXPR2VAR(sub.m_args[i]);
157-
if( v->m_presence != ASR::presenceType::Optional ) {
158-
return false;
159-
}
160-
}
161-
return true;
162-
} else {
163-
return false;
164-
}
165-
}
166-
167-
bool select_func_subrout(const ASR::symbol_t* proc, const Vec<ASR::expr_t*> &args,
168-
const Location& loc, const std::function<void (const std::string &, const Location &)> err) {
169-
bool result = false;
170-
if (ASR::is_a<ASR::Subroutine_t>(*proc)) {
171-
ASR::Subroutine_t *sub
172-
= ASR::down_cast<ASR::Subroutine_t>(proc);
173-
if (argument_types_match(args, *sub)) {
174-
result = true;
175-
}
176-
} else if (ASR::is_a<ASR::Function_t>(*proc)) {
177-
ASR::Function_t *fn
178-
= ASR::down_cast<ASR::Function_t>(proc);
179-
if (argument_types_match(args, *fn)) {
180-
result = true;
181-
}
182-
} else {
183-
err("Only Subroutine and Function supported in generic procedure", loc);
184-
}
185-
return result;
186-
}
187-
188142
std::map<int, ASR::symbol_t*> ast_overload;
143+
189144
template <class Derived>
190145
class CommonVisitor : public AST::BaseVisitor<Derived> {
191146
public:
@@ -2190,7 +2145,8 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
21902145
ASR::symbol_t *s = current_scope->resolve_symbol(call_name), *s_generic = nullptr;
21912146
if (s!=nullptr && s->type == ASR::symbolType::GenericProcedure) {
21922147
ASR::GenericProcedure_t *p = ASR::down_cast<ASR::GenericProcedure_t>(s);
2193-
int idx = select_generic_procedure(args, *p, x.base.base.loc);
2148+
int idx = ASRUtils::select_generic_procedure(args, *p, x.base.base.loc,
2149+
[&](const std::string &msg, const Location &loc) { throw SemanticError(msg, loc); });
21942150
// Create ExternalSymbol for procedures in different modules.
21952151
s_generic = s;
21962152
s = p->m_procs[idx];
@@ -2340,27 +2296,6 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
23402296
x.base.base.loc);
23412297
}
23422298
}
2343-
int select_generic_procedure(const Vec<ASR::expr_t*> &args,
2344-
const ASR::GenericProcedure_t &p, Location loc) {
2345-
for (size_t i=0; i < p.n_procs; i++) {
2346-
2347-
if( ASR::is_a<ASR::ClassProcedure_t>(*p.m_procs[i]) ) {
2348-
ASR::ClassProcedure_t *clss_fn
2349-
= ASR::down_cast<ASR::ClassProcedure_t>(p.m_procs[i]);
2350-
const ASR::symbol_t *proc = ASRUtils::symbol_get_past_external(clss_fn->m_proc);
2351-
if( select_func_subrout(proc, args, loc,
2352-
[&](const std::string &msg, const Location &loc) { throw SemanticError(msg, loc); })
2353-
){
2354-
return i;
2355-
}
2356-
} else {
2357-
if( select_func_subrout(p.m_procs[i], args, loc, [&](const std::string &msg, const Location &loc) { throw SemanticError(msg, loc); }) ) {
2358-
return i;
2359-
}
2360-
}
2361-
}
2362-
throw SemanticError("Arguments do not match for any generic procedure", loc);
2363-
}
23642299

23652300
void visit_ImportFrom(const AST::ImportFrom_t &/*x*/) {
23662301
// Handled by SymbolTableVisitor already

0 commit comments

Comments
 (0)