Skip to content

Commit

Permalink
Misc fixes (exaloop#410)
Browse files Browse the repository at this point in the history
* Fix corner case when typechecking scoped names with static compilation

* Undo log

* Fix nested loop domination; Minor aestethic fixes

* clang-format

* Add slice indices() method

* Fix overloads with static arguments

* Update itertools combinatorics functions

* Fix import domination issue (missing stack insert)

* Fix itertools

* Remove log

* Bump version

---------

Co-authored-by: Ibrahim Numanagić <ibrahimpasa@gmail.com>
  • Loading branch information
arshajii and inumanag authored Jul 2, 2023
1 parent e95f778 commit 6bb26e0
Show file tree
Hide file tree
Showing 16 changed files with 848 additions and 152 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
cmake_minimum_required(VERSION 3.14)
project(
Codon
VERSION "0.16.1"
VERSION "0.16.2"
HOMEPAGE_URL "https://github.com/exaloop/codon"
DESCRIPTION "high-performance, extensible Python compiler")
set(CODON_JIT_PYTHON_VERSION "0.1.5")
set(CODON_JIT_PYTHON_VERSION "0.1.6")
configure_file("${PROJECT_SOURCE_DIR}/cmake/config.h.in"
"${PROJECT_SOURCE_DIR}/codon/config/config.h")
configure_file("${PROJECT_SOURCE_DIR}/cmake/config.py.in"
Expand Down
3 changes: 2 additions & 1 deletion codon/parser/ast/types/link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ bool LinkType::isInstantiated() const { return kind == Link && type->isInstantia
std::string LinkType::debugString(char mode) const {
if (kind == Unbound || kind == Generic) {
if (mode == 2) {
return fmt::format("{}{}{}", kind == Unbound ? '?' : '#', id,
return fmt::format("{}{}{}{}", genericName.empty() ? "" : genericName + ":",
kind == Unbound ? '?' : '#', id,
trait ? ":" + trait->debugString(mode) : "");
}
if (trait)
Expand Down
15 changes: 10 additions & 5 deletions codon/parser/visitors/simplify/access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,16 @@ void SimplifyVisitor::visit(IdExpr *expr) {
// while True:
// if x > 10: break
// x = x + 1 # x must be dominated after the loop to ensure that it gets updated
if (auto loop = ctx->getBase()->getLoop()) {
bool inside = val->scope.size() >= loop->scope.size() &&
val->scope[loop->scope.size() - 1] == loop->scope.back();
if (!inside)
loop->seenVars.insert(expr->value);
if (ctx->getBase()->getLoop()) {
for (size_t li = ctx->getBase()->loops.size(); li-- > 0;) {
auto &loop = ctx->getBase()->loops[li];
bool inside = val->scope.size() >= loop.scope.size() &&
val->scope[loop.scope.size() - 1] == loop.scope.back();
if (!inside)
loop.seenVars.insert(expr->value);
else
break;
}
}

// Replace the variable with its canonical name
Expand Down
1 change: 1 addition & 0 deletions codon/parser/visitors/simplify/ctx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ SimplifyContext::Item SimplifyContext::findDominatingBinding(const std::string &
(*lastGood)->importPath);
item->accessChecked = {(*lastGood)->scope};
lastGood = it->second.insert(++lastGood, item);
stack.front().push_back(name);
// Make sure to prepend a binding declaration: `var` and `var__used__ = False`
// to the dominating scope.
scope.stmts[scope.blocks[prefix - 1]].push_back(std::make_unique<AssignStmt>(
Expand Down
3 changes: 2 additions & 1 deletion codon/parser/visitors/simplify/loops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ void SimplifyVisitor::visit(ForStmt *stmt) {

ctx->leaveConditionalBlock(&(stmt->suite->getSuite()->stmts));
// Dominate loop variables
for (auto &var : ctx->getBase()->getLoop()->seenVars)
for (auto &var : ctx->getBase()->getLoop()->seenVars) {
ctx->findDominatingBinding(var);
}
ctx->getBase()->loops.pop_back();
}

Expand Down
3 changes: 2 additions & 1 deletion codon/parser/visitors/translate/translate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ void TranslateVisitor::visit(PipeExpr *expr) {
simplePipeline &= !isGen(fn);

std::vector<ir::Value *> args;
args.reserve(call->args.size());
for (auto &a : call->args)
args.emplace_back(a.value->getEllipsis() ? nullptr : transform(a.value));
stages.emplace_back(fn, args, isGen(fn), false);
Expand Down Expand Up @@ -642,7 +643,7 @@ void TranslateVisitor::transformLLVMFunction(types::FuncType *type, FunctionStmt
ltrim(lp);
rtrim(lp);
// Extract declares and constants.
if (isDeclare && !startswith(lp, "declare ")) {
if (isDeclare && !startswith(lp, "declare ") && !startswith(lp, "@")) {
bool isConst = lp.find("private constant") != std::string::npos;
if (!isConst) {
isDeclare = false;
Expand Down
3 changes: 2 additions & 1 deletion codon/parser/visitors/typecheck/ctx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ int TypeContext::reorderNamedArgs(types::FuncType *func,
Emsg(Error::CALL_ARGS_MISSING, cache->rev(func->ast->name),
cache->reverseIdentifierLookup[func->ast->args[i].name]));
}
return score + onDone(starArgIndex, kwstarArgIndex, slots, partial);
auto s = onDone(starArgIndex, kwstarArgIndex, slots, partial);
return s != -1 ? score + s : -1;
}

void TypeContext::dump(int pad) {
Expand Down
3 changes: 3 additions & 0 deletions codon/parser/visitors/typecheck/ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ struct TypeContext : public Context<TypecheckItem> {
return item;
}
std::shared_ptr<TypecheckItem> find(const std::string &name) const override;
std::shared_ptr<TypecheckItem> find(const char *name) const {
return find(std::string(name));
}
/// Find an internal type. Assumes that it exists.
std::shared_ptr<TypecheckItem> forceFind(const std::string &name) const;
types::TypePtr getType(const std::string &name) const;
Expand Down
2 changes: 1 addition & 1 deletion codon/parser/visitors/typecheck/loops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void TypecheckVisitor::visit(ForStmt *stmt) {
unify(stmt->var->type,
iterType ? unify(val->type, iterType->generics[0].type) : val->type);

ctx->staticLoops.push_back("");
ctx->staticLoops.emplace_back();
ctx->blockLevel++;
transform(stmt->suite);
ctx->blockLevel--;
Expand Down
2 changes: 1 addition & 1 deletion codon/parser/visitors/typecheck/op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ void TypecheckVisitor::visit(InstantiateExpr *expr) {
} else {
if (expr->typeParams[i]->getNone()) // `None` -> `NoneType`
transformType(expr->typeParams[i]);
if (!expr->typeParams[i]->isType())
if (expr->typeParams[i]->type->getClass() && !expr->typeParams[i]->isType())
E(Error::EXPECTED_TYPE, expr->typeParams[i], "type");
t = ctx->instantiate(expr->typeParams[i]->getSrcInfo(),
expr->typeParams[i]->getType());
Expand Down
3 changes: 2 additions & 1 deletion codon/parser/visitors/typecheck/typecheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,9 @@ int TypecheckVisitor::canCall(const types::FuncTypePtr &fn,
if (slots[si].empty()) {
// is this "real" type?
if (in(niGenerics, fn->ast->args[si].name) &&
!fn->ast->args[si].defaultValue)
!fn->ast->args[si].defaultValue) {
return -1;
}
reordered.push_back({nullptr, 0});
} else {
reordered.push_back({args[slots[si][0]].value->type, slots[si][0]});
Expand Down
12 changes: 6 additions & 6 deletions stdlib/internal/types/collections/list.codon
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ class List:
self.arr = Array[T](10)
self.len = 0

def __init__(self, it: Generator[T]):
self.arr = Array[T](10)
def __init__(self, capacity: int):
self.arr = Array[T](capacity)
self.len = 0
for i in it:
self.append(i)

def __init__(self, other: List[T]):
self.arr = Array[T](other.len)
self.len = 0
for i in other:
self.append(i)

def __init__(self, capacity: int):
self.arr = Array[T](capacity)
def __init__(self, it: Generator[T]):
self.arr = Array[T](10)
self.len = 0
for i in it:
self.append(i)

def __init__(self, arr: Array[T], len: int):
self.arr = arr
Expand Down
8 changes: 8 additions & 0 deletions stdlib/internal/types/slice.codon
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class Slice:
stop: Optional[int]
step: Optional[int]

def __new__(stop: Optional[int]):
return Slice(None, stop, None)

def adjust_indices(self, length: int) -> Tuple[int, int, int, int]:
step: int = self.step if self.step is not None else 1
start: int = 0
Expand Down Expand Up @@ -47,6 +50,11 @@ class Slice:

return start, stop, step, 0

def indices(self, length: int):
if length < 0:
raise ValueError("length should not be negative")
return self.adjust_indices(length)[:-1]

def __repr__(self):
return f"slice({self.start}, {self.stop}, {self.step})"

Expand Down
Loading

0 comments on commit 6bb26e0

Please sign in to comment.