Skip to content

Commit

Permalink
Property setters (#499)
Browse files Browse the repository at this point in the history
* Fix __from_gpu_new__

* Fix GPU tests

* Update GPU debug codegen

* Add will-return attribute for GPU compilation

* Fix isinstance on unresolved types

* Fix union type instantiation and pendingRealizations placement

* Add float16, bfloat16 and float128 IR types

* Add float16, bfloat16 and float128 types

* Mark complex64 as no-python

* Fix float methods

* Add float tests

* Disable some float tests

* Fix bitset in reaching definitions analysis

* Fix static bool unification

* Add property setters

* Remove log

---------

Co-authored-by: Ibrahim Numanagić <ibrahimpasa@gmail.com>
  • Loading branch information
arshajii and inumanag committed Nov 22, 2023
1 parent a257678 commit 7fdbc7f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
14 changes: 13 additions & 1 deletion codon/parser/visitors/simplify/class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,20 @@ void SimplifyVisitor::visit(ClassStmt *stmt) {
// Add class methods
for (const auto &sp : getClassMethods(stmt->suite))
if (sp && sp->getFunction()) {
if (sp.get() != autoDeducedInit.second)
if (sp.get() != autoDeducedInit.second) {
auto &ds = sp->getFunction()->decorators;
for (auto &dc : ds) {
if (auto d = dc->getDot()) {
if (d->member == "setter" and d->expr->isId(sp->getFunction()->name) &&
sp->getFunction()->args.size() == 2) {
sp->getFunction()->name = format(".set_{}", sp->getFunction()->name);
dc = nullptr;
break;
}
}
}
fnStmts.push_back(transform(sp));
}
}

// After popping context block, record types and nested classes will disappear.
Expand Down
2 changes: 2 additions & 0 deletions codon/parser/visitors/simplify/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ void SimplifyVisitor::visit(FunctionStmt *stmt) {

// Parse attributes
for (auto i = stmt->decorators.size(); i-- > 0;) {
if (!stmt->decorators[i])
continue;
auto [isAttr, attrName] = getDecorator(stmt->decorators[i]);
if (!attrName.empty()) {
stmt->attributes.set(attrName);
Expand Down
9 changes: 8 additions & 1 deletion codon/parser/visitors/typecheck/assign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,14 @@ void TypecheckVisitor::visit(AssignMemberStmt *stmt) {
if (auto lhsClass = stmt->lhs->getType()->getClass()) {
auto member = ctx->findMember(lhsClass, stmt->member);

if (!member && stmt->lhs->isType()) {
if (!member) {
// Case: setters
auto setters = ctx->findMethod(lhsClass.get(), format(".set_{}", stmt->member));
if (!setters.empty()) {
resultStmt = transform(N<ExprStmt>(
N<CallExpr>(N<IdExpr>(setters[0]->ast->name), stmt->lhs, stmt->rhs)));
return;
}
// Case: class variables
if (auto cls = in(ctx->cache->classes, lhsClass->name))
if (auto var = in(cls->classVars, stmt->member)) {
Expand Down
27 changes: 27 additions & 0 deletions test/parser/simplify_stmt.codon
Original file line number Diff line number Diff line change
Expand Up @@ -1276,3 +1276,30 @@ def f(x: int) -> int:
return g(y - 1) * z
return g(4)
print(f(3)) #: 1296

#%% class_setter,barebones
class Foo:
_x: int

@property
def x(self):
print('getter')
return self._x

@x.setter
def x(self, v):
print('setter')
self._x = v

f = Foo(1)
print(f.x)
#: getter
#: 1

f.x = 99
print(f.x)
print(f._x)
#: setter
#: getter
#: 99
#: 99

0 comments on commit 7fdbc7f

Please sign in to comment.