Skip to content

Commit 69bac48

Browse files
committed
Remove abs, str compile time implementations from visit_Call
1 parent 672e8c4 commit 69bac48

7 files changed

+7
-188
lines changed

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 0 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,62 +2112,6 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
21122112
}
21132113
return;
21142114

2115-
} else if (call_name == "str_int") {
2116-
std::string rl_path = get_runtime_library_dir();
2117-
SymbolTable *st = current_scope;
2118-
while (st->parent != nullptr) {
2119-
st = st->parent;
2120-
}
2121-
bool ltypes;
2122-
std::string msym = "lpython_builtin";
2123-
ASR::symbol_t *t = (ASR::symbol_t*)(load_module(al, st,
2124-
msym, x.base.base.loc, true, rl_path, ltypes,
2125-
[&](const std::string &msg, const Location &loc) { throw SemanticError(msg, loc); }
2126-
));
2127-
LFORTRAN_ASSERT(!ltypes)
2128-
if (!t) {
2129-
throw SemanticError("The module '" + msym + "' cannot be loaded",
2130-
x.base.base.loc);
2131-
}
2132-
2133-
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(t);
2134-
2135-
std::string local_sym = "str_int";
2136-
t = m->m_symtab->resolve_symbol(local_sym);
2137-
if (!t) {
2138-
throw SemanticError("ICE: The symbol '" + local_sym + "' not found in the module '" + msym + "'",
2139-
x.base.base.loc);
2140-
}
2141-
if (ASR::is_a<ASR::Function_t>(*t)) {
2142-
if (current_scope->scope.find(local_sym) != current_scope->scope.end()) {
2143-
throw SemanticError("Function already defined",
2144-
x.base.base.loc);
2145-
}
2146-
ASR::Function_t *mfn = ASR::down_cast<ASR::Function_t>(t);
2147-
// `mfn` is the Function in a module. Now we construct
2148-
// an ExternalSymbol that points to it.
2149-
Str name;
2150-
name.from_str(al, local_sym);
2151-
char *cname = name.c_str(al);
2152-
ASR::asr_t *fn = ASR::make_ExternalSymbol_t(
2153-
al, mfn->base.base.loc,
2154-
/* a_symtab */ current_scope,
2155-
/* a_name */ cname,
2156-
(ASR::symbol_t*)mfn,
2157-
m->m_name, nullptr, 0, mfn->m_name,
2158-
ASR::accessType::Public
2159-
);
2160-
current_scope->scope[local_sym] = ASR::down_cast<ASR::symbol_t>(fn);
2161-
2162-
ASR::ttype_t *a_type = ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc,
2163-
4, nullptr, 0));
2164-
tmp = ASR::make_FunctionCall_t(al, x.base.base.loc, ASR::down_cast<ASR::symbol_t>(fn),
2165-
nullptr, args.p, args.size(), nullptr, 0, a_type, nullptr, nullptr);
2166-
return;
2167-
} else {
2168-
throw SemanticError("ICE: 'str_int' expected to be a function", x.base.base.loc);
2169-
}
2170-
21712115
} else if (call_name == "ord") {
21722116
std::string rl_path = get_runtime_library_dir();
21732117
SymbolTable *st = current_scope;
@@ -2327,97 +2271,6 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
23272271
throw SemanticError(call_name + "() must have one integer argument",
23282272
x.base.base.loc);
23292273
}
2330-
} else if (call_name == "abs") {
2331-
if (args.size() != 1) {
2332-
throw SemanticError(call_name + "() takes exactly one argument (" +
2333-
std::to_string(args.size()) + " given)", x.base.base.loc);
2334-
}
2335-
std::string rl_path = get_runtime_library_dir();
2336-
SymbolTable *st = current_scope;
2337-
while (st->parent != nullptr) {
2338-
st = st->parent;
2339-
}
2340-
bool ltypes;
2341-
std::string msym = "lpython_builtin";
2342-
ASR::symbol_t *t = (ASR::symbol_t*)(load_module(al, st,
2343-
msym, x.base.base.loc, true, rl_path, ltypes,
2344-
[&](const std::string &msg, const Location &loc) { throw SemanticError(msg, loc); }
2345-
));
2346-
LFORTRAN_ASSERT(!ltypes)
2347-
if (!t) {
2348-
throw SemanticError("The module '" + msym + "' cannot be loaded",
2349-
x.base.base.loc);
2350-
}
2351-
2352-
ASR::Module_t *m = ASR::down_cast<ASR::Module_t>(t);
2353-
2354-
std::string local_sym = "abs";
2355-
t = m->m_symtab->resolve_symbol(local_sym);
2356-
if (!t) {
2357-
throw SemanticError("ICE: The symbol '" + local_sym + "' not found in the module '" + msym + "'",
2358-
x.base.base.loc);
2359-
}
2360-
if (ASR::is_a<ASR::Function_t>(*t)) {
2361-
if (current_scope->scope.find(local_sym) != current_scope->scope.end()) {
2362-
throw SemanticError("Function already defined",
2363-
x.base.base.loc);
2364-
}
2365-
ASR::Function_t *mfn = ASR::down_cast<ASR::Function_t>(t);
2366-
// `mfn` is the Function in a module. Now we construct
2367-
// an ExternalSymbol that points to it.
2368-
Str name;
2369-
name.from_str(al, local_sym);
2370-
char *cname = name.c_str(al);
2371-
ASR::asr_t *fn = ASR::make_ExternalSymbol_t(
2372-
al, mfn->base.base.loc,
2373-
/* a_symtab */ current_scope,
2374-
/* a_name */ cname,
2375-
(ASR::symbol_t*)mfn,
2376-
m->m_name, nullptr, 0, mfn->m_name,
2377-
ASR::accessType::Public
2378-
);
2379-
current_scope->scope[local_sym] = ASR::down_cast<ASR::symbol_t>(fn);
2380-
2381-
ASR::ttype_t *a_type = ASRUtils::TYPE(ASR::make_Real_t(al,
2382-
x.base.base.loc, 8, nullptr, 0));
2383-
tmp = ASR::make_FunctionCall_t(al, x.base.base.loc, ASR::down_cast<ASR::symbol_t>(fn),
2384-
nullptr, args.p, args.size(), nullptr, 0, a_type, nullptr, nullptr);
2385-
return;
2386-
} else {
2387-
throw SemanticError("ICE: Abs expected to be a function", x.base.base.loc);
2388-
}
2389-
// Compile time value implementation:
2390-
/*
2391-
ASR::expr_t* arg = ASRUtils::expr_value(args[0]);
2392-
ASR::ttype_t* t = ASRUtils::expr_type(arg);
2393-
ASR::ttype_t* real_type = ASRUtils::TYPE(ASR::make_Real_t(al,
2394-
x.base.base.loc, 8, nullptr, 0));
2395-
ASR::ttype_t *int_type = ASRUtils::TYPE(ASR::make_Integer_t(al,
2396-
x.base.base.loc, 4, nullptr, 0));
2397-
if (ASRUtils::is_real(*t)) {
2398-
double rv = ASR::down_cast<ASR::ConstantReal_t>(arg)->m_r;
2399-
double val = std::abs(rv);
2400-
tmp = ASR::make_ConstantReal_t(al, x.base.base.loc, val, t);
2401-
} else if (ASRUtils::is_integer(*t)) {
2402-
int64_t rv = ASR::down_cast<ASR::ConstantInteger_t>(arg)->m_n;
2403-
int64_t val = std::abs(rv);
2404-
tmp = ASR::make_ConstantInteger_t(al, x.base.base.loc, val, t);
2405-
} else if (ASRUtils::is_complex(*t)) {
2406-
double re = ASR::down_cast<ASR::ConstantComplex_t>(arg)->m_re;
2407-
double im = ASR::down_cast<ASR::ConstantComplex_t>(arg)->m_im;
2408-
std::complex<double> c(re, im);
2409-
double result = std::abs(c);
2410-
tmp = ASR::make_ConstantReal_t(al, x.base.base.loc, result, real_type);
2411-
} else if (ASRUtils::is_logical(*t)) {
2412-
bool rv = ASR::down_cast<ASR::ConstantLogical_t>(arg)->m_value;
2413-
int8_t val = rv ? 1 : 0;
2414-
tmp = ASR::make_ConstantInteger_t(al, x.base.base.loc, val, int_type);
2415-
} else {
2416-
throw SemanticError(call_name + "() must have one real, integer, complex, or logical argument",
2417-
x.base.base.loc);
2418-
}
2419-
return;
2420-
*/
24212274
} else if (call_name == "bool") {
24222275
if (args.size() != 1) {
24232276
throw SemanticError(call_name + "() takes exactly one argument (" +
@@ -2538,40 +2391,6 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
25382391
ASRUtils::type_to_str(float_type) + "'", x.base.base.loc);
25392392
}
25402393
return;
2541-
} else if (call_name == "str") {
2542-
ASR::ttype_t* str_type = ASRUtils::TYPE(ASR::make_Character_t(al,
2543-
x.base.base.loc, 1, 1, nullptr, nullptr, 0));
2544-
if (args.size() == 0) { // create an empty string
2545-
tmp = ASR::make_ConstantString_t(al, x.base.base.loc, s2c(al, ""), str_type);
2546-
return;
2547-
}
2548-
ASR::expr_t* arg = ASRUtils::expr_value(args[0]);
2549-
ASR::ttype_t* arg_type = ASRUtils::expr_type(arg);
2550-
if (arg == nullptr) {
2551-
throw SemanticError("runtime str(x) is not supported, only compile time for now",
2552-
x.base.base.loc);
2553-
}
2554-
if (ASRUtils::is_integer(*arg_type)) {
2555-
int64_t ival = ASR::down_cast<ASR::ConstantInteger_t>(arg)->m_n;
2556-
std::string s = std::to_string(ival);
2557-
tmp = ASR::make_ConstantString_t(al, x.base.base.loc, s2c(al, s), str_type);
2558-
} else if (ASRUtils::is_real(*arg_type)) {
2559-
double rval = ASR::down_cast<ASR::ConstantReal_t>(arg)->m_r;
2560-
std::string s = std::to_string(rval);
2561-
tmp = ASR::make_ConstantString_t(al, x.base.base.loc, s2c(al, s), str_type);
2562-
} else if (ASRUtils::is_logical(*arg_type)) {
2563-
bool rv = ASR::down_cast<ASR::ConstantLogical_t>(arg)->m_value;
2564-
std::string s = rv ? "True" : "False";
2565-
tmp = ASR::make_ConstantString_t(al, x.base.base.loc, s2c(al, s), str_type);
2566-
} else if (ASRUtils::is_character(*arg_type)) {
2567-
char* c = ASR::down_cast<ASR::ConstantString_t>(arg)->m_s;
2568-
std::string s = std::string(c);
2569-
tmp = ASR::make_ConstantString_t(al, x.base.base.loc, s2c(al, s), str_type);
2570-
} else {
2571-
throw SemanticError("str() argument must be real, integer, logical, or a string, not '" +
2572-
ASRUtils::type_to_str(arg_type) + "'", x.base.base.loc);
2573-
}
2574-
return;
25752394
} else if (call_name == "divmod") {
25762395
if (args.size() != 2) {
25772396
throw SemanticError(call_name + "() takes exactly two arguments (" +

tests/reference/asr-constants1-5828e8a.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-constants1-5828e8a.stdout",
9-
"stdout_hash": "573efc7728d601d0818bf3507ab1b732bbabd7fc0273ec6632891af9",
9+
"stdout_hash": "970683502b70b9026b796c5ca2dc174fc8a822613ebea3f8e1fb201d",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)