blob: 24d5843aabd290918180dd303af5473cc79d6e32 [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 Storchakab619b092018-11-27 09:40:29 +0200580static expr_ty ast_for_call(struct compiling *, const node *, expr_ty,
581 const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000582
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000583static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400584static expr_ty parsestrplus(struct compiling *, const node *n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000585
Nick Coghlan650f0d02007-04-15 12:05:43 +0000586#define COMP_GENEXP 0
587#define COMP_LISTCOMP 1
588#define COMP_SETCOMP 2
589
Benjamin Peterson55e00432012-01-16 17:22:31 -0500590static int
591init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000592{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500593 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
594 if (!m)
595 return 0;
596 c->c_normalize = PyObject_GetAttrString(m, "normalize");
597 Py_DECREF(m);
598 if (!c->c_normalize)
599 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500600 return 1;
601}
602
603static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400604new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500605{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400606 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500607 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000608 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500609 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500610 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000611 /* Check whether there are non-ASCII characters in the
612 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500613 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200614 PyObject *id2;
Oren Milman7dc46d82017-09-30 20:16:24 +0300615 _Py_IDENTIFIER(NFKC);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500616 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500617 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200618 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500619 }
Oren Milman7dc46d82017-09-30 20:16:24 +0300620 PyObject *form = _PyUnicode_FromId(&PyId_NFKC);
621 if (form == NULL) {
622 Py_DECREF(id);
623 return NULL;
624 }
625 PyObject *args[2] = {form, id};
626 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500627 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200628 if (!id2)
629 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300630 if (!PyUnicode_Check(id2)) {
631 PyErr_Format(PyExc_TypeError,
632 "unicodedata.normalize() must return a string, not "
633 "%.200s",
634 Py_TYPE(id2)->tp_name);
635 Py_DECREF(id2);
636 return NULL;
637 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200638 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000639 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000640 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200641 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
642 Py_DECREF(id);
643 return NULL;
644 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000645 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646}
647
Benjamin Peterson55e00432012-01-16 17:22:31 -0500648#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650static int
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200651ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400653 PyObject *value, *errstr, *loc, *tmp;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200654 va_list va;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200656 va_start(va, errmsg);
657 errstr = PyUnicode_FromFormatV(errmsg, va);
658 va_end(va);
659 if (!errstr) {
660 return 0;
661 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200662 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000663 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000664 Py_INCREF(Py_None);
665 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000666 }
Ammar Askar025eb982018-09-24 17:12:49 -0400667 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200668 if (!tmp) {
669 Py_DECREF(errstr);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400670 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000671 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000672 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000673 Py_DECREF(errstr);
674 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400675 if (value) {
676 PyErr_SetObject(PyExc_SyntaxError, value);
677 Py_DECREF(value);
678 }
679 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680}
681
682/* num_stmts() returns number of contained statements.
683
684 Use this routine to determine how big a sequence is needed for
685 the statements in a parse tree. Its raison d'etre is this bit of
686 grammar:
687
688 stmt: simple_stmt | compound_stmt
689 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
690
691 A simple_stmt can contain multiple small_stmt elements joined
692 by semicolons. If the arg is a simple_stmt, the number of
693 small_stmt elements is returned.
694*/
695
696static int
697num_stmts(const node *n)
698{
699 int i, l;
700 node *ch;
701
702 switch (TYPE(n)) {
703 case single_input:
704 if (TYPE(CHILD(n, 0)) == NEWLINE)
705 return 0;
706 else
707 return num_stmts(CHILD(n, 0));
708 case file_input:
709 l = 0;
710 for (i = 0; i < NCH(n); i++) {
711 ch = CHILD(n, i);
712 if (TYPE(ch) == stmt)
713 l += num_stmts(ch);
714 }
715 return l;
716 case stmt:
717 return num_stmts(CHILD(n, 0));
718 case compound_stmt:
719 return 1;
720 case simple_stmt:
721 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
722 case suite:
723 if (NCH(n) == 1)
724 return num_stmts(CHILD(n, 0));
725 else {
726 l = 0;
727 for (i = 2; i < (NCH(n) - 1); i++)
728 l += num_stmts(CHILD(n, i));
729 return l;
730 }
731 default: {
732 char buf[128];
733
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000734 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000735 TYPE(n), NCH(n));
736 Py_FatalError(buf);
737 }
738 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700739 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000740}
741
742/* Transform the CST rooted at node * to the appropriate AST
743*/
744
745mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200746PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
747 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000749 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750 asdl_seq *stmts = NULL;
751 stmt_ty s;
752 node *ch;
753 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500754 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000755
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400756 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200757 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400758 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800759 c.c_normalize = NULL;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800760
761 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000762 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763
Jeremy Hyltona8293132006-02-28 17:58:27 +0000764 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765 switch (TYPE(n)) {
766 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200767 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500769 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770 for (i = 0; i < NCH(n) - 1; i++) {
771 ch = CHILD(n, i);
772 if (TYPE(ch) == NEWLINE)
773 continue;
774 REQ(ch, stmt);
775 num = num_stmts(ch);
776 if (num == 1) {
777 s = ast_for_stmt(&c, ch);
778 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500779 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000780 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781 }
782 else {
783 ch = CHILD(ch, 0);
784 REQ(ch, simple_stmt);
785 for (j = 0; j < num; j++) {
786 s = ast_for_stmt(&c, CHILD(ch, j * 2));
787 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500788 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000789 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790 }
791 }
792 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300793 res = Module(stmts, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500794 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795 case eval_input: {
796 expr_ty testlist_ast;
797
Nick Coghlan650f0d02007-04-15 12:05:43 +0000798 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000799 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500801 goto out;
802 res = Expression(testlist_ast, arena);
803 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804 }
805 case single_input:
806 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200807 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500809 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000810 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
811 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000812 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500813 goto out;
814 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815 }
816 else {
817 n = CHILD(n, 0);
818 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200819 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500821 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000823 s = ast_for_stmt(&c, n);
824 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500825 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000826 asdl_seq_SET(stmts, 0, s);
827 }
828 else {
829 /* Only a simple_stmt can contain multiple statements. */
830 REQ(n, simple_stmt);
831 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832 if (TYPE(CHILD(n, i)) == NEWLINE)
833 break;
834 s = ast_for_stmt(&c, CHILD(n, i));
835 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500836 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 asdl_seq_SET(stmts, i / 2, s);
838 }
839 }
840
Benjamin Peterson55e00432012-01-16 17:22:31 -0500841 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500843 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000845 PyErr_Format(PyExc_SystemError,
846 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500847 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500849 out:
850 if (c.c_normalize) {
851 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500852 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500853 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854}
855
Victor Stinner14e461d2013-08-26 22:28:21 +0200856mod_ty
857PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
858 PyArena *arena)
859{
860 mod_ty mod;
861 PyObject *filename;
862 filename = PyUnicode_DecodeFSDefault(filename_str);
863 if (filename == NULL)
864 return NULL;
865 mod = PyAST_FromNodeObject(n, flags, filename, arena);
866 Py_DECREF(filename);
867 return mod;
868
869}
870
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
872*/
873
874static operator_ty
875get_operator(const node *n)
876{
877 switch (TYPE(n)) {
878 case VBAR:
879 return BitOr;
880 case CIRCUMFLEX:
881 return BitXor;
882 case AMPER:
883 return BitAnd;
884 case LEFTSHIFT:
885 return LShift;
886 case RIGHTSHIFT:
887 return RShift;
888 case PLUS:
889 return Add;
890 case MINUS:
891 return Sub;
892 case STAR:
893 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400894 case AT:
895 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000896 case SLASH:
897 return Div;
898 case DOUBLESLASH:
899 return FloorDiv;
900 case PERCENT:
901 return Mod;
902 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000903 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000904 }
905}
906
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200907static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000908 "None",
909 "True",
910 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200911 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +0000912 NULL,
913};
914
915static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400916forbidden_name(struct compiling *c, identifier name, const node *n,
917 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000918{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000919 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200920 const char * const *p = FORBIDDEN;
921 if (!full_checks) {
922 /* In most cases, the parser will protect True, False, and None
923 from being assign to. */
924 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000925 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200926 for (; *p; p++) {
927 if (_PyUnicode_EqualToASCIIString(name, *p)) {
928 ast_error(c, n, "cannot assign to %U", name);
929 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +0000930 }
931 }
932 return 0;
933}
934
Serhiy Storchakab619b092018-11-27 09:40:29 +0200935static expr_ty
936copy_location(expr_ty e, const node *n)
937{
938 if (e) {
939 e->lineno = LINENO(n);
940 e->col_offset = n->n_col_offset;
941 }
942 return e;
943}
944
Jeremy Hyltona8293132006-02-28 17:58:27 +0000945/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000946
947 Only sets context for expr kinds that "can appear in assignment context"
948 (according to ../Parser/Python.asdl). For other expr kinds, it sets
949 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950*/
951
952static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000953set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954{
955 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000956 /* If a particular expression type can't be used for assign / delete,
957 set expr_name to its name and an error message will be generated.
958 */
959 const char* expr_name = NULL;
960
961 /* The ast defines augmented store and load contexts, but the
962 implementation here doesn't actually use them. The code may be
963 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000964 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000965 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000966 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000967 */
968 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969
970 switch (e->kind) {
971 case Attribute_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000972 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400973 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000974 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000975 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976 case Subscript_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000977 e->v.Subscript.ctx = ctx;
978 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000979 case Starred_kind:
980 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000981 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +0000982 return 0;
983 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +0000985 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -0500986 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +0000987 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000988 }
989 e->v.Name.ctx = ctx;
990 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991 case List_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000992 e->v.List.ctx = ctx;
993 s = e->v.List.elts;
994 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995 case Tuple_kind:
Berker Peksag094c9c92016-05-18 08:44:29 +0300996 e->v.Tuple.ctx = ctx;
997 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000998 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000999 case Lambda_kind:
1000 expr_name = "lambda";
1001 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001002 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001003 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001004 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001005 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001007 case UnaryOp_kind:
1008 expr_name = "operator";
1009 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001011 expr_name = "generator expression";
1012 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001013 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001014 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001015 expr_name = "yield expression";
1016 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001017 case Await_kind:
1018 expr_name = "await expression";
1019 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001020 case ListComp_kind:
1021 expr_name = "list comprehension";
1022 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001023 case SetComp_kind:
1024 expr_name = "set comprehension";
1025 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001026 case DictComp_kind:
1027 expr_name = "dict comprehension";
1028 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001029 case Dict_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001030 expr_name = "dict display";
1031 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001032 case Set_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001033 expr_name = "set display";
1034 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001035 case JoinedStr_kind:
1036 case FormattedValue_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001037 expr_name = "f-string expression";
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001038 break;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001039 case Constant_kind: {
1040 PyObject *value = e->v.Constant.value;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001041 if (value == Py_None || value == Py_False || value == Py_True
1042 || value == Py_Ellipsis)
1043 {
1044 return ast_error(c, n, "cannot %s %R",
1045 ctx == Store ? "assign to" : "delete",
1046 value);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001047 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001048 expr_name = "literal";
Benjamin Peterson442f2092012-12-06 17:41:04 -05001049 break;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001050 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001051 case Compare_kind:
1052 expr_name = "comparison";
1053 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001054 case IfExp_kind:
1055 expr_name = "conditional expression";
1056 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001057 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 PyErr_Format(PyExc_SystemError,
1059 "unexpected expression in assignment %d (line %d)",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001060 e->kind, e->lineno);
1061 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001063 /* Check for error string set by switch */
1064 if (expr_name) {
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001065 return ast_error(c, n, "cannot %s %s",
1066 ctx == Store ? "assign to" : "delete",
1067 expr_name);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001068 }
1069
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001070 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072 */
1073 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001074 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075
Thomas Wouters89f507f2006-12-13 04:49:30 +00001076 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001077 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001078 return 0;
1079 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001080 }
1081 return 1;
1082}
1083
1084static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001085ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086{
1087 REQ(n, augassign);
1088 n = CHILD(n, 0);
1089 switch (STR(n)[0]) {
1090 case '+':
1091 return Add;
1092 case '-':
1093 return Sub;
1094 case '/':
1095 if (STR(n)[1] == '/')
1096 return FloorDiv;
1097 else
1098 return Div;
1099 case '%':
1100 return Mod;
1101 case '<':
1102 return LShift;
1103 case '>':
1104 return RShift;
1105 case '&':
1106 return BitAnd;
1107 case '^':
1108 return BitXor;
1109 case '|':
1110 return BitOr;
1111 case '*':
1112 if (STR(n)[1] == '*')
1113 return Pow;
1114 else
1115 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001116 case '@':
1117 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001119 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001120 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121 }
1122}
1123
1124static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001125ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001127 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128 |'is' 'not'
1129 */
1130 REQ(n, comp_op);
1131 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001132 n = CHILD(n, 0);
1133 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134 case LESS:
1135 return Lt;
1136 case GREATER:
1137 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001138 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 return Eq;
1140 case LESSEQUAL:
1141 return LtE;
1142 case GREATEREQUAL:
1143 return GtE;
1144 case NOTEQUAL:
1145 return NotEq;
1146 case NAME:
1147 if (strcmp(STR(n), "in") == 0)
1148 return In;
1149 if (strcmp(STR(n), "is") == 0)
1150 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001151 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001153 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001155 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001156 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157 }
1158 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001159 /* handle "not in" and "is not" */
1160 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161 case NAME:
1162 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1163 return NotIn;
1164 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1165 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001166 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001168 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001170 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001171 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172 }
Neal Norwitz79792652005-11-14 04:25:03 +00001173 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001175 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176}
1177
1178static asdl_seq *
1179seq_for_testlist(struct compiling *c, const node *n)
1180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001182 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1183 */
Armin Rigo31441302005-10-21 12:57:31 +00001184 asdl_seq *seq;
1185 expr_ty expression;
1186 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001187 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001189 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190 if (!seq)
1191 return NULL;
1192
1193 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 const node *ch = CHILD(n, i);
Benjamin Peterson4905e802009-09-27 02:43:28 +00001195 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196
Benjamin Peterson4905e802009-09-27 02:43:28 +00001197 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001198 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001199 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200
1201 assert(i / 2 < seq->size);
1202 asdl_seq_SET(seq, i / 2, expression);
1203 }
1204 return seq;
1205}
1206
Neal Norwitzc1505362006-12-28 06:47:50 +00001207static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001208ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001209{
1210 identifier name;
1211 expr_ty annotation = NULL;
1212 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001213 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001214
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001215 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001216 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001217 name = NEW_IDENTIFIER(ch);
1218 if (!name)
1219 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001220 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001221 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001222
1223 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1224 annotation = ast_for_expr(c, CHILD(n, 2));
1225 if (!annotation)
1226 return NULL;
1227 }
1228
Victor Stinnerc106c682015-11-06 17:01:48 +01001229 ret = arg(name, annotation, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001230 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001231 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001232 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233}
1234
Guido van Rossum4f72a782006-10-27 23:31:49 +00001235/* returns -1 if failed to handle keyword only arguments
1236 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001237 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001238 ^^^
1239 start pointing here
1240 */
1241static int
1242handle_keywordonly_args(struct compiling *c, const node *n, int start,
1243 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1244{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001245 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001246 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001247 expr_ty expression, annotation;
1248 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001249 int i = start;
1250 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001251
1252 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001253 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001254 return -1;
1255 }
1256 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001257 while (i < NCH(n)) {
1258 ch = CHILD(n, i);
1259 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001260 case vfpdef:
1261 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001262 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001263 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001264 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001265 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001266 asdl_seq_SET(kwdefaults, j, expression);
1267 i += 2; /* '=' and test */
1268 }
1269 else { /* setting NULL if no default value exists */
1270 asdl_seq_SET(kwdefaults, j, NULL);
1271 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001272 if (NCH(ch) == 3) {
1273 /* ch is NAME ':' test */
1274 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001275 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001276 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001277 }
1278 else {
1279 annotation = NULL;
1280 }
1281 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001282 argname = NEW_IDENTIFIER(ch);
1283 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001284 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001285 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001286 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001287 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
1288 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001289 if (!arg)
1290 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001291 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001292 i += 2; /* the name and the comma */
1293 break;
1294 case DOUBLESTAR:
1295 return i;
1296 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001297 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001298 goto error;
1299 }
1300 }
1301 return i;
1302 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001304}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001305
Jeremy Hyltona8293132006-02-28 17:58:27 +00001306/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307
1308static arguments_ty
1309ast_for_arguments(struct compiling *c, const node *n)
1310{
Neal Norwitzc1505362006-12-28 06:47:50 +00001311 /* This function handles both typedargslist (function definition)
1312 and varargslist (lambda definition).
1313
1314 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001315 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1316 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1317 | '**' tfpdef [',']]]
1318 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1319 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001320 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001321 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1322 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1323 | '**' vfpdef [',']]]
1324 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1325 | '**' vfpdef [',']
1326 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001327 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001328
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001330 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1331 int nposdefaults = 0, found_default = 0;
1332 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001333 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001334 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001335 node *ch;
1336
1337 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001338 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001339 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001340 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001342 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343
Jeremy Hyltone921e022008-07-17 16:37:17 +00001344 /* First count the number of positional args & defaults. The
1345 variable i is the loop index for this for loop and the next.
1346 The next loop picks up where the first leaves off.
1347 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001348 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001349 ch = CHILD(n, i);
1350 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001351 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001352 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001353 if (i < NCH(n) && /* skip argument following star */
1354 (TYPE(CHILD(n, i)) == tfpdef ||
1355 TYPE(CHILD(n, i)) == vfpdef)) {
1356 i++;
1357 }
1358 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001359 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001360 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001361 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001362 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001365 defaults for keyword only args */
1366 for ( ; i < NCH(n); ++i) {
1367 ch = CHILD(n, i);
1368 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001369 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001370 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001371 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001372 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001373 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001374 kwonlyargs = (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 (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001377 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001379 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001380 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001381 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001383 since we set NULL as default for keyword only argument w/o default
1384 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001385 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001386 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001387 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001388 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001389
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001390 /* tfpdef: NAME [':' test]
1391 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392 */
1393 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001394 j = 0; /* index for defaults */
1395 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001397 ch = CHILD(n, i);
1398 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001399 case tfpdef:
1400 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001401 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1402 anything other than EQUAL or a comma? */
1403 /* XXX Should NCH(n) check be made a separate check? */
1404 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001405 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1406 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001407 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001408 assert(posdefaults != NULL);
1409 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001411 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001413 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001414 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001415 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001416 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001417 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001418 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001419 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001420 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001421 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422 i += 2; /* the name and the comma */
1423 break;
1424 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001425 if (i+1 >= NCH(n) ||
1426 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001427 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001428 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001429 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001430 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001431 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001432 if (TYPE(ch) == COMMA) {
1433 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001434 i += 2; /* now follows keyword only arguments */
1435 res = handle_keywordonly_args(c, n, i,
1436 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001437 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001438 i = res; /* res has new position to process */
1439 }
1440 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001441 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001442 if (!vararg)
1443 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001444
Guido van Rossum4f72a782006-10-27 23:31:49 +00001445 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001446 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1447 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001448 int res = 0;
1449 res = handle_keywordonly_args(c, n, i,
1450 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001451 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001452 i = res; /* res has new position to process */
1453 }
1454 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455 break;
1456 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001457 ch = CHILD(n, i+1); /* tfpdef */
1458 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001459 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001460 if (!kwarg)
1461 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462 i += 3;
1463 break;
1464 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001465 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001466 "unexpected node in varargslist: %d @ %d",
1467 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001468 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001469 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001471 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472}
1473
1474static expr_ty
1475ast_for_dotted_name(struct compiling *c, const node *n)
1476{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001477 expr_ty e;
1478 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001479 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480 int i;
1481
1482 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001483
1484 lineno = LINENO(n);
1485 col_offset = n->n_col_offset;
1486
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487 id = NEW_IDENTIFIER(CHILD(n, 0));
1488 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001489 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001490 e = Name(id, Load, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001491 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001492 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493
1494 for (i = 2; i < NCH(n); i+=2) {
1495 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001496 if (!id)
1497 return NULL;
1498 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena);
1499 if (!e)
1500 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501 }
1502
1503 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504}
1505
1506static expr_ty
1507ast_for_decorator(struct compiling *c, const node *n)
1508{
1509 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1510 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001511 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001514 REQ(CHILD(n, 0), AT);
1515 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1518 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001519 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001522 d = name_expr;
1523 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001524 }
1525 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001526 d = Call(name_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001527 n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001528 if (!d)
1529 return NULL;
1530 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531 }
1532 else {
Serhiy Storchakab619b092018-11-27 09:40:29 +02001533 d = ast_for_call(c, CHILD(n, 3), name_expr, CHILD(n, 2));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001534 if (!d)
1535 return NULL;
1536 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537 }
1538
1539 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540}
1541
1542static asdl_seq*
1543ast_for_decorators(struct compiling *c, const node *n)
1544{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001545 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001546 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001550 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551 if (!decorator_seq)
1552 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001555 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001556 if (!d)
1557 return NULL;
1558 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559 }
1560 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001561}
1562
1563static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001564ast_for_funcdef_impl(struct compiling *c, const node *n0,
1565 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001567 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
guoci90fc8982018-09-11 17:45:45 -04001568 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001569 identifier name;
1570 arguments_ty args;
1571 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001572 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001573 int name_i = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574
1575 REQ(n, funcdef);
1576
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001577 name = NEW_IDENTIFIER(CHILD(n, name_i));
1578 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001579 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001580 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001581 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001582 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1583 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001584 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001585 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1586 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1587 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001588 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001589 name_i += 2;
1590 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001591 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001592 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001593 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594
Yury Selivanov75445082015-05-11 22:57:16 -04001595 if (is_async)
1596 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07001597 LINENO(n0), n0->n_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001598 else
1599 return FunctionDef(name, args, body, decorator_seq, returns,
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001600 LINENO(n), n->n_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001601}
1602
1603static stmt_ty
1604ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1605{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001606 /* async_funcdef: 'async' funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001607 REQ(n, async_funcdef);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001608 REQ(CHILD(n, 0), NAME);
1609 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001610 REQ(CHILD(n, 1), funcdef);
1611
guoci90fc8982018-09-11 17:45:45 -04001612 return ast_for_funcdef_impl(c, n, decorator_seq,
1613 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001614}
1615
1616static stmt_ty
1617ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1618{
1619 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1620 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001621 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001622}
1623
1624
1625static stmt_ty
1626ast_for_async_stmt(struct compiling *c, const node *n)
1627{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001628 /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001629 REQ(n, async_stmt);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001630 REQ(CHILD(n, 0), NAME);
1631 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001632
1633 switch (TYPE(CHILD(n, 1))) {
1634 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001635 return ast_for_funcdef_impl(c, n, NULL,
1636 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001637 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001638 return ast_for_with_stmt(c, n,
1639 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001640
1641 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001642 return ast_for_for_stmt(c, n,
1643 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001644
1645 default:
1646 PyErr_Format(PyExc_SystemError,
1647 "invalid async stament: %s",
1648 STR(CHILD(n, 1)));
1649 return NULL;
1650 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001651}
1652
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001653static stmt_ty
1654ast_for_decorated(struct compiling *c, const node *n)
1655{
Yury Selivanov75445082015-05-11 22:57:16 -04001656 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001657 stmt_ty thing = NULL;
1658 asdl_seq *decorator_seq = NULL;
1659
1660 REQ(n, decorated);
1661
1662 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1663 if (!decorator_seq)
1664 return NULL;
1665
1666 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001667 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001669
1670 if (TYPE(CHILD(n, 1)) == funcdef) {
1671 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1672 } else if (TYPE(CHILD(n, 1)) == classdef) {
1673 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001674 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1675 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001676 }
1677 return thing;
1678}
1679
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680static expr_ty
1681ast_for_lambdef(struct compiling *c, const node *n)
1682{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001683 /* lambdef: 'lambda' [varargslist] ':' test
1684 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685 arguments_ty args;
1686 expr_ty expression;
1687
1688 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001689 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690 if (!args)
1691 return NULL;
1692 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001693 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695 }
1696 else {
1697 args = ast_for_arguments(c, CHILD(n, 1));
1698 if (!args)
1699 return NULL;
1700 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001701 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703 }
1704
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001705 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706}
1707
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001708static expr_ty
1709ast_for_ifexpr(struct compiling *c, const node *n)
1710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001712 expr_ty expression, body, orelse;
1713
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001714 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001715 body = ast_for_expr(c, CHILD(n, 0));
1716 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001717 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001718 expression = ast_for_expr(c, CHILD(n, 2));
1719 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001720 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001721 orelse = ast_for_expr(c, CHILD(n, 4));
1722 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001723 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001724 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
1725 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001726}
1727
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001729 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730
Nick Coghlan650f0d02007-04-15 12:05:43 +00001731 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732*/
1733
1734static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001735count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001736{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001737 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738
Guido van Rossumd8faa362007-04-27 19:54:29 +00001739 count_comp_for:
1740 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001741 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001742 if (NCH(n) == 2) {
1743 REQ(CHILD(n, 0), NAME);
1744 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1745 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001746 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001747 else if (NCH(n) == 1) {
1748 n = CHILD(n, 0);
1749 }
1750 else {
1751 goto error;
1752 }
1753 if (NCH(n) == (5)) {
1754 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001755 }
1756 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001757 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001758 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001759 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001760 REQ(n, comp_iter);
1761 n = CHILD(n, 0);
1762 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001763 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001764 else if (TYPE(n) == comp_if) {
1765 if (NCH(n) == 3) {
1766 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001767 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001768 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001769 else
1770 return n_fors;
1771 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001772
Jelle Zijlstraac317702017-10-05 20:24:46 -07001773 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001774 /* Should never be reached */
1775 PyErr_SetString(PyExc_SystemError,
1776 "logic error in count_comp_fors");
1777 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001778}
1779
Nick Coghlan650f0d02007-04-15 12:05:43 +00001780/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001781
Nick Coghlan650f0d02007-04-15 12:05:43 +00001782 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001783*/
1784
1785static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001786count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001787{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001788 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001789
Guido van Rossumd8faa362007-04-27 19:54:29 +00001790 while (1) {
1791 REQ(n, comp_iter);
1792 if (TYPE(CHILD(n, 0)) == comp_for)
1793 return n_ifs;
1794 n = CHILD(n, 0);
1795 REQ(n, comp_if);
1796 n_ifs++;
1797 if (NCH(n) == 2)
1798 return n_ifs;
1799 n = CHILD(n, 2);
1800 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001801}
1802
Guido van Rossum992d4a32007-07-11 13:09:30 +00001803static asdl_seq *
1804ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001807 asdl_seq *comps;
1808
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001809 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810 if (n_fors == -1)
1811 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001812
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001813 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001814 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001816
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001818 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001820 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001821 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001822 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001823 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824
Guido van Rossum992d4a32007-07-11 13:09:30 +00001825 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826
Jelle Zijlstraac317702017-10-05 20:24:46 -07001827 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001828 is_async = 1;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001829 REQ(CHILD(n, 0), NAME);
1830 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1831 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001832 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001833 else {
1834 sync_n = CHILD(n, 0);
1835 }
1836 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001837
Jelle Zijlstraac317702017-10-05 20:24:46 -07001838 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001839 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001840 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001842 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001843 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001845
Thomas Wouters89f507f2006-12-13 04:49:30 +00001846 /* Check the # of children rather than the length of t, since
1847 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001848 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001849 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001850 comp = comprehension(first, expression, NULL,
1851 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001852 else
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001853 comp = comprehension(Tuple(t, Store, first->lineno,
1854 first->col_offset, c->c_arena),
1855 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001856 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001858
Jelle Zijlstraac317702017-10-05 20:24:46 -07001859 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860 int j, n_ifs;
1861 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862
Jelle Zijlstraac317702017-10-05 20:24:46 -07001863 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001864 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001865 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001867
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001868 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001869 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001871
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001872 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001873 REQ(n, comp_iter);
1874 n = CHILD(n, 0);
1875 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876
Guido van Rossum992d4a32007-07-11 13:09:30 +00001877 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001878 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001879 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001880 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001881 if (NCH(n) == 3)
1882 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001884 /* on exit, must guarantee that n is a comp_for */
1885 if (TYPE(n) == comp_iter)
1886 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001887 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001888 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001889 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001891 return comps;
1892}
1893
1894static expr_ty
1895ast_for_itercomp(struct compiling *c, const node *n, int type)
1896{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001897 /* testlist_comp: (test|star_expr)
1898 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001899 expr_ty elt;
1900 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001901 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902
Guido van Rossum992d4a32007-07-11 13:09:30 +00001903 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001905 ch = CHILD(n, 0);
1906 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001907 if (!elt)
1908 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001909 if (elt->kind == Starred_kind) {
1910 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1911 return NULL;
1912 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913
Guido van Rossum992d4a32007-07-11 13:09:30 +00001914 comps = ast_for_comprehension(c, CHILD(n, 1));
1915 if (!comps)
1916 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001917
1918 if (type == COMP_GENEXP)
1919 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1920 else if (type == COMP_LISTCOMP)
1921 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1922 else if (type == COMP_SETCOMP)
1923 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena);
1924 else
1925 /* Should never happen */
1926 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001927}
1928
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001929/* Fills in the key, value pair corresponding to the dict element. In case
1930 * of an unpacking, key is NULL. *i is advanced by the number of ast
1931 * elements. Iff successful, nonzero is returned.
1932 */
1933static int
1934ast_for_dictelement(struct compiling *c, const node *n, int *i,
1935 expr_ty *key, expr_ty *value)
1936{
1937 expr_ty expression;
1938 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
1939 assert(NCH(n) - *i >= 2);
1940
1941 expression = ast_for_expr(c, CHILD(n, *i + 1));
1942 if (!expression)
1943 return 0;
1944 *key = NULL;
1945 *value = expression;
1946
1947 *i += 2;
1948 }
1949 else {
1950 assert(NCH(n) - *i >= 3);
1951
1952 expression = ast_for_expr(c, CHILD(n, *i));
1953 if (!expression)
1954 return 0;
1955 *key = expression;
1956
1957 REQ(CHILD(n, *i + 1), COLON);
1958
1959 expression = ast_for_expr(c, CHILD(n, *i + 2));
1960 if (!expression)
1961 return 0;
1962 *value = expression;
1963
1964 *i += 3;
1965 }
1966 return 1;
1967}
1968
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001969static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001970ast_for_dictcomp(struct compiling *c, const node *n)
1971{
1972 expr_ty key, value;
1973 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001974 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001976 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00001977 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001978 assert(key);
1979 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001981 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00001982 if (!comps)
1983 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984
Guido van Rossum992d4a32007-07-11 13:09:30 +00001985 return DictComp(key, value, comps, LINENO(n), n->n_col_offset, c->c_arena);
1986}
1987
1988static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001989ast_for_dictdisplay(struct compiling *c, const node *n)
1990{
1991 int i;
1992 int j;
1993 int size;
1994 asdl_seq *keys, *values;
1995
1996 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
1997 keys = _Py_asdl_seq_new(size, c->c_arena);
1998 if (!keys)
1999 return NULL;
2000
2001 values = _Py_asdl_seq_new(size, c->c_arena);
2002 if (!values)
2003 return NULL;
2004
2005 j = 0;
2006 for (i = 0; i < NCH(n); i++) {
2007 expr_ty key, value;
2008
2009 if (!ast_for_dictelement(c, n, &i, &key, &value))
2010 return NULL;
2011 asdl_seq_SET(keys, j, key);
2012 asdl_seq_SET(values, j, value);
2013
2014 j++;
2015 }
2016 keys->size = j;
2017 values->size = j;
2018 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena);
2019}
2020
2021static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002022ast_for_genexp(struct compiling *c, const node *n)
2023{
2024 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002025 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002026}
2027
2028static expr_ty
2029ast_for_listcomp(struct compiling *c, const node *n)
2030{
2031 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002032 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002033}
2034
2035static expr_ty
2036ast_for_setcomp(struct compiling *c, const node *n)
2037{
2038 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002039 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002040}
2041
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002042static expr_ty
2043ast_for_setdisplay(struct compiling *c, const node *n)
2044{
2045 int i;
2046 int size;
2047 asdl_seq *elts;
2048
2049 assert(TYPE(n) == (dictorsetmaker));
2050 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2051 elts = _Py_asdl_seq_new(size, c->c_arena);
2052 if (!elts)
2053 return NULL;
2054 for (i = 0; i < NCH(n); i += 2) {
2055 expr_ty expression;
2056 expression = ast_for_expr(c, CHILD(n, i));
2057 if (!expression)
2058 return NULL;
2059 asdl_seq_SET(elts, i / 2, expression);
2060 }
2061 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena);
2062}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002063
2064static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002065ast_for_atom(struct compiling *c, const node *n)
2066{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002067 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2068 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002069 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070 */
2071 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002074 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002075 PyObject *name;
2076 const char *s = STR(ch);
2077 size_t len = strlen(s);
2078 if (len >= 4 && len <= 5) {
2079 if (!strcmp(s, "None"))
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002080 return Constant(Py_None, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002081 if (!strcmp(s, "True"))
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002082 return Constant(Py_True, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002083 if (!strcmp(s, "False"))
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002084 return Constant(Py_False, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002085 }
2086 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002087 if (!name)
2088 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002089 /* All names start in Load context, but may later be changed. */
Benjamin Peterson30760062008-11-25 04:02:28 +00002090 return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
2091 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002093 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002094 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002095 const char *errtype = NULL;
2096 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2097 errtype = "unicode error";
2098 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2099 errtype = "value error";
2100 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002101 PyObject *type, *value, *tback, *errstr;
2102 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002103 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002104 if (errstr) {
2105 ast_error(c, n, "(%s) %U", errtype, errstr);
2106 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002107 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002108 else {
2109 PyErr_Clear();
2110 ast_error(c, n, "(%s) unknown error", errtype);
2111 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002112 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002113 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002114 Py_XDECREF(tback);
2115 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002116 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002117 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002118 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119 }
2120 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002121 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002122 if (!pynum)
2123 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002124
Victor Stinner43d81952013-07-17 00:57:58 +02002125 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2126 Py_DECREF(pynum);
2127 return NULL;
2128 }
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002129 return Constant(pynum, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130 }
Georg Brandldde00282007-03-18 19:01:53 +00002131 case ELLIPSIS: /* Ellipsis */
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002132 return Constant(Py_Ellipsis, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002133 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002134 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135
Thomas Wouters89f507f2006-12-13 04:49:30 +00002136 if (TYPE(ch) == RPAR)
2137 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138
Thomas Wouters89f507f2006-12-13 04:49:30 +00002139 if (TYPE(ch) == yield_expr)
2140 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002143 if (NCH(ch) == 1) {
2144 return ast_for_testlist(c, ch);
2145 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002146
Serhiy Storchakab619b092018-11-27 09:40:29 +02002147 if (TYPE(CHILD(ch, 1)) == comp_for) {
2148 return copy_location(ast_for_genexp(c, ch), n);
2149 }
2150 else {
2151 return copy_location(ast_for_testlist(c, ch), n);
2152 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002153 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002154 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155
Thomas Wouters89f507f2006-12-13 04:49:30 +00002156 if (TYPE(ch) == RSQB)
2157 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158
Nick Coghlan650f0d02007-04-15 12:05:43 +00002159 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002160 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2161 asdl_seq *elts = seq_for_testlist(c, ch);
2162 if (!elts)
2163 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002164
Thomas Wouters89f507f2006-12-13 04:49:30 +00002165 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
2166 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002167 else {
2168 return copy_location(ast_for_listcomp(c, ch), n);
2169 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002171 /* dictorsetmaker: ( ((test ':' test | '**' test)
2172 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2173 * ((test | '*' test)
2174 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002175 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002176 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002177 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002178 /* It's an empty dict. */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002179 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002180 }
2181 else {
2182 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2183 if (NCH(ch) == 1 ||
2184 (NCH(ch) > 1 &&
2185 TYPE(CHILD(ch, 1)) == COMMA)) {
2186 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002187 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002188 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002189 else if (NCH(ch) > 1 &&
2190 TYPE(CHILD(ch, 1)) == comp_for) {
2191 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002192 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002193 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002194 else if (NCH(ch) > 3 - is_dict &&
2195 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2196 /* It's a dictionary comprehension. */
2197 if (is_dict) {
2198 ast_error(c, n, "dict unpacking cannot be used in "
2199 "dict comprehension");
2200 return NULL;
2201 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002202 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002203 }
2204 else {
2205 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002206 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002207 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002208 return copy_location(res, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002209 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002210 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002211 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002212 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2213 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002214 }
2215}
2216
2217static slice_ty
2218ast_for_slice(struct compiling *c, const node *n)
2219{
2220 node *ch;
2221 expr_ty lower = NULL, upper = NULL, step = NULL;
2222
2223 REQ(n, subscript);
2224
2225 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002226 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002227 sliceop: ':' [test]
2228 */
2229 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002230 if (NCH(n) == 1 && TYPE(ch) == test) {
2231 /* 'step' variable hold no significance in terms of being used over
2232 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002234 if (!step)
2235 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236
Thomas Wouters89f507f2006-12-13 04:49:30 +00002237 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238 }
2239
2240 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002241 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002242 if (!lower)
2243 return NULL;
2244 }
2245
2246 /* If there's an upper bound it's in the second or third position. */
2247 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002248 if (NCH(n) > 1) {
2249 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002250
Thomas Wouters89f507f2006-12-13 04:49:30 +00002251 if (TYPE(n2) == test) {
2252 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002253 if (!upper)
2254 return NULL;
2255 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002256 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002258 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002259
Thomas Wouters89f507f2006-12-13 04:49:30 +00002260 if (TYPE(n2) == test) {
2261 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002262 if (!upper)
2263 return NULL;
2264 }
2265 }
2266
2267 ch = CHILD(n, NCH(n) - 1);
2268 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002269 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002270 ch = CHILD(ch, 1);
2271 if (TYPE(ch) == test) {
2272 step = ast_for_expr(c, ch);
2273 if (!step)
2274 return NULL;
2275 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276 }
2277 }
2278
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002279 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280}
2281
2282static expr_ty
2283ast_for_binop(struct compiling *c, const node *n)
2284{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002285 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002287 BinOp(BinOp(A, op, B), op, C).
2288 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289
Guido van Rossumd8faa362007-04-27 19:54:29 +00002290 int i, nops;
2291 expr_ty expr1, expr2, result;
2292 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002293
Guido van Rossumd8faa362007-04-27 19:54:29 +00002294 expr1 = ast_for_expr(c, CHILD(n, 0));
2295 if (!expr1)
2296 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297
Guido van Rossumd8faa362007-04-27 19:54:29 +00002298 expr2 = ast_for_expr(c, CHILD(n, 2));
2299 if (!expr2)
2300 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301
Guido van Rossumd8faa362007-04-27 19:54:29 +00002302 newoperator = get_operator(CHILD(n, 1));
2303 if (!newoperator)
2304 return NULL;
2305
2306 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
2307 c->c_arena);
2308 if (!result)
2309 return NULL;
2310
2311 nops = (NCH(n) - 1) / 2;
2312 for (i = 1; i < nops; i++) {
2313 expr_ty tmp_result, tmp;
2314 const node* next_oper = CHILD(n, i * 2 + 1);
2315
2316 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002317 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002318 return NULL;
2319
Guido van Rossumd8faa362007-04-27 19:54:29 +00002320 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2321 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322 return NULL;
2323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002325 LINENO(next_oper), next_oper->n_col_offset,
2326 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002328 return NULL;
2329 result = tmp_result;
2330 }
2331 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002332}
2333
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002334static expr_ty
2335ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002338 subscriptlist: subscript (',' subscript)* [',']
2339 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2340 */
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002341 REQ(n, trailer);
2342 if (TYPE(CHILD(n, 0)) == LPAR) {
2343 if (NCH(n) == 2)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002344 return Call(left_expr, NULL, NULL, LINENO(n),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002345 n->n_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002346 else
Serhiy Storchakab619b092018-11-27 09:40:29 +02002347 return ast_for_call(c, CHILD(n, 1), left_expr, CHILD(n, 0));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002348 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002349 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002350 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2351 if (!attr_id)
2352 return NULL;
2353 return Attribute(left_expr, attr_id, Load,
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002354 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002355 }
2356 else {
2357 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002358 REQ(CHILD(n, 2), RSQB);
2359 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002360 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002361 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2362 if (!slc)
2363 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002364 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
2365 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002366 }
2367 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002369 by treating the sequence as a tuple literal if there are
2370 no slice features.
2371 */
Victor Stinner4d73ae72018-11-22 14:45:16 +01002372 Py_ssize_t j;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002373 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002374 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002375 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002376 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002377 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002378 if (!slices)
2379 return NULL;
2380 for (j = 0; j < NCH(n); j += 2) {
2381 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002382 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002383 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002384 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002385 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002386 asdl_seq_SET(slices, j / 2, slc);
2387 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002388 if (!simple) {
2389 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002390 Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002391 }
2392 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002393 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002394 if (!elts)
2395 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002396 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2397 slc = (slice_ty)asdl_seq_GET(slices, j);
2398 assert(slc->kind == Index_kind && slc->v.Index.value);
2399 asdl_seq_SET(elts, j, slc->v.Index.value);
2400 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002401 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002402 if (!e)
2403 return NULL;
2404 return Subscript(left_expr, Index(e, c->c_arena),
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002405 Load, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002406 }
2407 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002408}
2409
2410static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002411ast_for_factor(struct compiling *c, const node *n)
2412{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002413 expr_ty expression;
2414
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002415 expression = ast_for_expr(c, CHILD(n, 1));
2416 if (!expression)
2417 return NULL;
2418
2419 switch (TYPE(CHILD(n, 0))) {
2420 case PLUS:
2421 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
2422 c->c_arena);
2423 case MINUS:
2424 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
2425 c->c_arena);
2426 case TILDE:
2427 return UnaryOp(Invert, expression, LINENO(n),
2428 n->n_col_offset, c->c_arena);
2429 }
2430 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2431 TYPE(CHILD(n, 0)));
2432 return NULL;
2433}
2434
2435static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002436ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002437{
Yury Selivanov75445082015-05-11 22:57:16 -04002438 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002439 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002440
2441 REQ(n, atom_expr);
2442 nch = NCH(n);
2443
Jelle Zijlstraac317702017-10-05 20:24:46 -07002444 if (TYPE(CHILD(n, 0)) == NAME && strcmp(STR(CHILD(n, 0)), "await") == 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04002445 start = 1;
2446 assert(nch > 1);
2447 }
2448
2449 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002450 if (!e)
2451 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002452 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002453 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002454 if (start && nch == 2) {
2455 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2456 }
2457
2458 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002459 node *ch = CHILD(n, i);
2460 if (TYPE(ch) != trailer)
2461 break;
2462 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002463 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002464 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002465 tmp->lineno = e->lineno;
2466 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002467 e = tmp;
2468 }
Yury Selivanov75445082015-05-11 22:57:16 -04002469
2470 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002471 /* there was an 'await' */
Yury Selivanov75445082015-05-11 22:57:16 -04002472 return Await(e, LINENO(n), n->n_col_offset, c->c_arena);
2473 }
2474 else {
2475 return e;
2476 }
2477}
2478
2479static expr_ty
2480ast_for_power(struct compiling *c, const node *n)
2481{
2482 /* power: atom trailer* ('**' factor)*
2483 */
2484 expr_ty e;
2485 REQ(n, power);
2486 e = ast_for_atom_expr(c, CHILD(n, 0));
2487 if (!e)
2488 return NULL;
2489 if (NCH(n) == 1)
2490 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002491 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2492 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002493 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002494 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002495 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002496 }
2497 return e;
2498}
2499
Guido van Rossum0368b722007-05-11 16:50:42 +00002500static expr_ty
2501ast_for_starred(struct compiling *c, const node *n)
2502{
2503 expr_ty tmp;
2504 REQ(n, star_expr);
2505
2506 tmp = ast_for_expr(c, CHILD(n, 1));
2507 if (!tmp)
2508 return NULL;
2509
2510 /* The Load context is changed later. */
2511 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
2512}
2513
2514
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515/* Do not name a variable 'expr'! Will cause a compile error.
2516*/
2517
2518static expr_ty
2519ast_for_expr(struct compiling *c, const node *n)
2520{
2521 /* handle the full range of simple expressions
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002522 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002523 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525 and_test: not_test ('and' not_test)*
2526 not_test: 'not' not_test | comparison
2527 comparison: expr (comp_op expr)*
2528 expr: xor_expr ('|' xor_expr)*
2529 xor_expr: and_expr ('^' and_expr)*
2530 and_expr: shift_expr ('&' shift_expr)*
2531 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2532 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002533 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002535 power: atom_expr ['**' factor]
Jelle Zijlstraac317702017-10-05 20:24:46 -07002536 atom_expr: ['await'] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002537 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002538 */
2539
2540 asdl_seq *seq;
2541 int i;
2542
2543 loop:
2544 switch (TYPE(n)) {
2545 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002546 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002547 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002548 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002550 else if (NCH(n) > 1)
2551 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002552 /* Fallthrough */
2553 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002554 case and_test:
2555 if (NCH(n) == 1) {
2556 n = CHILD(n, 0);
2557 goto loop;
2558 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002559 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560 if (!seq)
2561 return NULL;
2562 for (i = 0; i < NCH(n); i += 2) {
2563 expr_ty e = ast_for_expr(c, CHILD(n, i));
2564 if (!e)
2565 return NULL;
2566 asdl_seq_SET(seq, i / 2, e);
2567 }
2568 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002569 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
2570 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002571 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002572 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002573 case not_test:
2574 if (NCH(n) == 1) {
2575 n = CHILD(n, 0);
2576 goto loop;
2577 }
2578 else {
2579 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2580 if (!expression)
2581 return NULL;
2582
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002583 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
2584 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 }
2586 case comparison:
2587 if (NCH(n) == 1) {
2588 n = CHILD(n, 0);
2589 goto loop;
2590 }
2591 else {
2592 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002593 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002594 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002595 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596 if (!ops)
2597 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002598 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002600 return NULL;
2601 }
2602 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002603 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002604
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002605 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002606 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002607 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002608 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002609
2610 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002611 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002613 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002614
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002615 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002616 asdl_seq_SET(cmps, i / 2, expression);
2617 }
2618 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002619 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002621 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002622
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002623 return Compare(expression, ops, cmps, LINENO(n),
2624 n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625 }
2626 break;
2627
Guido van Rossum0368b722007-05-11 16:50:42 +00002628 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002630 /* The next five cases all handle BinOps. The main body of code
2631 is the same in each case, but the switch turned inside out to
2632 reuse the code for each type of operator.
2633 */
2634 case expr:
2635 case xor_expr:
2636 case and_expr:
2637 case shift_expr:
2638 case arith_expr:
2639 case term:
2640 if (NCH(n) == 1) {
2641 n = CHILD(n, 0);
2642 goto loop;
2643 }
2644 return ast_for_binop(c, n);
2645 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002646 node *an = NULL;
2647 node *en = NULL;
2648 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002649 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002650 if (NCH(n) > 1)
2651 an = CHILD(n, 1); /* yield_arg */
2652 if (an) {
2653 en = CHILD(an, NCH(an) - 1);
2654 if (NCH(an) == 2) {
2655 is_from = 1;
2656 exp = ast_for_expr(c, en);
2657 }
2658 else
2659 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002660 if (!exp)
2661 return NULL;
2662 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002663 if (is_from)
2664 return YieldFrom(exp, LINENO(n), n->n_col_offset, c->c_arena);
2665 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002666 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002667 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002668 if (NCH(n) == 1) {
2669 n = CHILD(n, 0);
2670 goto loop;
2671 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002672 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002673 case power:
2674 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002675 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002676 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002677 return NULL;
2678 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002679 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002680 return NULL;
2681}
2682
2683static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02002684ast_for_call(struct compiling *c, const node *n, expr_ty func,
2685 const node *maybegenbeg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002686{
2687 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002688 arglist: argument (',' argument)* [',']
2689 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002690 */
2691
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002692 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002693 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002694 asdl_seq *args;
2695 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002696
2697 REQ(n, arglist);
2698
2699 nargs = 0;
2700 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002702 node *ch = CHILD(n, i);
2703 if (TYPE(ch) == argument) {
2704 if (NCH(ch) == 1)
2705 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002706 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2707 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02002708 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002709 ast_error(c, ch, "invalid syntax");
2710 return NULL;
2711 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002712 if (NCH(n) > 1) {
2713 ast_error(c, ch, "Generator expression must be parenthesized");
2714 return NULL;
2715 }
2716 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002717 else if (TYPE(CHILD(ch, 0)) == STAR)
2718 nargs++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002720 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002721 nkeywords++;
2722 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002723 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002724
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002725 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002726 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002727 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002728 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002729 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002730 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002731
2732 nargs = 0; /* positional arguments + iterable argument unpackings */
2733 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2734 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002736 node *ch = CHILD(n, i);
2737 if (TYPE(ch) == argument) {
2738 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002739 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002740 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002741 /* a positional argument */
2742 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002743 if (ndoublestars) {
2744 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002745 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002746 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002747 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002748 else {
2749 ast_error(c, chch,
2750 "positional argument follows "
2751 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002752 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002753 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002754 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002755 e = ast_for_expr(c, chch);
2756 if (!e)
2757 return NULL;
2758 asdl_seq_SET(args, nargs++, e);
2759 }
2760 else if (TYPE(chch) == STAR) {
2761 /* an iterable argument unpacking */
2762 expr_ty starred;
2763 if (ndoublestars) {
2764 ast_error(c, chch,
2765 "iterable argument unpacking follows "
2766 "keyword argument unpacking");
2767 return NULL;
2768 }
2769 e = ast_for_expr(c, CHILD(ch, 1));
2770 if (!e)
2771 return NULL;
2772 starred = Starred(e, Load, LINENO(chch),
2773 chch->n_col_offset,
2774 c->c_arena);
2775 if (!starred)
2776 return NULL;
2777 asdl_seq_SET(args, nargs++, starred);
2778
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002779 }
2780 else if (TYPE(chch) == DOUBLESTAR) {
2781 /* a keyword argument unpacking */
2782 keyword_ty kw;
2783 i++;
2784 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002785 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002786 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002787 kw = keyword(NULL, e, c->c_arena);
2788 asdl_seq_SET(keywords, nkeywords++, kw);
2789 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002791 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002792 /* the lone generator expression */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002793 e = copy_location(ast_for_genexp(c, ch), maybegenbeg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002794 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002795 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002796 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002798 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002799 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002800 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002801 identifier key, tmp;
2802 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002803
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07002804 // To remain LL(1), the grammar accepts any test (basically, any
2805 // expression) in the keyword slot of a call site. So, we need
2806 // to manually enforce that the keyword is a NAME here.
2807 static const int name_tree[] = {
2808 test,
2809 or_test,
2810 and_test,
2811 not_test,
2812 comparison,
2813 expr,
2814 xor_expr,
2815 and_expr,
2816 shift_expr,
2817 arith_expr,
2818 term,
2819 factor,
2820 power,
2821 atom_expr,
2822 atom,
2823 0,
2824 };
2825 node *expr_node = chch;
2826 for (int i = 0; name_tree[i]; i++) {
2827 if (TYPE(expr_node) != name_tree[i])
2828 break;
2829 if (NCH(expr_node) != 1)
2830 break;
2831 expr_node = CHILD(expr_node, 0);
2832 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002833 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002834 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002835 "expression cannot contain assignment, "
2836 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002837 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002838 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07002839 key = new_identifier(STR(expr_node), c);
2840 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002841 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07002843 if (forbidden_name(c, key, chch, 1)) {
2844 return NULL;
2845 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002846 for (k = 0; k < nkeywords; k++) {
2847 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002848 if (tmp && !PyUnicode_Compare(tmp, key)) {
2849 ast_error(c, chch,
2850 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002851 return NULL;
2852 }
2853 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002854 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002855 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002856 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002857 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002858 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002859 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002860 asdl_seq_SET(keywords, nkeywords++, kw);
2861 }
2862 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002863 }
2864
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002865 return Call(func, args, keywords, func->lineno, func->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866}
2867
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002869ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002870{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002871 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002872 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002873 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002874 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002875 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002876 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002877 }
2878 else {
2879 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00002880 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002881 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00002883 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002884 else {
2885 asdl_seq *tmp = seq_for_testlist(c, n);
2886 if (!tmp)
2887 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002888 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002889 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00002890}
2891
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892static stmt_ty
2893ast_for_expr_stmt(struct compiling *c, const node *n)
2894{
2895 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002896 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
2897 ('=' (yield_expr|testlist_star_expr))*)
2898 annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +00002899 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04002900 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00002901 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00002902 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903 */
2904
2905 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002906 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002907 if (!e)
2908 return NULL;
2909
Thomas Wouters89f507f2006-12-13 04:49:30 +00002910 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002911 }
2912 else if (TYPE(CHILD(n, 1)) == augassign) {
2913 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002914 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002915 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916
Thomas Wouters89f507f2006-12-13 04:49:30 +00002917 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002918 if (!expr1)
2919 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002920 if(!set_context(c, expr1, Store, ch))
2921 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002922 /* set_context checks that most expressions are not the left side.
2923 Augmented assignments can only have a name, a subscript, or an
2924 attribute on the left, though, so we have to explicitly check for
2925 those. */
2926 switch (expr1->kind) {
2927 case Name_kind:
2928 case Attribute_kind:
2929 case Subscript_kind:
2930 break;
2931 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04002932 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00002933 return NULL;
2934 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002935
Thomas Wouters89f507f2006-12-13 04:49:30 +00002936 ch = CHILD(n, 2);
2937 if (TYPE(ch) == testlist)
2938 expr2 = ast_for_testlist(c, ch);
2939 else
2940 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002941 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002942 return NULL;
2943
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002944 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002945 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002946 return NULL;
2947
Thomas Wouters89f507f2006-12-13 04:49:30 +00002948 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002950 else if (TYPE(CHILD(n, 1)) == annassign) {
2951 expr_ty expr1, expr2, expr3;
2952 node *ch = CHILD(n, 0);
2953 node *deep, *ann = CHILD(n, 1);
2954 int simple = 1;
2955
2956 /* we keep track of parens to qualify (x) as expression not name */
2957 deep = ch;
2958 while (NCH(deep) == 1) {
2959 deep = CHILD(deep, 0);
2960 }
2961 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
2962 simple = 0;
2963 }
2964 expr1 = ast_for_testlist(c, ch);
2965 if (!expr1) {
2966 return NULL;
2967 }
2968 switch (expr1->kind) {
2969 case Name_kind:
2970 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
2971 return NULL;
2972 }
2973 expr1->v.Name.ctx = Store;
2974 break;
2975 case Attribute_kind:
2976 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
2977 return NULL;
2978 }
2979 expr1->v.Attribute.ctx = Store;
2980 break;
2981 case Subscript_kind:
2982 expr1->v.Subscript.ctx = Store;
2983 break;
2984 case List_kind:
2985 ast_error(c, ch,
2986 "only single target (not list) can be annotated");
2987 return NULL;
2988 case Tuple_kind:
2989 ast_error(c, ch,
2990 "only single target (not tuple) can be annotated");
2991 return NULL;
2992 default:
2993 ast_error(c, ch,
2994 "illegal target for annotation");
2995 return NULL;
2996 }
2997
2998 if (expr1->kind != Name_kind) {
2999 simple = 0;
3000 }
3001 ch = CHILD(ann, 1);
3002 expr2 = ast_for_expr(c, ch);
3003 if (!expr2) {
3004 return NULL;
3005 }
3006 if (NCH(ann) == 2) {
3007 return AnnAssign(expr1, expr2, NULL, simple,
3008 LINENO(n), n->n_col_offset, c->c_arena);
3009 }
3010 else {
3011 ch = CHILD(ann, 3);
3012 expr3 = ast_for_expr(c, ch);
3013 if (!expr3) {
3014 return NULL;
3015 }
3016 return AnnAssign(expr1, expr2, expr3, simple,
3017 LINENO(n), n->n_col_offset, c->c_arena);
3018 }
3019 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003020 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003021 int i;
3022 asdl_seq *targets;
3023 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003024 expr_ty expression;
3025
Thomas Wouters89f507f2006-12-13 04:49:30 +00003026 /* a normal assignment */
3027 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003028 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003029 if (!targets)
3030 return NULL;
3031 for (i = 0; i < NCH(n) - 2; i += 2) {
3032 expr_ty e;
3033 node *ch = CHILD(n, i);
3034 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003035 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003036 return NULL;
3037 }
3038 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003040 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003041
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003042 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003043 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003044 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003045
Thomas Wouters89f507f2006-12-13 04:49:30 +00003046 asdl_seq_SET(targets, i / 2, e);
3047 }
3048 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003049 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003050 expression = ast_for_testlist(c, value);
3051 else
3052 expression = ast_for_expr(c, value);
3053 if (!expression)
3054 return NULL;
3055 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003056 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003057}
3058
Benjamin Peterson78565b22009-06-28 19:19:51 +00003059
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003060static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003061ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003062{
3063 asdl_seq *seq;
3064 int i;
3065 expr_ty e;
3066
3067 REQ(n, exprlist);
3068
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003069 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003070 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003071 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003072 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003073 e = ast_for_expr(c, CHILD(n, i));
3074 if (!e)
3075 return NULL;
3076 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003077 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003078 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003079 }
3080 return seq;
3081}
3082
3083static stmt_ty
3084ast_for_del_stmt(struct compiling *c, const node *n)
3085{
3086 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003088 /* del_stmt: 'del' exprlist */
3089 REQ(n, del_stmt);
3090
3091 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3092 if (!expr_list)
3093 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003094 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003095}
3096
3097static stmt_ty
3098ast_for_flow_stmt(struct compiling *c, const node *n)
3099{
3100 /*
3101 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3102 | yield_stmt
3103 break_stmt: 'break'
3104 continue_stmt: 'continue'
3105 return_stmt: 'return' [testlist]
3106 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003107 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003108 raise_stmt: 'raise' [test [',' test [',' test]]]
3109 */
3110 node *ch;
3111
3112 REQ(n, flow_stmt);
3113 ch = CHILD(n, 0);
3114 switch (TYPE(ch)) {
3115 case break_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003116 return Break(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117 case continue_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003118 return Continue(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003119 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003120 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3121 if (!exp)
3122 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003123 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124 }
3125 case return_stmt:
3126 if (NCH(ch) == 1)
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003127 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003128 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003129 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130 if (!expression)
3131 return NULL;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003132 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133 }
3134 case raise_stmt:
3135 if (NCH(ch) == 1)
Collin Winter828f04a2007-08-31 00:04:24 +00003136 return Raise(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena);
3137 else if (NCH(ch) >= 2) {
3138 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003139 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3140 if (!expression)
3141 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003142 if (NCH(ch) == 4) {
3143 cause = ast_for_expr(c, CHILD(ch, 3));
3144 if (!cause)
3145 return NULL;
3146 }
3147 return Raise(expression, cause, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003148 }
Stefan Krahf432a322017-08-21 13:09:59 +02003149 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003150 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003151 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 "unexpected flow_stmt: %d", TYPE(ch));
3153 return NULL;
3154 }
3155}
3156
3157static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003158alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159{
3160 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003161 import_as_name: NAME ['as' NAME]
3162 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003163 dotted_name: NAME ('.' NAME)*
3164 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003165 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003166
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003167 loop:
3168 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003169 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003170 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003171 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003172 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003173 if (!name)
3174 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003175 if (NCH(n) == 3) {
3176 node *str_node = CHILD(n, 2);
3177 str = NEW_IDENTIFIER(str_node);
3178 if (!str)
3179 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003180 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003181 return NULL;
3182 }
3183 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003184 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003185 return NULL;
3186 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003187 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003188 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189 case dotted_as_name:
3190 if (NCH(n) == 1) {
3191 n = CHILD(n, 0);
3192 goto loop;
3193 }
3194 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003195 node *asname_node = CHILD(n, 2);
3196 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003197 if (!a)
3198 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003199 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003200 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003201 if (!a->asname)
3202 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003203 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003204 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205 return a;
3206 }
3207 break;
3208 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003209 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003210 node *name_node = CHILD(n, 0);
3211 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003212 if (!name)
3213 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003214 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003215 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003216 return alias(name, NULL, c->c_arena);
3217 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003218 else {
3219 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003220 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003221 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003224
3225 len = 0;
3226 for (i = 0; i < NCH(n); i += 2)
3227 /* length of string plus one for the dot */
3228 len += strlen(STR(CHILD(n, i))) + 1;
3229 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003230 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003231 if (!str)
3232 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003233 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003234 if (!s)
3235 return NULL;
3236 for (i = 0; i < NCH(n); i += 2) {
3237 char *sch = STR(CHILD(n, i));
3238 strcpy(s, STR(CHILD(n, i)));
3239 s += strlen(sch);
3240 *s++ = '.';
3241 }
3242 --s;
3243 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3245 PyBytes_GET_SIZE(str),
3246 NULL);
3247 Py_DECREF(str);
3248 if (!uni)
3249 return NULL;
3250 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003251 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003252 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3253 Py_DECREF(str);
3254 return NULL;
3255 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003256 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003257 }
3258 break;
3259 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003260 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003261 if (!str)
3262 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003263 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3264 Py_DECREF(str);
3265 return NULL;
3266 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003267 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003268 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003269 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003270 "unexpected import name: %d", TYPE(n));
3271 return NULL;
3272 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003273
3274 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003275 return NULL;
3276}
3277
3278static stmt_ty
3279ast_for_import_stmt(struct compiling *c, const node *n)
3280{
3281 /*
3282 import_stmt: import_name | import_from
3283 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003284 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3285 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003286 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003287 int lineno;
3288 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289 int i;
3290 asdl_seq *aliases;
3291
3292 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003293 lineno = LINENO(n);
3294 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003295 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003296 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003298 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003299 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003300 if (!aliases)
3301 return NULL;
3302 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003303 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003304 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003305 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003306 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003307 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003308 return Import(aliases, lineno, col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003310 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003311 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003312 int idx, ndots = 0;
3313 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003314 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003316 /* Count the number of dots (for relative imports) and check for the
3317 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003318 for (idx = 1; idx < NCH(n); idx++) {
3319 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003320 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3321 if (!mod)
3322 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003323 idx++;
3324 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003325 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003327 ndots += 3;
3328 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003329 } else if (TYPE(CHILD(n, idx)) != DOT) {
3330 break;
3331 }
3332 ndots++;
3333 }
3334 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003335 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003336 case STAR:
3337 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003338 n = CHILD(n, idx);
3339 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003340 break;
3341 case LPAR:
3342 /* from ... import (x, y, z) */
3343 n = CHILD(n, idx + 1);
3344 n_children = NCH(n);
3345 break;
3346 case import_as_names:
3347 /* from ... import x, y, z */
3348 n = CHILD(n, idx);
3349 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003350 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003351 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352 " surrounding parentheses");
3353 return NULL;
3354 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003355 break;
3356 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003357 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003358 return NULL;
3359 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003360
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003361 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003362 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003363 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003364
3365 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003366 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003367 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003368 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003369 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003370 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003371 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003372 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003373 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003374 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003375 if (!import_alias)
3376 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003377 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003378 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003379 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003380 if (mod != NULL)
3381 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003382 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003383 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003384 }
Neal Norwitz79792652005-11-14 04:25:03 +00003385 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003386 "unknown import statement: starts with command '%s'",
3387 STR(CHILD(n, 0)));
3388 return NULL;
3389}
3390
3391static stmt_ty
3392ast_for_global_stmt(struct compiling *c, const node *n)
3393{
3394 /* global_stmt: 'global' NAME (',' NAME)* */
3395 identifier name;
3396 asdl_seq *s;
3397 int i;
3398
3399 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003400 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003401 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003402 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003403 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003404 name = NEW_IDENTIFIER(CHILD(n, i));
3405 if (!name)
3406 return NULL;
3407 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003408 }
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003409 return Global(s, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003410}
3411
3412static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003413ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3414{
3415 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3416 identifier name;
3417 asdl_seq *s;
3418 int i;
3419
3420 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003421 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003422 if (!s)
3423 return NULL;
3424 for (i = 1; i < NCH(n); i += 2) {
3425 name = NEW_IDENTIFIER(CHILD(n, i));
3426 if (!name)
3427 return NULL;
3428 asdl_seq_SET(s, i / 2, name);
3429 }
3430 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena);
3431}
3432
3433static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003434ast_for_assert_stmt(struct compiling *c, const node *n)
3435{
3436 /* assert_stmt: 'assert' test [',' test] */
3437 REQ(n, assert_stmt);
3438 if (NCH(n) == 2) {
3439 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3440 if (!expression)
3441 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003442 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003443 }
3444 else if (NCH(n) == 4) {
3445 expr_ty expr1, expr2;
3446
3447 expr1 = ast_for_expr(c, CHILD(n, 1));
3448 if (!expr1)
3449 return NULL;
3450 expr2 = ast_for_expr(c, CHILD(n, 3));
3451 if (!expr2)
3452 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003453
Thomas Wouters89f507f2006-12-13 04:49:30 +00003454 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003455 }
Neal Norwitz79792652005-11-14 04:25:03 +00003456 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003457 "improper number of parts to 'assert' statement: %d",
3458 NCH(n));
3459 return NULL;
3460}
3461
3462static asdl_seq *
3463ast_for_suite(struct compiling *c, const node *n)
3464{
3465 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003466 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003467 stmt_ty s;
3468 int i, total, num, end, pos = 0;
3469 node *ch;
3470
3471 REQ(n, suite);
3472
3473 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003474 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003475 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003476 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003477 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003478 n = CHILD(n, 0);
3479 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003481 */
3482 end = NCH(n) - 1;
3483 if (TYPE(CHILD(n, end - 1)) == SEMI)
3484 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003485 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003486 for (i = 0; i < end; i += 2) {
3487 ch = CHILD(n, i);
3488 s = ast_for_stmt(c, ch);
3489 if (!s)
3490 return NULL;
3491 asdl_seq_SET(seq, pos++, s);
3492 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003493 }
3494 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003495 for (i = 2; i < (NCH(n) - 1); i++) {
3496 ch = CHILD(n, i);
3497 REQ(ch, stmt);
3498 num = num_stmts(ch);
3499 if (num == 1) {
3500 /* small_stmt or compound_stmt with only one child */
3501 s = ast_for_stmt(c, ch);
3502 if (!s)
3503 return NULL;
3504 asdl_seq_SET(seq, pos++, s);
3505 }
3506 else {
3507 int j;
3508 ch = CHILD(ch, 0);
3509 REQ(ch, simple_stmt);
3510 for (j = 0; j < NCH(ch); j += 2) {
3511 /* statement terminates with a semi-colon ';' */
3512 if (NCH(CHILD(ch, j)) == 0) {
3513 assert((j + 1) == NCH(ch));
3514 break;
3515 }
3516 s = ast_for_stmt(c, CHILD(ch, j));
3517 if (!s)
3518 return NULL;
3519 asdl_seq_SET(seq, pos++, s);
3520 }
3521 }
3522 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003523 }
3524 assert(pos == seq->size);
3525 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003526}
3527
3528static stmt_ty
3529ast_for_if_stmt(struct compiling *c, const node *n)
3530{
3531 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3532 ['else' ':' suite]
3533 */
3534 char *s;
3535
3536 REQ(n, if_stmt);
3537
3538 if (NCH(n) == 4) {
3539 expr_ty expression;
3540 asdl_seq *suite_seq;
3541
3542 expression = ast_for_expr(c, CHILD(n, 1));
3543 if (!expression)
3544 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003545 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003546 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003547 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548
Guido van Rossumd8faa362007-04-27 19:54:29 +00003549 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3550 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003551 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003552
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003553 s = STR(CHILD(n, 4));
3554 /* s[2], the third character in the string, will be
3555 's' for el_s_e, or
3556 'i' for el_i_f
3557 */
3558 if (s[2] == 's') {
3559 expr_ty expression;
3560 asdl_seq *seq1, *seq2;
3561
3562 expression = ast_for_expr(c, CHILD(n, 1));
3563 if (!expression)
3564 return NULL;
3565 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003566 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003567 return NULL;
3568 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003569 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003570 return NULL;
3571
Guido van Rossumd8faa362007-04-27 19:54:29 +00003572 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3573 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003574 }
3575 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003576 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003577 expr_ty expression;
3578 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003579 asdl_seq *orelse = NULL;
3580 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003581 /* must reference the child n_elif+1 since 'else' token is third,
3582 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003583 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3584 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3585 has_else = 1;
3586 n_elif -= 3;
3587 }
3588 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003589
Thomas Wouters89f507f2006-12-13 04:49:30 +00003590 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003591 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003592
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003593 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003594 if (!orelse)
3595 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003596 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003597 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003599 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3600 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003601 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003602 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3603 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003604 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003606 asdl_seq_SET(orelse, 0,
3607 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003608 LINENO(CHILD(n, NCH(n) - 6)),
3609 CHILD(n, NCH(n) - 6)->n_col_offset,
3610 c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003611 /* the just-created orelse handled the last elif */
3612 n_elif--;
3613 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003614
Thomas Wouters89f507f2006-12-13 04:49:30 +00003615 for (i = 0; i < n_elif; i++) {
3616 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003617 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003618 if (!newobj)
3619 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003621 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003623 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003624 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003625 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003626
Thomas Wouters89f507f2006-12-13 04:49:30 +00003627 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003628 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003629 LINENO(CHILD(n, off)),
3630 CHILD(n, off)->n_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003631 orelse = newobj;
3632 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003633 expression = ast_for_expr(c, CHILD(n, 1));
3634 if (!expression)
3635 return NULL;
3636 suite_seq = ast_for_suite(c, CHILD(n, 3));
3637 if (!suite_seq)
3638 return NULL;
3639 return If(expression, suite_seq, orelse,
3640 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003641 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003642
3643 PyErr_Format(PyExc_SystemError,
3644 "unexpected token in 'if' statement: %s", s);
3645 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003646}
3647
3648static stmt_ty
3649ast_for_while_stmt(struct compiling *c, const node *n)
3650{
3651 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3652 REQ(n, while_stmt);
3653
3654 if (NCH(n) == 4) {
3655 expr_ty expression;
3656 asdl_seq *suite_seq;
3657
3658 expression = ast_for_expr(c, CHILD(n, 1));
3659 if (!expression)
3660 return NULL;
3661 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003662 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003663 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003664 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003665 }
3666 else if (NCH(n) == 7) {
3667 expr_ty expression;
3668 asdl_seq *seq1, *seq2;
3669
3670 expression = ast_for_expr(c, CHILD(n, 1));
3671 if (!expression)
3672 return NULL;
3673 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003674 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003675 return NULL;
3676 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003677 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003678 return NULL;
3679
Thomas Wouters89f507f2006-12-13 04:49:30 +00003680 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003681 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003682
3683 PyErr_Format(PyExc_SystemError,
3684 "wrong number of tokens for 'while' statement: %d",
3685 NCH(n));
3686 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003687}
3688
3689static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04003690ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003691{
guoci90fc8982018-09-11 17:45:45 -04003692 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00003693 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003694 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003695 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003696 const node *node_target;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003697 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3698 REQ(n, for_stmt);
3699
3700 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003701 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003702 if (!seq)
3703 return NULL;
3704 }
3705
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003706 node_target = CHILD(n, 1);
3707 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003708 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003709 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003710 /* Check the # of children rather than the length of _target, since
3711 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003712 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003713 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003714 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003715 else
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003716 target = Tuple(_target, Store, first->lineno, first->col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003717
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003718 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003719 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003720 return NULL;
3721 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003722 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003723 return NULL;
3724
Yury Selivanov75445082015-05-11 22:57:16 -04003725 if (is_async)
3726 return AsyncFor(target, expression, suite_seq, seq,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07003727 LINENO(n0), n0->n_col_offset,
Yury Selivanov75445082015-05-11 22:57:16 -04003728 c->c_arena);
3729 else
3730 return For(target, expression, suite_seq, seq,
3731 LINENO(n), n->n_col_offset,
3732 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003733}
3734
3735static excepthandler_ty
3736ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3737{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003738 /* except_clause: 'except' [test ['as' test]] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003739 REQ(exc, except_clause);
3740 REQ(body, suite);
3741
3742 if (NCH(exc) == 1) {
3743 asdl_seq *suite_seq = ast_for_suite(c, body);
3744 if (!suite_seq)
3745 return NULL;
3746
Neal Norwitzad74aa82008-03-31 05:14:30 +00003747 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003748 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003749 }
3750 else if (NCH(exc) == 2) {
3751 expr_ty expression;
3752 asdl_seq *suite_seq;
3753
3754 expression = ast_for_expr(c, CHILD(exc, 1));
3755 if (!expression)
3756 return NULL;
3757 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003758 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003759 return NULL;
3760
Neal Norwitzad74aa82008-03-31 05:14:30 +00003761 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003762 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003763 }
3764 else if (NCH(exc) == 4) {
3765 asdl_seq *suite_seq;
3766 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003767 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003768 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003769 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003770 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003771 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003772 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003773 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003774 return NULL;
3775 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003776 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003777 return NULL;
3778
Neal Norwitzad74aa82008-03-31 05:14:30 +00003779 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003780 exc->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003781 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003782
3783 PyErr_Format(PyExc_SystemError,
3784 "wrong number of children for 'except' clause: %d",
3785 NCH(exc));
3786 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003787}
3788
3789static stmt_ty
3790ast_for_try_stmt(struct compiling *c, const node *n)
3791{
Neal Norwitzf599f422005-12-17 21:33:47 +00003792 const int nch = NCH(n);
3793 int n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003794 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Neal Norwitzf599f422005-12-17 21:33:47 +00003795
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003796 REQ(n, try_stmt);
3797
Neal Norwitzf599f422005-12-17 21:33:47 +00003798 body = ast_for_suite(c, CHILD(n, 2));
3799 if (body == NULL)
3800 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003801
Neal Norwitzf599f422005-12-17 21:33:47 +00003802 if (TYPE(CHILD(n, nch - 3)) == NAME) {
3803 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
3804 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
3805 /* we can assume it's an "else",
3806 because nch >= 9 for try-else-finally and
3807 it would otherwise have a type of except_clause */
3808 orelse = ast_for_suite(c, CHILD(n, nch - 4));
3809 if (orelse == NULL)
3810 return NULL;
3811 n_except--;
3812 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003813
Neal Norwitzf599f422005-12-17 21:33:47 +00003814 finally = ast_for_suite(c, CHILD(n, nch - 1));
3815 if (finally == NULL)
3816 return NULL;
3817 n_except--;
3818 }
3819 else {
3820 /* we can assume it's an "else",
3821 otherwise it would have a type of except_clause */
3822 orelse = ast_for_suite(c, CHILD(n, nch - 1));
3823 if (orelse == NULL)
3824 return NULL;
3825 n_except--;
3826 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003827 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003828 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003829 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003830 return NULL;
3831 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003832
Neal Norwitzf599f422005-12-17 21:33:47 +00003833 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003834 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00003835 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003836 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00003837 if (handlers == NULL)
3838 return NULL;
3839
3840 for (i = 0; i < n_except; i++) {
3841 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
3842 CHILD(n, 5 + i * 3));
3843 if (!e)
3844 return NULL;
3845 asdl_seq_SET(handlers, i, e);
3846 }
Neal Norwitzf599f422005-12-17 21:33:47 +00003847 }
3848
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003849 assert(finally != NULL || asdl_seq_LEN(handlers));
3850 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003851}
3852
Georg Brandl0c315622009-05-25 21:10:36 +00003853/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003854static withitem_ty
3855ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003856{
3857 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003858
Georg Brandl0c315622009-05-25 21:10:36 +00003859 REQ(n, with_item);
3860 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003861 if (!context_expr)
3862 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00003863 if (NCH(n) == 3) {
3864 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00003865
3866 if (!optional_vars) {
3867 return NULL;
3868 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003869 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003870 return NULL;
3871 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003872 }
3873
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003874 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003875}
3876
Georg Brandl0c315622009-05-25 21:10:36 +00003877/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
3878static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04003879ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00003880{
guoci90fc8982018-09-11 17:45:45 -04003881 const node * const n = is_async ? CHILD(n0, 1) : n0;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003882 int i, n_items;
3883 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00003884
3885 REQ(n, with_stmt);
3886
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003887 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003888 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02003889 if (!items)
3890 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003891 for (i = 1; i < NCH(n) - 2; i += 2) {
3892 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
3893 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00003894 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003895 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00003896 }
3897
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003898 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3899 if (!body)
3900 return NULL;
3901
Yury Selivanov75445082015-05-11 22:57:16 -04003902 if (is_async)
Benjamin Petersond13e59c2018-09-11 15:29:57 -07003903 return AsyncWith(items, body, LINENO(n0), n0->n_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04003904 else
3905 return With(items, body, LINENO(n), n->n_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00003906}
3907
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003908static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003909ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003910{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003911 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00003912 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003913 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003914 expr_ty call;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003915
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003916 REQ(n, classdef);
3917
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003918 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003919 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003920 if (!s)
3921 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003922 classname = NEW_IDENTIFIER(CHILD(n, 1));
3923 if (!classname)
3924 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003925 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003926 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003927 return ClassDef(classname, NULL, NULL, s, decorator_seq,
INADA Naokicb41b272017-02-23 00:31:59 +09003928 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003929 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003930
3931 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003932 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003933 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00003934 return NULL;
3935 classname = NEW_IDENTIFIER(CHILD(n, 1));
3936 if (!classname)
3937 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003938 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003939 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003940 return ClassDef(classname, NULL, NULL, s, decorator_seq,
INADA Naokicb41b272017-02-23 00:31:59 +09003941 LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003942 }
3943
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003944 /* class NAME '(' arglist ')' ':' suite */
3945 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003946 {
3947 PyObject *dummy_name;
3948 expr_ty dummy;
3949 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
3950 if (!dummy_name)
3951 return NULL;
3952 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, c->c_arena);
Serhiy Storchakab619b092018-11-27 09:40:29 +02003953 call = ast_for_call(c, CHILD(n, 3), dummy, NULL);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00003954 if (!call)
3955 return NULL;
3956 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003957 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003958 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003959 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003960 classname = NEW_IDENTIFIER(CHILD(n, 1));
3961 if (!classname)
3962 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003963 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003964 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003965
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003966 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03003967 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003968}
3969
3970static stmt_ty
3971ast_for_stmt(struct compiling *c, const node *n)
3972{
3973 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003974 assert(NCH(n) == 1);
3975 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003976 }
3977 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003978 assert(num_stmts(n) == 1);
3979 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003980 }
3981 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00003982 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003983 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
3984 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00003985 */
3986 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003987 case expr_stmt:
3988 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003989 case del_stmt:
3990 return ast_for_del_stmt(c, n);
3991 case pass_stmt:
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003992 return Pass(LINENO(n), n->n_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003993 case flow_stmt:
3994 return ast_for_flow_stmt(c, n);
3995 case import_stmt:
3996 return ast_for_import_stmt(c, n);
3997 case global_stmt:
3998 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003999 case nonlocal_stmt:
4000 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004001 case assert_stmt:
4002 return ast_for_assert_stmt(c, n);
4003 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004004 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004005 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4006 TYPE(n), NCH(n));
4007 return NULL;
4008 }
4009 }
4010 else {
4011 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004012 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004013 */
4014 node *ch = CHILD(n, 0);
4015 REQ(n, compound_stmt);
4016 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004017 case if_stmt:
4018 return ast_for_if_stmt(c, ch);
4019 case while_stmt:
4020 return ast_for_while_stmt(c, ch);
4021 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004022 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004023 case try_stmt:
4024 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004025 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004026 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004027 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004028 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004029 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004030 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004031 case decorated:
4032 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004033 case async_stmt:
4034 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004035 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004036 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004037 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004038 TYPE(n), NCH(n));
4039 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004040 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004041 }
4042}
4043
4044static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004045parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004046{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004047 const char *end;
4048 long x;
4049 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004050 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004051 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004052
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004053 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004054 errno = 0;
4055 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004056 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004057 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004058 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004059 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004060 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004061 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004062 }
4063 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004064 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004065 if (*end == '\0') {
4066 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004067 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004068 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004069 }
4070 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004071 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004072 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004073 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4074 if (compl.imag == -1.0 && PyErr_Occurred())
4075 return NULL;
4076 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004077 }
4078 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004079 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004080 dx = PyOS_string_to_double(s, NULL, NULL);
4081 if (dx == -1.0 && PyErr_Occurred())
4082 return NULL;
4083 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004084 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004085}
4086
4087static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004088parsenumber(struct compiling *c, const char *s)
4089{
4090 char *dup, *end;
4091 PyObject *res = NULL;
4092
4093 assert(s != NULL);
4094
4095 if (strchr(s, '_') == NULL) {
4096 return parsenumber_raw(c, s);
4097 }
4098 /* Create a duplicate without underscores. */
4099 dup = PyMem_Malloc(strlen(s) + 1);
4100 end = dup;
4101 for (; *s; s++) {
4102 if (*s != '_') {
4103 *end++ = *s;
4104 }
4105 }
4106 *end = '\0';
4107 res = parsenumber_raw(c, dup);
4108 PyMem_Free(dup);
4109 return res;
4110}
4111
4112static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004113decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004114{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004115 const char *s, *t;
4116 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004117 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4118 while (s < end && (*s & 0x80)) s++;
4119 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004120 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004121}
4122
Eric V. Smith56466482016-10-31 14:46:26 -04004123static int
4124warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004125 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004126{
4127 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4128 first_invalid_escape_char);
4129 if (msg == NULL) {
4130 return -1;
4131 }
Serhiy Storchaka65439122018-10-19 17:42:06 +03004132 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004133 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004134 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004135 {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004136 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004137 /* Replace the SyntaxWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004138 to get a more accurate error report */
4139 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004140 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004141 }
4142 Py_DECREF(msg);
4143 return -1;
4144 }
4145 Py_DECREF(msg);
4146 return 0;
4147}
4148
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004149static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004150decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4151 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004152{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004153 PyObject *v, *u;
4154 char *buf;
4155 char *p;
4156 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004157
Benjamin Peterson202803a2016-02-25 22:34:45 -08004158 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004159 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004160 return NULL;
4161 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4162 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4163 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4164 if (u == NULL)
4165 return NULL;
4166 p = buf = PyBytes_AsString(u);
4167 end = s + len;
4168 while (s < end) {
4169 if (*s == '\\') {
4170 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004171 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004172 strcpy(p, "u005c");
4173 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004174 if (s >= end)
4175 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004176 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004177 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004178 if (*s & 0x80) { /* XXX inefficient */
4179 PyObject *w;
4180 int kind;
4181 void *data;
4182 Py_ssize_t len, i;
4183 w = decode_utf8(c, &s, end);
4184 if (w == NULL) {
4185 Py_DECREF(u);
4186 return NULL;
4187 }
4188 kind = PyUnicode_KIND(w);
4189 data = PyUnicode_DATA(w);
4190 len = PyUnicode_GET_LENGTH(w);
4191 for (i = 0; i < len; i++) {
4192 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4193 sprintf(p, "\\U%08x", chr);
4194 p += 10;
4195 }
4196 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004197 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004198 Py_DECREF(w);
4199 } else {
4200 *p++ = *s++;
4201 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004202 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004203 len = p - buf;
4204 s = buf;
4205
Eric V. Smith56466482016-10-31 14:46:26 -04004206 const char *first_invalid_escape;
4207 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4208
4209 if (v != NULL && first_invalid_escape != NULL) {
4210 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4211 /* We have not decref u before because first_invalid_escape points
4212 inside u. */
4213 Py_XDECREF(u);
4214 Py_DECREF(v);
4215 return NULL;
4216 }
4217 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004218 Py_XDECREF(u);
4219 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004220}
4221
Eric V. Smith56466482016-10-31 14:46:26 -04004222static PyObject *
4223decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4224 size_t len)
4225{
4226 const char *first_invalid_escape;
4227 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4228 &first_invalid_escape);
4229 if (result == NULL)
4230 return NULL;
4231
4232 if (first_invalid_escape != NULL) {
4233 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4234 Py_DECREF(result);
4235 return NULL;
4236 }
4237 }
4238 return result;
4239}
4240
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004241/* Shift locations for the given node and all its children by adding `lineno`
4242 and `col_offset` to existing locations. */
4243static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4244{
4245 n->n_col_offset = n->n_col_offset + col_offset;
4246 for (int i = 0; i < NCH(n); ++i) {
4247 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4248 /* Shifting column offsets unnecessary if there's been newlines. */
4249 col_offset = 0;
4250 }
4251 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4252 }
4253 n->n_lineno = n->n_lineno + lineno;
4254}
4255
4256/* Fix locations for the given node and its children.
4257
4258 `parent` is the enclosing node.
4259 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004260 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004261*/
4262static void
4263fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4264{
4265 char *substr = NULL;
4266 char *start;
4267 int lines = LINENO(parent) - 1;
4268 int cols = parent->n_col_offset;
4269 /* Find the full fstring to fix location information in `n`. */
4270 while (parent && parent->n_type != STRING)
4271 parent = parent->n_child;
4272 if (parent && parent->n_str) {
4273 substr = strstr(parent->n_str, expr_str);
4274 if (substr) {
4275 start = substr;
4276 while (start > parent->n_str) {
4277 if (start[0] == '\n')
4278 break;
4279 start--;
4280 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004281 cols += (int)(substr - start);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004282 /* Fix lineno in mulitline strings. */
4283 while ((substr = strchr(substr + 1, '\n')))
4284 lines--;
4285 }
4286 }
4287 fstring_shift_node_locations(n, lines, cols);
4288}
4289
Eric V. Smith451d0e32016-09-09 21:56:20 -04004290/* Compile this expression in to an expr_ty. Add parens around the
4291 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004292static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004293fstring_compile_expr(const char *expr_start, const char *expr_end,
4294 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004295
Eric V. Smith235a6f02015-09-19 14:51:32 -04004296{
4297 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004298 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004299 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004300 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004301 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004302 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004303
Eric V. Smith1d44c412015-09-23 07:49:00 -04004304 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004305 assert(*(expr_start-1) == '{');
4306 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004307
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004308 /* If the substring is all whitespace, it's an error. We need to catch this
4309 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4310 because turning the expression '' in to '()' would go from being invalid
4311 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004312 for (s = expr_start; s != expr_end; s++) {
4313 char c = *s;
4314 /* The Python parser ignores only the following whitespace
4315 characters (\r already is converted to \n). */
4316 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004317 break;
4318 }
4319 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004320 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004321 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004322 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004323 }
4324
Eric V. Smith451d0e32016-09-09 21:56:20 -04004325 len = expr_end - expr_start;
4326 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4327 str = PyMem_RawMalloc(len + 3);
4328 if (str == NULL)
4329 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004330
Eric V. Smith451d0e32016-09-09 21:56:20 -04004331 str[0] = '(';
4332 memcpy(str+1, expr_start, len);
4333 str[len+1] = ')';
4334 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004335
4336 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004337 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4338 Py_eval_input, 0);
4339 if (!mod_n) {
4340 PyMem_RawFree(str);
4341 return NULL;
4342 }
4343 /* Reuse str to find the correct column offset. */
4344 str[0] = '{';
4345 str[len+1] = '}';
4346 fstring_fix_node_location(n, mod_n, str);
4347 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004348 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004349 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004350 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004351 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004352 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004353}
4354
4355/* Return -1 on error.
4356
4357 Return 0 if we reached the end of the literal.
4358
4359 Return 1 if we haven't reached the end of the literal, but we want
4360 the caller to process the literal up to this point. Used for
4361 doubled braces.
4362*/
4363static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004364fstring_find_literal(const char **str, const char *end, int raw,
4365 PyObject **literal, int recurse_lvl,
4366 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004367{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004368 /* Get any literal string. It ends when we hit an un-doubled left
4369 brace (which isn't part of a unicode name escape such as
4370 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004371
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004372 const char *s = *str;
4373 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004374 int result = 0;
4375
Eric V. Smith235a6f02015-09-19 14:51:32 -04004376 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004377 while (s < end) {
4378 char ch = *s++;
4379 if (!raw && ch == '\\' && s < end) {
4380 ch = *s++;
4381 if (ch == 'N') {
4382 if (s < end && *s++ == '{') {
4383 while (s < end && *s++ != '}') {
4384 }
4385 continue;
4386 }
4387 break;
4388 }
4389 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4390 return -1;
4391 }
4392 }
4393 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004394 /* Check for doubled braces, but only at the top level. If
4395 we checked at every level, then f'{0:{3}}' would fail
4396 with the two closing braces. */
4397 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004398 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004399 /* We're going to tell the caller that the literal ends
4400 here, but that they should continue scanning. But also
4401 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004402 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004403 result = 1;
4404 goto done;
4405 }
4406
4407 /* Where a single '{' is the start of a new expression, a
4408 single '}' is not allowed. */
4409 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004410 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004411 ast_error(c, n, "f-string: single '}' is not allowed");
4412 return -1;
4413 }
4414 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004415 /* We're either at a '{', which means we're starting another
4416 expression; or a '}', which means we're at the end of this
4417 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004418 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004419 break;
4420 }
4421 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004422 *str = s;
4423 assert(s <= end);
4424 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004425done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004426 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004427 if (raw)
4428 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004429 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004430 NULL, NULL);
4431 else
Eric V. Smith56466482016-10-31 14:46:26 -04004432 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004433 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004434 if (!*literal)
4435 return -1;
4436 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004437 return result;
4438}
4439
4440/* Forward declaration because parsing is recursive. */
4441static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004442fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004443 struct compiling *c, const node *n);
4444
Eric V. Smith451d0e32016-09-09 21:56:20 -04004445/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004446 expression (so it must be a '{'). Returns the FormattedValue node,
4447 which includes the expression, conversion character, and
4448 format_spec expression.
4449
4450 Note that I don't do a perfect job here: I don't make sure that a
4451 closing brace doesn't match an opening paren, for example. It
4452 doesn't need to error on all invalid expressions, just correctly
4453 find the end of all valid ones. Any errors inside the expression
4454 will be caught when we parse it later. */
4455static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004456fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004457 expr_ty *expression, struct compiling *c, const node *n)
4458{
4459 /* Return -1 on error, else 0. */
4460
Eric V. Smith451d0e32016-09-09 21:56:20 -04004461 const char *expr_start;
4462 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004463 expr_ty simple_expression;
4464 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004465 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004466
4467 /* 0 if we're not in a string, else the quote char we're trying to
4468 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004469 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004470
4471 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4472 int string_type = 0;
4473
4474 /* Keep track of nesting level for braces/parens/brackets in
4475 expressions. */
4476 Py_ssize_t nested_depth = 0;
4477
4478 /* Can only nest one level deep. */
4479 if (recurse_lvl >= 2) {
4480 ast_error(c, n, "f-string: expressions nested too deeply");
4481 return -1;
4482 }
4483
4484 /* The first char must be a left brace, or we wouldn't have gotten
4485 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004486 assert(**str == '{');
4487 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004488
Eric V. Smith451d0e32016-09-09 21:56:20 -04004489 expr_start = *str;
4490 for (; *str < end; (*str)++) {
4491 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004492
4493 /* Loop invariants. */
4494 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004495 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004496 if (quote_char)
4497 assert(string_type == 1 || string_type == 3);
4498 else
4499 assert(string_type == 0);
4500
Eric V. Smith451d0e32016-09-09 21:56:20 -04004501 ch = **str;
4502 /* Nowhere inside an expression is a backslash allowed. */
4503 if (ch == '\\') {
4504 /* Error: can't include a backslash character, inside
4505 parens or strings or not. */
4506 ast_error(c, n, "f-string expression part "
4507 "cannot include a backslash");
4508 return -1;
4509 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004510 if (quote_char) {
4511 /* We're inside a string. See if we're at the end. */
4512 /* This code needs to implement the same non-error logic
4513 as tok_get from tokenizer.c, at the letter_quote
4514 label. To actually share that code would be a
4515 nightmare. But, it's unlikely to change and is small,
4516 so duplicate it here. Note we don't need to catch all
4517 of the errors, since they'll be caught when parsing the
4518 expression. We just need to match the non-error
4519 cases. Thus we can ignore \n in single-quoted strings,
4520 for example. Or non-terminated strings. */
4521 if (ch == quote_char) {
4522 /* Does this match the string_type (single or triple
4523 quoted)? */
4524 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004525 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004526 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004527 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004528 string_type = 0;
4529 quote_char = 0;
4530 continue;
4531 }
4532 } else {
4533 /* We're at the end of a normal string. */
4534 quote_char = 0;
4535 string_type = 0;
4536 continue;
4537 }
4538 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004539 } else if (ch == '\'' || ch == '"') {
4540 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004541 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004542 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004543 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004544 } else {
4545 /* Start of a normal string. */
4546 string_type = 1;
4547 }
4548 /* Start looking for the end of the string. */
4549 quote_char = ch;
4550 } else if (ch == '[' || ch == '{' || ch == '(') {
4551 nested_depth++;
4552 } else if (nested_depth != 0 &&
4553 (ch == ']' || ch == '}' || ch == ')')) {
4554 nested_depth--;
4555 } else if (ch == '#') {
4556 /* Error: can't include a comment character, inside parens
4557 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004558 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004559 return -1;
4560 } else if (nested_depth == 0 &&
4561 (ch == '!' || ch == ':' || ch == '}')) {
4562 /* First, test for the special case of "!=". Since '=' is
4563 not an allowed conversion character, nothing is lost in
4564 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004565 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004566 /* This isn't a conversion character, just continue. */
4567 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004568 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004569 /* Normal way out of this loop. */
4570 break;
4571 } else {
4572 /* Just consume this char and loop around. */
4573 }
4574 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004575 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004576 /* If we leave this loop in a string or with mismatched parens, we
4577 don't care. We'll get a syntax error when compiling the
4578 expression. But, we can produce a better error message, so
4579 let's just do that.*/
4580 if (quote_char) {
4581 ast_error(c, n, "f-string: unterminated string");
4582 return -1;
4583 }
4584 if (nested_depth) {
4585 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4586 return -1;
4587 }
4588
Eric V. Smith451d0e32016-09-09 21:56:20 -04004589 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004590 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004591
4592 /* Compile the expression as soon as possible, so we show errors
4593 related to the expression before errors related to the
4594 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004595 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004596 if (!simple_expression)
4597 return -1;
4598
4599 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004600 if (**str == '!') {
4601 *str += 1;
4602 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004603 goto unexpected_end_of_string;
4604
Eric V. Smith451d0e32016-09-09 21:56:20 -04004605 conversion = **str;
4606 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004607
4608 /* Validate the conversion. */
4609 if (!(conversion == 's' || conversion == 'r'
4610 || conversion == 'a')) {
4611 ast_error(c, n, "f-string: invalid conversion character: "
4612 "expected 's', 'r', or 'a'");
4613 return -1;
4614 }
4615 }
4616
4617 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004618 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004619 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004620 if (**str == ':') {
4621 *str += 1;
4622 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004623 goto unexpected_end_of_string;
4624
4625 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004626 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004627 if (!format_spec)
4628 return -1;
4629 }
4630
Eric V. Smith451d0e32016-09-09 21:56:20 -04004631 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004632 goto unexpected_end_of_string;
4633
4634 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004635 assert(*str < end);
4636 assert(**str == '}');
4637 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004638
Eric V. Smith451d0e32016-09-09 21:56:20 -04004639 /* And now create the FormattedValue node that represents this
4640 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004641 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004642 format_spec, LINENO(n), n->n_col_offset,
4643 c->c_arena);
4644 if (!*expression)
4645 return -1;
4646
4647 return 0;
4648
4649unexpected_end_of_string:
4650 ast_error(c, n, "f-string: expecting '}'");
4651 return -1;
4652}
4653
4654/* Return -1 on error.
4655
4656 Return 0 if we have a literal (possible zero length) and an
4657 expression (zero length if at the end of the string.
4658
4659 Return 1 if we have a literal, but no expression, and we want the
4660 caller to call us again. This is used to deal with doubled
4661 braces.
4662
4663 When called multiple times on the string 'a{{b{0}c', this function
4664 will return:
4665
4666 1. the literal 'a{' with no expression, and a return value
4667 of 1. Despite the fact that there's no expression, the return
4668 value of 1 means we're not finished yet.
4669
4670 2. the literal 'b' and the expression '0', with a return value of
4671 0. The fact that there's an expression means we're not finished.
4672
4673 3. literal 'c' with no expression and a return value of 0. The
4674 combination of the return value of 0 with no expression means
4675 we're finished.
4676*/
4677static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004678fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4679 int recurse_lvl, PyObject **literal,
4680 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004681 struct compiling *c, const node *n)
4682{
4683 int result;
4684
4685 assert(*literal == NULL && *expression == NULL);
4686
4687 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004688 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004689 if (result < 0)
4690 goto error;
4691
4692 assert(result == 0 || result == 1);
4693
4694 if (result == 1)
4695 /* We have a literal, but don't look at the expression. */
4696 return 1;
4697
Eric V. Smith451d0e32016-09-09 21:56:20 -04004698 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004699 /* We're at the end of the string or the end of a nested
4700 f-string: no expression. The top-level error case where we
4701 expect to be at the end of the string but we're at a '}' is
4702 handled later. */
4703 return 0;
4704
4705 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004706 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004707
Eric V. Smith451d0e32016-09-09 21:56:20 -04004708 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004709 goto error;
4710
4711 return 0;
4712
4713error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004714 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004715 return -1;
4716}
4717
4718#define EXPRLIST_N_CACHED 64
4719
4720typedef struct {
4721 /* Incrementally build an array of expr_ty, so be used in an
4722 asdl_seq. Cache some small but reasonably sized number of
4723 expr_ty's, and then after that start dynamically allocating,
4724 doubling the number allocated each time. Note that the f-string
4725 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004726 Constant for the literal 'a'. So you add expr_ty's about twice as
Eric V. Smith235a6f02015-09-19 14:51:32 -04004727 fast as you add exressions in an f-string. */
4728
4729 Py_ssize_t allocated; /* Number we've allocated. */
4730 Py_ssize_t size; /* Number we've used. */
4731 expr_ty *p; /* Pointer to the memory we're actually
4732 using. Will point to 'data' until we
4733 start dynamically allocating. */
4734 expr_ty data[EXPRLIST_N_CACHED];
4735} ExprList;
4736
4737#ifdef NDEBUG
4738#define ExprList_check_invariants(l)
4739#else
4740static void
4741ExprList_check_invariants(ExprList *l)
4742{
4743 /* Check our invariants. Make sure this object is "live", and
4744 hasn't been deallocated. */
4745 assert(l->size >= 0);
4746 assert(l->p != NULL);
4747 if (l->size <= EXPRLIST_N_CACHED)
4748 assert(l->data == l->p);
4749}
4750#endif
4751
4752static void
4753ExprList_Init(ExprList *l)
4754{
4755 l->allocated = EXPRLIST_N_CACHED;
4756 l->size = 0;
4757
4758 /* Until we start allocating dynamically, p points to data. */
4759 l->p = l->data;
4760
4761 ExprList_check_invariants(l);
4762}
4763
4764static int
4765ExprList_Append(ExprList *l, expr_ty exp)
4766{
4767 ExprList_check_invariants(l);
4768 if (l->size >= l->allocated) {
4769 /* We need to alloc (or realloc) the memory. */
4770 Py_ssize_t new_size = l->allocated * 2;
4771
4772 /* See if we've ever allocated anything dynamically. */
4773 if (l->p == l->data) {
4774 Py_ssize_t i;
4775 /* We're still using the cached data. Switch to
4776 alloc-ing. */
4777 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4778 if (!l->p)
4779 return -1;
4780 /* Copy the cached data into the new buffer. */
4781 for (i = 0; i < l->size; i++)
4782 l->p[i] = l->data[i];
4783 } else {
4784 /* Just realloc. */
4785 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4786 if (!tmp) {
4787 PyMem_RawFree(l->p);
4788 l->p = NULL;
4789 return -1;
4790 }
4791 l->p = tmp;
4792 }
4793
4794 l->allocated = new_size;
4795 assert(l->allocated == 2 * l->size);
4796 }
4797
4798 l->p[l->size++] = exp;
4799
4800 ExprList_check_invariants(l);
4801 return 0;
4802}
4803
4804static void
4805ExprList_Dealloc(ExprList *l)
4806{
4807 ExprList_check_invariants(l);
4808
4809 /* If there's been an error, or we've never dynamically allocated,
4810 do nothing. */
4811 if (!l->p || l->p == l->data) {
4812 /* Do nothing. */
4813 } else {
4814 /* We have dynamically allocated. Free the memory. */
4815 PyMem_RawFree(l->p);
4816 }
4817 l->p = NULL;
4818 l->size = -1;
4819}
4820
4821static asdl_seq *
4822ExprList_Finish(ExprList *l, PyArena *arena)
4823{
4824 asdl_seq *seq;
4825
4826 ExprList_check_invariants(l);
4827
4828 /* Allocate the asdl_seq and copy the expressions in to it. */
4829 seq = _Py_asdl_seq_new(l->size, arena);
4830 if (seq) {
4831 Py_ssize_t i;
4832 for (i = 0; i < l->size; i++)
4833 asdl_seq_SET(seq, i, l->p[i]);
4834 }
4835 ExprList_Dealloc(l);
4836 return seq;
4837}
4838
4839/* The FstringParser is designed to add a mix of strings and
4840 f-strings, and concat them together as needed. Ultimately, it
4841 generates an expr_ty. */
4842typedef struct {
4843 PyObject *last_str;
4844 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004845 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004846} FstringParser;
4847
4848#ifdef NDEBUG
4849#define FstringParser_check_invariants(state)
4850#else
4851static void
4852FstringParser_check_invariants(FstringParser *state)
4853{
4854 if (state->last_str)
4855 assert(PyUnicode_CheckExact(state->last_str));
4856 ExprList_check_invariants(&state->expr_list);
4857}
4858#endif
4859
4860static void
4861FstringParser_Init(FstringParser *state)
4862{
4863 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004864 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004865 ExprList_Init(&state->expr_list);
4866 FstringParser_check_invariants(state);
4867}
4868
4869static void
4870FstringParser_Dealloc(FstringParser *state)
4871{
4872 FstringParser_check_invariants(state);
4873
4874 Py_XDECREF(state->last_str);
4875 ExprList_Dealloc(&state->expr_list);
4876}
4877
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004878/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004879static expr_ty
4880make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4881{
4882 PyObject *s = *str;
4883 *str = NULL;
4884 assert(PyUnicode_CheckExact(s));
4885 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4886 Py_DECREF(s);
4887 return NULL;
4888 }
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004889 return Constant(s, LINENO(n), n->n_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004890}
4891
4892/* Add a non-f-string (that is, a regular literal string). str is
4893 decref'd. */
4894static int
4895FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4896{
4897 FstringParser_check_invariants(state);
4898
4899 assert(PyUnicode_CheckExact(str));
4900
4901 if (PyUnicode_GET_LENGTH(str) == 0) {
4902 Py_DECREF(str);
4903 return 0;
4904 }
4905
4906 if (!state->last_str) {
4907 /* We didn't have a string before, so just remember this one. */
4908 state->last_str = str;
4909 } else {
4910 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004911 PyUnicode_AppendAndDel(&state->last_str, str);
4912 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004913 return -1;
4914 }
4915 FstringParser_check_invariants(state);
4916 return 0;
4917}
4918
Eric V. Smith451d0e32016-09-09 21:56:20 -04004919/* Parse an f-string. The f-string is in *str to end, with no
4920 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004921static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004922FstringParser_ConcatFstring(FstringParser *state, const char **str,
4923 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004924 struct compiling *c, const node *n)
4925{
4926 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004927 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004928
4929 /* Parse the f-string. */
4930 while (1) {
4931 PyObject *literal = NULL;
4932 expr_ty expression = NULL;
4933
4934 /* If there's a zero length literal in front of the
4935 expression, literal will be NULL. If we're at the end of
4936 the f-string, expression will be NULL (unless result == 1,
4937 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004938 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004939 &literal, &expression,
4940 c, n);
4941 if (result < 0)
4942 return -1;
4943
4944 /* Add the literal, if any. */
4945 if (!literal) {
4946 /* Do nothing. Just leave last_str alone (and possibly
4947 NULL). */
4948 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04004949 /* Note that the literal can be zero length, if the
4950 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004951 state->last_str = literal;
4952 literal = NULL;
4953 } else {
4954 /* We have a literal, concatenate it. */
4955 assert(PyUnicode_GET_LENGTH(literal) != 0);
4956 if (FstringParser_ConcatAndDel(state, literal) < 0)
4957 return -1;
4958 literal = NULL;
4959 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004960
4961 /* We've dealt with the literal now. It can't be leaked on further
4962 errors. */
4963 assert(literal == NULL);
4964
4965 /* See if we should just loop around to get the next literal
4966 and expression, while ignoring the expression this
4967 time. This is used for un-doubling braces, as an
4968 optimization. */
4969 if (result == 1)
4970 continue;
4971
4972 if (!expression)
4973 /* We're done with this f-string. */
4974 break;
4975
4976 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004977 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004978 if (!state->last_str) {
4979 /* Do nothing. No previous literal. */
4980 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004981 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004982 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4983 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4984 return -1;
4985 }
4986
4987 if (ExprList_Append(&state->expr_list, expression) < 0)
4988 return -1;
4989 }
4990
Eric V. Smith235a6f02015-09-19 14:51:32 -04004991 /* If recurse_lvl is zero, then we must be at the end of the
4992 string. Otherwise, we must be at a right brace. */
4993
Eric V. Smith451d0e32016-09-09 21:56:20 -04004994 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004995 ast_error(c, n, "f-string: unexpected end of string");
4996 return -1;
4997 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004998 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004999 ast_error(c, n, "f-string: expecting '}'");
5000 return -1;
5001 }
5002
5003 FstringParser_check_invariants(state);
5004 return 0;
5005}
5006
5007/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005008 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005009static expr_ty
5010FstringParser_Finish(FstringParser *state, struct compiling *c,
5011 const node *n)
5012{
5013 asdl_seq *seq;
5014
5015 FstringParser_check_invariants(state);
5016
5017 /* If we're just a constant string with no expressions, return
5018 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005019 if (!state->fmode) {
5020 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005021 if (!state->last_str) {
5022 /* Create a zero length string. */
5023 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5024 if (!state->last_str)
5025 goto error;
5026 }
5027 return make_str_node_and_del(&state->last_str, c, n);
5028 }
5029
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005030 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005031 last node in our expression list. */
5032 if (state->last_str) {
5033 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5034 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5035 goto error;
5036 }
5037 /* This has already been freed. */
5038 assert(state->last_str == NULL);
5039
5040 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5041 if (!seq)
5042 goto error;
5043
Eric V. Smith235a6f02015-09-19 14:51:32 -04005044 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
5045
5046error:
5047 FstringParser_Dealloc(state);
5048 return NULL;
5049}
5050
Eric V. Smith451d0e32016-09-09 21:56:20 -04005051/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5052 at end, parse it into an expr_ty. Return NULL on error. Adjust
5053 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005054static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005055fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005056 struct compiling *c, const node *n)
5057{
5058 FstringParser state;
5059
5060 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005061 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005062 c, n) < 0) {
5063 FstringParser_Dealloc(&state);
5064 return NULL;
5065 }
5066
5067 return FstringParser_Finish(&state, c, n);
5068}
5069
5070/* n is a Python string literal, including the bracketing quote
5071 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005072 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005073 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005074 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5075 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005076*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005077static int
5078parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5079 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005080{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005081 size_t len;
5082 const char *s = STR(n);
5083 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005084 int fmode = 0;
5085 *bytesmode = 0;
5086 *rawmode = 0;
5087 *result = NULL;
5088 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005089 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005090 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005091 if (quote == 'b' || quote == 'B') {
5092 quote = *++s;
5093 *bytesmode = 1;
5094 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005095 else if (quote == 'u' || quote == 'U') {
5096 quote = *++s;
5097 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005098 else if (quote == 'r' || quote == 'R') {
5099 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005100 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005101 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005102 else if (quote == 'f' || quote == 'F') {
5103 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005104 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005105 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005106 else {
5107 break;
5108 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005109 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005110 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005111 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005112 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005113 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005114 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005115 if (quote != '\'' && quote != '\"') {
5116 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005117 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005118 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005119 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005120 s++;
5121 len = strlen(s);
5122 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005123 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005124 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005125 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005126 }
5127 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005128 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005129 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005130 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005131 }
5132 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005133 /* A triple quoted string. We've already skipped one quote at
5134 the start and one at the end of the string. Now skip the
5135 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005136 s += 2;
5137 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005138 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005139 if (s[--len] != quote || s[--len] != quote) {
5140 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005141 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005142 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005143 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005144
Eric V. Smith451d0e32016-09-09 21:56:20 -04005145 if (fmode) {
5146 /* Just return the bytes. The caller will parse the resulting
5147 string. */
5148 *fstr = s;
5149 *fstrlen = len;
5150 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005151 }
5152
Eric V. Smith451d0e32016-09-09 21:56:20 -04005153 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005154 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005155 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005156 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005157 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005158 const char *ch;
5159 for (ch = s; *ch; ch++) {
5160 if (Py_CHARMASK(*ch) >= 0x80) {
5161 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005162 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005163 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005164 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005165 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005166 if (*rawmode)
5167 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005168 else
Eric V. Smith56466482016-10-31 14:46:26 -04005169 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005170 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005171 if (*rawmode)
5172 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005173 else
Eric V. Smith56466482016-10-31 14:46:26 -04005174 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005175 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005176 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005177}
5178
Eric V. Smith235a6f02015-09-19 14:51:32 -04005179/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5180 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005181 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005182 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005183 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005184 node if there's just an f-string (with no leading or trailing
5185 literals), or a JoinedStr node if there are multiple f-strings or
5186 any literals involved. */
5187static expr_ty
5188parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005189{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005190 int bytesmode = 0;
5191 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005192 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005193
5194 FstringParser state;
5195 FstringParser_Init(&state);
5196
5197 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005198 int this_bytesmode;
5199 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005200 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005201 const char *fstr;
5202 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005203
5204 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005205 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5206 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005207 goto error;
5208
5209 /* Check that we're not mixing bytes with unicode. */
5210 if (i != 0 && bytesmode != this_bytesmode) {
5211 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005212 /* s is NULL if the current string part is an f-string. */
5213 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005214 goto error;
5215 }
5216 bytesmode = this_bytesmode;
5217
Eric V. Smith451d0e32016-09-09 21:56:20 -04005218 if (fstr != NULL) {
5219 int result;
5220 assert(s == NULL && !bytesmode);
5221 /* This is an f-string. Parse and concatenate it. */
5222 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5223 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005224 if (result < 0)
5225 goto error;
5226 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005227 /* A string or byte string. */
5228 assert(s != NULL && fstr == NULL);
5229
Eric V. Smith451d0e32016-09-09 21:56:20 -04005230 assert(bytesmode ? PyBytes_CheckExact(s) :
5231 PyUnicode_CheckExact(s));
5232
Eric V. Smith451d0e32016-09-09 21:56:20 -04005233 if (bytesmode) {
5234 /* For bytes, concat as we go. */
5235 if (i == 0) {
5236 /* First time, just remember this value. */
5237 bytes_str = s;
5238 } else {
5239 PyBytes_ConcatAndDel(&bytes_str, s);
5240 if (!bytes_str)
5241 goto error;
5242 }
5243 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005244 /* This is a regular string. Concatenate it. */
5245 if (FstringParser_ConcatAndDel(&state, s) < 0)
5246 goto error;
5247 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005248 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005249 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005250 if (bytesmode) {
5251 /* Just return the bytes object and we're done. */
5252 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5253 goto error;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005254 return Constant(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005255 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005256
Eric V. Smith235a6f02015-09-19 14:51:32 -04005257 /* We're not a bytes string, bytes_str should never have been set. */
5258 assert(bytes_str == NULL);
5259
5260 return FstringParser_Finish(&state, c, n);
5261
5262error:
5263 Py_XDECREF(bytes_str);
5264 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005265 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005266}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005267
5268PyObject *
5269_PyAST_GetDocString(asdl_seq *body)
5270{
5271 if (!asdl_seq_LEN(body)) {
5272 return NULL;
5273 }
5274 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5275 if (st->kind != Expr_kind) {
5276 return NULL;
5277 }
5278 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005279 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5280 return e->v.Constant.value;
5281 }
5282 return NULL;
5283}