blob: e10e63f539c36f986cc1cbcca4c89f0fec4e58db [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
Serhiy Storchaka58159ef2019-01-12 09:46:50 +020016#define MAXLEVEL 200 /* Max parentheses level */
17
Benjamin Peterson832bfe22011-08-09 16:15:04 -050018static int validate_stmts(asdl_seq *);
19static int validate_exprs(asdl_seq *, expr_context_ty, int);
20static int validate_nonempty_seq(asdl_seq *, const char *, const char *);
21static int validate_stmt(stmt_ty);
22static int validate_expr(expr_ty, expr_context_ty);
23
24static int
25validate_comprehension(asdl_seq *gens)
26{
Victor Stinner4d73ae72018-11-22 14:45:16 +010027 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050028 if (!asdl_seq_LEN(gens)) {
29 PyErr_SetString(PyExc_ValueError, "comprehension with no generators");
30 return 0;
31 }
32 for (i = 0; i < asdl_seq_LEN(gens); i++) {
33 comprehension_ty comp = asdl_seq_GET(gens, i);
34 if (!validate_expr(comp->target, Store) ||
35 !validate_expr(comp->iter, Load) ||
36 !validate_exprs(comp->ifs, Load, 0))
37 return 0;
38 }
39 return 1;
40}
41
42static int
43validate_slice(slice_ty slice)
44{
45 switch (slice->kind) {
46 case Slice_kind:
47 return (!slice->v.Slice.lower || validate_expr(slice->v.Slice.lower, Load)) &&
48 (!slice->v.Slice.upper || validate_expr(slice->v.Slice.upper, Load)) &&
49 (!slice->v.Slice.step || validate_expr(slice->v.Slice.step, Load));
50 case ExtSlice_kind: {
Victor Stinner4d73ae72018-11-22 14:45:16 +010051 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050052 if (!validate_nonempty_seq(slice->v.ExtSlice.dims, "dims", "ExtSlice"))
53 return 0;
54 for (i = 0; i < asdl_seq_LEN(slice->v.ExtSlice.dims); i++)
55 if (!validate_slice(asdl_seq_GET(slice->v.ExtSlice.dims, i)))
56 return 0;
57 return 1;
58 }
59 case Index_kind:
60 return validate_expr(slice->v.Index.value, Load);
61 default:
62 PyErr_SetString(PyExc_SystemError, "unknown slice node");
63 return 0;
64 }
65}
66
67static int
68validate_keywords(asdl_seq *keywords)
69{
Victor Stinner4d73ae72018-11-22 14:45:16 +010070 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050071 for (i = 0; i < asdl_seq_LEN(keywords); i++)
72 if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
73 return 0;
74 return 1;
75}
76
77static int
78validate_args(asdl_seq *args)
79{
Victor Stinner4d73ae72018-11-22 14:45:16 +010080 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -050081 for (i = 0; i < asdl_seq_LEN(args); i++) {
82 arg_ty arg = asdl_seq_GET(args, i);
83 if (arg->annotation && !validate_expr(arg->annotation, Load))
84 return 0;
85 }
86 return 1;
87}
88
89static const char *
90expr_context_name(expr_context_ty ctx)
91{
92 switch (ctx) {
93 case Load:
94 return "Load";
95 case Store:
96 return "Store";
Emily Morehouse8f59ee02019-01-24 16:49:56 -070097 case NamedStore:
98 return "NamedStore";
Benjamin Peterson832bfe22011-08-09 16:15:04 -050099 case Del:
100 return "Del";
101 case AugLoad:
102 return "AugLoad";
103 case AugStore:
104 return "AugStore";
105 case Param:
106 return "Param";
107 default:
Barry Warsawb2e57942017-09-14 18:13:16 -0700108 Py_UNREACHABLE();
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500109 }
110}
111
112static int
113validate_arguments(arguments_ty args)
114{
115 if (!validate_args(args->args))
116 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700117 if (args->vararg && args->vararg->annotation
118 && !validate_expr(args->vararg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500119 return 0;
120 }
121 if (!validate_args(args->kwonlyargs))
122 return 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100123 if (args->kwarg && args->kwarg->annotation
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700124 && !validate_expr(args->kwarg->annotation, Load)) {
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500125 return 0;
126 }
127 if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) {
128 PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments");
129 return 0;
130 }
131 if (asdl_seq_LEN(args->kw_defaults) != asdl_seq_LEN(args->kwonlyargs)) {
132 PyErr_SetString(PyExc_ValueError, "length of kwonlyargs is not the same as "
133 "kw_defaults on arguments");
134 return 0;
135 }
136 return validate_exprs(args->defaults, Load, 0) && validate_exprs(args->kw_defaults, Load, 1);
137}
138
139static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100140validate_constant(PyObject *value)
141{
142 if (value == Py_None || value == Py_Ellipsis)
143 return 1;
144
145 if (PyLong_CheckExact(value)
146 || PyFloat_CheckExact(value)
147 || PyComplex_CheckExact(value)
148 || PyBool_Check(value)
149 || PyUnicode_CheckExact(value)
150 || PyBytes_CheckExact(value))
151 return 1;
152
153 if (PyTuple_CheckExact(value) || PyFrozenSet_CheckExact(value)) {
154 PyObject *it;
155
156 it = PyObject_GetIter(value);
157 if (it == NULL)
158 return 0;
159
160 while (1) {
161 PyObject *item = PyIter_Next(it);
162 if (item == NULL) {
163 if (PyErr_Occurred()) {
164 Py_DECREF(it);
165 return 0;
166 }
167 break;
168 }
169
170 if (!validate_constant(item)) {
171 Py_DECREF(it);
Victor Stinner726f6902016-01-27 00:11:47 +0100172 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100173 return 0;
174 }
Victor Stinner726f6902016-01-27 00:11:47 +0100175 Py_DECREF(item);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100176 }
177
178 Py_DECREF(it);
179 return 1;
180 }
181
182 return 0;
183}
184
185static int
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500186validate_expr(expr_ty exp, expr_context_ty ctx)
187{
188 int check_ctx = 1;
189 expr_context_ty actual_ctx;
190
191 /* First check expression context. */
192 switch (exp->kind) {
193 case Attribute_kind:
194 actual_ctx = exp->v.Attribute.ctx;
195 break;
196 case Subscript_kind:
197 actual_ctx = exp->v.Subscript.ctx;
198 break;
199 case Starred_kind:
200 actual_ctx = exp->v.Starred.ctx;
201 break;
202 case Name_kind:
203 actual_ctx = exp->v.Name.ctx;
204 break;
205 case List_kind:
206 actual_ctx = exp->v.List.ctx;
207 break;
208 case Tuple_kind:
209 actual_ctx = exp->v.Tuple.ctx;
210 break;
211 default:
212 if (ctx != Load) {
213 PyErr_Format(PyExc_ValueError, "expression which can't be "
214 "assigned to in %s context", expr_context_name(ctx));
215 return 0;
216 }
217 check_ctx = 0;
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100218 /* set actual_ctx to prevent gcc warning */
219 actual_ctx = 0;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500220 }
221 if (check_ctx && actual_ctx != ctx) {
222 PyErr_Format(PyExc_ValueError, "expression must have %s context but has %s instead",
223 expr_context_name(ctx), expr_context_name(actual_ctx));
224 return 0;
225 }
226
227 /* Now validate expression. */
228 switch (exp->kind) {
229 case BoolOp_kind:
230 if (asdl_seq_LEN(exp->v.BoolOp.values) < 2) {
231 PyErr_SetString(PyExc_ValueError, "BoolOp with less than 2 values");
232 return 0;
233 }
234 return validate_exprs(exp->v.BoolOp.values, Load, 0);
235 case BinOp_kind:
236 return validate_expr(exp->v.BinOp.left, Load) &&
237 validate_expr(exp->v.BinOp.right, Load);
238 case UnaryOp_kind:
239 return validate_expr(exp->v.UnaryOp.operand, Load);
240 case Lambda_kind:
241 return validate_arguments(exp->v.Lambda.args) &&
242 validate_expr(exp->v.Lambda.body, Load);
243 case IfExp_kind:
244 return validate_expr(exp->v.IfExp.test, Load) &&
245 validate_expr(exp->v.IfExp.body, Load) &&
246 validate_expr(exp->v.IfExp.orelse, Load);
247 case Dict_kind:
248 if (asdl_seq_LEN(exp->v.Dict.keys) != asdl_seq_LEN(exp->v.Dict.values)) {
249 PyErr_SetString(PyExc_ValueError,
250 "Dict doesn't have the same number of keys as values");
251 return 0;
252 }
Yury Selivanovb3d53132015-09-01 16:10:49 -0400253 /* null_ok=1 for keys expressions to allow dict unpacking to work in
254 dict literals, i.e. ``{**{a:b}}`` */
255 return validate_exprs(exp->v.Dict.keys, Load, /*null_ok=*/ 1) &&
256 validate_exprs(exp->v.Dict.values, Load, /*null_ok=*/ 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500257 case Set_kind:
258 return validate_exprs(exp->v.Set.elts, Load, 0);
259#define COMP(NAME) \
260 case NAME ## _kind: \
261 return validate_comprehension(exp->v.NAME.generators) && \
262 validate_expr(exp->v.NAME.elt, Load);
263 COMP(ListComp)
264 COMP(SetComp)
265 COMP(GeneratorExp)
266#undef COMP
267 case DictComp_kind:
268 return validate_comprehension(exp->v.DictComp.generators) &&
269 validate_expr(exp->v.DictComp.key, Load) &&
270 validate_expr(exp->v.DictComp.value, Load);
271 case Yield_kind:
272 return !exp->v.Yield.value || validate_expr(exp->v.Yield.value, Load);
Benjamin Peterson527c6222012-01-14 08:58:23 -0500273 case YieldFrom_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +0000274 return validate_expr(exp->v.YieldFrom.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400275 case Await_kind:
276 return validate_expr(exp->v.Await.value, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500277 case Compare_kind:
278 if (!asdl_seq_LEN(exp->v.Compare.comparators)) {
279 PyErr_SetString(PyExc_ValueError, "Compare with no comparators");
280 return 0;
281 }
282 if (asdl_seq_LEN(exp->v.Compare.comparators) !=
283 asdl_seq_LEN(exp->v.Compare.ops)) {
284 PyErr_SetString(PyExc_ValueError, "Compare has a different number "
285 "of comparators and operands");
286 return 0;
287 }
288 return validate_exprs(exp->v.Compare.comparators, Load, 0) &&
289 validate_expr(exp->v.Compare.left, Load);
290 case Call_kind:
291 return validate_expr(exp->v.Call.func, Load) &&
292 validate_exprs(exp->v.Call.args, Load, 0) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400293 validate_keywords(exp->v.Call.keywords);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100294 case Constant_kind:
295 if (!validate_constant(exp->v.Constant.value)) {
Victor Stinnerbe59d142016-01-27 00:39:12 +0100296 PyErr_Format(PyExc_TypeError,
297 "got an invalid type in Constant: %s",
298 Py_TYPE(exp->v.Constant.value)->tp_name);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100299 return 0;
300 }
301 return 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400302 case JoinedStr_kind:
303 return validate_exprs(exp->v.JoinedStr.values, Load, 0);
304 case FormattedValue_kind:
305 if (validate_expr(exp->v.FormattedValue.value, Load) == 0)
306 return 0;
307 if (exp->v.FormattedValue.format_spec)
308 return validate_expr(exp->v.FormattedValue.format_spec, Load);
309 return 1;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500310 case Attribute_kind:
311 return validate_expr(exp->v.Attribute.value, Load);
312 case Subscript_kind:
313 return validate_slice(exp->v.Subscript.slice) &&
314 validate_expr(exp->v.Subscript.value, Load);
315 case Starred_kind:
316 return validate_expr(exp->v.Starred.value, ctx);
317 case List_kind:
318 return validate_exprs(exp->v.List.elts, ctx, 0);
319 case Tuple_kind:
320 return validate_exprs(exp->v.Tuple.elts, ctx, 0);
Serhiy Storchaka3f228112018-09-27 17:42:37 +0300321 /* This last case doesn't have any checking. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500322 case Name_kind:
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500323 return 1;
324 default:
325 PyErr_SetString(PyExc_SystemError, "unexpected expression");
326 return 0;
327 }
328}
329
330static int
331validate_nonempty_seq(asdl_seq *seq, const char *what, const char *owner)
332{
333 if (asdl_seq_LEN(seq))
334 return 1;
335 PyErr_Format(PyExc_ValueError, "empty %s on %s", what, owner);
336 return 0;
337}
338
339static int
340validate_assignlist(asdl_seq *targets, expr_context_ty ctx)
341{
342 return validate_nonempty_seq(targets, "targets", ctx == Del ? "Delete" : "Assign") &&
343 validate_exprs(targets, ctx, 0);
344}
345
346static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300347validate_body(asdl_seq *body, const char *owner)
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500348{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300349 return validate_nonempty_seq(body, "body", owner) && validate_stmts(body);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500350}
351
352static int
353validate_stmt(stmt_ty stmt)
354{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100355 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500356 switch (stmt->kind) {
357 case FunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300358 return validate_body(stmt->v.FunctionDef.body, "FunctionDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500359 validate_arguments(stmt->v.FunctionDef.args) &&
360 validate_exprs(stmt->v.FunctionDef.decorator_list, Load, 0) &&
361 (!stmt->v.FunctionDef.returns ||
362 validate_expr(stmt->v.FunctionDef.returns, Load));
363 case ClassDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300364 return validate_body(stmt->v.ClassDef.body, "ClassDef") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500365 validate_exprs(stmt->v.ClassDef.bases, Load, 0) &&
366 validate_keywords(stmt->v.ClassDef.keywords) &&
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400367 validate_exprs(stmt->v.ClassDef.decorator_list, Load, 0);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500368 case Return_kind:
369 return !stmt->v.Return.value || validate_expr(stmt->v.Return.value, Load);
370 case Delete_kind:
371 return validate_assignlist(stmt->v.Delete.targets, Del);
372 case Assign_kind:
373 return validate_assignlist(stmt->v.Assign.targets, Store) &&
374 validate_expr(stmt->v.Assign.value, Load);
375 case AugAssign_kind:
376 return validate_expr(stmt->v.AugAssign.target, Store) &&
377 validate_expr(stmt->v.AugAssign.value, Load);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700378 case AnnAssign_kind:
379 if (stmt->v.AnnAssign.target->kind != Name_kind &&
380 stmt->v.AnnAssign.simple) {
381 PyErr_SetString(PyExc_TypeError,
382 "AnnAssign with simple non-Name target");
383 return 0;
384 }
385 return validate_expr(stmt->v.AnnAssign.target, Store) &&
386 (!stmt->v.AnnAssign.value ||
387 validate_expr(stmt->v.AnnAssign.value, Load)) &&
388 validate_expr(stmt->v.AnnAssign.annotation, Load);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500389 case For_kind:
390 return validate_expr(stmt->v.For.target, Store) &&
391 validate_expr(stmt->v.For.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300392 validate_body(stmt->v.For.body, "For") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500393 validate_stmts(stmt->v.For.orelse);
Yury Selivanov75445082015-05-11 22:57:16 -0400394 case AsyncFor_kind:
395 return validate_expr(stmt->v.AsyncFor.target, Store) &&
396 validate_expr(stmt->v.AsyncFor.iter, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300397 validate_body(stmt->v.AsyncFor.body, "AsyncFor") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400398 validate_stmts(stmt->v.AsyncFor.orelse);
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500399 case While_kind:
400 return validate_expr(stmt->v.While.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300401 validate_body(stmt->v.While.body, "While") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500402 validate_stmts(stmt->v.While.orelse);
403 case If_kind:
404 return validate_expr(stmt->v.If.test, Load) &&
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300405 validate_body(stmt->v.If.body, "If") &&
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500406 validate_stmts(stmt->v.If.orelse);
407 case With_kind:
408 if (!validate_nonempty_seq(stmt->v.With.items, "items", "With"))
409 return 0;
410 for (i = 0; i < asdl_seq_LEN(stmt->v.With.items); i++) {
411 withitem_ty item = asdl_seq_GET(stmt->v.With.items, i);
412 if (!validate_expr(item->context_expr, Load) ||
413 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
414 return 0;
415 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300416 return validate_body(stmt->v.With.body, "With");
Yury Selivanov75445082015-05-11 22:57:16 -0400417 case AsyncWith_kind:
418 if (!validate_nonempty_seq(stmt->v.AsyncWith.items, "items", "AsyncWith"))
419 return 0;
420 for (i = 0; i < asdl_seq_LEN(stmt->v.AsyncWith.items); i++) {
421 withitem_ty item = asdl_seq_GET(stmt->v.AsyncWith.items, i);
422 if (!validate_expr(item->context_expr, Load) ||
423 (item->optional_vars && !validate_expr(item->optional_vars, Store)))
424 return 0;
425 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300426 return validate_body(stmt->v.AsyncWith.body, "AsyncWith");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500427 case Raise_kind:
428 if (stmt->v.Raise.exc) {
429 return validate_expr(stmt->v.Raise.exc, Load) &&
430 (!stmt->v.Raise.cause || validate_expr(stmt->v.Raise.cause, Load));
431 }
432 if (stmt->v.Raise.cause) {
433 PyErr_SetString(PyExc_ValueError, "Raise with cause but no exception");
434 return 0;
435 }
436 return 1;
437 case Try_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300438 if (!validate_body(stmt->v.Try.body, "Try"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500439 return 0;
440 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
441 !asdl_seq_LEN(stmt->v.Try.finalbody)) {
442 PyErr_SetString(PyExc_ValueError, "Try has neither except handlers nor finalbody");
443 return 0;
444 }
445 if (!asdl_seq_LEN(stmt->v.Try.handlers) &&
446 asdl_seq_LEN(stmt->v.Try.orelse)) {
447 PyErr_SetString(PyExc_ValueError, "Try has orelse but no except handlers");
448 return 0;
449 }
450 for (i = 0; i < asdl_seq_LEN(stmt->v.Try.handlers); i++) {
451 excepthandler_ty handler = asdl_seq_GET(stmt->v.Try.handlers, i);
452 if ((handler->v.ExceptHandler.type &&
453 !validate_expr(handler->v.ExceptHandler.type, Load)) ||
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300454 !validate_body(handler->v.ExceptHandler.body, "ExceptHandler"))
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500455 return 0;
456 }
457 return (!asdl_seq_LEN(stmt->v.Try.finalbody) ||
458 validate_stmts(stmt->v.Try.finalbody)) &&
459 (!asdl_seq_LEN(stmt->v.Try.orelse) ||
460 validate_stmts(stmt->v.Try.orelse));
461 case Assert_kind:
462 return validate_expr(stmt->v.Assert.test, Load) &&
463 (!stmt->v.Assert.msg || validate_expr(stmt->v.Assert.msg, Load));
464 case Import_kind:
465 return validate_nonempty_seq(stmt->v.Import.names, "names", "Import");
466 case ImportFrom_kind:
Serhiy Storchakafbd15232016-06-27 21:39:12 +0300467 if (stmt->v.ImportFrom.level < 0) {
468 PyErr_SetString(PyExc_ValueError, "Negative ImportFrom level");
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500469 return 0;
470 }
471 return validate_nonempty_seq(stmt->v.ImportFrom.names, "names", "ImportFrom");
472 case Global_kind:
473 return validate_nonempty_seq(stmt->v.Global.names, "names", "Global");
474 case Nonlocal_kind:
475 return validate_nonempty_seq(stmt->v.Nonlocal.names, "names", "Nonlocal");
476 case Expr_kind:
477 return validate_expr(stmt->v.Expr.value, Load);
Yury Selivanov75445082015-05-11 22:57:16 -0400478 case AsyncFunctionDef_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300479 return validate_body(stmt->v.AsyncFunctionDef.body, "AsyncFunctionDef") &&
Yury Selivanov75445082015-05-11 22:57:16 -0400480 validate_arguments(stmt->v.AsyncFunctionDef.args) &&
481 validate_exprs(stmt->v.AsyncFunctionDef.decorator_list, Load, 0) &&
482 (!stmt->v.AsyncFunctionDef.returns ||
483 validate_expr(stmt->v.AsyncFunctionDef.returns, Load));
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500484 case Pass_kind:
485 case Break_kind:
486 case Continue_kind:
487 return 1;
488 default:
489 PyErr_SetString(PyExc_SystemError, "unexpected statement");
490 return 0;
491 }
492}
493
494static int
495validate_stmts(asdl_seq *seq)
496{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100497 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500498 for (i = 0; i < asdl_seq_LEN(seq); i++) {
499 stmt_ty stmt = asdl_seq_GET(seq, i);
500 if (stmt) {
501 if (!validate_stmt(stmt))
502 return 0;
503 }
504 else {
505 PyErr_SetString(PyExc_ValueError,
506 "None disallowed in statement list");
507 return 0;
508 }
509 }
510 return 1;
511}
512
513static int
514validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok)
515{
Victor Stinner4d73ae72018-11-22 14:45:16 +0100516 Py_ssize_t i;
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500517 for (i = 0; i < asdl_seq_LEN(exprs); i++) {
518 expr_ty expr = asdl_seq_GET(exprs, i);
519 if (expr) {
520 if (!validate_expr(expr, ctx))
521 return 0;
522 }
523 else if (!null_ok) {
524 PyErr_SetString(PyExc_ValueError,
525 "None disallowed in expression list");
526 return 0;
527 }
Victor Stinner0c39b1b2015-03-18 15:02:06 +0100528
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500529 }
530 return 1;
531}
532
533int
534PyAST_Validate(mod_ty mod)
535{
536 int res = 0;
537
538 switch (mod->kind) {
539 case Module_kind:
540 res = validate_stmts(mod->v.Module.body);
541 break;
542 case Interactive_kind:
543 res = validate_stmts(mod->v.Interactive.body);
544 break;
545 case Expression_kind:
546 res = validate_expr(mod->v.Expression.body, Load);
547 break;
548 case Suite_kind:
549 PyErr_SetString(PyExc_ValueError, "Suite is not valid in the CPython compiler");
550 break;
551 default:
552 PyErr_SetString(PyExc_SystemError, "impossible module node");
553 res = 0;
554 break;
555 }
556 return res;
557}
558
Benjamin Petersond3af6e32012-01-16 09:56:35 -0500559/* This is done here, so defines like "test" don't interfere with AST use above. */
Benjamin Peterson832bfe22011-08-09 16:15:04 -0500560#include "grammar.h"
561#include "parsetok.h"
562#include "graminit.h"
563
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564/* Data structure used internally */
565struct compiling {
Eric V. Smith163b5c62015-08-21 09:40:38 -0400566 PyArena *c_arena; /* Arena for allocating memory. */
Victor Stinner14e461d2013-08-26 22:28:21 +0200567 PyObject *c_filename; /* filename */
Benjamin Peterson55e00432012-01-16 17:22:31 -0500568 PyObject *c_normalize; /* Normalization function from unicodedata. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000569};
570
571static asdl_seq *seq_for_testlist(struct compiling *, const node *);
572static expr_ty ast_for_expr(struct compiling *, const node *);
573static stmt_ty ast_for_stmt(struct compiling *, const node *);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300574static asdl_seq *ast_for_suite(struct compiling *c, const node *n);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000575static asdl_seq *ast_for_exprlist(struct compiling *, const node *,
576 expr_context_ty);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000577static expr_ty ast_for_testlist(struct compiling *, const node *);
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000578static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579
guoci90fc8982018-09-11 17:45:45 -0400580static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool);
581static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool);
Yury Selivanov75445082015-05-11 22:57:16 -0400582
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583/* Note different signature for ast_for_call */
Serhiy Storchakab619b092018-11-27 09:40:29 +0200584static expr_ty ast_for_call(struct compiling *, const node *, expr_ty,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000585 const node *, const node *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000587static PyObject *parsenumber(struct compiling *, const char *);
Eric V. Smith235a6f02015-09-19 14:51:32 -0400588static expr_ty parsestrplus(struct compiling *, const node *n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000589static void get_last_end_pos(asdl_seq *, int *, int *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590
Nick Coghlan650f0d02007-04-15 12:05:43 +0000591#define COMP_GENEXP 0
592#define COMP_LISTCOMP 1
593#define COMP_SETCOMP 2
594
Benjamin Peterson55e00432012-01-16 17:22:31 -0500595static int
596init_normalization(struct compiling *c)
Martin v. Löwis47383402007-08-15 07:32:56 +0000597{
Benjamin Peterson55e00432012-01-16 17:22:31 -0500598 PyObject *m = PyImport_ImportModuleNoBlock("unicodedata");
599 if (!m)
600 return 0;
601 c->c_normalize = PyObject_GetAttrString(m, "normalize");
602 Py_DECREF(m);
603 if (!c->c_normalize)
604 return 0;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500605 return 1;
606}
607
608static identifier
Benjamin Petersond40528f2012-09-02 16:37:09 -0400609new_identifier(const char *n, struct compiling *c)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500610{
Benjamin Petersonc7dedb02012-09-02 16:36:01 -0400611 PyObject *id = PyUnicode_DecodeUTF8(n, strlen(n), NULL);
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500612 if (!id)
Benjamin Peterson30760062008-11-25 04:02:28 +0000613 return NULL;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500614 /* PyUnicode_DecodeUTF8 should always return a ready string. */
Benjamin Peterson5eda9132012-01-16 09:47:42 -0500615 assert(PyUnicode_IS_READY(id));
Martin v. Löwis47383402007-08-15 07:32:56 +0000616 /* Check whether there are non-ASCII characters in the
617 identifier; if so, normalize to NFKC. */
Benjamin Petersonde5827d2012-01-16 09:55:53 -0500618 if (!PyUnicode_IS_ASCII(id)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200619 PyObject *id2;
Oren Milman7dc46d82017-09-30 20:16:24 +0300620 _Py_IDENTIFIER(NFKC);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500621 if (!c->c_normalize && !init_normalization(c)) {
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500622 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200623 return NULL;
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500624 }
Oren Milman7dc46d82017-09-30 20:16:24 +0300625 PyObject *form = _PyUnicode_FromId(&PyId_NFKC);
626 if (form == NULL) {
627 Py_DECREF(id);
628 return NULL;
629 }
630 PyObject *args[2] = {form, id};
631 id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Benjamin Peterson0fa35ea2012-01-16 09:50:48 -0500632 Py_DECREF(id);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200633 if (!id2)
634 return NULL;
Oren Milman7dc46d82017-09-30 20:16:24 +0300635 if (!PyUnicode_Check(id2)) {
636 PyErr_Format(PyExc_TypeError,
637 "unicodedata.normalize() must return a string, not "
638 "%.200s",
639 Py_TYPE(id2)->tp_name);
640 Py_DECREF(id2);
641 return NULL;
642 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200643 id = id2;
Martin v. Löwis47383402007-08-15 07:32:56 +0000644 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000645 PyUnicode_InternInPlace(&id);
Victor Stinner43d81952013-07-17 00:57:58 +0200646 if (PyArena_AddPyObject(c->c_arena, id) < 0) {
647 Py_DECREF(id);
648 return NULL;
649 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000650 return id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651}
652
Benjamin Peterson55e00432012-01-16 17:22:31 -0500653#define NEW_IDENTIFIER(n) new_identifier(STR(n), c)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655static int
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200656ast_error(struct compiling *c, const node *n, const char *errmsg, ...)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000657{
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400658 PyObject *value, *errstr, *loc, *tmp;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200659 va_list va;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200661 va_start(va, errmsg);
662 errstr = PyUnicode_FromFormatV(errmsg, va);
663 va_end(va);
664 if (!errstr) {
665 return 0;
666 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200667 loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 if (!loc) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000669 Py_INCREF(Py_None);
670 loc = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000671 }
Ammar Askar025eb982018-09-24 17:12:49 -0400672 tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200673 if (!tmp) {
674 Py_DECREF(errstr);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400675 return 0;
Neal Norwitz8ad64aa2005-12-11 20:08:33 +0000676 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000677 value = PyTuple_Pack(2, errstr, tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678 Py_DECREF(errstr);
679 Py_DECREF(tmp);
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400680 if (value) {
681 PyErr_SetObject(PyExc_SyntaxError, value);
682 Py_DECREF(value);
683 }
684 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685}
686
687/* num_stmts() returns number of contained statements.
688
689 Use this routine to determine how big a sequence is needed for
690 the statements in a parse tree. Its raison d'etre is this bit of
691 grammar:
692
693 stmt: simple_stmt | compound_stmt
694 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
695
696 A simple_stmt can contain multiple small_stmt elements joined
697 by semicolons. If the arg is a simple_stmt, the number of
698 small_stmt elements is returned.
699*/
700
701static int
702num_stmts(const node *n)
703{
704 int i, l;
705 node *ch;
706
707 switch (TYPE(n)) {
708 case single_input:
709 if (TYPE(CHILD(n, 0)) == NEWLINE)
710 return 0;
711 else
712 return num_stmts(CHILD(n, 0));
713 case file_input:
714 l = 0;
715 for (i = 0; i < NCH(n); i++) {
716 ch = CHILD(n, i);
717 if (TYPE(ch) == stmt)
718 l += num_stmts(ch);
719 }
720 return l;
721 case stmt:
722 return num_stmts(CHILD(n, 0));
723 case compound_stmt:
724 return 1;
725 case simple_stmt:
726 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */
727 case suite:
728 if (NCH(n) == 1)
729 return num_stmts(CHILD(n, 0));
730 else {
731 l = 0;
732 for (i = 2; i < (NCH(n) - 1); i++)
733 l += num_stmts(CHILD(n, i));
734 return l;
735 }
736 default: {
737 char buf[128];
738
Benjamin Petersonb58dda72009-01-18 22:27:04 +0000739 sprintf(buf, "Non-statement found: %d %d",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000740 TYPE(n), NCH(n));
741 Py_FatalError(buf);
742 }
743 }
Barry Warsawb2e57942017-09-14 18:13:16 -0700744 Py_UNREACHABLE();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000745}
746
747/* Transform the CST rooted at node * to the appropriate AST
748*/
749
750mod_ty
Victor Stinner14e461d2013-08-26 22:28:21 +0200751PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
752 PyObject *filename, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000753{
Jeremy Hyltona8293132006-02-28 17:58:27 +0000754 int i, j, k, num;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000755 asdl_seq *stmts = NULL;
756 stmt_ty s;
757 node *ch;
758 struct compiling c;
Benjamin Peterson55e00432012-01-16 17:22:31 -0500759 mod_ty res = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400761 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200762 /* borrowed reference */
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400763 c.c_filename = filename;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800764 c.c_normalize = NULL;
Benjamin Peterson9d66d4a2016-02-25 23:25:14 -0800765
766 if (TYPE(n) == encoding_decl)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768
Jeremy Hyltona8293132006-02-28 17:58:27 +0000769 k = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770 switch (TYPE(n)) {
771 case file_input:
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200772 stmts = _Py_asdl_seq_new(num_stmts(n), arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500774 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775 for (i = 0; i < NCH(n) - 1; i++) {
776 ch = CHILD(n, i);
777 if (TYPE(ch) == NEWLINE)
778 continue;
779 REQ(ch, stmt);
780 num = num_stmts(ch);
781 if (num == 1) {
782 s = ast_for_stmt(&c, ch);
783 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500784 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000785 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 }
787 else {
788 ch = CHILD(ch, 0);
789 REQ(ch, simple_stmt);
790 for (j = 0; j < num; j++) {
791 s = ast_for_stmt(&c, CHILD(ch, j * 2));
792 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500793 goto out;
Jeremy Hyltona8293132006-02-28 17:58:27 +0000794 asdl_seq_SET(stmts, k++, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795 }
796 }
797 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +0300798 res = Module(stmts, arena);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500799 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 case eval_input: {
801 expr_ty testlist_ast;
802
Nick Coghlan650f0d02007-04-15 12:05:43 +0000803 /* XXX Why not comp_for here? */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +0000804 testlist_ast = ast_for_testlist(&c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805 if (!testlist_ast)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500806 goto out;
807 res = Expression(testlist_ast, arena);
808 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809 }
810 case single_input:
811 if (TYPE(CHILD(n, 0)) == NEWLINE) {
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200812 stmts = _Py_asdl_seq_new(1, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500814 goto out;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000815 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000816 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000817 arena));
Guido van Rossum360e4b82007-05-14 22:51:27 +0000818 if (!asdl_seq_GET(stmts, 0))
Benjamin Peterson55e00432012-01-16 17:22:31 -0500819 goto out;
820 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 }
822 else {
823 n = CHILD(n, 0);
824 num = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +0200825 stmts = _Py_asdl_seq_new(num, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000826 if (!stmts)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500827 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828 if (num == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +0000829 s = ast_for_stmt(&c, n);
830 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500831 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832 asdl_seq_SET(stmts, 0, s);
833 }
834 else {
835 /* Only a simple_stmt can contain multiple statements. */
836 REQ(n, simple_stmt);
837 for (i = 0; i < NCH(n); i += 2) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838 if (TYPE(CHILD(n, i)) == NEWLINE)
839 break;
840 s = ast_for_stmt(&c, CHILD(n, i));
841 if (!s)
Benjamin Peterson55e00432012-01-16 17:22:31 -0500842 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843 asdl_seq_SET(stmts, i / 2, s);
844 }
845 }
846
Benjamin Peterson55e00432012-01-16 17:22:31 -0500847 res = Interactive(stmts, arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848 }
Benjamin Petersonc8909dd2012-01-16 17:44:12 -0500849 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 default:
Guido van Rossum360e4b82007-05-14 22:51:27 +0000851 PyErr_Format(PyExc_SystemError,
852 "invalid node %d for PyAST_FromNode", TYPE(n));
Benjamin Peterson55e00432012-01-16 17:22:31 -0500853 goto out;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500855 out:
856 if (c.c_normalize) {
857 Py_DECREF(c.c_normalize);
Benjamin Peterson55e00432012-01-16 17:22:31 -0500858 }
Benjamin Peterson55e00432012-01-16 17:22:31 -0500859 return res;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860}
861
Victor Stinner14e461d2013-08-26 22:28:21 +0200862mod_ty
863PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str,
864 PyArena *arena)
865{
866 mod_ty mod;
867 PyObject *filename;
868 filename = PyUnicode_DecodeFSDefault(filename_str);
869 if (filename == NULL)
870 return NULL;
871 mod = PyAST_FromNodeObject(n, flags, filename, arena);
872 Py_DECREF(filename);
873 return mod;
874
875}
876
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877/* Return the AST repr. of the operator represented as syntax (|, ^, etc.)
878*/
879
880static operator_ty
881get_operator(const node *n)
882{
883 switch (TYPE(n)) {
884 case VBAR:
885 return BitOr;
886 case CIRCUMFLEX:
887 return BitXor;
888 case AMPER:
889 return BitAnd;
890 case LEFTSHIFT:
891 return LShift;
892 case RIGHTSHIFT:
893 return RShift;
894 case PLUS:
895 return Add;
896 case MINUS:
897 return Sub;
898 case STAR:
899 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400900 case AT:
901 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000902 case SLASH:
903 return Div;
904 case DOUBLESLASH:
905 return FloorDiv;
906 case PERCENT:
907 return Mod;
908 default:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000909 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910 }
911}
912
Serhiy Storchaka2d06e842015-12-25 19:53:18 +0200913static const char * const FORBIDDEN[] = {
Guido van Rossume7ba4952007-06-06 23:52:48 +0000914 "None",
915 "True",
916 "False",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200917 "__debug__",
Guido van Rossume7ba4952007-06-06 23:52:48 +0000918 NULL,
919};
920
921static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400922forbidden_name(struct compiling *c, identifier name, const node *n,
923 int full_checks)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000924{
Benjamin Peterson78565b22009-06-28 19:19:51 +0000925 assert(PyUnicode_Check(name));
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200926 const char * const *p = FORBIDDEN;
927 if (!full_checks) {
928 /* In most cases, the parser will protect True, False, and None
929 from being assign to. */
930 p += 3;
Benjamin Peterson70f52762009-06-28 23:32:44 +0000931 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +0200932 for (; *p; p++) {
933 if (_PyUnicode_EqualToASCIIString(name, *p)) {
934 ast_error(c, n, "cannot assign to %U", name);
935 return 1;
Guido van Rossume7ba4952007-06-06 23:52:48 +0000936 }
937 }
938 return 0;
939}
940
Serhiy Storchakab619b092018-11-27 09:40:29 +0200941static expr_ty
942copy_location(expr_ty e, const node *n)
943{
944 if (e) {
945 e->lineno = LINENO(n);
946 e->col_offset = n->n_col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +0000947 e->end_lineno = n->n_end_lineno;
948 e->end_col_offset = n->n_end_col_offset;
Serhiy Storchakab619b092018-11-27 09:40:29 +0200949 }
950 return e;
951}
952
Jeremy Hyltona8293132006-02-28 17:58:27 +0000953/* Set the context ctx for expr_ty e, recursively traversing e.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954
955 Only sets context for expr kinds that "can appear in assignment context"
956 (according to ../Parser/Python.asdl). For other expr kinds, it sets
957 an appropriate syntax error and returns false.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958*/
959
960static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000961set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962{
963 asdl_seq *s = NULL;
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000964 /* If a particular expression type can't be used for assign / delete,
965 set expr_name to its name and an error message will be generated.
966 */
967 const char* expr_name = NULL;
968
969 /* The ast defines augmented store and load contexts, but the
970 implementation here doesn't actually use them. The code may be
971 a little more complex than necessary as a result. It also means
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000972 that expressions in an augmented assignment have a Store context.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000973 Consider restructuring so that augmented assignment uses
Guido van Rossumc2e20742006-02-27 22:32:47 +0000974 set_context(), too.
Jeremy Hyltonc960f262006-01-27 15:18:39 +0000975 */
976 assert(ctx != AugStore && ctx != AugLoad);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977
978 switch (e->kind) {
979 case Attribute_kind:
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700980 if (ctx == NamedStore) {
981 expr_name = "attribute";
982 break;
983 }
984
Thomas Wouters89f507f2006-12-13 04:49:30 +0000985 e->v.Attribute.ctx = ctx;
Benjamin Petersonbd0df502012-09-02 15:04:51 -0400986 if (ctx == Store && forbidden_name(c, e->v.Attribute.attr, n, 1))
Benjamin Peterson70f52762009-06-28 23:32:44 +0000987 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000988 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989 case Subscript_kind:
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700990 if (ctx == NamedStore) {
991 expr_name = "subscript";
992 break;
993 }
994
Thomas Wouters89f507f2006-12-13 04:49:30 +0000995 e->v.Subscript.ctx = ctx;
996 break;
Guido van Rossum0368b722007-05-11 16:50:42 +0000997 case Starred_kind:
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700998 if (ctx == NamedStore) {
999 expr_name = "starred";
1000 break;
1001 }
1002
Guido van Rossum0368b722007-05-11 16:50:42 +00001003 e->v.Starred.ctx = ctx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001004 if (!set_context(c, e->v.Starred.value, ctx, n))
Guido van Rossum0368b722007-05-11 16:50:42 +00001005 return 0;
1006 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007 case Name_kind:
Guido van Rossume7ba4952007-06-06 23:52:48 +00001008 if (ctx == Store) {
Benjamin Peterson442f2092012-12-06 17:41:04 -05001009 if (forbidden_name(c, e->v.Name.id, n, 0))
Guido van Rossume7ba4952007-06-06 23:52:48 +00001010 return 0; /* forbidden_name() calls ast_error() */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001011 }
1012 e->v.Name.ctx = ctx;
1013 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014 case List_kind:
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001015 if (ctx == NamedStore) {
1016 expr_name = "list";
1017 break;
1018 }
1019
Thomas Wouters89f507f2006-12-13 04:49:30 +00001020 e->v.List.ctx = ctx;
1021 s = e->v.List.elts;
1022 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001023 case Tuple_kind:
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001024 if (ctx == NamedStore) {
1025 expr_name = "tuple";
1026 break;
1027 }
1028
Berker Peksag094c9c92016-05-18 08:44:29 +03001029 e->v.Tuple.ctx = ctx;
1030 s = e->v.Tuple.elts;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001031 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001032 case Lambda_kind:
1033 expr_name = "lambda";
1034 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001035 case Call_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001036 expr_name = "function call";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001037 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001038 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039 case BinOp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001040 case UnaryOp_kind:
1041 expr_name = "operator";
1042 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043 case GeneratorExp_kind:
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001044 expr_name = "generator expression";
1045 break;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001046 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001047 case YieldFrom_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001048 expr_name = "yield expression";
1049 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001050 case Await_kind:
1051 expr_name = "await expression";
1052 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001053 case ListComp_kind:
1054 expr_name = "list comprehension";
1055 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001056 case SetComp_kind:
1057 expr_name = "set comprehension";
1058 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001059 case DictComp_kind:
1060 expr_name = "dict comprehension";
1061 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001062 case Dict_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001063 expr_name = "dict display";
1064 break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001065 case Set_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001066 expr_name = "set display";
1067 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001068 case JoinedStr_kind:
1069 case FormattedValue_kind:
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001070 expr_name = "f-string expression";
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001071 break;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001072 case Constant_kind: {
1073 PyObject *value = e->v.Constant.value;
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001074 if (value == Py_None || value == Py_False || value == Py_True
1075 || value == Py_Ellipsis)
1076 {
1077 return ast_error(c, n, "cannot %s %R",
1078 ctx == Store ? "assign to" : "delete",
1079 value);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001080 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001081 expr_name = "literal";
Benjamin Peterson442f2092012-12-06 17:41:04 -05001082 break;
Serhiy Storchaka3f228112018-09-27 17:42:37 +03001083 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001084 case Compare_kind:
1085 expr_name = "comparison";
1086 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001087 case IfExp_kind:
1088 expr_name = "conditional expression";
1089 break;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001090 case NamedExpr_kind:
1091 expr_name = "named expression";
1092 break;
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001093 default:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 PyErr_Format(PyExc_SystemError,
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001095 "unexpected expression in %sassignment %d (line %d)",
1096 ctx == NamedStore ? "named ": "",
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001097 e->kind, e->lineno);
1098 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001100 /* Check for error string set by switch */
1101 if (expr_name) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001102 if (ctx == NamedStore) {
1103 return ast_error(c, n, "cannot use named assignment with %s",
1104 expr_name);
1105 }
1106 else {
1107 return ast_error(c, n, "cannot %s %s",
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02001108 ctx == Store ? "assign to" : "delete",
1109 expr_name);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001110 }
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001111 }
1112
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113 /* If the LHS is a list or tuple, we need to set the assignment
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 context for all the contained elements.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001115 */
1116 if (s) {
Victor Stinner4d73ae72018-11-22 14:45:16 +01001117 Py_ssize_t i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118
Thomas Wouters89f507f2006-12-13 04:49:30 +00001119 for (i = 0; i < asdl_seq_LEN(s); i++) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001120 if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n))
Thomas Wouters89f507f2006-12-13 04:49:30 +00001121 return 0;
1122 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123 }
1124 return 1;
1125}
1126
1127static operator_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001128ast_for_augassign(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129{
1130 REQ(n, augassign);
1131 n = CHILD(n, 0);
1132 switch (STR(n)[0]) {
1133 case '+':
1134 return Add;
1135 case '-':
1136 return Sub;
1137 case '/':
1138 if (STR(n)[1] == '/')
1139 return FloorDiv;
1140 else
1141 return Div;
1142 case '%':
1143 return Mod;
1144 case '<':
1145 return LShift;
1146 case '>':
1147 return RShift;
1148 case '&':
1149 return BitAnd;
1150 case '^':
1151 return BitXor;
1152 case '|':
1153 return BitOr;
1154 case '*':
1155 if (STR(n)[1] == '*')
1156 return Pow;
1157 else
1158 return Mult;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001159 case '@':
1160 return MatMult;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001162 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001163 return (operator_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 }
1165}
1166
1167static cmpop_ty
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001168ast_for_comp_op(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169{
Guido van Rossumb053cd82006-08-24 03:53:23 +00001170 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 |'is' 'not'
1172 */
1173 REQ(n, comp_op);
1174 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001175 n = CHILD(n, 0);
1176 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177 case LESS:
1178 return Lt;
1179 case GREATER:
1180 return Gt;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001181 case EQEQUAL: /* == */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182 return Eq;
1183 case LESSEQUAL:
1184 return LtE;
1185 case GREATEREQUAL:
1186 return GtE;
1187 case NOTEQUAL:
1188 return NotEq;
1189 case NAME:
1190 if (strcmp(STR(n), "in") == 0)
1191 return In;
1192 if (strcmp(STR(n), "is") == 0)
1193 return Is;
Stefan Krahf432a322017-08-21 13:09:59 +02001194 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001196 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197 STR(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001198 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001199 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200 }
1201 else if (NCH(n) == 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00001202 /* handle "not in" and "is not" */
1203 switch (TYPE(CHILD(n, 0))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204 case NAME:
1205 if (strcmp(STR(CHILD(n, 1)), "in") == 0)
1206 return NotIn;
1207 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
1208 return IsNot;
Stefan Krahf432a322017-08-21 13:09:59 +02001209 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001211 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212 STR(CHILD(n, 0)), STR(CHILD(n, 1)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001213 return (cmpop_ty)0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001214 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215 }
Neal Norwitz79792652005-11-14 04:25:03 +00001216 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217 NCH(n));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001218 return (cmpop_ty)0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219}
1220
1221static asdl_seq *
1222seq_for_testlist(struct compiling *c, const node *n)
1223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 /* testlist: test (',' test)* [',']
Benjamin Peterson4905e802009-09-27 02:43:28 +00001225 testlist_star_expr: test|star_expr (',' test|star_expr)* [',']
1226 */
Armin Rigo31441302005-10-21 12:57:31 +00001227 asdl_seq *seq;
1228 expr_ty expression;
1229 int i;
Benjamin Peterson4905e802009-09-27 02:43:28 +00001230 assert(TYPE(n) == testlist || TYPE(n) == testlist_star_expr || TYPE(n) == testlist_comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001231
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001232 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233 if (!seq)
1234 return NULL;
1235
1236 for (i = 0; i < NCH(n); i += 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 const node *ch = CHILD(n, i);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001238 assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001239
Benjamin Peterson4905e802009-09-27 02:43:28 +00001240 expression = ast_for_expr(c, ch);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001241 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243
1244 assert(i / 2 < seq->size);
1245 asdl_seq_SET(seq, i / 2, expression);
1246 }
1247 return seq;
1248}
1249
Neal Norwitzc1505362006-12-28 06:47:50 +00001250static arg_ty
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001251ast_for_arg(struct compiling *c, const node *n)
Neal Norwitzc1505362006-12-28 06:47:50 +00001252{
1253 identifier name;
1254 expr_ty annotation = NULL;
1255 node *ch;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001256 arg_ty ret;
Neal Norwitzc1505362006-12-28 06:47:50 +00001257
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001258 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);
Neal Norwitzc1505362006-12-28 06:47:50 +00001259 ch = CHILD(n, 0);
Neal Norwitzc1505362006-12-28 06:47:50 +00001260 name = NEW_IDENTIFIER(ch);
1261 if (!name)
1262 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001263 if (forbidden_name(c, name, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001264 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001265
1266 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) {
1267 annotation = ast_for_expr(c, CHILD(n, 2));
1268 if (!annotation)
1269 return NULL;
1270 }
1271
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001272 ret = arg(name, annotation, LINENO(n), n->n_col_offset,
1273 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001274 if (!ret)
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001275 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05001276 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001277}
1278
Guido van Rossum4f72a782006-10-27 23:31:49 +00001279/* returns -1 if failed to handle keyword only arguments
1280 returns new position to keep processing if successful
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001281 (',' tfpdef ['=' test])*
Guido van Rossum4f72a782006-10-27 23:31:49 +00001282 ^^^
1283 start pointing here
1284 */
1285static int
1286handle_keywordonly_args(struct compiling *c, const node *n, int start,
1287 asdl_seq *kwonlyargs, asdl_seq *kwdefaults)
1288{
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001289 PyObject *argname;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001290 node *ch;
Neal Norwitzc1505362006-12-28 06:47:50 +00001291 expr_ty expression, annotation;
1292 arg_ty arg;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001293 int i = start;
1294 int j = 0; /* index for kwdefaults and kwonlyargs */
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001295
1296 if (kwonlyargs == NULL) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001297 ast_error(c, CHILD(n, start), "named arguments must follow bare *");
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001298 return -1;
1299 }
1300 assert(kwdefaults != NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001301 while (i < NCH(n)) {
1302 ch = CHILD(n, i);
1303 switch (TYPE(ch)) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001304 case vfpdef:
1305 case tfpdef:
Guido van Rossum4f72a782006-10-27 23:31:49 +00001306 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001307 expression = ast_for_expr(c, CHILD(n, i + 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001308 if (!expression)
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001309 goto error;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001310 asdl_seq_SET(kwdefaults, j, expression);
1311 i += 2; /* '=' and test */
1312 }
1313 else { /* setting NULL if no default value exists */
1314 asdl_seq_SET(kwdefaults, j, NULL);
1315 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001316 if (NCH(ch) == 3) {
1317 /* ch is NAME ':' test */
1318 annotation = ast_for_expr(c, CHILD(ch, 2));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001319 if (!annotation)
Neal Norwitzc1505362006-12-28 06:47:50 +00001320 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001321 }
1322 else {
1323 annotation = NULL;
1324 }
1325 ch = CHILD(ch, 0);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001326 argname = NEW_IDENTIFIER(ch);
1327 if (!argname)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001328 goto error;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001329 if (forbidden_name(c, argname, ch, 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001330 goto error;
Victor Stinnerc106c682015-11-06 17:01:48 +01001331 arg = arg(argname, annotation, LINENO(ch), ch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001332 ch->n_end_lineno, ch->n_end_col_offset,
Victor Stinnerc106c682015-11-06 17:01:48 +01001333 c->c_arena);
Benjamin Petersond951e7b2008-11-25 22:19:53 +00001334 if (!arg)
1335 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001336 asdl_seq_SET(kwonlyargs, j++, arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001337 i += 2; /* the name and the comma */
1338 break;
1339 case DOUBLESTAR:
1340 return i;
1341 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001342 ast_error(c, ch, "unexpected node");
Guido van Rossum4f72a782006-10-27 23:31:49 +00001343 goto error;
1344 }
1345 }
1346 return i;
1347 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 return -1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001349}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350
Jeremy Hyltona8293132006-02-28 17:58:27 +00001351/* Create AST for argument list. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352
1353static arguments_ty
1354ast_for_arguments(struct compiling *c, const node *n)
1355{
Neal Norwitzc1505362006-12-28 06:47:50 +00001356 /* This function handles both typedargslist (function definition)
1357 and varargslist (lambda definition).
1358
1359 parameters: '(' [typedargslist] ')'
Robert Collinsdf395992015-08-12 08:00:06 +12001360 typedargslist: (tfpdef ['=' test] (',' tfpdef ['=' test])* [',' [
1361 '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1362 | '**' tfpdef [',']]]
1363 | '*' [tfpdef] (',' tfpdef ['=' test])* [',' ['**' tfpdef [',']]]
1364 | '**' tfpdef [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001365 tfpdef: NAME [':' test]
Robert Collinsdf395992015-08-12 08:00:06 +12001366 varargslist: (vfpdef ['=' test] (',' vfpdef ['=' test])* [',' [
1367 '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1368 | '**' vfpdef [',']]]
1369 | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]]
1370 | '**' vfpdef [',']
1371 )
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001372 vfpdef: NAME
Robert Collinsdf395992015-08-12 08:00:06 +12001373
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001374 */
Guido van Rossum4f72a782006-10-27 23:31:49 +00001375 int i, j, k, nposargs = 0, nkwonlyargs = 0;
1376 int nposdefaults = 0, found_default = 0;
1377 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001378 arg_ty vararg = NULL, kwarg = NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001379 arg_ty arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001380 node *ch;
1381
1382 if (TYPE(n) == parameters) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001383 if (NCH(n) == 2) /* () as argument list */
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001384 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001385 n = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001386 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001387 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388
Jeremy Hyltone921e022008-07-17 16:37:17 +00001389 /* First count the number of positional args & defaults. The
1390 variable i is the loop index for this for loop and the next.
1391 The next loop picks up where the first leaves off.
1392 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393 for (i = 0; i < NCH(n); i++) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001394 ch = CHILD(n, i);
1395 if (TYPE(ch) == STAR) {
Jeremy Hyltone921e022008-07-17 16:37:17 +00001396 /* skip star */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001397 i++;
Jeremy Hyltone921e022008-07-17 16:37:17 +00001398 if (i < NCH(n) && /* skip argument following star */
1399 (TYPE(CHILD(n, i)) == tfpdef ||
1400 TYPE(CHILD(n, i)) == vfpdef)) {
1401 i++;
1402 }
1403 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001404 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001405 if (TYPE(ch) == DOUBLESTAR) break;
Neal Norwitzc1505362006-12-28 06:47:50 +00001406 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001407 if (TYPE(ch) == EQUAL) nposdefaults++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 /* count the number of keyword only args &
Guido van Rossum4f72a782006-10-27 23:31:49 +00001410 defaults for keyword only args */
1411 for ( ; i < NCH(n); ++i) {
1412 ch = CHILD(n, i);
1413 if (TYPE(ch) == DOUBLESTAR) break;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001414 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001415 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001416 posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001417 if (!posargs && nposargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001418 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001419 kwonlyargs = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001420 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001421 if (!kwonlyargs && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001422 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 posdefaults = (nposdefaults ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001424 _Py_asdl_seq_new(nposdefaults, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001425 if (!posdefaults && nposdefaults)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001426 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 /* The length of kwonlyargs and kwdefaults are same
Guido van Rossum4f72a782006-10-27 23:31:49 +00001428 since we set NULL as default for keyword only argument w/o default
1429 - we have sequence data structure, but no dictionary */
Neal Norwitzc1505362006-12-28 06:47:50 +00001430 kwdefaults = (nkwonlyargs ?
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001431 _Py_asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001432 if (!kwdefaults && nkwonlyargs)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001433 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001434
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001435 /* tfpdef: NAME [':' test]
1436 vfpdef: NAME
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437 */
1438 i = 0;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001439 j = 0; /* index for defaults */
1440 k = 0; /* index for args */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441 while (i < NCH(n)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001442 ch = CHILD(n, i);
1443 switch (TYPE(ch)) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001444 case tfpdef:
1445 case vfpdef:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is
1447 anything other than EQUAL or a comma? */
1448 /* XXX Should NCH(n) check be made a separate check? */
1449 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001450 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
1451 if (!expression)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001452 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001453 assert(posdefaults != NULL);
1454 asdl_seq_SET(posdefaults, j++, expression);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455 i += 2;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001456 found_default = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00001458 else if (found_default) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001459 ast_error(c, n,
Guido van Rossum4f72a782006-10-27 23:31:49 +00001460 "non-default argument follows default argument");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001461 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001462 }
Benjamin Petersonbc4665e2012-03-12 11:00:41 -07001463 arg = ast_for_arg(c, ch);
Neal Norwitzc1505362006-12-28 06:47:50 +00001464 if (!arg)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001465 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001466 asdl_seq_SET(posargs, k++, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467 i += 2; /* the name and the comma */
1468 break;
1469 case STAR:
Robert Collinsdf395992015-08-12 08:00:06 +12001470 if (i+1 >= NCH(n) ||
1471 (i+2 == NCH(n) && TYPE(CHILD(n, i+1)) == COMMA)) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001472 ast_error(c, CHILD(n, i),
Amaury Forgeot d'Arc40d3a672007-12-09 21:49:48 +00001473 "named arguments must follow bare *");
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001474 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001475 }
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001476 ch = CHILD(n, i+1); /* tfpdef or COMMA */
Neal Norwitzc1505362006-12-28 06:47:50 +00001477 if (TYPE(ch) == COMMA) {
1478 int res = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001479 i += 2; /* now follows keyword only arguments */
1480 res = handle_keywordonly_args(c, n, i,
1481 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001482 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001483 i = res; /* res has new position to process */
1484 }
1485 else {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001486 vararg = ast_for_arg(c, ch);
Benjamin Peterson30760062008-11-25 04:02:28 +00001487 if (!vararg)
1488 return NULL;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001489
Guido van Rossum4f72a782006-10-27 23:31:49 +00001490 i += 3;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001491 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
1492 || TYPE(CHILD(n, i)) == vfpdef)) {
Guido van Rossum4f72a782006-10-27 23:31:49 +00001493 int res = 0;
1494 res = handle_keywordonly_args(c, n, i,
1495 kwonlyargs, kwdefaults);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001496 if (res == -1) return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001497 i = res; /* res has new position to process */
1498 }
1499 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500 break;
1501 case DOUBLESTAR:
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001502 ch = CHILD(n, i+1); /* tfpdef */
1503 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef);
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001504 kwarg = ast_for_arg(c, ch);
Amaury Forgeot d'Arc12844e62010-08-19 21:32:38 +00001505 if (!kwarg)
1506 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507 i += 3;
1508 break;
1509 default:
Neal Norwitz79792652005-11-14 04:25:03 +00001510 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511 "unexpected node in varargslist: %d @ %d",
1512 TYPE(ch), i);
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001513 return NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001514 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001515 }
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001516 return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517}
1518
1519static expr_ty
1520ast_for_dotted_name(struct compiling *c, const node *n)
1521{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001522 expr_ty e;
1523 identifier id;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001524 int lineno, col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525 int i;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001526 node *ch;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527
1528 REQ(n, dotted_name);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001529
1530 lineno = LINENO(n);
1531 col_offset = n->n_col_offset;
1532
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001533 ch = CHILD(n, 0);
1534 id = NEW_IDENTIFIER(ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535 if (!id)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001536 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001537 e = Name(id, Load, lineno, col_offset,
1538 ch->n_end_lineno, ch->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001540 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541
1542 for (i = 2; i < NCH(n); i+=2) {
1543 id = NEW_IDENTIFIER(CHILD(n, i));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001544 if (!id)
1545 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001546 e = Attribute(e, id, Load, lineno, col_offset,
1547 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001548 if (!e)
1549 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001550 }
1551
1552 return e;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001553}
1554
1555static expr_ty
1556ast_for_decorator(struct compiling *c, const node *n)
1557{
1558 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
1559 expr_ty d = NULL;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001560 expr_ty name_expr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562 REQ(n, decorator);
Jeremy Hyltonc960f262006-01-27 15:18:39 +00001563 REQ(CHILD(n, 0), AT);
1564 REQ(RCHILD(n, -1), NEWLINE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566 name_expr = ast_for_dotted_name(c, CHILD(n, 1));
1567 if (!name_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001568 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570 if (NCH(n) == 3) { /* No arguments */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001571 d = name_expr;
1572 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001573 }
1574 else if (NCH(n) == 5) { /* Call with no arguments */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001575 d = Call(name_expr, NULL, NULL, LINENO(n),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001576 n->n_col_offset, n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001577 if (!d)
1578 return NULL;
1579 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001580 }
1581 else {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001582 d = ast_for_call(c, CHILD(n, 3), name_expr, CHILD(n, 2), CHILD(n, 4));
Thomas Wouters89f507f2006-12-13 04:49:30 +00001583 if (!d)
1584 return NULL;
1585 name_expr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586 }
1587
1588 return d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589}
1590
1591static asdl_seq*
1592ast_for_decorators(struct compiling *c, const node *n)
1593{
Neal Norwitz84456bd2005-12-18 03:16:20 +00001594 asdl_seq* decorator_seq;
Neal Norwitze76adcd2005-11-15 05:04:31 +00001595 expr_ty d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598 REQ(n, decorators);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001599 decorator_seq = _Py_asdl_seq_new(NCH(n), c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600 if (!decorator_seq)
1601 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603 for (i = 0; i < NCH(n); i++) {
Jeremy Hyltona8293132006-02-28 17:58:27 +00001604 d = ast_for_decorator(c, CHILD(n, i));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001605 if (!d)
1606 return NULL;
1607 asdl_seq_SET(decorator_seq, i, d);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001608 }
1609 return decorator_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001610}
1611
1612static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04001613ast_for_funcdef_impl(struct compiling *c, const node *n0,
1614 asdl_seq *decorator_seq, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001615{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001616 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
guoci90fc8982018-09-11 17:45:45 -04001617 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00001618 identifier name;
1619 arguments_ty args;
1620 asdl_seq *body;
Neal Norwitzc1505362006-12-28 06:47:50 +00001621 expr_ty returns = NULL;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001622 int name_i = 1;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001623 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001624
1625 REQ(n, funcdef);
1626
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001627 name = NEW_IDENTIFIER(CHILD(n, name_i));
1628 if (!name)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001629 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04001630 if (forbidden_name(c, name, CHILD(n, name_i), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00001631 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001632 args = ast_for_arguments(c, CHILD(n, name_i + 1));
1633 if (!args)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001634 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001635 if (TYPE(CHILD(n, name_i+2)) == RARROW) {
1636 returns = ast_for_expr(c, CHILD(n, name_i + 3));
1637 if (!returns)
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00001638 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001639 name_i += 2;
1640 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001641 body = ast_for_suite(c, CHILD(n, name_i + 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001642 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001643 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001644 get_last_end_pos(body, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001645
Yury Selivanov75445082015-05-11 22:57:16 -04001646 if (is_async)
1647 return AsyncFunctionDef(name, args, body, decorator_seq, returns,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001648 LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001649 else
1650 return FunctionDef(name, args, body, decorator_seq, returns,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001651 LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04001652}
1653
1654static stmt_ty
1655ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1656{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001657 /* async_funcdef: 'async' funcdef */
Yury Selivanov75445082015-05-11 22:57:16 -04001658 REQ(n, async_funcdef);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001659 REQ(CHILD(n, 0), NAME);
1660 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001661 REQ(CHILD(n, 1), funcdef);
1662
guoci90fc8982018-09-11 17:45:45 -04001663 return ast_for_funcdef_impl(c, n, decorator_seq,
1664 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001665}
1666
1667static stmt_ty
1668ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
1669{
1670 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */
1671 return ast_for_funcdef_impl(c, n, decorator_seq,
guoci90fc8982018-09-11 17:45:45 -04001672 false /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001673}
1674
1675
1676static stmt_ty
1677ast_for_async_stmt(struct compiling *c, const node *n)
1678{
Jelle Zijlstraac317702017-10-05 20:24:46 -07001679 /* async_stmt: 'async' (funcdef | with_stmt | for_stmt) */
Yury Selivanov75445082015-05-11 22:57:16 -04001680 REQ(n, async_stmt);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001681 REQ(CHILD(n, 0), NAME);
1682 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
Yury Selivanov75445082015-05-11 22:57:16 -04001683
1684 switch (TYPE(CHILD(n, 1))) {
1685 case funcdef:
guoci90fc8982018-09-11 17:45:45 -04001686 return ast_for_funcdef_impl(c, n, NULL,
1687 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001688 case with_stmt:
guoci90fc8982018-09-11 17:45:45 -04001689 return ast_for_with_stmt(c, n,
1690 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001691
1692 case for_stmt:
guoci90fc8982018-09-11 17:45:45 -04001693 return ast_for_for_stmt(c, n,
1694 true /* is_async */);
Yury Selivanov75445082015-05-11 22:57:16 -04001695
1696 default:
1697 PyErr_Format(PyExc_SystemError,
1698 "invalid async stament: %s",
1699 STR(CHILD(n, 1)));
1700 return NULL;
1701 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702}
1703
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001704static stmt_ty
1705ast_for_decorated(struct compiling *c, const node *n)
1706{
Yury Selivanov75445082015-05-11 22:57:16 -04001707 /* decorated: decorators (classdef | funcdef | async_funcdef) */
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001708 stmt_ty thing = NULL;
1709 asdl_seq *decorator_seq = NULL;
1710
1711 REQ(n, decorated);
1712
1713 decorator_seq = ast_for_decorators(c, CHILD(n, 0));
1714 if (!decorator_seq)
1715 return NULL;
1716
1717 assert(TYPE(CHILD(n, 1)) == funcdef ||
Yury Selivanov75445082015-05-11 22:57:16 -04001718 TYPE(CHILD(n, 1)) == async_funcdef ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 TYPE(CHILD(n, 1)) == classdef);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001720
1721 if (TYPE(CHILD(n, 1)) == funcdef) {
1722 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
1723 } else if (TYPE(CHILD(n, 1)) == classdef) {
1724 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
Yury Selivanov75445082015-05-11 22:57:16 -04001725 } else if (TYPE(CHILD(n, 1)) == async_funcdef) {
1726 thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001727 }
1728 return thing;
1729}
1730
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731static expr_ty
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001732ast_for_namedexpr(struct compiling *c, const node *n)
1733{
1734 /* if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)*
1735 ['else' ':' suite]
1736 namedexpr_test: test [':=' test]
1737 argument: ( test [comp_for] |
1738 test ':=' test |
1739 test '=' test |
1740 '**' test |
1741 '*' test )
1742 */
1743 expr_ty target, value;
1744
1745 target = ast_for_expr(c, CHILD(n, 0));
1746 if (!target)
1747 return NULL;
1748
1749 value = ast_for_expr(c, CHILD(n, 2));
1750 if (!value)
1751 return NULL;
1752
1753 if (!set_context(c, target, NamedStore, n))
1754 return NULL;
1755
1756 return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno,
1757 n->n_end_col_offset, c->c_arena);
1758}
1759
1760static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761ast_for_lambdef(struct compiling *c, const node *n)
1762{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001763 /* lambdef: 'lambda' [varargslist] ':' test
1764 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001765 arguments_ty args;
1766 expr_ty expression;
1767
1768 if (NCH(n) == 3) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001769 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770 if (!args)
1771 return NULL;
1772 expression = ast_for_expr(c, CHILD(n, 2));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001773 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001774 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775 }
1776 else {
1777 args = ast_for_arguments(c, CHILD(n, 1));
1778 if (!args)
1779 return NULL;
1780 expression = ast_for_expr(c, CHILD(n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001781 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001783 }
1784
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001785 return Lambda(args, expression, LINENO(n), n->n_col_offset,
1786 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001787}
1788
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001789static expr_ty
1790ast_for_ifexpr(struct compiling *c, const node *n)
1791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 /* test: or_test 'if' or_test 'else' test */
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001793 expr_ty expression, body, orelse;
1794
Thomas Woutersaa8b6c52006-02-27 16:46:22 +00001795 assert(NCH(n) == 5);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001796 body = ast_for_expr(c, CHILD(n, 0));
1797 if (!body)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001798 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001799 expression = ast_for_expr(c, CHILD(n, 2));
1800 if (!expression)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001801 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001802 orelse = ast_for_expr(c, CHILD(n, 4));
1803 if (!orelse)
Thomas Wouters89f507f2006-12-13 04:49:30 +00001804 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001805 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001806 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001807 c->c_arena);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001808}
1809
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810/*
Nick Coghlan650f0d02007-04-15 12:05:43 +00001811 Count the number of 'for' loops in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812
Nick Coghlan650f0d02007-04-15 12:05:43 +00001813 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814*/
1815
1816static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001817count_comp_fors(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001819 int n_fors = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820
Guido van Rossumd8faa362007-04-27 19:54:29 +00001821 count_comp_for:
1822 n_fors++;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001823 REQ(n, comp_for);
Jelle Zijlstraac317702017-10-05 20:24:46 -07001824 if (NCH(n) == 2) {
1825 REQ(CHILD(n, 0), NAME);
1826 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1827 n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001828 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001829 else if (NCH(n) == 1) {
1830 n = CHILD(n, 0);
1831 }
1832 else {
1833 goto error;
1834 }
1835 if (NCH(n) == (5)) {
1836 n = CHILD(n, 4);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001837 }
1838 else {
Guido van Rossumd8faa362007-04-27 19:54:29 +00001839 return n_fors;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001840 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001841 count_comp_iter:
Guido van Rossum992d4a32007-07-11 13:09:30 +00001842 REQ(n, comp_iter);
1843 n = CHILD(n, 0);
1844 if (TYPE(n) == comp_for)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001845 goto count_comp_for;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001846 else if (TYPE(n) == comp_if) {
1847 if (NCH(n) == 3) {
1848 n = CHILD(n, 2);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001849 goto count_comp_iter;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001850 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00001851 else
1852 return n_fors;
1853 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00001854
Jelle Zijlstraac317702017-10-05 20:24:46 -07001855 error:
Guido van Rossumd8faa362007-04-27 19:54:29 +00001856 /* Should never be reached */
1857 PyErr_SetString(PyExc_SystemError,
1858 "logic error in count_comp_fors");
1859 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860}
1861
Nick Coghlan650f0d02007-04-15 12:05:43 +00001862/* Count the number of 'if' statements in a comprehension.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001863
Nick Coghlan650f0d02007-04-15 12:05:43 +00001864 Helper for ast_for_comprehension().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865*/
1866
1867static int
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001868count_comp_ifs(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001869{
Guido van Rossumd8faa362007-04-27 19:54:29 +00001870 int n_ifs = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001871
Guido van Rossumd8faa362007-04-27 19:54:29 +00001872 while (1) {
1873 REQ(n, comp_iter);
1874 if (TYPE(CHILD(n, 0)) == comp_for)
1875 return n_ifs;
1876 n = CHILD(n, 0);
1877 REQ(n, comp_if);
1878 n_ifs++;
1879 if (NCH(n) == 2)
1880 return n_ifs;
1881 n = CHILD(n, 2);
1882 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883}
1884
Guido van Rossum992d4a32007-07-11 13:09:30 +00001885static asdl_seq *
1886ast_for_comprehension(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001888 int i, n_fors;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001889 asdl_seq *comps;
1890
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001891 n_fors = count_comp_fors(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001892 if (n_fors == -1)
1893 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001894
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001895 comps = _Py_asdl_seq_new(n_fors, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001896 if (!comps)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001898
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001899 for (i = 0; i < n_fors; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00001900 comprehension_ty comp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001901 asdl_seq *t;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001902 expr_ty expression, first;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001903 node *for_ch;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001904 node *sync_n;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001905 int is_async = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906
Guido van Rossum992d4a32007-07-11 13:09:30 +00001907 REQ(n, comp_for);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908
Jelle Zijlstraac317702017-10-05 20:24:46 -07001909 if (NCH(n) == 2) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001910 is_async = 1;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001911 REQ(CHILD(n, 0), NAME);
1912 assert(strcmp(STR(CHILD(n, 0)), "async") == 0);
1913 sync_n = CHILD(n, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001914 }
Jelle Zijlstraac317702017-10-05 20:24:46 -07001915 else {
1916 sync_n = CHILD(n, 0);
1917 }
1918 REQ(sync_n, sync_comp_for);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001919
Jelle Zijlstraac317702017-10-05 20:24:46 -07001920 for_ch = CHILD(sync_n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001921 t = ast_for_exprlist(c, for_ch, Store);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001922 if (!t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001923 return NULL;
Jelle Zijlstraac317702017-10-05 20:24:46 -07001924 expression = ast_for_expr(c, CHILD(sync_n, 3));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001925 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001926 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001927
Thomas Wouters89f507f2006-12-13 04:49:30 +00001928 /* Check the # of children rather than the length of t, since
1929 (x for x, in ...) has 1 element in t, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00001930 first = (expr_ty)asdl_seq_GET(t, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001931 if (NCH(for_ch) == 1)
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001932 comp = comprehension(first, expression, NULL,
1933 is_async, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001934 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00001935 comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset,
1936 for_ch->n_end_lineno, for_ch->n_end_col_offset,
1937 c->c_arena),
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001938 expression, NULL, is_async, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001939 if (!comp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001941
Jelle Zijlstraac317702017-10-05 20:24:46 -07001942 if (NCH(sync_n) == 5) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001943 int j, n_ifs;
1944 asdl_seq *ifs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945
Jelle Zijlstraac317702017-10-05 20:24:46 -07001946 n = CHILD(sync_n, 4);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001947 n_ifs = count_comp_ifs(c, n);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001948 if (n_ifs == -1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001949 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001950
Antoine Pitroud01d396e2013-10-12 22:52:43 +02001951 ifs = _Py_asdl_seq_new(n_ifs, c->c_arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001952 if (!ifs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001953 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001954
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001955 for (j = 0; j < n_ifs; j++) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00001956 REQ(n, comp_iter);
1957 n = CHILD(n, 0);
1958 REQ(n, comp_if);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959
Guido van Rossum992d4a32007-07-11 13:09:30 +00001960 expression = ast_for_expr(c, CHILD(n, 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001961 if (!expression)
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00001962 return NULL;
Jeremy Hyltona8293132006-02-28 17:58:27 +00001963 asdl_seq_SET(ifs, j, expression);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001964 if (NCH(n) == 3)
1965 n = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001966 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001967 /* on exit, must guarantee that n is a comp_for */
1968 if (TYPE(n) == comp_iter)
1969 n = CHILD(n, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001970 comp->ifs = ifs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001971 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001972 asdl_seq_SET(comps, i, comp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001973 }
Guido van Rossum992d4a32007-07-11 13:09:30 +00001974 return comps;
1975}
1976
1977static expr_ty
1978ast_for_itercomp(struct compiling *c, const node *n, int type)
1979{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001980 /* testlist_comp: (test|star_expr)
1981 * ( comp_for | (',' (test|star_expr))* [','] ) */
Guido van Rossum992d4a32007-07-11 13:09:30 +00001982 expr_ty elt;
1983 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001984 node *ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985
Guido van Rossum992d4a32007-07-11 13:09:30 +00001986 assert(NCH(n) > 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001988 ch = CHILD(n, 0);
1989 elt = ast_for_expr(c, ch);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001990 if (!elt)
1991 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001992 if (elt->kind == Starred_kind) {
1993 ast_error(c, ch, "iterable unpacking cannot be used in comprehension");
1994 return NULL;
1995 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996
Guido van Rossum992d4a32007-07-11 13:09:30 +00001997 comps = ast_for_comprehension(c, CHILD(n, 1));
1998 if (!comps)
1999 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002000
2001 if (type == COMP_GENEXP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002002 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset,
2003 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002004 else if (type == COMP_LISTCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002005 return ListComp(elt, comps, LINENO(n), n->n_col_offset,
2006 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002007 else if (type == COMP_SETCOMP)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002008 return SetComp(elt, comps, LINENO(n), n->n_col_offset,
2009 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002010 else
2011 /* Should never happen */
2012 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013}
2014
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002015/* Fills in the key, value pair corresponding to the dict element. In case
2016 * of an unpacking, key is NULL. *i is advanced by the number of ast
2017 * elements. Iff successful, nonzero is returned.
2018 */
2019static int
2020ast_for_dictelement(struct compiling *c, const node *n, int *i,
2021 expr_ty *key, expr_ty *value)
2022{
2023 expr_ty expression;
2024 if (TYPE(CHILD(n, *i)) == DOUBLESTAR) {
2025 assert(NCH(n) - *i >= 2);
2026
2027 expression = ast_for_expr(c, CHILD(n, *i + 1));
2028 if (!expression)
2029 return 0;
2030 *key = NULL;
2031 *value = expression;
2032
2033 *i += 2;
2034 }
2035 else {
2036 assert(NCH(n) - *i >= 3);
2037
2038 expression = ast_for_expr(c, CHILD(n, *i));
2039 if (!expression)
2040 return 0;
2041 *key = expression;
2042
2043 REQ(CHILD(n, *i + 1), COLON);
2044
2045 expression = ast_for_expr(c, CHILD(n, *i + 2));
2046 if (!expression)
2047 return 0;
2048 *value = expression;
2049
2050 *i += 3;
2051 }
2052 return 1;
2053}
2054
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002055static expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00002056ast_for_dictcomp(struct compiling *c, const node *n)
2057{
2058 expr_ty key, value;
2059 asdl_seq *comps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002060 int i = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002062 if (!ast_for_dictelement(c, n, &i, &key, &value))
Guido van Rossum992d4a32007-07-11 13:09:30 +00002063 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002064 assert(key);
2065 assert(NCH(n) - i >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002067 comps = ast_for_comprehension(c, CHILD(n, i));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002068 if (!comps)
2069 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002071 return DictComp(key, value, comps, LINENO(n), n->n_col_offset,
2072 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002073}
2074
2075static expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002076ast_for_dictdisplay(struct compiling *c, const node *n)
2077{
2078 int i;
2079 int j;
2080 int size;
2081 asdl_seq *keys, *values;
2082
2083 size = (NCH(n) + 1) / 3; /* +1 in case no trailing comma */
2084 keys = _Py_asdl_seq_new(size, c->c_arena);
2085 if (!keys)
2086 return NULL;
2087
2088 values = _Py_asdl_seq_new(size, c->c_arena);
2089 if (!values)
2090 return NULL;
2091
2092 j = 0;
2093 for (i = 0; i < NCH(n); i++) {
2094 expr_ty key, value;
2095
2096 if (!ast_for_dictelement(c, n, &i, &key, &value))
2097 return NULL;
2098 asdl_seq_SET(keys, j, key);
2099 asdl_seq_SET(values, j, value);
2100
2101 j++;
2102 }
2103 keys->size = j;
2104 values->size = j;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002105 return Dict(keys, values, LINENO(n), n->n_col_offset,
2106 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002107}
2108
2109static expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00002110ast_for_genexp(struct compiling *c, const node *n)
2111{
2112 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002113 return ast_for_itercomp(c, n, COMP_GENEXP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002114}
2115
2116static expr_ty
2117ast_for_listcomp(struct compiling *c, const node *n)
2118{
2119 assert(TYPE(n) == (testlist_comp));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002120 return ast_for_itercomp(c, n, COMP_LISTCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002121}
2122
2123static expr_ty
2124ast_for_setcomp(struct compiling *c, const node *n)
2125{
2126 assert(TYPE(n) == (dictorsetmaker));
Guido van Rossum992d4a32007-07-11 13:09:30 +00002127 return ast_for_itercomp(c, n, COMP_SETCOMP);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002128}
2129
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002130static expr_ty
2131ast_for_setdisplay(struct compiling *c, const node *n)
2132{
2133 int i;
2134 int size;
2135 asdl_seq *elts;
2136
2137 assert(TYPE(n) == (dictorsetmaker));
2138 size = (NCH(n) + 1) / 2; /* +1 in case no trailing comma */
2139 elts = _Py_asdl_seq_new(size, c->c_arena);
2140 if (!elts)
2141 return NULL;
2142 for (i = 0; i < NCH(n); i += 2) {
2143 expr_ty expression;
2144 expression = ast_for_expr(c, CHILD(n, i));
2145 if (!expression)
2146 return NULL;
2147 asdl_seq_SET(elts, i / 2, expression);
2148 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002149 return Set(elts, LINENO(n), n->n_col_offset,
2150 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002151}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002152
2153static expr_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154ast_for_atom(struct compiling *c, const node *n)
2155{
Nick Coghlan650f0d02007-04-15 12:05:43 +00002156 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']'
2157 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+
Guido van Rossume7ba4952007-06-06 23:52:48 +00002158 | '...' | 'None' | 'True' | 'False'
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002159 */
2160 node *ch = CHILD(n, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002162 switch (TYPE(ch)) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002163 case NAME: {
Benjamin Peterson442f2092012-12-06 17:41:04 -05002164 PyObject *name;
2165 const char *s = STR(ch);
2166 size_t len = strlen(s);
2167 if (len >= 4 && len <= 5) {
2168 if (!strcmp(s, "None"))
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002169 return Constant(Py_None, LINENO(n), n->n_col_offset,
2170 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002171 if (!strcmp(s, "True"))
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002172 return Constant(Py_True, LINENO(n), n->n_col_offset,
2173 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002174 if (!strcmp(s, "False"))
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002175 return Constant(Py_False, LINENO(n), n->n_col_offset,
2176 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson442f2092012-12-06 17:41:04 -05002177 }
2178 name = new_identifier(s, c);
Benjamin Peterson30760062008-11-25 04:02:28 +00002179 if (!name)
2180 return NULL;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002181 /* All names start in Load context, but may later be changed. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002182 return Name(name, Load, LINENO(n), n->n_col_offset,
2183 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson30760062008-11-25 04:02:28 +00002184 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002185 case STRING: {
Eric V. Smith235a6f02015-09-19 14:51:32 -04002186 expr_ty str = parsestrplus(c, n);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002187 if (!str) {
Serhiy Storchaka5e61f142013-02-10 17:36:00 +02002188 const char *errtype = NULL;
2189 if (PyErr_ExceptionMatches(PyExc_UnicodeError))
2190 errtype = "unicode error";
2191 else if (PyErr_ExceptionMatches(PyExc_ValueError))
2192 errtype = "value error";
2193 if (errtype) {
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002194 PyObject *type, *value, *tback, *errstr;
2195 PyErr_Fetch(&type, &value, &tback);
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00002196 errstr = PyObject_Str(value);
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002197 if (errstr) {
2198 ast_error(c, n, "(%s) %U", errtype, errstr);
2199 Py_DECREF(errstr);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002200 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002201 else {
2202 PyErr_Clear();
2203 ast_error(c, n, "(%s) unknown error", errtype);
2204 }
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002205 Py_DECREF(type);
Victor Stinner0fae8f92013-07-17 21:51:42 +02002206 Py_XDECREF(value);
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002207 Py_XDECREF(tback);
2208 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002209 return NULL;
Guido van Rossumb5a755e2007-07-18 18:15:48 +00002210 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04002211 return str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002212 }
2213 case NUMBER: {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002214 PyObject *pynum = parsenumber(c, STR(ch));
Thomas Wouters89f507f2006-12-13 04:49:30 +00002215 if (!pynum)
2216 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002217
Victor Stinner43d81952013-07-17 00:57:58 +02002218 if (PyArena_AddPyObject(c->c_arena, pynum) < 0) {
2219 Py_DECREF(pynum);
2220 return NULL;
2221 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002222 return Constant(pynum, LINENO(n), n->n_col_offset,
2223 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002224 }
Georg Brandldde00282007-03-18 19:01:53 +00002225 case ELLIPSIS: /* Ellipsis */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002226 return Constant(Py_Ellipsis, LINENO(n), n->n_col_offset,
2227 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002228 case LPAR: /* some parenthesized expressions */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002229 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230
Thomas Wouters89f507f2006-12-13 04:49:30 +00002231 if (TYPE(ch) == RPAR)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002232 return Tuple(NULL, Load, LINENO(n), n->n_col_offset,
2233 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234
Thomas Wouters89f507f2006-12-13 04:49:30 +00002235 if (TYPE(ch) == yield_expr)
2236 return ast_for_expr(c, ch);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002239 if (NCH(ch) == 1) {
2240 return ast_for_testlist(c, ch);
2241 }
Benjamin Peterson78565b22009-06-28 19:19:51 +00002242
Serhiy Storchakab619b092018-11-27 09:40:29 +02002243 if (TYPE(CHILD(ch, 1)) == comp_for) {
2244 return copy_location(ast_for_genexp(c, ch), n);
2245 }
2246 else {
2247 return copy_location(ast_for_testlist(c, ch), n);
2248 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249 case LSQB: /* list (or list comprehension) */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002250 ch = CHILD(n, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251
Thomas Wouters89f507f2006-12-13 04:49:30 +00002252 if (TYPE(ch) == RSQB)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002253 return List(NULL, Load, LINENO(n), n->n_col_offset,
2254 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255
Nick Coghlan650f0d02007-04-15 12:05:43 +00002256 REQ(ch, testlist_comp);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002257 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) {
2258 asdl_seq *elts = seq_for_testlist(c, ch);
2259 if (!elts)
2260 return NULL;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002261
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002262 return List(elts, Load, LINENO(n), n->n_col_offset,
2263 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002264 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002265 else {
2266 return copy_location(ast_for_listcomp(c, ch), n);
2267 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268 case LBRACE: {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002269 /* dictorsetmaker: ( ((test ':' test | '**' test)
2270 * (comp_for | (',' (test ':' test | '**' test))* [','])) |
2271 * ((test | '*' test)
2272 * (comp_for | (',' (test | '*' test))* [','])) ) */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002273 expr_ty res;
Neal Norwitzc1505362006-12-28 06:47:50 +00002274 ch = CHILD(n, 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002275 if (TYPE(ch) == RBRACE) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002276 /* It's an empty dict. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002277 return Dict(NULL, NULL, LINENO(n), n->n_col_offset,
2278 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002279 }
2280 else {
2281 int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR);
2282 if (NCH(ch) == 1 ||
2283 (NCH(ch) > 1 &&
2284 TYPE(CHILD(ch, 1)) == COMMA)) {
2285 /* It's a set display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002286 res = ast_for_setdisplay(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002287 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002288 else if (NCH(ch) > 1 &&
2289 TYPE(CHILD(ch, 1)) == comp_for) {
2290 /* It's a set comprehension. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002291 res = ast_for_setcomp(c, ch);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002292 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002293 else if (NCH(ch) > 3 - is_dict &&
2294 TYPE(CHILD(ch, 3 - is_dict)) == comp_for) {
2295 /* It's a dictionary comprehension. */
2296 if (is_dict) {
2297 ast_error(c, n, "dict unpacking cannot be used in "
2298 "dict comprehension");
2299 return NULL;
2300 }
Benjamin Peterson58b53952015-09-25 22:44:43 -07002301 res = ast_for_dictcomp(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002302 }
2303 else {
2304 /* It's a dictionary display. */
Benjamin Peterson58b53952015-09-25 22:44:43 -07002305 res = ast_for_dictdisplay(c, ch);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002306 }
Serhiy Storchakab619b092018-11-27 09:40:29 +02002307 return copy_location(res, n);
Guido van Rossum86e58e22006-08-28 15:27:34 +00002308 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002310 default:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002311 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch));
2312 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002313 }
2314}
2315
2316static slice_ty
2317ast_for_slice(struct compiling *c, const node *n)
2318{
2319 node *ch;
2320 expr_ty lower = NULL, upper = NULL, step = NULL;
2321
2322 REQ(n, subscript);
2323
2324 /*
Georg Brandl52318d62006-09-06 07:06:08 +00002325 subscript: test | [test] ':' [test] [sliceop]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002326 sliceop: ':' [test]
2327 */
2328 ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002329 if (NCH(n) == 1 && TYPE(ch) == test) {
2330 /* 'step' variable hold no significance in terms of being used over
2331 other vars */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 step = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002333 if (!step)
2334 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335
Thomas Wouters89f507f2006-12-13 04:49:30 +00002336 return Index(step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002337 }
2338
2339 if (TYPE(ch) == test) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002340 lower = ast_for_expr(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002341 if (!lower)
2342 return NULL;
2343 }
2344
2345 /* If there's an upper bound it's in the second or third position. */
2346 if (TYPE(ch) == COLON) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002347 if (NCH(n) > 1) {
2348 node *n2 = CHILD(n, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002349
Thomas Wouters89f507f2006-12-13 04:49:30 +00002350 if (TYPE(n2) == test) {
2351 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002352 if (!upper)
2353 return NULL;
2354 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002355 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002356 } else if (NCH(n) > 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002357 node *n2 = CHILD(n, 2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002358
Thomas Wouters89f507f2006-12-13 04:49:30 +00002359 if (TYPE(n2) == test) {
2360 upper = ast_for_expr(c, n2);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002361 if (!upper)
2362 return NULL;
2363 }
2364 }
2365
2366 ch = CHILD(n, NCH(n) - 1);
2367 if (TYPE(ch) == sliceop) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00002368 if (NCH(ch) != 1) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002369 ch = CHILD(ch, 1);
2370 if (TYPE(ch) == test) {
2371 step = ast_for_expr(c, ch);
2372 if (!step)
2373 return NULL;
2374 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002375 }
2376 }
2377
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002378 return Slice(lower, upper, step, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002379}
2380
2381static expr_ty
2382ast_for_binop(struct compiling *c, const node *n)
2383{
Guido van Rossumd8faa362007-04-27 19:54:29 +00002384 /* Must account for a sequence of expressions.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 How should A op B op C by represented?
Guido van Rossumd8faa362007-04-27 19:54:29 +00002386 BinOp(BinOp(A, op, B), op, C).
2387 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002388
Guido van Rossumd8faa362007-04-27 19:54:29 +00002389 int i, nops;
2390 expr_ty expr1, expr2, result;
2391 operator_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002392
Guido van Rossumd8faa362007-04-27 19:54:29 +00002393 expr1 = ast_for_expr(c, CHILD(n, 0));
2394 if (!expr1)
2395 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002396
Guido van Rossumd8faa362007-04-27 19:54:29 +00002397 expr2 = ast_for_expr(c, CHILD(n, 2));
2398 if (!expr2)
2399 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002400
Guido van Rossumd8faa362007-04-27 19:54:29 +00002401 newoperator = get_operator(CHILD(n, 1));
2402 if (!newoperator)
2403 return NULL;
2404
2405 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002406 CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002407 c->c_arena);
2408 if (!result)
2409 return NULL;
2410
2411 nops = (NCH(n) - 1) / 2;
2412 for (i = 1; i < nops; i++) {
2413 expr_ty tmp_result, tmp;
2414 const node* next_oper = CHILD(n, i * 2 + 1);
2415
2416 newoperator = get_operator(next_oper);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002417 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002418 return NULL;
2419
Guido van Rossumd8faa362007-04-27 19:54:29 +00002420 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2));
2421 if (!tmp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002422 return NULL;
2423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 tmp_result = BinOp(result, newoperator, tmp,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002425 LINENO(next_oper), next_oper->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002426 CHILD(n, i * 2 + 2)->n_end_lineno,
2427 CHILD(n, i * 2 + 2)->n_end_col_offset,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002428 c->c_arena);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 if (!tmp_result)
Guido van Rossumd8faa362007-04-27 19:54:29 +00002430 return NULL;
2431 result = tmp_result;
2432 }
2433 return result;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002434}
2435
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002436static expr_ty
2437ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)
2438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002440 subscriptlist: subscript (',' subscript)* [',']
2441 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
2442 */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002443 const node *n_copy = n;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002444 REQ(n, trailer);
2445 if (TYPE(CHILD(n, 0)) == LPAR) {
2446 if (NCH(n) == 2)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002447 return Call(left_expr, NULL, NULL, LINENO(n), n->n_col_offset,
2448 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002449 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002450 return ast_for_call(c, CHILD(n, 1), left_expr, CHILD(n, 0), CHILD(n, 2));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002451 }
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002452 else if (TYPE(CHILD(n, 0)) == DOT) {
Benjamin Peterson30760062008-11-25 04:02:28 +00002453 PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));
2454 if (!attr_id)
2455 return NULL;
2456 return Attribute(left_expr, attr_id, Load,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002457 LINENO(n), n->n_col_offset,
2458 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton9ebfbf02006-02-27 16:50:35 +00002459 }
2460 else {
2461 REQ(CHILD(n, 0), LSQB);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002462 REQ(CHILD(n, 2), RSQB);
2463 n = CHILD(n, 1);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002464 if (NCH(n) == 1) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002465 slice_ty slc = ast_for_slice(c, CHILD(n, 0));
2466 if (!slc)
2467 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002468 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002469 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002470 c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002471 }
2472 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 /* The grammar is ambiguous here. The ambiguity is resolved
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002474 by treating the sequence as a tuple literal if there are
2475 no slice features.
2476 */
Victor Stinner4d73ae72018-11-22 14:45:16 +01002477 Py_ssize_t j;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002478 slice_ty slc;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002479 expr_ty e;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002480 int simple = 1;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002481 asdl_seq *slices, *elts;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002482 slices = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002483 if (!slices)
2484 return NULL;
2485 for (j = 0; j < NCH(n); j += 2) {
2486 slc = ast_for_slice(c, CHILD(n, j));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002487 if (!slc)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002488 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002489 if (slc->kind != Index_kind)
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002490 simple = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002491 asdl_seq_SET(slices, j / 2, slc);
2492 }
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002493 if (!simple) {
2494 return Subscript(left_expr, ExtSlice(slices, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002495 Load, LINENO(n), n->n_col_offset,
2496 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002497 }
2498 /* extract Index values and put them in a Tuple */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002499 elts = _Py_asdl_seq_new(asdl_seq_LEN(slices), c->c_arena);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00002500 if (!elts)
2501 return NULL;
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002502 for (j = 0; j < asdl_seq_LEN(slices); ++j) {
2503 slc = (slice_ty)asdl_seq_GET(slices, j);
2504 assert(slc->kind == Index_kind && slc->v.Index.value);
2505 asdl_seq_SET(elts, j, slc->v.Index.value);
2506 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002507 e = Tuple(elts, Load, LINENO(n), n->n_col_offset,
2508 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hyltonc7d37262006-02-27 17:29:29 +00002509 if (!e)
2510 return NULL;
2511 return Subscript(left_expr, Index(e, c->c_arena),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002512 Load, LINENO(n), n->n_col_offset,
2513 n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002514 }
2515 }
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002516}
2517
2518static expr_ty
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002519ast_for_factor(struct compiling *c, const node *n)
2520{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002521 expr_ty expression;
2522
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002523 expression = ast_for_expr(c, CHILD(n, 1));
2524 if (!expression)
2525 return NULL;
2526
2527 switch (TYPE(CHILD(n, 0))) {
2528 case PLUS:
2529 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002530 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002531 c->c_arena);
2532 case MINUS:
2533 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002534 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002535 c->c_arena);
2536 case TILDE:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002537 return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset,
2538 n->n_end_lineno, n->n_end_col_offset,
2539 c->c_arena);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002540 }
2541 PyErr_Format(PyExc_SystemError, "unhandled factor: %d",
2542 TYPE(CHILD(n, 0)));
2543 return NULL;
2544}
2545
2546static expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04002547ast_for_atom_expr(struct compiling *c, const node *n)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002548{
Yury Selivanov75445082015-05-11 22:57:16 -04002549 int i, nch, start = 0;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002550 expr_ty e, tmp;
Yury Selivanov75445082015-05-11 22:57:16 -04002551
2552 REQ(n, atom_expr);
2553 nch = NCH(n);
2554
Jelle Zijlstraac317702017-10-05 20:24:46 -07002555 if (TYPE(CHILD(n, 0)) == NAME && strcmp(STR(CHILD(n, 0)), "await") == 0) {
Yury Selivanov75445082015-05-11 22:57:16 -04002556 start = 1;
2557 assert(nch > 1);
2558 }
2559
2560 e = ast_for_atom(c, CHILD(n, start));
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002561 if (!e)
2562 return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002563 if (nch == 1)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002564 return e;
Yury Selivanov75445082015-05-11 22:57:16 -04002565 if (start && nch == 2) {
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002566 return Await(e, LINENO(n), n->n_col_offset,
2567 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002568 }
2569
2570 for (i = start + 1; i < nch; i++) {
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002571 node *ch = CHILD(n, i);
2572 if (TYPE(ch) != trailer)
2573 break;
2574 tmp = ast_for_trailer(c, ch, e);
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002575 if (!tmp)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002576 return NULL;
Benjamin Peterson7a66fc22015-02-02 10:51:20 -05002577 tmp->lineno = e->lineno;
2578 tmp->col_offset = e->col_offset;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002579 e = tmp;
2580 }
Yury Selivanov75445082015-05-11 22:57:16 -04002581
2582 if (start) {
Jelle Zijlstraac317702017-10-05 20:24:46 -07002583 /* there was an 'await' */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002584 return Await(e, LINENO(n), n->n_col_offset,
2585 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04002586 }
2587 else {
2588 return e;
2589 }
2590}
2591
2592static expr_ty
2593ast_for_power(struct compiling *c, const node *n)
2594{
2595 /* power: atom trailer* ('**' factor)*
2596 */
2597 expr_ty e;
2598 REQ(n, power);
2599 e = ast_for_atom_expr(c, CHILD(n, 0));
2600 if (!e)
2601 return NULL;
2602 if (NCH(n) == 1)
2603 return e;
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002604 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) {
2605 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1));
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002606 if (!f)
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002607 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002608 e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset,
2609 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002610 }
2611 return e;
2612}
2613
Guido van Rossum0368b722007-05-11 16:50:42 +00002614static expr_ty
2615ast_for_starred(struct compiling *c, const node *n)
2616{
2617 expr_ty tmp;
2618 REQ(n, star_expr);
2619
2620 tmp = ast_for_expr(c, CHILD(n, 1));
2621 if (!tmp)
2622 return NULL;
2623
2624 /* The Load context is changed later. */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002625 return Starred(tmp, Load, LINENO(n), n->n_col_offset,
2626 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Guido van Rossum0368b722007-05-11 16:50:42 +00002627}
2628
2629
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002630/* Do not name a variable 'expr'! Will cause a compile error.
2631*/
2632
2633static expr_ty
2634ast_for_expr(struct compiling *c, const node *n)
2635{
2636 /* handle the full range of simple expressions
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002637 namedexpr_test: test [':=' test]
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002638 test: or_test ['if' or_test 'else' test] | lambdef
Nick Coghlan650f0d02007-04-15 12:05:43 +00002639 test_nocond: or_test | lambdef_nocond
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002640 or_test: and_test ('or' and_test)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641 and_test: not_test ('and' not_test)*
2642 not_test: 'not' not_test | comparison
2643 comparison: expr (comp_op expr)*
2644 expr: xor_expr ('|' xor_expr)*
2645 xor_expr: and_expr ('^' and_expr)*
2646 and_expr: shift_expr ('&' shift_expr)*
2647 shift_expr: arith_expr (('<<'|'>>') arith_expr)*
2648 arith_expr: term (('+'|'-') term)*
Benjamin Petersond51374e2014-04-09 23:55:56 -04002649 term: factor (('*'|'@'|'/'|'%'|'//') factor)*
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002650 factor: ('+'|'-'|'~') factor | power
Yury Selivanov75445082015-05-11 22:57:16 -04002651 power: atom_expr ['**' factor]
Jelle Zijlstraac317702017-10-05 20:24:46 -07002652 atom_expr: ['await'] atom trailer*
Yury Selivanov75445082015-05-11 22:57:16 -04002653 yield_expr: 'yield' [yield_arg]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654 */
2655
2656 asdl_seq *seq;
2657 int i;
2658
2659 loop:
2660 switch (TYPE(n)) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002661 case namedexpr_test:
2662 if (NCH(n) == 3)
2663 return ast_for_namedexpr(c, n);
2664 /* Fallthrough */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002665 case test:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002666 case test_nocond:
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002667 if (TYPE(CHILD(n, 0)) == lambdef ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002668 TYPE(CHILD(n, 0)) == lambdef_nocond)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002669 return ast_for_lambdef(c, CHILD(n, 0));
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002670 else if (NCH(n) > 1)
2671 return ast_for_ifexpr(c, n);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002672 /* Fallthrough */
2673 case or_test:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002674 case and_test:
2675 if (NCH(n) == 1) {
2676 n = CHILD(n, 0);
2677 goto loop;
2678 }
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002679 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002680 if (!seq)
2681 return NULL;
2682 for (i = 0; i < NCH(n); i += 2) {
2683 expr_ty e = ast_for_expr(c, CHILD(n, i));
2684 if (!e)
2685 return NULL;
2686 asdl_seq_SET(seq, i / 2, e);
2687 }
2688 if (!strcmp(STR(CHILD(n, 1)), "and"))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002689 return BoolOp(And, seq, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002690 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002691 c->c_arena);
Neal Norwitz84456bd2005-12-18 03:16:20 +00002692 assert(!strcmp(STR(CHILD(n, 1)), "or"));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002693 return BoolOp(Or, seq, LINENO(n), n->n_col_offset,
2694 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695 case not_test:
2696 if (NCH(n) == 1) {
2697 n = CHILD(n, 0);
2698 goto loop;
2699 }
2700 else {
2701 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
2702 if (!expression)
2703 return NULL;
2704
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002705 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002706 n->n_end_lineno, n->n_end_col_offset,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002707 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002708 }
2709 case comparison:
2710 if (NCH(n) == 1) {
2711 n = CHILD(n, 0);
2712 goto loop;
2713 }
2714 else {
2715 expr_ty expression;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002716 asdl_int_seq *ops;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002717 asdl_seq *cmps;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002718 ops = _Py_asdl_int_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719 if (!ops)
2720 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002721 cmps = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002722 if (!cmps) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002723 return NULL;
2724 }
2725 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002726 cmpop_ty newoperator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002727
Christian Heimes81ee3ef2008-05-04 22:42:01 +00002728 newoperator = ast_for_comp_op(c, CHILD(n, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002729 if (!newoperator) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002730 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002731 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002732
2733 expression = ast_for_expr(c, CHILD(n, i + 1));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002734 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002736 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002738 asdl_seq_SET(ops, i / 2, newoperator);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002739 asdl_seq_SET(cmps, i / 2, expression);
2740 }
2741 expression = ast_for_expr(c, CHILD(n, 0));
Neal Norwitze76adcd2005-11-15 05:04:31 +00002742 if (!expression) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002744 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002745
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002746 return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset,
2747 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748 }
2749 break;
2750
Guido van Rossum0368b722007-05-11 16:50:42 +00002751 case star_expr:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 return ast_for_starred(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753 /* The next five cases all handle BinOps. The main body of code
2754 is the same in each case, but the switch turned inside out to
2755 reuse the code for each type of operator.
2756 */
2757 case expr:
2758 case xor_expr:
2759 case and_expr:
2760 case shift_expr:
2761 case arith_expr:
2762 case term:
2763 if (NCH(n) == 1) {
2764 n = CHILD(n, 0);
2765 goto loop;
2766 }
2767 return ast_for_binop(c, n);
2768 case yield_expr: {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002769 node *an = NULL;
2770 node *en = NULL;
2771 int is_from = 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002772 expr_ty exp = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002773 if (NCH(n) > 1)
2774 an = CHILD(n, 1); /* yield_arg */
2775 if (an) {
2776 en = CHILD(an, NCH(an) - 1);
2777 if (NCH(an) == 2) {
2778 is_from = 1;
2779 exp = ast_for_expr(c, en);
2780 }
2781 else
2782 exp = ast_for_testlist(c, en);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002783 if (!exp)
2784 return NULL;
2785 }
Benjamin Peterson527c6222012-01-14 08:58:23 -05002786 if (is_from)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002787 return YieldFrom(exp, LINENO(n), n->n_col_offset,
2788 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
2789 return Yield(exp, LINENO(n), n->n_col_offset,
2790 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002791 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002792 case factor:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793 if (NCH(n) == 1) {
2794 n = CHILD(n, 0);
2795 goto loop;
2796 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002797 return ast_for_factor(c, n);
Neil Schemenauer982e8d62005-10-25 09:16:05 +00002798 case power:
2799 return ast_for_power(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002800 default:
Neal Norwitz79792652005-11-14 04:25:03 +00002801 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002802 return NULL;
2803 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00002804 /* should never get here unless if error is set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805 return NULL;
2806}
2807
2808static expr_ty
Serhiy Storchakab619b092018-11-27 09:40:29 +02002809ast_for_call(struct compiling *c, const node *n, expr_ty func,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002810 const node *maybegenbeg, const node *closepar)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811{
2812 /*
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002813 arglist: argument (',' argument)* [',']
2814 argument: ( test [comp_for] | '*' test | test '=' test | '**' test )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002815 */
2816
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002817 int i, nargs, nkeywords;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002818 int ndoublestars;
Neal Norwitz84456bd2005-12-18 03:16:20 +00002819 asdl_seq *args;
2820 asdl_seq *keywords;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002821
2822 REQ(n, arglist);
2823
2824 nargs = 0;
2825 nkeywords = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002827 node *ch = CHILD(n, i);
2828 if (TYPE(ch) == argument) {
2829 if (NCH(ch) == 1)
2830 nargs++;
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002831 else if (TYPE(CHILD(ch, 1)) == comp_for) {
2832 nargs++;
Serhiy Storchakab619b092018-11-27 09:40:29 +02002833 if (!maybegenbeg) {
Serhiy Storchakaddbce132017-11-15 17:39:37 +02002834 ast_error(c, ch, "invalid syntax");
2835 return NULL;
2836 }
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002837 if (NCH(n) > 1) {
2838 ast_error(c, ch, "Generator expression must be parenthesized");
2839 return NULL;
2840 }
2841 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002842 else if (TYPE(CHILD(ch, 0)) == STAR)
2843 nargs++;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002844 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
2845 nargs++;
2846 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002847 else
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002848 /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002849 nkeywords++;
2850 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002851 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002852
Serhiy Storchaka9165f772017-11-15 08:49:40 +02002853 args = _Py_asdl_seq_new(nargs, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854 if (!args)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002855 return NULL;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02002856 keywords = _Py_asdl_seq_new(nkeywords, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857 if (!keywords)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002858 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002859
2860 nargs = 0; /* positional arguments + iterable argument unpackings */
2861 nkeywords = 0; /* keyword arguments + keyword argument unpackings */
2862 ndoublestars = 0; /* just keyword argument unpackings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002863 for (i = 0; i < NCH(n); i++) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00002864 node *ch = CHILD(n, i);
2865 if (TYPE(ch) == argument) {
2866 expr_ty e;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002867 node *chch = CHILD(ch, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002868 if (NCH(ch) == 1) {
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002869 /* a positional argument */
2870 if (nkeywords) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002871 if (ndoublestars) {
2872 ast_error(c, chch,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002873 "positional argument follows "
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002874 "keyword argument unpacking");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002875 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002876 else {
2877 ast_error(c, chch,
2878 "positional argument follows "
2879 "keyword argument");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002880 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002881 return NULL;
Benjamin Peterson2d735bc2008-08-19 20:57:10 +00002882 }
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002883 e = ast_for_expr(c, chch);
2884 if (!e)
2885 return NULL;
2886 asdl_seq_SET(args, nargs++, e);
2887 }
2888 else if (TYPE(chch) == STAR) {
2889 /* an iterable argument unpacking */
2890 expr_ty starred;
2891 if (ndoublestars) {
2892 ast_error(c, chch,
2893 "iterable argument unpacking follows "
2894 "keyword argument unpacking");
2895 return NULL;
2896 }
2897 e = ast_for_expr(c, CHILD(ch, 1));
2898 if (!e)
2899 return NULL;
2900 starred = Starred(e, Load, LINENO(chch),
2901 chch->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00002902 chch->n_end_lineno, chch->n_end_col_offset,
Yury Selivanov14acf5f2015-08-05 17:54:10 -04002903 c->c_arena);
2904 if (!starred)
2905 return NULL;
2906 asdl_seq_SET(args, nargs++, starred);
2907
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002908 }
2909 else if (TYPE(chch) == DOUBLESTAR) {
2910 /* a keyword argument unpacking */
2911 keyword_ty kw;
2912 i++;
2913 e = ast_for_expr(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002914 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002915 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002916 kw = keyword(NULL, e, c->c_arena);
2917 asdl_seq_SET(keywords, nkeywords++, kw);
2918 ndoublestars++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002920 else if (TYPE(CHILD(ch, 1)) == comp_for) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002921 /* the lone generator expression */
Serhiy Storchakab619b092018-11-27 09:40:29 +02002922 e = copy_location(ast_for_genexp(c, ch), maybegenbeg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002923 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002924 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00002925 asdl_seq_SET(args, nargs++, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002926 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07002927 else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) {
2928 /* treat colon equal as positional argument */
2929 if (nkeywords) {
2930 if (ndoublestars) {
2931 ast_error(c, chch,
2932 "positional argument follows "
2933 "keyword argument unpacking");
2934 }
2935 else {
2936 ast_error(c, chch,
2937 "positional argument follows "
2938 "keyword argument");
2939 }
2940 return NULL;
2941 }
2942 e = ast_for_namedexpr(c, ch);
2943 if (!e)
2944 return NULL;
2945 asdl_seq_SET(args, nargs++, e);
2946 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00002947 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002948 /* a keyword argument */
Thomas Wouters89f507f2006-12-13 04:49:30 +00002949 keyword_ty kw;
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002950 identifier key, tmp;
2951 int k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002952
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07002953 // To remain LL(1), the grammar accepts any test (basically, any
2954 // expression) in the keyword slot of a call site. So, we need
2955 // to manually enforce that the keyword is a NAME here.
2956 static const int name_tree[] = {
2957 test,
2958 or_test,
2959 and_test,
2960 not_test,
2961 comparison,
2962 expr,
2963 xor_expr,
2964 and_expr,
2965 shift_expr,
2966 arith_expr,
2967 term,
2968 factor,
2969 power,
2970 atom_expr,
2971 atom,
2972 0,
2973 };
2974 node *expr_node = chch;
2975 for (int i = 0; name_tree[i]; i++) {
2976 if (TYPE(expr_node) != name_tree[i])
2977 break;
2978 if (NCH(expr_node) != 1)
2979 break;
2980 expr_node = CHILD(expr_node, 0);
2981 }
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002982 if (TYPE(expr_node) != NAME) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002983 ast_error(c, chch,
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02002984 "expression cannot contain assignment, "
2985 "perhaps you meant \"==\"?");
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002986 return NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002987 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07002988 key = new_identifier(STR(expr_node), c);
2989 if (key == NULL) {
Benjamin Petersonc64ae922012-01-16 18:02:21 -05002990 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 }
Benjamin Petersonc9a71dd2018-09-12 17:14:39 -07002992 if (forbidden_name(c, key, chch, 1)) {
2993 return NULL;
2994 }
Benjamin Peterson07a1f942008-07-01 20:03:27 +00002995 for (k = 0; k < nkeywords; k++) {
2996 tmp = ((keyword_ty)asdl_seq_GET(keywords, k))->arg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002997 if (tmp && !PyUnicode_Compare(tmp, key)) {
2998 ast_error(c, chch,
2999 "keyword argument repeated");
Benjamin Peterson07a1f942008-07-01 20:03:27 +00003000 return NULL;
3001 }
3002 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003003 e = ast_for_expr(c, CHILD(ch, 2));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003004 if (!e)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003005 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003006 kw = keyword(key, e, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003007 if (!kw)
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003008 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003009 asdl_seq_SET(keywords, nkeywords++, kw);
3010 }
3011 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003012 }
3013
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003014 return Call(func, args, keywords, func->lineno, func->col_offset,
3015 closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003016}
3017
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003018static expr_ty
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003019ast_for_testlist(struct compiling *c, const node* n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003020{
Nick Coghlan650f0d02007-04-15 12:05:43 +00003021 /* testlist_comp: test (comp_for | (',' test)* [',']) */
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003022 /* testlist: test (',' test)* [','] */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023 assert(NCH(n) > 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003024 if (TYPE(n) == testlist_comp) {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003025 if (NCH(n) > 1)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003026 assert(TYPE(CHILD(n, 1)) != comp_for);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003027 }
3028 else {
3029 assert(TYPE(n) == testlist ||
Benjamin Peterson4905e802009-09-27 02:43:28 +00003030 TYPE(n) == testlist_star_expr);
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003031 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003032 if (NCH(n) == 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003033 return ast_for_expr(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003034 else {
3035 asdl_seq *tmp = seq_for_testlist(c, n);
3036 if (!tmp)
3037 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003038 return Tuple(tmp, Load, LINENO(n), n->n_col_offset,
3039 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003040 }
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003041}
3042
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003043static stmt_ty
3044ast_for_expr_stmt(struct compiling *c, const node *n)
3045{
3046 REQ(n, expr_stmt);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003047 /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
3048 ('=' (yield_expr|testlist_star_expr))*)
3049 annassign: ':' test ['=' test]
Benjamin Peterson4905e802009-09-27 02:43:28 +00003050 testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']
Benjamin Petersond51374e2014-04-09 23:55:56 -04003051 augassign: '+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^='
Thomas Wouters89f507f2006-12-13 04:49:30 +00003052 | '<<=' | '>>=' | '**=' | '//='
Martin Panter69332c12016-08-04 13:07:31 +00003053 test: ... here starts the operator precedence dance
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003054 */
3055
3056 if (NCH(n) == 1) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003057 expr_ty e = ast_for_testlist(c, CHILD(n, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003058 if (!e)
3059 return NULL;
3060
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003061 return Expr(e, LINENO(n), n->n_col_offset,
3062 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003063 }
3064 else if (TYPE(CHILD(n, 1)) == augassign) {
3065 expr_ty expr1, expr2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003066 operator_ty newoperator;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003067 node *ch = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003068
Thomas Wouters89f507f2006-12-13 04:49:30 +00003069 expr1 = ast_for_testlist(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003070 if (!expr1)
3071 return NULL;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003072 if(!set_context(c, expr1, Store, ch))
3073 return NULL;
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003074 /* set_context checks that most expressions are not the left side.
3075 Augmented assignments can only have a name, a subscript, or an
3076 attribute on the left, though, so we have to explicitly check for
3077 those. */
3078 switch (expr1->kind) {
3079 case Name_kind:
3080 case Attribute_kind:
3081 case Subscript_kind:
3082 break;
3083 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003084 ast_error(c, ch, "illegal expression for augmented assignment");
Benjamin Petersonbd27aef2009-10-03 20:27:13 +00003085 return NULL;
3086 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003087
Thomas Wouters89f507f2006-12-13 04:49:30 +00003088 ch = CHILD(n, 2);
3089 if (TYPE(ch) == testlist)
3090 expr2 = ast_for_testlist(c, ch);
3091 else
3092 expr2 = ast_for_expr(c, ch);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003093 if (!expr2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003094 return NULL;
3095
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003096 newoperator = ast_for_augassign(c, CHILD(n, 1));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003097 if (!newoperator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003098 return NULL;
3099
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003100 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset,
3101 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003102 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003103 else if (TYPE(CHILD(n, 1)) == annassign) {
3104 expr_ty expr1, expr2, expr3;
3105 node *ch = CHILD(n, 0);
3106 node *deep, *ann = CHILD(n, 1);
3107 int simple = 1;
3108
3109 /* we keep track of parens to qualify (x) as expression not name */
3110 deep = ch;
3111 while (NCH(deep) == 1) {
3112 deep = CHILD(deep, 0);
3113 }
3114 if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
3115 simple = 0;
3116 }
3117 expr1 = ast_for_testlist(c, ch);
3118 if (!expr1) {
3119 return NULL;
3120 }
3121 switch (expr1->kind) {
3122 case Name_kind:
3123 if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
3124 return NULL;
3125 }
3126 expr1->v.Name.ctx = Store;
3127 break;
3128 case Attribute_kind:
3129 if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
3130 return NULL;
3131 }
3132 expr1->v.Attribute.ctx = Store;
3133 break;
3134 case Subscript_kind:
3135 expr1->v.Subscript.ctx = Store;
3136 break;
3137 case List_kind:
3138 ast_error(c, ch,
3139 "only single target (not list) can be annotated");
3140 return NULL;
3141 case Tuple_kind:
3142 ast_error(c, ch,
3143 "only single target (not tuple) can be annotated");
3144 return NULL;
3145 default:
3146 ast_error(c, ch,
3147 "illegal target for annotation");
3148 return NULL;
3149 }
3150
3151 if (expr1->kind != Name_kind) {
3152 simple = 0;
3153 }
3154 ch = CHILD(ann, 1);
3155 expr2 = ast_for_expr(c, ch);
3156 if (!expr2) {
3157 return NULL;
3158 }
3159 if (NCH(ann) == 2) {
3160 return AnnAssign(expr1, expr2, NULL, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003161 LINENO(n), n->n_col_offset,
3162 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003163 }
3164 else {
3165 ch = CHILD(ann, 3);
Ivan Levkivskyi62c35a82019-01-25 01:39:19 +00003166 if (TYPE(ch) == testlist) {
3167 expr3 = ast_for_testlist(c, ch);
3168 }
3169 else {
3170 expr3 = ast_for_expr(c, ch);
3171 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003172 if (!expr3) {
3173 return NULL;
3174 }
3175 return AnnAssign(expr1, expr2, expr3, simple,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003176 LINENO(n), n->n_col_offset,
3177 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003178 }
3179 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003180 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003181 int i;
3182 asdl_seq *targets;
3183 node *value;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003184 expr_ty expression;
3185
Thomas Wouters89f507f2006-12-13 04:49:30 +00003186 /* a normal assignment */
3187 REQ(CHILD(n, 1), EQUAL);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003188 targets = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003189 if (!targets)
3190 return NULL;
3191 for (i = 0; i < NCH(n) - 2; i += 2) {
3192 expr_ty e;
3193 node *ch = CHILD(n, i);
3194 if (TYPE(ch) == yield_expr) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003195 ast_error(c, ch, "assignment to yield expression not possible");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003196 return NULL;
3197 }
3198 e = ast_for_testlist(c, ch);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003199 if (!e)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003200 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003201
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00003202 /* set context to assign */
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003203 if (!set_context(c, e, Store, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003204 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205
Thomas Wouters89f507f2006-12-13 04:49:30 +00003206 asdl_seq_SET(targets, i / 2, e);
3207 }
3208 value = CHILD(n, NCH(n) - 1);
Benjamin Peterson4905e802009-09-27 02:43:28 +00003209 if (TYPE(value) == testlist_star_expr)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003210 expression = ast_for_testlist(c, value);
3211 else
3212 expression = ast_for_expr(c, value);
3213 if (!expression)
3214 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003215 return Assign(targets, expression, LINENO(n), n->n_col_offset,
3216 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003217 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003218}
3219
Benjamin Peterson78565b22009-06-28 19:19:51 +00003220
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003221static asdl_seq *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003222ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003223{
3224 asdl_seq *seq;
3225 int i;
3226 expr_ty e;
3227
3228 REQ(n, exprlist);
3229
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003230 seq = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003231 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003232 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233 for (i = 0; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003234 e = ast_for_expr(c, CHILD(n, i));
3235 if (!e)
3236 return NULL;
3237 asdl_seq_SET(seq, i / 2, e);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00003238 if (context && !set_context(c, e, context, CHILD(n, i)))
Thomas Wouters89f507f2006-12-13 04:49:30 +00003239 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003240 }
3241 return seq;
3242}
3243
3244static stmt_ty
3245ast_for_del_stmt(struct compiling *c, const node *n)
3246{
3247 asdl_seq *expr_list;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003249 /* del_stmt: 'del' exprlist */
3250 REQ(n, del_stmt);
3251
3252 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del);
3253 if (!expr_list)
3254 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003255 return Delete(expr_list, LINENO(n), n->n_col_offset,
3256 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003257}
3258
3259static stmt_ty
3260ast_for_flow_stmt(struct compiling *c, const node *n)
3261{
3262 /*
3263 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
3264 | yield_stmt
3265 break_stmt: 'break'
3266 continue_stmt: 'continue'
3267 return_stmt: 'return' [testlist]
3268 yield_stmt: yield_expr
Nick Coghlan1f7ce622012-01-13 21:43:40 +10003269 yield_expr: 'yield' testlist | 'yield' 'from' test
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003270 raise_stmt: 'raise' [test [',' test [',' test]]]
3271 */
3272 node *ch;
3273
3274 REQ(n, flow_stmt);
3275 ch = CHILD(n, 0);
3276 switch (TYPE(ch)) {
3277 case break_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003278 return Break(LINENO(n), n->n_col_offset,
3279 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003280 case continue_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003281 return Continue(LINENO(n), n->n_col_offset,
3282 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003283 case yield_stmt: { /* will reduce to yield_expr */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003284 expr_ty exp = ast_for_expr(c, CHILD(ch, 0));
3285 if (!exp)
3286 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003287 return Expr(exp, LINENO(n), n->n_col_offset,
3288 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289 }
3290 case return_stmt:
3291 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003292 return Return(NULL, LINENO(n), n->n_col_offset,
3293 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003294 else {
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003295 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003296 if (!expression)
3297 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003298 return Return(expression, LINENO(n), n->n_col_offset,
3299 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003300 }
3301 case raise_stmt:
3302 if (NCH(ch) == 1)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003303 return Raise(NULL, NULL, LINENO(n), n->n_col_offset,
3304 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Collin Winter828f04a2007-08-31 00:04:24 +00003305 else if (NCH(ch) >= 2) {
3306 expr_ty cause = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003307 expr_ty expression = ast_for_expr(c, CHILD(ch, 1));
3308 if (!expression)
3309 return NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003310 if (NCH(ch) == 4) {
3311 cause = ast_for_expr(c, CHILD(ch, 3));
3312 if (!cause)
3313 return NULL;
3314 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003315 return Raise(expression, cause, LINENO(n), n->n_col_offset,
3316 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003317 }
Stefan Krahf432a322017-08-21 13:09:59 +02003318 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003319 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003320 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003321 "unexpected flow_stmt: %d", TYPE(ch));
3322 return NULL;
3323 }
3324}
3325
3326static alias_ty
Benjamin Peterson78565b22009-06-28 19:19:51 +00003327alias_for_import_name(struct compiling *c, const node *n, int store)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003328{
3329 /*
Thomas Wouters8ae12952006-02-28 22:42:15 +00003330 import_as_name: NAME ['as' NAME]
3331 dotted_as_name: dotted_name ['as' NAME]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003332 dotted_name: NAME ('.' NAME)*
3333 */
Benjamin Peterson78565b22009-06-28 19:19:51 +00003334 identifier str, name;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003335
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003336 loop:
3337 switch (TYPE(n)) {
Benjamin Petersonf63d6152011-06-20 21:40:19 -05003338 case import_as_name: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003339 node *name_node = CHILD(n, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003340 str = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003341 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003342 if (!name)
3343 return NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003344 if (NCH(n) == 3) {
3345 node *str_node = CHILD(n, 2);
3346 str = NEW_IDENTIFIER(str_node);
3347 if (!str)
3348 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003349 if (store && forbidden_name(c, str, str_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003350 return NULL;
3351 }
3352 else {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003353 if (forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003354 return NULL;
3355 }
Benjamin Peterson30760062008-11-25 04:02:28 +00003356 return alias(name, str, c->c_arena);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003357 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003358 case dotted_as_name:
3359 if (NCH(n) == 1) {
3360 n = CHILD(n, 0);
3361 goto loop;
3362 }
3363 else {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003364 node *asname_node = CHILD(n, 2);
3365 alias_ty a = alias_for_import_name(c, CHILD(n, 0), 0);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003366 if (!a)
3367 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003368 assert(!a->asname);
Benjamin Peterson78565b22009-06-28 19:19:51 +00003369 a->asname = NEW_IDENTIFIER(asname_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003370 if (!a->asname)
3371 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003372 if (forbidden_name(c, a->asname, asname_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003373 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374 return a;
3375 }
3376 break;
3377 case dotted_name:
Benjamin Peterson30760062008-11-25 04:02:28 +00003378 if (NCH(n) == 1) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003379 node *name_node = CHILD(n, 0);
3380 name = NEW_IDENTIFIER(name_node);
Benjamin Peterson30760062008-11-25 04:02:28 +00003381 if (!name)
3382 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003383 if (store && forbidden_name(c, name, name_node, 0))
Benjamin Peterson78565b22009-06-28 19:19:51 +00003384 return NULL;
Benjamin Peterson30760062008-11-25 04:02:28 +00003385 return alias(name, NULL, c->c_arena);
3386 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003387 else {
3388 /* Create a string of the form "a.b.c" */
Tim Peterse93e64f2006-01-08 02:28:41 +00003389 int i;
Tim Peters5db42c42006-01-08 02:25:34 +00003390 size_t len;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003391 char *s;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 PyObject *uni;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003393
3394 len = 0;
3395 for (i = 0; i < NCH(n); i += 2)
3396 /* length of string plus one for the dot */
3397 len += strlen(STR(CHILD(n, i))) + 1;
3398 len--; /* the last name doesn't have a dot */
Christian Heimes72b710a2008-05-26 13:28:38 +00003399 str = PyBytes_FromStringAndSize(NULL, len);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003400 if (!str)
3401 return NULL;
Christian Heimes72b710a2008-05-26 13:28:38 +00003402 s = PyBytes_AS_STRING(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003403 if (!s)
3404 return NULL;
3405 for (i = 0; i < NCH(n); i += 2) {
3406 char *sch = STR(CHILD(n, i));
3407 strcpy(s, STR(CHILD(n, i)));
3408 s += strlen(sch);
3409 *s++ = '.';
3410 }
3411 --s;
3412 *s = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003413 uni = PyUnicode_DecodeUTF8(PyBytes_AS_STRING(str),
3414 PyBytes_GET_SIZE(str),
3415 NULL);
3416 Py_DECREF(str);
3417 if (!uni)
3418 return NULL;
3419 str = uni;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003420 PyUnicode_InternInPlace(&str);
Victor Stinner43d81952013-07-17 00:57:58 +02003421 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3422 Py_DECREF(str);
3423 return NULL;
3424 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003425 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003426 }
3427 break;
3428 case STAR:
Martin v. Löwis5b222132007-06-10 09:51:05 +00003429 str = PyUnicode_InternFromString("*");
Alexey Izbyshev28853a22018-08-22 07:55:16 +03003430 if (!str)
3431 return NULL;
Victor Stinner43d81952013-07-17 00:57:58 +02003432 if (PyArena_AddPyObject(c->c_arena, str) < 0) {
3433 Py_DECREF(str);
3434 return NULL;
3435 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00003436 return alias(str, NULL, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003437 default:
Neal Norwitz79792652005-11-14 04:25:03 +00003438 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003439 "unexpected import name: %d", TYPE(n));
3440 return NULL;
3441 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003442
3443 PyErr_SetString(PyExc_SystemError, "unhandled import name condition");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003444 return NULL;
3445}
3446
3447static stmt_ty
3448ast_for_import_stmt(struct compiling *c, const node *n)
3449{
3450 /*
3451 import_stmt: import_name | import_from
3452 import_name: 'import' dotted_as_names
Georg Brandle66c8c72007-03-19 18:56:50 +00003453 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+)
3454 'import' ('*' | '(' import_as_names ')' | import_as_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003455 */
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003456 int lineno;
3457 int col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003458 int i;
3459 asdl_seq *aliases;
3460
3461 REQ(n, import_stmt);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003462 lineno = LINENO(n);
3463 col_offset = n->n_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003464 n = CHILD(n, 0);
Thomas Wouters8622e932006-02-27 17:14:45 +00003465 if (TYPE(n) == import_name) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003466 n = CHILD(n, 1);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003467 REQ(n, dotted_as_names);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003468 aliases = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003469 if (!aliases)
3470 return NULL;
3471 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003472 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003473 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003474 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003475 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003476 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003477 // Even though n is modified above, the end position is not changed
3478 return Import(aliases, lineno, col_offset,
3479 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003480 }
Thomas Wouters8622e932006-02-27 17:14:45 +00003481 else if (TYPE(n) == import_from) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003482 int n_children;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003483 int idx, ndots = 0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003484 const node *n_copy = n;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003485 alias_ty mod = NULL;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003486 identifier modname = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003487
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003488 /* Count the number of dots (for relative imports) and check for the
3489 optional module name */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003490 for (idx = 1; idx < NCH(n); idx++) {
3491 if (TYPE(CHILD(n, idx)) == dotted_name) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003492 mod = alias_for_import_name(c, CHILD(n, idx), 0);
3493 if (!mod)
3494 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003495 idx++;
3496 break;
Georg Brandle66c8c72007-03-19 18:56:50 +00003497 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 /* three consecutive dots are tokenized as one ELLIPSIS */
Georg Brandle66c8c72007-03-19 18:56:50 +00003499 ndots += 3;
3500 continue;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003501 } else if (TYPE(CHILD(n, idx)) != DOT) {
3502 break;
3503 }
3504 ndots++;
3505 }
3506 idx++; /* skip over the 'import' keyword */
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003507 switch (TYPE(CHILD(n, idx))) {
Thomas Wouters106203c2006-02-27 17:05:19 +00003508 case STAR:
3509 /* from ... import * */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003510 n = CHILD(n, idx);
3511 n_children = 1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003512 break;
3513 case LPAR:
3514 /* from ... import (x, y, z) */
3515 n = CHILD(n, idx + 1);
3516 n_children = NCH(n);
3517 break;
3518 case import_as_names:
3519 /* from ... import x, y, z */
3520 n = CHILD(n, idx);
3521 n_children = NCH(n);
Thomas Wouters106203c2006-02-27 17:05:19 +00003522 if (n_children % 2 == 0) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003523 ast_error(c, n, "trailing comma not allowed without"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003524 " surrounding parentheses");
3525 return NULL;
3526 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003527 break;
3528 default:
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003529 ast_error(c, n, "Unexpected node-type in from-import");
Thomas Wouters89f507f2006-12-13 04:49:30 +00003530 return NULL;
3531 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003532
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003533 aliases = _Py_asdl_seq_new((n_children + 1) / 2, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003534 if (!aliases)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003535 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003536
3537 /* handle "from ... import *" special b/c there's no children */
Thomas Wouters106203c2006-02-27 17:05:19 +00003538 if (TYPE(n) == STAR) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003539 alias_ty import_alias = alias_for_import_name(c, n, 1);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003540 if (!import_alias)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003541 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003542 asdl_seq_SET(aliases, 0, import_alias);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003543 }
Jeremy Hyltona8293132006-02-28 17:58:27 +00003544 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003545 for (i = 0; i < NCH(n); i += 2) {
Benjamin Peterson78565b22009-06-28 19:19:51 +00003546 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i), 1);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003547 if (!import_alias)
3548 return NULL;
Georg Brandl5c60ea32016-01-18 07:53:59 +01003549 asdl_seq_SET(aliases, i / 2, import_alias);
Jeremy Hyltona8293132006-02-28 17:58:27 +00003550 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003551 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003552 if (mod != NULL)
3553 modname = mod->name;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003554 return ImportFrom(modname, aliases, ndots, lineno, col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003555 n_copy->n_end_lineno, n_copy->n_end_col_offset,
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003556 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003557 }
Neal Norwitz79792652005-11-14 04:25:03 +00003558 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003559 "unknown import statement: starts with command '%s'",
3560 STR(CHILD(n, 0)));
3561 return NULL;
3562}
3563
3564static stmt_ty
3565ast_for_global_stmt(struct compiling *c, const node *n)
3566{
3567 /* global_stmt: 'global' NAME (',' NAME)* */
3568 identifier name;
3569 asdl_seq *s;
3570 int i;
3571
3572 REQ(n, global_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003573 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003574 if (!s)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003575 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576 for (i = 1; i < NCH(n); i += 2) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003577 name = NEW_IDENTIFIER(CHILD(n, i));
3578 if (!name)
3579 return NULL;
3580 asdl_seq_SET(s, i / 2, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003581 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003582 return Global(s, LINENO(n), n->n_col_offset,
3583 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584}
3585
3586static stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00003587ast_for_nonlocal_stmt(struct compiling *c, const node *n)
3588{
3589 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */
3590 identifier name;
3591 asdl_seq *s;
3592 int i;
3593
3594 REQ(n, nonlocal_stmt);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003595 s = _Py_asdl_seq_new(NCH(n) / 2, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003596 if (!s)
3597 return NULL;
3598 for (i = 1; i < NCH(n); i += 2) {
3599 name = NEW_IDENTIFIER(CHILD(n, i));
3600 if (!name)
3601 return NULL;
3602 asdl_seq_SET(s, i / 2, name);
3603 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003604 return Nonlocal(s, LINENO(n), n->n_col_offset,
3605 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton81e95022007-02-27 06:50:52 +00003606}
3607
3608static stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003609ast_for_assert_stmt(struct compiling *c, const node *n)
3610{
3611 /* assert_stmt: 'assert' test [',' test] */
3612 REQ(n, assert_stmt);
3613 if (NCH(n) == 2) {
3614 expr_ty expression = ast_for_expr(c, CHILD(n, 1));
3615 if (!expression)
3616 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003617 return Assert(expression, NULL, LINENO(n), n->n_col_offset,
3618 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003619 }
3620 else if (NCH(n) == 4) {
3621 expr_ty expr1, expr2;
3622
3623 expr1 = ast_for_expr(c, CHILD(n, 1));
3624 if (!expr1)
3625 return NULL;
3626 expr2 = ast_for_expr(c, CHILD(n, 3));
3627 if (!expr2)
3628 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003629
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003630 return Assert(expr1, expr2, LINENO(n), n->n_col_offset,
3631 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003632 }
Neal Norwitz79792652005-11-14 04:25:03 +00003633 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003634 "improper number of parts to 'assert' statement: %d",
3635 NCH(n));
3636 return NULL;
3637}
3638
3639static asdl_seq *
3640ast_for_suite(struct compiling *c, const node *n)
3641{
3642 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
Neal Norwitz84456bd2005-12-18 03:16:20 +00003643 asdl_seq *seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003644 stmt_ty s;
3645 int i, total, num, end, pos = 0;
3646 node *ch;
3647
3648 REQ(n, suite);
3649
3650 total = num_stmts(n);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003651 seq = _Py_asdl_seq_new(total, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003652 if (!seq)
Thomas Wouters89f507f2006-12-13 04:49:30 +00003653 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003654 if (TYPE(CHILD(n, 0)) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003655 n = CHILD(n, 0);
3656 /* simple_stmt always ends with a NEWLINE,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003657 and may have a trailing SEMI
Thomas Wouters89f507f2006-12-13 04:49:30 +00003658 */
3659 end = NCH(n) - 1;
3660 if (TYPE(CHILD(n, end - 1)) == SEMI)
3661 end--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003662 /* loop by 2 to skip semi-colons */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003663 for (i = 0; i < end; i += 2) {
3664 ch = CHILD(n, i);
3665 s = ast_for_stmt(c, ch);
3666 if (!s)
3667 return NULL;
3668 asdl_seq_SET(seq, pos++, s);
3669 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670 }
3671 else {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003672 for (i = 2; i < (NCH(n) - 1); i++) {
3673 ch = CHILD(n, i);
3674 REQ(ch, stmt);
3675 num = num_stmts(ch);
3676 if (num == 1) {
3677 /* small_stmt or compound_stmt with only one child */
3678 s = ast_for_stmt(c, ch);
3679 if (!s)
3680 return NULL;
3681 asdl_seq_SET(seq, pos++, s);
3682 }
3683 else {
3684 int j;
3685 ch = CHILD(ch, 0);
3686 REQ(ch, simple_stmt);
3687 for (j = 0; j < NCH(ch); j += 2) {
3688 /* statement terminates with a semi-colon ';' */
3689 if (NCH(CHILD(ch, j)) == 0) {
3690 assert((j + 1) == NCH(ch));
3691 break;
3692 }
3693 s = ast_for_stmt(c, CHILD(ch, j));
3694 if (!s)
3695 return NULL;
3696 asdl_seq_SET(seq, pos++, s);
3697 }
3698 }
3699 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003700 }
3701 assert(pos == seq->size);
3702 return seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003703}
3704
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003705static void
3706get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset)
3707{
3708 int tot = asdl_seq_LEN(s);
3709 // Suite should not be empty, but it is safe to just ignore it
3710 // if it will ever occur.
3711 if (!tot) {
3712 return;
3713 }
3714 stmt_ty last = asdl_seq_GET(s, tot - 1);
3715 *end_lineno = last->end_lineno;
3716 *end_col_offset = last->end_col_offset;
3717}
3718
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003719static stmt_ty
3720ast_for_if_stmt(struct compiling *c, const node *n)
3721{
3722 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
3723 ['else' ':' suite]
3724 */
3725 char *s;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003726 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003727
3728 REQ(n, if_stmt);
3729
3730 if (NCH(n) == 4) {
3731 expr_ty expression;
3732 asdl_seq *suite_seq;
3733
3734 expression = ast_for_expr(c, CHILD(n, 1));
3735 if (!expression)
3736 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003737 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003738 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003739 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003740 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003741
Guido van Rossumd8faa362007-04-27 19:54:29 +00003742 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003743 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003744 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003745
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003746 s = STR(CHILD(n, 4));
3747 /* s[2], the third character in the string, will be
3748 's' for el_s_e, or
3749 'i' for el_i_f
3750 */
3751 if (s[2] == 's') {
3752 expr_ty expression;
3753 asdl_seq *seq1, *seq2;
3754
3755 expression = ast_for_expr(c, CHILD(n, 1));
3756 if (!expression)
3757 return NULL;
3758 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003759 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003760 return NULL;
3761 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003762 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003763 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003764 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003765
Guido van Rossumd8faa362007-04-27 19:54:29 +00003766 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003767 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003768 }
3769 else if (s[2] == 'i') {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003770 int i, n_elif, has_else = 0;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003771 expr_ty expression;
3772 asdl_seq *suite_seq;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003773 asdl_seq *orelse = NULL;
3774 n_elif = NCH(n) - 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003775 /* must reference the child n_elif+1 since 'else' token is third,
3776 not fourth, child from the end. */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003777 if (TYPE(CHILD(n, (n_elif + 1))) == NAME
3778 && STR(CHILD(n, (n_elif + 1)))[2] == 's') {
3779 has_else = 1;
3780 n_elif -= 3;
3781 }
3782 n_elif /= 4;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003783
Thomas Wouters89f507f2006-12-13 04:49:30 +00003784 if (has_else) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003785 asdl_seq *suite_seq2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003786
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003787 orelse = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003788 if (!orelse)
3789 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003790 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003791 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003792 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003793 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4));
3794 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003795 return NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00003796 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1));
3797 if (!suite_seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003798 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003799 get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003801 asdl_seq_SET(orelse, 0,
3802 If(expression, suite_seq, suite_seq2,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003803 LINENO(CHILD(n, NCH(n) - 6)),
3804 CHILD(n, NCH(n) - 6)->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003805 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003806 /* the just-created orelse handled the last elif */
3807 n_elif--;
3808 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003809
Thomas Wouters89f507f2006-12-13 04:49:30 +00003810 for (i = 0; i < n_elif; i++) {
3811 int off = 5 + (n_elif - i - 1) * 4;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003812 asdl_seq *newobj = _Py_asdl_seq_new(1, c->c_arena);
Thomas Wouters89f507f2006-12-13 04:49:30 +00003813 if (!newobj)
3814 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003815 expression = ast_for_expr(c, CHILD(n, off));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003816 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003817 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003818 suite_seq = ast_for_suite(c, CHILD(n, off + 2));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003819 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003820 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003821
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003822 if (orelse != NULL) {
3823 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
3824 } else {
3825 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3826 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00003827 asdl_seq_SET(newobj, 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003828 If(expression, suite_seq, orelse,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003829 LINENO(CHILD(n, off)),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003830 CHILD(n, off)->n_col_offset,
3831 end_lineno, end_col_offset, c->c_arena));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003832 orelse = newobj;
3833 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00003834 expression = ast_for_expr(c, CHILD(n, 1));
3835 if (!expression)
3836 return NULL;
3837 suite_seq = ast_for_suite(c, CHILD(n, 3));
3838 if (!suite_seq)
3839 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003840 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
Guido van Rossumd8faa362007-04-27 19:54:29 +00003841 return If(expression, suite_seq, orelse,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003842 LINENO(n), n->n_col_offset,
3843 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003844 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003845
3846 PyErr_Format(PyExc_SystemError,
3847 "unexpected token in 'if' statement: %s", s);
3848 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003849}
3850
3851static stmt_ty
3852ast_for_while_stmt(struct compiling *c, const node *n)
3853{
3854 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
3855 REQ(n, while_stmt);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003856 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003857
3858 if (NCH(n) == 4) {
3859 expr_ty expression;
3860 asdl_seq *suite_seq;
3861
3862 expression = ast_for_expr(c, CHILD(n, 1));
3863 if (!expression)
3864 return NULL;
3865 suite_seq = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003866 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003867 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003868 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3869 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset,
3870 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003871 }
3872 else if (NCH(n) == 7) {
3873 expr_ty expression;
3874 asdl_seq *seq1, *seq2;
3875
3876 expression = ast_for_expr(c, CHILD(n, 1));
3877 if (!expression)
3878 return NULL;
3879 seq1 = ast_for_suite(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003880 if (!seq1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003881 return NULL;
3882 seq2 = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003883 if (!seq2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003884 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003885 get_last_end_pos(seq2, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003886
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003887 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset,
3888 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003889 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00003890
3891 PyErr_Format(PyExc_SystemError,
3892 "wrong number of tokens for 'while' statement: %d",
3893 NCH(n));
3894 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003895}
3896
3897static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04003898ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003899{
guoci90fc8982018-09-11 17:45:45 -04003900 const node * const n = is_async ? CHILD(n0, 1) : n0;
Neal Norwitz84456bd2005-12-18 03:16:20 +00003901 asdl_seq *_target, *seq = NULL, *suite_seq;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003902 expr_ty expression;
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003903 expr_ty target, first;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003904 const node *node_target;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003905 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003906 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
3907 REQ(n, for_stmt);
3908
3909 if (NCH(n) == 9) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00003910 seq = ast_for_suite(c, CHILD(n, 8));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003911 if (!seq)
3912 return NULL;
3913 }
3914
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003915 node_target = CHILD(n, 1);
3916 _target = ast_for_exprlist(c, node_target, Store);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003917 if (!_target)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003918 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003919 /* Check the # of children rather than the length of _target, since
3920 for x, in ... has 1 element in _target, but still requires a Tuple. */
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003921 first = (expr_ty)asdl_seq_GET(_target, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003922 if (NCH(node_target) == 1)
Benjamin Peterson2e4b0e12009-09-11 22:36:20 +00003923 target = first;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003924 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003925 target = Tuple(_target, Store, first->lineno, first->col_offset,
3926 node_target->n_end_lineno, node_target->n_end_col_offset,
3927 c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003928
Neil Schemenauerc5dd10a2005-10-25 07:54:54 +00003929 expression = ast_for_testlist(c, CHILD(n, 3));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003930 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003931 return NULL;
3932 suite_seq = ast_for_suite(c, CHILD(n, 5));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003933 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003934 return NULL;
3935
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003936 if (seq != NULL) {
3937 get_last_end_pos(seq, &end_lineno, &end_col_offset);
3938 } else {
3939 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
3940 }
Yury Selivanov75445082015-05-11 22:57:16 -04003941 if (is_async)
3942 return AsyncFor(target, expression, suite_seq, seq,
Benjamin Petersond13e59c2018-09-11 15:29:57 -07003943 LINENO(n0), n0->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003944 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04003945 else
3946 return For(target, expression, suite_seq, seq,
3947 LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003948 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003949}
3950
3951static excepthandler_ty
3952ast_for_except_clause(struct compiling *c, const node *exc, node *body)
3953{
Guido van Rossumd59da4b2007-05-22 18:11:13 +00003954 /* except_clause: 'except' [test ['as' test]] */
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003955 int end_lineno, end_col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003956 REQ(exc, except_clause);
3957 REQ(body, suite);
3958
3959 if (NCH(exc) == 1) {
3960 asdl_seq *suite_seq = ast_for_suite(c, body);
3961 if (!suite_seq)
3962 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003963 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003964
Neal Norwitzad74aa82008-03-31 05:14:30 +00003965 return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003966 exc->n_col_offset,
3967 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003968 }
3969 else if (NCH(exc) == 2) {
3970 expr_ty expression;
3971 asdl_seq *suite_seq;
3972
3973 expression = ast_for_expr(c, CHILD(exc, 1));
3974 if (!expression)
3975 return NULL;
3976 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003977 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003978 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003979 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003980
Neal Norwitzad74aa82008-03-31 05:14:30 +00003981 return ExceptHandler(expression, NULL, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003982 exc->n_col_offset,
3983 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003984 }
3985 else if (NCH(exc) == 4) {
3986 asdl_seq *suite_seq;
3987 expr_ty expression;
Guido van Rossum16be03e2007-01-10 18:51:35 +00003988 identifier e = NEW_IDENTIFIER(CHILD(exc, 3));
Thomas Wouters89f507f2006-12-13 04:49:30 +00003989 if (!e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003990 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04003991 if (forbidden_name(c, e, CHILD(exc, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00003992 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003993 expression = ast_for_expr(c, CHILD(exc, 1));
Neal Norwitz84456bd2005-12-18 03:16:20 +00003994 if (!expression)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003995 return NULL;
3996 suite_seq = ast_for_suite(c, body);
Neal Norwitz84456bd2005-12-18 03:16:20 +00003997 if (!suite_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003998 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00003999 get_last_end_pos(suite_seq, &end_lineno, &end_col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004000
Neal Norwitzad74aa82008-03-31 05:14:30 +00004001 return ExceptHandler(expression, e, suite_seq, LINENO(exc),
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004002 exc->n_col_offset,
4003 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004004 }
Neal Norwitz84456bd2005-12-18 03:16:20 +00004005
4006 PyErr_Format(PyExc_SystemError,
4007 "wrong number of children for 'except' clause: %d",
4008 NCH(exc));
4009 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004010}
4011
4012static stmt_ty
4013ast_for_try_stmt(struct compiling *c, const node *n)
4014{
Neal Norwitzf599f422005-12-17 21:33:47 +00004015 const int nch = NCH(n);
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004016 int end_lineno, end_col_offset, n_except = (nch - 3)/3;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004017 asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004018 excepthandler_ty last_handler;
Neal Norwitzf599f422005-12-17 21:33:47 +00004019
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004020 REQ(n, try_stmt);
4021
Neal Norwitzf599f422005-12-17 21:33:47 +00004022 body = ast_for_suite(c, CHILD(n, 2));
4023 if (body == NULL)
4024 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004025
Neal Norwitzf599f422005-12-17 21:33:47 +00004026 if (TYPE(CHILD(n, nch - 3)) == NAME) {
4027 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) {
4028 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) {
4029 /* we can assume it's an "else",
4030 because nch >= 9 for try-else-finally and
4031 it would otherwise have a type of except_clause */
4032 orelse = ast_for_suite(c, CHILD(n, nch - 4));
4033 if (orelse == NULL)
4034 return NULL;
4035 n_except--;
4036 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004037
Neal Norwitzf599f422005-12-17 21:33:47 +00004038 finally = ast_for_suite(c, CHILD(n, nch - 1));
4039 if (finally == NULL)
4040 return NULL;
4041 n_except--;
4042 }
4043 else {
4044 /* we can assume it's an "else",
4045 otherwise it would have a type of except_clause */
4046 orelse = ast_for_suite(c, CHILD(n, nch - 1));
4047 if (orelse == NULL)
4048 return NULL;
4049 n_except--;
4050 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004051 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004052 else if (TYPE(CHILD(n, nch - 3)) != except_clause) {
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004053 ast_error(c, n, "malformed 'try' statement");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004054 return NULL;
4055 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056
Neal Norwitzf599f422005-12-17 21:33:47 +00004057 if (n_except > 0) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004058 int i;
Neal Norwitzf599f422005-12-17 21:33:47 +00004059 /* process except statements to create a try ... except */
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004060 handlers = _Py_asdl_seq_new(n_except, c->c_arena);
Neal Norwitzf599f422005-12-17 21:33:47 +00004061 if (handlers == NULL)
4062 return NULL;
4063
4064 for (i = 0; i < n_except; i++) {
4065 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3),
4066 CHILD(n, 5 + i * 3));
4067 if (!e)
4068 return NULL;
4069 asdl_seq_SET(handlers, i, e);
4070 }
Neal Norwitzf599f422005-12-17 21:33:47 +00004071 }
4072
Benjamin Peterson43af12b2011-05-29 11:43:10 -05004073 assert(finally != NULL || asdl_seq_LEN(handlers));
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004074 if (finally != NULL) {
4075 // finally is always last
4076 get_last_end_pos(finally, &end_lineno, &end_col_offset);
4077 } else if (orelse != NULL) {
4078 // otherwise else is last
4079 get_last_end_pos(orelse, &end_lineno, &end_col_offset);
4080 } else {
4081 // inline the get_last_end_pos logic due to layout mismatch
4082 last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1);
4083 end_lineno = last_handler->end_lineno;
4084 end_col_offset = last_handler->end_col_offset;
4085 }
4086 return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset,
4087 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004088}
4089
Georg Brandl0c315622009-05-25 21:10:36 +00004090/* with_item: test ['as' expr] */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004091static withitem_ty
4092ast_for_with_item(struct compiling *c, const node *n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004093{
4094 expr_ty context_expr, optional_vars = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004095
Georg Brandl0c315622009-05-25 21:10:36 +00004096 REQ(n, with_item);
4097 context_expr = ast_for_expr(c, CHILD(n, 0));
Amaury Forgeot d'Arc92dc80a2010-08-19 17:43:15 +00004098 if (!context_expr)
4099 return NULL;
Georg Brandl0c315622009-05-25 21:10:36 +00004100 if (NCH(n) == 3) {
4101 optional_vars = ast_for_expr(c, CHILD(n, 2));
Guido van Rossumc2e20742006-02-27 22:32:47 +00004102
4103 if (!optional_vars) {
4104 return NULL;
4105 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004106 if (!set_context(c, optional_vars, Store, n)) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004107 return NULL;
4108 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004109 }
4110
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004111 return withitem(context_expr, optional_vars, c->c_arena);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004112}
4113
Georg Brandl0c315622009-05-25 21:10:36 +00004114/* with_stmt: 'with' with_item (',' with_item)* ':' suite */
4115static stmt_ty
guoci90fc8982018-09-11 17:45:45 -04004116ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async)
Georg Brandl0c315622009-05-25 21:10:36 +00004117{
guoci90fc8982018-09-11 17:45:45 -04004118 const node * const n = is_async ? CHILD(n0, 1) : n0;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004119 int i, n_items, end_lineno, end_col_offset;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004120 asdl_seq *items, *body;
Georg Brandl0c315622009-05-25 21:10:36 +00004121
4122 REQ(n, with_stmt);
4123
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004124 n_items = (NCH(n) - 2) / 2;
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004125 items = _Py_asdl_seq_new(n_items, c->c_arena);
Stefan Krah28a2ad52012-08-20 16:07:38 +02004126 if (!items)
4127 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004128 for (i = 1; i < NCH(n) - 2; i += 2) {
4129 withitem_ty item = ast_for_with_item(c, CHILD(n, i));
4130 if (!item)
Georg Brandl0c315622009-05-25 21:10:36 +00004131 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004132 asdl_seq_SET(items, (i - 1) / 2, item);
Georg Brandl0c315622009-05-25 21:10:36 +00004133 }
4134
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004135 body = ast_for_suite(c, CHILD(n, NCH(n) - 1));
4136 if (!body)
4137 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004138 get_last_end_pos(body, &end_lineno, &end_col_offset);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004139
Yury Selivanov75445082015-05-11 22:57:16 -04004140 if (is_async)
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004141 return AsyncWith(items, body, LINENO(n0), n0->n_col_offset,
4142 end_lineno, end_col_offset, c->c_arena);
Yury Selivanov75445082015-05-11 22:57:16 -04004143 else
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004144 return With(items, body, LINENO(n), n->n_col_offset,
4145 end_lineno, end_col_offset, c->c_arena);
Georg Brandl0c315622009-05-25 21:10:36 +00004146}
4147
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004148static stmt_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004149ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004150{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004151 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */
Benjamin Peterson30760062008-11-25 04:02:28 +00004152 PyObject *classname;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004153 asdl_seq *s;
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004154 expr_ty call;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004155 int end_lineno, end_col_offset;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004156
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004157 REQ(n, classdef);
4158
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004159 if (NCH(n) == 4) { /* class NAME ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004160 s = ast_for_suite(c, CHILD(n, 3));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004161 if (!s)
4162 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004163 get_last_end_pos(s, &end_lineno, &end_col_offset);
4164
Benjamin Peterson30760062008-11-25 04:02:28 +00004165 classname = NEW_IDENTIFIER(CHILD(n, 1));
4166 if (!classname)
4167 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004168 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004169 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004170 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004171 LINENO(n), n->n_col_offset,
4172 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004173 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004174
4175 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004176 s = ast_for_suite(c, CHILD(n, 5));
Thomas Wouters89f507f2006-12-13 04:49:30 +00004177 if (!s)
Benjamin Peterson30760062008-11-25 04:02:28 +00004178 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004179 get_last_end_pos(s, &end_lineno, &end_col_offset);
4180
Benjamin Peterson30760062008-11-25 04:02:28 +00004181 classname = NEW_IDENTIFIER(CHILD(n, 1));
4182 if (!classname)
4183 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004184 if (forbidden_name(c, classname, CHILD(n, 3), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004185 return NULL;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004186 return ClassDef(classname, NULL, NULL, s, decorator_seq,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004187 LINENO(n), n->n_col_offset,
4188 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004189 }
4190
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004191 /* class NAME '(' arglist ')' ':' suite */
4192 /* build up a fake Call node so we can extract its pieces */
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004193 {
4194 PyObject *dummy_name;
4195 expr_ty dummy;
4196 dummy_name = NEW_IDENTIFIER(CHILD(n, 1));
4197 if (!dummy_name)
4198 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004199 dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset,
4200 CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset,
4201 c->c_arena);
4202 call = ast_for_call(c, CHILD(n, 3), dummy, NULL, CHILD(n, 4));
Benjamin Petersond951e7b2008-11-25 22:19:53 +00004203 if (!call)
4204 return NULL;
4205 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03004206 s = ast_for_suite(c, CHILD(n, 6));
Neal Norwitz84456bd2005-12-18 03:16:20 +00004207 if (!s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004208 return NULL;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004209 get_last_end_pos(s, &end_lineno, &end_col_offset);
4210
Benjamin Peterson30760062008-11-25 04:02:28 +00004211 classname = NEW_IDENTIFIER(CHILD(n, 1));
4212 if (!classname)
4213 return NULL;
Benjamin Petersonbd0df502012-09-02 15:04:51 -04004214 if (forbidden_name(c, classname, CHILD(n, 1), 0))
Benjamin Peterson70f52762009-06-28 23:32:44 +00004215 return NULL;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004216
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004217 return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004218 decorator_seq, LINENO(n), n->n_col_offset,
4219 end_lineno, end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004220}
4221
4222static stmt_ty
4223ast_for_stmt(struct compiling *c, const node *n)
4224{
4225 if (TYPE(n) == stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004226 assert(NCH(n) == 1);
4227 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004228 }
4229 if (TYPE(n) == simple_stmt) {
Thomas Wouters89f507f2006-12-13 04:49:30 +00004230 assert(num_stmts(n) == 1);
4231 n = CHILD(n, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004232 }
4233 if (TYPE(n) == small_stmt) {
Neal Norwitzc1505362006-12-28 06:47:50 +00004234 n = CHILD(n, 0);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004235 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt
4236 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004237 */
4238 switch (TYPE(n)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004239 case expr_stmt:
4240 return ast_for_expr_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004241 case del_stmt:
4242 return ast_for_del_stmt(c, n);
4243 case pass_stmt:
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004244 return Pass(LINENO(n), n->n_col_offset,
4245 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004246 case flow_stmt:
4247 return ast_for_flow_stmt(c, n);
4248 case import_stmt:
4249 return ast_for_import_stmt(c, n);
4250 case global_stmt:
4251 return ast_for_global_stmt(c, n);
Jeremy Hylton81e95022007-02-27 06:50:52 +00004252 case nonlocal_stmt:
4253 return ast_for_nonlocal_stmt(c, n);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004254 case assert_stmt:
4255 return ast_for_assert_stmt(c, n);
4256 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004257 PyErr_Format(PyExc_SystemError,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004258 "unhandled small_stmt: TYPE=%d NCH=%d\n",
4259 TYPE(n), NCH(n));
4260 return NULL;
4261 }
4262 }
4263 else {
4264 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt
Yury Selivanov75445082015-05-11 22:57:16 -04004265 | funcdef | classdef | decorated | async_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +00004266 */
4267 node *ch = CHILD(n, 0);
4268 REQ(n, compound_stmt);
4269 switch (TYPE(ch)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004270 case if_stmt:
4271 return ast_for_if_stmt(c, ch);
4272 case while_stmt:
4273 return ast_for_while_stmt(c, ch);
4274 case for_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004275 return ast_for_for_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004276 case try_stmt:
4277 return ast_for_try_stmt(c, ch);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004278 case with_stmt:
Yury Selivanov75445082015-05-11 22:57:16 -04004279 return ast_for_with_stmt(c, ch, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004280 case funcdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004281 return ast_for_funcdef(c, ch, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004282 case classdef:
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004283 return ast_for_classdef(c, ch, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004284 case decorated:
4285 return ast_for_decorated(c, ch);
Yury Selivanov75445082015-05-11 22:57:16 -04004286 case async_stmt:
4287 return ast_for_async_stmt(c, ch);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004288 default:
Neal Norwitz79792652005-11-14 04:25:03 +00004289 PyErr_Format(PyExc_SystemError,
Jelle Zijlstra898ff922018-05-13 17:04:53 -04004290 "unhandled compound_stmt: TYPE=%d NCH=%d\n",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004291 TYPE(n), NCH(n));
4292 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004293 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004294 }
4295}
4296
4297static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004298parsenumber_raw(struct compiling *c, const char *s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004299{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004300 const char *end;
4301 long x;
4302 double dx;
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004303 Py_complex compl;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004304 int imflag;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004305
Mark Dickinsond3c827b2008-12-05 18:10:46 +00004306 assert(s != NULL);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004307 errno = 0;
4308 end = s + strlen(s) - 1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004309 imflag = *end == 'j' || *end == 'J';
Guido van Rossumd8faa362007-04-27 19:54:29 +00004310 if (s[0] == '0') {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004311 x = (long) PyOS_strtoul(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004312 if (x < 0 && errno == 0) {
Serhiy Storchakac6792272013-10-19 21:03:34 +03004313 return PyLong_FromString(s, (char **)0, 0);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004314 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004315 }
4316 else
Serhiy Storchakac6792272013-10-19 21:03:34 +03004317 x = PyOS_strtol(s, (char **)&end, 0);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004318 if (*end == '\0') {
4319 if (errno != 0)
Serhiy Storchakac6792272013-10-19 21:03:34 +03004320 return PyLong_FromString(s, (char **)0, 0);
Christian Heimes217cfd12007-12-02 14:31:20 +00004321 return PyLong_FromLong(x);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004322 }
4323 /* XXX Huge floats may silently fail */
Guido van Rossumd8faa362007-04-27 19:54:29 +00004324 if (imflag) {
Christian Heimes81ee3ef2008-05-04 22:42:01 +00004325 compl.real = 0.;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004326 compl.imag = PyOS_string_to_double(s, (char **)&end, NULL);
4327 if (compl.imag == -1.0 && PyErr_Occurred())
4328 return NULL;
4329 return PyComplex_FromCComplex(compl);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004330 }
4331 else
Guido van Rossumd8faa362007-04-27 19:54:29 +00004332 {
Mark Dickinson725bfd82009-05-03 20:33:40 +00004333 dx = PyOS_string_to_double(s, NULL, NULL);
4334 if (dx == -1.0 && PyErr_Occurred())
4335 return NULL;
4336 return PyFloat_FromDouble(dx);
Guido van Rossumd8faa362007-04-27 19:54:29 +00004337 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004338}
4339
4340static PyObject *
Brett Cannona721aba2016-09-09 14:57:09 -07004341parsenumber(struct compiling *c, const char *s)
4342{
4343 char *dup, *end;
4344 PyObject *res = NULL;
4345
4346 assert(s != NULL);
4347
4348 if (strchr(s, '_') == NULL) {
4349 return parsenumber_raw(c, s);
4350 }
4351 /* Create a duplicate without underscores. */
4352 dup = PyMem_Malloc(strlen(s) + 1);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004353 if (dup == NULL) {
4354 return PyErr_NoMemory();
4355 }
Brett Cannona721aba2016-09-09 14:57:09 -07004356 end = dup;
4357 for (; *s; s++) {
4358 if (*s != '_') {
4359 *end++ = *s;
4360 }
4361 }
4362 *end = '\0';
4363 res = parsenumber_raw(c, dup);
4364 PyMem_Free(dup);
4365 return res;
4366}
4367
4368static PyObject *
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004369decode_utf8(struct compiling *c, const char **sPtr, const char *end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004370{
Serhiy Storchakac6792272013-10-19 21:03:34 +03004371 const char *s, *t;
4372 t = s = *sPtr;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004373 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
4374 while (s < end && (*s & 0x80)) s++;
4375 *sPtr = s;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004376 return PyUnicode_DecodeUTF8(t, s - t, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004377}
4378
Eric V. Smith56466482016-10-31 14:46:26 -04004379static int
4380warn_invalid_escape_sequence(struct compiling *c, const node *n,
Serhiy Storchaka56cb4652017-10-20 17:08:15 +03004381 unsigned char first_invalid_escape_char)
Eric V. Smith56466482016-10-31 14:46:26 -04004382{
4383 PyObject *msg = PyUnicode_FromFormat("invalid escape sequence \\%c",
4384 first_invalid_escape_char);
4385 if (msg == NULL) {
4386 return -1;
4387 }
Serhiy Storchaka65439122018-10-19 17:42:06 +03004388 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
Eric V. Smith56466482016-10-31 14:46:26 -04004389 c->c_filename, LINENO(n),
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004390 NULL, NULL) < 0)
Eric V. Smith56466482016-10-31 14:46:26 -04004391 {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004392 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka65439122018-10-19 17:42:06 +03004393 /* Replace the SyntaxWarning exception with a SyntaxError
Serhiy Storchakaa5618622017-12-01 08:40:23 +02004394 to get a more accurate error report */
4395 PyErr_Clear();
Serhiy Storchaka97f1efb2018-11-20 19:27:16 +02004396 ast_error(c, n, "%U", msg);
Eric V. Smith56466482016-10-31 14:46:26 -04004397 }
4398 Py_DECREF(msg);
4399 return -1;
4400 }
4401 Py_DECREF(msg);
4402 return 0;
4403}
4404
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004405static PyObject *
Eric V. Smith56466482016-10-31 14:46:26 -04004406decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s,
4407 size_t len)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004408{
Guido van Rossumd8faa362007-04-27 19:54:29 +00004409 PyObject *v, *u;
4410 char *buf;
4411 char *p;
4412 const char *end;
Guido van Rossum29fd7122007-11-12 01:13:56 +00004413
Benjamin Peterson202803a2016-02-25 22:34:45 -08004414 /* check for integer overflow */
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07004415 if (len > SIZE_MAX / 6)
Benjamin Peterson202803a2016-02-25 22:34:45 -08004416 return NULL;
4417 /* "Ă€" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
4418 "\Ă€" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
4419 u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
4420 if (u == NULL)
4421 return NULL;
4422 p = buf = PyBytes_AsString(u);
4423 end = s + len;
4424 while (s < end) {
4425 if (*s == '\\') {
4426 *p++ = *s++;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004427 if (s >= end || *s & 0x80) {
Benjamin Peterson202803a2016-02-25 22:34:45 -08004428 strcpy(p, "u005c");
4429 p += 5;
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004430 if (s >= end)
4431 break;
Guido van Rossumd8faa362007-04-27 19:54:29 +00004432 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00004433 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004434 if (*s & 0x80) { /* XXX inefficient */
4435 PyObject *w;
4436 int kind;
4437 void *data;
4438 Py_ssize_t len, i;
4439 w = decode_utf8(c, &s, end);
4440 if (w == NULL) {
4441 Py_DECREF(u);
4442 return NULL;
4443 }
4444 kind = PyUnicode_KIND(w);
4445 data = PyUnicode_DATA(w);
4446 len = PyUnicode_GET_LENGTH(w);
4447 for (i = 0; i < len; i++) {
4448 Py_UCS4 chr = PyUnicode_READ(kind, data, i);
4449 sprintf(p, "\\U%08x", chr);
4450 p += 10;
4451 }
4452 /* Should be impossible to overflow */
Serhiy Storchakafff9a312017-03-21 08:53:25 +02004453 assert(p - buf <= PyBytes_GET_SIZE(u));
Benjamin Peterson202803a2016-02-25 22:34:45 -08004454 Py_DECREF(w);
4455 } else {
4456 *p++ = *s++;
4457 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004458 }
Benjamin Peterson202803a2016-02-25 22:34:45 -08004459 len = p - buf;
4460 s = buf;
4461
Eric V. Smith56466482016-10-31 14:46:26 -04004462 const char *first_invalid_escape;
4463 v = _PyUnicode_DecodeUnicodeEscape(s, len, NULL, &first_invalid_escape);
4464
4465 if (v != NULL && first_invalid_escape != NULL) {
4466 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4467 /* We have not decref u before because first_invalid_escape points
4468 inside u. */
4469 Py_XDECREF(u);
4470 Py_DECREF(v);
4471 return NULL;
4472 }
4473 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00004474 Py_XDECREF(u);
4475 return v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004476}
4477
Eric V. Smith56466482016-10-31 14:46:26 -04004478static PyObject *
4479decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s,
4480 size_t len)
4481{
4482 const char *first_invalid_escape;
4483 PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, 0, NULL,
4484 &first_invalid_escape);
4485 if (result == NULL)
4486 return NULL;
4487
4488 if (first_invalid_escape != NULL) {
4489 if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) {
4490 Py_DECREF(result);
4491 return NULL;
4492 }
4493 }
4494 return result;
4495}
4496
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004497/* Shift locations for the given node and all its children by adding `lineno`
4498 and `col_offset` to existing locations. */
4499static void fstring_shift_node_locations(node *n, int lineno, int col_offset)
4500{
4501 n->n_col_offset = n->n_col_offset + col_offset;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004502 n->n_end_col_offset = n->n_end_col_offset + col_offset;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004503 for (int i = 0; i < NCH(n); ++i) {
4504 if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) {
4505 /* Shifting column offsets unnecessary if there's been newlines. */
4506 col_offset = 0;
4507 }
4508 fstring_shift_node_locations(CHILD(n, i), lineno, col_offset);
4509 }
4510 n->n_lineno = n->n_lineno + lineno;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004511 n->n_end_lineno = n->n_end_lineno + lineno;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004512}
4513
4514/* Fix locations for the given node and its children.
4515
4516 `parent` is the enclosing node.
4517 `n` is the node which locations are going to be fixed relative to parent.
luzpaza5293b42017-11-05 07:37:50 -06004518 `expr_str` is the child node's string representation, including braces.
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004519*/
4520static void
4521fstring_fix_node_location(const node *parent, node *n, char *expr_str)
4522{
4523 char *substr = NULL;
4524 char *start;
4525 int lines = LINENO(parent) - 1;
4526 int cols = parent->n_col_offset;
4527 /* Find the full fstring to fix location information in `n`. */
4528 while (parent && parent->n_type != STRING)
4529 parent = parent->n_child;
4530 if (parent && parent->n_str) {
4531 substr = strstr(parent->n_str, expr_str);
4532 if (substr) {
4533 start = substr;
4534 while (start > parent->n_str) {
4535 if (start[0] == '\n')
4536 break;
4537 start--;
4538 }
Victor Stinnerfb7e7992018-04-30 23:51:02 +02004539 cols += (int)(substr - start);
Anthony Sottile995d9b92019-01-12 20:05:13 -08004540 /* adjust the start based on the number of newlines encountered
4541 before the f-string expression */
4542 for (char* p = parent->n_str; p < substr; p++) {
4543 if (*p == '\n') {
4544 lines++;
4545 }
4546 }
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004547 }
4548 }
4549 fstring_shift_node_locations(n, lines, cols);
4550}
4551
Eric V. Smith451d0e32016-09-09 21:56:20 -04004552/* Compile this expression in to an expr_ty. Add parens around the
4553 expression, in order to allow leading spaces in the expression. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004554static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004555fstring_compile_expr(const char *expr_start, const char *expr_end,
4556 struct compiling *c, const node *n)
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004557
Eric V. Smith235a6f02015-09-19 14:51:32 -04004558{
4559 PyCompilerFlags cf;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004560 node *mod_n;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004561 mod_ty mod;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004562 char *str;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004563 Py_ssize_t len;
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004564 const char *s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004565
Eric V. Smith1d44c412015-09-23 07:49:00 -04004566 assert(expr_end >= expr_start);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004567 assert(*(expr_start-1) == '{');
4568 assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':');
Eric V. Smith1d44c412015-09-23 07:49:00 -04004569
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004570 /* If the substring is all whitespace, it's an error. We need to catch this
4571 here, and not when we call PyParser_SimpleParseStringFlagsFilename,
4572 because turning the expression '' in to '()' would go from being invalid
4573 to valid. */
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004574 for (s = expr_start; s != expr_end; s++) {
4575 char c = *s;
4576 /* The Python parser ignores only the following whitespace
4577 characters (\r already is converted to \n). */
4578 if (!(c == ' ' || c == '\t' || c == '\n' || c == '\f')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004579 break;
4580 }
4581 }
Serhiy Storchaka2e9cd582017-06-08 23:43:54 +03004582 if (s == expr_end) {
Eric V. Smith1e5fcc32015-09-24 08:52:04 -04004583 ast_error(c, n, "f-string: empty expression not allowed");
Eric V. Smith451d0e32016-09-09 21:56:20 -04004584 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004585 }
4586
Eric V. Smith451d0e32016-09-09 21:56:20 -04004587 len = expr_end - expr_start;
4588 /* Allocate 3 extra bytes: open paren, close paren, null byte. */
4589 str = PyMem_RawMalloc(len + 3);
Zackery Spytz4c49da02018-12-07 03:11:30 -07004590 if (str == NULL) {
4591 PyErr_NoMemory();
Eric V. Smith451d0e32016-09-09 21:56:20 -04004592 return NULL;
Zackery Spytz4c49da02018-12-07 03:11:30 -07004593 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004594
Eric V. Smith451d0e32016-09-09 21:56:20 -04004595 str[0] = '(';
4596 memcpy(str+1, expr_start, len);
4597 str[len+1] = ')';
4598 str[len+2] = 0;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004599
4600 cf.cf_flags = PyCF_ONLY_AST;
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004601 mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>",
4602 Py_eval_input, 0);
4603 if (!mod_n) {
4604 PyMem_RawFree(str);
4605 return NULL;
4606 }
4607 /* Reuse str to find the correct column offset. */
4608 str[0] = '{';
4609 str[len+1] = '}';
4610 fstring_fix_node_location(n, mod_n, str);
4611 mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004612 PyMem_RawFree(str);
Ɓukasz Langae7c566c2017-09-06 17:27:58 -07004613 PyNode_Free(mod_n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004614 if (!mod)
Eric V. Smith451d0e32016-09-09 21:56:20 -04004615 return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004616 return mod->v.Expression.body;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004617}
4618
4619/* Return -1 on error.
4620
4621 Return 0 if we reached the end of the literal.
4622
4623 Return 1 if we haven't reached the end of the literal, but we want
4624 the caller to process the literal up to this point. Used for
4625 doubled braces.
4626*/
4627static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004628fstring_find_literal(const char **str, const char *end, int raw,
4629 PyObject **literal, int recurse_lvl,
4630 struct compiling *c, const node *n)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004631{
Eric V. Smith451d0e32016-09-09 21:56:20 -04004632 /* Get any literal string. It ends when we hit an un-doubled left
4633 brace (which isn't part of a unicode name escape such as
4634 "\N{EULER CONSTANT}"), or the end of the string. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004635
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004636 const char *s = *str;
4637 const char *literal_start = s;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004638 int result = 0;
4639
Eric V. Smith235a6f02015-09-19 14:51:32 -04004640 assert(*literal == NULL);
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004641 while (s < end) {
4642 char ch = *s++;
4643 if (!raw && ch == '\\' && s < end) {
4644 ch = *s++;
4645 if (ch == 'N') {
4646 if (s < end && *s++ == '{') {
4647 while (s < end && *s++ != '}') {
4648 }
4649 continue;
4650 }
4651 break;
4652 }
4653 if (ch == '{' && warn_invalid_escape_sequence(c, n, ch) < 0) {
4654 return -1;
4655 }
4656 }
4657 if (ch == '{' || ch == '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004658 /* Check for doubled braces, but only at the top level. If
4659 we checked at every level, then f'{0:{3}}' would fail
4660 with the two closing braces. */
4661 if (recurse_lvl == 0) {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004662 if (s < end && *s == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004663 /* We're going to tell the caller that the literal ends
4664 here, but that they should continue scanning. But also
4665 skip over the second brace when we resume scanning. */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004666 *str = s + 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004667 result = 1;
4668 goto done;
4669 }
4670
4671 /* Where a single '{' is the start of a new expression, a
4672 single '}' is not allowed. */
4673 if (ch == '}') {
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004674 *str = s - 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004675 ast_error(c, n, "f-string: single '}' is not allowed");
4676 return -1;
4677 }
4678 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004679 /* We're either at a '{', which means we're starting another
4680 expression; or a '}', which means we're at the end of this
4681 f-string (for a nested format_spec). */
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004682 s--;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004683 break;
4684 }
4685 }
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004686 *str = s;
4687 assert(s <= end);
4688 assert(s == end || *s == '{' || *s == '}');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004689done:
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004690 if (literal_start != s) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004691 if (raw)
4692 *literal = PyUnicode_DecodeUTF8Stateful(literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004693 s - literal_start,
Eric V. Smith451d0e32016-09-09 21:56:20 -04004694 NULL, NULL);
4695 else
Eric V. Smith56466482016-10-31 14:46:26 -04004696 *literal = decode_unicode_with_escapes(c, n, literal_start,
Serhiy Storchaka0cd7a3f2017-05-25 13:33:55 +03004697 s - literal_start);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004698 if (!*literal)
4699 return -1;
4700 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004701 return result;
4702}
4703
4704/* Forward declaration because parsing is recursive. */
4705static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04004706fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004707 struct compiling *c, const node *n);
4708
Eric V. Smith451d0e32016-09-09 21:56:20 -04004709/* Parse the f-string at *str, ending at end. We know *str starts an
Eric V. Smith235a6f02015-09-19 14:51:32 -04004710 expression (so it must be a '{'). Returns the FormattedValue node,
4711 which includes the expression, conversion character, and
4712 format_spec expression.
4713
4714 Note that I don't do a perfect job here: I don't make sure that a
4715 closing brace doesn't match an opening paren, for example. It
4716 doesn't need to error on all invalid expressions, just correctly
4717 find the end of all valid ones. Any errors inside the expression
4718 will be caught when we parse it later. */
4719static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004720fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004721 expr_ty *expression, struct compiling *c, const node *n)
4722{
4723 /* Return -1 on error, else 0. */
4724
Eric V. Smith451d0e32016-09-09 21:56:20 -04004725 const char *expr_start;
4726 const char *expr_end;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004727 expr_ty simple_expression;
4728 expr_ty format_spec = NULL; /* Optional format specifier. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004729 int conversion = -1; /* The conversion char. -1 if not specified. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004730
4731 /* 0 if we're not in a string, else the quote char we're trying to
4732 match (single or double quote). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004733 char quote_char = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004734
4735 /* If we're inside a string, 1=normal, 3=triple-quoted. */
4736 int string_type = 0;
4737
4738 /* Keep track of nesting level for braces/parens/brackets in
4739 expressions. */
4740 Py_ssize_t nested_depth = 0;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004741 char parenstack[MAXLEVEL];
Eric V. Smith235a6f02015-09-19 14:51:32 -04004742
4743 /* Can only nest one level deep. */
4744 if (recurse_lvl >= 2) {
4745 ast_error(c, n, "f-string: expressions nested too deeply");
4746 return -1;
4747 }
4748
4749 /* The first char must be a left brace, or we wouldn't have gotten
4750 here. Skip over it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004751 assert(**str == '{');
4752 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004753
Eric V. Smith451d0e32016-09-09 21:56:20 -04004754 expr_start = *str;
4755 for (; *str < end; (*str)++) {
4756 char ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004757
4758 /* Loop invariants. */
4759 assert(nested_depth >= 0);
Eric V. Smith451d0e32016-09-09 21:56:20 -04004760 assert(*str >= expr_start && *str < end);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004761 if (quote_char)
4762 assert(string_type == 1 || string_type == 3);
4763 else
4764 assert(string_type == 0);
4765
Eric V. Smith451d0e32016-09-09 21:56:20 -04004766 ch = **str;
4767 /* Nowhere inside an expression is a backslash allowed. */
4768 if (ch == '\\') {
4769 /* Error: can't include a backslash character, inside
4770 parens or strings or not. */
4771 ast_error(c, n, "f-string expression part "
4772 "cannot include a backslash");
4773 return -1;
4774 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004775 if (quote_char) {
4776 /* We're inside a string. See if we're at the end. */
4777 /* This code needs to implement the same non-error logic
4778 as tok_get from tokenizer.c, at the letter_quote
4779 label. To actually share that code would be a
4780 nightmare. But, it's unlikely to change and is small,
4781 so duplicate it here. Note we don't need to catch all
4782 of the errors, since they'll be caught when parsing the
4783 expression. We just need to match the non-error
4784 cases. Thus we can ignore \n in single-quoted strings,
4785 for example. Or non-terminated strings. */
4786 if (ch == quote_char) {
4787 /* Does this match the string_type (single or triple
4788 quoted)? */
4789 if (string_type == 3) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04004790 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004791 /* We're at the end of a triple quoted string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004792 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004793 string_type = 0;
4794 quote_char = 0;
4795 continue;
4796 }
4797 } else {
4798 /* We're at the end of a normal string. */
4799 quote_char = 0;
4800 string_type = 0;
4801 continue;
4802 }
4803 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004804 } else if (ch == '\'' || ch == '"') {
4805 /* Is this a triple quoted string? */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004806 if (*str+2 < end && *(*str+1) == ch && *(*str+2) == ch) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004807 string_type = 3;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004808 *str += 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004809 } else {
4810 /* Start of a normal string. */
4811 string_type = 1;
4812 }
4813 /* Start looking for the end of the string. */
4814 quote_char = ch;
4815 } else if (ch == '[' || ch == '{' || ch == '(') {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004816 if (nested_depth >= MAXLEVEL) {
4817 ast_error(c, n, "f-string: too many nested parenthesis");
4818 return -1;
4819 }
4820 parenstack[nested_depth] = ch;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004821 nested_depth++;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004822 } else if (ch == '#') {
4823 /* Error: can't include a comment character, inside parens
4824 or not. */
Eric V. Smith09835dc2016-09-11 18:58:20 -04004825 ast_error(c, n, "f-string expression part cannot include '#'");
Eric V. Smith235a6f02015-09-19 14:51:32 -04004826 return -1;
4827 } else if (nested_depth == 0 &&
4828 (ch == '!' || ch == ':' || ch == '}')) {
4829 /* First, test for the special case of "!=". Since '=' is
4830 not an allowed conversion character, nothing is lost in
4831 this test. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004832 if (ch == '!' && *str+1 < end && *(*str+1) == '=') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04004833 /* This isn't a conversion character, just continue. */
4834 continue;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004835 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004836 /* Normal way out of this loop. */
4837 break;
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004838 } else if (ch == ']' || ch == '}' || ch == ')') {
4839 if (!nested_depth) {
4840 ast_error(c, n, "f-string: unmatched '%c'", ch);
4841 return -1;
4842 }
4843 nested_depth--;
4844 int opening = parenstack[nested_depth];
4845 if (!((opening == '(' && ch == ')') ||
4846 (opening == '[' && ch == ']') ||
4847 (opening == '{' && ch == '}')))
4848 {
4849 ast_error(c, n,
4850 "f-string: closing parenthesis '%c' "
4851 "does not match opening parenthesis '%c'",
4852 ch, opening);
4853 return -1;
4854 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004855 } else {
4856 /* Just consume this char and loop around. */
4857 }
4858 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04004859 expr_end = *str;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004860 /* If we leave this loop in a string or with mismatched parens, we
4861 don't care. We'll get a syntax error when compiling the
4862 expression. But, we can produce a better error message, so
4863 let's just do that.*/
4864 if (quote_char) {
4865 ast_error(c, n, "f-string: unterminated string");
4866 return -1;
4867 }
4868 if (nested_depth) {
Serhiy Storchaka58159ef2019-01-12 09:46:50 +02004869 int opening = parenstack[nested_depth - 1];
4870 ast_error(c, n, "f-string: unmatched '%c'", opening);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004871 return -1;
4872 }
4873
Eric V. Smith451d0e32016-09-09 21:56:20 -04004874 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004875 goto unexpected_end_of_string;
Eric V. Smith1d44c412015-09-23 07:49:00 -04004876
4877 /* Compile the expression as soon as possible, so we show errors
4878 related to the expression before errors related to the
4879 conversion or format_spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004880 simple_expression = fstring_compile_expr(expr_start, expr_end, c, n);
Eric V. Smith1d44c412015-09-23 07:49:00 -04004881 if (!simple_expression)
4882 return -1;
4883
4884 /* Check for a conversion char, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004885 if (**str == '!') {
4886 *str += 1;
4887 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004888 goto unexpected_end_of_string;
4889
Eric V. Smith451d0e32016-09-09 21:56:20 -04004890 conversion = **str;
4891 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004892
4893 /* Validate the conversion. */
4894 if (!(conversion == 's' || conversion == 'r'
4895 || conversion == 'a')) {
4896 ast_error(c, n, "f-string: invalid conversion character: "
4897 "expected 's', 'r', or 'a'");
4898 return -1;
4899 }
4900 }
4901
4902 /* Check for the format spec, if present. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004903 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004904 goto unexpected_end_of_string;
Eric V. Smith451d0e32016-09-09 21:56:20 -04004905 if (**str == ':') {
4906 *str += 1;
4907 if (*str >= end)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004908 goto unexpected_end_of_string;
4909
4910 /* Parse the format spec. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004911 format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004912 if (!format_spec)
4913 return -1;
4914 }
4915
Eric V. Smith451d0e32016-09-09 21:56:20 -04004916 if (*str >= end || **str != '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004917 goto unexpected_end_of_string;
4918
4919 /* We're at a right brace. Consume it. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004920 assert(*str < end);
4921 assert(**str == '}');
4922 *str += 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004923
Eric V. Smith451d0e32016-09-09 21:56:20 -04004924 /* And now create the FormattedValue node that represents this
4925 entire expression with the conversion and format spec. */
Benjamin Peterson4ba5c882016-09-09 19:31:12 -07004926 *expression = FormattedValue(simple_expression, conversion,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004927 format_spec, LINENO(n), n->n_col_offset,
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00004928 n->n_end_lineno, n->n_end_col_offset,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004929 c->c_arena);
4930 if (!*expression)
4931 return -1;
4932
4933 return 0;
4934
4935unexpected_end_of_string:
4936 ast_error(c, n, "f-string: expecting '}'");
4937 return -1;
4938}
4939
4940/* Return -1 on error.
4941
4942 Return 0 if we have a literal (possible zero length) and an
4943 expression (zero length if at the end of the string.
4944
4945 Return 1 if we have a literal, but no expression, and we want the
4946 caller to call us again. This is used to deal with doubled
4947 braces.
4948
4949 When called multiple times on the string 'a{{b{0}c', this function
4950 will return:
4951
4952 1. the literal 'a{' with no expression, and a return value
4953 of 1. Despite the fact that there's no expression, the return
4954 value of 1 means we're not finished yet.
4955
4956 2. the literal 'b' and the expression '0', with a return value of
4957 0. The fact that there's an expression means we're not finished.
4958
4959 3. literal 'c' with no expression and a return value of 0. The
4960 combination of the return value of 0 with no expression means
4961 we're finished.
4962*/
4963static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04004964fstring_find_literal_and_expr(const char **str, const char *end, int raw,
4965 int recurse_lvl, PyObject **literal,
4966 expr_ty *expression,
Eric V. Smith235a6f02015-09-19 14:51:32 -04004967 struct compiling *c, const node *n)
4968{
4969 int result;
4970
4971 assert(*literal == NULL && *expression == NULL);
4972
4973 /* Get any literal string. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004974 result = fstring_find_literal(str, end, raw, literal, recurse_lvl, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04004975 if (result < 0)
4976 goto error;
4977
4978 assert(result == 0 || result == 1);
4979
4980 if (result == 1)
4981 /* We have a literal, but don't look at the expression. */
4982 return 1;
4983
Eric V. Smith451d0e32016-09-09 21:56:20 -04004984 if (*str >= end || **str == '}')
Eric V. Smith235a6f02015-09-19 14:51:32 -04004985 /* We're at the end of the string or the end of a nested
4986 f-string: no expression. The top-level error case where we
4987 expect to be at the end of the string but we're at a '}' is
4988 handled later. */
4989 return 0;
4990
4991 /* We must now be the start of an expression, on a '{'. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04004992 assert(**str == '{');
Eric V. Smith235a6f02015-09-19 14:51:32 -04004993
Eric V. Smith451d0e32016-09-09 21:56:20 -04004994 if (fstring_find_expr(str, end, raw, recurse_lvl, expression, c, n) < 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04004995 goto error;
4996
4997 return 0;
4998
4999error:
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005000 Py_CLEAR(*literal);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005001 return -1;
5002}
5003
5004#define EXPRLIST_N_CACHED 64
5005
5006typedef struct {
5007 /* Incrementally build an array of expr_ty, so be used in an
5008 asdl_seq. Cache some small but reasonably sized number of
5009 expr_ty's, and then after that start dynamically allocating,
5010 doubling the number allocated each time. Note that the f-string
5011 f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005012 Constant for the literal 'a'. So you add expr_ty's about twice as
Eric V. Smith235a6f02015-09-19 14:51:32 -04005013 fast as you add exressions in an f-string. */
5014
5015 Py_ssize_t allocated; /* Number we've allocated. */
5016 Py_ssize_t size; /* Number we've used. */
5017 expr_ty *p; /* Pointer to the memory we're actually
5018 using. Will point to 'data' until we
5019 start dynamically allocating. */
5020 expr_ty data[EXPRLIST_N_CACHED];
5021} ExprList;
5022
5023#ifdef NDEBUG
5024#define ExprList_check_invariants(l)
5025#else
5026static void
5027ExprList_check_invariants(ExprList *l)
5028{
5029 /* Check our invariants. Make sure this object is "live", and
5030 hasn't been deallocated. */
5031 assert(l->size >= 0);
5032 assert(l->p != NULL);
5033 if (l->size <= EXPRLIST_N_CACHED)
5034 assert(l->data == l->p);
5035}
5036#endif
5037
5038static void
5039ExprList_Init(ExprList *l)
5040{
5041 l->allocated = EXPRLIST_N_CACHED;
5042 l->size = 0;
5043
5044 /* Until we start allocating dynamically, p points to data. */
5045 l->p = l->data;
5046
5047 ExprList_check_invariants(l);
5048}
5049
5050static int
5051ExprList_Append(ExprList *l, expr_ty exp)
5052{
5053 ExprList_check_invariants(l);
5054 if (l->size >= l->allocated) {
5055 /* We need to alloc (or realloc) the memory. */
5056 Py_ssize_t new_size = l->allocated * 2;
5057
5058 /* See if we've ever allocated anything dynamically. */
5059 if (l->p == l->data) {
5060 Py_ssize_t i;
5061 /* We're still using the cached data. Switch to
5062 alloc-ing. */
5063 l->p = PyMem_RawMalloc(sizeof(expr_ty) * new_size);
5064 if (!l->p)
5065 return -1;
5066 /* Copy the cached data into the new buffer. */
5067 for (i = 0; i < l->size; i++)
5068 l->p[i] = l->data[i];
5069 } else {
5070 /* Just realloc. */
5071 expr_ty *tmp = PyMem_RawRealloc(l->p, sizeof(expr_ty) * new_size);
5072 if (!tmp) {
5073 PyMem_RawFree(l->p);
5074 l->p = NULL;
5075 return -1;
5076 }
5077 l->p = tmp;
5078 }
5079
5080 l->allocated = new_size;
5081 assert(l->allocated == 2 * l->size);
5082 }
5083
5084 l->p[l->size++] = exp;
5085
5086 ExprList_check_invariants(l);
5087 return 0;
5088}
5089
5090static void
5091ExprList_Dealloc(ExprList *l)
5092{
5093 ExprList_check_invariants(l);
5094
5095 /* If there's been an error, or we've never dynamically allocated,
5096 do nothing. */
5097 if (!l->p || l->p == l->data) {
5098 /* Do nothing. */
5099 } else {
5100 /* We have dynamically allocated. Free the memory. */
5101 PyMem_RawFree(l->p);
5102 }
5103 l->p = NULL;
5104 l->size = -1;
5105}
5106
5107static asdl_seq *
5108ExprList_Finish(ExprList *l, PyArena *arena)
5109{
5110 asdl_seq *seq;
5111
5112 ExprList_check_invariants(l);
5113
5114 /* Allocate the asdl_seq and copy the expressions in to it. */
5115 seq = _Py_asdl_seq_new(l->size, arena);
5116 if (seq) {
5117 Py_ssize_t i;
5118 for (i = 0; i < l->size; i++)
5119 asdl_seq_SET(seq, i, l->p[i]);
5120 }
5121 ExprList_Dealloc(l);
5122 return seq;
5123}
5124
5125/* The FstringParser is designed to add a mix of strings and
5126 f-strings, and concat them together as needed. Ultimately, it
5127 generates an expr_ty. */
5128typedef struct {
5129 PyObject *last_str;
5130 ExprList expr_list;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005131 int fmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005132} FstringParser;
5133
5134#ifdef NDEBUG
5135#define FstringParser_check_invariants(state)
5136#else
5137static void
5138FstringParser_check_invariants(FstringParser *state)
5139{
5140 if (state->last_str)
5141 assert(PyUnicode_CheckExact(state->last_str));
5142 ExprList_check_invariants(&state->expr_list);
5143}
5144#endif
5145
5146static void
5147FstringParser_Init(FstringParser *state)
5148{
5149 state->last_str = NULL;
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005150 state->fmode = 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005151 ExprList_Init(&state->expr_list);
5152 FstringParser_check_invariants(state);
5153}
5154
5155static void
5156FstringParser_Dealloc(FstringParser *state)
5157{
5158 FstringParser_check_invariants(state);
5159
5160 Py_XDECREF(state->last_str);
5161 ExprList_Dealloc(&state->expr_list);
5162}
5163
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005164/* Make a Constant node, but decref the PyUnicode object being added. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005165static expr_ty
5166make_str_node_and_del(PyObject **str, struct compiling *c, const node* n)
5167{
5168 PyObject *s = *str;
5169 *str = NULL;
5170 assert(PyUnicode_CheckExact(s));
5171 if (PyArena_AddPyObject(c->c_arena, s) < 0) {
5172 Py_DECREF(s);
5173 return NULL;
5174 }
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005175 return Constant(s, LINENO(n), n->n_col_offset,
5176 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005177}
5178
5179/* Add a non-f-string (that is, a regular literal string). str is
5180 decref'd. */
5181static int
5182FstringParser_ConcatAndDel(FstringParser *state, PyObject *str)
5183{
5184 FstringParser_check_invariants(state);
5185
5186 assert(PyUnicode_CheckExact(str));
5187
5188 if (PyUnicode_GET_LENGTH(str) == 0) {
5189 Py_DECREF(str);
5190 return 0;
5191 }
5192
5193 if (!state->last_str) {
5194 /* We didn't have a string before, so just remember this one. */
5195 state->last_str = str;
5196 } else {
5197 /* Concatenate this with the previous string. */
Serhiy Storchaka726fc132015-12-27 15:44:33 +02005198 PyUnicode_AppendAndDel(&state->last_str, str);
5199 if (!state->last_str)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005200 return -1;
5201 }
5202 FstringParser_check_invariants(state);
5203 return 0;
5204}
5205
Eric V. Smith451d0e32016-09-09 21:56:20 -04005206/* Parse an f-string. The f-string is in *str to end, with no
5207 'f' or quotes. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005208static int
Eric V. Smith451d0e32016-09-09 21:56:20 -04005209FstringParser_ConcatFstring(FstringParser *state, const char **str,
5210 const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005211 struct compiling *c, const node *n)
5212{
5213 FstringParser_check_invariants(state);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005214 state->fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005215
5216 /* Parse the f-string. */
5217 while (1) {
5218 PyObject *literal = NULL;
5219 expr_ty expression = NULL;
5220
5221 /* If there's a zero length literal in front of the
5222 expression, literal will be NULL. If we're at the end of
5223 the f-string, expression will be NULL (unless result == 1,
5224 see below). */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005225 int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005226 &literal, &expression,
5227 c, n);
5228 if (result < 0)
5229 return -1;
5230
5231 /* Add the literal, if any. */
5232 if (!literal) {
5233 /* Do nothing. Just leave last_str alone (and possibly
5234 NULL). */
5235 } else if (!state->last_str) {
ericvsmith11e97f22017-06-16 06:19:32 -04005236 /* Note that the literal can be zero length, if the
5237 input string is "\\\n" or "\\\r", among others. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005238 state->last_str = literal;
5239 literal = NULL;
5240 } else {
5241 /* We have a literal, concatenate it. */
5242 assert(PyUnicode_GET_LENGTH(literal) != 0);
5243 if (FstringParser_ConcatAndDel(state, literal) < 0)
5244 return -1;
5245 literal = NULL;
5246 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005247
5248 /* We've dealt with the literal now. It can't be leaked on further
5249 errors. */
5250 assert(literal == NULL);
5251
5252 /* See if we should just loop around to get the next literal
5253 and expression, while ignoring the expression this
5254 time. This is used for un-doubling braces, as an
5255 optimization. */
5256 if (result == 1)
5257 continue;
5258
5259 if (!expression)
5260 /* We're done with this f-string. */
5261 break;
5262
5263 /* We know we have an expression. Convert any existing string
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005264 to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005265 if (!state->last_str) {
5266 /* Do nothing. No previous literal. */
5267 } else {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005268 /* Convert the existing last_str literal to a Constant node. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005269 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5270 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5271 return -1;
5272 }
5273
5274 if (ExprList_Append(&state->expr_list, expression) < 0)
5275 return -1;
5276 }
5277
Eric V. Smith235a6f02015-09-19 14:51:32 -04005278 /* If recurse_lvl is zero, then we must be at the end of the
5279 string. Otherwise, we must be at a right brace. */
5280
Eric V. Smith451d0e32016-09-09 21:56:20 -04005281 if (recurse_lvl == 0 && *str < end-1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005282 ast_error(c, n, "f-string: unexpected end of string");
5283 return -1;
5284 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005285 if (recurse_lvl != 0 && **str != '}') {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005286 ast_error(c, n, "f-string: expecting '}'");
5287 return -1;
5288 }
5289
5290 FstringParser_check_invariants(state);
5291 return 0;
5292}
5293
5294/* Convert the partial state reflected in last_str and expr_list to an
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005295 expr_ty. The expr_ty can be a Constant, or a JoinedStr. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005296static expr_ty
5297FstringParser_Finish(FstringParser *state, struct compiling *c,
5298 const node *n)
5299{
5300 asdl_seq *seq;
5301
5302 FstringParser_check_invariants(state);
5303
5304 /* If we're just a constant string with no expressions, return
5305 that. */
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02005306 if (!state->fmode) {
5307 assert(!state->expr_list.size);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005308 if (!state->last_str) {
5309 /* Create a zero length string. */
5310 state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
5311 if (!state->last_str)
5312 goto error;
5313 }
5314 return make_str_node_and_del(&state->last_str, c, n);
5315 }
5316
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005317 /* Create a Constant node out of last_str, if needed. It will be the
Eric V. Smith235a6f02015-09-19 14:51:32 -04005318 last node in our expression list. */
5319 if (state->last_str) {
5320 expr_ty str = make_str_node_and_del(&state->last_str, c, n);
5321 if (!str || ExprList_Append(&state->expr_list, str) < 0)
5322 goto error;
5323 }
5324 /* This has already been freed. */
5325 assert(state->last_str == NULL);
5326
5327 seq = ExprList_Finish(&state->expr_list, c->c_arena);
5328 if (!seq)
5329 goto error;
5330
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005331 return JoinedStr(seq, LINENO(n), n->n_col_offset,
5332 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005333
5334error:
5335 FstringParser_Dealloc(state);
5336 return NULL;
5337}
5338
Eric V. Smith451d0e32016-09-09 21:56:20 -04005339/* Given an f-string (with no 'f' or quotes) that's in *str and ends
5340 at end, parse it into an expr_ty. Return NULL on error. Adjust
5341 str to point past the parsed portion. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005342static expr_ty
Eric V. Smith451d0e32016-09-09 21:56:20 -04005343fstring_parse(const char **str, const char *end, int raw, int recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005344 struct compiling *c, const node *n)
5345{
5346 FstringParser state;
5347
5348 FstringParser_Init(&state);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005349 if (FstringParser_ConcatFstring(&state, str, end, raw, recurse_lvl,
Eric V. Smith235a6f02015-09-19 14:51:32 -04005350 c, n) < 0) {
5351 FstringParser_Dealloc(&state);
5352 return NULL;
5353 }
5354
5355 return FstringParser_Finish(&state, c, n);
5356}
5357
5358/* n is a Python string literal, including the bracketing quote
5359 characters, and r, b, u, &/or f prefixes (if any), and embedded
Eric V. Smith451d0e32016-09-09 21:56:20 -04005360 escape sequences (if any). parsestr parses it, and sets *result to
Eric V. Smith235a6f02015-09-19 14:51:32 -04005361 decoded Python string object. If the string is an f-string, set
Eric V. Smith451d0e32016-09-09 21:56:20 -04005362 *fstr and *fstrlen to the unparsed string object. Return 0 if no
5363 errors occurred.
Eric V. Smith235a6f02015-09-19 14:51:32 -04005364*/
Eric V. Smith451d0e32016-09-09 21:56:20 -04005365static int
5366parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode,
5367 PyObject **result, const char **fstr, Py_ssize_t *fstrlen)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005368{
Guido van Rossumd8faa362007-04-27 19:54:29 +00005369 size_t len;
5370 const char *s = STR(n);
5371 int quote = Py_CHARMASK(*s);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005372 int fmode = 0;
5373 *bytesmode = 0;
5374 *rawmode = 0;
5375 *result = NULL;
5376 *fstr = NULL;
Antoine Pitrou4de74572013-02-09 23:11:27 +01005377 if (Py_ISALPHA(quote)) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005378 while (!*bytesmode || !*rawmode) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005379 if (quote == 'b' || quote == 'B') {
5380 quote = *++s;
5381 *bytesmode = 1;
5382 }
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00005383 else if (quote == 'u' || quote == 'U') {
5384 quote = *++s;
5385 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005386 else if (quote == 'r' || quote == 'R') {
5387 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005388 *rawmode = 1;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005389 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005390 else if (quote == 'f' || quote == 'F') {
5391 quote = *++s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005392 fmode = 1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005393 }
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01005394 else {
5395 break;
5396 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005397 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005398 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005399 if (fmode && *bytesmode) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005400 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005401 return -1;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005402 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005403 if (quote != '\'' && quote != '\"') {
5404 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005405 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005406 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005407 /* Skip the leading quote char. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005408 s++;
5409 len = strlen(s);
5410 if (len > INT_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005411 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumd8faa362007-04-27 19:54:29 +00005412 "string to parse is too long");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005413 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005414 }
5415 if (s[--len] != quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005416 /* Last quote char must match the first. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005417 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005418 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005419 }
5420 if (len >= 4 && s[0] == quote && s[1] == quote) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04005421 /* A triple quoted string. We've already skipped one quote at
5422 the start and one at the end of the string. Now skip the
5423 two at the start. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005424 s += 2;
5425 len -= 2;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005426 /* And check that the last two match. */
Guido van Rossumd8faa362007-04-27 19:54:29 +00005427 if (s[--len] != quote || s[--len] != quote) {
5428 PyErr_BadInternalCall();
Eric V. Smith451d0e32016-09-09 21:56:20 -04005429 return -1;
Thomas Wouters00e41de2007-02-23 19:56:57 +00005430 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005431 }
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005432
Eric V. Smith451d0e32016-09-09 21:56:20 -04005433 if (fmode) {
5434 /* Just return the bytes. The caller will parse the resulting
5435 string. */
5436 *fstr = s;
5437 *fstrlen = len;
5438 return 0;
Eric V. Smith6a4efce2016-09-03 09:18:34 -04005439 }
5440
Eric V. Smith451d0e32016-09-09 21:56:20 -04005441 /* Not an f-string. */
Benjamin Peterson768921c2016-02-25 23:13:53 -08005442 /* Avoid invoking escape decoding routines if possible. */
Eric V. Smith451d0e32016-09-09 21:56:20 -04005443 *rawmode = *rawmode || strchr(s, '\\') == NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005444 if (*bytesmode) {
Benjamin Peterson768921c2016-02-25 23:13:53 -08005445 /* Disallow non-ASCII characters. */
Benjamin Petersonbd0df502012-09-02 15:04:51 -04005446 const char *ch;
5447 for (ch = s; *ch; ch++) {
5448 if (Py_CHARMASK(*ch) >= 0x80) {
5449 ast_error(c, n, "bytes can only contain ASCII "
Guido van Rossumd8faa362007-04-27 19:54:29 +00005450 "literal characters.");
Eric V. Smith451d0e32016-09-09 21:56:20 -04005451 return -1;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005452 }
Thomas Wouters00e41de2007-02-23 19:56:57 +00005453 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005454 if (*rawmode)
5455 *result = PyBytes_FromStringAndSize(s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005456 else
Eric V. Smith56466482016-10-31 14:46:26 -04005457 *result = decode_bytes_with_escapes(c, n, s, len);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005458 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005459 if (*rawmode)
5460 *result = PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
Benjamin Peterson768921c2016-02-25 23:13:53 -08005461 else
Eric V. Smith56466482016-10-31 14:46:26 -04005462 *result = decode_unicode_with_escapes(c, n, s, len);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005463 }
Eric V. Smith451d0e32016-09-09 21:56:20 -04005464 return *result == NULL ? -1 : 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005465}
5466
Eric V. Smith235a6f02015-09-19 14:51:32 -04005467/* Accepts a STRING+ atom, and produces an expr_ty node. Run through
5468 each STRING atom, and process it as needed. For bytes, just
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005469 concatenate them together, and the result will be a Constant node. For
Eric V. Smith235a6f02015-09-19 14:51:32 -04005470 normal strings and f-strings, concatenate them together. The result
Serhiy Storchaka3f228112018-09-27 17:42:37 +03005471 will be a Constant node if there were no f-strings; a FormattedValue
Eric V. Smith235a6f02015-09-19 14:51:32 -04005472 node if there's just an f-string (with no leading or trailing
5473 literals), or a JoinedStr node if there are multiple f-strings or
5474 any literals involved. */
5475static expr_ty
5476parsestrplus(struct compiling *c, const node *n)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005477{
Eric V. Smith235a6f02015-09-19 14:51:32 -04005478 int bytesmode = 0;
5479 PyObject *bytes_str = NULL;
Guido van Rossumd8faa362007-04-27 19:54:29 +00005480 int i;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005481
5482 FstringParser state;
5483 FstringParser_Init(&state);
5484
5485 for (i = 0; i < NCH(n); i++) {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005486 int this_bytesmode;
5487 int this_rawmode;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005488 PyObject *s;
Eric V. Smith451d0e32016-09-09 21:56:20 -04005489 const char *fstr;
5490 Py_ssize_t fstrlen = -1; /* Silence a compiler warning. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04005491
5492 REQ(CHILD(n, i), STRING);
Eric V. Smith451d0e32016-09-09 21:56:20 -04005493 if (parsestr(c, CHILD(n, i), &this_bytesmode, &this_rawmode, &s,
5494 &fstr, &fstrlen) != 0)
Eric V. Smith235a6f02015-09-19 14:51:32 -04005495 goto error;
5496
5497 /* Check that we're not mixing bytes with unicode. */
5498 if (i != 0 && bytesmode != this_bytesmode) {
5499 ast_error(c, n, "cannot mix bytes and nonbytes literals");
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005500 /* s is NULL if the current string part is an f-string. */
5501 Py_XDECREF(s);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005502 goto error;
5503 }
5504 bytesmode = this_bytesmode;
5505
Eric V. Smith451d0e32016-09-09 21:56:20 -04005506 if (fstr != NULL) {
5507 int result;
5508 assert(s == NULL && !bytesmode);
5509 /* This is an f-string. Parse and concatenate it. */
5510 result = FstringParser_ConcatFstring(&state, &fstr, fstr+fstrlen,
5511 this_rawmode, 0, c, n);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005512 if (result < 0)
5513 goto error;
5514 } else {
Eric V. Smith9b88fdf2016-11-07 17:54:01 -05005515 /* A string or byte string. */
5516 assert(s != NULL && fstr == NULL);
5517
Eric V. Smith451d0e32016-09-09 21:56:20 -04005518 assert(bytesmode ? PyBytes_CheckExact(s) :
5519 PyUnicode_CheckExact(s));
5520
Eric V. Smith451d0e32016-09-09 21:56:20 -04005521 if (bytesmode) {
5522 /* For bytes, concat as we go. */
5523 if (i == 0) {
5524 /* First time, just remember this value. */
5525 bytes_str = s;
5526 } else {
5527 PyBytes_ConcatAndDel(&bytes_str, s);
5528 if (!bytes_str)
5529 goto error;
5530 }
5531 } else {
Eric V. Smith451d0e32016-09-09 21:56:20 -04005532 /* This is a regular string. Concatenate it. */
5533 if (FstringParser_ConcatAndDel(&state, s) < 0)
5534 goto error;
5535 }
Thomas Wouters89f507f2006-12-13 04:49:30 +00005536 }
Guido van Rossumd8faa362007-04-27 19:54:29 +00005537 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04005538 if (bytesmode) {
5539 /* Just return the bytes object and we're done. */
5540 if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0)
5541 goto error;
Ivan Levkivskyi9932a222019-01-22 11:18:22 +00005542 return Constant(bytes_str, LINENO(n), n->n_col_offset,
5543 n->n_end_lineno, n->n_end_col_offset, c->c_arena);
Eric V. Smith235a6f02015-09-19 14:51:32 -04005544 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005545
Eric V. Smith235a6f02015-09-19 14:51:32 -04005546 /* We're not a bytes string, bytes_str should never have been set. */
5547 assert(bytes_str == NULL);
5548
5549 return FstringParser_Finish(&state, c, n);
5550
5551error:
5552 Py_XDECREF(bytes_str);
5553 FstringParser_Dealloc(&state);
Guido van Rossumd8faa362007-04-27 19:54:29 +00005554 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005555}
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005556
5557PyObject *
5558_PyAST_GetDocString(asdl_seq *body)
5559{
5560 if (!asdl_seq_LEN(body)) {
5561 return NULL;
5562 }
5563 stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
5564 if (st->kind != Expr_kind) {
5565 return NULL;
5566 }
5567 expr_ty e = st->v.Expr.value;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03005568 if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) {
5569 return e->v.Constant.value;
5570 }
5571 return NULL;
5572}