From a6950bf11dd534487bda577b549206180e11502b Mon Sep 17 00:00:00 2001 From: whitequark Date: Thu, 16 Jul 2015 14:56:39 +0300 Subject: [PATCH] Move builtin.is_{builtin,exn_constructor} to types. --- artiq/compiler/builtins.py | 13 ------------- artiq/compiler/transforms/inferencer.py | 18 +++++++++--------- artiq/compiler/types.py | 17 +++++++++++++++-- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/artiq/compiler/builtins.py b/artiq/compiler/builtins.py index bbdc6462b..5fdd49708 100644 --- a/artiq/compiler/builtins.py +++ b/artiq/compiler/builtins.py @@ -120,19 +120,6 @@ def is_collection(typ): return isinstance(typ, types.TTuple) or \ types.is_mono(typ, "list") -def is_builtin(typ, name): - typ = typ.find() - return isinstance(typ, types.TBuiltin) and \ - typ.name == name - -def is_exn_constructor(typ, name=None): - typ = typ.find() - if name is not None: - return isinstance(typ, types.TExceptionConstructor) and \ - typ.name == name - else: - return isinstance(typ, types.TExceptionConstructor) - def is_mutable(typ): return typ.fold(False, lambda accum, typ: is_list(typ) or types.is_function(typ)) diff --git a/artiq/compiler/transforms/inferencer.py b/artiq/compiler/transforms/inferencer.py index cc224bb07..5400da937 100644 --- a/artiq/compiler/transforms/inferencer.py +++ b/artiq/compiler/transforms/inferencer.py @@ -432,7 +432,7 @@ class Inferencer(algorithm.Visitor): node.func.loc, notes=valid_forms) self.engine.process(diag) - if builtins.is_builtin(typ, "bool"): + if types.is_builtin(typ, "bool"): valid_forms = lambda: [ valid_form("bool() -> bool"), valid_form("bool(x:'a) -> bool") @@ -448,7 +448,7 @@ class Inferencer(algorithm.Visitor): self._unify(node.type, builtins.TBool(), node.loc, None) - elif builtins.is_builtin(typ, "int"): + elif types.is_builtin(typ, "int"): valid_forms = lambda: [ valid_form("int() -> int(width='a)"), valid_form("int(x:'a) -> int(width='b) where 'a is numeric"), @@ -482,7 +482,7 @@ class Inferencer(algorithm.Visitor): node.loc, None) else: diagnose(valid_forms()) - elif builtins.is_builtin(typ, "float"): + elif types.is_builtin(typ, "float"): valid_forms = lambda: [ valid_form("float() -> float"), valid_form("float(x:'a) -> float where 'a is numeric") @@ -501,7 +501,7 @@ class Inferencer(algorithm.Visitor): pass else: diagnose(valid_forms()) - elif builtins.is_builtin(typ, "list"): + elif types.is_builtin(typ, "list"): valid_forms = lambda: [ valid_form("list() -> list(elt='a)"), valid_form("list(x:'a) -> list(elt='b) where 'a is iterable") @@ -530,7 +530,7 @@ class Inferencer(algorithm.Visitor): self.engine.process(diag) else: diagnose(valid_forms()) - elif builtins.is_builtin(typ, "range"): + elif types.is_builtin(typ, "range"): valid_forms = lambda: [ valid_form("range(max:'a) -> range(elt='a)"), valid_form("range(min:'a, max:'a) -> range(elt='a)"), @@ -561,7 +561,7 @@ class Inferencer(algorithm.Visitor): self.engine.process(diag) else: diagnose(valid_forms()) - elif builtins.is_builtin(typ, "len"): + elif types.is_builtin(typ, "len"): valid_forms = lambda: [ valid_form("len(x:'a) -> int(width='b) where 'a is iterable"), ] @@ -588,7 +588,7 @@ class Inferencer(algorithm.Visitor): self.engine.process(diag) else: diagnose(valid_forms()) - elif builtins.is_builtin(typ, "round"): + elif types.is_builtin(typ, "round"): valid_forms = lambda: [ valid_form("round(x:float) -> int(width='a)"), ] @@ -604,7 +604,7 @@ class Inferencer(algorithm.Visitor): else: diagnose(valid_forms()) # TODO: add when it is clear what interface syscall() has - # elif builtins.is_builtin(typ, "syscall"): + # elif types.is_builtin(typ, "syscall"): # valid_Forms = lambda: [ # ] @@ -778,7 +778,7 @@ class Inferencer(algorithm.Visitor): def visit_ExceptHandlerT(self, node): self.generic_visit(node) - if not builtins.is_exn_constructor(node.filter.type): + if not types.is_exn_constructor(node.filter.type): diag = diagnostic.Diagnostic("error", "this expression must refer to an exception constructor", {"type": types.TypePrinter().name(node.filter.type)}, diff --git a/artiq/compiler/types.py b/artiq/compiler/types.py index eb89809b1..4cff68161 100644 --- a/artiq/compiler/types.py +++ b/artiq/compiler/types.py @@ -322,8 +322,21 @@ def is_tuple(typ, elts=None): def is_function(typ): return isinstance(typ.find(), TFunction) -def is_builtin(typ): - return isinstance(typ.find(), TBuiltin) +def is_builtin(typ, name=None): + typ = typ.find() + if name is None: + return isinstance(typ, TBuiltin) + else: + return isinstance(typ, TBuiltin) and \ + typ.name == name + +def is_exn_constructor(typ, name=None): + typ = typ.find() + if name is not None: + return isinstance(typ, TExceptionConstructor) and \ + typ.name == name + else: + return isinstance(typ, TExceptionConstructor) def get_value(typ): typ = typ.find()