Skip to content

Commit 9807c3f

Browse files
committed
init
1 parent d7c758f commit 9807c3f

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
lines changed

python/taichi/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
# Issue#2223: Do not reorder, or we're busted with partially initialized module
1717
from taichi import aot # isort:skip
1818

19+
from taichi.lang.ast.hint import new, hide
20+
1921

2022
def __getattr__(attr):
2123
if attr == "cfg":

python/taichi/lang/ast/ast_transformer.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from taichi.lang._ndrange import _Ndrange, ndrange
1717
from taichi.lang.argpack import ArgPackType
1818
from taichi.lang.ast.ast_transformer_utils import Builder, LoopStatus, ReturnStatus
19+
from taichi.lang.ast import hint
1920
from taichi.lang.ast.symbol_resolver import ASTResolver
2021
from taichi.lang.exception import (
2122
TaichiIndexError,
@@ -105,18 +106,29 @@ def build_assign_annotated(ctx, target, value, is_static_assign, annotation):
105106
f'Kernel argument "{target.id}" is immutable in the kernel. '
106107
f"If you want to change its value, please create a new variable."
107108
)
108-
anno = impl.expr_init(annotation)
109-
if is_static_assign:
110-
raise TaichiSyntaxError("Static assign cannot be used on annotated assignment")
111-
if is_local and not ctx.is_var_declared(target.id):
112-
var = ti_ops.cast(value, anno)
113-
var = impl.expr_init(var)
109+
if annotation is hint.new:
110+
var = impl.expr_init(value)
114111
ctx.create_variable(target.id, var)
112+
elif annotation is hint.hide:
113+
var = impl.expr_init(value)
114+
ctx.hide_variable(target.id, var)
115115
else:
116-
var = build_stmt(ctx, target)
117-
if var.ptr.get_rvalue_type() != anno:
118-
raise TaichiSyntaxError("Static assign cannot have type overloading")
119-
var._assign(value)
116+
anno = impl.expr_init(annotation)
117+
if is_static_assign:
118+
raise TaichiSyntaxError(
119+
"Static assign cannot be used on annotated assignment"
120+
)
121+
if is_local and not ctx.is_var_declared(target.id):
122+
var = ti_ops.cast(value, anno)
123+
var = impl.expr_init(var)
124+
ctx.create_variable(target.id, var)
125+
else:
126+
var = build_stmt(ctx, target)
127+
if var.ptr.get_rvalue_type() != anno:
128+
raise TaichiSyntaxError(
129+
"Static assign cannot have type overloading"
130+
)
131+
var._assign(value)
120132
return var
121133

122134
@staticmethod

python/taichi/lang/ast/ast_transformer_utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,15 @@ def is_var_declared(self, name):
240240
return False
241241

242242
def create_variable(self, name, var):
243-
if name in self.current_scope():
244-
raise TaichiSyntaxError("Recreating variables is not allowed")
243+
# if name in self.current_scope():
244+
# raise TaichiSyntaxError("Recreating variables is not allowed")
245245
self.current_scope()[name] = var
246246

247+
def hide_variable(self, name, var):
248+
for s in reversed(self.local_scopes):
249+
if name in s:
250+
s[name] = var
251+
247252
def check_loop_var(self, loop_var):
248253
if self.is_var_declared(loop_var):
249254
raise TaichiSyntaxError(

python/taichi/lang/ast/hint.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from typing import Any
2+
3+
4+
type new = Any
5+
type hide = Any

0 commit comments

Comments
 (0)