Commit 84b05dab authored by Kevin Modzelewski's avatar Kevin Modzelewski Committed by Kevin Modzelewski

Change varargs and kwargs to AST_Names

They were previously represented as strings, like in cpython,
but we want to treat them like names.
parent b1927655
......@@ -82,6 +82,7 @@ public:
bool isKilledAt(AST_Name* node, bool is_live_at_end) { return node->is_kill; }
bool visit_import(AST_Import* node) { RELEASE_ASSERT(0, "these should all get removed by the cfg"); }
bool visit_classdef(AST_ClassDef* node) {
_doStore(node->name);
......@@ -346,10 +347,10 @@ public:
}
virtual bool visit_arguments(AST_arguments* node) {
if (node->kwarg.s().size())
_doSet(node->kwarg_vreg);
if (node->vararg.s().size())
_doSet(node->vararg_vreg);
if (node->kwarg)
_doSet(node->kwarg);
if (node->vararg)
_doSet(node->vararg);
for (int i = 0; i < node->args.size(); i++) {
_doSet(node->args[i]);
}
......
......@@ -600,13 +600,13 @@ public:
e->accept(this);
}
if (node->args->vararg.s().size()) {
mangleNameInPlace(node->args->vararg, cur->private_name, scoping->getInternedStrings());
doWrite(node->args->vararg);
if (node->args->vararg) {
mangleNameInPlace(node->args->vararg->id, cur->private_name, scoping->getInternedStrings());
doWrite(node->args->vararg->id);
}
if (node->args->kwarg.s().size()) {
mangleNameInPlace(node->args->kwarg, cur->private_name, scoping->getInternedStrings());
doWrite(node->args->kwarg);
if (node->args->kwarg) {
mangleNameInPlace(node->args->kwarg->id, cur->private_name, scoping->getInternedStrings());
doWrite(node->args->kwarg->id);
}
this->currently_visiting_functiondef_args = false;
......@@ -668,13 +668,13 @@ public:
if (node == orig_node) {
for (AST_expr* e : node->args->args)
e->accept(this);
if (node->args->vararg.s().size()) {
mangleNameInPlace(node->args->vararg, cur->private_name, scoping->getInternedStrings());
doWrite(node->args->vararg);
if (node->args->vararg) {
mangleNameInPlace(node->args->vararg->id, cur->private_name, scoping->getInternedStrings());
doWrite(node->args->vararg->id);
}
if (node->args->kwarg.s().size()) {
mangleNameInPlace(node->args->kwarg, cur->private_name, scoping->getInternedStrings());
doWrite(node->args->kwarg);
if (node->args->kwarg) {
mangleNameInPlace(node->args->kwarg->id, cur->private_name, scoping->getInternedStrings());
doWrite(node->args->kwarg->id);
}
node->body->accept(this);
} else {
......
......@@ -97,26 +97,32 @@ public:
return pool->get(static_cast<BoxedString*>(ident)->s());
}
AST_Name* convertToName(identifier ident) {
if (!ident)
return NULL;
return new AST_Name(convert(ident), AST_TYPE::Store, -1, -1);
}
AST_arguments* convert(arguments_ty ident, AST* parent) {
auto r = new AST_arguments();
convertAll<expr_ty>(ident->args, r->args);
convertAll<expr_ty>(ident->defaults, r->defaults);
r->vararg = convert(ident->vararg);
r->kwarg = convert(ident->kwarg);
r->vararg = convertToName(ident->vararg);
r->kwarg = convertToName(ident->kwarg);
if ((!r->vararg.s().empty()) && (!r->kwarg.s().empty()) && (r->vararg == r->kwarg)) {
if (r->vararg && r->kwarg && (r->vararg->id == r->kwarg->id)) {
char buf[1024];
snprintf(buf, sizeof(buf), "duplicate argument '%s' in function definition", r->vararg.c_str());
snprintf(buf, sizeof(buf), "duplicate argument '%s' in function definition", r->vararg->id.c_str());
raiseSyntaxError(buf, parent->lineno, parent->col_offset, fn, "", true);
}
std::set<InternedString> seen;
if (!r->vararg.s().empty()) {
seen.insert(r->vararg);
if (r->vararg) {
seen.insert(r->vararg->id);
}
if (!r->kwarg.s().empty()) {
seen.insert(r->kwarg);
if (r->kwarg) {
seen.insert(r->kwarg->id);
}
checkDuplicateArgs(parent, r->args, &seen);
......
......@@ -77,13 +77,13 @@ ParamNames::ParamNames(AST* ast, InternedStringPool& pool)
}
}
vararg = arguments->vararg.s();
if (vararg.size())
vararg_name = new AST_Name(pool.get(vararg), AST_TYPE::Param, arguments->lineno, arguments->col_offset);
vararg_name = arguments->vararg;
if (vararg_name)
vararg = vararg_name->id.s();
kwarg = arguments->kwarg.s();
if (kwarg.size())
kwarg_name = new AST_Name(pool.get(kwarg), AST_TYPE::Param, arguments->lineno, arguments->col_offset);
kwarg_name = arguments->kwarg;
if (kwarg_name)
kwarg = kwarg_name->id.s();
} else {
RELEASE_ASSERT(0, "%d", ast->type);
}
......
......@@ -3212,8 +3212,7 @@ FunctionMetadata* wrapFunction(AST* node, AST_arguments* args, const std::vector
std::unique_ptr<SourceInfo> si(
new SourceInfo(source->parent_module, source->scoping, source->future_flags, node, body, source->getFn()));
if (args)
md = new FunctionMetadata(args->args.size(), args->vararg.s().size(), args->kwarg.s().size(),
std::move(si));
md = new FunctionMetadata(args->args.size(), args->vararg, args->kwarg, std::move(si));
else
md = new FunctionMetadata(0, false, false, std::move(si));
}
......
......@@ -218,9 +218,9 @@ AST_arguments* read_arguments(BufferedReader* reader) {
readExprVector(rtn->args, reader);
rtn->col_offset = -1;
readExprVector(rtn->defaults, reader);
rtn->kwarg = reader->readAndInternString();
rtn->kwarg = ast_cast<AST_Name>(readASTExpr(reader));
rtn->lineno = -1;
rtn->vararg = reader->readAndInternString();
rtn->vararg = ast_cast<AST_Name>(readASTExpr(reader));
return rtn;
}
......@@ -1042,9 +1042,9 @@ AST_Module* parse_file(const char* fn, FutureFlags inherited_flags) {
const char* getMagic() {
if (ENABLE_PYPA_PARSER)
return "a\ncQ";
return "a\ncR";
else
return "a\nCQ";
return "a\nCR";
}
#define MAGIC_STRING_LENGTH 4
......
......@@ -301,8 +301,12 @@ AST_arguments* readItem(pypa::AstArguments& a, InternedStringPool& interned_stri
readVector(ptr->defaults, a.defaults, interned_strings);
ptr->defaults.erase(std::remove(ptr->defaults.begin(), ptr->defaults.end(), nullptr), ptr->defaults.end());
readVector(ptr->args, a.arguments, interned_strings);
ptr->kwarg = readName(a.kwargs, interned_strings);
ptr->vararg = readName(a.args, interned_strings);
InternedString kwarg_name = readName(a.kwargs, interned_strings);
if (kwarg_name.s().size())
ptr->kwarg = new AST_Name(kwarg_name, AST_TYPE::Store, -1, -1);
InternedString vararg_name = readName(a.args, interned_strings);
if (vararg_name.s().size())
ptr->vararg = new AST_Name(vararg_name, AST_TYPE::Store, -1, -1);
return ptr;
}
......
......@@ -176,8 +176,8 @@ private:
virtual bool visit_arguments(AST_arguments* node) {
writeExprVector(node->args);
writeExprVector(node->defaults);
writeString(node->kwarg);
writeString(node->vararg);
writeExpr(node->kwarg);
writeExpr(node->vararg);
return true;
}
virtual bool visit_assert(AST_Assert* node) {
......
......@@ -256,6 +256,10 @@ void AST_arguments::accept(ASTVisitor* v) {
visitVector(defaults, v);
visitVector(args, v);
if (kwarg)
kwarg->accept(v);
if (vararg)
vararg->accept(v);
}
void AST_Assert::accept(ASTVisitor* v) {
......
......@@ -226,15 +226,14 @@ public:
static const AST_TYPE::AST_TYPE TYPE = AST_TYPE::alias;
};
class AST_Name;
class AST_arguments : public AST {
public:
// no lineno, col_offset attributes
std::vector<AST_expr*> args, defaults;
// These are represented as strings, not names; not sure why.
// If they don't exist, the string is empty.
InternedString kwarg, vararg;
int kwarg_vreg = -1, vararg_vreg = -1;
AST_Name* kwarg = NULL, * vararg = NULL;
virtual void accept(ASTVisitor* v);
......@@ -1118,7 +1117,7 @@ public:
};
template <typename T> T* ast_cast(AST* node) {
assert(node->type == T::TYPE);
assert(!node || node->type == T::TYPE);
return static_cast<T*>(node);
}
......
......@@ -1007,8 +1007,8 @@ private:
InternedString func_name = internString("<comprehension>");
func->name = func_name;
func->args = new AST_arguments();
func->args->vararg = internString("");
func->args->kwarg = internString("");
func->args->vararg = NULL;
func->args->kwarg = NULL;
scoping_analysis->registerScopeReplacement(node, func); // critical bit
return new AST_MakeFunction(func);
}
......@@ -2686,20 +2686,12 @@ public:
AssignVRegsVisitor(ScopeInfo* scope_info) : scope_info(scope_info), current_block(0), next_vreg(0) {}
bool visit_alias(AST_alias* node) override {
if (node->asname.s().size())
node->asname_vreg = assignVReg(node->asname);
else
node->name_vreg = assignVReg(node->name);
return true;
RELEASE_ASSERT(0, "these should be removed by the cfg");
}
bool visit_arguments(AST_arguments* node) override {
for (AST_expr* d : node->defaults)
d->accept(this);
if (node->kwarg.s().size())
node->kwarg_vreg = assignVReg(node->kwarg);
if (node->vararg.s().size())
node->vararg_vreg = assignVReg(node->vararg);
return true;
}
......
......@@ -2,8 +2,7 @@
def f1():
exec ""
from os import path as os_path
print os_path.join("a", "b")
from sys import version as sys_version
f1()
def f2():
......
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