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

cfg: convert lambdas to function defs

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