Commit 4b096d98 authored by Marius Wachtler's avatar Marius Wachtler

cfg: convert lambdas to function defs

parent 232557a4
......@@ -98,12 +98,6 @@ public:
return true;
}
bool visit_lambda(AST_Lambda* node) {
for (auto* d : node->args->defaults)
d->accept(this);
return true;
}
bool visit_name(AST_Name* node) {
if (node->vreg == -1)
return true;
......
......@@ -391,8 +391,6 @@ private:
void* visit_index(AST_Index* node) override { return getType(node->value); }
void* visit_lambda(AST_Lambda* node) override { return typeFromClass(function_cls); }
void* visit_langprimitive(AST_LangPrimitive* node) override {
switch (node->opcode) {
case AST_LangPrimitive::CHECK_EXC_MATCH:
......
......@@ -103,7 +103,6 @@ private:
Value visit_expr(AST_Expr* node);
Value visit_extslice(AST_ExtSlice* node);
Value visit_index(AST_Index* node);
Value visit_lambda(AST_Lambda* node);
Value visit_list(AST_List* node);
Value visit_name(AST_Name* node);
Value visit_num(AST_Num* node);
......@@ -1481,8 +1480,6 @@ Value ASTInterpreter::visit_expr(AST_expr* node) {
return visit_compare((AST_Compare*)node);
case AST_TYPE::Dict:
return visit_dict((AST_Dict*)node);
case AST_TYPE::Lambda:
return visit_lambda((AST_Lambda*)node);
case AST_TYPE::List:
return visit_list((AST_List*)node);
case AST_TYPE::Name:
......@@ -1640,15 +1637,6 @@ Value ASTInterpreter::visit_repr(AST_Repr* node) {
return Value(repr(v.o), jit ? jit->emitRepr(v) : NULL);
}
Value ASTInterpreter::visit_lambda(AST_Lambda* node) {
AST_Return* expr = new AST_Return();
expr->value = node->body;
expr->lineno = node->body->lineno;
std::vector<AST_stmt*> body = { expr };
return createFunction(node, node->args, body);
}
Value ASTInterpreter::visit_dict(AST_Dict* node) {
RELEASE_ASSERT(node->keys.size() == node->values.size(), "not implemented");
......
......@@ -122,8 +122,8 @@ BORROWED(BoxedString*) SourceInfo::getName() noexcept {
case AST_TYPE::ClassDef:
return ast_cast<AST_ClassDef>(ast)->name.getBox();
case AST_TYPE::FunctionDef:
return ast_cast<AST_FunctionDef>(ast)->name.getBox();
case AST_TYPE::Lambda:
if (ast_cast<AST_FunctionDef>(ast)->name != InternedString())
return ast_cast<AST_FunctionDef>(ast)->name.getBox();
return lambda_name;
case AST_TYPE::Module:
case AST_TYPE::Expression:
......
......@@ -1281,18 +1281,6 @@ private:
CompilerVariable* evalIndex(AST_Index* node, const UnwindInfo& unw_info) { return evalExpr(node->value, unw_info); }
CompilerVariable* evalLambda(AST_Lambda* node, const UnwindInfo& unw_info) {
AST_Return* expr = new AST_Return();
expr->value = node->body;
expr->lineno = node->body->lineno;
std::vector<AST_stmt*> body = { expr };
CompilerVariable* func = _createFunction(node, unw_info, node->args, body);
ConcreteCompilerVariable* converted = func->makeConverted(emitter, func->getBoxType());
return converted;
}
CompilerVariable* evalList(AST_List* node, const UnwindInfo& unw_info) {
std::vector<CompilerVariable*> elts;
......@@ -1844,9 +1832,6 @@ private:
case AST_TYPE::Dict:
rtn = evalDict(ast_cast<AST_Dict>(node), unw_info);
break;
case AST_TYPE::Lambda:
rtn = evalLambda(ast_cast<AST_Lambda>(node), unw_info);
break;
case AST_TYPE::List:
rtn = evalList(ast_cast<AST_List>(node), unw_info);
break;
......
......@@ -1405,7 +1405,12 @@ bool PrintVisitor::visit_functiondef(AST_FunctionDef* node) {
printIndent();
}
stream << "def " << node->name.s() << "(";
stream << "def ";
if (node->name != InternedString())
stream << node->name.s();
else
stream << "<lambda>";
stream << "(";
node->args->accept(this);
stream << ")";
......
......@@ -542,7 +542,7 @@ class AST_FunctionDef : public AST_stmt {
public:
std::vector<AST_stmt*> body;
std::vector<AST_expr*> decorator_list;
InternedString name;
InternedString name; // if the name is not set this is a lambda
AST_arguments* args;
virtual void accept(ASTVisitor* v);
......
......@@ -1199,14 +1199,25 @@ private:
}
AST_expr* remapLambda(AST_Lambda* node) {
auto rtn = new AST_Lambda();
rtn->body = node->body; // don't remap now; will be CFG'ed later
rtn->args = remapArguments(node->args);
rtn->lineno = node->lineno;
rtn->col_offset = node->col_offset;
// lambdas create scope, need to register as replacement
scoping_analysis->registerScopeReplacement(node, rtn);
return rtn;
// we convert lambdas into normal functions (but don't give it a name)
auto def = new AST_FunctionDef();
def->lineno = node->lineno;
def->col_offset = node->col_offset;
auto stmt = new AST_Return;
stmt->lineno = node->lineno;
stmt->col_offset = node->col_offset;
stmt->value = node->body; // don't remap now; will be CFG'ed later
def->body = { stmt };
def->args = remapArguments(node->args);
scoping_analysis->registerScopeReplacement(node, def);
auto tmp = nodeName();
pushAssign(tmp, new AST_MakeFunction(def));
return makeLoad(tmp, def, /* is_kill */ false);
}
AST_expr* remapLangPrimitive(AST_LangPrimitive* node) {
......
......@@ -75,6 +75,7 @@ public:
// assert(this->pool == rhs.pool || this->pool == invalidPool() || rhs.pool == invalidPool());
return this->_str == rhs._str;
}
bool operator!=(InternedString rhs) const { return !(*this == rhs); }
// This function compares the actual string contents
bool operator<(InternedString rhs) const { return this->s().compare(rhs.s()) == -1; }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment