fhdl.ast: clean up stub error messages. NFC.

This commit is contained in:
whitequark 2018-12-14 22:54:07 +00:00
parent 2001359b66
commit c4ba5a3915

View file

@ -1,3 +1,4 @@
from abc import ABCMeta, abstractmethod
import builtins import builtins
import traceback import traceback
from collections import OrderedDict from collections import OrderedDict
@ -23,7 +24,7 @@ class DUID:
DUID.__next_uid += 1 DUID.__next_uid += 1
class Value: class Value(metaclass=ABCMeta):
@staticmethod @staticmethod
def wrap(obj): def wrap(obj):
"""Ensures that the passed object is a Migen value. Booleans and integers """Ensures that the passed object is a Migen value. Booleans and integers
@ -33,8 +34,7 @@ class Value:
elif isinstance(obj, (bool, int)): elif isinstance(obj, (bool, int)):
return Const(obj) return Const(obj)
else: else:
raise TypeError("Object {} of type {} is not a Migen value" raise TypeError("Object {!r} is not a Migen value".format(repr(obj)))
.format(repr(obj), type(obj)))
def __init__(self, src_loc_at=0): def __init__(self, src_loc_at=0):
super().__init__() super().__init__()
@ -171,6 +171,7 @@ class Value:
""" """
return Assign(self, value) return Assign(self, value)
@abstractmethod
def shape(self): def shape(self):
"""Bit length and signedness of a value. """Bit length and signedness of a value.
@ -187,16 +188,16 @@ class Value:
>>> Value.shape(C(0xaa)) >>> Value.shape(C(0xaa))
8, False 8, False
""" """
raise NotImplementedError # :nocov: pass # :nocov:
def _lhs_signals(self): def _lhs_signals(self):
raise TypeError("Value {!r} cannot be used in assignments".format(self)) raise TypeError("Value {!r} cannot be used in assignments".format(self))
@abstractmethod
def _rhs_signals(self): def _rhs_signals(self):
raise NotImplementedError # :nocov: pass # :nocov:
def __hash__(self): __hash__ = None
raise TypeError("Unhashable type: {}".format(type(self).__name__))
class Const(Value): class Const(Value):
@ -304,17 +305,15 @@ class Operator(Value):
else: else:
extra = 0 extra = 0
return obs[0][0] + extra, obs[0][1] return obs[0][0] + extra, obs[0][1]
elif self.op == "&" or self.op == "^" or self.op == "|": elif self.op in ("&", "^", "|"):
return self._bitwise_binary_shape(*obs) return self._bitwise_binary_shape(*obs)
elif (self.op == "<" or self.op == "<=" or self.op == "==" or self.op == "!=" or elif self.op in ("<", "<=", "==", "!=", ">", ">=", "b"):
self.op == ">" or self.op == ">=" or self.op == "b"):
return 1, False return 1, False
elif self.op == "~": elif self.op == "~":
return obs[0] return obs[0]
elif self.op == "m": elif self.op == "m":
return self._bitwise_binary_shape(obs[1], obs[2]) return self._bitwise_binary_shape(obs[1], obs[2])
else: raise NotImplementedError("Operator '{!r}' not implemented".format(self.op)) # :nocov:
raise TypeError # :nocov:
def _rhs_signals(self): def _rhs_signals(self):
return union(op._rhs_signals() for op in self.operands) return union(op._rhs_signals() for op in self.operands)
@ -612,6 +611,12 @@ class ClockSignal(Value):
raise TypeError("Clock domain name must be a string, not {!r}".format(domain)) raise TypeError("Clock domain name must be a string, not {!r}".format(domain))
self.domain = domain self.domain = domain
def shape(self):
return 1, False
def _rhs_signals(self):
raise NotImplementedError("ClockSignal must be lowered to a concrete signal") # :nocov:
def __repr__(self): def __repr__(self):
return "(clk {})".format(self.domain) return "(clk {})".format(self.domain)
@ -636,6 +641,12 @@ class ResetSignal(Value):
self.domain = domain self.domain = domain
self.allow_reset_less = allow_reset_less self.allow_reset_less = allow_reset_less
def shape(self):
return 1, False
def _rhs_signals(self):
raise NotImplementedError("ResetSignal must be lowered to a concrete signal") # :nocov:
def __repr__(self): def __repr__(self):
return "(rst {})".format(self.domain) return "(rst {})".format(self.domain)
@ -682,7 +693,8 @@ class Switch(Statement):
elif isinstance(key, str): elif isinstance(key, str):
assert len(key) == len(self.test) assert len(key) == len(self.test)
else: else:
raise TypeError raise TypeError("Object {!r} cannot be used as a switch key"
.format(key))
if not isinstance(stmts, Iterable): if not isinstance(stmts, Iterable):
stmts = [stmts] stmts = [stmts]
self.cases[key] = Statement.wrap(stmts) self.cases[key] = Statement.wrap(stmts)
@ -745,8 +757,8 @@ class ValueKey:
return hash(id(self.value)) return hash(id(self.value))
elif isinstance(self.value, Slice): elif isinstance(self.value, Slice):
return hash((ValueKey(self.value.value), self.value.start, self.value.end)) return hash((ValueKey(self.value.value), self.value.start, self.value.end))
else: else: # :nocov:
raise TypeError raise TypeError("Object {!r} cannot be used as a key in value collections")
def __eq__(self, other): def __eq__(self, other):
if not isinstance(other, ValueKey): if not isinstance(other, ValueKey):
@ -762,8 +774,8 @@ class ValueKey:
return (ValueKey(self.value.value) == ValueKey(other.value.value) and return (ValueKey(self.value.value) == ValueKey(other.value.value) and
self.value.start == other.value.start and self.value.start == other.value.start and
self.value.end == other.value.end) self.value.end == other.value.end)
else: else: # :nocov:
raise TypeError raise TypeError("Object {!r} cannot be used as a key in value collections")
def __lt__(self, other): def __lt__(self, other):
if not isinstance(other, ValueKey): if not isinstance(other, ValueKey):
@ -779,8 +791,8 @@ class ValueKey:
return (ValueKey(self.value.value) < ValueKey(other.value.value) and return (ValueKey(self.value.value) < ValueKey(other.value.value) and
self.value.start < other.value.start and self.value.start < other.value.start and
self.value.end < other.value.end) self.value.end < other.value.end)
else: else: # :nocov:
raise TypeError raise TypeError("Object {!r} cannot be used as a key in value collections")
def __repr__(self): def __repr__(self):
return "<{}.ValueKey {!r}>".format(__name__, self.value) return "<{}.ValueKey {!r}>".format(__name__, self.value)