specialize.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. """Special case IR generation of calls to specific builtin functions.
  2. Most special cases should be handled using the data driven "primitive
  3. ops" system, but certain operations require special handling that has
  4. access to the AST/IR directly and can make decisions/optimizations
  5. based on it. These special cases can be implemented here.
  6. For example, we use specializers to statically emit the length of a
  7. fixed length tuple and to emit optimized code for any()/all() calls with
  8. generator comprehensions as the argument.
  9. See comment below for more documentation.
  10. """
  11. from __future__ import annotations
  12. from typing import Callable, Optional
  13. from mypy.nodes import (
  14. ARG_NAMED,
  15. ARG_POS,
  16. CallExpr,
  17. DictExpr,
  18. Expression,
  19. GeneratorExpr,
  20. IntExpr,
  21. ListExpr,
  22. MemberExpr,
  23. NameExpr,
  24. RefExpr,
  25. StrExpr,
  26. TupleExpr,
  27. )
  28. from mypy.types import AnyType, TypeOfAny
  29. from mypyc.ir.ops import (
  30. BasicBlock,
  31. Extend,
  32. Integer,
  33. RaiseStandardError,
  34. Register,
  35. Truncate,
  36. Unreachable,
  37. Value,
  38. )
  39. from mypyc.ir.rtypes import (
  40. RInstance,
  41. RPrimitive,
  42. RTuple,
  43. RType,
  44. bool_rprimitive,
  45. c_int_rprimitive,
  46. dict_rprimitive,
  47. int16_rprimitive,
  48. int32_rprimitive,
  49. int64_rprimitive,
  50. int_rprimitive,
  51. is_bool_rprimitive,
  52. is_dict_rprimitive,
  53. is_fixed_width_rtype,
  54. is_float_rprimitive,
  55. is_int16_rprimitive,
  56. is_int32_rprimitive,
  57. is_int64_rprimitive,
  58. is_int_rprimitive,
  59. is_list_rprimitive,
  60. is_uint8_rprimitive,
  61. list_rprimitive,
  62. set_rprimitive,
  63. str_rprimitive,
  64. uint8_rprimitive,
  65. )
  66. from mypyc.irbuild.builder import IRBuilder
  67. from mypyc.irbuild.for_helpers import (
  68. comprehension_helper,
  69. sequence_from_generator_preallocate_helper,
  70. translate_list_comprehension,
  71. translate_set_comprehension,
  72. )
  73. from mypyc.irbuild.format_str_tokenizer import (
  74. FormatOp,
  75. convert_format_expr_to_str,
  76. join_formatted_strings,
  77. tokenizer_format_call,
  78. )
  79. from mypyc.primitives.dict_ops import (
  80. dict_items_op,
  81. dict_keys_op,
  82. dict_setdefault_spec_init_op,
  83. dict_values_op,
  84. )
  85. from mypyc.primitives.list_ops import new_list_set_item_op
  86. from mypyc.primitives.tuple_ops import new_tuple_set_item_op
  87. # Specializers are attempted before compiling the arguments to the
  88. # function. Specializers can return None to indicate that they failed
  89. # and the call should be compiled normally. Otherwise they should emit
  90. # code for the call and return a Value containing the result.
  91. #
  92. # Specializers take three arguments: the IRBuilder, the CallExpr being
  93. # compiled, and the RefExpr that is the left hand side of the call.
  94. Specializer = Callable[["IRBuilder", CallExpr, RefExpr], Optional[Value]]
  95. # Dictionary containing all configured specializers.
  96. #
  97. # Specializers can operate on methods as well, and are keyed on the
  98. # name and RType in that case.
  99. specializers: dict[tuple[str, RType | None], list[Specializer]] = {}
  100. def _apply_specialization(
  101. builder: IRBuilder, expr: CallExpr, callee: RefExpr, name: str | None, typ: RType | None = None
  102. ) -> Value | None:
  103. # TODO: Allow special cases to have default args or named args. Currently they don't since
  104. # they check that everything in arg_kinds is ARG_POS.
  105. # If there is a specializer for this function, try calling it.
  106. # Return the first successful one.
  107. if name and (name, typ) in specializers:
  108. for specializer in specializers[name, typ]:
  109. val = specializer(builder, expr, callee)
  110. if val is not None:
  111. return val
  112. return None
  113. def apply_function_specialization(
  114. builder: IRBuilder, expr: CallExpr, callee: RefExpr
  115. ) -> Value | None:
  116. """Invoke the Specializer callback for a function if one has been registered"""
  117. return _apply_specialization(builder, expr, callee, callee.fullname)
  118. def apply_method_specialization(
  119. builder: IRBuilder, expr: CallExpr, callee: MemberExpr, typ: RType | None = None
  120. ) -> Value | None:
  121. """Invoke the Specializer callback for a method if one has been registered"""
  122. name = callee.fullname if typ is None else callee.name
  123. return _apply_specialization(builder, expr, callee, name, typ)
  124. def specialize_function(
  125. name: str, typ: RType | None = None
  126. ) -> Callable[[Specializer], Specializer]:
  127. """Decorator to register a function as being a specializer.
  128. There may exist multiple specializers for one function. When
  129. translating method calls, the earlier appended specializer has
  130. higher priority.
  131. """
  132. def wrapper(f: Specializer) -> Specializer:
  133. specializers.setdefault((name, typ), []).append(f)
  134. return f
  135. return wrapper
  136. @specialize_function("builtins.globals")
  137. def translate_globals(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
  138. if len(expr.args) == 0:
  139. return builder.load_globals_dict()
  140. return None
  141. @specialize_function("builtins.abs")
  142. @specialize_function("builtins.int")
  143. @specialize_function("builtins.float")
  144. @specialize_function("builtins.complex")
  145. @specialize_function("mypy_extensions.i64")
  146. @specialize_function("mypy_extensions.i32")
  147. @specialize_function("mypy_extensions.i16")
  148. @specialize_function("mypy_extensions.u8")
  149. def translate_builtins_with_unary_dunder(
  150. builder: IRBuilder, expr: CallExpr, callee: RefExpr
  151. ) -> Value | None:
  152. """Specialize calls on native classes that implement the associated dunder.
  153. E.g. i64(x) gets specialized to x.__int__() if x is a native instance.
  154. """
  155. if len(expr.args) == 1 and expr.arg_kinds == [ARG_POS] and isinstance(callee, NameExpr):
  156. arg = expr.args[0]
  157. arg_typ = builder.node_type(arg)
  158. shortname = callee.fullname.split(".")[1]
  159. if shortname in ("i64", "i32", "i16", "u8"):
  160. method = "__int__"
  161. else:
  162. method = f"__{shortname}__"
  163. if isinstance(arg_typ, RInstance) and arg_typ.class_ir.has_method(method):
  164. obj = builder.accept(arg)
  165. return builder.gen_method_call(obj, method, [], None, expr.line)
  166. return None
  167. @specialize_function("builtins.len")
  168. def translate_len(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
  169. if len(expr.args) == 1 and expr.arg_kinds == [ARG_POS]:
  170. arg = expr.args[0]
  171. expr_rtype = builder.node_type(arg)
  172. if isinstance(expr_rtype, RTuple):
  173. # len() of fixed-length tuple can be trivially determined
  174. # statically, though we still need to evaluate it.
  175. builder.accept(arg)
  176. return Integer(len(expr_rtype.types))
  177. else:
  178. if is_list_rprimitive(builder.node_type(arg)):
  179. borrow = True
  180. else:
  181. borrow = False
  182. obj = builder.accept(arg, can_borrow=borrow)
  183. return builder.builtin_len(obj, expr.line)
  184. return None
  185. @specialize_function("builtins.list")
  186. def dict_methods_fast_path(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
  187. """Specialize a common case when list() is called on a dictionary
  188. view method call.
  189. For example:
  190. foo = list(bar.keys())
  191. """
  192. if not (len(expr.args) == 1 and expr.arg_kinds == [ARG_POS]):
  193. return None
  194. arg = expr.args[0]
  195. if not (isinstance(arg, CallExpr) and not arg.args and isinstance(arg.callee, MemberExpr)):
  196. return None
  197. base = arg.callee.expr
  198. attr = arg.callee.name
  199. rtype = builder.node_type(base)
  200. if not (is_dict_rprimitive(rtype) and attr in ("keys", "values", "items")):
  201. return None
  202. obj = builder.accept(base)
  203. # Note that it is not safe to use fast methods on dict subclasses,
  204. # so the corresponding helpers in CPy.h fallback to (inlined)
  205. # generic logic.
  206. if attr == "keys":
  207. return builder.call_c(dict_keys_op, [obj], expr.line)
  208. elif attr == "values":
  209. return builder.call_c(dict_values_op, [obj], expr.line)
  210. else:
  211. return builder.call_c(dict_items_op, [obj], expr.line)
  212. @specialize_function("builtins.list")
  213. def translate_list_from_generator_call(
  214. builder: IRBuilder, expr: CallExpr, callee: RefExpr
  215. ) -> Value | None:
  216. """Special case for simplest list comprehension.
  217. For example:
  218. list(f(x) for x in some_list/some_tuple/some_str)
  219. 'translate_list_comprehension()' would take care of other cases
  220. if this fails.
  221. """
  222. if (
  223. len(expr.args) == 1
  224. and expr.arg_kinds[0] == ARG_POS
  225. and isinstance(expr.args[0], GeneratorExpr)
  226. ):
  227. return sequence_from_generator_preallocate_helper(
  228. builder,
  229. expr.args[0],
  230. empty_op_llbuilder=builder.builder.new_list_op_with_length,
  231. set_item_op=new_list_set_item_op,
  232. )
  233. return None
  234. @specialize_function("builtins.tuple")
  235. def translate_tuple_from_generator_call(
  236. builder: IRBuilder, expr: CallExpr, callee: RefExpr
  237. ) -> Value | None:
  238. """Special case for simplest tuple creation from a generator.
  239. For example:
  240. tuple(f(x) for x in some_list/some_tuple/some_str)
  241. 'translate_safe_generator_call()' would take care of other cases
  242. if this fails.
  243. """
  244. if (
  245. len(expr.args) == 1
  246. and expr.arg_kinds[0] == ARG_POS
  247. and isinstance(expr.args[0], GeneratorExpr)
  248. ):
  249. return sequence_from_generator_preallocate_helper(
  250. builder,
  251. expr.args[0],
  252. empty_op_llbuilder=builder.builder.new_tuple_with_length,
  253. set_item_op=new_tuple_set_item_op,
  254. )
  255. return None
  256. @specialize_function("builtins.set")
  257. def translate_set_from_generator_call(
  258. builder: IRBuilder, expr: CallExpr, callee: RefExpr
  259. ) -> Value | None:
  260. """Special case for set creation from a generator.
  261. For example:
  262. set(f(...) for ... in iterator/nested_generators...)
  263. """
  264. if (
  265. len(expr.args) == 1
  266. and expr.arg_kinds[0] == ARG_POS
  267. and isinstance(expr.args[0], GeneratorExpr)
  268. ):
  269. return translate_set_comprehension(builder, expr.args[0])
  270. return None
  271. @specialize_function("builtins.min")
  272. @specialize_function("builtins.max")
  273. def faster_min_max(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
  274. if expr.arg_kinds == [ARG_POS, ARG_POS]:
  275. x, y = builder.accept(expr.args[0]), builder.accept(expr.args[1])
  276. result = Register(builder.node_type(expr))
  277. # CPython evaluates arguments reversely when calling min(...) or max(...)
  278. if callee.fullname == "builtins.min":
  279. comparison = builder.binary_op(y, x, "<", expr.line)
  280. else:
  281. comparison = builder.binary_op(y, x, ">", expr.line)
  282. true_block, false_block, next_block = BasicBlock(), BasicBlock(), BasicBlock()
  283. builder.add_bool_branch(comparison, true_block, false_block)
  284. builder.activate_block(true_block)
  285. builder.assign(result, builder.coerce(y, result.type, expr.line), expr.line)
  286. builder.goto(next_block)
  287. builder.activate_block(false_block)
  288. builder.assign(result, builder.coerce(x, result.type, expr.line), expr.line)
  289. builder.goto(next_block)
  290. builder.activate_block(next_block)
  291. return result
  292. return None
  293. @specialize_function("builtins.tuple")
  294. @specialize_function("builtins.frozenset")
  295. @specialize_function("builtins.dict")
  296. @specialize_function("builtins.min")
  297. @specialize_function("builtins.max")
  298. @specialize_function("builtins.sorted")
  299. @specialize_function("collections.OrderedDict")
  300. @specialize_function("join", str_rprimitive)
  301. @specialize_function("extend", list_rprimitive)
  302. @specialize_function("update", dict_rprimitive)
  303. @specialize_function("update", set_rprimitive)
  304. def translate_safe_generator_call(
  305. builder: IRBuilder, expr: CallExpr, callee: RefExpr
  306. ) -> Value | None:
  307. """Special cases for things that consume iterators where we know we
  308. can safely compile a generator into a list.
  309. """
  310. if (
  311. len(expr.args) > 0
  312. and expr.arg_kinds[0] == ARG_POS
  313. and isinstance(expr.args[0], GeneratorExpr)
  314. ):
  315. if isinstance(callee, MemberExpr):
  316. return builder.gen_method_call(
  317. builder.accept(callee.expr),
  318. callee.name,
  319. (
  320. [translate_list_comprehension(builder, expr.args[0])]
  321. + [builder.accept(arg) for arg in expr.args[1:]]
  322. ),
  323. builder.node_type(expr),
  324. expr.line,
  325. expr.arg_kinds,
  326. expr.arg_names,
  327. )
  328. else:
  329. return builder.call_refexpr_with_args(
  330. expr,
  331. callee,
  332. (
  333. [translate_list_comprehension(builder, expr.args[0])]
  334. + [builder.accept(arg) for arg in expr.args[1:]]
  335. ),
  336. )
  337. return None
  338. @specialize_function("builtins.any")
  339. def translate_any_call(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
  340. if (
  341. len(expr.args) == 1
  342. and expr.arg_kinds == [ARG_POS]
  343. and isinstance(expr.args[0], GeneratorExpr)
  344. ):
  345. return any_all_helper(builder, expr.args[0], builder.false, lambda x: x, builder.true)
  346. return None
  347. @specialize_function("builtins.all")
  348. def translate_all_call(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
  349. if (
  350. len(expr.args) == 1
  351. and expr.arg_kinds == [ARG_POS]
  352. and isinstance(expr.args[0], GeneratorExpr)
  353. ):
  354. return any_all_helper(
  355. builder,
  356. expr.args[0],
  357. builder.true,
  358. lambda x: builder.unary_op(x, "not", expr.line),
  359. builder.false,
  360. )
  361. return None
  362. def any_all_helper(
  363. builder: IRBuilder,
  364. gen: GeneratorExpr,
  365. initial_value: Callable[[], Value],
  366. modify: Callable[[Value], Value],
  367. new_value: Callable[[], Value],
  368. ) -> Value:
  369. retval = Register(bool_rprimitive)
  370. builder.assign(retval, initial_value(), -1)
  371. loop_params = list(zip(gen.indices, gen.sequences, gen.condlists, gen.is_async))
  372. true_block, false_block, exit_block = BasicBlock(), BasicBlock(), BasicBlock()
  373. def gen_inner_stmts() -> None:
  374. comparison = modify(builder.accept(gen.left_expr))
  375. builder.add_bool_branch(comparison, true_block, false_block)
  376. builder.activate_block(true_block)
  377. builder.assign(retval, new_value(), -1)
  378. builder.goto(exit_block)
  379. builder.activate_block(false_block)
  380. comprehension_helper(builder, loop_params, gen_inner_stmts, gen.line)
  381. builder.goto_and_activate(exit_block)
  382. return retval
  383. @specialize_function("builtins.sum")
  384. def translate_sum_call(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
  385. # specialized implementation is used if:
  386. # - only one or two arguments given (if not, sum() has been given invalid arguments)
  387. # - first argument is a Generator (there is no benefit to optimizing the performance of eg.
  388. # sum([1, 2, 3]), so non-Generator Iterables are not handled)
  389. if not (
  390. len(expr.args) in (1, 2)
  391. and expr.arg_kinds[0] == ARG_POS
  392. and isinstance(expr.args[0], GeneratorExpr)
  393. ):
  394. return None
  395. # handle 'start' argument, if given
  396. if len(expr.args) == 2:
  397. # ensure call to sum() was properly constructed
  398. if expr.arg_kinds[1] not in (ARG_POS, ARG_NAMED):
  399. return None
  400. start_expr = expr.args[1]
  401. else:
  402. start_expr = IntExpr(0)
  403. gen_expr = expr.args[0]
  404. target_type = builder.node_type(expr)
  405. retval = Register(target_type)
  406. builder.assign(retval, builder.coerce(builder.accept(start_expr), target_type, -1), -1)
  407. def gen_inner_stmts() -> None:
  408. call_expr = builder.accept(gen_expr.left_expr)
  409. builder.assign(retval, builder.binary_op(retval, call_expr, "+", -1), -1)
  410. loop_params = list(
  411. zip(gen_expr.indices, gen_expr.sequences, gen_expr.condlists, gen_expr.is_async)
  412. )
  413. comprehension_helper(builder, loop_params, gen_inner_stmts, gen_expr.line)
  414. return retval
  415. @specialize_function("dataclasses.field")
  416. @specialize_function("attr.ib")
  417. @specialize_function("attr.attrib")
  418. @specialize_function("attr.Factory")
  419. def translate_dataclasses_field_call(
  420. builder: IRBuilder, expr: CallExpr, callee: RefExpr
  421. ) -> Value | None:
  422. """Special case for 'dataclasses.field', 'attr.attrib', and 'attr.Factory'
  423. function calls because the results of such calls are type-checked
  424. by mypy using the types of the arguments to their respective
  425. functions, resulting in attempted coercions by mypyc that throw a
  426. runtime error.
  427. """
  428. builder.types[expr] = AnyType(TypeOfAny.from_error)
  429. return None
  430. @specialize_function("builtins.next")
  431. def translate_next_call(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
  432. """Special case for calling next() on a generator expression, an
  433. idiom that shows up some in mypy.
  434. For example, next(x for x in l if x.id == 12, None) will
  435. generate code that searches l for an element where x.id == 12
  436. and produce the first such object, or None if no such element
  437. exists.
  438. """
  439. if not (
  440. expr.arg_kinds in ([ARG_POS], [ARG_POS, ARG_POS])
  441. and isinstance(expr.args[0], GeneratorExpr)
  442. ):
  443. return None
  444. gen = expr.args[0]
  445. retval = Register(builder.node_type(expr))
  446. default_val = builder.accept(expr.args[1]) if len(expr.args) > 1 else None
  447. exit_block = BasicBlock()
  448. def gen_inner_stmts() -> None:
  449. # next takes the first element of the generator, so if
  450. # something gets produced, we are done.
  451. builder.assign(retval, builder.accept(gen.left_expr), gen.left_expr.line)
  452. builder.goto(exit_block)
  453. loop_params = list(zip(gen.indices, gen.sequences, gen.condlists, gen.is_async))
  454. comprehension_helper(builder, loop_params, gen_inner_stmts, gen.line)
  455. # Now we need the case for when nothing got hit. If there was
  456. # a default value, we produce it, and otherwise we raise
  457. # StopIteration.
  458. if default_val:
  459. builder.assign(retval, default_val, gen.left_expr.line)
  460. builder.goto(exit_block)
  461. else:
  462. builder.add(RaiseStandardError(RaiseStandardError.STOP_ITERATION, None, expr.line))
  463. builder.add(Unreachable())
  464. builder.activate_block(exit_block)
  465. return retval
  466. @specialize_function("builtins.isinstance")
  467. def translate_isinstance(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
  468. """Special case for builtins.isinstance.
  469. Prevent coercions on the thing we are checking the instance of -
  470. there is no need to coerce something to a new type before checking
  471. what type it is, and the coercion could lead to bugs.
  472. """
  473. if (
  474. len(expr.args) == 2
  475. and expr.arg_kinds == [ARG_POS, ARG_POS]
  476. and isinstance(expr.args[1], (RefExpr, TupleExpr))
  477. ):
  478. builder.types[expr.args[0]] = AnyType(TypeOfAny.from_error)
  479. irs = builder.flatten_classes(expr.args[1])
  480. if irs is not None:
  481. can_borrow = all(
  482. ir.is_ext_class and not ir.inherits_python and not ir.allow_interpreted_subclasses
  483. for ir in irs
  484. )
  485. obj = builder.accept(expr.args[0], can_borrow=can_borrow)
  486. return builder.builder.isinstance_helper(obj, irs, expr.line)
  487. return None
  488. @specialize_function("setdefault", dict_rprimitive)
  489. def translate_dict_setdefault(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
  490. """Special case for 'dict.setdefault' which would only construct
  491. default empty collection when needed.
  492. The dict_setdefault_spec_init_op checks whether the dict contains
  493. the key and would construct the empty collection only once.
  494. For example, this specializer works for the following cases:
  495. d.setdefault(key, set()).add(value)
  496. d.setdefault(key, []).append(value)
  497. d.setdefault(key, {})[inner_key] = inner_val
  498. """
  499. if (
  500. len(expr.args) == 2
  501. and expr.arg_kinds == [ARG_POS, ARG_POS]
  502. and isinstance(callee, MemberExpr)
  503. ):
  504. arg = expr.args[1]
  505. if isinstance(arg, ListExpr):
  506. if len(arg.items):
  507. return None
  508. data_type = Integer(1, c_int_rprimitive, expr.line)
  509. elif isinstance(arg, DictExpr):
  510. if len(arg.items):
  511. return None
  512. data_type = Integer(2, c_int_rprimitive, expr.line)
  513. elif (
  514. isinstance(arg, CallExpr)
  515. and isinstance(arg.callee, NameExpr)
  516. and arg.callee.fullname == "builtins.set"
  517. ):
  518. if len(arg.args):
  519. return None
  520. data_type = Integer(3, c_int_rprimitive, expr.line)
  521. else:
  522. return None
  523. callee_dict = builder.accept(callee.expr)
  524. key_val = builder.accept(expr.args[0])
  525. return builder.call_c(
  526. dict_setdefault_spec_init_op, [callee_dict, key_val, data_type], expr.line
  527. )
  528. return None
  529. @specialize_function("format", str_rprimitive)
  530. def translate_str_format(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
  531. if (
  532. isinstance(callee, MemberExpr)
  533. and isinstance(callee.expr, StrExpr)
  534. and expr.arg_kinds.count(ARG_POS) == len(expr.arg_kinds)
  535. ):
  536. format_str = callee.expr.value
  537. tokens = tokenizer_format_call(format_str)
  538. if tokens is None:
  539. return None
  540. literals, format_ops = tokens
  541. # Convert variables to strings
  542. substitutions = convert_format_expr_to_str(builder, format_ops, expr.args, expr.line)
  543. if substitutions is None:
  544. return None
  545. return join_formatted_strings(builder, literals, substitutions, expr.line)
  546. return None
  547. @specialize_function("join", str_rprimitive)
  548. def translate_fstring(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
  549. """Special case for f-string, which is translated into str.join()
  550. in mypy AST.
  551. This specializer optimizes simplest f-strings which don't contain
  552. any format operation.
  553. """
  554. if (
  555. isinstance(callee, MemberExpr)
  556. and isinstance(callee.expr, StrExpr)
  557. and callee.expr.value == ""
  558. and expr.arg_kinds == [ARG_POS]
  559. and isinstance(expr.args[0], ListExpr)
  560. ):
  561. for item in expr.args[0].items:
  562. if isinstance(item, StrExpr):
  563. continue
  564. elif isinstance(item, CallExpr):
  565. if not isinstance(item.callee, MemberExpr) or item.callee.name != "format":
  566. return None
  567. elif (
  568. not isinstance(item.callee.expr, StrExpr) or item.callee.expr.value != "{:{}}"
  569. ):
  570. return None
  571. if not isinstance(item.args[1], StrExpr) or item.args[1].value != "":
  572. return None
  573. else:
  574. return None
  575. format_ops = []
  576. exprs: list[Expression] = []
  577. for item in expr.args[0].items:
  578. if isinstance(item, StrExpr) and item.value != "":
  579. format_ops.append(FormatOp.STR)
  580. exprs.append(item)
  581. elif isinstance(item, CallExpr):
  582. format_ops.append(FormatOp.STR)
  583. exprs.append(item.args[0])
  584. substitutions = convert_format_expr_to_str(builder, format_ops, exprs, expr.line)
  585. if substitutions is None:
  586. return None
  587. return join_formatted_strings(builder, None, substitutions, expr.line)
  588. return None
  589. @specialize_function("mypy_extensions.i64")
  590. def translate_i64(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
  591. if len(expr.args) != 1 or expr.arg_kinds[0] != ARG_POS:
  592. return None
  593. arg = expr.args[0]
  594. arg_type = builder.node_type(arg)
  595. if is_int64_rprimitive(arg_type):
  596. return builder.accept(arg)
  597. elif is_int32_rprimitive(arg_type) or is_int16_rprimitive(arg_type):
  598. val = builder.accept(arg)
  599. return builder.add(Extend(val, int64_rprimitive, signed=True, line=expr.line))
  600. elif is_uint8_rprimitive(arg_type):
  601. val = builder.accept(arg)
  602. return builder.add(Extend(val, int64_rprimitive, signed=False, line=expr.line))
  603. elif is_int_rprimitive(arg_type) or is_bool_rprimitive(arg_type):
  604. val = builder.accept(arg)
  605. return builder.coerce(val, int64_rprimitive, expr.line)
  606. return None
  607. @specialize_function("mypy_extensions.i32")
  608. def translate_i32(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
  609. if len(expr.args) != 1 or expr.arg_kinds[0] != ARG_POS:
  610. return None
  611. arg = expr.args[0]
  612. arg_type = builder.node_type(arg)
  613. if is_int32_rprimitive(arg_type):
  614. return builder.accept(arg)
  615. elif is_int64_rprimitive(arg_type):
  616. val = builder.accept(arg)
  617. return builder.add(Truncate(val, int32_rprimitive, line=expr.line))
  618. elif is_int16_rprimitive(arg_type):
  619. val = builder.accept(arg)
  620. return builder.add(Extend(val, int32_rprimitive, signed=True, line=expr.line))
  621. elif is_uint8_rprimitive(arg_type):
  622. val = builder.accept(arg)
  623. return builder.add(Extend(val, int32_rprimitive, signed=False, line=expr.line))
  624. elif is_int_rprimitive(arg_type) or is_bool_rprimitive(arg_type):
  625. val = builder.accept(arg)
  626. val = truncate_literal(val, int32_rprimitive)
  627. return builder.coerce(val, int32_rprimitive, expr.line)
  628. return None
  629. @specialize_function("mypy_extensions.i16")
  630. def translate_i16(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
  631. if len(expr.args) != 1 or expr.arg_kinds[0] != ARG_POS:
  632. return None
  633. arg = expr.args[0]
  634. arg_type = builder.node_type(arg)
  635. if is_int16_rprimitive(arg_type):
  636. return builder.accept(arg)
  637. elif is_int32_rprimitive(arg_type) or is_int64_rprimitive(arg_type):
  638. val = builder.accept(arg)
  639. return builder.add(Truncate(val, int16_rprimitive, line=expr.line))
  640. elif is_uint8_rprimitive(arg_type):
  641. val = builder.accept(arg)
  642. return builder.add(Extend(val, int16_rprimitive, signed=False, line=expr.line))
  643. elif is_int_rprimitive(arg_type) or is_bool_rprimitive(arg_type):
  644. val = builder.accept(arg)
  645. val = truncate_literal(val, int16_rprimitive)
  646. return builder.coerce(val, int16_rprimitive, expr.line)
  647. return None
  648. @specialize_function("mypy_extensions.u8")
  649. def translate_u8(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
  650. if len(expr.args) != 1 or expr.arg_kinds[0] != ARG_POS:
  651. return None
  652. arg = expr.args[0]
  653. arg_type = builder.node_type(arg)
  654. if is_uint8_rprimitive(arg_type):
  655. return builder.accept(arg)
  656. elif (
  657. is_int16_rprimitive(arg_type)
  658. or is_int32_rprimitive(arg_type)
  659. or is_int64_rprimitive(arg_type)
  660. ):
  661. val = builder.accept(arg)
  662. return builder.add(Truncate(val, uint8_rprimitive, line=expr.line))
  663. elif is_int_rprimitive(arg_type) or is_bool_rprimitive(arg_type):
  664. val = builder.accept(arg)
  665. val = truncate_literal(val, uint8_rprimitive)
  666. return builder.coerce(val, uint8_rprimitive, expr.line)
  667. return None
  668. def truncate_literal(value: Value, rtype: RPrimitive) -> Value:
  669. """If value is an integer literal value, truncate it to given native int rtype.
  670. For example, truncate 256 into 0 if rtype is u8.
  671. """
  672. if not isinstance(value, Integer):
  673. return value # Not a literal, nothing to do
  674. x = value.numeric_value()
  675. max_unsigned = (1 << (rtype.size * 8)) - 1
  676. x = x & max_unsigned
  677. if rtype.is_signed and x >= (max_unsigned + 1) // 2:
  678. # Adjust to make it a negative value
  679. x -= max_unsigned + 1
  680. return Integer(x, rtype)
  681. @specialize_function("builtins.int")
  682. def translate_int(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
  683. if len(expr.args) != 1 or expr.arg_kinds[0] != ARG_POS:
  684. return None
  685. arg = expr.args[0]
  686. arg_type = builder.node_type(arg)
  687. if (
  688. is_bool_rprimitive(arg_type)
  689. or is_int_rprimitive(arg_type)
  690. or is_fixed_width_rtype(arg_type)
  691. ):
  692. src = builder.accept(arg)
  693. return builder.coerce(src, int_rprimitive, expr.line)
  694. return None
  695. @specialize_function("builtins.bool")
  696. def translate_bool(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
  697. if len(expr.args) != 1 or expr.arg_kinds[0] != ARG_POS:
  698. return None
  699. arg = expr.args[0]
  700. src = builder.accept(arg)
  701. return builder.builder.bool_value(src)
  702. @specialize_function("builtins.float")
  703. def translate_float(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
  704. if len(expr.args) != 1 or expr.arg_kinds[0] != ARG_POS:
  705. return None
  706. arg = expr.args[0]
  707. arg_type = builder.node_type(arg)
  708. if is_float_rprimitive(arg_type):
  709. # No-op float conversion.
  710. return builder.accept(arg)
  711. return None