ast_opt.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  1. /* AST Optimizer */
  2. #include "Python.h"
  3. #include "pycore_ast.h" // _PyAST_GetDocString()
  4. #include "pycore_compile.h" // _PyASTOptimizeState
  5. #include "pycore_long.h" // _PyLong
  6. #include "pycore_pystate.h" // _PyThreadState_GET()
  7. #include "pycore_format.h" // F_LJUST
  8. static int
  9. make_const(expr_ty node, PyObject *val, PyArena *arena)
  10. {
  11. // Even if no new value was calculated, make_const may still
  12. // need to clear an error (e.g. for division by zero)
  13. if (val == NULL) {
  14. if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) {
  15. return 0;
  16. }
  17. PyErr_Clear();
  18. return 1;
  19. }
  20. if (_PyArena_AddPyObject(arena, val) < 0) {
  21. Py_DECREF(val);
  22. return 0;
  23. }
  24. node->kind = Constant_kind;
  25. node->v.Constant.kind = NULL;
  26. node->v.Constant.value = val;
  27. return 1;
  28. }
  29. #define COPY_NODE(TO, FROM) (memcpy((TO), (FROM), sizeof(struct _expr)))
  30. static int
  31. has_starred(asdl_expr_seq *elts)
  32. {
  33. Py_ssize_t n = asdl_seq_LEN(elts);
  34. for (Py_ssize_t i = 0; i < n; i++) {
  35. expr_ty e = (expr_ty)asdl_seq_GET(elts, i);
  36. if (e->kind == Starred_kind) {
  37. return 1;
  38. }
  39. }
  40. return 0;
  41. }
  42. static PyObject*
  43. unary_not(PyObject *v)
  44. {
  45. int r = PyObject_IsTrue(v);
  46. if (r < 0)
  47. return NULL;
  48. return PyBool_FromLong(!r);
  49. }
  50. static int
  51. fold_unaryop(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
  52. {
  53. expr_ty arg = node->v.UnaryOp.operand;
  54. if (arg->kind != Constant_kind) {
  55. /* Fold not into comparison */
  56. if (node->v.UnaryOp.op == Not && arg->kind == Compare_kind &&
  57. asdl_seq_LEN(arg->v.Compare.ops) == 1) {
  58. /* Eq and NotEq are often implemented in terms of one another, so
  59. folding not (self == other) into self != other breaks implementation
  60. of !=. Detecting such cases doesn't seem worthwhile.
  61. Python uses </> for 'is subset'/'is superset' operations on sets.
  62. They don't satisfy not folding laws. */
  63. cmpop_ty op = asdl_seq_GET(arg->v.Compare.ops, 0);
  64. switch (op) {
  65. case Is:
  66. op = IsNot;
  67. break;
  68. case IsNot:
  69. op = Is;
  70. break;
  71. case In:
  72. op = NotIn;
  73. break;
  74. case NotIn:
  75. op = In;
  76. break;
  77. // The remaining comparison operators can't be safely inverted
  78. case Eq:
  79. case NotEq:
  80. case Lt:
  81. case LtE:
  82. case Gt:
  83. case GtE:
  84. op = 0; // The AST enums leave "0" free as an "unused" marker
  85. break;
  86. // No default case, so the compiler will emit a warning if new
  87. // comparison operators are added without being handled here
  88. }
  89. if (op) {
  90. asdl_seq_SET(arg->v.Compare.ops, 0, op);
  91. COPY_NODE(node, arg);
  92. return 1;
  93. }
  94. }
  95. return 1;
  96. }
  97. typedef PyObject *(*unary_op)(PyObject*);
  98. static const unary_op ops[] = {
  99. [Invert] = PyNumber_Invert,
  100. [Not] = unary_not,
  101. [UAdd] = PyNumber_Positive,
  102. [USub] = PyNumber_Negative,
  103. };
  104. PyObject *newval = ops[node->v.UnaryOp.op](arg->v.Constant.value);
  105. return make_const(node, newval, arena);
  106. }
  107. /* Check whether a collection doesn't containing too much items (including
  108. subcollections). This protects from creating a constant that needs
  109. too much time for calculating a hash.
  110. "limit" is the maximal number of items.
  111. Returns the negative number if the total number of items exceeds the
  112. limit. Otherwise returns the limit minus the total number of items.
  113. */
  114. static Py_ssize_t
  115. check_complexity(PyObject *obj, Py_ssize_t limit)
  116. {
  117. if (PyTuple_Check(obj)) {
  118. Py_ssize_t i;
  119. limit -= PyTuple_GET_SIZE(obj);
  120. for (i = 0; limit >= 0 && i < PyTuple_GET_SIZE(obj); i++) {
  121. limit = check_complexity(PyTuple_GET_ITEM(obj, i), limit);
  122. }
  123. return limit;
  124. }
  125. else if (PyFrozenSet_Check(obj)) {
  126. Py_ssize_t i = 0;
  127. PyObject *item;
  128. Py_hash_t hash;
  129. limit -= PySet_GET_SIZE(obj);
  130. while (limit >= 0 && _PySet_NextEntry(obj, &i, &item, &hash)) {
  131. limit = check_complexity(item, limit);
  132. }
  133. }
  134. return limit;
  135. }
  136. #define MAX_INT_SIZE 128 /* bits */
  137. #define MAX_COLLECTION_SIZE 256 /* items */
  138. #define MAX_STR_SIZE 4096 /* characters */
  139. #define MAX_TOTAL_ITEMS 1024 /* including nested collections */
  140. static PyObject *
  141. safe_multiply(PyObject *v, PyObject *w)
  142. {
  143. if (PyLong_Check(v) && PyLong_Check(w) &&
  144. !_PyLong_IsZero((PyLongObject *)v) && !_PyLong_IsZero((PyLongObject *)w)
  145. ) {
  146. size_t vbits = _PyLong_NumBits(v);
  147. size_t wbits = _PyLong_NumBits(w);
  148. if (vbits == (size_t)-1 || wbits == (size_t)-1) {
  149. return NULL;
  150. }
  151. if (vbits + wbits > MAX_INT_SIZE) {
  152. return NULL;
  153. }
  154. }
  155. else if (PyLong_Check(v) && (PyTuple_Check(w) || PyFrozenSet_Check(w))) {
  156. Py_ssize_t size = PyTuple_Check(w) ? PyTuple_GET_SIZE(w) :
  157. PySet_GET_SIZE(w);
  158. if (size) {
  159. long n = PyLong_AsLong(v);
  160. if (n < 0 || n > MAX_COLLECTION_SIZE / size) {
  161. return NULL;
  162. }
  163. if (n && check_complexity(w, MAX_TOTAL_ITEMS / n) < 0) {
  164. return NULL;
  165. }
  166. }
  167. }
  168. else if (PyLong_Check(v) && (PyUnicode_Check(w) || PyBytes_Check(w))) {
  169. Py_ssize_t size = PyUnicode_Check(w) ? PyUnicode_GET_LENGTH(w) :
  170. PyBytes_GET_SIZE(w);
  171. if (size) {
  172. long n = PyLong_AsLong(v);
  173. if (n < 0 || n > MAX_STR_SIZE / size) {
  174. return NULL;
  175. }
  176. }
  177. }
  178. else if (PyLong_Check(w) &&
  179. (PyTuple_Check(v) || PyFrozenSet_Check(v) ||
  180. PyUnicode_Check(v) || PyBytes_Check(v)))
  181. {
  182. return safe_multiply(w, v);
  183. }
  184. return PyNumber_Multiply(v, w);
  185. }
  186. static PyObject *
  187. safe_power(PyObject *v, PyObject *w)
  188. {
  189. if (PyLong_Check(v) && PyLong_Check(w) &&
  190. !_PyLong_IsZero((PyLongObject *)v) && _PyLong_IsPositive((PyLongObject *)w)
  191. ) {
  192. size_t vbits = _PyLong_NumBits(v);
  193. size_t wbits = PyLong_AsSize_t(w);
  194. if (vbits == (size_t)-1 || wbits == (size_t)-1) {
  195. return NULL;
  196. }
  197. if (vbits > MAX_INT_SIZE / wbits) {
  198. return NULL;
  199. }
  200. }
  201. return PyNumber_Power(v, w, Py_None);
  202. }
  203. static PyObject *
  204. safe_lshift(PyObject *v, PyObject *w)
  205. {
  206. if (PyLong_Check(v) && PyLong_Check(w) &&
  207. !_PyLong_IsZero((PyLongObject *)v) && !_PyLong_IsZero((PyLongObject *)w)
  208. ) {
  209. size_t vbits = _PyLong_NumBits(v);
  210. size_t wbits = PyLong_AsSize_t(w);
  211. if (vbits == (size_t)-1 || wbits == (size_t)-1) {
  212. return NULL;
  213. }
  214. if (wbits > MAX_INT_SIZE || vbits > MAX_INT_SIZE - wbits) {
  215. return NULL;
  216. }
  217. }
  218. return PyNumber_Lshift(v, w);
  219. }
  220. static PyObject *
  221. safe_mod(PyObject *v, PyObject *w)
  222. {
  223. if (PyUnicode_Check(v) || PyBytes_Check(v)) {
  224. return NULL;
  225. }
  226. return PyNumber_Remainder(v, w);
  227. }
  228. static expr_ty
  229. parse_literal(PyObject *fmt, Py_ssize_t *ppos, PyArena *arena)
  230. {
  231. const void *data = PyUnicode_DATA(fmt);
  232. int kind = PyUnicode_KIND(fmt);
  233. Py_ssize_t size = PyUnicode_GET_LENGTH(fmt);
  234. Py_ssize_t start, pos;
  235. int has_percents = 0;
  236. start = pos = *ppos;
  237. while (pos < size) {
  238. if (PyUnicode_READ(kind, data, pos) != '%') {
  239. pos++;
  240. }
  241. else if (pos+1 < size && PyUnicode_READ(kind, data, pos+1) == '%') {
  242. has_percents = 1;
  243. pos += 2;
  244. }
  245. else {
  246. break;
  247. }
  248. }
  249. *ppos = pos;
  250. if (pos == start) {
  251. return NULL;
  252. }
  253. PyObject *str = PyUnicode_Substring(fmt, start, pos);
  254. /* str = str.replace('%%', '%') */
  255. if (str && has_percents) {
  256. _Py_DECLARE_STR(dbl_percent, "%%");
  257. Py_SETREF(str, PyUnicode_Replace(str, &_Py_STR(dbl_percent),
  258. _Py_LATIN1_CHR('%'), -1));
  259. }
  260. if (!str) {
  261. return NULL;
  262. }
  263. if (_PyArena_AddPyObject(arena, str) < 0) {
  264. Py_DECREF(str);
  265. return NULL;
  266. }
  267. return _PyAST_Constant(str, NULL, -1, -1, -1, -1, arena);
  268. }
  269. #define MAXDIGITS 3
  270. static int
  271. simple_format_arg_parse(PyObject *fmt, Py_ssize_t *ppos,
  272. int *spec, int *flags, int *width, int *prec)
  273. {
  274. Py_ssize_t pos = *ppos, len = PyUnicode_GET_LENGTH(fmt);
  275. Py_UCS4 ch;
  276. #define NEXTC do { \
  277. if (pos >= len) { \
  278. return 0; \
  279. } \
  280. ch = PyUnicode_READ_CHAR(fmt, pos); \
  281. pos++; \
  282. } while (0)
  283. *flags = 0;
  284. while (1) {
  285. NEXTC;
  286. switch (ch) {
  287. case '-': *flags |= F_LJUST; continue;
  288. case '+': *flags |= F_SIGN; continue;
  289. case ' ': *flags |= F_BLANK; continue;
  290. case '#': *flags |= F_ALT; continue;
  291. case '0': *flags |= F_ZERO; continue;
  292. }
  293. break;
  294. }
  295. if ('0' <= ch && ch <= '9') {
  296. *width = 0;
  297. int digits = 0;
  298. while ('0' <= ch && ch <= '9') {
  299. *width = *width * 10 + (ch - '0');
  300. NEXTC;
  301. if (++digits >= MAXDIGITS) {
  302. return 0;
  303. }
  304. }
  305. }
  306. if (ch == '.') {
  307. NEXTC;
  308. *prec = 0;
  309. if ('0' <= ch && ch <= '9') {
  310. int digits = 0;
  311. while ('0' <= ch && ch <= '9') {
  312. *prec = *prec * 10 + (ch - '0');
  313. NEXTC;
  314. if (++digits >= MAXDIGITS) {
  315. return 0;
  316. }
  317. }
  318. }
  319. }
  320. *spec = ch;
  321. *ppos = pos;
  322. return 1;
  323. #undef NEXTC
  324. }
  325. static expr_ty
  326. parse_format(PyObject *fmt, Py_ssize_t *ppos, expr_ty arg, PyArena *arena)
  327. {
  328. int spec, flags, width = -1, prec = -1;
  329. if (!simple_format_arg_parse(fmt, ppos, &spec, &flags, &width, &prec)) {
  330. // Unsupported format.
  331. return NULL;
  332. }
  333. if (spec == 's' || spec == 'r' || spec == 'a') {
  334. char buf[1 + MAXDIGITS + 1 + MAXDIGITS + 1], *p = buf;
  335. if (!(flags & F_LJUST) && width > 0) {
  336. *p++ = '>';
  337. }
  338. if (width >= 0) {
  339. p += snprintf(p, MAXDIGITS + 1, "%d", width);
  340. }
  341. if (prec >= 0) {
  342. p += snprintf(p, MAXDIGITS + 2, ".%d", prec);
  343. }
  344. expr_ty format_spec = NULL;
  345. if (p != buf) {
  346. PyObject *str = PyUnicode_FromString(buf);
  347. if (str == NULL) {
  348. return NULL;
  349. }
  350. if (_PyArena_AddPyObject(arena, str) < 0) {
  351. Py_DECREF(str);
  352. return NULL;
  353. }
  354. format_spec = _PyAST_Constant(str, NULL, -1, -1, -1, -1, arena);
  355. if (format_spec == NULL) {
  356. return NULL;
  357. }
  358. }
  359. return _PyAST_FormattedValue(arg, spec, format_spec,
  360. arg->lineno, arg->col_offset,
  361. arg->end_lineno, arg->end_col_offset,
  362. arena);
  363. }
  364. // Unsupported format.
  365. return NULL;
  366. }
  367. static int
  368. optimize_format(expr_ty node, PyObject *fmt, asdl_expr_seq *elts, PyArena *arena)
  369. {
  370. Py_ssize_t pos = 0;
  371. Py_ssize_t cnt = 0;
  372. asdl_expr_seq *seq = _Py_asdl_expr_seq_new(asdl_seq_LEN(elts) * 2 + 1, arena);
  373. if (!seq) {
  374. return 0;
  375. }
  376. seq->size = 0;
  377. while (1) {
  378. expr_ty lit = parse_literal(fmt, &pos, arena);
  379. if (lit) {
  380. asdl_seq_SET(seq, seq->size++, lit);
  381. }
  382. else if (PyErr_Occurred()) {
  383. return 0;
  384. }
  385. if (pos >= PyUnicode_GET_LENGTH(fmt)) {
  386. break;
  387. }
  388. if (cnt >= asdl_seq_LEN(elts)) {
  389. // More format units than items.
  390. return 1;
  391. }
  392. assert(PyUnicode_READ_CHAR(fmt, pos) == '%');
  393. pos++;
  394. expr_ty expr = parse_format(fmt, &pos, asdl_seq_GET(elts, cnt), arena);
  395. cnt++;
  396. if (!expr) {
  397. return !PyErr_Occurred();
  398. }
  399. asdl_seq_SET(seq, seq->size++, expr);
  400. }
  401. if (cnt < asdl_seq_LEN(elts)) {
  402. // More items than format units.
  403. return 1;
  404. }
  405. expr_ty res = _PyAST_JoinedStr(seq,
  406. node->lineno, node->col_offset,
  407. node->end_lineno, node->end_col_offset,
  408. arena);
  409. if (!res) {
  410. return 0;
  411. }
  412. COPY_NODE(node, res);
  413. // PySys_FormatStderr("format = %R\n", fmt);
  414. return 1;
  415. }
  416. static int
  417. fold_binop(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
  418. {
  419. expr_ty lhs, rhs;
  420. lhs = node->v.BinOp.left;
  421. rhs = node->v.BinOp.right;
  422. if (lhs->kind != Constant_kind) {
  423. return 1;
  424. }
  425. PyObject *lv = lhs->v.Constant.value;
  426. if (node->v.BinOp.op == Mod &&
  427. rhs->kind == Tuple_kind &&
  428. PyUnicode_Check(lv) &&
  429. !has_starred(rhs->v.Tuple.elts))
  430. {
  431. return optimize_format(node, lv, rhs->v.Tuple.elts, arena);
  432. }
  433. if (rhs->kind != Constant_kind) {
  434. return 1;
  435. }
  436. PyObject *rv = rhs->v.Constant.value;
  437. PyObject *newval = NULL;
  438. switch (node->v.BinOp.op) {
  439. case Add:
  440. newval = PyNumber_Add(lv, rv);
  441. break;
  442. case Sub:
  443. newval = PyNumber_Subtract(lv, rv);
  444. break;
  445. case Mult:
  446. newval = safe_multiply(lv, rv);
  447. break;
  448. case Div:
  449. newval = PyNumber_TrueDivide(lv, rv);
  450. break;
  451. case FloorDiv:
  452. newval = PyNumber_FloorDivide(lv, rv);
  453. break;
  454. case Mod:
  455. newval = safe_mod(lv, rv);
  456. break;
  457. case Pow:
  458. newval = safe_power(lv, rv);
  459. break;
  460. case LShift:
  461. newval = safe_lshift(lv, rv);
  462. break;
  463. case RShift:
  464. newval = PyNumber_Rshift(lv, rv);
  465. break;
  466. case BitOr:
  467. newval = PyNumber_Or(lv, rv);
  468. break;
  469. case BitXor:
  470. newval = PyNumber_Xor(lv, rv);
  471. break;
  472. case BitAnd:
  473. newval = PyNumber_And(lv, rv);
  474. break;
  475. // No builtin constants implement the following operators
  476. case MatMult:
  477. return 1;
  478. // No default case, so the compiler will emit a warning if new binary
  479. // operators are added without being handled here
  480. }
  481. return make_const(node, newval, arena);
  482. }
  483. static PyObject*
  484. make_const_tuple(asdl_expr_seq *elts)
  485. {
  486. for (int i = 0; i < asdl_seq_LEN(elts); i++) {
  487. expr_ty e = (expr_ty)asdl_seq_GET(elts, i);
  488. if (e->kind != Constant_kind) {
  489. return NULL;
  490. }
  491. }
  492. PyObject *newval = PyTuple_New(asdl_seq_LEN(elts));
  493. if (newval == NULL) {
  494. return NULL;
  495. }
  496. for (int i = 0; i < asdl_seq_LEN(elts); i++) {
  497. expr_ty e = (expr_ty)asdl_seq_GET(elts, i);
  498. PyObject *v = e->v.Constant.value;
  499. PyTuple_SET_ITEM(newval, i, Py_NewRef(v));
  500. }
  501. return newval;
  502. }
  503. static int
  504. fold_tuple(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
  505. {
  506. PyObject *newval;
  507. if (node->v.Tuple.ctx != Load)
  508. return 1;
  509. newval = make_const_tuple(node->v.Tuple.elts);
  510. return make_const(node, newval, arena);
  511. }
  512. static int
  513. fold_subscr(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
  514. {
  515. PyObject *newval;
  516. expr_ty arg, idx;
  517. arg = node->v.Subscript.value;
  518. idx = node->v.Subscript.slice;
  519. if (node->v.Subscript.ctx != Load ||
  520. arg->kind != Constant_kind ||
  521. idx->kind != Constant_kind)
  522. {
  523. return 1;
  524. }
  525. newval = PyObject_GetItem(arg->v.Constant.value, idx->v.Constant.value);
  526. return make_const(node, newval, arena);
  527. }
  528. /* Change literal list or set of constants into constant
  529. tuple or frozenset respectively. Change literal list of
  530. non-constants into tuple.
  531. Used for right operand of "in" and "not in" tests and for iterable
  532. in "for" loop and comprehensions.
  533. */
  534. static int
  535. fold_iter(expr_ty arg, PyArena *arena, _PyASTOptimizeState *state)
  536. {
  537. PyObject *newval;
  538. if (arg->kind == List_kind) {
  539. /* First change a list into tuple. */
  540. asdl_expr_seq *elts = arg->v.List.elts;
  541. if (has_starred(elts)) {
  542. return 1;
  543. }
  544. expr_context_ty ctx = arg->v.List.ctx;
  545. arg->kind = Tuple_kind;
  546. arg->v.Tuple.elts = elts;
  547. arg->v.Tuple.ctx = ctx;
  548. /* Try to create a constant tuple. */
  549. newval = make_const_tuple(elts);
  550. }
  551. else if (arg->kind == Set_kind) {
  552. newval = make_const_tuple(arg->v.Set.elts);
  553. if (newval) {
  554. Py_SETREF(newval, PyFrozenSet_New(newval));
  555. }
  556. }
  557. else {
  558. return 1;
  559. }
  560. return make_const(arg, newval, arena);
  561. }
  562. static int
  563. fold_compare(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
  564. {
  565. asdl_int_seq *ops;
  566. asdl_expr_seq *args;
  567. Py_ssize_t i;
  568. ops = node->v.Compare.ops;
  569. args = node->v.Compare.comparators;
  570. /* Change literal list or set in 'in' or 'not in' into
  571. tuple or frozenset respectively. */
  572. i = asdl_seq_LEN(ops) - 1;
  573. int op = asdl_seq_GET(ops, i);
  574. if (op == In || op == NotIn) {
  575. if (!fold_iter((expr_ty)asdl_seq_GET(args, i), arena, state)) {
  576. return 0;
  577. }
  578. }
  579. return 1;
  580. }
  581. static int astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
  582. static int astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
  583. static int astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
  584. static int astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
  585. static int astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
  586. static int astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
  587. static int astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
  588. static int astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
  589. static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
  590. static int astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
  591. static int astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
  592. static int astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTOptimizeState *state);
  593. #define CALL(FUNC, TYPE, ARG) \
  594. if (!FUNC((ARG), ctx_, state)) \
  595. return 0;
  596. #define CALL_OPT(FUNC, TYPE, ARG) \
  597. if ((ARG) != NULL && !FUNC((ARG), ctx_, state)) \
  598. return 0;
  599. #define CALL_SEQ(FUNC, TYPE, ARG) { \
  600. int i; \
  601. asdl_ ## TYPE ## _seq *seq = (ARG); /* avoid variable capture */ \
  602. for (i = 0; i < asdl_seq_LEN(seq); i++) { \
  603. TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
  604. if (elt != NULL && !FUNC(elt, ctx_, state)) \
  605. return 0; \
  606. } \
  607. }
  608. static int
  609. astfold_body(asdl_stmt_seq *stmts, PyArena *ctx_, _PyASTOptimizeState *state)
  610. {
  611. int docstring = _PyAST_GetDocString(stmts) != NULL;
  612. CALL_SEQ(astfold_stmt, stmt, stmts);
  613. if (!docstring && _PyAST_GetDocString(stmts) != NULL) {
  614. stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0);
  615. asdl_expr_seq *values = _Py_asdl_expr_seq_new(1, ctx_);
  616. if (!values) {
  617. return 0;
  618. }
  619. asdl_seq_SET(values, 0, st->v.Expr.value);
  620. expr_ty expr = _PyAST_JoinedStr(values, st->lineno, st->col_offset,
  621. st->end_lineno, st->end_col_offset,
  622. ctx_);
  623. if (!expr) {
  624. return 0;
  625. }
  626. st->v.Expr.value = expr;
  627. }
  628. return 1;
  629. }
  630. static int
  631. astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
  632. {
  633. switch (node_->kind) {
  634. case Module_kind:
  635. CALL(astfold_body, asdl_seq, node_->v.Module.body);
  636. break;
  637. case Interactive_kind:
  638. CALL_SEQ(astfold_stmt, stmt, node_->v.Interactive.body);
  639. break;
  640. case Expression_kind:
  641. CALL(astfold_expr, expr_ty, node_->v.Expression.body);
  642. break;
  643. // The following top level nodes don't participate in constant folding
  644. case FunctionType_kind:
  645. break;
  646. // No default case, so the compiler will emit a warning if new top level
  647. // compilation nodes are added without being handled here
  648. }
  649. return 1;
  650. }
  651. static int
  652. astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
  653. {
  654. if (++state->recursion_depth > state->recursion_limit) {
  655. PyErr_SetString(PyExc_RecursionError,
  656. "maximum recursion depth exceeded during compilation");
  657. return 0;
  658. }
  659. switch (node_->kind) {
  660. case BoolOp_kind:
  661. CALL_SEQ(astfold_expr, expr, node_->v.BoolOp.values);
  662. break;
  663. case BinOp_kind:
  664. CALL(astfold_expr, expr_ty, node_->v.BinOp.left);
  665. CALL(astfold_expr, expr_ty, node_->v.BinOp.right);
  666. CALL(fold_binop, expr_ty, node_);
  667. break;
  668. case UnaryOp_kind:
  669. CALL(astfold_expr, expr_ty, node_->v.UnaryOp.operand);
  670. CALL(fold_unaryop, expr_ty, node_);
  671. break;
  672. case Lambda_kind:
  673. CALL(astfold_arguments, arguments_ty, node_->v.Lambda.args);
  674. CALL(astfold_expr, expr_ty, node_->v.Lambda.body);
  675. break;
  676. case IfExp_kind:
  677. CALL(astfold_expr, expr_ty, node_->v.IfExp.test);
  678. CALL(astfold_expr, expr_ty, node_->v.IfExp.body);
  679. CALL(astfold_expr, expr_ty, node_->v.IfExp.orelse);
  680. break;
  681. case Dict_kind:
  682. CALL_SEQ(astfold_expr, expr, node_->v.Dict.keys);
  683. CALL_SEQ(astfold_expr, expr, node_->v.Dict.values);
  684. break;
  685. case Set_kind:
  686. CALL_SEQ(astfold_expr, expr, node_->v.Set.elts);
  687. break;
  688. case ListComp_kind:
  689. CALL(astfold_expr, expr_ty, node_->v.ListComp.elt);
  690. CALL_SEQ(astfold_comprehension, comprehension, node_->v.ListComp.generators);
  691. break;
  692. case SetComp_kind:
  693. CALL(astfold_expr, expr_ty, node_->v.SetComp.elt);
  694. CALL_SEQ(astfold_comprehension, comprehension, node_->v.SetComp.generators);
  695. break;
  696. case DictComp_kind:
  697. CALL(astfold_expr, expr_ty, node_->v.DictComp.key);
  698. CALL(astfold_expr, expr_ty, node_->v.DictComp.value);
  699. CALL_SEQ(astfold_comprehension, comprehension, node_->v.DictComp.generators);
  700. break;
  701. case GeneratorExp_kind:
  702. CALL(astfold_expr, expr_ty, node_->v.GeneratorExp.elt);
  703. CALL_SEQ(astfold_comprehension, comprehension, node_->v.GeneratorExp.generators);
  704. break;
  705. case Await_kind:
  706. CALL(astfold_expr, expr_ty, node_->v.Await.value);
  707. break;
  708. case Yield_kind:
  709. CALL_OPT(astfold_expr, expr_ty, node_->v.Yield.value);
  710. break;
  711. case YieldFrom_kind:
  712. CALL(astfold_expr, expr_ty, node_->v.YieldFrom.value);
  713. break;
  714. case Compare_kind:
  715. CALL(astfold_expr, expr_ty, node_->v.Compare.left);
  716. CALL_SEQ(astfold_expr, expr, node_->v.Compare.comparators);
  717. CALL(fold_compare, expr_ty, node_);
  718. break;
  719. case Call_kind:
  720. CALL(astfold_expr, expr_ty, node_->v.Call.func);
  721. CALL_SEQ(astfold_expr, expr, node_->v.Call.args);
  722. CALL_SEQ(astfold_keyword, keyword, node_->v.Call.keywords);
  723. break;
  724. case FormattedValue_kind:
  725. CALL(astfold_expr, expr_ty, node_->v.FormattedValue.value);
  726. CALL_OPT(astfold_expr, expr_ty, node_->v.FormattedValue.format_spec);
  727. break;
  728. case JoinedStr_kind:
  729. CALL_SEQ(astfold_expr, expr, node_->v.JoinedStr.values);
  730. break;
  731. case Attribute_kind:
  732. CALL(astfold_expr, expr_ty, node_->v.Attribute.value);
  733. break;
  734. case Subscript_kind:
  735. CALL(astfold_expr, expr_ty, node_->v.Subscript.value);
  736. CALL(astfold_expr, expr_ty, node_->v.Subscript.slice);
  737. CALL(fold_subscr, expr_ty, node_);
  738. break;
  739. case Starred_kind:
  740. CALL(astfold_expr, expr_ty, node_->v.Starred.value);
  741. break;
  742. case Slice_kind:
  743. CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.lower);
  744. CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.upper);
  745. CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.step);
  746. break;
  747. case List_kind:
  748. CALL_SEQ(astfold_expr, expr, node_->v.List.elts);
  749. break;
  750. case Tuple_kind:
  751. CALL_SEQ(astfold_expr, expr, node_->v.Tuple.elts);
  752. CALL(fold_tuple, expr_ty, node_);
  753. break;
  754. case Name_kind:
  755. if (node_->v.Name.ctx == Load &&
  756. _PyUnicode_EqualToASCIIString(node_->v.Name.id, "__debug__")) {
  757. state->recursion_depth--;
  758. return make_const(node_, PyBool_FromLong(!state->optimize), ctx_);
  759. }
  760. break;
  761. case NamedExpr_kind:
  762. CALL(astfold_expr, expr_ty, node_->v.NamedExpr.value);
  763. break;
  764. case Constant_kind:
  765. // Already a constant, nothing further to do
  766. break;
  767. // No default case, so the compiler will emit a warning if new expression
  768. // kinds are added without being handled here
  769. }
  770. state->recursion_depth--;
  771. return 1;
  772. }
  773. static int
  774. astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
  775. {
  776. CALL(astfold_expr, expr_ty, node_->value);
  777. return 1;
  778. }
  779. static int
  780. astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
  781. {
  782. CALL(astfold_expr, expr_ty, node_->target);
  783. CALL(astfold_expr, expr_ty, node_->iter);
  784. CALL_SEQ(astfold_expr, expr, node_->ifs);
  785. CALL(fold_iter, expr_ty, node_->iter);
  786. return 1;
  787. }
  788. static int
  789. astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
  790. {
  791. CALL_SEQ(astfold_arg, arg, node_->posonlyargs);
  792. CALL_SEQ(astfold_arg, arg, node_->args);
  793. CALL_OPT(astfold_arg, arg_ty, node_->vararg);
  794. CALL_SEQ(astfold_arg, arg, node_->kwonlyargs);
  795. CALL_SEQ(astfold_expr, expr, node_->kw_defaults);
  796. CALL_OPT(astfold_arg, arg_ty, node_->kwarg);
  797. CALL_SEQ(astfold_expr, expr, node_->defaults);
  798. return 1;
  799. }
  800. static int
  801. astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
  802. {
  803. if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) {
  804. CALL_OPT(astfold_expr, expr_ty, node_->annotation);
  805. }
  806. return 1;
  807. }
  808. static int
  809. astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
  810. {
  811. if (++state->recursion_depth > state->recursion_limit) {
  812. PyErr_SetString(PyExc_RecursionError,
  813. "maximum recursion depth exceeded during compilation");
  814. return 0;
  815. }
  816. switch (node_->kind) {
  817. case FunctionDef_kind:
  818. CALL_SEQ(astfold_type_param, type_param, node_->v.FunctionDef.type_params);
  819. CALL(astfold_arguments, arguments_ty, node_->v.FunctionDef.args);
  820. CALL(astfold_body, asdl_seq, node_->v.FunctionDef.body);
  821. CALL_SEQ(astfold_expr, expr, node_->v.FunctionDef.decorator_list);
  822. if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) {
  823. CALL_OPT(astfold_expr, expr_ty, node_->v.FunctionDef.returns);
  824. }
  825. break;
  826. case AsyncFunctionDef_kind:
  827. CALL_SEQ(astfold_type_param, type_param, node_->v.AsyncFunctionDef.type_params);
  828. CALL(astfold_arguments, arguments_ty, node_->v.AsyncFunctionDef.args);
  829. CALL(astfold_body, asdl_seq, node_->v.AsyncFunctionDef.body);
  830. CALL_SEQ(astfold_expr, expr, node_->v.AsyncFunctionDef.decorator_list);
  831. if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) {
  832. CALL_OPT(astfold_expr, expr_ty, node_->v.AsyncFunctionDef.returns);
  833. }
  834. break;
  835. case ClassDef_kind:
  836. CALL_SEQ(astfold_type_param, type_param, node_->v.ClassDef.type_params);
  837. CALL_SEQ(astfold_expr, expr, node_->v.ClassDef.bases);
  838. CALL_SEQ(astfold_keyword, keyword, node_->v.ClassDef.keywords);
  839. CALL(astfold_body, asdl_seq, node_->v.ClassDef.body);
  840. CALL_SEQ(astfold_expr, expr, node_->v.ClassDef.decorator_list);
  841. break;
  842. case Return_kind:
  843. CALL_OPT(astfold_expr, expr_ty, node_->v.Return.value);
  844. break;
  845. case Delete_kind:
  846. CALL_SEQ(astfold_expr, expr, node_->v.Delete.targets);
  847. break;
  848. case Assign_kind:
  849. CALL_SEQ(astfold_expr, expr, node_->v.Assign.targets);
  850. CALL(astfold_expr, expr_ty, node_->v.Assign.value);
  851. break;
  852. case AugAssign_kind:
  853. CALL(astfold_expr, expr_ty, node_->v.AugAssign.target);
  854. CALL(astfold_expr, expr_ty, node_->v.AugAssign.value);
  855. break;
  856. case AnnAssign_kind:
  857. CALL(astfold_expr, expr_ty, node_->v.AnnAssign.target);
  858. if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) {
  859. CALL(astfold_expr, expr_ty, node_->v.AnnAssign.annotation);
  860. }
  861. CALL_OPT(astfold_expr, expr_ty, node_->v.AnnAssign.value);
  862. break;
  863. case TypeAlias_kind:
  864. CALL(astfold_expr, expr_ty, node_->v.TypeAlias.name);
  865. CALL_SEQ(astfold_type_param, type_param, node_->v.TypeAlias.type_params);
  866. CALL(astfold_expr, expr_ty, node_->v.TypeAlias.value);
  867. break;
  868. case For_kind:
  869. CALL(astfold_expr, expr_ty, node_->v.For.target);
  870. CALL(astfold_expr, expr_ty, node_->v.For.iter);
  871. CALL_SEQ(astfold_stmt, stmt, node_->v.For.body);
  872. CALL_SEQ(astfold_stmt, stmt, node_->v.For.orelse);
  873. CALL(fold_iter, expr_ty, node_->v.For.iter);
  874. break;
  875. case AsyncFor_kind:
  876. CALL(astfold_expr, expr_ty, node_->v.AsyncFor.target);
  877. CALL(astfold_expr, expr_ty, node_->v.AsyncFor.iter);
  878. CALL_SEQ(astfold_stmt, stmt, node_->v.AsyncFor.body);
  879. CALL_SEQ(astfold_stmt, stmt, node_->v.AsyncFor.orelse);
  880. break;
  881. case While_kind:
  882. CALL(astfold_expr, expr_ty, node_->v.While.test);
  883. CALL_SEQ(astfold_stmt, stmt, node_->v.While.body);
  884. CALL_SEQ(astfold_stmt, stmt, node_->v.While.orelse);
  885. break;
  886. case If_kind:
  887. CALL(astfold_expr, expr_ty, node_->v.If.test);
  888. CALL_SEQ(astfold_stmt, stmt, node_->v.If.body);
  889. CALL_SEQ(astfold_stmt, stmt, node_->v.If.orelse);
  890. break;
  891. case With_kind:
  892. CALL_SEQ(astfold_withitem, withitem, node_->v.With.items);
  893. CALL_SEQ(astfold_stmt, stmt, node_->v.With.body);
  894. break;
  895. case AsyncWith_kind:
  896. CALL_SEQ(astfold_withitem, withitem, node_->v.AsyncWith.items);
  897. CALL_SEQ(astfold_stmt, stmt, node_->v.AsyncWith.body);
  898. break;
  899. case Raise_kind:
  900. CALL_OPT(astfold_expr, expr_ty, node_->v.Raise.exc);
  901. CALL_OPT(astfold_expr, expr_ty, node_->v.Raise.cause);
  902. break;
  903. case Try_kind:
  904. CALL_SEQ(astfold_stmt, stmt, node_->v.Try.body);
  905. CALL_SEQ(astfold_excepthandler, excepthandler, node_->v.Try.handlers);
  906. CALL_SEQ(astfold_stmt, stmt, node_->v.Try.orelse);
  907. CALL_SEQ(astfold_stmt, stmt, node_->v.Try.finalbody);
  908. break;
  909. case TryStar_kind:
  910. CALL_SEQ(astfold_stmt, stmt, node_->v.TryStar.body);
  911. CALL_SEQ(astfold_excepthandler, excepthandler, node_->v.TryStar.handlers);
  912. CALL_SEQ(astfold_stmt, stmt, node_->v.TryStar.orelse);
  913. CALL_SEQ(astfold_stmt, stmt, node_->v.TryStar.finalbody);
  914. break;
  915. case Assert_kind:
  916. CALL(astfold_expr, expr_ty, node_->v.Assert.test);
  917. CALL_OPT(astfold_expr, expr_ty, node_->v.Assert.msg);
  918. break;
  919. case Expr_kind:
  920. CALL(astfold_expr, expr_ty, node_->v.Expr.value);
  921. break;
  922. case Match_kind:
  923. CALL(astfold_expr, expr_ty, node_->v.Match.subject);
  924. CALL_SEQ(astfold_match_case, match_case, node_->v.Match.cases);
  925. break;
  926. // The following statements don't contain any subexpressions to be folded
  927. case Import_kind:
  928. case ImportFrom_kind:
  929. case Global_kind:
  930. case Nonlocal_kind:
  931. case Pass_kind:
  932. case Break_kind:
  933. case Continue_kind:
  934. break;
  935. // No default case, so the compiler will emit a warning if new statement
  936. // kinds are added without being handled here
  937. }
  938. state->recursion_depth--;
  939. return 1;
  940. }
  941. static int
  942. astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
  943. {
  944. switch (node_->kind) {
  945. case ExceptHandler_kind:
  946. CALL_OPT(astfold_expr, expr_ty, node_->v.ExceptHandler.type);
  947. CALL_SEQ(astfold_stmt, stmt, node_->v.ExceptHandler.body);
  948. break;
  949. // No default case, so the compiler will emit a warning if new handler
  950. // kinds are added without being handled here
  951. }
  952. return 1;
  953. }
  954. static int
  955. astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
  956. {
  957. CALL(astfold_expr, expr_ty, node_->context_expr);
  958. CALL_OPT(astfold_expr, expr_ty, node_->optional_vars);
  959. return 1;
  960. }
  961. static int
  962. astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
  963. {
  964. // Currently, this is really only used to form complex/negative numeric
  965. // constants in MatchValue and MatchMapping nodes
  966. // We still recurse into all subexpressions and subpatterns anyway
  967. if (++state->recursion_depth > state->recursion_limit) {
  968. PyErr_SetString(PyExc_RecursionError,
  969. "maximum recursion depth exceeded during compilation");
  970. return 0;
  971. }
  972. switch (node_->kind) {
  973. case MatchValue_kind:
  974. CALL(astfold_expr, expr_ty, node_->v.MatchValue.value);
  975. break;
  976. case MatchSingleton_kind:
  977. break;
  978. case MatchSequence_kind:
  979. CALL_SEQ(astfold_pattern, pattern, node_->v.MatchSequence.patterns);
  980. break;
  981. case MatchMapping_kind:
  982. CALL_SEQ(astfold_expr, expr, node_->v.MatchMapping.keys);
  983. CALL_SEQ(astfold_pattern, pattern, node_->v.MatchMapping.patterns);
  984. break;
  985. case MatchClass_kind:
  986. CALL(astfold_expr, expr_ty, node_->v.MatchClass.cls);
  987. CALL_SEQ(astfold_pattern, pattern, node_->v.MatchClass.patterns);
  988. CALL_SEQ(astfold_pattern, pattern, node_->v.MatchClass.kwd_patterns);
  989. break;
  990. case MatchStar_kind:
  991. break;
  992. case MatchAs_kind:
  993. if (node_->v.MatchAs.pattern) {
  994. CALL(astfold_pattern, pattern_ty, node_->v.MatchAs.pattern);
  995. }
  996. break;
  997. case MatchOr_kind:
  998. CALL_SEQ(astfold_pattern, pattern, node_->v.MatchOr.patterns);
  999. break;
  1000. // No default case, so the compiler will emit a warning if new pattern
  1001. // kinds are added without being handled here
  1002. }
  1003. state->recursion_depth--;
  1004. return 1;
  1005. }
  1006. static int
  1007. astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
  1008. {
  1009. CALL(astfold_pattern, expr_ty, node_->pattern);
  1010. CALL_OPT(astfold_expr, expr_ty, node_->guard);
  1011. CALL_SEQ(astfold_stmt, stmt, node_->body);
  1012. return 1;
  1013. }
  1014. static int
  1015. astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
  1016. {
  1017. switch (node_->kind) {
  1018. case TypeVar_kind:
  1019. CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVar.bound);
  1020. break;
  1021. case ParamSpec_kind:
  1022. break;
  1023. case TypeVarTuple_kind:
  1024. break;
  1025. }
  1026. return 1;
  1027. }
  1028. #undef CALL
  1029. #undef CALL_OPT
  1030. #undef CALL_SEQ
  1031. int
  1032. _PyAST_Optimize(mod_ty mod, PyArena *arena, _PyASTOptimizeState *state)
  1033. {
  1034. PyThreadState *tstate;
  1035. int starting_recursion_depth;
  1036. /* Setup recursion depth check counters */
  1037. tstate = _PyThreadState_GET();
  1038. if (!tstate) {
  1039. return 0;
  1040. }
  1041. /* Be careful here to prevent overflow. */
  1042. int recursion_depth = C_RECURSION_LIMIT - tstate->c_recursion_remaining;
  1043. starting_recursion_depth = recursion_depth;
  1044. state->recursion_depth = starting_recursion_depth;
  1045. state->recursion_limit = C_RECURSION_LIMIT;
  1046. int ret = astfold_mod(mod, arena, state);
  1047. assert(ret || PyErr_Occurred());
  1048. /* Check that the recursion depth counting balanced correctly */
  1049. if (ret && state->recursion_depth != starting_recursion_depth) {
  1050. PyErr_Format(PyExc_SystemError,
  1051. "AST optimizer recursion depth mismatch (before=%d, after=%d)",
  1052. starting_recursion_depth, state->recursion_depth);
  1053. return 0;
  1054. }
  1055. return ret;
  1056. }