builder.py 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. """Builder class used to transform a mypy AST to the IR form.
  2. The IRBuilder class maintains transformation state and provides access
  3. to various helpers used to implement the transform.
  4. The top-level transform control logic is in mypyc.irbuild.main.
  5. mypyc.irbuild.visitor.IRBuilderVisitor is used to dispatch based on mypy
  6. AST node type to code that actually does the bulk of the work. For
  7. example, expressions are transformed in mypyc.irbuild.expression and
  8. functions are transformed in mypyc.irbuild.function.
  9. """
  10. from __future__ import annotations
  11. from contextlib import contextmanager
  12. from typing import Any, Callable, Final, Iterator, Sequence, Union
  13. from typing_extensions import overload
  14. from mypy.build import Graph
  15. from mypy.maptype import map_instance_to_supertype
  16. from mypy.nodes import (
  17. ARG_NAMED,
  18. ARG_POS,
  19. GDEF,
  20. LDEF,
  21. ArgKind,
  22. CallExpr,
  23. Decorator,
  24. Expression,
  25. FuncDef,
  26. IndexExpr,
  27. IntExpr,
  28. Lvalue,
  29. MemberExpr,
  30. MypyFile,
  31. NameExpr,
  32. OpExpr,
  33. OverloadedFuncDef,
  34. RefExpr,
  35. StarExpr,
  36. Statement,
  37. SymbolNode,
  38. TupleExpr,
  39. TypeAlias,
  40. TypeInfo,
  41. UnaryExpr,
  42. Var,
  43. )
  44. from mypy.types import (
  45. AnyType,
  46. DeletedType,
  47. Instance,
  48. ProperType,
  49. TupleType,
  50. Type,
  51. TypedDictType,
  52. TypeOfAny,
  53. UninhabitedType,
  54. UnionType,
  55. get_proper_type,
  56. )
  57. from mypy.util import split_target
  58. from mypy.visitor import ExpressionVisitor, StatementVisitor
  59. from mypyc.common import BITMAP_BITS, SELF_NAME, TEMP_ATTR_NAME
  60. from mypyc.crash import catch_errors
  61. from mypyc.errors import Errors
  62. from mypyc.ir.class_ir import ClassIR, NonExtClassInfo
  63. from mypyc.ir.func_ir import INVALID_FUNC_DEF, FuncDecl, FuncIR, FuncSignature, RuntimeArg
  64. from mypyc.ir.ops import (
  65. NAMESPACE_MODULE,
  66. Assign,
  67. BasicBlock,
  68. Branch,
  69. ComparisonOp,
  70. GetAttr,
  71. InitStatic,
  72. Integer,
  73. IntOp,
  74. LoadStatic,
  75. Op,
  76. RaiseStandardError,
  77. Register,
  78. SetAttr,
  79. TupleGet,
  80. Unreachable,
  81. Value,
  82. )
  83. from mypyc.ir.rtypes import (
  84. RInstance,
  85. RTuple,
  86. RType,
  87. RUnion,
  88. bitmap_rprimitive,
  89. c_pyssize_t_rprimitive,
  90. dict_rprimitive,
  91. int_rprimitive,
  92. is_float_rprimitive,
  93. is_list_rprimitive,
  94. is_none_rprimitive,
  95. is_object_rprimitive,
  96. is_tagged,
  97. is_tuple_rprimitive,
  98. none_rprimitive,
  99. object_rprimitive,
  100. str_rprimitive,
  101. )
  102. from mypyc.irbuild.context import FuncInfo, ImplicitClass
  103. from mypyc.irbuild.ll_builder import LowLevelIRBuilder
  104. from mypyc.irbuild.mapper import Mapper
  105. from mypyc.irbuild.nonlocalcontrol import (
  106. BaseNonlocalControl,
  107. GeneratorNonlocalControl,
  108. LoopNonlocalControl,
  109. NonlocalControl,
  110. )
  111. from mypyc.irbuild.prebuildvisitor import PreBuildVisitor
  112. from mypyc.irbuild.prepare import RegisterImplInfo
  113. from mypyc.irbuild.targets import (
  114. AssignmentTarget,
  115. AssignmentTargetAttr,
  116. AssignmentTargetIndex,
  117. AssignmentTargetRegister,
  118. AssignmentTargetTuple,
  119. )
  120. from mypyc.irbuild.util import bytes_from_str, is_constant
  121. from mypyc.options import CompilerOptions
  122. from mypyc.primitives.dict_ops import dict_get_item_op, dict_set_item_op
  123. from mypyc.primitives.generic_ops import iter_op, next_op, py_setattr_op
  124. from mypyc.primitives.list_ops import list_get_item_unsafe_op, list_pop_last, to_list
  125. from mypyc.primitives.misc_ops import check_unpack_count_op, get_module_dict_op, import_op
  126. from mypyc.primitives.registry import CFunctionDescription, function_ops
  127. # These int binary operations can borrow their operands safely, since the
  128. # primitives take this into consideration.
  129. int_borrow_friendly_op: Final = {"+", "-", "==", "!=", "<", "<=", ">", ">="}
  130. class IRVisitor(ExpressionVisitor[Value], StatementVisitor[None]):
  131. pass
  132. class UnsupportedException(Exception):
  133. pass
  134. SymbolTarget = Union[AssignmentTargetRegister, AssignmentTargetAttr]
  135. class IRBuilder:
  136. def __init__(
  137. self,
  138. current_module: str,
  139. types: dict[Expression, Type],
  140. graph: Graph,
  141. errors: Errors,
  142. mapper: Mapper,
  143. pbv: PreBuildVisitor,
  144. visitor: IRVisitor,
  145. options: CompilerOptions,
  146. singledispatch_impls: dict[FuncDef, list[RegisterImplInfo]],
  147. ) -> None:
  148. self.builder = LowLevelIRBuilder(current_module, errors, mapper, options)
  149. self.builders = [self.builder]
  150. self.symtables: list[dict[SymbolNode, SymbolTarget]] = [{}]
  151. self.runtime_args: list[list[RuntimeArg]] = [[]]
  152. self.function_name_stack: list[str] = []
  153. self.class_ir_stack: list[ClassIR] = []
  154. self.current_module = current_module
  155. self.mapper = mapper
  156. self.types = types
  157. self.graph = graph
  158. self.ret_types: list[RType] = []
  159. self.functions: list[FuncIR] = []
  160. self.classes: list[ClassIR] = []
  161. self.final_names: list[tuple[str, RType]] = []
  162. self.callable_class_names: set[str] = set()
  163. self.options = options
  164. # These variables keep track of the number of lambdas, implicit indices, and implicit
  165. # iterators instantiated so we avoid name conflicts. The indices and iterators are
  166. # instantiated from for-loops.
  167. self.lambda_counter = 0
  168. self.temp_counter = 0
  169. # These variables are populated from the first-pass PreBuildVisitor.
  170. self.free_variables = pbv.free_variables
  171. self.prop_setters = pbv.prop_setters
  172. self.encapsulating_funcs = pbv.encapsulating_funcs
  173. self.nested_fitems = pbv.nested_funcs.keys()
  174. self.fdefs_to_decorators = pbv.funcs_to_decorators
  175. self.module_import_groups = pbv.module_import_groups
  176. self.singledispatch_impls = singledispatch_impls
  177. self.visitor = visitor
  178. # This list operates similarly to a function call stack for nested functions. Whenever a
  179. # function definition begins to be generated, a FuncInfo instance is added to the stack,
  180. # and information about that function (e.g. whether it is nested, its environment class to
  181. # be generated) is stored in that FuncInfo instance. When the function is done being
  182. # generated, its corresponding FuncInfo is popped off the stack.
  183. self.fn_info = FuncInfo(INVALID_FUNC_DEF, "", "")
  184. self.fn_infos: list[FuncInfo] = [self.fn_info]
  185. # This list operates as a stack of constructs that modify the
  186. # behavior of nonlocal control flow constructs.
  187. self.nonlocal_control: list[NonlocalControl] = []
  188. self.errors = errors
  189. # Notionally a list of all of the modules imported by the
  190. # module being compiled, but stored as an OrderedDict so we
  191. # can also do quick lookups.
  192. self.imports: dict[str, None] = {}
  193. self.can_borrow = False
  194. # High-level control
  195. def set_module(self, module_name: str, module_path: str) -> None:
  196. """Set the name and path of the current module.
  197. This must be called before transforming any AST nodes.
  198. """
  199. self.module_name = module_name
  200. self.module_path = module_path
  201. self.builder.set_module(module_name, module_path)
  202. @overload
  203. def accept(self, node: Expression, *, can_borrow: bool = False) -> Value:
  204. ...
  205. @overload
  206. def accept(self, node: Statement) -> None:
  207. ...
  208. def accept(self, node: Statement | Expression, *, can_borrow: bool = False) -> Value | None:
  209. """Transform an expression or a statement.
  210. If can_borrow is true, prefer to generate a borrowed reference.
  211. Borrowed references are faster since they don't require reference count
  212. manipulation, but they are only safe to use in specific contexts.
  213. """
  214. with self.catch_errors(node.line):
  215. if isinstance(node, Expression):
  216. old_can_borrow = self.can_borrow
  217. self.can_borrow = can_borrow
  218. try:
  219. res = node.accept(self.visitor)
  220. res = self.coerce(res, self.node_type(node), node.line)
  221. # If we hit an error during compilation, we want to
  222. # keep trying, so we can produce more error
  223. # messages. Generate a temp of the right type to keep
  224. # from causing more downstream trouble.
  225. except UnsupportedException:
  226. res = Register(self.node_type(node))
  227. self.can_borrow = old_can_borrow
  228. if not can_borrow:
  229. self.flush_keep_alives()
  230. return res
  231. else:
  232. try:
  233. node.accept(self.visitor)
  234. except UnsupportedException:
  235. pass
  236. return None
  237. def flush_keep_alives(self) -> None:
  238. self.builder.flush_keep_alives()
  239. # Pass through methods for the most common low-level builder ops, for convenience.
  240. def add(self, op: Op) -> Value:
  241. return self.builder.add(op)
  242. def goto(self, target: BasicBlock) -> None:
  243. self.builder.goto(target)
  244. def activate_block(self, block: BasicBlock) -> None:
  245. self.builder.activate_block(block)
  246. def goto_and_activate(self, block: BasicBlock) -> None:
  247. self.builder.goto_and_activate(block)
  248. def self(self) -> Register:
  249. return self.builder.self()
  250. def py_get_attr(self, obj: Value, attr: str, line: int) -> Value:
  251. return self.builder.py_get_attr(obj, attr, line)
  252. def load_str(self, value: str) -> Value:
  253. return self.builder.load_str(value)
  254. def load_bytes_from_str_literal(self, value: str) -> Value:
  255. """Load bytes object from a string literal.
  256. The literal characters of BytesExpr (the characters inside b'')
  257. are stored in BytesExpr.value, whose type is 'str' not 'bytes'.
  258. Thus we perform a special conversion here.
  259. """
  260. return self.builder.load_bytes(bytes_from_str(value))
  261. def load_int(self, value: int) -> Value:
  262. return self.builder.load_int(value)
  263. def load_float(self, value: float) -> Value:
  264. return self.builder.load_float(value)
  265. def unary_op(self, lreg: Value, expr_op: str, line: int) -> Value:
  266. return self.builder.unary_op(lreg, expr_op, line)
  267. def binary_op(self, lreg: Value, rreg: Value, expr_op: str, line: int) -> Value:
  268. return self.builder.binary_op(lreg, rreg, expr_op, line)
  269. def coerce(self, src: Value, target_type: RType, line: int, force: bool = False) -> Value:
  270. return self.builder.coerce(src, target_type, line, force, can_borrow=self.can_borrow)
  271. def none_object(self) -> Value:
  272. return self.builder.none_object()
  273. def none(self) -> Value:
  274. return self.builder.none()
  275. def true(self) -> Value:
  276. return self.builder.true()
  277. def false(self) -> Value:
  278. return self.builder.false()
  279. def new_list_op(self, values: list[Value], line: int) -> Value:
  280. return self.builder.new_list_op(values, line)
  281. def new_set_op(self, values: list[Value], line: int) -> Value:
  282. return self.builder.new_set_op(values, line)
  283. def translate_is_op(self, lreg: Value, rreg: Value, expr_op: str, line: int) -> Value:
  284. return self.builder.translate_is_op(lreg, rreg, expr_op, line)
  285. def py_call(
  286. self,
  287. function: Value,
  288. arg_values: list[Value],
  289. line: int,
  290. arg_kinds: list[ArgKind] | None = None,
  291. arg_names: Sequence[str | None] | None = None,
  292. ) -> Value:
  293. return self.builder.py_call(function, arg_values, line, arg_kinds, arg_names)
  294. def add_bool_branch(self, value: Value, true: BasicBlock, false: BasicBlock) -> None:
  295. self.builder.add_bool_branch(value, true, false)
  296. def load_native_type_object(self, fullname: str) -> Value:
  297. return self.builder.load_native_type_object(fullname)
  298. def gen_method_call(
  299. self,
  300. base: Value,
  301. name: str,
  302. arg_values: list[Value],
  303. result_type: RType | None,
  304. line: int,
  305. arg_kinds: list[ArgKind] | None = None,
  306. arg_names: list[str | None] | None = None,
  307. ) -> Value:
  308. return self.builder.gen_method_call(
  309. base, name, arg_values, result_type, line, arg_kinds, arg_names, self.can_borrow
  310. )
  311. def load_module(self, name: str) -> Value:
  312. return self.builder.load_module(name)
  313. def call_c(self, desc: CFunctionDescription, args: list[Value], line: int) -> Value:
  314. return self.builder.call_c(desc, args, line)
  315. def int_op(self, type: RType, lhs: Value, rhs: Value, op: int, line: int) -> Value:
  316. return self.builder.int_op(type, lhs, rhs, op, line)
  317. def compare_tagged(self, lhs: Value, rhs: Value, op: str, line: int) -> Value:
  318. return self.builder.compare_tagged(lhs, rhs, op, line)
  319. def compare_tuples(self, lhs: Value, rhs: Value, op: str, line: int) -> Value:
  320. return self.builder.compare_tuples(lhs, rhs, op, line)
  321. def builtin_len(self, val: Value, line: int) -> Value:
  322. return self.builder.builtin_len(val, line)
  323. def new_tuple(self, items: list[Value], line: int) -> Value:
  324. return self.builder.new_tuple(items, line)
  325. # Helpers for IR building
  326. def add_to_non_ext_dict(
  327. self, non_ext: NonExtClassInfo, key: str, val: Value, line: int
  328. ) -> None:
  329. # Add an attribute entry into the class dict of a non-extension class.
  330. key_unicode = self.load_str(key)
  331. self.call_c(dict_set_item_op, [non_ext.dict, key_unicode, val], line)
  332. def gen_import(self, id: str, line: int) -> None:
  333. self.imports[id] = None
  334. needs_import, out = BasicBlock(), BasicBlock()
  335. self.check_if_module_loaded(id, line, needs_import, out)
  336. self.activate_block(needs_import)
  337. value = self.call_c(import_op, [self.load_str(id)], line)
  338. self.add(InitStatic(value, id, namespace=NAMESPACE_MODULE))
  339. self.goto_and_activate(out)
  340. def check_if_module_loaded(
  341. self, id: str, line: int, needs_import: BasicBlock, out: BasicBlock
  342. ) -> None:
  343. """Generate code that checks if the module `id` has been loaded yet.
  344. Arguments:
  345. id: name of module to check if imported
  346. line: line number that the import occurs on
  347. needs_import: the BasicBlock that is run if the module has not been loaded yet
  348. out: the BasicBlock that is run if the module has already been loaded"""
  349. first_load = self.load_module(id)
  350. comparison = self.translate_is_op(first_load, self.none_object(), "is not", line)
  351. self.add_bool_branch(comparison, out, needs_import)
  352. def get_module(self, module: str, line: int) -> Value:
  353. # Python 3.7 has a nice 'PyImport_GetModule' function that we can't use :(
  354. mod_dict = self.call_c(get_module_dict_op, [], line)
  355. # Get module object from modules dict.
  356. return self.call_c(dict_get_item_op, [mod_dict, self.load_str(module)], line)
  357. def get_module_attr(self, module: str, attr: str, line: int) -> Value:
  358. """Look up an attribute of a module without storing it in the local namespace.
  359. For example, get_module_attr('typing', 'TypedDict', line) results in
  360. the value of 'typing.TypedDict'.
  361. Import the module if needed.
  362. """
  363. self.gen_import(module, line)
  364. module_obj = self.get_module(module, line)
  365. return self.py_get_attr(module_obj, attr, line)
  366. def assign_if_null(self, target: Register, get_val: Callable[[], Value], line: int) -> None:
  367. """If target is NULL, assign value produced by get_val to it."""
  368. error_block, body_block = BasicBlock(), BasicBlock()
  369. self.add(Branch(target, error_block, body_block, Branch.IS_ERROR))
  370. self.activate_block(error_block)
  371. self.add(Assign(target, self.coerce(get_val(), target.type, line)))
  372. self.goto(body_block)
  373. self.activate_block(body_block)
  374. def assign_if_bitmap_unset(
  375. self, target: Register, get_val: Callable[[], Value], index: int, line: int
  376. ) -> None:
  377. error_block, body_block = BasicBlock(), BasicBlock()
  378. o = self.int_op(
  379. bitmap_rprimitive,
  380. self.builder.args[-1 - index // BITMAP_BITS],
  381. Integer(1 << (index & (BITMAP_BITS - 1)), bitmap_rprimitive),
  382. IntOp.AND,
  383. line,
  384. )
  385. b = self.add(ComparisonOp(o, Integer(0, bitmap_rprimitive), ComparisonOp.EQ))
  386. self.add(Branch(b, error_block, body_block, Branch.BOOL))
  387. self.activate_block(error_block)
  388. self.add(Assign(target, self.coerce(get_val(), target.type, line)))
  389. self.goto(body_block)
  390. self.activate_block(body_block)
  391. def maybe_add_implicit_return(self) -> None:
  392. if is_none_rprimitive(self.ret_types[-1]) or is_object_rprimitive(self.ret_types[-1]):
  393. self.add_implicit_return()
  394. else:
  395. self.add_implicit_unreachable()
  396. def add_implicit_return(self) -> None:
  397. block = self.builder.blocks[-1]
  398. if not block.terminated:
  399. retval = self.coerce(self.builder.none(), self.ret_types[-1], -1)
  400. self.nonlocal_control[-1].gen_return(self, retval, self.fn_info.fitem.line)
  401. def add_implicit_unreachable(self) -> None:
  402. block = self.builder.blocks[-1]
  403. if not block.terminated:
  404. self.add(Unreachable())
  405. def disallow_class_assignments(self, lvalues: list[Lvalue], line: int) -> None:
  406. # Some best-effort attempts to disallow assigning to class
  407. # variables that aren't marked ClassVar, since we blatantly
  408. # miscompile the interaction between instance and class
  409. # variables.
  410. for lvalue in lvalues:
  411. if (
  412. isinstance(lvalue, MemberExpr)
  413. and isinstance(lvalue.expr, RefExpr)
  414. and isinstance(lvalue.expr.node, TypeInfo)
  415. ):
  416. var = lvalue.expr.node[lvalue.name].node
  417. if isinstance(var, Var) and not var.is_classvar:
  418. self.error("Only class variables defined as ClassVar can be assigned to", line)
  419. def non_function_scope(self) -> bool:
  420. # Currently the stack always has at least two items: dummy and top-level.
  421. return len(self.fn_infos) <= 2
  422. def init_final_static(
  423. self,
  424. lvalue: Lvalue,
  425. rvalue_reg: Value,
  426. class_name: str | None = None,
  427. *,
  428. type_override: RType | None = None,
  429. ) -> None:
  430. assert isinstance(lvalue, NameExpr)
  431. assert isinstance(lvalue.node, Var)
  432. if lvalue.node.final_value is None:
  433. if class_name is None:
  434. name = lvalue.name
  435. else:
  436. name = f"{class_name}.{lvalue.name}"
  437. assert name is not None, "Full name not set for variable"
  438. coerced = self.coerce(rvalue_reg, type_override or self.node_type(lvalue), lvalue.line)
  439. self.final_names.append((name, coerced.type))
  440. self.add(InitStatic(coerced, name, self.module_name))
  441. def load_final_static(
  442. self, fullname: str, typ: RType, line: int, error_name: str | None = None
  443. ) -> Value:
  444. split_name = split_target(self.graph, fullname)
  445. assert split_name is not None
  446. module, name = split_name
  447. return self.builder.load_static_checked(
  448. typ,
  449. name,
  450. module,
  451. line=line,
  452. error_msg=f'value for final name "{error_name}" was not set',
  453. )
  454. def load_literal_value(self, val: int | str | bytes | float | complex | bool) -> Value:
  455. """Load value of a final name, class-level attribute, or constant folded expression."""
  456. if isinstance(val, bool):
  457. if val:
  458. return self.true()
  459. else:
  460. return self.false()
  461. elif isinstance(val, int):
  462. return self.builder.load_int(val)
  463. elif isinstance(val, float):
  464. return self.builder.load_float(val)
  465. elif isinstance(val, str):
  466. return self.builder.load_str(val)
  467. elif isinstance(val, bytes):
  468. return self.builder.load_bytes(val)
  469. elif isinstance(val, complex):
  470. return self.builder.load_complex(val)
  471. else:
  472. assert False, "Unsupported literal value"
  473. def get_assignment_target(
  474. self, lvalue: Lvalue, line: int = -1, *, for_read: bool = False
  475. ) -> AssignmentTarget:
  476. if line == -1:
  477. line = lvalue.line
  478. if isinstance(lvalue, NameExpr):
  479. # If we are visiting a decorator, then the SymbolNode we really want to be looking at
  480. # is the function that is decorated, not the entire Decorator node itself.
  481. symbol = lvalue.node
  482. if isinstance(symbol, Decorator):
  483. symbol = symbol.func
  484. if symbol is None:
  485. # New semantic analyzer doesn't create ad-hoc Vars for special forms.
  486. assert lvalue.is_special_form
  487. symbol = Var(lvalue.name)
  488. if not for_read and isinstance(symbol, Var) and symbol.is_cls:
  489. self.error("Cannot assign to the first argument of classmethod", line)
  490. if lvalue.kind == LDEF:
  491. if symbol not in self.symtables[-1]:
  492. if isinstance(symbol, Var) and not isinstance(symbol.type, DeletedType):
  493. reg_type = self.type_to_rtype(symbol.type)
  494. else:
  495. reg_type = self.node_type(lvalue)
  496. # If the function is a generator function, then first define a new variable
  497. # in the current function's environment class. Next, define a target that
  498. # refers to the newly defined variable in that environment class. Add the
  499. # target to the table containing class environment variables, as well as the
  500. # current environment.
  501. if self.fn_info.is_generator:
  502. return self.add_var_to_env_class(
  503. symbol, reg_type, self.fn_info.generator_class, reassign=False
  504. )
  505. # Otherwise define a new local variable.
  506. return self.add_local_reg(symbol, reg_type)
  507. else:
  508. # Assign to a previously defined variable.
  509. return self.lookup(symbol)
  510. elif lvalue.kind == GDEF:
  511. globals_dict = self.load_globals_dict()
  512. name = self.load_str(lvalue.name)
  513. return AssignmentTargetIndex(globals_dict, name)
  514. else:
  515. assert False, lvalue.kind
  516. elif isinstance(lvalue, IndexExpr):
  517. # Indexed assignment x[y] = e
  518. base = self.accept(lvalue.base)
  519. index = self.accept(lvalue.index)
  520. return AssignmentTargetIndex(base, index)
  521. elif isinstance(lvalue, MemberExpr):
  522. # Attribute assignment x.y = e
  523. can_borrow = self.is_native_attr_ref(lvalue)
  524. obj = self.accept(lvalue.expr, can_borrow=can_borrow)
  525. return AssignmentTargetAttr(obj, lvalue.name, can_borrow=can_borrow)
  526. elif isinstance(lvalue, TupleExpr):
  527. # Multiple assignment a, ..., b = e
  528. star_idx: int | None = None
  529. lvalues = []
  530. for idx, item in enumerate(lvalue.items):
  531. targ = self.get_assignment_target(item)
  532. lvalues.append(targ)
  533. if isinstance(item, StarExpr):
  534. if star_idx is not None:
  535. self.error("Two starred expressions in assignment", line)
  536. star_idx = idx
  537. return AssignmentTargetTuple(lvalues, star_idx)
  538. elif isinstance(lvalue, StarExpr):
  539. return self.get_assignment_target(lvalue.expr)
  540. assert False, "Unsupported lvalue: %r" % lvalue
  541. def read(
  542. self, target: Value | AssignmentTarget, line: int = -1, can_borrow: bool = False
  543. ) -> Value:
  544. if isinstance(target, Value):
  545. return target
  546. if isinstance(target, AssignmentTargetRegister):
  547. return target.register
  548. if isinstance(target, AssignmentTargetIndex):
  549. reg = self.gen_method_call(
  550. target.base, "__getitem__", [target.index], target.type, line
  551. )
  552. if reg is not None:
  553. return reg
  554. assert False, target.base.type
  555. if isinstance(target, AssignmentTargetAttr):
  556. if isinstance(target.obj.type, RInstance) and target.obj.type.class_ir.is_ext_class:
  557. borrow = can_borrow and target.can_borrow
  558. return self.add(GetAttr(target.obj, target.attr, line, borrow=borrow))
  559. else:
  560. return self.py_get_attr(target.obj, target.attr, line)
  561. assert False, "Unsupported lvalue: %r" % target
  562. def assign(self, target: Register | AssignmentTarget, rvalue_reg: Value, line: int) -> None:
  563. if isinstance(target, Register):
  564. self.add(Assign(target, self.coerce_rvalue(rvalue_reg, target.type, line)))
  565. elif isinstance(target, AssignmentTargetRegister):
  566. rvalue_reg = self.coerce_rvalue(rvalue_reg, target.type, line)
  567. self.add(Assign(target.register, rvalue_reg))
  568. elif isinstance(target, AssignmentTargetAttr):
  569. if isinstance(target.obj_type, RInstance):
  570. rvalue_reg = self.coerce_rvalue(rvalue_reg, target.type, line)
  571. self.add(SetAttr(target.obj, target.attr, rvalue_reg, line))
  572. else:
  573. key = self.load_str(target.attr)
  574. boxed_reg = self.builder.box(rvalue_reg)
  575. self.call_c(py_setattr_op, [target.obj, key, boxed_reg], line)
  576. elif isinstance(target, AssignmentTargetIndex):
  577. target_reg2 = self.gen_method_call(
  578. target.base, "__setitem__", [target.index, rvalue_reg], None, line
  579. )
  580. assert target_reg2 is not None, target.base.type
  581. elif isinstance(target, AssignmentTargetTuple):
  582. if isinstance(rvalue_reg.type, RTuple) and target.star_idx is None:
  583. rtypes = rvalue_reg.type.types
  584. assert len(rtypes) == len(target.items)
  585. for i in range(len(rtypes)):
  586. item_value = self.add(TupleGet(rvalue_reg, i, line))
  587. self.assign(target.items[i], item_value, line)
  588. elif (
  589. is_list_rprimitive(rvalue_reg.type) or is_tuple_rprimitive(rvalue_reg.type)
  590. ) and target.star_idx is None:
  591. self.process_sequence_assignment(target, rvalue_reg, line)
  592. else:
  593. self.process_iterator_tuple_assignment(target, rvalue_reg, line)
  594. else:
  595. assert False, "Unsupported assignment target"
  596. def coerce_rvalue(self, rvalue: Value, rtype: RType, line: int) -> Value:
  597. if is_float_rprimitive(rtype) and is_tagged(rvalue.type):
  598. typename = rvalue.type.short_name()
  599. if typename == "short_int":
  600. typename = "int"
  601. self.error(
  602. "Incompatible value representations in assignment "
  603. + f'(expression has type "{typename}", variable has type "float")',
  604. line,
  605. )
  606. return self.coerce(rvalue, rtype, line)
  607. def process_sequence_assignment(
  608. self, target: AssignmentTargetTuple, rvalue: Value, line: int
  609. ) -> None:
  610. """Process assignment like 'x, y = s', where s is a variable-length list or tuple."""
  611. # Check the length of sequence.
  612. expected_len = Integer(len(target.items), c_pyssize_t_rprimitive)
  613. self.builder.call_c(check_unpack_count_op, [rvalue, expected_len], line)
  614. # Read sequence items.
  615. values = []
  616. for i in range(len(target.items)):
  617. item = target.items[i]
  618. index = self.builder.load_int(i)
  619. if is_list_rprimitive(rvalue.type):
  620. item_value = self.call_c(list_get_item_unsafe_op, [rvalue, index], line)
  621. else:
  622. item_value = self.builder.gen_method_call(
  623. rvalue, "__getitem__", [index], item.type, line
  624. )
  625. values.append(item_value)
  626. # Assign sequence items to the target lvalues.
  627. for lvalue, value in zip(target.items, values):
  628. self.assign(lvalue, value, line)
  629. def process_iterator_tuple_assignment_helper(
  630. self, litem: AssignmentTarget, ritem: Value, line: int
  631. ) -> None:
  632. error_block, ok_block = BasicBlock(), BasicBlock()
  633. self.add(Branch(ritem, error_block, ok_block, Branch.IS_ERROR))
  634. self.activate_block(error_block)
  635. self.add(
  636. RaiseStandardError(RaiseStandardError.VALUE_ERROR, "not enough values to unpack", line)
  637. )
  638. self.add(Unreachable())
  639. self.activate_block(ok_block)
  640. self.assign(litem, ritem, line)
  641. def process_iterator_tuple_assignment(
  642. self, target: AssignmentTargetTuple, rvalue_reg: Value, line: int
  643. ) -> None:
  644. iterator = self.call_c(iter_op, [rvalue_reg], line)
  645. # This may be the whole lvalue list if there is no starred value
  646. split_idx = target.star_idx if target.star_idx is not None else len(target.items)
  647. # Assign values before the first starred value
  648. for litem in target.items[:split_idx]:
  649. ritem = self.call_c(next_op, [iterator], line)
  650. error_block, ok_block = BasicBlock(), BasicBlock()
  651. self.add(Branch(ritem, error_block, ok_block, Branch.IS_ERROR))
  652. self.activate_block(error_block)
  653. self.add(
  654. RaiseStandardError(
  655. RaiseStandardError.VALUE_ERROR, "not enough values to unpack", line
  656. )
  657. )
  658. self.add(Unreachable())
  659. self.activate_block(ok_block)
  660. self.assign(litem, ritem, line)
  661. # Assign the starred value and all values after it
  662. if target.star_idx is not None:
  663. post_star_vals = target.items[split_idx + 1 :]
  664. iter_list = self.call_c(to_list, [iterator], line)
  665. iter_list_len = self.builtin_len(iter_list, line)
  666. post_star_len = Integer(len(post_star_vals))
  667. condition = self.binary_op(post_star_len, iter_list_len, "<=", line)
  668. error_block, ok_block = BasicBlock(), BasicBlock()
  669. self.add(Branch(condition, ok_block, error_block, Branch.BOOL))
  670. self.activate_block(error_block)
  671. self.add(
  672. RaiseStandardError(
  673. RaiseStandardError.VALUE_ERROR, "not enough values to unpack", line
  674. )
  675. )
  676. self.add(Unreachable())
  677. self.activate_block(ok_block)
  678. for litem in reversed(post_star_vals):
  679. ritem = self.call_c(list_pop_last, [iter_list], line)
  680. self.assign(litem, ritem, line)
  681. # Assign the starred value
  682. self.assign(target.items[target.star_idx], iter_list, line)
  683. # There is no starred value, so check if there are extra values in rhs that
  684. # have not been assigned.
  685. else:
  686. extra = self.call_c(next_op, [iterator], line)
  687. error_block, ok_block = BasicBlock(), BasicBlock()
  688. self.add(Branch(extra, ok_block, error_block, Branch.IS_ERROR))
  689. self.activate_block(error_block)
  690. self.add(
  691. RaiseStandardError(
  692. RaiseStandardError.VALUE_ERROR, "too many values to unpack", line
  693. )
  694. )
  695. self.add(Unreachable())
  696. self.activate_block(ok_block)
  697. def push_loop_stack(self, continue_block: BasicBlock, break_block: BasicBlock) -> None:
  698. self.nonlocal_control.append(
  699. LoopNonlocalControl(self.nonlocal_control[-1], continue_block, break_block)
  700. )
  701. def pop_loop_stack(self) -> None:
  702. self.nonlocal_control.pop()
  703. def make_spill_target(self, type: RType) -> AssignmentTarget:
  704. """Moves a given Value instance into the generator class' environment class."""
  705. name = f"{TEMP_ATTR_NAME}{self.temp_counter}"
  706. self.temp_counter += 1
  707. target = self.add_var_to_env_class(Var(name), type, self.fn_info.generator_class)
  708. return target
  709. def spill(self, value: Value) -> AssignmentTarget:
  710. """Moves a given Value instance into the generator class' environment class."""
  711. target = self.make_spill_target(value.type)
  712. # Shouldn't be able to fail, so -1 for line
  713. self.assign(target, value, -1)
  714. return target
  715. def maybe_spill(self, value: Value) -> Value | AssignmentTarget:
  716. """
  717. Moves a given Value instance into the environment class for generator functions. For
  718. non-generator functions, leaves the Value instance as it is.
  719. Returns an AssignmentTarget associated with the Value for generator functions and the
  720. original Value itself for non-generator functions.
  721. """
  722. if self.fn_info.is_generator:
  723. return self.spill(value)
  724. return value
  725. def maybe_spill_assignable(self, value: Value) -> Register | AssignmentTarget:
  726. """
  727. Moves a given Value instance into the environment class for generator functions. For
  728. non-generator functions, allocate a temporary Register.
  729. Returns an AssignmentTarget associated with the Value for generator functions and an
  730. assignable Register for non-generator functions.
  731. """
  732. if self.fn_info.is_generator:
  733. return self.spill(value)
  734. if isinstance(value, Register):
  735. return value
  736. # Allocate a temporary register for the assignable value.
  737. reg = Register(value.type)
  738. self.assign(reg, value, -1)
  739. return reg
  740. def extract_int(self, e: Expression) -> int | None:
  741. if isinstance(e, IntExpr):
  742. return e.value
  743. elif isinstance(e, UnaryExpr) and e.op == "-" and isinstance(e.expr, IntExpr):
  744. return -e.expr.value
  745. else:
  746. return None
  747. def get_sequence_type(self, expr: Expression) -> RType:
  748. return self.get_sequence_type_from_type(self.types[expr])
  749. def get_sequence_type_from_type(self, target_type: Type) -> RType:
  750. target_type = get_proper_type(target_type)
  751. if isinstance(target_type, UnionType):
  752. return RUnion.make_simplified_union(
  753. [self.get_sequence_type_from_type(item) for item in target_type.items]
  754. )
  755. assert isinstance(target_type, Instance), target_type
  756. if target_type.type.fullname == "builtins.str":
  757. return str_rprimitive
  758. else:
  759. return self.type_to_rtype(target_type.args[0])
  760. def get_dict_base_type(self, expr: Expression) -> list[Instance]:
  761. """Find dict type of a dict-like expression.
  762. This is useful for dict subclasses like SymbolTable.
  763. """
  764. target_type = get_proper_type(self.types[expr])
  765. if isinstance(target_type, UnionType):
  766. types = [get_proper_type(item) for item in target_type.items]
  767. else:
  768. types = [target_type]
  769. dict_types = []
  770. for t in types:
  771. if isinstance(t, TypedDictType):
  772. t = t.fallback
  773. dict_base = next(base for base in t.type.mro if base.fullname == "typing.Mapping")
  774. else:
  775. assert isinstance(t, Instance), t
  776. dict_base = next(base for base in t.type.mro if base.fullname == "builtins.dict")
  777. dict_types.append(map_instance_to_supertype(t, dict_base))
  778. return dict_types
  779. def get_dict_key_type(self, expr: Expression) -> RType:
  780. dict_base_types = self.get_dict_base_type(expr)
  781. if len(dict_base_types) == 1:
  782. return self.type_to_rtype(dict_base_types[0].args[0])
  783. else:
  784. rtypes = [self.type_to_rtype(t.args[0]) for t in dict_base_types]
  785. return RUnion.make_simplified_union(rtypes)
  786. def get_dict_value_type(self, expr: Expression) -> RType:
  787. dict_base_types = self.get_dict_base_type(expr)
  788. if len(dict_base_types) == 1:
  789. return self.type_to_rtype(dict_base_types[0].args[1])
  790. else:
  791. rtypes = [self.type_to_rtype(t.args[1]) for t in dict_base_types]
  792. return RUnion.make_simplified_union(rtypes)
  793. def get_dict_item_type(self, expr: Expression) -> RType:
  794. key_type = self.get_dict_key_type(expr)
  795. value_type = self.get_dict_value_type(expr)
  796. return RTuple([key_type, value_type])
  797. def _analyze_iterable_item_type(self, expr: Expression) -> Type:
  798. """Return the item type given by 'expr' in an iterable context."""
  799. # This logic is copied from mypy's TypeChecker.analyze_iterable_item_type.
  800. if expr not in self.types:
  801. # Mypy thinks this is unreachable.
  802. iterable: ProperType = AnyType(TypeOfAny.from_error)
  803. else:
  804. iterable = get_proper_type(self.types[expr])
  805. echk = self.graph[self.module_name].type_checker().expr_checker
  806. iterator = echk.check_method_call_by_name("__iter__", iterable, [], [], expr)[0]
  807. from mypy.join import join_types
  808. if isinstance(iterable, TupleType):
  809. joined: Type = UninhabitedType()
  810. for item in iterable.items:
  811. joined = join_types(joined, item)
  812. return joined
  813. else:
  814. # Non-tuple iterable.
  815. return echk.check_method_call_by_name("__next__", iterator, [], [], expr)[0]
  816. def is_native_module(self, module: str) -> bool:
  817. """Is the given module one compiled by mypyc?"""
  818. return module in self.mapper.group_map
  819. def is_native_ref_expr(self, expr: RefExpr) -> bool:
  820. if expr.node is None:
  821. return False
  822. if "." in expr.node.fullname:
  823. return self.is_native_module(expr.node.fullname.rpartition(".")[0])
  824. return True
  825. def is_native_module_ref_expr(self, expr: RefExpr) -> bool:
  826. return self.is_native_ref_expr(expr) and expr.kind == GDEF
  827. def is_synthetic_type(self, typ: TypeInfo) -> bool:
  828. """Is a type something other than just a class we've created?"""
  829. return typ.is_named_tuple or typ.is_newtype or typ.typeddict_type is not None
  830. def get_final_ref(self, expr: MemberExpr) -> tuple[str, Var, bool] | None:
  831. """Check if `expr` is a final attribute.
  832. This needs to be done differently for class and module attributes to
  833. correctly determine fully qualified name. Return a tuple that consists of
  834. the qualified name, the corresponding Var node, and a flag indicating whether
  835. the final name was defined in a compiled module. Return None if `expr` does not
  836. refer to a final attribute.
  837. """
  838. final_var = None
  839. if isinstance(expr.expr, RefExpr) and isinstance(expr.expr.node, TypeInfo):
  840. # a class attribute
  841. sym = expr.expr.node.get(expr.name)
  842. if sym and isinstance(sym.node, Var):
  843. # Enum attribute are treated as final since they are added to the global cache
  844. expr_fullname = expr.expr.node.bases[0].type.fullname
  845. is_final = sym.node.is_final or expr_fullname == "enum.Enum"
  846. if is_final:
  847. final_var = sym.node
  848. fullname = f"{sym.node.info.fullname}.{final_var.name}"
  849. native = self.is_native_module(expr.expr.node.module_name)
  850. elif self.is_module_member_expr(expr):
  851. # a module attribute
  852. if isinstance(expr.node, Var) and expr.node.is_final:
  853. final_var = expr.node
  854. fullname = expr.node.fullname
  855. native = self.is_native_ref_expr(expr)
  856. if final_var is not None:
  857. return fullname, final_var, native
  858. return None
  859. def emit_load_final(
  860. self, final_var: Var, fullname: str, name: str, native: bool, typ: Type, line: int
  861. ) -> Value | None:
  862. """Emit code for loading value of a final name (if possible).
  863. Args:
  864. final_var: Var corresponding to the final name
  865. fullname: its qualified name
  866. name: shorter name to show in errors
  867. native: whether the name was defined in a compiled module
  868. typ: its type
  869. line: line number where loading occurs
  870. """
  871. if final_var.final_value is not None: # this is safe even for non-native names
  872. return self.load_literal_value(final_var.final_value)
  873. elif native:
  874. return self.load_final_static(fullname, self.mapper.type_to_rtype(typ), line, name)
  875. else:
  876. return None
  877. def is_module_member_expr(self, expr: MemberExpr) -> bool:
  878. return isinstance(expr.expr, RefExpr) and isinstance(expr.expr.node, MypyFile)
  879. def call_refexpr_with_args(
  880. self, expr: CallExpr, callee: RefExpr, arg_values: list[Value]
  881. ) -> Value:
  882. # Handle data-driven special-cased primitive call ops.
  883. if callee.fullname and expr.arg_kinds == [ARG_POS] * len(arg_values):
  884. fullname = get_call_target_fullname(callee)
  885. call_c_ops_candidates = function_ops.get(fullname, [])
  886. target = self.builder.matching_call_c(
  887. call_c_ops_candidates, arg_values, expr.line, self.node_type(expr)
  888. )
  889. if target:
  890. return target
  891. # Standard native call if signature and fullname are good and all arguments are positional
  892. # or named.
  893. callee_node = callee.node
  894. if isinstance(callee_node, OverloadedFuncDef):
  895. callee_node = callee_node.impl
  896. # TODO: use native calls for any decorated functions which have all their decorators
  897. # removed, not just singledispatch functions (which we don't do now just in case those
  898. # decorated functions are callable classes or cannot be called without the python API for
  899. # some other reason)
  900. if (
  901. isinstance(callee_node, Decorator)
  902. and callee_node.func not in self.fdefs_to_decorators
  903. and callee_node.func in self.singledispatch_impls
  904. ):
  905. callee_node = callee_node.func
  906. if (
  907. callee_node is not None
  908. and callee.fullname
  909. and callee_node in self.mapper.func_to_decl
  910. and all(kind in (ARG_POS, ARG_NAMED) for kind in expr.arg_kinds)
  911. ):
  912. decl = self.mapper.func_to_decl[callee_node]
  913. return self.builder.call(decl, arg_values, expr.arg_kinds, expr.arg_names, expr.line)
  914. # Fall back to a Python call
  915. function = self.accept(callee)
  916. return self.py_call(
  917. function, arg_values, expr.line, arg_kinds=expr.arg_kinds, arg_names=expr.arg_names
  918. )
  919. def shortcircuit_expr(self, expr: OpExpr) -> Value:
  920. return self.builder.shortcircuit_helper(
  921. expr.op,
  922. self.node_type(expr),
  923. lambda: self.accept(expr.left),
  924. lambda: self.accept(expr.right),
  925. expr.line,
  926. )
  927. # Basic helpers
  928. def flatten_classes(self, arg: RefExpr | TupleExpr) -> list[ClassIR] | None:
  929. """Flatten classes in isinstance(obj, (A, (B, C))).
  930. If at least one item is not a reference to a native class, return None.
  931. """
  932. if isinstance(arg, RefExpr):
  933. if isinstance(arg.node, TypeInfo) and self.is_native_module_ref_expr(arg):
  934. ir = self.mapper.type_to_ir.get(arg.node)
  935. if ir:
  936. return [ir]
  937. return None
  938. else:
  939. res: list[ClassIR] = []
  940. for item in arg.items:
  941. if isinstance(item, (RefExpr, TupleExpr)):
  942. item_part = self.flatten_classes(item)
  943. if item_part is None:
  944. return None
  945. res.extend(item_part)
  946. else:
  947. return None
  948. return res
  949. def enter(self, fn_info: FuncInfo | str = "") -> None:
  950. if isinstance(fn_info, str):
  951. fn_info = FuncInfo(name=fn_info)
  952. self.builder = LowLevelIRBuilder(
  953. self.current_module, self.errors, self.mapper, self.options
  954. )
  955. self.builder.set_module(self.module_name, self.module_path)
  956. self.builders.append(self.builder)
  957. self.symtables.append({})
  958. self.runtime_args.append([])
  959. self.fn_info = fn_info
  960. self.fn_infos.append(self.fn_info)
  961. self.ret_types.append(none_rprimitive)
  962. if fn_info.is_generator:
  963. self.nonlocal_control.append(GeneratorNonlocalControl())
  964. else:
  965. self.nonlocal_control.append(BaseNonlocalControl())
  966. self.activate_block(BasicBlock())
  967. def leave(self) -> tuple[list[Register], list[RuntimeArg], list[BasicBlock], RType, FuncInfo]:
  968. builder = self.builders.pop()
  969. self.symtables.pop()
  970. runtime_args = self.runtime_args.pop()
  971. ret_type = self.ret_types.pop()
  972. fn_info = self.fn_infos.pop()
  973. self.nonlocal_control.pop()
  974. self.builder = self.builders[-1]
  975. self.fn_info = self.fn_infos[-1]
  976. return builder.args, runtime_args, builder.blocks, ret_type, fn_info
  977. @contextmanager
  978. def enter_method(
  979. self,
  980. class_ir: ClassIR,
  981. name: str,
  982. ret_type: RType,
  983. fn_info: FuncInfo | str = "",
  984. self_type: RType | None = None,
  985. ) -> Iterator[None]:
  986. """Generate IR for a method.
  987. If the method takes arguments, you should immediately afterwards call
  988. add_argument() for each non-self argument (self is created implicitly).
  989. Args:
  990. class_ir: Add method to this class
  991. name: Short name of the method
  992. ret_type: Return type of the method
  993. fn_info: Optionally, additional information about the method
  994. self_type: If not None, override default type of the implicit 'self'
  995. argument (by default, derive type from class_ir)
  996. """
  997. self.enter(fn_info)
  998. self.function_name_stack.append(name)
  999. self.class_ir_stack.append(class_ir)
  1000. self.ret_types[-1] = ret_type
  1001. if self_type is None:
  1002. self_type = RInstance(class_ir)
  1003. self.add_argument(SELF_NAME, self_type)
  1004. try:
  1005. yield
  1006. finally:
  1007. arg_regs, args, blocks, ret_type, fn_info = self.leave()
  1008. sig = FuncSignature(args, ret_type)
  1009. name = self.function_name_stack.pop()
  1010. class_ir = self.class_ir_stack.pop()
  1011. decl = FuncDecl(name, class_ir.name, self.module_name, sig)
  1012. ir = FuncIR(decl, arg_regs, blocks)
  1013. class_ir.methods[name] = ir
  1014. class_ir.method_decls[name] = ir.decl
  1015. self.functions.append(ir)
  1016. def add_argument(self, var: str | Var, typ: RType, kind: ArgKind = ARG_POS) -> Register:
  1017. """Declare an argument in the current function.
  1018. You should use this instead of directly calling add_local() in new code.
  1019. """
  1020. if isinstance(var, str):
  1021. var = Var(var)
  1022. reg = self.add_local(var, typ, is_arg=True)
  1023. self.runtime_args[-1].append(RuntimeArg(var.name, typ, kind))
  1024. return reg
  1025. def lookup(self, symbol: SymbolNode) -> SymbolTarget:
  1026. return self.symtables[-1][symbol]
  1027. def add_local(self, symbol: SymbolNode, typ: RType, is_arg: bool = False) -> Register:
  1028. """Add register that represents a symbol to the symbol table.
  1029. Args:
  1030. is_arg: is this a function argument
  1031. """
  1032. assert isinstance(symbol, SymbolNode)
  1033. reg = Register(
  1034. typ, remangle_redefinition_name(symbol.name), is_arg=is_arg, line=symbol.line
  1035. )
  1036. self.symtables[-1][symbol] = AssignmentTargetRegister(reg)
  1037. if is_arg:
  1038. self.builder.args.append(reg)
  1039. return reg
  1040. def add_local_reg(
  1041. self, symbol: SymbolNode, typ: RType, is_arg: bool = False
  1042. ) -> AssignmentTargetRegister:
  1043. """Like add_local, but return an assignment target instead of value."""
  1044. self.add_local(symbol, typ, is_arg)
  1045. target = self.symtables[-1][symbol]
  1046. assert isinstance(target, AssignmentTargetRegister)
  1047. return target
  1048. def add_self_to_env(self, cls: ClassIR) -> AssignmentTargetRegister:
  1049. """Low-level function that adds a 'self' argument.
  1050. This is only useful if using enter() instead of enter_method().
  1051. """
  1052. return self.add_local_reg(Var(SELF_NAME), RInstance(cls), is_arg=True)
  1053. def add_target(self, symbol: SymbolNode, target: SymbolTarget) -> SymbolTarget:
  1054. self.symtables[-1][symbol] = target
  1055. return target
  1056. def type_to_rtype(self, typ: Type | None) -> RType:
  1057. return self.mapper.type_to_rtype(typ)
  1058. def node_type(self, node: Expression) -> RType:
  1059. if isinstance(node, IntExpr):
  1060. # TODO: Don't special case IntExpr
  1061. return int_rprimitive
  1062. if node not in self.types:
  1063. return object_rprimitive
  1064. mypy_type = self.types[node]
  1065. return self.type_to_rtype(mypy_type)
  1066. def add_var_to_env_class(
  1067. self, var: SymbolNode, rtype: RType, base: FuncInfo | ImplicitClass, reassign: bool = False
  1068. ) -> AssignmentTarget:
  1069. # First, define the variable name as an attribute of the environment class, and then
  1070. # construct a target for that attribute.
  1071. self.fn_info.env_class.attributes[var.name] = rtype
  1072. attr_target = AssignmentTargetAttr(base.curr_env_reg, var.name)
  1073. if reassign:
  1074. # Read the local definition of the variable, and set the corresponding attribute of
  1075. # the environment class' variable to be that value.
  1076. reg = self.read(self.lookup(var), self.fn_info.fitem.line)
  1077. self.add(SetAttr(base.curr_env_reg, var.name, reg, self.fn_info.fitem.line))
  1078. # Override the local definition of the variable to instead point at the variable in
  1079. # the environment class.
  1080. return self.add_target(var, attr_target)
  1081. def is_builtin_ref_expr(self, expr: RefExpr) -> bool:
  1082. assert expr.node, "RefExpr not resolved"
  1083. return "." in expr.node.fullname and expr.node.fullname.split(".")[0] == "builtins"
  1084. def load_global(self, expr: NameExpr) -> Value:
  1085. """Loads a Python-level global.
  1086. This takes a NameExpr and uses its name as a key to retrieve the corresponding PyObject *
  1087. from the _globals dictionary in the C-generated code.
  1088. """
  1089. # If the global is from 'builtins', turn it into a module attr load instead
  1090. if self.is_builtin_ref_expr(expr):
  1091. assert expr.node, "RefExpr not resolved"
  1092. return self.load_module_attr_by_fullname(expr.node.fullname, expr.line)
  1093. if (
  1094. self.is_native_module_ref_expr(expr)
  1095. and isinstance(expr.node, TypeInfo)
  1096. and not self.is_synthetic_type(expr.node)
  1097. ):
  1098. assert expr.fullname
  1099. return self.load_native_type_object(expr.fullname)
  1100. return self.load_global_str(expr.name, expr.line)
  1101. def load_global_str(self, name: str, line: int) -> Value:
  1102. _globals = self.load_globals_dict()
  1103. reg = self.load_str(name)
  1104. return self.call_c(dict_get_item_op, [_globals, reg], line)
  1105. def load_globals_dict(self) -> Value:
  1106. return self.add(LoadStatic(dict_rprimitive, "globals", self.module_name))
  1107. def load_module_attr_by_fullname(self, fullname: str, line: int) -> Value:
  1108. module, _, name = fullname.rpartition(".")
  1109. left = self.load_module(module)
  1110. return self.py_get_attr(left, name, line)
  1111. def is_native_attr_ref(self, expr: MemberExpr) -> bool:
  1112. """Is expr a direct reference to a native (struct) attribute of an instance?"""
  1113. obj_rtype = self.node_type(expr.expr)
  1114. return (
  1115. isinstance(obj_rtype, RInstance)
  1116. and obj_rtype.class_ir.is_ext_class
  1117. and obj_rtype.class_ir.has_attr(expr.name)
  1118. and not obj_rtype.class_ir.get_method(expr.name)
  1119. )
  1120. # Lacks a good type because there wasn't a reasonable type in 3.5 :(
  1121. def catch_errors(self, line: int) -> Any:
  1122. return catch_errors(self.module_path, line)
  1123. def warning(self, msg: str, line: int) -> None:
  1124. self.errors.warning(msg, self.module_path, line)
  1125. def error(self, msg: str, line: int) -> None:
  1126. self.errors.error(msg, self.module_path, line)
  1127. def note(self, msg: str, line: int) -> None:
  1128. self.errors.note(msg, self.module_path, line)
  1129. def gen_arg_defaults(builder: IRBuilder) -> None:
  1130. """Generate blocks for arguments that have default values.
  1131. If the passed value is an error value, then assign the default
  1132. value to the argument.
  1133. """
  1134. fitem = builder.fn_info.fitem
  1135. nb = 0
  1136. for arg in fitem.arguments:
  1137. if arg.initializer:
  1138. target = builder.lookup(arg.variable)
  1139. def get_default() -> Value:
  1140. assert arg.initializer is not None
  1141. # If it is constant, don't bother storing it
  1142. if is_constant(arg.initializer):
  1143. return builder.accept(arg.initializer)
  1144. # Because gen_arg_defaults runs before calculate_arg_defaults, we
  1145. # add the static/attribute to final_names/the class here.
  1146. elif not builder.fn_info.is_nested:
  1147. name = fitem.fullname + "." + arg.variable.name
  1148. builder.final_names.append((name, target.type))
  1149. return builder.add(LoadStatic(target.type, name, builder.module_name))
  1150. else:
  1151. name = arg.variable.name
  1152. builder.fn_info.callable_class.ir.attributes[name] = target.type
  1153. return builder.add(
  1154. GetAttr(builder.fn_info.callable_class.self_reg, name, arg.line)
  1155. )
  1156. assert isinstance(target, AssignmentTargetRegister)
  1157. reg = target.register
  1158. if not reg.type.error_overlap:
  1159. builder.assign_if_null(target.register, get_default, arg.initializer.line)
  1160. else:
  1161. builder.assign_if_bitmap_unset(
  1162. target.register, get_default, nb, arg.initializer.line
  1163. )
  1164. nb += 1
  1165. def remangle_redefinition_name(name: str) -> str:
  1166. """Remangle names produced by mypy when allow-redefinition is used and a name
  1167. is used with multiple types within a single block.
  1168. We only need to do this for locals, because the name is used as the name of the register;
  1169. for globals, the name itself is stored in a register for the purpose of doing dict
  1170. lookups.
  1171. """
  1172. return name.replace("'", "__redef__")
  1173. def get_call_target_fullname(ref: RefExpr) -> str:
  1174. if isinstance(ref.node, TypeAlias):
  1175. # Resolve simple type aliases. In calls they evaluate to the type they point to.
  1176. target = get_proper_type(ref.node.target)
  1177. if isinstance(target, Instance):
  1178. return target.type.fullname
  1179. return ref.fullname