blob: 0d78cc511641b56c6b34b87f50378b899f7d83ce [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file includes functions to transform a concrete syntax tree (CST) to
Benjamin Peterson832bfe22011-08-09 16:15:04 -05003 * an abstract syntax tree (AST). The main function is PyAST_FromNode().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004 *
5 */
6#include "Python.h"
7#include "Python-ast.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#include "node.h"
9#include "ast.h"
10#include "token.h"
Ɓukasz Langae7c566c2017-09-06 17:27:58 -070011#include "pythonrun.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000012
13#include <assert.h>
Serhiy Storchakaddbce132017-11-15 17:39:37 +020014#include <stdbool.h>
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015
Benjamin Peterson832bfe22011-08-09 16:15:04 -050016static int validate_stmts(asdl_seq *);
17static int validate_exprs(asdl_seq *, expr_context_ty, int);
18static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
19static int validate_stmt(stmt_ty);
20static int validate_expr(expr_ty, expr_context_ty);
21
22static int
23validate_comprehension(asdl_seq *gens)
24{
Victor Stinner4d73ae72018-11-22 14:45:16 +010025 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050026 if (!asdl_seq_LEN(gens)) {
27 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
28 return 0;
29 }
30 for (i = 0; i < asdl_seq_LEN(gens); i++) {
31 comprehension_ty comp = asdl_seq_GET(gens, i);
32 if (!validate_expr(comp->target, Store) ||
33 !validate_expr(comp->iter, Load) ||
34 !validate_exprs(comp->ifs, Load, 0))
35 return 0;
36 }
37 return 1;
38}
39
40static int
41validate_slice(slice_ty slice)
42{
43 switch (slice->kind) {
44 case Slice_kind:
45 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
46 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
47 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
48 case ExtSlice_kind: {
Victor Stinner4d73ae72018-11-22 14:45:16 +010049 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050050 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
51 return 0;
52 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
53 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
54 return 0;
55 return 1;
56 }
57 case Index_kind:
58 return validate_expr(slice->v.Index.value, Load);
59 default:
60 PyErr_SetString(PyExc_SystemError, "unknown slice node");
61 return 0;
62 }
63}
64
65static int
66validate_keywords(asdl_seq *keywords)
67{
Victor Stinner4d73ae72018-11-22 14:45:16 +010068 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050069 for (i = 0; i < asdl_seq_LEN(keywords); i++)
70 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
71 return 0;
72 return 1;
73}
74
75static int
76validate_args(asdl_seq *args)
77{
Victor Stinner4d73ae72018-11-22 14:45:16 +010078 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050079 for (i = 0; i < asdl_seq_LEN(args); i++) {
80 arg_ty arg = asdl_seq_GET(args, i);
81 if (arg->annotation && !validate_expr(arg->annotation, Load))
82 return 0;
83 }
84 return 1;
85}
86
87static const char *
88expr_context_name(expr_context_ty ctx)
89{
90 switch (ctx) {
91 case Load:
92 return "Load";
93 case Store:
94 return "Store";
95 case Del:
96 return "Del";
97 case AugLoad:
98 return "AugLoad";
99 case AugStore:
100 return "AugStore";
101 case Param:
102 return "Param";
103 default:
Barry Warsawb2e57942017-09-14 18:13:16 -0700104 Py_UNREACHABLE();
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500105 }
106}
107
108static int
109validate_arguments(arguments_ty args)
110{
111 if (!validate_args(args->args))
112 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700113 if (args->vararg && args->vararg->annotation
114 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500115 return 0;
116 }
117 if (!validate_args(args->kwonlyargs))
118 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100119 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700120 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500121 return 0;
122 }
123 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
124 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
125 return 0;
126 }
127 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
128 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
129 "kw_defaults on arguments");
130 return 0;
131 }
132 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
133}
134
135static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100136validate_constant(PyObject *value)
137{
138 if (value == Py_None || value == Py_Ellipsis)
139 return 1;
140
141 if (PyLong_CheckExact(value)
142 || PyFloat_CheckExact(value)
143 || PyComplex_CheckExact(value)
144 || PyBool_Check(value)
145 || PyUnicode_CheckExact(value)
146 || PyBytes_CheckExact(value))
147 return 1;
148
149 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
150 PyObject *it;
151
152 it = PyObject_GetIter(value);
153 if (it == NULL)
154 return 0;
155
156 while (1) {
157 PyObject *item = PyIter_Next(it);
158 if (item == NULL) {
159 if (PyErr_Occurred()) {
160 Py_DECREF(it);
161 return 0;
162 }
163 break;
164 }
165
166 if (!validate_constant(item)) {
167 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100168 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100169 return 0;
170 }
Victor Stinner726f6902016-01-27 00:11:47 +0100171 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100172 }
173
174 Py_DECREF(it);
175 return 1;
176 }
177
178 return 0;
179}
180
181static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500182validate_expr(expr_ty exp, expr_context_ty ctx)
183{
184 int check_ctx = 1;
185 expr_context_ty actual_ctx;
186
187 /* First check expression context. */
188 switch (exp->kind) {
189 case Attribute_kind:
190 actual_ctx = exp->v.Attribute.ctx;
191 break;
192 case Subscript_kind:
193 actual_ctx = exp->v.Subscript.ctx;
194 break;
195 case Starred_kind:
196 actual_ctx = exp->v.Starred.ctx;
197 break;
198 case Name_kind:
199 actual_ctx = exp->v.Name.ctx;
200 break;
201 case List_kind:
202 actual_ctx = exp->v.List.ctx;
203 break;
204 case Tuple_kind:
205 actual_ctx = exp->v.Tuple.ctx;
206 break;
207 default:
208 if (ctx != Load) {
209 PyErr_Format(PyExc_ValueError, "expression which can't be "
210 "assigned to in %s context", expr_context_name(ctx));
211 return 0;
212 }
213 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100214 /* set actual_ctx to prevent gcc warning */
215 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500216 }
217 if (check_ctx && actual_ctx != ctx) {
218 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
219 expr_context_name(ctx), expr_context_name(actual_ctx));
220 return 0;
221 }
222
223 /* Now validate expression. */
224 switch (exp->kind) {
225 case BoolOp_kind:
226 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
227 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
228 return 0;
229 }
230 return validate_exprs(exp->v.BoolOp.values, Load, 0);
231 case BinOp_kind:
232 return validate_expr(exp->v.BinOp.left, Load) &&
233 validate_expr(exp->v.BinOp.right, Load);
234 case UnaryOp_kind:
235 return validate_expr(exp->v.UnaryOp.operand, Load);
236 case Lambda_kind:
237 return validate_arguments(exp->v.Lambda.args) &&
238 validate_expr(exp->v.Lambda.body, Load);
239 case IfExp_kind:
240 return validate_expr(exp->v.IfExp.test, Load) &&
241 validate_expr(exp->v.IfExp.body, Load) &&
242 validate_expr(exp->v.IfExp.orelse, Load);
243 case Dict_kind:
244 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
245 PyErr_SetString(PyExc_ValueError,
246 "Dict doesn't have the same number of keys as values");
247 return 0;
248 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400249 /* null_ok=1 for keys expressions to allow dict unpacking to work in
250 dict literals, i.e. ``{**{a:b}}`` */
251 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
252 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500253 case Set_kind:
254 return validate_exprs(exp->v.Set.elts, Load, 0);
255#define COMP(NAME) \
256 case NAME ## _kind: \
257 return validate_comprehension(exp->v.NAME.generators) && \
258 validate_expr(exp->v.NAME.elt, Load);
259 COMP(ListComp)
260 COMP(SetComp)
261 COMP(GeneratorExp)
262#undef COMP
263 case DictComp_kind:
264 return validate_comprehension(exp->v.DictComp.generators) &&
265 validate_expr(exp->v.DictComp.key, Load) &&
266 validate_expr(exp->v.DictComp.value, Load);
267 case Yield_kind:
268 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500269 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000270 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400271 case Await_kind:
272 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500273 case Compare_kind:
274 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
275 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
276 return 0;
277 }
278 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
279 asdl_seq_LEN(exp->v.Compare.ops)) {
280 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
281 "of comparators and operands");
282 return 0;
283 }
284 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
285 validate_expr(exp->v.Compare.left, Load);
286 case Call_kind:
287 return validate_expr(exp->v.Call.func, Load) &&
288 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400289 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100290 case Constant_kind:
291 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100292 PyErr_Format(PyExc_TypeError,
293 "got an invalid type in Constant: %s",
294 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100295 return 0;
296 }
297 return 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400298 case JoinedStr_kind:
299 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
300 case FormattedValue_kind:
301 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
302 return 0;
303 if (exp->v.FormattedValue.format_spec)
304 return validate_expr(exp->v.FormattedValue.format_spec, Load);
305 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500306 case Attribute_kind:
307 return validate_expr(exp->v.Attribute.value, Load);
308 case Subscript_kind:
309 return validate_slice(exp->v.Subscript.slice) &&
310 validate_expr(exp->v.Subscript.value, Load);
311 case Starred_kind:
312 return validate_expr(exp->v.Starred.value, ctx);
313 case List_kind:
314 return validate_exprs(exp->v.List.elts, ctx, 0);
315 case Tuple_kind:
316 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300317 /* This last case doesn't have any checking. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500318 case Name_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500319 return 1;
320 default:
321 PyErr_SetString(PyExc_SystemError, "unexpected expression");
322 return 0;
323 }
324}
325
326static int
327validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
328{
329 if (asdl_seq_LEN(seq))
330 return 1;
331 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
332 return 0;
333}
334
335static int
336validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
337{
338 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
339 validate_exprs(targets, ctx, 0);
340}
341
342static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300343validate_body(asdl_seq *body, const char *owner)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500344{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300345 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500346}
347
348static int
349validate_stmt(stmt_ty stmt)
350{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100351 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500352 switch (stmt->kind) {
353 case FunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300354 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500355 validate_arguments(stmt->v.FunctionDef.args) &&
356 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
357 (!stmt->v.FunctionDef.returns ||
358 validate_expr(stmt->v.FunctionDef.returns, Load));
359 case ClassDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300360 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500361 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
362 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400363 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500364 case Return_kind:
365 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
366 case Delete_kind:
367 return validate_assignlist(stmt->v.Delete.targets, Del);
368 case Assign_kind:
369 return validate_assignlist(stmt->v.Assign.targets, Store) &&
370 validate_expr(stmt->v.Assign.value, Load);
371 case AugAssign_kind:
372 return validate_expr(stmt->v.AugAssign.target, Store) &&
373 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700374 case AnnAssign_kind:
375 if (stmt->v.AnnAssign.target->kind != Name_kind &&
376 stmt->v.AnnAssign.simple) {
377 PyErr_SetString(PyExc_TypeError,
378 "AnnAssign with simple non-Name target");
379 return 0;
380 }
381 return validate_expr(stmt->v.AnnAssign.target, Store) &&
382 (!stmt->v.AnnAssign.value ||
383 validate_expr(stmt->v.AnnAssign.value, Load)) &&
384 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500385 case For_kind:
386 return validate_expr(stmt->v.For.target, Store) &&
387 validate_expr(stmt->v.For.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300388 validate_body(stmt->v.For.body, "For") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500389 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400390 case AsyncFor_kind:
391 return validate_expr(stmt->v.AsyncFor.target, Store) &&
392 validate_expr(stmt->v.AsyncFor.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300393 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400394 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500395 case While_kind:
396 return validate_expr(stmt->v.While.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300397 validate_body(stmt->v.While.body, "While") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500398 validate_stmts(stmt->v.While.orelse);
399 case If_kind:
400 return validate_expr(stmt->v.If.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300401 validate_body(stmt->v.If.body, "If") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500402 validate_stmts(stmt->v.If.orelse);
403 case With_kind:
404 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
405 return 0;
406 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
407 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
408 if (!validate_expr(item->context_expr, Load) ||
409 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
410 return 0;
411 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300412 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400413 case AsyncWith_kind:
414 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
415 return 0;
416 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
417 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
418 if (!validate_expr(item->context_expr, Load) ||
419 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
420 return 0;
421 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300422 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500423 case Raise_kind:
424 if (stmt->v.Raise.exc) {
425 return validate_expr(stmt->v.Raise.exc, Load) &&
426 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
427 }
428 if (stmt->v.Raise.cause) {
429 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
430 return 0;
431 }
432 return 1;
433 case Try_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300434 if (!validate_body(stmt->v.Try.body, "Try"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500435 return 0;
436 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
437 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
438 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
439 return 0;
440 }
441 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
442 asdl_seq_LEN(stmt->v.Try.orelse)) {
443 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
444 return 0;
445 }
446 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
447 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
448 if ((handler->v.ExceptHandler.type &&
449 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300450 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500451 return 0;
452 }
453 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
454 validate_stmts(stmt->v.Try.finalbody)) &&
455 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
456 validate_stmts(stmt->v.Try.orelse));
457 case Assert_kind:
458 return validate_expr(stmt->v.Assert.test, Load) &&
459 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
460 case Import_kind:
461 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
462 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300463 if (stmt->v.ImportFrom.level < 0) {
464 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500465 return 0;
466 }
467 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
468 case Global_kind:
469 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
470 case Nonlocal_kind:
471 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
472 case Expr_kind:
473 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400474 case AsyncFunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300475 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400476 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
477 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
478 (!stmt->v.AsyncFunctionDef.returns ||
479 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500480 case Pass_kind:
481 case Break_kind:
482 case Continue_kind:
483 return 1;
484 default:
485 PyErr_SetString(PyExc_SystemError, "unexpected statement");
486 return 0;
487 }
488}
489
490static int
491validate_stmts(asdl_seq *seq)
492{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100493 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500494 for (i = 0; i < asdl_seq_LEN(seq); i++) {
495 stmt_ty stmt = asdl_seq_GET(seq, i);
496 if (stmt) {
497 if (!validate_stmt(stmt))
498 return 0;
499 }
500 else {
501 PyErr_SetString(PyExc_ValueError,
502 "None disallowed in statement list");
503 return 0;
504 }
505 }
506 return 1;
507}
508
509static int
510validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
511{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100512 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500513 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
514 expr_ty expr = asdl_seq_GET(exprs, i);
515 if (expr) {
516 if (!validate_expr(expr, ctx))
517 return 0;
518 }
519 else if (!null_ok) {
520 PyErr_SetString(PyExc_ValueError,
521 "None disallowed in expression list");
522 return 0;
523 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100524
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500525 }
526 return 1;
527}
528
529int
530PyAST_Validate(mod_ty mod)
531{
532 int res = 0;
533
534 switch (mod->kind) {
535 case Module_kind:
536 res = validate_stmts(mod->v.Module.body);
537 break;
538 case Interactive_kind:
539 res = validate_stmts(mod->v.Interactive.body);
540 break;
541 case Expression_kind:
542 res = validate_expr(mod->v.Expression.body, Load);
543 break;
544 case Suite_kind:
545 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
546 break;
547 default:
548 PyErr_SetString(PyExc_SystemError, "impossible module node");
549 res = 0;
550 break;
551 }
552 return res;
553}
554
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500555/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500556#include "grammar.h"
557#include "parsetok.h"
558#include "graminit.h"
559
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000560/* Data structure used internally */
561struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400562 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200563 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500564 PyObject *c_normalize; /* Normalization function from unicodedata. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000565};
566
567static asdl_seq *seq_for_testlist(struct compiling *, const node *);
568static expr_ty ast_for_expr(struct compiling *, const node *);
569static stmt_ty ast_for_stmt(struct compiling *, const node *);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300570static asdl_seq *ast_for_suite(struct compiling *c, const node *n);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000571static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
572 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000573static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000574static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575
guoci90fc8982018-09-11 17:45:45 -0400576static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
577static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
Yury Selivanov75445082015-05-11 22:57:16 -0400578
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579/* Note different signature for ast_for_call */
Serhiy Storchakaddbce132017-11-15 17:39:37 +0200580static expr_ty ast_for_call(struct compiling *, const node *, expr_ty, bool);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000582static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400583static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584
Nick Coghlan650f0d02007-04-15 12:05:43 +0000585#define COMP_GENEXP 0
586#define COMP_LISTCOMP 1
587#define COMP_SETCOMP 2
588
Benjamin Peterson55e00432012-01-16 17:22:31 -0500589static int
590init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000591{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500592 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
593 if (!m)
594 return 0;
595 c->c_normalize = PyObject_GetAttrString(m, "normalize");
596 Py_DECREF(m);
597 if (!c->c_normalize)
598 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500599 return 1;
600}
601
602static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400603new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500604{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400605 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500606 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000607 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500608 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500609 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000610 /* Check whether there are non-ASCII characters in the
611 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500612 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200613 PyObject *id2;
Oren Milman7dc46d82017-09-30 20:16:24 +0300614 _Py_IDENTIFIER(NFKC);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500615 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500616 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200617 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500618 }
Oren Milman7dc46d82017-09-30 20:16:24 +0300619 PyObject *form = _PyUnicode_FromId(&PyId_NFKC);
620 if (form == NULL) {
621 Py_DECREF(id);
622 return NULL;
623 }
624 PyObject *args[2] = {form, id};
625 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500626 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200627 if (!id2)
628 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300629 if (!PyUnicode_Check(id2)) {
630 PyErr_Format(PyExc_TypeError,
631 "unicodedata.normalize() must return a string, not "
632 "%.200s",
633 Py_TYPE(id2)->tp_name);
634 Py_DECREF(id2);
635 return NULL;
636 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200637 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000638 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000639 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200640 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
641 Py_DECREF(id);
642 return NULL;
643 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000644 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645}
646
Benjamin Peterson55e00432012-01-16 17:22:31 -0500647#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649static int
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200650ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400652 PyObject *value, *errstr, *loc, *tmp;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200653 va_list va;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200655 va_start(va, errmsg);
656 errstr = PyUnicode_FromFormatV(errmsg, va);
657 va_end(va);
658 if (!errstr) {
659 return 0;
660 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200661 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000662 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000663 Py_INCREF(Py_None);
664 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000665 }
Ammar Askar025eb982018-09-24 17:12:49 -0400666 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200667 if (!tmp) {
668 Py_DECREF(errstr);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400669 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000670 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000671 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672 Py_DECREF(errstr);
673 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400674 if (value) {
675 PyErr_SetObject(PyExc_SyntaxError, value);
676 Py_DECREF(value);
677 }
678 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679}
680
681/* num_stmts() returns number of contained statements.
682
683 Use this routine to determine how big a sequence is needed for
684 the statements in a parse tree. Its raison d'etre is this bit of
685 grammar:
686
687 stmt: simple_stmt | compound_stmt
688 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
689
690 A simple_stmt can contain multiple small_stmt elements joined
691 by semicolons. If the arg is a simple_stmt, the number of
692 small_stmt elements is returned.
693*/
694
695static int
696num_stmts(const node *n)
697{
698 int i, l;
699 node *ch;
700
701 switch (TYPE(n)) {
702 case single_input:
703 if (TYPE(CHILD(n, 0)) == NEWLINE)
704 return 0;
705 else
706 return num_stmts(CHILD(n, 0));
707 case file_input:
708 l = 0;
709 for (i = 0; i < NCH(n); i++) {
710 ch = CHILD(n, i);
711 if (TYPE(ch) == stmt)
712 l += num_stmts(ch);
713 }
714 return l;
715 case stmt:
716 return num_stmts(CHILD(n, 0));
717 case compound_stmt:
718 return 1;
719 case simple_stmt:
720 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
721 case suite:
722 if (NCH(n) == 1)
723 return num_stmts(CHILD(n, 0));
724 else {
725 l = 0;
726 for (i = 2; i < (NCH(n) - 1); i++)
727 l += num_stmts(CHILD(n, i));
728 return l;
729 }
730 default: {
731 char buf[128];
732
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000733 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000734 TYPE(n), NCH(n));
735 Py_FatalError(buf);
736 }
737 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700738 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000739}
740
741/* Transform the CST rooted at node * to the appropriate AST
742*/
743
744mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200745PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
746 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000748 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749 asdl_seq *stmts = NULL;
750 stmt_ty s;
751 node *ch;
752 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500753 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400755 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200756 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400757 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800758 c.c_normalize = NULL;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800759
760 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000762
Jeremy Hyltona8293132006-02-28 17:58:27 +0000763 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764 switch (TYPE(n)) {
765 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200766 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500768 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769 for (i = 0; i < NCH(n) - 1; i++) {
770 ch = CHILD(n, i);
771 if (TYPE(ch) == NEWLINE)
772 continue;
773 REQ(ch, stmt);
774 num = num_stmts(ch);
775 if (num == 1) {
776 s = ast_for_stmt(&c, ch);
777 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500778 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000779 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780 }
781 else {
782 ch = CHILD(ch, 0);
783 REQ(ch, simple_stmt);
784 for (j = 0; j < num; j++) {
785 s = ast_for_stmt(&c, CHILD(ch, j * 2));
786 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500787 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000788 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 }
790 }
791 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300792 res = Module(stmts, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500793 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794 case eval_input: {
795 expr_ty testlist_ast;
796
Nick Coghlan650f0d02007-04-15 12:05:43 +0000797 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000798 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500800 goto out;
801 res = Expression(testlist_ast, arena);
802 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000803 }
804 case single_input:
805 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200806 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500808 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000809 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
810 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000811 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500812 goto out;
813 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814 }
815 else {
816 n = CHILD(n, 0);
817 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200818 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500820 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000822 s = ast_for_stmt(&c, n);
823 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500824 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825 asdl_seq_SET(stmts, 0, s);
826 }
827 else {
828 /* Only a simple_stmt can contain multiple statements. */
829 REQ(n, simple_stmt);
830 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 if (TYPE(CHILD(n, i)) == NEWLINE)
832 break;
833 s = ast_for_stmt(&c, CHILD(n, i));
834 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500835 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836 asdl_seq_SET(stmts, i / 2, s);
837 }
838 }
839
Benjamin Peterson55e00432012-01-16 17:22:31 -0500840 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500842 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000844 PyErr_Format(PyExc_SystemError,
845 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500846 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000847 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500848 out:
849 if (c.c_normalize) {
850 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500851 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500852 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853}
854
Victor Stinner14e461d2013-08-26 22:28:21 +0200855mod_ty
856PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
857 PyArena *arena)
858{
859 mod_ty mod;
860 PyObject *filename;
861 filename = PyUnicode_DecodeFSDefault(filename_str);
862 if (filename == NULL)
863 return NULL;
864 mod = PyAST_FromNodeObject(n, flags, filename, arena);
865 Py_DECREF(filename);
866 return mod;
867
868}
869
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
871*/
872
873static operator_ty
874get_operator(const node *n)
875{
876 switch (TYPE(n)) {
877 case VBAR:
878 return BitOr;
879 case CIRCUMFLEX:
880 return BitXor;
881 case AMPER:
882 return BitAnd;
883 case LEFTSHIFT:
884 return LShift;
885 case RIGHTSHIFT:
886 return RShift;
887 case PLUS:
888 return Add;
889 case MINUS:
890 return Sub;
891 case STAR:
892 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400893 case AT:
894 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000895 case SLASH:
896 return Div;
897 case DOUBLESLASH:
898 return FloorDiv;
899 case PERCENT:
900 return Mod;
901 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000902 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000903 }
904}
905
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200906static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000907 "None",
908 "True",
909 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200910 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +0000911 NULL,
912};
913
914static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400915forbidden_name(struct compiling *c, identifier name, const node *n,
916 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000917{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000918 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200919 const char * const *p = FORBIDDEN;
920 if (!full_checks) {
921 /* In most cases, the parser will protect True, False, and None
922 from being assign to. */
923 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000924 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200925 for (; *p; p++) {
926 if (_PyUnicode_EqualToASCIIString(name, *p)) {
927 ast_error(c, n, "cannot assign to %U", name);
928 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +0000929 }
930 }
931 return 0;
932}
933
Jeremy Hyltona8293132006-02-28 17:58:27 +0000934/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000935
936 Only sets context for expr kinds that "can appear in assignment context"
937 (according to ../Parser/Python.asdl). For other expr kinds, it sets
938 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939*/
940
941static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000942set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943{
944 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000945 /* If a particular expression type can't be used for assign / delete,
946 set expr_name to its name and an error message will be generated.
947 */
948 const char* expr_name = NULL;
949
950 /* The ast defines augmented store and load contexts, but the
951 implementation here doesn't actually use them. The code may be
952 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000953 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000954 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000955 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000956 */
957 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958
959 switch (e->kind) {
960 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000961 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400962 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000963 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000964 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000966 e->v.Subscript.ctx = ctx;
967 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000968 case Starred_kind:
969 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000970 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +0000971 return 0;
972 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000973 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000974 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -0500975 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +0000976 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000977 }
978 e->v.Name.ctx = ctx;
979 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000981 e->v.List.ctx = ctx;
982 s = e->v.List.elts;
983 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +0300985 e->v.Tuple.ctx = ctx;
986 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000987 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000988 case Lambda_kind:
989 expr_name = "lambda";
990 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000992 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +0000993 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000994 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000996 case UnaryOp_kind:
997 expr_name = "operator";
998 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001000 expr_name = "generator expression";
1001 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001002 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001003 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001004 expr_name = "yield expression";
1005 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001006 case Await_kind:
1007 expr_name = "await expression";
1008 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001009 case ListComp_kind:
1010 expr_name = "list comprehension";
1011 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001012 case SetComp_kind:
1013 expr_name = "set comprehension";
1014 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001015 case DictComp_kind:
1016 expr_name = "dict comprehension";
1017 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001018 case Dict_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001019 expr_name = "dict display";
1020 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001021 case Set_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001022 expr_name = "set display";
1023 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001024 case JoinedStr_kind:
1025 case FormattedValue_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001026 expr_name = "f-string expression";
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001027 break;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001028 case Constant_kind: {
1029 PyObject *value = e->v.Constant.value;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001030 if (value == Py_None || value == Py_False || value == Py_True
1031 || value == Py_Ellipsis)
1032 {
1033 return ast_error(c, n, "cannot %s %R",
1034 ctx == Store ? "assign to" : "delete",
1035 value);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001036 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001037 expr_name = "literal";
Benjamin Peterson442f2092012-12-06 17:41:04 -05001038 break;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001039 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001040 case Compare_kind:
1041 expr_name = "comparison";
1042 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001043 case IfExp_kind:
1044 expr_name = "conditional expression";
1045 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001046 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 PyErr_Format(PyExc_SystemError,
1048 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001049 e->kind, e->lineno);
1050 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001051 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001052 /* Check for error string set by switch */
1053 if (expr_name) {
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001054 return ast_error(c, n, "cannot %s %s",
1055 ctx == Store ? "assign to" : "delete",
1056 expr_name);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001057 }
1058
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061 */
1062 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001063 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001064
Thomas Wouters89f507f2006-12-13 04:49:30 +00001065 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001066 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001067 return 0;
1068 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001069 }
1070 return 1;
1071}
1072
1073static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001074ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075{
1076 REQ(n, augassign);
1077 n = CHILD(n, 0);
1078 switch (STR(n)[0]) {
1079 case '+':
1080 return Add;
1081 case '-':
1082 return Sub;
1083 case '/':
1084 if (STR(n)[1] == '/')
1085 return FloorDiv;
1086 else
1087 return Div;
1088 case '%':
1089 return Mod;
1090 case '<':
1091 return LShift;
1092 case '>':
1093 return RShift;
1094 case '&':
1095 return BitAnd;
1096 case '^':
1097 return BitXor;
1098 case '|':
1099 return BitOr;
1100 case '*':
1101 if (STR(n)[1] == '*')
1102 return Pow;
1103 else
1104 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001105 case '@':
1106 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001108 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001109 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110 }
1111}
1112
1113static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001114ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001115{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001116 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117 |'is' 'not'
1118 */
1119 REQ(n, comp_op);
1120 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001121 n = CHILD(n, 0);
1122 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123 case LESS:
1124 return Lt;
1125 case GREATER:
1126 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001127 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128 return Eq;
1129 case LESSEQUAL:
1130 return LtE;
1131 case GREATEREQUAL:
1132 return GtE;
1133 case NOTEQUAL:
1134 return NotEq;
1135 case NAME:
1136 if (strcmp(STR(n), "in") == 0)
1137 return In;
1138 if (strcmp(STR(n), "is") == 0)
1139 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001140 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001142 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001144 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001145 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146 }
1147 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001148 /* handle "not in" and "is not" */
1149 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 case NAME:
1151 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1152 return NotIn;
1153 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1154 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001155 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001157 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001159 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001160 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161 }
Neal Norwitz79792652005-11-14 04:25:03 +00001162 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001164 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165}
1166
1167static asdl_seq *
1168seq_for_testlist(struct compiling *c, const node *n)
1169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001171 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1172 */
Armin Rigo31441302005-10-21 12:57:31 +00001173 asdl_seq *seq;
1174 expr_ty expression;
1175 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001176 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001178 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179 if (!seq)
1180 return NULL;
1181
1182 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001184 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185
Benjamin Peterson4905e802009-09-27 02:43:28 +00001186 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001187 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189
1190 assert(i / 2 < seq->size);
1191 asdl_seq_SET(seq, i / 2, expression);
1192 }
1193 return seq;
1194}
1195
Neal Norwitzc1505362006-12-28 06:47:50 +00001196static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001197ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001198{
1199 identifier name;
1200 expr_ty annotation = NULL;
1201 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001202 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001203
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001204 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001205 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001206 name = NEW_IDENTIFIER(ch);
1207 if (!name)
1208 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001209 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001210 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001211
1212 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1213 annotation = ast_for_expr(c, CHILD(n, 2));
1214 if (!annotation)
1215 return NULL;
1216 }
1217
Victor Stinnerc106c682015-11-06 17:01:48 +01001218 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001219 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001220 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001221 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222}
1223
Guido van Rossum4f72a782006-10-27 23:31:49 +00001224/* returns -1 if failed to handle keyword only arguments
1225 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001226 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001227 ^^^
1228 start pointing here
1229 */
1230static int
1231handle_keywordonly_args(struct compiling *c, const node *n, int start,
1232 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1233{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001234 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001235 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001236 expr_ty expression, annotation;
1237 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001238 int i = start;
1239 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001240
1241 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001242 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001243 return -1;
1244 }
1245 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001246 while (i < NCH(n)) {
1247 ch = CHILD(n, i);
1248 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001249 case vfpdef:
1250 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001251 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001252 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001253 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001254 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001255 asdl_seq_SET(kwdefaults, j, expression);
1256 i += 2; /* '=' and test */
1257 }
1258 else { /* setting NULL if no default value exists */
1259 asdl_seq_SET(kwdefaults, j, NULL);
1260 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001261 if (NCH(ch) == 3) {
1262 /* ch is NAME ':' test */
1263 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001264 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001265 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001266 }
1267 else {
1268 annotation = NULL;
1269 }
1270 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001271 argname = NEW_IDENTIFIER(ch);
1272 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001273 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001274 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001275 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001276 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1277 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001278 if (!arg)
1279 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001280 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001281 i += 2; /* the name and the comma */
1282 break;
1283 case DOUBLESTAR:
1284 return i;
1285 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001286 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001287 goto error;
1288 }
1289 }
1290 return i;
1291 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001293}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001294
Jeremy Hyltona8293132006-02-28 17:58:27 +00001295/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296
1297static arguments_ty
1298ast_for_arguments(struct compiling *c, const node *n)
1299{
Neal Norwitzc1505362006-12-28 06:47:50 +00001300 /* This function handles both typedargslist (function definition)
1301 and varargslist (lambda definition).
1302
1303 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001304 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1305 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1306 | '**' tfpdef [',']]]
1307 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1308 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001309 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001310 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1311 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1312 | '**' vfpdef [',']]]
1313 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1314 | '**' vfpdef [',']
1315 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001316 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001317
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001319 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1320 int nposdefaults = 0, found_default = 0;
1321 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001322 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001323 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001324 node *ch;
1325
1326 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001327 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001328 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001329 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001331 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001332
Jeremy Hyltone921e022008-07-17 16:37:17 +00001333 /* First count the number of positional args & defaults. The
1334 variable i is the loop index for this for loop and the next.
1335 The next loop picks up where the first leaves off.
1336 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001338 ch = CHILD(n, i);
1339 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001340 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001341 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001342 if (i < NCH(n) && /* skip argument following star */
1343 (TYPE(CHILD(n, i)) == tfpdef ||
1344 TYPE(CHILD(n, i)) == vfpdef)) {
1345 i++;
1346 }
1347 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001348 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001349 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001350 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001351 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001354 defaults for keyword only args */
1355 for ( ; i < NCH(n); ++i) {
1356 ch = CHILD(n, i);
1357 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001358 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001359 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001360 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001361 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001362 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001363 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001364 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001365 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001366 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001368 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001369 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001370 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001372 since we set NULL as default for keyword only argument w/o default
1373 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001374 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001375 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001376 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001377 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001378
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001379 /* tfpdef: NAME [':' test]
1380 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381 */
1382 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001383 j = 0; /* index for defaults */
1384 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001386 ch = CHILD(n, i);
1387 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001388 case tfpdef:
1389 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001390 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1391 anything other than EQUAL or a comma? */
1392 /* XXX Should NCH(n) check be made a separate check? */
1393 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001394 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1395 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001396 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001397 assert(posdefaults != NULL);
1398 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001399 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001400 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001401 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001402 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001403 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001404 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001405 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001406 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001407 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001408 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001409 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001410 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001411 i += 2; /* the name and the comma */
1412 break;
1413 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001414 if (i+1 >= NCH(n) ||
1415 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001416 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001417 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001418 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001419 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001420 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001421 if (TYPE(ch) == COMMA) {
1422 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001423 i += 2; /* now follows keyword only arguments */
1424 res = handle_keywordonly_args(c, n, i,
1425 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001426 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001427 i = res; /* res has new position to process */
1428 }
1429 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001430 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001431 if (!vararg)
1432 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001433
Guido van Rossum4f72a782006-10-27 23:31:49 +00001434 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001435 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1436 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001437 int res = 0;
1438 res = handle_keywordonly_args(c, n, i,
1439 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001440 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001441 i = res; /* res has new position to process */
1442 }
1443 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444 break;
1445 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001446 ch = CHILD(n, i+1); /* tfpdef */
1447 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001448 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001449 if (!kwarg)
1450 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451 i += 3;
1452 break;
1453 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001454 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455 "unexpected node in varargslist: %d @ %d",
1456 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001457 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001458 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001460 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461}
1462
1463static expr_ty
1464ast_for_dotted_name(struct compiling *c, const node *n)
1465{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001466 expr_ty e;
1467 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001468 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469 int i;
1470
1471 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001472
1473 lineno = LINENO(n);
1474 col_offset = n->n_col_offset;
1475
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476 id = NEW_IDENTIFIER(CHILD(n, 0));
1477 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001478 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001479 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001481 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482
1483 for (i = 2; i < NCH(n); i+=2) {
1484 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001485 if (!id)
1486 return NULL;
1487 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1488 if (!e)
1489 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490 }
1491
1492 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493}
1494
1495static expr_ty
1496ast_for_decorator(struct compiling *c, const node *n)
1497{
1498 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1499 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001500 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001503 REQ(CHILD(n, 0), AT);
1504 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1507 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001508 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001511 d = name_expr;
1512 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513 }
1514 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001515 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001516 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001517 if (!d)
1518 return NULL;
1519 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520 }
1521 else {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02001522 d = ast_for_call(c, CHILD(n, 3), name_expr, true);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001523 if (!d)
1524 return NULL;
1525 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001526 }
1527
1528 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529}
1530
1531static asdl_seq*
1532ast_for_decorators(struct compiling *c, const node *n)
1533{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001534 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001535 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001539 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540 if (!decorator_seq)
1541 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001544 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001545 if (!d)
1546 return NULL;
1547 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548 }
1549 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001550}
1551
1552static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001553ast_for_funcdef_impl(struct compiling *c, const node *n0,
1554 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001555{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001556 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
guoci90fc8982018-09-11 17:45:45 -04001557 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001558 identifier name;
1559 arguments_ty args;
1560 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001561 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001562 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001563
1564 REQ(n, funcdef);
1565
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566 name = NEW_IDENTIFIER(CHILD(n, name_i));
1567 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001568 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001569 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001570 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001571 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1572 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001573 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001574 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1575 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1576 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001577 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001578 name_i += 2;
1579 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001580 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001582 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583
Yury Selivanov75445082015-05-11 22:57:16 -04001584 if (is_async)
1585 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07001586 LINENO(n0), n0->n_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001587 else
1588 return FunctionDef(name, args, body, decorator_seq, returns,
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001589 LINENO(n), n->n_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001590}
1591
1592static stmt_ty
1593ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1594{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001595 /* async_funcdef: 'async' funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001596 REQ(n, async_funcdef);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001597 REQ(CHILD(n, 0), NAME);
1598 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001599 REQ(CHILD(n, 1), funcdef);
1600
guoci90fc8982018-09-11 17:45:45 -04001601 return ast_for_funcdef_impl(c, n, decorator_seq,
1602 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001603}
1604
1605static stmt_ty
1606ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1607{
1608 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1609 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001610 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001611}
1612
1613
1614static stmt_ty
1615ast_for_async_stmt(struct compiling *c, const node *n)
1616{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001617 /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001618 REQ(n, async_stmt);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001619 REQ(CHILD(n, 0), NAME);
1620 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001621
1622 switch (TYPE(CHILD(n, 1))) {
1623 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001624 return ast_for_funcdef_impl(c, n, NULL,
1625 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001626 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001627 return ast_for_with_stmt(c, n,
1628 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001629
1630 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001631 return ast_for_for_stmt(c, n,
1632 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001633
1634 default:
1635 PyErr_Format(PyExc_SystemError,
1636 "invalid async stament: %s",
1637 STR(CHILD(n, 1)));
1638 return NULL;
1639 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001640}
1641
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001642static stmt_ty
1643ast_for_decorated(struct compiling *c, const node *n)
1644{
Yury Selivanov75445082015-05-11 22:57:16 -04001645 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001646 stmt_ty thing = NULL;
1647 asdl_seq *decorator_seq = NULL;
1648
1649 REQ(n, decorated);
1650
1651 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1652 if (!decorator_seq)
1653 return NULL;
1654
1655 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001656 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001658
1659 if (TYPE(CHILD(n, 1)) == funcdef) {
1660 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1661 } else if (TYPE(CHILD(n, 1)) == classdef) {
1662 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001663 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1664 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001665 }
1666 return thing;
1667}
1668
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669static expr_ty
1670ast_for_lambdef(struct compiling *c, const node *n)
1671{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001672 /* lambdef: 'lambda' [varargslist] ':' test
1673 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674 arguments_ty args;
1675 expr_ty expression;
1676
1677 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001678 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001679 if (!args)
1680 return NULL;
1681 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001682 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684 }
1685 else {
1686 args = ast_for_arguments(c, CHILD(n, 1));
1687 if (!args)
1688 return NULL;
1689 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001690 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692 }
1693
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001694 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695}
1696
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001697static expr_ty
1698ast_for_ifexpr(struct compiling *c, const node *n)
1699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001701 expr_ty expression, body, orelse;
1702
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001703 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001704 body = ast_for_expr(c, CHILD(n, 0));
1705 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001706 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001707 expression = ast_for_expr(c, CHILD(n, 2));
1708 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001709 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001710 orelse = ast_for_expr(c, CHILD(n, 4));
1711 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001712 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001713 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1714 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001715}
1716
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001718 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001719
Nick Coghlan650f0d02007-04-15 12:05:43 +00001720 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001721*/
1722
1723static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001724count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001726 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001727
Guido van Rossumd8faa362007-04-27 19:54:29 +00001728 count_comp_for:
1729 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001730 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001731 if (NCH(n) == 2) {
1732 REQ(CHILD(n, 0), NAME);
1733 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1734 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001735 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001736 else if (NCH(n) == 1) {
1737 n = CHILD(n, 0);
1738 }
1739 else {
1740 goto error;
1741 }
1742 if (NCH(n) == (5)) {
1743 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001744 }
1745 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001746 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001747 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001748 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001749 REQ(n, comp_iter);
1750 n = CHILD(n, 0);
1751 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001752 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001753 else if (TYPE(n) == comp_if) {
1754 if (NCH(n) == 3) {
1755 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001756 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001757 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001758 else
1759 return n_fors;
1760 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001761
Jelle Zijlstraac317702017-10-05 20:24:46 -07001762 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001763 /* Should never be reached */
1764 PyErr_SetString(PyExc_SystemError,
1765 "logic error in count_comp_fors");
1766 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767}
1768
Nick Coghlan650f0d02007-04-15 12:05:43 +00001769/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770
Nick Coghlan650f0d02007-04-15 12:05:43 +00001771 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001772*/
1773
1774static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001775count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001776{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001777 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001778
Guido van Rossumd8faa362007-04-27 19:54:29 +00001779 while (1) {
1780 REQ(n, comp_iter);
1781 if (TYPE(CHILD(n, 0)) == comp_for)
1782 return n_ifs;
1783 n = CHILD(n, 0);
1784 REQ(n, comp_if);
1785 n_ifs++;
1786 if (NCH(n) == 2)
1787 return n_ifs;
1788 n = CHILD(n, 2);
1789 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790}
1791
Guido van Rossum992d4a32007-07-11 13:09:30 +00001792static asdl_seq *
1793ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001795 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001796 asdl_seq *comps;
1797
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001798 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001799 if (n_fors == -1)
1800 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001801
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001802 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001803 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001804 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001805
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001807 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001808 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001809 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001810 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001811 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001812 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813
Guido van Rossum992d4a32007-07-11 13:09:30 +00001814 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815
Jelle Zijlstraac317702017-10-05 20:24:46 -07001816 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001817 is_async = 1;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001818 REQ(CHILD(n, 0), NAME);
1819 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1820 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001821 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001822 else {
1823 sync_n = CHILD(n, 0);
1824 }
1825 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001826
Jelle Zijlstraac317702017-10-05 20:24:46 -07001827 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001828 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001829 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001831 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001832 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001833 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001834
Thomas Wouters89f507f2006-12-13 04:49:30 +00001835 /* Check the # of children rather than the length of t, since
1836 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001837 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001838 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001839 comp = comprehension(first, expression, NULL,
1840 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841 else
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001842 comp = comprehension(Tuple(t, Store, first->lineno,
1843 first->col_offset, c->c_arena),
1844 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001845 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001847
Jelle Zijlstraac317702017-10-05 20:24:46 -07001848 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001849 int j, n_ifs;
1850 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851
Jelle Zijlstraac317702017-10-05 20:24:46 -07001852 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001853 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001854 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001855 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001856
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001857 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001858 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001859 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001860
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001862 REQ(n, comp_iter);
1863 n = CHILD(n, 0);
1864 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865
Guido van Rossum992d4a32007-07-11 13:09:30 +00001866 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001867 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001868 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001869 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001870 if (NCH(n) == 3)
1871 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001872 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001873 /* on exit, must guarantee that n is a comp_for */
1874 if (TYPE(n) == comp_iter)
1875 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001876 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001877 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001878 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001879 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001880 return comps;
1881}
1882
1883static expr_ty
1884ast_for_itercomp(struct compiling *c, const node *n, int type)
1885{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001886 /* testlist_comp: (test|star_expr)
1887 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001888 expr_ty elt;
1889 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001890 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891
Guido van Rossum992d4a32007-07-11 13:09:30 +00001892 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001894 ch = CHILD(n, 0);
1895 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001896 if (!elt)
1897 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001898 if (elt->kind == Starred_kind) {
1899 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1900 return NULL;
1901 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902
Guido van Rossum992d4a32007-07-11 13:09:30 +00001903 comps = ast_for_comprehension(c, CHILD(n, 1));
1904 if (!comps)
1905 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001906
1907 if (type == COMP_GENEXP)
1908 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1909 else if (type == COMP_LISTCOMP)
1910 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1911 else if (type == COMP_SETCOMP)
1912 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1913 else
1914 /* Should never happen */
1915 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001916}
1917
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001918/* Fills in the key, value pair corresponding to the dict element. In case
1919 * of an unpacking, key is NULL. *i is advanced by the number of ast
1920 * elements. Iff successful, nonzero is returned.
1921 */
1922static int
1923ast_for_dictelement(struct compiling *c, const node *n, int *i,
1924 expr_ty *key, expr_ty *value)
1925{
1926 expr_ty expression;
1927 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1928 assert(NCH(n) - *i >= 2);
1929
1930 expression = ast_for_expr(c, CHILD(n, *i + 1));
1931 if (!expression)
1932 return 0;
1933 *key = NULL;
1934 *value = expression;
1935
1936 *i += 2;
1937 }
1938 else {
1939 assert(NCH(n) - *i >= 3);
1940
1941 expression = ast_for_expr(c, CHILD(n, *i));
1942 if (!expression)
1943 return 0;
1944 *key = expression;
1945
1946 REQ(CHILD(n, *i + 1), COLON);
1947
1948 expression = ast_for_expr(c, CHILD(n, *i + 2));
1949 if (!expression)
1950 return 0;
1951 *value = expression;
1952
1953 *i += 3;
1954 }
1955 return 1;
1956}
1957
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001959ast_for_dictcomp(struct compiling *c, const node *n)
1960{
1961 expr_ty key, value;
1962 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001963 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001965 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001966 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001967 assert(key);
1968 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001970 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001971 if (!comps)
1972 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973
Guido van Rossum992d4a32007-07-11 13:09:30 +00001974 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1975}
1976
1977static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001978ast_for_dictdisplay(struct compiling *c, const node *n)
1979{
1980 int i;
1981 int j;
1982 int size;
1983 asdl_seq *keys, *values;
1984
1985 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
1986 keys = _Py_asdl_seq_new(size, c->c_arena);
1987 if (!keys)
1988 return NULL;
1989
1990 values = _Py_asdl_seq_new(size, c->c_arena);
1991 if (!values)
1992 return NULL;
1993
1994 j = 0;
1995 for (i = 0; i < NCH(n); i++) {
1996 expr_ty key, value;
1997
1998 if (!ast_for_dictelement(c, n, &i, &key, &value))
1999 return NULL;
2000 asdl_seq_SET(keys, j, key);
2001 asdl_seq_SET(values, j, value);
2002
2003 j++;
2004 }
2005 keys->size = j;
2006 values->size = j;
2007 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2008}
2009
2010static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002011ast_for_genexp(struct compiling *c, const node *n)
2012{
2013 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002014 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002015}
2016
2017static expr_ty
2018ast_for_listcomp(struct compiling *c, const node *n)
2019{
2020 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002021 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002022}
2023
2024static expr_ty
2025ast_for_setcomp(struct compiling *c, const node *n)
2026{
2027 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002028 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002029}
2030
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002031static expr_ty
2032ast_for_setdisplay(struct compiling *c, const node *n)
2033{
2034 int i;
2035 int size;
2036 asdl_seq *elts;
2037
2038 assert(TYPE(n) == (dictorsetmaker));
2039 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2040 elts = _Py_asdl_seq_new(size, c->c_arena);
2041 if (!elts)
2042 return NULL;
2043 for (i = 0; i < NCH(n); i += 2) {
2044 expr_ty expression;
2045 expression = ast_for_expr(c, CHILD(n, i));
2046 if (!expression)
2047 return NULL;
2048 asdl_seq_SET(elts, i / 2, expression);
2049 }
2050 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2051}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002052
2053static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054ast_for_atom(struct compiling *c, const node *n)
2055{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002056 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2057 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002058 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002059 */
2060 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002062 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002063 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002064 PyObject *name;
2065 const char *s = STR(ch);
2066 size_t len = strlen(s);
2067 if (len >= 4 && len <= 5) {
2068 if (!strcmp(s, "None"))
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002069 return Constant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002070 if (!strcmp(s, "True"))
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002071 return Constant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002072 if (!strcmp(s, "False"))
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002073 return Constant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002074 }
2075 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002076 if (!name)
2077 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002078 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002079 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2080 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002082 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002083 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002084 const char *errtype = NULL;
2085 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2086 errtype = "unicode error";
2087 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2088 errtype = "value error";
2089 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002090 PyObject *type, *value, *tback, *errstr;
2091 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002092 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002093 if (errstr) {
2094 ast_error(c, n, "(%s) %U", errtype, errstr);
2095 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002096 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002097 else {
2098 PyErr_Clear();
2099 ast_error(c, n, "(%s) unknown error", errtype);
2100 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002101 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002102 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002103 Py_XDECREF(tback);
2104 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002105 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002106 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002107 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002108 }
2109 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002110 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002111 if (!pynum)
2112 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002113
Victor Stinner43d81952013-07-17 00:57:58 +02002114 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2115 Py_DECREF(pynum);
2116 return NULL;
2117 }
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002118 return Constant(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119 }
Georg Brandldde00282007-03-18 19:01:53 +00002120 case ELLIPSIS: /* Ellipsis */
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002121 return Constant(Py_Ellipsis, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002122 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002123 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124
Thomas Wouters89f507f2006-12-13 04:49:30 +00002125 if (TYPE(ch) == RPAR)
2126 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127
Thomas Wouters89f507f2006-12-13 04:49:30 +00002128 if (TYPE(ch) == yield_expr)
2129 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002132 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002133 return ast_for_genexp(c, ch);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002134
Nick Coghlan650f0d02007-04-15 12:05:43 +00002135 return ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002136 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002137 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138
Thomas Wouters89f507f2006-12-13 04:49:30 +00002139 if (TYPE(ch) == RSQB)
2140 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141
Nick Coghlan650f0d02007-04-15 12:05:43 +00002142 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002143 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2144 asdl_seq *elts = seq_for_testlist(c, ch);
2145 if (!elts)
2146 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002147
Thomas Wouters89f507f2006-12-13 04:49:30 +00002148 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2149 }
2150 else
2151 return ast_for_listcomp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002153 /* dictorsetmaker: ( ((test ':' test | '**' test)
2154 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2155 * ((test | '*' test)
2156 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002157 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002158 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002159 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002160 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002161 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002162 }
2163 else {
2164 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2165 if (NCH(ch) == 1 ||
2166 (NCH(ch) > 1 &&
2167 TYPE(CHILD(ch, 1)) == COMMA)) {
2168 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002169 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002170 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002171 else if (NCH(ch) > 1 &&
2172 TYPE(CHILD(ch, 1)) == comp_for) {
2173 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002174 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002175 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002176 else if (NCH(ch) > 3 - is_dict &&
2177 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2178 /* It's a dictionary comprehension. */
2179 if (is_dict) {
2180 ast_error(c, n, "dict unpacking cannot be used in "
2181 "dict comprehension");
2182 return NULL;
2183 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002184 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002185 }
2186 else {
2187 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002188 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002189 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002190 if (res) {
2191 res->lineno = LINENO(n);
2192 res->col_offset = n->n_col_offset;
2193 }
2194 return res;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002195 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002196 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002197 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002198 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2199 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002200 }
2201}
2202
2203static slice_ty
2204ast_for_slice(struct compiling *c, const node *n)
2205{
2206 node *ch;
2207 expr_ty lower = NULL, upper = NULL, step = NULL;
2208
2209 REQ(n, subscript);
2210
2211 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002212 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213 sliceop: ':' [test]
2214 */
2215 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002216 if (NCH(n) == 1 && TYPE(ch) == test) {
2217 /* 'step' variable hold no significance in terms of being used over
2218 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002220 if (!step)
2221 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222
Thomas Wouters89f507f2006-12-13 04:49:30 +00002223 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002224 }
2225
2226 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002227 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002228 if (!lower)
2229 return NULL;
2230 }
2231
2232 /* If there's an upper bound it's in the second or third position. */
2233 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002234 if (NCH(n) > 1) {
2235 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002236
Thomas Wouters89f507f2006-12-13 04:49:30 +00002237 if (TYPE(n2) == test) {
2238 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239 if (!upper)
2240 return NULL;
2241 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002242 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002243 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002244 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002245
Thomas Wouters89f507f2006-12-13 04:49:30 +00002246 if (TYPE(n2) == test) {
2247 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002248 if (!upper)
2249 return NULL;
2250 }
2251 }
2252
2253 ch = CHILD(n, NCH(n) - 1);
2254 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002255 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002256 ch = CHILD(ch, 1);
2257 if (TYPE(ch) == test) {
2258 step = ast_for_expr(c, ch);
2259 if (!step)
2260 return NULL;
2261 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002262 }
2263 }
2264
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002265 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266}
2267
2268static expr_ty
2269ast_for_binop(struct compiling *c, const node *n)
2270{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002271 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002273 BinOp(BinOp(A, op, B), op, C).
2274 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275
Guido van Rossumd8faa362007-04-27 19:54:29 +00002276 int i, nops;
2277 expr_ty expr1, expr2, result;
2278 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002279
Guido van Rossumd8faa362007-04-27 19:54:29 +00002280 expr1 = ast_for_expr(c, CHILD(n, 0));
2281 if (!expr1)
2282 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283
Guido van Rossumd8faa362007-04-27 19:54:29 +00002284 expr2 = ast_for_expr(c, CHILD(n, 2));
2285 if (!expr2)
2286 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002287
Guido van Rossumd8faa362007-04-27 19:54:29 +00002288 newoperator = get_operator(CHILD(n, 1));
2289 if (!newoperator)
2290 return NULL;
2291
2292 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2293 c->c_arena);
2294 if (!result)
2295 return NULL;
2296
2297 nops = (NCH(n) - 1) / 2;
2298 for (i = 1; i < nops; i++) {
2299 expr_ty tmp_result, tmp;
2300 const node* next_oper = CHILD(n, i * 2 + 1);
2301
2302 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002303 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304 return NULL;
2305
Guido van Rossumd8faa362007-04-27 19:54:29 +00002306 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2307 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002308 return NULL;
2309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002311 LINENO(next_oper), next_oper->n_col_offset,
2312 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002314 return NULL;
2315 result = tmp_result;
2316 }
2317 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002318}
2319
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002320static expr_ty
2321ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002324 subscriptlist: subscript (',' subscript)* [',']
2325 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2326 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002327 REQ(n, trailer);
2328 if (TYPE(CHILD(n, 0)) == LPAR) {
2329 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002330 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002331 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002332 else
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002333 return ast_for_call(c, CHILD(n, 1), left_expr, true);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002334 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002335 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002336 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2337 if (!attr_id)
2338 return NULL;
2339 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002340 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002341 }
2342 else {
2343 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002344 REQ(CHILD(n, 2), RSQB);
2345 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002346 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002347 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2348 if (!slc)
2349 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002350 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2351 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002352 }
2353 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002355 by treating the sequence as a tuple literal if there are
2356 no slice features.
2357 */
Victor Stinner4d73ae72018-11-22 14:45:16 +01002358 Py_ssize_t j;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002359 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002360 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002361 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002362 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002363 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002364 if (!slices)
2365 return NULL;
2366 for (j = 0; j < NCH(n); j += 2) {
2367 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002368 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002369 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002370 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002371 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002372 asdl_seq_SET(slices, j / 2, slc);
2373 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002374 if (!simple) {
2375 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002376 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002377 }
2378 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002379 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002380 if (!elts)
2381 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002382 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2383 slc = (slice_ty)asdl_seq_GET(slices, j);
2384 assert(slc->kind == Index_kind && slc->v.Index.value);
2385 asdl_seq_SET(elts, j, slc->v.Index.value);
2386 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002387 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002388 if (!e)
2389 return NULL;
2390 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002391 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002392 }
2393 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002394}
2395
2396static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002397ast_for_factor(struct compiling *c, const node *n)
2398{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002399 expr_ty expression;
2400
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002401 expression = ast_for_expr(c, CHILD(n, 1));
2402 if (!expression)
2403 return NULL;
2404
2405 switch (TYPE(CHILD(n, 0))) {
2406 case PLUS:
2407 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2408 c->c_arena);
2409 case MINUS:
2410 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2411 c->c_arena);
2412 case TILDE:
2413 return UnaryOp(Invert, expression, LINENO(n),
2414 n->n_col_offset, c->c_arena);
2415 }
2416 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2417 TYPE(CHILD(n, 0)));
2418 return NULL;
2419}
2420
2421static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002422ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002423{
Yury Selivanov75445082015-05-11 22:57:16 -04002424 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002425 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002426
2427 REQ(n, atom_expr);
2428 nch = NCH(n);
2429
Jelle Zijlstraac317702017-10-05 20:24:46 -07002430 if (TYPE(CHILD(n, 0)) == NAME && strcmp(STR(CHILD(n, 0)), "await") == 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04002431 start = 1;
2432 assert(nch > 1);
2433 }
2434
2435 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002436 if (!e)
2437 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002438 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002439 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002440 if (start && nch == 2) {
2441 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2442 }
2443
2444 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002445 node *ch = CHILD(n, i);
2446 if (TYPE(ch) != trailer)
2447 break;
2448 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002449 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002450 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002451 tmp->lineno = e->lineno;
2452 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002453 e = tmp;
2454 }
Yury Selivanov75445082015-05-11 22:57:16 -04002455
2456 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002457 /* there was an 'await' */
Yury Selivanov75445082015-05-11 22:57:16 -04002458 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2459 }
2460 else {
2461 return e;
2462 }
2463}
2464
2465static expr_ty
2466ast_for_power(struct compiling *c, const node *n)
2467{
2468 /* power: atom trailer* ('**' factor)*
2469 */
2470 expr_ty e;
2471 REQ(n, power);
2472 e = ast_for_atom_expr(c, CHILD(n, 0));
2473 if (!e)
2474 return NULL;
2475 if (NCH(n) == 1)
2476 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002477 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2478 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002479 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002480 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002481 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002482 }
2483 return e;
2484}
2485
Guido van Rossum0368b722007-05-11 16:50:42 +00002486static expr_ty
2487ast_for_starred(struct compiling *c, const node *n)
2488{
2489 expr_ty tmp;
2490 REQ(n, star_expr);
2491
2492 tmp = ast_for_expr(c, CHILD(n, 1));
2493 if (!tmp)
2494 return NULL;
2495
2496 /* The Load context is changed later. */
2497 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2498}
2499
2500
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002501/* Do not name a variable 'expr'! Will cause a compile error.
2502*/
2503
2504static expr_ty
2505ast_for_expr(struct compiling *c, const node *n)
2506{
2507 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002508 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002509 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511 and_test: not_test ('and' not_test)*
2512 not_test: 'not' not_test | comparison
2513 comparison: expr (comp_op expr)*
2514 expr: xor_expr ('|' xor_expr)*
2515 xor_expr: and_expr ('^' and_expr)*
2516 and_expr: shift_expr ('&' shift_expr)*
2517 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2518 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002519 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002520 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002521 power: atom_expr ['**' factor]
Jelle Zijlstraac317702017-10-05 20:24:46 -07002522 atom_expr: ['await'] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002523 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002524 */
2525
2526 asdl_seq *seq;
2527 int i;
2528
2529 loop:
2530 switch (TYPE(n)) {
2531 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002532 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002533 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002534 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002536 else if (NCH(n) > 1)
2537 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002538 /* Fallthrough */
2539 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540 case and_test:
2541 if (NCH(n) == 1) {
2542 n = CHILD(n, 0);
2543 goto loop;
2544 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002545 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546 if (!seq)
2547 return NULL;
2548 for (i = 0; i < NCH(n); i += 2) {
2549 expr_ty e = ast_for_expr(c, CHILD(n, i));
2550 if (!e)
2551 return NULL;
2552 asdl_seq_SET(seq, i / 2, e);
2553 }
2554 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002555 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2556 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002557 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002558 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559 case not_test:
2560 if (NCH(n) == 1) {
2561 n = CHILD(n, 0);
2562 goto loop;
2563 }
2564 else {
2565 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2566 if (!expression)
2567 return NULL;
2568
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002569 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2570 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002571 }
2572 case comparison:
2573 if (NCH(n) == 1) {
2574 n = CHILD(n, 0);
2575 goto loop;
2576 }
2577 else {
2578 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002579 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002580 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002581 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002582 if (!ops)
2583 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002584 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002586 return NULL;
2587 }
2588 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002589 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002590
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002591 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002592 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002593 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002594 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002595
2596 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002597 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002599 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002600
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002601 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002602 asdl_seq_SET(cmps, i / 2, expression);
2603 }
2604 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002605 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002607 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002608
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002609 return Compare(expression, ops, cmps, LINENO(n),
2610 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002611 }
2612 break;
2613
Guido van Rossum0368b722007-05-11 16:50:42 +00002614 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002615 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002616 /* The next five cases all handle BinOps. The main body of code
2617 is the same in each case, but the switch turned inside out to
2618 reuse the code for each type of operator.
2619 */
2620 case expr:
2621 case xor_expr:
2622 case and_expr:
2623 case shift_expr:
2624 case arith_expr:
2625 case term:
2626 if (NCH(n) == 1) {
2627 n = CHILD(n, 0);
2628 goto loop;
2629 }
2630 return ast_for_binop(c, n);
2631 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002632 node *an = NULL;
2633 node *en = NULL;
2634 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002635 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002636 if (NCH(n) > 1)
2637 an = CHILD(n, 1); /* yield_arg */
2638 if (an) {
2639 en = CHILD(an, NCH(an) - 1);
2640 if (NCH(an) == 2) {
2641 is_from = 1;
2642 exp = ast_for_expr(c, en);
2643 }
2644 else
2645 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002646 if (!exp)
2647 return NULL;
2648 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002649 if (is_from)
2650 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2651 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002652 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002653 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654 if (NCH(n) == 1) {
2655 n = CHILD(n, 0);
2656 goto loop;
2657 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002658 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002659 case power:
2660 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002662 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663 return NULL;
2664 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002665 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002666 return NULL;
2667}
2668
2669static expr_ty
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002670ast_for_call(struct compiling *c, const node *n, expr_ty func, bool allowgen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002671{
2672 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002673 arglist: argument (',' argument)* [',']
2674 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002675 */
2676
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002677 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002678 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002679 asdl_seq *args;
2680 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002681
2682 REQ(n, arglist);
2683
2684 nargs = 0;
2685 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002686 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002687 node *ch = CHILD(n, i);
2688 if (TYPE(ch) == argument) {
2689 if (NCH(ch) == 1)
2690 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002691 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2692 nargs++;
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002693 if (!allowgen) {
2694 ast_error(c, ch, "invalid syntax");
2695 return NULL;
2696 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002697 if (NCH(n) > 1) {
2698 ast_error(c, ch, "Generator expression must be parenthesized");
2699 return NULL;
2700 }
2701 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002702 else if (TYPE(CHILD(ch, 0)) == STAR)
2703 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002704 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002705 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002706 nkeywords++;
2707 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002708 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002709
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002710 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002711 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002712 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002713 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002715 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002716
2717 nargs = 0; /* positional arguments + iterable argument unpackings */
2718 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2719 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002720 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002721 node *ch = CHILD(n, i);
2722 if (TYPE(ch) == argument) {
2723 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002724 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002725 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002726 /* a positional argument */
2727 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002728 if (ndoublestars) {
2729 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002730 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002731 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002732 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002733 else {
2734 ast_error(c, chch,
2735 "positional argument follows "
2736 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002737 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002738 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002739 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002740 e = ast_for_expr(c, chch);
2741 if (!e)
2742 return NULL;
2743 asdl_seq_SET(args, nargs++, e);
2744 }
2745 else if (TYPE(chch) == STAR) {
2746 /* an iterable argument unpacking */
2747 expr_ty starred;
2748 if (ndoublestars) {
2749 ast_error(c, chch,
2750 "iterable argument unpacking follows "
2751 "keyword argument unpacking");
2752 return NULL;
2753 }
2754 e = ast_for_expr(c, CHILD(ch, 1));
2755 if (!e)
2756 return NULL;
2757 starred = Starred(e, Load, LINENO(chch),
2758 chch->n_col_offset,
2759 c->c_arena);
2760 if (!starred)
2761 return NULL;
2762 asdl_seq_SET(args, nargs++, starred);
2763
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002764 }
2765 else if (TYPE(chch) == DOUBLESTAR) {
2766 /* a keyword argument unpacking */
2767 keyword_ty kw;
2768 i++;
2769 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002771 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002772 kw = keyword(NULL, e, c->c_arena);
2773 asdl_seq_SET(keywords, nkeywords++, kw);
2774 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002776 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002777 /* the lone generator expression */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002778 e = ast_for_genexp(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002779 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002780 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002781 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002782 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002783 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002784 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002785 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002786 identifier key, tmp;
2787 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07002789 // To remain LL(1), the grammar accepts any test (basically, any
2790 // expression) in the keyword slot of a call site. So, we need
2791 // to manually enforce that the keyword is a NAME here.
2792 static const int name_tree[] = {
2793 test,
2794 or_test,
2795 and_test,
2796 not_test,
2797 comparison,
2798 expr,
2799 xor_expr,
2800 and_expr,
2801 shift_expr,
2802 arith_expr,
2803 term,
2804 factor,
2805 power,
2806 atom_expr,
2807 atom,
2808 0,
2809 };
2810 node *expr_node = chch;
2811 for (int i = 0; name_tree[i]; i++) {
2812 if (TYPE(expr_node) != name_tree[i])
2813 break;
2814 if (NCH(expr_node) != 1)
2815 break;
2816 expr_node = CHILD(expr_node, 0);
2817 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002818 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002819 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002820 "expression cannot contain assignment, "
2821 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002822 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002823 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07002824 key = new_identifier(STR(expr_node), c);
2825 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002826 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07002828 if (forbidden_name(c, key, chch, 1)) {
2829 return NULL;
2830 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002831 for (k = 0; k < nkeywords; k++) {
2832 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002833 if (tmp && !PyUnicode_Compare(tmp, key)) {
2834 ast_error(c, chch,
2835 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002836 return NULL;
2837 }
2838 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002839 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002840 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002841 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002842 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002843 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002844 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002845 asdl_seq_SET(keywords, nkeywords++, kw);
2846 }
2847 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002848 }
2849
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002850 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002851}
2852
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002854ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002855{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002856 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002857 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002858 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002859 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002860 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002861 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002862 }
2863 else {
2864 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002865 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002866 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002868 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869 else {
2870 asdl_seq *tmp = seq_for_testlist(c, n);
2871 if (!tmp)
2872 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002873 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002874 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002875}
2876
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002877static stmt_ty
2878ast_for_expr_stmt(struct compiling *c, const node *n)
2879{
2880 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002881 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
2882 ('=' (yield_expr|testlist_star_expr))*)
2883 annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +00002884 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002885 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002886 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00002887 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002888 */
2889
2890 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002891 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892 if (!e)
2893 return NULL;
2894
Thomas Wouters89f507f2006-12-13 04:49:30 +00002895 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896 }
2897 else if (TYPE(CHILD(n, 1)) == augassign) {
2898 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002899 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002900 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002901
Thomas Wouters89f507f2006-12-13 04:49:30 +00002902 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903 if (!expr1)
2904 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002905 if(!set_context(c, expr1, Store, ch))
2906 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002907 /* set_context checks that most expressions are not the left side.
2908 Augmented assignments can only have a name, a subscript, or an
2909 attribute on the left, though, so we have to explicitly check for
2910 those. */
2911 switch (expr1->kind) {
2912 case Name_kind:
2913 case Attribute_kind:
2914 case Subscript_kind:
2915 break;
2916 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002917 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002918 return NULL;
2919 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002920
Thomas Wouters89f507f2006-12-13 04:49:30 +00002921 ch = CHILD(n, 2);
2922 if (TYPE(ch) == testlist)
2923 expr2 = ast_for_testlist(c, ch);
2924 else
2925 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002926 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002927 return NULL;
2928
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002929 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002930 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002931 return NULL;
2932
Thomas Wouters89f507f2006-12-13 04:49:30 +00002933 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002934 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002935 else if (TYPE(CHILD(n, 1)) == annassign) {
2936 expr_ty expr1, expr2, expr3;
2937 node *ch = CHILD(n, 0);
2938 node *deep, *ann = CHILD(n, 1);
2939 int simple = 1;
2940
2941 /* we keep track of parens to qualify (x) as expression not name */
2942 deep = ch;
2943 while (NCH(deep) == 1) {
2944 deep = CHILD(deep, 0);
2945 }
2946 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
2947 simple = 0;
2948 }
2949 expr1 = ast_for_testlist(c, ch);
2950 if (!expr1) {
2951 return NULL;
2952 }
2953 switch (expr1->kind) {
2954 case Name_kind:
2955 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
2956 return NULL;
2957 }
2958 expr1->v.Name.ctx = Store;
2959 break;
2960 case Attribute_kind:
2961 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
2962 return NULL;
2963 }
2964 expr1->v.Attribute.ctx = Store;
2965 break;
2966 case Subscript_kind:
2967 expr1->v.Subscript.ctx = Store;
2968 break;
2969 case List_kind:
2970 ast_error(c, ch,
2971 "only single target (not list) can be annotated");
2972 return NULL;
2973 case Tuple_kind:
2974 ast_error(c, ch,
2975 "only single target (not tuple) can be annotated");
2976 return NULL;
2977 default:
2978 ast_error(c, ch,
2979 "illegal target for annotation");
2980 return NULL;
2981 }
2982
2983 if (expr1->kind != Name_kind) {
2984 simple = 0;
2985 }
2986 ch = CHILD(ann, 1);
2987 expr2 = ast_for_expr(c, ch);
2988 if (!expr2) {
2989 return NULL;
2990 }
2991 if (NCH(ann) == 2) {
2992 return AnnAssign(expr1, expr2, NULL, simple,
2993 LINENO(n), n->n_col_offset, c->c_arena);
2994 }
2995 else {
2996 ch = CHILD(ann, 3);
2997 expr3 = ast_for_expr(c, ch);
2998 if (!expr3) {
2999 return NULL;
3000 }
3001 return AnnAssign(expr1, expr2, expr3, simple,
3002 LINENO(n), n->n_col_offset, c->c_arena);
3003 }
3004 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003005 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003006 int i;
3007 asdl_seq *targets;
3008 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003009 expr_ty expression;
3010
Thomas Wouters89f507f2006-12-13 04:49:30 +00003011 /* a normal assignment */
3012 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003013 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003014 if (!targets)
3015 return NULL;
3016 for (i = 0; i < NCH(n) - 2; i += 2) {
3017 expr_ty e;
3018 node *ch = CHILD(n, i);
3019 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003020 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003021 return NULL;
3022 }
3023 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003025 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003026
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003027 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003028 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003029 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003030
Thomas Wouters89f507f2006-12-13 04:49:30 +00003031 asdl_seq_SET(targets, i / 2, e);
3032 }
3033 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003034 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003035 expression = ast_for_testlist(c, value);
3036 else
3037 expression = ast_for_expr(c, value);
3038 if (!expression)
3039 return NULL;
3040 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003041 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003042}
3043
Benjamin Peterson78565b22009-06-28 19:19:51 +00003044
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003045static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003046ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003047{
3048 asdl_seq *seq;
3049 int i;
3050 expr_ty e;
3051
3052 REQ(n, exprlist);
3053
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003054 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003055 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003056 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003057 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003058 e = ast_for_expr(c, CHILD(n, i));
3059 if (!e)
3060 return NULL;
3061 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003062 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003063 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003064 }
3065 return seq;
3066}
3067
3068static stmt_ty
3069ast_for_del_stmt(struct compiling *c, const node *n)
3070{
3071 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003073 /* del_stmt: 'del' exprlist */
3074 REQ(n, del_stmt);
3075
3076 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3077 if (!expr_list)
3078 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003079 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003080}
3081
3082static stmt_ty
3083ast_for_flow_stmt(struct compiling *c, const node *n)
3084{
3085 /*
3086 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3087 | yield_stmt
3088 break_stmt: 'break'
3089 continue_stmt: 'continue'
3090 return_stmt: 'return' [testlist]
3091 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003092 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003093 raise_stmt: 'raise' [test [',' test [',' test]]]
3094 */
3095 node *ch;
3096
3097 REQ(n, flow_stmt);
3098 ch = CHILD(n, 0);
3099 switch (TYPE(ch)) {
3100 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003101 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003102 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003103 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003104 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003105 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3106 if (!exp)
3107 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003108 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003109 }
3110 case return_stmt:
3111 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003112 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003113 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003114 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003115 if (!expression)
3116 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003117 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003118 }
3119 case raise_stmt:
3120 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003121 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3122 else if (NCH(ch) >= 2) {
3123 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3125 if (!expression)
3126 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003127 if (NCH(ch) == 4) {
3128 cause = ast_for_expr(c, CHILD(ch, 3));
3129 if (!cause)
3130 return NULL;
3131 }
3132 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133 }
Stefan Krahf432a322017-08-21 13:09:59 +02003134 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003135 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003136 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003137 "unexpected flow_stmt: %d", TYPE(ch));
3138 return NULL;
3139 }
3140}
3141
3142static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003143alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003144{
3145 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003146 import_as_name: NAME ['as' NAME]
3147 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003148 dotted_name: NAME ('.' NAME)*
3149 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003150 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003151
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 loop:
3153 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003154 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003155 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003156 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003157 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003158 if (!name)
3159 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003160 if (NCH(n) == 3) {
3161 node *str_node = CHILD(n, 2);
3162 str = NEW_IDENTIFIER(str_node);
3163 if (!str)
3164 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003165 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003166 return NULL;
3167 }
3168 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003169 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003170 return NULL;
3171 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003172 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003173 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003174 case dotted_as_name:
3175 if (NCH(n) == 1) {
3176 n = CHILD(n, 0);
3177 goto loop;
3178 }
3179 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003180 node *asname_node = CHILD(n, 2);
3181 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003182 if (!a)
3183 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003184 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003185 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003186 if (!a->asname)
3187 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003188 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003189 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003190 return a;
3191 }
3192 break;
3193 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003194 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003195 node *name_node = CHILD(n, 0);
3196 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003197 if (!name)
3198 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003199 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003200 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003201 return alias(name, NULL, c->c_arena);
3202 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003203 else {
3204 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003205 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003206 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003207 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003209
3210 len = 0;
3211 for (i = 0; i < NCH(n); i += 2)
3212 /* length of string plus one for the dot */
3213 len += strlen(STR(CHILD(n, i))) + 1;
3214 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003215 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003216 if (!str)
3217 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003218 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003219 if (!s)
3220 return NULL;
3221 for (i = 0; i < NCH(n); i += 2) {
3222 char *sch = STR(CHILD(n, i));
3223 strcpy(s, STR(CHILD(n, i)));
3224 s += strlen(sch);
3225 *s++ = '.';
3226 }
3227 --s;
3228 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3230 PyBytes_GET_SIZE(str),
3231 NULL);
3232 Py_DECREF(str);
3233 if (!uni)
3234 return NULL;
3235 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003236 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003237 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3238 Py_DECREF(str);
3239 return NULL;
3240 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003241 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003242 }
3243 break;
3244 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003245 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003246 if (!str)
3247 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003248 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3249 Py_DECREF(str);
3250 return NULL;
3251 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003252 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003253 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003254 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003255 "unexpected import name: %d", TYPE(n));
3256 return NULL;
3257 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003258
3259 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003260 return NULL;
3261}
3262
3263static stmt_ty
3264ast_for_import_stmt(struct compiling *c, const node *n)
3265{
3266 /*
3267 import_stmt: import_name | import_from
3268 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003269 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3270 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003271 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003272 int lineno;
3273 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274 int i;
3275 asdl_seq *aliases;
3276
3277 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003278 lineno = LINENO(n);
3279 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003280 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003281 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003282 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003283 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003284 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003285 if (!aliases)
3286 return NULL;
3287 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003288 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003289 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003290 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003291 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003292 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003293 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003294 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003295 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003296 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003297 int idx, ndots = 0;
3298 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003299 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003301 /* Count the number of dots (for relative imports) and check for the
3302 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003303 for (idx = 1; idx < NCH(n); idx++) {
3304 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003305 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3306 if (!mod)
3307 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003308 idx++;
3309 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003310 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003312 ndots += 3;
3313 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003314 } else if (TYPE(CHILD(n, idx)) != DOT) {
3315 break;
3316 }
3317 ndots++;
3318 }
3319 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003320 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003321 case STAR:
3322 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003323 n = CHILD(n, idx);
3324 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003325 break;
3326 case LPAR:
3327 /* from ... import (x, y, z) */
3328 n = CHILD(n, idx + 1);
3329 n_children = NCH(n);
3330 break;
3331 case import_as_names:
3332 /* from ... import x, y, z */
3333 n = CHILD(n, idx);
3334 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003335 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003336 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003337 " surrounding parentheses");
3338 return NULL;
3339 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003340 break;
3341 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003342 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003343 return NULL;
3344 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003345
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003346 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003347 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003348 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003349
3350 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003351 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003352 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003353 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003354 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003355 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003357 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003358 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003359 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003360 if (!import_alias)
3361 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003362 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003363 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003364 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003365 if (mod != NULL)
3366 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003367 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003368 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003369 }
Neal Norwitz79792652005-11-14 04:25:03 +00003370 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003371 "unknown import statement: starts with command '%s'",
3372 STR(CHILD(n, 0)));
3373 return NULL;
3374}
3375
3376static stmt_ty
3377ast_for_global_stmt(struct compiling *c, const node *n)
3378{
3379 /* global_stmt: 'global' NAME (',' NAME)* */
3380 identifier name;
3381 asdl_seq *s;
3382 int i;
3383
3384 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003385 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003386 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003387 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003388 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003389 name = NEW_IDENTIFIER(CHILD(n, i));
3390 if (!name)
3391 return NULL;
3392 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003393 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003394 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003395}
3396
3397static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003398ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3399{
3400 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3401 identifier name;
3402 asdl_seq *s;
3403 int i;
3404
3405 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003406 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003407 if (!s)
3408 return NULL;
3409 for (i = 1; i < NCH(n); i += 2) {
3410 name = NEW_IDENTIFIER(CHILD(n, i));
3411 if (!name)
3412 return NULL;
3413 asdl_seq_SET(s, i / 2, name);
3414 }
3415 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3416}
3417
3418static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003419ast_for_assert_stmt(struct compiling *c, const node *n)
3420{
3421 /* assert_stmt: 'assert' test [',' test] */
3422 REQ(n, assert_stmt);
3423 if (NCH(n) == 2) {
3424 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3425 if (!expression)
3426 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003427 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003428 }
3429 else if (NCH(n) == 4) {
3430 expr_ty expr1, expr2;
3431
3432 expr1 = ast_for_expr(c, CHILD(n, 1));
3433 if (!expr1)
3434 return NULL;
3435 expr2 = ast_for_expr(c, CHILD(n, 3));
3436 if (!expr2)
3437 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438
Thomas Wouters89f507f2006-12-13 04:49:30 +00003439 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003440 }
Neal Norwitz79792652005-11-14 04:25:03 +00003441 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003442 "improper number of parts to 'assert' statement: %d",
3443 NCH(n));
3444 return NULL;
3445}
3446
3447static asdl_seq *
3448ast_for_suite(struct compiling *c, const node *n)
3449{
3450 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003451 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003452 stmt_ty s;
3453 int i, total, num, end, pos = 0;
3454 node *ch;
3455
3456 REQ(n, suite);
3457
3458 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003459 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003460 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003461 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003462 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003463 n = CHILD(n, 0);
3464 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003465 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003466 */
3467 end = NCH(n) - 1;
3468 if (TYPE(CHILD(n, end - 1)) == SEMI)
3469 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003470 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003471 for (i = 0; i < end; i += 2) {
3472 ch = CHILD(n, i);
3473 s = ast_for_stmt(c, ch);
3474 if (!s)
3475 return NULL;
3476 asdl_seq_SET(seq, pos++, s);
3477 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003478 }
3479 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003480 for (i = 2; i < (NCH(n) - 1); i++) {
3481 ch = CHILD(n, i);
3482 REQ(ch, stmt);
3483 num = num_stmts(ch);
3484 if (num == 1) {
3485 /* small_stmt or compound_stmt with only one child */
3486 s = ast_for_stmt(c, ch);
3487 if (!s)
3488 return NULL;
3489 asdl_seq_SET(seq, pos++, s);
3490 }
3491 else {
3492 int j;
3493 ch = CHILD(ch, 0);
3494 REQ(ch, simple_stmt);
3495 for (j = 0; j < NCH(ch); j += 2) {
3496 /* statement terminates with a semi-colon ';' */
3497 if (NCH(CHILD(ch, j)) == 0) {
3498 assert((j + 1) == NCH(ch));
3499 break;
3500 }
3501 s = ast_for_stmt(c, CHILD(ch, j));
3502 if (!s)
3503 return NULL;
3504 asdl_seq_SET(seq, pos++, s);
3505 }
3506 }
3507 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003508 }
3509 assert(pos == seq->size);
3510 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003511}
3512
3513static stmt_ty
3514ast_for_if_stmt(struct compiling *c, const node *n)
3515{
3516 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3517 ['else' ':' suite]
3518 */
3519 char *s;
3520
3521 REQ(n, if_stmt);
3522
3523 if (NCH(n) == 4) {
3524 expr_ty expression;
3525 asdl_seq *suite_seq;
3526
3527 expression = ast_for_expr(c, CHILD(n, 1));
3528 if (!expression)
3529 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003530 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003531 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003532 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533
Guido van Rossumd8faa362007-04-27 19:54:29 +00003534 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3535 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003536 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003537
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003538 s = STR(CHILD(n, 4));
3539 /* s[2], the third character in the string, will be
3540 's' for el_s_e, or
3541 'i' for el_i_f
3542 */
3543 if (s[2] == 's') {
3544 expr_ty expression;
3545 asdl_seq *seq1, *seq2;
3546
3547 expression = ast_for_expr(c, CHILD(n, 1));
3548 if (!expression)
3549 return NULL;
3550 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003551 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003552 return NULL;
3553 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003554 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003555 return NULL;
3556
Guido van Rossumd8faa362007-04-27 19:54:29 +00003557 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3558 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003559 }
3560 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003561 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003562 expr_ty expression;
3563 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003564 asdl_seq *orelse = NULL;
3565 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003566 /* must reference the child n_elif+1 since 'else' token is third,
3567 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003568 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3569 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3570 has_else = 1;
3571 n_elif -= 3;
3572 }
3573 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003574
Thomas Wouters89f507f2006-12-13 04:49:30 +00003575 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003576 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003577
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003578 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003579 if (!orelse)
3580 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003581 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003582 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003583 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003584 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3585 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003586 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003587 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3588 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003589 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003591 asdl_seq_SET(orelse, 0,
3592 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003593 LINENO(CHILD(n, NCH(n) - 6)),
3594 CHILD(n, NCH(n) - 6)->n_col_offset,
3595 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003596 /* the just-created orelse handled the last elif */
3597 n_elif--;
3598 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003599
Thomas Wouters89f507f2006-12-13 04:49:30 +00003600 for (i = 0; i < n_elif; i++) {
3601 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003602 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003603 if (!newobj)
3604 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003605 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003606 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003607 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003608 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003609 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003610 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003611
Thomas Wouters89f507f2006-12-13 04:49:30 +00003612 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003613 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003614 LINENO(CHILD(n, off)),
3615 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003616 orelse = newobj;
3617 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003618 expression = ast_for_expr(c, CHILD(n, 1));
3619 if (!expression)
3620 return NULL;
3621 suite_seq = ast_for_suite(c, CHILD(n, 3));
3622 if (!suite_seq)
3623 return NULL;
3624 return If(expression, suite_seq, orelse,
3625 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003626 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003627
3628 PyErr_Format(PyExc_SystemError,
3629 "unexpected token in 'if' statement: %s", s);
3630 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003631}
3632
3633static stmt_ty
3634ast_for_while_stmt(struct compiling *c, const node *n)
3635{
3636 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3637 REQ(n, while_stmt);
3638
3639 if (NCH(n) == 4) {
3640 expr_ty expression;
3641 asdl_seq *suite_seq;
3642
3643 expression = ast_for_expr(c, CHILD(n, 1));
3644 if (!expression)
3645 return NULL;
3646 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003647 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003648 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003649 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003650 }
3651 else if (NCH(n) == 7) {
3652 expr_ty expression;
3653 asdl_seq *seq1, *seq2;
3654
3655 expression = ast_for_expr(c, CHILD(n, 1));
3656 if (!expression)
3657 return NULL;
3658 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003659 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003660 return NULL;
3661 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003662 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003663 return NULL;
3664
Thomas Wouters89f507f2006-12-13 04:49:30 +00003665 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003666 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003667
3668 PyErr_Format(PyExc_SystemError,
3669 "wrong number of tokens for 'while' statement: %d",
3670 NCH(n));
3671 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003672}
3673
3674static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04003675ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003676{
guoci90fc8982018-09-11 17:45:45 -04003677 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00003678 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003679 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003680 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003681 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003682 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3683 REQ(n, for_stmt);
3684
3685 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003686 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003687 if (!seq)
3688 return NULL;
3689 }
3690
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003691 node_target = CHILD(n, 1);
3692 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003693 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003694 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003695 /* Check the # of children rather than the length of _target, since
3696 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003697 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003698 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003699 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003700 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003701 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003702
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003703 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003704 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003705 return NULL;
3706 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003707 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003708 return NULL;
3709
Yury Selivanov75445082015-05-11 22:57:16 -04003710 if (is_async)
3711 return AsyncFor(target, expression, suite_seq, seq,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07003712 LINENO(n0), n0->n_col_offset,
Yury Selivanov75445082015-05-11 22:57:16 -04003713 c->c_arena);
3714 else
3715 return For(target, expression, suite_seq, seq,
3716 LINENO(n), n->n_col_offset,
3717 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003718}
3719
3720static excepthandler_ty
3721ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3722{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003723 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003724 REQ(exc, except_clause);
3725 REQ(body, suite);
3726
3727 if (NCH(exc) == 1) {
3728 asdl_seq *suite_seq = ast_for_suite(c, body);
3729 if (!suite_seq)
3730 return NULL;
3731
Neal Norwitzad74aa82008-03-31 05:14:30 +00003732 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003733 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003734 }
3735 else if (NCH(exc) == 2) {
3736 expr_ty expression;
3737 asdl_seq *suite_seq;
3738
3739 expression = ast_for_expr(c, CHILD(exc, 1));
3740 if (!expression)
3741 return NULL;
3742 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003743 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003744 return NULL;
3745
Neal Norwitzad74aa82008-03-31 05:14:30 +00003746 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003747 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003748 }
3749 else if (NCH(exc) == 4) {
3750 asdl_seq *suite_seq;
3751 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003752 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003753 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003754 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003755 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003756 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003757 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003758 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003759 return NULL;
3760 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003761 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003762 return NULL;
3763
Neal Norwitzad74aa82008-03-31 05:14:30 +00003764 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003765 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003766 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003767
3768 PyErr_Format(PyExc_SystemError,
3769 "wrong number of children for 'except' clause: %d",
3770 NCH(exc));
3771 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003772}
3773
3774static stmt_ty
3775ast_for_try_stmt(struct compiling *c, const node *n)
3776{
Neal Norwitzf599f422005-12-17 21:33:47 +00003777 const int nch = NCH(n);
3778 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003779 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003780
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003781 REQ(n, try_stmt);
3782
Neal Norwitzf599f422005-12-17 21:33:47 +00003783 body = ast_for_suite(c, CHILD(n, 2));
3784 if (body == NULL)
3785 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003786
Neal Norwitzf599f422005-12-17 21:33:47 +00003787 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3788 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3789 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3790 /* we can assume it's an "else",
3791 because nch >= 9 for try-else-finally and
3792 it would otherwise have a type of except_clause */
3793 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3794 if (orelse == NULL)
3795 return NULL;
3796 n_except--;
3797 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003798
Neal Norwitzf599f422005-12-17 21:33:47 +00003799 finally = ast_for_suite(c, CHILD(n, nch - 1));
3800 if (finally == NULL)
3801 return NULL;
3802 n_except--;
3803 }
3804 else {
3805 /* we can assume it's an "else",
3806 otherwise it would have a type of except_clause */
3807 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3808 if (orelse == NULL)
3809 return NULL;
3810 n_except--;
3811 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003812 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003813 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003814 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003815 return NULL;
3816 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003817
Neal Norwitzf599f422005-12-17 21:33:47 +00003818 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003819 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003820 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003821 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003822 if (handlers == NULL)
3823 return NULL;
3824
3825 for (i = 0; i < n_except; i++) {
3826 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3827 CHILD(n, 5 + i * 3));
3828 if (!e)
3829 return NULL;
3830 asdl_seq_SET(handlers, i, e);
3831 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003832 }
3833
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003834 assert(finally != NULL || asdl_seq_LEN(handlers));
3835 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003836}
3837
Georg Brandl0c315622009-05-25 21:10:36 +00003838/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003839static withitem_ty
3840ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003841{
3842 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003843
Georg Brandl0c315622009-05-25 21:10:36 +00003844 REQ(n, with_item);
3845 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003846 if (!context_expr)
3847 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003848 if (NCH(n) == 3) {
3849 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003850
3851 if (!optional_vars) {
3852 return NULL;
3853 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003854 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003855 return NULL;
3856 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003857 }
3858
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003859 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003860}
3861
Georg Brandl0c315622009-05-25 21:10:36 +00003862/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3863static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04003864ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003865{
guoci90fc8982018-09-11 17:45:45 -04003866 const node * const n = is_async ? CHILD(n0, 1) : n0;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003867 int i, n_items;
3868 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003869
3870 REQ(n, with_stmt);
3871
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003872 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003873 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003874 if (!items)
3875 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003876 for (i = 1; i < NCH(n) - 2; i += 2) {
3877 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3878 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003879 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003880 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003881 }
3882
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003883 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3884 if (!body)
3885 return NULL;
3886
Yury Selivanov75445082015-05-11 22:57:16 -04003887 if (is_async)
Benjamin Petersond13e59c2018-09-11 15:29:57 -07003888 return AsyncWith(items, body, LINENO(n0), n0->n_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04003889 else
3890 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003891}
3892
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003893static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003894ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003895{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003896 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003897 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003898 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003899 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003900
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003901 REQ(n, classdef);
3902
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003903 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003904 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003905 if (!s)
3906 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003907 classname = NEW_IDENTIFIER(CHILD(n, 1));
3908 if (!classname)
3909 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003910 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003911 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003912 return ClassDef(classname, NULL, NULL, s, decorator_seq,
INADA Naokicb41b272017-02-23 00:31:59 +09003913 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003914 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003915
3916 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003917 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003918 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003919 return NULL;
3920 classname = NEW_IDENTIFIER(CHILD(n, 1));
3921 if (!classname)
3922 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003923 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003924 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003925 return ClassDef(classname, NULL, NULL, s, decorator_seq,
INADA Naokicb41b272017-02-23 00:31:59 +09003926 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003927 }
3928
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003929 /* class NAME '(' arglist ')' ':' suite */
3930 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003931 {
3932 PyObject *dummy_name;
3933 expr_ty dummy;
3934 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3935 if (!dummy_name)
3936 return NULL;
3937 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
Serhiy Storchakaddbce132017-11-15 17:39:37 +02003938 call = ast_for_call(c, CHILD(n, 3), dummy, false);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003939 if (!call)
3940 return NULL;
3941 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003942 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003943 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003944 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003945 classname = NEW_IDENTIFIER(CHILD(n, 1));
3946 if (!classname)
3947 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003948 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003949 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003950
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003951 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003952 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003953}
3954
3955static stmt_ty
3956ast_for_stmt(struct compiling *c, const node *n)
3957{
3958 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003959 assert(NCH(n) == 1);
3960 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003961 }
3962 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003963 assert(num_stmts(n) == 1);
3964 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003965 }
3966 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003967 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003968 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3969 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003970 */
3971 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003972 case expr_stmt:
3973 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003974 case del_stmt:
3975 return ast_for_del_stmt(c, n);
3976 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003977 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003978 case flow_stmt:
3979 return ast_for_flow_stmt(c, n);
3980 case import_stmt:
3981 return ast_for_import_stmt(c, n);
3982 case global_stmt:
3983 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003984 case nonlocal_stmt:
3985 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003986 case assert_stmt:
3987 return ast_for_assert_stmt(c, n);
3988 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003989 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003990 "unhandled small_stmt: TYPE=%d NCH=%d\n",
3991 TYPE(n), NCH(n));
3992 return NULL;
3993 }
3994 }
3995 else {
3996 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04003997 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003998 */
3999 node *ch = CHILD(n, 0);
4000 REQ(n, compound_stmt);
4001 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004002 case if_stmt:
4003 return ast_for_if_stmt(c, ch);
4004 case while_stmt:
4005 return ast_for_while_stmt(c, ch);
4006 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004007 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004008 case try_stmt:
4009 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004010 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004011 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004012 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004013 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004014 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004015 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004016 case decorated:
4017 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004018 case async_stmt:
4019 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004020 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004021 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004022 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004023 TYPE(n), NCH(n));
4024 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004025 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004026 }
4027}
4028
4029static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004030parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004031{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004032 const char *end;
4033 long x;
4034 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004035 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004036 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004037
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004038 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004039 errno = 0;
4040 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004041 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004042 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004043 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004044 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004045 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004046 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004047 }
4048 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004049 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004050 if (*end == '\0') {
4051 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004052 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004053 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004054 }
4055 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004056 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004057 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004058 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4059 if (compl.imag == -1.0 && PyErr_Occurred())
4060 return NULL;
4061 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004062 }
4063 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004064 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004065 dx = PyOS_string_to_double(s, NULL, NULL);
4066 if (dx == -1.0 && PyErr_Occurred())
4067 return NULL;
4068 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004069 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004070}
4071
4072static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004073parsenumber(struct compiling *c, const char *s)
4074{
4075 char *dup, *end;
4076 PyObject *res = NULL;
4077
4078 assert(s != NULL);
4079
4080 if (strchr(s, '_') == NULL) {
4081 return parsenumber_raw(c, s);
4082 }
4083 /* Create a duplicate without underscores. */
4084 dup = PyMem_Malloc(strlen(s) + 1);
4085 end = dup;
4086 for (; *s; s++) {
4087 if (*s != '_') {
4088 *end++ = *s;
4089 }
4090 }
4091 *end = '\0';
4092 res = parsenumber_raw(c, dup);
4093 PyMem_Free(dup);
4094 return res;
4095}
4096
4097static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004098decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004099{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004100 const char *s, *t;
4101 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004102 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4103 while (s < end && (*s & 0x80)) s++;
4104 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004105 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004106}
4107
Eric V. Smith56466482016-10-31 14:46:26 -04004108static int
4109warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004110 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004111{
4112 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4113 first_invalid_escape_char);
4114 if (msg == NULL) {
4115 return -1;
4116 }
Serhiy Storchaka65439122018-10-19 17:42:06 +03004117 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004118 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004119 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004120 {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004121 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004122 /* Replace the SyntaxWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004123 to get a more accurate error report */
4124 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004125 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004126 }
4127 Py_DECREF(msg);
4128 return -1;
4129 }
4130 Py_DECREF(msg);
4131 return 0;
4132}
4133
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004134static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004135decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4136 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004137{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004138 PyObject *v, *u;
4139 char *buf;
4140 char *p;
4141 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004142
Benjamin Peterson202803a2016-02-25 22:34:45 -08004143 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004144 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004145 return NULL;
4146 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4147 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4148 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4149 if (u == NULL)
4150 return NULL;
4151 p = buf = PyBytes_AsString(u);
4152 end = s + len;
4153 while (s < end) {
4154 if (*s == '\\') {
4155 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004156 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004157 strcpy(p, "u005c");
4158 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004159 if (s >= end)
4160 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004161 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004162 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004163 if (*s & 0x80) { /* XXX inefficient */
4164 PyObject *w;
4165 int kind;
4166 void *data;
4167 Py_ssize_t len, i;
4168 w = decode_utf8(c, &s, end);
4169 if (w == NULL) {
4170 Py_DECREF(u);
4171 return NULL;
4172 }
4173 kind = PyUnicode_KIND(w);
4174 data = PyUnicode_DATA(w);
4175 len = PyUnicode_GET_LENGTH(w);
4176 for (i = 0; i < len; i++) {
4177 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4178 sprintf(p, "\\U%08x", chr);
4179 p += 10;
4180 }
4181 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004182 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004183 Py_DECREF(w);
4184 } else {
4185 *p++ = *s++;
4186 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004187 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004188 len = p - buf;
4189 s = buf;
4190
Eric V. Smith56466482016-10-31 14:46:26 -04004191 const char *first_invalid_escape;
4192 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4193
4194 if (v != NULL && first_invalid_escape != NULL) {
4195 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4196 /* We have not decref u before because first_invalid_escape points
4197 inside u. */
4198 Py_XDECREF(u);
4199 Py_DECREF(v);
4200 return NULL;
4201 }
4202 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004203 Py_XDECREF(u);
4204 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004205}
4206
Eric V. Smith56466482016-10-31 14:46:26 -04004207static PyObject *
4208decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4209 size_t len)
4210{
4211 const char *first_invalid_escape;
4212 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4213 &first_invalid_escape);
4214 if (result == NULL)
4215 return NULL;
4216
4217 if (first_invalid_escape != NULL) {
4218 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4219 Py_DECREF(result);
4220 return NULL;
4221 }
4222 }
4223 return result;
4224}
4225
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004226/* Shift locations for the given node and all its children by adding `lineno`
4227 and `col_offset` to existing locations. */
4228static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4229{
4230 n->n_col_offset = n->n_col_offset + col_offset;
4231 for (int i = 0; i < NCH(n); ++i) {
4232 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4233 /* Shifting column offsets unnecessary if there's been newlines. */
4234 col_offset = 0;
4235 }
4236 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4237 }
4238 n->n_lineno = n->n_lineno + lineno;
4239}
4240
4241/* Fix locations for the given node and its children.
4242
4243 `parent` is the enclosing node.
4244 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004245 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004246*/
4247static void
4248fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4249{
4250 char *substr = NULL;
4251 char *start;
4252 int lines = LINENO(parent) - 1;
4253 int cols = parent->n_col_offset;
4254 /* Find the full fstring to fix location information in `n`. */
4255 while (parent && parent->n_type != STRING)
4256 parent = parent->n_child;
4257 if (parent && parent->n_str) {
4258 substr = strstr(parent->n_str, expr_str);
4259 if (substr) {
4260 start = substr;
4261 while (start > parent->n_str) {
4262 if (start[0] == '\n')
4263 break;
4264 start--;
4265 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004266 cols += (int)(substr - start);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004267 /* Fix lineno in mulitline strings. */
4268 while ((substr = strchr(substr + 1, '\n')))
4269 lines--;
4270 }
4271 }
4272 fstring_shift_node_locations(n, lines, cols);
4273}
4274
Eric V. Smith451d0e32016-09-09 21:56:20 -04004275/* Compile this expression in to an expr_ty. Add parens around the
4276 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004277static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004278fstring_compile_expr(const char *expr_start, const char *expr_end,
4279 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004280
Eric V. Smith235a6f02015-09-19 14:51:32 -04004281{
4282 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004283 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004284 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004285 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004286 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004287 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004288
Eric V. Smith1d44c412015-09-23 07:49:00 -04004289 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004290 assert(*(expr_start-1) == '{');
4291 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004292
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004293 /* If the substring is all whitespace, it's an error. We need to catch this
4294 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4295 because turning the expression '' in to '()' would go from being invalid
4296 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004297 for (s = expr_start; s != expr_end; s++) {
4298 char c = *s;
4299 /* The Python parser ignores only the following whitespace
4300 characters (\r already is converted to \n). */
4301 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004302 break;
4303 }
4304 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004305 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004306 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004307 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004308 }
4309
Eric V. Smith451d0e32016-09-09 21:56:20 -04004310 len = expr_end - expr_start;
4311 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4312 str = PyMem_RawMalloc(len + 3);
4313 if (str == NULL)
4314 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004315
Eric V. Smith451d0e32016-09-09 21:56:20 -04004316 str[0] = '(';
4317 memcpy(str+1, expr_start, len);
4318 str[len+1] = ')';
4319 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004320
4321 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004322 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4323 Py_eval_input, 0);
4324 if (!mod_n) {
4325 PyMem_RawFree(str);
4326 return NULL;
4327 }
4328 /* Reuse str to find the correct column offset. */
4329 str[0] = '{';
4330 str[len+1] = '}';
4331 fstring_fix_node_location(n, mod_n, str);
4332 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004333 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004334 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004335 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004336 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004337 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004338}
4339
4340/* Return -1 on error.
4341
4342 Return 0 if we reached the end of the literal.
4343
4344 Return 1 if we haven't reached the end of the literal, but we want
4345 the caller to process the literal up to this point. Used for
4346 doubled braces.
4347*/
4348static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004349fstring_find_literal(const char **str, const char *end, int raw,
4350 PyObject **literal, int recurse_lvl,
4351 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004352{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004353 /* Get any literal string. It ends when we hit an un-doubled left
4354 brace (which isn't part of a unicode name escape such as
4355 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004356
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004357 const char *s = *str;
4358 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004359 int result = 0;
4360
Eric V. Smith235a6f02015-09-19 14:51:32 -04004361 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004362 while (s < end) {
4363 char ch = *s++;
4364 if (!raw && ch == '\\' && s < end) {
4365 ch = *s++;
4366 if (ch == 'N') {
4367 if (s < end && *s++ == '{') {
4368 while (s < end && *s++ != '}') {
4369 }
4370 continue;
4371 }
4372 break;
4373 }
4374 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4375 return -1;
4376 }
4377 }
4378 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004379 /* Check for doubled braces, but only at the top level. If
4380 we checked at every level, then f'{0:{3}}' would fail
4381 with the two closing braces. */
4382 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004383 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004384 /* We're going to tell the caller that the literal ends
4385 here, but that they should continue scanning. But also
4386 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004387 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004388 result = 1;
4389 goto done;
4390 }
4391
4392 /* Where a single '{' is the start of a new expression, a
4393 single '}' is not allowed. */
4394 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004395 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004396 ast_error(c, n, "f-string: single '}' is not allowed");
4397 return -1;
4398 }
4399 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004400 /* We're either at a '{', which means we're starting another
4401 expression; or a '}', which means we're at the end of this
4402 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004403 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004404 break;
4405 }
4406 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004407 *str = s;
4408 assert(s <= end);
4409 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004410done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004411 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004412 if (raw)
4413 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004414 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004415 NULL, NULL);
4416 else
Eric V. Smith56466482016-10-31 14:46:26 -04004417 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004418 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004419 if (!*literal)
4420 return -1;
4421 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004422 return result;
4423}
4424
4425/* Forward declaration because parsing is recursive. */
4426static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004427fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004428 struct compiling *c, const node *n);
4429
Eric V. Smith451d0e32016-09-09 21:56:20 -04004430/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004431 expression (so it must be a '{'). Returns the FormattedValue node,
4432 which includes the expression, conversion character, and
4433 format_spec expression.
4434
4435 Note that I don't do a perfect job here: I don't make sure that a
4436 closing brace doesn't match an opening paren, for example. It
4437 doesn't need to error on all invalid expressions, just correctly
4438 find the end of all valid ones. Any errors inside the expression
4439 will be caught when we parse it later. */
4440static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004441fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004442 expr_ty *expression, struct compiling *c, const node *n)
4443{
4444 /* Return -1 on error, else 0. */
4445
Eric V. Smith451d0e32016-09-09 21:56:20 -04004446 const char *expr_start;
4447 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004448 expr_ty simple_expression;
4449 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004450 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004451
4452 /* 0 if we're not in a string, else the quote char we're trying to
4453 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004454 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004455
4456 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4457 int string_type = 0;
4458
4459 /* Keep track of nesting level for braces/parens/brackets in
4460 expressions. */
4461 Py_ssize_t nested_depth = 0;
4462
4463 /* Can only nest one level deep. */
4464 if (recurse_lvl >= 2) {
4465 ast_error(c, n, "f-string: expressions nested too deeply");
4466 return -1;
4467 }
4468
4469 /* The first char must be a left brace, or we wouldn't have gotten
4470 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004471 assert(**str == '{');
4472 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004473
Eric V. Smith451d0e32016-09-09 21:56:20 -04004474 expr_start = *str;
4475 for (; *str < end; (*str)++) {
4476 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004477
4478 /* Loop invariants. */
4479 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004480 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004481 if (quote_char)
4482 assert(string_type == 1 || string_type == 3);
4483 else
4484 assert(string_type == 0);
4485
Eric V. Smith451d0e32016-09-09 21:56:20 -04004486 ch = **str;
4487 /* Nowhere inside an expression is a backslash allowed. */
4488 if (ch == '\\') {
4489 /* Error: can't include a backslash character, inside
4490 parens or strings or not. */
4491 ast_error(c, n, "f-string expression part "
4492 "cannot include a backslash");
4493 return -1;
4494 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004495 if (quote_char) {
4496 /* We're inside a string. See if we're at the end. */
4497 /* This code needs to implement the same non-error logic
4498 as tok_get from tokenizer.c, at the letter_quote
4499 label. To actually share that code would be a
4500 nightmare. But, it's unlikely to change and is small,
4501 so duplicate it here. Note we don't need to catch all
4502 of the errors, since they'll be caught when parsing the
4503 expression. We just need to match the non-error
4504 cases. Thus we can ignore \n in single-quoted strings,
4505 for example. Or non-terminated strings. */
4506 if (ch == quote_char) {
4507 /* Does this match the string_type (single or triple
4508 quoted)? */
4509 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004510 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004511 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004512 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004513 string_type = 0;
4514 quote_char = 0;
4515 continue;
4516 }
4517 } else {
4518 /* We're at the end of a normal string. */
4519 quote_char = 0;
4520 string_type = 0;
4521 continue;
4522 }
4523 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004524 } else if (ch == '\'' || ch == '"') {
4525 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004526 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004527 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004528 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004529 } else {
4530 /* Start of a normal string. */
4531 string_type = 1;
4532 }
4533 /* Start looking for the end of the string. */
4534 quote_char = ch;
4535 } else if (ch == '[' || ch == '{' || ch == '(') {
4536 nested_depth++;
4537 } else if (nested_depth != 0 &&
4538 (ch == ']' || ch == '}' || ch == ')')) {
4539 nested_depth--;
4540 } else if (ch == '#') {
4541 /* Error: can't include a comment character, inside parens
4542 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004543 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004544 return -1;
4545 } else if (nested_depth == 0 &&
4546 (ch == '!' || ch == ':' || ch == '}')) {
4547 /* First, test for the special case of "!=". Since '=' is
4548 not an allowed conversion character, nothing is lost in
4549 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004550 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004551 /* This isn't a conversion character, just continue. */
4552 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004553 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004554 /* Normal way out of this loop. */
4555 break;
4556 } else {
4557 /* Just consume this char and loop around. */
4558 }
4559 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004560 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004561 /* If we leave this loop in a string or with mismatched parens, we
4562 don't care. We'll get a syntax error when compiling the
4563 expression. But, we can produce a better error message, so
4564 let's just do that.*/
4565 if (quote_char) {
4566 ast_error(c, n, "f-string: unterminated string");
4567 return -1;
4568 }
4569 if (nested_depth) {
4570 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4571 return -1;
4572 }
4573
Eric V. Smith451d0e32016-09-09 21:56:20 -04004574 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004575 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004576
4577 /* Compile the expression as soon as possible, so we show errors
4578 related to the expression before errors related to the
4579 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004580 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004581 if (!simple_expression)
4582 return -1;
4583
4584 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004585 if (**str == '!') {
4586 *str += 1;
4587 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004588 goto unexpected_end_of_string;
4589
Eric V. Smith451d0e32016-09-09 21:56:20 -04004590 conversion = **str;
4591 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004592
4593 /* Validate the conversion. */
4594 if (!(conversion == 's' || conversion == 'r'
4595 || conversion == 'a')) {
4596 ast_error(c, n, "f-string: invalid conversion character: "
4597 "expected 's', 'r', or 'a'");
4598 return -1;
4599 }
4600 }
4601
4602 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004603 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004604 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004605 if (**str == ':') {
4606 *str += 1;
4607 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004608 goto unexpected_end_of_string;
4609
4610 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004611 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004612 if (!format_spec)
4613 return -1;
4614 }
4615
Eric V. Smith451d0e32016-09-09 21:56:20 -04004616 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004617 goto unexpected_end_of_string;
4618
4619 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004620 assert(*str < end);
4621 assert(**str == '}');
4622 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004623
Eric V. Smith451d0e32016-09-09 21:56:20 -04004624 /* And now create the FormattedValue node that represents this
4625 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004626 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004627 format_spec, LINENO(n), n->n_col_offset,
4628 c->c_arena);
4629 if (!*expression)
4630 return -1;
4631
4632 return 0;
4633
4634unexpected_end_of_string:
4635 ast_error(c, n, "f-string: expecting '}'");
4636 return -1;
4637}
4638
4639/* Return -1 on error.
4640
4641 Return 0 if we have a literal (possible zero length) and an
4642 expression (zero length if at the end of the string.
4643
4644 Return 1 if we have a literal, but no expression, and we want the
4645 caller to call us again. This is used to deal with doubled
4646 braces.
4647
4648 When called multiple times on the string 'a{{b{0}c', this function
4649 will return:
4650
4651 1. the literal 'a{' with no expression, and a return value
4652 of 1. Despite the fact that there's no expression, the return
4653 value of 1 means we're not finished yet.
4654
4655 2. the literal 'b' and the expression '0', with a return value of
4656 0. The fact that there's an expression means we're not finished.
4657
4658 3. literal 'c' with no expression and a return value of 0. The
4659 combination of the return value of 0 with no expression means
4660 we're finished.
4661*/
4662static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004663fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4664 int recurse_lvl, PyObject **literal,
4665 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004666 struct compiling *c, const node *n)
4667{
4668 int result;
4669
4670 assert(*literal == NULL && *expression == NULL);
4671
4672 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004673 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004674 if (result < 0)
4675 goto error;
4676
4677 assert(result == 0 || result == 1);
4678
4679 if (result == 1)
4680 /* We have a literal, but don't look at the expression. */
4681 return 1;
4682
Eric V. Smith451d0e32016-09-09 21:56:20 -04004683 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004684 /* We're at the end of the string or the end of a nested
4685 f-string: no expression. The top-level error case where we
4686 expect to be at the end of the string but we're at a '}' is
4687 handled later. */
4688 return 0;
4689
4690 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004691 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004692
Eric V. Smith451d0e32016-09-09 21:56:20 -04004693 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004694 goto error;
4695
4696 return 0;
4697
4698error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004699 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004700 return -1;
4701}
4702
4703#define EXPRLIST_N_CACHED 64
4704
4705typedef struct {
4706 /* Incrementally build an array of expr_ty, so be used in an
4707 asdl_seq. Cache some small but reasonably sized number of
4708 expr_ty's, and then after that start dynamically allocating,
4709 doubling the number allocated each time. Note that the f-string
4710 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004711 Constant for the literal 'a'. So you add expr_ty's about twice as
Eric V. Smith235a6f02015-09-19 14:51:32 -04004712 fast as you add exressions in an f-string. */
4713
4714 Py_ssize_t allocated; /* Number we've allocated. */
4715 Py_ssize_t size; /* Number we've used. */
4716 expr_ty *p; /* Pointer to the memory we're actually
4717 using. Will point to 'data' until we
4718 start dynamically allocating. */
4719 expr_ty data[EXPRLIST_N_CACHED];
4720} ExprList;
4721
4722#ifdef NDEBUG
4723#define ExprList_check_invariants(l)
4724#else
4725static void
4726ExprList_check_invariants(ExprList *l)
4727{
4728 /* Check our invariants. Make sure this object is "live", and
4729 hasn't been deallocated. */
4730 assert(l->size >= 0);
4731 assert(l->p != NULL);
4732 if (l->size <= EXPRLIST_N_CACHED)
4733 assert(l->data == l->p);
4734}
4735#endif
4736
4737static void
4738ExprList_Init(ExprList *l)
4739{
4740 l->allocated = EXPRLIST_N_CACHED;
4741 l->size = 0;
4742
4743 /* Until we start allocating dynamically, p points to data. */
4744 l->p = l->data;
4745
4746 ExprList_check_invariants(l);
4747}
4748
4749static int
4750ExprList_Append(ExprList *l, expr_ty exp)
4751{
4752 ExprList_check_invariants(l);
4753 if (l->size >= l->allocated) {
4754 /* We need to alloc (or realloc) the memory. */
4755 Py_ssize_t new_size = l->allocated * 2;
4756
4757 /* See if we've ever allocated anything dynamically. */
4758 if (l->p == l->data) {
4759 Py_ssize_t i;
4760 /* We're still using the cached data. Switch to
4761 alloc-ing. */
4762 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4763 if (!l->p)
4764 return -1;
4765 /* Copy the cached data into the new buffer. */
4766 for (i = 0; i < l->size; i++)
4767 l->p[i] = l->data[i];
4768 } else {
4769 /* Just realloc. */
4770 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4771 if (!tmp) {
4772 PyMem_RawFree(l->p);
4773 l->p = NULL;
4774 return -1;
4775 }
4776 l->p = tmp;
4777 }
4778
4779 l->allocated = new_size;
4780 assert(l->allocated == 2 * l->size);
4781 }
4782
4783 l->p[l->size++] = exp;
4784
4785 ExprList_check_invariants(l);
4786 return 0;
4787}
4788
4789static void
4790ExprList_Dealloc(ExprList *l)
4791{
4792 ExprList_check_invariants(l);
4793
4794 /* If there's been an error, or we've never dynamically allocated,
4795 do nothing. */
4796 if (!l->p || l->p == l->data) {
4797 /* Do nothing. */
4798 } else {
4799 /* We have dynamically allocated. Free the memory. */
4800 PyMem_RawFree(l->p);
4801 }
4802 l->p = NULL;
4803 l->size = -1;
4804}
4805
4806static asdl_seq *
4807ExprList_Finish(ExprList *l, PyArena *arena)
4808{
4809 asdl_seq *seq;
4810
4811 ExprList_check_invariants(l);
4812
4813 /* Allocate the asdl_seq and copy the expressions in to it. */
4814 seq = _Py_asdl_seq_new(l->size, arena);
4815 if (seq) {
4816 Py_ssize_t i;
4817 for (i = 0; i < l->size; i++)
4818 asdl_seq_SET(seq, i, l->p[i]);
4819 }
4820 ExprList_Dealloc(l);
4821 return seq;
4822}
4823
4824/* The FstringParser is designed to add a mix of strings and
4825 f-strings, and concat them together as needed. Ultimately, it
4826 generates an expr_ty. */
4827typedef struct {
4828 PyObject *last_str;
4829 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004830 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004831} FstringParser;
4832
4833#ifdef NDEBUG
4834#define FstringParser_check_invariants(state)
4835#else
4836static void
4837FstringParser_check_invariants(FstringParser *state)
4838{
4839 if (state->last_str)
4840 assert(PyUnicode_CheckExact(state->last_str));
4841 ExprList_check_invariants(&state->expr_list);
4842}
4843#endif
4844
4845static void
4846FstringParser_Init(FstringParser *state)
4847{
4848 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004849 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004850 ExprList_Init(&state->expr_list);
4851 FstringParser_check_invariants(state);
4852}
4853
4854static void
4855FstringParser_Dealloc(FstringParser *state)
4856{
4857 FstringParser_check_invariants(state);
4858
4859 Py_XDECREF(state->last_str);
4860 ExprList_Dealloc(&state->expr_list);
4861}
4862
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004863/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004864static expr_ty
4865make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4866{
4867 PyObject *s = *str;
4868 *str = NULL;
4869 assert(PyUnicode_CheckExact(s));
4870 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4871 Py_DECREF(s);
4872 return NULL;
4873 }
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004874 return Constant(s, LINENO(n), n->n_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004875}
4876
4877/* Add a non-f-string (that is, a regular literal string). str is
4878 decref'd. */
4879static int
4880FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4881{
4882 FstringParser_check_invariants(state);
4883
4884 assert(PyUnicode_CheckExact(str));
4885
4886 if (PyUnicode_GET_LENGTH(str) == 0) {
4887 Py_DECREF(str);
4888 return 0;
4889 }
4890
4891 if (!state->last_str) {
4892 /* We didn't have a string before, so just remember this one. */
4893 state->last_str = str;
4894 } else {
4895 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004896 PyUnicode_AppendAndDel(&state->last_str, str);
4897 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004898 return -1;
4899 }
4900 FstringParser_check_invariants(state);
4901 return 0;
4902}
4903
Eric V. Smith451d0e32016-09-09 21:56:20 -04004904/* Parse an f-string. The f-string is in *str to end, with no
4905 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004906static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004907FstringParser_ConcatFstring(FstringParser *state, const char **str,
4908 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004909 struct compiling *c, const node *n)
4910{
4911 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004912 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004913
4914 /* Parse the f-string. */
4915 while (1) {
4916 PyObject *literal = NULL;
4917 expr_ty expression = NULL;
4918
4919 /* If there's a zero length literal in front of the
4920 expression, literal will be NULL. If we're at the end of
4921 the f-string, expression will be NULL (unless result == 1,
4922 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004923 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004924 &literal, &expression,
4925 c, n);
4926 if (result < 0)
4927 return -1;
4928
4929 /* Add the literal, if any. */
4930 if (!literal) {
4931 /* Do nothing. Just leave last_str alone (and possibly
4932 NULL). */
4933 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04004934 /* Note that the literal can be zero length, if the
4935 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004936 state->last_str = literal;
4937 literal = NULL;
4938 } else {
4939 /* We have a literal, concatenate it. */
4940 assert(PyUnicode_GET_LENGTH(literal) != 0);
4941 if (FstringParser_ConcatAndDel(state, literal) < 0)
4942 return -1;
4943 literal = NULL;
4944 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004945
4946 /* We've dealt with the literal now. It can't be leaked on further
4947 errors. */
4948 assert(literal == NULL);
4949
4950 /* See if we should just loop around to get the next literal
4951 and expression, while ignoring the expression this
4952 time. This is used for un-doubling braces, as an
4953 optimization. */
4954 if (result == 1)
4955 continue;
4956
4957 if (!expression)
4958 /* We're done with this f-string. */
4959 break;
4960
4961 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004962 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004963 if (!state->last_str) {
4964 /* Do nothing. No previous literal. */
4965 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004966 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004967 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4968 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4969 return -1;
4970 }
4971
4972 if (ExprList_Append(&state->expr_list, expression) < 0)
4973 return -1;
4974 }
4975
Eric V. Smith235a6f02015-09-19 14:51:32 -04004976 /* If recurse_lvl is zero, then we must be at the end of the
4977 string. Otherwise, we must be at a right brace. */
4978
Eric V. Smith451d0e32016-09-09 21:56:20 -04004979 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004980 ast_error(c, n, "f-string: unexpected end of string");
4981 return -1;
4982 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004983 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004984 ast_error(c, n, "f-string: expecting '}'");
4985 return -1;
4986 }
4987
4988 FstringParser_check_invariants(state);
4989 return 0;
4990}
4991
4992/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004993 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004994static expr_ty
4995FstringParser_Finish(FstringParser *state, struct compiling *c,
4996 const node *n)
4997{
4998 asdl_seq *seq;
4999
5000 FstringParser_check_invariants(state);
5001
5002 /* If we're just a constant string with no expressions, return
5003 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005004 if (!state->fmode) {
5005 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005006 if (!state->last_str) {
5007 /* Create a zero length string. */
5008 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5009 if (!state->last_str)
5010 goto error;
5011 }
5012 return make_str_node_and_del(&state->last_str, c, n);
5013 }
5014
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005015 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005016 last node in our expression list. */
5017 if (state->last_str) {
5018 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5019 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5020 goto error;
5021 }
5022 /* This has already been freed. */
5023 assert(state->last_str == NULL);
5024
5025 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5026 if (!seq)
5027 goto error;
5028
Eric V. Smith235a6f02015-09-19 14:51:32 -04005029 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
5030
5031error:
5032 FstringParser_Dealloc(state);
5033 return NULL;
5034}
5035
Eric V. Smith451d0e32016-09-09 21:56:20 -04005036/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5037 at end, parse it into an expr_ty. Return NULL on error. Adjust
5038 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005039static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005040fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005041 struct compiling *c, const node *n)
5042{
5043 FstringParser state;
5044
5045 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005046 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005047 c, n) < 0) {
5048 FstringParser_Dealloc(&state);
5049 return NULL;
5050 }
5051
5052 return FstringParser_Finish(&state, c, n);
5053}
5054
5055/* n is a Python string literal, including the bracketing quote
5056 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005057 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005058 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005059 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5060 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005061*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005062static int
5063parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5064 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005065{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005066 size_t len;
5067 const char *s = STR(n);
5068 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005069 int fmode = 0;
5070 *bytesmode = 0;
5071 *rawmode = 0;
5072 *result = NULL;
5073 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005074 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005075 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005076 if (quote == 'b' || quote == 'B') {
5077 quote = *++s;
5078 *bytesmode = 1;
5079 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005080 else if (quote == 'u' || quote == 'U') {
5081 quote = *++s;
5082 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005083 else if (quote == 'r' || quote == 'R') {
5084 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005085 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005086 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005087 else if (quote == 'f' || quote == 'F') {
5088 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005089 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005090 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005091 else {
5092 break;
5093 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005094 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005095 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005096 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005097 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005098 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005099 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005100 if (quote != '\'' && quote != '\"') {
5101 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005102 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005103 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005104 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005105 s++;
5106 len = strlen(s);
5107 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005108 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005109 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005110 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005111 }
5112 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005113 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005114 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005115 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005116 }
5117 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005118 /* A triple quoted string. We've already skipped one quote at
5119 the start and one at the end of the string. Now skip the
5120 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005121 s += 2;
5122 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005123 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005124 if (s[--len] != quote || s[--len] != quote) {
5125 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005126 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005127 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005128 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005129
Eric V. Smith451d0e32016-09-09 21:56:20 -04005130 if (fmode) {
5131 /* Just return the bytes. The caller will parse the resulting
5132 string. */
5133 *fstr = s;
5134 *fstrlen = len;
5135 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005136 }
5137
Eric V. Smith451d0e32016-09-09 21:56:20 -04005138 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005139 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005140 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005141 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005142 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005143 const char *ch;
5144 for (ch = s; *ch; ch++) {
5145 if (Py_CHARMASK(*ch) >= 0x80) {
5146 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005147 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005148 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005149 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005150 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005151 if (*rawmode)
5152 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005153 else
Eric V. Smith56466482016-10-31 14:46:26 -04005154 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005155 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005156 if (*rawmode)
5157 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005158 else
Eric V. Smith56466482016-10-31 14:46:26 -04005159 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005160 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005161 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005162}
5163
Eric V. Smith235a6f02015-09-19 14:51:32 -04005164/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5165 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005166 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005167 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005168 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005169 node if there's just an f-string (with no leading or trailing
5170 literals), or a JoinedStr node if there are multiple f-strings or
5171 any literals involved. */
5172static expr_ty
5173parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005174{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005175 int bytesmode = 0;
5176 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005177 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005178
5179 FstringParser state;
5180 FstringParser_Init(&state);
5181
5182 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005183 int this_bytesmode;
5184 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005185 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005186 const char *fstr;
5187 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005188
5189 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005190 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5191 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005192 goto error;
5193
5194 /* Check that we're not mixing bytes with unicode. */
5195 if (i != 0 && bytesmode != this_bytesmode) {
5196 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005197 /* s is NULL if the current string part is an f-string. */
5198 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005199 goto error;
5200 }
5201 bytesmode = this_bytesmode;
5202
Eric V. Smith451d0e32016-09-09 21:56:20 -04005203 if (fstr != NULL) {
5204 int result;
5205 assert(s == NULL && !bytesmode);
5206 /* This is an f-string. Parse and concatenate it. */
5207 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5208 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005209 if (result < 0)
5210 goto error;
5211 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005212 /* A string or byte string. */
5213 assert(s != NULL && fstr == NULL);
5214
Eric V. Smith451d0e32016-09-09 21:56:20 -04005215 assert(bytesmode ? PyBytes_CheckExact(s) :
5216 PyUnicode_CheckExact(s));
5217
Eric V. Smith451d0e32016-09-09 21:56:20 -04005218 if (bytesmode) {
5219 /* For bytes, concat as we go. */
5220 if (i == 0) {
5221 /* First time, just remember this value. */
5222 bytes_str = s;
5223 } else {
5224 PyBytes_ConcatAndDel(&bytes_str, s);
5225 if (!bytes_str)
5226 goto error;
5227 }
5228 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005229 /* This is a regular string. Concatenate it. */
5230 if (FstringParser_ConcatAndDel(&state, s) < 0)
5231 goto error;
5232 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005233 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005234 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005235 if (bytesmode) {
5236 /* Just return the bytes object and we're done. */
5237 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5238 goto error;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005239 return Constant(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005240 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005241
Eric V. Smith235a6f02015-09-19 14:51:32 -04005242 /* We're not a bytes string, bytes_str should never have been set. */
5243 assert(bytes_str == NULL);
5244
5245 return FstringParser_Finish(&state, c, n);
5246
5247error:
5248 Py_XDECREF(bytes_str);
5249 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005250 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005251}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005252
5253PyObject *
5254_PyAST_GetDocString(asdl_seq *body)
5255{
5256 if (!asdl_seq_LEN(body)) {
5257 return NULL;
5258 }
5259 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5260 if (st->kind != Expr_kind) {
5261 return NULL;
5262 }
5263 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005264 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5265 return e->v.Constant.value;
5266 }
5267 return NULL;
5268}