blob: 8a305a80ffac47d8d86b9d706325fcda3af9935a [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);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004100 if (dup == NULL) {
4101 return PyErr_NoMemory();
4102 }
Brett Cannona721aba2016-09-09 14:57:09 -07004103 end = dup;
4104 for (; *s; s++) {
4105 if (*s != '_') {
4106 *end++ = *s;
4107 }
4108 }
4109 *end = '\0';
4110 res = parsenumber_raw(c, dup);
4111 PyMem_Free(dup);
4112 return res;
4113}
4114
4115static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004116decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004117{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004118 const char *s, *t;
4119 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004120 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4121 while (s < end && (*s & 0x80)) s++;
4122 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004123 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004124}
4125
Eric V. Smith56466482016-10-31 14:46:26 -04004126static int
4127warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004128 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004129{
4130 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4131 first_invalid_escape_char);
4132 if (msg == NULL) {
4133 return -1;
4134 }
Serhiy Storchaka65439122018-10-19 17:42:06 +03004135 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004136 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004137 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004138 {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004139 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004140 /* Replace the SyntaxWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004141 to get a more accurate error report */
4142 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004143 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004144 }
4145 Py_DECREF(msg);
4146 return -1;
4147 }
4148 Py_DECREF(msg);
4149 return 0;
4150}
4151
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004152static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004153decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4154 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004155{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004156 PyObject *v, *u;
4157 char *buf;
4158 char *p;
4159 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004160
Benjamin Peterson202803a2016-02-25 22:34:45 -08004161 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004162 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004163 return NULL;
4164 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4165 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4166 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4167 if (u == NULL)
4168 return NULL;
4169 p = buf = PyBytes_AsString(u);
4170 end = s + len;
4171 while (s < end) {
4172 if (*s == '\\') {
4173 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004174 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004175 strcpy(p, "u005c");
4176 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004177 if (s >= end)
4178 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004179 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004180 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004181 if (*s & 0x80) { /* XXX inefficient */
4182 PyObject *w;
4183 int kind;
4184 void *data;
4185 Py_ssize_t len, i;
4186 w = decode_utf8(c, &s, end);
4187 if (w == NULL) {
4188 Py_DECREF(u);
4189 return NULL;
4190 }
4191 kind = PyUnicode_KIND(w);
4192 data = PyUnicode_DATA(w);
4193 len = PyUnicode_GET_LENGTH(w);
4194 for (i = 0; i < len; i++) {
4195 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4196 sprintf(p, "\\U%08x", chr);
4197 p += 10;
4198 }
4199 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004200 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004201 Py_DECREF(w);
4202 } else {
4203 *p++ = *s++;
4204 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004205 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004206 len = p - buf;
4207 s = buf;
4208
Eric V. Smith56466482016-10-31 14:46:26 -04004209 const char *first_invalid_escape;
4210 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4211
4212 if (v != NULL && first_invalid_escape != NULL) {
4213 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4214 /* We have not decref u before because first_invalid_escape points
4215 inside u. */
4216 Py_XDECREF(u);
4217 Py_DECREF(v);
4218 return NULL;
4219 }
4220 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004221 Py_XDECREF(u);
4222 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004223}
4224
Eric V. Smith56466482016-10-31 14:46:26 -04004225static PyObject *
4226decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4227 size_t len)
4228{
4229 const char *first_invalid_escape;
4230 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4231 &first_invalid_escape);
4232 if (result == NULL)
4233 return NULL;
4234
4235 if (first_invalid_escape != NULL) {
4236 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4237 Py_DECREF(result);
4238 return NULL;
4239 }
4240 }
4241 return result;
4242}
4243
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004244/* Shift locations for the given node and all its children by adding `lineno`
4245 and `col_offset` to existing locations. */
4246static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4247{
4248 n->n_col_offset = n->n_col_offset + col_offset;
4249 for (int i = 0; i < NCH(n); ++i) {
4250 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4251 /* Shifting column offsets unnecessary if there's been newlines. */
4252 col_offset = 0;
4253 }
4254 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4255 }
4256 n->n_lineno = n->n_lineno + lineno;
4257}
4258
4259/* Fix locations for the given node and its children.
4260
4261 `parent` is the enclosing node.
4262 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004263 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004264*/
4265static void
4266fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4267{
4268 char *substr = NULL;
4269 char *start;
4270 int lines = LINENO(parent) - 1;
4271 int cols = parent->n_col_offset;
4272 /* Find the full fstring to fix location information in `n`. */
4273 while (parent && parent->n_type != STRING)
4274 parent = parent->n_child;
4275 if (parent && parent->n_str) {
4276 substr = strstr(parent->n_str, expr_str);
4277 if (substr) {
4278 start = substr;
4279 while (start > parent->n_str) {
4280 if (start[0] == '\n')
4281 break;
4282 start--;
4283 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004284 cols += (int)(substr - start);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004285 /* Fix lineno in mulitline strings. */
4286 while ((substr = strchr(substr + 1, '\n')))
4287 lines--;
4288 }
4289 }
4290 fstring_shift_node_locations(n, lines, cols);
4291}
4292
Eric V. Smith451d0e32016-09-09 21:56:20 -04004293/* Compile this expression in to an expr_ty. Add parens around the
4294 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004295static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004296fstring_compile_expr(const char *expr_start, const char *expr_end,
4297 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004298
Eric V. Smith235a6f02015-09-19 14:51:32 -04004299{
4300 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004301 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004302 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004303 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004304 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004305 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004306
Eric V. Smith1d44c412015-09-23 07:49:00 -04004307 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004308 assert(*(expr_start-1) == '{');
4309 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004310
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004311 /* If the substring is all whitespace, it's an error. We need to catch this
4312 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4313 because turning the expression '' in to '()' would go from being invalid
4314 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004315 for (s = expr_start; s != expr_end; s++) {
4316 char c = *s;
4317 /* The Python parser ignores only the following whitespace
4318 characters (\r already is converted to \n). */
4319 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004320 break;
4321 }
4322 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004323 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004324 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004325 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004326 }
4327
Eric V. Smith451d0e32016-09-09 21:56:20 -04004328 len = expr_end - expr_start;
4329 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4330 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004331 if (str == NULL) {
4332 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004333 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004334 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004335
Eric V. Smith451d0e32016-09-09 21:56:20 -04004336 str[0] = '(';
4337 memcpy(str+1, expr_start, len);
4338 str[len+1] = ')';
4339 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004340
4341 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004342 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4343 Py_eval_input, 0);
4344 if (!mod_n) {
4345 PyMem_RawFree(str);
4346 return NULL;
4347 }
4348 /* Reuse str to find the correct column offset. */
4349 str[0] = '{';
4350 str[len+1] = '}';
4351 fstring_fix_node_location(n, mod_n, str);
4352 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004353 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004354 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004355 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004356 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004357 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004358}
4359
4360/* Return -1 on error.
4361
4362 Return 0 if we reached the end of the literal.
4363
4364 Return 1 if we haven't reached the end of the literal, but we want
4365 the caller to process the literal up to this point. Used for
4366 doubled braces.
4367*/
4368static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004369fstring_find_literal(const char **str, const char *end, int raw,
4370 PyObject **literal, int recurse_lvl,
4371 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004372{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004373 /* Get any literal string. It ends when we hit an un-doubled left
4374 brace (which isn't part of a unicode name escape such as
4375 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004376
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004377 const char *s = *str;
4378 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004379 int result = 0;
4380
Eric V. Smith235a6f02015-09-19 14:51:32 -04004381 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004382 while (s < end) {
4383 char ch = *s++;
4384 if (!raw && ch == '\\' && s < end) {
4385 ch = *s++;
4386 if (ch == 'N') {
4387 if (s < end && *s++ == '{') {
4388 while (s < end && *s++ != '}') {
4389 }
4390 continue;
4391 }
4392 break;
4393 }
4394 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4395 return -1;
4396 }
4397 }
4398 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004399 /* Check for doubled braces, but only at the top level. If
4400 we checked at every level, then f'{0:{3}}' would fail
4401 with the two closing braces. */
4402 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004403 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004404 /* We're going to tell the caller that the literal ends
4405 here, but that they should continue scanning. But also
4406 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004407 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004408 result = 1;
4409 goto done;
4410 }
4411
4412 /* Where a single '{' is the start of a new expression, a
4413 single '}' is not allowed. */
4414 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004415 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004416 ast_error(c, n, "f-string: single '}' is not allowed");
4417 return -1;
4418 }
4419 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004420 /* We're either at a '{', which means we're starting another
4421 expression; or a '}', which means we're at the end of this
4422 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004423 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004424 break;
4425 }
4426 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004427 *str = s;
4428 assert(s <= end);
4429 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004430done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004431 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004432 if (raw)
4433 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004434 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004435 NULL, NULL);
4436 else
Eric V. Smith56466482016-10-31 14:46:26 -04004437 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004438 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004439 if (!*literal)
4440 return -1;
4441 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004442 return result;
4443}
4444
4445/* Forward declaration because parsing is recursive. */
4446static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004447fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004448 struct compiling *c, const node *n);
4449
Eric V. Smith451d0e32016-09-09 21:56:20 -04004450/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004451 expression (so it must be a '{'). Returns the FormattedValue node,
4452 which includes the expression, conversion character, and
4453 format_spec expression.
4454
4455 Note that I don't do a perfect job here: I don't make sure that a
4456 closing brace doesn't match an opening paren, for example. It
4457 doesn't need to error on all invalid expressions, just correctly
4458 find the end of all valid ones. Any errors inside the expression
4459 will be caught when we parse it later. */
4460static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004461fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004462 expr_ty *expression, struct compiling *c, const node *n)
4463{
4464 /* Return -1 on error, else 0. */
4465
Eric V. Smith451d0e32016-09-09 21:56:20 -04004466 const char *expr_start;
4467 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004468 expr_ty simple_expression;
4469 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004470 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004471
4472 /* 0 if we're not in a string, else the quote char we're trying to
4473 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004474 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004475
4476 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4477 int string_type = 0;
4478
4479 /* Keep track of nesting level for braces/parens/brackets in
4480 expressions. */
4481 Py_ssize_t nested_depth = 0;
4482
4483 /* Can only nest one level deep. */
4484 if (recurse_lvl >= 2) {
4485 ast_error(c, n, "f-string: expressions nested too deeply");
4486 return -1;
4487 }
4488
4489 /* The first char must be a left brace, or we wouldn't have gotten
4490 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004491 assert(**str == '{');
4492 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004493
Eric V. Smith451d0e32016-09-09 21:56:20 -04004494 expr_start = *str;
4495 for (; *str < end; (*str)++) {
4496 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004497
4498 /* Loop invariants. */
4499 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004500 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004501 if (quote_char)
4502 assert(string_type == 1 || string_type == 3);
4503 else
4504 assert(string_type == 0);
4505
Eric V. Smith451d0e32016-09-09 21:56:20 -04004506 ch = **str;
4507 /* Nowhere inside an expression is a backslash allowed. */
4508 if (ch == '\\') {
4509 /* Error: can't include a backslash character, inside
4510 parens or strings or not. */
4511 ast_error(c, n, "f-string expression part "
4512 "cannot include a backslash");
4513 return -1;
4514 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004515 if (quote_char) {
4516 /* We're inside a string. See if we're at the end. */
4517 /* This code needs to implement the same non-error logic
4518 as tok_get from tokenizer.c, at the letter_quote
4519 label. To actually share that code would be a
4520 nightmare. But, it's unlikely to change and is small,
4521 so duplicate it here. Note we don't need to catch all
4522 of the errors, since they'll be caught when parsing the
4523 expression. We just need to match the non-error
4524 cases. Thus we can ignore \n in single-quoted strings,
4525 for example. Or non-terminated strings. */
4526 if (ch == quote_char) {
4527 /* Does this match the string_type (single or triple
4528 quoted)? */
4529 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004530 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004531 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004532 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004533 string_type = 0;
4534 quote_char = 0;
4535 continue;
4536 }
4537 } else {
4538 /* We're at the end of a normal string. */
4539 quote_char = 0;
4540 string_type = 0;
4541 continue;
4542 }
4543 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004544 } else if (ch == '\'' || ch == '"') {
4545 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004546 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004547 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004548 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004549 } else {
4550 /* Start of a normal string. */
4551 string_type = 1;
4552 }
4553 /* Start looking for the end of the string. */
4554 quote_char = ch;
4555 } else if (ch == '[' || ch == '{' || ch == '(') {
4556 nested_depth++;
4557 } else if (nested_depth != 0 &&
4558 (ch == ']' || ch == '}' || ch == ')')) {
4559 nested_depth--;
4560 } else if (ch == '#') {
4561 /* Error: can't include a comment character, inside parens
4562 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004563 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004564 return -1;
4565 } else if (nested_depth == 0 &&
4566 (ch == '!' || ch == ':' || ch == '}')) {
4567 /* First, test for the special case of "!=". Since '=' is
4568 not an allowed conversion character, nothing is lost in
4569 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004570 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004571 /* This isn't a conversion character, just continue. */
4572 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004573 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004574 /* Normal way out of this loop. */
4575 break;
4576 } else {
4577 /* Just consume this char and loop around. */
4578 }
4579 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004580 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004581 /* If we leave this loop in a string or with mismatched parens, we
4582 don't care. We'll get a syntax error when compiling the
4583 expression. But, we can produce a better error message, so
4584 let's just do that.*/
4585 if (quote_char) {
4586 ast_error(c, n, "f-string: unterminated string");
4587 return -1;
4588 }
4589 if (nested_depth) {
4590 ast_error(c, n, "f-string: mismatched '(', '{', or '['");
4591 return -1;
4592 }
4593
Eric V. Smith451d0e32016-09-09 21:56:20 -04004594 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004595 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004596
4597 /* Compile the expression as soon as possible, so we show errors
4598 related to the expression before errors related to the
4599 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004600 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004601 if (!simple_expression)
4602 return -1;
4603
4604 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004605 if (**str == '!') {
4606 *str += 1;
4607 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004608 goto unexpected_end_of_string;
4609
Eric V. Smith451d0e32016-09-09 21:56:20 -04004610 conversion = **str;
4611 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004612
4613 /* Validate the conversion. */
4614 if (!(conversion == 's' || conversion == 'r'
4615 || conversion == 'a')) {
4616 ast_error(c, n, "f-string: invalid conversion character: "
4617 "expected 's', 'r', or 'a'");
4618 return -1;
4619 }
4620 }
4621
4622 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004623 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004624 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004625 if (**str == ':') {
4626 *str += 1;
4627 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004628 goto unexpected_end_of_string;
4629
4630 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004631 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004632 if (!format_spec)
4633 return -1;
4634 }
4635
Eric V. Smith451d0e32016-09-09 21:56:20 -04004636 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004637 goto unexpected_end_of_string;
4638
4639 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004640 assert(*str < end);
4641 assert(**str == '}');
4642 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004643
Eric V. Smith451d0e32016-09-09 21:56:20 -04004644 /* And now create the FormattedValue node that represents this
4645 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004646 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004647 format_spec, LINENO(n), n->n_col_offset,
4648 c->c_arena);
4649 if (!*expression)
4650 return -1;
4651
4652 return 0;
4653
4654unexpected_end_of_string:
4655 ast_error(c, n, "f-string: expecting '}'");
4656 return -1;
4657}
4658
4659/* Return -1 on error.
4660
4661 Return 0 if we have a literal (possible zero length) and an
4662 expression (zero length if at the end of the string.
4663
4664 Return 1 if we have a literal, but no expression, and we want the
4665 caller to call us again. This is used to deal with doubled
4666 braces.
4667
4668 When called multiple times on the string 'a{{b{0}c', this function
4669 will return:
4670
4671 1. the literal 'a{' with no expression, and a return value
4672 of 1. Despite the fact that there's no expression, the return
4673 value of 1 means we're not finished yet.
4674
4675 2. the literal 'b' and the expression '0', with a return value of
4676 0. The fact that there's an expression means we're not finished.
4677
4678 3. literal 'c' with no expression and a return value of 0. The
4679 combination of the return value of 0 with no expression means
4680 we're finished.
4681*/
4682static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004683fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4684 int recurse_lvl, PyObject **literal,
4685 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004686 struct compiling *c, const node *n)
4687{
4688 int result;
4689
4690 assert(*literal == NULL && *expression == NULL);
4691
4692 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004693 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004694 if (result < 0)
4695 goto error;
4696
4697 assert(result == 0 || result == 1);
4698
4699 if (result == 1)
4700 /* We have a literal, but don't look at the expression. */
4701 return 1;
4702
Eric V. Smith451d0e32016-09-09 21:56:20 -04004703 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004704 /* We're at the end of the string or the end of a nested
4705 f-string: no expression. The top-level error case where we
4706 expect to be at the end of the string but we're at a '}' is
4707 handled later. */
4708 return 0;
4709
4710 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004711 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004712
Eric V. Smith451d0e32016-09-09 21:56:20 -04004713 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004714 goto error;
4715
4716 return 0;
4717
4718error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004719 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004720 return -1;
4721}
4722
4723#define EXPRLIST_N_CACHED 64
4724
4725typedef struct {
4726 /* Incrementally build an array of expr_ty, so be used in an
4727 asdl_seq. Cache some small but reasonably sized number of
4728 expr_ty's, and then after that start dynamically allocating,
4729 doubling the number allocated each time. Note that the f-string
4730 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004731 Constant for the literal 'a'. So you add expr_ty's about twice as
Eric V. Smith235a6f02015-09-19 14:51:32 -04004732 fast as you add exressions in an f-string. */
4733
4734 Py_ssize_t allocated; /* Number we've allocated. */
4735 Py_ssize_t size; /* Number we've used. */
4736 expr_ty *p; /* Pointer to the memory we're actually
4737 using. Will point to 'data' until we
4738 start dynamically allocating. */
4739 expr_ty data[EXPRLIST_N_CACHED];
4740} ExprList;
4741
4742#ifdef NDEBUG
4743#define ExprList_check_invariants(l)
4744#else
4745static void
4746ExprList_check_invariants(ExprList *l)
4747{
4748 /* Check our invariants. Make sure this object is "live", and
4749 hasn't been deallocated. */
4750 assert(l->size >= 0);
4751 assert(l->p != NULL);
4752 if (l->size <= EXPRLIST_N_CACHED)
4753 assert(l->data == l->p);
4754}
4755#endif
4756
4757static void
4758ExprList_Init(ExprList *l)
4759{
4760 l->allocated = EXPRLIST_N_CACHED;
4761 l->size = 0;
4762
4763 /* Until we start allocating dynamically, p points to data. */
4764 l->p = l->data;
4765
4766 ExprList_check_invariants(l);
4767}
4768
4769static int
4770ExprList_Append(ExprList *l, expr_ty exp)
4771{
4772 ExprList_check_invariants(l);
4773 if (l->size >= l->allocated) {
4774 /* We need to alloc (or realloc) the memory. */
4775 Py_ssize_t new_size = l->allocated * 2;
4776
4777 /* See if we've ever allocated anything dynamically. */
4778 if (l->p == l->data) {
4779 Py_ssize_t i;
4780 /* We're still using the cached data. Switch to
4781 alloc-ing. */
4782 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
4783 if (!l->p)
4784 return -1;
4785 /* Copy the cached data into the new buffer. */
4786 for (i = 0; i < l->size; i++)
4787 l->p[i] = l->data[i];
4788 } else {
4789 /* Just realloc. */
4790 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
4791 if (!tmp) {
4792 PyMem_RawFree(l->p);
4793 l->p = NULL;
4794 return -1;
4795 }
4796 l->p = tmp;
4797 }
4798
4799 l->allocated = new_size;
4800 assert(l->allocated == 2 * l->size);
4801 }
4802
4803 l->p[l->size++] = exp;
4804
4805 ExprList_check_invariants(l);
4806 return 0;
4807}
4808
4809static void
4810ExprList_Dealloc(ExprList *l)
4811{
4812 ExprList_check_invariants(l);
4813
4814 /* If there's been an error, or we've never dynamically allocated,
4815 do nothing. */
4816 if (!l->p || l->p == l->data) {
4817 /* Do nothing. */
4818 } else {
4819 /* We have dynamically allocated. Free the memory. */
4820 PyMem_RawFree(l->p);
4821 }
4822 l->p = NULL;
4823 l->size = -1;
4824}
4825
4826static asdl_seq *
4827ExprList_Finish(ExprList *l, PyArena *arena)
4828{
4829 asdl_seq *seq;
4830
4831 ExprList_check_invariants(l);
4832
4833 /* Allocate the asdl_seq and copy the expressions in to it. */
4834 seq = _Py_asdl_seq_new(l->size, arena);
4835 if (seq) {
4836 Py_ssize_t i;
4837 for (i = 0; i < l->size; i++)
4838 asdl_seq_SET(seq, i, l->p[i]);
4839 }
4840 ExprList_Dealloc(l);
4841 return seq;
4842}
4843
4844/* The FstringParser is designed to add a mix of strings and
4845 f-strings, and concat them together as needed. Ultimately, it
4846 generates an expr_ty. */
4847typedef struct {
4848 PyObject *last_str;
4849 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004850 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004851} FstringParser;
4852
4853#ifdef NDEBUG
4854#define FstringParser_check_invariants(state)
4855#else
4856static void
4857FstringParser_check_invariants(FstringParser *state)
4858{
4859 if (state->last_str)
4860 assert(PyUnicode_CheckExact(state->last_str));
4861 ExprList_check_invariants(&state->expr_list);
4862}
4863#endif
4864
4865static void
4866FstringParser_Init(FstringParser *state)
4867{
4868 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004869 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004870 ExprList_Init(&state->expr_list);
4871 FstringParser_check_invariants(state);
4872}
4873
4874static void
4875FstringParser_Dealloc(FstringParser *state)
4876{
4877 FstringParser_check_invariants(state);
4878
4879 Py_XDECREF(state->last_str);
4880 ExprList_Dealloc(&state->expr_list);
4881}
4882
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004883/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004884static expr_ty
4885make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
4886{
4887 PyObject *s = *str;
4888 *str = NULL;
4889 assert(PyUnicode_CheckExact(s));
4890 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
4891 Py_DECREF(s);
4892 return NULL;
4893 }
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004894 return Constant(s, LINENO(n), n->n_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004895}
4896
4897/* Add a non-f-string (that is, a regular literal string). str is
4898 decref'd. */
4899static int
4900FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
4901{
4902 FstringParser_check_invariants(state);
4903
4904 assert(PyUnicode_CheckExact(str));
4905
4906 if (PyUnicode_GET_LENGTH(str) == 0) {
4907 Py_DECREF(str);
4908 return 0;
4909 }
4910
4911 if (!state->last_str) {
4912 /* We didn't have a string before, so just remember this one. */
4913 state->last_str = str;
4914 } else {
4915 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02004916 PyUnicode_AppendAndDel(&state->last_str, str);
4917 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004918 return -1;
4919 }
4920 FstringParser_check_invariants(state);
4921 return 0;
4922}
4923
Eric V. Smith451d0e32016-09-09 21:56:20 -04004924/* Parse an f-string. The f-string is in *str to end, with no
4925 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004926static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004927FstringParser_ConcatFstring(FstringParser *state, const char **str,
4928 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004929 struct compiling *c, const node *n)
4930{
4931 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004932 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004933
4934 /* Parse the f-string. */
4935 while (1) {
4936 PyObject *literal = NULL;
4937 expr_ty expression = NULL;
4938
4939 /* If there's a zero length literal in front of the
4940 expression, literal will be NULL. If we're at the end of
4941 the f-string, expression will be NULL (unless result == 1,
4942 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004943 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004944 &literal, &expression,
4945 c, n);
4946 if (result < 0)
4947 return -1;
4948
4949 /* Add the literal, if any. */
4950 if (!literal) {
4951 /* Do nothing. Just leave last_str alone (and possibly
4952 NULL). */
4953 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04004954 /* Note that the literal can be zero length, if the
4955 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004956 state->last_str = literal;
4957 literal = NULL;
4958 } else {
4959 /* We have a literal, concatenate it. */
4960 assert(PyUnicode_GET_LENGTH(literal) != 0);
4961 if (FstringParser_ConcatAndDel(state, literal) < 0)
4962 return -1;
4963 literal = NULL;
4964 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004965
4966 /* We've dealt with the literal now. It can't be leaked on further
4967 errors. */
4968 assert(literal == NULL);
4969
4970 /* See if we should just loop around to get the next literal
4971 and expression, while ignoring the expression this
4972 time. This is used for un-doubling braces, as an
4973 optimization. */
4974 if (result == 1)
4975 continue;
4976
4977 if (!expression)
4978 /* We're done with this f-string. */
4979 break;
4980
4981 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004982 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004983 if (!state->last_str) {
4984 /* Do nothing. No previous literal. */
4985 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004986 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004987 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
4988 if (!str || ExprList_Append(&state->expr_list, str) < 0)
4989 return -1;
4990 }
4991
4992 if (ExprList_Append(&state->expr_list, expression) < 0)
4993 return -1;
4994 }
4995
Eric V. Smith235a6f02015-09-19 14:51:32 -04004996 /* If recurse_lvl is zero, then we must be at the end of the
4997 string. Otherwise, we must be at a right brace. */
4998
Eric V. Smith451d0e32016-09-09 21:56:20 -04004999 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005000 ast_error(c, n, "f-string: unexpected end of string");
5001 return -1;
5002 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005003 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005004 ast_error(c, n, "f-string: expecting '}'");
5005 return -1;
5006 }
5007
5008 FstringParser_check_invariants(state);
5009 return 0;
5010}
5011
5012/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005013 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005014static expr_ty
5015FstringParser_Finish(FstringParser *state, struct compiling *c,
5016 const node *n)
5017{
5018 asdl_seq *seq;
5019
5020 FstringParser_check_invariants(state);
5021
5022 /* If we're just a constant string with no expressions, return
5023 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005024 if (!state->fmode) {
5025 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005026 if (!state->last_str) {
5027 /* Create a zero length string. */
5028 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5029 if (!state->last_str)
5030 goto error;
5031 }
5032 return make_str_node_and_del(&state->last_str, c, n);
5033 }
5034
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005035 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005036 last node in our expression list. */
5037 if (state->last_str) {
5038 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5039 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5040 goto error;
5041 }
5042 /* This has already been freed. */
5043 assert(state->last_str == NULL);
5044
5045 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5046 if (!seq)
5047 goto error;
5048
Eric V. Smith235a6f02015-09-19 14:51:32 -04005049 return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
5050
5051error:
5052 FstringParser_Dealloc(state);
5053 return NULL;
5054}
5055
Eric V. Smith451d0e32016-09-09 21:56:20 -04005056/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5057 at end, parse it into an expr_ty. Return NULL on error. Adjust
5058 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005059static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005060fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005061 struct compiling *c, const node *n)
5062{
5063 FstringParser state;
5064
5065 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005066 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005067 c, n) < 0) {
5068 FstringParser_Dealloc(&state);
5069 return NULL;
5070 }
5071
5072 return FstringParser_Finish(&state, c, n);
5073}
5074
5075/* n is a Python string literal, including the bracketing quote
5076 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005077 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005078 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005079 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5080 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005081*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005082static int
5083parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5084 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005085{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005086 size_t len;
5087 const char *s = STR(n);
5088 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005089 int fmode = 0;
5090 *bytesmode = 0;
5091 *rawmode = 0;
5092 *result = NULL;
5093 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005094 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005095 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005096 if (quote == 'b' || quote == 'B') {
5097 quote = *++s;
5098 *bytesmode = 1;
5099 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005100 else if (quote == 'u' || quote == 'U') {
5101 quote = *++s;
5102 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005103 else if (quote == 'r' || quote == 'R') {
5104 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005105 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005106 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005107 else if (quote == 'f' || quote == 'F') {
5108 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005109 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005110 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005111 else {
5112 break;
5113 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005114 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005115 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005116 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005117 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005118 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005119 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005120 if (quote != '\'' && quote != '\"') {
5121 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005122 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005123 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005124 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005125 s++;
5126 len = strlen(s);
5127 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005128 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005129 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005130 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005131 }
5132 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005133 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005134 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005135 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005136 }
5137 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005138 /* A triple quoted string. We've already skipped one quote at
5139 the start and one at the end of the string. Now skip the
5140 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005141 s += 2;
5142 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005143 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005144 if (s[--len] != quote || s[--len] != quote) {
5145 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005146 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005147 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005148 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005149
Eric V. Smith451d0e32016-09-09 21:56:20 -04005150 if (fmode) {
5151 /* Just return the bytes. The caller will parse the resulting
5152 string. */
5153 *fstr = s;
5154 *fstrlen = len;
5155 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005156 }
5157
Eric V. Smith451d0e32016-09-09 21:56:20 -04005158 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005159 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005160 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005161 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005162 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005163 const char *ch;
5164 for (ch = s; *ch; ch++) {
5165 if (Py_CHARMASK(*ch) >= 0x80) {
5166 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005167 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005168 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005169 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005170 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005171 if (*rawmode)
5172 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005173 else
Eric V. Smith56466482016-10-31 14:46:26 -04005174 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005175 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005176 if (*rawmode)
5177 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005178 else
Eric V. Smith56466482016-10-31 14:46:26 -04005179 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005180 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005181 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005182}
5183
Eric V. Smith235a6f02015-09-19 14:51:32 -04005184/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5185 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005186 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005187 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005188 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005189 node if there's just an f-string (with no leading or trailing
5190 literals), or a JoinedStr node if there are multiple f-strings or
5191 any literals involved. */
5192static expr_ty
5193parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005194{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005195 int bytesmode = 0;
5196 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005197 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005198
5199 FstringParser state;
5200 FstringParser_Init(&state);
5201
5202 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005203 int this_bytesmode;
5204 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005205 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005206 const char *fstr;
5207 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005208
5209 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005210 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5211 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005212 goto error;
5213
5214 /* Check that we're not mixing bytes with unicode. */
5215 if (i != 0 && bytesmode != this_bytesmode) {
5216 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005217 /* s is NULL if the current string part is an f-string. */
5218 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005219 goto error;
5220 }
5221 bytesmode = this_bytesmode;
5222
Eric V. Smith451d0e32016-09-09 21:56:20 -04005223 if (fstr != NULL) {
5224 int result;
5225 assert(s == NULL && !bytesmode);
5226 /* This is an f-string. Parse and concatenate it. */
5227 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5228 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005229 if (result < 0)
5230 goto error;
5231 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005232 /* A string or byte string. */
5233 assert(s != NULL && fstr == NULL);
5234
Eric V. Smith451d0e32016-09-09 21:56:20 -04005235 assert(bytesmode ? PyBytes_CheckExact(s) :
5236 PyUnicode_CheckExact(s));
5237
Eric V. Smith451d0e32016-09-09 21:56:20 -04005238 if (bytesmode) {
5239 /* For bytes, concat as we go. */
5240 if (i == 0) {
5241 /* First time, just remember this value. */
5242 bytes_str = s;
5243 } else {
5244 PyBytes_ConcatAndDel(&bytes_str, s);
5245 if (!bytes_str)
5246 goto error;
5247 }
5248 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005249 /* This is a regular string. Concatenate it. */
5250 if (FstringParser_ConcatAndDel(&state, s) < 0)
5251 goto error;
5252 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005253 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005254 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005255 if (bytesmode) {
5256 /* Just return the bytes object and we're done. */
5257 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5258 goto error;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005259 return Constant(bytes_str, LINENO(n), n->n_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005260 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005261
Eric V. Smith235a6f02015-09-19 14:51:32 -04005262 /* We're not a bytes string, bytes_str should never have been set. */
5263 assert(bytes_str == NULL);
5264
5265 return FstringParser_Finish(&state, c, n);
5266
5267error:
5268 Py_XDECREF(bytes_str);
5269 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005270 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005271}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005272
5273PyObject *
5274_PyAST_GetDocString(asdl_seq *body)
5275{
5276 if (!asdl_seq_LEN(body)) {
5277 return NULL;
5278 }
5279 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5280 if (st->kind != Expr_kind) {
5281 return NULL;
5282 }
5283 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005284 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5285 return e->v.Constant.value;
5286 }
5287 return NULL;
5288}