2016-12-02 23:02:44 +08:00
|
|
|
"""
|
|
|
|
:class:`CastMonomorphizer` uses explicit casts to monomorphize
|
|
|
|
expressions of undetermined integer type to either 32 or 64 bits.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from pythonparser import algorithm, diagnostic
|
2016-12-05 13:08:57 +08:00
|
|
|
from .. import types, builtins, asttyped
|
2016-12-02 23:02:44 +08:00
|
|
|
|
|
|
|
class CastMonomorphizer(algorithm.Visitor):
|
|
|
|
def __init__(self, engine):
|
|
|
|
self.engine = engine
|
|
|
|
|
|
|
|
def visit_CallT(self, node):
|
2016-12-05 13:04:26 +08:00
|
|
|
if (types.is_builtin(node.func.type, "int") or
|
|
|
|
types.is_builtin(node.func.type, "int32") or
|
|
|
|
types.is_builtin(node.func.type, "int64")):
|
2016-12-02 23:02:44 +08:00
|
|
|
typ = node.type.find()
|
|
|
|
if (not types.is_var(typ["width"]) and
|
2019-01-12 21:37:20 +08:00
|
|
|
len(node.args) == 1 and
|
2016-12-02 23:02:44 +08:00
|
|
|
builtins.is_int(node.args[0].type) and
|
|
|
|
types.is_var(node.args[0].type.find()["width"])):
|
2016-12-05 13:08:57 +08:00
|
|
|
if isinstance(node.args[0], asttyped.BinOpT):
|
|
|
|
# Binary operations are a bit special: they can widen, and so their
|
|
|
|
# return type is indeterminate until both argument types are fully known.
|
|
|
|
# In case we first monomorphize the return type, and then some argument type,
|
|
|
|
# and argument type is wider than return type, we'll introduce a conflict.
|
|
|
|
return
|
|
|
|
|
2016-12-02 23:02:44 +08:00
|
|
|
node.args[0].type.unify(typ)
|
|
|
|
|
compiler: monomorphize casts first, but more carefully.
This reverts 425cd7851, which broke the use of casts to define
integer width.
Instead of it, two steps are taken:
* First, literals are monomorphized, leading to predictable result.
* Second, casts are monomorphized, in a top-bottom way. I.e.
consider the expression `int64(round(x))`. If round() was visited
first, the intermediate precision would be 32-bit, which is
clearly undesirable. Therefore, contextual rules should take
priority over non-contextual ones.
Fixes #1252.
2019-02-07 14:07:55 +08:00
|
|
|
if types.is_builtin(node.func.type, "int") or \
|
|
|
|
types.is_builtin(node.func.type, "round"):
|
|
|
|
typ = node.type.find()
|
|
|
|
if types.is_var(typ["width"]):
|
|
|
|
typ["width"].unify(types.TValue(32))
|
|
|
|
|
|
|
|
self.generic_visit(node)
|
|
|
|
|
|
|
|
def visit_CoerceT(self, node):
|
|
|
|
if isinstance(node.value, asttyped.NumT) and \
|
|
|
|
builtins.is_int(node.type) and \
|
|
|
|
builtins.is_int(node.value.type) and \
|
|
|
|
not types.is_var(node.type["width"]) and \
|
|
|
|
types.is_var(node.value.type["width"]):
|
|
|
|
node.value.type.unify(node.type)
|
|
|
|
|
|
|
|
self.generic_visit(node)
|