Skip to content

Commit 5a2306a

Browse files
author
whitequark
committedAug 8, 2016
compiler.embedding: implement type annotations for function arguments.
Fixes #318.
1 parent 8a243d3 commit 5a2306a

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed
 

Diff for: ‎artiq/compiler/embedding.py

+47
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,53 @@ def __init__(self, engine, prelude, globals, host_environment, quote):
415415
self.host_environment = host_environment
416416
self.quote = quote
417417

418+
def match_annotation(self, annot):
419+
if isinstance(annot, ast.Name):
420+
if annot.id == "TNone":
421+
return builtins.TNone()
422+
if annot.id == "TBool":
423+
return builtins.TBool()
424+
if annot.id == "TInt32":
425+
return builtins.TInt(types.TValue(32))
426+
if annot.id == "TInt64":
427+
return builtins.TInt(types.TValue(64))
428+
if annot.id == "TFloat":
429+
return builtins.TFloat()
430+
if annot.id == "TStr":
431+
return builtins.TStr()
432+
if annot.id == "TRange32":
433+
return builtins.TRange(builtins.TInt(types.TValue(32)))
434+
if annot.id == "TRange64":
435+
return builtins.TRange(builtins.TInt(types.TValue(64)))
436+
if annot.id == "TVar":
437+
return types.TVar()
438+
elif (isinstance(annot, ast.Call) and
439+
annot.keywords is None and
440+
annot.starargs is None and
441+
annot.kwargs is None and
442+
isinstance(annot.func, ast.Name)):
443+
if annot.func.id == "TList" and len(annot.args) == 1:
444+
elttyp = self.match_annotation(annot.args[0])
445+
if elttyp is not None:
446+
return builtins.TList()
447+
else:
448+
return None
449+
450+
if annot is not None:
451+
diag = diagnostic.Diagnostic("error",
452+
"unrecognized type annotation", {},
453+
annot.loc)
454+
self.engine.process(diag)
455+
456+
def visit_arg(self, node):
457+
typ = self._find_name(node.arg, node.loc)
458+
annottyp = self.match_annotation(node.annotation)
459+
if annottyp is not None:
460+
typ.unify(annottyp)
461+
return asttyped.argT(type=typ,
462+
arg=node.arg, annotation=None,
463+
arg_loc=node.arg_loc, colon_loc=node.colon_loc, loc=node.loc)
464+
418465
def visit_quoted_function(self, node, function):
419466
extractor = LocalExtractor(env_stack=self.env_stack, engine=self.engine)
420467
extractor.visit(node)

Diff for: ‎artiq/compiler/transforms/asttyped_rewriter.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,14 @@ def visit_ClassDef(self, node):
307307
self.in_class = old_in_class
308308

309309
def visit_arg(self, node):
310+
if node.annotation is not None:
311+
diag = diagnostic.Diagnostic("fatal",
312+
"type annotations are not supported here", {},
313+
node.annotation.loc)
314+
self.engine.process(diag)
315+
310316
return asttyped.argT(type=self._find_name(node.arg, node.loc),
311-
arg=node.arg, annotation=self.visit(node.annotation),
317+
arg=node.arg, annotation=None,
312318
arg_loc=node.arg_loc, colon_loc=node.colon_loc, loc=node.loc)
313319

314320
def visit_Num(self, node):

Diff for: ‎artiq/test/coredevice/test_embedding.py

+15
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,21 @@ def test_args(self):
137137
exp.builtin()
138138

139139

140+
class _Annotation(EnvExperiment):
141+
def build(self):
142+
self.setattr_device("core")
143+
144+
@kernel
145+
def overflow(self, x: TInt64) -> TBool:
146+
return (x << 32) != 0
147+
148+
149+
class AnnotationTest(ExperimentCase):
150+
def test_annotation(self):
151+
exp = self.create(_Annotation)
152+
self.assertEqual(exp.overflow(1), True)
153+
154+
140155
class _Payload1MB(EnvExperiment):
141156
def build(self):
142157
self.setattr_device("core")

0 commit comments

Comments
 (0)
Please sign in to comment.