Skip to content

Commit 435559f

Browse files
author
whitequark
committedAug 10, 2015
Allow type annotations on remotely called functions.
1 parent b28a874 commit 435559f

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed
 

Diff for: ‎artiq/compiler/embedding.py

+35-7
Original file line numberDiff line numberDiff line change
@@ -232,22 +232,44 @@ def _quote_embedded_function(self, function):
232232
quote_function=self._quote_function)
233233
return asttyped_rewriter.visit(function_node)
234234

235-
def _function_def_note(self, function):
235+
def _function_loc(self, function):
236236
filename = function.__code__.co_filename
237237
line = function.__code__.co_firstlineno
238238
name = function.__code__.co_name
239239

240240
source_line = linecache.getline(filename, line)
241241
column = re.search("def", source_line).start(0)
242242
source_buffer = source.Buffer(source_line, filename, line)
243-
loc = source.Range(source_buffer, column, column)
243+
return source.Range(source_buffer, column, column)
244+
245+
def _function_def_note(self, function):
244246
return diagnostic.Diagnostic("note",
245247
"definition of function '{function}'",
246-
{"function": name},
247-
loc)
248+
{"function": function.__name__},
249+
self._function_loc(function))
250+
251+
def _extract_annot(self, function, annot, kind, call_loc):
252+
if not isinstance(annot, types.Type):
253+
note = diagnostic.Diagnostic("note",
254+
"in function called remotely here", {},
255+
call_loc)
256+
diag = diagnostic.Diagnostic("error",
257+
"type annotation for {kind}, '{annot}', is not an ARTIQ type",
258+
{"kind": kind, "annot": repr(annot)},
259+
self._function_loc(function),
260+
notes=[note])
261+
self.engine.process(diag)
262+
263+
return types.TVar()
264+
else:
265+
return annot
248266

249267
def _type_of_param(self, function, loc, param):
250-
if param.default is not inspect.Parameter.empty:
268+
if param.annotation is not inspect.Parameter.empty:
269+
# Type specified explicitly.
270+
return self._extract_annot(function, param.annotation,
271+
"argument {}".format(param.name), loc)
272+
elif param.default is not inspect.Parameter.empty:
251273
# Try and infer the type from the default value.
252274
# This is tricky, because the default value might not have
253275
# a well-defined type in APython.
@@ -300,8 +322,14 @@ def _quote_rpc_function(self, function, loc):
300322
else:
301323
optarg_types[param.name] = self._type_of_param(function, loc, param)
302324

303-
# Fixed for now.
304-
ret_type = builtins.TInt(types.TValue(32))
325+
if signature.return_annotation is not inspect.Signature.empty:
326+
ret_type = self._extract_annot(function, signature.return_annotation,
327+
"return type", loc)
328+
else:
329+
diag = diagnostic.Diagnostic("fatal",
330+
"function must have a return type specified to be called remotely", {},
331+
self._function_loc(function))
332+
self.engine.process(diag)
305333

306334
rpc_type = types.TRPCFunction(arg_types, optarg_types, ret_type,
307335
service=self._map(function))

Diff for: ‎artiq/language/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
from artiq.language import core, environment, units, scan
1+
from artiq.language import core, types, environment, units, scan
22
from artiq.language.core import *
3+
from artiq.language.types import *
34
from artiq.language.environment import *
45
from artiq.language.units import *
56
from artiq.language.scan import *
67

78

89
__all__ = []
910
__all__.extend(core.__all__)
11+
__all__.extend(types.__all__)
1012
__all__.extend(environment.__all__)
1113
__all__.extend(units.__all__)
1214
__all__.extend(scan.__all__)

Diff for: ‎artiq/language/types.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Values representing ARTIQ types, to be used in function type
3+
annotations.
4+
"""
5+
6+
from artiq.compiler import types, builtins
7+
8+
__all__ = ["TNone", "TBool", "TInt32", "TInt64", "TFloat",
9+
"TStr", "TList", "TRange32", "TRange64"]
10+
11+
TNone = builtins.TNone()
12+
TBool = builtins.TBool()
13+
TInt32 = builtins.TInt(types.TValue(32))
14+
TInt64 = builtins.TInt(types.TValue(64))
15+
TFloat = builtins.TFloat()
16+
TStr = builtins.TStr()
17+
TList = builtins.TList
18+
TRange32 = builtins.TRange(builtins.TInt(types.TValue(32)))
19+
TRange64 = builtins.TRange(builtins.TInt(types.TValue(64)))

0 commit comments

Comments
 (0)
Please sign in to comment.